From 4667a2ca811f6f2b20c1cfb3223dd8b90af4952c Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Wed, 17 Aug 2022 15:59:36 +0000 Subject: [PATCH] snapshots: Pass path instead of file descriptor Signed-off-by: Michael Tremer --- src/libpakfire/build.c | 26 +++------------- src/libpakfire/include/pakfire/snapshot.h | 6 ++-- src/libpakfire/snapshot.c | 37 ++++++++++++++--------- tests/libpakfire/snapshot.c | 20 ++++++------ 4 files changed, 39 insertions(+), 50 deletions(-) diff --git a/src/libpakfire/build.c b/src/libpakfire/build.c index 7c37fbdc..59d4b16e 100644 --- a/src/libpakfire/build.c +++ b/src/libpakfire/build.c @@ -1146,7 +1146,6 @@ ERROR: Initializes the build environment */ static int pakfire_build_init(struct pakfire_build* build) { - FILE* f = NULL; char path[PATH_MAX]; int r; @@ -1177,18 +1176,10 @@ 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 - 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 @@ -1198,17 +1189,8 @@ 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, f); - fclose(f); - + r = pakfire_snapshot_create(build->pakfire, path); if (r) return r; } diff --git a/src/libpakfire/include/pakfire/snapshot.h b/src/libpakfire/include/pakfire/snapshot.h index fe440497..e8099b8c 100644 --- a/src/libpakfire/include/pakfire/snapshot.h +++ b/src/libpakfire/include/pakfire/snapshot.h @@ -23,12 +23,10 @@ #ifdef PAKFIRE_PRIVATE -#include - #include -int pakfire_snapshot_create(struct pakfire* pakfire, FILE* f); -int pakfire_snapshot_restore(struct pakfire* pakfire, FILE* f); +int pakfire_snapshot_create(struct pakfire* pakfire, const char* path); +int pakfire_snapshot_restore(struct pakfire* pakfire, const char* path); #endif diff --git a/src/libpakfire/snapshot.c b/src/libpakfire/snapshot.c index 8214a594..6ee8e2af 100644 --- a/src/libpakfire/snapshot.c +++ b/src/libpakfire/snapshot.c @@ -70,7 +70,8 @@ ERROR: 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"); @@ -104,7 +105,7 @@ static struct archive* pakfire_snapshot_create_archive(struct pakfire* pakfire, 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; @@ -116,12 +117,13 @@ 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; } @@ -133,7 +135,7 @@ int pakfire_snapshot_create(struct pakfire* pakfire, FILE* snapshot) { 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); @@ -152,7 +154,7 @@ int pakfire_snapshot_create(struct pakfire* pakfire, FILE* snapshot) { // 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; @@ -230,14 +232,20 @@ 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; @@ -258,7 +266,7 @@ static int pakfire_snapshot_extract(struct pakfire* pakfire, FILE* f) { 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; @@ -289,10 +297,10 @@ static int pakfire_snapshot_extract(struct pakfire* pakfire, FILE* f) { // 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; @@ -333,16 +341,17 @@ 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; diff --git a/tests/libpakfire/snapshot.c b/tests/libpakfire/snapshot.c index bd4c44ba..c14641a9 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(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; -- 2.47.3