]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
filesystems: add internal APIs to convert fs magic to name
authorLennart Poettering <lennart@poettering.net>
Mon, 15 Nov 2021 10:22:43 +0000 (11:22 +0100)
committerLennart Poettering <lennart@poettering.net>
Mon, 15 Nov 2021 21:43:03 +0000 (22:43 +0100)
We previously had tooling for the opposite direction, let's complete the
work.

src/basic/filesystems.c
src/basic/filesystems.h
src/basic/generate-filesystem-list.py
src/basic/generate-filesystem-switch-case.py [new file with mode: 0755]
src/basic/meson.build

index d2c70f54cbadabbb28f4eef6bb9734fca38b236f..628a7dfa919a16f0fe241fc424c4be2f959ae79c 100644 (file)
@@ -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;
 }
 
index 48f6c0118765145dc7a9f1b1ddecd7424e64e8ed..6d07a97ba7c79206f5468e53ec2f514491c73090 100644 (file)
@@ -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);
 
index 8271b3fbeb27f14848ddab3da3a18e988fbdb6e2..52b74f176300fa67363d401d69cb18faddbf5673 100755 (executable)
@@ -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 (executable)
index 0000000..73b1d65
--- /dev/null
@@ -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
index 042ab86d5eb4f5fa4396b7f1e3b57f7d9a30d950..f0d5a1d2eab72ccb8abe31960b09a94f0fe39175 100644 (file)
@@ -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',