Skip to content

Commit 7358326

Browse files
committed
Disk: add --disk-use-available
fix #543
1 parent 434a879 commit 7358326

File tree

9 files changed

+58
-9
lines changed

9 files changed

+58
-9
lines changed

doc/json_schema.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -889,6 +889,11 @@
889889
"title": "Set if unknown (unable to detect sizes) volumes should be printed",
890890
"default": false
891891
},
892+
"useAvailable": {
893+
"type": "boolean",
894+
"title": "Use f_bavail (lpFreeBytesAvailableToCaller for Windows) instead of f_bfree to calculate used bytes",
895+
"default": false
896+
},
892897
"key": {
893898
"$ref": "#/$defs/key"
894899
},

src/data/help.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ Module specific options:
130130
--disk-show-hidden <?value>: Set if hidden volumes should be printed. Default is false
131131
--disk-show-subvolumes <?value>: Set if subvolumes should be printed. Default is false
132132
--disk-show-unknown <?value>: Set if unknown (unable to detect sizes) volumes should be printed. Default is false
133+
--disk-use-available <?value>: Use f_bavail (lpFreeBytesAvailableToCaller for Windows) instead of f_bfree to calculate used bytes. Default is false
133134
--bluetooth-show-disconnected: <?value>: Set if disconnected bluetooth devices should be printed. Default is false
134135
--display-compact-type: <?string>: Set if all displays should be printed in one line. Default is none
135136
--display-detect-name: <?value>: Set if display name should be detected and printed (if supported). Default is false

src/detection/disk/disk.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ static int compareDisks(const void* disk1, const void* disk2)
77
return ffStrbufCompAlphabetically(&((const FFDisk*) disk1)->mountpoint, &((const FFDisk*) disk2)->mountpoint);
88
}
99

