diff options
author | David Oberhollenzer <david.oberhollenzer@sigma-star.at> | 2023-04-29 16:08:38 +0200 |
---|---|---|
committer | David Oberhollenzer <david.oberhollenzer@sigma-star.at> | 2023-04-30 14:41:42 +0200 |
commit | 7a39921d7fff089c87ac183d3c0d6e42e5cbaa04 (patch) | |
tree | 723bce4ebe163fc939919bd877c2919f48d84534 /bin/gensquashfs/src | |
parent | 4390eddccfb3918291e7b8e4d708411f9b828c04 (diff) |
Move the pattern matching from gensquashfs to dir_tree_iterator_t
A simple unit test is added that mainly checks for the behavior of
recursing into a sub-tree and only matching the children at the end,
but not reporting the parents that don't match. The behavior is
inteded to immitate the `find` command.
Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
Diffstat (limited to 'bin/gensquashfs/src')
-rw-r--r-- | bin/gensquashfs/src/fstree_from_dir.c | 16 | ||||
-rw-r--r-- | bin/gensquashfs/src/fstree_from_file.c | 59 | ||||
-rw-r--r-- | bin/gensquashfs/src/mkfs.c | 2 | ||||
-rw-r--r-- | bin/gensquashfs/src/mkfs.h | 12 |
4 files changed, 16 insertions, 73 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); |