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
committerSven Eden <yamakuzure@gmx.net>2018-05-30 07:58:59 +0200
commita9f82387ce656c98cad320372002eee70369fdb4 (patch)
treecf23834b2e2a680db0fae7d6dc7b7a1e2408b18f /src/basic/stat-util.c
parent59f611201b378b84526a37608cbfa07621ec2ab3 (diff)
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 270e80fee..313ed4f2c 100644
--- a/src/basic/stat-util.c
+++ b/src/basic/stat-util.c
@@ -281,3 +281,32 @@ int path_is_temporary_fs(const char *path) {
return fd_is_temporary_fs(fd);
}
#endif // 0
+
+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);
+}