btrfs: fix btrfs_snapshot()

parent edc59b8c
...@@ -310,49 +310,52 @@ out: ...@@ -310,49 +310,52 @@ out:
int btrfs_snapshot(const char *orig, const char *new) int btrfs_snapshot(const char *orig, const char *new)
{ {
int fd, fddst, ret;
size_t retlen; size_t retlen;
struct btrfs_ioctl_vol_args_v2 args; struct btrfs_ioctl_vol_args_v2 args;
char *newdir, *newname; char *newdir, *newname;
char *newfull = NULL; char *newfull = NULL;
int saved_errno = -1;
int fd = -1, fddst = -1, ret = -1;
newfull = strdup(new); newfull = strdup(new);
if (!newfull) { if (!newfull)
ERROR("Error: out of memory");
goto out; goto out;
}
// make sure the directory doesn't already exist ret = rmdir(newfull);
if (rmdir(newfull) < 0 && errno != ENOENT) { if (ret < 0 && errno != ENOENT)
SYSERROR("Error removing empty new rootfs");
goto out; goto out;
}
newname = basename(newfull); newname = basename(newfull);
newdir = dirname(newfull);
fd = open(orig, O_RDONLY); fd = open(orig, O_RDONLY);
if (fd < 0) { if (fd < 0)
SYSERROR("Error opening original rootfs %s", orig);
goto out; goto out;
}
newdir = dirname(newfull);
fddst = open(newdir, O_RDONLY); fddst = open(newdir, O_RDONLY);
if (fddst < 0) { if (fddst < 0)
SYSERROR("Error opening new container dir %s", newdir);
goto out; goto out;
}
memset(&args, 0, sizeof(args)); memset(&args, 0, sizeof(args));
args.fd = fd;
retlen = strlcpy(args.name, newname, BTRFS_SUBVOL_NAME_MAX); retlen = strlcpy(args.name, newname, BTRFS_SUBVOL_NAME_MAX);
if (retlen >= BTRFS_SUBVOL_NAME_MAX) if (retlen >= BTRFS_SUBVOL_NAME_MAX)
goto out; goto out;
ret = ioctl(fddst, BTRFS_IOC_SNAP_CREATE_V2, &args); ret = ioctl(fddst, BTRFS_IOC_SNAP_CREATE_V2, &args);
INFO("btrfs: snapshot create ioctl returned %d", ret); saved_errno = errno;
out: out:
if (fddst != -1) if (fddst != -1)
close(fddst); close(fddst);
if (fd != -1) if (fd != -1)
close(fd); close(fd);
free(newfull); free(newfull);
if (saved_errno >= 0)
errno = saved_errno;
return ret; return ret;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment