]> git.ipfire.org Git - pakfire.git/commitdiff
progress: Anchor progress on the context
authorMichael Tremer <michael.tremer@ipfire.org>
Mon, 16 Oct 2023 16:56:15 +0000 (16:56 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Mon, 16 Oct 2023 17:05:24 +0000 (17:05 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/cli/lib/progressbar.c
src/cli/lib/progressbar.h
src/libpakfire/compress.c
src/libpakfire/ctx.c
src/libpakfire/dist.c
src/libpakfire/include/pakfire/ctx.h
src/libpakfire/include/pakfire/progress.h
src/libpakfire/progress.c

index d226aa1be8d495b750d80895233b8fdcc680df15..af81b09e1c702e8b09db8c21edee508f6a3737d2 100644 (file)
@@ -125,7 +125,7 @@ static void cli_progressbar_free(struct cli_progressbar* p) {
        free(p);
 }
 
-static void __cli_progressbar_free(struct pakfire* pakfire,
+static void __cli_progressbar_free(struct pakfire_ctx* ctx,
                struct pakfire_progress* progress, void* data) {
        struct cli_progressbar* progressbar = data;
 
@@ -241,7 +241,7 @@ ERROR:
        return (void *)(intptr_t)r;
 }
 
-static int cli_progressbar_start(struct pakfire* pakfire, struct pakfire_progress* progress,
+static int cli_progressbar_start(struct pakfire_ctx* ctx, struct pakfire_progress* progress,
                void* data, unsigned long int max_value) {
        struct cli_progressbar* progressbar = data;
 
@@ -252,7 +252,7 @@ static int cli_progressbar_start(struct pakfire* pakfire, struct pakfire_progres
        return pthread_create(&progressbar->renderer, NULL, cli_progressbar_renderer, progressbar);
 }
 
-static int cli_progressbar_finish(struct pakfire* pakfire, struct pakfire_progress* progress,
+static int cli_progressbar_finish(struct pakfire_ctx* ctx, struct pakfire_progress* progress,
                void* data) {
        struct cli_progressbar* progressbar = data;
        void* retval = NULL;
@@ -562,8 +562,7 @@ static int cli_progressbar_add_transfer_speed(struct cli_progressbar* p) {
        return cli_progressbar_add_widget(p, cli_progressbar_transfer_speed, NULL, 0, NULL);
 }
 
-int cli_setup_progressbar(struct pakfire_ctx* ctx, struct pakfire* pakfire,
-               struct pakfire_progress* p, void* data) {
+int cli_setup_progressbar(struct pakfire_ctx* ctx, struct pakfire_progress* p, void* data) {
        struct cli_progressbar* progressbar = NULL;
        int r;
 
index 15d76f13e8e02745aa3110e0bc6bc3263a84b392..d8d782a0a3c12d14b17fa042e068eb0a4447ec9d 100644 (file)
 #ifndef PAKFIRE_CLI_PROGRESSBAR_H
 #define PAKFIRE_CLI_PROGRESSBAR_H
 
-#include <pakfire/pakfire.h>
+#include <pakfire/ctx.h>
 #include <pakfire/progress.h>
 
-int cli_setup_progressbar(struct pakfire_ctx* ctx, struct pakfire* pakfire,
-       struct pakfire_progress* p, void* data);
+int cli_setup_progressbar(struct pakfire_ctx* ctx, struct pakfire_progress* p, void* data);
 
 #endif /* PAKFIRE_CLI_PROGRESSBAR_H */
index 2aae82d7c39abf15ff3a06d688b96d59d59ddd15..d4e0209d521e7a004eeea51d8034c1e116b8f8e2 100644 (file)
@@ -27,6 +27,7 @@
 #include <lzma.h>
 #include <zstd.h>
 
+#include <pakfire/ctx.h>
 #include <pakfire/compress.h>
 #include <pakfire/file.h>
 #include <pakfire/filelist.h>
@@ -788,6 +789,8 @@ int pakfire_extract(struct pakfire* pakfire, struct archive* archive,
        int progress_flags = PAKFIRE_PROGRESS_SHOW_PERCENTAGE;
        int r = 1;
 
+       struct pakfire_ctx* ctx = pakfire_ctx(pakfire);
+
        // Use / if no prefix is set
        if (!prefix)
                prefix = "/";
@@ -823,7 +826,7 @@ int pakfire_extract(struct pakfire* pakfire, struct archive* archive,
                progress_flags |= PAKFIRE_PROGRESS_SHOW_TRANSFER_SPEED;
 
        // Create the progress indicator
-       r = pakfire_progress_create(&data.progress, pakfire, progress_flags, NULL);
+       r = pakfire_progress_create(&data.progress, ctx, progress_flags, NULL);
        if (r)
                goto ERROR;
 
@@ -856,6 +859,8 @@ ERROR:
                pakfire_progress_unref(data.progress);
        if (data.writer)
                archive_write_free(data.writer);
+       if (ctx)
+               pakfire_ctx_unref(ctx);
 
        return r;
 }
@@ -1011,6 +1016,8 @@ int pakfire_compress(struct pakfire* pakfire, struct archive* archive,
                PAKFIRE_PROGRESS_SHOW_BYTES_TRANSFERRED;
        int r = 1;
 
+       struct pakfire_ctx* ctx = pakfire_ctx(pakfire);
+
        struct pakfire_compress data = {
                .pakfire  = pakfire,
                .archive  = archive,
@@ -1031,7 +1038,7 @@ int pakfire_compress(struct pakfire* pakfire, struct archive* archive,
        const size_t size = pakfire_filelist_total_size(filelist);
 
        // Create the progress indicator
-       r = pakfire_progress_create(&data.progress, pakfire, progress_flags, NULL);
+       r = pakfire_progress_create(&data.progress, ctx, progress_flags, NULL);
        if (r)
                goto ERROR;
 
@@ -1069,6 +1076,8 @@ ERROR:
                pakfire_progress_unref(data.progress);
        if (data.linkresolver)
                archive_entry_linkresolver_free(data.linkresolver);
+       if (ctx)
+               pakfire_ctx_unref(ctx);
 
        return r;
 }
index 8a3bd53a7dee8cec948ab5cc60c808d2b9dfa755..b1aff246e396a0e5558cf8ddb42c7680f3dbffb8 100644 (file)
@@ -260,12 +260,11 @@ PAKFIRE_EXPORT void pakfire_ctx_set_progress_callback(struct pakfire_ctx* ctx,
        ctx->progress.data     = data;
 }
 
-int pakfire_ctx_setup_progress(struct pakfire_ctx* ctx, struct pakfire* pakfire,
-               struct pakfire_progress* progress) {
+int pakfire_ctx_setup_progress(struct pakfire_ctx* ctx, struct pakfire_progress* progress) {
        if (!ctx->progress.callback)
                return 0;
 
-       return ctx->progress.callback(ctx, pakfire, ctx->progress.data, progress);
+       return ctx->progress.callback(ctx, ctx->progress.data, progress);
 }
 
 // Pick Solution
index bc649571316b93270ca3f4c5fb0645d38219c683..1b28e9f25d134e776bb6d3eefad6889be313f310 100644 (file)
@@ -353,8 +353,10 @@ static int pakfire_dist_add_sources(struct pakfire* pakfire, struct pakfire_pack
        if (!sources)
                return 0;
 
+       struct pakfire_ctx* ctx = pakfire_ctx(pakfire);
+
        // Create a downloader
-       r = pakfire_downloader_create(&downloader, pakfire);
+       r = pakfire_downloader_create(&downloader, ctx);
        if (r)
                goto ERROR;
 
@@ -386,6 +388,8 @@ ERROR:
                pakfire_downloader_unref(downloader);
        if (mirrorlist)
                pakfire_mirrorlist_unref(mirrorlist);
+       if (ctx)
+               pakfire_ctx_unref(ctx);
        if (sources)
                free(sources);
 
index 05b9de86b71ec51cdb9694b379e161331d1d3331..0ea96bfcf9bb92e251b8cf900d906c3b5403f5ff 100644 (file)
@@ -56,7 +56,7 @@ void pakfire_ctx_set_confirm_callback(struct pakfire_ctx* ctx,
 
 // Progress
 
-typedef int (*pakfire_progress_callback)(struct pakfire_ctx*ctx, struct pakfire* pakfire,
+typedef int (*pakfire_progress_callback)(struct pakfire_ctx* ctx,
        struct pakfire_progress* progress, void* data);
 
 void pakfire_ctx_set_progress_callback(struct pakfire_ctx* ctx,
@@ -82,8 +82,7 @@ int pakfire_ctx_confirm(struct pakfire_ctx* ctx, struct pakfire* pakfire,
 
 // Progress
 
-int pakfire_ctx_setup_progress(struct pakfire_ctx* ctx, struct pakfire* pakfire,
-       struct pakfire_progress* progress);
+int pakfire_ctx_setup_progress(struct pakfire_ctx* ctx, struct pakfire_progress* progress);
 
 // Pick Solution
 
index d62e2d2a286c6bc3b6b7d7726ca156c91b61c04e..60d1ce96afc52163eff108cfac5acffe1179fd77 100644 (file)
@@ -25,6 +25,8 @@
 
 struct pakfire_progress;
 
+#include <pakfire/ctx.h>
+
 enum pakfire_progress_flags {
        PAKFIRE_PROGRESS_NO_PROGRESS            = (1 << 0),
        PAKFIRE_PROGRESS_SHOW_PERCENTAGE        = (1 << 1),
@@ -41,13 +43,13 @@ int pakfire_progress_has_flag(struct pakfire_progress* p, int flag);
        Callbacks
 */
 typedef int (*pakfire_progress_start_callback)
-       (struct pakfire* pakfire, struct pakfire_progress* progress, void* data, unsigned long int value);
+       (struct pakfire_ctx* ctx, struct pakfire_progress* progress, void* data, unsigned long int value);
 typedef int (*pakfire_progress_finish_callback)
-       (struct pakfire* pakfire, struct pakfire_progress* progress, void* data);
+       (struct pakfire_ctx* ctx, struct pakfire_progress* progress, void* data);
 typedef int (*pakfire_progress_update_callback)
-       (struct pakfire* pakfire, struct pakfire_progress* progress, void* data, unsigned long int value);
+       (struct pakfire_ctx* ctx, struct pakfire_progress* progress, void* data, unsigned long int value);
 typedef void (*pakfire_progress_free_callback)
-       (struct pakfire* pakfire, struct pakfire_progress* progress, void* data);
+       (struct pakfire_ctx* ctx, struct pakfire_progress* progress, void* data);
 
 void pakfire_progress_set_callback_data(struct pakfire_progress* p, void* data);
 
@@ -74,7 +76,7 @@ double pakfire_progress_get_transfer_speed(struct pakfire_progress* p);
 #include <pakfire/pakfire.h>
 
 int pakfire_progress_create(struct pakfire_progress** progress,
-       struct pakfire* pakfire, int flags, struct pakfire_progress* parent);
+       struct pakfire_ctx* ctx, int flags, struct pakfire_progress* parent);
 
 struct pakfire_progress* pakfire_progress_ref(struct pakfire_progress* p);
 struct pakfire_progress* pakfire_progress_unref(struct pakfire_progress* p);
index 4fa90b5e6dd0ac3d2fc0ad799337b3ff1effad3f..f1d1c6bb9b9f404a422657f3ea904748d42702b8 100644 (file)
 
 #include <pakfire/ctx.h>
 #include <pakfire/logging.h>
-#include <pakfire/pakfire.h>
 #include <pakfire/private.h>
 #include <pakfire/progress.h>
 
 struct pakfire_progress {
        struct pakfire_ctx* ctx;
-       struct pakfire* pakfire;
        int nrefs;
 
        // Title
@@ -74,33 +72,31 @@ static void pakfire_progress_free(struct pakfire_progress* p) {
 
        // Call the free callback
        if (p->callbacks.free)
-               p->callbacks.free(p->pakfire, p, p->callbacks.data);
+               p->callbacks.free(p->ctx, p, p->callbacks.data);
 
        if (p->parent)
                pakfire_progress_unref(p->parent);
        if (p->title)
                free(p->title);
-       if (p->pakfire)
-               pakfire_unref(p->pakfire);
        if (p->ctx)
                pakfire_ctx_unref(p->ctx);
        free(p);
 }
 
-static int pakfire_progress_default_start_callback(struct pakfire* pakfire,
+static int pakfire_progress_default_start_callback(struct pakfire_ctx* ctx,
                struct pakfire_progress* p, void* data, unsigned long int value) {
        // Fetch the title
        const char* title = pakfire_progress_get_title(p);
 
        // Log the title
        if (title)
-               INFO(pakfire, "%s\n", title);
+               CTX_INFO(ctx, "%s\n", title);
 
        return 0;
 }
 
 int pakfire_progress_create(struct pakfire_progress** progress,
-               struct pakfire* pakfire, int flags, struct pakfire_progress* parent) {
+               struct pakfire_ctx* ctx, int flags, struct pakfire_progress* parent) {
        struct pakfire_progress* p = NULL;
        int r;
 
@@ -110,10 +106,7 @@ int pakfire_progress_create(struct pakfire_progress** progress,
                return -errno;
 
        // Store a reference to the context
-       p->ctx = pakfire_ctx(pakfire);
-
-       // Store a reference to Pakfire
-       p->pakfire = pakfire_ref(pakfire);
+       p->ctx = pakfire_ctx_ref(ctx);
 
        // Initialize the reference counter
        p->nrefs = 1;
@@ -133,7 +126,7 @@ int pakfire_progress_create(struct pakfire_progress** progress,
 
        // Call setup
        if (!pakfire_progress_has_flag(p, PAKFIRE_PROGRESS_NO_PROGRESS)) {
-               r = pakfire_ctx_setup_progress(p->ctx, p->pakfire, p);
+               r = pakfire_ctx_setup_progress(p->ctx, p);
                if (r)
                        goto ERROR;
        }
@@ -219,7 +212,7 @@ int pakfire_progress_start(struct pakfire_progress* p, unsigned long int value)
 
        // Call the start callback
        if (p->callbacks.start) {
-               r = p->callbacks.start(p->pakfire, p, p->callbacks.data, value);
+               r = p->callbacks.start(p->ctx, p, p->callbacks.data, value);
                if (r)
                        return r;
        }
@@ -246,7 +239,7 @@ int pakfire_progress_finish(struct pakfire_progress* p) {
 
        // Call the finish callback
        if (p->callbacks.finish) {
-               r = p->callbacks.finish(p->pakfire, p, p->callbacks.data);
+               r = p->callbacks.finish(p->ctx, p, p->callbacks.data);
                if (r)
                        return r;
        }
@@ -273,7 +266,7 @@ int pakfire_progress_update(struct pakfire_progress* p, unsigned long int value)
 
        // Call the update callback
        if (p->callbacks.update) {
-               r = p->callbacks.update(p->pakfire, p, p->callbacks.data, value);
+               r = p->callbacks.update(p->ctx, p, p->callbacks.data, value);
                if (r)
                        return r;
        }