Files
fastfetch/src/modules/disk/disk.c
T

491 lines
22 KiB
C
Raw Normal View History

2023-04-02 00:13:11 +08:00
#include "common/printing.h"
2023-06-10 15:01:54 +08:00
#include "common/jsonconfig.h"
2024-01-20 11:28:38 +08:00
#include "common/percent.h"
2025-07-30 14:30:36 +08:00
#include "common/size.h"
#include "common/time.h"
2023-04-02 00:13:11 +08:00
#include "detection/disk/disk.h"
#include "modules/disk/disk.h"
#ifndef _WIN32
2026-03-29 09:28:49 +08:00
# include <fnmatch.h>
#endif
2023-04-02 00:13:11 +08:00
#pragma GCC diagnostic ignored "-Wsign-conversion"
2026-03-29 09:28:49 +08:00
static void printDisk(FFDiskOptions* options, const FFDisk* disk, uint32_t index) {
FF_STRBUF_AUTO_DESTROY key = ffStrbufCreate();
2023-04-02 00:13:11 +08:00
2026-03-29 09:28:49 +08:00
if (options->moduleArgs.key.length == 0) {
ffStrbufSetF(&key, "%s (%s)", FF_DISK_MODULE_NAME, disk->mountpoint.chars);
2026-03-29 09:28:49 +08:00
} else {
FF_STRBUF_AUTO_DESTROY mountpointLink = ffStrbufCreate();
FF_STRBUF_AUTO_DESTROY nameLink = ffStrbufCreate();
2026-03-29 09:28:49 +08:00
#ifdef __linux__
if (getenv("WSL_DISTRO_NAME") != NULL && getenv("WT_SESSION") != NULL) {
if (ffStrbufEqualS(&disk->filesystem, "9p") && ffStrbufStartsWithS(&disk->mountpoint, "/mnt/")) {
ffStrbufSetF(&mountpointLink, "\e]8;;file:///%c:/\e\\%s\e]8;;\e\\", disk->mountpoint.chars[5], disk->mountpoint.chars);
ffStrbufSetF(&nameLink, "\e]8;;file:///%c:/\e\\%s\e]8;;\e\\", disk->mountpoint.chars[5], disk->name.chars);
2026-03-29 09:28:49 +08:00
} else {
ffStrbufSetF(&mountpointLink, "\e]8;;file:////wsl.localhost/%s%s\e\\%s\e]8;;\e\\", getenv("WSL_DISTRO_NAME"), disk->mountpoint.chars, disk->mountpoint.chars);
ffStrbufSetF(&nameLink, "\e]8;;file:////wsl.localhost/%s%s\e\\%s\e]8;;\e\\", getenv("WSL_DISTRO_NAME"), disk->mountpoint.chars, disk->name.chars);
}
2026-03-29 09:28:49 +08:00
} else
#endif
{
ffStrbufSetF(&mountpointLink, "\e]8;;file://%s\e\\%s\e]8;;\e\\", disk->mountpoint.chars, disk->mountpoint.chars);
ffStrbufSetF(&nameLink, "\e]8;;file://%s\e\\%s\e]8;;\e\\", disk->mountpoint.chars, disk->name.chars);
}
2024-12-11 10:47:02 +08:00
FF_PARSE_FORMAT_STRING_CHECKED(&key, &options->moduleArgs.key, ((FFformatarg[]) {
2026-03-29 09:28:49 +08:00
FF_ARG(disk->mountpoint, "mountpoint"),
FF_ARG(disk->name, "name"),
FF_ARG(disk->mountFrom, "mount-from"),
FF_ARG(options->moduleArgs.keyIcon, "icon"),
FF_ARG(index, "index"),
FF_ARG(disk->filesystem, "filesystem"),
FF_ARG(mountpointLink, "mountpoint-link"),
FF_ARG(nameLink, "name-link"),
}));
2023-04-02 00:13:11 +08:00
}
FF_STRBUF_AUTO_DESTROY usedPretty = ffStrbufCreate();
2025-07-30 14:30:36 +08:00
ffSizeAppendNum(disk->bytesUsed, &usedPretty);
2023-04-02 00:13:11 +08:00
FF_STRBUF_AUTO_DESTROY totalPretty = ffStrbufCreate();
2025-07-30 14:30:36 +08:00
ffSizeAppendNum(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;
FFPercentageTypeFlags percentType = options->percent.type == 0 ? instance.config.display.percentType : options->percent.type;
2023-04-02 00:13:11 +08:00
2026-03-29 09:28:49 +08:00
if (options->moduleArgs.outputFormat.length == 0) {
ffPrintLogoAndKey(key.chars, 0, &options->moduleArgs, FF_PRINT_TYPE_NO_CUSTOM_KEY);
2023-04-02 00:13:11 +08:00
FF_STRBUF_AUTO_DESTROY str = ffStrbufCreate();
2023-04-02 00:13:11 +08:00
2026-03-29 09:28:49 +08:00
if (disk->bytesTotal > 0) {
if (percentType & FF_PERCENTAGE_TYPE_BAR_BIT) {
ffPercentAppendBar(&str, bytesPercentage, options->percent, &options->moduleArgs);
2023-04-02 00:13:11 +08:00
ffStrbufAppendC(&str, ' ');
}
2026-03-29 09:28:49 +08:00
if (!(percentType & FF_PERCENTAGE_TYPE_HIDE_OTHERS_BIT)) {
2023-04-02 00:13:11 +08:00
ffStrbufAppendF(&str, "%s / %s ", usedPretty.chars, totalPretty.chars);
2026-03-29 09:28:49 +08:00
}
2023-04-02 00:13:11 +08:00
2026-03-29 09:28:49 +08:00
if (percentType & FF_PERCENTAGE_TYPE_NUM_BIT) {
ffPercentAppendNum(&str, bytesPercentage, options->percent, str.length > 0, &options->moduleArgs);
2023-04-02 00:13:11 +08:00
ffStrbufAppendC(&str, ' ');
}
2026-03-29 09:28:49 +08:00
} else {
2023-04-02 00:13:11 +08:00
ffStrbufAppendS(&str, "Unknown ");
2026-03-29 09:28:49 +08:00
}
2023-04-02 00:13:11 +08:00
2026-03-29 09:28:49 +08:00
if (!(percentType & FF_PERCENTAGE_TYPE_HIDE_OTHERS_BIT)) {
if (disk->filesystem.length) {
ffStrbufAppendF(&str, "- %s ", disk->filesystem.chars);
2026-03-29 09:28:49 +08:00
}
2023-08-26 12:59:30 +08:00
ffStrbufAppendC(&str, '[');
2026-03-29 09:28:49 +08:00
if (disk->type & FF_DISK_VOLUME_TYPE_EXTERNAL_BIT) {
2023-08-26 12:59:30 +08:00
ffStrbufAppendS(&str, "External, ");
2026-03-29 09:28:49 +08:00
}
if (disk->type & FF_DISK_VOLUME_TYPE_SUBVOLUME_BIT) {
2023-08-26 12:59:30 +08:00
ffStrbufAppendS(&str, "Subvolume, ");
2026-03-29 09:28:49 +08:00
}
if (disk->type & FF_DISK_VOLUME_TYPE_HIDDEN_BIT) {
2023-08-26 12:59:30 +08:00
ffStrbufAppendS(&str, "Hidden, ");
2026-03-29 09:28:49 +08:00
}
if (disk->type & FF_DISK_VOLUME_TYPE_READONLY_BIT) {
2023-08-26 12:59:30 +08:00
ffStrbufAppendS(&str, "Read-only, ");
2026-03-29 09:28:49 +08:00
}
if (str.chars[str.length - 1] == '[') {
2023-08-26 12:59:30 +08:00
ffStrbufSubstrBefore(&str, str.length - 1);
2026-03-29 09:28:49 +08:00
} else {
2023-08-26 12:59:30 +08:00
ffStrbufTrimRight(&str, ' ');
str.chars[str.length - 1] = ']';
}
}
2023-04-02 00:13:11 +08:00
ffStrbufTrimRight(&str, ' ');
ffStrbufPutTo(&str, stdout);
2026-03-29 09:28:49 +08:00
} else {
FF_STRBUF_AUTO_DESTROY bytesPercentageNum = ffStrbufCreate();
2026-03-29 09:28:49 +08:00
if (percentType & FF_PERCENTAGE_TYPE_NUM_BIT) {
ffPercentAppendNum(&bytesPercentageNum, bytesPercentage, options->percent, false, &options->moduleArgs);
2026-03-29 09:28:49 +08:00
}
FF_STRBUF_AUTO_DESTROY bytesPercentageBar = ffStrbufCreate();
2026-03-29 09:28:49 +08:00
if (percentType & FF_PERCENTAGE_TYPE_BAR_BIT) {
ffPercentAppendBar(&bytesPercentageBar, bytesPercentage, options->percent, &options->moduleArgs);
2026-03-29 09:28:49 +08:00
}
double filesPercentage = disk->filesTotal > 0 ? ((double) disk->filesUsed / (double) disk->filesTotal) * 100.0 : 0;
FF_STRBUF_AUTO_DESTROY filesPercentageNum = ffStrbufCreate();
2026-03-29 09:28:49 +08:00
if (percentType & FF_PERCENTAGE_TYPE_NUM_BIT) {
ffPercentAppendNum(&filesPercentageNum, filesPercentage, options->percent, false, &options->moduleArgs);
2026-03-29 09:28:49 +08:00
}
FF_STRBUF_AUTO_DESTROY filesPercentageBar = ffStrbufCreate();
2026-03-29 09:28:49 +08:00
if (percentType & FF_PERCENTAGE_TYPE_BAR_BIT) {
ffPercentAppendBar(&filesPercentageBar, filesPercentage, options->percent, &options->moduleArgs);
2026-03-29 09:28:49 +08:00
}
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
FF_STRBUF_AUTO_DESTROY freePretty = ffStrbufCreate();
ffSizeAppendNum(disk->bytesFree, &freePretty);
FF_STRBUF_AUTO_DESTROY availPretty = ffStrbufCreate();
ffSizeAppendNum(disk->bytesAvailable, &availPretty);
uint64_t now = ffTimeGetNow();
uint64_t duration = now - disk->createTime;
uint32_t milliseconds = (uint32_t) (duration % 1000);
duration /= 1000;
uint32_t seconds = (uint32_t) (duration % 60);
duration /= 60;
uint32_t minutes = (uint32_t) (duration % 60);
duration /= 60;
uint32_t hours = (uint32_t) (duration % 24);
duration /= 24;
uint32_t days = (uint32_t) duration;
FFTimeGetAgeResult age = ffTimeGetAge(disk->createTime, now);
2024-12-11 10:47:02 +08:00
FF_PRINT_FORMAT_CHECKED(key.chars, 0, &options->moduleArgs, FF_PRINT_TYPE_NO_CUSTOM_KEY, ((FFformatarg[]) {
2026-03-29 09:28:49 +08:00
FF_ARG(usedPretty, "size-used"),
FF_ARG(totalPretty, "size-total"),
FF_ARG(bytesPercentageNum, "size-percentage"),
FF_ARG(disk->filesUsed, "files-used"),
FF_ARG(disk->filesTotal, "files-total"),
FF_ARG(filesPercentageNum, "files-percentage"),
FF_ARG(isExternal, "is-external"),
FF_ARG(isHidden, "is-hidden"),
FF_ARG(disk->filesystem, "filesystem"),
FF_ARG(disk->name, "name"),
FF_ARG(isReadOnly, "is-readonly"),
2026-03-30 10:27:23 +08:00
{ FF_ARG_TYPE_STRING, ffTimeToShortStr(disk->createTime), "create-time" },
2026-03-29 09:28:49 +08:00
FF_ARG(bytesPercentageBar, "size-percentage-bar"),
FF_ARG(filesPercentageBar, "files-percentage-bar"),
FF_ARG(days, "days"),
FF_ARG(hours, "hours"),
FF_ARG(minutes, "minutes"),
FF_ARG(seconds, "seconds"),
FF_ARG(milliseconds, "milliseconds"),
FF_ARG(disk->mountpoint, "mountpoint"),
FF_ARG(disk->mountFrom, "mount-from"),
FF_ARG(age.years, "years"),
FF_ARG(age.daysOfYear, "days-of-year"),
FF_ARG(age.yearsFraction, "years-fraction"),
FF_ARG(freePretty, "size-free"),
FF_ARG(availPretty, "size-available"),
}));
2023-04-02 00:13:11 +08:00
}
}
2026-03-29 09:28:49 +08:00
bool ffPrintDisk(FFDiskOptions* options) {
2026-04-14 10:41:18 +08:00
FF_LIST_AUTO_DESTROY disks = ffListCreate();
2023-08-26 10:36:00 +08:00
const char* error = ffDetectDisks(options, &disks);
2023-06-12 22:09:52 +08:00
2026-03-29 09:28:49 +08:00
if (error) {
ffPrintError(FF_DISK_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "%s", error);
return false;
2023-04-02 00:13:11 +08:00
}
2026-03-29 09:28:49 +08:00
if (disks.length == 0) {
ffPrintError(FF_DISK_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "No disks found");
return false;
}
uint32_t index = 0;
2026-03-29 09:28:49 +08:00
FF_LIST_FOR_EACH (FFDisk, disk, disks) {
if (__builtin_expect(options->folders.length == 0, 1) && (disk->type & ~options->showTypes)) {
continue;
2026-03-29 09:28:49 +08:00
}
printDisk(options, disk, ++index);
2023-06-12 22:09:52 +08:00
}
2026-03-29 09:28:49 +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);
}
return true;
2023-04-02 00:13:11 +08:00
}
2026-03-29 09:28:49 +08:00
static bool setSeparatedList(FFstrbuf* strbuf, yyjson_val* val, char separator) {
if (yyjson_is_str(val)) {
ffStrbufSetJsonVal(strbuf, val);
return true;
}
2026-03-29 09:28:49 +08:00
if (yyjson_is_arr(val)) {
ffStrbufClear(strbuf);
2026-03-29 09:28:49 +08:00
yyjson_val* elem;
size_t eidx, emax;
2026-03-29 09:28:49 +08:00
yyjson_arr_foreach (val, eidx, emax, elem) {
if (yyjson_is_str(elem)) {
if (strbuf->length > 0) {
ffStrbufAppendC(strbuf, separator);
2026-03-29 09:28:49 +08:00
}
ffStrbufAppendJsonVal(strbuf, elem);
}
}
return true;
}
return false;
}
2026-03-29 09:28:49 +08:00
void ffParseDiskJsonObject(FFDiskOptions* options, yyjson_val* module) {
yyjson_val *key, *val;
2023-08-17 16:09:41 +08:00
size_t idx, max;
2026-03-29 09:28:49 +08:00
yyjson_obj_foreach (module, idx, max, key, val) {
if (ffJsonConfigParseModuleArgs(key, val, &options->moduleArgs)) {
2023-08-17 16:09:41 +08:00
continue;
2026-03-29 09:28:49 +08:00
}
2023-04-02 00:13:11 +08:00
2026-03-29 09:28:49 +08:00
if (unsafe_yyjson_equals_str(key, "folders")) {
setSeparatedList(&options->folders, val, FF_DISK_FOLDER_SEPARATOR);
2023-08-17 16:09:41 +08:00
continue;
}
2023-04-02 00:13:11 +08:00
2026-03-29 09:28:49 +08:00
if (unsafe_yyjson_equals_str(key, "hideFolders")) {
setSeparatedList(&options->hideFolders, val, FF_DISK_FOLDER_SEPARATOR);
2025-05-19 16:42:11 +08:00
continue;
}
2026-03-29 09:28:49 +08:00
if (unsafe_yyjson_equals_str(key, "hideFS")) {
setSeparatedList(&options->hideFS, val, ':');
2025-05-21 16:40:42 +08:00
continue;
}
2026-03-29 09:28:49 +08:00
if (unsafe_yyjson_equals_str(key, "showRegular")) {
if (yyjson_get_bool(val)) {
2025-08-03 09:10:17 +08:00
options->showTypes |= FF_DISK_VOLUME_TYPE_REGULAR_BIT;
2026-03-29 09:28:49 +08:00
} else {
2025-08-03 09:10:17 +08:00
options->showTypes &= ~FF_DISK_VOLUME_TYPE_REGULAR_BIT;
2026-03-29 09:28:49 +08:00
}
2025-08-03 09:10:17 +08:00
continue;
}
2026-03-29 09:28:49 +08:00
if (unsafe_yyjson_equals_str(key, "showExternal")) {
if (yyjson_get_bool(val)) {
2023-08-26 09:32:19 +08:00
options->showTypes |= FF_DISK_VOLUME_TYPE_EXTERNAL_BIT;
2026-03-29 09:28:49 +08:00
} else {
2023-08-26 09:32:19 +08:00
options->showTypes &= ~FF_DISK_VOLUME_TYPE_EXTERNAL_BIT;
2026-03-29 09:28:49 +08:00
}
2023-08-17 16:09:41 +08:00
continue;
}
2023-04-02 00:13:11 +08:00
2026-03-29 09:28:49 +08:00
if (unsafe_yyjson_equals_str(key, "showHidden")) {
if (yyjson_get_bool(val)) {
2023-08-26 09:32:19 +08:00
options->showTypes |= FF_DISK_VOLUME_TYPE_HIDDEN_BIT;
2026-03-29 09:28:49 +08:00
} else {
2023-08-26 09:32:19 +08:00
options->showTypes &= ~FF_DISK_VOLUME_TYPE_HIDDEN_BIT;
2026-03-29 09:28:49 +08:00
}
2023-08-17 16:09:41 +08:00
continue;
}
2023-04-02 00:13:11 +08:00
2026-03-29 09:28:49 +08:00
if (unsafe_yyjson_equals_str(key, "showSubvolumes")) {
if (yyjson_get_bool(val)) {
2023-08-26 09:32:19 +08:00
options->showTypes |= FF_DISK_VOLUME_TYPE_SUBVOLUME_BIT;
2026-03-29 09:28:49 +08:00
} else {
2023-08-26 09:32:19 +08:00
options->showTypes &= ~FF_DISK_VOLUME_TYPE_SUBVOLUME_BIT;
2026-03-29 09:28:49 +08:00
}
2023-08-17 16:09:41 +08:00
continue;
}
2023-04-02 00:13:11 +08:00
2026-03-29 09:28:49 +08:00
if (unsafe_yyjson_equals_str(key, "showReadOnly")) {
if (yyjson_get_bool(val)) {
2023-08-26 12:50:45 +08:00
options->showTypes |= FF_DISK_VOLUME_TYPE_READONLY_BIT;
2026-03-29 09:28:49 +08:00
} else {
2023-08-26 12:50:45 +08:00
options->showTypes &= ~FF_DISK_VOLUME_TYPE_READONLY_BIT;
2026-03-29 09:28:49 +08:00
}
2023-08-26 12:50:45 +08:00
continue;
}
2026-03-29 09:28:49 +08:00
if (unsafe_yyjson_equals_str(key, "showUnknown")) {
if (yyjson_get_bool(val)) {
2023-08-26 09:32:19 +08:00
options->showTypes |= FF_DISK_VOLUME_TYPE_UNKNOWN_BIT;
2026-03-29 09:28:49 +08:00
} else {
2023-08-26 09:32:19 +08:00
options->showTypes &= ~FF_DISK_VOLUME_TYPE_UNKNOWN_BIT;
2026-03-29 09:28:49 +08:00
}
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
2026-03-29 09:28:49 +08:00
if (unsafe_yyjson_equals_str(key, "useAvailable")) {
if (yyjson_get_bool(val)) {
2023-08-26 10:36:00 +08:00
options->calcType = FF_DISK_CALC_TYPE_AVAILABLE;
2026-03-29 09:28:49 +08:00
} else {
2023-08-26 10:36:00 +08:00
options->calcType = FF_DISK_CALC_TYPE_FREE;
2026-03-29 09:28:49 +08:00
}
2023-08-26 10:36:00 +08:00
continue;
}
2026-03-29 09:28:49 +08:00
if (ffPercentParseJsonObject(key, val, &options->percent)) {
continue;
2026-03-29 09:28:49 +08:00
}
ffPrintError(FF_DISK_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "Unknown JSON key %s", unsafe_yyjson_get_str(key));
2023-04-02 00:13:11 +08:00
}
}
2023-08-31 14:13:14 +08:00
2026-03-29 09:28:49 +08:00
void ffGenerateDiskJsonConfig(FFDiskOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) {
ffJsonConfigGenerateModuleArgsConfig(doc, module, &options->moduleArgs);
2023-10-24 14:41:29 +08:00
yyjson_mut_obj_add_bool(doc, module, "showRegular", !!(options->showTypes & FF_DISK_VOLUME_TYPE_REGULAR_BIT));
2023-10-24 14:41:29 +08:00
yyjson_mut_obj_add_bool(doc, module, "showExternal", !!(options->showTypes & FF_DISK_VOLUME_TYPE_EXTERNAL_BIT));
2023-10-24 14:41:29 +08:00
yyjson_mut_obj_add_bool(doc, module, "showHidden", !!(options->showTypes & FF_DISK_VOLUME_TYPE_HIDDEN_BIT));
2023-10-24 14:41:29 +08:00
yyjson_mut_obj_add_bool(doc, module, "showSubvolumes", !!(options->showTypes & FF_DISK_VOLUME_TYPE_SUBVOLUME_BIT));
2023-10-24 14:41:29 +08:00
yyjson_mut_obj_add_bool(doc, module, "showReadOnly", !!(options->showTypes & FF_DISK_VOLUME_TYPE_READONLY_BIT));
2023-10-24 14:41:29 +08:00
yyjson_mut_obj_add_bool(doc, module, "showUnknown", !!(options->showTypes & FF_DISK_VOLUME_TYPE_UNKNOWN_BIT));
2023-10-24 14:41:29 +08:00
yyjson_mut_obj_add_strbuf(doc, module, "folders", &options->folders);
2023-10-24 14:41:29 +08:00
yyjson_mut_obj_add_strbuf(doc, module, "hideFolders", &options->hideFolders);
2025-05-21 08:55:11 +08:00
yyjson_mut_obj_add_strbuf(doc, module, "hideFS", &options->hideFS);
2025-05-21 16:40:42 +08:00
yyjson_mut_obj_add_bool(doc, module, "useAvailable", options->calcType == FF_DISK_CALC_TYPE_AVAILABLE);
ffPercentGenerateJsonConfig(doc, module, options->percent);
2023-10-24 14:41:29 +08:00
}
2026-03-29 09:28:49 +08:00
bool ffGenerateDiskJsonResult(FFDiskOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) {
2026-04-14 10:41:18 +08:00
FF_LIST_AUTO_DESTROY disks = ffListCreate();
2023-08-31 14:13:14 +08:00
const char* error = ffDetectDisks(options, &disks);
2026-03-29 09:28:49 +08:00
if (error) {
yyjson_mut_obj_add_str(doc, module, "error", error);
return false;
2023-08-31 14:13:14 +08:00
}
yyjson_mut_val* arr = yyjson_mut_obj_add_arr(doc, module, "result");
2026-03-29 09:28:49 +08:00
FF_LIST_FOR_EACH (FFDisk, item, disks) {
2023-08-31 14:13:14 +08:00
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");
2026-03-29 09:28:49 +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");
2026-03-29 09:28:49 +08:00
} 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
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");
2026-03-29 09:28:49 +08:00
if (item->type & FF_DISK_VOLUME_TYPE_REGULAR_BIT) {
2023-09-17 20:08:46 +08:00
yyjson_mut_arr_add_str(doc, typeArr, "Regular");
2026-03-29 09:28:49 +08:00
}
if (item->type & FF_DISK_VOLUME_TYPE_EXTERNAL_BIT) {
2023-08-31 14:13:14 +08:00
yyjson_mut_arr_add_str(doc, typeArr, "External");
2026-03-29 09:28:49 +08:00
}
if (item->type & FF_DISK_VOLUME_TYPE_SUBVOLUME_BIT) {
2023-08-31 14:13:14 +08:00
yyjson_mut_arr_add_str(doc, typeArr, "Subvolume");
2026-03-29 09:28:49 +08:00
}
if (item->type & FF_DISK_VOLUME_TYPE_HIDDEN_BIT) {
2023-08-31 14:13:14 +08:00
yyjson_mut_arr_add_str(doc, typeArr, "Hidden");
2026-03-29 09:28:49 +08:00
}
if (item->type & FF_DISK_VOLUME_TYPE_READONLY_BIT) {
2023-08-31 14:13:14 +08:00
yyjson_mut_arr_add_str(doc, typeArr, "Read-only");
2026-03-29 09:28:49 +08:00
}
if (item->type & FF_DISK_VOLUME_TYPE_UNKNOWN_BIT) {
yyjson_mut_arr_add_str(doc, typeArr, "Unknown");
2026-03-29 09:28:49 +08:00
}
2024-04-09 15:15:33 +08:00
const char* pstr = ffTimeToFullStr(item->createTime);
2026-03-29 09:28:49 +08:00
if (*pstr) {
yyjson_mut_obj_add_strcpy(doc, obj, "createTime", pstr);
2026-03-29 09:28:49 +08:00
} else {
yyjson_mut_obj_add_null(doc, obj, "createTime");
2026-03-29 09:28:49 +08:00
}
2023-08-31 14:13:14 +08:00
}
2026-03-29 09:28:49 +08:00
FF_LIST_FOR_EACH (FFDisk, item, disks) {
2023-08-31 14:13:14 +08:00
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);
}
return true;
2023-08-31 14:13:14 +08:00
}
2023-10-07 13:40:40 +08:00
2026-03-29 09:28:49 +08:00
void ffInitDiskOptions(FFDiskOptions* options) {
ffOptionInitModuleArg(&options->moduleArgs, "");
ffStrbufInit(&options->folders);
2026-03-29 09:28:49 +08:00
#if _WIN32 || __APPLE__ || __ANDROID__
ffStrbufInit(&options->hideFolders);
2026-03-29 09:28:49 +08:00
#else
2025-10-11 18:20:56 +08:00
ffStrbufInitS(&options->hideFolders, "/efi:/boot:/boot/*");
2026-03-29 09:28:49 +08:00
#endif
ffStrbufInit(&options->hideFS);
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;
2026-03-30 10:27:23 +08:00
options->percent = (FFPercentageModuleConfig) { 50, 80, 0 };
}
2026-03-29 09:28:49 +08:00
void ffDestroyDiskOptions(FFDiskOptions* options) {
ffOptionDestroyModuleArg(&options->moduleArgs);
ffStrbufDestroy(&options->folders);
ffStrbufDestroy(&options->hideFolders);
ffStrbufDestroy(&options->hideFS);
}
2025-08-03 22:50:56 +08:00
FFModuleBaseInfo ffDiskModuleInfo = {
2024-12-11 10:47:02 +08:00
.name = FF_DISK_MODULE_NAME,
.description = "Print partitions, space usage, file system, etc",
.initOptions = (void*) ffInitDiskOptions,
.destroyOptions = (void*) ffDestroyDiskOptions,
2024-12-11 10:47:02 +08:00
.parseJsonObject = (void*) ffParseDiskJsonObject,
.printModule = (void*) ffPrintDisk,
.generateJsonResult = (void*) ffGenerateDiskJsonResult,
.generateJsonConfig = (void*) ffGenerateDiskJsonConfig,
.formatArgs = FF_FORMAT_ARG_LIST(((FFModuleFormatArg[]) {
2026-03-30 10:27:23 +08:00
{ "Size used", "size-used" },
{ "Size total", "size-total" },
{ "Size percentage num", "size-percentage" },
{ "Files used", "files-used" },
{ "Files total", "files-total" },
{ "Files percentage num", "files-percentage" },
{ "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" },
{ "Size percentage bar", "size-percentage-bar" },
{ "Files percentage bar", "files-percentage-bar" },
{ "Days after creation", "days" },
{ "Hours after creation", "hours" },
{ "Minutes after creation", "minutes" },
{ "Seconds after creation", "seconds" },
{ "Milliseconds after creation", "milliseconds" },
{ "Mount point / drive letter", "mountpoint" },
{ "Mount from (device path)", "mount-from" },
{ "Years integer after creation", "years" },
{ "Days of year after creation", "days-of-year" },
{ "Years fraction after creation", "years-fraction" },
{ "Size free", "size-free" },
{ "Size available", "size-available" },
}))
};