aboutsummaryrefslogtreecommitdiff
path: root/lib/util/test/dir_iterator.c
diff options
context:
space:
mode:
authorDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2023-04-21 23:02:32 +0200
committerDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2023-04-29 00:26:32 +0200
commit1b832b6dfb8d9da2b94f07ccc95c03614b378786 (patch)
tree953d52de8ccf3573f6cf68740c64d017623578d9 /lib/util/test/dir_iterator.c
parent9caccbfec112c53133aff09119eda623ae0644fe (diff)
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 <david.oberhollenzer@sigma-star.at>
Diffstat (limited to 'lib/util/test/dir_iterator.c')
-rw-r--r--lib/util/test/dir_iterator.c36
1 files changed, 22 insertions, 14 deletions
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 */