]> git.ipfire.org Git - pakfire.git/commitdiff
xfer: Keep a reference to ourselves
authorMichael Tremer <michael.tremer@ipfire.org>
Sun, 13 Oct 2024 10:18:17 +0000 (10:18 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sun, 13 Oct 2024 10:18:17 +0000 (10:18 +0000)
This is necessary so that the handle won't be cleaned up when the last
reference to the xfer is gone. It should be good enough to add the xfer
to the HTTP client and then wait for everything to complete.

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

index beb12f307d890579b42977db3ef4ffecdaf6746b..c086b95436396726a4472b73b28dcfcb706c2143 100644 (file)
@@ -307,7 +307,7 @@ static int pakfire_xfer_setup(struct pakfire_xfer* xfer) {
        curl_easy_setopt(xfer->handle, CURLOPT_ACCEPT_ENCODING, "");
 
        // Reference back to this xfer
-       curl_easy_setopt(xfer->handle, CURLOPT_PRIVATE, xfer);
+       curl_easy_setopt(xfer->handle, CURLOPT_PRIVATE, pakfire_xfer_ref(xfer));
 
        // Follow any redirects
        curl_easy_setopt(xfer->handle, CURLOPT_FOLLOWLOCATION, 1);
@@ -1130,6 +1130,7 @@ static int pakfire_xfer_save(struct pakfire_xfer* xfer) {
 }
 
 pakfire_xfer_error_code_t pakfire_xfer_done(struct pakfire_xfer* xfer, int code) {
+       struct pakfire_xfer* private = NULL;
        CURL* h = xfer->handle;
        int r;
        const char* scheme = NULL;
@@ -1142,6 +1143,29 @@ pakfire_xfer_error_code_t pakfire_xfer_done(struct pakfire_xfer* xfer, int code)
        curl_off_t upload_size = 0;
        curl_off_t upload_speed = 0;
 
+       // Fetch the private pointer to this xfer
+       r = curl_easy_getinfo(h, CURLINFO_PRIVATE, &private);
+       if (r) {
+               CTX_ERROR(xfer->ctx,
+                       "Could not fetch the private pointer: %s\n", curl_easy_strerror(r));
+               goto ERROR;
+       }
+
+       // Check if the private pointer was set
+       if (!private) {
+               CTX_ERROR(xfer->ctx, "Private pointer not set\n");
+               r = -EINVAL;
+               goto ERROR;
+       }
+
+       // Reset the private pointer
+       r = curl_easy_setopt(h, CURLOPT_PRIVATE, NULL);
+       if (r) {
+               CTX_ERROR(xfer->ctx,
+                       "Could not reset the private pointer: %s\n", curl_easy_strerror(r));
+               goto ERROR;
+       }
+
        // Finish progress
        r = pakfire_progress_finish(xfer->progress);
        if (r)
@@ -1283,6 +1307,8 @@ ERROR:
        // Remove the handle
        if (xfer->client)
                pakfire_httpclient_remove_xfer(xfer->client, xfer);
+       if (private)
+               pakfire_xfer_unref(private);
 
        return r;
 }