diff options
author | David Oberhollenzer <david.oberhollenzer@sigma-star.at> | 2019-05-02 16:30:33 +0200 |
---|---|---|
committer | David Oberhollenzer <david.oberhollenzer@sigma-star.at> | 2019-05-03 21:45:46 +0200 |
commit | 2aa2f13131663afa0d13259365f824b05e9a2f02 (patch) | |
tree | b5b8d7d1f46717d5b49278ec300f769e68641fd6 | |
parent | 03916fcbb453684fa13135e2b46f5a82d13d78a7 (diff) |
unsquashfs: add argument processing, file listing parameter
Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
-rw-r--r-- | include/util.h | 2 | ||||
-rw-r--r-- | lib/Makemodule.am | 1 | ||||
-rw-r--r-- | lib/util/print_version.c | 25 | ||||
-rw-r--r-- | mkfs/options.c | 17 | ||||
-rw-r--r-- | unpack/Makemodule.am | 1 | ||||
-rw-r--r-- | unpack/list_files.c | 90 | ||||
-rw-r--r-- | unpack/unsquashfs.c | 135 | ||||
-rw-r--r-- | unpack/unsquashfs.h | 6 |
8 files changed, 249 insertions, 28 deletions
diff --git a/include/util.h b/include/util.h index 16f3162..b1a98c0 100644 --- a/include/util.h +++ b/include/util.h @@ -10,4 +10,6 @@ ssize_t write_retry(int fd, void *data, size_t size); ssize_t read_retry(int fd, void *buffer, size_t size); +void print_version(void); + #endif /* UTIL_H */ diff --git a/lib/Makemodule.am b/lib/Makemodule.am index a1bf0f2..3b171c6 100644 --- a/lib/Makemodule.am +++ b/lib/Makemodule.am @@ -18,6 +18,7 @@ libsquashfs_a_SOURCES += lib/sqfs/readdir.c libutil_a_SOURCES = lib/util/canonicalize_name.c lib/util/write_retry.c libutil_a_SOURCES += lib/util/read_retry.c include/util.h +libutil_a_SOURCES += lib/util/print_version.c if WITH_ZLIB libcompress_a_SOURCES += lib/comp/zlib.c diff --git a/lib/util/print_version.c b/lib/util/print_version.c new file mode 100644 index 0000000..7d1b91a --- /dev/null +++ b/lib/util/print_version.c @@ -0,0 +1,25 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later */ +#include "config.h" +#include "util.h" + +#include <stdio.h> + +#define LICENSE_SHORT "GPLv3+" +#define LICENSE_LONG "GNU GPL version 3 or later" +#define LICENSE_URL "https://gnu.org/licenses/gpl.html" + +extern char *__progname; + +static const char *version_string = +"%s (%s) %s\n" +"Copyright (c) 2019 David Oberhollenzer\n" +"License " LICENSE_SHORT ": " LICENSE_LONG " <" LICENSE_URL ">.\n" +"This is free software: you are free to change and redistribute it.\n" +"There is NO WARRANTY, to the extent permitted by law.\n" +"\n" +"Written by David Oberhollenzer.\n"; + +void print_version(void) +{ + printf(version_string, __progname, PACKAGE_NAME, PACKAGE_VERSION); +} diff --git a/mkfs/options.c b/mkfs/options.c index d19e5af..a1077b4 100644 --- a/mkfs/options.c +++ b/mkfs/options.c @@ -1,5 +1,6 @@ /* SPDX-License-Identifier: GPL-3.0-or-later */ #include "mksquashfs.h" +#include "util.h" #include <stdlib.h> #include <getopt.h> @@ -37,21 +38,8 @@ static const char *defaults[] = { NULL }; -#define LICENSE_SHORT "GPLv3+" -#define LICENSE_LONG "GNU GPL version 3 or later" -#define LICENSE_URL "https://gnu.org/licenses/gpl.html" - extern char *__progname; -static const char *version_string = -"%s (%s) %s\n" -"Copyright (c) 2019 David Oberhollenzer\n" -"License " LICENSE_SHORT ": " LICENSE_LONG " <" LICENSE_URL ">.\n" -"This is free software: you are free to change and redistribute it.\n" -"There is NO WARRANTY, to the extent permitted by law.\n" -"\n" -"Written by David Oberhollenzer.\n"; - static const char *help_string = "Usage: %s [OPTIONS] <file-list> <squashfs-file>\n" "\n" @@ -286,8 +274,7 @@ void process_command_line(options_t *opt, int argc, char **argv) exit(EXIT_SUCCESS); case 'V': - printf(version_string, __progname, - PACKAGE_NAME, PACKAGE_VERSION); + print_version(); exit(EXIT_SUCCESS); default: goto fail_arg; diff --git a/unpack/Makemodule.am b/unpack/Makemodule.am index 2a08295..a199375 100644 --- a/unpack/Makemodule.am +++ b/unpack/Makemodule.am @@ -1,5 +1,6 @@ unsquashfs_SOURCES = unpack/unsquashfs.c unpack/tree_node_from_inode.c unsquashfs_SOURCES += unpack/unsquashfs.h unpack/read_fstree.c +unsquashfs_SOURCES += unpack/list_files.c unsquashfs_LDADD = libsquashfs.a libfstree.a libcompress.a libutil.a if WITH_LZMA diff --git a/unpack/list_files.c b/unpack/list_files.c new file mode 100644 index 0000000..aaf0652 --- /dev/null +++ b/unpack/list_files.c @@ -0,0 +1,90 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later */ +#include "unsquashfs.h" + +static void mode_to_str(uint16_t mode, char *p) +{ + switch (mode & S_IFMT) { + case S_IFDIR: *(p++) = 'd'; break; + case S_IFCHR: *(p++) = 'c'; break; + case S_IFBLK: *(p++) = 'b'; break; + case S_IFREG: *(p++) = '-'; break; + case S_IFLNK: *(p++) = 'l'; break; + case S_IFSOCK: *(p++) = 's'; break; + case S_IFIFO: *(p++) = 'p'; break; + default: *(p++) = '?'; break; + } + + *(p++) = (mode & S_IRUSR) ? 'r' : '-'; + *(p++) = (mode & S_IWUSR) ? 'w' : '-'; + + switch (mode & (S_IXUSR | S_ISUID)) { + case S_IXUSR | S_ISUID: *(p++) = 's'; break; + case S_IXUSR: *(p++) = 'x'; break; + case S_ISUID: *(p++) = 'S'; break; + default: *(p++) = '-'; break; + } + + *(p++) = (mode & S_IRGRP) ? 'r' : '-'; + *(p++) = (mode & S_IWGRP) ? 'w' : '-'; + + switch (mode & (S_IXGRP | S_ISGID)) { + case S_IXGRP | S_ISGID: *(p++) = 's'; break; + case S_IXGRP: *(p++) = 'x'; break; + case S_ISGID: *(p++) = 'S'; break; + case 0: *(p++) = '-'; break; + } + + *(p++) = (mode & S_IROTH) ? 'r' : '-'; + *(p++) = (mode & S_IWOTH) ? 'w' : '-'; + + switch (mode & (S_IXOTH | S_ISVTX)) { + case S_IXOTH | S_ISVTX: *(p++) = 't'; break; + case S_IXOTH: *(p++) = 'x'; break; + case S_ISVTX: *(p++) = 'T'; break; + case 0: *(p++) = '-'; break; + } + + *p = '\0'; +} + +static int count_int_chars(unsigned int i) +{ + int count = 1; + + while (i > 10) { + ++count; + i /= 10; + } + + return count; +} + +void list_files(tree_node_t *node) +{ + int i, max_uid_chars = 0, max_gid_chars = 0; + char modestr[12]; + tree_node_t *n; + + if (S_ISDIR(node->mode)) { + for (n = node->data.dir->children; n != NULL; n = n->next) { + i = count_int_chars(n->uid); + max_uid_chars = i > max_uid_chars ? i : max_uid_chars; + + i = count_int_chars(n->gid); + max_gid_chars = i > max_gid_chars ? i : max_gid_chars; + } + + for (n = node->data.dir->children; n != NULL; n = n->next) { + mode_to_str(n->mode, modestr); + + printf("%s %*u/%-*u %s\n", modestr, + max_uid_chars, n->uid, + max_gid_chars, n->gid, n->name); + } + } else { + mode_to_str(node->mode, modestr); + + printf("%s %u/%u %s\n", modestr, + node->uid, node->gid, node->name); + } +} diff --git a/unpack/unsquashfs.c b/unpack/unsquashfs.c index d332dee..b253bae 100644 --- a/unpack/unsquashfs.c +++ b/unpack/unsquashfs.c @@ -1,28 +1,119 @@ /* SPDX-License-Identifier: GPL-3.0-or-later */ #include "unsquashfs.h" +static struct option long_opts[] = { + { "list", required_argument, NULL, 'l' }, + { "help", no_argument, NULL, 'h' }, + { "version", no_argument, NULL, 'V' }, +}; + +static const char *short_opts = "l:hV"; + +static const char *help_string = +"Usage: %s [OPTIONS] <squashfs-file>\n" +"\n" +"View or extract the contents of a squashfs image.\n" +"\n" +"Possible options:\n" +" --list, -l <path> Produce a directory listing for a given path in the\n" +" squashfs image.\n" +" --help, -h Print help text and exit.\n" +" --version, -V Print version information and exit.\n" +"\n"; + extern const char *__progname; +static tree_node_t *find_node(tree_node_t *n, const char *path) +{ + const char *end; + size_t len; + + while (path != NULL && *path != '\0') { + if (!S_ISDIR(n->mode)) { + errno = ENOTDIR; + return NULL; + } + + end = strchrnul(path, '/'); + len = end - path; + + for (n = n->data.dir->children; n != NULL; n = n->next) { + if (strncmp(path, n->name, len) != 0) + continue; + if (n->name[len] != '\0') + continue; + break; + } + + if (n == NULL) { + errno = ENOENT; + return NULL; + } + + path = *end ? (end + 1) : end; + } + + return n; +} + int main(int argc, char **argv) { - int fd, status = EXIT_FAILURE; + int i, fd, status = EXIT_FAILURE; + char *lspath = NULL; sqfs_super_t super; compressor_t *cmp; fstree_t fs; - if (argc != 2) { - fprintf(stderr, "Usage: %s <filename>\n", __progname); - return EXIT_FAILURE; + for (;;) { + i = getopt_long(argc, argv, short_opts, long_opts, NULL); + if (i == -1) + break; + + switch (i) { + case 'l': + if (optarg != NULL) { + free(lspath); + + lspath = strdup(optarg ? optarg : ""); + if (lspath == NULL) { + perror("processing arguments"); + return EXIT_FAILURE; + } + + if (canonicalize_name(lspath)) { + fprintf(stderr, "Invalid pth: %s\n", + optarg); + goto out_cmd; + } + } + break; + case 'h': + printf(help_string, __progname); + status = EXIT_SUCCESS; + goto out_cmd; + case 'V': + print_version(); + status = EXIT_SUCCESS; + goto out_cmd; + default: + goto fail_arg; + } } - fd = open(argv[1], O_RDONLY); + if (optind >= argc) { + fprintf(stderr, "Usage: %s [OPTIONS] <filename>\n", + __progname); + goto out_cmd; + } + + fd = open(argv[optind], O_RDONLY); if (fd < 0) { - perror(argv[1]); - return EXIT_FAILURE; + perror(argv[optind]); + goto out_cmd; } if (sqfs_super_read(&super, fd)) - goto out; + goto out_fd; if ((super.version_major != SQFS_VERSION_MAJOR) || (super.version_minor != SQFS_VERSION_MINOR)) { @@ -31,34 +122,52 @@ int main(int argc, char **argv) "We currently only supports version %d.%d (sorry).\n", super.version_major, super.version_minor, SQFS_VERSION_MAJOR, SQFS_VERSION_MINOR); - goto out; + goto out_fd; } if (super.flags & SQFS_FLAG_COMPRESSOR_OPTIONS) { fputs("Image has been built with compressor options.\n" "This is not yet supported.\n", stderr); - goto out; + goto out_fd; } if (!compressor_exists(super.compression_id)) { fputs("Image uses a compressor that has not been built in\n", stderr); - goto out; + goto out_fd; } cmp = compressor_create(super.compression_id, false, super.block_size); if (cmp == NULL) - goto out; + goto out_fd; if (read_fstree(&fs, fd, &super, cmp)) goto out_cmp; + if (lspath != NULL) { + tree_node_t *n = find_node(fs.root, lspath); + + if (n == NULL) { + perror(lspath); + goto out_fs; + } + + list_files(n); + } + status = EXIT_SUCCESS; +out_fs: fstree_cleanup(&fs); out_cmp: cmp->destroy(cmp); -out: +out_fd: close(fd); +out_cmd: + free(lspath); return status; +fail_arg: + fprintf(stderr, "Try `%s --help' for more information.\n", __progname); + free(lspath); + return EXIT_FAILURE; } diff --git a/unpack/unsquashfs.h b/unpack/unsquashfs.h index 5553012..52b3889 100644 --- a/unpack/unsquashfs.h +++ b/unpack/unsquashfs.h @@ -7,10 +7,14 @@ #include "compress.h" #include "id_table.h" #include "fstree.h" +#include "config.h" +#include "util.h" #include <string.h> #include <stdlib.h> #include <unistd.h> +#include <getopt.h> +#include <errno.h> #include <fcntl.h> #include <stdio.h> @@ -21,4 +25,6 @@ tree_node_t *tree_node_from_inode(sqfs_inode_generic_t *inode, int read_fstree(fstree_t *out, int fd, sqfs_super_t *super, compressor_t *cmp); +void list_files(tree_node_t *node); + #endif /* UNSQUASHFS_H */ |