Exemplo n.º 1
0
        /// <summary>
        ///     Sends an ATA command in 48-bit format
        /// </summary>
        /// <returns>0 if no error occurred, otherwise, errno</returns>
        /// <param name="ptId">Platform ID for executing the command</param>
        /// <param name="fd">File handle</param>
        /// <param name="buffer">Buffer for SCSI command response</param>
        /// <param name="timeout">Timeout in seconds</param>
        /// <param name="duration">Time it took to execute the command in milliseconds</param>
        /// <param name="sense"><c>True</c> if ATA returned non-OK status</param>
        /// <param name="registers">Registers to send to the device</param>
        /// <param name="errorRegisters">Registers returned by the device</param>
        /// <param name="protocol">ATA protocol to use</param>
        /// <param name="transferRegister">What register contains the transfer length</param>
        /// <param name="transferBlocks">Set to <c>true</c> if the transfer length is in block, otherwise it is in bytes</param>
        /// <exception cref="InvalidOperationException">If the specified platform is not supported</exception>
        internal static int SendAtaCommand(PlatformID ptId, object fd, AtaRegistersLba48 registers,
                                           out AtaErrorRegistersLba48 errorRegisters, AtaProtocol protocol,
                                           AtaTransferRegister transferRegister, ref byte[] buffer, uint timeout,
                                           bool transferBlocks, out double duration, out bool sense)
        {
            switch (ptId)
            {
            case PlatformID.Win32NT:
            {
                // No check for Windows version. A 48-bit ATA disk simply does not work on earlier systems
                return(Windows.Command.SendAtaCommand((SafeFileHandle)fd, registers, out errorRegisters, protocol,
                                                      ref buffer, timeout, out duration, out sense));
            }

            case PlatformID.Linux:
            {
                return(Linux.Command.SendAtaCommand((int)fd, registers, out errorRegisters, protocol,
                                                    transferRegister, ref buffer, timeout, transferBlocks,
                                                    out duration, out sense));
            }

            case PlatformID.FreeBSD:
            {
                return(FreeBSD.Command.SendAtaCommand((IntPtr)fd, registers, out errorRegisters, protocol,
                                                      ref buffer, timeout, out duration, out sense));
            }

            default: throw new InvalidOperationException($"Platform {ptId} not yet supported.");
            }
        }
Exemplo n.º 2
0
        /// <summary>
        ///     Sends a SCSI command
        /// </summary>
        /// <returns>0 if no error occurred, otherwise, errno</returns>
        /// <param name="fd">File handle</param>
        /// <param name="cdb">SCSI CDB</param>
        /// <param name="buffer">Buffer for SCSI command response</param>
        /// <param name="senseBuffer">Buffer with the SCSI sense</param>
        /// <param name="timeout">Timeout in seconds</param>
        /// <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>
        /// <exception cref="InvalidOperationException">If the specified platform is not supported</exception>
        internal static int SendScsiCommand(object fd, byte[] cdb, ref byte[] buffer, out byte[] senseBuffer,
                                            uint timeout, ScsiDirection direction, out double duration, out bool sense)
        {
            PlatformID ptId = DetectOS.GetRealPlatformID();

            return(SendScsiCommand(ptId, fd, cdb, ref buffer, out senseBuffer, timeout, direction, out duration,
                                   out sense));
        }
Exemplo n.º 3
0
        /// <summary>
        ///     Sends a MMC/SD command
        /// </summary>
        /// <returns>The result of the command.</returns>
        /// <param name="fd">File handle</param>
        /// <param name="command">MMC/SD opcode</param>
        /// <param name="buffer">Buffer for MMC/SD command response</param>
        /// <param name="timeout">Timeout in seconds</param>
        /// <param name="duration">Time it took to execute the command in milliseconds</param>
        /// <param name="sense"><c>True</c> if MMC/SD returned non-OK status</param>
        /// <param name="write"><c>True</c> if data is sent from host to card</param>
        /// <param name="isApplication"><c>True</c> if command should be preceded with CMD55</param>
        /// <param name="flags">Flags indicating kind and place of response</param>
        /// <param name="blocks">How many blocks to transfer</param>
        /// <param name="argument">Command argument</param>
        /// <param name="response">Response registers</param>
        /// <param name="blockSize">Size of block in bytes</param>
        /// <exception cref="InvalidOperationException">If the specified platform is not supported</exception>
        internal static int SendMmcCommand(object 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)
        {
            PlatformID ptId = DetectOS.GetRealPlatformID();

            return(SendMmcCommand(ptId, (int)fd, command, write, isApplication, flags, argument, blockSize, blocks,
                                  ref buffer, out response, out duration, out sense, timeout));
        }
