1
0
Fork 0
mirror of https://github.com/pygos/pkg-utils.git synced 2024-11-21 20:39:46 +01:00

Allow empty packages, i.e. packages not containing any files

Signed-off-by: David Oberhollenzer <goliath@infraroot.at>
This commit is contained in:
David Oberhollenzer 2019-02-07 23:14:45 +01:00
parent d55ee9bd72
commit 95c7302d25
8 changed files with 63 additions and 62 deletions

View file

@ -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 */

View file

@ -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;
}

View file

@ -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;

View file

@ -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;
}

View file

@ -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);

View file

@ -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);

View file

@ -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;

View file

@ -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;
}