From: Michael Tremer Date: Thu, 17 Oct 2024 18:50:24 +0000 (+0000) Subject: linter: Refactor X-Git-Tag: 0.9.30~1022 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=d7411325702328c9bf7b10bcd406c19aff991863;p=pakfire.git linter: Refactor Signed-off-by: Michael Tremer --- diff --git a/src/libpakfire/dist.c b/src/libpakfire/dist.c index 4d1407778..d23f8602c 100644 --- a/src/libpakfire/dist.c +++ b/src/libpakfire/dist.c @@ -31,7 +31,6 @@ #include #include #include -#include #include #include #include @@ -480,11 +479,6 @@ PAKFIRE_EXPORT int pakfire_dist(struct pakfire* pakfire, const char* path, if (r) goto ERROR; - // Perform some linting - r = pakfire_lint_makefile(pakfire, makefile, path, pkg); - if (r) - goto ERROR; - // Create a packager r = pakfire_packager_create(&packager, pakfire, pkg); if (r) @@ -505,6 +499,8 @@ PAKFIRE_EXPORT int pakfire_dist(struct pakfire* pakfire, const char* path, if (r) goto ERROR; + // XXX Lint the package + ERROR: if (packager) pakfire_packager_unref(packager); diff --git a/src/libpakfire/include/pakfire/linter.h b/src/libpakfire/include/pakfire/linter.h index 3c3626a97..abe09f97c 100644 --- a/src/libpakfire/include/pakfire/linter.h +++ b/src/libpakfire/include/pakfire/linter.h @@ -21,11 +21,17 @@ #ifndef PAKFIRE_LINTER_H #define PAKFIRE_LINTER_H +#include #include -#include -#include -int pakfire_lint_makefile(struct pakfire* pakfire, struct pakfire_parser* makefile, - const char* path, struct pakfire_package* pkg); +struct pakfire_linter; + +int pakfire_linter_create(struct pakfire_linter** linter, + struct pakfire_ctx* ctx, struct pakfire_package* pkg); + +struct pakfire_linter* pakfire_linter_ref(struct pakfire_linter* linter); +struct pakfire_linter* pakfire_linter_unref(struct pakfire_linter* linter); + +int pakfire_linter_lint(struct pakfire_linter* linter); #endif /* PAKFIRE_LINTER_H */ diff --git a/src/libpakfire/linter.c b/src/libpakfire/linter.c index 5b6608f41..0b2d37f40 100644 --- a/src/libpakfire/linter.c +++ b/src/libpakfire/linter.c @@ -18,61 +18,84 @@ # # #############################################################################*/ -// Enable legacy logging -#define PAKFIRE_LEGACY_LOGGING +#include +#include #include #include #include -#include -#include -#include -#include -#include - -static int pakfire_lint_makefile_name(struct pakfire* pakfire, - const char* path, struct pakfire_package* pkg) { - char basename[PATH_MAX]; - int r; - // Fetch the package name - const char* name = pakfire_package_get_string(pkg, PAKFIRE_PKG_NAME); +struct pakfire_linter { + struct pakfire_ctx* ctx; + int nrefs; - // Check if the makefile ended on .nm - if (!pakfire_string_endswith(path, ".nm")) { - ERROR(pakfire, "Invalid extension for makefile: %s\n", path); - return 1; - } + // Package + struct pakfire_package* pkg; +}; - // Determine the basename of the file - r = pakfire_path_basename(basename, path); - if (r) - return r; +static void pakfire_linter_free(struct pakfire_linter* linter) { + if (linter->pkg) + pakfire_package_unref(linter->pkg); + if (linter->ctx) + pakfire_ctx_unref(linter->ctx); + free(linter); +} - // Remove the file extension - r = pakfire_path_strip_extension(basename); - if (r) { - ERROR(pakfire, "Could not strip extension from makefile: %m\n"); - return r; - } +int pakfire_linter_create(struct pakfire_linter** linter, + struct pakfire_ctx* ctx, struct pakfire_package* pkg) { + struct pakfire_linter* l = NULL; + + // Allocate a new object + l = calloc(1, sizeof(*l)); + if (!l) + return -errno; + + // Reference the context + l->ctx = pakfire_ctx_ref(ctx); + + // Initialize reference counter + l->nrefs = 1; + + // Store the package + l->pkg = pakfire_package_ref(pkg); + + // Return the pointer + *linter = l; + + return 0; +} + +struct pakfire_linter* pakfire_linter_ref(struct pakfire_linter* linter) { + ++linter->nrefs; + + return linter; +} + +struct pakfire_linter* pakfire_linter_unref(struct pakfire_linter* linter) { + if (--linter->nrefs > 0) + return linter; + + pakfire_linter_free(linter); + return NULL; +} + +static int pakfire_linter_name(struct pakfire_linter* linter) { + int r; + + // Fetch the package name + const char* name = pakfire_package_get_string(linter->pkg, PAKFIRE_PKG_NAME); - // Check if the package name matches - if (strcmp(basename, name) != 0) { - ERROR(pakfire, "%s: Makefile has a different package name (%s)\n", - path, name); - return 1; - } + // XXX Do something here... return 0; } -int pakfire_lint_makefile(struct pakfire* pakfire, struct pakfire_parser* makefile, - const char* path, struct pakfire_package* pkg) { +int pakfire_linter_lint(struct pakfire_linter* linter) { int r; - // Check makefile name - r = pakfire_lint_makefile_name(pakfire, path, pkg); - if (r) + // Lint the name + r = pakfire_linter_name(linter); + if (r < 0) return r; return 0;