From: Michael Tremer Date: Fri, 31 Jan 2025 12:46:42 +0000 (+0000) Subject: xfer: Tidy up mirror handling X-Git-Tag: 0.9.30~213 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=6514f0a8fbfe0a45f23c2fc990156969b933008e;p=pakfire.git xfer: Tidy up mirror handling No functional changes Signed-off-by: Michael Tremer --- diff --git a/src/pakfire/xfer.c b/src/pakfire/xfer.c index 3dee705d..44df4880 100644 --- a/src/pakfire/xfer.c +++ b/src/pakfire/xfer.c @@ -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) {