master ohmu/fastfetch-openbsd / src / modules / btrfs / btrfs.c
ohmu  ·  2026-10-15
  1#include "common/printing.h"
  2#include "common/jsonconfig.h"
  3#include "common/percent.h"
  4#include "common/size.h"
  5#include "common/strutil.h"
  6#include "detection/btrfs/btrfs.h"
  7#include "modules/btrfs/btrfs.h"
  8
  9static void printBtrfs(FFBtrfsOptions* options, FFBtrfsResult* result, uint8_t index) {
 10    FF_STRBUF_AUTO_DESTROY buffer = ffStrbufCreate();
 11    if (options->moduleArgs.key.length == 0) {
 12        if (result->name.length > 0) {
 13            ffStrbufSetF(&buffer, "%s (%s)", FF_MODULE_GET_DISPLAY_NAME(Btrfs), result->name.chars);
 14        } else {
 15            ffStrbufSetS(&buffer, FF_MODULE_GET_DISPLAY_NAME(Btrfs));
 16        }
 17    } else {
 18        ffStrbufClear(&buffer);
 19        FF_PARSE_FORMAT_STRING_CHECKED(&buffer, &options->moduleArgs.key, ((FFformatarg[]) {
 20                                                                              FF_ARG(index, "index"),
 21                                                                              FF_ARG(result->name, "name"),
 22                                                                              FF_ARG(options->moduleArgs.keyIcon, "icon"),
 23                                                                              FF_ARG(FF_MODULE_GET_DISPLAY_NAME(Btrfs), "module-name"),
 24                                                                          }));
 25    }
 26
 27    uint64_t used = 0, allocated = 0, total = result->totalSize;
 28    for (uint32_t i = 0; i < ARRAY_SIZE(result->allocation); ++i) {
 29        uint64_t times = result->allocation[i].copies;
 30        used += result->allocation[i].used * times;
 31        allocated += result->allocation[i].total * times;
 32    }
 33
 34    FF_STRBUF_AUTO_DESTROY usedPretty = ffStrbufCreate();
 35    ffSizeAppendNum(used, &usedPretty);
 36    FF_STRBUF_AUTO_DESTROY allocatedPretty = ffStrbufCreate();
 37    ffSizeAppendNum(allocated, &allocatedPretty);
 38    FF_STRBUF_AUTO_DESTROY totalPretty = ffStrbufCreate();
 39    ffSizeAppendNum(total, &totalPretty);
 40
 41    double usedPercentage = total > 0 ? (double) used / (double) total * 100.0 : 0;
 42    double allocatedPercentage = total > 0 ? (double) allocated / (double) total * 100.0 : 0;
 43
 44    FFPercentageTypeFlags percentType = options->percent.type == 0 ? instance.config.display.percentType : options->percent.type;
 45
 46    if (options->moduleArgs.outputFormat.length == 0) {
 47        ffPrintLogoAndKey(buffer.chars, index, &options->moduleArgs, FF_PRINT_TYPE_NO_CUSTOM_KEY);
 48
 49        ffStrbufClear(&buffer);
 50        ffStrbufSetF(&buffer, "%s / %s (", usedPretty.chars, totalPretty.chars);
 51        ffPercentAppendNum(&buffer, usedPercentage, options->percent, false, &options->moduleArgs);
 52        ffStrbufAppendS(&buffer, ", ");
 53        ffPercentAppendNum(&buffer, allocatedPercentage, options->percent, false, &options->moduleArgs);
 54        ffStrbufAppendF(&buffer, " allocated)");
 55        ffStrbufPutTo(&buffer, stdout);
 56    } else {
 57        FF_STRBUF_AUTO_DESTROY usedPercentageNum = ffStrbufCreate();
 58        if (percentType & FF_PERCENTAGE_TYPE_NUM_BIT) {
 59            ffPercentAppendNum(&usedPercentageNum, usedPercentage, options->percent, false, &options->moduleArgs);
 60        }
 61        FF_STRBUF_AUTO_DESTROY usedPercentageBar = ffStrbufCreate();
 62        if (percentType & FF_PERCENTAGE_TYPE_BAR_BIT) {
 63            ffPercentAppendBar(&usedPercentageBar, usedPercentage, options->percent, &options->moduleArgs);
 64        }
 65
 66        FF_STRBUF_AUTO_DESTROY allocatedPercentageNum = ffStrbufCreate();
 67        if (percentType & FF_PERCENTAGE_TYPE_NUM_BIT) {
 68            ffPercentAppendNum(&allocatedPercentageNum, allocatedPercentage, options->percent, false, &options->moduleArgs);
 69        }
 70        FF_STRBUF_AUTO_DESTROY allocatedPercentageBar = ffStrbufCreate();
 71        if (percentType & FF_PERCENTAGE_TYPE_BAR_BIT) {
 72            ffPercentAppendBar(&allocatedPercentageBar, allocatedPercentage, options->percent, &options->moduleArgs);
 73        }
 74
 75        FF_STRBUF_AUTO_DESTROY nodeSizePretty = ffStrbufCreate();
 76        ffSizeAppendNum(result->nodeSize, &nodeSizePretty);
 77        FF_STRBUF_AUTO_DESTROY sectorSizePretty = ffStrbufCreate();
 78        ffSizeAppendNum(result->sectorSize, &sectorSizePretty);
 79
 80        FF_PRINT_FORMAT_CHECKED(buffer.chars, 0, &options->moduleArgs, FF_PRINT_TYPE_NO_CUSTOM_KEY, ((FFformatarg[]) {
 81                                                                                                        FF_ARG(result->name, "name"),
 82                                                                                                        FF_ARG(result->uuid, "uuid"),
 83                                                                                                        FF_ARG(result->devices, "devices"),
 84                                                                                                        FF_ARG(result->features, "features"),
 85                                                                                                        FF_ARG(usedPretty, "used"),
 86                                                                                                        FF_ARG(allocatedPretty, "allocated"),
 87                                                                                                        FF_ARG(totalPretty, "total"),
 88                                                                                                        FF_ARG(usedPercentageNum, "used-percentage"),
 89                                                                                                        FF_ARG(allocatedPercentageNum, "allocated-percentage"),
 90                                                                                                        FF_ARG(usedPercentageBar, "used-percentage-bar"),
 91                                                                                                        FF_ARG(allocatedPercentageBar, "allocated-percentage-bar"),
 92                                                                                                        FF_ARG(nodeSizePretty, "node-size"),
 93                                                                                                        FF_ARG(sectorSizePretty, "sector-size"),
 94                                                                                                    }));
 95    }
 96}
 97
 98bool ffPrintBtrfs(FFBtrfsOptions* options) {
 99    FF_LIST_AUTO_DESTROY results = ffListCreate();
100
101    const char* error = ffDetectBtrfs(&results);
102
103    if (error) {
104        ffPrintError(FF_MODULE_GET_DISPLAY_NAME(Btrfs), 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "%s", error);
105        return false;
106    }
107    if (results.length == 0) {
108        ffPrintError(FF_MODULE_GET_DISPLAY_NAME(Btrfs), 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "%s", "No btrfs drive found");
109        return false;
110    }
111
112    for (uint32_t i = 0; i < results.length; i++) {
113        FFBtrfsResult* result = FF_LIST_GET(FFBtrfsResult, results, i);
114        uint8_t index = results.length == 1 ? 0 : (uint8_t) (i + 1);
115        printBtrfs(options, result, index);
116    }
117
118    FF_LIST_FOR_EACH (FFBtrfsResult, result, results) {
119        ffStrbufDestroy(&result->name);
120        ffStrbufDestroy(&result->uuid);
121        ffStrbufDestroy(&result->devices);
122        ffStrbufDestroy(&result->features);
123    }
124
125    return true;
126}
127
128void ffParseBtrfsJsonObject(FFBtrfsOptions* options, yyjson_val* module) {
129    yyjson_val *key, *val;
130    size_t idx, max;
131    yyjson_obj_foreach (module, idx, max, key, val) {
132        if (ffJsonConfigParseModuleArgs(key, val, &options->moduleArgs)) {
133            continue;
134        }
135
136        if (ffPercentParseJsonObject(key, val, &options->percent)) {
137            continue;
138        }
139
140        ffPrintError(FF_MODULE_GET_DISPLAY_NAME(Btrfs), 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "Unknown JSON key %s", unsafe_yyjson_get_str(key));
141    }
142}
143
144[[gnu::cold]]
145void ffGenerateBtrfsJsonConfig(FFBtrfsOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) {
146    ffJsonConfigGenerateModuleArgsConfig(doc, module, &options->moduleArgs);
147
148    ffPercentGenerateJsonConfig(doc, module, options->percent);
149}
150
151bool ffGenerateBtrfsJsonResult([[maybe_unused]] FFBtrfsOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) {
152    FF_LIST_AUTO_DESTROY results = ffListCreate();
153
154    const char* error = ffDetectBtrfs(&results);
155    if (error) {
156        yyjson_mut_obj_add_str(doc, module, "error", error);
157        return false;
158    }
159
160    yyjson_mut_val* arr = yyjson_mut_obj_add_arr(doc, module, "result");
161
162    FF_LIST_FOR_EACH (FFBtrfsResult, btrfs, results) {
163        yyjson_mut_val* obj = yyjson_mut_arr_add_obj(doc, arr);
164        yyjson_mut_obj_add_strbuf(doc, obj, "name", &btrfs->name);
165        yyjson_mut_obj_add_strbuf(doc, obj, "uuid", &btrfs->uuid);
166        yyjson_mut_obj_add_strbuf(doc, obj, "devices", &btrfs->devices);
167        yyjson_mut_obj_add_strbuf(doc, obj, "features", &btrfs->features);
168        yyjson_mut_obj_add_uint(doc, obj, "generation", btrfs->generation);
169        yyjson_mut_obj_add_uint(doc, obj, "nodeSize", btrfs->nodeSize);
170        yyjson_mut_obj_add_uint(doc, obj, "sectorSize", btrfs->sectorSize);
171        yyjson_mut_obj_add_uint(doc, obj, "totalSize", btrfs->totalSize);
172        yyjson_mut_val* globalReservation = yyjson_mut_obj_add_obj(doc, obj, "globalReservation");
173        yyjson_mut_obj_add_uint(doc, globalReservation, "used", btrfs->globalReservationUsed);
174        yyjson_mut_obj_add_uint(doc, globalReservation, "total", btrfs->globalReservationTotal);
175        yyjson_mut_val* allocation = yyjson_mut_obj_add_arr(doc, obj, "allocation");
176        for (uint32_t i = 0; i < ARRAY_SIZE(btrfs->allocation); ++i) {
177            yyjson_mut_val* item = yyjson_mut_arr_add_obj(doc, allocation);
178            yyjson_mut_obj_add_str(doc, item, "type", btrfs->allocation[i].type);
179            yyjson_mut_obj_add_str(doc, item, "profile", btrfs->allocation[i].profile);
180            yyjson_mut_obj_add_uint(doc, item, "copies", btrfs->allocation[i].copies);
181            yyjson_mut_obj_add_uint(doc, item, "used", btrfs->allocation[i].used);
182            yyjson_mut_obj_add_uint(doc, item, "total", btrfs->allocation[i].total);
183        }
184    }
185
186    FF_LIST_FOR_EACH (FFBtrfsResult, btrfs, results) {
187        ffStrbufDestroy(&btrfs->name);
188        ffStrbufDestroy(&btrfs->uuid);
189        ffStrbufDestroy(&btrfs->devices);
190        ffStrbufDestroy(&btrfs->features);
191    }
192
193    return true;
194}
195
196void ffInitBtrfsOptions(FFBtrfsOptions* options) {
197    ffOptionInitModuleArg(&options->moduleArgs, "󱑛");
198    options->percent = (FFPercentageModuleConfig) { 50, 80, 0 };
199}
200
201void ffDestroyBtrfsOptions(FFBtrfsOptions* options) {
202    ffOptionDestroyModuleArg(&options->moduleArgs);
203}
204
205FFModuleBaseInfo ffBtrfsModuleInfo = {
206    .name = "Btrfs",
207    .description = "Print Linux BTRFS volumes",
208    .displayName = {
209        .en = "BTRFS",
210        .ar = "BTRFS",
211        .cs = "BTRFS",
212        .de = "BTRFS",
213        .es = "BTRFS",
214        .fi = "BTRFS",
215        .fr = "BTRFS",
216        .gl = "BTRFS",
217        .he = "BTRFS",
218        .id = "BTRFS",
219        .it = "BTRFS",
220        .ja = "BTRFS",
221        .ko = "BTRFS",
222        .pl = "BTRFS",
223        .pt = "BTRFS",
224        .ru = "BTRFS",
225        .tr = "BTRFS",
226        .uk = "BTRFS",
227        .vi = "BTRFS",
228        .zh_CN = "BTRFS",
229        .zh_TW = "BTRFS",
230    },
231    .initOptions = (void*) ffInitBtrfsOptions,
232    .destroyOptions = (void*) ffDestroyBtrfsOptions,
233    .parseJsonObject = (void*) ffParseBtrfsJsonObject,
234    .printModule = (void*) ffPrintBtrfs,
235    .generateJsonResult = (void*) ffGenerateBtrfsJsonResult,
236    .generateJsonConfig = (void*) ffGenerateBtrfsJsonConfig,
237    .formatArgs = FF_FORMAT_ARG_LIST(((FFModuleFormatArg[]) {
238        { "Name / Label *", "name" },
239        { "UUID", "uuid" },
240        { "Associated devices", "devices" },
241        { "Enabled features", "features" },
242        { "Size used", "used" },
243        { "Size allocated", "allocated" },
244        { "Size total", "total" },
245        { "Used percentage num", "used-percentage" },
246        { "Allocated percentage num", "allocated-percentage" },
247        { "Used percentage bar", "used-percentage-bar" },
248        { "Allocated percentage bar", "allocated-percentage-bar" },
249        { "Node size", "node-size" },
250        { "Sector size", "sector-size" },
251    })),
252    .defaultOrder = 43,
253};