-
-
Notifications
You must be signed in to change notification settings - Fork 882
Expand file tree
/
Copy pathphysicaldisk.c
More file actions
310 lines (270 loc) · 13.5 KB
/
Copy pathphysicaldisk.c
File metadata and controls
310 lines (270 loc) · 13.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
#include "common/printing.h"
#include "common/jsonconfig.h"
#include "common/temps.h"
#include "common/size.h"
#include "detection/physicaldisk/physicaldisk.h"
#include "modules/physicaldisk/physicaldisk.h"
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, "%s (%s)", FF_MODULE_GET_DISPLAY_NAME(PhysicalDisk), dev->name.length ? dev->name.chars : dev->devPath.chars);
} else {
ffStrbufClear(key);
FF_PARSE_FORMAT_STRING_CHECKED(key, &options->moduleArgs.key, ((FFformatarg[]) {
FF_ARG(index, "index"),
FF_ARG(dev->name, "name"),
FF_ARG(dev->devPath, "dev-path"),
FF_ARG(options->moduleArgs.keyIcon, "icon"),
FF_ARG(FF_MODULE_GET_DISPLAY_NAME(PhysicalDisk), "module-name"),
}));
}
}
bool ffPrintPhysicalDisk(FFPhysicalDiskOptions* options) {
FF_LIST_AUTO_DESTROY result = ffListCreate();
const char* error = ffDetectPhysicalDisk(&result, options);
if (error) {
ffPrintError(FF_MODULE_GET_DISPLAY_NAME(PhysicalDisk), 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "%s", error);
return false;
}
ffListSort(&result, sizeof(FFPhysicalDiskResult), (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);
ffSizeAppendNum(dev->size, &buffer);
const char* physicalType = dev->type & FF_PHYSICALDISK_TYPE_VIRTUAL
? "Virtual"
: 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"
: "";
const char* readOnlyType = dev->type & FF_PHYSICALDISK_TYPE_READONLY
? "Read-only"
: "";
if (options->moduleArgs.outputFormat.length == 0) {
ffPrintLogoAndKey(key.chars, 0, &options->moduleArgs, FF_PRINT_TYPE_NO_CUSTOM_KEY);
if (physicalType[0] || removableType[0] || readOnlyType[0]) {
ffStrbufAppendS(&buffer, " [");
if (physicalType[0]) {
ffStrbufAppendS(&buffer, physicalType);
}
if (removableType[0]) {
if (buffer.chars[buffer.length - 1] != '[') {
ffStrbufAppendS(&buffer, ", ");
}
ffStrbufAppendS(&buffer, removableType);
}
if (readOnlyType[0]) {
if (buffer.chars[buffer.length - 1] != '[') {
ffStrbufAppendS(&buffer, ", ");
}
ffStrbufAppendS(&buffer, readOnlyType);
}
ffStrbufAppendC(&buffer, ']');
}
if (dev->temperature != FF_PHYSICALDISK_TEMP_UNSET) {
if (buffer.length > 0) {
ffStrbufAppendS(&buffer, " - ");
}
ffTempsAppendNum(dev->temperature, &buffer, options->tempConfig, &options->moduleArgs);
}
ffStrbufPutTo(&buffer, stdout);
} else {
FF_STRBUF_AUTO_DESTROY tempStr = ffStrbufCreate();
ffTempsAppendNum(dev->temperature, &tempStr, options->tempConfig, &options->moduleArgs);
if (dev->type & FF_PHYSICALDISK_TYPE_READWRITE) {
readOnlyType = "Read-write";
}
FF_PRINT_FORMAT_CHECKED(key.chars, 0, &options->moduleArgs, FF_PRINT_TYPE_NO_CUSTOM_KEY, ((FFformatarg[]) {
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"),
}));
}
++index;
}
FF_LIST_FOR_EACH (FFPhysicalDiskResult, dev, result) {
ffStrbufDestroy(&dev->name);
ffStrbufDestroy(&dev->interconnect);
ffStrbufDestroy(&dev->devPath);
ffStrbufDestroy(&dev->serial);
ffStrbufDestroy(&dev->revision);
}
return true;
}
void ffParsePhysicalDiskJsonObject(FFPhysicalDiskOptions* options, yyjson_val* module) {
yyjson_val *key, *val;
size_t idx, max;
yyjson_obj_foreach (module, idx, max, key, val) {
if (ffJsonConfigParseModuleArgs(key, val, &options->moduleArgs)) {
continue;
}
if (unsafe_yyjson_equals_str(key, "namePrefix")) {
ffStrbufSetJsonVal(&options->namePrefix, val);
continue;
}
if (unsafe_yyjson_equals_str(key, "hideVirtual")) {
if (!yyjson_is_bool(val)) {
ffPrintError(FF_MODULE_GET_DISPLAY_NAME(PhysicalDisk), 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "hideVirtual must be a boolean");
} else {
if (unsafe_yyjson_is_true(val)) {
options->hideType |= FF_PHYSICALDISK_TYPE_VIRTUAL;
} else {
options->hideType &= ~FF_PHYSICALDISK_TYPE_VIRTUAL;
}
}
continue;
}
if (unsafe_yyjson_equals_str(key, "hideUnused")) {
if (!yyjson_is_bool(val)) {
ffPrintError(FF_MODULE_GET_DISPLAY_NAME(PhysicalDisk), 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "hideUnused must be a boolean");
} else {
if (unsafe_yyjson_is_true(val)) {
options->hideType |= FF_PHYSICALDISK_TYPE_UNUSED;
} else {
options->hideType &= ~FF_PHYSICALDISK_TYPE_UNUSED;
}
}
continue;
}
if (ffTempsParseJsonObject(key, val, &options->temp, &options->tempConfig)) {
continue;
}
ffPrintError(FF_MODULE_GET_DISPLAY_NAME(PhysicalDisk), 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "Unknown JSON key %s", unsafe_yyjson_get_str(key));
}
}
void ffGeneratePhysicalDiskJsonConfig(FFPhysicalDiskOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) {
ffJsonConfigGenerateModuleArgsConfig(doc, module, &options->moduleArgs);
yyjson_mut_obj_add_strbuf(doc, module, "namePrefix", &options->namePrefix);
ffTempsGenerateJsonConfig(doc, module, options->temp, options->tempConfig);
yyjson_mut_obj_add_bool(doc, module, "hideVirtual", !!(options->hideType & FF_PHYSICALDISK_TYPE_VIRTUAL));
yyjson_mut_obj_add_bool(doc, module, "hideUnused", !!(options->hideType & FF_PHYSICALDISK_TYPE_UNUSED));
}
bool ffGeneratePhysicalDiskJsonResult(FFPhysicalDiskOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) {
FF_LIST_AUTO_DESTROY result = ffListCreate();
const char* error = ffDetectPhysicalDisk(&result, options);
if (error) {
yyjson_mut_obj_add_str(doc, module, "error", error);
return false;
}
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_VIRTUAL) {
yyjson_mut_obj_add_str(doc, obj, "kind", "Virtual");
} else 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");
}
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");
}
yyjson_mut_obj_add_bool(doc, obj, "unknown", !!(dev->type & FF_PHYSICALDISK_TYPE_UNUSED));
yyjson_mut_obj_add_strbuf(doc, obj, "revision", &dev->revision);
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");
}
}
FF_LIST_FOR_EACH (FFPhysicalDiskResult, dev, result) {
ffStrbufDestroy(&dev->name);
ffStrbufDestroy(&dev->interconnect);
ffStrbufDestroy(&dev->devPath);
ffStrbufDestroy(&dev->serial);
ffStrbufDestroy(&dev->revision);
}
return true;
}
void ffInitPhysicalDiskOptions(FFPhysicalDiskOptions* options) {
ffOptionInitModuleArg(&options->moduleArgs, "");
ffStrbufInit(&options->namePrefix);
options->temp = false;
options->tempConfig = (FFColorRangeConfig) { 50, 70 };
options->hideType = FF_PHYSICALDISK_TYPE_UNUSED;
}
void ffDestroyPhysicalDiskOptions(FFPhysicalDiskOptions* options) {
ffOptionDestroyModuleArg(&options->moduleArgs);
ffStrbufDestroy(&options->namePrefix);
}
FFModuleBaseInfo ffPhysicalDiskModuleInfo = {
.name = "PhysicalDisk",
.description = "Print physical disk information",
.displayName = {
.en = "Physical Disk",
.ar = "القرص الفعلي",
.cs = "Fyzický disk",
.de = "Physische Festplatte",
.es = "Disco físico",
.fr = "Disque physique",
.gl = "Disco físico",
.he = "דיסק פיזי",
.id = "Disk Fisik",
.it = "Disco fisico",
.ja = "物理ディスク",
.ko = "물리 디스크",
.pl = "Dysk fizyczny",
.pt = "Disco físico",
.ru = "Физический диск",
.tr = "Fiziksel Disk",
.uk = "Фізичний диск",
.vi = "Ổ đĩa vật lý",
.zh_CN = "物理磁盘",
.zh_TW = "物理磁碟",
},
.initOptions = (void*) ffInitPhysicalDiskOptions,
.destroyOptions = (void*) ffDestroyPhysicalDiskOptions,
.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" },
})),
.defaultOrder = 68,
};