}
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,