summaryrefslogtreecommitdiff
path: root/cmds-subvolume.c
diff options
context:
space:
mode:
authorMiao Xie <miaox@cn.fujitsu.com>2012-09-18 14:51:49 +0800
committerroot <root@localhost.localdomain>2012-10-04 16:26:32 -0400
commit162df1e30c7c0492ae9fb551d74452e643f5fea2 (patch)
treee57d3bb0c42efd150124a37d8adbe7632f0c649b /cmds-subvolume.c
parent7cb1cf754229062ea412aa651a0f750ce42c113d (diff)
Btrfs-progs: restructure list_subvolumes
The current code of list_subvols() has very bad scalability, if we want to add new filter conditions or new sort methods, we have to modify lots of code. Beside that, the most code of list_snapshots() is similar to list_subvols(), So I restructure list_subvols(), and split the subvolume filter function, the subvolume sort function and the output function from list_subvols(). In order to implement it, we defined some importtant structures: struct btrfs_list_filter { btrfs_list_filter_func filter_func; void *data; }; struct btrfs_list_comparer { btrfs_list_comp_func comp_func; int is_descending; }; struct { char *name; char *column_name; int need_print; } btrfs_list_columns[]; If we want to add a new filter condition, we can choose a suitable filter function, or implement a new filter function[1], and add it into a set of the filters, and then pass the filter set into list_subvols(). We also can mix several filters (just add those filters into the set, and pass the set into list_subvols()) if the users specify two or more filter conditions. The subvolume sort function is similar to the subvolume filter function. The differentiation is the order of comparers in the array which is passed into list_subvols() show us the priority of the sort methods. The output function is different with the above two functions, we define a array to manage all the columns that can be outputed, and use a member variant (->need_print) to control the output of the relative column. Some columns are outputed by default. But we can change it according to the requirement of the users. After appling this patch, we needn't implement a independent list_snapshots() function, just pass a filter function which is used to identify the snapshot into list_subvols(). [1]: If we implement new filter functions or compare functions, we must add them into the array all_filter_funcs or the array all_comp_funcs, and modify the relative enum variants(btrfs_list_filter_enum, btrfs_list_comp_enum). Signed-off-by: Miao Xie <miaox@cn.fujitsu.com>
Diffstat (limited to 'cmds-subvolume.c')
-rw-r--r--cmds-subvolume.c59
1 files changed, 46 insertions, 13 deletions
diff --git a/cmds-subvolume.c b/cmds-subvolume.c
index cd4b5a72..59f4dfdd 100644
--- a/cmds-subvolume.c
+++ b/cmds-subvolume.c
@@ -28,6 +28,7 @@
#include "ioctl.h"
#include "qgroup.h"
+#include "ctree.h"
#include "commands.h"
#include "btrfs-list.h"
@@ -270,13 +271,15 @@ static const char * const cmd_subvol_list_usage[] = {
static int cmd_subvol_list(int argc, char **argv)
{
+ struct btrfs_list_filter_set *filter_set;
+ struct btrfs_list_comparer_set *comparer_set;
int fd;
int ret;
- int print_parent = 0;
- int print_snap_only = 0;
- int order = 0;
+ int order;
char *subvol;
- int print_uuid = 0;
+
+ filter_set = btrfs_list_alloc_filter_set();
+ comparer_set = btrfs_list_alloc_comparer_set();
optind = 1;
while(1) {
@@ -286,14 +289,21 @@ static int cmd_subvol_list(int argc, char **argv)
switch(c) {
case 'p':
- print_parent = 1;
+ btrfs_list_setup_print_column(BTRFS_LIST_PARENT);
break;
case 's':
- print_snap_only = 1;
order = atoi(optarg);
+ btrfs_list_setup_filter(&filter_set,
+ BTRFS_LIST_FILTER_SNAPSHOT_ONLY,
+ 0);
+ btrfs_list_setup_comparer(&comparer_set,
+ BTRFS_LIST_COMP_OGEN,
+ !order);
+ btrfs_list_setup_print_column(BTRFS_LIST_OGENERATION);
+ btrfs_list_setup_print_column(BTRFS_LIST_OTIME);
break;
case 'u':
- print_uuid =1;
+ btrfs_list_setup_print_column(BTRFS_LIST_UUID);
break;
default:
usage(cmd_subvol_list_usage);
@@ -320,10 +330,8 @@ static int cmd_subvol_list(int argc, char **argv)
fprintf(stderr, "ERROR: can't access '%s'\n", subvol);
return 12;
}
- if (!print_snap_only)
- ret = list_subvols(fd, print_parent, 0, print_uuid);
- else
- ret = list_snapshots(fd, print_parent, order, print_uuid);
+
+ ret = btrfs_list_subvols(fd, filter_set, comparer_set);
if (ret)
return 19;
return 0;
@@ -483,6 +491,8 @@ static int cmd_subvol_get_default(int argc, char **argv)
int fd;
int ret;
char *subvol;
+ struct btrfs_list_filter_set *filter_set;
+ u64 default_id;
if (check_argc_exact(argc, 2))
usage(cmd_subvol_get_default_usage);
@@ -504,7 +514,30 @@ static int cmd_subvol_get_default(int argc, char **argv)
fprintf(stderr, "ERROR: can't access '%s'\n", subvol);
return 12;
}
- ret = list_subvols(fd, 0, 1, 0);
+
+ ret = btrfs_list_get_default_subvolume(fd, &default_id);
+ if (ret) {
+ fprintf(stderr, "ERROR: can't perform the search - %s\n",
+ strerror(errno));
+ return ret;
+ }
+
+ if (default_id == 0) {
+ fprintf(stderr, "ERROR: 'default' dir item not found\n");
+ return ret;
+ }
+
+ /* no need to resolve roots if FS_TREE is default */
+ if (default_id == BTRFS_FS_TREE_OBJECTID) {
+ printf("ID 5 (FS_TREE)\n");
+ return ret;
+ }
+
+ filter_set = btrfs_list_alloc_filter_set();
+ btrfs_list_setup_filter(&filter_set, BTRFS_LIST_FILTER_ROOTID,
+ default_id);
+
+ ret = btrfs_list_subvols(fd, filter_set, NULL);
if (ret)
return 19;
return 0;
@@ -585,7 +618,7 @@ static int cmd_find_new(int argc, char **argv)
fprintf(stderr, "ERROR: can't access '%s'\n", subvol);
return 12;
}
- ret = find_updated_files(fd, 0, last_gen);
+ ret = btrfs_list_find_updated_files(fd, 0, last_gen);
if (ret)
return 19;
return 0;