Exemplo n.º 4
0
        /// <summary>
        ///     Sends an ATA command in 48-bit LBA format
        /// </summary>
        /// <returns>0 if no error occurred, otherwise, errno</returns>
        /// <param name="fd">File handle</param>
        /// <param name="buffer">Buffer for SCSI command response</param>
        /// <param name="timeout">Timeout in seconds</param>
        /// <param name="duration">Time it took to execute the command in milliseconds</param>
        /// <param name="sense"><c>True</c> if ATA returned non-OK status</param>
        /// <param name="registers">Registers to send to the device</param>
        /// <param name="errorRegisters">Registers returned by the device</param>
        /// <param name="protocol">ATA protocol to use</param>
        /// <param name="transferRegister">What register contains the transfer length</param>
        /// <param name="transferBlocks">Set to <c>true</c> if the transfer length is in block, otherwise it is in bytes</param>
        /// <exception cref="InvalidOperationException">If the specified platform is not supported</exception>
        internal static int SendAtaCommand(object fd, AtaRegistersLba48 registers,
                                           out AtaErrorRegistersLba48 errorRegisters, AtaProtocol protocol,
                                           AtaTransferRegister transferRegister, ref byte[] buffer, uint timeout,
                                           bool transferBlocks, out double duration, out bool sense)
        {
            PlatformID ptId = DetectOS.GetRealPlatformID();

            return(SendAtaCommand(ptId, fd, registers, out errorRegisters, protocol, transferRegister, ref buffer,
                                  timeout, transferBlocks, out duration, out sense));
        }
Exemplo n.º 5
0
        /// <summary>
        ///     Sends an ATA command in 28-bit LBA format
        /// </summary>
        /// <returns>0 if no error occurred, otherwise, errno</returns>
        /// <param name="ptId">Platform ID for executing the command</param>
        /// <param name="fd">File handle</param>
        /// <param name="buffer">Buffer for SCSI command response</param>
        /// <param name="timeout">Timeout in seconds</param>
        /// <param name="duration">Time it took to execute the command in milliseconds</param>
        /// <param name="sense"><c>True</c> if ATA returned non-OK status</param>
        /// <param name="registers">Registers to send to the device</param>
        /// <param name="errorRegisters">Registers returned by the device</param>
        /// <param name="protocol">ATA protocol to use</param>
        /// <param name="transferRegister">What register contains the transfer length</param>
        /// <param name="transferBlocks">Set to <c>true</c> if the transfer length is in block, otherwise it is in bytes</param>
        /// <exception cref="InvalidOperationException">If the specified platform is not supported</exception>
        internal static int SendAtaCommand(PlatformID ptId, object fd, AtaRegistersLba28 registers,
                                           out AtaErrorRegistersLba28 errorRegisters, AtaProtocol protocol,
                                           AtaTransferRegister transferRegister, ref byte[] buffer, uint timeout,
                                           bool transferBlocks, out double duration, out bool sense)
        {
            switch (ptId)
            {
            case PlatformID.Win32NT:
            {
                if (Environment.OSVersion.Version.Major == 5 && Environment.OSVersion.Version.Minor == 1 &&
                    (Environment.OSVersion.ServicePack == "Service Pack 1" ||
                     Environment.OSVersion.ServicePack == "") ||
                    Environment.OSVersion.Version.Major == 5 && Environment.OSVersion.Version.Minor == 0)
                {
                    return(Windows.Command.SendIdeCommand((SafeFileHandle)fd, registers, out errorRegisters,
                                                          protocol, ref buffer, timeout, out duration, out sense));
                }
                // Windows NT 4 or earlier, requires special ATA pass thru SCSI. But DiscImageChef cannot run there (or can it?)
                if (Environment.OSVersion.Version.Major <= 4)
                {
                    throw new InvalidOperationException("Windows NT 4.0 or earlier is not supported.");
                }

                return(Windows.Command.SendAtaCommand((SafeFileHandle)fd, registers, out errorRegisters, protocol,
                                                      ref buffer, timeout, out duration, out sense));
            }

            case PlatformID.Linux:
            {
                return(Linux.Command.SendAtaCommand((int)fd, registers, out errorRegisters, protocol,
                                                    transferRegister, ref buffer, timeout, transferBlocks,
                                                    out duration, out sense));
            }

            case PlatformID.FreeBSD:
            {
                return(FreeBSD.Command.SendAtaCommand((IntPtr)fd, registers, out errorRegisters, protocol,
                                                      ref buffer, timeout, out duration, out sense));
            }

            default: throw new InvalidOperationException($"Platform {ptId} not yet supported.");
            }
        }
Exemplo n.º 6
0
        /// <summary>
        ///     Sends a MMC/SD command
        /// </summary>
        /// <returns>The result of the command.</returns>
        /// <param name="ptId">Platform ID for executing the command</param>
        /// <param name="fd">File handle</param>
        /// <param name="command">MMC/SD opcode</param>
        /// <param name="buffer">Buffer for MMC/SD command response</param>
        /// <param name="timeout">Timeout in seconds</param>
        /// <param name="duration">Time it took to execute the command in milliseconds</param>
        /// <param name="sense"><c>True</c> if MMC/SD returned non-OK status</param>
        /// <param name="write"><c>True</c> if data is sent from host to card</param>
        /// <param name="isApplication"><c>True</c> if command should be preceded with CMD55</param>
        /// <param name="flags">Flags indicating kind and place of response</param>
        /// <param name="blocks">How many blocks to transfer</param>
        /// <param name="argument">Command argument</param>
        /// <param name="response">Response registers</param>
        /// <param name="blockSize">Size of block in bytes</param>
        /// <exception cref="InvalidOperationException">If the specified platform is not supported</exception>
        internal static int SendMmcCommand(PlatformID ptId, object 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)
        {
            switch (ptId)
            {
            case PlatformID.Win32NT:
            {
                return(Windows.Command.SendMmcCommand((SafeFileHandle)fd, command, write, isApplication, flags,
                                                      argument, blockSize, blocks, ref buffer, out response,
                                                      out duration, out sense, timeout));
            }

            case PlatformID.Linux:
            {
                return(Linux.Command.SendMmcCommand((int)fd, command, write, isApplication, flags, argument,
                                                    blockSize, blocks, ref buffer, out response, out duration,
                                                    out sense, timeout));
            }

            default: throw new InvalidOperationException($"Platform {ptId} not yet supported.");
            }
        }
