From 677f2a6743769ae6244a89b43f4aa6665ac3eea0 Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Fri, 25 Aug 2023 12:35:11 +0000 Subject: [PATCH] filelist: Use our own pattern matching function throughout Since we (once again) have another way to match paths in packages, there is a flag which allows to switch to a different way to match paths. Signed-off-by: Michael Tremer --- src/libpakfire/build.c | 6 +-- src/libpakfire/dist.c | 2 +- src/libpakfire/filelist.c | 54 +++++++++++++++++------ src/libpakfire/include/pakfire/filelist.h | 6 ++- src/libpakfire/repo.c | 2 +- src/libpakfire/snapshot.c | 2 +- 6 files changed, 51 insertions(+), 21 deletions(-) diff --git a/src/libpakfire/build.c b/src/libpakfire/build.c index d5326db04..82dc635c2 100644 --- a/src/libpakfire/build.c +++ b/src/libpakfire/build.c @@ -645,7 +645,7 @@ static int pakfire_build_package_add_files(struct pakfire_build* build, // Scan for files r = pakfire_filelist_scan(filelist, build->buildroot, - (const char**)includes, (const char**)excludes); + (const char**)includes, (const char**)excludes, PAKFIRE_FILELIST_EXTENDED_MATCHING); if (r) goto ERROR; @@ -1223,7 +1223,7 @@ static int pakfire_build_run_post_build_checks(struct pakfire_build* build) { } // Scan for all files in BUILDROOT - r = pakfire_filelist_scan(filelist, build->buildroot, NULL, NULL); + r = pakfire_filelist_scan(filelist, build->buildroot, NULL, NULL, 0); if (r) goto ERROR; @@ -1802,7 +1802,7 @@ static int pakfire_build_check_unpackaged_files(struct pakfire_build* build) { goto ERROR; // Scan for all files in BUILDROOT - r = pakfire_filelist_scan(filelist, build->buildroot, NULL, NULL); + r = pakfire_filelist_scan(filelist, build->buildroot, NULL, NULL, 0); if (r) goto ERROR; diff --git a/src/libpakfire/dist.c b/src/libpakfire/dist.c index 61ed0a4f6..1b810d21e 100644 --- a/src/libpakfire/dist.c +++ b/src/libpakfire/dist.c @@ -363,7 +363,7 @@ static int pakfire_dist_add_files(struct pakfire* pakfire, goto ERROR; // Scan for any files - r = pakfire_filelist_scan(filelist, root, NULL, pakfire_dist_excludes); + r = pakfire_filelist_scan(filelist, root, NULL, pakfire_dist_excludes, 0); if (r) goto ERROR; diff --git a/src/libpakfire/filelist.c b/src/libpakfire/filelist.c index 9302adffb..faa9ca71d 100644 --- a/src/libpakfire/filelist.c +++ b/src/libpakfire/filelist.c @@ -19,7 +19,6 @@ #############################################################################*/ #include -#include #include #include @@ -204,14 +203,37 @@ int pakfire_filelist_remove_all( return pakfire_filelist_walk(removees, __pakfire_filelist_remove_one, list, 0); } -// Returns true if s contains globbing characters -static int is_glob(const char* s) { - if (strchr(s, '*')) - return 1; +static int pakfire_filelist_match_patterns(const char* path, + const char** patterns, const int flags) { + char buffer[PATH_MAX]; + int r; - if (strchr(s, '?')) - return 1; + // Check inputs + if (!path || !patterns) + return -EINVAL; + + for (const char** pattern = patterns; *pattern; pattern++) { + // Try to find a simple match + if (pakfire_path_match(*pattern, path)) + return 1; + // If requested perform extended matching + if (flags & PAKFIRE_FILELIST_EXTENDED_MATCHING) { + // / matches everything + if (strcmp(*pattern, "/") == 0) + return 1; + + // Try to match any sub-directories and files + r = pakfire_string_format(buffer, "%s/**", *pattern); + if (r) + return 1; + + if (pakfire_path_match(buffer, path)) + return 1; + } + } + + // No match return 0; } @@ -219,6 +241,7 @@ struct pakfire_filelist_matches { const char* root; const char** includes; const char** excludes; + const int flags; }; static int pakfire_filelist_scan_filter(struct archive* archive, void* p, @@ -239,28 +262,31 @@ static int pakfire_filelist_scan_filter(struct archive* archive, void* p, if (!path || !*path) return 0; - // Store the new path - archive_entry_set_pathname(entry, path); - // Skip excludes - if (matches->excludes && pakfire_filelist_match_patterns(path, matches->excludes)) + if (matches->excludes && + pakfire_filelist_match_patterns(path, matches->excludes, matches->flags)) return 0; // Skip what is not included - if (matches->includes && !pakfire_filelist_match_patterns(path, matches->includes)) + if (matches->includes && + !pakfire_filelist_match_patterns(path, matches->includes, matches->flags)) return 0; + // Store the new path + archive_entry_set_pathname(entry, path); + return 1; } int pakfire_filelist_scan(struct pakfire_filelist* list, const char* root, - const char** includes, const char** excludes) { + const char** includes, const char** excludes, int flags) { struct pakfire_file* file = NULL; struct archive_entry* entry = NULL; struct pakfire_filelist_matches matches = { .root = root, .includes = includes, .excludes = excludes, + .flags = flags, }; int r = 1; @@ -364,7 +390,7 @@ int pakfire_filelist_contains(struct pakfire_filelist* list, const char* pattern if (!path) return -1; - int r = fnmatch(pattern, path, 0); + int r = pakfire_path_match(pattern, path); if (r == 0) return 1; } diff --git a/src/libpakfire/include/pakfire/filelist.h b/src/libpakfire/include/pakfire/filelist.h index 1ae3fd05b..f4a286340 100644 --- a/src/libpakfire/include/pakfire/filelist.h +++ b/src/libpakfire/include/pakfire/filelist.h @@ -47,8 +47,12 @@ size_t pakfire_filelist_total_size(struct pakfire_filelist* list); int pakfire_filelist_remove_all(struct pakfire_filelist* list, struct pakfire_filelist* removees); +enum pakfire_filelist_scan_flags { + PAKFIRE_FILELIST_EXTENDED_MATCHING = (1 << 0), +}; + int pakfire_filelist_scan(struct pakfire_filelist* list, const char* root, - const char** includes, const char** excludes); + const char** includes, const char** excludes, int flags); int pakfire_filelist_contains(struct pakfire_filelist* list, const char* pattern); diff --git a/src/libpakfire/repo.c b/src/libpakfire/repo.c index 89e9b8ecf..5ae76ae92 100644 --- a/src/libpakfire/repo.c +++ b/src/libpakfire/repo.c @@ -1274,7 +1274,7 @@ PAKFIRE_EXPORT int pakfire_repo_scan(struct pakfire_repo* repo, int flags) { static const char* includes[] = { "*.pfm", NULL }; // Find all package files - r = pakfire_filelist_scan(filelist, path, includes, NULL); + r = pakfire_filelist_scan(filelist, path, includes, NULL, 0); if (r) goto ERROR; diff --git a/src/libpakfire/snapshot.c b/src/libpakfire/snapshot.c index 22bc34d78..83382a182 100644 --- a/src/libpakfire/snapshot.c +++ b/src/libpakfire/snapshot.c @@ -72,7 +72,7 @@ int pakfire_snapshot_compress(struct pakfire* pakfire, FILE* f) { DEBUG(pakfire, "Creating snapshot of %s...\n", root); // Scan for all files - r = pakfire_filelist_scan(filelist, root, NULL, NULL); + r = pakfire_filelist_scan(filelist, root, NULL, NULL, 0); if (r) goto ERROR; -- 2.39.5