diff options
author | Lennart Poettering <lennart@poettering.net> | 2018-02-19 18:01:05 +0100 |
---|---|---|
committer | Lennart Poettering <lennart@poettering.net> | 2018-02-20 15:39:31 +0100 |
commit | 3cc44114038621237a9d8ee4be3ff836138a55c1 (patch) | |
tree | 667e511d5a69feb4c11ab5830f2669b723764401 /src/basic/stat-util.c | |
parent | 9c66f528138f4fc856b3e9e137245b7048d5747d (diff) | |
download | systemd-3cc44114038621237a9d8ee4be3ff836138a55c1.tar.gz |
stat-util: unify code that checks whether something is a regular file
Let's add a common implementation for regular file checks, that are
careful to return the right error code (EISDIR/EISLNK/EBADFD) when we
are encountering a wrong file node.
Diffstat (limited to 'src/basic/stat-util.c')
-rw-r--r-- | src/basic/stat-util.c | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/src/basic/stat-util.c b/src/basic/stat-util.c index 0fb6750a07..3689f6e983 100644 --- a/src/basic/stat-util.c +++ b/src/basic/stat-util.c @@ -269,3 +269,32 @@ int path_is_temporary_fs(const char *path) { return fd_is_temporary_fs(fd); } + +int stat_verify_regular(const struct stat *st) { + assert(st); + + /* Checks whether the specified stat() structure refers to a regular file. If not returns an appropriate error + * code. */ + + if (S_ISDIR(st->st_mode)) + return -EISDIR; + + if (S_ISLNK(st->st_mode)) + return -ELOOP; + + if (!S_ISREG(st->st_mode)) + return -EBADFD; + + return 0; +} + +int fd_verify_regular(int fd) { + struct stat st; + + assert(fd >= 0); + + if (fstat(fd, &st) < 0) + return -errno; + + return stat_verify_regular(&st); +} |