Exemplo n.º 7
0
        public static void LoadSettings()
        {
            Current = new SetSettings();
            PlatformID ptId = DetectOS.GetRealPlatformID();

            FileStream   prefsFs = null;
            StreamReader prefsSr = null;

            try
            {
                switch (ptId)
                {
                case PlatformID.MacOSX:
                case PlatformID.iOS:
                {
                    string preferencesPath =
                        Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "Library",
                                     "Preferences");

                    string preferencesFilePath =
                        Path.Combine(preferencesPath, "com.claunia.museum.apprepodbmgr.plist");

                    if (!File.Exists(preferencesFilePath))
                    {
                        SetDefaultSettings();
                        SaveSettings();
                    }

                    prefsFs = new FileStream(preferencesFilePath, FileMode.Open);
                    var parsedPreferences = (NSDictionary)BinaryPropertyListParser.Parse(prefsFs);

                    if (parsedPreferences != null)
                    {
                        Current.TemporaryFolder = parsedPreferences.TryGetValue("TemporaryFolder", out NSObject obj)
                                                          ? ((NSString)obj).ToString() : Path.GetTempPath();

                        Current.DatabasePath = parsedPreferences.TryGetValue("DatabasePath", out obj)
                                                       ? ((NSString)obj).ToString()
                                                       : Path.
                                               Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
                                                       "apprepodbmgr.db");

                        Current.RepositoryPath = parsedPreferences.TryGetValue("RepositoryPath", out obj)
                                                         ? ((NSString)obj).ToString()
                                                         : Path.
                                                 Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
                                                         "apprepo");

                        Current.UnArchiverPath = parsedPreferences.TryGetValue("UnArchiverPath", out obj)
                                                         ? ((NSString)obj).ToString() : null;

                        if (parsedPreferences.TryGetValue("CompressionAlgorithm", out obj))
                        {
                            if (!Enum.TryParse(((NSString)obj).ToString(), true, out Current.CompressionAlgorithm))
                            {
                                Current.CompressionAlgorithm = AlgoEnum.GZip;
                            }
                        }
                        else
                        {
                            Current.CompressionAlgorithm = AlgoEnum.GZip;
                        }

                        Current.UseAntivirus = parsedPreferences.TryGetValue("UseAntivirus", out obj) &&
                                               ((NSNumber)obj).ToBool();

                        Current.UseClamd = parsedPreferences.TryGetValue("UseClamd", out obj) &&
                                           ((NSNumber)obj).ToBool();

                        Current.ClamdHost = parsedPreferences.TryGetValue("ClamdHost", out obj)
                                                    ? ((NSString)obj).ToString() : null;

                        if (parsedPreferences.TryGetValue("ClamdPort", out obj))
                        {
                            Current.ClamdPort = (ushort)((NSNumber)obj).ToLong();
                        }
                        else
                        {
                            Current.ClamdPort = 3310;
                        }

                        Current.ClamdIsLocal = parsedPreferences.TryGetValue("ClamdIsLocal", out obj) &&
                                               ((NSNumber)obj).ToBool();

                        Current.ClamdIsLocal = parsedPreferences.TryGetValue("UseVirusTotal", out obj) &&
                                               ((NSNumber)obj).ToBool();

                        Current.ClamdHost = parsedPreferences.TryGetValue("VirusTotalKey", out obj)
                                                    ? ((NSString)obj).ToString() : null;

                        prefsFs.Close();
                    }
                    else
                    {
                        prefsFs.Close();

                        SetDefaultSettings();
                        SaveSettings();
                    }
                }

                break;

                case PlatformID.Win32NT:
                case PlatformID.Win32S:
                case PlatformID.Win32Windows:
                case PlatformID.WinCE:
                case PlatformID.WindowsPhone:
                {
                    RegistryKey parentKey = Registry.CurrentUser.OpenSubKey("SOFTWARE")?.
                                            OpenSubKey("Canary Islands Computer Museum");

                    if (parentKey == null)
                    {
                        SetDefaultSettings();
                        SaveSettings();

                        return;
                    }

                    RegistryKey key = parentKey.OpenSubKey("AppRepoDBMgr");

                    if (key == null)
                    {
                        SetDefaultSettings();
                        SaveSettings();

                        return;
                    }

                    Current.TemporaryFolder = (string)key.GetValue("TemporaryFolder");
                    Current.DatabasePath    = (string)key.GetValue("DatabasePath");
                    Current.RepositoryPath  = (string)key.GetValue("RepositoryPath");
                    Current.UnArchiverPath  = (string)key.GetValue("UnArchiverPath");

                    if (!Enum.TryParse((string)key.GetValue("CompressionAlgorithm"), true,
                                       out Current.CompressionAlgorithm))
                    {
                        Current.CompressionAlgorithm = AlgoEnum.GZip;
                    }

                    Current.UseAntivirus  = bool.Parse((string)key.GetValue("UseAntivirus"));
                    Current.UseClamd      = bool.Parse((string)key.GetValue("UseClamd"));
                    Current.ClamdHost     = (string)key.GetValue("ClamdHost");
                    Current.ClamdPort     = ushort.Parse((string)key.GetValue("ClamdPort"));
                    Current.ClamdIsLocal  = bool.Parse((string)key.GetValue("ClamdIsLocal"));
                    Current.UseVirusTotal = bool.Parse((string)key.GetValue("UseVirusTotal"));
                    Current.VirusTotalKey = (string)key.GetValue("VirusTotalKey");
                }

                break;

                default:
                {
                    string configPath =
                        Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".config");

                    string settingsPath = Path.Combine(configPath, "AppRepoDBMgr.xml");

                    if (!Directory.Exists(configPath))
                    {
                        SetDefaultSettings();
                        SaveSettings();

                        return;
                    }

                    var xs = new XmlSerializer(Current.GetType());
                    prefsSr = new StreamReader(settingsPath);
                    Current = (SetSettings)xs.Deserialize(prefsSr);
                    prefsSr.Close();
                }

                break;
                }
            }
            catch
            {
                prefsFs?.Close();
                prefsSr?.Close();

                SetDefaultSettings();
                SaveSettings();
            }
        }
