chiark / gitweb /
btrfs-util: introduce btrfs_is_filesystem() and make use of it where appropriate
authorLennart Poettering <lennart@poettering.net>
Wed, 22 Apr 2015 11:08:19 +0000 (13:08 +0200)
committerSven Eden <yamakuzure@gmx.net>
Tue, 14 Mar 2017 07:09:35 +0000 (08:09 +0100)
Let's unify the code that checks whether an fd is on btrfs a bit.

(Also, rename btrfs_is_snapshot() to btrfs_is_subvol(), since that's
usually how this is referred to in our code)

src/shared/btrfs-util.c
src/shared/btrfs-util.h
src/shared/machine-image.c

index 5bf87a389e46faa94d26600700a022eaaca7ea06..5a1ed60558be24e0eea6c3f52c4fd111bf231c24 100644 (file)
@@ -83,10 +83,22 @@ static int extract_subvolume_name(const char *path, const char **subvolume) {
         return 0;
 }
 
-int btrfs_is_snapshot(int fd) {
-        struct stat st;
+int btrfs_is_filesystem(int fd) {
         struct statfs sfs;
 
+        assert(fd >= 0);
+
+        if (fstatfs(fd, &sfs) < 0)
+                return -errno;
+
+        return F_TYPE_EQUAL(sfs.f_type, BTRFS_SUPER_MAGIC);
+}
+
+int btrfs_is_subvol(int fd) {
+        struct stat st;
+
+        assert(fd >= 0);
+
         /* On btrfs subvolumes always have the inode 256 */
 
         if (fstat(fd, &st) < 0)
@@ -95,10 +107,7 @@ int btrfs_is_snapshot(int fd) {
         if (!S_ISDIR(st.st_mode) || st.st_ino != 256)
                 return 0;
 
-        if (fstatfs(fd, &sfs) < 0)
-                return -errno;
-
-        return F_TYPE_EQUAL(sfs.f_type, BTRFS_SUPER_MAGIC);
+        return btrfs_is_filesystem(fd);
 }
 
 int btrfs_subvol_make(const char *path) {
@@ -970,7 +979,7 @@ int btrfs_subvol_snapshot_fd(int old_fd, const char *new_path, BtrfsSnapshotFlag
         assert(old_fd >= 0);
         assert(new_path);
 
-        r = btrfs_is_snapshot(old_fd);
+        r = btrfs_is_subvol(old_fd);
         if (r < 0)
                 return r;
         if (r == 0) {
index 02e46e30eaf8735eb301cef25bb10fc82833f111..a7eb895c93f348ac7dcd8b6dd24da872de9faf1b 100644 (file)
@@ -49,7 +49,8 @@ typedef enum BtrfsSnapshotFlags {
         BTRFS_SNAPSHOT_RECURSIVE = 4,
 } BtrfsSnapshotFlags;
 
-int btrfs_is_snapshot(int fd);
+int btrfs_is_filesystem(int fd);
+int btrfs_is_subvol(int fd);
 
 int btrfs_subvol_make(const char *path);
 int btrfs_subvol_make_label(const char *path);
index 0b41860b5d820b5f0240c5087763da7b56fcf09b..bc215f0ad5ca60e60d8fa600c6879717c354d65c 100644 (file)
@@ -136,12 +136,11 @@ static int image_make(
 
                 /* btrfs subvolumes have inode 256 */
                 if (st.st_ino == 256) {
-                        struct statfs sfs;
 
-                        if (fstatfs(fd, &sfs) < 0)
-                                return -errno;
-
-                        if (F_TYPE_EQUAL(sfs.f_type, BTRFS_SUPER_MAGIC)) {
+                        r = btrfs_is_filesystem(fd);
+                        if (r < 0)
+                                return r;
+                        if (r) {
                                 BtrfsSubvolInfo info;
                                 BtrfsQuotaInfo quota;