diff options
author | Patrick McDermott <patrick.mcdermott@libiquity.com> | 2023-04-29 00:40:24 (EDT) |
---|---|---|
committer | Patrick McDermott <patrick.mcdermott@libiquity.com> | 2023-04-29 01:14:38 (EDT) |
commit | 05574410807fe5834200111f091f48d099326c67 (patch) | |
tree | 89f95377022138e8347a136d2858d3085f512268 | |
parent | 558630f3d34a8f8aa54fe6821980de3ba1b6668e (diff) |
opk: Print data files
-rw-r--r-- | src/opk.c | 107 | ||||
-rw-r--r-- | src/opk.h | 13 |
2 files changed, 99 insertions, 21 deletions
@@ -37,6 +37,8 @@ struct _opkg_opk_opk_seek_name { struct opkg_opk_opk { struct _opkg_opk_opk_seek_name *print_control_head; struct _opkg_opk_opk_seek_name *print_control_tail; + struct _opkg_opk_opk_seek_name *print_data_head; + struct _opkg_opk_opk_seek_name *print_data_tail; int list_control; int list_data; FILE *file; @@ -60,6 +62,8 @@ opkg_opk_opk_init(void) opk->print_control_head = NULL; opk->print_control_tail = NULL; + opk->print_data_head = NULL; + opk->print_data_tail = NULL; opk->list_control = 0; opk->list_data = 0; opk->previously_printed = 0; @@ -104,6 +108,13 @@ opkg_opk_opk_print_control(struct opkg_opk_opk *opk, const char *name) } int +opkg_opk_opk_print_data(struct opkg_opk_opk *opk, const char *name) +{ + return _opkg_opk_opk_add_seek_name(&opk->print_data_head, + &opk->print_data_tail, name); +} + +int opkg_opk_opk_list_control(struct opkg_opk_opk *opk) { opk->list_control = 1; @@ -288,7 +299,7 @@ _opkg_opk_opk_read_control(struct opkg_opk_opk *opk) } static int -_opkg_opk_opk_list_members(struct opkg_opk_opk *opk) +_opkg_opk_opk_read_data(struct opkg_opk_opk *opk) { struct opkg_opk_ustar_member *head; struct opkg_opk_ustar_member *tail; @@ -299,6 +310,9 @@ _opkg_opk_opk_list_members(struct opkg_opk_opk *opk) size_t gname_len; size_t gname_len_max; uint64_t size_max; + int ret_read; + char *buffer; + size_t size; long int size_len_max; char fmt[28]; /* "%c%s %-32s/%-32s %11d %s %s" */ @@ -306,15 +320,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. */ + if (opk->list_data == 0 && opk->print_data_head == NULL) { + /* Not listing or printing any data files. */ return OPKG_OPK_OK; } - if (opk->previously_printed == 1) { - puts(""); - } - if (_opkg_opk_opk_init_inner(opk) != OPKG_OPK_OK) { return OPKG_OPK_ERROR; } @@ -327,23 +337,65 @@ _opkg_opk_opk_list_members(struct opkg_opk_opk *opk) size_max = 0; while ((ret = opkg_opk_ustar_list(opk->inner_ustar, &member)) == OPKG_OPK_OK) { - if (head == NULL) { - head = member; - } else { - tail->next = member; + if (opk->list_data > 0) { + if (head == NULL) { + head = member; + } else { + tail->next = member; + } + tail = member; + uname_len = strlen(member->uname); + if (uname_len > uname_len_max) { + uname_len_max = uname_len; + } + gname_len = strlen(member->gname); + if (gname_len > gname_len_max) { + gname_len_max = gname_len; + } + if (member->size > size_max) { + size_max = member->size; + } } - tail = member; - uname_len = strlen(member->uname); - if (uname_len > uname_len_max) { - uname_len_max = uname_len; + + if (opk->print_data_head == NULL) { + if (opk->list_data == 0) { + free(member); + } + continue; } - gname_len = strlen(member->gname); - if (gname_len > gname_len_max) { - gname_len_max = gname_len; + if (_opkg_opk_opk_check_name(member->name, + &opk->print_data_head, + &opk->print_data_tail) != + OPKG_OPK_OK) { + /* Name not requested for printing. */ + if (opk->list_data == 0) { + free(member); + } + continue; + } + if (opk->list_data == 0) { + free(member); } - if (member->size > size_max) { - size_max = member->size; + if (opk->previously_printed == 1) { + puts(""); } + while ((ret_read = opkg_opk_ustar_read(opk->inner_ustar, + &buffer, &size)) == OPKG_OPK_OK) + { + if (fwrite(buffer, 1, size, stdout) != size) { + fputs(_("Error: Failed to print data file\n") + , stderr); + _opkg_opk_opk_free_inner(opk); + return OPKG_OPK_ERROR; + } + } + if (ret_read == OPKG_OPK_ERROR) { + fputs(_("Error: Failed to read data file\n"), + stderr); + _opkg_opk_opk_free_inner(opk); + return OPKG_OPK_ERROR; + } + opk->previously_printed = 1; } if (ret == OPKG_OPK_ERROR) { fputs(_("Error: Failed to list data files\n"), stderr); @@ -354,7 +406,15 @@ _opkg_opk_opk_list_members(struct opkg_opk_opk *opk) tail->next = NULL; } + if (opk->list_data == 0) { + _opkg_opk_opk_free_inner(opk); + return OPKG_OPK_OK; + } + /* Print and free members. */ + if (opk->previously_printed == 1) { + puts(""); + } ret = OPKG_OPK_OK; size_len_max = lrint(ceil(log10(size_max))); snprintf(fmt, sizeof(fmt), "%%c%%s %%-%zus/%%-%zus %%%lid %%s %%s", @@ -488,7 +548,7 @@ opkg_opk_opk_read(struct opkg_opk_opk *opk, const char *file_name) return OPKG_OPK_ERROR; } free(member); - if (_opkg_opk_opk_list_members(opk) != OPKG_OPK_OK) { + if (_opkg_opk_opk_read_data(opk) != OPKG_OPK_OK) { ret = OPKG_OPK_ERROR; goto out3; } @@ -513,5 +573,10 @@ opkg_opk_opk_free(struct opkg_opk_opk *opk) opk->print_control_head = opk->print_control_head->next; free(name); } + while (opk->print_data_head != NULL) { + name = opk->print_data_head; + opk->print_data_head = opk->print_data_head->next; + free(name); + } free(opk); } @@ -47,6 +47,19 @@ int opkg_opk_opk_print_control(struct opkg_opk_opk *opk, const char *name); /* + * Adds a name to a list of data files to print when opkg_opk_opk_read() is + * called. + * Parameters: + * - opk: Package structure. + * - name: Name of data file to print. + * Returns: + * - OPKG_OPK_OK if the name was added to the list. + * - OPKG_OPK_ERROR on memory exhaustion. + */ +int +opkg_opk_opk_print_data(struct opkg_opk_opk *opk, const char *name); + +/* * Lists control files when opkg_opk_opk_read() is called. * Parameters: * - opk: Package structure. |