/* * Copyright (C) 2023 Patrick McDermott * * This file is part of opkg-opkg. * * opkg-opkg is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * opkg-opkg is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with opkg-opkg. If not, see . */ #include #include #include #include #include #include "defs.h" #include "gzip.h" #include "i18n.h" #include "opk.h" #include "ustar.h" struct _opkg_opk_opk_seek_name { const char *name; int found; struct _opkg_opk_opk_seek_name *next; }; struct opkg_opk_opk { struct _opkg_opk_opk_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; 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; } static int _opkg_opk_opk_add_seek_name(struct _opkg_opk_opk_seek_name **names, const char *name) { struct _opkg_opk_opk_seek_name *seek_name; seek_name = malloc(sizeof(*seek_name)); if (seek_name == NULL) { return OPKG_OPK_ERROR; } seek_name->name = name; seek_name->found = 0; seek_name->next = *names; *names = seek_name; return OPKG_OPK_OK; } int opkg_opk_opk_print_control(struct opkg_opk_opk *opk, const char *name) { return _opkg_opk_opk_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) { struct opkg_opk_opk *opk = user_data; *buffer = opk->file_buffer; *size = fread(opk->file_buffer, 1, sizeof(opk->file_buffer), opk->file); if (feof(opk->file)) { return OPKG_OPK_END; } else if (ferror(opk->file) || *size == 0) { return OPKG_OPK_ERROR; } else { return OPKG_OPK_OK; } } static int _opkg_opk_opk_init_inner(struct opkg_opk_opk *opk) { /* Initialize inner gzip decompressor. */ opk->inner_gzip = opkg_opk_gzip_init_read( (opkg_opk_gzip_read_func *) &opkg_opk_ustar_read, opk->outer_ustar); if (opk->inner_gzip == NULL) { fputs(_("Error: Failed to initialize\n"), stderr); return OPKG_OPK_ERROR; } /* Initialize inner ustar unarchiver. */ opk->inner_ustar = opkg_opk_ustar_init(opk->inner_gzip); if (opk->inner_ustar == NULL) { fputs(_("Error: Failed to initialize\n"), stderr); opkg_opk_gzip_free(opk->inner_gzip); return OPKG_OPK_ERROR; } return OPKG_OPK_OK; } static void _opkg_opk_opk_free_inner(struct opkg_opk_opk *opk) { opkg_opk_ustar_free(opk->inner_ustar); opkg_opk_gzip_free(opk->inner_gzip); } static int _opkg_opk_opk_seek(struct opkg_opk_ustar *ustar, struct _opkg_opk_opk_seek_name *names) { struct opkg_opk_ustar_member *member; int found; int found_all; struct _opkg_opk_opk_seek_name *seek_name; for (;;) { /* Get next header record. */ if (opkg_opk_ustar_list(ustar, &member) != OPKG_OPK_OK) { return OPKG_OPK_ERROR; /* Error or end (not found) */ } /* Check each requested name. */ found = 0; found_all = 1; for (seek_name = names; seek_name != NULL; seek_name = seek_name->next) { if (seek_name->found == 1) { continue; /* Previously found this member */ } if (strcmp(member->name, seek_name->name) == 0) { if (found == 0) { seek_name->found = 1; found = 1; continue; } } if (member->name[0] == '.' && member->name[1] == '/' && strcmp(member->name + 2, seek_name->name) == 0) { if (found == 0) { seek_name->found = 1; found = 1; continue; } } found_all = 0; } free(member); if (found == 1) { if (found_all == 1) { /* All requested members found */ return OPKG_OPK_END; } return OPKG_OPK_OK; /* Member found, but more remain */ } } return OPKG_OPK_ERROR; /* Member not found */ } 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) != OPKG_OPK_OK) { return OPKG_OPK_ERROR; } while ((ret_seek = _opkg_opk_opk_seek(opk->inner_ustar, opk->print_control)) != OPKG_OPK_ERROR) { 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 control file\n") , stderr); _opkg_opk_opk_free_inner(opk); return OPKG_OPK_ERROR; } } if (ret_read == OPKG_OPK_ERROR) { fputs(_("Error: Failed to read control file\n"), stderr); _opkg_opk_opk_free_inner(opk); return OPKG_OPK_ERROR; } opk->previously_printed = 1; if (ret_seek == OPKG_OPK_END) { break; } } if (ret_seek == OPKG_OPK_ERROR) { fputs(_("Error: Failed to find control file\n"), stderr); _opkg_opk_opk_free_inner(opk); return OPKG_OPK_ERROR; } _opkg_opk_opk_free_inner(opk); return OPKG_OPK_OK; } static int _opkg_opk_opk_list_members(struct opkg_opk_opk *opk) { struct opkg_opk_ustar_member *head; struct opkg_opk_ustar_member *tail; struct opkg_opk_ustar_member *member; int ret; size_t uname_len; size_t uname_len_max; size_t gname_len; size_t gname_len_max; uint64_t size_max; long int size_len_max; char fmt[28]; /* "%c%s %-32s/%-32s %11d %s %s" */ size_t len; char mode[10]; char mtime[20]; if (opk->list_data == 0) { /* Not listing data. */ return OPKG_OPK_OK; } if (opk->previously_printed == 1) { puts(""); } if (_opkg_opk_opk_init_inner(opk) != OPKG_OPK_OK) { return OPKG_OPK_ERROR; } /* Build singly-linked list and find maximum column widths. */ head = NULL; tail = NULL; uname_len_max = 0; gname_len_max = 0; 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; } 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; } } if (ret == OPKG_OPK_ERROR) { fputs(_("Error: Failed to list data files\n"), stderr); _opkg_opk_opk_free_inner(opk); return OPKG_OPK_ERROR; } if (tail != NULL) { tail->next = NULL; } /* Print and free members. */ ret = OPKG_OPK_OK; size_len_max = lrint(ceil(log10(size_max))); snprintf(fmt, sizeof(fmt), "%%c%%s %%-%zus/%%-%zus %%%lid %%s %%s", uname_len_max, gname_len_max, size_len_max); len = 34 + uname_len_max + gname_len_max + size_len_max; for (member = head; member != NULL;) { if (member->mode & 00400) mode[0] = 'r'; else mode[0] = '-'; if (member->mode & 00200) mode[1] = 'w'; else mode[1] = '-'; if (member->mode & 00100) mode[2] = 'x'; else mode[2] = '-'; if (member->mode & 04000) mode[2] = 's'; if (member->mode & 00040) mode[3] = 'r'; else mode[3] = '-'; if (member->mode & 00020) mode[4] = 'w'; else mode[4] = '-'; if (member->mode & 00010) mode[5] = 'x'; else mode[5] = '-'; if (member->mode & 02000) mode[5] = 's'; if (member->mode & 00004) mode[6] = 'r'; else mode[6] = '-'; if (member->mode & 00002) mode[7] = 'w'; else mode[7] = '-'; if (member->mode & 00001) mode[8] = 'x'; else mode[8] = '-'; if (member->mode & 01000) mode[8] = 't'; mode[9] = '\0'; strftime(mtime, sizeof(mtime), "%Y-%m-%d %H:%M:%S", localtime(&member->mtime)); #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wformat-nonliteral" if (printf(fmt, member->type, mode, member->uname, member->gname, member->size, mtime, member->name) != (int) (len + strlen(member->name))) { ret = OPKG_OPK_ERROR; } #pragma GCC diagnostic pop if (member->type == 'l' && printf(" -> %s", member->linkname) != (int) (strlen(member->linkname) + 4)) { ret = OPKG_OPK_ERROR; } if (puts("") == EOF) { ret = OPKG_OPK_ERROR; } head = member; member = member->next; free(head); } opk->previously_printed = 1; _opkg_opk_opk_free_inner(opk); return ret; } int opkg_opk_opk_read(struct opkg_opk_opk *opk, const char *file_name) { int ret; struct opkg_opk_ustar_member *member; char *version_buffer; size_t version_size; 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_list(opk->outer_ustar, &member) != OPKG_OPK_OK || strcmp(member->name, "debian-binary") != 0) { free(member); fputs(_("Error: Failed to find \"debian-binary\" in archive\n"), stderr); ret = OPKG_OPK_ERROR; goto out3; } free(member); if (opkg_opk_ustar_read(opk->outer_ustar, &version_buffer, &version_size) != OPKG_OPK_OK) { fputs(_("Error: Failed to read \"debian-binary\" in archive\n"), stderr); ret = OPKG_OPK_ERROR; goto out3; } if (version_size < 4 || strncmp(version_buffer, "2.", 2) != 0) { fputs(_("Error: Unsupported package version\n"), stderr); ret = OPKG_OPK_ERROR; goto out3; } /* Read control archive. */ if (opkg_opk_ustar_list(opk->outer_ustar, &member) != OPKG_OPK_OK || strcmp(member->name, "control.tar.gz") != 0) { free(member); fputs(_("Error: Failed to find \"control.tar.gz\" in archive\n") , stderr); return OPKG_OPK_ERROR; } free(member); if (_opkg_opk_opk_read_control(opk) != OPKG_OPK_OK) { ret = OPKG_OPK_ERROR; goto out3; } /* Read data archive. */ if (opkg_opk_ustar_list(opk->outer_ustar, &member) != OPKG_OPK_OK || strcmp(member->name, "data.tar.gz") != 0) { free(member); fputs(_("Error: Failed to find \"data.tar.gz\" in archive\n"), stderr); return OPKG_OPK_ERROR; } free(member); 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) { struct _opkg_opk_opk_seek_name *name; while (opk->print_control != NULL) { name = opk->print_control; opk->print_control = opk->print_control->next; free(name); } free(opk); }