2023-04-02 00:13:11 +08:00
|
|
|
#include "common/printing.h"
|
2023-06-10 15:01:54 +08:00
|
|
|
#include "common/jsonconfig.h"
|
2023-04-02 00:13:11 +08:00
|
|
|
#include "common/parsing.h"
|
2024-01-20 11:28:38 +08:00
|
|
|
#include "common/percent.h"
|
2024-04-09 20:37:43 +08:00
|
|
|
#include "common/time.h"
|
2023-04-02 00:13:11 +08:00
|
|
|
#include "detection/disk/disk.h"
|
|
|
|
|
#include "modules/disk/disk.h"
|
2023-06-19 15:21:49 +08:00
|
|
|
#include "util/stringUtils.h"
|
2023-04-02 00:13:11 +08:00
|
|
|
|
2024-07-08 15:21:12 +08:00
|
|
|
#define FF_DISK_NUM_FORMAT_ARGS 14
|
2023-04-02 00:13:11 +08:00
|
|
|
#pragma GCC diagnostic ignored "-Wsign-conversion"
|
|
|
|
|
|
2023-06-24 10:59:53 +08:00
|
|
|
static void printDisk(FFDiskOptions* options, const FFDisk* disk)
|
2023-04-02 00:13:11 +08:00
|
|
|
{
|
2023-04-15 15:29:54 +08:00
|
|
|
FF_STRBUF_AUTO_DESTROY key = ffStrbufCreate();
|
2023-04-02 00:13:11 +08:00
|
|
|
|
|
|
|
|
if(options->moduleArgs.key.length == 0)
|
|
|
|
|
{
|
2023-10-26 10:26:15 +08:00
|
|
|
if(instance.config.display.pipe)
|
2023-07-16 23:00:39 +08:00
|
|
|
ffStrbufAppendF(&key, "%s (%s)", FF_DISK_MODULE_NAME, disk->mountpoint.chars);
|
|
|
|
|
else
|
2023-07-31 20:29:33 +08:00
|
|
|
{
|
|
|
|
|
#ifdef __linux__
|
|
|
|
|
if (getenv("WSL_DISTRO_NAME") != NULL && getenv("WT_SESSION") != NULL)
|
|
|
|
|
{
|
|
|
|
|
if (ffStrbufEqualS(&disk->filesystem, "9p") && ffStrbufStartsWithS(&disk->mountpoint, "/mnt/"))
|
|
|
|
|
ffStrbufAppendF(&key, "%s (\e]8;;file:///%c:/\e\\%s\e]8;;\e\\)", FF_DISK_MODULE_NAME, disk->mountpoint.chars[5], disk->mountpoint.chars);
|
|
|
|
|
else
|
|
|
|
|
ffStrbufAppendF(&key, "%s (\e]8;;file:////wsl.localhost/%s%s\e\\%s\e]8;;\e\\)", FF_DISK_MODULE_NAME, getenv("WSL_DISTRO_NAME"), disk->mountpoint.chars, disk->mountpoint.chars);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
#endif
|
2023-07-16 23:00:39 +08:00
|
|
|
ffStrbufAppendF(&key, "%s (\e]8;;file://%s\e\\%s\e]8;;\e\\)", FF_DISK_MODULE_NAME, disk->mountpoint.chars, disk->mountpoint.chars);
|
2023-07-31 20:29:33 +08:00
|
|
|
}
|
2023-04-02 00:13:11 +08:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2024-07-24 14:27:46 +08:00
|
|
|
FF_PARSE_FORMAT_STRING_CHECKED(&key, &options->moduleArgs.key, 4, ((FFformatarg[]){
|
2024-05-27 16:41:37 +08:00
|
|
|
{FF_FORMAT_ARG_TYPE_STRBUF, &disk->mountpoint, "mountpoint"},
|
|
|
|
|
{FF_FORMAT_ARG_TYPE_STRBUF, &disk->name, "name"},
|
|
|
|
|
{FF_FORMAT_ARG_TYPE_STRBUF, &disk->mountFrom, "mount-from"},
|
2024-07-24 14:27:46 +08:00
|
|
|
{FF_FORMAT_ARG_TYPE_STRBUF, &options->moduleArgs.keyIcon, "icon"},
|
2024-03-07 10:47:28 +08:00
|
|
|
}));
|
2023-04-02 00:13:11 +08:00
|
|
|
}
|
|
|
|
|
|
2023-04-15 15:29:54 +08:00
|
|
|
FF_STRBUF_AUTO_DESTROY usedPretty = ffStrbufCreate();
|
2023-07-27 15:17:54 +08:00
|
|
|
ffParseSize(disk->bytesUsed, &usedPretty);
|
2023-04-02 00:13:11 +08:00
|
|
|
|
2023-04-15 15:29:54 +08:00
|
|
|
FF_STRBUF_AUTO_DESTROY totalPretty = ffStrbufCreate();
|
2023-07-27 15:17:54 +08:00
|
|
|
ffParseSize(disk->bytesTotal, &totalPretty);
|
2023-04-02 00:13:11 +08:00
|
|
|
|
2023-08-16 16:46:21 +08:00
|
|
|
double bytesPercentage = disk->bytesTotal > 0 ? (double) disk->bytesUsed / (double) disk->bytesTotal * 100.0 : 0;
|
2023-04-02 00:13:11 +08:00
|
|
|
|
|
|
|
|
if(options->moduleArgs.outputFormat.length == 0)
|
|
|
|
|
{
|
2023-08-15 21:40:22 +08:00
|
|
|
ffPrintLogoAndKey(key.chars, 0, &options->moduleArgs, FF_PRINT_TYPE_NO_CUSTOM_KEY);
|
2023-04-02 00:13:11 +08:00
|
|
|
|
2023-04-15 15:29:54 +08:00
|
|
|
FF_STRBUF_AUTO_DESTROY str = ffStrbufCreate();
|
2023-04-02 00:13:11 +08:00
|
|
|
|
|
|
|
|
if(disk->bytesTotal > 0)
|
|
|
|
|
{
|
2023-10-26 10:26:15 +08:00
|
|
|
if(instance.config.display.percentType & FF_PERCENTAGE_TYPE_BAR_BIT)
|
2023-04-02 00:13:11 +08:00
|
|
|
{
|
2024-05-08 15:25:59 +08:00
|
|
|
ffPercentAppendBar(&str, bytesPercentage, options->percent, &options->moduleArgs);
|
2023-04-02 00:13:11 +08:00
|
|
|
ffStrbufAppendC(&str, ' ');
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-26 10:26:15 +08:00
|
|
|
if(!(instance.config.display.percentType & FF_PERCENTAGE_TYPE_HIDE_OTHERS_BIT))
|
2023-04-02 00:13:11 +08:00
|
|
|
ffStrbufAppendF(&str, "%s / %s ", usedPretty.chars, totalPretty.chars);
|
|
|
|
|
|
2023-10-26 10:26:15 +08:00
|
|
|
if(instance.config.display.percentType & FF_PERCENTAGE_TYPE_NUM_BIT)
|
2023-04-02 00:13:11 +08:00
|
|
|
{
|
2024-05-08 15:25:59 +08:00
|
|
|
ffPercentAppendNum(&str, bytesPercentage, options->percent, str.length > 0, &options->moduleArgs);
|
2023-04-02 00:13:11 +08:00
|
|
|
ffStrbufAppendC(&str, ' ');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
ffStrbufAppendS(&str, "Unknown ");
|
|
|
|
|
|
2023-10-26 10:26:15 +08:00
|
|
|
if(!(instance.config.display.percentType & FF_PERCENTAGE_TYPE_HIDE_OTHERS_BIT))
|
2023-06-15 00:30:27 +08:00
|
|
|
{
|
2023-06-19 16:17:31 +08:00
|
|
|
if(disk->filesystem.length)
|
|
|
|
|
ffStrbufAppendF(&str, "- %s ", disk->filesystem.chars);
|
2023-06-15 00:30:27 +08:00
|
|
|
|
2023-08-26 12:59:30 +08:00
|
|
|
ffStrbufAppendC(&str, '[');
|
2023-08-26 09:32:19 +08:00
|
|
|
if(disk->type & FF_DISK_VOLUME_TYPE_EXTERNAL_BIT)
|
2023-08-26 12:59:30 +08:00
|
|
|
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] = ']';
|
|
|
|
|
}
|
2023-06-15 00:30:27 +08:00
|
|
|
}
|
2023-04-02 00:13:11 +08:00
|
|
|
|
|
|
|
|
ffStrbufTrimRight(&str, ' ');
|
|
|
|
|
ffStrbufPutTo(&str, stdout);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2024-07-08 15:21:12 +08:00
|
|
|
FF_STRBUF_AUTO_DESTROY bytesPercentageNum = ffStrbufCreate();
|
|
|
|
|
ffPercentAppendNum(&bytesPercentageNum, bytesPercentage, options->percent, false, &options->moduleArgs);
|
|
|
|
|
FF_STRBUF_AUTO_DESTROY bytesPercentageBar = ffStrbufCreate();
|
|
|
|
|
ffPercentAppendBar(&bytesPercentageBar, bytesPercentage, options->percent, &options->moduleArgs);
|
|
|
|
|
|
2023-08-24 09:12:24 +08:00
|
|
|
double filesPercentage = disk->filesTotal > 0 ? ((double) disk->filesUsed / (double) disk->filesTotal) * 100.0 : 0;
|
2024-07-08 15:21:12 +08:00
|
|
|
FF_STRBUF_AUTO_DESTROY filesPercentageNum = ffStrbufCreate();
|
|
|
|
|
ffPercentAppendNum(&filesPercentageNum, filesPercentage, options->percent, false, &options->moduleArgs);
|
|
|
|
|
FF_STRBUF_AUTO_DESTROY filesPercentageBar = ffStrbufCreate();
|
|
|
|
|
ffPercentAppendBar(&filesPercentageBar, filesPercentage, options->percent, &options->moduleArgs);
|
2023-04-02 00:13:11 +08:00
|
|
|
|
2023-08-26 09:32:19 +08:00
|
|
|
bool isExternal = !!(disk->type & FF_DISK_VOLUME_TYPE_EXTERNAL_BIT);
|
|
|
|
|
bool isHidden = !!(disk->type & FF_DISK_VOLUME_TYPE_HIDDEN_BIT);
|
2023-08-26 12:50:45 +08:00
|
|
|
bool isReadOnly = !!(disk->type & FF_DISK_VOLUME_TYPE_READONLY_BIT);
|
2024-04-09 15:15:33 +08:00
|
|
|
|
2024-03-07 10:47:28 +08:00
|
|
|
FF_PRINT_FORMAT_CHECKED(key.chars, 0, &options->moduleArgs, FF_PRINT_TYPE_NO_CUSTOM_KEY, FF_DISK_NUM_FORMAT_ARGS, ((FFformatarg[]) {
|
2024-05-27 16:41:37 +08:00
|
|
|
{FF_FORMAT_ARG_TYPE_STRBUF, &usedPretty, "size-used"},
|
|
|
|
|
{FF_FORMAT_ARG_TYPE_STRBUF, &totalPretty, "size-total"},
|
2024-07-08 15:21:12 +08:00
|
|
|
{FF_FORMAT_ARG_TYPE_STRBUF, &bytesPercentageNum, "size-percentage"},
|
2024-05-27 16:41:37 +08:00
|
|
|
{FF_FORMAT_ARG_TYPE_UINT, &disk->filesUsed, "files-used"},
|
|
|
|
|
{FF_FORMAT_ARG_TYPE_UINT, &disk->filesTotal, "files-total"},
|
2024-07-08 15:21:12 +08:00
|
|
|
{FF_FORMAT_ARG_TYPE_STRBUF, &filesPercentageNum, "files-percentage"},
|
2024-05-27 16:41:37 +08:00
|
|
|
{FF_FORMAT_ARG_TYPE_BOOL, &isExternal, "is-external"},
|
|
|
|
|
{FF_FORMAT_ARG_TYPE_BOOL, &isHidden, "is-hidden"},
|
|
|
|
|
{FF_FORMAT_ARG_TYPE_STRBUF, &disk->filesystem, "filesystem"},
|
|
|
|
|
{FF_FORMAT_ARG_TYPE_STRBUF, &disk->name, "name"},
|
|
|
|
|
{FF_FORMAT_ARG_TYPE_BOOL, &isReadOnly, "is-readonly"},
|
|
|
|
|
{FF_FORMAT_ARG_TYPE_STRING, ffTimeToShortStr(disk->createTime), "create-time"},
|
2024-07-08 15:21:12 +08:00
|
|
|
{FF_FORMAT_ARG_TYPE_STRBUF, &bytesPercentageBar, "size-percentage-bar"},
|
|
|
|
|
{FF_FORMAT_ARG_TYPE_STRBUF, &filesPercentageBar, "files-percentage-bar"},
|
2024-03-07 10:47:28 +08:00
|
|
|
}));
|
2023-04-02 00:13:11 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-24 10:59:53 +08:00
|
|
|
void ffPrintDisk(FFDiskOptions* options)
|
2023-04-02 00:13:11 +08:00
|
|
|
{
|
2023-06-12 22:09:52 +08:00
|
|
|
FF_LIST_AUTO_DESTROY disks = ffListCreate(sizeof (FFDisk));
|
2023-08-26 10:36:00 +08:00
|
|
|
const char* error = ffDetectDisks(options, &disks);
|
2023-06-12 22:09:52 +08:00
|
|
|
|
|
|
|
|
if(error)
|
2023-04-02 00:13:11 +08:00
|
|
|
{
|
2024-03-07 11:10:18 +08:00
|
|
|
ffPrintError(FF_DISK_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "%s", error);
|
2023-04-02 00:13:11 +08:00
|
|
|
}
|
|
|
|
|
else
|
2023-06-12 22:09:52 +08:00
|
|
|
{
|
2024-04-30 15:12:49 +08:00
|
|
|
FF_LIST_FOR_EACH(FFDisk, disk, disks)
|
|
|
|
|
{
|
|
|
|
|
if(__builtin_expect(options->folders.length == 0, 1) && (disk->type & ~options->showTypes))
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
printDisk(options, disk);
|
|
|
|
|
}
|
2023-06-12 22:09:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FF_LIST_FOR_EACH(FFDisk, disk, disks)
|
|
|
|
|
{
|
2023-09-17 20:08:46 +08:00
|
|
|
ffStrbufDestroy(&disk->mountFrom);
|
2023-06-12 22:09:52 +08:00
|
|
|
ffStrbufDestroy(&disk->mountpoint);
|
|
|
|
|
ffStrbufDestroy(&disk->filesystem);
|
|
|
|
|
ffStrbufDestroy(&disk->name);
|
|
|
|
|
}
|
2023-04-02 00:13:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool ffParseDiskCommandOptions(FFDiskOptions* options, const char* key, const char* value)
|
|
|
|
|
{
|
|
|
|
|
const char* subKey = ffOptionTestPrefix(key, FF_DISK_MODULE_NAME);
|
|
|
|
|
if (!subKey) return false;
|
|
|
|
|
if (ffOptionParseModuleArgs(key, subKey, value, &options->moduleArgs))
|
|
|
|
|
return true;
|
|
|
|
|
|
2023-06-19 15:21:49 +08:00
|
|
|
if (ffStrEqualsIgnCase(subKey, "folders"))
|
2023-04-02 00:13:11 +08:00
|
|
|
{
|
|
|
|
|
ffOptionParseString(key, value, &options->folders);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-19 15:21:49 +08:00
|
|
|
if (ffStrEqualsIgnCase(subKey, "show-regular"))
|
2023-04-02 00:13:11 +08:00
|
|
|
{
|
|
|
|
|
if (ffOptionParseBoolean(value))
|
2023-08-26 09:32:19 +08:00
|
|
|
options->showTypes |= FF_DISK_VOLUME_TYPE_REGULAR_BIT;
|
2023-04-02 00:13:11 +08:00
|
|
|
else
|
2023-08-26 09:32:19 +08:00
|
|
|
options->showTypes &= ~FF_DISK_VOLUME_TYPE_REGULAR_BIT;
|
2023-04-02 00:13:11 +08:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-19 15:21:49 +08:00
|
|
|
if (ffStrEqualsIgnCase(subKey, "show-external"))
|
2023-04-02 00:13:11 +08:00
|
|
|
{
|
|
|
|
|
if (ffOptionParseBoolean(value))
|
2023-08-26 09:32:19 +08:00
|
|
|
options->showTypes |= FF_DISK_VOLUME_TYPE_EXTERNAL_BIT;
|
2023-04-02 00:13:11 +08:00
|
|
|
else
|
2023-08-26 09:32:19 +08:00
|
|
|
options->showTypes &= ~FF_DISK_VOLUME_TYPE_EXTERNAL_BIT;
|
2023-04-02 00:13:11 +08:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-19 15:21:49 +08:00
|
|
|
if (ffStrEqualsIgnCase(subKey, "show-hidden"))
|
2023-04-02 00:13:11 +08:00
|
|
|
{
|
|
|
|
|
if (ffOptionParseBoolean(value))
|
2023-08-26 09:32:19 +08:00
|
|
|
options->showTypes |= FF_DISK_VOLUME_TYPE_HIDDEN_BIT;
|
2023-04-02 00:13:11 +08:00
|
|
|
else
|
2023-08-26 09:32:19 +08:00
|
|
|
options->showTypes &= ~FF_DISK_VOLUME_TYPE_HIDDEN_BIT;
|
2023-04-02 00:13:11 +08:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-19 15:21:49 +08:00
|
|
|
if (ffStrEqualsIgnCase(subKey, "show-subvolumes"))
|
2023-04-02 00:13:11 +08:00
|
|
|
{
|
|
|
|
|
if (ffOptionParseBoolean(value))
|
2023-08-26 09:32:19 +08:00
|
|
|
options->showTypes |= FF_DISK_VOLUME_TYPE_SUBVOLUME_BIT;
|
2023-04-02 00:13:11 +08:00
|
|
|
else
|
2023-08-26 09:32:19 +08:00
|
|
|
options->showTypes &= ~FF_DISK_VOLUME_TYPE_SUBVOLUME_BIT;
|
2023-04-02 00:13:11 +08:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-26 12:50:45 +08:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-19 15:21:49 +08:00
|
|
|
if (ffStrEqualsIgnCase(subKey, "show-unknown"))
|
2023-04-02 00:13:11 +08:00
|
|
|
{
|
|
|
|
|
if (ffOptionParseBoolean(value))
|
2023-08-26 09:32:19 +08:00
|
|
|
options->showTypes |= FF_DISK_VOLUME_TYPE_UNKNOWN_BIT;
|
2023-04-02 00:13:11 +08:00
|
|
|
else
|
2023-08-26 09:32:19 +08:00
|
|
|
options->showTypes &= ~FF_DISK_VOLUME_TYPE_UNKNOWN_BIT;
|
2023-04-02 00:13:11 +08:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-26 10:36:00 +08:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-20 18:40:12 +08:00
|
|
|
if (ffPercentParseCommandOptions(key, subKey, value, &options->percent))
|
|
|
|
|
return true;
|
|
|
|
|
|
2023-04-02 00:13:11 +08:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-17 10:56:54 +08:00
|
|
|
void ffParseDiskJsonObject(FFDiskOptions* options, yyjson_val* module)
|
2023-04-02 00:13:11 +08:00
|
|
|
{
|
2023-08-17 16:09:41 +08:00
|
|
|
yyjson_val *key_, *val;
|
|
|
|
|
size_t idx, max;
|
|
|
|
|
yyjson_obj_foreach(module, idx, max, key_, val)
|
2023-04-02 00:13:11 +08:00
|
|
|
{
|
2023-08-17 16:09:41 +08:00
|
|
|
const char* key = yyjson_get_str(key_);
|
|
|
|
|
if(ffStrEqualsIgnCase(key, "type"))
|
|
|
|
|
continue;
|
2023-04-02 00:13:11 +08:00
|
|
|
|
2023-08-17 16:09:41 +08:00
|
|
|
if (ffJsonConfigParseModuleArgs(key, val, &options->moduleArgs))
|
|
|
|
|
continue;
|
2023-04-02 00:13:11 +08:00
|
|
|
|
2023-08-17 16:09:41 +08:00
|
|
|
if (ffStrEqualsIgnCase(key, "folders"))
|
|
|
|
|
{
|
|
|
|
|
ffStrbufSetS(&options->folders, yyjson_get_str(val));
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2023-04-02 00:13:11 +08:00
|
|
|
|
2023-08-17 16:09:41 +08:00
|
|
|
if (ffStrEqualsIgnCase(key, "showExternal"))
|
|
|
|
|
{
|
|
|
|
|
if (yyjson_get_bool(val))
|
2023-08-26 09:32:19 +08:00
|
|
|
options->showTypes |= FF_DISK_VOLUME_TYPE_EXTERNAL_BIT;
|
2023-08-17 16:09:41 +08:00
|
|
|
else
|
2023-08-26 09:32:19 +08:00
|
|
|
options->showTypes &= ~FF_DISK_VOLUME_TYPE_EXTERNAL_BIT;
|
2023-08-17 16:09:41 +08:00
|
|
|
continue;
|
|
|
|
|
}
|
2023-04-02 00:13:11 +08:00
|
|
|
|
2023-08-17 16:09:41 +08:00
|
|
|
if (ffStrEqualsIgnCase(key, "showHidden"))
|
|
|
|
|
{
|
|
|
|
|
if (yyjson_get_bool(val))
|
2023-08-26 09:32:19 +08:00
|
|
|
options->showTypes |= FF_DISK_VOLUME_TYPE_HIDDEN_BIT;
|
2023-08-17 16:09:41 +08:00
|
|
|
else
|
2023-08-26 09:32:19 +08:00
|
|
|
options->showTypes &= ~FF_DISK_VOLUME_TYPE_HIDDEN_BIT;
|
2023-08-17 16:09:41 +08:00
|
|
|
continue;
|
|
|
|
|
}
|
2023-04-02 00:13:11 +08:00
|
|
|
|
2023-08-17 16:09:41 +08:00
|
|
|
if (ffStrEqualsIgnCase(key, "showSubvolumes"))
|
|
|
|
|
{
|
|
|
|
|
if (yyjson_get_bool(val))
|
2023-08-26 09:32:19 +08:00
|
|
|
options->showTypes |= FF_DISK_VOLUME_TYPE_SUBVOLUME_BIT;
|
2023-08-17 16:09:41 +08:00
|
|
|
else
|
2023-08-26 09:32:19 +08:00
|
|
|
options->showTypes &= ~FF_DISK_VOLUME_TYPE_SUBVOLUME_BIT;
|
2023-08-17 16:09:41 +08:00
|
|
|
continue;
|
|
|
|
|
}
|
2023-04-02 00:13:11 +08:00
|
|
|
|
2023-08-26 12:50:45 +08:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-17 16:09:41 +08:00
|
|
|
if (ffStrEqualsIgnCase(key, "showUnknown"))
|
|
|
|
|
{
|
|
|
|
|
if (yyjson_get_bool(val))
|
2023-08-26 09:32:19 +08:00
|
|
|
options->showTypes |= FF_DISK_VOLUME_TYPE_UNKNOWN_BIT;
|
2023-08-17 16:09:41 +08:00
|
|
|
else
|
2023-08-26 09:32:19 +08:00
|
|
|
options->showTypes &= ~FF_DISK_VOLUME_TYPE_UNKNOWN_BIT;
|
2023-08-17 16:09:41 +08:00
|
|
|
continue;
|
2023-04-02 00:13:11 +08:00
|
|
|
}
|
2023-08-17 16:09:41 +08:00
|
|
|
|
2023-08-26 10:36:00 +08:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-20 18:40:12 +08:00
|
|
|
if (ffPercentParseJsonObject(key, val, &options->percent))
|
|
|
|
|
continue;
|
|
|
|
|
|
2024-03-07 11:10:18 +08:00
|
|
|
ffPrintError(FF_DISK_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "Unknown JSON key %s", key);
|
2023-04-02 00:13:11 +08:00
|
|
|
}
|
|
|
|
|
}
|
2023-08-31 14:13:14 +08:00
|
|
|
|
2023-10-24 14:41:29 +08:00
|
|
|
void ffGenerateDiskJsonConfig(FFDiskOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module)
|
|
|
|
|
{
|
|
|
|
|
__attribute__((__cleanup__(ffDestroyDiskOptions))) FFDiskOptions defaultOptions;
|
|
|
|
|
ffInitDiskOptions(&defaultOptions);
|
|
|
|
|
|
|
|
|
|
ffJsonConfigGenerateModuleArgsConfig(doc, module, &defaultOptions.moduleArgs, &options->moduleArgs);
|
|
|
|
|
|
|
|
|
|
if (defaultOptions.showTypes != options->showTypes)
|
|
|
|
|
{
|
|
|
|
|
if (options->showTypes & FF_DISK_VOLUME_TYPE_EXTERNAL_BIT)
|
|
|
|
|
yyjson_mut_obj_add_bool(doc, module, "showExternal", true);
|
|
|
|
|
|
|
|
|
|
if (options->showTypes & FF_DISK_VOLUME_TYPE_HIDDEN_BIT)
|
|
|
|
|
yyjson_mut_obj_add_bool(doc, module, "showHidden", true);
|
|
|
|
|
|
|
|
|
|
if (options->showTypes & FF_DISK_VOLUME_TYPE_SUBVOLUME_BIT)
|
|
|
|
|
yyjson_mut_obj_add_bool(doc, module, "showSubvolumes", true);
|
|
|
|
|
|
|
|
|
|
if (options->showTypes & FF_DISK_VOLUME_TYPE_READONLY_BIT)
|
|
|
|
|
yyjson_mut_obj_add_bool(doc, module, "showReadOnly", true);
|
|
|
|
|
|
|
|
|
|
if (options->showTypes & FF_DISK_VOLUME_TYPE_UNKNOWN_BIT)
|
|
|
|
|
yyjson_mut_obj_add_bool(doc, module, "showUnknown", true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!ffStrbufEqual(&options->folders, &defaultOptions.folders))
|
|
|
|
|
yyjson_mut_obj_add_strbuf(doc, module, "folders", &options->folders);
|
|
|
|
|
|
|
|
|
|
if (defaultOptions.calcType != options->calcType)
|
|
|
|
|
yyjson_mut_obj_add_bool(doc, module, "useAvailable", options->calcType == FF_DISK_CALC_TYPE_AVAILABLE);
|
2024-01-20 18:40:12 +08:00
|
|
|
|
|
|
|
|
ffPercentGenerateJsonConfig(doc, module, defaultOptions.percent, options->percent);
|
2023-10-24 14:41:29 +08:00
|
|
|
}
|
|
|
|
|
|
2023-10-23 10:14:22 +08:00
|
|
|
void ffGenerateDiskJsonResult(FFDiskOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module)
|
2023-08-31 14:13:14 +08:00
|
|
|
{
|
|
|
|
|
FF_LIST_AUTO_DESTROY disks = ffListCreate(sizeof (FFDisk));
|
|
|
|
|
const char* error = ffDetectDisks(options, &disks);
|
|
|
|
|
|
|
|
|
|
if(error)
|
|
|
|
|
{
|
|
|
|
|
yyjson_mut_obj_add_str(doc, module, "result", error);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
yyjson_mut_val* arr = yyjson_mut_obj_add_arr(doc, module, "result");
|
|
|
|
|
|
|
|
|
|
FF_LIST_FOR_EACH(FFDisk, item, disks)
|
|
|
|
|
{
|
|
|
|
|
yyjson_mut_val* obj = yyjson_mut_arr_add_obj(doc, arr);
|
2023-09-17 20:08:46 +08:00
|
|
|
|
|
|
|
|
yyjson_mut_val* bytes = yyjson_mut_obj_add_obj(doc, obj, "bytes");
|
|
|
|
|
yyjson_mut_obj_add_uint(doc, bytes, "available", item->bytesAvailable);
|
|
|
|
|
yyjson_mut_obj_add_uint(doc, bytes, "free", item->bytesFree);
|
|
|
|
|
yyjson_mut_obj_add_uint(doc, bytes, "total", item->bytesTotal);
|
|
|
|
|
yyjson_mut_obj_add_uint(doc, bytes, "used", item->bytesUsed);
|
|
|
|
|
|
|
|
|
|
yyjson_mut_val* files = yyjson_mut_obj_add_obj(doc, obj, "files");
|
2024-03-25 10:09:05 +08:00
|
|
|
if (item->filesTotal == 0 && item->filesUsed == 0)
|
|
|
|
|
{
|
|
|
|
|
yyjson_mut_obj_add_null(doc, files, "total");
|
|
|
|
|
yyjson_mut_obj_add_null(doc, files, "used");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
yyjson_mut_obj_add_uint(doc, files, "total", item->filesTotal);
|
|
|
|
|
yyjson_mut_obj_add_uint(doc, files, "used", item->filesUsed);
|
|
|
|
|
}
|
2023-09-17 20:08:46 +08:00
|
|
|
|
2023-09-18 20:01:41 +08:00
|
|
|
yyjson_mut_obj_add_strbuf(doc, obj, "filesystem", &item->filesystem);
|
2023-08-31 14:13:14 +08:00
|
|
|
yyjson_mut_obj_add_strbuf(doc, obj, "mountpoint", &item->mountpoint);
|
2023-09-17 20:08:46 +08:00
|
|
|
yyjson_mut_obj_add_strbuf(doc, obj, "mountFrom", &item->mountFrom);
|
2023-08-31 14:13:14 +08:00
|
|
|
yyjson_mut_obj_add_strbuf(doc, obj, "name", &item->name);
|
2023-09-30 20:32:14 +08:00
|
|
|
yyjson_mut_val* typeArr = yyjson_mut_obj_add_arr(doc, obj, "volumeType");
|
2023-09-17 20:08:46 +08:00
|
|
|
if(item->type & FF_DISK_VOLUME_TYPE_REGULAR_BIT)
|
|
|
|
|
yyjson_mut_arr_add_str(doc, typeArr, "Regular");
|
2023-08-31 14:13:14 +08:00
|
|
|
if(item->type & FF_DISK_VOLUME_TYPE_EXTERNAL_BIT)
|
|
|
|
|
yyjson_mut_arr_add_str(doc, typeArr, "External");
|
|
|
|
|
if(item->type & FF_DISK_VOLUME_TYPE_SUBVOLUME_BIT)
|
|
|
|
|
yyjson_mut_arr_add_str(doc, typeArr, "Subvolume");
|
|
|
|
|
if(item->type & FF_DISK_VOLUME_TYPE_HIDDEN_BIT)
|
|
|
|
|
yyjson_mut_arr_add_str(doc, typeArr, "Hidden");
|
|
|
|
|
if(item->type & FF_DISK_VOLUME_TYPE_READONLY_BIT)
|
|
|
|
|
yyjson_mut_arr_add_str(doc, typeArr, "Read-only");
|
2024-05-04 15:15:00 +08:00
|
|
|
if(item->type & FF_DISK_VOLUME_TYPE_UNKNOWN_BIT)
|
|
|
|
|
yyjson_mut_arr_add_str(doc, typeArr, "Unknown");
|
2024-04-09 15:15:33 +08:00
|
|
|
|
2024-04-09 20:37:43 +08:00
|
|
|
const char* pstr = ffTimeToFullStr(item->createTime);
|
|
|
|
|
if (*pstr)
|
|
|
|
|
yyjson_mut_obj_add_strcpy(doc, obj, "createTime", pstr);
|
|
|
|
|
else
|
|
|
|
|
yyjson_mut_obj_add_null(doc, obj, "createTime");
|
2023-08-31 14:13:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FF_LIST_FOR_EACH(FFDisk, item, disks)
|
|
|
|
|
{
|
|
|
|
|
ffStrbufDestroy(&item->mountpoint);
|
2023-09-17 20:08:46 +08:00
|
|
|
ffStrbufDestroy(&item->mountFrom);
|
2023-08-31 14:13:14 +08:00
|
|
|
ffStrbufDestroy(&item->filesystem);
|
|
|
|
|
ffStrbufDestroy(&item->name);
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-10-07 13:40:40 +08:00
|
|
|
|
|
|
|
|
void ffPrintDiskHelpFormat(void)
|
|
|
|
|
{
|
2024-03-07 10:47:28 +08:00
|
|
|
FF_PRINT_MODULE_FORMAT_HELP_CHECKED(FF_DISK_MODULE_NAME, "{1} / {2} ({3}) - {9}", FF_DISK_NUM_FORMAT_ARGS, ((const char* []) {
|
2024-05-27 16:41:37 +08:00
|
|
|
"Size used - size-used",
|
|
|
|
|
"Size total - size-total",
|
2024-07-08 15:21:12 +08:00
|
|
|
"Size percentage num - size-percentage",
|
2024-05-27 16:41:37 +08:00
|
|
|
"Files used - files-used",
|
|
|
|
|
"Files total - files-total",
|
2024-07-08 15:21:12 +08:00
|
|
|
"Files percentage num - files-percentage",
|
2024-05-27 16:41:37 +08:00
|
|
|
"True if external volume - is-external",
|
|
|
|
|
"True if hidden volume - is-hidden",
|
|
|
|
|
"Filesystem - filesystem",
|
|
|
|
|
"Label / name - name",
|
|
|
|
|
"True if read-only - is-readonly",
|
|
|
|
|
"Create time in local timezone - create-time",
|
2024-07-08 15:21:12 +08:00
|
|
|
"Size percentage bar - size-percentage-bar",
|
|
|
|
|
"Files percentage bar - files-percentage-bar",
|
2024-03-07 10:47:28 +08:00
|
|
|
}));
|
2023-10-07 13:40:40 +08:00
|
|
|
}
|
2023-10-23 13:32:18 +08:00
|
|
|
|
|
|
|
|
void ffInitDiskOptions(FFDiskOptions* options)
|
|
|
|
|
{
|
2023-10-23 13:39:07 +08:00
|
|
|
ffOptionInitModuleBaseInfo(
|
|
|
|
|
&options->moduleInfo,
|
|
|
|
|
FF_DISK_MODULE_NAME,
|
2024-07-05 10:16:25 +08:00
|
|
|
"Print partitions, space usage, file system, etc",
|
2023-10-23 13:39:07 +08:00
|
|
|
ffParseDiskCommandOptions,
|
|
|
|
|
ffParseDiskJsonObject,
|
|
|
|
|
ffPrintDisk,
|
|
|
|
|
ffGenerateDiskJsonResult,
|
|
|
|
|
ffPrintDiskHelpFormat,
|
2023-10-24 14:41:29 +08:00
|
|
|
ffGenerateDiskJsonConfig
|
2023-10-23 13:39:07 +08:00
|
|
|
);
|
2024-07-23 23:02:35 +08:00
|
|
|
ffOptionInitModuleArg(&options->moduleArgs, "");
|
2023-10-23 13:32:18 +08:00
|
|
|
|
|
|
|
|
ffStrbufInit(&options->folders);
|
|
|
|
|
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;
|
2024-02-26 15:12:04 +08:00
|
|
|
options->percent = (FFColorRangeConfig) { 50, 80 };
|
2023-10-23 13:32:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ffDestroyDiskOptions(FFDiskOptions* options)
|
|
|
|
|
{
|
|
|
|
|
ffOptionDestroyModuleArg(&options->moduleArgs);
|
|
|
|
|
}
|