Exemplo n.º 8
0
        public static void SaveSettings()
        {
            try
            {
                PlatformID ptId = DetectOS.GetRealPlatformID();

                switch (ptId)
                {
                case PlatformID.MacOSX:
                case PlatformID.iOS:
                {
                    var root = new NSDictionary
                    {
                        {
                            "TemporaryFolder", Current.TemporaryFolder
                        },
                        {
                            "DatabasePath", Current.DatabasePath
                        },
                        {
                            "RepositoryPath", Current.RepositoryPath
                        },
                        {
                            "UnArchiverPath", Current.UnArchiverPath
                        },
                        {
                            "CompressionAlgorithm", Current.CompressionAlgorithm.ToString()
                        },
                        {
                            "UseAntivirus", Current.UseAntivirus
                        },
                        {
                            "UseClamd", Current.UseClamd
                        },
                        {
                            "ClamdHost", Current.ClamdHost
                        },
                        {
                            "ClamdPort", Current.ClamdPort
                        },
                        {
                            "ClamdIsLocal", Current.ClamdIsLocal
                        },
                        {
                            "UseVirusTotal", Current.UseVirusTotal
                        },
                        {
                            "VirusTotalKey", Current.VirusTotalKey
                        }
                    };

                    string preferencesPath =
                        Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "Library",
                                     "Preferences");

                    string preferencesFilePath =
                        Path.Combine(preferencesPath, "com.claunia.museum.apprepodbmgr.plist");

                    var fs = new FileStream(preferencesFilePath, FileMode.Create);
                    BinaryPropertyListWriter.Write(fs, root);
                    fs.Close();
                }

                break;

                case PlatformID.Win32NT:
                case PlatformID.Win32S:
                case PlatformID.Win32Windows:
                case PlatformID.WinCE:
                case PlatformID.WindowsPhone:
                {
                    RegistryKey parentKey = Registry.CurrentUser.OpenSubKey("SOFTWARE", true)?.
                                            CreateSubKey("Canary Islands Computer Museum");

                    RegistryKey key = parentKey?.CreateSubKey("AppRepoDBMgr");

                    if (key != null)
                    {
                        key.SetValue("TemporaryFolder", Current.TemporaryFolder);
                        key.SetValue("DatabasePath", Current.DatabasePath);
                        key.SetValue("RepositoryPath", Current.RepositoryPath);

                        if (Current.UnArchiverPath != null)
                        {
                            key.SetValue("UnArchiverPath", Current.UnArchiverPath);
                        }

                        key.SetValue("CompressionAlgorithm", Current.CompressionAlgorithm);
                        key.SetValue("UseAntivirus", Current.UseAntivirus);
                        key.SetValue("UseClamd", Current.UseClamd);
                        key.SetValue("ClamdHost", Current.ClamdHost == null ? "" : Current.ClamdHost);
                        key.SetValue("ClamdPort", Current.ClamdPort);
                        key.SetValue("ClamdIsLocal", Current.ClamdIsLocal);
                        key.SetValue("UseVirusTotal", Current.UseVirusTotal);
                        key.SetValue("VirusTotalKey", Current.VirusTotalKey == null ? "" : Current.VirusTotalKey);
                    }
                }

                break;

                default:
                {
                    string configPath =
                        Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".config");

                    string settingsPath = Path.Combine(configPath, "AppRepoDBMgr.xml");

                    if (!Directory.Exists(configPath))
                    {
                        Directory.CreateDirectory(configPath);
                    }

                    var fs = new FileStream(settingsPath, FileMode.Create);
                    var xs = new XmlSerializer(Current.GetType());
                    xs.Serialize(fs, Current);
                    fs.Close();
                }

                break;
                }
            }
            #pragma warning disable RECS0022     // A catch clause that catches System.Exception and has an empty body
            catch
                #pragma warning restore RECS0022 // A catch clause that catches System.Exception and has an empty body
            {
                if (Debugger.IsAttached)
                {
                    throw;
                }
            }
        }
