From: Michael Tremer Date: Tue, 28 Jan 2025 11:40:27 +0000 (+0000) Subject: repos: Add a convenient way to iterate over all local archives X-Git-Tag: 0.9.30~323 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=887d663a52d674ca95fc541b30960058e217f564;p=pakfire.git repos: Add a convenient way to iterate over all local archives Signed-off-by: Michael Tremer --- diff --git a/src/pakfire/build.c b/src/pakfire/build.c index 10eb47ff..a20f9238 100644 --- a/src/pakfire/build.c +++ b/src/pakfire/build.c @@ -2450,36 +2450,17 @@ ERROR: return r; } -static int pakfire_build_lint_package( - struct pakfire_ctx* ctx, struct pakfire_package* pkg, void* data) { - struct pakfire_archive* archive = NULL; - int r; - - // Fetch the archive - archive = pakfire_package_get_archive(pkg); - if (!archive) { - r = -errno; - goto ERROR; - } - - // Run the linter - r = pakfire_archive_lint(archive, NULL, NULL); - if (r < 0) - goto ERROR; - -ERROR: - if (archive) - pakfire_archive_unref(archive); - - return r; +static int pakfire_build_lint_archive( + struct pakfire_ctx* ctx, struct pakfire_archive* archive, void* data) { + return pakfire_archive_lint(archive, NULL, NULL); } static int pakfire_build_lint(struct pakfire_build* build) { int r; // Lint all packages - r = pakfire_repo_walk_packages(build->repo, - pakfire_build_lint_package, build, PAKFIRE_PACKAGELIST_KEEPGOING); + r = pakfire_repo_walk_archives(build->repo, + pakfire_build_lint_archive, build, PAKFIRE_PACKAGELIST_KEEPGOING); if (r > 0) ERROR(build->ctx, "Linting failed\n"); diff --git a/src/pakfire/repo.c b/src/pakfire/repo.c index aeda4cf0..bd36e4d8 100644 --- a/src/pakfire/repo.c +++ b/src/pakfire/repo.c @@ -2160,3 +2160,71 @@ ERROR: return r; } + +typedef int (*pakfire_repo_walk_archives_callback) + (struct pakfire_ctx* ctx, struct pakfire_archive* archive, void* data); + +struct pakfire_repo_walk_archives_state { + pakfire_repo_walk_archives_callback callback; + void* data; +}; + +static int __pakfire_repo_walk_archives( + struct pakfire_ctx* ctx, struct pakfire_package* pkg, void* data) { + const struct pakfire_repo_walk_archives_state* state = data; + struct pakfire_archive* archive = NULL; + int r; + + // Check if we actually have a callback + if (!state->callback) + return -EINVAL; + + // Fetch the archive + archive = pakfire_package_get_archive(pkg); + if (!archive) + return -errno; + + // Call the callback + r = state->callback(ctx, archive, state->data); + + // Cleanup + pakfire_archive_unref(archive); + + return r; +} + +int pakfire_repo_walk_archives(struct pakfire_repo* self, + int (*callback)(struct pakfire_ctx* ctx, struct pakfire_archive* archive, void* data), void* data, int flags) { + struct pakfire_packagelist* list = NULL; + int r; + + struct pakfire_repo_walk_archives_state state = { + .callback = callback, + .data = data, + }; + + // This only works on local repositories + if (!pakfire_repo_is_local(self)) + return -ENOTSUP; + + // Create a new packagelist + r = pakfire_packagelist_create(&list, self->ctx); + if (r < 0) + goto ERROR; + + // Import all packages + r = pakfire_repo_to_packagelist(self, list); + if (r < 0) + goto ERROR; + + // Iterate over all packages + r = pakfire_packagelist_walk(list, __pakfire_repo_walk_archives, &state, flags); + if (r < 0) + goto ERROR; + +ERROR: + if (list) + pakfire_packagelist_unref(list); + + return r; +} diff --git a/src/pakfire/repo.h b/src/pakfire/repo.h index cac73af0..bbaa2b74 100644 --- a/src/pakfire/repo.h +++ b/src/pakfire/repo.h @@ -137,5 +137,7 @@ struct pakfire_mirrorlist* pakfire_repo_get_mirrorlist(struct pakfire_repo* repo int pakfire_repo_walk_packages(struct pakfire_repo* self, int (*callback)(struct pakfire_ctx* ctx, struct pakfire_package* pkg, void* data), void* data, int flags); +int pakfire_repo_walk_archives(struct pakfire_repo* self, + int (*callback)(struct pakfire_ctx* ctx, struct pakfire_archive* archive, void* data), void* data, int flags); #endif /* PAKFIRE_REPO_H */