]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
btrfs-util: Add btrfs_subvol_remove_at()
authorDaan De Meyer <daan.j.demeyer@gmail.com>
Thu, 1 Jun 2023 12:32:39 +0000 (14:32 +0200)
committerDaan De Meyer <daan.j.demeyer@gmail.com>
Tue, 6 Jun 2023 12:42:03 +0000 (14:42 +0200)
We also remove btrfs_subvol_remove_fd() because btrfs_subvol_remove_at()
is more general.

src/shared/btrfs-util.c
src/shared/btrfs-util.h
src/shared/rm-rf.c

index 4ee1f494ebf4b28c1916d7464a2b238eba44477f..9f8fd024a41eea1aaadc83262f59245aaaf9993e 100644 (file)
@@ -17,6 +17,7 @@
 #include "alloc-util.h"
 #include "blockdev-util.h"
 #include "btrfs-util.h"
+#include "chase.h"
 #include "chattr-util.h"
 #include "copy.h"
 #include "fd-util.h"
@@ -1123,25 +1124,21 @@ static int subvol_remove_children(int fd, const char *subvolume, uint64_t subvol
         return 0;
 }
 
-int btrfs_subvol_remove(const char *path, BtrfsRemoveFlags flags) {
+int btrfs_subvol_remove_at(int dir_fd, const char *path, BtrfsRemoveFlags flags) {
         _cleanup_free_ char *subvolume = NULL;
         _cleanup_close_ int fd = -EBADF;
         int r;
 
         assert(path);
 
-        r = extract_subvolume_name(path, &subvolume);
-        if (r < 0)
-                return r;
-
-        fd = open_parent(path, O_CLOEXEC, 0);
+        fd = chase_and_openat(dir_fd, path, CHASE_PARENT|CHASE_EXTRACT_FILENAME, O_CLOEXEC, &subvolume);
         if (fd < 0)
                 return fd;
 
-        return subvol_remove_children(fd, subvolume, 0, flags);
-}
+        r = validate_subvolume_name(subvolume);
+        if (r < 0)
+                return r;
 
-int btrfs_subvol_remove_fd(int fd, const char *subvolume, BtrfsRemoveFlags flags) {
         return subvol_remove_children(fd, subvolume, 0, flags);
 }
 
index cee27ae5a679e43e99c53f70f82a54ef850b1f18..f79bb6e7ef826692808408326ed654b5b557db99 100644 (file)
@@ -84,8 +84,10 @@ static inline int btrfs_subvol_snapshot(const char *old_path, const char *new_pa
         return btrfs_subvol_snapshot_full(old_path, new_path, flags, NULL, NULL, NULL);
 }
 
-int btrfs_subvol_remove(const char *path, BtrfsRemoveFlags flags);
-int btrfs_subvol_remove_fd(int fd, const char *subvolume, BtrfsRemoveFlags flags);
+int btrfs_subvol_remove_at(int dir_fd, const char *path, BtrfsRemoveFlags flags);
+static inline int btrfs_subvol_remove(const char *path, BtrfsRemoveFlags flags) {
+        return btrfs_subvol_remove_at(AT_FDCWD, path, flags);
+}
 
 int btrfs_subvol_set_read_only_fd(int fd, bool b);
 int btrfs_subvol_set_read_only(const char *path, bool b);
index 8e459f9d693d83d2c424700487df7ebfaf22ea46..2b3944485150aad58038ad3db3fce32861568ac0 100644 (file)
@@ -228,7 +228,7 @@ static int rm_rf_inner_child(
                 if ((flags & REMOVE_SUBVOLUME) && btrfs_might_be_subvol(&st)) {
                         /* This could be a subvolume, try to remove it */
 
-                        r = btrfs_subvol_remove_fd(fd, fname, BTRFS_REMOVE_RECURSIVE|BTRFS_REMOVE_QUOTA);
+                        r = btrfs_subvol_remove_at(fd, fname, BTRFS_REMOVE_RECURSIVE|BTRFS_REMOVE_QUOTA);
                         if (r < 0) {
                                 if (!IN_SET(r, -ENOTTY, -EINVAL))
                                         return r;