Skip to content

Commit 8fa6aee

Browse files
committed
Disk: move physicalType to module DiskIO
Physical type applies to physical disks, not partitions
1 parent 4194e56 commit 8fa6aee

File tree

11 files changed

+112
-159
lines changed

11 files changed

+112
-159
lines changed

src/detection/disk/disk.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,13 @@
55

66
#include "fastfetch.h"
77

8-
typedef enum FFDiskPhysicalType
9-
{
10-
FF_DISK_PHYSICAL_TYPE_UNKNOWN,
11-
FF_DISK_PHYSICAL_TYPE_HDD,
12-
FF_DISK_PHYSICAL_TYPE_SSD,
13-
} FFDiskPhysicalType;
14-
158
typedef struct FFDisk
169
{
1710
FFstrbuf mountFrom;
1811
FFstrbuf mountpoint;
1912
FFstrbuf filesystem;
2013
FFstrbuf name;
2114
FFDiskVolumeType type;
22-
FFDiskPhysicalType physicalType;
2315

2416
uint64_t bytesUsed;
2517
uint64_t bytesFree;

src/detection/disk/disk_bsd.c

Lines changed: 0 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -25,60 +25,13 @@ static void detectFsInfo(struct statfs* fs, FFDisk* disk)
2525

2626
#include <sys/attr.h>
2727
#include <unistd.h>
28-
#include <IOKit/IOKitLib.h>
29-
#include <IOKit/storage/IOStorageDeviceCharacteristics.h>
3028

3129
struct VolAttrBuf {
3230
uint32_t length;
3331
attrreference_t volNameRef;
3432
char volNameSpace[MAXPATHLEN];
3533
} __attribute__((aligned(4), packed));
3634

37-
void detectDiskType(FFDisk* disk) // Not thread safe
38-
{
39-
if (!ffStrbufStartsWithS(&disk->mountFrom, "/dev/disk")) return;
40-
41-
static uint8_t cache[100]; // disk_id => physical_type + 1
42-
const char* numStart = disk->mountFrom.chars + strlen("/dev/disk");
43-
char* numEnd = NULL;
44-
unsigned long diskId = strtoul(numStart, &numEnd, 10);
45-
if (numEnd == numStart || diskId >= 100)
46-
return;
47-
48-
if (cache[diskId])
49-
{
50-
disk->physicalType = cache[diskId] - 1;
51-
return;
52-
}
53-
54-
io_iterator_t iterator;
55-
char temp = *numEnd; *numEnd = '\0'; // Check for root disk directly
56-
*numEnd = temp;
57-
if(IOServiceGetMatchingServices(MACH_PORT_NULL, IOBSDNameMatching(MACH_PORT_NULL, 0, disk->mountFrom.chars + strlen("/dev/")), &iterator) == kIOReturnSuccess)
58-
{
59-
for (io_registry_entry_t registryEntry = IOIteratorNext(iterator); registryEntry; IORegistryEntryGetParentEntry(registryEntry, kIOServicePlane, &registryEntry))
60-
{
61-
FF_CFTYPE_AUTO_RELEASE CFDictionaryRef deviceCharacteristics = (CFDictionaryRef) IORegistryEntryCreateCFProperty(registryEntry, CFSTR(kIOPropertyDeviceCharacteristicsKey), kCFAllocatorDefault, kNilOptions);
62-
if (!deviceCharacteristics)
63-
continue;
64-
65-
CFStringRef diskType = (CFStringRef) CFDictionaryGetValue(deviceCharacteristics, CFSTR(kIOPropertyMediumTypeKey));
66-
if (diskType)
67-
{
68-
if (CFStringCompare(diskType, CFSTR(kIOPropertyMediumTypeSolidStateKey), 0) == 0)
69-
disk->physicalType = FF_DISK_PHYSICAL_TYPE_SSD;
70-
else if (CFStringCompare(diskType, CFSTR(kIOPropertyMediumTypeRotationalKey), 0) == 0)
71-
disk->physicalType = FF_DISK_PHYSICAL_TYPE_HDD;
72-
}
73-
break;
74-
}
75-
76-
IOObjectRelease(iterator);
77-
}
78-
79-
cache[diskId] = (uint8_t) (disk->physicalType + 1);
80-
}
81-
8235
void detectFsInfo(struct statfs* fs, FFDisk* disk)
8336
{
8437
if(fs->f_flags & MNT_DONTBROWSE)
@@ -94,8 +47,6 @@ void detectFsInfo(struct statfs* fs, FFDisk* disk)
9447
.volattr = ATTR_VOL_INFO | ATTR_VOL_NAME,
9548
}, &attrBuf, sizeof(attrBuf), 0) == 0)
9649
ffStrbufInitNS(&disk->name, attrBuf.volNameRef.attr_length - 1 /* excluding '\0' */, attrBuf.volNameSpace);
97-
98-
detectDiskType(disk);
9950
}
10051
#endif
10152

