]> git.ipfire.org Git - pakfire.git/commitdiff
repo: Refactor opening existing repositories
authorMichael Tremer <michael.tremer@ipfire.org>
Thu, 30 Jan 2025 09:04:30 +0000 (09:04 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Thu, 30 Jan 2025 09:04:30 +0000 (09:04 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/pakfire/package.c
src/pakfire/pakfire.c
src/pakfire/repo.c
src/pakfire/repo.h

index 87538a961334a4f455179c11410d42d536772725..8b102421d93be60c154bc0d339bd4f98b65b15b5 100644 (file)
@@ -1648,10 +1648,15 @@ int pakfire_package_matches_dep(struct pakfire_package* pkg,
 }
 
 struct pakfire_repo* pakfire_package_get_repo(struct pakfire_package* pkg) {
+       int r;
+
+       // Open the repository if not done, yet
        if (!pkg->repo) {
                Solvable* s = get_solvable(pkg);
 
-               pkg->repo = pakfire_repo_create_from_repo(pkg->pakfire, s->repo);
+               r = pakfire_repo_open(&pkg->repo, pkg->pakfire, s->repo);
+               if (r < 0)
+                       return NULL;
        }
 
        return pakfire_repo_ref(pkg->repo);
index 6a7d90f200ee496daefb134e06f6b989a1273bde..46c2e00b56e31c5873a26a2345522d65f7bf0439 100644 (file)
@@ -1121,9 +1121,9 @@ int pakfire_repo_walk(struct pakfire* pakfire,
 
        // Run func for every repository
        FOR_REPOS(i, solv_repo) {
-               repo = pakfire_repo_create_from_repo(pakfire, solv_repo);
-               if (!repo)
-                       return 1;
+               r = pakfire_repo_open(&repo, pakfire, solv_repo);
+               if (r < 0)
+                       return r;
 
                // Run callback
                r = callback(pakfire, repo, p);
@@ -1242,8 +1242,8 @@ struct pakfire_repolist* pakfire_get_repos(struct pakfire* pakfire) {
                        continue;
 
                // Create repository
-               repo = pakfire_repo_create_from_repo(pakfire, solv_repo);
-               if (!repo)
+               r = pakfire_repo_open(&repo, pakfire, solv_repo);
+               if (r < 0)
                        goto ERROR;
 
                // Append it to the list
@@ -1264,16 +1264,23 @@ ERROR:
 }
 
 struct pakfire_repo* pakfire_get_repo(struct pakfire* pakfire, const char* name) {
+       struct pakfire_repo* repo = NULL;
+       Repo* solv_repo = NULL;
+       int r;
+       int i;
+
        Pool* pool = pakfire_get_solv_pool(pakfire);
        if (!pool)
                return NULL;
 
-       Repo* repo;
-       int i;
+       FOR_REPOS(i, solv_repo) {
+               if (strcmp(solv_repo->name, name) == 0) {
+                       r = pakfire_repo_open(&repo, pakfire, solv_repo);
+                       if (r < 0)
+                               return NULL;
 
-       FOR_REPOS(i, repo) {
-               if (strcmp(repo->name, name) == 0)
-                       return pakfire_repo_create_from_repo(pakfire, repo);
+                       return repo;
+               }
        }
 
        // Nothing found
@@ -1281,10 +1288,18 @@ struct pakfire_repo* pakfire_get_repo(struct pakfire* pakfire, const char* name)
 }
 
 struct pakfire_repo* pakfire_get_installed_repo(struct pakfire* pakfire) {
+       struct pakfire_repo* repo = NULL;
+       int r;
+
        if (!pakfire->pool->installed)
                return NULL;
 
-       return pakfire_repo_create_from_repo(pakfire, pakfire->pool->installed);
+       // Open the repository
+       r = pakfire_repo_open(&repo, pakfire, pakfire->pool->installed);
+       if (r < 0)
+               return NULL;
+
+       return repo;
 }
 
 /*
index 9fc4eafe0048abb3224164e644f96d6b90ac1eaf..2bb251e8a318f6738cdd2649de5a22fb7ec15a1f 100644 (file)
@@ -852,24 +852,33 @@ ERROR:
        return r;
 }
 
-struct pakfire_repo* pakfire_repo_create_from_repo(struct pakfire* pakfire, Repo* r) {
-       struct pakfire_repo* repo = calloc(1, sizeof(*repo));
-       if (repo) {
-               // Store a reference to the context
-               repo->ctx = pakfire_ctx(pakfire);
+int pakfire_repo_open(struct pakfire_repo** repo, struct pakfire* pakfire, Repo* _repo) {
+       struct pakfire_repo* self = NULL;
 
-               // Store a reference to Pakfire
-               repo->pakfire = pakfire_ref(pakfire);
+       // Allocate some memory
+       self = calloc(1, sizeof(*self));
+       if (!self)
+               return -errno;
 
-               // Initialize the reference counter
-               repo->nrefs = 1;
+       // Store a reference to the context
+       self->ctx = pakfire_ctx(pakfire);
 
-               // Reference repository
-               repo->repo = r;
-               repo->appdata = r->appdata;
-       }
+       // Store a reference to Pakfire
+       self->pakfire = pakfire_ref(pakfire);
 
-       return repo;
+       // Initialize the reference counter
+       self->nrefs = 1;
+
+       // Store a reference to the repository
+       self->repo = _repo;
+
+       // Initialize appdata
+       self->appdata = self->repo->appdata;
+
+       // Return the pointer
+       *repo = self;
+
+       return 0;
 }
 
 struct pakfire_repo* pakfire_repo_ref(struct pakfire_repo* repo) {
index cc8d8c3264e6f6e2c38c495a7f54347a3ed9174a..400dcc5aca90dfbb187e96e651411309f1f2a551 100644 (file)
@@ -39,6 +39,7 @@ struct pakfire_repo;
 #include <pakfire/xfer.h>
 
 int pakfire_repo_create(struct pakfire_repo** repo, struct pakfire* pakfire, const char* name);
+int pakfire_repo_open(struct pakfire_repo** repo, struct pakfire* pakfire, Repo* _repo);
 
 struct pakfire_repo* pakfire_repo_ref(struct pakfire_repo* repo);
 struct pakfire_repo* pakfire_repo_unref(struct pakfire_repo* repo);
@@ -129,7 +130,6 @@ int pakfire_repo_download_package(struct pakfire_xfer** xfer,
 int pakfire_repo_add(struct pakfire_repo* repo, const char* path,
        struct pakfire_package** package);
 
-struct pakfire_repo* pakfire_repo_create_from_repo(struct pakfire* pakfire, Repo* r);
 void pakfire_repo_free_all(struct pakfire* pakfire);
 
 Repo* pakfire_repo_get_repo(struct pakfire_repo* repo);