]> git.ipfire.org Git - people/stevee/pakfire.git/commitdiff
packages: Make constructor function more similar to others
authorMichael Tremer <michael.tremer@ipfire.org>
Tue, 25 Oct 2022 16:05:35 +0000 (16:05 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Tue, 25 Oct 2022 16:05:35 +0000 (16:05 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
12 files changed:
src/_pakfire/package.c
src/_pakfire/repo.c
src/libpakfire/archive.c
src/libpakfire/db.c
src/libpakfire/include/pakfire/package.h
src/libpakfire/package.c
src/libpakfire/packagelist.c
src/libpakfire/parser.c
src/libpakfire/repo.c
src/libpakfire/transaction.c
tests/libpakfire/package.c
tests/libpakfire/packager.c

index 1141de0f65d88e64de36d946a8d31251501dc267..c9c65abf96b20bdf57326ce8438e2ee0f7160d3c 100644 (file)
@@ -67,8 +67,8 @@ static int Package_init(PackageObject* self, PyObject* args, PyObject* kwds) {
                        &name, &evr, &arch))
                return -1;
 
-       self->package = pakfire_package_create(pakfire->pakfire, repo->repo, name, evr, arch);
-       if (!self->package) {
+       int r = pakfire_package_create(&self->package, pakfire->pakfire, repo->repo, name, evr, arch);
+       if (r) {
                PyErr_SetFromErrno(PyExc_OSError);
                return -1;
        }
index d3480bdcd94e5ba6e0c3ab6f28cf717c228ba594..fbe3ed69e8e2f6a48f98bbd3df8f19dec1f84708 100644 (file)
@@ -241,7 +241,13 @@ static PyObject* Repo__add_package(RepoObject* self, PyObject* args) {
                return NULL;
 
        struct pakfire* pakfire = pakfire_repo_get_pakfire(self->repo);
-       struct pakfire_package* pkg = pakfire_package_create(pakfire, self->repo, name, evr, arch);
+       struct pakfire_package* pkg = NULL;
+
+       int r = pakfire_package_create(&pkg, pakfire, self->repo, name, evr, arch);
+       if (r) {
+               PyErr_SetFromErrno(PyExc_OSError);
+               return NULL;
+       }
 
        PyObject* obj = new_package(&PackageType, pkg);
 
index 9b39bdf08b0a8726df90410a1d4625118008b664..4d1bd41c0425d0f47770c2945c9da8efee56eb10 100644 (file)
@@ -1046,6 +1046,7 @@ ERROR:
 
 static int pakfire_archive_make_package_from_json(struct pakfire_archive* archive,
                struct pakfire_repo* repo, struct pakfire_package** package) {
+       struct pakfire_package* pkg = NULL;
        struct pakfire_filelist* filelist = NULL;
        int r;
 
@@ -1060,10 +1061,9 @@ static int pakfire_archive_make_package_from_json(struct pakfire_archive* archiv
        const char* arch = pakfire_archive_metadata_get(archive, "arch", NULL);
 
        // Create a new package object
-       struct pakfire_package* pkg = pakfire_package_create(archive->pakfire,
-               repo, name, evr, arch);
-       if (!pkg)
-               return 1;
+       r = pakfire_package_create(&pkg, archive->pakfire, repo, name, evr, arch);
+       if (r)
+               return r;
 
 #ifdef ENABLE_DEBUG
        const char* nevra = pakfire_package_get_string(pkg, PAKFIRE_PKG_NEVRA);
@@ -1328,9 +1328,10 @@ static int pakfire_archive_make_legacy_package(struct pakfire_archive* archive,
                free(type);
        }
 
-       struct pakfire_package* pkg = pakfire_package_create(
-               archive->pakfire, repo, name, evr, (arch) ? arch : "src"
-       );
+       struct pakfire_package* pkg = NULL;
+
+       r = pakfire_package_create(&pkg, archive->pakfire, repo, name, evr,
+               (arch) ? arch : "src");
 
        if (name)
                free(name);
@@ -1339,6 +1340,9 @@ static int pakfire_archive_make_legacy_package(struct pakfire_archive* archive,
        if (arch)
                free(arch);
 
+       if (r)
+               goto ERROR;
+
        // Set package pointer right here to avoid an endless loop when loading the filelist
        *package = pkg;
 
index fa5214416ce2bdf401f215a07bb9e2d958469741..98dc007332c8a80bb99e92dcd6b2e9ffe833f7c9 100644 (file)
@@ -1743,8 +1743,8 @@ static int pakfire_db_load_package(struct pakfire_db* db, struct pakfire_repo* r
        }
 
        // Create package
-       pkg = pakfire_package_create(db->pakfire, repo, name, evr, arch);
-       if (!pkg) {
+       r = pakfire_package_create(&pkg, db->pakfire, repo, name, evr, arch);
+       if (r) {
                ERROR(db->pakfire, "Could not create package\n");
                goto ERROR;
        }
index 43d35cb5b068b0a0d6e9e97816045234770107a3..1c5112231a08d81bb7875983631e61ca95e5ec4c 100644 (file)
@@ -47,8 +47,8 @@ enum pakfire_package_key {
        PAKFIRE_PKG_MAINTAINER,
 };
 
-struct pakfire_package* pakfire_package_create(struct pakfire* pakfire, struct pakfire_repo* repo,
-       const char* name, const char* evr, const char* arch);
+int pakfire_package_create(struct pakfire_package** package, struct pakfire* pakfire,
+       struct pakfire_repo* repo, const char* name, const char* evr, const char* arch);
 
 struct pakfire_package* pakfire_package_ref(struct pakfire_package* pkg);
 struct pakfire_package* pakfire_package_unref(struct pakfire_package* pkg);
@@ -130,7 +130,8 @@ enum pakfire_package_dump_flags {
 #include <solv/pooltypes.h>
 #include <uuid/uuid.h>
 
-struct pakfire_package* pakfire_package_create_from_solvable(struct pakfire* pakfire, Id id);
+int pakfire_package_create_from_solvable(struct pakfire_package** package,
+       struct pakfire* pakfire, Id id);
 void pakfire_package_set_build_id_from_uuid(struct pakfire_package* pkg, uuid_t* build_id);
 
 uint64_t pakfire_package_get_dbid(struct pakfire_package* pkg);
index d1a4bb5f74a1fb0512e9d23a208f46108d05790d..a677da06c463d460277f51906feaae96efe16ff2 100644 (file)
@@ -62,32 +62,41 @@ struct pakfire_package {
        char path[PATH_MAX];
 };
 
-struct pakfire_package* pakfire_package_create_from_solvable(struct pakfire* pakfire, Id id) {
+int pakfire_package_create_from_solvable(struct pakfire_package** package,
+               struct pakfire* pakfire, Id id) {
        struct pakfire_package* pkg = calloc(1, sizeof(*pkg));
        if (!pkg)
-               return NULL;
+               return 1;
 
        pkg->pakfire = pakfire_ref(pakfire);
-       pkg->id = id;
-
-       // Initialize reference counter
        pkg->nrefs = 1;
 
-       return pkg;
+       // Store the ID
+       pkg->id = id;
+
+       // Success
+       *package = pkg;
+       return 0;
 }
 
-PAKFIRE_EXPORT struct pakfire_package* pakfire_package_create(
+PAKFIRE_EXPORT int pakfire_package_create(struct pakfire_package** package,
                struct pakfire* pakfire, struct pakfire_repo* repo,
                const char* name, const char* evr, const char* arch) {
-       struct pakfire_package* pkg = NULL;
        struct pakfire_repo* dummy = NULL;
+       int r;
+
+       // Check for some valid input
+       if (!name || !evr || !arch) {
+               errno = EINVAL;
+               return 1;
+       }
 
        // Default to dummy repository
        if (!repo) {
                dummy = pakfire_get_repo(pakfire, PAKFIRE_REPO_DUMMY);
                if (!dummy) {
                        errno = ENOENT;
-                       return NULL;
+                       return 1;
                }
 
                repo = dummy;
@@ -95,26 +104,46 @@ PAKFIRE_EXPORT struct pakfire_package* pakfire_package_create(
 
        // Allocate a new solvable
        Id id = pakfire_repo_add_solvable(repo);
-       if (!id)
+       if (!id) {
+               ERROR(pakfire, "Could not allocate a solvable: %m\n");
+               r = 1;
                goto ERROR;
+       }
 
        // Create a new package object
-       pkg = pakfire_package_create_from_solvable(pakfire, id);
-       if (!pkg)
+       r = pakfire_package_create_from_solvable(package, pakfire, id);
+       if (r)
                goto ERROR;
 
-       pkg->repo = pakfire_repo_ref(repo);
+       // Reference the repository
+       (*package)->repo = pakfire_repo_ref(repo);
 
-       // Set the given attributes
-       pakfire_package_set_string(pkg, PAKFIRE_PKG_NAME, name);
-       pakfire_package_set_string(pkg, PAKFIRE_PKG_EVR,  evr);
-       pakfire_package_set_string(pkg, PAKFIRE_PKG_ARCH, arch);
+       // Set the name
+       r = pakfire_package_set_string(*package, PAKFIRE_PKG_NAME, name);
+       if (r) {
+               ERROR(pakfire, "Could not set package name '%s': %m\n", name);
+               goto ERROR;
+       }
+
+       // Set EVR
+       r = pakfire_package_set_string(*package, PAKFIRE_PKG_EVR, evr);
+       if (r) {
+               ERROR(pakfire, "Could not set package EVR '%s': %m\n", evr);
+               goto ERROR;
+       }
+
+       // Set arch
+       r = pakfire_package_set_string(*package, PAKFIRE_PKG_ARCH, arch);
+       if (r) {
+               ERROR(pakfire, "Could not set package arch '%s': %m\n", arch);
+               goto ERROR;
+       }
 
 ERROR:
        if (dummy)
                pakfire_repo_unref(dummy);
 
-       return pkg;
+       return r;
 }
 
 static void pakfire_package_free(struct pakfire_package* pkg) {
index e62647aa42b6474ab03f9aefeadad40739460441..4942cfaa58a43c2f99fb5411f3e9246712092130 100644 (file)
@@ -66,14 +66,16 @@ PAKFIRE_EXPORT int pakfire_packagelist_create(
 }
 
 int pakfire_packagelist_create_from_queue(struct pakfire_packagelist** list, struct pakfire* pakfire, Queue* q) {
+       struct pakfire_package* pkg = NULL;
+
        // Create a new package list
        int r = pakfire_packagelist_create(list, pakfire);
        if (r)
                return r;
 
        for (int i = 0; i < q->count; i++) {
-               struct pakfire_package* pkg = pakfire_package_create_from_solvable(pakfire, q->elements[i]);
-               if (!pkg)
+               r = pakfire_package_create_from_solvable(&pkg, pakfire, q->elements[i]);
+               if (r)
                        goto ERROR;
 
                pakfire_packagelist_push(*list, pkg);
index ee9420e019d185ed99524f1b9979a6f7dd44bc5e..9b3c3b11ac18e4a3ca5c8d264a40c3f9756335c6 100644 (file)
@@ -912,8 +912,8 @@ int pakfire_parser_create_package(struct pakfire_parser* parser,
        }
 
        // Create a new package object
-       *pkg = pakfire_package_create(parser->pakfire, repo, name, evr, arch);
-       if (!*pkg) {
+       r = pakfire_package_create(pkg, parser->pakfire, repo, name, evr, arch);
+       if (r) {
                ERROR(parser->pakfire, "Could not create package\n");
                goto CLEANUP;
        }
index b0558c9a23930420d09d72e65f53c9f2e79a8c62..025143b194ee9f56accf5ed3c77dcf5c9f72ee31 100644 (file)
@@ -1021,8 +1021,8 @@ static int pakfire_repo_delete_all_packages(
                Id id = pool_solvable2id(pool, s);
 
                // Allocate package
-               pkg = pakfire_package_create_from_solvable(repo->pakfire, id);
-               if (!pkg)
+               r = pakfire_package_create_from_solvable(&pkg, repo->pakfire, id);
+               if (r)
                        return 1;
 
                const char* nevra = pakfire_package_get_string(pkg, PAKFIRE_PKG_NEVRA);
index 11675307d7d755af414467aa23cfba1150e398d5..42b7a3968b47b522f8d922738dc8d5231a6fd33d 100644 (file)
@@ -153,6 +153,8 @@ static void pakfire_transaction_free(struct pakfire_transaction* transaction) {
 
 static int pakfire_transaction_import_transaction(
                struct pakfire_transaction* transaction, Solver* solver) {
+       int r;
+
        // Clone the transaction to keep a copy of it
        Transaction* t = solver_create_transaction(solver);
        if (!t)
@@ -181,8 +183,10 @@ static int pakfire_transaction_import_transaction(
 
        // Create all packages
        for (unsigned int i = 0; i < transaction->num; i++) {
-               transaction->packages[i] = pakfire_package_create_from_solvable(
+               r = pakfire_package_create_from_solvable(&transaction->packages[i],
                        transaction->pakfire, t->steps.elements[i]);
+               if (r)
+                       return r;
        }
 
        return 0;
@@ -453,6 +457,7 @@ static char* pakfire_transaction_join_lines(char** lines) {
 
 PAKFIRE_EXPORT char* pakfire_transaction_dump(struct pakfire_transaction* transaction, size_t width) {
        char headline[1024];
+       int r;
 
        Pool* pool = transaction->transaction->pool;
        const int mode =
@@ -569,15 +574,21 @@ PAKFIRE_EXPORT char* pakfire_transaction_dump(struct pakfire_transaction* transa
 
                // List all packages
                for (int j = 0; j < pkgs.count; j++) {
-                       struct pakfire_package* old_pkg = pakfire_package_create_from_solvable(
-                               transaction->pakfire, pkgs.elements[j]);
+                       struct pakfire_package* old_pkg = NULL;
                        struct pakfire_package* new_pkg = NULL;
 
+                       r = pakfire_package_create_from_solvable(&old_pkg, transaction->pakfire,
+                               pkgs.elements[j]);
+                       if (r)
+                               continue;
+
                        switch (class) {
                                case SOLVER_TRANSACTION_UPGRADED:
                                case SOLVER_TRANSACTION_DOWNGRADED:
-                                       new_pkg = pakfire_package_create_from_solvable(transaction->pakfire,
+                                       r = pakfire_package_create_from_solvable(&new_pkg, transaction->pakfire,
                                                transaction_obs_pkg(transaction->transaction, pkgs.elements[j]));
+                                       if (r)
+                                               continue;
 
                                        pakfire_transaction_add_package_change(&lines, width, old_pkg, new_pkg);
                                        break;
index 76428701a14ffd0090c23229c1df54014d00a06d..485723fba2d5f09673a64943cffcfc009575a4e8 100644 (file)
@@ -29,7 +29,7 @@ static int test_create(const struct test* t) {
        struct pakfire_package* pkg = NULL;
        int r = EXIT_FAILURE;
 
-       ASSERT(pkg = pakfire_package_create(t->pakfire, NULL, "test", "1.0-1", "src"));
+       ASSERT_SUCCESS(pakfire_package_create(&pkg, t->pakfire, NULL, "test", "1.0-1", "src"));
 
        ASSERT_STRING_EQUALS(pakfire_package_get_string(pkg, PAKFIRE_PKG_NAME), "test");
        ASSERT_STRING_EQUALS(pakfire_package_get_string(pkg, PAKFIRE_PKG_EVR), "1.0-1");
index a1a18b8be14888c3c8f0ed0a5afd4f7657a00298..0ffa8bbc7028b6daa217acf85d599e6c24426eaf 100644 (file)
@@ -36,9 +36,7 @@ static int test_create(const struct test* t) {
        ASSERT_SUCCESS(pakfire_repo_create(&repo, t->pakfire, "test"));
        ASSERT(repo);
 
-       pkg = pakfire_package_create(t->pakfire, repo,
-               "test", "1.0-1", "src");
-       ASSERT(pkg);
+       ASSERT_SUCCESS(pakfire_package_create(&pkg, t->pakfire, repo, "test", "1.0-1", "src"));
 
        // Create packager
        ASSERT_SUCCESS(pakfire_packager_create(&packager, t->pakfire, pkg));
@@ -80,9 +78,8 @@ static int test_compare_metadata(const struct test* t) {
        struct pakfire_repo* repo = pakfire_get_repo(t->pakfire, PAKFIRE_REPO_DUMMY);
        ASSERT(repo);
 
-       pkg1 = pakfire_package_create(t->pakfire, repo,
-               "test", "1.0-1", "x86_64");
-       ASSERT(pkg1);
+       ASSERT_SUCCESS(pakfire_package_create(&pkg1, t->pakfire, repo,
+               "test", "1.0-1", "x86_64"));
 
        // Set all metadata
        pakfire_package_set_string(pkg1, PAKFIRE_PKG_SUMMARY, "SUMMARY");