10-
const char* ffDetectDisks(FFlist* disks)
10+
const char* ffDetectDisks(FFDiskOptions* options, FFlist* disks)
1111
{
1212
const char* error = ffDetectDisksImpl(disks);
1313

@@ -23,6 +23,12 @@ const char* ffDetectDisks(FFlist* disks)
2323
{
2424
if(disk->bytesTotal == 0)
2525
disk->type |= FF_DISK_VOLUME_TYPE_UNKNOWN_BIT;
26+
else
27+
{
28+
disk->bytesUsed = disk->bytesTotal - (
29+
options->calcType == FF_DISK_CALC_TYPE_FREE ? disk->bytesFree : disk->bytesAvailable
30+
);
31+
}
2632
}
2733

2834
return NULL;

src/detection/disk/disk.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ typedef struct FFDisk
1313
FFDiskVolumeType type;
1414

1515
uint64_t bytesUsed;
16+
uint64_t bytesFree;
17+
uint64_t bytesAvailable;
1618
uint64_t bytesTotal;
1719

1820
uint32_t filesUsed;
@@ -23,6 +25,6 @@ typedef struct FFDisk
2325
* Returns a List of FFDisk, sorted alphabetically by mountpoint.
2426
* If error is not set, disks contains at least one disk.
2527
*/
26-
const char* ffDetectDisks(FFlist* result /* list of FFDisk */);
28+
const char* ffDetectDisks(FFDiskOptions* options, FFlist* result /* list of FFDisk */);
2729

2830
#endif

src/detection/disk/disk_bsd.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ const char* ffDetectDisksImpl(FFlist* disks)
4545
#endif
4646

4747
disk->bytesTotal = fs->f_blocks * fs->f_bsize;
48-
disk->bytesUsed = disk->bytesTotal - ((uint64_t)fs->f_bfree * fs->f_bsize);
48+
disk->bytesFree = (uint64_t)fs->f_bfree * fs->f_bsize;
49+
disk->bytesAvailable = (uint64_t)fs->f_bavail * fs->f_bsize;
50+
disk->bytesUsed = 0; // To be filled in ./disk.c
4951

5052
disk->filesTotal = (uint32_t) fs->f_files;
5153
disk->filesUsed = (uint32_t) (disk->filesTotal - (uint64_t)fs->f_ffree);

src/detection/disk/disk_linux.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,9 @@ static void detectStats(FFDisk* disk)
209209
memset(&fs, 0, sizeof(struct statvfs)); //Set all values to 0, so our values get initialized to 0 too
210210

211211
disk->bytesTotal = fs.f_blocks * fs.f_frsize;
212-
disk->bytesUsed = disk->bytesTotal - (fs.f_bfree * fs.f_frsize);
212+
disk->bytesFree = fs.f_bfree * fs.f_frsize;
213+
disk->bytesAvailable = fs.f_bavail * fs.f_frsize;
214+
disk->bytesUsed = 0; // To be filled in ./disk.c
213215

214216
disk->filesTotal = (uint32_t) fs.f_files;
215217
disk->filesUsed = (uint32_t) (disk->filesTotal - fs.f_ffree);

src/detection/disk/disk_windows.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,18 @@ const char* ffDetectDisksImpl(FFlist* disks)
2525
FFDisk* disk = ffListAdd(disks);
2626
ffStrbufInitWS(&disk->mountpoint, mountpoint);
2727

28-
uint64_t bytesFree;
29-
if(!GetDiskFreeSpaceExW(mountpoint, NULL, (PULARGE_INTEGER)&disk->bytesTotal, (PULARGE_INTEGER)&bytesFree))
28+
if(!GetDiskFreeSpaceExW(
29+
mountpoint,
30+
(PULARGE_INTEGER)&disk->bytesAvailable,
31+
(PULARGE_INTEGER)&disk->bytesTotal,
32+
(PULARGE_INTEGER)&disk->bytesFree
33+
))
3034
{
3135
disk->bytesTotal = 0;
32-
bytesFree = 0;
36+
disk->bytesFree = 0;
37+
disk->bytesAvailable = 0;
3338
}
34-
disk->bytesUsed = disk->bytesTotal - bytesFree;
39+
disk->bytesUsed = 0; // To be filled in ./disk.c
3540

3641
if(driveType == DRIVE_REMOVABLE || driveType == DRIVE_REMOTE || driveType == DRIVE_CDROM)
3742
disk->type = FF_DISK_VOLUME_TYPE_EXTERNAL_BIT;

src/modules/disk/disk.c

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ static void printAutodetected(FFDiskOptions* options, const FFlist* disks)
166166
void ffPrintDisk(FFDiskOptions* options)
167167
{
168168
FF_LIST_AUTO_DESTROY disks = ffListCreate(sizeof (FFDisk));
169-
const char* error = ffDetectDisks(&disks);
169+
const char* error = ffDetectDisks(options, &disks);
170170

171171
if(error)
172172
{
@@ -195,6 +195,7 @@ void ffInitDiskOptions(FFDiskOptions* options)
195195

196196
ffStrbufInit(&options->folders);
197197
options->showTypes = FF_DISK_VOLUME_TYPE_REGULAR_BIT | FF_DISK_VOLUME_TYPE_EXTERNAL_BIT;
198+
options->calcType = FF_DISK_CALC_TYPE_FREE;
198199
}
199200

200201
bool ffParseDiskCommandOptions(FFDiskOptions* options, const char* key, const char* value)
@@ -255,6 +256,15 @@ bool ffParseDiskCommandOptions(FFDiskOptions* options, const char* key, const ch
255256
return true;
256257
}
257258

259+
if (ffStrEqualsIgnCase(subKey, "use-available"))
260+
{
261+
if (ffOptionParseBoolean(value))
262+
options->calcType = FF_DISK_CALC_TYPE_AVAILABLE;
263+
else
264+
options->calcType = FF_DISK_CALC_TYPE_FREE;
265+
return true;
266+
}
267+
258268
return false;
259269
}
260270

@@ -318,6 +328,15 @@ void ffParseDiskJsonObject(FFDiskOptions* options, yyjson_val* module)
318328
continue;
319329
}
320330

331+
if (ffStrEqualsIgnCase(key, "useAvailable"))
332+
{
333+
if (yyjson_get_bool(val))
334+
options->calcType = FF_DISK_CALC_TYPE_AVAILABLE;
335+
else
336+
options->calcType = FF_DISK_CALC_TYPE_FREE;
337+
continue;
338+
}
339+
321340
ffPrintError(FF_DISK_MODULE_NAME, 0, &options->moduleArgs, "Unknown JSON key %s", key);
322341
}
323342
}

src/modules/disk/option.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,18 @@ typedef enum FFDiskVolumeType
1414
FF_DISK_VOLUME_TYPE_UNKNOWN_BIT = 1 << 4,
1515
} FFDiskVolumeType;
1616

17+
typedef enum FFDiskCalcType
18+
{
19+
FF_DISK_CALC_TYPE_FREE,
20+
FF_DISK_CALC_TYPE_AVAILABLE,
21+
} FFDiskCalcType;
22+
1723
typedef struct FFDiskOptions
1824
{
1925
FFModuleBaseInfo moduleInfo;
2026
FFModuleArgs moduleArgs;
2127

2228
FFstrbuf folders;
2329
FFDiskVolumeType showTypes;
30+
FFDiskCalcType calcType;
2431
} FFDiskOptions;

0 commit comments

Comments
 (0)