]> git.ipfire.org Git - pakfire.git/commitdiff
ctx: Move the magic context to here
authorMichael Tremer <michael.tremer@ipfire.org>
Sat, 5 Oct 2024 14:43:24 +0000 (14:43 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 5 Oct 2024 14:43:24 +0000 (14:43 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/ctx.c
src/libpakfire/file.c
src/libpakfire/include/pakfire/ctx.h
src/libpakfire/include/pakfire/pakfire.h
src/libpakfire/pakfire.c

index eb1e084966e1bfb830539bf56575f40c1f9f18b7..417fe66bc70f2250d0846ddd53747b160503db38 100644 (file)
@@ -27,6 +27,8 @@
 #include <syslog.h>
 #include <wordexp.h>
 
+#include <magic.h>
+
 #include <pakfire/config.h>
 #include <pakfire/ctx.h>
 #include <pakfire/logging.h>
@@ -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;
+}
index c096c5dbd4358f598fae0addfa322d0a06571455..c60a1e4390a16b9c46d7ac796c29a29d83c33d42 100644 (file)
@@ -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;
 
index ea09146e3dd81bd72cd59b3dd23f0bf03464bf61..efcbed363012aba1d453697dc8f9ebf522d5df77 100644 (file)
@@ -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.h>
+
+magic_t pakfire_ctx_magic(struct pakfire_ctx* ctx);
+
 #endif /* PAKFIRE_PRIVATE */
 
 #endif /* PAKFIRE_CTX_H */
index bae86bd893271830aeac915a0a897e235a5fe49a..6d0e9c7b5452bd7f4ff9ead0dc54f27a479c8846 100644 (file)
@@ -91,7 +91,6 @@ int pakfire_check(struct pakfire* pakfire, struct pakfire_filelist* errors);
 
 #include <sys/types.h>
 
-#include <magic.h>
 #include <solv/pool.h>
 
 #include <pakfire/config.h>
@@ -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);
index b82dc4e06e216e0c0938759237126685cca421b6..c4a0590d3db5e3df108536c305093377f0b30509 100644 (file)
@@ -36,7 +36,6 @@
 
 #include <archive.h>
 #include <archive_entry.h>
-#include <magic.h>
 #include <solv/evr.h>
 #include <solv/pool.h>
 #include <solv/poolarch.h>
@@ -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;