aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--bin/gensquashfs/src/fstree_from_dir.c16
-rw-r--r--bin/gensquashfs/src/fstree_from_file.c59
-rw-r--r--bin/gensquashfs/src/mkfs.c2
-rw-r--r--bin/gensquashfs/src/mkfs.h12
-rw-r--r--include/util/dir_tree_iterator.h2
-rw-r--r--lib/util/Makemodule.am7
-rw-r--r--lib/util/src/dir_tree_iterator.c17
-rw-r--r--lib/util/test/dir_tree_iterator3.c105
8 files changed, 146 insertions, 74 deletions
diff --git a/bin/gensquashfs/src/fstree_from_dir.c b/bin/gensquashfs/src/fstree_from_dir.c
index c960430..6709368 100644
--- a/bin/gensquashfs/src/fstree_from_dir.c
+++ b/bin/gensquashfs/src/fstree_from_dir.c
@@ -21,8 +21,7 @@ static sqfs_u32 clamp_timestamp(sqfs_s64 ts)
return ts;
}
-int fstree_from_dir(fstree_t *fs, dir_iterator_t *dir,
- scan_node_callback cb, void *user)
+int fstree_from_dir(fstree_t *fs, dir_iterator_t *dir)
{
for (;;) {
dir_entry_t *ent = NULL;
@@ -40,18 +39,7 @@ int fstree_from_dir(fstree_t *fs, dir_iterator_t *dir,
n = fstree_get_node_by_path(fs, fs->root, ent->name,
false, true);
- if (n == NULL)
- ret = 1;
-
- if (ret == 0 && cb != NULL)
- ret = cb(user, ent);
-
- if (ret < 0) {
- free(ent);
- return -1;
- }
-
- if (ret > 0) {
+ if (n == NULL) {
if (S_ISDIR(ent->mode))
dir_tree_iterator_skip(dir);
free(ent);
diff --git a/bin/gensquashfs/src/fstree_from_file.c b/bin/gensquashfs/src/fstree_from_file.c
index 4cdfe66..318c129 100644
--- a/bin/gensquashfs/src/fstree_from_file.c
+++ b/bin/gensquashfs/src/fstree_from_file.c
@@ -17,15 +17,6 @@
#include <errno.h>
#include <ctype.h>
-struct glob_context {
- unsigned int glob_flags;
- char *name_pattern;
-};
-
-enum {
- GLOB_FLAG_PATH = 0x00010000,
-};
-
static const struct {
const char *name;
unsigned int clear_flag;
@@ -118,29 +109,6 @@ static int add_hard_link(fstree_t *fs, const char *filename, size_t line_num,
return 0;
}
-static int glob_node_callback(void *user, dir_entry_t *ent)
-{
- struct glob_context *ctx = user;
- int ret;
-
- if (ctx->name_pattern != NULL) {
- if (ctx->glob_flags & GLOB_FLAG_PATH) {
- ret = fnmatch(ctx->name_pattern,
- ent->name, FNM_PATHNAME);
- } else {
- const char *name = strrchr(ent->name, '/');
- name = (name == NULL) ? ent->name : (name + 1);
-
- ret = fnmatch(ctx->name_pattern, name, 0);
- }
-
- if (ret != 0)
- return 1;
- }
-
- return 0;
-}
-
static size_t name_string_length(const char *str)
{
size_t len = 0;
@@ -182,19 +150,15 @@ static int glob_files(fstree_t *fs, const char *filename, size_t line_num,
const char *basepath, unsigned int glob_flags,
const char *extra)
{
+ char *name_pattern = NULL, *prefix = NULL;
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;
- char *prefix;
int ret;
- memset(&ctx, 0, sizeof(ctx));
- ctx.glob_flags = glob_flags;
-
/* fetch the actual target node */
root = fstree_get_node_by_path(fs, fs->root, path, true, false);
if (root == NULL) {
@@ -258,14 +222,14 @@ static int glob_files(fstree_t *fs, const char *filename, size_t line_num,
len = name_string_length(extra);
- free(ctx.name_pattern);
- ctx.name_pattern = strndup(extra, len);
+ free(name_pattern);
+ name_pattern = strndup(extra, len);
extra += len;
while (isspace(*extra))
++extra;
- quote_remove(ctx.name_pattern);
+ quote_remove(name_pattern);
continue;
}
@@ -275,15 +239,15 @@ static int glob_files(fstree_t *fs, const char *filename, size_t line_num,
len = name_string_length(extra);
- free(ctx.name_pattern);
- ctx.name_pattern = strndup(extra, len);
+ free(name_pattern);
+ name_pattern = strndup(extra, len);
extra += len;
while (isspace(*extra))
++extra;
- quote_remove(ctx.name_pattern);
- ctx.glob_flags |= GLOB_FLAG_PATH;
+ quote_remove(name_pattern);
+ glob_flags |= DIR_SCAN_MATCH_FULL_PATH;
continue;
}
@@ -297,7 +261,7 @@ static int glob_files(fstree_t *fs, const char *filename, size_t line_num,
fprintf(stderr, "%s: " PRI_SZ ": unknown option.\n",
filename, line_num);
- free(ctx.name_pattern);
+ free(name_pattern);
free(prefix);
return -1;
} else {
@@ -316,6 +280,7 @@ static int glob_files(fstree_t *fs, const char *filename, size_t line_num,
cfg.def_gid = basic->st_gid;
cfg.def_mode = basic->st_mode;
cfg.prefix = prefix;
+ cfg.name_pattern = name_pattern;
if (basepath == NULL) {
dir = dir_tree_iterator_create(extra == NULL ? "." : extra,
@@ -341,13 +306,13 @@ static int glob_files(fstree_t *fs, const char *filename, size_t line_num,
}
if (dir != NULL) {
- ret = fstree_from_dir(fs, dir, glob_node_callback, &ctx);
+ ret = fstree_from_dir(fs, dir);
sqfs_drop(dir);
} else {
ret = -1;
}
- free(ctx.name_pattern);
+ free(name_pattern);
free(prefix);
return ret;
}
diff --git a/bin/gensquashfs/src/mkfs.c b/bin/gensquashfs/src/mkfs.c
index 8ad394b..b64a217 100644
--- a/bin/gensquashfs/src/mkfs.c
+++ b/bin/gensquashfs/src/mkfs.c
@@ -179,7 +179,7 @@ int main(int argc, char **argv)
if (dir == NULL)
goto out;
- ret = fstree_from_dir(&sqfs.fs, dir, NULL, NULL);
+ ret = fstree_from_dir(&sqfs.fs, dir);
sqfs_drop(dir);
if (ret != 0)
goto out;
diff --git a/bin/gensquashfs/src/mkfs.h b/bin/gensquashfs/src/mkfs.h
index 3157694..81ad038 100644
--- a/bin/gensquashfs/src/mkfs.h
+++ b/bin/gensquashfs/src/mkfs.h
@@ -42,15 +42,6 @@
#include <errno.h>
#include <ctype.h>
-/*
- Optionally used by fstree_from_dir to execute custom actions for
- each discovered node.
-
- If it returns a value > 0, the new node is discarded, if < 0, scanning is
- aborted and returns a failure status.
- */
-typedef int (*scan_node_callback)(void *user, dir_entry_t *ent);
-
typedef struct {
sqfs_writer_cfg_t cfg;
unsigned int dirscan_flags;
@@ -129,8 +120,7 @@ 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, dir_iterator_t *dir,
- scan_node_callback cb, void *user);
+int fstree_from_dir(fstree_t *fs, dir_iterator_t *dir);
int fstree_sort_files(fstree_t *fs, istream_t *sortfile);
diff --git a/include/util/dir_tree_iterator.h b/include/util/dir_tree_iterator.h
index b5ebe90..204f609 100644
--- a/include/util/dir_tree_iterator.h
+++ b/include/util/dir_tree_iterator.h
@@ -25,6 +25,7 @@ enum {
DIR_SCAN_ONE_FILESYSTEM = 0x1000,
DIR_SCAN_NO_RECURSION = 0x2000,
+ DIR_SCAN_MATCH_FULL_PATH = 0x4000,
};
typedef struct {
@@ -34,6 +35,7 @@ typedef struct {
sqfs_u32 def_mode;
sqfs_s64 def_mtime;
const char *prefix;
+ const char *name_pattern;
} dir_tree_cfg_t;
dir_iterator_t *dir_tree_iterator_create(const char *path,
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;
+}