From: Daan De Meyer Date: Thu, 1 Jun 2023 12:32:39 +0000 (+0200) Subject: btrfs-util: Add btrfs_subvol_remove_at() X-Git-Tag: v254-rc1~269^2~4 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=24dbe6039a6f2ef6bd5db4f2ff4836cfffed3c31;p=thirdparty%2Fsystemd.git btrfs-util: Add btrfs_subvol_remove_at() We also remove btrfs_subvol_remove_fd() because btrfs_subvol_remove_at() is more general. --- diff --git a/src/shared/btrfs-util.c b/src/shared/btrfs-util.c index 4ee1f494ebf..9f8fd024a41 100644 --- a/src/shared/btrfs-util.c +++ b/src/shared/btrfs-util.c @@ -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); } diff --git a/src/shared/btrfs-util.h b/src/shared/btrfs-util.h index cee27ae5a67..f79bb6e7ef8 100644 --- a/src/shared/btrfs-util.h +++ b/src/shared/btrfs-util.h @@ -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); diff --git a/src/shared/rm-rf.c b/src/shared/rm-rf.c index 8e459f9d693..2b394448515 100644 --- a/src/shared/rm-rf.c +++ b/src/shared/rm-rf.c @@ -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;