btrfs-progs: Free resources when returning error from cmd_subvol_create()

cmd_subvol_create() currently returns without freeing resources
in almost every error case.  Switch to a goto arrangement
so all cleanup can be done in one place.

Signed-off-by: Eric Sandeen <sandeen@redhat.com>
This commit is contained in:
Eric Sandeen 2013-03-04 16:40:00 -06:00 committed by David Sterba
parent 323318fd35
commit b1d5f20c3a

View File

@ -70,7 +70,8 @@ static const char * const cmd_subvol_create_usage[] = {
static int cmd_subvol_create(int argc, char **argv) static int cmd_subvol_create(int argc, char **argv)
{ {
int res, fddst, len, e; int retval, res, len;
int fddst = -1;
char *newname; char *newname;
char *dstdir; char *dstdir;
char *dst; char *dst;
@ -103,10 +104,11 @@ static int cmd_subvol_create(int argc, char **argv)
dst = argv[optind]; dst = argv[optind];
retval = 1; /* failure */
res = test_isdir(dst); res = test_isdir(dst);
if (res >= 0) { if (res >= 0) {
fprintf(stderr, "ERROR: '%s' exists\n", dst); fprintf(stderr, "ERROR: '%s' exists\n", dst);
return 1; goto out;
} }
newname = strdup(dst); newname = strdup(dst);
@ -118,20 +120,20 @@ static int cmd_subvol_create(int argc, char **argv)
strchr(newname, '/') ){ strchr(newname, '/') ){
fprintf(stderr, "ERROR: uncorrect subvolume name ('%s')\n", fprintf(stderr, "ERROR: uncorrect subvolume name ('%s')\n",
newname); newname);
return 1; goto out;
} }
len = strlen(newname); len = strlen(newname);
if (len == 0 || len >= BTRFS_VOL_NAME_MAX) { if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
fprintf(stderr, "ERROR: subvolume name too long ('%s)\n", fprintf(stderr, "ERROR: subvolume name too long ('%s)\n",
newname); newname);
return 1; goto out;
} }
fddst = open_file_or_dir(dstdir); fddst = open_file_or_dir(dstdir);
if (fddst < 0) { if (fddst < 0) {
fprintf(stderr, "ERROR: can't access to '%s'\n", dstdir); fprintf(stderr, "ERROR: can't access to '%s'\n", dstdir);
return 1; goto out;
} }
printf("Create subvolume '%s/%s'\n", dstdir, newname); printf("Create subvolume '%s/%s'\n", dstdir, newname);
@ -154,18 +156,19 @@ static int cmd_subvol_create(int argc, char **argv)
res = ioctl(fddst, BTRFS_IOC_SUBVOL_CREATE, &args); res = ioctl(fddst, BTRFS_IOC_SUBVOL_CREATE, &args);
} }
e = errno;
close(fddst);
free(inherit);
if (res < 0) { if (res < 0) {
fprintf(stderr, "ERROR: cannot create subvolume - %s\n", fprintf(stderr, "ERROR: cannot create subvolume - %s\n",
strerror(e)); strerror(errno));
return 1; goto out;
} }
return 0; retval = 0; /* success */
out:
if (fddst != -1)
close(fddst);
free(inherit);
return retval;
} }
/* /*