]> git.ipfire.org Git - pakfire.git/commitdiff
pakfire: Move path setup
authorMichael Tremer <michael.tremer@ipfire.org>
Tue, 4 Feb 2025 18:06:05 +0000 (18:06 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Tue, 4 Feb 2025 18:06:05 +0000 (18:06 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/pakfire/pakfire.c

index 747323cf156e34d75266f8e7cf2ce986843a9d94..fb21cea4182af1cdf4d620c3961ff987b191af76 100644 (file)
@@ -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)