#include <fcntl.h>
#include <linux/limits.h>
#include <stdlib.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <unistd.h>
#include <archive.h>
+#include <archive_entry.h>
#include <pakfire/logging.h>
#include <pakfire/package.h>
#include <pakfire/private.h>
#include <pakfire/types.h>
+#define BUFFER_SIZE 64 * 1024
+
struct pakfire_packager {
Pakfire pakfire;
int nrefs;
return NULL;
}
+
+PAKFIRE_EXPORT int pakfire_packager_add(struct pakfire_packager* packager,
+ const char* path) {
+ FILE* f = NULL;
+ struct stat st;
+ char buffer[BUFFER_SIZE];
+
+ // Check if path is set
+ if (!path)
+ return EINVAL;
+
+ // Stat the input file
+ int r = stat(path, &st);
+ if (r) {
+ ERROR(packager->pakfire, "stat() on %s failed: %s\n", path, strerror(errno));
+ return r;
+ }
+
+ // Create a new file entry
+ struct archive_entry* entry = archive_entry_new();
+ if (!entry)
+ return ENOMEM;
+
+ // Set path in archive
+ archive_entry_set_pathname(entry, path);
+
+ // Copy all attributes from stat()
+ archive_entry_copy_stat(entry, &st);
+
+ // Write the header
+ r = archive_write_header(packager->payload, entry);
+ if (r) {
+ ERROR(packager->pakfire, "Error writing file header: %s\n",
+ archive_error_string(packager->payload));
+ goto ERROR;
+ }
+
+ // Copy the data of regular files
+ if (archive_entry_filetype(entry) == AE_IFREG) {
+ f = fopen(path, "r");
+ if (!f) {
+ ERROR(packager->pakfire, "Could not open %s: %s\n", path, strerror(errno));
+ r = errno;
+ goto ERROR;
+ }
+
+ while (!feof(f)) {
+ // Read a block from file
+ size_t bytes_read = fread(buffer, 1, sizeof(buffer), f);
+
+ if (ferror(f)) {
+ ERROR(packager->pakfire, "Error reading from file %s: %s\n",
+ path, strerror(errno));
+ r = errno;
+ goto ERROR;
+ }
+
+ // Write the block to the archive
+ ssize_t bytes_written = archive_write_data(packager->payload, buffer, bytes_read);
+ if (bytes_written < 0) {
+ ERROR(packager->pakfire, "Error writing data to archive: %s\n",
+ archive_error_string(packager->payload));
+ r = 1;
+ goto ERROR;
+ }
+ }
+ }
+
+ // Successful
+ r = 0;
+
+ERROR:
+ if (entry)
+ archive_entry_free(entry);
+
+ if (f)
+ fclose(f);
+
+ return r;
+}
#include <pakfire/package.h>
#include <pakfire/packager.h>
#include <pakfire/repo.h>
+#include <pakfire/util.h>
#include "../testsuite.h"
// Create packager
int r = pakfire_packager_create(&packager, pkg);
ASSERT(r == 0);
-
+
+ // Add a file to the package
+ char* path = pakfire_path_join(TEST_SRC_PATH, "data/beep-1.3-2.ip3.x86_64.pfm");
+ ASSERT(path);
+
+ r = pakfire_packager_add(packager, path);
+ ASSERT(r == 0);
+
// Cleanup
pakfire_packager_unref(packager);
pakfire_package_unref(pkg);
pakfire_repo_unref(repo);
+ free(path);
return EXIT_SUCCESS;
}