From: Michael Tremer Date: Wed, 15 Mar 2023 18:48:55 +0000 (+0000) Subject: Revert "snapshots: Pass path instead of file descriptor" X-Git-Url: http://git.ipfire.org/gitweb/gitweb.cgi?a=commitdiff_plain;h=6889a3d1c482fb7201b0494ea369afa1bff47500;p=people%2Fstevee%2Fpakfire.git Revert "snapshots: Pass path instead of file descriptor" This reverts commit 4667a2ca811f6f2b20c1cfb3223dd8b90af4952c. Signed-off-by: Michael Tremer --- diff --git a/src/libpakfire/build.c b/src/libpakfire/build.c index 9d7307bd..afd9fed3 100644 --- a/src/libpakfire/build.c +++ b/src/libpakfire/build.c @@ -1660,6 +1660,7 @@ static int pakfire_build_install_packages(struct pakfire_build* build, Initializes the build environment */ static int pakfire_build_init(struct pakfire_build* build) { + FILE* f = NULL; char path[PATH_MAX]; int r; @@ -1681,10 +1682,18 @@ static int pakfire_build_init(struct pakfire_build* build) { // 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 @@ -1694,8 +1703,17 @@ static int pakfire_build_init(struct pakfire_build* build) { // 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; } diff --git a/src/libpakfire/include/pakfire/snapshot.h b/src/libpakfire/include/pakfire/snapshot.h index e8099b8c..fe440497 100644 --- a/src/libpakfire/include/pakfire/snapshot.h +++ b/src/libpakfire/include/pakfire/snapshot.h @@ -23,10 +23,12 @@ #ifdef PAKFIRE_PRIVATE +#include + #include -int pakfire_snapshot_create(struct pakfire* pakfire, const char* path); -int pakfire_snapshot_restore(struct pakfire* pakfire, const char* path); +int pakfire_snapshot_create(struct pakfire* pakfire, FILE* f); +int pakfire_snapshot_restore(struct pakfire* pakfire, FILE* f); #endif diff --git a/src/libpakfire/snapshot.c b/src/libpakfire/snapshot.c index ac9fbfca..76c36fc6 100644 --- a/src/libpakfire/snapshot.c +++ b/src/libpakfire/snapshot.c @@ -36,8 +36,7 @@ #include #include -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"); @@ -71,7 +70,7 @@ static struct archive* pakfire_snapshot_create_archive( 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; @@ -83,13 +82,12 @@ 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; } @@ -101,7 +99,7 @@ int pakfire_snapshot_create(struct pakfire* pakfire, const char* path) { 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); @@ -115,7 +113,8 @@ int pakfire_snapshot_create(struct pakfire* pakfire, const char* path) { 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; @@ -146,18 +145,12 @@ 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; @@ -174,7 +167,7 @@ static int pakfire_snapshot_extract(struct pakfire* pakfire, const char* path) { 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; @@ -193,17 +186,16 @@ 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; diff --git a/tests/libpakfire/snapshot.c b/tests/libpakfire/snapshot.c index c14641a9..bd4c44ba 100644 --- a/tests/libpakfire/snapshot.c +++ b/tests/libpakfire/snapshot.c @@ -19,34 +19,34 @@ #############################################################################*/ #include -#include #include #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;