@@ -122,7 +73,6 @@ const char* ffDetectDisksImpl(FFlist* disks)
12273
#endif
12374

12475
FFDisk* disk = ffListAdd(disks);
125-
disk->physicalType = FF_DISK_PHYSICAL_TYPE_UNKNOWN;
12676

12777
disk->bytesTotal = fs->f_blocks * fs->f_bsize;
12878
disk->bytesFree = (uint64_t)fs->f_bfree * fs->f_bsize;

src/detection/disk/disk_linux.c

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ static bool isSubvolume(const FFlist* disks, FFDisk* currentDisk)
195195
return false;
196196
}
197197

198-
static bool detectPhysicalTypeAndReturnRemovable(FFDisk* currentDisk)
198+
static bool isRemovable(FFDisk* currentDisk)
199199
{
200200
// https://stackoverflow.com/a/73302025
201201
// Note my USB mobile hard disk isn't detected as removable, but my USB flash disk does.
@@ -218,20 +218,10 @@ static bool detectPhysicalTypeAndReturnRemovable(FFDisk* currentDisk)
218218

219219
if (!ffStrStartsWith(partitionName, entry->d_name)) continue;
220220

221-
ffStrbufAppendS(&basePath, entry->d_name);
222-
index = basePath.length;
223-
// /sys/block/sdx/queue/rotational
224-
ffStrbufAppendS(&basePath, "/queue/rotational");
225-
FF_STRBUF_AUTO_DESTROY buffer = ffStrbufCreate();
226-
if (ffReadFileBuffer(basePath.chars, &buffer))
227-
currentDisk->physicalType = ffStrbufEqualS(&buffer, "1") ? FF_DISK_PHYSICAL_TYPE_HDD : FF_DISK_PHYSICAL_TYPE_SSD;
228-
else
229-
currentDisk->physicalType = FF_DISK_PHYSICAL_TYPE_UNKNOWN;
230-
ffStrbufSubstrBefore(&basePath, index);
231-
232221
// /sys/block/sdx/removable
222+
ffStrbufAppendS(&basePath, entry->d_name);
233223
ffStrbufAppendS(&basePath, "/removable");
234-
224+
FF_STRBUF_AUTO_DESTROY buffer = ffStrbufCreate();
235225
return ffReadFileBuffer(basePath.chars, &buffer) && ffStrbufEqualS(&buffer, "1");
236226
}
237227

@@ -240,10 +230,9 @@ static bool detectPhysicalTypeAndReturnRemovable(FFDisk* currentDisk)
240230

