]> git.ipfire.org Git - pakfire.git/commitdiff
filelist: Remove the legacy logger and some mild refactoring
authorMichael Tremer <michael.tremer@ipfire.org>
Fri, 25 Oct 2024 09:59:50 +0000 (09:59 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Fri, 25 Oct 2024 09:59:50 +0000 (09:59 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/filelist.c

index be59048fda802ab445df71fce7d338d9924bd745..f6823441674cadb07c357f9c34504e01a8547454 100644 (file)
 #include <stdlib.h>
 #include <sys/queue.h>
 
+// libarchive
 #include <archive.h>
 
-// Enable legacy logging
-#define PAKFIRE_LEGACY_LOGGING
-
+#include <pakfire/ctx.h>
 #include <pakfire/file.h>
 #include <pakfire/filelist.h>
 #include <pakfire/i18n.h>
@@ -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,