From 04aa1d971efb44eefa2290ed981c61842b7f62b7 Mon Sep 17 00:00:00 2001 From: David Oberhollenzer Date: Sat, 4 May 2019 21:02:09 +0200 Subject: Rename tools to not collide with squashfs-tools This commit changes the names of the tools to gensquashfs and rdsquashfs so they don't collide with the names used by the squashfs-tools package and the two can be installed side by side. Signed-off-by: David Oberhollenzer --- .gitignore | 4 +- mkfs/Makemodule.am | 12 +- mkfs/block.c | 2 +- mkfs/meta.c | 2 +- mkfs/mkfs.c | 118 +++++++++++++++++ mkfs/mkfs.h | 61 +++++++++ mkfs/mksquashfs.c | 118 ----------------- mkfs/mksquashfs.h | 61 --------- mkfs/options.c | 2 +- unpack/Makemodule.am | 16 +-- unpack/extract_file.c | 2 +- unpack/list_files.c | 2 +- unpack/rdsquashfs.c | 289 ++++++++++++++++++++++++++++++++++++++++++ unpack/rdsquashfs.h | 49 +++++++ unpack/read_fstree.c | 2 +- unpack/restore_fstree.c | 2 +- unpack/tree_node_from_inode.c | 2 +- unpack/unsquashfs.c | 289 ------------------------------------------ unpack/unsquashfs.h | 49 ------- 19 files changed, 541 insertions(+), 541 deletions(-) create mode 100644 mkfs/mkfs.c create mode 100644 mkfs/mkfs.h delete mode 100644 mkfs/mksquashfs.c delete mode 100644 mkfs/mksquashfs.h create mode 100644 unpack/rdsquashfs.c create mode 100644 unpack/rdsquashfs.h delete mode 100644 unpack/unsquashfs.c delete mode 100644 unpack/unsquashfs.h diff --git a/.gitignore b/.gitignore index 944a414..2d67338 100644 --- a/.gitignore +++ b/.gitignore @@ -18,5 +18,5 @@ config.h *.o *.a *~ -mksquashfs -unsquashfs +gensquashfs +rdsquashfs diff --git a/mkfs/Makemodule.am b/mkfs/Makemodule.am index 3914e71..5af5ad5 100644 --- a/mkfs/Makemodule.am +++ b/mkfs/Makemodule.am @@ -1,13 +1,13 @@ -mksquashfs_SOURCES = mkfs/mksquashfs.c mkfs/mksquashfs.h mkfs/block.c -mksquashfs_SOURCES += mkfs/options.c mkfs/meta.c -mksquashfs_LDADD = libsquashfs.a libfstree.a libcompress.a libutil.a +gensquashfs_SOURCES = mkfs/mkfs.c mkfs/mkfs.h mkfs/block.c +gensquashfs_SOURCES += mkfs/options.c mkfs/meta.c +gensquashfs_LDADD = libsquashfs.a libfstree.a libcompress.a libutil.a if WITH_XZ -mksquashfs_LDADD += $(XZ_LIBS) +gensquashfs_LDADD += $(XZ_LIBS) endif if WITH_GZIP -mksquashfs_LDADD += $(ZLIB_LIBS) +gensquashfs_LDADD += $(ZLIB_LIBS) endif -bin_PROGRAMS += mksquashfs +bin_PROGRAMS += gensquashfs diff --git a/mkfs/block.c b/mkfs/block.c index 3472346..76b261d 100644 --- a/mkfs/block.c +++ b/mkfs/block.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later */ -#include "mksquashfs.h" +#include "mkfs.h" #include "util.h" static int write_block(file_info_t *fi, sqfs_info_t *info) diff --git a/mkfs/meta.c b/mkfs/meta.c index c48aec3..f7de5c6 100644 --- a/mkfs/meta.c +++ b/mkfs/meta.c @@ -1,6 +1,6 @@ /* SPDX-License-Identifier: GPL-3.0-or-later */ #include "meta_writer.h" -#include "mksquashfs.h" +#include "mkfs.h" #include "util.h" #include diff --git a/mkfs/mkfs.c b/mkfs/mkfs.c new file mode 100644 index 0000000..d708d37 --- /dev/null +++ b/mkfs/mkfs.c @@ -0,0 +1,118 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later */ +#include "mkfs.h" +#include "util.h" + +static int padd_file(sqfs_info_t *info) +{ + size_t padd_sz = info->super.bytes_used % info->opt.devblksz; + uint8_t *buffer; + ssize_t ret; + + if (padd_sz == 0) + return 0; + + padd_sz = info->opt.devblksz - padd_sz; + + buffer = calloc(1, padd_sz); + if (buffer == NULL) { + perror("padding output file to block size"); + return -1; + } + + ret = write_retry(info->outfd, buffer, padd_sz); + + if (ret < 0) { + perror("Error padding squashfs image to page size"); + free(buffer); + return -1; + } + + if ((size_t)ret < padd_sz) { + fputs("Truncated write trying to padd squashfs image\n", + stderr); + return -1; + } + + free(buffer); + return 0; +} + +int main(int argc, char **argv) +{ + int status = EXIT_FAILURE; + sqfs_info_t info; + + memset(&info, 0, sizeof(info)); + + process_command_line(&info.opt, argc, argv); + + if (sqfs_super_init(&info.super, info.opt.blksz, info.opt.def_mtime, + info.opt.compressor)) { + return EXIT_FAILURE; + } + + if (id_table_init(&info.idtbl)) + return EXIT_FAILURE; + + info.outfd = open(info.opt.outfile, info.opt.outmode, 0644); + if (info.outfd < 0) { + perror(info.opt.outfile); + goto out_idtbl; + } + + if (sqfs_super_write(&info.super, info.outfd)) + goto out_outfd; + + if (fstree_init(&info.fs, info.opt.blksz, info.opt.def_mtime, + info.opt.def_mode, info.opt.def_uid, + info.opt.def_gid)) { + goto out_outfd; + } + + if (fstree_from_file(&info.fs, info.opt.infile)) + goto out_fstree; + + fstree_sort(&info.fs); + + info.cmp = compressor_create(info.super.compression_id, true, + info.super.block_size); + if (info.cmp == NULL) { + fputs("Error creating compressor\n", stderr); + goto out_outfd; + } + + if (write_data_to_image(&info)) + goto out_cmp; + + if (sqfs_write_inodes(&info)) + goto out_cmp; + + info.super.fragment_entry_count = info.num_fragments; + + if (sqfs_write_table(info.outfd, &info.super, info.fragments, + sizeof(info.fragments[0]), info.num_fragments, + &info.super.fragment_table_start, info.cmp)) { + goto out_cmp; + } + + if (id_table_write(&info.idtbl, info.outfd, &info.super, info.cmp)) + goto out_cmp; + + if (sqfs_super_write(&info.super, info.outfd)) + goto out_cmp; + + if (padd_file(&info)) + goto out_cmp; + + status = EXIT_SUCCESS; +out_cmp: + free(info.fragments); + info.cmp->destroy(info.cmp); +out_fstree: + fstree_cleanup(&info.fs); +out_outfd: + close(info.outfd); +out_idtbl: + id_table_cleanup(&info.idtbl); + return status; +} diff --git a/mkfs/mkfs.h b/mkfs/mkfs.h new file mode 100644 index 0000000..8a867f2 --- /dev/null +++ b/mkfs/mkfs.h @@ -0,0 +1,61 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later */ +#ifndef MKFS_H +#define MKFS_H + +#include "squashfs.h" +#include "compress.h" +#include "id_table.h" +#include "fstree.h" +#include "config.h" +#include "table.h" + +#include +#include +#include +#include +#include +#include + +typedef struct { + unsigned int def_uid; + unsigned int def_gid; + unsigned int def_mode; + unsigned int def_mtime; + int outmode; + int compressor; + int blksz; + int devblksz; + const char *infile; + const char *outfile; +} options_t; + +typedef struct { + int outfd; + options_t opt; + sqfs_super_t super; + fstree_t fs; + void *block; + void *fragment; + void *scratch; + + sqfs_fragment_t *fragments; + size_t num_fragments; + size_t max_fragments; + + int file_block_count; + file_info_t *frag_list; + size_t frag_offset; + + id_table_t idtbl; + size_t inode_counter; + + compressor_t *cmp; +} sqfs_info_t; + +void process_command_line(options_t *opt, int argc, char **argv); + +int write_data_to_image(sqfs_info_t *info); + +int sqfs_write_inodes(sqfs_info_t *info); + +#endif /* MKFS_H */ diff --git a/mkfs/mksquashfs.c b/mkfs/mksquashfs.c deleted file mode 100644 index 2f2e508..0000000 --- a/mkfs/mksquashfs.c +++ /dev/null @@ -1,118 +0,0 @@ -/* SPDX-License-Identifier: GPL-3.0-or-later */ -#include "mksquashfs.h" -#include "util.h" - -static int padd_file(sqfs_info_t *info) -{ - size_t padd_sz = info->super.bytes_used % info->opt.devblksz; - uint8_t *buffer; - ssize_t ret; - - if (padd_sz == 0) - return 0; - - padd_sz = info->opt.devblksz - padd_sz; - - buffer = calloc(1, padd_sz); - if (buffer == NULL) { - perror("padding output file to block size"); - return -1; - } - - ret = write_retry(info->outfd, buffer, padd_sz); - - if (ret < 0) { - perror("Error padding squashfs image to page size"); - free(buffer); - return -1; - } - - if ((size_t)ret < padd_sz) { - fputs("Truncated write trying to padd squashfs image\n", - stderr); - return -1; - } - - free(buffer); - return 0; -} - -int main(int argc, char **argv) -{ - int status = EXIT_FAILURE; - sqfs_info_t info; - - memset(&info, 0, sizeof(info)); - - process_command_line(&info.opt, argc, argv); - - if (sqfs_super_init(&info.super, info.opt.blksz, info.opt.def_mtime, - info.opt.compressor)) { - return EXIT_FAILURE; - } - - if (id_table_init(&info.idtbl)) - return EXIT_FAILURE; - - info.outfd = open(info.opt.outfile, info.opt.outmode, 0644); - if (info.outfd < 0) { - perror(info.opt.outfile); - goto out_idtbl; - } - - if (sqfs_super_write(&info.super, info.outfd)) - goto out_outfd; - - if (fstree_init(&info.fs, info.opt.blksz, info.opt.def_mtime, - info.opt.def_mode, info.opt.def_uid, - info.opt.def_gid)) { - goto out_outfd; - } - - if (fstree_from_file(&info.fs, info.opt.infile)) - goto out_fstree; - - fstree_sort(&info.fs); - - info.cmp = compressor_create(info.super.compression_id, true, - info.super.block_size); - if (info.cmp == NULL) { - fputs("Error creating compressor\n", stderr); - goto out_outfd; - } - - if (write_data_to_image(&info)) - goto out_cmp; - - if (sqfs_write_inodes(&info)) - goto out_cmp; - - info.super.fragment_entry_count = info.num_fragments; - - if (sqfs_write_table(info.outfd, &info.super, info.fragments, - sizeof(info.fragments[0]), info.num_fragments, - &info.super.fragment_table_start, info.cmp)) { - goto out_cmp; - } - - if (id_table_write(&info.idtbl, info.outfd, &info.super, info.cmp)) - goto out_cmp; - - if (sqfs_super_write(&info.super, info.outfd)) - goto out_cmp; - - if (padd_file(&info)) - goto out_cmp; - - status = EXIT_SUCCESS; -out_cmp: - free(info.fragments); - info.cmp->destroy(info.cmp); -out_fstree: - fstree_cleanup(&info.fs); -out_outfd: - close(info.outfd); -out_idtbl: - id_table_cleanup(&info.idtbl); - return status; -} diff --git a/mkfs/mksquashfs.h b/mkfs/mksquashfs.h deleted file mode 100644 index 106822c..0000000 --- a/mkfs/mksquashfs.h +++ /dev/null @@ -1,61 +0,0 @@ -/* SPDX-License-Identifier: GPL-3.0-or-later */ -#ifndef MKSQUASHFS_H -#define MKSQUASHFS_H - -#include "squashfs.h" -#include "compress.h" -#include "id_table.h" -#include "fstree.h" -#include "config.h" -#include "table.h" - -#include -#include -#include -#include -#include -#include - -typedef struct { - unsigned int def_uid; - unsigned int def_gid; - unsigned int def_mode; - unsigned int def_mtime; - int outmode; - int compressor; - int blksz; - int devblksz; - const char *infile; - const char *outfile; -} options_t; - -typedef struct { - int outfd; - options_t opt; - sqfs_super_t super; - fstree_t fs; - void *block; - void *fragment; - void *scratch; - - sqfs_fragment_t *fragments; - size_t num_fragments; - size_t max_fragments; - - int file_block_count; - file_info_t *frag_list; - size_t frag_offset; - - id_table_t idtbl; - size_t inode_counter; - - compressor_t *cmp; -} sqfs_info_t; - -void process_command_line(options_t *opt, int argc, char **argv); - -int write_data_to_image(sqfs_info_t *info); - -int sqfs_write_inodes(sqfs_info_t *info); - -#endif /* MKSQUASHFS_H */ diff --git a/mkfs/options.c b/mkfs/options.c index f1fe192..4d49a9b 100644 --- a/mkfs/options.c +++ b/mkfs/options.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later */ -#include "mksquashfs.h" +#include "mkfs.h" #include "util.h" #include diff --git a/unpack/Makemodule.am b/unpack/Makemodule.am index ccb5707..6a0eab0 100644 --- a/unpack/Makemodule.am +++ b/unpack/Makemodule.am @@ -1,15 +1,15 @@ -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 unpack/extract_file.c -unsquashfs_SOURCES += unpack/restore_fstree.c -unsquashfs_LDADD = libsquashfs.a libfstree.a libcompress.a libutil.a +rdsquashfs_SOURCES = unpack/rdsquashfs.c unpack/tree_node_from_inode.c +rdsquashfs_SOURCES += unpack/rdsquashfs.h unpack/read_fstree.c +rdsquashfs_SOURCES += unpack/list_files.c unpack/extract_file.c +rdsquashfs_SOURCES += unpack/restore_fstree.c +rdsquashfs_LDADD = libsquashfs.a libfstree.a libcompress.a libutil.a if WITH_XZ -unsquashfs_LDADD += $(XZ_LIBS) +rdsquashfs_LDADD += $(XZ_LIBS) endif if WITH_GZIP -unsquashfs_LDADD += $(ZLIB_LIBS) +rdsquashfs_LDADD += $(ZLIB_LIBS) endif -bin_PROGRAMS += unsquashfs +bin_PROGRAMS += rdsquashfs diff --git a/unpack/extract_file.c b/unpack/extract_file.c index 368f6d7..0e62f75 100644 --- a/unpack/extract_file.c +++ b/unpack/extract_file.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later */ -#include "unsquashfs.h" +#include "rdsquashfs.h" int extract_file(file_info_t *fi, compressor_t *cmp, size_t block_size, frag_reader_t *frag, int sqfsfd, int outfd) diff --git a/unpack/list_files.c b/unpack/list_files.c index 86bc185..b9bb161 100644 --- a/unpack/list_files.c +++ b/unpack/list_files.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later */ -#include "unsquashfs.h" +#include "rdsquashfs.h" #include diff --git a/unpack/rdsquashfs.c b/unpack/rdsquashfs.c new file mode 100644 index 0000000..ec14e0e --- /dev/null +++ b/unpack/rdsquashfs.c @@ -0,0 +1,289 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later */ +#include "rdsquashfs.h" + +enum { + OP_NONE = 0, + OP_LS, + OP_CAT, + OP_UNPACK, +}; + +static struct option long_opts[] = { + { "list", required_argument, NULL, 'l' }, + { "cat", required_argument, NULL, 'c' }, + { "unpack-root", required_argument, NULL, 'u' }, + { "no-dev", no_argument, NULL, 'D' }, + { "no-sock", no_argument, NULL, 'S' }, + { "no-fifo", no_argument, NULL, 'F' }, + { "no-slink", no_argument, NULL, 'L' }, + { "no-empty-dir", no_argument, NULL, 'E' }, + { "chmod", no_argument, NULL, 'C' }, + { "chown", no_argument, NULL, 'O' }, + { "help", no_argument, NULL, 'h' }, + { "version", no_argument, NULL, 'V' }, +}; + +static const char *short_opts = "l:c:u:DSFLCOEhV"; + +static const char *help_string = +"Usage: %s [OPTIONS] \n" +"\n" +"View or extract the contents of a squashfs image.\n" +"\n" +"Possible options:\n" +" --list, -l Produce a directory listing for a given path in the\n" +" squashfs image.\n" +" --cat, -c If the specified path is a regular file in the,\n" +" image, dump its contents to stdout.\n" +" --unpack-root Unpack the contents of the filesystem into the\n" +" specified path.\n" +" --no-dev, -D Do not unpack device special files.\n" +" --no-sock, -S Do not unpack socket files.\n" +" --no-fifo, -F Do not unpack named pipes.\n" +" --no-slink, -L Do not unpack symbolic links.\n" +" --no-empty-dir, -E Do not unpack directories that would end up empty.\n" +" --chmod, -C Change permission flags of unpacked files to those\n" +" store in the squashfs image.\n" +" --chown, -O Change ownership of unpacked files to the UID/GID\n" +" set in the squashfs iamge.\n" +"\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; +} + +static char *get_path(char *old, const char *arg) +{ + char *path; + + free(old); + + path = strdup(arg); + if (path == NULL) { + perror("processing arguments"); + exit(EXIT_FAILURE); + } + + if (canonicalize_name(path)) { + fprintf(stderr, "Invalid path: %s\n", arg); + free(path); + exit(EXIT_FAILURE); + } + + return path; +} + +int main(int argc, char **argv) +{ + int i, fd, status = EXIT_FAILURE, op = OP_NONE, unpack_flags = 0; + const char *unpack_root = NULL; + frag_reader_t *frag = NULL; + char *cmdpath = NULL; + sqfs_super_t super; + compressor_t *cmp; + tree_node_t *n; + fstree_t fs; + + for (;;) { + i = getopt_long(argc, argv, short_opts, long_opts, NULL); + if (i == -1) + break; + + switch (i) { + case 'D': + unpack_flags |= UNPACK_NO_DEVICES; + break; + case 'S': + unpack_flags |= UNPACK_NO_SOCKETS; + break; + case 'F': + unpack_flags |= UNPACK_NO_FIFO; + break; + case 'L': + unpack_flags |= UNPACK_NO_SLINKS; + break; + case 'C': + unpack_flags |= UNPACK_CHMOD; + break; + case 'O': + unpack_flags |= UNPACK_CHOWN; + break; + case 'E': + unpack_flags |= UNPACK_NO_EMPTY; + break; + case 'c': + op = OP_CAT; + cmdpath = get_path(cmdpath, optarg); + break; + case 'l': + op = OP_LS; + cmdpath = get_path(cmdpath, optarg); + break; + case 'u': + op = OP_UNPACK; + unpack_root = optarg; + 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; + } + } + + if (op == OP_NONE) { + fputs("No opteration specified\n", stderr); + goto fail_arg; + } + + if (optind >= argc) { + fputs("Missing image argument\n", stderr); + goto fail_arg; + } + + fd = open(argv[optind], O_RDONLY); + if (fd < 0) { + perror(argv[optind]); + goto out_cmd; + } + + if (sqfs_super_read(&super, fd)) + goto out_fd; + + if ((super.version_major != SQFS_VERSION_MAJOR) || + (super.version_minor != SQFS_VERSION_MINOR)) { + fprintf(stderr, + "The image uses squashfs version %d.%d\n" + "We currently only supports version %d.%d (sorry).\n", + super.version_major, super.version_minor, + SQFS_VERSION_MAJOR, SQFS_VERSION_MINOR); + 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_fd; + } + + if (!compressor_exists(super.compression_id)) { + fputs("Image uses a compressor that has not been built in\n", + stderr); + goto out_fd; + } + + cmp = compressor_create(super.compression_id, false, super.block_size); + if (cmp == NULL) + goto out_fd; + + if (read_fstree(&fs, fd, &super, cmp, unpack_flags)) + goto out_cmp; + + switch (op) { + case OP_LS: + n = find_node(fs.root, cmdpath); + if (n == NULL) { + perror(cmdpath); + goto out_fs; + } + + list_files(n); + break; + case OP_CAT: + n = find_node(fs.root, cmdpath); + if (n == NULL) { + perror(cmdpath); + goto out_fs; + } + + if (!S_ISREG(n->mode)) { + fprintf(stderr, "/%s: not a regular file\n", cmdpath); + goto out_fs; + } + + if (super.fragment_entry_count > 0 && + super.fragment_table_start < super.bytes_used && + !(super.flags & SQFS_FLAG_NO_FRAGMENTS)) { + frag = frag_reader_create(&super, fd, cmp); + if (frag == NULL) + goto out_fs; + } + + if (extract_file(n->data.file, cmp, super.block_size, + frag, fd, STDOUT_FILENO)) { + goto out_fs; + } + break; + case OP_UNPACK: + if (super.fragment_entry_count > 0 && + super.fragment_table_start < super.bytes_used && + !(super.flags & SQFS_FLAG_NO_FRAGMENTS)) { + frag = frag_reader_create(&super, fd, cmp); + if (frag == NULL) + goto out_fs; + } + + if (restore_fstree(unpack_root, fs.root, cmp, super.block_size, + frag, fd, unpack_flags)) { + goto out_fs; + } + break; + } + + status = EXIT_SUCCESS; +out_fs: + if (frag != NULL) + frag_reader_destroy(frag); + fstree_cleanup(&fs); +out_cmp: + cmp->destroy(cmp); +out_fd: + close(fd); +out_cmd: + free(cmdpath); + return status; +fail_arg: + fprintf(stderr, "Try `%s --help' for more information.\n", __progname); + free(cmdpath); + return EXIT_FAILURE; +} diff --git a/unpack/rdsquashfs.h b/unpack/rdsquashfs.h new file mode 100644 index 0000000..995eee7 --- /dev/null +++ b/unpack/rdsquashfs.h @@ -0,0 +1,49 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later */ +#ifndef RDSQUASHFS_H +#define RDSQUASHFS_H + +#include "meta_reader.h" +#include "frag_reader.h" +#include "squashfs.h" +#include "compress.h" +#include "id_table.h" +#include "fstree.h" +#include "config.h" +#include "util.h" + +#include +#include +#include +#include +#include +#include +#include + +enum UNPACK_FLAGS { + UNPACK_NO_DEVICES = 0x01, + UNPACK_NO_SOCKETS = 0x02, + UNPACK_NO_FIFO = 0x04, + UNPACK_NO_SLINKS = 0x08, + UNPACK_NO_EMPTY = 0x10, + UNPACK_CHMOD = 0x20, + UNPACK_CHOWN = 0x40, +}; + +tree_node_t *tree_node_from_inode(sqfs_inode_generic_t *inode, + const id_table_t *idtbl, + const char *name, + size_t block_size); + +int read_fstree(fstree_t *out, int fd, sqfs_super_t *super, compressor_t *cmp, + int flags); + +void list_files(tree_node_t *node); + +int extract_file(file_info_t *fi, compressor_t *cmp, size_t block_size, + frag_reader_t *frag, int sqfsfd, int outfd); + +int restore_fstree(const char *rootdir, tree_node_t *root, compressor_t *cmp, + size_t block_size, frag_reader_t *frag, int sqfsfd, + int flags); + +#endif /* RDSQUASHFS_H */ diff --git a/unpack/read_fstree.c b/unpack/read_fstree.c index f200243..0b75d8b 100644 --- a/unpack/read_fstree.c +++ b/unpack/read_fstree.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later */ -#include "unsquashfs.h" +#include "rdsquashfs.h" static int should_skip(int type, int flags) diff --git a/unpack/restore_fstree.c b/unpack/restore_fstree.c index 8ef21c3..5a20c5f 100644 --- a/unpack/restore_fstree.c +++ b/unpack/restore_fstree.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later */ -#include "unsquashfs.h" +#include "rdsquashfs.h" static int restore_directory(int dirfd, tree_node_t *n, compressor_t *cmp, size_t block_size, frag_reader_t *frag, diff --git a/unpack/tree_node_from_inode.c b/unpack/tree_node_from_inode.c index 95056a7..2e720d3 100644 --- a/unpack/tree_node_from_inode.c +++ b/unpack/tree_node_from_inode.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later */ -#include "unsquashfs.h" +#include "rdsquashfs.h" static size_t compute_size(sqfs_inode_generic_t *inode, const char *name, size_t block_size) diff --git a/unpack/unsquashfs.c b/unpack/unsquashfs.c deleted file mode 100644 index 8ec1b94..0000000 --- a/unpack/unsquashfs.c +++ /dev/null @@ -1,289 +0,0 @@ -/* SPDX-License-Identifier: GPL-3.0-or-later */ -#include "unsquashfs.h" - -enum { - OP_NONE = 0, - OP_LS, - OP_CAT, - OP_UNPACK, -}; - -static struct option long_opts[] = { - { "list", required_argument, NULL, 'l' }, - { "cat", required_argument, NULL, 'c' }, - { "unpack-root", required_argument, NULL, 'u' }, - { "no-dev", no_argument, NULL, 'D' }, - { "no-sock", no_argument, NULL, 'S' }, - { "no-fifo", no_argument, NULL, 'F' }, - { "no-slink", no_argument, NULL, 'L' }, - { "no-empty-dir", no_argument, NULL, 'E' }, - { "chmod", no_argument, NULL, 'C' }, - { "chown", no_argument, NULL, 'O' }, - { "help", no_argument, NULL, 'h' }, - { "version", no_argument, NULL, 'V' }, -}; - -static const char *short_opts = "l:c:u:DSFLCOEhV"; - -static const char *help_string = -"Usage: %s [OPTIONS] \n" -"\n" -"View or extract the contents of a squashfs image.\n" -"\n" -"Possible options:\n" -" --list, -l Produce a directory listing for a given path in the\n" -" squashfs image.\n" -" --cat, -c If the specified path is a regular file in the,\n" -" image, dump its contents to stdout.\n" -" --unpack-root Unpack the contents of the filesystem into the\n" -" specified path.\n" -" --no-dev, -D Do not unpack device special files.\n" -" --no-sock, -S Do not unpack socket files.\n" -" --no-fifo, -F Do not unpack named pipes.\n" -" --no-slink, -L Do not unpack symbolic links.\n" -" --no-empty-dir, -E Do not unpack directories that would end up empty.\n" -" --chmod, -C Change permission flags of unpacked files to those\n" -" store in the squashfs image.\n" -" --chown, -O Change ownership of unpacked files to the UID/GID\n" -" set in the squashfs iamge.\n" -"\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; -} - -static char *get_path(char *old, const char *arg) -{ - char *path; - - free(old); - - path = strdup(arg); - if (path == NULL) { - perror("processing arguments"); - exit(EXIT_FAILURE); - } - - if (canonicalize_name(path)) { - fprintf(stderr, "Invalid path: %s\n", arg); - free(path); - exit(EXIT_FAILURE); - } - - return path; -} - -int main(int argc, char **argv) -{ - int i, fd, status = EXIT_FAILURE, op = OP_NONE, unpack_flags = 0; - const char *unpack_root = NULL; - frag_reader_t *frag = NULL; - char *cmdpath = NULL; - sqfs_super_t super; - compressor_t *cmp; - tree_node_t *n; - fstree_t fs; - - for (;;) { - i = getopt_long(argc, argv, short_opts, long_opts, NULL); - if (i == -1) - break; - - switch (i) { - case 'D': - unpack_flags |= UNPACK_NO_DEVICES; - break; - case 'S': - unpack_flags |= UNPACK_NO_SOCKETS; - break; - case 'F': - unpack_flags |= UNPACK_NO_FIFO; - break; - case 'L': - unpack_flags |= UNPACK_NO_SLINKS; - break; - case 'C': - unpack_flags |= UNPACK_CHMOD; - break; - case 'O': - unpack_flags |= UNPACK_CHOWN; - break; - case 'E': - unpack_flags |= UNPACK_NO_EMPTY; - break; - case 'c': - op = OP_CAT; - cmdpath = get_path(cmdpath, optarg); - break; - case 'l': - op = OP_LS; - cmdpath = get_path(cmdpath, optarg); - break; - case 'u': - op = OP_UNPACK; - unpack_root = optarg; - 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; - } - } - - if (op == OP_NONE) { - fputs("No opteration specified\n", stderr); - goto fail_arg; - } - - if (optind >= argc) { - fputs("Missing image argument\n", stderr); - goto fail_arg; - } - - fd = open(argv[optind], O_RDONLY); - if (fd < 0) { - perror(argv[optind]); - goto out_cmd; - } - - if (sqfs_super_read(&super, fd)) - goto out_fd; - - if ((super.version_major != SQFS_VERSION_MAJOR) || - (super.version_minor != SQFS_VERSION_MINOR)) { - fprintf(stderr, - "The image uses squashfs version %d.%d\n" - "We currently only supports version %d.%d (sorry).\n", - super.version_major, super.version_minor, - SQFS_VERSION_MAJOR, SQFS_VERSION_MINOR); - 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_fd; - } - - if (!compressor_exists(super.compression_id)) { - fputs("Image uses a compressor that has not been built in\n", - stderr); - goto out_fd; - } - - cmp = compressor_create(super.compression_id, false, super.block_size); - if (cmp == NULL) - goto out_fd; - - if (read_fstree(&fs, fd, &super, cmp, unpack_flags)) - goto out_cmp; - - switch (op) { - case OP_LS: - n = find_node(fs.root, cmdpath); - if (n == NULL) { - perror(cmdpath); - goto out_fs; - } - - list_files(n); - break; - case OP_CAT: - n = find_node(fs.root, cmdpath); - if (n == NULL) { - perror(cmdpath); - goto out_fs; - } - - if (!S_ISREG(n->mode)) { - fprintf(stderr, "/%s: not a regular file\n", cmdpath); - goto out_fs; - } - - if (super.fragment_entry_count > 0 && - super.fragment_table_start < super.bytes_used && - !(super.flags & SQFS_FLAG_NO_FRAGMENTS)) { - frag = frag_reader_create(&super, fd, cmp); - if (frag == NULL) - goto out_fs; - } - - if (extract_file(n->data.file, cmp, super.block_size, - frag, fd, STDOUT_FILENO)) { - goto out_fs; - } - break; - case OP_UNPACK: - if (super.fragment_entry_count > 0 && - super.fragment_table_start < super.bytes_used && - !(super.flags & SQFS_FLAG_NO_FRAGMENTS)) { - frag = frag_reader_create(&super, fd, cmp); - if (frag == NULL) - goto out_fs; - } - - if (restore_fstree(unpack_root, fs.root, cmp, super.block_size, - frag, fd, unpack_flags)) { - goto out_fs; - } - break; - } - - status = EXIT_SUCCESS; -out_fs: - if (frag != NULL) - frag_reader_destroy(frag); - fstree_cleanup(&fs); -out_cmp: - cmp->destroy(cmp); -out_fd: - close(fd); -out_cmd: - free(cmdpath); - return status; -fail_arg: - fprintf(stderr, "Try `%s --help' for more information.\n", __progname); - free(cmdpath); - return EXIT_FAILURE; -} diff --git a/unpack/unsquashfs.h b/unpack/unsquashfs.h deleted file mode 100644 index a8784fc..0000000 --- a/unpack/unsquashfs.h +++ /dev/null @@ -1,49 +0,0 @@ -/* SPDX-License-Identifier: GPL-3.0-or-later */ -#ifndef UNSQUASHFS_H -#define UNSQUASHFS_H - -#include "meta_reader.h" -#include "frag_reader.h" -#include "squashfs.h" -#include "compress.h" -#include "id_table.h" -#include "fstree.h" -#include "config.h" -#include "util.h" - -#include -#include -#include -#include -#include -#include -#include - -enum UNPACK_FLAGS { - UNPACK_NO_DEVICES = 0x01, - UNPACK_NO_SOCKETS = 0x02, - UNPACK_NO_FIFO = 0x04, - UNPACK_NO_SLINKS = 0x08, - UNPACK_NO_EMPTY = 0x10, - UNPACK_CHMOD = 0x20, - UNPACK_CHOWN = 0x40, -}; - -tree_node_t *tree_node_from_inode(sqfs_inode_generic_t *inode, - const id_table_t *idtbl, - const char *name, - size_t block_size); - -int read_fstree(fstree_t *out, int fd, sqfs_super_t *super, compressor_t *cmp, - int flags); - -void list_files(tree_node_t *node); - -int extract_file(file_info_t *fi, compressor_t *cmp, size_t block_size, - frag_reader_t *frag, int sqfsfd, int outfd); - -int restore_fstree(const char *rootdir, tree_node_t *root, compressor_t *cmp, - size_t block_size, frag_reader_t *frag, int sqfsfd, - int flags); - -#endif /* UNSQUASHFS_H */ -- cgit v1.2.3