REFACTOR: Reformat code.

This commit is contained in:
2017-12-19 20:33:03 +00:00
parent 77edc7c91c
commit e6f6ace80b
704 changed files with 82627 additions and 83641 deletions

View File

@@ -51,14 +51,14 @@ namespace DiscImageChef.Devices.Linux
/// <param name="direction">SCSI command transfer direction</param>
/// <param name="duration">Time it took to execute the command in milliseconds</param>
/// <param name="sense"><c>True</c> if SCSI error returned non-OK status and <paramref name="senseBuffer"/> contains SCSI sense</param>
internal static int SendScsiCommand(int fd, byte[] cdb, ref byte[] buffer, out byte[] senseBuffer, uint timeout, ScsiIoctlDirection direction, out double duration, out bool sense)
internal static int SendScsiCommand(int fd, byte[] cdb, ref byte[] buffer, out byte[] senseBuffer, uint timeout,
ScsiIoctlDirection direction, out double duration, out bool sense)
{
senseBuffer = null;
duration = 0;
sense = false;
if(buffer == null)
return -1;
if(buffer == null) return -1;
sg_io_hdr_t io_hdr = new sg_io_hdr_t();
@@ -82,8 +82,7 @@ namespace DiscImageChef.Devices.Linux
int error = Extern.ioctlSg(fd, LinuxIoctl.SG_IO, ref io_hdr);
DateTime end = DateTime.UtcNow;
if(error < 0)
error = Marshal.GetLastWin32Error();
if(error < 0) error = Marshal.GetLastWin32Error();
Marshal.Copy(io_hdr.dxferp, buffer, 0, buffer.Length);
Marshal.Copy(io_hdr.cmdp, cdb, 0, cdb.Length);
@@ -91,10 +90,8 @@ namespace DiscImageChef.Devices.Linux
sense |= (io_hdr.info & SgInfo.OkMask) != SgInfo.Ok;
if(io_hdr.duration > 0)
duration = io_hdr.duration;
else
duration = (end - start).TotalMilliseconds;
if(io_hdr.duration > 0) duration = io_hdr.duration;
else duration = (end - start).TotalMilliseconds;
Marshal.FreeHGlobal(io_hdr.dxferp);
Marshal.FreeHGlobal(io_hdr.cmdp);
@@ -112,36 +109,30 @@ namespace DiscImageChef.Devices.Linux
case AtaProtocol.HardReset:
case AtaProtocol.NonData:
case AtaProtocol.SoftReset:
case AtaProtocol.ReturnResponse:
return ScsiIoctlDirection.None;
case AtaProtocol.ReturnResponse: return ScsiIoctlDirection.None;
case AtaProtocol.PioIn:
case AtaProtocol.UDmaIn:
return ScsiIoctlDirection.In;
case AtaProtocol.UDmaIn: return ScsiIoctlDirection.In;
case AtaProtocol.PioOut:
case AtaProtocol.UDmaOut:
return ScsiIoctlDirection.Out;
default:
return ScsiIoctlDirection.Unspecified;
case AtaProtocol.UDmaOut: return ScsiIoctlDirection.Out;
default: return ScsiIoctlDirection.Unspecified;
}
}
internal static int SendAtaCommand(int fd, AtaRegistersCHS registers,
out AtaErrorRegistersCHS errorRegisters, AtaProtocol protocol,
AtaTransferRegister transferRegister, ref byte[] buffer, uint timeout,
bool transferBlocks, out double duration, out bool sense)
internal static int SendAtaCommand(int fd, AtaRegistersCHS registers, out AtaErrorRegistersCHS errorRegisters,
AtaProtocol protocol, AtaTransferRegister transferRegister,
ref byte[] buffer, uint timeout, bool transferBlocks, out double duration,
out bool sense)
{
duration = 0;
sense = false;
errorRegisters = new AtaErrorRegistersCHS();
if(buffer == null)
return -1;
if(buffer == null) return -1;
byte[] cdb = new byte[16];
cdb[0] = (byte)ScsiCommands.AtaPassThrough16;
cdb[1] = (byte)(((byte)protocol << 1) & 0x1E);
if(transferRegister != AtaTransferRegister.NoTransfer &&
protocol != AtaProtocol.NonData)
if(transferRegister != AtaTransferRegister.NoTransfer && protocol != AtaProtocol.NonData)
{
switch(protocol)
{
@@ -154,8 +145,7 @@ namespace DiscImageChef.Devices.Linux
break;
}
if(transferBlocks)
cdb[2] |= 0x04;
if(transferBlocks) cdb[2] |= 0x04;
cdb[2] |= (byte)((int)transferRegister & 0x03);
}
@@ -171,10 +161,10 @@ namespace DiscImageChef.Devices.Linux
cdb[14] = registers.command;
byte[] senseBuffer;
int error = SendScsiCommand(fd, cdb, ref buffer, out senseBuffer, timeout, AtaProtocolToScsiDirection(protocol), out duration, out sense);
int error = SendScsiCommand(fd, cdb, ref buffer, out senseBuffer, timeout,
AtaProtocolToScsiDirection(protocol), out duration, out sense);
if(senseBuffer.Length < 22 || (senseBuffer[8] != 0x09 && senseBuffer[9] != 0x0C))
return error;
if(senseBuffer.Length < 22 || (senseBuffer[8] != 0x09 && senseBuffer[9] != 0x0C)) return error;
errorRegisters.error = senseBuffer[11];
@@ -191,22 +181,20 @@ namespace DiscImageChef.Devices.Linux
}
internal static int SendAtaCommand(int fd, AtaRegistersLBA28 registers,
out AtaErrorRegistersLBA28 errorRegisters, AtaProtocol protocol,
AtaTransferRegister transferRegister, ref byte[] buffer, uint timeout,
bool transferBlocks, out double duration, out bool sense)
out AtaErrorRegistersLBA28 errorRegisters, AtaProtocol protocol,
AtaTransferRegister transferRegister, ref byte[] buffer, uint timeout,
bool transferBlocks, out double duration, out bool sense)
{
duration = 0;
sense = false;
errorRegisters = new AtaErrorRegistersLBA28();
if(buffer == null)
return -1;
if(buffer == null) return -1;
byte[] cdb = new byte[16];
cdb[0] = (byte)ScsiCommands.AtaPassThrough16;
cdb[1] = (byte)(((byte)protocol << 1) & 0x1E);
if(transferRegister != AtaTransferRegister.NoTransfer &&
protocol != AtaProtocol.NonData)
if(transferRegister != AtaTransferRegister.NoTransfer && protocol != AtaProtocol.NonData)
{
switch(protocol)
{
@@ -219,8 +207,7 @@ namespace DiscImageChef.Devices.Linux
break;
}
if(transferBlocks)
cdb[2] |= 0x04;
if(transferBlocks) cdb[2] |= 0x04;
cdb[2] |= (byte)((int)transferRegister & 0x03);
}
@@ -236,10 +223,10 @@ namespace DiscImageChef.Devices.Linux
cdb[14] = registers.command;
byte[] senseBuffer;
int error = SendScsiCommand(fd, cdb, ref buffer, out senseBuffer, timeout, AtaProtocolToScsiDirection(protocol), out duration, out sense);
int error = SendScsiCommand(fd, cdb, ref buffer, out senseBuffer, timeout,
AtaProtocolToScsiDirection(protocol), out duration, out sense);
if(senseBuffer.Length < 22 || (senseBuffer[8] != 0x09 && senseBuffer[9] != 0x0C))
return error;
if(senseBuffer.Length < 22 || (senseBuffer[8] != 0x09 && senseBuffer[9] != 0x0C)) return error;
errorRegisters.error = senseBuffer[11];
@@ -256,23 +243,21 @@ namespace DiscImageChef.Devices.Linux
}
internal static int SendAtaCommand(int fd, AtaRegistersLBA48 registers,
out AtaErrorRegistersLBA48 errorRegisters, AtaProtocol protocol,
AtaTransferRegister transferRegister, ref byte[] buffer, uint timeout,
bool transferBlocks, out double duration, out bool sense)
out AtaErrorRegistersLBA48 errorRegisters, AtaProtocol protocol,
AtaTransferRegister transferRegister, ref byte[] buffer, uint timeout,
bool transferBlocks, out double duration, out bool sense)
{
duration = 0;
sense = false;
errorRegisters = new AtaErrorRegistersLBA48();
if(buffer == null)
return -1;
if(buffer == null) return -1;
byte[] cdb = new byte[16];
cdb[0] = (byte)ScsiCommands.AtaPassThrough16;
cdb[1] = (byte)(((byte)protocol << 1) & 0x1E);
cdb[1] |= 0x01;
if(transferRegister != AtaTransferRegister.NoTransfer &&
protocol != AtaProtocol.NonData)
if(transferRegister != AtaTransferRegister.NoTransfer && protocol != AtaProtocol.NonData)
{
switch(protocol)
{
@@ -285,8 +270,7 @@ namespace DiscImageChef.Devices.Linux
break;
}
if(transferBlocks)
cdb[2] |= 0x04;
if(transferBlocks) cdb[2] |= 0x04;
cdb[2] |= (byte)((int)transferRegister & 0x03);
}
@@ -307,10 +291,10 @@ namespace DiscImageChef.Devices.Linux
cdb[14] = registers.command;
byte[] senseBuffer;
int error = SendScsiCommand(fd, cdb, ref buffer, out senseBuffer, timeout, AtaProtocolToScsiDirection(protocol), out duration, out sense);
int error = SendScsiCommand(fd, cdb, ref buffer, out senseBuffer, timeout,
AtaProtocolToScsiDirection(protocol), out duration, out sense);
if(senseBuffer.Length < 22 || (senseBuffer[8] != 0x09 && senseBuffer[9] != 0x0C))
return error;
if(senseBuffer.Length < 22 || (senseBuffer[8] != 0x09 && senseBuffer[9] != 0x0C)) return error;
errorRegisters.error = senseBuffer[11];
@@ -345,14 +329,15 @@ namespace DiscImageChef.Devices.Linux
/// <param name="argument">Command argument</param>
/// <param name="response">Response registers</param>
/// <param name="blockSize">Size of block in bytes</param>
internal static int SendMmcCommand(int fd, MmcCommands command, bool write, bool isApplication, MmcFlags flags, uint argument, uint blockSize, uint blocks, ref byte[] buffer, out uint[] response, out double duration, out bool sense, uint timeout = 0)
internal static int SendMmcCommand(int fd, MmcCommands command, bool write, bool isApplication, MmcFlags flags,
uint argument, uint blockSize, uint blocks, ref byte[] buffer,
out uint[] response, out double duration, out bool sense, uint timeout = 0)
{
response = null;
duration = 0;
sense = false;
if(buffer == null)
return -1;
if(buffer == null) return -1;
mmc_ioc_cmd io_cmd = new mmc_ioc_cmd();
@@ -380,8 +365,7 @@ namespace DiscImageChef.Devices.Linux
sense |= error < 0;
if(error < 0)
error = Marshal.GetLastWin32Error();
if(error < 0) error = Marshal.GetLastWin32Error();
Marshal.Copy(bufPtr, buffer, 0, buffer.Length);
@@ -401,16 +385,14 @@ namespace DiscImageChef.Devices.Linux
if(Interop.DetectOS.Is64Bit())
{
long result64 = Extern.readlink64(path, buf, 4096);
if(result64 <= 0)
return null;
if(result64 <= 0) return null;
resultSize = (int)result64;
}
else
{
int result = Extern.readlink(path, buf, 4096);
if(result <= 0)
return null;
if(result <= 0) return null;
resultSize = result;
}
@@ -421,5 +403,4 @@ namespace DiscImageChef.Devices.Linux
return System.Text.Encoding.ASCII.GetString(resultString);
}
}
}
}

