From f6a6b1c9dae1538b9fed7ab52b5ffd31cc411326 Mon Sep 17 00:00:00 2001 From: Patrick McDermott Date: Fri, 28 Apr 2023 18:33:32 -0400 Subject: opk: Manage control file list and listing flag Instead of main() doing so. --- (limited to 'src/opk.c') diff --git a/src/opk.c b/src/opk.c index 982ad3f..a6cafce 100644 --- a/src/opk.c +++ b/src/opk.c @@ -29,17 +29,49 @@ #include "ustar.h" struct opkg_opk_opk { - FILE *file; - char file_buffer[8192]; - struct opkg_opk_gzip *outer_gzip; - struct opkg_opk_ustar *outer_ustar; - char *version_buffer; - size_t version_size; - struct opkg_opk_gzip *inner_gzip; - struct opkg_opk_ustar *inner_ustar; - int previously_printed; + struct opkg_opk_ustar_seek_name *print_control; + int list_data; + FILE *file; + char file_buffer[8192]; + struct opkg_opk_gzip *outer_gzip; + struct opkg_opk_ustar *outer_ustar; + char *version_buffer; + size_t version_size; + struct opkg_opk_gzip *inner_gzip; + struct opkg_opk_ustar *inner_ustar; + int previously_printed; }; +struct opkg_opk_opk * +opkg_opk_opk_init(void) +{ + struct opkg_opk_opk *opk; + + opk = malloc(sizeof(*opk)); + if (opk == NULL) { + return NULL; + } + + opk->print_control = NULL; + opk->list_data = 0; + opk->previously_printed = 0; + + return opk; +} + +int +opkg_opk_opk_print_control(struct opkg_opk_opk *opk, const char *name) +{ + return opkg_opk_ustar_add_seek_name(&opk->print_control, name); +} + +int +opkg_opk_opk_list_data(struct opkg_opk_opk *opk) +{ + opk->list_data = 1; + return OPKG_OPK_OK; +} + static int _opkg_opk_opk_file_read(void *user_data, char **buffer, size_t *size) { @@ -57,75 +89,6 @@ _opkg_opk_opk_file_read(void *user_data, char **buffer, size_t *size) } } -struct opkg_opk_opk * -opkg_opk_opk_init(const char *file_name) -{ - struct opkg_opk_opk *opk; - - opk = malloc(sizeof(*opk)); - if (opk == NULL) { - goto error0; - } - - /* Open outer archive. */ - opk->file = fopen(file_name, "rb"); - if (opk->file == NULL) { - fprintf(stderr, _("Error: Failed to open file \"%s\"\n"), - file_name); - goto error1; - } - - /* Initialize outer gzip decompressor. */ - opk->outer_gzip = opkg_opk_gzip_init_read(&_opkg_opk_opk_file_read, - opk); - if (opk->outer_gzip == NULL) { - fputs(_("Error: Failed to initialize\n"), stderr); - goto error2; - } - - /* Initialize outer ustar unarchiver. */ - opk->outer_ustar = opkg_opk_ustar_init(opk->outer_gzip); - if (opk->outer_ustar == NULL) { - fputs(_("Error: Failed to initialize\n"), stderr); - goto error3; - } - - /* Check package version. */ - if (opkg_opk_ustar_seek_one(opk->outer_ustar, "debian-binary") != - OPKG_OPK_OK) { - fputs(_("Error: Failed to find \"debian-binary\" in archive\n"), - stderr); - goto error4; - } - if (opkg_opk_ustar_read(opk->outer_ustar, - &opk->version_buffer, &opk->version_size) != - OPKG_OPK_OK) { - fputs(_("Error: Failed to read \"debian-binary\" in archive\n"), - stderr); - goto error4; - } - if (opk->version_size < 4 || strncmp(opk->version_buffer, "2.", 2) != 0) - { - fputs(_("Error: Unsupported package version\n"), stderr); - goto error4; - } - - opk->previously_printed = 0; - - return opk; - - error4: - opkg_opk_ustar_free(opk->outer_ustar); - error3: - opkg_opk_gzip_free(opk->outer_gzip); - error2: - fclose(opk->file); - error1: - free(opk); - error0: - return NULL; -} - static int _opkg_opk_opk_init_inner(struct opkg_opk_opk *opk, const char *member) { @@ -173,21 +136,26 @@ _opkg_opk_opk_free_inner(struct opkg_opk_opk *opk) opkg_opk_gzip_free(opk->inner_gzip); } -int -opkg_opk_opk_read_control(struct opkg_opk_opk *opk, - struct opkg_opk_ustar_seek_name *names) +static int +_opkg_opk_opk_read_control(struct opkg_opk_opk *opk) { char *buffer; size_t size; int ret_seek; int ret_read; + if (opk->print_control == NULL) { + /* No control files requested. */ + return OPKG_OPK_OK; + } + if (_opkg_opk_opk_init_inner(opk, "control.tar.gz") != OPKG_OPK_OK) { return OPKG_OPK_ERROR; } - while ((ret_seek = opkg_opk_ustar_seek(opk->inner_ustar, names)) != - OPKG_OPK_ERROR) { + while ((ret_seek = opkg_opk_ustar_seek(opk->inner_ustar, + opk->print_control)) != OPKG_OPK_ERROR) + { if (opk->previously_printed == 1) { puts(""); } @@ -223,8 +191,8 @@ opkg_opk_opk_read_control(struct opkg_opk_opk *opk, return OPKG_OPK_OK; } -int -opkg_opk_opk_list_members(struct opkg_opk_opk *opk) +static int +_opkg_opk_opk_list_members(struct opkg_opk_opk *opk) { struct opkg_opk_ustar_member *head; struct opkg_opk_ustar_member *tail; @@ -242,6 +210,11 @@ opkg_opk_opk_list_members(struct opkg_opk_opk *opk) char mode[10]; char mtime[20]; + if (opk->list_data == 0) { + /* Not listing data. */ + return OPKG_OPK_OK; + } + if (opk->previously_printed == 1) { puts(""); } @@ -335,11 +308,87 @@ opkg_opk_opk_list_members(struct opkg_opk_opk *opk) return ret; } -void -opkg_opk_opk_free(struct opkg_opk_opk *opk) +int +opkg_opk_opk_read(struct opkg_opk_opk *opk, const char *file_name) { + int ret; + + ret = OPKG_OPK_OK; + + /* Open outer archive. */ + opk->file = fopen(file_name, "rb"); + if (opk->file == NULL) { + fprintf(stderr, _("Error: Failed to open file \"%s\"\n"), + file_name); + ret = OPKG_OPK_ERROR; + goto out0; + } + + /* Initialize outer gzip decompressor. */ + opk->outer_gzip = opkg_opk_gzip_init_read(&_opkg_opk_opk_file_read, + opk); + if (opk->outer_gzip == NULL) { + fputs(_("Error: Failed to initialize\n"), stderr); + ret = OPKG_OPK_ERROR; + goto out1; + } + + /* Initialize outer ustar unarchiver. */ + opk->outer_ustar = opkg_opk_ustar_init(opk->outer_gzip); + if (opk->outer_ustar == NULL) { + fputs(_("Error: Failed to initialize\n"), stderr); + ret = OPKG_OPK_ERROR; + goto out2; + } + + /* Check package version. */ + if (opkg_opk_ustar_seek_one(opk->outer_ustar, "debian-binary") != + OPKG_OPK_OK) { + fputs(_("Error: Failed to find \"debian-binary\" in archive\n"), + stderr); + ret = OPKG_OPK_ERROR; + goto out3; + } + if (opkg_opk_ustar_read(opk->outer_ustar, + &opk->version_buffer, &opk->version_size) != + OPKG_OPK_OK) { + fputs(_("Error: Failed to read \"debian-binary\" in archive\n"), + stderr); + ret = OPKG_OPK_ERROR; + goto out3; + } + if (opk->version_size < 4 || strncmp(opk->version_buffer, "2.", 2) != 0) + { + fputs(_("Error: Unsupported package version\n"), stderr); + ret = OPKG_OPK_ERROR; + goto out3; + } + + /* Perform the configured read actions. */ + if (_opkg_opk_opk_read_control(opk) != OPKG_OPK_OK) { + ret = OPKG_OPK_ERROR; + goto out3; + } + if (_opkg_opk_opk_list_members(opk) != OPKG_OPK_OK) { + ret = OPKG_OPK_ERROR; + goto out3; + } + + out3: opkg_opk_ustar_free(opk->outer_ustar); + out2: opkg_opk_gzip_free(opk->outer_gzip); + out1: fclose(opk->file); + out0: + return ret; +} + +void +opkg_opk_opk_free(struct opkg_opk_opk *opk) +{ + if (opk->print_control != NULL) { + opkg_opk_ustar_free_seek_names(opk->print_control); + } free(opk); } -- cgit v0.9.1