From: Michael Tremer Date: Mon, 2 Oct 2023 17:13:25 +0000 (+0000) Subject: dist: Refactor downloading sources X-Git-Tag: 0.9.30~1560 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=6165d29ada766685983d085fe346e57bddcc9de4;p=pakfire.git dist: Refactor downloading sources Signed-off-by: Michael Tremer --- diff --git a/src/libpakfire/dist.c b/src/libpakfire/dist.c index 0be7044d8..e7ebba390 100644 --- a/src/libpakfire/dist.c +++ b/src/libpakfire/dist.c @@ -28,6 +28,7 @@ #include #include #include +#include #include #include #include @@ -219,17 +220,11 @@ ERROR: return r; } -static int pakfire_dist_create_downloader_and_mirrorlist( - struct pakfire* pakfire, struct pakfire_parser* makefile, - struct pakfire_downloader** downloader, struct pakfire_mirrorlist** mirrorlist) { +static int pakfire_dist_get_mirrorlist(struct pakfire* pakfire, + struct pakfire_parser* makefile, struct pakfire_mirrorlist** list) { + struct pakfire_mirrorlist* m = NULL; char* p = NULL; - - // Create the downloader - int r = pakfire_downloader_create(downloader, pakfire); - if (r) { - ERROR(pakfire, "Could not initialize downloader\n"); - return r; - } + int r; // Fetch source_dl char* source_dl = pakfire_parser_get(makefile, NULL, "source_dl"); @@ -239,7 +234,7 @@ static int pakfire_dist_create_downloader_and_mirrorlist( return 0; // Create mirrorlist - r = pakfire_mirrorlist_create(mirrorlist, pakfire); + r = pakfire_mirrorlist_create(&m, pakfire); if (r) { ERROR(pakfire, "Could not create the mirrorlist\n"); goto ERROR; @@ -247,9 +242,8 @@ static int pakfire_dist_create_downloader_and_mirrorlist( // Add all mirrors const char* mirror = strtok_r(source_dl, " ", &p); - while (mirror) { - r = pakfire_mirrorlist_add_mirror(*mirrorlist, mirror); + r = pakfire_mirrorlist_add_mirror(m, mirror); if (r) goto ERROR; @@ -257,21 +251,59 @@ static int pakfire_dist_create_downloader_and_mirrorlist( } // Success - r = 0; + *list = pakfire_mirrorlist_ref(m); ERROR: + if (m) + pakfire_mirrorlist_unref(m); if (source_dl) free(source_dl); return r; } +static int pakfire_dist_download_source(struct pakfire_downloader* downloader, + struct pakfire_mirrorlist* mirrorlist, const char* filename, const char* cache_path) { + struct pakfire_transfer* transfer = NULL; + int r; + + // Do not download if the file exists + if (pakfire_path_exists(cache_path)) + return 0; + + // Create a new transfer + r = pakfire_downloader_transfer_create(&transfer, downloader, filename); + if (r) + goto ERROR; + + // Set the mirrorlist + r = pakfire_downloader_transfer_set_mirrorlist(transfer, mirrorlist); + if (r) + goto ERROR; + + // Set the target + r = pakfire_downloader_transfer_set_target(transfer, cache_path); + if (r) + goto ERROR; + + // Run the download + r = pakfire_downloader_transfer_run(transfer, 0); + if (r) + goto ERROR; + +ERROR: + if (transfer) + pakfire_downloader_transfer_unref(transfer); + + return r; +} + static int pakfire_dist_add_source(struct pakfire* pakfire, struct pakfire_packager* packager, struct pakfire_package* pkg, struct pakfire_downloader* downloader, struct pakfire_mirrorlist* mirrorlist, const char* filename) { - int r; char archive_path[PATH_MAX]; char cache_path[PATH_MAX]; + int r; const char* name = pakfire_package_get_string(pkg, PAKFIRE_PKG_NAME); @@ -280,13 +312,10 @@ static int pakfire_dist_add_source(struct pakfire* pakfire, struct pakfire_packa if (r) return r; - // Download the file if it does not exist in the cache - if (access(cache_path, R_OK) != 0) { - r = pakfire_downloader_retrieve(downloader, NULL, mirrorlist, - NULL, filename, cache_path, 0, NULL, 0, 0); - if (r) - return r; - } + // Download the source file + r = pakfire_dist_download_source(downloader, mirrorlist, filename, cache_path); + if (r) + return r; r = pakfire_string_format(archive_path, "files/%s", filename); if (r) @@ -298,6 +327,8 @@ static int pakfire_dist_add_source(struct pakfire* pakfire, struct pakfire_packa static int pakfire_dist_add_sources(struct pakfire* pakfire, struct pakfire_packager* packager, struct pakfire_package* pkg, struct pakfire_parser* makefile) { + struct pakfire_downloader* downloader = NULL; + struct pakfire_mirrorlist* mirrorlist = NULL; char* sources = NULL; char* p = NULL; int r; @@ -309,12 +340,13 @@ static int pakfire_dist_add_sources(struct pakfire* pakfire, struct pakfire_pack if (!sources) return 0; - struct pakfire_downloader* downloader = NULL; - struct pakfire_mirrorlist* mirrorlist = NULL; + // Create a downloader + r = pakfire_downloader_create(&downloader, pakfire); + if (r) + goto ERROR; - // Create a downloader and mirrorlist - int r = pakfire_dist_create_downloader_and_mirrorlist(pakfire, makefile, - &downloader, &mirrorlist); + // Fetch the mirrorlist + r = pakfire_dist_get_mirrorlist(pakfire, makefile, &mirrorlist); if (r) goto ERROR;