Files
Aaru/Aaru.Devices/Linux/ListDevices.cs

153 lines
6.0 KiB
C#
Raw Normal View History

// /***************************************************************************
2020-02-27 12:31:25 +00:00
// Aaru Data Preservation Suite
// ----------------------------------------------------------------------------
//
// Filename : ListDevices.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// Component : Linux direct device access.
//
// --[ Description ] ----------------------------------------------------------
//
// Gets a list of known physical devices.
//
// --[ License ] --------------------------------------------------------------
//
// This library is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation; either version 2.1 of the
// License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, see <http://www.gnu.org/licenses/>.
//
// ----------------------------------------------------------------------------
2024-12-19 10:45:18 +00:00
// Copyright © 2011-2025 Natalia Portillo
// ****************************************************************************/
2017-12-19 19:33:46 +00:00
2017-08-22 03:38:02 +01:00
using System;
using System.IO;
2022-11-14 01:15:06 +00:00
using System.Runtime.Versioning;
using System.Text;
namespace Aaru.Devices.Linux;
2022-11-14 01:15:06 +00:00
[SupportedOSPlatform("linux")]
2022-03-07 07:36:44 +00:00
static class ListDevices
{
2022-03-06 13:29:38 +00:00
const string PATH_SYS_DEVBLOCK = "/sys/block/";
/// <summary>Gets a list of all known storage devices on Linux</summary>
/// <returns>List of devices</returns>
internal static DeviceInfo[] GetList()
{
2022-03-06 13:29:38 +00:00
string[] sysdevs = Directory.GetFileSystemEntries(PATH_SYS_DEVBLOCK, "*", SearchOption.TopDirectoryOnly);
2023-10-03 23:12:01 +01:00
var devices = new DeviceInfo[sysdevs.Length];
bool hasUdev;
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
IntPtr udev = IntPtr.Zero;
2022-03-06 13:29:38 +00:00
try
{
udev = Extern.udev_new();
hasUdev = udev != IntPtr.Zero;
}
catch
{
hasUdev = false;
}
2023-10-03 23:12:01 +01:00
for(var i = 0; i < sysdevs.Length; i++)
2022-03-06 13:29:38 +00:00
{
devices[i] = new DeviceInfo
2020-02-29 18:03:35 +00:00
{
2022-03-06 13:29:38 +00:00
Path = "/dev/" + Path.GetFileName(sysdevs[i])
};
2022-03-06 13:29:38 +00:00
if(hasUdev)
{
2022-03-06 13:29:38 +00:00
IntPtr udevDev =
Extern.udev_device_new_from_subsystem_sysname(udev, "block", Path.GetFileName(sysdevs[i]));
2022-03-06 13:29:38 +00:00
devices[i].Vendor = Extern.udev_device_get_property_value(udevDev, "ID_VENDOR");
devices[i].Model = Extern.udev_device_get_property_value(udevDev, "ID_MODEL");
2020-02-29 18:03:35 +00:00
2024-05-01 04:05:22 +01:00
if(!string.IsNullOrEmpty(devices[i].Model)) devices[i].Model = devices[i].Model.Replace('_', ' ');
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
devices[i].Serial = Extern.udev_device_get_property_value(udevDev, "ID_SCSI_SERIAL");
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
if(string.IsNullOrEmpty(devices[i].Serial))
devices[i].Serial = Extern.udev_device_get_property_value(udevDev, "ID_SERIAL_SHORT");
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
devices[i].Bus = Extern.udev_device_get_property_value(udevDev, "ID_BUS");
}
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
StreamReader sr;
if(File.Exists(Path.Combine(sysdevs[i], "device/vendor")) && string.IsNullOrEmpty(devices[i].Vendor))
2022-03-06 13:29:38 +00:00
{
sr = new StreamReader(Path.Combine(sysdevs[i], "device/vendor"), Encoding.ASCII);
devices[i].Vendor = sr.ReadLine()?.Trim();
}
else if(devices[i].Path.StartsWith("/dev/loop", StringComparison.CurrentCulture))
devices[i].Vendor = "Linux";
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
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();
}
else if(devices[i].Path.StartsWith("/dev/loop", StringComparison.CurrentCulture))
devices[i].Model = "Linux";
if(File.Exists(Path.Combine(sysdevs[i], "device/serial")) && string.IsNullOrEmpty(devices[i].Serial))
2022-03-06 13:29:38 +00:00
{
sr = new StreamReader(Path.Combine(sysdevs[i], "device/serial"), Encoding.ASCII);
devices[i].Serial = sr.ReadLine()?.Trim();
}
if(string.IsNullOrEmpty(devices[i].Vendor) || devices[i].Vendor == "ATA")
2023-10-03 23:12:01 +01:00
{
2022-03-06 13:29:38 +00:00
if(devices[i].Model != null)
{
2022-03-06 13:29:38 +00:00
string[] pieces = devices[i].Model.Split(' ');
2022-03-06 13:29:38 +00:00
if(pieces.Length > 1)
{
2022-03-06 13:29:38 +00:00
devices[i].Vendor = pieces[0];
2022-11-14 01:15:06 +00:00
devices[i].Model = devices[i].Model[(pieces[0].Length + 1)..];
}
}
2023-10-03 23:12:01 +01:00
}
2022-03-06 13:29:38 +00:00
// TODO: Get better device type from sysfs paths
if(string.IsNullOrEmpty(devices[i].Bus))
{
if(devices[i].Path.StartsWith("/dev/loop", StringComparison.CurrentCulture))
devices[i].Bus = "loop";
else if(devices[i].Path.StartsWith("/dev/nvme", StringComparison.CurrentCulture))
devices[i].Bus = "NVMe";
else if(devices[i].Path.StartsWith("/dev/mmc", StringComparison.CurrentCulture))
devices[i].Bus = "MMC/SD";
}
2022-03-06 13:29:38 +00:00
else
devices[i].Bus = devices[i].Bus.ToUpper();
devices[i].Supported = devices[i].Bus switch
{
"ATA" or "ATAPI" or "SCSI" or "USB" or "PCMCIA" or "FireWire" or "MMC/SD" =>
true,
_ => devices[i].Supported
};
}
2022-03-06 13:29:38 +00:00
return devices;
}
2017-12-19 20:33:03 +00:00
}