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
|
#include "disk.h"
#include "common/mallocHelper.h"
#include "common/strutil.h"
#include <stdalign.h>
#include <sys/mount.h>
#include <sys/stat.h>
#ifdef __NetBSD__
#include <sys/types.h>
#include <sys/statvfs.h>
#define statfs statvfs
#define f_flags f_flag
#define f_bsize f_frsize
#endif
#ifdef __FreeBSD__
#if __has_include(<libgeom.h>)
#include <libgeom.h>
static const char* detectFsLabel(struct statfs* fs, FFDisk* disk) {
if (!ffStrStartsWith(fs->f_mntfromname, "/dev/")) {
return "Only block devices are supported";
}
// Detect volume label in geom tree
static struct gmesh geomTree;
static struct gclass* cLabels;
if (!cLabels) {
if (geomTree.lg_ident) {
return "Previous geom_gettree() failed";
}
if (geom_gettree(&geomTree) < 0) {
geomTree.lg_ident = (void*) (intptr_t) -1;
return "geom_gettree() failed";
}
for (cLabels = geomTree.lg_class.lh_first; cLabels && !ffStrEquals(cLabels->lg_name, "LABEL"); cLabels = cLabels->lg_class.le_next);
if (!cLabels) {
return "Class LABEL is not found";
}
}
for (struct ggeom* label = cLabels->lg_geom.lh_first; label; label = label->lg_geom.le_next) {
struct gprovider* provider = label->lg_provider.lh_first;
if (!provider || !ffStrEquals(label->lg_name, fs->f_mntfromname + strlen("/dev/"))) {
continue;
}
const char* str = strchr(provider->lg_name, '/');
ffStrbufSetS(&disk->name, str ? str + 1 : provider->lg_name);
}
return NULL;
}
#else
static const char* detectFsLabel(FF_A_UNUSED struct statfs* fs, FF_A_UNUSED FFDisk* disk) {
return "Fastfetch was compiled without libgeom support";
}
#endif
static void detectFsInfo(struct statfs* fs, FFDisk* disk) {
if (ffStrbufEqualS(&disk->filesystem, "zfs")) {
disk->type = !ffStrbufStartsWithS(&disk->mountFrom, "zroot/") || ffStrbufStartsWithS(&disk->mountFrom, "zroot/ROOT/")
? FF_DISK_VOLUME_TYPE_REGULAR_BIT
: FF_DISK_VOLUME_TYPE_SUBVOLUME_BIT;
} else if (fs->f_flags & MNT_IGNORE) {
disk->type = FF_DISK_VOLUME_TYPE_HIDDEN_BIT;
} else if (!(fs->f_flags & MNT_LOCAL)) {
disk->type = FF_DISK_VOLUME_TYPE_EXTERNAL_BIT;
} else {
disk->type = FF_DISK_VOLUME_TYPE_REGULAR_BIT;
}
detectFsLabel(fs, disk);
}
#elif __APPLE__
#include "common/apple/cf_helpers.h"
#include <sys/attr.h>
#include <unistd.h>
#ifndef MAC_OS_X_VERSION_10_15
#define MNT_REMOVABLE 0x00000200
#endif
struct CmnAttrBuf {
uint32_t length;
attrreference_t nameRef;
char nameSpace[NAME_MAX * 3 + 1];
} FF_A_PACKED;
void detectFsInfo(struct statfs* fs, FFDisk* disk) {
if (fs->f_flags & MNT_DONTBROWSE) {
disk->type = FF_DISK_VOLUME_TYPE_HIDDEN_BIT;
} else if (fs->f_flags & MNT_REMOVABLE || !(fs->f_flags & MNT_LOCAL)) {
disk->type = FF_DISK_VOLUME_TYPE_EXTERNAL_BIT;
} else {
disk->type = FF_DISK_VOLUME_TYPE_REGULAR_BIT;
}
alignas(4) struct CmnAttrBuf attrBuf;
if (getattrlist(disk->mountpoint.chars, &(struct attrlist) {
.bitmapcount = ATTR_BIT_MAP_COUNT,
.commonattr = ATTR_CMN_NAME,
},
&attrBuf,
sizeof(attrBuf),
0) == 0) {
ffStrbufInitNS(&disk->name, attrBuf.nameRef.attr_length - 1 /* excluding '\0' */, attrBuf.nameSpace);
}
}
#else
static void detectFsInfo(struct statfs* fs, FFDisk* disk) {
#ifdef MNT_IGNORE
if (fs->f_flags & MNT_IGNORE) {
disk->type = FF_DISK_VOLUME_TYPE_HIDDEN_BIT;
} else
#endif
if (!(fs->f_flags & MNT_LOCAL)) {
disk->type = FF_DISK_VOLUME_TYPE_EXTERNAL_BIT;
} else {
disk->type = FF_DISK_VOLUME_TYPE_REGULAR_BIT;
}
}
#endif
const char* ffDetectDisksImpl(FFDiskOptions* options, FFlist* disks) {
#ifndef __NetBSD__
int size = getfsstat(NULL, 0, MNT_WAIT);
if (size <= 0) {
return "getfsstat(NULL, 0, MNT_WAIT) failed";
}
#else
int size = getvfsstat(NULL, 0, ST_WAIT);
if (size <= 0) {
return "getvfsstat(NULL, 0, ST_WAIT) failed";
}
#endif
FF_AUTO_FREE struct statfs* buf = malloc(sizeof(*buf) * (unsigned) size);
#ifndef __NetBSD__
if (getfsstat(buf, (int) (sizeof(*buf) * (unsigned) size), MNT_NOWAIT) <= 0) {
return "getfsstat(buf, size, MNT_NOWAIT) failed";
}
#else
if (getvfsstat(buf, sizeof(*buf) * (unsigned) size, ST_NOWAIT) <= 0) {
return "getvfsstat(buf, size, ST_NOWAIT) failed";
}
#endif
for (struct statfs* fs = buf; fs < buf + size; ++fs) {
if (__builtin_expect(options->folders.length > 0, 0)) {
if (!ffStrbufSeparatedContainS(&options->folders, fs->f_mntonname, FF_DISK_FOLDER_SEPARATOR)) {
continue;
}
} else if (!ffStrEquals(fs->f_mntonname, "/") && !ffStrStartsWith(fs->f_mntfromname, "/dev/") && !ffStrEquals(fs->f_fstypename, "zfs") && !ffStrEquals(fs->f_fstypename, "fusefs.sshfs")) {
continue;
}
if (options->hideFolders.length && ffDiskMatchesFolderPatterns(&options->hideFolders, fs->f_mntonname, FF_DISK_FOLDER_SEPARATOR)) {
continue;
}
if (options->hideFS.length && ffStrbufSeparatedContainS(&options->hideFS, fs->f_fstypename, ':')) {
continue;
}
#ifdef __FreeBSD__
// f_bavail and f_ffree are signed on FreeBSD...
if (fs->f_bavail < 0) {
fs->f_bavail = 0;
}
if (fs->f_ffree < 0) {
fs->f_ffree = 0;
}
#endif
FFDisk* disk = FF_LIST_ADD(FFDisk, *disks);
disk->bytesTotal = (uint64_t) fs->f_blocks * (uint64_t) fs->f_bsize;
disk->bytesFree = (uint64_t) fs->f_bfree * (uint64_t) fs->f_bsize;
disk->bytesAvailable = (uint64_t) fs->f_bavail * (uint64_t) fs->f_bsize;
disk->bytesUsed = 0; // To be filled in ./disk.c
disk->filesTotal = (uint32_t) fs->f_files;
disk->filesUsed = (uint32_t) fs->f_files - (uint32_t) fs->f_ffree;
ffStrbufInitS(&disk->mountFrom, fs->f_mntfromname);
ffStrbufInitS(&disk->mountpoint, fs->f_mntonname);
ffStrbufInitS(&disk->filesystem, fs->f_fstypename);
ffStrbufInit(&disk->name);
disk->type = 0;
disk->createTime = 0;
detectFsInfo(fs, disk);
if (fs->f_flags & MNT_RDONLY) {
disk->type |= FF_DISK_VOLUME_TYPE_READONLY_BIT;
}
#ifdef __OpenBSD__
#define st_birthtimespec __st_birthtim
#endif
#ifndef __DragonFly__
struct stat st;
if (stat(fs->f_mntonname, &st) == 0 && st.st_birthtimespec.tv_sec > 0) {
disk->createTime = (uint64_t) (((uint64_t) st.st_birthtimespec.tv_sec * 1000) + ((uint64_t) st.st_birthtimespec.tv_nsec / 1000000));
}
#endif
}
return NULL;
}
|