From 60d11eca6649700c1e39c19c36c903cad747e775 Mon Sep 17 00:00:00 2001 From: wangshilong Date: Wed, 19 Sep 2012 17:21:51 +0800 Subject: Btrfs-progs: introduce -g -c --sort options into btrfs subvol list command This patch introduces '-g' '-c' '--sort' options The option '-g' can help you filter the subvolumes by the generation, you may use it just like: btrfs subvol list -g +/-value '+' means the generation of the subvolumes should >= the value you specified. '-' means the generation should <= the value If you don't input either '+' nor '-', this command will list the subvolumes that their generation equals to the value. However if you want to find gengeration between value1 and value2 you may use the above like: btrfs sub list -g -value1 -g +value2 The option '-c' can help you filter the subvolumes by the ogeneration, you may use it just like: btrfs subvol list -c +/-value The usage is the same to '-g' You might want to list subvolumes in order of some items, such as root id, gen and so on, you can use '--sort'. Now you can sort the subvolumes by root id, gen, ogen and path. For example: If you want to list subvolumes in order of rootid, you can use the option like that: btrfs sub list --sort=+/-rooid Here, '+' means the result is sorted by ascending order. '-' is by descending order. If you don't specify either '+' nor '-', the result is sorted by default - ascending order. If you want to combine sort items, you do it like that: btrfs sub list --sort=-rootid,+path,ogen,gen Signed-off-by: Wang Shilong Signed-off-by: Miao Xie --- cmds-subvolume.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 46 insertions(+), 4 deletions(-) (limited to 'cmds-subvolume.c') diff --git a/cmds-subvolume.c b/cmds-subvolume.c index f385816f..f5da022f 100644 --- a/cmds-subvolume.c +++ b/cmds-subvolume.c @@ -23,6 +23,7 @@ #include #include #include +#include #include "kerncompat.h" #include "ioctl.h" @@ -259,7 +260,8 @@ static int cmd_subvol_delete(int argc, char **argv) } static const char * const cmd_subvol_list_usage[] = { - "btrfs subvolume list [-pur] [-s 0|1] ", + "btrfs subvolume list [-pur] [-s 0|1] [-g [+|-]value] [-c [+|-]value] " + "[--sort=gen,ogen,rootid,path] ", "List subvolumes (and snapshots)", "", "-p print parent ID", @@ -267,7 +269,17 @@ static const char * const cmd_subvol_list_usage[] = { "-s value list snapshots with generation in ascending/descending order", " (1: ascending, 0: descending)", "-r list readonly subvolumes (including snapshots)", - NULL + "-g [+|-]value", + " filter the subvolumes by generation", + " (+value: >= value; -value: <= value; value: = value)", + "-c [+|-]value", + " filter the subvolumes by ogeneration", + " (+value: >= value; -value: <= value; value: = value)", + "--sort=gen,ogen,rootid,path", + " list the subvolume in order of gen, ogen, rootid or path", + " you also can add '+' or '-' in front of each items.", + " (+:ascending, -:descending, ascending default)", + NULL, }; static int cmd_subvol_list(int argc, char **argv) @@ -278,14 +290,20 @@ static int cmd_subvol_list(int argc, char **argv) int fd; int ret; int order; + int c; char *subvol; + struct option long_options[] = { + {"sort", 1, NULL, 'S'}, + {0, 0, 0, 0} + }; filter_set = btrfs_list_alloc_filter_set(); comparer_set = btrfs_list_alloc_comparer_set(); optind = 1; while(1) { - int c = getopt(argc, argv, "ps:ur"); + c = getopt_long(argc, argv, + "ps:urg:c:", long_options, NULL); if (c < 0) break; @@ -303,13 +321,37 @@ static int cmd_subvol_list(int argc, char **argv) !order); btrfs_list_setup_print_column(BTRFS_LIST_OGENERATION); btrfs_list_setup_print_column(BTRFS_LIST_OTIME); - break; + case 'u': btrfs_list_setup_print_column(BTRFS_LIST_UUID); break; case 'r': flags |= BTRFS_ROOT_SUBVOL_RDONLY; break; + case 'g': + btrfs_list_setup_print_column(BTRFS_LIST_GENERATION); + ret = btrfs_list_parse_filter_string(optarg, + &filter_set, + BTRFS_LIST_FILTER_GEN); + if (ret) + usage(cmd_subvol_list_usage); + break; + + case 'c': + btrfs_list_setup_print_column(BTRFS_LIST_OGENERATION); + ret = btrfs_list_parse_filter_string(optarg, + &filter_set, + BTRFS_LIST_FILTER_CGEN); + if (ret) + usage(cmd_subvol_list_usage); + break; + case 'S': + ret = btrfs_list_parse_sort_string(optarg, + &comparer_set); + if (ret) + usage(cmd_subvol_list_usage); + break; + default: usage(cmd_subvol_list_usage); } -- cgit v1.2.3