Disk: add volume name detection

This commit is contained in:
李通洲
2022-10-18 18:39:28 +08:00
parent 7bbd2aee94
commit e3ea39afaa
6 changed files with 28 additions and 19 deletions
+1 -1
View File
@@ -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: {}
+1
View File
@@ -16,6 +16,7 @@ typedef struct FFDisk
{
FFstrbuf mountpoint;
FFstrbuf filesystem;
FFstrbuf name;
FFDiskType type;
uint64_t bytesUsed;
+14 -14
View File
@@ -4,16 +4,6 @@
#import <Foundation/Foundation.h>
#import <AppKit/AppKit.h>
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
+2
View File
@@ -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)
+7 -2
View File
@@ -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
+3 -2
View File
@@ -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}
});
}