View File

@@ -182,5 +182,4 @@ namespace DiscImageChef.Devices.Linux
/// </summary>
MixedIo = 0x04
}
}
}

View File

@@ -39,10 +39,7 @@ namespace DiscImageChef.Devices.Linux
static class Extern
{
[DllImport("libc", CharSet = CharSet.Ansi, SetLastError = true)]
internal static extern int open(
string pathname,
[MarshalAs(UnmanagedType.U4)]
FileFlags flags);
internal static extern int open(string pathname, [MarshalAs(UnmanagedType.U4)] FileFlags flags);
[DllImport("libc")]
internal static extern int close(int fd);
@@ -66,10 +63,10 @@ namespace DiscImageChef.Devices.Linux
internal static extern IntPtr udev_new();
[DllImport("libudev", CharSet = CharSet.Ansi, SetLastError = true)]
internal static extern IntPtr udev_device_new_from_subsystem_sysname(IntPtr udev, string subsystem, string sysname);
internal static extern IntPtr udev_device_new_from_subsystem_sysname(
IntPtr udev, string subsystem, string sysname);
[DllImport("libudev", CharSet = CharSet.Ansi, SetLastError = true)]
internal static extern string udev_device_get_property_value(IntPtr udev_device, string key);
}
}
}

View File

@@ -55,10 +55,7 @@ namespace DiscImageChef.Devices.Linux
udev = Extern.udev_new();
hasUdev = udev != IntPtr.Zero;
}
catch
{
hasUdev = false;
}
catch { hasUdev = false; }
for(int i = 0; i < sysdevs.Length; i++)
{
@@ -67,11 +64,11 @@ namespace DiscImageChef.Devices.Linux
if(hasUdev)
{
udevDev = Extern.udev_device_new_from_subsystem_sysname(udev, "block", Path.GetFileName(sysdevs[i]));
udevDev = Extern.udev_device_new_from_subsystem_sysname(udev, "block",
Path.GetFileName(sysdevs[i]));
devices[i].vendor = Extern.udev_device_get_property_value(udevDev, "ID_VENDOR");
devices[i].model = Extern.udev_device_get_property_value(udevDev, "ID_MODEL");
if(!string.IsNullOrEmpty(devices[i].model))
devices[i].model = devices[i].model.Replace('_', ' ');
if(!string.IsNullOrEmpty(devices[i].model)) devices[i].model = devices[i].model.Replace('_', ' ');
devices[i].serial = Extern.udev_device_get_property_value(udevDev, "ID_SCSI_SERIAL");
if(string.IsNullOrEmpty(devices[i].serial))
devices[i].serial = Extern.udev_device_get_property_value(udevDev, "ID_SERIAL_SHORT");
@@ -86,7 +83,8 @@ namespace DiscImageChef.Devices.Linux
else if(devices[i].path.StartsWith("/dev/loop", StringComparison.CurrentCulture))
devices[i].vendor = "Linux";
if(File.Exists(Path.Combine(sysdevs[i], "device/model")) && (string.IsNullOrEmpty(devices[i].model) || devices[i].bus == "ata"))
if(File.Exists(Path.Combine(sysdevs[i], "device/model")) &&
(string.IsNullOrEmpty(devices[i].model) || devices[i].bus == "ata"))
{
sr = new StreamReader(Path.Combine(sysdevs[i], "device/model"), Encoding.ASCII);
devices[i].model = sr.ReadLine().Trim();
@@ -123,8 +121,7 @@ namespace DiscImageChef.Devices.Linux
else if(devices[i].path.StartsWith("/dev/mmc", StringComparison.CurrentCulture))
devices[i].bus = "MMC/SD";
}
else
devices[i].bus = devices[i].bus.ToUpper();
else devices[i].bus = devices[i].bus.ToUpper();
switch(devices[i].bus)
{
@@ -143,4 +140,4 @@ namespace DiscImageChef.Devices.Linux
return devices;
}
}
}
}

