]> git.ipfire.org Git - pakfire.git/commitdiff
filelist: Unify include/exclude pattern matching
authorMichael Tremer <michael.tremer@ipfire.org>
Tue, 29 Nov 2022 17:09:05 +0000 (17:09 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Tue, 29 Nov 2022 17:09:05 +0000 (17:09 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/filelist.c

index f27266c730e3e42b4d0d4190c2d38d986d1a50fb..e63a87f72302bd37eb75913ae27b19054d98eeaf 100644 (file)
@@ -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);