Commit efaf56b722e9595f43c4177384f7e54c88fbc285

Stefan Sperling 2019-01-08T19:56:13

add got_pathset_for_each_reverse()

diff --git a/lib/got_lib_pathset.h b/lib/got_lib_pathset.h
index a9b72d3..2eb5498 100644
--- a/lib/got_lib_pathset.h
+++ b/lib/got_lib_pathset.h
@@ -28,4 +28,7 @@ int got_pathset_contains(struct got_pathset *, const char *);
 const struct got_error *got_pathset_for_each(struct got_pathset *,
     const struct got_error *(*cb)(const char *, void *, void *),
     void *);
+const struct got_error *got_pathset_for_each_reverse(struct got_pathset *,
+    const struct got_error *(*cb)(const char *, void *, void *),
+    void *);
 int got_pathset_num_elements(struct got_pathset *);
diff --git a/lib/pathset.c b/lib/pathset.c
index 2ba5c86..6373c0d 100644
--- a/lib/pathset.c
+++ b/lib/pathset.c
@@ -199,6 +199,21 @@ got_pathset_for_each(struct got_pathset *set,
 	return NULL;
 }
 
+const struct got_error *
+got_pathset_for_each_reverse(struct got_pathset *set,
+    const struct got_error *(*cb)(const char *, void *, void *), void *arg)
+{
+	const struct got_error *err;
+	struct got_pathset_element *entry, *tmp;
+
+	RB_FOREACH_REVERSE_SAFE(entry, got_pathset_tree, &set->entries, tmp) {
+		err = (*cb)(entry->path, entry->data, arg);
+		if (err)
+			return err;
+	}
+	return NULL;
+}
+
 int
 got_pathset_num_elements(struct got_pathset *set)
 {
diff --git a/regress/pathset/pathset_test.c b/regress/pathset/pathset_test.c
index c003ce5..29d5f29 100644
--- a/regress/pathset/pathset_test.c
+++ b/regress/pathset/pathset_test.c
@@ -130,7 +130,8 @@ done:
 }
 
 static const struct got_error *
-pathset_iter_ordering_cb(const char *path, void *data, void *arg) {
+pathset_iter_order_cb(const char *path, void *data, void *arg)
+{
 	static int i;
 	test_printf("%s\n", path);
 	if (i == 0 && strcmp(path, "/") != 0)
@@ -151,8 +152,31 @@ pathset_iter_ordering_cb(const char *path, void *data, void *arg) {
 	return NULL;
 }
 
+static const struct got_error *
+pathset_iter_reverse_order_cb(const char *path, void *data, void *arg)
+{
+	static int i;
+	test_printf("%s\n", path);
+	if (i == 0 && strcmp(path, "/usr.sbin/zic") != 0)
+		abort();
+	if (i == 1 && strcmp(path, "/usr.sbin/unbound") != 0)
+		abort();
+	if (i == 2 && strcmp(path, "/usr.sbin") != 0)
+		abort();
+	if (i == 3 && strcmp(path, "/usr.bin/vi") != 0)
+		abort();
+	if (i == 4 && strcmp(path, "/usr.bin") != 0)
+		abort();
+	if (i == 5 && strcmp(path, "/") != 0)
+		abort();
+	if (i > 5)
+		abort();
+	i++;
+	return NULL;
+}
+
 static int
-pathset_iter_ordering(void)
+pathset_iter_order(void)
 {
 	const struct got_error *err = NULL;
 	struct got_pathset *set;
@@ -187,7 +211,11 @@ pathset_iter_ordering(void)
 	if (err)
 		goto done;
 
-	got_pathset_for_each(set, pathset_iter_ordering_cb, NULL);
+	test_printf("normal order:\n");
+	got_pathset_for_each(set, pathset_iter_order_cb, NULL);
+	test_printf("reverse order:\n");
+	got_pathset_for_each_reverse(set, pathset_iter_reverse_order_cb,
+	    NULL);
 done:
 	got_pathset_free(set);
 	return (err == NULL);
@@ -229,7 +257,7 @@ main(int argc, char *argv[])
 	argv += optind;
 
 	RUN_TEST(pathset_add_remove_iter(), "pathset_add_remove_iter");
-	RUN_TEST(pathset_iter_ordering(), "pathset_iter_ordering");
+	RUN_TEST(pathset_iter_order(), "pathset_iter_order");
 
 	return failure ? 1 : 0;
 }