diff options
author | David Oberhollenzer <david.oberhollenzer@sigma-star.at> | 2023-09-21 15:53:14 +0200 |
---|---|---|
committer | David Oberhollenzer <david.oberhollenzer@sigma-star.at> | 2023-10-24 15:57:18 +0200 |
commit | 7f89eb3cfff465cf32d03a2ae6919252eae67e9b (patch) | |
tree | ed776d71106000dfa34ad4de3402fff6fea6c56a /bin/sqfs2tar/src | |
parent | c4ab32879df8b5e83b0ebd091ce2c750f53f5633 (diff) |
libutil: add a string list helper to replace some of the adhoc ones
Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
Diffstat (limited to 'bin/sqfs2tar/src')
-rw-r--r-- | bin/sqfs2tar/src/iterator.c | 20 | ||||
-rw-r--r-- | bin/sqfs2tar/src/options.c | 41 | ||||
-rw-r--r-- | bin/sqfs2tar/src/sqfs2tar.c | 4 | ||||
-rw-r--r-- | bin/sqfs2tar/src/sqfs2tar.h | 4 |
4 files changed, 26 insertions, 43 deletions
diff --git a/bin/sqfs2tar/src/iterator.c b/bin/sqfs2tar/src/iterator.c index 42f01e2..7879d61 100644 --- a/bin/sqfs2tar/src/iterator.c +++ b/bin/sqfs2tar/src/iterator.c @@ -56,21 +56,21 @@ static bool keep_entry(const sqfs_dir_entry_t *ent) { size_t nlen; - if (num_subdirs == 0) + if (subdirs.count == 0) return true; nlen = strlen(ent->name); - for (size_t i = 0; i < num_subdirs; ++i) { - size_t plen = strlen(subdirs[i]); + for (size_t i = 0; i < subdirs.count; ++i) { + size_t plen = strlen(subdirs.strings[i]); if (nlen <= plen) { - if ((nlen == plen || subdirs[i][nlen] == '/') && - strncmp(subdirs[i], ent->name, nlen) == 0) { + if ((nlen == plen || subdirs.strings[i][nlen] == '/') && + strncmp(subdirs.strings[i], ent->name, nlen) == 0) { return true; } } else if (ent->name[plen] == '/' && - strncmp(subdirs[i], ent->name, plen) == 0) { + strncmp(subdirs.strings[i], ent->name, plen) == 0) { return true; } } @@ -124,8 +124,8 @@ static int next(sqfs_dir_iterator_t *base, sqfs_dir_entry_t **out) if (keep_entry(ent)) { /* XXX: skip the entry, but we MUST recurse here! */ - if (num_subdirs == 1 && !keep_as_dir && - strlen(ent->name) <= strlen(subdirs[0])) { + if (subdirs.count == 1 && !keep_as_dir && + strlen(ent->name) <= strlen(subdirs.strings[0])) { sqfs_free(ent); continue; } @@ -138,8 +138,8 @@ static int next(sqfs_dir_iterator_t *base, sqfs_dir_entry_t **out) sqfs_free(ent); } - if (num_subdirs == 1 && !keep_as_dir) { - size_t plen = strlen(subdirs[0]) + 1; + if (subdirs.count == 1 && !keep_as_dir) { + size_t plen = strlen(subdirs.strings[0]) + 1; memmove(ent->name, ent->name + plen, strlen(ent->name + plen) + 1); diff --git a/bin/sqfs2tar/src/options.c b/bin/sqfs2tar/src/options.c index ba1588d..82c1d0a 100644 --- a/bin/sqfs2tar/src/options.c +++ b/bin/sqfs2tar/src/options.c @@ -70,19 +70,15 @@ bool no_xattr = false; bool no_links = false; char *root_becomes = NULL; -char **subdirs = NULL; -size_t num_subdirs = 0; -static size_t max_subdirs = 0; +strlist_t subdirs = { 0, 0, 0 }; int compressor = 0; const char *filename = NULL; void process_args(int argc, char **argv) { - size_t idx, new_count; const char *name; int i, ret; - char **new; for (;;) { i = getopt_long(argc, argv, short_opts, long_opts, NULL); @@ -99,27 +95,10 @@ void process_args(int argc, char **argv) } break; case 'd': - if (num_subdirs == max_subdirs) { - new_count = max_subdirs ? max_subdirs * 2 : 16; - new = realloc(subdirs, - new_count * sizeof(subdirs[0])); - if (new == NULL) - goto fail_errno; - - max_subdirs = new_count; - subdirs = new; - } - - subdirs[num_subdirs] = strdup(optarg); - if (subdirs[num_subdirs] == NULL) - goto fail_errno; - - if (canonicalize_name(subdirs[num_subdirs])) { - perror(optarg); + if (strlist_append(&subdirs, optarg)) { + fputs("out-of-memory\n", stderr); goto fail; } - - ++num_subdirs; break; case 'r': free(root_becomes); @@ -175,6 +154,14 @@ void process_args(int argc, char **argv) } } + for (size_t idx = 0; idx < subdirs.count; ++idx) { + if (canonicalize_name(subdirs.strings[idx])) { + fprintf(stderr, "Invalid name `%s`\n", + subdirs.strings[idx]); + goto fail; + } + } + if (optind >= argc) { fputs("Missing argument: squashfs image\n", stderr); goto fail_arg; @@ -187,7 +174,7 @@ void process_args(int argc, char **argv) goto fail_arg; } - if (num_subdirs > 1) + if (subdirs.count > 1) keep_as_dir = true; return; @@ -204,9 +191,7 @@ out_success: ret = EXIT_SUCCESS; goto out_exit; out_exit: - for (idx = 0; idx < num_subdirs; ++idx) - free(subdirs[idx]); + strlist_cleanup(&subdirs); free(root_becomes); - free(subdirs); exit(ret); } diff --git a/bin/sqfs2tar/src/sqfs2tar.c b/bin/sqfs2tar/src/sqfs2tar.c index 0f83abf..7404221 100644 --- a/bin/sqfs2tar/src/sqfs2tar.c +++ b/bin/sqfs2tar/src/sqfs2tar.c @@ -171,9 +171,7 @@ int main(int argc, char **argv) out: sqfs_drop(it); sqfs_drop(out_file); - for (i = 0; i < num_subdirs; ++i) - free(subdirs[i]); - free(subdirs); + strlist_cleanup(&subdirs); free(root_becomes); return status; } diff --git a/bin/sqfs2tar/src/sqfs2tar.h b/bin/sqfs2tar/src/sqfs2tar.h index a1a5927..4c9a8f2 100644 --- a/bin/sqfs2tar/src/sqfs2tar.h +++ b/bin/sqfs2tar/src/sqfs2tar.h @@ -11,6 +11,7 @@ #include "common.h" #include "util/util.h" +#include "util/strlist.h" #include "tar/tar.h" #include "xfrm/compress.h" #include "io/xfrm.h" @@ -30,8 +31,7 @@ extern bool no_xattr; extern bool no_links; extern char *root_becomes; -extern char **subdirs; -extern size_t num_subdirs; +extern strlist_t subdirs; extern int compressor; extern const char *filename; |