From: Daan De Meyer Date: Thu, 1 Jun 2023 11:58:29 +0000 (+0200) Subject: btrfs-util: Add btrfs_is_subvol_at() X-Git-Tag: v254-rc1~269^2~9 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=6d2fd8df0a355342f5a8049f86d0ac915dd563fb;p=thirdparty%2Fsystemd.git btrfs-util: Add btrfs_is_subvol_at() --- diff --git a/src/shared/btrfs-util.c b/src/shared/btrfs-util.c index 03f9c0d0e69..4ee1f494ebf 100644 --- a/src/shared/btrfs-util.c +++ b/src/shared/btrfs-util.c @@ -70,32 +70,20 @@ static int extract_subvolume_name(const char *path, char **ret) { return 0; } -int btrfs_is_subvol_fd(int fd) { +int btrfs_is_subvol_at(int dir_fd, const char *path) { struct stat st; - assert(fd >= 0); + assert(dir_fd >= 0 || dir_fd == AT_FDCWD); /* On btrfs subvolumes always have the inode 256 */ - if (fstat(fd, &st) < 0) + if (fstatat(dir_fd, strempty(path), &st, (isempty(path) ? AT_EMPTY_PATH : 0)) < 0) return -errno; if (!btrfs_might_be_subvol(&st)) return 0; - return fd_is_fs_type(fd, BTRFS_SUPER_MAGIC); -} - -int btrfs_is_subvol(const char *path) { - _cleanup_close_ int fd = -EBADF; - - assert(path); - - fd = open(path, O_RDONLY|O_NOCTTY|O_CLOEXEC|O_DIRECTORY); - if (fd < 0) - return -errno; - - return btrfs_is_subvol_fd(fd); + return is_fs_type_at(dir_fd, path, BTRFS_SUPER_MAGIC); } int btrfs_subvol_make_fd(int fd, const char *subvolume) { diff --git a/src/shared/btrfs-util.h b/src/shared/btrfs-util.h index 0ce6d4b96ad..cee27ae5a67 100644 --- a/src/shared/btrfs-util.h +++ b/src/shared/btrfs-util.h @@ -43,8 +43,13 @@ typedef enum BtrfsRemoveFlags { BTRFS_REMOVE_QUOTA = 1 << 1, } BtrfsRemoveFlags; -int btrfs_is_subvol_fd(int fd); -int btrfs_is_subvol(const char *path); +int btrfs_is_subvol_at(int dir_fd, const char *path); +static inline int btrfs_is_subvol_fd(int fd) { + return btrfs_is_subvol_at(fd, NULL); +} +static inline int btrfs_is_subvol(const char *path) { + return btrfs_is_subvol_at(AT_FDCWD, path); +} int btrfs_get_block_device_at(int dir_fd, const char *path, dev_t *ret); static inline int btrfs_get_block_device(const char *path, dev_t *ret) {