Exemplo n.º 9
0
        /// <summary>
        ///     Sends a SCSI command
        /// </summary>
        /// <returns>0 if no error occurred, otherwise, errno</returns>
        /// <param name="ptId">Platform ID for executing the command</param>
        /// <param name="fd">File handle</param>
        /// <param name="cdb">SCSI CDB</param>
        /// <param name="buffer">Buffer for SCSI command response</param>
        /// <param name="senseBuffer">Buffer with the SCSI sense</param>
        /// <param name="timeout">Timeout in seconds</param>
        /// <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>
        /// <exception cref="InvalidOperationException">If the specified platform is not supported</exception>
        internal static int SendScsiCommand(PlatformID ptId, object fd, byte[] cdb, ref byte[] buffer,
                                            out byte[] senseBuffer, uint timeout, ScsiDirection direction,
                                            out double duration, out bool sense)
        {
            switch (ptId)
            {
            case PlatformID.Win32NT:
            {
                ScsiIoctlDirection dir;

                switch (direction)
                {
                case ScsiDirection.In:
                    dir = ScsiIoctlDirection.In;
                    break;

                case ScsiDirection.Out:
                    dir = ScsiIoctlDirection.Out;
                    break;

                default:
                    dir = ScsiIoctlDirection.Unspecified;
                    break;
                }

                return(Windows.Command.SendScsiCommand((SafeFileHandle)fd, cdb, ref buffer, out senseBuffer,
                                                       timeout, dir, out duration, out sense));
            }

            case PlatformID.Linux:
            {
                Linux.ScsiIoctlDirection dir;

                switch (direction)
                {
                case ScsiDirection.In:
                    dir = Linux.ScsiIoctlDirection.In;
                    break;

                case ScsiDirection.Out:
                    dir = Linux.ScsiIoctlDirection.Out;
                    break;

                case ScsiDirection.Bidirectional:
                    dir = Linux.ScsiIoctlDirection.Unspecified;
                    break;

                case ScsiDirection.None:
                    dir = Linux.ScsiIoctlDirection.None;
                    break;

                default:
                    dir = Linux.ScsiIoctlDirection.Unknown;
                    break;
                }

                return(Linux.Command.SendScsiCommand((int)fd, cdb, ref buffer, out senseBuffer, timeout, dir,
                                                     out duration, out sense));
            }

            case PlatformID.FreeBSD:
            {
                CcbFlags flags = 0;

                switch (direction)
                {
                case ScsiDirection.In:
                    flags = CcbFlags.CamDirIn;
                    break;

                case ScsiDirection.Out:
                    flags = CcbFlags.CamDirOut;
                    break;

                case ScsiDirection.Bidirectional:
                    flags = CcbFlags.CamDirBoth;
                    break;

                case ScsiDirection.None:
                    flags = CcbFlags.CamDirNone;
                    break;
                }

                return(IntPtr.Size == 8
                               ? FreeBSD.Command.SendScsiCommand64((IntPtr)fd, cdb, ref buffer, out senseBuffer,
                                                                   timeout, flags, out duration, out sense)
                               : FreeBSD.Command.SendScsiCommand((IntPtr)fd, cdb, ref buffer, out senseBuffer, timeout,
                                                                 flags, out duration, out sense));
            }

            default: throw new InvalidOperationException($"Platform {ptId} not yet supported.");
            }
        }
