Disk(Linux): Better detection of physical drives

This commit is contained in:
Linus Dierheimer
2023-02-09 11:20:37 +01:00
parent 3a9403a2f1
commit fcc5170a10
+28 -17
View File
@@ -15,28 +15,35 @@
#define readdir readdir64
#endif
static bool isPhysicalDevice(const char* device)
static bool isPhysicalDevice(FFstrbuf* device)
{
struct stat deviceStat;
if(stat(device->chars, &deviceStat) != 0)
return false;
//Ignore all devices that are not block devices
if(!S_ISBLK(deviceStat.st_mode))
return false;
//Ignore all devices that are not physical devices
//DrvFs is a filesystem plugin to WSL that was designed to support interop between WSL and the Windows filesystem.
if(strcmp(device, "drvfs") == 0)
if(ffStrbufEqualS(device, "drvfs"))
return true;
//ZFS root pool. The format is rpool/<POOL_NAME>/<VOLUME_NAME>/<SUBVOLUME_NAME>
if(strncmp(device, "rpool/", 6) == 0)
if(ffStrbufStartsWithS(device, "rpool/"))
return true;
//Pseudo filesystems don't have a device in /dev
const char* devPrefix = "/dev/";
if(strncmp(device, devPrefix, strlen(devPrefix)) != 0)
if(!ffStrbufStartsWithS(device, "/dev/"))
return false;
//Skip /dev/ prefix
device += strlen(devPrefix);
if(
strncmp(device, "loop", 4) == 0 || //Ignore loop devices
strncmp(device, "ram", 3) == 0 || //Ignore ram devices
strncmp(device, "fd", 2) == 0 //Ignore fd devices
ffStrbufStartsWithS(device, "/dev/loop") || //Ignore loop devices
ffStrbufStartsWithS(device, "/dev/ram") || //Ignore ram devices
ffStrbufStartsWithS(device, "/dev/fd") //Ignore fd devices
) return false;
return true;
@@ -208,12 +215,6 @@ void ffDetectDisksImpl(FFDiskResult* disks)
while(getline(&line, &len, mountsFile) != EOF)
{
if(!isPhysicalDevice(line))
continue;
//We have a valid device, add it to the list
FFDisk* disk = ffListAdd(&disks->disks);
//Format of the file: "<device> <mountpoint> <filesystem> <options> ..." (Same as fstab)
char* currentPos = line;
@@ -222,6 +223,16 @@ void ffDetectDisksImpl(FFDiskResult* disks)
ffStrbufInit(device);
appendNextEntry(device, &currentPos);
if(!isPhysicalDevice(device))
{
ffStrbufDestroy(device);
devices.length--;
continue;
}
//We have a valid device, add it to the list
FFDisk* disk = ffListAdd(&disks->disks);
//detect mountpoint
ffStrbufInit(&disk->mountpoint);
appendNextEntry(&disk->mountpoint, &currentPos);