Disk: add --disk-use-available

fix #543
This commit is contained in:
李通洲
2023-08-26 10:36:00 +08:00
parent 434a879538
commit 7358326a13
9 changed files with 58 additions and 9 deletions
+5
View File
@@ -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"
},
+1
View File
@@ -130,6 +130,7 @@ Module specific options:
--disk-show-hidden <?value>: Set if hidden volumes should be printed. Default is false
--disk-show-subvolumes <?value>: Set if subvolumes should be printed. Default is false
--disk-show-unknown <?value>: Set if unknown (unable to detect sizes) volumes should be printed. Default is false
--disk-use-available <?value>: Use f_bavail (lpFreeBytesAvailableToCaller for Windows) instead of f_bfree to calculate used bytes. Default is false
--bluetooth-show-disconnected: <?value>: Set if disconnected bluetooth devices should be printed. Default is false
--display-compact-type: <?string>: Set if all displays should be printed in one line. Default is none
--display-detect-name: <?value>: Set if display name should be detected and printed (if supported). Default is false
+7 -1
View File
@@ -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;
+3 -1
View File
@@ -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
+3 -1
View File
@@ -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);
+3 -1
View File
@@ -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);
+9 -4
View File
@@ -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;
+20 -1
View File
@@ -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);
}
}
+7
View File
@@ -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;