コード例 #1
0
        internal AbstractStorage(StorageInfo storageInfo, string name, string firmwareRevision, string id, int index, ISettings settings)
            : base(name, new Identifier(id, index.ToString(CultureInfo.InvariantCulture)), settings)
        {
            _storageInfo     = storageInfo;
            FirmwareRevision = firmwareRevision;
            Index            = index;

            string[] logicalDrives = WindowsStorage.GetLogicalDrives(index);
            var      driveInfoList = new List <DriveInfo>(logicalDrives.Length);

            foreach (string logicalDrive in logicalDrives)
            {
                try
                {
                    var di = new DriveInfo(logicalDrive);
                    if (di.TotalSize > 0)
                    {
                        driveInfoList.Add(new DriveInfo(logicalDrive));
                    }
                }
                catch (ArgumentException) { }
                catch (IOException) { }
                catch (UnauthorizedAccessException) { }
            }

            DriveInfos = driveInfoList.ToArray();
        }
コード例 #2
0
        public static AbstractStorage CreateInstance(string deviceId, uint driveNumber, ulong diskSize, int scsiPort, ISettings settings)
        {
            StorageInfo info = WindowsStorage.GetStorageInfo(deviceId, driveNumber);

            info.DiskSize = diskSize;
            info.DeviceId = deviceId;
            info.Scsi     = $@"\\.\SCSI{scsiPort}:";

            if (info.Removable || info.BusType == Kernel32.STORAGE_BUS_TYPE.BusTypeVirtual || info.BusType == Kernel32.STORAGE_BUS_TYPE.BusTypeFileBackedVirtual)
            {
                return(null);
            }

            //fallback, when it is not possible to read out with the nvme implementation,
            //try it with the sata smart implementation
            if (info.BusType == Kernel32.STORAGE_BUS_TYPE.BusTypeNvme)
            {
                var x = NVMeGeneric.CreateInstance(info, settings);
                if (x != null)
                {
                    return(x);
                }
            }

            if (info.BusType == Kernel32.STORAGE_BUS_TYPE.BusTypeAta ||
                info.BusType == Kernel32.STORAGE_BUS_TYPE.BusTypeSata ||
                info.BusType == Kernel32.STORAGE_BUS_TYPE.BusTypeNvme)
            {
                return(AtaStorage.CreateInstance(info, settings));
            }
            return(StorageGeneric.CreateInstance(info, settings));
        }
コード例 #3
0
        internal static AbstractStorage CreateInstance(StorageInfo info, ISettings settings)
        {
            ISmart smart            = new WindowsSmart(info.Index);
            string name             = null;
            string firmwareRevision = null;

            Kernel32.SMART_ATTRIBUTE[] values = { };

            if (smart.IsValid)
            {
                bool nameValid    = smart.ReadNameAndFirmwareRevision(out name, out firmwareRevision);
                bool smartEnabled = smart.EnableSmart();

                if (smartEnabled)
                {
                    values = smart.ReadSmartData();
                }

                if (!nameValid)
                {
                    name             = null;
                    firmwareRevision = null;
                }
            }
            else
            {
                string[] logicalDrives = WindowsStorage.GetLogicalDrives(info.Index);
                if (logicalDrives == null || logicalDrives.Length == 0)
                {
                    smart.Close();
                    return(null);
                }

                bool hasNonZeroSizeDrive = false;
                foreach (string logicalDrive in logicalDrives)
                {
                    try
                    {
                        var di = new DriveInfo(logicalDrive);
                        if (di.TotalSize > 0)
                        {
                            hasNonZeroSizeDrive = true;
                            break;
                        }
                    }
                    catch (ArgumentException) { }
                    catch (IOException) { }
                    catch (UnauthorizedAccessException) { }
                }

                if (!hasNonZeroSizeDrive)
                {
                    smart.Close();
                    return(null);
                }
            }

            if (string.IsNullOrEmpty(name))
            {
                name = string.IsNullOrEmpty(info.Name) ? "Generic Hard Disk" : info.Name;
            }

            if (string.IsNullOrEmpty(firmwareRevision))
            {
                firmwareRevision = string.IsNullOrEmpty(info.Revision) ? "Unknown" : info.Revision;
            }

            foreach (Type type in HddTypes)
            {
                // get the array of name prefixes for the current type
                var namePrefixes = type.GetCustomAttributes(typeof(NamePrefixAttribute), true) as NamePrefixAttribute[];

                // get the array of the required SMART attributes for the current type
                var requiredAttributes = type.GetCustomAttributes(typeof(RequireSmartAttribute), true) as RequireSmartAttribute[];

                // check if all required attributes are present
                bool allRequiredAttributesFound = true;
                if (requiredAttributes != null)
                {
                    foreach (var requireAttribute in requiredAttributes)
                    {
                        bool attributeFound = false;
                        foreach (Kernel32.SMART_ATTRIBUTE value in values)
                        {
                            if (value.Id == requireAttribute.AttributeId)
                            {
                                attributeFound = true;
                                break;
                            }
                        }

                        if (!attributeFound)
                        {
                            allRequiredAttributesFound = false;
                            break;
                        }
                    }
                }

                // if an attribute is missing, then try the next type
                if (!allRequiredAttributesFound)
                {
                    continue;
                }


                // check if there is a matching name prefix for this type
                if (namePrefixes != null)
                {
                    foreach (NamePrefixAttribute prefix in namePrefixes)
                    {
                        if (name.StartsWith(prefix.Prefix, StringComparison.InvariantCulture))
                        {
                            var flags = BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance;
                            return(Activator.CreateInstance(type, flags, null, new object[] { info, smart, name, firmwareRevision, info.Index, settings }, null) as AtaStorage);
                        }
                    }
                }
            }

            // no matching type has been found
            smart.Close();
            return(null);
        }