From: Michael Tremer Date: Fri, 20 Oct 2023 12:30:32 +0000 (+0000) Subject: archive: Refactor function to read data into a buffer X-Git-Tag: 0.9.30~1413 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=105372c433c6f65dbe93c84c56378e9f5eb63673;p=pakfire.git archive: Refactor function to read data into a buffer Signed-off-by: Michael Tremer --- diff --git a/src/libpakfire/archive.c b/src/libpakfire/archive.c index 368320f47..984d4d7de 100644 --- a/src/libpakfire/archive.c +++ b/src/libpakfire/archive.c @@ -342,29 +342,40 @@ static int pakfire_archive_parse_scriptlet(struct pakfire_archive* archive, } static int pakfire_archive_copy_data_to_buffer(struct pakfire_archive* archive, - struct archive* a, struct archive_entry* entry, char** data, size_t* data_size) { - *data = NULL; - *data_size = 0; + struct archive* a, struct archive_entry* entry, char** data, size_t* length) { + size_t bytes_read = 0; + char* buffer = NULL; + int r; + // Fetch how large the buffer needs to be size_t required_size = archive_entry_size(entry); if (!required_size) return 0; // Allocate a block of the required size - *data = calloc(1, required_size + 1); - if (!*data) - return ENOMEM; + buffer = calloc(1, required_size + 1); + if (!buffer) + return -errno; - ssize_t bytes_read = archive_read_data(a, *data, required_size); - if (bytes_read < 0) { + // Read the data into the buffer + bytes_read = archive_read_data(a, buffer, required_size); + if (bytes_read < required_size) { CTX_ERROR(archive->ctx, "Could not read from archive: %s\n", archive_error_string(a)); - free(*data); - return 1; + r = -errno; + goto ERROR; } - *data_size = bytes_read; + // Return the output + *data = buffer; + *length = bytes_read; return 0; + +ERROR: + if (buffer) + free(buffer); + + return r; } static int __pakfire_archive_read_metadata(struct pakfire_ctx* ctx, struct archive* a,