From: Lennart Poettering Date: Mon, 27 Mar 2023 16:08:27 +0000 (+0200) Subject: stat-util: add inode_type_to_string() helper for showing mode_t inode type as string X-Git-Tag: v254-rc1~864^2~12 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=d83ce136367a36da3671c94cfe549549f99e4167;p=thirdparty%2Fsystemd.git stat-util: add inode_type_to_string() helper for showing mode_t inode type as string --- diff --git a/src/basic/stat-util.c b/src/basic/stat-util.c index 6eaa3da4592..2b09ab63b25 100644 --- a/src/basic/stat-util.c +++ b/src/basic/stat-util.c @@ -471,3 +471,26 @@ int inode_compare_func(const struct stat *a, const struct stat *b) { } DEFINE_HASH_OPS_WITH_KEY_DESTRUCTOR(inode_hash_ops, struct stat, inode_hash_func, inode_compare_func, free); + +const char* inode_type_to_string(mode_t m) { + + /* Returns a short string for the inode type. We use the same name as the underlying macros for each + * inode type. */ + + switch (m & S_IFMT) { + case S_IFREG: + return "reg"; + case S_IFDIR: + return "dir"; + case S_IFCHR: + return "chr"; + case S_IFBLK: + return "blk"; + case S_IFIFO: + return "fifo"; + case S_IFSOCK: + return "sock"; + } + + return NULL; +} diff --git a/src/basic/stat-util.h b/src/basic/stat-util.h index 24684d5794a..726d0644aa6 100644 --- a/src/basic/stat-util.h +++ b/src/basic/stat-util.h @@ -101,3 +101,5 @@ int statx_fallback(int dfd, const char *path, int flags, unsigned mask, struct s void inode_hash_func(const struct stat *q, struct siphash *state); int inode_compare_func(const struct stat *a, const struct stat *b); extern const struct hash_ops inode_hash_ops; + +const char* inode_type_to_string(mode_t m);