From 0f53cf81f66f4b62f68661f0d5a1a919b1db2686 Mon Sep 17 00:00:00 2001 From: Miao Xie Date: Thu, 20 Sep 2012 19:04:22 +0800 Subject: [PATCH] Btrfs-progs: introduce '-t' option into subvolume list command This patch introduces '-t' option into subvolume list command. By this option, we can output the result as a table. Signed-off-by: Wang Shilong Signed-off-by: Miao Xie --- btrfs-list.c | 64 +++++++++++++++++++++++++++++++++++++++++++++--- btrfs-list.h | 3 ++- cmds-subvolume.c | 14 ++++++++--- man/btrfs.8.in | 2 ++ 4 files changed, 74 insertions(+), 9 deletions(-) diff --git a/btrfs-list.c b/btrfs-list.c index c6d9a18f..d605871c 100644 --- a/btrfs-list.c +++ b/btrfs-list.c @@ -1334,6 +1334,25 @@ static void print_subvolume_column(struct root_info *subv, } } +static void print_single_volume_info_table(struct root_info *subv) +{ + int i; + + for (i = 0; i < BTRFS_LIST_ALL; i++) { + if (!btrfs_list_columns[i].need_print) + continue; + + print_subvolume_column(subv, i); + + if (i != BTRFS_LIST_PATH) + printf("\t"); + + if (i == BTRFS_LIST_TOP_LEVEL) + printf("\t"); + } + printf("\n"); +} + static void print_single_volume_info_default(struct root_info *subv) { int i; @@ -1351,21 +1370,58 @@ static void print_single_volume_info_default(struct root_info *subv) printf("\n"); } -static void print_all_volume_info_default(struct root_lookup *sorted_tree) +static void print_all_volume_info_tab_head() +{ + int i; + int len; + char barrier[20]; + + for (i = 0; i < BTRFS_LIST_ALL; i++) { + if (btrfs_list_columns[i].need_print) + printf("%s\t", btrfs_list_columns[i].name); + + if (i == BTRFS_LIST_ALL-1) + printf("\n"); + } + + for (i = 0; i < BTRFS_LIST_ALL; i++) { + memset(barrier, 0, sizeof(barrier)); + + if (btrfs_list_columns[i].need_print) { + len = strlen(btrfs_list_columns[i].name); + while (len--) + strcat(barrier, "-"); + + printf("%s\t", barrier); + } + if (i == BTRFS_LIST_ALL-1) + printf("\n"); + } +} + +static void print_all_volume_info(struct root_lookup *sorted_tree, + int is_tab_result) { struct rb_node *n; struct root_info *entry; + if (is_tab_result) + print_all_volume_info_tab_head(); + n = rb_first(&sorted_tree->root); while (n) { entry = rb_entry(n, struct root_info, sort_node); - print_single_volume_info_default(entry); + if (is_tab_result) + print_single_volume_info_table(entry); + else + print_single_volume_info_default(entry); n = rb_next(n); } } int btrfs_list_subvols(int fd, struct btrfs_list_filter_set *filter_set, - struct btrfs_list_comparer_set *comp_set) + struct btrfs_list_comparer_set *comp_set, + int is_tab_result) { struct root_lookup root_lookup; struct root_lookup root_sort; @@ -1389,7 +1445,7 @@ int btrfs_list_subvols(int fd, struct btrfs_list_filter_set *filter_set, __filter_and_sort_subvol(&root_lookup, &root_sort, filter_set, comp_set); - print_all_volume_info_default(&root_sort); + print_all_volume_info(&root_sort, is_tab_result); __free_all_subvolumn(&root_lookup); return ret; } diff --git a/btrfs-list.h b/btrfs-list.h index 26a5c173..9d3687ea 100644 --- a/btrfs-list.h +++ b/btrfs-list.h @@ -98,7 +98,8 @@ int btrfs_list_setup_comparer(struct btrfs_list_comparer_set **comp_set, int is_descending); int btrfs_list_subvols(int fd, struct btrfs_list_filter_set *filter_set, - struct btrfs_list_comparer_set *comp_set); + struct btrfs_list_comparer_set *comp_set, + int is_tab_result); int btrfs_list_find_updated_files(int fd, u64 root_id, u64 oldest_gen); int btrfs_list_get_default_subvolume(int fd, u64 *default_id); char *btrfs_list_path_for_root(int fd, u64 root); diff --git a/cmds-subvolume.c b/cmds-subvolume.c index f5da022f..8399e72e 100644 --- a/cmds-subvolume.c +++ b/cmds-subvolume.c @@ -260,12 +260,13 @@ static int cmd_subvol_delete(int argc, char **argv) } static const char * const cmd_subvol_list_usage[] = { - "btrfs subvolume list [-pur] [-s 0|1] [-g [+|-]value] [-c [+|-]value] " + "btrfs subvolume list [-purt] [-s 0|1] [-g [+|-]value] [-c [+|-]value] " "[--sort=gen,ogen,rootid,path] ", "List subvolumes (and snapshots)", "", "-p print parent ID", "-u print the uuid of subvolumes (and snapshots)", + "-t print the result as a table", "-s value list snapshots with generation in ascending/descending order", " (1: ascending, 0: descending)", "-r list readonly subvolumes (including snapshots)", @@ -292,6 +293,7 @@ static int cmd_subvol_list(int argc, char **argv) int order; int c; char *subvol; + int is_tab_result = 0; struct option long_options[] = { {"sort", 1, NULL, 'S'}, {0, 0, 0, 0} @@ -303,7 +305,7 @@ static int cmd_subvol_list(int argc, char **argv) optind = 1; while(1) { c = getopt_long(argc, argv, - "ps:urg:c:", long_options, NULL); + "ps:urg:c:t", long_options, NULL); if (c < 0) break; @@ -311,6 +313,9 @@ static int cmd_subvol_list(int argc, char **argv) case 'p': btrfs_list_setup_print_column(BTRFS_LIST_PARENT); break; + case 't': + is_tab_result = 1; + break; case 's': order = atoi(optarg); btrfs_list_setup_filter(&filter_set, @@ -382,7 +387,8 @@ static int cmd_subvol_list(int argc, char **argv) return 12; } - ret = btrfs_list_subvols(fd, filter_set, comparer_set); + ret = btrfs_list_subvols(fd, filter_set, comparer_set, + is_tab_result); if (ret) return 19; return 0; @@ -588,7 +594,7 @@ static int cmd_subvol_get_default(int argc, char **argv) btrfs_list_setup_filter(&filter_set, BTRFS_LIST_FILTER_ROOTID, default_id); - ret = btrfs_list_subvols(fd, filter_set, NULL); + ret = btrfs_list_subvols(fd, filter_set, NULL, 0); if (ret) return 19; return 0; diff --git a/man/btrfs.8.in b/man/btrfs.8.in index 5c95ccc6..3f7765d6 100644 --- a/man/btrfs.8.in +++ b/man/btrfs.8.in @@ -122,6 +122,8 @@ If \fI-p\fR is given, then \fIparent \fR is added to the output between ID and top level. The parent's ID may be used at mount time via the \fIsubvolrootid=\fR option. +\fB-t\fP print the result as a table. + \fB-r\fP only readonly subvolumes in the filesystem wille be listed. \fB-s\fP only snapshot subvolumes in the filesystem will be listed.