]> git.ipfire.org Git - thirdparty/lxc.git/commitdiff
utils: add has_fs_type() + is_fs_type()
authorChristian Brauner <christian.brauner@ubuntu.com>
Tue, 1 Aug 2017 21:23:24 +0000 (23:23 +0200)
committerStéphane Graber <stgraber@ubuntu.com>
Tue, 15 Aug 2017 20:36:32 +0000 (16:36 -0400)
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
src/lxc/utils.c
src/lxc/utils.h

index 2a027393276d5c5b5b71e4dc99a99606e3870626..5409e7c04729cf2c180881d404de158f7735aed4 100644 (file)
@@ -2383,3 +2383,25 @@ void *must_realloc(void *orig, size_t sz)
 
        return ret;
 }
+
+bool is_fs_type(const struct statfs *fs, fs_type_magic magic_val)
+{
+       return (fs->f_type == (fs_type_magic)magic_val);
+}
+
+bool has_fs_type(const char *path, fs_type_magic magic_val)
+{
+       bool has_type;
+       int ret;
+       struct statfs sb;
+
+       ret = statfs(path, &sb);
+       if (ret < 0)
+               return false;
+
+       has_type = is_fs_type(&sb, magic_val);
+       if (!has_type && magic_val == RAMFS_MAGIC)
+               WARN("When the ramfs it a tmpfs statfs() might report tmpfs");
+
+       return has_type;
+}
index 990510e369bbb1fe44ffcf79b2d2173c684c3bb0..6351492b70069d4ebacf7249b1df3f01b91baa8f 100644 (file)
@@ -36,6 +36,7 @@
 #include <linux/loop.h>
 #include <sys/syscall.h>
 #include <sys/types.h>
+#include <sys/vfs.h>
 
 #include "initutils.h"
 
@@ -386,4 +387,9 @@ char *must_copy_string(const char *entry);
 /* Re-alllocate a pointer, do not fail */
 void *must_realloc(void *orig, size_t sz);
 
+/* __typeof__ should be safe to use with all compilers. */
+typedef __typeof__(((struct statfs *)NULL)->f_type) fs_type_magic;
+bool has_fs_type(const char *path, fs_type_magic magic_val);
+bool is_fs_type(const struct statfs *fs, fs_type_magic magic_val);
+
 #endif /* __LXC_UTILS_H */