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
- if (f) {
- r = pakfire_snapshot_restore(build->pakfire, f);
- fclose(f);
-
- // Exit on error
- if (r)
- return r;
- }
+ r = pakfire_snapshot_restore(build->pakfire, path);
+ if (r && errno != ENOENT)
+ 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, f);
- fclose(f);
-
+ r = pakfire_snapshot_create(build->pakfire, path);
if (r)
return r;
}
return 1;
}
-static struct archive* pakfire_snapshot_create_archive(struct pakfire* pakfire, FILE* f) {
+static struct archive* pakfire_snapshot_create_archive(
+ struct pakfire* pakfire, const char* path) {
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_FILE(a, f);
+ r = archive_write_open_filename(a, path);
if (r)
goto ERROR;
return NULL;
}
-int pakfire_snapshot_create(struct pakfire* pakfire, FILE* snapshot) {
+int pakfire_snapshot_create(struct pakfire* pakfire, const char* path) {
struct pakfire_filelist* filelist = NULL;
struct archive* a = NULL;
int r = 1;
- if (!snapshot) {
+ // Check input
+ if (!path) {
errno = EINVAL;
return 1;
}
const char* root = pakfire_get_path(pakfire);
- INFO(pakfire, "Creating snapshot of %s...\n", root);
+ INFO(pakfire, "Creating snapshot of %s to %s...\n", root, path);
// Scan for all files
r = pakfire_filelist_scan(filelist, root, NULL, NULL);
// Sort the filelist in place
pakfire_filelist_sort(filelist);
- a = pakfire_snapshot_create_archive(pakfire, snapshot);
+ a = pakfire_snapshot_create_archive(pakfire, path);
if (!a) {
ERROR(pakfire, "Could not open archive for writing\n");
goto ERROR;
return r;
}
-static int pakfire_snapshot_extract(struct pakfire* pakfire, FILE* f) {
+static int pakfire_snapshot_extract(struct pakfire* pakfire, const char* path) {
struct pakfire_progressbar* progressbar = NULL;
char buffer[PATH_MAX];
struct stat st;
int r = 1;
+ // Check input
+ if (!path) {
+ errno = EINVAL;
+ return 1;
+ }
+
// Stat the input file
- r = fstat(fileno(f), &st);
+ r = stat(path, &st);
if (r) {
ERROR(pakfire, "Could not stat snapshot: %m\n");
return 1;
goto ERROR;
// Open the given file for reading
- r = archive_read_open_FILE(a, f);
+ r = archive_read_open_filename(a, path, 64 * 1024);
if (r) {
ERROR(pakfire, "Could not open archive: %s\n", archive_error_string(a));
goto ERROR;
// Update progress
pakfire_progressbar_update(progressbar, bytes_read);
- const char* path = archive_entry_pathname(e);
+ const char* _path = archive_entry_pathname(e);
// Update path
- r = pakfire_make_path(pakfire, buffer, path);
+ r = pakfire_make_path(pakfire, buffer, _path);
if (r < 0)
goto ERROR;
return r;
}
-int pakfire_snapshot_restore(struct pakfire* pakfire, FILE* f) {
+int pakfire_snapshot_restore(struct pakfire* pakfire, const char* path) {
struct pakfire_db* db = NULL;
- if (!f) {
+ // Check input
+ if (!path) {
errno = EINVAL;
return 1;
}
// Extract the archive
- int r = pakfire_snapshot_extract(pakfire, f);
+ int r = pakfire_snapshot_extract(pakfire, path);
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(NULL);
+ FILE* f = test_mktemp(&path);
ASSERT(f);
// Create the snapshot
- ASSERT_SUCCESS(pakfire_snapshot_create(t->pakfire, f));
+ ASSERT_SUCCESS(pakfire_snapshot_create(t->pakfire, path));
- // Determine the size of the snapshot
- size_t size = ftell(f);
+ // Stat the file
+ ASSERT_SUCCESS(stat(path, &st));
- LOG("Snapshot has a size of %jd bytes\n", size);
+ LOG("Snapshot has a size of %jd bytes\n", st.st_size);
// Check if the snapshot has something in it
- ASSERT(size >= 1024);
-
- // Rewind the file descriptor to the beginning
- rewind(f);
+ ASSERT(st.st_size >= 1024);
// Perform a restore
- ASSERT_SUCCESS(pakfire_snapshot_restore(t->pakfire, f));
+ ASSERT_SUCCESS(pakfire_snapshot_restore(t->pakfire, path));
// Everything passed
r = EXIT_SUCCESS;