]> git.ipfire.org Git - people/stevee/pakfire.git/commitdiff
snapshots: Pass path instead of file descriptor
authorMichael Tremer <michael.tremer@ipfire.org>
Wed, 17 Aug 2022 15:59:36 +0000 (15:59 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Wed, 17 Aug 2022 15:59:36 +0000 (15:59 +0000)
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 7c37fbdc7e3bc69b76116f0bb406be5769301331..59d4b16e5b7dcfde5d439cf87893c6f6cd692d75 100644 (file)
@@ -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;
        }
index fe4404979b6310b89e71f2a4b43837a3454058db..e8099b8ca0c89700930e22f5d85d1305b9244090 100644 (file)
 
 #ifdef PAKFIRE_PRIVATE
 
-#include <stdio.h>
-
 #include <pakfire/pakfire.h>
 
-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
 
index 8214a594c9feed3fb85e5b474fc164e801c1ab90..6ee8e2aff4a9e9f4586609a487653f3c91a58fe6 100644 (file)
@@ -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;
 
index bd4c44ba650c4cf8cd721daecc933fe8aa108aed..c14641a945e702bff32fb957064d03f01de56315 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(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;