-
Notifications
You must be signed in to change notification settings - Fork 1
/
fsinfo.c
176 lines (135 loc) · 3.85 KB
/
fsinfo.c
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
/*
Tool displaying information about a file system
Author: R. Koucha
Date: 06-Apr-2020
*/
#include <errno.h>
#include <sys/types.h>
#include <stdio.h>
#include <libgen.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <sys/sysmacros.h>
#include <sys/vfs.h>
#include <sys/mount.h>
#include <linux/magic.h>
#include "util.h"
static const char *get_mntflags(unsigned long flags)
{
static char flag_str[1024];
unsigned int offset = 0;
int strsize = sizeof(flag_str);
int rc;
#define WRITE_FLAG(str, strsize, off, flag) do { \
if (off) { \
str[off] = ' '; \
off ++; \
strsize --; \
} \
rc = snprintf(str + off, strsize, "%s", flag); \
off += rc; \
strsize -= rc; \
} while(0)
flag_str[0] = '\0';
if (flags & MS_MANDLOCK) {
WRITE_FLAG(flag_str, strsize, offset, "MANDATORY_LOCKING");
}
if (flags & MS_NOATIME) {
WRITE_FLAG(flag_str, strsize, offset, "NOATIME");
}
if (flags & MS_NODEV) {
WRITE_FLAG(flag_str, strsize, offset, "NODEV");
}
if (flags & MS_NODIRATIME) {
WRITE_FLAG(flag_str, strsize, offset, "NODIRATIME");
}
if (flags & MS_NOEXEC) {
WRITE_FLAG(flag_str, strsize, offset, "NOEXEC");
}
if (flags & MS_NOSUID) {
WRITE_FLAG(flag_str, strsize, offset, "NOSUID");
}
if (flags & MS_RDONLY) {
WRITE_FLAG(flag_str, strsize, offset, "RDONLY");
}
if (flags & MS_RELATIME) {
WRITE_FLAG(flag_str, strsize, offset, "RELATIME");
}
if (flags & MS_SYNCHRONOUS) {
WRITE_FLAG(flag_str, strsize, offset, "SYNCHRONOUS");
}
if (flags & MS_REMOUNT) {
WRITE_FLAG(flag_str, strsize, offset, "REMOUNT");
}
if (flags & MS_BIND) {
WRITE_FLAG(flag_str, strsize, offset, "BIND");
}
if (flags & MS_SLAVE) {
WRITE_FLAG(flag_str, strsize, offset, "SLAVE");
}
if (flags & MS_SHARED) {
WRITE_FLAG(flag_str, strsize, offset, "SHARED");
}
if (flags & MS_PRIVATE) {
WRITE_FLAG(flag_str, strsize, offset, "PRIVATE");
}
if (flags & MS_UNBINDABLE) {
WRITE_FLAG(flag_str, strsize, offset, "UNBINDABLE");
}
return flag_str;
} // get_mntflags
static const char *get_fstype(unsigned long fstype)
{
switch(fstype)
{
case RAMFS_MAGIC: return "RAMFS";
case TMPFS_MAGIC: return "TMPFS";
case HUGETLBFS_MAGIC : return "HUGETLBFS";
//case EXT2_SUPER_MAGIC:
//case EXT3_SUPER_MAGIC:
case EXT4_SUPER_MAGIC: return "EXT2/3/4FS";
case BTRFS_SUPER_MAGIC: return "BTRFS";
case OVERLAYFS_SUPER_MAGIC: return "OVERLAYFS";
case CGROUP_SUPER_MAGIC: return "CGROUPV1";
case CGROUP2_SUPER_MAGIC: return "CGROUPV2";
case DEVPTS_SUPER_MAGIC: return "DEVPTSFS";
case PROC_SUPER_MAGIC: return "PROCFS";
case SYSFS_MAGIC: return "SYSFS";
case NSFS_MAGIC: return "NSFS";
default: return "????";
} // End switch
} // get_fstype
int main(int ac, char *av[])
{
int rc;
struct statfs st;
if (ac != 2) {
fprintf(stderr, "Usage: %s fname\n", basename(av[0]));
return 1;
}
// Get information on the file system
rc = statfs(av[1], &st);
if (0 != rc) {
ERR("statfs(%s): '%m' (%d)\n", av[1], errno);
return 1;
}
printf("Type : 0x%lx (%s)\n"
"Block size : %lu\n"
"Total blocks : %lu\n"
"Total free blocks: %lu\n"
"Total files : %lu\n"
"Max name length : %lu\n"
"Mount flags : 0x%lx (%s)\n"
,
(unsigned long)(st.f_type), get_fstype((unsigned long)(st.f_type)),
(unsigned long)(st.f_bsize),
(unsigned long)(st.f_blocks),
(unsigned long)(st.f_bfree),
(unsigned long)(st.f_files),
(unsigned long)(st.f_namelen),
(unsigned long)(st.f_flags), get_mntflags((unsigned long)(st.f_flags))
);
return 0;
} // main