From 04f19bac2faf04b32f0c716c88d92bc2f27310c7 Mon Sep 17 00:00:00 2001 From: David Oberhollenzer Date: Wed, 30 Jan 2019 10:40:02 +0100 Subject: [PATCH] pack: read package name from description This commit changes the pack command to read the output package name from the description file instead of the command line. The command line argument is replaced with a package repository directory to which the package file is then written. Signed-off-by: David Oberhollenzer --- main/cmd/pack/desc.c | 25 +++++++++++++++++++++++++ main/cmd/pack/pack.c | 34 ++++++++++++++++++++++++---------- main/cmd/pack/pack.h | 1 + 3 files changed, 50 insertions(+), 10 deletions(-) diff --git a/main/cmd/pack/desc.c b/main/cmd/pack/desc.c index 23ab820..f73d8ce 100644 --- a/main/cmd/pack/desc.c +++ b/main/cmd/pack/desc.c @@ -41,11 +41,29 @@ static int handle_requires(input_file_t *f, pkg_desc_t *desc) return 0; } +static int handle_name(input_file_t *f, pkg_desc_t *desc) +{ + if (desc->name != NULL) { + fprintf(stderr, "%s: %zu: name redefined\n", + f->filename, f->linenum); + return -1; + } + + desc->name = strdup(f->line); + if (desc->name == NULL) { + fputs("out of memory\n", stderr); + return -1; + } + + return 0; +} + static const struct { const char *name; int (*handle)(input_file_t *f, pkg_desc_t *desc); } line_hooks[] = { { "requires", handle_requires }, + { "name", handle_name }, }; #define NUM_LINE_HOOKS (sizeof(line_hooks) / sizeof(line_hooks[0])) @@ -95,6 +113,11 @@ int desc_read(const char *path, pkg_desc_t *desc) goto fail; } + if (desc->name == NULL) { + fprintf(stderr, "%s: no name given in package description\n", + f.filename); + } + cleanup_file(&f); return 0; fail: @@ -112,4 +135,6 @@ void desc_free(pkg_desc_t *desc) free(dep); } + + free(desc->name); } diff --git a/main/cmd/pack/pack.c b/main/cmd/pack/pack.c index 360c5fd..470c355 100644 --- a/main/cmd/pack/pack.c +++ b/main/cmd/pack/pack.c @@ -5,11 +5,11 @@ static const struct option long_opts[] = { { "file-compressor", required_argument, NULL, 'f' }, { "description", required_argument, NULL, 'd' }, { "file-list", required_argument, NULL, 'l' }, - { "output", required_argument, NULL, 'o' }, + { "repo-dir", required_argument, NULL, 'r' }, { NULL, 0, NULL, 0 }, }; -static const char *short_opts = "t:f:l:o:d:"; +static const char *short_opts = "t:f:l:r:d:"; static compressor_t *get_default_compressor(void) { @@ -60,9 +60,22 @@ static int alloc_file_ids(image_entry_t *list) return 0; } +static pkg_writer_t *open_writer(pkg_desc_t *desc, const char *repodir) +{ + char *path; + + if (mkdir_p(repodir)) + return NULL; + + path = alloca(strlen(repodir) + strlen(desc->name) + 16); + sprintf(path, "%s/%s.pkg", repodir, desc->name); + + return pkg_writer_open(path); +} + static int cmd_pack(int argc, char **argv) { - const char *filelist = NULL, *filename = NULL, *descfile = NULL; + const char *filelist = NULL, *repodir = NULL, *descfile = NULL; compressor_t *cmp_toc, *cmp_files; image_entry_t *list; pkg_writer_t *wr; @@ -96,12 +109,12 @@ static int cmd_pack(int argc, char **argv) case 'l': filelist = optarg; break; - case 'o': - filename = optarg; - break; case 'd': descfile = optarg; break; + case 'r': + repodir = optarg; + break; default: tell_read_help(argv[0]); return EXIT_FAILURE; @@ -114,8 +127,8 @@ static int cmd_pack(int argc, char **argv) return EXIT_FAILURE; } - if (filename == NULL) { - fputs("missing argument: output package file\n", stderr); + if (repodir == NULL) { + fputs("missing argument: repository directory\n", stderr); tell_read_help(argv[0]); return EXIT_FAILURE; } @@ -141,7 +154,7 @@ static int cmd_pack(int argc, char **argv) if (alloc_file_ids(list)) goto fail_fp; - wr = pkg_writer_open(filename); + wr = open_writer(&desc, repodir); if (wr == NULL) goto fail_fp; @@ -175,7 +188,8 @@ static command_t pack = { "Read a list of files from the given lists file and generate a package.\n" "Possible options:\n" " --file-list, -l Specify a file containing a list of input files.\n" -" --output, -o Specify the path of the resulting package file.\n" +" --repo-dir, -r Specify the output repository path to store the\n" +" package in.\n" "\n" " --toc-compressor, -t \n" " --file-compressor, -f \n" diff --git a/main/cmd/pack/pack.h b/main/cmd/pack/pack.h index 2accccf..3e44648 100644 --- a/main/cmd/pack/pack.h +++ b/main/cmd/pack/pack.h @@ -36,6 +36,7 @@ typedef struct dependency_t { typedef struct { dependency_t *deps; + char *name; } pkg_desc_t; image_entry_t *filelist_mkdir(input_file_t *f);