From 11e36726f61e8615bb873c2d322d85dafcd73e7b Mon Sep 17 00:00:00 2001 From: David Oberhollenzer Date: Fri, 28 Apr 2023 12:22:18 +0200 Subject: Move type based filtering to libutil dir_tree_iterator_t Limited unit testing for the flags is added, particularly the abillity to recurse into sub-directories, but not report the parents as individual entries. Signed-off-by: David Oberhollenzer --- lib/util/test/dir_tree_iterator.c | 9 ++- lib/util/test/dir_tree_iterator2.c | 160 +++++++++++++++++++++++++++++++++++++ 2 files changed, 166 insertions(+), 3 deletions(-) create mode 100644 lib/util/test/dir_tree_iterator2.c (limited to 'lib/util/test') diff --git a/lib/util/test/dir_tree_iterator.c b/lib/util/test/dir_tree_iterator.c index e47efed..7c1aa88 100644 --- a/lib/util/test/dir_tree_iterator.c +++ b/lib/util/test/dir_tree_iterator.c @@ -23,11 +23,14 @@ int main(int argc, char **argv) { dir_entry_t *ent[17]; dir_iterator_t *dir; + dir_tree_cfg_t cfg; size_t i; int ret; (void)argc; (void)argv; - dir = dir_tree_iterator_create(TEST_PATH); + memset(&cfg, 0, sizeof(cfg)); + + dir = dir_tree_iterator_create(TEST_PATH, &cfg); TEST_NOT_NULL(dir); for (i = 0; i < 16; ++i) { @@ -88,7 +91,7 @@ int main(int argc, char **argv) /* retry with skipping */ printf("**********\n"); - dir = dir_tree_iterator_create(TEST_PATH); + dir = dir_tree_iterator_create(TEST_PATH, &cfg); TEST_NOT_NULL(dir); for (i = 0; i < 13; ++i) { @@ -146,7 +149,7 @@ int main(int argc, char **argv) /* retry with skipping */ printf("**********\n"); - dir = dir_tree_iterator_create(TEST_PATH); + dir = dir_tree_iterator_create(TEST_PATH, &cfg); TEST_NOT_NULL(dir); for (i = 0; i < 9; ++i) { diff --git a/lib/util/test/dir_tree_iterator2.c b/lib/util/test/dir_tree_iterator2.c new file mode 100644 index 0000000..4b2b6f1 --- /dev/null +++ b/lib/util/test/dir_tree_iterator2.c @@ -0,0 +1,160 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later */ +/* + * dir_tree_iterator2.c + * + * Copyright (C) 2023 David Oberhollenzer + */ +#include "config.h" + +#include "util/dir_tree_iterator.h" +#include "sqfs/error.h" +#include "util/test.h" +#include "compat.h" + +static int compare_entries(const void *a, const void *b) +{ + const dir_entry_t *const *lhs = a; + const dir_entry_t *const *rhs = b; + + return strcmp((*lhs)->name, (*rhs)->name); +} + +int main(int argc, char **argv) +{ + dir_entry_t *ent[17]; + dir_iterator_t *dir; + dir_tree_cfg_t cfg; + size_t i; + int ret; + (void)argc; (void)argv; + + /********** without files **********/ + memset(&cfg, 0, sizeof(cfg)); + cfg.flags |= DIR_SCAN_NO_FILE; + + dir = dir_tree_iterator_create(TEST_PATH, &cfg); + TEST_NOT_NULL(dir); + + for (i = 0; i < 4; ++i) { + ret = dir->next(dir, &ent[i]); + TEST_NOT_NULL(ent[i]); + TEST_EQUAL_I(ret, 0); + printf("READ %s\n", ent[i]->name); + } + + ret = dir->next(dir, &ent[4]); + TEST_NULL(ent[4]); + TEST_ASSERT(ret > 0); + + dir = sqfs_drop(dir); + + qsort(ent, 4, sizeof(ent[0]), compare_entries); + + printf("After sort:\n"); + for (i = 0; i < 4; ++i) + printf("%s\n", ent[i]->name); + + TEST_STR_EQUAL(ent[0]->name, "dira"); + TEST_ASSERT(S_ISDIR(ent[0]->mode)); + TEST_STR_EQUAL(ent[1]->name, "dirb"); + TEST_ASSERT(S_ISDIR(ent[1]->mode)); + TEST_STR_EQUAL(ent[2]->name, "dirb/dirx"); + TEST_ASSERT(S_ISDIR(ent[2]->mode)); + TEST_STR_EQUAL(ent[3]->name, "dirc"); + TEST_ASSERT(S_ISDIR(ent[3]->mode)); + + for (i = 0; i < 4; ++i) + free(ent[i]); + + /********** recursive but without dirs **********/ + memset(&cfg, 0, sizeof(cfg)); + cfg.flags |= DIR_SCAN_NO_DIR; + + dir = dir_tree_iterator_create(TEST_PATH, &cfg); + TEST_NOT_NULL(dir); + + for (i = 0; i < 12; ++i) { + ret = dir->next(dir, &ent[i]); + TEST_NOT_NULL(ent[i]); + TEST_EQUAL_I(ret, 0); + printf("READ %s\n", ent[i]->name); + } + + ret = dir->next(dir, &ent[12]); + TEST_NULL(ent[12]); + TEST_ASSERT(ret > 0); + + dir = sqfs_drop(dir); + + qsort(ent, 12, sizeof(ent[0]), compare_entries); + + printf("After sort:\n"); + for (i = 0; i < 12; ++i) + printf("%s\n", ent[i]->name); + + TEST_STR_EQUAL(ent[0]->name, "dira/file_a0"); + TEST_ASSERT(S_ISREG(ent[0]->mode)); + TEST_STR_EQUAL(ent[1]->name, "dira/file_a1"); + TEST_ASSERT(S_ISREG(ent[1]->mode)); + TEST_STR_EQUAL(ent[2]->name, "dira/file_a2"); + TEST_ASSERT(S_ISREG(ent[2]->mode)); + TEST_STR_EQUAL(ent[3]->name, "dirb/dirx/file_x0"); + TEST_ASSERT(S_ISREG(ent[3]->mode)); + TEST_STR_EQUAL(ent[4]->name, "dirb/dirx/file_x1"); + TEST_ASSERT(S_ISREG(ent[4]->mode)); + TEST_STR_EQUAL(ent[5]->name, "dirb/dirx/file_x2"); + TEST_ASSERT(S_ISREG(ent[5]->mode)); + TEST_STR_EQUAL(ent[6]->name, "dirb/file_b0"); + TEST_ASSERT(S_ISREG(ent[6]->mode)); + TEST_STR_EQUAL(ent[7]->name, "dirb/file_b1"); + TEST_ASSERT(S_ISREG(ent[7]->mode)); + TEST_STR_EQUAL(ent[8]->name, "dirb/file_b2"); + TEST_ASSERT(S_ISREG(ent[8]->mode)); + TEST_STR_EQUAL(ent[9]->name, "dirc/file_c0"); + TEST_ASSERT(S_ISREG(ent[9]->mode)); + TEST_STR_EQUAL(ent[10]->name, "dirc/file_c1"); + TEST_ASSERT(S_ISREG(ent[10]->mode)); + TEST_STR_EQUAL(ent[11]->name, "dirc/file_c2"); + TEST_ASSERT(S_ISREG(ent[11]->mode)); + + for (i = 0; i < 12; ++i) + free(ent[i]); + + /********** non-recursive **********/ + memset(&cfg, 0, sizeof(cfg)); + cfg.flags |= DIR_SCAN_NO_RECURSION; + + dir = dir_tree_iterator_create(TEST_PATH, &cfg); + TEST_NOT_NULL(dir); + + for (i = 0; i < 3; ++i) { + ret = dir->next(dir, &ent[i]); + TEST_NOT_NULL(ent[i]); + TEST_EQUAL_I(ret, 0); + printf("READ %s\n", ent[i]->name); + } + + ret = dir->next(dir, &ent[3]); + TEST_NULL(ent[3]); + TEST_ASSERT(ret > 0); + + dir = sqfs_drop(dir); + + qsort(ent, 3, sizeof(ent[0]), compare_entries); + + printf("After sort:\n"); + for (i = 0; i < 3; ++i) + printf("%s\n", ent[i]->name); + + TEST_STR_EQUAL(ent[0]->name, "dira"); + TEST_ASSERT(S_ISDIR(ent[0]->mode)); + TEST_STR_EQUAL(ent[1]->name, "dirb"); + TEST_ASSERT(S_ISDIR(ent[1]->mode)); + TEST_STR_EQUAL(ent[2]->name, "dirc"); + TEST_ASSERT(S_ISDIR(ent[2]->mode)); + + for (i = 0; i < 3; ++i) + free(ent[i]); + + return EXIT_SUCCESS; +} -- cgit v1.2.3