_public_ int sd_journal_open_directory_fd(sd_journal **ret, int fd, int flags) {
_cleanup_(sd_journal_closep) sd_journal *j = NULL;
- struct stat st;
bool take_fd;
int r;
assert_return(fd >= 0, -EBADF);
assert_return((flags & ~OPEN_DIRECTORY_FD_ALLOWED_FLAGS) == 0, -EINVAL);
- if (fstat(fd, &st) < 0)
- return -errno;
-
- if (!S_ISDIR(st.st_mode))
- return -EBADFD;
+ r = fd_verify_directory(fd);
+ if (r < 0)
+ return r;
take_fd = FLAGS_SET(flags, SD_JOURNAL_TAKE_DIRECTORY_FD);
j = journal_new(flags & ~SD_JOURNAL_TAKE_DIRECTORY_FD, NULL, NULL);
struct btrfs_ioctl_vol_args vol_args = {};
_cleanup_close_ int subvol_fd = -EBADF;
- struct stat st;
bool made_writable = false;
int r;
assert(fd >= 0);
assert(subvolume);
- if (fstat(fd, &st) < 0)
- return -errno;
-
- if (!S_ISDIR(st.st_mode))
- return -EINVAL;
+ r = fd_verify_directory(fd);
+ if (r < 0)
+ return r;
subvol_fd = openat(fd, subvolume, O_RDONLY|O_NOCTTY|O_CLOEXEC|O_DIRECTORY|O_NOFOLLOW);
if (subvol_fd < 0)
#include "fd-util.h"
#include "fs-util.h"
#include "path-util.h"
+#include "stat-util.h"
#include "strv.h"
#include "user-util.h"
#include "xattr-util.h"
int duplicated_fd = -EBADF;
struct stat st;
+ int r;
/* Note that the slightly different order of fstat() and the checks here and in
* path_chown_recursive(). That's because when we open the directory ourselves we can specify
if (fstat(fd, &st) < 0)
return -errno;
- if (!S_ISDIR(st.st_mode))
- return -ENOTDIR;
+ r = stat_verify_directory(&st);
+ if (r < 0)
+ return r;
if (!uid_is_valid(uid) && !gid_is_valid(gid) && FLAGS_SET(mask, 07777))
return 0; /* nothing to do */
(unprivileged_mode && ERRNO_IS_NEG_PRIVILEGE(r)) ? LOG_DEBUG : LOG_ERR, r,
"Failed to determine block device node of \"%s\": %m", path);
- if (!S_ISDIR(sx.stx_mode))
- return log_error_errno(SYNTHETIC_ERRNO(ENOTDIR), "Path \"%s\" is not a directory", path);
+ r = statx_verify_directory(&sx);
+ if (r < 0)
+ return log_error_errno(r, "Path \"%s\" is not a directory", path);
if (!FLAGS_SET(sx.stx_attributes, STATX_ATTR_MOUNT_ROOT))
return log_full_errno(searching ? LOG_DEBUG : LOG_ERR,
if (fstat(fd, &st) < 0)
return log_error_errno(errno, "Failed to stat '%s': %m", p);
- if (!S_ISDIR(st.st_mode))
- return log_error_errno(SYNTHETIC_ERRNO(ENOTDIR), "ESP path '%s' is not a directory.", p);
+ r = stat_verify_directory(&st);
+ if (r < 0)
+ return log_error_errno(r, "ESP path '%s' is not a directory.", p);
if (ret_path)
*ret_path = TAKE_PTR(p);
if (fstat(fd, &st) < 0)
return log_error_errno(errno, "Failed to stat '%s': %m", p);
- if (!S_ISDIR(st.st_mode))
- return log_error_errno(SYNTHETIC_ERRNO(ENOTDIR), "XBOOTLDR path '%s' is not a directory.", p);
+ r = stat_verify_directory(&st);
+ if (r < 0)
+ return log_error_errno(r, "XBOOTLDR path '%s' is not a directory.", p);
if (ret_path)
*ret_path = TAKE_PTR(p);
if (fstat(dfd, &st) < 0)
return -errno;
- if (!S_ISDIR(st.st_mode))
- return -ENOTDIR;
+ r = stat_verify_directory(&st);
+ if (r < 0)
+ return r;
if (FLAGS_SET(st.st_mode, 0700)) { /* Already set? */
if (refuse_already_set)