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] 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} }); }