]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
btrfs-util: Add btrfs_is_subvol_at()
authorDaan De Meyer <daan.j.demeyer@gmail.com>
Thu, 1 Jun 2023 11:58:29 +0000 (13:58 +0200)
committerDaan De Meyer <daan.j.demeyer@gmail.com>
Tue, 6 Jun 2023 12:42:03 +0000 (14:42 +0200)
src/shared/btrfs-util.c
src/shared/btrfs-util.h

index 03f9c0d0e69944544b0993721dfdeed53fd7eca3..4ee1f494ebf4b28c1916d7464a2b238eba44477f 100644 (file)
@@ -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) {
index 0ce6d4b96ada717bb8ea1bd57b933c439300efc4..cee27ae5a679e43e99c53f70f82a54ef850b1f18 100644 (file)
@@ -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) {