]> git.ipfire.org Git - pakfire.git/commitdiff
Create a unified downloader for all repositories
authorMichael Tremer <michael.tremer@ipfire.org>
Wed, 24 Mar 2021 16:27:09 +0000 (16:27 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Wed, 24 Mar 2021 16:27:09 +0000 (16:27 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/include/pakfire/repo.h
src/libpakfire/repo.c
src/libpakfire/transaction.c

index 8e511b7bbec0197d2ea47b6bb676e51fc3ab8eeb..72f771caeb2a24af507f12b57e7160e995c220e9 100644 (file)
@@ -61,6 +61,8 @@ int pakfire_repo_set_keyfile(PakfireRepo repo, const char* keyfile);
 const char* pakfire_repo_get_mirrorlist(PakfireRepo repo);
 int pakfire_repo_set_mirrorlist(PakfireRepo repo, const char* mirrorlist);
 
+struct pakfire_mirrorlist* pakfire_repo_get_mirrors(PakfireRepo repo);
+
 char* pakfire_repo_get_config(PakfireRepo repo);
 
 int pakfire_repo_is_installed_repo(PakfireRepo repo);
@@ -89,6 +91,8 @@ int pakfire_repo_refresh(PakfireRepo repo, const int force);
 
 #include <solv/repo.h>
 
+#include <pakfire/downloader.h>
+
 Id pakfire_repo_add_solvable(PakfireRepo repo);
 
 PakfireRepo pakfire_repo_create_from_repo(Pakfire pakfire, Repo* r);
@@ -97,7 +101,7 @@ void pakfire_repo_free_all(Pakfire pakfire);
 Repo* pakfire_repo_get_repo(PakfireRepo repo);
 Repodata* pakfire_repo_get_repodata(PakfireRepo repo);
 
-int pakfire_repo_download_packages(PakfireRepo repo, PakfirePackageList packages);
+struct pakfire_mirrorlist* pakfire_repo_get_mirrors(PakfireRepo repo);
 
 #endif
 
index be06c568fa588c67913edf2684bb65067a608321..d1ac432a81586db5be6bb703917bde2dcb9dad0b 100644 (file)
@@ -40,7 +40,6 @@
 #include <pakfire/errno.h>
 #include <pakfire/logging.h>
 #include <pakfire/package.h>
-#include <pakfire/packagelist.h>
 #include <pakfire/pakfire.h>
 #include <pakfire/private.h>
 #include <pakfire/repo.h>
@@ -621,6 +620,10 @@ PAKFIRE_EXPORT int pakfire_repo_set_mirrorlist(PakfireRepo repo, const char* mir
        return 0;
 }
 
+struct pakfire_mirrorlist* pakfire_repo_get_mirrors(PakfireRepo repo) {
+       return pakfire_mirrorlist_ref(repo->appdata->mirrors);
+}
+
 PAKFIRE_EXPORT char* pakfire_repo_get_config(PakfireRepo repo) {
        if (pakfire_repo_is_installed_repo(repo) == 0)
                return NULL;
@@ -830,42 +833,3 @@ PAKFIRE_EXPORT int pakfire_repo_refresh(PakfireRepo repo, const int force) {
        // Refresh metadata
        return pakfire_repo_refresh_metadata(repo, force);
 }
-
-int pakfire_repo_download_packages(PakfireRepo repo, PakfirePackageList packages) {
-       int r;
-
-       struct pakfire_downloader* downloader = pakfire_repo_downloader(repo);
-       if (!downloader)
-               return 1;
-
-       const size_t num_packages = pakfire_packagelist_count(packages);
-
-       for (unsigned int i = 0; i < num_packages; i++) {
-               PakfirePackage pkg = pakfire_packagelist_get(packages, i);
-
-               // Skip packages that are not in this repository
-               if (!pakfire_package_is_in_repo(pkg, repo->repo)) {
-                       pakfire_package_unref(pkg);
-                       continue;
-               }
-
-               char* cache_path = pakfire_package_get_cache_path(pkg);
-
-               // Add transfer to downloader
-               r = pakfire_downloader_add_transfer(downloader,
-                       repo->appdata->mirrors, pakfire_package_get_filename(pkg), cache_path);
-               if (r) {
-                       pakfire_package_unref(pkg);
-                       free(cache_path);
-                       goto ERROR;
-               }
-       }
-
-       // Run the download
-       r = pakfire_downloader_run(downloader);
-
-ERROR:
-       pakfire_downloader_unref(downloader);
-
-       return r;
-}
index 98fab2e675a961e6280a90725a7ab45d23593ea8..929b0dfff15e588994c547e0c3a3b437c0ea8bd3 100644 (file)
 #############################################################################*/
 
 #include <assert.h>
+#include <errno.h>
 #include <stdlib.h>
 
 #include <solv/transaction.h>
 
 #include <pakfire/db.h>
+#include <pakfire/downloader.h>
 #include <pakfire/i18n.h>
 #include <pakfire/logging.h>
 #include <pakfire/package.h>
@@ -423,17 +425,58 @@ ERROR:
        return r;
 }
 
+static int pakfire_transaction_download_package(PakfireTransaction transaction,
+               struct pakfire_downloader* downloader, PakfirePackage pkg) {
+       int r = 1;
+       PakfireRepo repo = NULL;
+       struct pakfire_mirrorlist* mirrors = NULL;
+       char* cache_path = NULL;
+
+       // Fetch the repository to download from
+       repo = pakfire_package_get_repo(pkg);
+       if (!repo)
+               goto ERROR;
+
+       // Fetch mirrorlist
+       mirrors = pakfire_repo_get_mirrors(repo);
+       if (!mirrors)
+               goto ERROR;
+
+       // Where to store the package?
+       cache_path = pakfire_package_get_cache_path(pkg);
+       if (!cache_path)
+               goto ERROR;
+
+       // What file to download?
+       const char* filename = pakfire_package_get_filename(pkg);
+       if (!filename)
+               goto ERROR;
+
+       // Add transfer to downloader
+       r = pakfire_downloader_add_transfer(downloader, mirrors, filename, cache_path);
+
+ERROR:
+       if (cache_path)
+               free(cache_path);
+       if (mirrors)
+               pakfire_mirrorlist_unref(mirrors);
+       if (repo)
+               pakfire_repo_unref(repo);
+
+       return r;
+}
+
 PAKFIRE_EXPORT int pakfire_transaction_download(PakfireTransaction transaction) {
-       /*
-               XXX TODO
-               This is not a very pretty solution, because we have to run a downloader
-               for each repository. It would be better to add all downloads to one downloader.
-       */
+       struct pakfire_downloader* downloader;
        int r;
 
-       PakfirePackageList packages = pakfire_packagelist_create(transaction->pakfire);
-       if (!packages)
+       // Initialize the downloader
+       r = pakfire_downloader_create(&downloader, transaction->pakfire);
+       if (r) {
+               ERROR(transaction->pakfire, "Could not initialize downloader: %s\n",
+                       strerror(errno));
                return 1;
+       }
 
        // Add all packages that need to be downloaded
        for (unsigned int i = 0; i < transaction->num_steps; i++) {
@@ -446,38 +489,19 @@ PAKFIRE_EXPORT int pakfire_transaction_download(PakfireTransaction transaction)
                // Fetch the package
                PakfirePackage pkg = pakfire_step_get_package(step);
 
-               // Add it to the list
-               pakfire_packagelist_push(packages, pkg);
-
+               // Enqueue download
+               r = pakfire_transaction_download_package(transaction, downloader, pkg);
                pakfire_package_unref(pkg);
-       }
-
-       // If the list is empty, we do not need to continue
-       if (pakfire_packagelist_count(packages) == 0) {
-               r = 0;
-               goto ERROR;
-       }
-
-       Pool* pool = pakfire_get_solv_pool(transaction->pakfire);
-       Repo* repo;
-       int i;
-
-       FOR_REPOS(i, repo) {
-               PakfireRepo _repo = pakfire_repo_create_from_repo(transaction->pakfire, repo);
-
-               r = pakfire_repo_download_packages(_repo, packages);
-               pakfire_repo_unref(_repo);
 
                if (r)
                        goto ERROR;
        }
 
-       // Success
-       r = 0;
+       // Run the downloader
+       r = pakfire_downloader_run(downloader);
 
 ERROR:
-       if (packages)
-               pakfire_packagelist_unref(packages);
+       pakfire_downloader_unref(downloader);
 
        return r;
 }