commit efaf56b722e9595f43c4177384f7e54c88fbc285 from: Stefan Sperling date: Tue Jan 08 18:56:13 2019 UTC add got_pathset_for_each_reverse() commit - aaa0878e4df2825d2597e407b5015a0e663ec7f8 commit + efaf56b722e9595f43c4177384f7e54c88fbc285 blob - a9b72d3ee4624c553d9cb7e7dfcaa30e4360a5b2 blob + 2eb549818d8d6c862e1ce24147a7c43c36d91754 --- lib/got_lib_pathset.h +++ lib/got_lib_pathset.h @@ -28,4 +28,7 @@ int got_pathset_contains(struct got_pathset *, const c 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 *); blob - 2ba5c863e4fd65416044b32696705d6ff47fb160 blob + 6373c0d0b3bc7daac7f957b796f78b80f03b7c97 --- lib/pathset.c +++ 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) { blob - c003ce56c94d3d571813042a5f7fa377886226ee blob + 29d5f290dcaf0a7a26ab04774cabba469daa5e7f --- regress/pathset/pathset_test.c +++ 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, 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; }