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();

View File

@@ -35,7 +35,6 @@ namespace Aaru.Gui.ViewModels.Windows;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reactive;
@@ -246,74 +245,72 @@ public sealed class MainWindowViewModel : ViewModelBase
case DeviceModel deviceModel:
{
if(deviceModel.ViewModel is null)
try
{
var dev = Device.Create(deviceModel.Path);
{
var dev = Device.Create(deviceModel.Path, out ErrorNumber devErrno);
if(dev is Devices.Remote.Device remoteDev)
switch(dev)
{
case null:
ContentPanel = $"Error {devErrno} opening device";
return;
case Devices.Remote.Device remoteDev:
Statistics.AddRemote(remoteDev.RemoteApplication, remoteDev.RemoteVersion,
remoteDev.RemoteOperatingSystem,
remoteDev.RemoteOperatingSystemVersion,
remoteDev.RemoteArchitecture);
if(dev.Error)
{
ContentPanel = $"Error {dev.LastError} opening device";
return;
}
var devInfo = new DeviceInfo(dev);
deviceModel.ViewModel = new DeviceInfoViewModel(devInfo, _view);
if(!dev.IsRemovable)
deviceModel.Media.Add(new MediaModel
{
NonRemovable = true,
Name = "Non-removable device commands not yet implemented"
});
else
{
// TODO: Removable non-SCSI?
var scsiInfo = new ScsiInfo(dev);
if(!scsiInfo.MediaInserted)
deviceModel.Media.Add(new MediaModel
{
NoMediaInserted = true,
Icon = _ejectIcon,
Name = "No media inserted"
});
else
{
var mediaResource =
new Uri($"avares://Aaru.Gui/Assets/Logos/Media/{scsiInfo.MediaType}.png");
deviceModel.Media.Add(new MediaModel
{
DevicePath = deviceModel.Path,
Icon = _assets.Exists(mediaResource) ? new Bitmap(_assets.Open(mediaResource))
: null,
Name = $"{scsiInfo.MediaType}",
ViewModel = new MediaInfoViewModel(scsiInfo, deviceModel.Path, _view)
});
}
}
dev.Close();
break;
}
catch(SystemException ex)
{
if(Debugger.IsAttached)
throw;
ContentPanel = ex.Message;
AaruConsole.ErrorWriteLine(ex.Message);
if(dev.Error)
{
ContentPanel = $"Error {dev.LastError} opening device";
return;
}
var devInfo = new DeviceInfo(dev);
deviceModel.ViewModel = new DeviceInfoViewModel(devInfo, _view);
if(!dev.IsRemovable)
deviceModel.Media.Add(new MediaModel
{
NonRemovable = true,
Name = "Non-removable device commands not yet implemented"
});
else
{
// TODO: Removable non-SCSI?
var scsiInfo = new ScsiInfo(dev);
if(!scsiInfo.MediaInserted)
deviceModel.Media.Add(new MediaModel
{
NoMediaInserted = true,
Icon = _ejectIcon,
Name = "No media inserted"
});
else
{
var mediaResource =
new Uri($"avares://Aaru.Gui/Assets/Logos/Media/{scsiInfo.MediaType}.png");
deviceModel.Media.Add(new MediaModel
{
DevicePath = deviceModel.Path,
Icon = _assets.Exists(mediaResource) ? new Bitmap(_assets.Open(mediaResource))
: null,
Name = $"{scsiInfo.MediaType}",
ViewModel = new MediaInfoViewModel(scsiInfo, deviceModel.Path, _view)
});
}
}
dev.Close();
}
ContentPanel = new Views.Panels.DeviceInfo
{
DataContext = deviceModel.ViewModel
@@ -786,10 +783,10 @@ public sealed class MainWindowViewModel : ViewModelBase
Path = device.Path
};
try
{
var dev = Device.Create(device.Path);
var dev = Device.Create(device.Path, out _);
if(dev != null)
{
if(dev is Devices.Remote.Device remoteDev)
Statistics.AddRemote(remoteDev.RemoteApplication, remoteDev.RemoteVersion,
remoteDev.RemoteOperatingSystem, remoteDev.RemoteOperatingSystemVersion,
@@ -839,10 +836,6 @@ public sealed class MainWindowViewModel : ViewModelBase
dev.Close();
}
catch
{
// ignored
}
_devicesRoot.Devices.Add(deviceModel);
}

View File

@@ -724,25 +724,25 @@ public sealed class MediaDumpViewModel : ViewModelBase
UpdateStatus("Opening device...");
try
{
_dev = Device.Create(_devicePath);
_dev = Device.Create(_devicePath, out ErrorNumber devErrno);
if(_dev is Devices.Remote.Device remoteDev)
switch(_dev)
{
case null:
StoppingErrorMessage($"Error {devErrno} opening device.");
return;
case Devices.Remote.Device remoteDev:
Statistics.AddRemote(remoteDev.RemoteApplication, remoteDev.RemoteVersion,
remoteDev.RemoteOperatingSystem, remoteDev.RemoteOperatingSystemVersion,
remoteDev.RemoteArchitecture);
if(_dev.Error)
{
StoppingErrorMessage($"Error {_dev.LastError} opening device.");
return;
}
break;
}
catch(Exception exception)
if(_dev.Error)
{
StoppingErrorMessage($"Exception {exception.Message} opening device.");
StoppingErrorMessage($"Error {_dev.LastError} opening device.");
return;
}

View File

@@ -36,6 +36,7 @@ using System.Collections.ObjectModel;
using System.Reactive;
using System.Threading;
using System.Threading.Tasks;
using Aaru.CommonTypes.Enums;
using Aaru.Core;
using Aaru.Core.Devices.Scanning;
using Aaru.Devices;
@@ -328,11 +329,28 @@ public sealed class MediaScanViewModel : ViewModelBase
char.IsLetter(_devicePath[0]))
_devicePath = "\\\\.\\" + char.ToUpper(_devicePath[0]) + ':';
var dev = Device.Create(_devicePath);
var dev = Device.Create(_devicePath, out ErrorNumber devErrno);
if(dev is Devices.Remote.Device remoteDev)
Statistics.AddRemote(remoteDev.RemoteApplication, remoteDev.RemoteVersion, remoteDev.RemoteOperatingSystem,
remoteDev.RemoteOperatingSystemVersion, remoteDev.RemoteArchitecture);
switch(dev)
{
case null:
await MessageBoxManager.
GetMessageBoxStandardWindow("Error", $"Error {devErrno} opening device.", ButtonEnum.Ok,
Icon.Error).ShowDialog(_view);
StopVisible = false;
StartVisible = true;
CloseVisible = true;
ProgressVisible = false;
return;
case Devices.Remote.Device remoteDev:
Statistics.AddRemote(remoteDev.RemoteApplication, remoteDev.RemoteVersion,
remoteDev.RemoteOperatingSystem, remoteDev.RemoteOperatingSystemVersion,
remoteDev.RemoteArchitecture);
break;
}
if(dev.Error)
{

View File

@@ -39,7 +39,7 @@ static partial class MainClass
AaruConsole.WriteLine("Going to open {0}. Press any key to continue...", devPath);
Console.ReadKey();
var dev = Aaru.Devices.Device.Create(devPath);
var dev = Aaru.Devices.Device.Create(devPath, out _);
while(true)
{

View File

@@ -50,7 +50,6 @@ using Aaru.Database;
using Aaru.Database.Models;
using Aaru.Decoders.SCSI;
using Aaru.Decoders.SCSI.MMC;
using Aaru.Devices;
using Aaru.Helpers;
using Aaru.Settings;
using Newtonsoft.Json;
@@ -124,27 +123,25 @@ sealed class DeviceReportCommand : Command
char.IsLetter(devicePath[0]))
devicePath = "\\\\.\\" + char.ToUpper(devicePath[0]) + ':';
Device dev;
var dev = Device.Create(devicePath, out ErrorNumber devErrno);
try
switch(dev)
{
dev = Device.Create(devicePath);
case null:
AaruConsole.ErrorWriteLine($"Could not open device, error {devErrno}.");
if(dev is Devices.Remote.Device remoteDev)
return (int)devErrno;
case Devices.Remote.Device remoteDev:
Statistics.AddRemote(remoteDev.RemoteApplication, remoteDev.RemoteVersion,
remoteDev.RemoteOperatingSystem, remoteDev.RemoteOperatingSystemVersion,
remoteDev.RemoteArchitecture);
if(dev.Error)
{
AaruConsole.ErrorWriteLine(Error.Print(dev.LastError));
return (int)ErrorNumber.CannotOpenDevice;
}
break;
}
catch(DeviceException e)
if(dev.Error)
{
AaruConsole.ErrorWriteLine(e.Message);
AaruConsole.ErrorWriteLine(Error.Print(dev.LastError));
return (int)ErrorNumber.CannotOpenDevice;
}

View File

@@ -128,27 +128,25 @@ sealed class DeviceInfoCommand : Command
char.IsLetter(devicePath[0]))
devicePath = "\\\\.\\" + char.ToUpper(devicePath[0]) + ':';
Device dev;
var dev = Device.Create(devicePath, out ErrorNumber devErrno);
try
switch(dev)
{
dev = Device.Create(devicePath);
case null:
AaruConsole.ErrorWriteLine($"Could not open device, error {devErrno}.");
if(dev is Devices.Remote.Device remoteDev)
return (int)devErrno;
case Devices.Remote.Device remoteDev:
Statistics.AddRemote(remoteDev.RemoteApplication, remoteDev.RemoteVersion,
remoteDev.RemoteOperatingSystem, remoteDev.RemoteOperatingSystemVersion,
remoteDev.RemoteArchitecture);
if(dev.Error)
{
AaruConsole.ErrorWriteLine(Error.Print(dev.LastError));
return (int)ErrorNumber.CannotOpenDevice;
}
break;
}
catch(DeviceException e)
if(dev.Error)
{
AaruConsole.ErrorWriteLine(e.Message);
AaruConsole.ErrorWriteLine(Error.Print(dev.LastError));
return (int)ErrorNumber.CannotOpenDevice;
}

View File

@@ -536,34 +536,37 @@ sealed class DumpMediaCommand : Command
char.IsLetter(devicePath[0]))
devicePath = "\\\\.\\" + char.ToUpper(devicePath[0]) + ':';
Device dev = null;
Device dev = null;
ErrorNumber devErrno = ErrorNumber.NoError;
try
Spectre.ProgressSingleSpinner(ctx =>
{
Spectre.ProgressSingleSpinner(ctx =>
{
ctx.AddTask("Opening device...").IsIndeterminate();
dev = Device.Create(devicePath);
});
ctx.AddTask("Opening device...").IsIndeterminate();
dev = Device.Create(devicePath, out devErrno);
});
if(dev is Devices.Remote.Device remoteDev)
Statistics.AddRemote(remoteDev.RemoteApplication, remoteDev.RemoteVersion,
remoteDev.RemoteOperatingSystem, remoteDev.RemoteOperatingSystemVersion,
remoteDev.RemoteArchitecture);
if(dev.Error)
switch(dev)
{
case null:
{
AaruConsole.ErrorWriteLine(Error.Print(dev.LastError));
AaruConsole.ErrorWriteLine($"Could not open device, error {devErrno}.");
if(isResponse)
continue;
return (int)ErrorNumber.CannotOpenDevice;
return (int)devErrno;
}
case Devices.Remote.Device remoteDev:
Statistics.AddRemote(remoteDev.RemoteApplication, remoteDev.RemoteVersion,
remoteDev.RemoteOperatingSystem, remoteDev.RemoteOperatingSystemVersion,
remoteDev.RemoteArchitecture);
break;
}
catch(DeviceException e)
if(dev.Error)
{
AaruConsole.ErrorWriteLine(e.Message ?? Error.Print(e.LastError));
AaruConsole.ErrorWriteLine(Error.Print(dev.LastError));
if(isResponse)
continue;

View File

@@ -52,7 +52,6 @@ using Aaru.Decoders.DVD;
using Aaru.Decoders.SCSI.MMC;
using Aaru.Decoders.SCSI.SSC;
using Aaru.Decoders.Xbox;
using Aaru.Devices;
using Aaru.Settings;
using Spectre.Console;
using BCA = Aaru.Decoders.Bluray.BCA;
@@ -129,31 +128,32 @@ sealed class MediaInfoCommand : Command
char.IsLetter(devicePath[0]))
devicePath = "\\\\.\\" + char.ToUpper(devicePath[0]) + ':';
Device dev = null;
Device dev = null;
ErrorNumber devErrno = ErrorNumber.NoError;
try
Spectre.ProgressSingleSpinner(ctx =>
{
Spectre.ProgressSingleSpinner(ctx =>
{
ctx.AddTask("Opening device...").IsIndeterminate();
dev = Device.Create(devicePath);
});
ctx.AddTask("Opening device...").IsIndeterminate();
dev = Device.Create(devicePath, out devErrno);
});
if(dev is Devices.Remote.Device remoteDev)
switch(dev)
{
case null:
AaruConsole.ErrorWriteLine($"Could not open device, error {devErrno}.");
return (int)devErrno;
case Devices.Remote.Device remoteDev:
Statistics.AddRemote(remoteDev.RemoteApplication, remoteDev.RemoteVersion,
remoteDev.RemoteOperatingSystem, remoteDev.RemoteOperatingSystemVersion,
remoteDev.RemoteArchitecture);
if(dev.Error)
{
AaruConsole.ErrorWriteLine(Error.Print(dev.LastError));
return (int)ErrorNumber.CannotOpenDevice;
}
break;
}
catch(DeviceException e)
if(dev.Error)
{
AaruConsole.ErrorWriteLine(e.Message);
AaruConsole.ErrorWriteLine(Error.Print(dev.LastError));
return (int)ErrorNumber.CannotOpenDevice;
}

View File

@@ -130,31 +130,32 @@ sealed class MediaScanCommand : Command
char.IsLetter(devicePath[0]))
devicePath = "\\\\.\\" + char.ToUpper(devicePath[0]) + ':';
Device dev = null;
Device dev = null;
ErrorNumber devErrno = ErrorNumber.NoError;
try
Spectre.ProgressSingleSpinner(ctx =>
{
Spectre.ProgressSingleSpinner(ctx =>
{
ctx.AddTask("Opening device...").IsIndeterminate();
dev = Device.Create(devicePath);
});
ctx.AddTask("Opening device...").IsIndeterminate();
dev = Device.Create(devicePath, out devErrno);
});
if(dev is Devices.Remote.Device remoteDev)
switch(dev)
{
case null:
AaruConsole.ErrorWriteLine($"Could not open device, error {devErrno}.");
return (int)devErrno;
case Devices.Remote.Device remoteDev:
Statistics.AddRemote(remoteDev.RemoteApplication, remoteDev.RemoteVersion,
remoteDev.RemoteOperatingSystem, remoteDev.RemoteOperatingSystemVersion,
remoteDev.RemoteArchitecture);
if(dev.Error)
{
AaruConsole.ErrorWriteLine(Error.Print(dev.LastError));
return (int)ErrorNumber.CannotOpenDevice;
}
break;
}
catch(DeviceException e)
if(dev.Error)
{
AaruConsole.ErrorWriteLine(e.Message);
AaruConsole.ErrorWriteLine(Error.Print(dev.LastError));
return (int)ErrorNumber.CannotOpenDevice;
}