From: Christian Brauner Date: Mon, 10 Dec 2018 13:15:01 +0000 (+0100) Subject: btrfs: fix btrfs_snapshot() X-Git-Tag: lxc-2.0.10~69 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=6787abb592d4289d39e2379f0168ad75efc61d00;p=thirdparty%2Flxc.git btrfs: fix btrfs_snapshot() Signed-off-by: Christian Brauner --- diff --git a/src/lxc/storage/btrfs.c b/src/lxc/storage/btrfs.c index c351bee67..d2727b6dc 100644 --- a/src/lxc/storage/btrfs.c +++ b/src/lxc/storage/btrfs.c @@ -310,49 +310,52 @@ out: int btrfs_snapshot(const char *orig, const char *new) { - int fd, fddst, ret; size_t retlen; struct btrfs_ioctl_vol_args_v2 args; char *newdir, *newname; char *newfull = NULL; + int saved_errno = -1; + int fd = -1, fddst = -1, ret = -1; newfull = strdup(new); - if (!newfull) { - ERROR("Error: out of memory"); + if (!newfull) goto out; - } - // make sure the directory doesn't already exist - if (rmdir(newfull) < 0 && errno != ENOENT) { - SYSERROR("Error removing empty new rootfs"); + + ret = rmdir(newfull); + if (ret < 0 && errno != ENOENT) goto out; - } + newname = basename(newfull); - newdir = dirname(newfull); fd = open(orig, O_RDONLY); - if (fd < 0) { - SYSERROR("Error opening original rootfs %s", orig); + if (fd < 0) goto out; - } + + newdir = dirname(newfull); fddst = open(newdir, O_RDONLY); - if (fddst < 0) { - SYSERROR("Error opening new container dir %s", newdir); + if (fddst < 0) goto out; - } memset(&args, 0, sizeof(args)); + args.fd = fd; retlen = strlcpy(args.name, newname, BTRFS_SUBVOL_NAME_MAX); if (retlen >= BTRFS_SUBVOL_NAME_MAX) goto out; ret = ioctl(fddst, BTRFS_IOC_SNAP_CREATE_V2, &args); - INFO("btrfs: snapshot create ioctl returned %d", ret); + saved_errno = errno; out: if (fddst != -1) close(fddst); + if (fd != -1) close(fd); + free(newfull); + + if (saved_errno >= 0) + errno = saved_errno; + return ret; }