From: Christian Brauner Date: Tue, 1 Aug 2017 21:23:24 +0000 (+0200) Subject: utils: add has_fs_type() + is_fs_type() X-Git-Tag: lxc-2.1.0~33^2~5 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a035c53af117ac70dfc43c7c772efb9702461b21;p=thirdparty%2Flxc.git utils: add has_fs_type() + is_fs_type() Signed-off-by: Christian Brauner --- diff --git a/src/lxc/utils.c b/src/lxc/utils.c index f89c837d5..88692035f 100644 --- a/src/lxc/utils.c +++ b/src/lxc/utils.c @@ -2384,3 +2384,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; +} diff --git a/src/lxc/utils.h b/src/lxc/utils.h index 3465e6a6f..addfb7a05 100644 --- a/src/lxc/utils.h +++ b/src/lxc/utils.h @@ -36,6 +36,7 @@ #include #include #include +#include #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 */