From: Michael Tremer Date: Tue, 25 Oct 2022 16:05:35 +0000 (+0000) Subject: packages: Make constructor function more similar to others X-Git-Tag: 0.9.28~220 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=33ad2a01d6166d20f03f52bbdaf5222d828674c2;p=pakfire.git packages: Make constructor function more similar to others Signed-off-by: Michael Tremer --- diff --git a/src/_pakfire/package.c b/src/_pakfire/package.c index 1141de0f6..c9c65abf9 100644 --- a/src/_pakfire/package.c +++ b/src/_pakfire/package.c @@ -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; } diff --git a/src/_pakfire/repo.c b/src/_pakfire/repo.c index d3480bdcd..fbe3ed69e 100644 --- a/src/_pakfire/repo.c +++ b/src/_pakfire/repo.c @@ -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); diff --git a/src/libpakfire/archive.c b/src/libpakfire/archive.c index 9b39bdf08..4d1bd41c0 100644 --- a/src/libpakfire/archive.c +++ b/src/libpakfire/archive.c @@ -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; diff --git a/src/libpakfire/db.c b/src/libpakfire/db.c index fa5214416..98dc00733 100644 --- a/src/libpakfire/db.c +++ b/src/libpakfire/db.c @@ -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; } diff --git a/src/libpakfire/include/pakfire/package.h b/src/libpakfire/include/pakfire/package.h index 43d35cb5b..1c5112231 100644 --- a/src/libpakfire/include/pakfire/package.h +++ b/src/libpakfire/include/pakfire/package.h @@ -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 #include -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); diff --git a/src/libpakfire/package.c b/src/libpakfire/package.c index d1a4bb5f7..a677da06c 100644 --- a/src/libpakfire/package.c +++ b/src/libpakfire/package.c @@ -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) { diff --git a/src/libpakfire/packagelist.c b/src/libpakfire/packagelist.c index e62647aa4..4942cfaa5 100644 --- a/src/libpakfire/packagelist.c +++ b/src/libpakfire/packagelist.c @@ -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); diff --git a/src/libpakfire/parser.c b/src/libpakfire/parser.c index ee9420e01..9b3c3b11a 100644 --- a/src/libpakfire/parser.c +++ b/src/libpakfire/parser.c @@ -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; } diff --git a/src/libpakfire/repo.c b/src/libpakfire/repo.c index b0558c9a2..025143b19 100644 --- a/src/libpakfire/repo.c +++ b/src/libpakfire/repo.c @@ -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); diff --git a/src/libpakfire/transaction.c b/src/libpakfire/transaction.c index 11675307d..42b7a3968 100644 --- a/src/libpakfire/transaction.c +++ b/src/libpakfire/transaction.c @@ -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; diff --git a/tests/libpakfire/package.c b/tests/libpakfire/package.c index 76428701a..485723fba 100644 --- a/tests/libpakfire/package.c +++ b/tests/libpakfire/package.c @@ -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"); diff --git a/tests/libpakfire/packager.c b/tests/libpakfire/packager.c index a1a18b8be..0ffa8bbc7 100644 --- a/tests/libpakfire/packager.c +++ b/tests/libpakfire/packager.c @@ -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");