]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
dirent-util: split out new function stat_mode_to_dirent_type()
authorLennart Poettering <lennart@poettering.net>
Fri, 1 Oct 2021 13:46:42 +0000 (15:46 +0200)
committerLennart Poettering <lennart@poettering.net>
Thu, 7 Oct 2021 09:58:26 +0000 (11:58 +0200)
This contains the mapping between mode_t inode type flags and dirent's
d_type. Splitting this out allows us to use the mapping elsewhere later.

src/basic/dirent-util.c
src/basic/dirent-util.h

index aba14481df258449e9cb4096a11c47dc07b23b3b..a70871b33dac991c46da9fc5f302798ee5e3249a 100644 (file)
@@ -7,6 +7,18 @@
 #include "path-util.h"
 #include "string-util.h"
 
+int stat_mode_to_dirent_type(mode_t mode) {
+        return
+                S_ISREG(mode)  ? DT_REG  :
+                S_ISDIR(mode)  ? DT_DIR  :
+                S_ISLNK(mode)  ? DT_LNK  :
+                S_ISFIFO(mode) ? DT_FIFO :
+                S_ISSOCK(mode) ? DT_SOCK :
+                S_ISCHR(mode)  ? DT_CHR  :
+                S_ISBLK(mode)  ? DT_BLK  :
+                                 DT_UNKNOWN;
+}
+
 static int dirent_ensure_type(DIR *d, struct dirent *de) {
         struct stat st;
 
@@ -24,15 +36,7 @@ static int dirent_ensure_type(DIR *d, struct dirent *de) {
         if (fstatat(dirfd(d), de->d_name, &st, AT_SYMLINK_NOFOLLOW) < 0)
                 return -errno;
 
-        de->d_type =
-                S_ISREG(st.st_mode)  ? DT_REG  :
-                S_ISDIR(st.st_mode)  ? DT_DIR  :
-                S_ISLNK(st.st_mode)  ? DT_LNK  :
-                S_ISFIFO(st.st_mode) ? DT_FIFO :
-                S_ISSOCK(st.st_mode) ? DT_SOCK :
-                S_ISCHR(st.st_mode)  ? DT_CHR  :
-                S_ISBLK(st.st_mode)  ? DT_BLK  :
-                                       DT_UNKNOWN;
+        de->d_type = stat_mode_to_dirent_type(st.st_mode);
 
         return 0;
 }
index c7956e7c1b7819ca839ccd989947151cbefeabae..5bbd54df5a8a4beff5b647b4cca7dbb82a9f24f0 100644 (file)
@@ -8,6 +8,8 @@
 #include "macro.h"
 #include "path-util.h"
 
+int stat_mode_to_dirent_type(mode_t mode);
+
 bool dirent_is_file(const struct dirent *de) _pure_;
 bool dirent_is_file_with_suffix(const struct dirent *de, const char *suffix) _pure_;