]> git.ipfire.org Git - pakfire.git/commitdiff
httpclient: Make the termination code clearer
authorMichael Tremer <michael.tremer@ipfire.org>
Thu, 26 Jun 2025 13:58:05 +0000 (13:58 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Thu, 26 Jun 2025 13:58:05 +0000 (13:58 +0000)
We now simply set a flag when we launched the loop ourselves and
automatically terminate once everything is done.

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

index 9662da42d0f0e2efa0634f526c4ce90dc921292b..2ef55bcddac66099c53aca4acb2ef26680c361f7 100644 (file)
@@ -55,7 +55,7 @@ struct pakfire_httpclient {
 
        // Flags
        enum {
-               PAKFIRE_HTTPCLIENT_STANDALONE = (1 << 0),
+               PAKFIRE_HTTPCLIENT_TERMINATE_ON_COMPLETION = (1 << 0),
        } flags;
 
        // Base URL
@@ -316,10 +316,9 @@ static int pakfire_httpclient_check(struct pakfire_httpclient* self) {
                return r;
 
        // If we control the loop, terminate it if there are no more transfers left
-       if (pakfire_httpclient_has_flag(self, PAKFIRE_HTTPCLIENT_STANDALONE)) {
-               if (self->still_running <= 0)
-                       return sd_event_exit(self->loop, 0);
-       }
+       if (pakfire_httpclient_has_flag(self, PAKFIRE_HTTPCLIENT_TERMINATE_ON_COMPLETION)
+                       && (self->still_running <= 0))
+               return sd_event_exit(self->loop, 0);
 
        return 0;
 }
@@ -519,9 +518,6 @@ static int pakfire_httpclient_setup_loop(struct pakfire_httpclient* self, sd_eve
 
                        return r;
                }
-
-               // We are no in standalone-mode
-               self->flags |= PAKFIRE_HTTPCLIENT_STANDALONE;
        }
 
        // Create a new timer
@@ -771,11 +767,10 @@ int pakfire_httpclient_dequeue(struct pakfire_httpclient* self, struct pakfire_x
 int pakfire_httpclient_run(struct pakfire_httpclient* self, const char* title) {
        int r = 0;
 
-       // This can only be run in standalone-mode
-       if (!pakfire_httpclient_has_flag(self, PAKFIRE_HTTPCLIENT_STANDALONE)) {
-               ERROR(self->ctx, "Trying to launch HTTP client that is not in standalone-mode\n");
-               return -ENOTSUP;
-       }
+       // This function will start the event loop (usually used when we are running
+       // without an event loop in place). We need to remember to terminate the event
+       // loop once we have completed all transfers.
+       self->flags |= PAKFIRE_HTTPCLIENT_TERMINATE_ON_COMPLETION;
 
        // Set the title
        r = pakfire_progress_set_title(self->progress, "%s", title);
@@ -815,5 +810,8 @@ ERROR:
        // We are finished!
        pakfire_progress_finish(self->progress);
 
+       // Reset
+       self->flags &= ~PAKFIRE_HTTPCLIENT_TERMINATE_ON_COMPLETION;
+
        return r;
 }