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.
This commit is contained in:
Lennart Poettering
2018-02-20 15:39:31 +01:00
parent 9c66f52813
commit 3cc4411403
8 changed files with 73 additions and 58 deletions
+29
View File
@@ -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);
}