]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
btrfs-util: Add btrfs_subvol_set_read_only_at()
authorDaan De Meyer <daan.j.demeyer@gmail.com>
Thu, 1 Jun 2023 12:43:30 +0000 (14:43 +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 9f8fd024a41eea1aaadc83262f59245aaaf9993e..a3e175768b1dd1b3a6142f110ef14856f7c449e5 100644 (file)
@@ -152,11 +152,16 @@ int btrfs_subvol_make_fallback(const char *path, mode_t mode) {
         return 0; /* plain directory */
 }
 
-int btrfs_subvol_set_read_only_fd(int fd, bool b) {
+int btrfs_subvol_set_read_only_at(int dir_fd, const char *path, bool b) {
+        _cleanup_close_ int fd = -EBADF;
         uint64_t flags, nflags;
         struct stat st;
 
-        assert(fd >= 0);
+        assert(dir_fd >= 0 || dir_fd == AT_FDCWD);
+
+        fd = xopenat(dir_fd, path, O_RDONLY|O_NOCTTY|O_CLOEXEC|O_DIRECTORY, /* xopen_flags = */ 0, /* mode = */ 0);
+        if (fd < 0)
+                return fd;
 
         if (fstat(fd, &st) < 0)
                 return -errno;
@@ -174,16 +179,6 @@ int btrfs_subvol_set_read_only_fd(int fd, bool b) {
         return RET_NERRNO(ioctl(fd, BTRFS_IOC_SUBVOL_SETFLAGS, &nflags));
 }
 
-int btrfs_subvol_set_read_only(const char *path, bool b) {
-        _cleanup_close_ int fd = -EBADF;
-
-        fd = open(path, O_RDONLY|O_NOCTTY|O_CLOEXEC|O_DIRECTORY);
-        if (fd < 0)
-                return -errno;
-
-        return btrfs_subvol_set_read_only_fd(fd, b);
-}
-
 int btrfs_subvol_get_read_only_fd(int fd) {
         uint64_t flags;
         struct stat st;
index f79bb6e7ef826692808408326ed654b5b557db99..56a0169027c6cf2a9cde467e74ace9be18531b56 100644 (file)
@@ -89,8 +89,14 @@ 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);
+int btrfs_subvol_set_read_only_at(int dir_fd, const char *path, bool b);
+static inline int btrfs_subvol_set_read_only_fd(int fd, bool b) {
+        return btrfs_subvol_set_read_only_at(fd, NULL, b);
+}
+static inline int btrfs_subvol_set_read_only(const char *path, bool b) {
+        return btrfs_subvol_set_read_only_at(AT_FDCWD, path, b);
+}
+
 int btrfs_subvol_get_read_only_fd(int fd);
 
 int btrfs_subvol_get_id(int fd, const char *subvolume, uint64_t *ret);