Debian Codegraph @

src/analyze/analyze-filesystems.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <stdio.h>
4
5 #include "alloc-util.h"
6 #include "analyze.h"
7 #include "analyze-filesystems.h"
8 #include "ansi-color.h"
9 #include "fileio.h"
10 #include "filesystems.h"
11 #include "log.h"
12 #include "nulstr-util.h"
13 #include "pager.h"
14 #include "set.h"
15 #include "string-util.h"
16 #include "strv.h"
17
18 static int load_available_kernel_filesystems(Set **ret) {
19 _cleanup_set_free_ Set *filesystems = NULL;
20 _cleanup_free_ char *t = NULL;
21 int r;
22
23 assert(ret);
24
25 /* Let's read the available filesystems */
26
27 r = read_full_file("/proc/filesystems", &t, /* ret_size= */ NULL);
28 if (r < 0)
29 return r;
30
31 for (int i = 0;;) {
32 _cleanup_free_ char *line = NULL;
33 const char *p;
34
35 r = string_extract_line(t, i++, &line);
36 if (r < 0)
37 return log_oom();
38 if (r == 0)
39 break;
40
41 if (!line)
42 line = t;
43
44 p = strchr(line, '\t');
45 if (!p)
46 continue;
47
48 p += strspn(p, WHITESPACE);
49
50 r = set_put_strdup(&filesystems, p);
51 if (r < 0)
52 return log_error_errno(r, "Failed to add filesystem to list: %m");
53 }
54
55 *ret = TAKE_PTR(filesystems);
56 return 0;
57 }
58
59 static void filesystem_set_remove(Set *s, const FilesystemSet *set) {
60 NULSTR_FOREACH(filesystem, set->value) {
61 if (filesystem[0] == '@')
62 continue;
63
64 free(set_remove(s, filesystem));
65 }
66 }
67
68 static void dump_filesystem_set(const FilesystemSet *set) {
69 int r;
70
71 if (!set)
72 return;
73
74 printf("%s%s%s\n"
75 " # %s\n",
76 ansi_highlight(),
77 set->name,
78 ansi_normal(),
79 set->help);
80
81 NULSTR_FOREACH(filesystem, set->value) {
82 const statfs_f_type_t *magic;
83
84 if (filesystem[0] == '@') {
85 printf(" %s%s%s\n", ansi_underline(), filesystem, ansi_normal());
86 continue;
87 }
88
89 r = fs_type_from_string(filesystem, &magic);
90 assert_se(r >= 0);
91
92 printf(" %s", filesystem);
93
94 for (size_t i = 0; magic[i] != 0; i++) {
95 const char *primary;
96 if (i == 0)
97 printf(" %s(magic: ", ansi_grey());
98 else
99 printf(", ");
100
101 printf("0x%llx", (unsigned long long) magic[i]);
102
103 primary = fs_type_to_string(magic[i]);
104 if (primary && !streq(primary, filesystem))
105 printf("[%s]", primary);
106
107 if (magic[i+1] == 0)
108 printf(")%s", ansi_normal());
109 }
110
111 printf("\n");
112 }
113 }
114
115 int verb_filesystems(int argc, char *argv[], uintptr_t _data, void *userdata) {
116 #if ! HAVE_LIBBPF
117 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "Not compiled with libbpf support, sorry.");
118 #endif
119
120 pager_open(arg_pager_flags);
121
122 char **args = strv_skip(argv, 1);
123 if (args)
124 STRV_FOREACH(name, args) {
125 if (name != args)
126 puts("");
127
128 const FilesystemSet *set = filesystem_set_find(*name);
129 if (!set) {
130 /* make sure the error appears below normal output */
131 fflush(stdout);
132
133 return log_error_errno(SYNTHETIC_ERRNO(ENOENT),
134 "Filesystem set \"%s\" not found.", *name);
135 }
136
137 dump_filesystem_set(set);
138 }
139 else {
140 _cleanup_set_free_ Set *kernel = NULL, *known = NULL;
141 int k;
142
143 NULSTR_FOREACH(fs, filesystem_sets[FILESYSTEM_SET_KNOWN].value)
144 if (set_put_strdup(&known, fs) < 0)
145 return log_oom();
146
147 k = load_available_kernel_filesystems(&kernel);
148
149 for (FilesystemGroups i = 0; i < _FILESYSTEM_SET_MAX; i++) {
150 const FilesystemSet *set = filesystem_sets + i;
151 if (i > 0)
152 puts("");
153
154 dump_filesystem_set(set);
155 filesystem_set_remove(kernel, set);
156 if (i != FILESYSTEM_SET_KNOWN)
157 filesystem_set_remove(known, set);
158 }
159
160 if (arg_quiet) /* Let's not show the extra stuff in quiet mode */
161 return 0;
162
163 if (!set_isempty(known)) {
164 printf("\n"
165 "# %sUngrouped filesystems%s (known but not included in any of the groups except @known):\n",
166 ansi_highlight(), ansi_normal());
167
168 _cleanup_free_ char **l = set_get_strv(known);
169 if (!l)
170 return log_oom();
171
172 strv_sort(l);
173
174 STRV_FOREACH(filesystem, l) {
175 const statfs_f_type_t *magic;
176 bool is_primary = false;
177
178 assert_se(fs_type_from_string(*filesystem, &magic) >= 0);
179
180 for (size_t i = 0; magic[i] != 0; i++) {
181 const char *primary;
182
183 primary = fs_type_to_string(magic[i]);
184 assert(primary);
185
186 if (streq(primary, *filesystem))
187 is_primary = true;
188 }
189
190 if (!is_primary) {
191 log_debug("Skipping ungrouped file system '%s', because it's an alias for another one.", *filesystem);
192 continue;
193 }
194
195 printf("# %s\n", *filesystem);
196 }
197 }
198
199 if (k < 0) {
200 fputc('\n', stdout);
201 fflush(stdout);
202 log_notice_errno(k, "# Not showing unlisted filesystems, couldn't retrieve kernel filesystem list: %m");
203 } else if (!set_isempty(kernel)) {
204 _cleanup_free_ char **l = NULL;
205
206 printf("\n"
207 "# %sUnlisted filesystems%s (available to the local kernel, but not included in any of the groups listed above):\n",
208 ansi_highlight(), ansi_normal());
209
210 l = set_get_strv(kernel);
211 if (!l)
212 return log_oom();
213
214 strv_sort(l);
215
216 STRV_FOREACH(filesystem, l)
217 printf("# %s\n", *filesystem);
218 }
219 }
220
221 return EXIT_SUCCESS;
222 }