btrfs-progs: btrfstune: consolidate error handling in main()

The upcoming "--device" option requires memory to parse devices, which
should be freed before returning from the main() function. As a
preparation for adding the "--device" option to the "btrfstune" command,
provide a consolidated error return exit from the main function with a
"goto" labeled "free_out". The label "free_out" may not make sense
currently, but it will when the "--device" option is added.

There are several return statements within the main function, and
changing all of them in the main "--device" feature patch would deviate
from the actual for the feature changes. Hence, it made sense to create
a preparatory patch.

The return value for any failure remains the same as in the original code.

Signed-off-by: Anand Jain <anand.jain@oracle.com>
Signed-off-by: David Sterba <dsterba@suse.com>
This commit is contained in:
Anand Jain 2023-08-03 07:29:46 +08:00 committed by David Sterba
parent 9ce233965f
commit 59fe1f46fe

View File

@ -232,19 +232,23 @@ int BOX_MAIN(btrfstune)(int argc, char *argv[])
set_argv0(argv);
device = argv[optind];
if (check_argc_exact(argc - optind, 1))
return 1;
if (check_argc_exact(argc - optind, 1)) {
ret = 1;
goto free_out;
}
if (random_fsid && new_fsid_str) {
error("random fsid can't be used with specified fsid");
return 1;
ret = 1;
goto free_out;
}
if (!super_flags && !seeding_flag && !(random_fsid || new_fsid_str) &&
!change_metadata_uuid && csum_type == -1 && !to_bg_tree &&
!to_extent_tree && !to_fst) {
error("at least one option should be specified");
usage(&tune_cmd, 1);
return 1;
ret = 1;
goto free_out;
}
if (new_fsid_str) {
@ -253,18 +257,21 @@ int BOX_MAIN(btrfstune)(int argc, char *argv[])
ret = uuid_parse(new_fsid_str, tmp);
if (ret < 0) {
error("could not parse UUID: %s", new_fsid_str);
return 1;
ret = 1;
goto free_out;
}
if (!test_uuid_unique(new_fsid_str)) {
error("fsid %s is not unique", new_fsid_str);
return 1;
ret = 1;
goto free_out;
}
}
fd = open(device, O_RDWR);
if (fd < 0) {
error("mount check: cannot open %s: %m", device);
return 1;
ret = 1;
goto free_out;
}
ret = check_mounted_where(fd, device, NULL, 0, NULL,
@ -273,18 +280,21 @@ int BOX_MAIN(btrfstune)(int argc, char *argv[])
errno = -ret;
error("could not check mount status of %s: %m", device);
close(fd);
return 1;
ret = 1;
goto free_out;
} else if (ret) {
error("%s is mounted", device);
close(fd);
return 1;
ret = 1;
goto free_out;
}
root = open_ctree_fd(fd, device, 0, ctree_flags);
if (!root) {
error("open ctree failed");
return 1;
ret = 1;
goto free_out;
}
/*
@ -451,5 +461,6 @@ out:
close_ctree(root);
btrfs_close_all_devices();
free_out:
return ret;
}