2026-01-06 09:48:38 +08:00
|
|
|
#include "common/printing.h"
|
|
|
|
|
#include "common/jsonconfig.h"
|
|
|
|
|
#include "common/temps.h"
|
|
|
|
|
#include "common/size.h"
|
|
|
|
|
#include "common/stringUtils.h"
|
2023-12-18 10:43:20 +08:00
|
|
|
#include "detection/physicaldisk/physicaldisk.h"
|
|
|
|
|
#include "modules/physicaldisk/physicaldisk.h"
|
|
|
|
|
|
|
|
|
|
#define FF_PHYSICALDISK_DISPLAY_NAME "Physical Disk"
|
|
|
|
|
|
|
|
|
|
static int sortDevices(const FFPhysicalDiskResult* left, const FFPhysicalDiskResult* right)
|
|
|
|
|
{
|
|
|
|
|
return ffStrbufComp(&left->name, &right->name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void formatKey(const FFPhysicalDiskOptions* options, FFPhysicalDiskResult* dev, uint32_t index, FFstrbuf* key)
|
|
|
|
|
{
|
|
|
|
|
if(options->moduleArgs.key.length == 0)
|
|
|
|
|
{
|
|
|
|
|
ffStrbufSetF(key, FF_PHYSICALDISK_DISPLAY_NAME " (%s)", dev->name.length ? dev->name.chars : dev->devPath.chars);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
ffStrbufClear(key);
|
2024-12-11 10:47:02 +08:00
|
|
|
FF_PARSE_FORMAT_STRING_CHECKED(key, &options->moduleArgs.key, ((FFformatarg[]){
|
2026-03-12 15:07:19 +08:00
|
|
|
FF_ARG(index, "index"),
|
|
|
|
|
FF_ARG(dev->name, "name"),
|
|
|
|
|
FF_ARG(dev->devPath, "dev-path"),
|
|
|
|
|
FF_ARG(options->moduleArgs.keyIcon, "icon"),
|
2024-03-07 10:47:28 +08:00
|
|
|
}));
|
2023-12-18 10:43:20 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-18 13:38:30 +08:00
|
|
|
bool ffPrintPhysicalDisk(FFPhysicalDiskOptions* options)
|
2023-12-18 10:43:20 +08:00
|
|
|
{
|
|
|
|
|
FF_LIST_AUTO_DESTROY result = ffListCreate(sizeof(FFPhysicalDiskResult));
|
|
|
|
|
const char* error = ffDetectPhysicalDisk(&result, options);
|
|
|
|
|
|
|
|
|
|
if(error)
|
|
|
|
|
{
|
2024-03-07 11:10:18 +08:00
|
|
|
ffPrintError(FF_PHYSICALDISK_DISPLAY_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "%s", error);
|
2025-08-18 13:38:30 +08:00
|
|
|
return false;
|
2023-12-18 10:43:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ffListSort(&result, (const void*) sortDevices);
|
|
|
|
|
|
|
|
|
|
uint32_t index = 0;
|
|
|
|
|
FF_STRBUF_AUTO_DESTROY key = ffStrbufCreate();
|
|
|
|
|
FF_STRBUF_AUTO_DESTROY buffer = ffStrbufCreate();
|
|
|
|
|
|
|
|
|
|
FF_LIST_FOR_EACH(FFPhysicalDiskResult, dev, result)
|
|
|
|
|
{
|
|
|
|
|
formatKey(options, dev, result.length == 1 ? 0 : index + 1, &key);
|
|
|
|
|
ffStrbufClear(&buffer);
|
2025-07-30 14:30:36 +08:00
|
|
|
ffSizeAppendNum(dev->size, &buffer);
|
2023-12-18 10:43:20 +08:00
|
|
|
|
|
|
|
|
const char* physicalType = dev->type & FF_PHYSICALDISK_TYPE_HDD
|
|
|
|
|
? "HDD"
|
|
|
|
|
: dev->type & FF_PHYSICALDISK_TYPE_SSD
|
|
|
|
|
? "SSD"
|
|
|
|
|
: "";
|
|
|
|
|
const char* removableType = dev->type & FF_PHYSICALDISK_TYPE_REMOVABLE
|
|
|
|
|
? "Removable"
|
|
|
|
|
: dev->type & FF_PHYSICALDISK_TYPE_FIXED
|
|
|
|
|
? "Fixed"
|
|
|
|
|
: "";
|
2023-12-25 08:55:17 +08:00
|
|
|
const char* readOnlyType = dev->type & FF_PHYSICALDISK_TYPE_READONLY
|
|
|
|
|
? "Read-only"
|
|
|
|
|
: "";
|
2023-12-18 10:43:20 +08:00
|
|
|
|
|
|
|
|
if(options->moduleArgs.outputFormat.length == 0)
|
|
|
|
|
{
|
2024-06-27 17:49:30 +08:00
|
|
|
ffPrintLogoAndKey(key.chars, 0, &options->moduleArgs, FF_PRINT_TYPE_NO_CUSTOM_KEY);
|
2023-12-18 10:43:20 +08:00
|
|
|
|
2023-12-25 08:55:17 +08:00
|
|
|
if (physicalType[0] || removableType[0] || readOnlyType[0])
|
2023-12-18 10:43:20 +08:00
|
|
|
{
|
|
|
|
|
ffStrbufAppendS(&buffer, " [");
|
|
|
|
|
if (physicalType[0])
|
|
|
|
|
ffStrbufAppendS(&buffer, physicalType);
|
|
|
|
|
if (removableType[0])
|
|
|
|
|
{
|
2023-12-25 08:55:17 +08:00
|
|
|
if (buffer.chars[buffer.length - 1] != '[')
|
2023-12-18 10:43:20 +08:00
|
|
|
ffStrbufAppendS(&buffer, ", ");
|
|
|
|
|
ffStrbufAppendS(&buffer, removableType);
|
|
|
|
|
}
|
2023-12-25 08:55:17 +08:00
|
|
|
if (readOnlyType[0])
|
|
|
|
|
{
|
|
|
|
|
if (buffer.chars[buffer.length - 1] != '[')
|
|
|
|
|
ffStrbufAppendS(&buffer, ", ");
|
|
|
|
|
ffStrbufAppendS(&buffer, readOnlyType);
|
|
|
|
|
}
|
2023-12-18 10:43:20 +08:00
|
|
|
ffStrbufAppendC(&buffer, ']');
|
|
|
|
|
}
|
2023-12-26 19:23:26 +08:00
|
|
|
|
2025-08-14 09:47:14 +08:00
|
|
|
if (dev->temperature != FF_PHYSICALDISK_TEMP_UNSET)
|
2023-12-26 19:23:26 +08:00
|
|
|
{
|
|
|
|
|
if(buffer.length > 0)
|
|
|
|
|
ffStrbufAppendS(&buffer, " - ");
|
|
|
|
|
|
2024-05-08 15:25:59 +08:00
|
|
|
ffTempsAppendNum(dev->temperature, &buffer, options->tempConfig, &options->moduleArgs);
|
2023-12-26 19:23:26 +08:00
|
|
|
}
|
2023-12-18 10:43:20 +08:00
|
|
|
ffStrbufPutTo(&buffer, stdout);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2024-02-26 10:46:13 +08:00
|
|
|
FF_STRBUF_AUTO_DESTROY tempStr = ffStrbufCreate();
|
2024-05-08 15:25:59 +08:00
|
|
|
ffTempsAppendNum(dev->temperature, &tempStr, options->tempConfig, &options->moduleArgs);
|
2023-12-25 18:33:39 +08:00
|
|
|
if (dev->type & FF_PHYSICALDISK_TYPE_READWRITE)
|
|
|
|
|
readOnlyType = "Read-write";
|
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-12 15:07:19 +08:00
|
|
|
FF_ARG(buffer, "size"),
|
|
|
|
|
FF_ARG(dev->name, "name"),
|
|
|
|
|
FF_ARG(dev->interconnect, "interconnect"),
|
|
|
|
|
FF_ARG(dev->devPath, "dev-path"),
|
|
|
|
|
FF_ARG(dev->serial, "serial"),
|
|
|
|
|
FF_ARG(physicalType, "physical-type"),
|
|
|
|
|
FF_ARG(removableType, "removable-type"),
|
|
|
|
|
FF_ARG(readOnlyType, "readonly-type"),
|
|
|
|
|
FF_ARG(dev->revision, "revision"),
|
|
|
|
|
FF_ARG(tempStr, "temperature"),
|
2024-03-07 10:47:28 +08:00
|
|
|
}));
|
2023-12-18 10:43:20 +08:00
|
|
|
}
|
|
|
|
|
++index;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FF_LIST_FOR_EACH(FFPhysicalDiskResult, dev, result)
|
|
|
|
|
{
|
|
|
|
|
ffStrbufDestroy(&dev->name);
|
|
|
|
|
ffStrbufDestroy(&dev->interconnect);
|
|
|
|
|
ffStrbufDestroy(&dev->devPath);
|
|
|
|
|
ffStrbufDestroy(&dev->serial);
|
2024-03-01 10:41:42 +08:00
|
|
|
ffStrbufDestroy(&dev->revision);
|
2023-12-18 10:43:20 +08:00
|
|
|
}
|
2025-08-18 13:38:30 +08:00
|
|
|
|
|
|
|
|
return true;
|
2023-12-18 10:43:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ffParsePhysicalDiskJsonObject(FFPhysicalDiskOptions* options, yyjson_val* module)
|
|
|
|
|
{
|
2025-08-02 18:51:03 +08:00
|
|
|
yyjson_val *key, *val;
|
2023-12-18 10:43:20 +08:00
|
|
|
size_t idx, max;
|
2025-08-02 18:51:03 +08:00
|
|
|
yyjson_obj_foreach(module, idx, max, key, val)
|
2023-12-18 10:43:20 +08:00
|
|
|
{
|
|
|
|
|
if (ffJsonConfigParseModuleArgs(key, val, &options->moduleArgs))
|
|
|
|
|
continue;
|
|
|
|
|
|
2025-08-02 18:51:03 +08:00
|
|
|
if (unsafe_yyjson_equals_str(key, "namePrefix"))
|
2023-12-18 10:43:20 +08:00
|
|
|
{
|
2025-08-02 17:41:19 +08:00
|
|
|
ffStrbufSetJsonVal(&options->namePrefix, val);
|
2023-12-18 10:43:20 +08:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-26 16:39:41 +08:00
|
|
|
if (ffTempsParseJsonObject(key, val, &options->temp, &options->tempConfig))
|
2023-12-26 19:23:26 +08:00
|
|
|
continue;
|
|
|
|
|
|
2025-08-02 18:51:03 +08:00
|
|
|
ffPrintError(FF_PHYSICALDISK_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "Unknown JSON key %s", unsafe_yyjson_get_str(key));
|
2023-12-18 10:43:20 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ffGeneratePhysicalDiskJsonConfig(FFPhysicalDiskOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module)
|
|
|
|
|
{
|
2025-08-04 16:46:28 +08:00
|
|
|
ffJsonConfigGenerateModuleArgsConfig(doc, module, &options->moduleArgs);
|
2023-12-18 10:43:20 +08:00
|
|
|
|
2025-08-04 16:46:28 +08:00
|
|
|
yyjson_mut_obj_add_strbuf(doc, module, "namePrefix", &options->namePrefix);
|
2023-12-18 10:43:20 +08:00
|
|
|
|
2025-08-04 16:46:28 +08:00
|
|
|
ffTempsGenerateJsonConfig(doc, module, options->temp, options->tempConfig);
|
2023-12-18 10:43:20 +08:00
|
|
|
}
|
|
|
|
|
|
2025-08-18 13:38:30 +08:00
|
|
|
bool ffGeneratePhysicalDiskJsonResult(FFPhysicalDiskOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module)
|
2023-12-18 10:43:20 +08:00
|
|
|
{
|
|
|
|
|
FF_LIST_AUTO_DESTROY result = ffListCreate(sizeof(FFPhysicalDiskResult));
|
|
|
|
|
const char* error = ffDetectPhysicalDisk(&result, options);
|
|
|
|
|
|
|
|
|
|
if(error)
|
|
|
|
|
{
|
|
|
|
|
yyjson_mut_obj_add_str(doc, module, "error", error);
|
2025-08-18 13:38:30 +08:00
|
|
|
return false;
|
2023-12-18 10:43:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
yyjson_mut_val* arr = yyjson_mut_obj_add_arr(doc, module, "result");
|
|
|
|
|
FF_LIST_FOR_EACH(FFPhysicalDiskResult, dev, result)
|
|
|
|
|
{
|
|
|
|
|
yyjson_mut_val* obj = yyjson_mut_arr_add_obj(doc, arr);
|
|
|
|
|
yyjson_mut_obj_add_strbuf(doc, obj, "name", &dev->name);
|
|
|
|
|
yyjson_mut_obj_add_strbuf(doc, obj, "devPath", &dev->devPath);
|
|
|
|
|
yyjson_mut_obj_add_strbuf(doc, obj, "interconnect", &dev->interconnect);
|
|
|
|
|
|
|
|
|
|
if (dev->type & FF_PHYSICALDISK_TYPE_HDD)
|
|
|
|
|
yyjson_mut_obj_add_str(doc, obj, "kind", "HDD");
|
|
|
|
|
else if (dev->type & FF_PHYSICALDISK_TYPE_SSD)
|
|
|
|
|
yyjson_mut_obj_add_str(doc, obj, "kind", "SSD");
|
|
|
|
|
else
|
|
|
|
|
yyjson_mut_obj_add_null(doc, obj, "kind");
|
|
|
|
|
|
|
|
|
|
yyjson_mut_obj_add_uint(doc, obj, "size", dev->size);
|
|
|
|
|
yyjson_mut_obj_add_strbuf(doc, obj, "serial", &dev->serial);
|
|
|
|
|
|
|
|
|
|
if (dev->type & FF_PHYSICALDISK_TYPE_REMOVABLE)
|
|
|
|
|
yyjson_mut_obj_add_bool(doc, obj, "removable", true);
|
|
|
|
|
else if (dev->type & FF_PHYSICALDISK_TYPE_FIXED)
|
|
|
|
|
yyjson_mut_obj_add_bool(doc, obj, "removable", false);
|
|
|
|
|
else
|
|
|
|
|
yyjson_mut_obj_add_null(doc, obj, "removable");
|
2023-12-25 08:55:17 +08:00
|
|
|
|
|
|
|
|
if (dev->type & FF_PHYSICALDISK_TYPE_READONLY)
|
|
|
|
|
yyjson_mut_obj_add_bool(doc, obj, "readOnly", true);
|
|
|
|
|
else if (dev->type & FF_PHYSICALDISK_TYPE_READWRITE)
|
|
|
|
|
yyjson_mut_obj_add_bool(doc, obj, "readOnly", false);
|
|
|
|
|
else
|
|
|
|
|
yyjson_mut_obj_add_null(doc, obj, "readOnly");
|
2023-12-25 18:33:39 +08:00
|
|
|
|
|
|
|
|
yyjson_mut_obj_add_strbuf(doc, obj, "revision", &dev->revision);
|
2023-12-26 19:23:26 +08:00
|
|
|
|
2025-08-14 09:47:14 +08:00
|
|
|
if (dev->temperature != FF_PHYSICALDISK_TEMP_UNSET)
|
|
|
|
|
yyjson_mut_obj_add_real(doc, obj, "temperature", dev->temperature);
|
|
|
|
|
else
|
|
|
|
|
yyjson_mut_obj_add_null(doc, obj, "temperature");
|
2023-12-18 10:43:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FF_LIST_FOR_EACH(FFPhysicalDiskResult, dev, result)
|
|
|
|
|
{
|
|
|
|
|
ffStrbufDestroy(&dev->name);
|
|
|
|
|
ffStrbufDestroy(&dev->interconnect);
|
|
|
|
|
ffStrbufDestroy(&dev->devPath);
|
2024-03-01 10:41:42 +08:00
|
|
|
ffStrbufDestroy(&dev->serial);
|
|
|
|
|
ffStrbufDestroy(&dev->revision);
|
2023-12-18 10:43:20 +08:00
|
|
|
}
|
2025-08-18 13:38:30 +08:00
|
|
|
|
|
|
|
|
return true;
|
2023-12-18 10:43:20 +08:00
|
|
|
}
|
|
|
|
|
|
2025-08-04 14:25:14 +08:00
|
|
|
void ffInitPhysicalDiskOptions(FFPhysicalDiskOptions* options)
|
|
|
|
|
{
|
|
|
|
|
ffOptionInitModuleArg(&options->moduleArgs, "");
|
|
|
|
|
|
|
|
|
|
ffStrbufInit(&options->namePrefix);
|
|
|
|
|
options->temp = false;
|
|
|
|
|
options->tempConfig = (FFColorRangeConfig) { 50, 70 };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ffDestroyPhysicalDiskOptions(FFPhysicalDiskOptions* options)
|
|
|
|
|
{
|
|
|
|
|
ffOptionDestroyModuleArg(&options->moduleArgs);
|
|
|
|
|
ffStrbufDestroy(&options->namePrefix);
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-03 22:50:56 +08:00
|
|
|
FFModuleBaseInfo ffPhysicalDiskModuleInfo = {
|
2024-12-11 10:47:02 +08:00
|
|
|
.name = FF_PHYSICALDISK_MODULE_NAME,
|
|
|
|
|
.description = "Print physical disk information",
|
2025-08-03 18:39:41 +08:00
|
|
|
.initOptions = (void*) ffInitPhysicalDiskOptions,
|
|
|
|
|
.destroyOptions = (void*) ffDestroyPhysicalDiskOptions,
|
2024-12-11 10:47:02 +08:00
|
|
|
.parseJsonObject = (void*) ffParsePhysicalDiskJsonObject,
|
|
|
|
|
.printModule = (void*) ffPrintPhysicalDisk,
|
|
|
|
|
.generateJsonResult = (void*) ffGeneratePhysicalDiskJsonResult,
|
|
|
|
|
.generateJsonConfig = (void*) ffGeneratePhysicalDiskJsonConfig,
|
|
|
|
|
.formatArgs = FF_FORMAT_ARG_LIST(((FFModuleFormatArg[]) {
|
|
|
|
|
{"Device size (formatted)", "size"},
|
|
|
|
|
{"Device name", "name"},
|
|
|
|
|
{"Device interconnect type", "interconnect"},
|
|
|
|
|
{"Device raw file path", "dev-path"},
|
|
|
|
|
{"Serial number", "serial"},
|
|
|
|
|
{"Device kind (SSD or HDD)", "physical-type"},
|
|
|
|
|
{"Device kind (Removable or Fixed)", "removable-type"},
|
|
|
|
|
{"Device kind (Read-only or Read-write)", "readonly-type"},
|
|
|
|
|
{"Product revision", "revision"},
|
|
|
|
|
{"Device temperature (formatted)", "temperature"},
|
|
|
|
|
}))
|
|
|
|
|
};
|