]> git.ipfire.org Git - pakfire.git/commitdiff
httpclient: Register the progress with the client
authorMichael Tremer <michael.tremer@ipfire.org>
Mon, 12 Aug 2024 10:48:44 +0000 (10:48 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Mon, 12 Aug 2024 10:48:44 +0000 (10:48 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/httpclient.c

index 4ec33229a4f2c16516e5e9de13dda08390ecf1cc..825fe5d8dde15cd0618b30f1a635e9f9650795ae 100644 (file)
@@ -51,6 +51,9 @@ struct pakfire_httpclient {
        // Timer
        sd_event_source* timer;
 
+       // Progress
+       struct pakfire_progress* progress;
+
        int parallel;
 
        // cURL share handle
@@ -440,6 +443,23 @@ static int pakfire_httpclient_setup_curl(struct pakfire_httpclient* client) {
        return 0;
 }
 
+static int pakfire_httpclient_setup_progress(struct pakfire_httpclient* client) {
+       int r;
+
+       const int flags =
+               PAKFIRE_PROGRESS_SHOW_PERCENTAGE |
+               PAKFIRE_PROGRESS_SHOW_ETA |
+               PAKFIRE_PROGRESS_SHOW_BYTES_TRANSFERRED |
+               PAKFIRE_PROGRESS_SHOW_TRANSFER_SPEED;
+
+       // Create a new progress indicator
+       r = pakfire_progress_create(&client->progress, client->ctx, flags, NULL);
+       if (r)
+               return r;
+
+       return 0;
+}
+
 static void pakfire_httpclient_free(struct pakfire_httpclient* client) {
        struct pakfire_xfer_element* x = NULL;
 
@@ -459,6 +479,8 @@ static void pakfire_httpclient_free(struct pakfire_httpclient* client) {
                pakfire_httpclient_xfer_free(x);
        }
 
+       if (client->progress)
+               pakfire_progress_unref(client->progress);
        if (client->share)
                curl_share_cleanup(client->share);
        if (client->curl)
@@ -510,6 +532,11 @@ int pakfire_httpclient_create(struct pakfire_httpclient** client, struct pakfire
        if (r)
                goto ERROR;
 
+       // Setup progress
+       r = pakfire_httpclient_setup_progress(c);
+       if (r)
+               goto ERROR;
+
        // Success
        *client = c;
 
@@ -589,10 +616,6 @@ static unsigned int pakfire_httpclient_total_queued_xfers(struct pakfire_httpcli
 }
 
 int pakfire_httpclient_run(struct pakfire_httpclient* client, const char* title) {
-       struct pakfire_progress* progress = NULL;
-       int progress_flags =
-               PAKFIRE_PROGRESS_SHOW_PERCENTAGE |
-               PAKFIRE_PROGRESS_SHOW_ETA;
        int r = 0;
 
        struct pakfire_xfer_element* x = NULL;
@@ -600,38 +623,21 @@ int pakfire_httpclient_run(struct pakfire_httpclient* client, const char* title)
        // Fetch the total downloadsize
        const size_t downloadsize = pakfire_httpclient_total_downloadsize(client);
 
-       // If we know the downloadsize, we can show more details
-       if (downloadsize) {
-               progress_flags |=
-                       PAKFIRE_PROGRESS_SHOW_BYTES_TRANSFERRED |
-                       PAKFIRE_PROGRESS_SHOW_TRANSFER_SPEED;
-       } else {
-               progress_flags |=
-                       PAKFIRE_PROGRESS_SHOW_COUNTER;
-       }
-
        // Fetch the total number of queued transfers
        const unsigned int num_queued_xfers = pakfire_httpclient_total_queued_xfers(client);
 
-       // Create a new progress indicator
-       r = pakfire_progress_create(&progress, client->ctx, progress_flags, NULL);
+       // Set the title
+       r = pakfire_progress_set_title(client->progress, title);
        if (r)
                goto ERROR;
 
-       // Set the title
-       if (title) {
-               r = pakfire_progress_set_title(progress, title);
-               if (r)
-                       goto ERROR;
-       }
-
        // Start the progress
-       r = pakfire_progress_start(progress, (downloadsize) ? downloadsize : num_queued_xfers);
+       r = pakfire_progress_start(client->progress, (downloadsize) ? downloadsize : num_queued_xfers);
        if (r)
                goto ERROR;
 
        // Make sure that we have up to parallel transfers active
-       r = pakfire_httpclient_start_transfers(client, progress);
+       r = pakfire_httpclient_start_transfers(client, client->progress);
        if (r)
                goto ERROR;
 
@@ -646,7 +652,7 @@ int pakfire_httpclient_run(struct pakfire_httpclient* client, const char* title)
        } while (client->still_running > 0);
 
        // We are finished!
-       r = pakfire_progress_finish(progress);
+       r = pakfire_progress_finish(client->progress);
        if (r)
                goto ERROR;
 
@@ -660,8 +666,5 @@ ERROR:
                pakfire_xfer_fail(x->xfer);
        }
 
-       if (progress)
-               pakfire_progress_unref(progress);
-
        return r;
 }