241231
static void detectType(const FFlist* disks, FFDisk* currentDisk)
242232
{
243-
bool isRemovable = detectPhysicalTypeAndReturnRemovable(currentDisk);
244-
245233
if(currentDisk->type != FF_DISK_VOLUME_TYPE_NONE) return;
246-
if(isRemovable)
234+
235+
if(isRemovable(currentDisk))
247236
currentDisk->type = FF_DISK_VOLUME_TYPE_EXTERNAL_BIT;
248237
else if(isSubvolume(disks, currentDisk))
249238
currentDisk->type = FF_DISK_VOLUME_TYPE_SUBVOLUME_BIT;
@@ -289,7 +278,6 @@ const char* ffDetectDisksImpl(FFlist* disks)
289278
//We have a valid device, add it to the list
290279
FFDisk* disk = ffListAdd(disks);
291280
disk->type = FF_DISK_VOLUME_TYPE_NONE;
292-
disk->physicalType = FF_DISK_PHYSICAL_TYPE_UNKNOWN;
293281

294282
//detect mountFrom
295283
ffStrbufInitS(&disk->mountFrom, device->mnt_fsname);

src/detection/disk/disk_windows.c

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -28,34 +28,6 @@ const char* ffDetectDisksImpl(FFlist* disks)
2828
ffStrbufInitWS(&disk->mountpoint, mountpoint);
2929

3030
wchar_t volumeName[64];
31-
32-
disk->physicalType = FF_DISK_PHYSICAL_TYPE_UNKNOWN;
33-
if(mountpoint[1] == ':')
34-
{
35-
memcpy(volumeName, L"\\\\.\\ :", sizeof(L"\\\\.\\ :"));
36-
volumeName[4] = mountpoint[0];
37-
FF_AUTO_CLOSE_FD HANDLE handle = CreateFileW(volumeName, FILE_READ_ATTRIBUTES, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
38-
if (handle != INVALID_HANDLE_VALUE)
39-
{
40-
DEVICE_SEEK_PENALTY_DESCRIPTOR dspd = {};
41-
DWORD retSize = 0;
42-
if(DeviceIoControl(
43-
handle,
44-
IOCTL_STORAGE_QUERY_PROPERTY,
45-
&(STORAGE_PROPERTY_QUERY) {
46-
.PropertyId = StorageDeviceSeekPenaltyProperty,
47-
.QueryType = PropertyStandardQuery,
48-
},
49-
sizeof(STORAGE_PROPERTY_QUERY),
50-
&dspd,
51-
sizeof(dspd),
52-
&retSize,
53-
NULL
54-
) && retSize == sizeof(dspd))
55-
disk->physicalType = dspd.IncursSeekPenalty ? FF_DISK_PHYSICAL_TYPE_HDD : FF_DISK_PHYSICAL_TYPE_SSD;
56-
}
57-
}
58-
5931
if(GetVolumeNameForVolumeMountPointW(mountpoint, volumeName, sizeof(volumeName) / sizeof(*volumeName)))
6032
ffStrbufInitWS(&disk->mountFrom, volumeName);
6133
else

src/detection/diskio/diskio.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,18 @@
22

33
#include "fastfetch.h"
44

5+
typedef enum FFDiskIOPhysicalType
6+
{
7+
FF_DISKIO_PHYSICAL_TYPE_UNKNOWN,
8+
FF_DISKIO_PHYSICAL_TYPE_HDD,
9+
FF_DISKIO_PHYSICAL_TYPE_SSD,
10+
} FFDiskIOPhysicalType;
11+
512
typedef struct FFDiskIOResult
613
{
714
FFstrbuf name;
8-
FFstrbuf type;
15+
FFstrbuf interconnect;
16+
FFDiskIOPhysicalType type;
917
FFstrbuf devPath;
1018
uint64_t bytesRead;
1119
uint64_t readCount;

src/detection/diskio/diskio_apple.c

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <IOKit/IOBSD.h>
66
#include <IOKit/storage/IOMedia.h>
77
#include <IOKit/storage/IOBlockStorageDriver.h>
8+
#include <IOKit/storage/IOStorageDeviceCharacteristics.h>
89
#include <IOKit/storage/IOStorageProtocolCharacteristics.h>
910

1011
static inline void wrapIoObjectRelease(io_service_t* service)
@@ -47,7 +48,8 @@ const char* ffDiskIOGetIoCounters(FFlist* result, FFDiskIOOptions* options)
4748
FFDiskIOResult* device = (FFDiskIOResult*) ffListAdd(result);
4849
ffStrbufInitS(&device->name, deviceName);
4950
ffStrbufInit(&device->devPath);
50-
ffStrbufInit(&device->type);
51+
device->type = FF_DISKIO_PHYSICAL_TYPE_UNKNOWN;
52+
5153
ffCfDictGetInt64(statistics, CFSTR(kIOBlockStorageDriverStatisticsBytesReadKey), (int64_t*) &device->bytesRead);
5254
ffCfDictGetInt64(statistics, CFSTR(kIOBlockStorageDriverStatisticsBytesWrittenKey), (int64_t*) &device->bytesWritten);
5355
ffCfDictGetInt64(statistics, CFSTR(kIOBlockStorageDriverStatisticsReadsKey), (int64_t*) &device->readCount);
@@ -60,12 +62,26 @@ const char* ffDiskIOGetIoCounters(FFlist* result, FFDiskIOOptions* options)
6062
ffStrbufPrependS(&device->devPath, "/dev/");
6163
}
6264

65+
ffStrbufInit(&device->interconnect);
6366
FF_IOOBJECT_AUTO_RELEASE io_registry_entry_t entryPhysical = 0;
6467
if (IORegistryEntryGetParentEntry(entryDriver, kIOServicePlane, &entryPhysical) == KERN_SUCCESS)
6568
{
6669
FF_CFTYPE_AUTO_RELEASE CFDictionaryRef protocolCharacteristics = IORegistryEntryCreateCFProperty(entryPhysical, CFSTR(kIOPropertyProtocolCharacteristicsKey), kCFAllocatorDefault, kNilOptions);
6770
if (protocolCharacteristics)
68-
ffCfDictGetString(protocolCharacteristics, CFSTR("Physical Interconnect"), &device->type);
71+
ffCfDictGetString(protocolCharacteristics, CFSTR("Physical Interconnect"), &device->interconnect);
72+
73+
FF_CFTYPE_AUTO_RELEASE CFDictionaryRef deviceCharacteristics = IORegistryEntryCreateCFProperty(entryPhysical, CFSTR(kIOPropertyDeviceCharacteristicsKey), kCFAllocatorDefault, kNilOptions);
74+
if (deviceCharacteristics)
75+
{
76+
CFStringRef mediumType = (CFStringRef) CFDictionaryGetValue(deviceCharacteristics, CFSTR(kIOPropertyMediumTypeKey));
77+
if (mediumType)
78+
{
79+
if (CFStringCompare(mediumType, CFSTR(kIOPropertyMediumTypeSolidStateKey), 0) == 0)
80+
device->type = FF_DISKIO_PHYSICAL_TYPE_SSD;
81+
else if (CFStringCompare(mediumType, CFSTR(kIOPropertyMediumTypeRotationalKey), 0) == 0)
82+
device->type = FF_DISKIO_PHYSICAL_TYPE_HDD;
83+
}
84+
}
6985
}
7086
}
7187

src/detection/diskio/diskio_bsd.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,13 @@ const char* ffDiskIOGetIoCounters(FFlist* result, FFDiskIOOptions* options)
2929
FFDiskIOResult* device = (FFDiskIOResult*) ffListAdd(result);
3030
ffStrbufInitS(&device->name, deviceName);
3131
ffStrbufInitF(&device->devPath, "/dev/%s", deviceName);
32-
33-
ffStrbufInit(&device->type);
32+
device->type = FF_DISKIO_PHYSICAL_TYPE_UNKNOWN;
33+
ffStrbufInit(&device->interconnect);
3434
switch (current->device_type & DEVSTAT_TYPE_IF_MASK)
3535
{
36-
case DEVSTAT_TYPE_IF_SCSI: ffStrbufAppendS(&device->type, "SCSI"); break;
37-
case DEVSTAT_TYPE_IF_IDE: ffStrbufAppendS(&device->type, "IDE"); break;
38-
case DEVSTAT_TYPE_IF_OTHER: ffStrbufAppendS(&device->type, "OTHER"); break;
36+
case DEVSTAT_TYPE_IF_SCSI: ffStrbufAppendS(&device->interconnect, "SCSI"); break;
37+
case DEVSTAT_TYPE_IF_IDE: ffStrbufAppendS(&device->interconnect, "IDE"); break;
38+
case DEVSTAT_TYPE_IF_OTHER: ffStrbufAppendS(&device->interconnect, "OTHER"); break;
3939
}
4040
device->bytesRead = current->bytes[DEVSTAT_READ];
4141
device->readCount = current->operations[DEVSTAT_READ];

src/detection/diskio/diskio_linux.c

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,10 @@ const char* ffDiskIOGetIoCounters(FFlist* result, FFDiskIOOptions* options)
5353
}
5454
if (flag) continue;
5555

