]> git.ipfire.org Git - pakfire.git/commitdiff
pakfire: Tidy up the code to create a new pakfire instance slightly
authorMichael Tremer <michael.tremer@ipfire.org>
Thu, 17 Oct 2024 15:28:40 +0000 (15:28 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Thu, 17 Oct 2024 15:28:40 +0000 (15:28 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/pakfire.c

index 8223d77a01ecd4602149673a3960ef13cc489ab7..9f5e338b651ce4e1104e9e97f999715facb74887 100644 (file)
@@ -765,30 +765,32 @@ 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;
 
-       // Reset pakfire pointer
-       *pakfire = NULL;
-
        // Default to the native architecture
        if (!arch)
                arch = pakfire_arch_native();
 
-       struct pakfire* p = calloc(1, sizeof(*p));
+       // Allocate a new object
+       p = calloc(1, sizeof(*p));
        if (!p)
                return -errno;
 
        // Reference the context
        p->ctx = pakfire_ctx_ref(ctx);
 
+       // Initialize the reference counter
        p->nrefs = 1;
+
+       // Store the flags
        p->flags = flags;
 
        // Store the nominal architecture
        r = pakfire_string_set(p->arches.nominal, arch);
-       if (r)
+       if (r < 0)
                goto ERROR;
 
        // Determine the effective architecture
@@ -801,7 +803,7 @@ PAKFIRE_EXPORT int pakfire_create(struct pakfire** pakfire, struct pakfire_ctx*
 
        // Setup user/group
        r = pakfire_setup_user(p);
-       if (r)
+       if (r < 0)
                goto ERROR;
 
        // Initialise configuration
@@ -870,7 +872,9 @@ PAKFIRE_EXPORT int pakfire_create(struct pakfire** pakfire, struct pakfire_ctx*
        }
 
        // Set path
-       pakfire_string_set(p->path, path);
+       r = pakfire_string_set(p->path, path);
+       if (r < 0)
+               goto ERROR;
 
        // Read /etc/os-release
        r = pakfire_read_os_release(p);
@@ -924,20 +928,22 @@ PAKFIRE_EXPORT int pakfire_create(struct pakfire** pakfire, struct pakfire_ctx*
 
        // Populate pool
        r = pakfire_populate_pool(p);
-       if (r)
+       if (r < 0)
                goto ERROR;
 
        // Create repositories
        r = pakfire_repo_import(p, p->config);
-       if (r)
+       if (r < 0)
                goto ERROR;
 
-       *pakfire = p;
+       // Return the pointer
+       *pakfire = pakfire_ref(p);
 
        return 0;
 
 ERROR:
-       pakfire_free(p);
+       if (p)
+               pakfire_unref(p);
 
        return r;
 }
@@ -953,7 +959,6 @@ PAKFIRE_EXPORT struct pakfire* pakfire_unref(struct pakfire* pakfire) {
                return pakfire;
 
        pakfire_free(pakfire);
-
        return NULL;
 }