]> git.ipfire.org Git - pakfire.git/commitdiff
archive: Refactor function to read data into a buffer
authorMichael Tremer <michael.tremer@ipfire.org>
Fri, 20 Oct 2023 12:30:32 +0000 (12:30 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Fri, 20 Oct 2023 12:30:32 +0000 (12:30 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/archive.c

index 368320f472ec08bb51ef83ce442ff9ef806a2cbe..984d4d7deb94fa1c3acc2ab82fb81a46567c279a 100644 (file)
@@ -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,