|
| 1 | +#include "physicaldisk.h" |
| 2 | +#include "util/stringUtils.h" |
| 3 | +#include "sys/scsi/generic/inquiry.h" |
| 4 | + |
| 5 | +#include <libdevinfo.h> |
| 6 | + |
| 7 | +struct FFWalkTreeBundle |
| 8 | +{ |
| 9 | + FFPhysicalDiskOptions* options; |
| 10 | + FFlist* disks; |
| 11 | +}; |
| 12 | + |
| 13 | +static int walkDevTree(di_node_t node, struct FFWalkTreeBundle* bundle) |
| 14 | +{ |
| 15 | + if (ffStrEquals(di_node_name(node), "sd")) |
| 16 | + { |
| 17 | + char* productId; |
| 18 | + char* vendorId; |
| 19 | + if (di_prop_lookup_strings(DDI_DEV_T_ANY, node, "inquiry-product-id", &productId) > 0 |
| 20 | + && di_prop_lookup_strings(DDI_DEV_T_ANY, node, "inquiry-vendor-id", &vendorId) > 0) |
| 21 | + { |
| 22 | + FF_STRBUF_AUTO_DESTROY name = ffStrbufCreateF("%s %s", vendorId, productId); |
| 23 | + if (bundle->options->namePrefix.length && !ffStrbufStartsWithIgnCase(&name, &bundle->options->namePrefix)) |
| 24 | + return DI_WALK_CONTINUE; |
| 25 | + |
| 26 | + FFPhysicalDiskResult* device = (FFPhysicalDiskResult*) ffListAdd(bundle->disks); |
| 27 | + ffStrbufInitMove(&device->name, &name); |
| 28 | + ffStrbufInit(&device->devPath); |
| 29 | + ffStrbufInit(&device->serial); |
| 30 | + ffStrbufInit(&device->revision); |
| 31 | + ffStrbufInit(&device->interconnect); |
| 32 | + device->temperature = FF_PHYSICALDISK_TEMP_UNSET; |
| 33 | + device->type = FF_PHYSICALDISK_TYPE_NONE; |
| 34 | + device->size = 0; |
| 35 | + |
| 36 | + char* buf; |
| 37 | + if (di_prop_lookup_strings(DDI_DEV_T_ANY, node, "inquiry-serial-no", &buf) > 0) |
| 38 | + ffStrbufSetS(&device->serial, buf); |
| 39 | + if (di_prop_lookup_strings(DDI_DEV_T_ANY, node, "inquiry-revision-id", &buf) > 0) |
| 40 | + ffStrbufSetS(&device->revision, buf); |
| 41 | + if (di_prop_lookup_strings(DDI_DEV_T_ANY, node, "class", &buf) > 0) |
| 42 | + ffStrbufSetS(&device->interconnect, buf); |
| 43 | + |
| 44 | + device->type |= di_prop_find(DDI_DEV_T_ANY, node, "removable-media") ? FF_PHYSICALDISK_TYPE_REMOVABLE : FF_PHYSICALDISK_TYPE_FIXED; |
| 45 | + |
| 46 | + int* value; |
| 47 | + if (di_prop_lookup_ints(DDI_DEV_T_ANY, node, "device-solid-state", &value) > 0) |
| 48 | + device->type |= *value ? FF_PHYSICALDISK_TYPE_SSD : FF_PHYSICALDISK_TYPE_HDD; |
| 49 | + if (di_prop_lookup_ints(DDI_DEV_T_ANY, node, "inquiry-device-type", &value) > 0) |
| 50 | + device->type |= *value == DTYPE_DIRECT ? FF_PHYSICALDISK_TYPE_READWRITE : *value == DTYPE_RODIRECT ? FF_PHYSICALDISK_TYPE_READONLY : 0; |
| 51 | + |
| 52 | + int64_t* nblocks; |
| 53 | + if (di_prop_lookup_int64(DDI_DEV_T_ANY, node, "device-nblocks", &nblocks) > 0 |
| 54 | + && di_prop_lookup_ints(DDI_DEV_T_ANY, node, "device-blksize", &value) > 0) |
| 55 | + device->size = (uint64_t) ((uint64_t) *nblocks * (uint64_t) *value); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + return DI_WALK_CONTINUE; |
| 60 | +} |
| 61 | + |
| 62 | +const char* ffDetectPhysicalDisk(FFlist* result, FFPhysicalDiskOptions* options) |
| 63 | +{ |
| 64 | + di_node_t rootNode = di_init("/", DINFOCPYALL); |
| 65 | + if (rootNode == DI_NODE_NIL) |
| 66 | + return "di_init() failed"; |
| 67 | + di_walk_node(rootNode, DI_WALK_CLDFIRST, &(struct FFWalkTreeBundle) { options, result }, (void*) walkDevTree); |
| 68 | + di_fini(rootNode); |
| 69 | + |
| 70 | + return NULL; |
| 71 | +} |
0 commit comments