]> git.ipfire.org Git - pakfire.git/commitdiff
repos: Add a convenient way to iterate over all local archives
authorMichael Tremer <michael.tremer@ipfire.org>
Tue, 28 Jan 2025 11:40:27 +0000 (11:40 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Tue, 28 Jan 2025 11:40:27 +0000 (11:40 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/pakfire/build.c
src/pakfire/repo.c
src/pakfire/repo.h

index 10eb47ffc07d53f7e8e9bf2e088ca2771f878d72..a20f92386668c5403c26c3bac478586847b55045 100644 (file)
@@ -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");
index aeda4cf0b8e9122d922fffb162e7b153fefe5b10..bd36e4d834ae72cd64bc1451d6ff1cf2fd1ce094 100644 (file)
@@ -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;
+}
index cac73af04a0bb358f7ecdd00721e16c6d008defb..bbaa2b7435e4b26bf407fb43fc09165e903fde17 100644 (file)
@@ -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 */