|
| 1 | +#include "btrfs.h" |
| 2 | + |
| 3 | +#include "common/io/io.h" |
| 4 | + |
| 5 | +enum { uuidLen = (uint32_t) __builtin_strlen("00000000-0000-0000-0000-000000000000") }; |
| 6 | + |
| 7 | +static const char* enumerateDevices(FFBtrfsResult* item, FFstrbuf* path, FFstrbuf* buffer) |
| 8 | +{ |
| 9 | + ffStrbufAppendS(path, "devices/"); |
| 10 | + FF_AUTO_CLOSE_DIR DIR* dirp = opendir(path->chars); |
| 11 | + if(dirp == NULL) |
| 12 | + return "opendir(\"/sys/fs/btrfs/UUID/devices\") == NULL"; |
| 13 | + const uint32_t devicesPathLen = path->length; |
| 14 | + |
| 15 | + struct dirent* entry; |
| 16 | + while ((entry = readdir(dirp)) != NULL) |
| 17 | + { |
| 18 | + if (entry->d_name[0] == '.') |
| 19 | + continue; |
| 20 | + |
| 21 | + if (item->devices.length) |
| 22 | + ffStrbufAppendC(&item->devices, ','); |
| 23 | + ffStrbufAppendS(&item->devices, entry->d_name); |
| 24 | + ffStrbufAppendS(path, entry->d_name); |
| 25 | + ffStrbufAppendS(path, "/size"); |
| 26 | + |
| 27 | + if (ffReadFileBuffer(path->chars, buffer)) |
| 28 | + item->totalSize += ffStrbufToUInt(buffer, 0) * 512; |
| 29 | + |
| 30 | + ffStrbufSubstrBefore(path, devicesPathLen); |
| 31 | + } |
| 32 | + |
| 33 | + return NULL; |
| 34 | +} |
| 35 | + |
| 36 | +static const char* enumerateFeatures(FFBtrfsResult* item, FFstrbuf* path) |
| 37 | +{ |
| 38 | + ffStrbufAppendS(path, "features/"); |
| 39 | + FF_AUTO_CLOSE_DIR DIR* dirp = opendir(path->chars); |
| 40 | + if(dirp == NULL) |
| 41 | + return "opendir(\"/sys/fs/btrfs/UUID/features\") == NULL"; |
| 42 | + const uint32_t featuresPathLen = path->length; |
| 43 | + |
| 44 | + struct dirent* entry; |
| 45 | + while ((entry = readdir(dirp)) != NULL) |
| 46 | + { |
| 47 | + if (entry->d_name[0] == '.') |
| 48 | + continue; |
| 49 | + if (item->features.length) |
| 50 | + ffStrbufAppendC(&item->features, ','); |
| 51 | + ffStrbufAppendS(&item->features, entry->d_name); |
| 52 | + |
| 53 | + ffStrbufSubstrBefore(path, featuresPathLen); |
| 54 | + } |
| 55 | + |
| 56 | + return NULL; |
| 57 | +} |
| 58 | + |
| 59 | +static const char* detectAllocation(FFBtrfsResult* item, FFstrbuf* path, FFstrbuf* buffer) |
| 60 | +{ |
| 61 | + ffStrbufAppendS(path, "allocation/"); |
| 62 | + const uint32_t AllocationPathLen = path->length; |
| 63 | + |
| 64 | + ffStrbufAppendS(path, "global_rsv_size"); |
| 65 | + if (ffReadFileBuffer(path->chars, buffer)) |
| 66 | + item->globalReservationTotal = ffStrbufToUInt(buffer, 0); |
| 67 | + else |
| 68 | + return "ffReadFileBuffer(\"/sys/fs/btrfs/UUID/allocation/global_rsv_size\") == NULL"; |
| 69 | + ffStrbufSubstrBefore(path, AllocationPathLen); |
| 70 | + |
| 71 | + ffStrbufAppendS(path, "global_rsv_reserved"); |
| 72 | + if (ffReadFileBuffer(path->chars, buffer)) |
| 73 | + item->globalReservationUsed = ffStrbufToUInt(buffer, 0); |
| 74 | + ffStrbufSubstrBefore(path, AllocationPathLen); |
| 75 | + item->globalReservationUsed = item->globalReservationTotal - item->globalReservationUsed; |
| 76 | + |
| 77 | + #define FF_BTRFS_DETECT_TYPE(index, _type) \ |
| 78 | + ffStrbufAppendS(path, #_type "/total_bytes"); \ |
| 79 | + if (ffReadFileBuffer(path->chars, buffer)) \ |
| 80 | + item->allocation[index].total = ffStrbufToUInt(buffer, 0); \ |
| 81 | + ffStrbufSubstrBefore(path, AllocationPathLen); \ |
| 82 | + \ |
| 83 | + ffStrbufAppendS(path, #_type "/bytes_used"); \ |
| 84 | + if (ffReadFileBuffer(path->chars, buffer)) \ |
| 85 | + item->allocation[index].used = ffStrbufToUInt(buffer, 0); \ |
| 86 | + ffStrbufSubstrBefore(path, AllocationPathLen); \ |
| 87 | + \ |
| 88 | + ffStrbufAppendS(path, #_type "/dup"); \ |
| 89 | + item->allocation[index].dup = ffPathExists(path->chars, FF_PATHTYPE_DIRECTORY); \ |
| 90 | + ffStrbufSubstrBefore(path, AllocationPathLen); \ |
| 91 | + \ |
| 92 | + item->allocation[index].type = #_type; |
| 93 | + |
| 94 | + FF_BTRFS_DETECT_TYPE(0, data); |
| 95 | + FF_BTRFS_DETECT_TYPE(1, metadata); |
| 96 | + FF_BTRFS_DETECT_TYPE(2, system); |
| 97 | + |
| 98 | + #undef FF_BTRFS_DETECT_TYPE |
| 99 | + |
| 100 | + return NULL; |
| 101 | +} |
| 102 | + |
| 103 | +const char* ffDetectBtrfs(FFlist* result) |
| 104 | +{ |
| 105 | + FF_STRBUF_AUTO_DESTROY path = ffStrbufCreateS("/sys/fs/btrfs/"); |
| 106 | + const uint32_t basePathLen = path.length; |
| 107 | + |
| 108 | + FF_AUTO_CLOSE_DIR DIR* dirp = opendir(path.chars); |
| 109 | + if(dirp == NULL) |
| 110 | + return "opendir(\"/sys/fs/btrfs\") == NULL"; |
| 111 | + |
| 112 | + FF_STRBUF_AUTO_DESTROY buffer = ffStrbufCreate(); |
| 113 | + |
| 114 | + struct dirent* entry; |
| 115 | + while ((entry = readdir(dirp)) != NULL) |
| 116 | + { |
| 117 | + if (entry->d_name[0] == '.') |
| 118 | + continue; |
| 119 | + if (strlen(entry->d_name) != uuidLen) |
| 120 | + continue; |
| 121 | + |
| 122 | + FFBtrfsResult* item = ffListAdd(result); |
| 123 | + (*item) = (FFBtrfsResult){}; |
| 124 | + ffStrbufInitNS(&item->uuid, uuidLen, entry->d_name); |
| 125 | + ffStrbufInit(&item->name); |
| 126 | + ffStrbufInit(&item->devices); |
| 127 | + ffStrbufInit(&item->features); |
| 128 | + |
| 129 | + ffStrbufAppendNS(&path, uuidLen, entry->d_name); |
| 130 | + ffStrbufAppendC(&path, '/'); |
| 131 | + const uint32_t itemPathLen = path.length; |
| 132 | + |
| 133 | + ffStrbufAppendS(&path, "label"); |
| 134 | + if (ffAppendFileBuffer(path.chars, &item->name)) |
| 135 | + ffStrbufTrimRightSpace(&item->name); |
| 136 | + ffStrbufSubstrBefore(&path, itemPathLen); |
| 137 | + |
| 138 | + enumerateDevices(item, &path, &buffer); |
| 139 | + ffStrbufSubstrBefore(&path, itemPathLen); |
| 140 | + |
| 141 | + enumerateFeatures(item, &path); |
| 142 | + ffStrbufSubstrBefore(&path, itemPathLen); |
| 143 | + |
| 144 | + ffStrbufAppendS(&path, "generation"); |
| 145 | + if (ffReadFileBuffer(path.chars, &buffer)) |
| 146 | + item->generation = (uint32_t) ffStrbufToUInt(&buffer, 0); |
| 147 | + ffStrbufSubstrBefore(&path, itemPathLen); |
| 148 | + |
| 149 | + ffStrbufAppendS(&path, "nodesize"); |
| 150 | + if (ffReadFileBuffer(path.chars, &buffer)) |
| 151 | + item->nodeSize = (uint32_t) ffStrbufToUInt(&buffer, 0); |
| 152 | + ffStrbufSubstrBefore(&path, itemPathLen); |
| 153 | + |
| 154 | + ffStrbufAppendS(&path, "sectorsize"); |
| 155 | + if (ffReadFileBuffer(path.chars, &buffer)) |
| 156 | + item->sectorSize = (uint32_t) ffStrbufToUInt(&buffer, 0); |
| 157 | + ffStrbufSubstrBefore(&path, itemPathLen); |
| 158 | + |
| 159 | + detectAllocation(item, &path, &buffer); |
| 160 | + |
| 161 | + // finally |
| 162 | + ffStrbufSubstrBefore(&path, basePathLen); |
| 163 | + } |
| 164 | + |
| 165 | + return NULL; |
| 166 | +} |
0 commit comments