aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2023-04-29 01:21:39 +0200
committerDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2023-04-29 01:21:39 +0200
commit9d1d9ec6dfd525a392ac158609da6153f6525046 (patch)
tree031985a33cdfa19e92cdc491b6e1095f001e94ec
parent2285036e10863aba48dc6eed3c1a791118d11956 (diff)
libutil: Add an option to the dir_tree_iterator_t to add a path prefix
Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
-rw-r--r--include/util/dir_tree_iterator.h1
-rw-r--r--lib/util/src/dir_tree_iterator.c16
-rw-r--r--lib/util/test/dir_tree_iterator2.c62
3 files changed, 79 insertions, 0 deletions
diff --git a/include/util/dir_tree_iterator.h b/include/util/dir_tree_iterator.h
index 3c624c9..c865738 100644
--- a/include/util/dir_tree_iterator.h
+++ b/include/util/dir_tree_iterator.h
@@ -26,6 +26,7 @@ enum {
typedef struct {
sqfs_u32 flags;
sqfs_s64 def_mtime;
+ const char *prefix;
} dir_tree_cfg_t;
dir_iterator_t *dir_tree_iterator_create(const char *path,
diff --git a/lib/util/src/dir_tree_iterator.c b/lib/util/src/dir_tree_iterator.c
index ccc63ac..37ddbea 100644
--- a/lib/util/src/dir_tree_iterator.c
+++ b/lib/util/src/dir_tree_iterator.c
@@ -165,6 +165,22 @@ retry:
}
}
+ if (it->cfg.prefix != NULL) {
+ size_t slen = strlen(ent->name), len = strlen(it->cfg.prefix);
+ void *new = realloc(ent, sizeof(*ent) + len + 1 + slen + 1);
+
+ if (new == NULL) {
+ ret = SQFS_ERROR_ALLOC;
+ goto fail;
+ }
+
+ ent = new;
+ memmove(ent->name + len + 1, ent->name, slen + 1);
+ memcpy(ent->name, it->cfg.prefix, len);
+ ent->name[len] = '/';
+ plen += len + 1;
+ }
+
if (!(it->cfg.flags & DIR_SCAN_KEEP_TIME))
ent->mtime = it->cfg.def_mtime;
diff --git a/lib/util/test/dir_tree_iterator2.c b/lib/util/test/dir_tree_iterator2.c
index 4b2b6f1..aac7765 100644
--- a/lib/util/test/dir_tree_iterator2.c
+++ b/lib/util/test/dir_tree_iterator2.c
@@ -156,5 +156,67 @@ int main(int argc, char **argv)
for (i = 0; i < 3; ++i)
free(ent[i]);
+ /********** with prefix inserted **********/
+ memset(&cfg, 0, sizeof(cfg));
+ cfg.prefix = "foobar";
+
+ dir = dir_tree_iterator_create(TEST_PATH, &cfg);
+ TEST_NOT_NULL(dir);
+
+ for (i = 0; i < 16; ++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[16]);
+ TEST_NULL(ent[16]);
+ TEST_ASSERT(ret > 0);
+
+ dir = sqfs_drop(dir);
+
+ qsort(ent, 16, sizeof(ent[0]), compare_entries);
+
+ printf("After sort:\n");
+ for (i = 0; i < 16; ++i)
+ printf("%s\n", ent[i]->name);
+
+ TEST_STR_EQUAL(ent[0]->name, "foobar/dira");
+ TEST_ASSERT(S_ISDIR(ent[0]->mode));
+ TEST_STR_EQUAL(ent[1]->name, "foobar/dira/file_a0");
+ TEST_ASSERT(S_ISREG(ent[1]->mode));
+ TEST_STR_EQUAL(ent[2]->name, "foobar/dira/file_a1");
+ TEST_ASSERT(S_ISREG(ent[2]->mode));
+ TEST_STR_EQUAL(ent[3]->name, "foobar/dira/file_a2");
+ TEST_ASSERT(S_ISREG(ent[3]->mode));
+ TEST_STR_EQUAL(ent[4]->name, "foobar/dirb");
+ TEST_ASSERT(S_ISDIR(ent[4]->mode));
+ TEST_STR_EQUAL(ent[5]->name, "foobar/dirb/dirx");
+ TEST_ASSERT(S_ISDIR(ent[5]->mode));
+ TEST_STR_EQUAL(ent[6]->name, "foobar/dirb/dirx/file_x0");
+ TEST_ASSERT(S_ISREG(ent[6]->mode));
+ TEST_STR_EQUAL(ent[7]->name, "foobar/dirb/dirx/file_x1");
+ TEST_ASSERT(S_ISREG(ent[7]->mode));
+ TEST_STR_EQUAL(ent[8]->name, "foobar/dirb/dirx/file_x2");
+ TEST_ASSERT(S_ISREG(ent[8]->mode));
+ TEST_STR_EQUAL(ent[9]->name, "foobar/dirb/file_b0");
+ TEST_ASSERT(S_ISREG(ent[9]->mode));
+ TEST_STR_EQUAL(ent[10]->name, "foobar/dirb/file_b1");
+ TEST_ASSERT(S_ISREG(ent[10]->mode));
+ TEST_STR_EQUAL(ent[11]->name, "foobar/dirb/file_b2");
+ TEST_ASSERT(S_ISREG(ent[11]->mode));
+ TEST_STR_EQUAL(ent[12]->name, "foobar/dirc");
+ TEST_ASSERT(S_ISDIR(ent[12]->mode));
+ TEST_STR_EQUAL(ent[13]->name, "foobar/dirc/file_c0");
+ TEST_ASSERT(S_ISREG(ent[13]->mode));
+ TEST_STR_EQUAL(ent[14]->name, "foobar/dirc/file_c1");
+ TEST_ASSERT(S_ISREG(ent[14]->mode));
+ TEST_STR_EQUAL(ent[15]->name, "foobar/dirc/file_c2");
+ TEST_ASSERT(S_ISREG(ent[15]->mode));
+
+ for (i = 0; i < 16; ++i)
+ free(ent[i]);
+
return EXIT_SUCCESS;
}