56-
char pathSysBlockStat[PATH_MAX];
57-
snprintf(pathSysBlockStat, PATH_MAX, "/sys/block/%s/stat", devName);
56+
char pathSysBlock[PATH_MAX];
57+
snprintf(pathSysBlock, PATH_MAX, "/sys/block/%s/stat", devName);
5858

59-
FF_AUTO_CLOSE_FILE FILE* sysBlockStat = fopen(pathSysBlockStat, "r");
59+
FF_AUTO_CLOSE_FILE FILE* sysBlockStat = fopen(pathSysBlock, "r");
6060
if (!sysBlockStat) continue;
6161

6262
// I/Os merges sectors ticks ...
@@ -73,19 +73,19 @@ const char* ffDiskIOGetIoCounters(FFlist* result, FFDiskIOOptions* options)
7373
ffStrbufInitNS(&device->name, (uint32_t) (slash2 - slash - 1), slash + 1);
7474
else
7575
ffStrbufInitS(&device->name, slash + 1);
76-
ffStrbufInitNS(&device->type, (uint32_t) (slash - entry->d_name), entry->d_name);
76+
ffStrbufInitNS(&device->interconnect, (uint32_t) (slash - entry->d_name), entry->d_name);
7777
}
7878
else
7979
{
8080
ffStrbufInitS(&device->name, entry->d_name);
81-
ffStrbufInit(&device->type);
81+
ffStrbufInit(&device->interconnect);
8282
}
8383
ffStrbufReplaceAllC(&device->name, '_', ' ');
8484

