Use error number for device instead of exception.

This commit is contained in:
2022-03-26 20:18:01 +00:00
parent 89a2c52911
commit 755da6cd2c
15 changed files with 219 additions and 222 deletions

View File

@@ -56,7 +56,6 @@
<ConsolePause>false</ConsolePause>
</PropertyGroup>
<ItemGroup>
<Compile Include="Device\DeviceException.cs" />
<Compile Include="Device\ScsiCommands\MediaTek.cs" />
<Compile Include="Device\ScsiCommands\MiniDisc.cs" />
<Compile Include="Device\ScsiCommands\Optical.cs" />

View File

@@ -50,10 +50,11 @@ public partial class Device
{
/// <summary>Opens the device for sending direct commands</summary>
/// <param name="devicePath">Device path</param>
public static Device Create(string devicePath)
public static Device Create(string devicePath, out ErrorNumber errno)
{
Device dev = null;
Uri aaruUri;
errno = ErrorNumber.NoError;
try
{
@@ -61,18 +62,20 @@ public partial class Device
}
catch(Exception)
{
return null;
aaruUri = null;
}
if(aaruUri.Scheme is "dic" or "aaru")
dev = Remote.Device.Create(aaruUri);
if(aaruUri?.Scheme is "dic" or "aaru")
dev = Remote.Device.Create(aaruUri, out errno);
else if(OperatingSystem.IsLinux())
dev = Linux.Device.Create(devicePath);
dev = Linux.Device.Create(devicePath, out errno);
else if(OperatingSystem.IsWindows())
dev = Windows.Device.Create(devicePath);
dev = Windows.Device.Create(devicePath, out errno);
else
errno = ErrorNumber.NotSupported;
if(dev is null)
throw new DeviceException("Platform not supported.");
return null;
if(dev.Type == DeviceType.SCSI ||
dev.Type == DeviceType.ATAPI)

View File

@@ -1,47 +0,0 @@
// /***************************************************************************
// Aaru Data Preservation Suite
// ----------------------------------------------------------------------------
//
// Filename : DeviceException.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// Component : Devices.
//
// --[ Description ] ----------------------------------------------------------
//
// Exception to be returned by the device constructor.
//
// --[ License ] --------------------------------------------------------------
//
// This library is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation; either version 2.1 of the
// License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, see <http://www.gnu.org/licenses/>.
//
// ----------------------------------------------------------------------------
// Copyright © 2011-2022 Natalia Portillo
// ****************************************************************************/
namespace Aaru.Devices;
using System;
/// <inheritdoc />
/// <summary>Exception to be returned by the device constructor</summary>
public sealed class DeviceException : Exception
{
internal DeviceException(string message) : base(message) {}
internal DeviceException(int lastError) => LastError = lastError;
/// <summary>Last error sent by the operating systen</summary>
public int LastError { get; }
}

View File

@@ -54,8 +54,10 @@ partial class Device : Devices.Device
Device() {}
internal new static Device Create(string devicePath)
internal new static Device Create(string devicePath, out ErrorNumber errno)
{
errno = ErrorNumber.NoError;
var dev = new Device
{
PlatformId = DetectOS.GetRealPlatformID(),
@@ -86,7 +88,11 @@ partial class Device : Devices.Device
}
if(dev.Error)
throw new DeviceException(dev.LastError);
{
errno = (ErrorNumber)dev.LastError;
return null;
}
// Seems ioctl(2) does not allow the atomicity needed
if(dev.PlatformId == PlatformID.Linux)
@@ -96,7 +102,11 @@ partial class Device : Devices.Device
dev.ScsiType = PeripheralDeviceTypes.UnknownDevice;
if(dev.Error)
throw new DeviceException(dev.LastError);
{
errno = (ErrorNumber)dev.LastError;
return null;
}
string devPath;

View File

@@ -75,8 +75,10 @@ public sealed partial class Device : Devices.Device
/// <summary>Opens the device for sending direct commands</summary>
/// <param name="aaruUri">AaruRemote URI</param>
/// <returns>Device</returns>
internal static Device Create(Uri aaruUri)
internal static Device Create(Uri aaruUri, out ErrorNumber errno)
{
errno = ErrorNumber.NoError;
var dev = new Device
{
PlatformId = DetectOS.GetRealPlatformID(),
@@ -100,11 +102,16 @@ public sealed partial class Device : Devices.Device
dev._remote = new Remote(aaruUri);
dev.Error = !dev._remote.Open(devicePath, out int errno);
dev.LastError = errno;
dev.Error = !dev._remote.Open(devicePath, out int remoteErrno);
dev.LastError = remoteErrno;
// TODO: Convert error codes
if(dev.Error)
throw new DeviceException(dev.LastError);
{
errno = (ErrorNumber)remoteErrno;
return null;
}
if(dev._remote.ServerOperatingSystem == "Linux")
_readMultipleBlockCannotSetBlockCount = true;
@@ -113,7 +120,12 @@ public sealed partial class Device : Devices.Device
dev.ScsiType = PeripheralDeviceTypes.UnknownDevice;
if(dev.Error)
throw new DeviceException(dev.LastError);
if(dev.Error)
{
errno = (ErrorNumber)dev.LastError;
return null;
}
dev.Type = dev._remote.GetDeviceType();

View File

@@ -51,8 +51,10 @@ partial class Device : Devices.Device
Device() {}
internal new static Device Create(string devicePath)
internal new static Device Create(string devicePath, out ErrorNumber errno)
{
errno = ErrorNumber.NoError;
var dev = new Device
{
PlatformId = DetectOS.GetRealPlatformID(),
@@ -71,13 +73,21 @@ partial class Device : Devices.Device
}
if(dev.Error)
throw new DeviceException(dev.LastError);
{
errno = (ErrorNumber)dev.LastError;
return null;
}
dev.Type = DeviceType.Unknown;
dev.ScsiType = PeripheralDeviceTypes.UnknownDevice;
if(dev.Error)
throw new DeviceException(dev.LastError);
{
errno = (ErrorNumber)dev.LastError;
return null;
}
// Windows is answering SCSI INQUIRY for all device types so it needs to be detected first
var query = new StoragePropertyQuery();