From 434a879538b013efd122aec1480462ccf55d89a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Sat, 26 Aug 2023 09:32:19 +0800 Subject: [PATCH 01/10] Disk: rename FFDiskType to FFDiskVolumeType --- src/detection/disk/disk.c | 2 +- src/detection/disk/disk.h | 2 +- src/detection/disk/disk_apple.m | 6 ++-- src/detection/disk/disk_bsd.c | 10 +++---- src/detection/disk/disk_linux.c | 14 ++++----- src/detection/disk/disk_windows.c | 6 ++-- src/modules/disk/disk.c | 48 +++++++++++++++---------------- src/modules/disk/option.h | 18 ++++++------ 8 files changed, 53 insertions(+), 53 deletions(-) diff --git a/src/detection/disk/disk.c b/src/detection/disk/disk.c index e033f6c93..00eba85c0 100644 --- a/src/detection/disk/disk.c +++ b/src/detection/disk/disk.c @@ -22,7 +22,7 @@ const char* ffDetectDisks(FFlist* disks) FF_LIST_FOR_EACH(FFDisk, disk, *disks) { if(disk->bytesTotal == 0) - disk->type |= FF_DISK_TYPE_UNKNOWN_BIT; + disk->type |= FF_DISK_VOLUME_TYPE_UNKNOWN_BIT; } return NULL; diff --git a/src/detection/disk/disk.h b/src/detection/disk/disk.h index b4d9e53e7..24c28c9cf 100644 --- a/src/detection/disk/disk.h +++ b/src/detection/disk/disk.h @@ -10,7 +10,7 @@ typedef struct FFDisk FFstrbuf mountpoint; FFstrbuf filesystem; FFstrbuf name; - FFDiskType type; + FFDiskVolumeType type; uint64_t bytesUsed; uint64_t bytesTotal; diff --git a/src/detection/disk/disk_apple.m b/src/detection/disk/disk_apple.m index 8868dd801..81bcdeb48 100644 --- a/src/detection/disk/disk_apple.m +++ b/src/detection/disk/disk_apple.m @@ -7,11 +7,11 @@ void detectFsInfo(struct statfs* fs, FFDisk* disk) { // FreeBSD doesn't support these flags if(fs->f_flags & MNT_DONTBROWSE) - disk->type = FF_DISK_TYPE_HIDDEN_BIT; + disk->type = FF_DISK_VOLUME_TYPE_HIDDEN_BIT; else if(fs->f_flags & MNT_REMOVABLE) - disk->type = FF_DISK_TYPE_EXTERNAL_BIT; + disk->type = FF_DISK_VOLUME_TYPE_EXTERNAL_BIT; else - disk->type = FF_DISK_TYPE_REGULAR_BIT; + disk->type = FF_DISK_VOLUME_TYPE_REGULAR_BIT; ffStrbufInitS(&disk->name, [NSFileManager.defaultManager displayNameAtPath:@(fs->f_mntonname)].UTF8String); } diff --git a/src/detection/disk/disk_bsd.c b/src/detection/disk/disk_bsd.c index c4414d2a1..8701f60bd 100644 --- a/src/detection/disk/disk_bsd.c +++ b/src/detection/disk/disk_bsd.c @@ -10,15 +10,15 @@ static void detectFsInfo(struct statfs* fs, FFDisk* disk) if(ffStrbufEqualS(&disk->filesystem, "zfs")) { disk->type = !ffStrStartsWith(fs->f_mntfromname, "zroot/") || ffStrStartsWith(fs->f_mntfromname, "zroot/ROOT/") - ? FF_DISK_TYPE_REGULAR_BIT - : FF_DISK_TYPE_SUBVOLUME_BIT; + ? FF_DISK_VOLUME_TYPE_REGULAR_BIT + : FF_DISK_VOLUME_TYPE_SUBVOLUME_BIT; } else if(!ffStrStartsWith(fs->f_mntfromname, "/dev/")) - disk->type = FF_DISK_TYPE_HIDDEN_BIT; + disk->type = FF_DISK_VOLUME_TYPE_HIDDEN_BIT; else if(!(fs->f_flags & MNT_LOCAL)) - disk->type = FF_DISK_TYPE_EXTERNAL_BIT; + disk->type = FF_DISK_VOLUME_TYPE_EXTERNAL_BIT; else - disk->type = FF_DISK_TYPE_REGULAR_BIT; + disk->type = FF_DISK_VOLUME_TYPE_REGULAR_BIT; ffStrbufInit(&disk->name); } diff --git a/src/detection/disk/disk_linux.c b/src/detection/disk/disk_linux.c index 1d2e111b4..69a85ac64 100644 --- a/src/detection/disk/disk_linux.c +++ b/src/detection/disk/disk_linux.c @@ -155,11 +155,11 @@ static void detectName(FFDisk* disk, const FFstrbuf* device) static void detectType(FF_MAYBE_UNUSED const FFlist* devices, FFDisk* currentDisk, FF_MAYBE_UNUSED const char* options) { if(ffStrbufEqualS(¤tDisk->mountpoint, "/") || ffStrbufEqualS(¤tDisk->mountpoint, "/storage/emulated")) - currentDisk->type = FF_DISK_TYPE_REGULAR_BIT; + currentDisk->type = FF_DISK_VOLUME_TYPE_REGULAR_BIT; else if(ffStrbufStartsWithS(¤tDisk->mountpoint, "/mnt/media_rw/")) - currentDisk->type = FF_DISK_TYPE_EXTERNAL_BIT; + currentDisk->type = FF_DISK_VOLUME_TYPE_EXTERNAL_BIT; else - currentDisk->type = FF_DISK_TYPE_HIDDEN_BIT; + currentDisk->type = FF_DISK_VOLUME_TYPE_HIDDEN_BIT; } #else @@ -191,13 +191,13 @@ static bool isSubvolume(const FFlist* devices) static void detectType(const FFlist* devices, FFDisk* currentDisk, const char* options) { if(isSubvolume(devices)) - currentDisk->type = FF_DISK_TYPE_SUBVOLUME_BIT; + currentDisk->type = FF_DISK_VOLUME_TYPE_SUBVOLUME_BIT; else if(strstr(options, "nosuid") != NULL || strstr(options, "nodev") != NULL) - currentDisk->type = FF_DISK_TYPE_EXTERNAL_BIT; + currentDisk->type = FF_DISK_VOLUME_TYPE_EXTERNAL_BIT; else if(ffStrbufStartsWithS(¤tDisk->mountpoint, "/boot") || ffStrbufStartsWithS(¤tDisk->mountpoint, "/efi")) - currentDisk->type = FF_DISK_TYPE_HIDDEN_BIT; + currentDisk->type = FF_DISK_VOLUME_TYPE_HIDDEN_BIT; else - currentDisk->type = FF_DISK_TYPE_REGULAR_BIT; + currentDisk->type = FF_DISK_VOLUME_TYPE_REGULAR_BIT; } #endif diff --git a/src/detection/disk/disk_windows.c b/src/detection/disk/disk_windows.c index 472b9730f..ecda28889 100644 --- a/src/detection/disk/disk_windows.c +++ b/src/detection/disk/disk_windows.c @@ -34,11 +34,11 @@ const char* ffDetectDisksImpl(FFlist* disks) disk->bytesUsed = disk->bytesTotal - bytesFree; if(driveType == DRIVE_REMOVABLE || driveType == DRIVE_REMOTE || driveType == DRIVE_CDROM) - disk->type = FF_DISK_TYPE_EXTERNAL_BIT; + disk->type = FF_DISK_VOLUME_TYPE_EXTERNAL_BIT; else if(driveType == DRIVE_FIXED) - disk->type = FF_DISK_TYPE_REGULAR_BIT; + disk->type = FF_DISK_VOLUME_TYPE_REGULAR_BIT; else - disk->type = FF_DISK_TYPE_HIDDEN_BIT; + disk->type = FF_DISK_VOLUME_TYPE_HIDDEN_BIT; ffStrbufInit(&disk->filesystem); ffStrbufInit(&disk->name); diff --git a/src/modules/disk/disk.c b/src/modules/disk/disk.c index a6d8b98c6..8eed33b96 100644 --- a/src/modules/disk/disk.c +++ b/src/modules/disk/disk.c @@ -79,11 +79,11 @@ static void printDisk(FFDiskOptions* options, const FFDisk* disk) if(disk->filesystem.length) ffStrbufAppendF(&str, "- %s ", disk->filesystem.chars); - if(disk->type & FF_DISK_TYPE_EXTERNAL_BIT) + if(disk->type & FF_DISK_VOLUME_TYPE_EXTERNAL_BIT) ffStrbufAppendS(&str, "[External]"); - else if(disk->type & FF_DISK_TYPE_SUBVOLUME_BIT) + else if(disk->type & FF_DISK_VOLUME_TYPE_SUBVOLUME_BIT) ffStrbufAppendS(&str, "[Subvolume]"); - else if(disk->type & FF_DISK_TYPE_HIDDEN_BIT) + else if(disk->type & FF_DISK_VOLUME_TYPE_HIDDEN_BIT) ffStrbufAppendS(&str, "[Hidden]"); } @@ -98,8 +98,8 @@ static void printDisk(FFDiskOptions* options, const FFDisk* disk) double filesPercentage = disk->filesTotal > 0 ? ((double) disk->filesUsed / (double) disk->filesTotal) * 100.0 : 0; ffAppendPercentNum(&filesPercentageStr, filesPercentage, 50, 80, false); - bool isExternal = !!(disk->type & FF_DISK_TYPE_EXTERNAL_BIT); - bool isHidden = !!(disk->type & FF_DISK_TYPE_HIDDEN_BIT); + bool isExternal = !!(disk->type & FF_DISK_VOLUME_TYPE_EXTERNAL_BIT); + bool isHidden = !!(disk->type & FF_DISK_VOLUME_TYPE_HIDDEN_BIT); ffPrintFormatString(key.chars, 0, &options->moduleArgs, FF_PRINT_TYPE_NO_CUSTOM_KEY, FF_DISK_NUM_FORMAT_ARGS, (FFformatarg[]){ {FF_FORMAT_ARG_TYPE_STRBUF, &usedPretty}, {FF_FORMAT_ARG_TYPE_STRBUF, &totalPretty}, @@ -194,7 +194,7 @@ void ffInitDiskOptions(FFDiskOptions* options) ffOptionInitModuleArg(&options->moduleArgs); ffStrbufInit(&options->folders); - options->showTypes = FF_DISK_TYPE_REGULAR_BIT | FF_DISK_TYPE_EXTERNAL_BIT; + options->showTypes = FF_DISK_VOLUME_TYPE_REGULAR_BIT | FF_DISK_VOLUME_TYPE_EXTERNAL_BIT; } bool ffParseDiskCommandOptions(FFDiskOptions* options, const char* key, const char* value) @@ -213,45 +213,45 @@ bool ffParseDiskCommandOptions(FFDiskOptions* options, const char* key, const ch if (ffStrEqualsIgnCase(subKey, "show-regular")) { if (ffOptionParseBoolean(value)) - options->showTypes |= FF_DISK_TYPE_REGULAR_BIT; + options->showTypes |= FF_DISK_VOLUME_TYPE_REGULAR_BIT; else - options->showTypes &= ~FF_DISK_TYPE_REGULAR_BIT; + options->showTypes &= ~FF_DISK_VOLUME_TYPE_REGULAR_BIT; return true; } if (ffStrEqualsIgnCase(subKey, "show-external")) { if (ffOptionParseBoolean(value)) - options->showTypes |= FF_DISK_TYPE_EXTERNAL_BIT; + options->showTypes |= FF_DISK_VOLUME_TYPE_EXTERNAL_BIT; else - options->showTypes &= ~FF_DISK_TYPE_EXTERNAL_BIT; + options->showTypes &= ~FF_DISK_VOLUME_TYPE_EXTERNAL_BIT; return true; } if (ffStrEqualsIgnCase(subKey, "show-hidden")) { if (ffOptionParseBoolean(value)) - options->showTypes |= FF_DISK_TYPE_HIDDEN_BIT; + options->showTypes |= FF_DISK_VOLUME_TYPE_HIDDEN_BIT; else - options->showTypes &= ~FF_DISK_TYPE_HIDDEN_BIT; + options->showTypes &= ~FF_DISK_VOLUME_TYPE_HIDDEN_BIT; return true; } if (ffStrEqualsIgnCase(subKey, "show-subvolumes")) { if (ffOptionParseBoolean(value)) - options->showTypes |= FF_DISK_TYPE_SUBVOLUME_BIT; + options->showTypes |= FF_DISK_VOLUME_TYPE_SUBVOLUME_BIT; else - options->showTypes &= ~FF_DISK_TYPE_SUBVOLUME_BIT; + options->showTypes &= ~FF_DISK_VOLUME_TYPE_SUBVOLUME_BIT; return true; } if (ffStrEqualsIgnCase(subKey, "show-unknown")) { if (ffOptionParseBoolean(value)) - options->showTypes |= FF_DISK_TYPE_UNKNOWN_BIT; + options->showTypes |= FF_DISK_VOLUME_TYPE_UNKNOWN_BIT; else - options->showTypes &= ~FF_DISK_TYPE_UNKNOWN_BIT; + options->showTypes &= ~FF_DISK_VOLUME_TYPE_UNKNOWN_BIT; return true; } @@ -285,36 +285,36 @@ void ffParseDiskJsonObject(FFDiskOptions* options, yyjson_val* module) if (ffStrEqualsIgnCase(key, "showExternal")) { if (yyjson_get_bool(val)) - options->showTypes |= FF_DISK_TYPE_EXTERNAL_BIT; + options->showTypes |= FF_DISK_VOLUME_TYPE_EXTERNAL_BIT; else - options->showTypes &= ~FF_DISK_TYPE_EXTERNAL_BIT; + options->showTypes &= ~FF_DISK_VOLUME_TYPE_EXTERNAL_BIT; continue; } if (ffStrEqualsIgnCase(key, "showHidden")) { if (yyjson_get_bool(val)) - options->showTypes |= FF_DISK_TYPE_HIDDEN_BIT; + options->showTypes |= FF_DISK_VOLUME_TYPE_HIDDEN_BIT; else - options->showTypes &= ~FF_DISK_TYPE_HIDDEN_BIT; + options->showTypes &= ~FF_DISK_VOLUME_TYPE_HIDDEN_BIT; continue; } if (ffStrEqualsIgnCase(key, "showSubvolumes")) { if (yyjson_get_bool(val)) - options->showTypes |= FF_DISK_TYPE_SUBVOLUME_BIT; + options->showTypes |= FF_DISK_VOLUME_TYPE_SUBVOLUME_BIT; else - options->showTypes &= ~FF_DISK_TYPE_SUBVOLUME_BIT; + options->showTypes &= ~FF_DISK_VOLUME_TYPE_SUBVOLUME_BIT; continue; } if (ffStrEqualsIgnCase(key, "showUnknown")) { if (yyjson_get_bool(val)) - options->showTypes |= FF_DISK_TYPE_UNKNOWN_BIT; + options->showTypes |= FF_DISK_VOLUME_TYPE_UNKNOWN_BIT; else - options->showTypes &= ~FF_DISK_TYPE_UNKNOWN_BIT; + options->showTypes &= ~FF_DISK_VOLUME_TYPE_UNKNOWN_BIT; continue; } diff --git a/src/modules/disk/option.h b/src/modules/disk/option.h index 07e603cd1..4b0789ad0 100644 --- a/src/modules/disk/option.h +++ b/src/modules/disk/option.h @@ -4,15 +4,15 @@ #include "common/option.h" -typedef enum FFDiskType +typedef enum FFDiskVolumeType { - FF_DISK_TYPE_NONE = 0, - FF_DISK_TYPE_REGULAR_BIT = 1 << 0, - FF_DISK_TYPE_HIDDEN_BIT = 1 << 1, - FF_DISK_TYPE_EXTERNAL_BIT = 1 << 2, - FF_DISK_TYPE_SUBVOLUME_BIT = 1 << 3, - FF_DISK_TYPE_UNKNOWN_BIT = 1 << 4, -} FFDiskType; + FF_DISK_VOLUME_TYPE_NONE = 0, + FF_DISK_VOLUME_TYPE_REGULAR_BIT = 1 << 0, + FF_DISK_VOLUME_TYPE_HIDDEN_BIT = 1 << 1, + FF_DISK_VOLUME_TYPE_EXTERNAL_BIT = 1 << 2, + FF_DISK_VOLUME_TYPE_SUBVOLUME_BIT = 1 << 3, + FF_DISK_VOLUME_TYPE_UNKNOWN_BIT = 1 << 4, +} FFDiskVolumeType; typedef struct FFDiskOptions { @@ -20,5 +20,5 @@ typedef struct FFDiskOptions FFModuleArgs moduleArgs; FFstrbuf folders; - FFDiskType showTypes; + FFDiskVolumeType showTypes; } FFDiskOptions; From 7358326a134be1119e6b7526bfaf5d68da1de43e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Sat, 26 Aug 2023 10:36:00 +0800 Subject: [PATCH 02/10] Disk: add `--disk-use-available` fix #543 --- doc/json_schema.json | 5 +++++ src/data/help.txt | 1 + src/detection/disk/disk.c | 8 +++++++- src/detection/disk/disk.h | 4 +++- src/detection/disk/disk_bsd.c | 4 +++- src/detection/disk/disk_linux.c | 4 +++- src/detection/disk/disk_windows.c | 13 +++++++++---- src/modules/disk/disk.c | 21 ++++++++++++++++++++- src/modules/disk/option.h | 7 +++++++ 9 files changed, 58 insertions(+), 9 deletions(-) diff --git a/doc/json_schema.json b/doc/json_schema.json index 07061c18f..be2334b2e 100644 --- a/doc/json_schema.json +++ b/doc/json_schema.json @@ -889,6 +889,11 @@ "title": "Set if unknown (unable to detect sizes) volumes should be printed", "default": false }, + "useAvailable": { + "type": "boolean", + "title": "Use f_bavail (lpFreeBytesAvailableToCaller for Windows) instead of f_bfree to calculate used bytes", + "default": false + }, "key": { "$ref": "#/$defs/key" }, diff --git a/src/data/help.txt b/src/data/help.txt index 48a076e88..6c484d29f 100644 --- a/src/data/help.txt +++ b/src/data/help.txt @@ -130,6 +130,7 @@ Module specific options: --disk-show-hidden : Set if hidden volumes should be printed. Default is false --disk-show-subvolumes : Set if subvolumes should be printed. Default is false --disk-show-unknown : Set if unknown (unable to detect sizes) volumes should be printed. Default is false + --disk-use-available : Use f_bavail (lpFreeBytesAvailableToCaller for Windows) instead of f_bfree to calculate used bytes. Default is false --bluetooth-show-disconnected: : Set if disconnected bluetooth devices should be printed. Default is false --display-compact-type: : Set if all displays should be printed in one line. Default is none --display-detect-name: : Set if display name should be detected and printed (if supported). Default is false diff --git a/src/detection/disk/disk.c b/src/detection/disk/disk.c index 00eba85c0..3e92ca0eb 100644 --- a/src/detection/disk/disk.c +++ b/src/detection/disk/disk.c @@ -7,7 +7,7 @@ static int compareDisks(const void* disk1, const void* disk2) return ffStrbufCompAlphabetically(&((const FFDisk*) disk1)->mountpoint, &((const FFDisk*) disk2)->mountpoint); } -const char* ffDetectDisks(FFlist* disks) +const char* ffDetectDisks(FFDiskOptions* options, FFlist* disks) { const char* error = ffDetectDisksImpl(disks); @@ -23,6 +23,12 @@ const char* ffDetectDisks(FFlist* disks) { if(disk->bytesTotal == 0) disk->type |= FF_DISK_VOLUME_TYPE_UNKNOWN_BIT; + else + { + disk->bytesUsed = disk->bytesTotal - ( + options->calcType == FF_DISK_CALC_TYPE_FREE ? disk->bytesFree : disk->bytesAvailable + ); + } } return NULL; diff --git a/src/detection/disk/disk.h b/src/detection/disk/disk.h index 24c28c9cf..fced34a92 100644 --- a/src/detection/disk/disk.h +++ b/src/detection/disk/disk.h @@ -13,6 +13,8 @@ typedef struct FFDisk FFDiskVolumeType type; uint64_t bytesUsed; + uint64_t bytesFree; + uint64_t bytesAvailable; uint64_t bytesTotal; uint32_t filesUsed; @@ -23,6 +25,6 @@ typedef struct FFDisk * Returns a List of FFDisk, sorted alphabetically by mountpoint. * If error is not set, disks contains at least one disk. */ -const char* ffDetectDisks(FFlist* result /* list of FFDisk */); +const char* ffDetectDisks(FFDiskOptions* options, FFlist* result /* list of FFDisk */); #endif diff --git a/src/detection/disk/disk_bsd.c b/src/detection/disk/disk_bsd.c index 8701f60bd..89d66b4ad 100644 --- a/src/detection/disk/disk_bsd.c +++ b/src/detection/disk/disk_bsd.c @@ -45,7 +45,9 @@ const char* ffDetectDisksImpl(FFlist* disks) #endif disk->bytesTotal = fs->f_blocks * fs->f_bsize; - disk->bytesUsed = disk->bytesTotal - ((uint64_t)fs->f_bfree * fs->f_bsize); + disk->bytesFree = (uint64_t)fs->f_bfree * fs->f_bsize; + disk->bytesAvailable = (uint64_t)fs->f_bavail * fs->f_bsize; + disk->bytesUsed = 0; // To be filled in ./disk.c disk->filesTotal = (uint32_t) fs->f_files; disk->filesUsed = (uint32_t) (disk->filesTotal - (uint64_t)fs->f_ffree); diff --git a/src/detection/disk/disk_linux.c b/src/detection/disk/disk_linux.c index 69a85ac64..1f0f6acd3 100644 --- a/src/detection/disk/disk_linux.c +++ b/src/detection/disk/disk_linux.c @@ -209,7 +209,9 @@ static void detectStats(FFDisk* disk) memset(&fs, 0, sizeof(struct statvfs)); //Set all values to 0, so our values get initialized to 0 too disk->bytesTotal = fs.f_blocks * fs.f_frsize; - disk->bytesUsed = disk->bytesTotal - (fs.f_bfree * fs.f_frsize); + disk->bytesFree = fs.f_bfree * fs.f_frsize; + disk->bytesAvailable = fs.f_bavail * fs.f_frsize; + disk->bytesUsed = 0; // To be filled in ./disk.c disk->filesTotal = (uint32_t) fs.f_files; disk->filesUsed = (uint32_t) (disk->filesTotal - fs.f_ffree); diff --git a/src/detection/disk/disk_windows.c b/src/detection/disk/disk_windows.c index ecda28889..291d59ba6 100644 --- a/src/detection/disk/disk_windows.c +++ b/src/detection/disk/disk_windows.c @@ -25,13 +25,18 @@ const char* ffDetectDisksImpl(FFlist* disks) FFDisk* disk = ffListAdd(disks); ffStrbufInitWS(&disk->mountpoint, mountpoint); - uint64_t bytesFree; - if(!GetDiskFreeSpaceExW(mountpoint, NULL, (PULARGE_INTEGER)&disk->bytesTotal, (PULARGE_INTEGER)&bytesFree)) + if(!GetDiskFreeSpaceExW( + mountpoint, + (PULARGE_INTEGER)&disk->bytesAvailable, + (PULARGE_INTEGER)&disk->bytesTotal, + (PULARGE_INTEGER)&disk->bytesFree + )) { disk->bytesTotal = 0; - bytesFree = 0; + disk->bytesFree = 0; + disk->bytesAvailable = 0; } - disk->bytesUsed = disk->bytesTotal - bytesFree; + disk->bytesUsed = 0; // To be filled in ./disk.c if(driveType == DRIVE_REMOVABLE || driveType == DRIVE_REMOTE || driveType == DRIVE_CDROM) disk->type = FF_DISK_VOLUME_TYPE_EXTERNAL_BIT; diff --git a/src/modules/disk/disk.c b/src/modules/disk/disk.c index 8eed33b96..724061176 100644 --- a/src/modules/disk/disk.c +++ b/src/modules/disk/disk.c @@ -166,7 +166,7 @@ static void printAutodetected(FFDiskOptions* options, const FFlist* disks) void ffPrintDisk(FFDiskOptions* options) { FF_LIST_AUTO_DESTROY disks = ffListCreate(sizeof (FFDisk)); - const char* error = ffDetectDisks(&disks); + const char* error = ffDetectDisks(options, &disks); if(error) { @@ -195,6 +195,7 @@ void ffInitDiskOptions(FFDiskOptions* options) ffStrbufInit(&options->folders); options->showTypes = FF_DISK_VOLUME_TYPE_REGULAR_BIT | FF_DISK_VOLUME_TYPE_EXTERNAL_BIT; + options->calcType = FF_DISK_CALC_TYPE_FREE; } bool ffParseDiskCommandOptions(FFDiskOptions* options, const char* key, const char* value) @@ -255,6 +256,15 @@ bool ffParseDiskCommandOptions(FFDiskOptions* options, const char* key, const ch return true; } + if (ffStrEqualsIgnCase(subKey, "use-available")) + { + if (ffOptionParseBoolean(value)) + options->calcType = FF_DISK_CALC_TYPE_AVAILABLE; + else + options->calcType = FF_DISK_CALC_TYPE_FREE; + return true; + } + return false; } @@ -318,6 +328,15 @@ void ffParseDiskJsonObject(FFDiskOptions* options, yyjson_val* module) continue; } + if (ffStrEqualsIgnCase(key, "useAvailable")) + { + if (yyjson_get_bool(val)) + options->calcType = FF_DISK_CALC_TYPE_AVAILABLE; + else + options->calcType = FF_DISK_CALC_TYPE_FREE; + continue; + } + ffPrintError(FF_DISK_MODULE_NAME, 0, &options->moduleArgs, "Unknown JSON key %s", key); } } diff --git a/src/modules/disk/option.h b/src/modules/disk/option.h index 4b0789ad0..f24090c34 100644 --- a/src/modules/disk/option.h +++ b/src/modules/disk/option.h @@ -14,6 +14,12 @@ typedef enum FFDiskVolumeType FF_DISK_VOLUME_TYPE_UNKNOWN_BIT = 1 << 4, } FFDiskVolumeType; +typedef enum FFDiskCalcType +{ + FF_DISK_CALC_TYPE_FREE, + FF_DISK_CALC_TYPE_AVAILABLE, +} FFDiskCalcType; + typedef struct FFDiskOptions { FFModuleBaseInfo moduleInfo; @@ -21,4 +27,5 @@ typedef struct FFDiskOptions FFstrbuf folders; FFDiskVolumeType showTypes; + FFDiskCalcType calcType; } FFDiskOptions; From 32d64360275153d53bab3498a02b8c1f7626f9e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Sat, 26 Aug 2023 12:50:45 +0800 Subject: [PATCH 03/10] Disk: add `--disk-show-readonly` --- doc/json_schema.json | 5 +++++ src/data/help.txt | 1 + src/detection/disk/disk_bsd.c | 3 +++ src/detection/disk/disk_linux.c | 3 +++ src/detection/disk/disk_windows.c | 5 ++++- src/modules/disk/disk.c | 26 +++++++++++++++++++++++--- src/modules/disk/option.h | 1 + 7 files changed, 40 insertions(+), 4 deletions(-) diff --git a/doc/json_schema.json b/doc/json_schema.json index be2334b2e..60c3c5259 100644 --- a/doc/json_schema.json +++ b/doc/json_schema.json @@ -884,6 +884,11 @@ "title": "Set if subvolumes should be printed", "default": false }, + "showReadOnly": { + "type": "boolean", + "title": "Set if read only volumes should be printed", + "default": false + }, "showUnknown": { "type": "boolean", "title": "Set if unknown (unable to detect sizes) volumes should be printed", diff --git a/src/data/help.txt b/src/data/help.txt index 6c484d29f..eb975f786 100644 --- a/src/data/help.txt +++ b/src/data/help.txt @@ -129,6 +129,7 @@ Module specific options: --disk-show-external : Set if external volume should be printed. Default is true --disk-show-hidden : Set if hidden volumes should be printed. Default is false --disk-show-subvolumes : Set if subvolumes should be printed. Default is false + --disk-show-readonly : Set if read only volumes should be printed. Default is false --disk-show-unknown : Set if unknown (unable to detect sizes) volumes should be printed. Default is false --disk-use-available : Use f_bavail (lpFreeBytesAvailableToCaller for Windows) instead of f_bfree to calculate used bytes. Default is false --bluetooth-show-disconnected: : Set if disconnected bluetooth devices should be printed. Default is false diff --git a/src/detection/disk/disk_bsd.c b/src/detection/disk/disk_bsd.c index 89d66b4ad..04168484b 100644 --- a/src/detection/disk/disk_bsd.c +++ b/src/detection/disk/disk_bsd.c @@ -55,6 +55,9 @@ const char* ffDetectDisksImpl(FFlist* disks) ffStrbufInitS(&disk->mountpoint, fs->f_mntonname); ffStrbufInitS(&disk->filesystem, fs->f_fstypename); detectFsInfo(fs, disk); + + if(fs->f_flags & MNT_EXRDONLY) + disk->type |= FF_DISK_VOLUME_TYPE_READONLY_BIT; } return NULL; diff --git a/src/detection/disk/disk_linux.c b/src/detection/disk/disk_linux.c index 1f0f6acd3..fb5597baa 100644 --- a/src/detection/disk/disk_linux.c +++ b/src/detection/disk/disk_linux.c @@ -215,6 +215,9 @@ static void detectStats(FFDisk* disk) disk->filesTotal = (uint32_t) fs.f_files; disk->filesUsed = (uint32_t) (disk->filesTotal - fs.f_ffree); + + if(fs.f_flag & ST_RDONLY) + disk->type |= FF_DISK_VOLUME_TYPE_READONLY_BIT; } const char* ffDetectDisksImpl(FFlist* disks) diff --git a/src/detection/disk/disk_windows.c b/src/detection/disk/disk_windows.c index 291d59ba6..5b3b39232 100644 --- a/src/detection/disk/disk_windows.c +++ b/src/detection/disk/disk_windows.c @@ -52,11 +52,12 @@ const char* ffDetectDisksImpl(FFlist* disks) //https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-getvolumeinformationa#remarks UINT errorMode = SetErrorMode(SEM_FAILCRITICALERRORS); + DWORD diskFlags; BOOL result = GetVolumeInformationW(mountpoint, diskName, sizeof(diskName) / sizeof(*diskName), //Volume name NULL, //Serial number NULL, //Max component length - NULL, //File system flags + &diskFlags, //File system flags diskFileSystem, sizeof(diskFileSystem) / sizeof(*diskFileSystem) ); SetErrorMode(errorMode); @@ -65,6 +66,8 @@ const char* ffDetectDisksImpl(FFlist* disks) { ffStrbufSetWS(&disk->filesystem, diskFileSystem); ffStrbufSetWS(&disk->name, diskName); + if(diskFlags & FILE_READ_ONLY_VOLUME) + disk->type |= FF_DISK_VOLUME_TYPE_READONLY_BIT; } //Unsupported diff --git a/src/modules/disk/disk.c b/src/modules/disk/disk.c index 724061176..7d8460163 100644 --- a/src/modules/disk/disk.c +++ b/src/modules/disk/disk.c @@ -100,6 +100,7 @@ static void printDisk(FFDiskOptions* options, const FFDisk* disk) bool isExternal = !!(disk->type & FF_DISK_VOLUME_TYPE_EXTERNAL_BIT); bool isHidden = !!(disk->type & FF_DISK_VOLUME_TYPE_HIDDEN_BIT); + bool isReadOnly = !!(disk->type & FF_DISK_VOLUME_TYPE_READONLY_BIT); ffPrintFormatString(key.chars, 0, &options->moduleArgs, FF_PRINT_TYPE_NO_CUSTOM_KEY, FF_DISK_NUM_FORMAT_ARGS, (FFformatarg[]){ {FF_FORMAT_ARG_TYPE_STRBUF, &usedPretty}, {FF_FORMAT_ARG_TYPE_STRBUF, &totalPretty}, @@ -110,7 +111,8 @@ static void printDisk(FFDiskOptions* options, const FFDisk* disk) {FF_FORMAT_ARG_TYPE_BOOL, &isExternal}, {FF_FORMAT_ARG_TYPE_BOOL, &isHidden}, {FF_FORMAT_ARG_TYPE_STRBUF, &disk->filesystem}, - {FF_FORMAT_ARG_TYPE_STRBUF, &disk->name} + {FF_FORMAT_ARG_TYPE_STRBUF, &disk->name}, + {FF_FORMAT_ARG_TYPE_BOOL, &isReadOnly}, }); } } @@ -156,7 +158,7 @@ static void printAutodetected(FFDiskOptions* options, const FFlist* disks) { FF_LIST_FOR_EACH(FFDisk, disk, *disks) { - if(!(disk->type & options->showTypes)) + if(disk->type & ~options->showTypes) continue; printDisk(options, disk); @@ -194,7 +196,7 @@ void ffInitDiskOptions(FFDiskOptions* options) ffOptionInitModuleArg(&options->moduleArgs); ffStrbufInit(&options->folders); - options->showTypes = FF_DISK_VOLUME_TYPE_REGULAR_BIT | FF_DISK_VOLUME_TYPE_EXTERNAL_BIT; + options->showTypes = FF_DISK_VOLUME_TYPE_REGULAR_BIT | FF_DISK_VOLUME_TYPE_EXTERNAL_BIT | FF_DISK_VOLUME_TYPE_READONLY_BIT; options->calcType = FF_DISK_CALC_TYPE_FREE; } @@ -247,6 +249,15 @@ bool ffParseDiskCommandOptions(FFDiskOptions* options, const char* key, const ch return true; } + if (ffStrEqualsIgnCase(subKey, "show-readonly")) + { + if (ffOptionParseBoolean(value)) + options->showTypes |= FF_DISK_VOLUME_TYPE_READONLY_BIT; + else + options->showTypes &= ~FF_DISK_VOLUME_TYPE_READONLY_BIT; + return true; + } + if (ffStrEqualsIgnCase(subKey, "show-unknown")) { if (ffOptionParseBoolean(value)) @@ -319,6 +330,15 @@ void ffParseDiskJsonObject(FFDiskOptions* options, yyjson_val* module) continue; } + if (ffStrEqualsIgnCase(key, "showReadOnly")) + { + if (yyjson_get_bool(val)) + options->showTypes |= FF_DISK_VOLUME_TYPE_READONLY_BIT; + else + options->showTypes &= ~FF_DISK_VOLUME_TYPE_READONLY_BIT; + continue; + } + if (ffStrEqualsIgnCase(key, "showUnknown")) { if (yyjson_get_bool(val)) diff --git a/src/modules/disk/option.h b/src/modules/disk/option.h index f24090c34..9b727e542 100644 --- a/src/modules/disk/option.h +++ b/src/modules/disk/option.h @@ -12,6 +12,7 @@ typedef enum FFDiskVolumeType FF_DISK_VOLUME_TYPE_EXTERNAL_BIT = 1 << 2, FF_DISK_VOLUME_TYPE_SUBVOLUME_BIT = 1 << 3, FF_DISK_VOLUME_TYPE_UNKNOWN_BIT = 1 << 4, + FF_DISK_VOLUME_TYPE_READONLY_BIT = 1 << 5, } FFDiskVolumeType; typedef enum FFDiskCalcType From 2f96c847b932d7e39a28acef6ac99530c88951ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Sat, 26 Aug 2023 12:59:30 +0800 Subject: [PATCH 04/10] Disk: print `Read-only` --- src/detection/disk/disk_bsd.c | 2 +- src/modules/disk/disk.c | 20 +++++++++++++++----- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/src/detection/disk/disk_bsd.c b/src/detection/disk/disk_bsd.c index 04168484b..b6c656e8b 100644 --- a/src/detection/disk/disk_bsd.c +++ b/src/detection/disk/disk_bsd.c @@ -56,7 +56,7 @@ const char* ffDetectDisksImpl(FFlist* disks) ffStrbufInitS(&disk->filesystem, fs->f_fstypename); detectFsInfo(fs, disk); - if(fs->f_flags & MNT_EXRDONLY) + if(fs->f_flags & MNT_RDONLY) disk->type |= FF_DISK_VOLUME_TYPE_READONLY_BIT; } diff --git a/src/modules/disk/disk.c b/src/modules/disk/disk.c index 7d8460163..202d8e1a8 100644 --- a/src/modules/disk/disk.c +++ b/src/modules/disk/disk.c @@ -79,12 +79,22 @@ static void printDisk(FFDiskOptions* options, const FFDisk* disk) if(disk->filesystem.length) ffStrbufAppendF(&str, "- %s ", disk->filesystem.chars); + ffStrbufAppendC(&str, '['); if(disk->type & FF_DISK_VOLUME_TYPE_EXTERNAL_BIT) - ffStrbufAppendS(&str, "[External]"); - else if(disk->type & FF_DISK_VOLUME_TYPE_SUBVOLUME_BIT) - ffStrbufAppendS(&str, "[Subvolume]"); - else if(disk->type & FF_DISK_VOLUME_TYPE_HIDDEN_BIT) - ffStrbufAppendS(&str, "[Hidden]"); + ffStrbufAppendS(&str, "External, "); + if(disk->type & FF_DISK_VOLUME_TYPE_SUBVOLUME_BIT) + ffStrbufAppendS(&str, "Subvolume, "); + if(disk->type & FF_DISK_VOLUME_TYPE_HIDDEN_BIT) + ffStrbufAppendS(&str, "Hidden, "); + if(disk->type & FF_DISK_VOLUME_TYPE_READONLY_BIT) + ffStrbufAppendS(&str, "Read-only, "); + if (str.chars[str.length - 1] == '[') + ffStrbufSubstrBefore(&str, str.length - 1); + else + { + ffStrbufTrimRight(&str, ' '); + str.chars[str.length - 1] = ']'; + } } ffStrbufTrimRight(&str, ' '); From e51999edd14f5db62ef1f264c014f09469a08988 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Sat, 26 Aug 2023 13:40:11 +0800 Subject: [PATCH 05/10] Doc: update changelog --- CHANGELOG.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2870f05ed..74c29baf2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +# dev + +Features: +* Add option `--disk-use-available` (#543) +* Add option `--disk-show-readonly` + # 2.0.4 Bugfixes: From 5259aeb039d95c60db5b842b8ba878f1d1005c9f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Sat, 26 Aug 2023 05:27:31 -0400 Subject: [PATCH 06/10] Display: fix segfault when using libxrandr Fix #544 --- src/detection/displayserver/linux/xlib.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/detection/displayserver/linux/xlib.c b/src/detection/displayserver/linux/xlib.c index 19b6fee37..8ff3776f1 100644 --- a/src/detection/displayserver/linux/xlib.c +++ b/src/detection/displayserver/linux/xlib.c @@ -116,6 +116,7 @@ typedef struct XrandrData { FF_LIBRARY_SYMBOL(XInternAtom) FF_LIBRARY_SYMBOL(XGetAtomName); + FF_LIBRARY_SYMBOL(XFree); FF_LIBRARY_SYMBOL(XRRGetScreenInfo) FF_LIBRARY_SYMBOL(XRRConfigCurrentConfiguration) FF_LIBRARY_SYMBOL(XRRConfigCurrentRate) @@ -207,9 +208,11 @@ static bool xrandrHandleOutput(XrandrData* data, RROutput output, FFstrbuf* name Atom atomEdid = data->ffXInternAtom(data->display, "EDID", true); if (atomEdid != None) { - unsigned long nitems = 0; + int actual_format = 0; + unsigned long nitems = 0, bytes_after = 0; + Atom actual_type = None; uint8_t* edidData = NULL; - if (data->ffXRRGetOutputProperty(data->display, output, atomEdid, 0, 100, 0, 0, AnyPropertyType, NULL, NULL, &nitems, NULL, &edidData) == Success) + if (data->ffXRRGetOutputProperty(data->display, output, atomEdid, 0, 100, false, false, AnyPropertyType, &actual_type, &actual_format, &nitems, &bytes_after, &edidData) == Success) { if (nitems >= 128) { @@ -228,8 +231,9 @@ static bool xrandrHandleOutput(XrandrData* data, RROutput output, FFstrbuf* name static bool xrandrHandleMonitor(XrandrData* data, XRRMonitorInfo* monitorInfo) { bool foundOutput = false; - - FF_STRBUF_AUTO_DESTROY name = ffStrbufCreateS(data->ffXGetAtomName(data->display, monitorInfo->name)); + char* xname = data->ffXGetAtomName(data->display, monitorInfo->name); + FF_STRBUF_AUTO_DESTROY name = ffStrbufCreateS(xname); + data->ffXFree(xname); for(int i = 0; i < monitorInfo->noutput; i++) { if(xrandrHandleOutput(data, monitorInfo->outputs[i], &name, monitorInfo->primary)) @@ -341,6 +345,7 @@ void ffdsConnectXrandr(FFDisplayServerResult* result) FF_LIBRARY_LOAD_SYMBOL_VAR(xrandr, data, XInternAtom,); FF_LIBRARY_LOAD_SYMBOL_VAR(xrandr, data, XGetAtomName,); + FF_LIBRARY_LOAD_SYMBOL_VAR(xrandr, data, XFree,); FF_LIBRARY_LOAD_SYMBOL_VAR(xrandr, data, XRRGetScreenInfo,) FF_LIBRARY_LOAD_SYMBOL_VAR(xrandr, data, XRRConfigCurrentRate,); FF_LIBRARY_LOAD_SYMBOL_VAR(xrandr, data, XRRConfigCurrentConfiguration,); From ce2f2f4416c5dcfd40a21d8575be9438ca27416d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Sat, 26 Aug 2023 05:31:00 -0400 Subject: [PATCH 07/10] Doc: update changelog --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 74c29baf2..1a50660f8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ # dev +Bugfixes: +* Fix segfault when using libxrandr (#544, Display, Linux) + Features: * Add option `--disk-use-available` (#543) * Add option `--disk-show-readonly` From 0534882f4eb5fd16209da800acacfe0264ddcd2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Sat, 26 Aug 2023 05:43:15 -0400 Subject: [PATCH 08/10] Cursor: Don't print 0px --- CHANGELOG.md | 3 ++- src/modules/cursor/cursor.c | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1a50660f8..9defef35e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,8 @@ -# dev +# 2.0.5 Bugfixes: * Fix segfault when using libxrandr (#544, Display, Linux) +* Don't print 0px (#544, Cursor) Features: * Add option `--disk-use-available` (#543) diff --git a/src/modules/cursor/cursor.c b/src/modules/cursor/cursor.c index ffe91f10f..c33e13d26 100644 --- a/src/modules/cursor/cursor.c +++ b/src/modules/cursor/cursor.c @@ -31,7 +31,7 @@ void ffPrintCursor(FFCursorOptions* options) ffPrintLogoAndKey(FF_CURSOR_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT); ffStrbufWriteTo(&result.theme, stdout); - if(result.size.length > 0) + if(result.size.length > 0 && !ffStrbufEqualS(&result.size, "0")) printf(" (%spx)", result.size.chars); putchar('\n'); From 2f14d7742e3b91615d5de5129b1882758c86635b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Sat, 26 Aug 2023 05:52:06 -0400 Subject: [PATCH 09/10] Display (Linux): fix memleaks --- src/detection/displayserver/linux/xlib.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/detection/displayserver/linux/xlib.c b/src/detection/displayserver/linux/xlib.c index 8ff3776f1..4c9333d88 100644 --- a/src/detection/displayserver/linux/xlib.c +++ b/src/detection/displayserver/linux/xlib.c @@ -220,6 +220,8 @@ static bool xrandrHandleOutput(XrandrData* data, RROutput output, FFstrbuf* name ffEdidGetName(edidData, name); } } + if (edidData) + data->ffXFree(edidData); } bool res = xrandrHandleCrtc(data, outputInfo->crtc, name, primary); From 55444b9c4eddcb8a5b526da070d943e0a0846da7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Sat, 26 Aug 2023 08:16:06 -0400 Subject: [PATCH 10/10] Release v2.0.5 --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index d1d41f204..a799086ba 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,7 +1,7 @@ cmake_minimum_required(VERSION 3.12.0) # target_link_libraries with OBJECT libs & project homepage url project(fastfetch - VERSION 2.0.4 + VERSION 2.0.5 LANGUAGES C DESCRIPTION "Fast system information tool" HOMEPAGE_URL "https://github.com/fastfetch-cli/fastfetch"