]> git.ipfire.org Git - pakfire.git/blobdiff - src/libpakfire/package.c
packages: Make constructor function more similar to others
[pakfire.git] / src / libpakfire / package.c
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) {