From e3ea39afaa040a0e3534bbbd8bf73ba8e5fa9ff7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Tue, 18 Oct 2022 18:39:28 +0800 Subject: [PATCH 1/2] Disk: add volume name detection --- presets/verbose | 2 +- src/detection/disk/disk.h | 1 + src/detection/disk/disk_apple.m | 28 ++++++++++++++-------------- src/detection/disk/disk_linux.c | 2 ++ src/detection/disk/disk_windows.c | 9 +++++++-- src/modules/disk.c | 5 +++-- 6 files changed, 28 insertions(+), 19 deletions(-) diff --git a/presets/verbose b/presets/verbose index 95512dc86..3ab55d1c0 100644 --- a/presets/verbose +++ b/presets/verbose @@ -19,7 +19,7 @@ --cpu-usage-format Percentage: {} --gpu-format Vendor: {}; Name: {}; Driver: {}; Temperature: {}; CoreCount: {} --memory-format Used: {}; Total: {}; Percentage: {} ---disk-format SizeUsed: {}; SizeTotal: {}; SizePercentage: {}; FilesUsed: {}; FilesTotal: {}; FilesPercentage: {}; Removable: {}; Hidden: {}; Filesystem: {} +--disk-format SizeUsed: {}; SizeTotal: {}; SizePercentage: {}; FilesUsed: {}; FilesTotal: {}; FilesPercentage: {}; Removable: {}; Hidden: {}; Filesystem: {}; Name: {} --battery-format Manufactor: {}; Model: {}; Technology: {}; Capacity: {}; Status: {} --poweradapter-format Watts: {}; Name: {}; Manufactor: {}; Model: {}; Description: {} --player-format Pretty: {}; Name: {}; Bus: {}; Url: {} diff --git a/src/detection/disk/disk.h b/src/detection/disk/disk.h index 30243d73c..e8b6b100b 100644 --- a/src/detection/disk/disk.h +++ b/src/detection/disk/disk.h @@ -16,6 +16,7 @@ typedef struct FFDisk { FFstrbuf mountpoint; FFstrbuf filesystem; + FFstrbuf name; FFDiskType type; uint64_t bytesUsed; diff --git a/src/detection/disk/disk_apple.m b/src/detection/disk/disk_apple.m index 804a75dd8..a267d1177 100644 --- a/src/detection/disk/disk_apple.m +++ b/src/detection/disk/disk_apple.m @@ -4,16 +4,6 @@ #import #import -static bool getBool(NSURL* url, NSURLResourceKey key) -{ - NSError *error; - NSNumber* result; - if([url getResourceValue:&result forKey:key error:&error] == NO) - return false; - - return result.boolValue; -} - void ffDetectDisksImpl(FFDiskResult* disks) { NSArray *keys = [NSArray arrayWithObjects:NSURLVolumeNameKey, nil]; @@ -32,8 +22,9 @@ void ffDetectDisksImpl(FFDiskResult* disks) ffStrbufInitS(&disk->mountpoint, [url.relativePath cStringUsingEncoding:NSUTF8StringEncoding]); NSString* filesystem; - [[NSWorkspace sharedWorkspace] getFileSystemInfoForPath:url.relativePath - isRemovable:nil + BOOL removable; + [NSWorkspace.sharedWorkspace getFileSystemInfoForPath:url.relativePath + isRemovable:&removable isWritable:nil isUnmountable:nil description:nil @@ -41,13 +32,22 @@ void ffDetectDisksImpl(FFDiskResult* disks) ]; ffStrbufInitS(&disk->filesystem, [filesystem cStringUsingEncoding:NSUTF8StringEncoding]); - if(getBool(url, NSURLVolumeIsRemovableKey)) + NSError* error; + + NSNumber* isBrowsable; + if(removable) disk->type = FF_DISK_TYPE_EXTERNAL; - else if(getBool(url, NSURLVolumeIsBrowsableKey)) + else if([url getResourceValue:&isBrowsable forKey:NSURLVolumeIsBrowsableKey error:&error] == YES && isBrowsable.boolValue) disk->type = FF_DISK_TYPE_REGULAR; else disk->type = FF_DISK_TYPE_HIDDEN; + NSString* volumeName; + if([url getResourceValue:&volumeName forKey:NSURLVolumeNameKey error:&error] == YES) + ffStrbufInitS(&disk->name, [volumeName cStringUsingEncoding:NSUTF8StringEncoding]); + else + ffStrbufInit(&disk->name); + struct statvfs fs; if(statvfs(disk->mountpoint.chars, &fs) != 0) memset(&fs, 0, sizeof(struct statvfs)); //Set all values to 0, so our values get initialized to 0 too diff --git a/src/detection/disk/disk_linux.c b/src/detection/disk/disk_linux.c index bbf4e22a8..2ff4395a9 100644 --- a/src/detection/disk/disk_linux.c +++ b/src/detection/disk/disk_linux.c @@ -72,6 +72,8 @@ void ffDetectDisksImpl(FFDiskResult* disks) disk->filesTotal = (uint32_t) fs.f_files; disk->filesUsed = (uint32_t) (disk->filesTotal - fs.f_ffree); + + ffStrbufInit(&disk->name); //TODO: implement this } if(line != NULL) diff --git a/src/detection/disk/disk_windows.c b/src/detection/disk/disk_windows.c index 28e010b0b..2d11739b4 100644 --- a/src/detection/disk/disk_windows.c +++ b/src/detection/disk/disk_windows.c @@ -22,7 +22,7 @@ void ffDetectDisksImpl(FFDiskResult* disks) UINT driveType = GetDriveTypeA(mountpoint); if(driveType == DRIVE_NO_ROOT_DIR) { - i += strlen(mountpoint); + i += (uint32_t)strlen(mountpoint); continue; } @@ -45,13 +45,18 @@ void ffDetectDisksImpl(FFDiskResult* disks) disk->type = FF_DISK_TYPE_HIDDEN; ffStrbufInitA(&disk->filesystem, MAX_PATH + 1); + ffStrbufInitA(&disk->name, MAX_PATH + 1); + //https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-getvolumeinformationa#remarks + UINT errorMode = SetErrorMode(SEM_FAILCRITICALERRORS); GetVolumeInformationA(mountpoint, - NULL, 0, //Volume name + disk->name.chars, disk->name.allocated, //Volume name NULL, //Serial number NULL, //Max component length NULL, //File system flags disk->filesystem.chars, disk->filesystem.allocated ); + SetErrorMode(errorMode); + ffStrbufRecalculateLength(&disk->name); ffStrbufRecalculateLength(&disk->filesystem); //TODO: implement diff --git a/src/modules/disk.c b/src/modules/disk.c index 88dd6634d..3ec02f59f 100644 --- a/src/modules/disk.c +++ b/src/modules/disk.c @@ -4,7 +4,7 @@ #include "detection/disk/disk.h" #define FF_DISK_MODULE_NAME "Disk" -#define FF_DISK_NUM_FORMAT_ARGS 9 +#define FF_DISK_NUM_FORMAT_ARGS 10 static void printDisk(FFinstance* instance, const FFDisk* disk) { @@ -49,7 +49,8 @@ static void printDisk(FFinstance* instance, const FFDisk* disk) {FF_FORMAT_ARG_TYPE_UINT8, &disk->filesPercentage}, {FF_FORMAT_ARG_TYPE_BOOL, FF_FORMAT_ARG_VALUE_BOOL(disk->type == FF_DISK_TYPE_EXTERNAL)}, {FF_FORMAT_ARG_TYPE_BOOL, FF_FORMAT_ARG_VALUE_BOOL(disk->type == FF_DISK_TYPE_HIDDEN)}, - {FF_FORMAT_ARG_TYPE_STRBUF, &disk->filesystem} + {FF_FORMAT_ARG_TYPE_STRBUF, &disk->filesystem}, + {FF_FORMAT_ARG_TYPE_STRBUF, &disk->name} }); } From 3d5ef186baa0496edca28a034144dea769d92f3b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Tue, 18 Oct 2022 18:50:38 +0800 Subject: [PATCH 2/2] Disk: don't convert NAN to integer; don't print 0B / 0B (0%) fix ASAN warning: `runtime error: nan is outside the range of representable values of type 'unsigned char'` --- src/detection/disk/disk.c | 7 ------- src/detection/disk/disk.h | 2 -- src/modules/disk.c | 14 +++++++++++--- 3 files changed, 11 insertions(+), 12 deletions(-) diff --git a/src/detection/disk/disk.c b/src/detection/disk/disk.c index 3ce33aed5..de25d634e 100644 --- a/src/detection/disk/disk.c +++ b/src/detection/disk/disk.c @@ -19,13 +19,6 @@ const FFDiskResult* ffDetectDisks() if(result.disks.length == 0 && result.error.length == 0) ffStrbufAppendS(&result.error, "No disks found"); - for(uint32_t i = 0; i < result.disks.length; ++i) - { - FFDisk* disk = ffListGet(&result.disks, i); - disk->bytesPercentage = (uint8_t) (((long double) disk->bytesUsed / (long double) disk->bytesTotal) * 100.0); - disk->filesPercentage = (uint8_t) (((long double) disk->filesUsed / (long double) disk->filesTotal) * 100.0); - } - //We need to sort the disks, so that we can detect, which disk a path resides on // For example for /boot/efi/bootmgr we need to check /boot/efi before /boot //Note that we sort alphabetically here for a better ordering when printing the list, diff --git a/src/detection/disk/disk.h b/src/detection/disk/disk.h index e8b6b100b..c061ce6c9 100644 --- a/src/detection/disk/disk.h +++ b/src/detection/disk/disk.h @@ -21,11 +21,9 @@ typedef struct FFDisk uint64_t bytesUsed; uint64_t bytesTotal; - uint8_t bytesPercentage; uint32_t filesUsed; uint32_t filesTotal; - uint8_t filesPercentage; } FFDisk; typedef struct FFDiskResult diff --git a/src/modules/disk.c b/src/modules/disk.c index 3ec02f59f..0245d8a4a 100644 --- a/src/modules/disk.c +++ b/src/modules/disk.c @@ -30,23 +30,31 @@ static void printDisk(FFinstance* instance, const FFDisk* disk) ffStrbufInit(&totalPretty); ffParseSize(disk->bytesTotal, instance->config.binaryPrefixType, &totalPretty); + uint8_t bytesPercentage = disk->bytesTotal > 0 ? (uint8_t) (((long double) disk->bytesUsed / (long double) disk->bytesTotal) * 100.0) : 0; + if(instance->config.disk.outputFormat.length == 0) { ffPrintLogoAndKey(instance, key.chars, 0, NULL); - printf("%s / %s (%u%%)", usedPretty.chars, totalPretty.chars, disk->bytesPercentage); + if(disk->bytesTotal > 0) + printf("%s / %s (%u%%)", usedPretty.chars, totalPretty.chars, bytesPercentage); + else + fputs("unknown", stdout); + if(disk->type == FF_DISK_TYPE_EXTERNAL) printf(" [Removable]"); putchar('\n'); } else { + uint8_t filesPercentage = disk->filesTotal > 0 ? (uint8_t) (((double) disk->filesUsed / (double) disk->filesTotal) * 100.0) : 0; + ffPrintFormatString(instance, key.chars, 0, NULL, &instance->config.disk.outputFormat, FF_DISK_NUM_FORMAT_ARGS, (FFformatarg[]){ {FF_FORMAT_ARG_TYPE_STRBUF, &usedPretty}, {FF_FORMAT_ARG_TYPE_STRBUF, &totalPretty}, - {FF_FORMAT_ARG_TYPE_UINT8, &disk->bytesPercentage}, + {FF_FORMAT_ARG_TYPE_UINT8, &bytesPercentage}, {FF_FORMAT_ARG_TYPE_UINT, &disk->filesUsed}, {FF_FORMAT_ARG_TYPE_UINT, &disk->filesTotal}, - {FF_FORMAT_ARG_TYPE_UINT8, &disk->filesPercentage}, + {FF_FORMAT_ARG_TYPE_UINT8, &filesPercentage}, {FF_FORMAT_ARG_TYPE_BOOL, FF_FORMAT_ARG_VALUE_BOOL(disk->type == FF_DISK_TYPE_EXTERNAL)}, {FF_FORMAT_ARG_TYPE_BOOL, FF_FORMAT_ARG_VALUE_BOOL(disk->type == FF_DISK_TYPE_HIDDEN)}, {FF_FORMAT_ARG_TYPE_STRBUF, &disk->filesystem},