Exemplo n.º 10
0
        public static void SaveSettings()
        {
            try
            {
                PlatformID ptId = DetectOS.GetRealPlatformID();

                switch (ptId)
                {
                // In case of macOS or iOS settings will be saved in ~/Library/Preferences/com.claunia.discimagechef.plist
                case PlatformID.MacOSX:
                case PlatformID.iOS:
                {
                    NSDictionary root = new NSDictionary
                    {
                        { "SaveReportsGlobally", Current.SaveReportsGlobally },
                        { "ShareReports", Current.ShareReports }
                    };
                    if (Current.Stats != null)
                    {
                        NSDictionary stats = new NSDictionary
                        {
                            { "ShareStats", Current.Stats.ShareStats },
                            { "BenchmarkStats", Current.Stats.BenchmarkStats },
                            { "CommandStats", Current.Stats.CommandStats },
                            { "DeviceStats", Current.Stats.DeviceStats },
                            { "FilesystemStats", Current.Stats.FilesystemStats },
                            { "FilterStats", Current.Stats.FilterStats },
                            { "MediaImageStats", Current.Stats.MediaImageStats },
                            { "MediaScanStats", Current.Stats.MediaScanStats },
                            { "PartitionStats", Current.Stats.PartitionStats },
                            { "MediaStats", Current.Stats.MediaStats },
                            { "VerifyStats", Current.Stats.VerifyStats }
                        };
                        root.Add("Stats", stats);
                    }

                    string preferencesPath =
                        Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "Library",
                                     "Preferences");
                    string preferencesFilePath = Path.Combine(preferencesPath, "com.claunia.discimagechef.plist");

                    FileStream fs = new FileStream(preferencesFilePath, FileMode.Create);
                    BinaryPropertyListWriter.Write(fs, root);
                    fs.Close();
                }
                break;

                // In case of Windows settings will be saved in the registry: HKLM/SOFTWARE/Claunia.com/DiscImageChef
                case PlatformID.Win32NT:
                case PlatformID.Win32S:
                case PlatformID.Win32Windows:
                case PlatformID.WinCE:
                case PlatformID.WindowsPhone:
                {
                    RegistryKey parentKey =
                        Registry.CurrentUser.OpenSubKey("SOFTWARE", true)?.CreateSubKey("Claunia.com");
                    RegistryKey key = parentKey?.CreateSubKey("DiscImageChef");

                    if (key != null)
                    {
                        key.SetValue("SaveReportsGlobally", Current.SaveReportsGlobally);
                        key.SetValue("ShareReports", Current.ShareReports);

                        if (Current.Stats != null)
                        {
                            key.SetValue("Statistics", true);
                            key.SetValue("ShareStats", Current.Stats.ShareStats);
                            key.SetValue("BenchmarkStats", Current.Stats.BenchmarkStats);
                            key.SetValue("CommandStats", Current.Stats.CommandStats);
                            key.SetValue("DeviceStats", Current.Stats.DeviceStats);
                            key.SetValue("FilesystemStats", Current.Stats.FilesystemStats);
                            key.SetValue("FilterStats", Current.Stats.FilterStats);
                            key.SetValue("MediaImageStats", Current.Stats.MediaImageStats);
                            key.SetValue("MediaScanStats", Current.Stats.MediaScanStats);
                            key.SetValue("PartitionStats", Current.Stats.PartitionStats);
                            key.SetValue("MediaStats", Current.Stats.MediaStats);
                            key.SetValue("VerifyStats", Current.Stats.VerifyStats);
                        }
                        else
                        {
                            key.SetValue("Statistics", true);
                            key.DeleteValue("ShareStats", false);
                            key.DeleteValue("BenchmarkStats", false);
                            key.DeleteValue("CommandStats", false);
                            key.DeleteValue("DeviceStats", false);
                            key.DeleteValue("FilesystemStats", false);
                            key.DeleteValue("MediaImageStats", false);
                            key.DeleteValue("MediaScanStats", false);
                            key.DeleteValue("PartitionStats", false);
                            key.DeleteValue("MediaStats", false);
                            key.DeleteValue("VerifyStats", false);
                        }
                    }
                }
                break;

                // Otherwise, settings will be saved in ~/.config/DiscImageChef.xml
                default:
                {
                    string configPath =
                        Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".config");
                    string settingsPath = Path.Combine(configPath, "DiscImageChef.xml");

                    if (!Directory.Exists(configPath))
                    {
                        Directory.CreateDirectory(configPath);
                    }

                    FileStream    fs = new FileStream(settingsPath, FileMode.Create);
                    XmlSerializer xs = new XmlSerializer(Current.GetType());
                    xs.Serialize(fs, Current);
                    fs.Close();
                }
                break;
                }
            }
#pragma warning disable RECS0022 // A catch clause that catches System.Exception and has an empty body
            catch
            {
                // ignored
            }
#pragma warning restore RECS0022 // A catch clause that catches System.Exception and has an empty body
        }
