stat-util: add statx() flavours of stat_verify_regular() + stat_verify_socket()

This commit is contained in:
Lennart Poettering
2026-02-25 12:13:31 +01:00
parent 36d7e177ad
commit 97fe03e12f
2 changed files with 48 additions and 25 deletions

View 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);

View 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);