On Linux try to open in read/write and exclusive mode, if not retry in readonly.

This commit is contained in:
2018-04-02 23:09:18 +01:00
parent 2a9f5ff828
commit 94d51f5411

View File

@@ -72,7 +72,8 @@ namespace DiscImageChef.Devices
case PlatformID.Win32NT: case PlatformID.Win32NT:
{ {
FileHandle = Extern.CreateFile(devicePath, FileAccess.GenericRead | FileAccess.GenericWrite, FileHandle = Extern.CreateFile(devicePath, FileAccess.GenericRead | FileAccess.GenericWrite,
FileShare.Read | FileShare.Write, IntPtr.Zero, FileMode.OpenExisting, FileShare.Read | FileShare.Write, IntPtr.Zero,
FileMode.OpenExisting,
FileAttributes.Normal, IntPtr.Zero); FileAttributes.Normal, IntPtr.Zero);
if(((SafeFileHandle)FileHandle).IsInvalid) if(((SafeFileHandle)FileHandle).IsInvalid)
@@ -85,13 +86,27 @@ namespace DiscImageChef.Devices
} }
case PlatformID.Linux: case PlatformID.Linux:
{ {
FileHandle = Linux.Extern.open(devicePath, FileFlags.Readonly | FileFlags.NonBlocking); FileHandle =
Linux.Extern.open(devicePath,
FileFlags.ReadWrite | FileFlags.NonBlocking | FileFlags.CreateNew);
if((int)FileHandle < 0)
{
LastError = Marshal.GetLastWin32Error();
if(LastError == 13) // EACCES
{
FileHandle = Linux.Extern.open(devicePath, FileFlags.Readonly | FileFlags.NonBlocking);
if((int)FileHandle < 0) if((int)FileHandle < 0)
{ {
Error = true; Error = true;
LastError = Marshal.GetLastWin32Error(); LastError = Marshal.GetLastWin32Error();
} }
}
else Error = true;
LastError = Marshal.GetLastWin32Error();
}
break; break;
} }
@@ -224,6 +239,7 @@ namespace DiscImageChef.Devices
if(ataid.HasValue) scsiSense = ScsiInquiry(out inqBuf, out _); if(ataid.HasValue) scsiSense = ScsiInquiry(out inqBuf, out _);
} }
else Manufacturer = "ATA"; else Manufacturer = "ATA";
break; break;
} }
} }
@@ -311,6 +327,7 @@ namespace DiscImageChef.Devices
} }
} }
} }
break; break;
case PlatformID.Linux: case PlatformID.Linux:
if(devicePath.StartsWith("/dev/sd", StringComparison.Ordinal) || if(devicePath.StartsWith("/dev/sd", StringComparison.Ordinal) ||
@@ -323,25 +340,33 @@ namespace DiscImageChef.Devices
string devPath = devicePath.Substring(5); string devPath = devicePath.Substring(5);
if(File.Exists("/sys/block/" + devPath + "/device/csd")) if(File.Exists("/sys/block/" + devPath + "/device/csd"))
{ {
int len = ConvertFromHexAscii("/sys/block/" + devPath + "/device/csd", out cachedCsd); int len =
ConvertFromHexAscii("/sys/block/" + devPath + "/device/csd", out cachedCsd);
if(len == 0) cachedCsd = null; if(len == 0) cachedCsd = null;
} }
if(File.Exists("/sys/block/" + devPath + "/device/cid")) if(File.Exists("/sys/block/" + devPath + "/device/cid"))
{ {
int len = ConvertFromHexAscii("/sys/block/" + devPath + "/device/cid", out cachedCid); int len =
ConvertFromHexAscii("/sys/block/" + devPath + "/device/cid", out cachedCid);
if(len == 0) cachedCid = null; if(len == 0) cachedCid = null;
} }
if(File.Exists("/sys/block/" + devPath + "/device/scr")) if(File.Exists("/sys/block/" + devPath + "/device/scr"))
{ {
int len = ConvertFromHexAscii("/sys/block/" + devPath + "/device/scr", out cachedScr); int len =
ConvertFromHexAscii("/sys/block/" + devPath + "/device/scr", out cachedScr);
if(len == 0) cachedScr = null; if(len == 0) cachedScr = null;
} }
if(File.Exists("/sys/block/" + devPath + "/device/ocr")) if(File.Exists("/sys/block/" + devPath + "/device/ocr"))
{ {
int len = ConvertFromHexAscii("/sys/block/" + devPath + "/device/ocr", out cachedOcr); int len =
ConvertFromHexAscii("/sys/block/" + devPath + "/device/ocr", out cachedOcr);
if(len == 0) cachedOcr = null; if(len == 0) cachedOcr = null;
} }
} }
break; break;
default: default:
scsiSense = ScsiInquiry(out inqBuf, out _); scsiSense = ScsiInquiry(out inqBuf, out _);
@@ -466,6 +491,7 @@ namespace DiscImageChef.Devices
UsbSerialString = UsbSerialString =
usbDevice.SerialNumber; // This is incorrect filled by Windows with SCSI/ATA serial number usbDevice.SerialNumber; // This is incorrect filled by Windows with SCSI/ATA serial number
} }
break; break;
default: default:
IsUsb = false; IsUsb = false;
@@ -663,7 +689,9 @@ namespace DiscImageChef.Devices
if(string.IsNullOrEmpty(Manufacturer)) Manufacturer = UsbManufacturerString; if(string.IsNullOrEmpty(Manufacturer)) Manufacturer = UsbManufacturerString;
if(string.IsNullOrEmpty(Model)) Model = UsbProductString; if(string.IsNullOrEmpty(Model)) Model = UsbProductString;
if(string.IsNullOrEmpty(Serial)) Serial = UsbSerialString; if(string.IsNullOrEmpty(Serial)) Serial = UsbSerialString;
else foreach(char c in Serial.Where(char.IsControl)) Serial = UsbSerialString; else
foreach(char c in Serial.Where(char.IsControl))
Serial = UsbSerialString;
} }
if(!IsFireWire) return; if(!IsFireWire) return;
@@ -671,7 +699,9 @@ namespace DiscImageChef.Devices
if(string.IsNullOrEmpty(Manufacturer)) Manufacturer = FireWireVendorName; if(string.IsNullOrEmpty(Manufacturer)) Manufacturer = FireWireVendorName;
if(string.IsNullOrEmpty(Model)) Model = FireWireModelName; if(string.IsNullOrEmpty(Model)) Model = FireWireModelName;
if(string.IsNullOrEmpty(Serial)) Serial = $"{firewireGuid:X16}"; if(string.IsNullOrEmpty(Serial)) Serial = $"{firewireGuid:X16}";
else foreach(char c in Serial.Where(char.IsControl)) Serial = $"{firewireGuid:X16}"; else
foreach(char c in Serial.Where(char.IsControl))
Serial = $"{firewireGuid:X16}";
} }
static int ConvertFromHexAscii(string file, out byte[] outBuf) static int ConvertFromHexAscii(string file, out byte[] outBuf)