protected AbstractStorage(string name, string firmwareRevision,
                                  string id, int index, ISettings settings)
            : base(name, new Identifier(id,
                                        index.ToString(CultureInfo.InvariantCulture)), settings)
        {
            this.firmwareRevision = firmwareRevision;

            this.index = index;
            this.count = 0;

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

            foreach (string logicalDrive in logicalDrives)
            {
                try {
                    DriveInfo di = new DriveInfo(logicalDrive);
                    if (di.TotalSize > 0)
                    {
                        driveInfoList.Add(new DriveInfo(logicalDrive));
                    }
                } catch (ArgumentException) {
                } catch (IOException) {
                } catch (UnauthorizedAccessException) {
                }
            }
            driveInfos = driveInfoList.ToArray();
        }
        public static AbstractStorage CreateInstance(StorageInfo info, ISettings settings)
        {
            string name = string.IsNullOrEmpty(info.Name) ? "Generic Hard Disk" : info.Name;
            IEnumerable <string> logicalDrives = WindowsStorage.GetLogicalDrives(info.Index);

            if (logicalDrives.Any())
            {
                logicalDrives = logicalDrives.Select(x => $"{x}:");
                name         += " (" + string.Join(", ", logicalDrives) + ")";
            }

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

            return(new StorageGeneric(name, firmwareRevision, info.Index, settings));
        }
        public static AbstractStorage CreateInstance(StorageInfo storageInfo, NVMeGeneric previousNvme, ISettings settings)
        {
            NVMeInfo nvmeInfo = GetDeviceInfo(storageInfo, previousNvme != null ? previousNvme.info.LogicalDeviceNumber : -1);

            if (nvmeInfo == null)
            {
                Logging.LogInfo($"Device {storageInfo.Index} ({storageInfo.Name}) identifies as NVMe device, but does not support all requires features.");
            }

            IEnumerable <string> logicalDrives = WindowsStorage.GetLogicalDrives(storageInfo.Index);
            string name = nvmeInfo.Model;

            if (logicalDrives.Any())
            {
                logicalDrives = logicalDrives.Select(x => $"{x}:");
                name         += " (" + string.Join(", ", logicalDrives) + ")";
            }

            return(new NVMeGeneric(name, nvmeInfo, storageInfo.Index, settings));
        }
Пример #4
0
        public static AbstractStorage CreateInstance(StorageInfo info, ISettings settings)
        {
            ISmart smart = new WindowsSmart(info.Index);

            string name             = null;
            string firmwareRevision = null;

            DriveAttributeValue[] values        = { };
            IEnumerable <string>  logicalDrives = WindowsStorage.GetLogicalDrives(info.Index);

            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
            {
                if (logicalDrives == null || !logicalDrives.Any())
                {
                    smart.Close();
                    return(null);
                }

                bool hasNonZeroSizeDrive = false;
                foreach (string logicalDrive in logicalDrives)
                {
                    try {
                        DriveInfo di = new DriveInfo(logicalDrive);
                        if (di.TotalSize > 0)
                        {
                            hasNonZeroSizeDrive = true;
                            break;
                        }
                    } catch (Exception x) when(x is ArgumentException || x is IOException || x is UnauthorizedAccessException)
                    {
                        Logging.LogError(x, $"Unable to get drive info on {info.Name} for logical drive {logicalDrive}");
                    }
                }

                if (!hasNonZeroSizeDrive)
                {
                    Logging.LogInfo($"Excluding {info.Name} because it has no valid partitions and is not SMART capable.");
                    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;
            }

            if (logicalDrives.Any())
            {
                logicalDrives = logicalDrives.Select(x => $"{x}:");
                name         += " (" + string.Join(", ", logicalDrives) + ")";
            }

            Logging.LogInfo($"Attempting to initialize sensor instance for {name}");

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

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

                // check if all required attributes are present
                bool allRequiredAttributesFound = true;
                foreach (var requireAttribute in requiredAttributes)
                {
                    bool adttributeFound = false;
                    foreach (DriveAttributeValue value in values)
                    {
                        if (value.Identifier == requireAttribute.AttributeId)
                        {
                            adttributeFound = true;
                            break;
                        }
                    }
                    if (!adttributeFound)
                    {
                        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
                foreach (NamePrefixAttribute prefix in namePrefixes)
                {
                    if (name.StartsWith(prefix.Prefix, StringComparison.InvariantCulture))
                    {
                        Logging.LogInfo($"Drive appears to be an instance of {type}");
                        return(Activator.CreateInstance(type, smart, name, firmwareRevision,
                                                        info.Index, settings) as ATAStorage);
                    }
                }
            }

            Logging.LogInfo($"Could not find a matching sensor type for this device");
            // no matching type has been found
            smart.Close();
            return(null);
        }
        public static AbstractStorage CreateInstance(StorageInfo info, ISettings settings)
        {
            ISmart smart = new WindowsSmart(info.Index);

            string name             = null;
            string firmwareRevision = null;

            DriveAttributeValue[] 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 {
                        DriveInfo 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
                NamePrefixAttribute[] namePrefixes = type.GetCustomAttributes(
                    typeof(NamePrefixAttribute), true) as NamePrefixAttribute[];

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

                // check if all required attributes are present
                bool allRequiredAttributesFound = true;
                foreach (var requireAttribute in requiredAttributes)
                {
                    bool adttributeFound = false;
                    foreach (DriveAttributeValue value in values)
                    {
                        if (value.Identifier == requireAttribute.AttributeId)
                        {
                            adttributeFound = true;
                            break;
                        }
                    }
                    if (!adttributeFound)
                    {
                        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
                foreach (NamePrefixAttribute prefix in namePrefixes)
                {
                    if (name.StartsWith(prefix.Prefix, StringComparison.InvariantCulture))
                    {
                        return(Activator.CreateInstance(type, smart, name, firmwareRevision,
                                                        info.Index, settings) as ATAStorage);
                    }
                }
            }

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