]> git.ipfire.org Git - pakfire.git/commitdiff
dist: Refactor downloading sources
authorMichael Tremer <michael.tremer@ipfire.org>
Mon, 2 Oct 2023 17:13:25 +0000 (17:13 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Mon, 2 Oct 2023 17:13:25 +0000 (17:13 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/dist.c

index 0be7044d8d49de1d60842e1a4b13cb3524264a2f..e7ebba390b17dd9b3aff28390f25061c8b926624 100644 (file)
@@ -28,6 +28,7 @@
 #include <pakfire/arch.h>
 #include <pakfire/dist.h>
 #include <pakfire/downloader.h>
+#include <pakfire/i18n.h>
 #include <pakfire/linter.h>
 #include <pakfire/logging.h>
 #include <pakfire/package.h>
@@ -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;