From 09a336507da6cede3815a4bbc57264b9b558209a Mon Sep 17 00:00:00 2001 From: David Oberhollenzer Date: Tue, 29 Jan 2019 00:14:06 +0100 Subject: [PATCH] Add flags to dump command to dump header data vs file list Signed-off-by: David Oberhollenzer --- Makefile.am | 1 + main/cmd/dump/dump.c | 31 ++++++++++++++++++--- main/cmd/dump/dump.h | 8 ++++++ main/cmd/dump/dump_header.c | 54 +++++++++++++++++++++++++++++++++++++ 4 files changed, 90 insertions(+), 4 deletions(-) create mode 100644 main/cmd/dump/dump_header.c diff --git a/Makefile.am b/Makefile.am index 13144f5..eec3052 100644 --- a/Makefile.am +++ b/Makefile.am @@ -30,6 +30,7 @@ pkg_SOURCES += main/cmd/pack/write_hdr.c # dump command pkg_SOURCES += main/cmd/dump/dump.c main/cmd/dump/dump.h main/cmd/dump/dump_toc.c +pkg_SOURCES += main/cmd/dump/dump_header.c # unpack command pkg_SOURCES += main/cmd/unpack/unpack.c main/cmd/unpack/unpack.h diff --git a/main/cmd/dump/dump.c b/main/cmd/dump/dump.c index dcb2da0..507ca79 100644 --- a/main/cmd/dump/dump.c +++ b/main/cmd/dump/dump.c @@ -1,12 +1,14 @@ #include "dump.h" static const struct option long_opts[] = { + { "dependencies", no_argument, NULL, 'd' }, + { "list-files", no_argument, NULL, 'l' }, { "format", required_argument, NULL, 'f' }, { "root", required_argument, NULL, 'r' }, { NULL, 0, NULL, 0 }, }; -static const char *short_opts = "f:r:"; +static const char *short_opts = "dlf:r:"; static int cmd_dump(int argc, char **argv) { @@ -15,7 +17,7 @@ static int cmd_dump(int argc, char **argv) const char *root = NULL; int ret = EXIT_FAILURE; pkg_reader_t *rd; - int i; + int i, flags = 0; for (;;) { i = getopt_long(argc, argv, short_opts, long_opts, NULL); @@ -38,12 +40,21 @@ static int cmd_dump(int argc, char **argv) case 'r': root = optarg; break; + case 'l': + flags |= DUMP_TOC; + break; + case 'd': + flags |= DUMP_DEPS; + break; default: tell_read_help(argv[0]); return EXIT_FAILURE; } } + if (flags == 0) + flags = DUMP_ALL; + if (optind >= argc) { fputs("missing argument: package file\n", stderr); tell_read_help(argv[0]); @@ -57,12 +68,19 @@ static int cmd_dump(int argc, char **argv) if (optind < argc) fputs("warning: ignoring extra arguments\n", stderr); + if (flags & DUMP_DEPS) { + if (dump_header(rd, flags)) + goto out; + } + list = image_entry_list_from_package(rd); if (list == NULL) goto out; - if (dump_toc(list, root, format)) - goto out; + if (flags & DUMP_TOC) { + if (dump_toc(list, root, format)) + goto out; + } ret = EXIT_SUCCESS; out: @@ -81,6 +99,11 @@ static command_t dump = { "files, et cetera.\n" "\n" "Possible options:\n" +" --dependencies, -d Show dependency information of a package.\n" +"\n" +" --list-files, -l Produce a list of files. The format for the list\n" +" can be specified with --format.\n" +"\n" " --format, -f Specify what format to use for printing the table\n" " of contents. Default is a pretty printed, human\n" " readable version.\n" diff --git a/main/cmd/dump/dump.h b/main/cmd/dump/dump.h index e18bd1a..720f618 100644 --- a/main/cmd/dump/dump.h +++ b/main/cmd/dump/dump.h @@ -19,6 +19,14 @@ typedef enum { TOC_FORMAT_INITRD = 2, } TOC_FORMAT; +typedef enum { + DUMP_TOC = 0x01, + DUMP_DEPS = 0x02, + DUMP_ALL = (DUMP_TOC | DUMP_DEPS), +} DUMP_FLAGS; + int dump_toc(image_entry_t *list, const char *root, TOC_FORMAT format); +int dump_header(pkg_reader_t *pkg, int flags); + #endif /* DUMP_H */ diff --git a/main/cmd/dump/dump_header.c b/main/cmd/dump/dump_header.c new file mode 100644 index 0000000..4b8f61b --- /dev/null +++ b/main/cmd/dump/dump_header.c @@ -0,0 +1,54 @@ +#include "dump.h" + +static const char *dependency_type[] = { + [PKG_DEPENDENCY_REQUIRES] = "requires", +}; + +int dump_header(pkg_reader_t *pkg, int flags) +{ + pkg_dependency_t dep; + uint8_t buffer[257]; + pkg_header_t hdr; + ssize_t ret; + uint16_t i; + + ret = pkg_reader_read_payload(pkg, &hdr, sizeof(hdr)); + if (ret < 0) + return -1; + if ((size_t)ret < sizeof(hdr)) + goto fail_trunc; + + hdr.num_depends = le16toh(hdr.num_depends); + + if (flags & DUMP_DEPS) { + printf("Dependencies:\n"); + + for (i = 0; i < hdr.num_depends; ++i) { + ret = pkg_reader_read_payload(pkg, &dep, sizeof(dep)); + if (ret < 0) + return -1; + if ((size_t)ret < sizeof(hdr)) + goto fail_trunc; + + ret = pkg_reader_read_payload(pkg, buffer, + dep.name_length); + if (ret < 0) + return -1; + if ((size_t)ret < dep.name_length) + goto fail_trunc; + + buffer[dep.name_length] = '\0'; + + printf("\t%s: %s\n", dependency_type[dep.type], + (char *)buffer); + } + + fputc('\n', stdout); + } + + return 0; +fail_trunc: + fprintf(stderr, "%s: truncated header record\n", + pkg_reader_get_filename(pkg)); + return -1; +}