int pakfire_archive_copy_data(struct pakfire* pakfire,
struct archive* src, struct archive* dst);
+int pakfire_archive_copy_data_from_file(struct pakfire* pakfire,
+ struct archive* archive, FILE* f);
int pakfire_archive_copy_data_to_buffer(struct pakfire* pakfire, struct archive* a,
struct archive_entry* entry, char** data, size_t* data_size);
#include <pakfire/pwd.h>
#include <pakfire/util.h>
-#define BUFFER_SIZE 64 * 1024
-
struct pakfire_packager {
struct pakfire* pakfire;
int nrefs;
return packager->filename;
}
-static int pakfire_packager_copy_data(struct pakfire_packager* packager,
- struct archive* a, FILE* f) {
- char buffer[BUFFER_SIZE];
-
- rewind(f);
-
- while (!feof(f)) {
- // Read a block from file
- size_t bytes_read = fread(buffer, 1, sizeof(buffer), f);
-
- // Check if any error occured
- if (ferror(f)) {
- ERROR(packager->pakfire, "Error reading from file: %m\n");
- return 1;
- }
-
- // Write the block to the archive
- ssize_t bytes_written = archive_write_data(a, buffer, bytes_read);
- if (bytes_written < 0) {
- ERROR(packager->pakfire, "Error writing data to archive: %s\n",
- archive_error_string(a));
- return 1;
- }
- }
-
- return 0;
-}
-
static struct archive_entry* pakfire_packager_create_file(
struct pakfire_packager* packager, const char* filename, size_t size, mode_t mode) {
// Create a new file entry
}
// Copy data
- r = pakfire_packager_copy_data(packager, a, f);
+ r = pakfire_archive_copy_data_from_file(packager->pakfire, a, f);
if (r) {
const char* error = archive_error_string(a);
if (!error)
goto ERROR;
}
- r = pakfire_packager_copy_data(packager, mtree, f);
+ r = pakfire_archive_copy_data_from_file(packager->pakfire, mtree, f);
if (r) {
ERROR(packager->pakfire, "Error copying data to mtree: %s\n",
archive_error_string(mtree));
// Copy the data of regular files
if (archive_entry_filetype(entry) == AE_IFREG) {
// Copy the payload into the archive
- r = pakfire_packager_copy_data(packager, packager->payload, f);
+ r = pakfire_archive_copy_data_from_file(packager->pakfire, packager->payload, f);
if (r)
goto ERROR;
// Copy the payload into the mtree for hashing
- r = pakfire_packager_copy_data(packager, packager->mtree, f);
+ r = pakfire_archive_copy_data_from_file(packager->pakfire, packager->mtree, f);
if (r)
goto ERROR;
}
}
static int pakfire_copy(struct pakfire* pakfire, const char* src, const char* dst) {
- char buffer[512 * 1024];
struct archive* reader = NULL;
struct archive* writer = NULL;
struct archive_entry* entry = NULL;
DEBUG(pakfire, "Copying %s to %s\n", src, dst);
+ // Open the source file
+ f = fopen(src, "r");
+ if (!f) {
+ ERROR(pakfire, "Could not open %s: %m\n", src);
+ goto ERROR;
+ }
+
// Allocate reader
reader = archive_read_disk_new();
if (!reader)
// Set the source path
archive_entry_copy_sourcepath(entry, src);
+ // Set the destination path
+ archive_entry_set_pathname(entry, dst);
+
// Read everything from source file
- r = archive_read_disk_entry_from_file(reader, entry, -1, NULL);
+ r = archive_read_disk_entry_from_file(reader, entry, fileno(f), NULL);
if (r) {
ERROR(pakfire, "Could not read from %s: %m\n", src);
goto ERROR;
}
- // Set the destination path
- archive_entry_set_pathname(entry, dst);
-
// Write file to destination
r = archive_write_header(writer, entry);
if (r) {
// Copy payload
if (archive_entry_filetype(entry) == AE_IFREG) {
- f = fopen(src, "r");
- if (!f) {
- r = 1;
+ r = pakfire_archive_copy_data_from_file(pakfire, writer, f);
+ if (r)
goto ERROR;
- }
-
- while (!feof(f)) {
- size_t bytes_read = fread(buffer, 1, sizeof(buffer), f);
-
- // Check if any error occured
- if (ferror(f)) {
- ERROR(pakfire, "Error reading from file: %m\n");
- r = 1;
- goto ERROR;
- }
-
- ssize_t bytes_written = archive_write_data(writer, buffer, bytes_read);
- if (bytes_written < 0) {
- ERROR(pakfire, "Error writing data: %s\n", archive_error_string(writer));
- r = 1;
- goto ERROR;
- }
- }
}
ERROR:
#include <pakfire/package.h>
#include <pakfire/util.h>
+#define BUFFER_SIZE 64 * 1024
#define NSEC_PER_SEC 1000000000
static const struct pakfire_rich_operation {
}
}
+int pakfire_archive_copy_data_from_file(struct pakfire* pakfire,
+ struct archive* archive, FILE* f) {
+ char buffer[BUFFER_SIZE];
+
+ size_t bytes_read = 0;
+ ssize_t bytes_written = 0;
+
+ // Read file from the very beginning - also allows calling this multiple times
+ rewind(f);
+
+ // Loop through the entire length of the file
+ while (!feof(f)) {
+ // Read a block from file
+ bytes_read = fread(buffer, 1, sizeof(buffer), f);
+
+ // Check if any error occured
+ if (ferror(f)) {
+ ERROR(pakfire, "Read error: %m\n");
+ return 1;
+ }
+
+ // Write the block to the archive
+ bytes_written = archive_write_data(archive, buffer, bytes_read);
+ if (bytes_written < 0) {
+ ERROR(pakfire, "Write error: %s\n", archive_error_string(archive));
+ return 1;
+ }
+ }
+
+ return 0;
+}
+
int pakfire_archive_copy_data_to_buffer(struct pakfire* pakfire, struct archive* a,
struct archive_entry* entry, char** data, size_t* data_size) {
*data = NULL;