View File

@@ -42,28 +42,28 @@ namespace DiscImageChef.Devices.Linux
/// <summary>
/// Always 'S' for SG v3
/// </summary>
public int interface_id; /* [i] 'S' (required) */
public ScsiIoctlDirection dxfer_direction; /* [i] */
public byte cmd_len; /* [i] */
public byte mx_sb_len; /* [i] */
public int interface_id; /* [i] 'S' (required) */
public ScsiIoctlDirection dxfer_direction; /* [i] */
public byte cmd_len; /* [i] */
public byte mx_sb_len; /* [i] */
public ushort iovec_count; /* [i] */
public uint dxfer_len; /* [i] */
public IntPtr dxferp; /* [i], [*io] */
public IntPtr cmdp; /* [i], [*i] */
public IntPtr sbp; /* [i], [*o] */
public uint timeout; /* [i] unit: millisecs */
public uint flags; /* [i] */
public int pack_id; /* [i->o] */
public IntPtr usr_ptr; /* [i->o] */
public byte status; /* [o] */
public byte masked_status;/* [o] */
public byte msg_status; /* [o] */
public byte sb_len_wr; /* [o] */
public uint dxfer_len; /* [i] */
public IntPtr dxferp; /* [i], [*io] */
public IntPtr cmdp; /* [i], [*i] */
public IntPtr sbp; /* [i], [*o] */
public uint timeout; /* [i] unit: millisecs */
public uint flags; /* [i] */
public int pack_id; /* [i->o] */
public IntPtr usr_ptr; /* [i->o] */
public byte status; /* [o] */
public byte masked_status; /* [o] */
public byte msg_status; /* [o] */
public byte sb_len_wr; /* [o] */
public ushort host_status; /* [o] */
public ushort driver_status;/* [o] */
public int resid; /* [o] */
public uint duration; /* [o] */
public SgInfo info; /* [o] */
public ushort driver_status; /* [o] */
public int resid; /* [o] */
public uint duration; /* [o] */
public SgInfo info; /* [o] */
}
[StructLayout(LayoutKind.Sequential)]
@@ -82,8 +82,7 @@ namespace DiscImageChef.Devices.Linux
/// <summary>
/// CMD response
/// </summary>
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
public uint[] response;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)] public uint[] response;
public MmcFlags flags;
public uint blksz;
public uint blocks;
@@ -122,5 +121,4 @@ namespace DiscImageChef.Devices.Linux
/// </summary>
public ulong data_ptr;
}
}
}