From 1b832b6dfb8d9da2b94f07ccc95c03614b378786 Mon Sep 17 00:00:00 2001 From: David Oberhollenzer Date: Fri, 21 Apr 2023 23:02:32 +0200 Subject: libutil: Add a stacked, recursive directory tree iterator The concept is simple: Use the existing, platform dependent iterator to walk a directory. If a directory entry is encountered, recurse into it using the open_subdir handler, reconstruct the full path for any entries discovered using the directory stack. An additional function is added to skip a sub-hierarchy. Signed-off-by: David Oberhollenzer --- lib/util/test/dir_iterator.c | 36 ++++++++++++++++++++++-------------- 1 file changed, 22 insertions(+), 14 deletions(-) (limited to 'lib/util/test/dir_iterator.c') diff --git a/lib/util/test/dir_iterator.c b/lib/util/test/dir_iterator.c index d209afd..5ebdaf7 100644 --- a/lib/util/test/dir_iterator.c +++ b/lib/util/test/dir_iterator.c @@ -144,25 +144,31 @@ int main(int argc, char **argv) TEST_EQUAL_I(ret, 0); ret = dir->next(dir, &ent[5]); - TEST_NULL(ent[5]); + TEST_NOT_NULL(ent[5]); + TEST_EQUAL_I(ret, 0); + + ret = dir->next(dir, &dent); + TEST_NULL(dent); TEST_ASSERT(ret > 0); dir = sqfs_drop(dir); - qsort(ent, 5, sizeof(ent[0]), compare_entries); + qsort(ent, 6, sizeof(ent[0]), compare_entries); TEST_STR_EQUAL(ent[0]->name, "."); TEST_ASSERT(S_ISDIR(ent[0]->mode)); TEST_STR_EQUAL(ent[1]->name, ".."); TEST_ASSERT(S_ISDIR(ent[1]->mode)); - TEST_STR_EQUAL(ent[2]->name, "file_b0"); - TEST_ASSERT(S_ISREG(ent[2]->mode)); - TEST_STR_EQUAL(ent[3]->name, "file_b1"); + TEST_STR_EQUAL(ent[2]->name, "dirx"); + TEST_ASSERT(S_ISDIR(ent[2]->mode)); + TEST_STR_EQUAL(ent[3]->name, "file_b0"); TEST_ASSERT(S_ISREG(ent[3]->mode)); - TEST_STR_EQUAL(ent[4]->name, "file_b2"); + TEST_STR_EQUAL(ent[4]->name, "file_b1"); TEST_ASSERT(S_ISREG(ent[4]->mode)); + TEST_STR_EQUAL(ent[5]->name, "file_b2"); + TEST_ASSERT(S_ISREG(ent[5]->mode)); - for (i = 0; i < 5; ++i) + for (i = 0; i < 6; ++i) free(ent[i]); /* scan first sub hierarchy */ @@ -288,7 +294,7 @@ int main(int argc, char **argv) free(ent[i]); /* sub iterator b */ - for (i = 0; i < 5; ++i) { + for (i = 0; i < 6; ++i) { ret = subb->next(subb, &ent[i]); TEST_NOT_NULL(ent[0]); TEST_EQUAL_I(ret, 0); @@ -305,20 +311,22 @@ int main(int argc, char **argv) TEST_ASSERT(ret > 0); subb = sqfs_drop(subb); - qsort(ent, 5, sizeof(ent[0]), compare_entries); + qsort(ent, 6, sizeof(ent[0]), compare_entries); TEST_STR_EQUAL(ent[0]->name, "."); TEST_ASSERT(S_ISDIR(ent[0]->mode)); TEST_STR_EQUAL(ent[1]->name, ".."); TEST_ASSERT(S_ISDIR(ent[1]->mode)); - TEST_STR_EQUAL(ent[2]->name, "file_b0"); - TEST_ASSERT(S_ISREG(ent[2]->mode)); - TEST_STR_EQUAL(ent[3]->name, "file_b1"); + TEST_STR_EQUAL(ent[2]->name, "dirx"); + TEST_ASSERT(S_ISDIR(ent[2]->mode)); + TEST_STR_EQUAL(ent[3]->name, "file_b0"); TEST_ASSERT(S_ISREG(ent[3]->mode)); - TEST_STR_EQUAL(ent[4]->name, "file_b2"); + TEST_STR_EQUAL(ent[4]->name, "file_b1"); TEST_ASSERT(S_ISREG(ent[4]->mode)); + TEST_STR_EQUAL(ent[5]->name, "file_b2"); + TEST_ASSERT(S_ISREG(ent[5]->mode)); - for (i = 0; i < 5; ++i) + for (i = 0; i < 6; ++i) free(ent[i]); /* sub iterator c */ -- cgit v1.2.3