From 6b352aa209e0ae58e1353fe45ee8ee8d34336cbd Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Thu, 17 Oct 2024 16:58:44 +0000 Subject: [PATCH] pakfire: Refactor how we mount the ramdisk, and snapshots Signed-off-by: Michael Tremer --- src/libpakfire/include/pakfire/mount.h | 2 - src/libpakfire/mount.c | 21 ------- src/libpakfire/pakfire.c | 86 ++++++++++++++++---------- 3 files changed, 54 insertions(+), 55 deletions(-) diff --git a/src/libpakfire/include/pakfire/mount.h b/src/libpakfire/include/pakfire/mount.h index 6e90a93b9..92e489eb5 100644 --- a/src/libpakfire/include/pakfire/mount.h +++ b/src/libpakfire/include/pakfire/mount.h @@ -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); diff --git a/src/libpakfire/mount.c b/src/libpakfire/mount.c index 0f7559c25..6843296f0 100644 --- a/src/libpakfire/mount.c +++ b/src/libpakfire/mount.c @@ -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]; diff --git a/src/libpakfire/pakfire.c b/src/libpakfire/pakfire.c index fab65610d..77985ba16 100644 --- a/src/libpakfire/pakfire.c +++ b/src/libpakfire/pakfire.c @@ -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; -- 2.47.2