btrfs-progs: use libbtrfsutil for get-default

The only thing of note here is the "top level" column. This used to mean
something else, but for a long time it has been equal to the parent ID.
I preserved this for backwards compatability.

Signed-off-by: Omar Sandoval <osandov@fb.com>
Signed-off-by: David Sterba <dsterba@suse.com>
This commit is contained in:
Omar Sandoval 2018-01-19 14:52:11 -08:00 committed by David Sterba
parent 2116398b1d
commit 9e73a416f0

View File

@ -14,6 +14,7 @@
* Boston, MA 021110-1307, USA. * Boston, MA 021110-1307, USA.
*/ */
#include <inttypes.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -806,31 +807,25 @@ static const char * const cmd_subvol_get_default_usage[] = {
static int cmd_subvol_get_default(int argc, char **argv) static int cmd_subvol_get_default(int argc, char **argv)
{ {
int fd = -1; int fd = -1;
int ret; int ret = 1;
char *subvol; uint64_t default_id;
struct btrfs_list_filter_set *filter_set;
u64 default_id;
DIR *dirstream = NULL; DIR *dirstream = NULL;
enum btrfs_util_error err;
struct btrfs_util_subvolume_info subvol;
char *path;
clean_args_no_options(argc, argv, cmd_subvol_get_default_usage); clean_args_no_options(argc, argv, cmd_subvol_get_default_usage);
if (check_argc_exact(argc - optind, 1)) if (check_argc_exact(argc - optind, 1))
usage(cmd_subvol_get_default_usage); usage(cmd_subvol_get_default_usage);
subvol = argv[1]; fd = btrfs_open_dir(argv[1], &dirstream, 1);
fd = btrfs_open_dir(subvol, &dirstream, 1);
if (fd < 0) if (fd < 0)
return 1; return 1;
ret = btrfs_list_get_default_subvolume(fd, &default_id); err = btrfs_util_get_default_subvolume_fd(fd, &default_id);
if (ret) { if (err) {
error("failed to look up default subvolume: %m"); error_btrfs_util(err);
goto out;
}
ret = 1;
if (default_id == 0) {
error("'default' dir item not found");
goto out; goto out;
} }
@ -841,24 +836,27 @@ static int cmd_subvol_get_default(int argc, char **argv)
goto out; goto out;
} }
filter_set = btrfs_list_alloc_filter_set(); err = btrfs_util_subvolume_info_fd(fd, default_id, &subvol);
btrfs_list_setup_filter(&filter_set, BTRFS_LIST_FILTER_ROOTID, if (err) {
default_id); error_btrfs_util(err);
goto out;
}
/* by default we shall print the following columns*/ err = btrfs_util_subvolume_path_fd(fd, default_id, &path);
btrfs_list_setup_print_column(BTRFS_LIST_OBJECTID); if (err) {
btrfs_list_setup_print_column(BTRFS_LIST_GENERATION); error_btrfs_util(err);
btrfs_list_setup_print_column(BTRFS_LIST_TOP_LEVEL); goto out;
btrfs_list_setup_print_column(BTRFS_LIST_PATH); }
ret = btrfs_list_subvols_print(fd, filter_set, NULL, printf("ID %" PRIu64 " gen %" PRIu64 " top level %" PRIu64 " path %s\n",
BTRFS_LIST_LAYOUT_DEFAULT, 1, NULL); subvol.id, subvol.generation, subvol.parent_id, path);
if (filter_set) free(path);
free(filter_set);
ret = 0;
out: out:
close_file_or_dir(fd, dirstream); close_file_or_dir(fd, dirstream);
return !!ret; return ret;
} }
static const char * const cmd_subvol_set_default_usage[] = { static const char * const cmd_subvol_set_default_usage[] = {