From 22a78e0df357715d70bd77e3d2d5362e6d0b3ff1 Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Tue, 4 Feb 2025 18:06:05 +0000 Subject: [PATCH] pakfire: Move path setup Signed-off-by: Michael Tremer --- src/pakfire/pakfire.c | 104 +++++++++++++++++++++++------------------- 1 file changed, 57 insertions(+), 47 deletions(-) diff --git a/src/pakfire/pakfire.c b/src/pakfire/pakfire.c index 747323cf..fb21cea4 100644 --- a/src/pakfire/pakfire.c +++ b/src/pakfire/pakfire.c @@ -255,6 +255,61 @@ static Id pakfire_namespace_callback(Pool* pool, void* data, Id ns, Id id) { return 0; } +static int pakfire_setup_path(struct pakfire* self, const char* path) { + int r; + + // Template when running Pakfire in a temporary environment + char tmppath[PATH_MAX] = PAKFIRE_TMP_DIR "/pakfire-root-XXXXXX"; + + // If we don't have a path, create something temporary... + if (!path) { + // Create a new temporary directory + path = pakfire_mkdtemp(tmppath); + if (!path) + return -errno; + + // Destroy the temporary directory afterwards + self->internal_flags |= PAKFIRE_DESTROY_ON_FREE; + + // Mount the snapshot + if (self->snapshot) { + r = pakfire_snapshot_mount(self->snapshot, path); + if (r < 0) { + ERROR(self->ctx, "Could not mount snapshot: %s\n", strerror(-r)); + return r; + } + + // Mount a tmpfs + } else { + r = mount("pakfire_root", path, "tmpfs", 0, NULL); + if (r) { + ERROR(self->ctx, "Could not mount tmpfs: %m\n"); + return -errno; + } + + // Umount path later + self->internal_flags |= PAKFIRE_UMOUNT_PATH; + } + } + + // Store the path + r = pakfire_string_set(self->path, path); + if (r < 0) + return r; + + // Open the path + r = open(self->path, O_DIRECTORY); + if (r < 0) { + ERROR(self->ctx, "Could not open %s: %m\n", self->path); + return -errno; + } + + // Store the file descriptor + self->fd = r; + + return 0; +} + static int pakfire_lock_running_kernel(struct pakfire* pakfire) { struct utsname utsname; char buffer[NAME_MAX]; @@ -1020,56 +1075,11 @@ int pakfire_create(struct pakfire** pakfire, struct pakfire_ctx* ctx, } } - // If we don't have a path, create something temporary... - if (!path) { - // 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) { - r = -errno; - goto ERROR; - } - - // Destroy the temporary directory afterwards - p->internal_flags |= PAKFIRE_DESTROY_ON_FREE; - - // Mount the snapshot - if (p->snapshot) { - r = pakfire_snapshot_mount(p->snapshot, path); - if (r < 0) { - ERROR(p->ctx, "Could not mount snapshot: %s\n", strerror(-r)); - goto ERROR; - } - - // Mount a tmpfs - } else { - r = mount("pakfire_root", path, "tmpfs", 0, NULL); - if (r) { - ERROR(p->ctx, "Could not mount tmpfs: %m\n"); - r = -errno; - goto ERROR; - } - - // Umount path later - p->internal_flags |= PAKFIRE_UMOUNT_PATH; - } - } - - // Store the path - r = pakfire_string_set(p->path, path); + // Setup path + r = pakfire_setup_path(p, path); if (r < 0) goto ERROR; - // Open the path - r = p->fd = open(p->path, O_DIRECTORY); - if (r < 0) { - ERROR(p->ctx, "Could not open %s: %m\n", p->path); - r = -errno; - goto ERROR; - } - // Setup the pool r = pakfire_setup_pool(p); if (r < 0) -- 2.39.5