]> git.ipfire.org Git - pakfire.git/commitdiff
xfer: Tidy up mirror handling
authorMichael Tremer <michael.tremer@ipfire.org>
Fri, 31 Jan 2025 12:46:42 +0000 (12:46 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Fri, 31 Jan 2025 13:05:30 +0000 (13:05 +0000)
No functional changes

Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/pakfire/xfer.c

index 3dee705de4c2a1372acb3d2c09898b05383dec7b..44df48807c7d02af705f9db37bb386299bf4fa55 100644 (file)
@@ -518,28 +518,54 @@ const char* pakfire_xfer_get_effective_url(struct pakfire_xfer* xfer) {
        return xfer->effective_url;
 }
 
+/*
+       Helper function to set a chosen mirror
+*/
+static int pakfire_xfer_select_mirror(struct pakfire_xfer* self, struct pakfire_mirror* mirror) {
+       // Free the previous mirror
+       if (self->mirror) {
+               pakfire_mirror_unref(self->mirror);
+               self->mirror = NULL;
+       }
+
+       // Set the new mirror (if available)
+       if (mirror) {
+               DEBUG(self->ctx, "Selected mirror %s\n", pakfire_mirror_get_url(mirror));
+
+               self->mirror = pakfire_mirror_ref(mirror);
+       }
+
+       return 0;
+}
+
 int pakfire_xfer_set_mirrorlist(struct pakfire_xfer* xfer, struct pakfire_mirrorlist* mirrors) {
+       struct pakfire_mirror* mirror = NULL;
+       int r;
+
        // Drop all references to a former mirrorlist
        if (xfer->mirrors) {
                pakfire_mirrorlist_unref(xfer->mirrors);
                xfer->mirrors = NULL;
        }
 
-       // Drop all references to the formerly selected mirror
-       if (xfer->mirror) {
-               pakfire_mirror_unref(xfer->mirror);
-               xfer->mirror = NULL;
-       }
-
        // If a new list was passed, we store a reference to it
        if (mirrors) {
                xfer->mirrors = pakfire_mirrorlist_ref(mirrors);
 
                // Select the first mirror from the list
-               xfer->mirror = pakfire_mirrorlist_get_first(xfer->mirrors);
+               mirror = pakfire_mirrorlist_get_first(xfer->mirrors);
        }
 
-       return 0;
+       // Select the mirror (or reset if NULL)
+       r = pakfire_xfer_select_mirror(xfer, mirror);
+       if (r < 0)
+               goto ERROR;
+
+ERROR:
+       if (mirror)
+               pakfire_mirror_unref(mirror);
+
+       return r;
 }
 
 // Size
@@ -915,34 +941,29 @@ static pakfire_xfer_error_code_t pakfire_xfer_code(CURLcode code, int http_statu
        }
 }
 
+/*
+       Called after something went wrong and we want to try again with another mirror
+*/
 static int pakfire_xfer_next_mirror(struct pakfire_xfer* xfer) {
        struct pakfire_mirror* mirror = NULL;
+       int r = 0;
 
        // If our mirror is broken, we select the next one
        if (xfer->mirror) {
                // Choose the next mirror
                mirror = pakfire_mirrorlist_get_next(xfer->mirrors, xfer->mirror);
 
-               // Free the previous mirror
-               pakfire_mirror_unref(xfer->mirror);
-
                // Store the new mirror
-               if (mirror) {
-                       DEBUG(xfer->ctx, "Selected mirror %s\n", pakfire_mirror_get_url(mirror));
-
-                       xfer->mirror = pakfire_mirror_ref(mirror);
-
-               // If we could not find a mirror, we clear the pointer
-               } else {
-                       xfer->mirror = NULL;
-               }
+               r = pakfire_xfer_select_mirror(xfer, mirror);
+               if (r < 0)
+                       goto ERROR;
        }
 
-       // Cleanup
+ERROR:
        if (mirror)
                pakfire_mirror_unref(mirror);
 
-       return 0;
+       return r;
 }
 
 static int pakfire_xfer_fail(struct pakfire_xfer* xfer, int code) {