From: Michael Tremer Date: Sat, 5 Oct 2024 14:43:24 +0000 (+0000) Subject: ctx: Move the magic context to here X-Git-Tag: 0.9.30~1153 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=97bb06ce3aac2cb7f70b773bce3bab540d52971a;p=pakfire.git ctx: Move the magic context to here Signed-off-by: Michael Tremer --- diff --git a/src/libpakfire/ctx.c b/src/libpakfire/ctx.c index eb1e08496..417fe66bc 100644 --- a/src/libpakfire/ctx.c +++ b/src/libpakfire/ctx.c @@ -27,6 +27,8 @@ #include #include +#include + #include #include #include @@ -78,6 +80,9 @@ struct pakfire_ctx { pakfire_pick_solution_callback callback; void* data; } pick_solution; + + // Magic Context + magic_t magic; }; static int parse_log_level(const char* level) { @@ -161,6 +166,9 @@ static int pakfire_ctx_load_config(struct pakfire_ctx* ctx, const char* path) { } static void pakfire_ctx_free(struct pakfire_ctx* ctx) { + // Release Magic Context + if (ctx->magic) + magic_close(ctx->magic); if (ctx->config) pakfire_config_unref(ctx->config); free(ctx); @@ -370,3 +378,37 @@ int pakfire_ctx_pick_solution(struct pakfire_ctx* ctx, struct pakfire* pakfire, return ctx->pick_solution.callback(ctx, pakfire, ctx->pick_solution.data, transaction); } + +// Magic + +magic_t pakfire_ctx_magic(struct pakfire_ctx* ctx) { + int r; + + // Initialize the context if not already done + if (!ctx->magic) { + // Allocate a new context + ctx->magic = magic_open(MAGIC_MIME_TYPE | MAGIC_ERROR | MAGIC_NO_CHECK_TOKENS); + if (!ctx->magic) { + CTX_ERROR(ctx, "Could not allocate magic context: %m\n"); + return NULL; + } + + // Load the database + r = magic_load(ctx->magic, NULL); + if (r) { + CTX_ERROR(ctx, "Could not open the magic database: %s\n", magic_error(ctx->magic)); + goto ERROR; + } + } + + return ctx->magic; + +ERROR: + if (ctx->magic) + magic_close(ctx->magic); + + // Reset the pointer + ctx->magic = NULL; + + return NULL; +} diff --git a/src/libpakfire/file.c b/src/libpakfire/file.c index c096c5dbd..c60a1e439 100644 --- a/src/libpakfire/file.c +++ b/src/libpakfire/file.c @@ -1319,7 +1319,7 @@ int pakfire_file_detect_mimetype(struct pakfire_file* file) { return 0; // Fetch the magic cookie - magic_t magic = pakfire_get_magic(file->pakfire); + magic_t magic = pakfire_ctx_magic(file->ctx); if (!magic) return 1; diff --git a/src/libpakfire/include/pakfire/ctx.h b/src/libpakfire/include/pakfire/ctx.h index ea09146e3..efcbed363 100644 --- a/src/libpakfire/include/pakfire/ctx.h +++ b/src/libpakfire/include/pakfire/ctx.h @@ -110,6 +110,12 @@ int pakfire_ctx_setup_progress(struct pakfire_ctx* ctx, struct pakfire_progress* int pakfire_ctx_pick_solution(struct pakfire_ctx* ctx, struct pakfire* pakfire, struct pakfire_transaction* transaction); +// Magic + +#include + +magic_t pakfire_ctx_magic(struct pakfire_ctx* ctx); + #endif /* PAKFIRE_PRIVATE */ #endif /* PAKFIRE_CTX_H */ diff --git a/src/libpakfire/include/pakfire/pakfire.h b/src/libpakfire/include/pakfire/pakfire.h index bae86bd89..6d0e9c7b5 100644 --- a/src/libpakfire/include/pakfire/pakfire.h +++ b/src/libpakfire/include/pakfire/pakfire.h @@ -91,7 +91,6 @@ int pakfire_check(struct pakfire* pakfire, struct pakfire_filelist* errors); #include -#include #include #include @@ -126,8 +125,6 @@ int pakfire_confirm(struct pakfire* pakfire, const char* message, const char* qu int pakfire_pick_solution(struct pakfire* pakfire, struct pakfire_transaction* transaction); int pakfire_setup_progress(struct pakfire* pakfire, struct pakfire_progress* progress); -magic_t pakfire_get_magic(struct pakfire* pakfire); - const char* pakfire_get_distro_name(struct pakfire* pakfire); const char* pakfire_get_distro_id(struct pakfire* pakfire); const char* pakfire_get_distro_vendor(struct pakfire* pakfire); diff --git a/src/libpakfire/pakfire.c b/src/libpakfire/pakfire.c index b82dc4e06..c4a0590d3 100644 --- a/src/libpakfire/pakfire.c +++ b/src/libpakfire/pakfire.c @@ -36,7 +36,6 @@ #include #include -#include #include #include #include @@ -107,9 +106,6 @@ struct pakfire { // Distro struct pakfire_distro distro; - // Magic Context - magic_t magic; - // States unsigned int destroy_on_free:1; unsigned int pool_ready:1; @@ -393,10 +389,6 @@ static void pakfire_free(struct pakfire* pakfire) { pakfire_repo_unref(repo); } - // Release Magic Context - if (pakfire->magic) - magic_close(pakfire->magic); - // Release lock (if not already done so) pakfire_release_lock(pakfire); @@ -1057,39 +1049,6 @@ int __pakfire_cache_path(struct pakfire* pakfire, char* path, size_t length, return __pakfire_path_append(path, length, pakfire->cache_path, buffer); } -magic_t pakfire_get_magic(struct pakfire* pakfire) { - int r; - - // Initialize the context if not already done - if (!pakfire->magic) { - // Allocate a new context - pakfire->magic = magic_open(MAGIC_MIME_TYPE | MAGIC_ERROR | MAGIC_NO_CHECK_TOKENS); - if (!pakfire->magic) { - CTX_ERROR(pakfire->ctx, "Could not allocate magic context: %m\n"); - return NULL; - } - - // Load the database - r = magic_load(pakfire->magic, NULL); - if (r) { - CTX_ERROR(pakfire->ctx, "Could not open the magic database: %s\n", - magic_error(pakfire->magic)); - goto ERROR; - } - } - - return pakfire->magic; - -ERROR: - if (pakfire->magic) - magic_close(pakfire->magic); - - // Reset the pointer - pakfire->magic = NULL; - - return NULL; -} - int pakfire_repo_walk(struct pakfire* pakfire, pakfire_repo_walk_callback callback, void* p) { struct pakfire_repo* repo = NULL;