diff --git a/include/pkgio.h b/include/pkgio.h index 2f5a455..a7d78f9 100644 --- a/include/pkgio.h +++ b/include/pkgio.h @@ -13,6 +13,6 @@ enum { int pkg_unpack(int rootfd, int flags, pkg_reader_t *rd); -image_entry_t *image_entry_list_from_package(pkg_reader_t *pkg); +int image_entry_list_from_package(pkg_reader_t *pkg, image_entry_t ** list); #endif /* PKGIO_H */ diff --git a/main/cmd/dump/dump.c b/main/cmd/dump/dump.c index 9494fc2..c264d88 100644 --- a/main/cmd/dump/dump.c +++ b/main/cmd/dump/dump.c @@ -75,11 +75,10 @@ static int cmd_dump(int argc, char **argv) goto out; } - list = image_entry_list_from_package(rd); - if (list == NULL) + if (image_entry_list_from_package(rd, &list)) goto out; - if (flags & DUMP_TOC) { + if (flags & DUMP_TOC && list != NULL) { if (dump_toc(list, root, format)) goto out; } diff --git a/main/cmd/install/install.c b/main/cmd/install/install.c index 2e26114..001574f 100644 --- a/main/cmd/install/install.c +++ b/main/cmd/install/install.c @@ -58,13 +58,12 @@ static int list_files(int repofd, const char *rootdir, TOC_FORMAT format, if (rd == NULL) return -1; - toc = image_entry_list_from_package(rd); - if (toc == NULL) { + if (image_entry_list_from_package(rd, &toc)) { pkg_reader_close(rd); return -1; } - if (dump_toc(toc, rootdir, format)) { + if (toc != NULL && dump_toc(toc, rootdir, format)) { image_entry_free_list(toc); pkg_reader_close(rd); return -1; diff --git a/main/cmd/pack/filelist.c b/main/cmd/pack/filelist.c index e4dbb8d..73211b4 100644 --- a/main/cmd/pack/filelist.c +++ b/main/cmd/pack/filelist.c @@ -258,27 +258,49 @@ static const keyword_handler_t line_hooks[] = { #define NUM_LINE_HOOKS (sizeof(line_hooks) / sizeof(line_hooks[0])) -image_entry_t *filelist_read(const char *filename) +static int alloc_file_ids(image_entry_t *list) +{ + image_entry_t *ent; + uint64_t file_id = 0; + + for (ent = list; ent != NULL; ent = ent->next) { + if (!S_ISREG(ent->mode)) + continue; + + if (file_id > 0x00000000FFFFFFFFUL) { + fprintf(stderr, "too many input files\n"); + return -1; + } + + ent->data.file.id = file_id++; + } + + return 0; +} + +int filelist_read(const char *filename, image_entry_t **out) { image_entry_t *list = NULL; input_file_t f; if (open_file(&f, filename)) - return NULL; + return -1; if (process_file(&f, line_hooks, NUM_LINE_HOOKS, &list)) goto fail; - if (list == NULL) { - fprintf(stderr, "%s: does not contain any entries\n", - f.filename); - goto fail; + if (list != NULL) { + list = image_entry_sort(list); + + if (alloc_file_ids(list)) + goto fail; } cleanup_file(&f); - return list; + *out = list; + return 0; fail: cleanup_file(&f); image_entry_free_list(list); - return NULL; + return -1; } diff --git a/main/cmd/pack/pack.c b/main/cmd/pack/pack.c index 69a63c7..cfa07f0 100644 --- a/main/cmd/pack/pack.c +++ b/main/cmd/pack/pack.c @@ -40,26 +40,6 @@ static compressor_t *try_get_compressor(const char *name) return cmp; } -static int alloc_file_ids(image_entry_t *list) -{ - image_entry_t *ent; - uint64_t file_id = 0; - - for (ent = list; ent != NULL; ent = ent->next) { - if (!S_ISREG(ent->mode)) - continue; - - if (file_id > 0x00000000FFFFFFFFUL) { - fprintf(stderr, "too many input files\n"); - return -1; - } - - ent->data.file.id = file_id++; - } - - return 0; -} - static pkg_writer_t *open_writer(pkg_desc_t *desc, const char *repodir) { char *path; @@ -77,7 +57,7 @@ static int cmd_pack(int argc, char **argv) { const char *filelist = NULL, *repodir = NULL, *descfile = NULL; compressor_t *cmp_toc, *cmp_files; - image_entry_t *list; + image_entry_t *list = NULL; pkg_writer_t *wr; pkg_desc_t desc; int i; @@ -133,27 +113,15 @@ static int cmd_pack(int argc, char **argv) return EXIT_FAILURE; } - if (filelist == NULL) { - fputs("missing argument: input file list\n", stderr); - tell_read_help(argv[0]); - return EXIT_FAILURE; - } - if (optind < argc) fputs("warning: ignoring extra arguments\n", stderr); if (desc_read(descfile, &desc)) return EXIT_FAILURE; - list = filelist_read(filelist); - if (list == NULL) + if (filelist != NULL && filelist_read(filelist, &list)) goto fail_desc; - list = image_entry_sort(list); - - if (alloc_file_ids(list)) - goto fail_fp; - wr = open_writer(&desc, repodir); if (wr == NULL) goto fail_fp; @@ -161,11 +129,13 @@ static int cmd_pack(int argc, char **argv) if (write_header_data(wr, &desc)) goto fail; - if (write_toc(wr, list, cmp_toc)) - goto fail; + if (list != NULL) { + if (write_toc(wr, list, cmp_toc)) + goto fail; - if (write_files(wr, list, cmp_files)) - goto fail; + if (write_files(wr, list, cmp_files)) + goto fail; + } pkg_writer_close(wr); image_entry_free_list(list); diff --git a/main/cmd/pack/pack.h b/main/cmd/pack/pack.h index ea7854f..0fb3526 100644 --- a/main/cmd/pack/pack.h +++ b/main/cmd/pack/pack.h @@ -42,7 +42,7 @@ int filelist_mkslink(input_file_t *f, void *obj); int filelist_mkfile(input_file_t *f, void *obj); -image_entry_t *filelist_read(const char *filename); +int filelist_read(const char *filename, image_entry_t **out); int write_toc(pkg_writer_t *wr, image_entry_t *list, compressor_t *cmp); diff --git a/main/pkg_unpack.c b/main/pkg_unpack.c index d7604af..86651fb 100644 --- a/main/pkg_unpack.c +++ b/main/pkg_unpack.c @@ -198,13 +198,15 @@ int pkg_unpack(int rootfd, int flags, pkg_reader_t *rd) record_t *hdr; int ret; - list = image_entry_list_from_package(rd); - if (list == NULL) + if (image_entry_list_from_package(rd, &list)) return -1; if (pkg_reader_rewind(rd)) goto fail; + if (list == NULL) + return 0; + if (create_hierarchy(rootfd, list, flags)) goto fail; diff --git a/main/pkgio_rd_image_entry.c b/main/pkgio_rd_image_entry.c index 8e70617..cdf6f7f 100644 --- a/main/pkgio_rd_image_entry.c +++ b/main/pkgio_rd_image_entry.c @@ -83,20 +83,28 @@ fail_trunc: return -1; } -image_entry_t *image_entry_list_from_package(pkg_reader_t *pkg) +int image_entry_list_from_package(pkg_reader_t *pkg, image_entry_t **out) { image_entry_t *list = NULL, *end = NULL, *imgent; toc_entry_t ent; record_t *hdr; ssize_t ret; char *path; + int status; if (pkg_reader_rewind(pkg)) - return NULL; + return -1; do { - if (pkg_reader_get_next_record(pkg) <= 0) - return NULL; + status = pkg_reader_get_next_record(pkg); + + if (status == 0) { + *out = NULL; + return 0; + } + + if (status < 0) + return -1; hdr = pkg_reader_current_record_header(pkg); } while (hdr->magic != PKG_MAGIC_TOC); @@ -170,7 +178,8 @@ image_entry_t *image_entry_list_from_package(pkg_reader_t *pkg) goto fail_multi; } - return image_entry_sort(list); + *out = image_entry_sort(list); + return 0; fail_oom: fputs("out of memory\n", stderr); goto fail; @@ -184,5 +193,5 @@ fail_multi: goto fail; fail: image_entry_free_list(list); - return NULL; + return -1; }