From: Michael Tremer Date: Mon, 16 Oct 2023 16:56:15 +0000 (+0000) Subject: progress: Anchor progress on the context X-Git-Tag: 0.9.30~1482 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=be12460fd3ef8d88656f5221438868f448a36780;p=pakfire.git progress: Anchor progress on the context Signed-off-by: Michael Tremer --- diff --git a/src/cli/lib/progressbar.c b/src/cli/lib/progressbar.c index d226aa1be..af81b09e1 100644 --- a/src/cli/lib/progressbar.c +++ b/src/cli/lib/progressbar.c @@ -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; diff --git a/src/cli/lib/progressbar.h b/src/cli/lib/progressbar.h index 15d76f13e..d8d782a0a 100644 --- a/src/cli/lib/progressbar.h +++ b/src/cli/lib/progressbar.h @@ -21,10 +21,9 @@ #ifndef PAKFIRE_CLI_PROGRESSBAR_H #define PAKFIRE_CLI_PROGRESSBAR_H -#include +#include #include -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 */ diff --git a/src/libpakfire/compress.c b/src/libpakfire/compress.c index 2aae82d7c..d4e0209d5 100644 --- a/src/libpakfire/compress.c +++ b/src/libpakfire/compress.c @@ -27,6 +27,7 @@ #include #include +#include #include #include #include @@ -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; } diff --git a/src/libpakfire/ctx.c b/src/libpakfire/ctx.c index 8a3bd53a7..b1aff246e 100644 --- a/src/libpakfire/ctx.c +++ b/src/libpakfire/ctx.c @@ -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 diff --git a/src/libpakfire/dist.c b/src/libpakfire/dist.c index bc6495713..1b28e9f25 100644 --- a/src/libpakfire/dist.c +++ b/src/libpakfire/dist.c @@ -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); diff --git a/src/libpakfire/include/pakfire/ctx.h b/src/libpakfire/include/pakfire/ctx.h index 05b9de86b..0ea96bfcf 100644 --- a/src/libpakfire/include/pakfire/ctx.h +++ b/src/libpakfire/include/pakfire/ctx.h @@ -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 diff --git a/src/libpakfire/include/pakfire/progress.h b/src/libpakfire/include/pakfire/progress.h index d62e2d2a2..60d1ce96a 100644 --- a/src/libpakfire/include/pakfire/progress.h +++ b/src/libpakfire/include/pakfire/progress.h @@ -25,6 +25,8 @@ struct pakfire_progress; +#include + 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 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); diff --git a/src/libpakfire/progress.c b/src/libpakfire/progress.c index 4fa90b5e6..f1d1c6bb9 100644 --- a/src/libpakfire/progress.c +++ b/src/libpakfire/progress.c @@ -24,13 +24,11 @@ #include #include -#include #include #include 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; }