From: Michael Tremer Date: Wed, 24 Mar 2021 16:27:09 +0000 (+0000) Subject: Create a unified downloader for all repositories X-Git-Tag: 0.9.28~1285^2~467 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=751ed354d317f6050579f2c820dab8563e87955e;p=pakfire.git Create a unified downloader for all repositories Signed-off-by: Michael Tremer --- diff --git a/src/libpakfire/include/pakfire/repo.h b/src/libpakfire/include/pakfire/repo.h index 8e511b7bb..72f771cae 100644 --- a/src/libpakfire/include/pakfire/repo.h +++ b/src/libpakfire/include/pakfire/repo.h @@ -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 +#include + 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 diff --git a/src/libpakfire/repo.c b/src/libpakfire/repo.c index be06c568f..d1ac432a8 100644 --- a/src/libpakfire/repo.c +++ b/src/libpakfire/repo.c @@ -40,7 +40,6 @@ #include #include #include -#include #include #include #include @@ -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; -} diff --git a/src/libpakfire/transaction.c b/src/libpakfire/transaction.c index 98fab2e67..929b0dfff 100644 --- a/src/libpakfire/transaction.c +++ b/src/libpakfire/transaction.c @@ -19,11 +19,13 @@ #############################################################################*/ #include +#include #include #include #include +#include #include #include #include @@ -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; }