Exemplo n.º 11
0
        /// <summary>
        ///     Loads settings
        /// </summary>
        public static void LoadSettings()
        {
            Current = new DicSettings();
            PlatformID ptId = DetectOS.GetRealPlatformID();

            try
            {
                switch (ptId)
                {
                // In case of macOS or iOS statistics and reports will be saved in ~/Library/Application Support/Claunia.com/DiscImageChef
                case PlatformID.MacOSX:
                case PlatformID.iOS:
                {
                    string appSupportPath =
                        Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "Library",
                                     "Application Support", "Claunia.com");
                    if (!Directory.Exists(appSupportPath))
                    {
                        Directory.CreateDirectory(appSupportPath);
                    }

                    string dicPath = Path.Combine(appSupportPath, "DiscImageChef");
                    if (!Directory.Exists(dicPath))
                    {
                        Directory.CreateDirectory(dicPath);
                    }

                    ReportsPath = Path.Combine(dicPath, "Reports");
                    if (!Directory.Exists(ReportsPath))
                    {
                        Directory.CreateDirectory(ReportsPath);
                    }

                    StatsPath = Path.Combine(dicPath, "Statistics");
                    if (!Directory.Exists(StatsPath))
                    {
                        Directory.CreateDirectory(StatsPath);
                    }
                }
                break;

                // In case of Windows statistics and reports will be saved in %APPDATA%\Claunia.com\DiscImageChef
                case PlatformID.Win32NT:
                case PlatformID.Win32S:
                case PlatformID.Win32Windows:
                case PlatformID.WinCE:
                case PlatformID.WindowsPhone:
                {
                    string appSupportPath =
                        Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
                                     "Claunia.com");
                    if (!Directory.Exists(appSupportPath))
                    {
                        Directory.CreateDirectory(appSupportPath);
                    }

                    string dicPath = Path.Combine(appSupportPath, "DiscImageChef");
                    if (!Directory.Exists(dicPath))
                    {
                        Directory.CreateDirectory(dicPath);
                    }

                    ReportsPath = Path.Combine(dicPath, "Reports");
                    if (!Directory.Exists(ReportsPath))
                    {
                        Directory.CreateDirectory(ReportsPath);
                    }

                    StatsPath = Path.Combine(dicPath, "Statistics");
                    if (!Directory.Exists(StatsPath))
                    {
                        Directory.CreateDirectory(StatsPath);
                    }
                }
                break;

                // Otherwise, statistics and reports will be saved in ~/.claunia.com/DiscImageChef
                default:
                {
                    string appSupportPath =
                        Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile),
                                     ".claunia.com");
                    if (!Directory.Exists(appSupportPath))
                    {
                        Directory.CreateDirectory(appSupportPath);
                    }

                    string dicPath = Path.Combine(appSupportPath, "DiscImageChef");
                    if (!Directory.Exists(dicPath))
                    {
                        Directory.CreateDirectory(dicPath);
                    }

                    ReportsPath = Path.Combine(dicPath, "Reports");
                    if (!Directory.Exists(ReportsPath))
                    {
                        Directory.CreateDirectory(ReportsPath);
                    }

                    StatsPath = Path.Combine(dicPath, "Statistics");
                    if (!Directory.Exists(StatsPath))
                    {
                        Directory.CreateDirectory(StatsPath);
                    }
                }
                break;
                }
            }
            catch { ReportsPath = null; }

            FileStream   prefsFs = null;
            StreamReader prefsSr = null;

            try
            {
                switch (ptId)
                {
                // In case of macOS or iOS settings will be saved in ~/Library/Preferences/com.claunia.discimagechef.plist
                case PlatformID.MacOSX:
                case PlatformID.iOS:
                {
                    string preferencesPath =
                        Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "Library",
                                     "Preferences");
                    string preferencesFilePath = Path.Combine(preferencesPath, "com.claunia.discimagechef.plist");

                    if (!File.Exists(preferencesFilePath))
                    {
                        SetDefaultSettings();
                        SaveSettings();
                    }

                    prefsFs = new FileStream(preferencesFilePath, FileMode.Open, FileAccess.Read);

                    NSDictionary parsedPreferences = (NSDictionary)BinaryPropertyListParser.Parse(prefsFs);
                    if (parsedPreferences != null)
                    {
                        Current.SaveReportsGlobally =
                            parsedPreferences.TryGetValue("SaveReportsGlobally", out NSObject obj) &&
                            ((NSNumber)obj).ToBool();

                        Current.ShareReports = parsedPreferences.TryGetValue("ShareReports", out obj) &&
                                               ((NSNumber)obj).ToBool();

                        if (parsedPreferences.TryGetValue("Stats", out obj))
                        {
                            NSDictionary stats = (NSDictionary)obj;

                            if (stats != null)
                            {
                                Current.Stats = new StatsSettings
                                {
                                    ShareStats =
                                        stats.TryGetValue("ShareStats", out NSObject obj2) &&
                                        ((NSNumber)obj2).ToBool(),
                                    BenchmarkStats =
                                        stats.TryGetValue("BenchmarkStats", out obj2) && ((NSNumber)obj2).ToBool(),
                                    CommandStats =
                                        stats.TryGetValue("CommandStats", out obj2) && ((NSNumber)obj2).ToBool(),
                                    DeviceStats =
                                        stats.TryGetValue("DeviceStats", out obj2) && ((NSNumber)obj2).ToBool(),
                                    FilesystemStats =
                                        stats.TryGetValue("FilesystemStats", out obj2) && ((NSNumber)obj2).ToBool(),
                                    FilterStats =
                                        stats.TryGetValue("FilterStats", out obj2) && ((NSNumber)obj2).ToBool(),
                                    MediaImageStats =
                                        stats.TryGetValue("MediaImageStats", out obj2) && ((NSNumber)obj2).ToBool(),
                                    MediaScanStats =
                                        stats.TryGetValue("MediaScanStats", out obj2) && ((NSNumber)obj2).ToBool(),
                                    PartitionStats =
                                        stats.TryGetValue("PartitionStats", out obj2) && ((NSNumber)obj2).ToBool(),
                                    MediaStats =
                                        stats.TryGetValue("MediaStats", out obj2) && ((NSNumber)obj2).ToBool(),
                                    VerifyStats =
                                        stats.TryGetValue("VerifyStats", out obj2) && ((NSNumber)obj2).ToBool()
                                }
                            }
                            ;
                        }
                        else
                        {
                            Current.Stats = null;
                        }

                        prefsFs.Close();
                    }
                    else
                    {
                        prefsFs.Close();

                        SetDefaultSettings();
                        SaveSettings();
                    }
                }
                break;

                // In case of Windows settings will be saved in the registry: HKLM/SOFTWARE/Claunia.com/DiscImageChef
                case PlatformID.Win32NT:
                case PlatformID.Win32S:
                case PlatformID.Win32Windows:
                case PlatformID.WinCE:
                case PlatformID.WindowsPhone:
                {
                    RegistryKey parentKey = Registry.CurrentUser.OpenSubKey("SOFTWARE")?.OpenSubKey("Claunia.com");
                    if (parentKey == null)
                    {
                        SetDefaultSettings();
                        SaveSettings();
                        return;
                    }

                    RegistryKey key = parentKey.OpenSubKey("DiscImageChef");
                    if (key == null)
                    {
                        SetDefaultSettings();
                        SaveSettings();
                        return;
                    }

                    Current.SaveReportsGlobally = Convert.ToBoolean(key.GetValue("SaveReportsGlobally"));
                    Current.ShareReports        = Convert.ToBoolean(key.GetValue("ShareReports"));

                    bool stats = Convert.ToBoolean(key.GetValue("Statistics"));
                    if (stats)
                    {
                        Current.Stats = new StatsSettings
                        {
                            ShareStats      = Convert.ToBoolean(key.GetValue("ShareStats")),
                            BenchmarkStats  = Convert.ToBoolean(key.GetValue("BenchmarkStats")),
                            CommandStats    = Convert.ToBoolean(key.GetValue("CommandStats")),
                            DeviceStats     = Convert.ToBoolean(key.GetValue("DeviceStats")),
                            FilesystemStats = Convert.ToBoolean(key.GetValue("FilesystemStats")),
                            FilterStats     = Convert.ToBoolean(key.GetValue("FilterStats")),
                            MediaImageStats = Convert.ToBoolean(key.GetValue("MediaImageStats")),
                            MediaScanStats  = Convert.ToBoolean(key.GetValue("MediaScanStats")),
                            PartitionStats  = Convert.ToBoolean(key.GetValue("PartitionStats")),
                            MediaStats      = Convert.ToBoolean(key.GetValue("MediaStats")),
                            VerifyStats     = Convert.ToBoolean(key.GetValue("VerifyStats"))
                        }
                    }
                    ;
                }

                break;

                // Otherwise, settings will be saved in ~/.config/DiscImageChef.xml
                default:
                {
                    string configPath =
                        Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".config");
                    string settingsPath = Path.Combine(configPath, "DiscImageChef.xml");

                    if (!Directory.Exists(configPath))
                    {
                        SetDefaultSettings();
                        SaveSettings();
                        return;
                    }

                    XmlSerializer xs = new XmlSerializer(Current.GetType());
                    prefsSr = new StreamReader(settingsPath);
                    Current = (DicSettings)xs.Deserialize(prefsSr);
                }

                break;
                }
            }
            catch
            {
                prefsFs?.Close();
                prefsSr?.Close();
                SetDefaultSettings();
                SaveSettings();
            }
        }
