add got_pathset_for_each_reverse()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
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;
}