stat-util: introduce a common (stat|fd)_verify_regular_or_block() helper

We already had one in repart.c, let's generalize it, and use it
everywhere.
This commit is contained in:
Lennart Poettering
2026-04-13 13:20:42 +02:00
parent 2c664b9531
commit 6b0a3ef8c7
7 changed files with 41 additions and 45 deletions
+22
View File
@@ -220,6 +220,28 @@ int is_device_node(const char *path) {
return verify_stat_at(AT_FDCWD, path, false, stat_verify_device_node, false);
}
int stat_verify_regular_or_block(const struct stat *st) {
assert(st);
if (S_ISDIR(st->st_mode))
return -EISDIR;
if (S_ISLNK(st->st_mode))
return -ELOOP;
if (!S_ISREG(st->st_mode) && !S_ISBLK(st->st_mode))
return -EBADFD;
return 0;
}
int fd_verify_regular_or_block(int fd) {
if (IN_SET(fd, AT_FDCWD, XAT_FDROOT))
return -EISDIR;
return verify_stat_at(fd, /* path= */ NULL, /* follow= */ false, stat_verify_regular_or_block, /* verify= */ true);
}
int dir_is_empty_at(int dir_fd, const char *path, bool ignore_hidden_or_backup) {
_cleanup_close_ int fd = -EBADF;
struct dirent *buf;
+3
View File
@@ -31,6 +31,9 @@ int fd_verify_linked(int fd);
int stat_verify_device_node(const struct stat *st);
int is_device_node(const char *path);
int stat_verify_regular_or_block(const struct stat *st);
int fd_verify_regular_or_block(int fd);
int dir_is_empty_at(int dir_fd, const char *path, bool ignore_hidden_or_backup);
static inline int dir_is_empty(const char *path, bool ignore_hidden_or_backup) {
return dir_is_empty_at(AT_FDCWD, path, ignore_hidden_or_backup);