From 62f9666ae03d3a2f0205ffa54eb4267faca43b74 Mon Sep 17 00:00:00 2001 From: Franck Bui Date: Fri, 27 Apr 2018 17:04:47 +0200 Subject: [PATCH] btrfs-util: introduce btrfs_subvol_make_fd() --- src/basic/btrfs-util.c | 26 +++++++++++++++++++------- src/basic/btrfs-util.h | 1 + 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/src/basic/btrfs-util.c b/src/basic/btrfs-util.c index efac0b94207..abd34824e7d 100644 --- a/src/basic/btrfs-util.c +++ b/src/basic/btrfs-util.c @@ -116,8 +116,25 @@ int btrfs_is_subvol(const char *path) { return btrfs_is_subvol_fd(fd); } -int btrfs_subvol_make(const char *path) { +int btrfs_subvol_make_fd(int fd, const char *subvolume) { struct btrfs_ioctl_vol_args args = {}; + int r; + + assert(subvolume); + + r = validate_subvolume_name(subvolume); + if (r < 0) + return r; + + strncpy(args.name, subvolume, sizeof(args.name)-1); + + if (ioctl(fd, BTRFS_IOC_SUBVOL_CREATE, &args) < 0) + return -errno; + + return 0; +} + +int btrfs_subvol_make(const char *path) { _cleanup_close_ int fd = -1; const char *subvolume; int r; @@ -132,12 +149,7 @@ int btrfs_subvol_make(const char *path) { if (fd < 0) return fd; - strncpy(args.name, subvolume, sizeof(args.name)-1); - - if (ioctl(fd, BTRFS_IOC_SUBVOL_CREATE, &args) < 0) - return -errno; - - return 0; + return btrfs_subvol_make_fd(fd, subvolume); } int btrfs_subvol_set_read_only_fd(int fd, bool b) { diff --git a/src/basic/btrfs-util.h b/src/basic/btrfs-util.h index a594387b5a9..b0cf6739f77 100644 --- a/src/basic/btrfs-util.h +++ b/src/basic/btrfs-util.h @@ -65,6 +65,7 @@ int btrfs_resize_loopback_fd(int fd, uint64_t size, bool grow_only); int btrfs_resize_loopback(const char *path, uint64_t size, bool grow_only); int btrfs_subvol_make(const char *path); +int btrfs_subvol_make_fd(int fd, const char *subvolume); int btrfs_subvol_snapshot_fd(int old_fd, const char *new_path, BtrfsSnapshotFlags flags); int btrfs_subvol_snapshot(const char *old_path, const char *new_path, BtrfsSnapshotFlags flags); -- 2.47.3