]> git.ipfire.org Git - pakfire.git/commitdiff
filelist: Use our own pattern matching function throughout
authorMichael Tremer <michael.tremer@ipfire.org>
Fri, 25 Aug 2023 12:35:11 +0000 (12:35 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Fri, 25 Aug 2023 12:35:11 +0000 (12:35 +0000)
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 <michael.tremer@ipfire.org>
src/libpakfire/build.c
src/libpakfire/dist.c
src/libpakfire/filelist.c
src/libpakfire/include/pakfire/filelist.h
src/libpakfire/repo.c
src/libpakfire/snapshot.c

index d5326db042b4397d6b707e8d089e7ca6be78301d..82dc635c2cc85d5b79e67e0d8d87b6f20c8749d0 100644 (file)
@@ -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;
 
index 61ed0a4f6d497c42fd073c529f61fb5b427502de..1b810d21e4fc19e7fec155a86735ac5081c6994e 100644 (file)
@@ -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;
 
index 9302adffbd2eba0c2dd99e1961e8c60ca7f67fd7..faa9ca71db734136ef9a4f70f0fb86e6efa4bd0b 100644 (file)
@@ -19,7 +19,6 @@
 #############################################################################*/
 
 #include <errno.h>
-#include <fnmatch.h>
 #include <stdlib.h>
 #include <sys/queue.h>
 
@@ -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;
        }
index 1ae3fd05b0c5fd27887286e9c5e9855cc98a052f..f4a286340b27157cc22a131bf276a656efc48513 100644 (file)
@@ -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);
 
index 89e9b8ecf3587469913821147c3e9d19902cb8c7..5ae76ae921517bf45db5da733b54d3f82a14d8d5 100644 (file)
@@ -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;
 
index 22bc34d78be59b04de075bcf97c94779d39fda36..83382a1826e47948a43326b5ad2998840b8544ae 100644 (file)
@@ -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;