Initializes the build environment
*/
static int pakfire_build_init(struct pakfire_build* build) {
+ FILE* f = NULL;
char path[PATH_MAX];
int r;
// Extract snapshot
if (!pakfire_build_has_flag(build, PAKFIRE_BUILD_DISABLE_SNAPSHOT)) {
+ // Open the snapshot
+ f = fopen(path, "r");
+
// Try restoring the snapshot
- r = pakfire_snapshot_restore(build->pakfire, path);
- if (r && errno != ENOENT)
- return r;
+ if (f) {
+ r = pakfire_snapshot_restore(build->pakfire, f);
+ fclose(f);
+
+ // Exit on error
+ if (r)
+ return r;
+ }
}
// Install or update any build dependencies
// Update the snapshot if there were changes
if (!pakfire_build_has_flag(build, PAKFIRE_BUILD_DISABLE_SNAPSHOT) && snapshot_needs_update) {
+ // Open snapshot file for writing
+ f = fopen(path, "w");
+ if (!f) {
+ ERROR(build->pakfire, "Could not open snapshot file for writing: %m\n");
+ return 1;
+ }
+
// Create a new snapshot
- r = pakfire_snapshot_create(build->pakfire, path);
+ r = pakfire_snapshot_create(build->pakfire, f);
+ fclose(f);
+
if (r)
return r;
}
#include <pakfire/snapshot.h>
#include <pakfire/util.h>
-static struct archive* pakfire_snapshot_create_archive(
- struct pakfire* pakfire, const char* path) {
+static struct archive* pakfire_snapshot_create_archive(struct pakfire* pakfire, FILE* f) {
struct archive* a = archive_write_new();
if (!a) {
ERROR(pakfire, "archive_write_new() failed\n");
archive_write_set_bytes_in_last_block(a, 1);
// Write archive to file
- r = archive_write_open_filename(a, path);
+ r = archive_write_open_FILE(a, f);
if (r)
goto ERROR;
return NULL;
}
-int pakfire_snapshot_create(struct pakfire* pakfire, const char* path) {
+int pakfire_snapshot_create(struct pakfire* pakfire, FILE* snapshot) {
struct pakfire_filelist* filelist = NULL;
struct archive* archive = NULL;
int r = 1;
- // Check input
- if (!path) {
+ if (!snapshot) {
errno = EINVAL;
return 1;
}
const char* root = pakfire_get_path(pakfire);
- INFO(pakfire, "Creating snapshot of %s to %s...\n", root, path);
+ INFO(pakfire, "Creating snapshot of %s...\n", root);
// Scan for all files
r = pakfire_filelist_scan(filelist, root, NULL, NULL);
goto ERROR;
}
- archive = pakfire_snapshot_create_archive(pakfire, path);
+ // Create a new archive
+ archive = pakfire_snapshot_create_archive(pakfire, snapshot);
if (!archive) {
ERROR(pakfire, "Could not open archive for writing\n");
goto ERROR;
return r;
}
-static int pakfire_snapshot_extract(struct pakfire* pakfire, const char* path) {
+static int pakfire_snapshot_extract(struct pakfire* pakfire, FILE* f) {
struct stat st;
int r = 1;
- // Check input
- if (!path) {
- errno = EINVAL;
- return 1;
- }
-
// Stat the input file
- r = stat(path, &st);
+ r = fstat(fileno(f), &st);
if (r) {
ERROR(pakfire, "Could not stat snapshot: %m\n");
return 1;
archive_read_support_filter_zstd(archive);
// Open the given file for reading
- r = archive_read_open_filename(archive, path, PAKFIRE_BUFFER_SIZE);
+ r = archive_read_open_FILE(archive, f);
if (r) {
ERROR(pakfire, "Could not open archive: %s\n", archive_error_string(archive));
goto ERROR;
return r;
}
-int pakfire_snapshot_restore(struct pakfire* pakfire, const char* path) {
+int pakfire_snapshot_restore(struct pakfire* pakfire, FILE* f) {
struct pakfire_db* db = NULL;
- // Check input
- if (!path) {
+ if (!f) {
errno = EINVAL;
return 1;
}
// Extract the archive
- int r = pakfire_snapshot_extract(pakfire, path);
+ int r = pakfire_snapshot_extract(pakfire, f);
if (r)
return r;
#############################################################################*/
#include <stdio.h>
-#include <sys/stat.h>
#include <pakfire/snapshot.h>
#include "../testsuite.h"
static int test_create_and_restore(const struct test* t) {
- char* path = NULL;
- struct stat st;
int r = EXIT_FAILURE;
// Create a temporary file
- FILE* f = test_mktemp(&path);
+ FILE* f = test_mktemp(NULL);
ASSERT(f);
// Create the snapshot
- ASSERT_SUCCESS(pakfire_snapshot_create(t->pakfire, path));
+ ASSERT_SUCCESS(pakfire_snapshot_create(t->pakfire, f));
- // Stat the file
- ASSERT_SUCCESS(stat(path, &st));
+ // Determine the size of the snapshot
+ size_t size = ftell(f);
- LOG("Snapshot has a size of %jd bytes\n", st.st_size);
+ LOG("Snapshot has a size of %jd bytes\n", size);
// Check if the snapshot has something in it
- ASSERT(st.st_size >= 1024);
+ ASSERT(size >= 1024);
+
+ // Rewind the file descriptor to the beginning
+ rewind(f);
// Perform a restore
- ASSERT_SUCCESS(pakfire_snapshot_restore(t->pakfire, path));
+ ASSERT_SUCCESS(pakfire_snapshot_restore(t->pakfire, f));
// Everything passed
r = EXIT_SUCCESS;