diff mbox

Btrfs-progs: make btrfs_list_setup_filter to modify a set filter

Message ID 1348836948-19537-1-git-send-email-Anand.Jain@oracle.com (mailing list archive)
State New, archived
Headers show

Commit Message

Anand Jain Sept. 28, 2012, 12:55 p.m. UTC
From: Anand Jain <anand.jain@oracle.com>

Signed-off-by: Anand Jain <anand.jain@oracle.com>
---
 btrfs-list.c |   50 ++++++++++++++++++++++++++++++++------------------
 1 files changed, 32 insertions(+), 18 deletions(-)
diff mbox

Patch

diff --git a/btrfs-list.c b/btrfs-list.c
index e5f0f96..b1c9714 100644
--- a/btrfs-list.c
+++ b/btrfs-list.c
@@ -1213,37 +1213,51 @@  void btrfs_list_free_filter_set(struct btrfs_list_filter_set *filter_set)
 	free(filter_set);
 }
 
+static int btrfs_list_is_filter_set(struct btrfs_list_filter_set *fset,
+			enum btrfs_list_filter_enum filter)
+{
+	int i;
+	for (i=0; i < fset->nfilters; i++) {
+		if (fset->filters[i].filter_func == all_filter_funcs[filter])
+			return i;
+	}
+	return -1;
+}
+
 int btrfs_list_setup_filter(struct btrfs_list_filter_set **filter_set,
 			    enum btrfs_list_filter_enum filter, u64 data)
 {
 	struct btrfs_list_filter_set *set = *filter_set;
 	int size;
+	int nfilter;
 
 	BUG_ON(!set);
 	BUG_ON(filter >= BTRFS_LIST_FILTER_MAX);
 	BUG_ON(set->nfilters > set->total);
 
-	if (set->nfilters == set->total) {
-		size = set->total + BTRFS_LIST_NFILTERS_INCREASE;
-		size = sizeof(*set) + size * sizeof(struct btrfs_list_filter);
-		set = realloc(set, size);
-		if (!set) {
-			fprintf(stderr, "memory allocation failed\n");
-			exit(1);
-		}
+	nfilter = btrfs_list_is_filter_set(set, filter);
+	if (nfilter < 0) {
+		if (set->nfilters == set->total) {
+			size = set->total + BTRFS_LIST_NFILTERS_INCREASE;
+			size = sizeof(*set) + size * sizeof(struct btrfs_list_filter);
+			set = realloc(set, size);
+			if (!set) {
+				fprintf(stderr, "memory allocation failed\n");
+				exit(1);
+			}
 
-		memset(&set->filters[set->total], 0,
-		       BTRFS_LIST_NFILTERS_INCREASE *
-		       sizeof(struct btrfs_list_filter));
-		set->total += BTRFS_LIST_NFILTERS_INCREASE;
-		*filter_set = set;
+			memset(&set->filters[set->total], 0,
+			       BTRFS_LIST_NFILTERS_INCREASE *
+			       sizeof(struct btrfs_list_filter));
+			set->total += BTRFS_LIST_NFILTERS_INCREASE;
+			*filter_set = set;
+		}
+		nfilter = set->nfilters;
+		set->nfilters++;
 	}
 
-	BUG_ON(set->filters[set->nfilters].filter_func);
-
-	set->filters[set->nfilters].filter_func = all_filter_funcs[filter];
-	set->filters[set->nfilters].data = data;
-	set->nfilters++;
+	set->filters[nfilter].filter_func = all_filter_funcs[filter];
+	set->filters[nfilter].data = data;
 	return 0;
 }