From 18b7f23465ae5f33b051ca6578fd3547f6990844 Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Tue, 29 Nov 2022 17:09:05 +0000 Subject: [PATCH] filelist: Unify include/exclude pattern matching Signed-off-by: Michael Tremer --- src/libpakfire/filelist.c | 36 +++++++----------------------------- 1 file changed, 7 insertions(+), 29 deletions(-) diff --git a/src/libpakfire/filelist.c b/src/libpakfire/filelist.c index f27266c73..e63a87f72 100644 --- a/src/libpakfire/filelist.c +++ b/src/libpakfire/filelist.c @@ -174,27 +174,19 @@ static int is_glob(const char* s) { return 0; } -static int pakfire_filelist_is_included(const char* path, const char** includes) { - // If the includes list is empty, everything is included - if (!includes) - return 1; - +static int pakfire_filelist_match_patterns(const char* path, const char** patterns) { int r; - for (const char** include = includes; *include; include++) { - // "/" includes everything - if (strcmp(*include, "/") == 0) - return 1; - + for (const char** pattern = patterns; *pattern; pattern++) { // Match any subdirectories - if (pakfire_string_startswith(path, *include)) + if (pakfire_string_startswith(path, *pattern)) return 1; // Skip fnmatch if the pattern doesn't have any globbing characters - if (!is_glob(*include)) + if (!is_glob(*pattern)) continue; - r = fnmatch(*include, path, 0); + r = fnmatch(*pattern, path, 0); // Found a match if (r == 0) @@ -213,20 +205,6 @@ static int pakfire_filelist_is_included(const char* path, const char** includes) return 0; } -static int pakfire_filelist_is_excluded(const char* path, const char** excludes) { - // If the exclude list is empty, nothing can be excluded - if (!excludes) - return 0; - - for (const char** exclude = excludes; *exclude; exclude++) { - if (pakfire_string_startswith(path, *exclude)) - return 1; - } - - // No match - return 0; -} - int pakfire_filelist_scan(struct pakfire_filelist* list, const char* root, const char** includes, const char** excludes) { struct pakfire_file* file = NULL; @@ -287,7 +265,7 @@ int pakfire_filelist_scan(struct pakfire_filelist* list, const char* root, continue; // Skip what is not included - if (!pakfire_filelist_is_included(path, includes)) { + if (includes && !pakfire_filelist_match_patterns(path, includes)) { DEBUG(list->pakfire, "Skipping %s...\n", path); // We do not mark the whole tree as to skip because some matches might @@ -296,7 +274,7 @@ int pakfire_filelist_scan(struct pakfire_filelist* list, const char* root, } // Skip excludes - if (pakfire_filelist_is_excluded(path, excludes)) { + if (excludes && pakfire_filelist_match_patterns(path, excludes)) { DEBUG(list->pakfire, "Skipping %s...\n", path); r = fts_set(tree, node, FTS_SKIP); -- 2.39.5