]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
stat-util: add inode_type_to_string() helper for showing mode_t inode type as string
authorLennart Poettering <lennart@poettering.net>
Mon, 27 Mar 2023 16:08:27 +0000 (18:08 +0200)
committerLennart Poettering <lennart@poettering.net>
Wed, 29 Mar 2023 16:27:04 +0000 (18:27 +0200)
src/basic/stat-util.c
src/basic/stat-util.h

index 6eaa3da45926b5f417e5a8929df5ad34c5ffacaf..2b09ab63b2520eb72b2bb6a33b961fa235dda96f 100644 (file)
@@ -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;
+}
index 24684d5794ad02bdd7e1d9cd2f55a364081f236c..726d0644aa634a142e760df12b2911e022eccc44 100644 (file)
@@ -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);