mirror of
https://github.com/morgan9e/systemd
synced 2026-04-14 08:25:20 +09:00
We want to check if the magic we got from statfs() is one of the magics listed
for one of the file systems in the given group. To do this, we'd iteratate over
the file system names, convert each name to an array of magics, and compare
those to the one we got. We were using gperf-generated lookup table for this,
so the string lookups were quick, but still this seems unnecessarily complex.
Let's just generate a simple lookup function, because we can:
$ src/basic/filesystem-sets.py fs-in-group
bool fs_in_group(const struct statfs *st, FilesystemGroups fs_group) {
switch (fs_group) {
case FILESYSTEM_SET_BASIC_API:
return F_TYPE_EQUAL(st->f_type, CGROUP2_SUPER_MAGIC)
|| F_TYPE_EQUAL(st->f_type, CGROUP_SUPER_MAGIC)
|| F_TYPE_EQUAL(st->f_type, DEVPTS_SUPER_MAGIC)
|| F_TYPE_EQUAL(st->f_type, MQUEUE_MAGIC)
|| F_TYPE_EQUAL(st->f_type, PROC_SUPER_MAGIC)
|| F_TYPE_EQUAL(st->f_type, SYSFS_MAGIC)
|| F_TYPE_EQUAL(st->f_type, TMPFS_MAGIC);
case FILESYSTEM_SET_ANONYMOUS:
return F_TYPE_EQUAL(st->f_type, ANON_INODE_FS_MAGIC)
|| F_TYPE_EQUAL(st->f_type, PIPEFS_MAGIC)
|| F_TYPE_EQUAL(st->f_type, SOCKFS_MAGIC);
...
We flatten the nested lookup of group=>fs=>magic into a single level.
The compiler can work its magic here to make the lookup quick.
32 lines
832 B
C
32 lines
832 B
C
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
|
|
|
#include "filesystems-gperf.h"
|
|
#include "nulstr-util.h"
|
|
#include "stat-util.h"
|
|
#include "string-util.h"
|
|
|
|
int fs_type_from_string(const char *name, const statfs_f_type_t **ret) {
|
|
const struct FilesystemMagic *fs_magic;
|
|
|
|
assert(name);
|
|
assert(ret);
|
|
|
|
fs_magic = filesystems_gperf_lookup(name, strlen(name));
|
|
if (!fs_magic)
|
|
return -EINVAL;
|
|
|
|
*ret = fs_magic->magic;
|
|
return 0;
|
|
}
|
|
|
|
const FilesystemSet* filesystem_set_find(const char *name) {
|
|
if (isempty(name) || name[0] != '@')
|
|
return NULL;
|
|
|
|
for (FilesystemGroups i = 0; i < _FILESYSTEM_SET_MAX; i++)
|
|
if (streq(filesystem_sets[i].name, name))
|
|
return filesystem_sets + i;
|
|
|
|
return NULL;
|
|
}
|