aboutsummaryrefslogtreecommitdiff
path: root/bin/gensquashfs
diff options
context:
space:
mode:
authorDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2023-04-28 19:36:56 +0200
committerDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2023-04-29 00:26:32 +0200
commit2285036e10863aba48dc6eed3c1a791118d11956 (patch)
treed94808261eb23df4c25db3c6de6dd45df2e692c7 /bin/gensquashfs
parent46b01473eee7301cb7b49533af16abe0ee15c286 (diff)
gensquashfs: Dismantle the scan_dir wrapper
Create the directory iterator externally and pass it to fstree_from_dir. The unit test is also removed, because the heavy lifting is now done outside the function. Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
Diffstat (limited to 'bin/gensquashfs')
-rw-r--r--bin/gensquashfs/Makemodule.am11
-rw-r--r--bin/gensquashfs/src/fstree_from_dir.c22
-rw-r--r--bin/gensquashfs/src/fstree_from_file.c32
-rw-r--r--bin/gensquashfs/src/mkfs.c18
-rw-r--r--bin/gensquashfs/src/mkfs.h5
-rw-r--r--bin/gensquashfs/test/fstree_from_dir.c181
-rw-r--r--bin/gensquashfs/test/testdir/dira/file_a00
-rw-r--r--bin/gensquashfs/test/testdir/dira/file_a10
-rw-r--r--bin/gensquashfs/test/testdir/dira/file_a20
-rw-r--r--bin/gensquashfs/test/testdir/dirb/file_b00
-rw-r--r--bin/gensquashfs/test/testdir/dirb/file_b10
-rw-r--r--bin/gensquashfs/test/testdir/dirb/file_b20
-rw-r--r--bin/gensquashfs/test/testdir/dirc/file_c00
-rw-r--r--bin/gensquashfs/test/testdir/dirc/file_c10
-rw-r--r--bin/gensquashfs/test/testdir/dirc/file_c20
15 files changed, 38 insertions, 231 deletions
diff --git a/bin/gensquashfs/Makemodule.am b/bin/gensquashfs/Makemodule.am
index ff426ae..fd39aaf 100644
--- a/bin/gensquashfs/Makemodule.am
+++ b/bin/gensquashfs/Makemodule.am
@@ -46,14 +46,6 @@ test_fstree_glob1_CPPFLAGS = $(AM_CPPFLAGS) -I$(top_srcdir)/bin/gensquashfs/src
test_fstree_glob1_CPPFLAGS += -DTESTPATH=$(GENDATADIR)
test_fstree_glob1_LDADD = libcommon.a libfstree.a libio.a libutil.a libcompat.a
-test_fstree_from_dir_SOURCES = bin/gensquashfs/test/fstree_from_dir.c \
- bin/gensquashfs/src/fstree_from_dir.c \
- bin/gensquashfs/src/mkfs.h
-test_fstree_from_dir_CPPFLAGS = $(AM_CPPFLAGS)
-test_fstree_from_dir_CPPFLAGS += -I$(top_srcdir)/bin/gensquashfs/src
-test_fstree_from_dir_CPPFLAGS += -DTESTPATH=$(GENDATADIR)/testdir
-test_fstree_from_dir_LDADD = libcommon.a libfstree.a libutil.a libcompat.a
-
test_sort_file_SOURCES = bin/gensquashfs/test/sort_file.c \
bin/gensquashfs/src/fstree_from_file.c \
bin/gensquashfs/src/fstree_from_dir.c \
@@ -70,7 +62,7 @@ fstree_fuzz_CPPFLAGS = $(AM_CPPFLAGS) -I$(top_srcdir)/bin/gensquashfs/src
fstree_fuzz_LDADD = libcommon.a libfstree.a libio.a libutil.a libcompat.a
GENSQUASHFS_TESTS = \
- test_filemap_xattr test_fstree_from_file test_fstree_from_dir \
+ test_filemap_xattr test_fstree_from_file \
test_fstree_glob1 test_sort_file
noinst_PROGRAMS += fstree_fuzz
@@ -81,4 +73,3 @@ TESTS += $(GENSQUASHFS_TESTS)
EXTRA_DIST += $(GENDATADIR)/xattr1.txt $(GENDATADIR)/fstree1.txt
EXTRA_DIST += $(GENDATADIR)/fstree_glob1.txt $(GENDATADIR)/fstree_glob2.txt
EXTRA_DIST += $(GENDATADIR)/fstree_glob3.txt
-EXTRA_DIST += $(GENDATADIR)/testdir
diff --git a/bin/gensquashfs/src/fstree_from_dir.c b/bin/gensquashfs/src/fstree_from_dir.c
index 22493c5..b2d27f1 100644
--- a/bin/gensquashfs/src/fstree_from_dir.c
+++ b/bin/gensquashfs/src/fstree_from_dir.c
@@ -21,7 +21,7 @@ static sqfs_u32 clamp_timestamp(sqfs_s64 ts)
return ts;
}
-static int scan_dir(fstree_t *fs, tree_node_t *root, dir_iterator_t *dir,
+int fstree_from_dir(fstree_t *fs, tree_node_t *root, dir_iterator_t *dir,
scan_node_callback cb, void *user)
{
for (;;) {
@@ -84,23 +84,3 @@ static int scan_dir(fstree_t *fs, tree_node_t *root, dir_iterator_t *dir,
return 0;
}
-
-int fstree_from_dir(fstree_t *fs, tree_node_t *root, const char *path,
- scan_node_callback cb, void *user, unsigned int flags)
-{
- dir_iterator_t *dir;
- dir_tree_cfg_t cfg;
- int ret;
-
- memset(&cfg, 0, sizeof(cfg));
- cfg.flags = flags;
- cfg.def_mtime = fs->defaults.mtime;
-
- dir = dir_tree_iterator_create(path, &cfg);
- if (dir == NULL)
- return -1;
-
- ret = scan_dir(fs, root, dir, cb, user);
- sqfs_drop(dir);
- return ret;
-}
diff --git a/bin/gensquashfs/src/fstree_from_file.c b/bin/gensquashfs/src/fstree_from_file.c
index e75c819..77f9ebd 100644
--- a/bin/gensquashfs/src/fstree_from_file.c
+++ b/bin/gensquashfs/src/fstree_from_file.c
@@ -245,9 +245,11 @@ static int glob_files(fstree_t *fs, const char *filename, size_t line_num,
const char *extra)
{
unsigned int scan_flags = 0, all_flags;
+ dir_iterator_t *dir = NULL;
struct glob_context ctx;
bool first_clear_flag;
size_t i, count, len;
+ dir_tree_cfg_t cfg;
tree_node_t *root;
int ret;
@@ -362,15 +364,13 @@ static int glob_files(fstree_t *fs, const char *filename, size_t line_num,
extra = NULL;
/* do the scan */
+ memset(&cfg, 0, sizeof(cfg));
+ cfg.flags = scan_flags;
+ cfg.def_mtime = fs->defaults.mtime;
+
if (basepath == NULL) {
- if (extra == NULL) {
- ret = fstree_from_dir(fs, root, ".", glob_node_callback,
- &ctx, scan_flags);
- } else {
- ret = fstree_from_dir(fs, root, extra,
- glob_node_callback,
- &ctx, scan_flags);
- }
+ dir = dir_tree_iterator_create(extra == NULL ? "." : extra,
+ &cfg);
} else {
size_t plen = strlen(basepath);
size_t slen = strlen(extra);
@@ -379,18 +379,24 @@ static int glob_files(fstree_t *fs, const char *filename, size_t line_num,
if (temp == NULL) {
fprintf(stderr, "%s: " PRI_SZ ": allocation failure.\n",
filename, line_num);
- ret = -1;
} else {
memcpy(temp, basepath, plen);
temp[plen] = '/';
memcpy(temp + plen + 1, extra, slen);
temp[plen + 1 + slen] = '\0';
- ret = fstree_from_dir(fs, root, temp,
- glob_node_callback, &ctx,
- scan_flags);
- free(temp);
+ dir = dir_tree_iterator_create(temp, &cfg);
}
+
+ free(temp);
+ }
+
+ if (dir != NULL) {
+ ret = fstree_from_dir(fs, root, dir,
+ glob_node_callback, &ctx);
+ sqfs_drop(dir);
+ } else {
+ ret = -1;
}
free(ctx.name_pattern);
diff --git a/bin/gensquashfs/src/mkfs.c b/bin/gensquashfs/src/mkfs.c
index 683077b..f2973b6 100644
--- a/bin/gensquashfs/src/mkfs.c
+++ b/bin/gensquashfs/src/mkfs.c
@@ -166,10 +166,22 @@ int main(int argc, char **argv)
}
if (opt.infile == NULL) {
- if (fstree_from_dir(&sqfs.fs, sqfs.fs.root, opt.packdir,
- NULL, NULL, opt.dirscan_flags)) {
+ dir_iterator_t *dir = NULL;
+ dir_tree_cfg_t cfg;
+ int ret;
+
+ memset(&cfg, 0, sizeof(cfg));
+ cfg.flags = opt.dirscan_flags;
+ cfg.def_mtime = sqfs.fs.defaults.mtime;
+
+ dir = dir_tree_iterator_create(opt.packdir, &cfg);
+ if (dir == NULL)
+ goto out;
+
+ ret = fstree_from_dir(&sqfs.fs, sqfs.fs.root, dir, NULL, NULL);
+ sqfs_drop(dir);
+ if (ret != 0)
goto out;
- }
} else {
if (read_fstree(&sqfs.fs, &opt, sqfs.xwr, sehnd))
goto out;
diff --git a/bin/gensquashfs/src/mkfs.h b/bin/gensquashfs/src/mkfs.h
index 9064a75..6923164 100644
--- a/bin/gensquashfs/src/mkfs.h
+++ b/bin/gensquashfs/src/mkfs.h
@@ -130,9 +130,8 @@ int fstree_from_file_stream(fstree_t *fs, istream_t *file,
Returns 0 on success, prints to stderr on failure.
*/
-int fstree_from_dir(fstree_t *fs, tree_node_t *root,
- const char *path, scan_node_callback cb, void *user,
- unsigned int flags);
+int fstree_from_dir(fstree_t *fs, tree_node_t *root, dir_iterator_t *dir,
+ scan_node_callback cb, void *user);
int fstree_sort_files(fstree_t *fs, istream_t *sortfile);
diff --git a/bin/gensquashfs/test/fstree_from_dir.c b/bin/gensquashfs/test/fstree_from_dir.c
deleted file mode 100644
index 7d6fc06..0000000
--- a/bin/gensquashfs/test/fstree_from_dir.c
+++ /dev/null
@@ -1,181 +0,0 @@
-/* SPDX-License-Identifier: GPL-3.0-or-later */
-/*
- * fstree_from_dir.c
- *
- * Copyright (C) 2019 David Oberhollenzer <goliath@infraroot.at>
- */
-#include "config.h"
-
-#include "util/test.h"
-#include "mkfs.h"
-
-static void check_hierarchy(tree_node_t *root, bool recursive)
-{
- tree_node_t *n, *m;
-
- n = root->data.children;
- TEST_NOT_NULL(n);
- TEST_STR_EQUAL(n->name, "dira");
- TEST_ASSERT(S_ISDIR(n->mode));
- TEST_ASSERT(n->parent == root);
-
- if (recursive) {
- m = n->data.children;
- TEST_NOT_NULL(m);
- TEST_STR_EQUAL(m->name, "file_a0");
- TEST_ASSERT(S_ISREG(m->mode));
- TEST_ASSERT(m->parent == n);
-
- m = m->next;
- TEST_NOT_NULL(m);
- TEST_STR_EQUAL(m->name, "file_a1");
- TEST_ASSERT(S_ISREG(m->mode));
- TEST_ASSERT(m->parent == n);
-
- m = m->next;
- TEST_NOT_NULL(m);
- TEST_STR_EQUAL(m->name, "file_a2");
- TEST_ASSERT(S_ISREG(m->mode));
- TEST_ASSERT(m->parent == n);
-
- m = m->next;
- TEST_NULL(m);
- } else {
- TEST_NULL(n->data.children);
- }
-
- n = n->next;
- TEST_NOT_NULL(n);
- TEST_STR_EQUAL(n->name, "dirb");
- TEST_ASSERT(S_ISDIR(n->mode));
- TEST_ASSERT(n->parent == root);
-
- if (recursive) {
- m = n->data.children;
- TEST_NOT_NULL(m);
- TEST_STR_EQUAL(m->name, "file_b0");
- TEST_ASSERT(S_ISREG(m->mode));
- TEST_ASSERT(m->parent == n);
-
- m = m->next;
- TEST_NOT_NULL(m);
- TEST_STR_EQUAL(m->name, "file_b1");
- TEST_ASSERT(S_ISREG(m->mode));
- TEST_ASSERT(m->parent == n);
-
- m = m->next;
- TEST_NOT_NULL(m);
- TEST_STR_EQUAL(m->name, "file_b2");
- TEST_ASSERT(S_ISREG(m->mode));
- TEST_ASSERT(m->parent == n);
-
- m = m->next;
- TEST_NULL(m);
- } else {
- TEST_NULL(n->data.children);
- }
-
- n = n->next;
- TEST_NOT_NULL(n);
- TEST_STR_EQUAL(n->name, "dirc");
- TEST_ASSERT(S_ISDIR(n->mode));
- TEST_ASSERT(n->parent == root);
-
- if (recursive) {
- m = n->data.children;
- TEST_NOT_NULL(m);
- TEST_STR_EQUAL(m->name, "file_c0");
- TEST_ASSERT(S_ISREG(m->mode));
- TEST_ASSERT(m->parent == n);
-
- m = m->next;
- TEST_NOT_NULL(m);
- TEST_STR_EQUAL(m->name, "file_c1");
- TEST_ASSERT(S_ISREG(m->mode));
- TEST_ASSERT(m->parent == n);
-
- m = m->next;
- TEST_NOT_NULL(m);
- TEST_STR_EQUAL(m->name, "file_c2");
- TEST_ASSERT(S_ISREG(m->mode));
- TEST_ASSERT(m->parent == n);
-
- m = m->next;
- TEST_NULL(m);
- } else {
- TEST_NULL(n->data.children);
- }
-
- n = n->next;
- TEST_NULL(n);
-}
-
-int main(int argc, char **argv)
-{
- fstree_defaults_t fsd;
- struct stat sb;
- tree_node_t *n;
- fstree_t fs;
- (void)argc; (void)argv;
-
- TEST_ASSERT(parse_fstree_defaults(&fsd, NULL) == 0);
-
- /* recursively scan into root */
- TEST_ASSERT(fstree_init(&fs, &fsd) == 0);
- TEST_ASSERT(fstree_from_dir(&fs, fs.root, TEST_PATH,
- NULL, NULL, 0) == 0);
-
- fstree_post_process(&fs);
- check_hierarchy(fs.root, true);
- fstree_cleanup(&fs);
-
- /* non-recursively scan into root */
- TEST_ASSERT(fstree_init(&fs, &fsd) == 0);
- TEST_ASSERT(fstree_from_dir(&fs, fs.root, TEST_PATH, NULL, NULL,
- DIR_SCAN_NO_RECURSION) == 0);
-
- fstree_post_process(&fs);
- check_hierarchy(fs.root, false);
- fstree_cleanup(&fs);
-
- /* recursively scan into a sub-directory of root */
- memset(&sb, 0, sizeof(sb));
- sb.st_mode = S_IFDIR | 0755;
-
- TEST_ASSERT(fstree_init(&fs, &fsd) == 0);
-
- n = fstree_mknode(fs.root, "foodir", 6, NULL, &sb);
- TEST_NOT_NULL(n);
- fs.root->data.children = n;
-
- TEST_ASSERT(fstree_from_dir(&fs, n, TEST_PATH, NULL, NULL, 0) == 0);
-
- TEST_ASSERT(fs.root->data.children == n);
- TEST_NULL(n->next);
-
- fstree_post_process(&fs);
- check_hierarchy(n, true);
- fstree_cleanup(&fs);
-
- /* non-recursively scan into a sub-directory of root */
- memset(&sb, 0, sizeof(sb));
- sb.st_mode = S_IFDIR | 0755;
-
- TEST_ASSERT(fstree_init(&fs, &fsd) == 0);
-
- n = fstree_mknode(fs.root, "foodir", 6, NULL, &sb);
- TEST_NOT_NULL(n);
- fs.root->data.children = n;
-
- TEST_ASSERT(fstree_from_dir(&fs, n, TEST_PATH, NULL, NULL,
- DIR_SCAN_NO_RECURSION) == 0);
-
- TEST_ASSERT(fs.root->data.children == n);
- TEST_NULL(n->next);
-
- fstree_post_process(&fs);
- check_hierarchy(n, false);
- fstree_cleanup(&fs);
-
- return EXIT_SUCCESS;
-}
diff --git a/bin/gensquashfs/test/testdir/dira/file_a0 b/bin/gensquashfs/test/testdir/dira/file_a0
deleted file mode 100644
index e69de29..0000000
--- a/bin/gensquashfs/test/testdir/dira/file_a0
+++ /dev/null
diff --git a/bin/gensquashfs/test/testdir/dira/file_a1 b/bin/gensquashfs/test/testdir/dira/file_a1
deleted file mode 100644
index e69de29..0000000
--- a/bin/gensquashfs/test/testdir/dira/file_a1
+++ /dev/null
diff --git a/bin/gensquashfs/test/testdir/dira/file_a2 b/bin/gensquashfs/test/testdir/dira/file_a2
deleted file mode 100644
index e69de29..0000000
--- a/bin/gensquashfs/test/testdir/dira/file_a2
+++ /dev/null
diff --git a/bin/gensquashfs/test/testdir/dirb/file_b0 b/bin/gensquashfs/test/testdir/dirb/file_b0
deleted file mode 100644
index e69de29..0000000
--- a/bin/gensquashfs/test/testdir/dirb/file_b0
+++ /dev/null
diff --git a/bin/gensquashfs/test/testdir/dirb/file_b1 b/bin/gensquashfs/test/testdir/dirb/file_b1
deleted file mode 100644
index e69de29..0000000
--- a/bin/gensquashfs/test/testdir/dirb/file_b1
+++ /dev/null
diff --git a/bin/gensquashfs/test/testdir/dirb/file_b2 b/bin/gensquashfs/test/testdir/dirb/file_b2
deleted file mode 100644
index e69de29..0000000
--- a/bin/gensquashfs/test/testdir/dirb/file_b2
+++ /dev/null
diff --git a/bin/gensquashfs/test/testdir/dirc/file_c0 b/bin/gensquashfs/test/testdir/dirc/file_c0
deleted file mode 100644
index e69de29..0000000
--- a/bin/gensquashfs/test/testdir/dirc/file_c0
+++ /dev/null
diff --git a/bin/gensquashfs/test/testdir/dirc/file_c1 b/bin/gensquashfs/test/testdir/dirc/file_c1
deleted file mode 100644
index e69de29..0000000
--- a/bin/gensquashfs/test/testdir/dirc/file_c1
+++ /dev/null
diff --git a/bin/gensquashfs/test/testdir/dirc/file_c2 b/bin/gensquashfs/test/testdir/dirc/file_c2
deleted file mode 100644
index e69de29..0000000
--- a/bin/gensquashfs/test/testdir/dirc/file_c2
+++ /dev/null