]> git.ipfire.org Git - people/stevee/pakfire.git/commitdiff
Revert "snapshots: Pass path instead of file descriptor"
authorMichael Tremer <michael.tremer@ipfire.org>
Wed, 15 Mar 2023 18:48:55 +0000 (18:48 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Wed, 15 Mar 2023 18:48:55 +0000 (18:48 +0000)
This reverts commit 4667a2ca811f6f2b20c1cfb3223dd8b90af4952c.

Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/build.c
src/libpakfire/include/pakfire/snapshot.h
src/libpakfire/snapshot.c
tests/libpakfire/snapshot.c

index 9d7307bd44e57677830dcad44dacb9051564d11e..afd9fed305aa18f3da476e5296f25d3708bee23e 100644 (file)
@@ -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;
        }
index e8099b8ca0c89700930e22f5d85d1305b9244090..fe4404979b6310b89e71f2a4b43837a3454058db 100644 (file)
 
 #ifdef PAKFIRE_PRIVATE
 
+#include <stdio.h>
+
 #include <pakfire/pakfire.h>
 
-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
 
index ac9fbfcaa642c60e8726fd55bad4bded6d98364c..76c36fc6eccd428206650ea7b55f309eb7d73f03 100644 (file)
@@ -36,8 +36,7 @@
 #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");
@@ -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;
 
index c14641a945e702bff32fb957064d03f01de56315..bd4c44ba650c4cf8cd721daecc933fe8aa108aed 100644 (file)
 #############################################################################*/
 
 #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;