diff options
author | David Oberhollenzer <david.oberhollenzer@sigma-star.at> | 2023-04-29 16:08:38 +0200 |
---|---|---|
committer | David Oberhollenzer <david.oberhollenzer@sigma-star.at> | 2023-04-30 14:41:42 +0200 |
commit | 7a39921d7fff089c87ac183d3c0d6e42e5cbaa04 (patch) | |
tree | 723bce4ebe163fc939919bd877c2919f48d84534 /lib/util | |
parent | 4390eddccfb3918291e7b8e4d708411f9b828c04 (diff) |
Move the pattern matching from gensquashfs to dir_tree_iterator_t
A simple unit test is added that mainly checks for the behavior of
recursing into a sub-tree and only matching the children at the end,
but not reporting the parents that don't match. The behavior is
inteded to immitate the `find` command.
Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
Diffstat (limited to 'lib/util')
-rw-r--r-- | lib/util/Makemodule.am | 7 | ||||
-rw-r--r-- | lib/util/src/dir_tree_iterator.c | 17 | ||||
-rw-r--r-- | lib/util/test/dir_tree_iterator3.c | 105 |
3 files changed, 128 insertions, 1 deletions
diff --git a/lib/util/Makemodule.am b/lib/util/Makemodule.am index 37eac93..15c5b88 100644 --- a/lib/util/Makemodule.am +++ b/lib/util/Makemodule.am @@ -96,11 +96,16 @@ test_dir_tree_iterator2_LDADD = libutil.a libcompat.a test_dir_tree_iterator2_CPPFLAGS = $(AM_CPPFLAGS) test_dir_tree_iterator2_CPPFLAGS += -DTESTPATH=$(top_srcdir)/lib/util/test/testdir +test_dir_tree_iterator3_SOURCES = lib/util/test/dir_tree_iterator3.c +test_dir_tree_iterator3_LDADD = libutil.a libcompat.a +test_dir_tree_iterator3_CPPFLAGS = $(AM_CPPFLAGS) +test_dir_tree_iterator3_CPPFLAGS += -DTESTPATH=$(top_srcdir)/lib/util/test/testdir + LIBUTIL_TESTS = \ test_str_table test_rbtree test_xxhash test_threadpool test_ismemzero \ test_canonicalize_name test_filename_sane test_filename_sane_w32 \ test_sdate_epoch test_hex_decode test_base64_decode test_dir_iterator \ - test_dir_tree_iterator test_dir_tree_iterator2 + test_dir_tree_iterator test_dir_tree_iterator2 test_dir_tree_iterator3 check_PROGRAMS += $(LIBUTIL_TESTS) TESTS += $(LIBUTIL_TESTS) diff --git a/lib/util/src/dir_tree_iterator.c b/lib/util/src/dir_tree_iterator.c index 55fbabb..a63209b 100644 --- a/lib/util/src/dir_tree_iterator.c +++ b/lib/util/src/dir_tree_iterator.c @@ -216,6 +216,23 @@ retry: } } + if (it->cfg.name_pattern != NULL) { + if (it->cfg.flags & DIR_SCAN_MATCH_FULL_PATH) { + ret = fnmatch(it->cfg.name_pattern, + ent->name, FNM_PATHNAME); + } else { + const char *name = strrchr(ent->name, '/'); + name = (name == NULL) ? ent->name : (name + 1); + + ret = fnmatch(it->cfg.name_pattern, name, 0); + } + + if (ret != 0) { + free(ent); + goto retry; + } + } + *out = ent; return it->state; fail: diff --git a/lib/util/test/dir_tree_iterator3.c b/lib/util/test/dir_tree_iterator3.c new file mode 100644 index 0000000..7b14190 --- /dev/null +++ b/lib/util/test/dir_tree_iterator3.c @@ -0,0 +1,105 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later */ +/* + * dir_tree_iterator2.c + * + * Copyright (C) 2023 David Oberhollenzer <goliath@infraroot.at> + */ +#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; + + /********** match name **********/ + memset(&cfg, 0, sizeof(cfg)); + cfg.name_pattern = "file_x*"; + + 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, "dirb/dirx/file_x0"); + TEST_ASSERT(S_ISREG(ent[0]->mode)); + TEST_STR_EQUAL(ent[1]->name, "dirb/dirx/file_x1"); + TEST_ASSERT(S_ISREG(ent[1]->mode)); + TEST_STR_EQUAL(ent[2]->name, "dirb/dirx/file_x2"); + TEST_ASSERT(S_ISREG(ent[2]->mode)); + + for (i = 0; i < 3; ++i) + free(ent[i]); + + /********** match path **********/ + memset(&cfg, 0, sizeof(cfg)); + cfg.name_pattern = "dir*/file_*0"; + cfg.flags |= DIR_SCAN_MATCH_FULL_PATH; + + 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/file_a0"); + TEST_ASSERT(S_ISREG(ent[0]->mode)); + TEST_STR_EQUAL(ent[1]->name, "dirb/file_b0"); + TEST_ASSERT(S_ISREG(ent[1]->mode)); + TEST_STR_EQUAL(ent[2]->name, "dirc/file_c0"); + TEST_ASSERT(S_ISREG(ent[2]->mode)); + + for (i = 0; i < 3; ++i) + free(ent[i]); + + return EXIT_SUCCESS; +} |