Exemplo n.º 12
0
        /// <summary>
        ///     Initializes the dump log
        /// </summary>
        /// <param name="outputFile">Output log file</param>
        /// <param name="dev">Device</param>
        public DumpLog(string outputFile, Device dev)
        {
            if (string.IsNullOrEmpty(outputFile))
            {
                return;
            }

            logSw = new StreamWriter(outputFile, true);

            logSw.WriteLine("Start logging at {0}", DateTime.Now);

            PlatformID platId  = DetectOS.GetRealPlatformID();
            string     platVer = DetectOS.GetVersion();
            AssemblyInformationalVersionAttribute assemblyVersion =
                Attribute.GetCustomAttribute(typeof(DumpLog).Assembly, typeof(AssemblyInformationalVersionAttribute)) as
                AssemblyInformationalVersionAttribute;

            logSw.WriteLine("################# System information #################");
            logSw.WriteLine("{0} {1} ({2}-bit)", DetectOS.GetPlatformName(platId, platVer), platVer,
                            Environment.Is64BitOperatingSystem ? 64 : 32);
            if (DetectOS.IsMono)
            {
                logSw.WriteLine("Mono {0}", Version.GetMonoVersion());
            }
            else
            {
                logSw.WriteLine(".NET Framework {0}", Environment.Version);
            }

            logSw.WriteLine();

            logSw.WriteLine("################# Program information ################");
            logSw.WriteLine("DiscImageChef {0}", assemblyVersion?.InformationalVersion);
            logSw.WriteLine("Running in {0}-bit", Environment.Is64BitProcess ? 64 : 32);
            #if DEBUG
            logSw.WriteLine("DEBUG version");
            #endif
            logSw.WriteLine("Command line: {0}", Environment.CommandLine);
            logSw.WriteLine();

            logSw.WriteLine("################# Device information #################");
            logSw.WriteLine("Manufacturer: {0}", dev.Manufacturer);
            logSw.WriteLine("Model: {0}", dev.Model);
            logSw.WriteLine("Firmware revision: {0}", dev.Revision);
            logSw.WriteLine("Serial number: {0}", dev.Serial);
            logSw.WriteLine("Removable device: {0}", dev.IsRemovable);
            logSw.WriteLine("Device type: {0}", dev.Type);
            logSw.WriteLine("CompactFlash device: {0}", dev.IsCompactFlash);
            logSw.WriteLine("PCMCIA device: {0}", dev.IsPcmcia);
            logSw.WriteLine("USB device: {0}", dev.IsUsb);
            if (dev.IsUsb)
            {
                logSw.WriteLine("USB manufacturer: {0}", dev.UsbManufacturerString);
                logSw.WriteLine("USB product: {0}", dev.UsbProductString);
                logSw.WriteLine("USB serial: {0}", dev.UsbSerialString);
                logSw.WriteLine("USB vendor ID: {0:X4}h", dev.UsbVendorId);
                logSw.WriteLine("USB product ID: {0:X4}h", dev.UsbProductId);
            }

            logSw.WriteLine("FireWire device: {0}", dev.IsFireWire);
            if (dev.IsFireWire)
            {
                logSw.WriteLine("FireWire vendor: {0}", dev.FireWireVendorName);
                logSw.WriteLine("FireWire model: {0}", dev.FireWireModelName);
                logSw.WriteLine("FireWire GUID: 0x{0:X16}", dev.FireWireGuid);
                logSw.WriteLine("FireWire vendor ID: 0x{0:X8}", dev.FireWireVendor);
                logSw.WriteLine("FireWire product ID: 0x{0:X8}", dev.FireWireModel);
            }

            logSw.WriteLine();
            logSw.WriteLine("######################################################");

            logSw.WriteLine();
            logSw.WriteLine("################ Dumping progress log ################");
            logSw.Flush();
        }