]> git.ipfire.org Git - pakfire.git/commitdiff
pakfire: Refactor how we mount the ramdisk, and snapshots
authorMichael Tremer <michael.tremer@ipfire.org>
Thu, 17 Oct 2024 16:58:44 +0000 (16:58 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Thu, 17 Oct 2024 17:14:53 +0000 (17:14 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/include/pakfire/mount.h
src/libpakfire/mount.c
src/libpakfire/pakfire.c

index 6e90a93b9e1c2993363ca70baecf4f7929298546..92e489eb5214ec7fe23e2269d6e46c2b19a5fdc8 100644 (file)
@@ -35,8 +35,6 @@ int pakfire_mount_change_propagation(struct pakfire* pakfire, const char* path,
 
 int pakfire_mount_make_mounpoint(struct pakfire* pakfire, const char* path);
 
-int pakfire_make_ramdisk(struct pakfire* pakfire, char* path, const char* args);
-
 int pakfire_bind(struct pakfire* pakfire, const char* src, const char* dst, int flags);
 
 int pakfire_mount_list(struct pakfire* pakfire);
index 0f7559c25995506a3950fa6885d0e39c6db6a695..6843296f0e05dbb0e5517a78b08471e6889375d8 100644 (file)
@@ -481,27 +481,6 @@ int pakfire_mount_all(struct pakfire* pakfire, pakfire_mntns_t ns, int flags) {
        return 0;
 }
 
-int pakfire_make_ramdisk(struct pakfire* pakfire, char* path, const char* args) {
-       int r;
-
-       // Create a new temporary directory
-       char* p = pakfire_mkdtemp(path);
-       if (!p)
-               return -errno;
-
-       // Mount the ramdisk
-       r = pakfire_mount(pakfire, "pakfire_ramdisk", p, "tmpfs", 0, args);
-       if (r) {
-               PAKFIRE_ERROR(pakfire, "Could not mount ramdisk at %s (%s): %s\n",
-                       p, args, strerror(errno));
-               return r;
-       }
-
-       PAKFIRE_DEBUG(pakfire, "Ramdisk mounted at %s (%s)\n", p, args);
-
-       return 0;
-}
-
 int pakfire_bind(struct pakfire* pakfire, const char* src, const char* dst, int flags) {
        struct stat st;
        char mountpoint[PATH_MAX];
index fab65610dc4ce9b3481630ef62150e0d9b8ec4b2..77985ba16e067062eee52860d4f29205b38b0f3f 100644 (file)
@@ -79,6 +79,12 @@ struct pakfire {
 
        int flags;
 
+       // Keep some internal state
+       enum pakfire_internal_flags {
+               PAKFIRE_HAS_PATH        = (1 << 0),
+               PAKFIRE_DESTROY_ON_FREE = (1 << 1),
+       } internal_flags;
+
        // Lock
        FILE* lock;
 
@@ -108,7 +114,6 @@ struct pakfire {
        struct pakfire_snapshot* snapshot;
 
        // States
-       unsigned int destroy_on_free:1;
        unsigned int pool_ready:1;
        unsigned int in_free:1;
 };
@@ -392,7 +397,7 @@ static void pakfire_free(struct pakfire* pakfire) {
        if (pakfire->snapshot)
                pakfire_snapshot_unref(pakfire->snapshot);
 
-       if (pakfire->destroy_on_free && *pakfire->path) {
+       if (pakfire->internal_flags & PAKFIRE_DESTROY_ON_FREE) {
                CTX_DEBUG(pakfire->ctx, "Destroying %s\n", pakfire->path);
 
                // Umount the ramdisk
@@ -766,14 +771,30 @@ ERROR:
 PAKFIRE_EXPORT int pakfire_create(struct pakfire** pakfire, struct pakfire_ctx* ctx,
                const char* path, const char* arch, FILE* conf, int flags) {
        struct pakfire* p = NULL;
-       char tempdir[PATH_MAX] = PAKFIRE_TMP_DIR "/pakfire-root-XXXXXX";
        char private_dir[PATH_MAX];
-       int r = 1;
+       int r;
 
        // Default to the native architecture
        if (!arch)
                arch = pakfire_arch_native();
 
+       // Check path
+       if (path) {
+               // Check that we don't have path set when using snapshots
+               if (flags & PAKFIRE_USE_SNAPSHOT) {
+                       CTX_ERROR(ctx, "Cannot use path with snapshots\n");
+                       r = -EINVAL;
+                       goto ERROR;
+               }
+
+               // Path must be absolute
+               if (!pakfire_string_startswith(path, "/")) {
+                       CTX_ERROR(ctx, "Invalid path: %s\n", path);
+                       r = -EINVAL;
+                       goto ERROR;
+               }
+       }
+
        // Allocate a new object
        p = calloc(1, sizeof(*p));
        if (!p)
@@ -823,27 +844,32 @@ PAKFIRE_EXPORT int pakfire_create(struct pakfire** pakfire, struct pakfire_ctx*
                goto ERROR;
        }
 
-       // Path must be absolute
-       if (path && !pakfire_string_startswith(path, "/")) {
-               CTX_ERROR(p->ctx, "Invalid path: %s\n", path);
-               r = -EINVAL;
-               goto ERROR;
-       }
-
-       // If using a snapshot, path cannot be set
+       // Are we using a snapshot?
        if (p->flags & PAKFIRE_USE_SNAPSHOT) {
-               if (path) {
-                       CTX_ERROR(p->ctx, "Cannot use snapshots with path\n");
-                       r = -EINVAL;
-                       goto ERROR;
-               }
-
                // Find the most recent snapshot
                r = pakfire_snapshot_find(&p->snapshot, p);
                if (r < 0) {
                        CTX_ERROR(p->ctx, "Could not find a snapshot: %s\n", strerror(-r));
                        goto ERROR;
                }
+       }
+
+       // Store if we have a path
+       if (path) {
+               p->internal_flags |= PAKFIRE_HAS_PATH;
+
+       // Otherwise we create our own temporary path
+       } else {
+               // Template when running Pakfire in a temporary environment
+               char tmppath[PATH_MAX] = PAKFIRE_TMP_DIR "/pakfire-root-XXXXXX";
+
+               // Create a new temporary directory
+               path = pakfire_mkdtemp(tmppath);
+               if (!path)
+                       return -errno;
+
+               // Destroy the temporary directory afterwards
+               p->internal_flags |= PAKFIRE_DESTROY_ON_FREE;
 
                // Mount the snapshot
                if (p->snapshot) {
@@ -852,23 +878,19 @@ PAKFIRE_EXPORT int pakfire_create(struct pakfire** pakfire, struct pakfire_ctx*
                                CTX_ERROR(p->ctx, "Could not mount snapshot: %s\n", strerror(-r));
                                goto ERROR;
                        }
-               }
-       }
 
-       // Create a ramdisk if no path was given
-       if (!path) {
-               r = pakfire_make_ramdisk(p, tempdir, NULL);
-               if (r)
-                       goto ERROR;
-
-               // Use the ramdisk as path
-               path = tempdir;
-
-               // Destroy everything when done
-               p->destroy_on_free = 1;
+               // Mount a tmpfs
+               } else {
+                       r = mount("pakfire_root", path, "tmpfs", 0, NULL);
+                       if (r) {
+                               CTX_ERROR(p->ctx, "Could not mount tmpfs: %m\n");
+                               r = -errno;
+                               goto ERROR;
+                       }
+               }
        }
 
-       // Set path
+       // Store the path
        r = pakfire_string_set(p->path, path);
        if (r < 0)
                goto ERROR;