]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
stat-util: add statx() flavours of stat_verify_regular() + stat_verify_socket()
authorLennart Poettering <lennart@amutable.com>
Wed, 25 Feb 2026 11:13:31 +0000 (12:13 +0100)
committerLennart Poettering <lennart@amutable.com>
Tue, 3 Mar 2026 07:48:01 +0000 (08:48 +0100)
src/basic/stat-util.c
src/basic/stat-util.h

index 43f82dd92680917a919fcef568f2b7548ceffa97..c5702ca6ab3df2b1ae33ada7121dd7e7b7b58668 100644 (file)
@@ -49,22 +49,35 @@ static int verify_stat_at(
         return verify ? r : r >= 0;
 }
 
+static int mode_verify_regular(mode_t mode) {
+        if (S_ISDIR(mode))
+                return -EISDIR;
+
+        if (S_ISLNK(mode))
+                return -ELOOP;
+
+        if (!S_ISREG(mode))
+                return -EBADFD;
+
+        return 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;
+        return mode_verify_regular(st->st_mode);
+}
 
-        if (S_ISLNK(st->st_mode))
-                return -ELOOP;
+int statx_verify_regular(const struct statx *stx) {
+        assert(stx);
 
-        if (!S_ISREG(st->st_mode))
-                return -EBADFD;
+        if (!FLAGS_SET(stx->stx_mask, STATX_TYPE))
+                return -ENODATA;
 
-        return 0;
+        return mode_verify_regular(stx->stx_mode);
 }
 
 int verify_regular_at(int fd, const char *path, bool follow) {
@@ -78,31 +91,29 @@ int fd_verify_regular(int fd) {
         return verify_regular_at(fd, /* path= */ NULL, /* follow= */ false);
 }
 
-int stat_verify_directory(const struct stat *st) {
-        assert(st);
-
-        if (S_ISLNK(st->st_mode))
+static int mode_verify_directory(mode_t mode) {
+        if (S_ISLNK(mode))
                 return -ELOOP;
 
-        if (!S_ISDIR(st->st_mode))
+        if (!S_ISDIR(mode))
                 return -ENOTDIR;
 
         return 0;
 }
 
+int stat_verify_directory(const struct stat *st) {
+        assert(st);
+
+        return mode_verify_directory(st->st_mode);
+}
+
 int statx_verify_directory(const struct statx *stx) {
         assert(stx);
 
         if (!FLAGS_SET(stx->stx_mask, STATX_TYPE))
                 return -ENODATA;
 
-        if (S_ISLNK(stx->stx_mode))
-                return -ELOOP;
-
-        if (!S_ISDIR(stx->stx_mode))
-                return -ENOTDIR;
-
-        return 0;
+        return mode_verify_directory(stx->stx_mode);
 }
 
 int fd_verify_directory(int fd) {
@@ -142,21 +153,31 @@ int is_symlink(const char *path) {
         return verify_stat_at(AT_FDCWD, path, false, stat_verify_symlink, false);
 }
 
-int stat_verify_socket(const struct stat *st) {
-        assert(st);
-
-        if (S_ISLNK(st->st_mode))
+static mode_t mode_verify_socket(mode_t mode) {
+        if (S_ISLNK(mode))
                 return -ELOOP;
 
-        if (S_ISDIR(st->st_mode))
+        if (S_ISDIR(mode))
                 return -EISDIR;
 
-        if (!S_ISSOCK(st->st_mode))
+        if (!S_ISSOCK(mode))
                 return -ENOTSOCK;
 
         return 0;
 }
 
+int stat_verify_socket(const struct stat *st) {
+        assert(st);
+
+        return mode_verify_socket(st->st_mode);
+}
+
+int statx_verify_socket(const struct statx *stx) {
+        assert(stx);
+
+        return mode_verify_socket(stx->stx_mode);
+}
+
 int is_socket(const char *path) {
         assert(!isempty(path));
         return verify_stat_at(AT_FDCWD, path, /* follow= */ true, stat_verify_socket, /* verify= */ false);
index 0e4bec513b1ad889b89867cfeb5efa3ef85ff4cb..2336c775e96076ca4b0e0b72c2209dbfab0a698d 100644 (file)
@@ -7,6 +7,7 @@
 #include "basic-forward.h"
 
 int stat_verify_regular(const struct stat *st);
+int statx_verify_regular(const struct statx *stx);
 int verify_regular_at(int fd, const char *path, bool follow);
 int fd_verify_regular(int fd);
 
@@ -21,6 +22,7 @@ int fd_verify_symlink(int fd);
 int is_symlink(const char *path);
 
 int stat_verify_socket(const struct stat *st);
+int statx_verify_socket(const struct statx *stx);
 int is_socket(const char *path);
 
 int stat_verify_linked(const struct stat *st);