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

219 lines
11 KiB
C
Raw Normal View History

2026-01-06 09:48:38 +08:00
#include "common/printing.h"
#include "common/jsonconfig.h"
#include "common/percent.h"
#include "common/size.h"
#include "common/FFstrbuf.h"
#include "common/stringUtils.h"
2026-01-05 16:19:47 +08:00
#include "detection/zpool/zpool.h"
#include "modules/zpool/zpool.h"
2024-08-28 10:55:04 +08:00
2026-03-29 09:28:49 +08:00
static void printZpool(FFZpoolOptions* options, FFZpoolResult* result, uint8_t index) {
2024-08-28 10:55:04 +08:00
FF_STRBUF_AUTO_DESTROY buffer = ffStrbufCreate();
2026-03-29 09:28:49 +08:00
if (options->moduleArgs.key.length == 0) {
if (result->name.length > 0) {
2024-08-28 10:55:04 +08:00
ffStrbufSetF(&buffer, "%s (%s)", FF_ZPOOL_MODULE_NAME, result->name.chars);
2026-03-29 09:28:49 +08:00
} else {
2024-08-28 10:55:04 +08:00
ffStrbufSetS(&buffer, FF_ZPOOL_MODULE_NAME);
2026-03-29 09:28:49 +08:00
}
} else {
2024-08-28 10:55:04 +08:00
ffStrbufClear(&buffer);
2024-12-11 10:47:02 +08:00
FF_PARSE_FORMAT_STRING_CHECKED(&buffer, &options->moduleArgs.key, ((FFformatarg[]) {
2026-03-29 09:28:49 +08:00
FF_ARG(index, "index"),
FF_ARG(result->name, "name"),
FF_ARG(result->guid, "guid"),
FF_ARG(options->moduleArgs.keyIcon, "icon"),
}));
2024-08-28 10:55:04 +08:00
}
FF_STRBUF_AUTO_DESTROY usedPretty = ffStrbufCreate();
2025-07-30 14:30:36 +08:00
ffSizeAppendNum(result->used, &usedPretty);
2024-08-28 10:55:04 +08:00
FF_STRBUF_AUTO_DESTROY allocatedPretty = ffStrbufCreate();
ffSizeAppendNum(result->allocated, &allocatedPretty);
2024-08-28 10:55:04 +08:00
FF_STRBUF_AUTO_DESTROY totalPretty = ffStrbufCreate();
2025-07-30 14:30:36 +08:00
ffSizeAppendNum(result->total, &totalPretty);
2024-08-28 10:55:04 +08:00
double usedPercentage = result->total > 0 ? (double) result->used / (double) result->total * 100.0 : 0;
double allocatedPercentage = result->total > 0 ? (double) result->allocated / (double) result->total * 100.0 : 0;
FFPercentageTypeFlags percentType = options->percent.type == 0 ? instance.config.display.percentType : options->percent.type;
2024-08-28 10:55:04 +08:00
2026-03-29 09:28:49 +08:00
if (options->moduleArgs.outputFormat.length == 0) {
2024-08-28 10:55:04 +08:00
ffPrintLogoAndKey(buffer.chars, index, &options->moduleArgs, FF_PRINT_TYPE_NO_CUSTOM_KEY);
ffStrbufClear(&buffer);
ffStrbufSetF(&buffer, "%s / %s (", usedPretty.chars, totalPretty.chars);
ffPercentAppendNum(&buffer, usedPercentage, options->percent, false, &options->moduleArgs);
2024-08-28 10:55:04 +08:00
ffStrbufAppendS(&buffer, ", ");
ffPercentAppendNum(&buffer, allocatedPercentage, options->percent, false, &options->moduleArgs);
ffStrbufAppendS(&buffer, " allocated, ");
2024-10-13 17:18:53 +08:00
ffPercentAppendNum(&buffer, result->fragmentation, options->percent, false, &options->moduleArgs);
2024-08-30 21:24:29 +08:00
ffStrbufAppendF(&buffer, " frag) - %s", result->state.chars);
2026-03-29 09:28:49 +08:00
if (result->readOnly) {
ffStrbufAppendS(&buffer, " [Read-only]");
2026-03-29 09:28:49 +08:00
}
2024-08-28 10:55:04 +08:00
ffStrbufPutTo(&buffer, stdout);
2026-03-29 09:28:49 +08:00
} else {
FF_STRBUF_AUTO_DESTROY usedPercentageNum = ffStrbufCreate();
2026-03-29 09:28:49 +08:00
if (percentType & FF_PERCENTAGE_TYPE_NUM_BIT) {
ffPercentAppendNum(&usedPercentageNum, usedPercentage, options->percent, false, &options->moduleArgs);
2026-03-29 09:28:49 +08:00
}
FF_STRBUF_AUTO_DESTROY usedPercentageBar = ffStrbufCreate();
2026-03-29 09:28:49 +08:00
if (percentType & FF_PERCENTAGE_TYPE_BAR_BIT) {
ffPercentAppendBar(&usedPercentageBar, usedPercentage, options->percent, &options->moduleArgs);
2026-03-29 09:28:49 +08:00
}
FF_STRBUF_AUTO_DESTROY allocatedPercentageNum = ffStrbufCreate();
2026-03-29 09:28:49 +08:00
if (percentType & FF_PERCENTAGE_TYPE_NUM_BIT) {
ffPercentAppendNum(&allocatedPercentageNum, allocatedPercentage, options->percent, false, &options->moduleArgs);
2026-03-29 09:28:49 +08:00
}
FF_STRBUF_AUTO_DESTROY allocatedPercentageBar = ffStrbufCreate();
2026-03-29 09:28:49 +08:00
if (percentType & FF_PERCENTAGE_TYPE_BAR_BIT) {
ffPercentAppendBar(&allocatedPercentageBar, allocatedPercentage, options->percent, &options->moduleArgs);
2026-03-29 09:28:49 +08:00
}
2024-08-28 10:55:04 +08:00
FF_STRBUF_AUTO_DESTROY fragPercentageNum = ffStrbufCreate();
2026-03-29 09:28:49 +08:00
if (percentType & FF_PERCENTAGE_TYPE_NUM_BIT) {
ffPercentAppendNum(&fragPercentageNum, result->fragmentation, options->percent, false, &options->moduleArgs);
2026-03-29 09:28:49 +08:00
}
2024-08-28 10:55:04 +08:00
FF_STRBUF_AUTO_DESTROY fragPercentageBar = ffStrbufCreate();
2026-03-29 09:28:49 +08:00
if (percentType & FF_PERCENTAGE_TYPE_BAR_BIT) {
ffPercentAppendBar(&fragPercentageBar, result->fragmentation, options->percent, &options->moduleArgs);
2026-03-29 09:28:49 +08:00
}
2024-08-28 10:55:04 +08:00
2024-12-11 10:47:02 +08:00
FF_PRINT_FORMAT_CHECKED(buffer.chars, 0, &options->moduleArgs, FF_PRINT_TYPE_NO_CUSTOM_KEY, ((FFformatarg[]) {
2026-03-29 09:28:49 +08:00
FF_ARG(result->name, "name"),
FF_ARG(result->guid, "guid"),
FF_ARG(result->state, "state"),
FF_ARG(usedPretty, "size-used"),
FF_ARG(allocatedPretty, "size-allocated"),
FF_ARG(totalPretty, "size-total"),
FF_ARG(usedPercentageNum, "used-percentage"),
FF_ARG(allocatedPercentageNum, "allocated-percentage"),
FF_ARG(fragPercentageNum, "frag-percentage"),
FF_ARG(usedPercentageBar, "used-percentage-bar"),
FF_ARG(allocatedPercentageBar, "allocated-percentage-bar"),
FF_ARG(fragPercentageBar, "frag-percentage-bar"),
FF_ARG(result->readOnly, "is-readonly"),
}));
2024-08-28 10:55:04 +08:00
}
}
2026-03-29 09:28:49 +08:00
bool ffPrintZpool(FFZpoolOptions* options) {
2024-08-28 10:55:04 +08:00
FF_LIST_AUTO_DESTROY results = ffListCreate(sizeof(FFZpoolResult));
const char* error = ffDetectZpool(&results);
2026-03-29 09:28:49 +08:00
if (error) {
2024-08-28 10:55:04 +08:00
ffPrintError(FF_ZPOOL_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "%s", error);
return false;
2024-08-28 10:55:04 +08:00
}
2026-03-29 09:28:49 +08:00
if (results.length == 0) {
2024-08-28 10:55:04 +08:00
ffPrintError(FF_ZPOOL_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "%s", "No zpool found");
return false;
2024-08-28 10:55:04 +08:00
}
2026-03-29 09:28:49 +08:00
for (uint32_t i = 0; i < results.length; i++) {
2024-08-28 10:55:04 +08:00
FFZpoolResult* result = FF_LIST_GET(FFZpoolResult, results, i);
uint8_t index = results.length == 1 ? 0 : (uint8_t) (i + 1);
printZpool(options, result, index);
}
2026-03-29 09:28:49 +08:00
FF_LIST_FOR_EACH (FFZpoolResult, result, results) {
2024-08-28 10:55:04 +08:00
ffStrbufDestroy(&result->name);
ffStrbufDestroy(&result->state);
}
return true;
2024-08-28 10:55:04 +08:00
}
2026-03-29 09:28:49 +08:00
void ffParseZpoolJsonObject(FFZpoolOptions* options, yyjson_val* module) {
yyjson_val *key, *val;
2024-08-28 10:55:04 +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)) {
2024-08-28 10:55:04 +08:00
continue;
2026-03-29 09:28:49 +08:00
}
2024-08-28 10:55:04 +08:00
2026-03-29 09:28:49 +08:00
if (ffPercentParseJsonObject(key, val, &options->percent)) {
2024-08-28 10:55:04 +08:00
continue;
2026-03-29 09:28:49 +08:00
}
2024-08-28 10:55:04 +08:00
ffPrintError(FF_ZPOOL_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "Unknown JSON key %s", unsafe_yyjson_get_str(key));
2024-08-28 10:55:04 +08:00
}
}
2026-03-29 09:28:49 +08:00
void ffGenerateZpoolJsonConfig(FFZpoolOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) {
ffJsonConfigGenerateModuleArgsConfig(doc, module, &options->moduleArgs);
2024-08-28 10:55:04 +08:00
ffPercentGenerateJsonConfig(doc, module, options->percent);
2024-08-28 10:55:04 +08:00
}
2026-03-29 09:28:49 +08:00
bool ffGenerateZpoolJsonResult(FF_MAYBE_UNUSED FFZpoolOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) {
2024-08-28 10:55:04 +08:00
FF_LIST_AUTO_DESTROY results = ffListCreate(sizeof(FFZpoolResult));
const char* error = ffDetectZpool(&results);
2026-03-29 09:28:49 +08:00
if (error) {
2024-08-28 10:55:04 +08:00
yyjson_mut_obj_add_str(doc, module, "error", error);
return false;
2024-08-28 10:55:04 +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 (FFZpoolResult, zpool, results) {
2024-08-28 10:55:04 +08:00
yyjson_mut_val* obj = yyjson_mut_arr_add_obj(doc, arr);
yyjson_mut_obj_add_strbuf(doc, obj, "name", &zpool->name);
yyjson_mut_obj_add_strbuf(doc, obj, "state", &zpool->state);
yyjson_mut_obj_add_uint(doc, obj, "guid", zpool->guid);
2024-08-28 10:55:04 +08:00
yyjson_mut_obj_add_uint(doc, obj, "used", zpool->used);
yyjson_mut_obj_add_uint(doc, obj, "allocated", zpool->allocated);
2024-08-28 10:55:04 +08:00
yyjson_mut_obj_add_uint(doc, obj, "total", zpool->total);
2026-03-29 09:28:49 +08:00
if (zpool->fragmentation != -DBL_MAX) {
2025-08-14 09:47:14 +08:00
yyjson_mut_obj_add_real(doc, obj, "fragmentation", zpool->fragmentation);
2026-03-29 09:28:49 +08:00
} else {
2025-08-14 09:47:14 +08:00
yyjson_mut_obj_add_null(doc, obj, "fragmentation");
2026-03-29 09:28:49 +08:00
}
yyjson_mut_obj_add_bool(doc, obj, "readOnly", zpool->readOnly);
2024-08-28 10:55:04 +08:00
}
2026-03-29 09:28:49 +08:00
FF_LIST_FOR_EACH (FFZpoolResult, zpool, results) {
2024-09-14 16:18:35 +08:00
ffStrbufDestroy(&zpool->name);
ffStrbufDestroy(&zpool->state);
2024-08-28 10:55:04 +08:00
}
return true;
2024-08-28 10:55:04 +08:00
}
2026-03-29 09:28:49 +08:00
void ffInitZpoolOptions(FFZpoolOptions* options) {
ffOptionInitModuleArg(&options->moduleArgs, "󱑛");
2026-03-29 09:28:49 +08:00
options->percent = (FFPercentageModuleConfig) {50, 80, 0};
}
2026-03-29 09:28:49 +08:00
void ffDestroyZpoolOptions(FFZpoolOptions* options) {
ffOptionDestroyModuleArg(&options->moduleArgs);
}
2025-08-03 22:50:56 +08:00
FFModuleBaseInfo ffZpoolModuleInfo = {
2024-12-11 10:47:02 +08:00
.name = FF_ZPOOL_MODULE_NAME,
.description = "Print ZFS storage pools",
.initOptions = (void*) ffInitZpoolOptions,
.destroyOptions = (void*) ffDestroyZpoolOptions,
2024-12-11 10:47:02 +08:00
.parseJsonObject = (void*) ffParseZpoolJsonObject,
.printModule = (void*) ffPrintZpool,
.generateJsonResult = (void*) ffGenerateZpoolJsonResult,
.generateJsonConfig = (void*) ffGenerateZpoolJsonConfig,
.formatArgs = FF_FORMAT_ARG_LIST(((FFModuleFormatArg[]) {
{"Zpool name", "name"},
{"Zpool guid", "guid"},
2024-12-11 10:47:02 +08:00
{"Zpool state", "state"},
{"Size used", "used"},
{"Size allocated", "allocated"},
2024-12-11 10:47:02 +08:00
{"Size total", "total"},
{"Size used percentage num", "used-percentage"},
{"Size allocated percentage num", "allocated-percentage"},
2024-12-11 10:47:02 +08:00
{"Fragmentation percentage num", "fragmentation-percentage"},
{"Size used percentage bar", "used-percentage-bar"},
{"Size allocated percentage bar", "allocated-percentage-bar"},
2024-12-11 10:47:02 +08:00
{"Fragmentation percentage bar", "fragmentation-percentage-bar"},
{"Is read-only", "is-readonly"},
}))
};