From 8e34342c6cf0b4ae9c592837cce4f8384ec59f30 Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Fri, 25 Oct 2024 09:59:50 +0000 Subject: [PATCH] filelist: Remove the legacy logger and some mild refactoring Signed-off-by: Michael Tremer --- src/libpakfire/filelist.c | 50 ++++++++++++++++++++++++--------------- 1 file changed, 31 insertions(+), 19 deletions(-) diff --git a/src/libpakfire/filelist.c b/src/libpakfire/filelist.c index be59048fd..f68234416 100644 --- a/src/libpakfire/filelist.c +++ b/src/libpakfire/filelist.c @@ -22,11 +22,10 @@ #include #include +// libarchive #include -// Enable legacy logging -#define PAKFIRE_LEGACY_LOGGING - +#include #include #include #include @@ -52,26 +51,35 @@ struct pakfire_filelist { }; PAKFIRE_EXPORT int pakfire_filelist_create(struct pakfire_filelist** list, struct pakfire* pakfire) { - struct pakfire_filelist* l = calloc(1, sizeof(*l)); + struct pakfire_filelist* l = NULL; + + // Allocate a new object + l = calloc(1, sizeof(*l)); if (!l) - return -ENOMEM; + return -errno; // Store a reference to the context l->ctx = pakfire_ctx(pakfire); + // Store a reference to Pakfire l->pakfire = pakfire_ref(pakfire); + + // Initialize the reference counter l->nrefs = 1; // Initialise files TAILQ_INIT(&l->files); + // Return the pointer *list = l; + return 0; } static void pakfire_filelist_free(struct pakfire_filelist* list) { pakfire_filelist_clear(list); - pakfire_unref(list->pakfire); + if (list->pakfire) + pakfire_unref(list->pakfire); if (list->ctx) pakfire_ctx_unref(list->ctx); free(list); @@ -155,7 +163,7 @@ PAKFIRE_EXPORT int pakfire_filelist_add(struct pakfire_filelist* list, struct pa // Allocate a new element element = calloc(1, sizeof *element); if (!element) { - ERROR(list->pakfire, "Could not allocate a new filelist element: %m\n"); + CTX_ERROR(list->ctx, "Could not allocate a new filelist element: %m\n"); return 1; } @@ -317,25 +325,25 @@ int pakfire_filelist_scan(struct pakfire_filelist* list, const char* root, return 1; } - DEBUG(list->pakfire, "Scanning %s...\n", root); + CTX_DEBUG(list->ctx, "Scanning %s...\n", root); if (includes) { - DEBUG(list->pakfire, " Includes:\n"); + CTX_DEBUG(list->ctx, " Includes:\n"); for (const char** include = includes; *include; include++) - DEBUG(list->pakfire, " %s\n", *include); + CTX_DEBUG(list->ctx, " %s\n", *include); } if (excludes) { - DEBUG(list->pakfire, " Excludes:\n"); + CTX_DEBUG(list->ctx, " Excludes:\n"); for (const char** exclude = excludes; *exclude; exclude++) - DEBUG(list->pakfire, " %s\n", *exclude); + CTX_DEBUG(list->ctx, " %s\n", *exclude); } // Check if the path exists if (!pakfire_path_exists(root)) { - DEBUG(list->pakfire, "Path to scan (%s) does not exist\n", root); + CTX_DEBUG(list->ctx, "Path to scan (%s) does not exist\n", root); r = 0; goto ERROR; } @@ -348,8 +356,8 @@ int pakfire_filelist_scan(struct pakfire_filelist* list, const char* root, // Start reading from here r = archive_read_disk_open(reader, root); if (r) { - ERROR(list->pakfire, "Could not open %s: %s\n", root, - archive_error_string(reader)); + CTX_ERROR(list->ctx, "Could not open %s: %s\n", + root, archive_error_string(reader)); goto ERROR; } @@ -357,7 +365,7 @@ int pakfire_filelist_scan(struct pakfire_filelist* list, const char* root, r = archive_read_disk_set_metadata_filter_callback(reader, pakfire_filelist_scan_filter, &matches); if (r) { - ERROR(list->pakfire, "Could not set filter callback: %s\n", + CTX_ERROR(list->ctx, "Could not set filter callback: %s\n", archive_error_string(reader)); goto ERROR; } @@ -379,7 +387,7 @@ int pakfire_filelist_scan(struct pakfire_filelist* list, const char* root, // Raise any other errors default: - ERROR(list->pakfire, "Could not read next file: %s\n", + CTX_ERROR(list->ctx, "Could not read next file: %s\n", archive_error_string(reader)); goto ERROR; } @@ -480,12 +488,16 @@ static int __pakfire_filelist_dump( struct pakfire* pakfire, struct pakfire_file* file, void* data) { int* flags = (int*)data; + struct pakfire_ctx* ctx = pakfire_ctx(pakfire); + char* s = pakfire_file_dump(file, *flags); if (s) { - INFO(pakfire, "%s\n", s); + CTX_INFO(ctx, "%s\n", s); free(s); } + pakfire_ctx_unref(ctx); + return 0; } @@ -504,7 +516,7 @@ int pakfire_filelist_verify(struct pakfire_filelist* list, struct pakfire_fileli const size_t length = pakfire_filelist_length(list); - DEBUG(list->pakfire, "Verifying filelist (%zu file(s))...\n", length); + CTX_DEBUG(list->ctx, "Verifying filelist (%zu file(s))...\n", length); // Setup progress r = pakfire_progress_create(&progress, list->ctx, -- 2.39.5