summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2021-11-15 11:22:43 +0100
committerLennart Poettering <lennart@poettering.net>2021-11-15 22:43:03 +0100
commit6d439321e8e1f45c8263a29b0f10dddae537169b (patch)
tree08aabcdfd636e07b737fd7ca3cadf11c30ca3e93
parent3818d6a3a5a9bb5e23db12121066de848c95ad79 (diff)
downloadsystemd-6d439321e8e1f45c8263a29b0f10dddae537169b.tar.gz
filesystems: add internal APIs to convert fs magic to name
We previously had tooling for the opposite direction, let's complete the work.
-rw-r--r--src/basic/filesystems.c12
-rw-r--r--src/basic/filesystems.h1
-rwxr-xr-xsrc/basic/generate-filesystem-list.py3
-rwxr-xr-xsrc/basic/generate-filesystem-switch-case.py63
-rw-r--r--src/basic/meson.build16
5 files changed, 91 insertions, 4 deletions
diff --git a/src/basic/filesystems.c b/src/basic/filesystems.c
index d2c70f54cb..628a7dfa91 100644
--- a/src/basic/filesystems.c
+++ b/src/basic/filesystems.c
@@ -1,6 +1,17 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#include "filesystems-gperf.h"
+#include "stat-util.h"
+
+const char *fs_type_to_string(statfs_f_type_t magic) {
+
+ switch (magic) {
+#include "filesystem-switch-case.h"
+ }
+
+ return NULL;
+}
+
int fs_type_from_string(const char *name, const statfs_f_type_t **ret) {
const struct FilesystemMagic *fs_magic;
@@ -13,7 +24,6 @@ int fs_type_from_string(const char *name, const statfs_f_type_t **ret) {
return -EINVAL;
*ret = fs_magic->magic;
-
return 0;
}
diff --git a/src/basic/filesystems.h b/src/basic/filesystems.h
index 48f6c01187..6d07a97ba7 100644
--- a/src/basic/filesystems.h
+++ b/src/basic/filesystems.h
@@ -34,6 +34,7 @@ extern const FilesystemSet filesystem_sets[];
const FilesystemSet *filesystem_set_find(const char *name);
+const char *fs_type_to_string(statfs_f_type_t magic);
int fs_type_from_string(const char *name, const statfs_f_type_t **ret);
int fs_in_group(const struct statfs *s, enum FilesystemGroups fs_group);
diff --git a/src/basic/generate-filesystem-list.py b/src/basic/generate-filesystem-list.py
index 8271b3fbeb..52b74f1763 100755
--- a/src/basic/generate-filesystem-list.py
+++ b/src/basic/generate-filesystem-list.py
@@ -6,6 +6,9 @@ import sys
keywords_section = False
for line in open(sys.argv[1]):
+ if line[0] == '#':
+ continue
+
if keywords_section:
print('"{}\\0"'.format(line.split(',')[0].strip()))
elif line.startswith('%%'):
diff --git a/src/basic/generate-filesystem-switch-case.py b/src/basic/generate-filesystem-switch-case.py
new file mode 100755
index 0000000000..73b1d65657
--- /dev/null
+++ b/src/basic/generate-filesystem-switch-case.py
@@ -0,0 +1,63 @@
+#!/usr/bin/env python3
+# SPDX-License-Identifier: LGPL-2.1-or-later
+
+import sys
+
+
+def filter_fsname(name):
+ # File system magics are sometimes not unique, because file systems got new
+ # revisions or got renamed. Let's prefer newer over older here, and thus
+ # ignore the old names. Specifically:
+ #
+ # → cgroupfs took over the magic of cpuset
+ # → devtmpfs is not a file system of its own, but just a "named superblock" of tmpfs
+ # → ext4 is the newest revision of ext2 + ext3
+ # → fuseblk is closely related to fuse, so close that they share a single magic, but the latter is more common
+ # → gfs2 is the newest revision of gfs
+ # → vfat is the newest revision of msdos
+ # → ncpfs (not ncp) was the last name of the netware `file_system_type` name before it was removed in 2018
+ # → nfs4 is the newest revision of nfs
+ # → orangefs is the new name of pvfs2
+ # → smb3 is an alias for cifs
+
+ return name in (
+ "cpuset",
+ "devtmpfs",
+ "ext2",
+ "ext3",
+ "fuseblk",
+ "gfs",
+ "msdos",
+ "ncp",
+ "nfs",
+ "pvfs2",
+ "smb3",
+ )
+
+
+gperf_file = sys.argv[1]
+keywords_section = False
+
+for line in open(gperf_file):
+ if line[0] == "#":
+ continue
+
+ if keywords_section:
+ name, ids = line.split(",", 1)
+
+ name = name.strip()
+ if filter_fsname(name):
+ continue
+
+ ids = ids.strip()
+ assert ids[0] == "{"
+ assert ids[-1] == "}"
+ ids = ids[1:-1]
+
+ for id in ids.split(","):
+ print(f"case (statfs_f_type_t) {id.strip()}:")
+
+ print(f' return "{name}";')
+
+ if line.startswith("%%"):
+ keywords_section = True
diff --git a/src/basic/meson.build b/src/basic/meson.build
index 042ab86d5e..f0d5a1d2ea 100644
--- a/src/basic/meson.build
+++ b/src/basic/meson.build
@@ -383,8 +383,8 @@ filesystem_includes = ['linux/magic.h',
check_filesystems = find_program('check-filesystems.sh')
r = run_command([check_filesystems, cpp, 'filesystems-gperf.gperf'] + filesystem_includes)
if r.returncode() != 0
- error('found unknown filesystem(s) defined in kernel headers:\n\n' + r.stdout())
- r.stdout()
+ error('found unknown filesystem(s) defined in kernel headers:\n\n' + r.stdout())
+ r.stdout()
endif
filesystems_gperf_h = custom_target(
@@ -403,7 +403,17 @@ filesystem_list_h = custom_target(
'@INPUT@'],
capture : true)
-basic_sources += [filesystem_list_h, filesystems_gperf_h]
+generate_filesystem_switch_case_h = find_program('generate-filesystem-switch-case.py')
+fname = 'filesystem-switch-case.h'
+filesystem_switch_case_h = custom_target(
+ fname,
+ input : 'filesystems-gperf.gperf',
+ output : 'filesystem-switch-case.h',
+ command : [generate_filesystem_switch_case_h,
+ '@INPUT@'],
+ capture : true)
+
+basic_sources += [filesystem_list_h, filesystem_switch_case_h, filesystems_gperf_h]
libbasic = static_library(
'basic',