summaryrefslogtreecommitdiff
path: root/src/basic/stat-util.c
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2018-02-19 18:01:05 +0100
committerLennart Poettering <lennart@poettering.net>2018-02-20 15:39:31 +0100
commit3cc44114038621237a9d8ee4be3ff836138a55c1 (patch)
tree667e511d5a69feb4c11ab5830f2669b723764401 /src/basic/stat-util.c
parent9c66f528138f4fc856b3e9e137245b7048d5747d (diff)
downloadsystemd-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.c29
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);
+}