Skip to content

Commit c24328e

Browse files
committed
Btrfs: add new module
1 parent c58f4c2 commit c24328e

File tree

13 files changed

+494
-4
lines changed

13 files changed

+494
-4
lines changed

CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,7 @@ set(LIBFASTFETCH_SRC
342342
src/modules/bootmgr/bootmgr.c
343343
src/modules/brightness/brightness.c
344344
src/modules/break/break.c
345+
src/modules/btrfs/btrfs.c
345346
src/modules/camera/camera.c
346347
src/modules/chassis/chassis.c
347348
src/modules/colors/colors.c
@@ -428,6 +429,7 @@ if(LINUX)
428429
src/detection/board/board_linux.c
429430
src/detection/bootmgr/bootmgr_linux.c
430431
src/detection/brightness/brightness_linux.c
432+
src/detection/btrfs/btrfs_linux.c
431433
src/detection/chassis/chassis_linux.c
432434
src/detection/cpu/cpu_linux.c
433435
src/detection/cpucache/cpucache_linux.c
@@ -508,6 +510,7 @@ elseif(ANDROID)
508510
src/detection/board/board_android.c
509511
src/detection/bootmgr/bootmgr_nosupport.c
510512
src/detection/brightness/brightness_nosupport.c
513+
src/detection/btrfs/btrfs_nosupport.c
511514
src/detection/chassis/chassis_nosupport.c
512515
src/detection/cpu/cpu_linux.c
513516
src/detection/cpucache/cpucache_linux.c
@@ -573,6 +576,7 @@ elseif(FreeBSD)
573576
src/detection/board/board_bsd.c
574577
src/detection/bootmgr/bootmgr_bsd.c
575578
src/detection/brightness/brightness_bsd.c
579+
src/detection/btrfs/btrfs_nosupport.c
576580
src/detection/chassis/chassis_bsd.c
577581
src/detection/cpu/cpu_bsd.c
578582
src/detection/cpucache/cpucache_shared.c
@@ -652,6 +656,7 @@ elseif(APPLE)
652656
src/detection/board/board_apple.c
653657
src/detection/bootmgr/bootmgr_apple.c
654658
src/detection/brightness/brightness_apple.c
659+
src/detection/btrfs/btrfs_nosupport.c
655660
src/detection/chassis/chassis_nosupport.c
656661
src/detection/cpu/cpu_apple.c
657662
src/detection/cpucache/cpucache_apple.c
@@ -718,6 +723,7 @@ elseif(WIN32)
718723
src/detection/board/board_windows.c
719724
src/detection/bootmgr/bootmgr_windows.c
720725
src/detection/brightness/brightness_windows.cpp
726+
src/detection/btrfs/btrfs_nosupport.c
721727
src/detection/chassis/chassis_windows.c
722728
src/detection/cpu/cpu_windows.c
723729
src/detection/cpucache/cpucache_windows.c
@@ -785,6 +791,7 @@ elseif(SunOS)
785791
src/detection/board/board_windows.c
786792
src/detection/bootmgr/bootmgr_nosupport.c
787793
src/detection/brightness/brightness_nosupport.c
794+
src/detection/btrfs/btrfs_nosupport.c
788795
src/detection/chassis/chassis_windows.c
789796
src/detection/cpu/cpu_sunos.c
790797
src/detection/cpucache/cpucache_shared.c

src/common/modules.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ static FFModuleBaseInfo* B[] = {
1313
(void*) &instance.config.modules.bootmgr,
1414
(void*) &instance.config.modules.break_,
1515
(void*) &instance.config.modules.brightness,
16+
(void*) &instance.config.modules.btrfs,
1617
NULL,
1718
};
1819

src/detection/btrfs/btrfs.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#pragma once
2+
3+
#include "fastfetch.h"
4+
5+
typedef struct FFBtrfsDiskUsage
6+
{
7+
uint64_t total;
8+
uint64_t used;
9+
const char* type;
10+
bool dup;
11+
} FFBtrfsDiskUsage;
12+
13+
typedef struct FFBtrfsResult
14+
{
15+
FFstrbuf name;
16+
FFstrbuf uuid;
17+
FFstrbuf devices;
18+
FFstrbuf features;
19+
uint32_t generation;
20+
uint32_t nodeSize;
21+
uint32_t sectorSize;
22+
uint64_t totalSize;
23+
uint64_t globalReservationUsed;
24+
uint64_t globalReservationTotal;
25+
FFBtrfsDiskUsage allocation[3];
26+
} FFBtrfsResult;
27+
28+
const char* ffDetectBtrfs(FFlist* result /* list of FFBtrfsResult */);

src/detection/btrfs/btrfs_linux.c

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
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+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#include "btrfs.h"
2+
3+
const char* ffDetectBtrfs(FF_MAYBE_UNUSED FFlist* result)
4+
{
5+
return "Not supported on this platform";
6+
}

0 commit comments

Comments
 (0)