From: Daan De Meyer Date: Thu, 1 Jun 2023 12:43:30 +0000 (+0200) Subject: btrfs-util: Add btrfs_subvol_set_read_only_at() X-Git-Tag: v254-rc1~269^2~2 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=77c66be37b9b55c690cb9981775d369146b6a120;p=thirdparty%2Fsystemd.git btrfs-util: Add btrfs_subvol_set_read_only_at() --- diff --git a/src/shared/btrfs-util.c b/src/shared/btrfs-util.c index 9f8fd024a41..a3e175768b1 100644 --- a/src/shared/btrfs-util.c +++ b/src/shared/btrfs-util.c @@ -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; diff --git a/src/shared/btrfs-util.h b/src/shared/btrfs-util.h index f79bb6e7ef8..56a0169027c 100644 --- a/src/shared/btrfs-util.h +++ b/src/shared/btrfs-util.h @@ -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);