summaryrefslogtreecommitdiffstats
path: root/src/basic/dirent-util.c
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2021-10-01 15:46:42 +0200
committerLennart Poettering <lennart@poettering.net>2021-10-07 11:58:26 +0200
commit21d46382e0a9f7d0b371c424de56fbd229c840f6 (patch)
treed314e2a24d49c5f9b0c74074a7538e140da2caeb /src/basic/dirent-util.c
parenttest: make OpenSSL checks optional in TEST-50-DISSECT (diff)
downloadsystemd-21d46382e0a9f7d0b371c424de56fbd229c840f6.tar.xz
systemd-21d46382e0a9f7d0b371c424de56fbd229c840f6.zip
dirent-util: split out new function stat_mode_to_dirent_type()
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.
Diffstat (limited to 'src/basic/dirent-util.c')
-rw-r--r--src/basic/dirent-util.c22
1 files changed, 13 insertions, 9 deletions
diff --git a/src/basic/dirent-util.c b/src/basic/dirent-util.c
index aba14481df..a70871b33d 100644
--- a/src/basic/dirent-util.c
+++ b/src/basic/dirent-util.c
@@ -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;
}