8585
if (options->namePrefix.length && !ffStrbufStartsWith(&device->name, &options->namePrefix))
8686
{
8787
ffStrbufDestroy(&device->name);
88-
ffStrbufDestroy(&device->type);
88+
ffStrbufDestroy(&device->interconnect);
8989
result->length--;
9090
continue;
9191
}
@@ -95,6 +95,13 @@ const char* ffDiskIOGetIoCounters(FFlist* result, FFDiskIOOptions* options)
9595
device->bytesWritten = sectorWritten * 512;
9696
device->readCount = nRead;
9797
device->writeCount = nWritten;
98+
99+
snprintf(pathSysBlock, PATH_MAX, "/sys/block/%s/queue/rotational", devName);
100+
FF_STRBUF_AUTO_DESTROY buffer = ffStrbufCreate();
101+
if (ffReadFileBuffer(pathSysBlock, &buffer))
102+
device->type = ffStrbufEqualS(&buffer, "1") ? FF_DISKIO_PHYSICAL_TYPE_HDD : FF_DISKIO_PHYSICAL_TYPE_SSD;
103+
else
104+
device->type = FF_DISKIO_PHYSICAL_TYPE_UNKNOWN;
98105
}
99106

100107
return NULL;

src/detection/diskio/diskio_windows.c

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,14 @@ const char* ffDiskIOGetIoCounters(FFlist* result, FFDiskIOOptions* options)
3131
{
3232
FFDiskIOResult* device = (FFDiskIOResult*) ffListAdd(result);
3333
ffStrbufInit(&device->name);
34-
ffStrbufInit(&device->type);
34+
ffStrbufInit(&device->interconnect);
3535
if (ffRegReadStrbuf(hKey, pNum, &device->name, NULL))
3636
{
3737
// SCSI\Disk&Ven_NVMe&Prod_WDC_PC_SN810_SDC\5&19cebb7&0&000000
3838
uint32_t index = ffStrbufFirstIndexC(&device->name, '\\');
3939
if (index != device->name.length)
4040
{
41-
ffStrbufAppendNS(&device->type, index, device->name.chars); // SCSI
41+
ffStrbufAppendNS(&device->interconnect, index, device->name.chars); // SCSI
4242
ffStrbufSubstrAfter(&device->name, index + 1);
4343
}
4444
ffStrbufSubstrBeforeLastC(&device->name, '\\');
@@ -53,7 +53,7 @@ const char* ffDiskIOGetIoCounters(FFlist* result, FFDiskIOOptions* options)
5353
if (options->namePrefix.length && !ffStrbufStartsWith(&device->name, &options->namePrefix))
5454
{
5555
ffStrbufDestroy(&device->name);
56-
ffStrbufDestroy(&device->type);
56+
ffStrbufDestroy(&device->interconnect);
5757
result->length--;
5858
continue;
5959
}
@@ -63,6 +63,25 @@ const char* ffDiskIOGetIoCounters(FFlist* result, FFDiskIOOptions* options)
6363
device->readCount = (uint64_t) diskPerformance.ReadCount;
6464
device->bytesWritten = (uint64_t) diskPerformance.BytesWritten.QuadPart;
6565
device->writeCount = (uint64_t) diskPerformance.WriteCount;
66+
67+
DEVICE_SEEK_PENALTY_DESCRIPTOR dspd = {};
68+
DWORD retSize = 0;
69+
if(DeviceIoControl(
70+
hDevice,
71+
IOCTL_STORAGE_QUERY_PROPERTY,
72+
&(STORAGE_PROPERTY_QUERY) {
73+
.PropertyId = StorageDeviceSeekPenaltyProperty,
74+
.QueryType = PropertyStandardQuery,
75+
},
76+
sizeof(STORAGE_PROPERTY_QUERY),
77+
&dspd,
78+
sizeof(dspd),
79+
&retSize,
80+
NULL
81+
) && retSize == sizeof(dspd))
82+
device->type = dspd.IncursSeekPenalty ? FF_DISKIO_PHYSICAL_TYPE_HDD : FF_DISKIO_PHYSICAL_TYPE_SSD;
83+
else
84+
device->type = FF_DISKIO_PHYSICAL_TYPE_UNKNOWN;
6685
}
6786
}
6887

0 commit comments

Comments
 (0)