|
| 1 | +#include "physicaldisk.h" |
| 2 | +#include "common/io/io.h" |
| 3 | +#include "common/properties.h" |
| 4 | +#include "util/stringUtils.h" |
| 5 | + |
| 6 | +#include <ctype.h> |
| 7 | +#include <limits.h> |
| 8 | + |
| 9 | +const char* ffDetectPhysicalDisk(FFlist* result, FFPhysicalDiskOptions* options) |
| 10 | +{ |
| 11 | + FF_AUTO_CLOSE_DIR DIR* sysBlockDirp = opendir("/sys/block/"); |
| 12 | + if(sysBlockDirp == NULL) |
| 13 | + return "opendir(\"/sys/block/\") == NULL"; |
| 14 | + |
| 15 | + struct dirent* sysBlockEntry; |
| 16 | + while ((sysBlockEntry = readdir(sysBlockDirp)) != NULL) |
| 17 | + { |
| 18 | + const char* const devName = sysBlockEntry->d_name; |
| 19 | + |
| 20 | + if (devName[0] == '.') |
| 21 | + continue; |
| 22 | + |
| 23 | + char pathSysBlock[PATH_MAX]; |
| 24 | + snprintf(pathSysBlock, PATH_MAX, "/sys/block/%s", devName); |
| 25 | + |
| 26 | + char pathSysDeviceReal[PATH_MAX] = ""; |
| 27 | + readlink(pathSysBlock, pathSysDeviceReal, sizeof(pathSysDeviceReal) - 1); |
| 28 | + |
| 29 | + if (strstr(pathSysDeviceReal, "/virtual/")) // virtual device |
| 30 | + continue; |
| 31 | + |
| 32 | + snprintf(pathSysBlock, PATH_MAX, "/sys/block/%s/device", devName); |
| 33 | + if (!ffPathExists(pathSysBlock, FF_PATHTYPE_DIRECTORY)) |
| 34 | + continue; |
| 35 | + |
| 36 | + FFPhysicalDiskResult* device = (FFPhysicalDiskResult*) ffListAdd(result); |
| 37 | + device->type = FF_PHYSICALDISK_TYPE_NONE; |
| 38 | + ffStrbufInit(&device->name); |
| 39 | + |
| 40 | + { |
| 41 | + snprintf(pathSysBlock, PATH_MAX, "/sys/block/%s/device/vendor", devName); |
| 42 | + ffAppendFileBuffer(pathSysBlock, &device->name); |
| 43 | + if (device->name.length > 0) |
| 44 | + ffStrbufAppendC(&device->name, ' '); |
| 45 | + |
| 46 | + snprintf(pathSysBlock, PATH_MAX, "/sys/block/%s/device/model", devName); |
| 47 | + ffAppendFileBuffer(pathSysBlock, &device->name); |
| 48 | + ffStrbufTrim(&device->name, ' '); |
| 49 | + |
| 50 | + if (device->name.length == 0) |
| 51 | + ffStrbufSetS(&device->name, devName); |
| 52 | + |
| 53 | + if (options->namePrefix.length && !ffStrbufStartsWith(&device->name, &options->namePrefix)) |
| 54 | + { |
| 55 | + ffStrbufDestroy(&device->name); |
| 56 | + result->length--; |
| 57 | + continue; |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + ffStrbufInitF(&device->devPath, "/dev/%s", devName); |
| 62 | + |
| 63 | + { |
| 64 | + ffStrbufInit(&device->interconnect); |
| 65 | + if (strstr(pathSysDeviceReal, "/usb") != NULL) |
| 66 | + ffStrbufSetS(&device->interconnect, "usb"); |
| 67 | + else |
| 68 | + { |
| 69 | + snprintf(pathSysBlock, PATH_MAX, "/sys/block/%s/device/transport", devName); |
| 70 | + if (!ffAppendFileBuffer(pathSysBlock, &device->interconnect)) |
| 71 | + { |
| 72 | + snprintf(pathSysBlock, PATH_MAX, "/sys/block/%s/device/uevent", devName); |
| 73 | + if (ffParsePropFile(pathSysBlock, "DEVTYPE=", &device->interconnect)) |
| 74 | + ffStrbufSubstrBeforeLastC(&device->interconnect, '_'); |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + { |
| 80 | + snprintf(pathSysBlock, PATH_MAX, "/sys/block/%s/queue/rotational", devName); |
| 81 | + char isRotationalChar = '1'; |
| 82 | + if (ffReadFileData(pathSysBlock, 1, &isRotationalChar) > 0) |
| 83 | + device->type |= isRotationalChar == '1' ? FF_PHYSICALDISK_TYPE_HDD : FF_PHYSICALDISK_TYPE_SSD; |
| 84 | + } |
| 85 | + |
| 86 | + { |
| 87 | + snprintf(pathSysBlock, PATH_MAX, "/sys/block/%s/size", devName); |
| 88 | + char blkSize[32]; |
| 89 | + ssize_t fileSize = ffReadFileData(pathSysBlock, sizeof(blkSize) - 1, blkSize); |
| 90 | + if (fileSize > 0) |
| 91 | + { |
| 92 | + blkSize[fileSize] = 0; |
| 93 | + device->size = (uint64_t) strtoul(blkSize, NULL, 10) * 512; |
| 94 | + } |
| 95 | + else |
| 96 | + device->size = 0; |
| 97 | + } |
| 98 | + |
| 99 | + { |
| 100 | + char removableChar = '0'; |
| 101 | + snprintf(pathSysBlock, PATH_MAX, "/sys/block/%s/removable", devName); |
| 102 | + if (ffReadFileData(pathSysBlock, 1, &removableChar) > 0) |
| 103 | + device->type |= removableChar == '1' ? FF_PHYSICALDISK_TYPE_REMOVABLE : FF_PHYSICALDISK_TYPE_FIXED; |
| 104 | + } |
| 105 | + |
| 106 | + { |
| 107 | + ffStrbufInit(&device->serial); |
| 108 | + snprintf(pathSysBlock, PATH_MAX, "/sys/block/%s/device/serial", devName); |
| 109 | + ffReadFileBuffer(pathSysBlock, &device->serial); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + return NULL; |
| 114 | +} |
0 commit comments