]> git.ipfire.org Git - pakfire.git/commitdiff
path: Move the match function into the path library
authorMichael Tremer <michael.tremer@ipfire.org>
Sat, 26 Oct 2024 12:18:10 +0000 (12:18 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 26 Oct 2024 12:18:10 +0000 (12:18 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/include/pakfire/path.h
src/libpakfire/include/pakfire/util.h
src/libpakfire/linter-file.c
src/libpakfire/path.c
src/libpakfire/util.c
tests/libpakfire/path.c
tests/libpakfire/util.c

index 820b02c5c74f6d411a71c827199048bd7e092779..0e2693adfade3377d96c2ce867c27536be718791 100644 (file)
@@ -53,4 +53,6 @@ int pakfire_path_is_absolute(const char* s);
        __pakfire_path_absolute(path, sizeof(path), s)
 int __pakfire_path_absolute(char* buffer, const size_t length, const char* s);
 
+int pakfire_path_match(const char* p, const char* s);
+
 #endif /* PAKFIRE_PATH_H */
index 59011cdd94f2173e7df61a708125f61305f8e091..befce026f4e8a5592d1a8cb3373ad7dc6db41fc6 100644 (file)
@@ -50,7 +50,6 @@ static inline void* pakfire_realloc(void* p, size_t size) {
 }
 
 int pakfire_path_exists(const char* path);
-int pakfire_path_match(const char* p, const char* s);
 time_t pakfire_path_age(const char* path);
 
 int pakfire_path_strip_extension(char* path);
index 4a9f173dad0c9d4e14f7294496c7ce12609c5f5a..35fa677773daf024a939663ce201660ea2a9f850 100644 (file)
@@ -28,8 +28,8 @@
 #include <pakfire/file.h>
 #include <pakfire/linter.h>
 #include <pakfire/linter-file.h>
+#include <pakfire/path.h>
 #include <pakfire/string.h>
-#include <pakfire/util.h>
 
 struct pakfire_linter_file {
        struct pakfire_ctx* ctx;
index 1bf0c02554e63eab9ec3baec6635f55a2d7b2b4d..7331bb24559445911728066f280aff2ffda88f53 100644 (file)
@@ -546,3 +546,90 @@ ERROR:
 
        return r;
 }
+
+/*
+       This function will handle any stars in the pattern matching
+
+       stars will be set to non-zero if we encountered a double star
+*/
+static int __pakfire_path_match_star(const char* p, const char* s) {
+       unsigned int stars = 0;
+       int r;
+
+       // Count how many stars we found
+       while (p && *p == '*') {
+               // Consume the star
+               p++;
+
+               // Increment the counter
+               stars++;
+       }
+
+       // We do not allow more than two stars
+       if (stars > 2) {
+               errno = EINVAL;
+               return -1;
+       }
+
+       // Consume the string...
+       for (; *s; s++) {
+               // Found slash!
+               if (*s == '/' && stars == 1)
+                       return pakfire_path_match(p, s);
+
+               // Otherwise read as many characters as possible
+               r = pakfire_path_match(p, s);
+               if (r)
+                       return r;
+       }
+
+       // The pattern has not entirely been consumed
+       if (p && *p)
+               return 0;
+
+       // If we reached the end of the string, * has consumed everything
+       return 1;
+}
+
+/*
+       This is our custom implementation of fnmatch()
+       which supports ** and stops at slashes.
+*/
+int pakfire_path_match(const char* p, const char* s) {
+       // Empty pattern matches nothing
+       if (!p || !*p)
+               return 0;
+
+       // Consume the pattern and string...
+       for (; *p; p++, s++) {
+               switch (*p) {
+                       // Match any character
+                       case '?':
+                               // No match if we reached the end
+                               if (!*s)
+                                       return 0;
+
+                               continue;
+
+                       // Match multiple characters
+                       case '*':
+                               return __pakfire_path_match_star(p, s);
+
+                       // All other characters
+                       default:
+                               // Character matches
+                               if (*s == *p)
+                                       continue;
+
+                               // No match
+                               return 0;
+               }
+       }
+
+       // There are unmatched characters left
+       if (*s)
+               return 0;
+
+       // We reached the end of the string and all characters matched
+       return 1;
+}
index 56c6bea5e275bf3f16e7df15530ce989d929e2a5..c6791e35501a12d9086ada3adef8a4f84ca9d7e8 100644 (file)
@@ -83,93 +83,6 @@ int pakfire_path_exists(const char* path) {
        return !access(path, F_OK);
 }
 
-/*
-       This function will handle any stars in the pattern matching
-
-       stars will be set to non-zero if we encountered a double star
-*/
-static int __pakfire_path_match_star(const char* p, const char* s) {
-       unsigned int stars = 0;
-       int r;
-
-       // Count how many stars we found
-       while (p && *p == '*') {
-               // Consume the star
-               p++;
-
-               // Increment the counter
-               stars++;
-       }
-
-       // We do not allow more than two stars
-       if (stars > 2) {
-               errno = EINVAL;
-               return -1;
-       }
-
-       // Consume the string...
-       for (; *s; s++) {
-               // Found slash!
-               if (*s == '/' && stars == 1)
-                       return pakfire_path_match(p, s);
-
-               // Otherwise read as many characters as possible
-               r = pakfire_path_match(p, s);
-               if (r)
-                       return r;
-       }
-
-       // The pattern has not entirely been consumed
-       if (p && *p)
-               return 0;
-
-       // If we reached the end of the string, * has consumed everything
-       return 1;
-}
-
-/*
-       This is our custom implementation of fnmatch()
-       which supports ** and stops at slashes.
-*/
-int pakfire_path_match(const char* p, const char* s) {
-       // Empty pattern matches nothing
-       if (!p || !*p)
-               return 0;
-
-       // Consume the pattern and string...
-       for (; *p; p++, s++) {
-               switch (*p) {
-                       // Match any character
-                       case '?':
-                               // No match if we reached the end
-                               if (!*s)
-                                       return 0;
-
-                               continue;
-
-                       // Match multiple characters
-                       case '*':
-                               return __pakfire_path_match_star(p, s);
-
-                       // All other characters
-                       default:
-                               // Character matches
-                               if (*s == *p)
-                                       continue;
-
-                               // No match
-                               return 0;
-               }
-       }
-
-       // There are unmatched characters left
-       if (*s)
-               return 0;
-
-       // We reached the end of the string and all characters matched
-       return 1;
-}
-
 time_t pakfire_path_age(const char* path) {
        struct stat st;
 
index c7214c46ff73c8050e870f4c3c2a1b71194ed90b..2ca72872632e1128d50e2ca160b735aff3e0ff0c 100644 (file)
@@ -142,6 +142,42 @@ FAIL:
        return EXIT_FAILURE;
 }
 
+static int test_path_match(const struct test* t) {
+       // Simple string match
+       ASSERT_TRUE(pakfire_path_match("/abc", "/abc"));
+
+       // Simple negative string match
+       ASSERT_FALSE(pakfire_path_match("/abc", "/def"));
+
+       // Simple star match
+       ASSERT_TRUE(pakfire_path_match("/usr/*", "/usr/bin"));
+       ASSERT_FALSE(pakfire_path_match("/usr/*", "/usr/bin/bash"));
+
+       // Double star match
+       ASSERT_TRUE(pakfire_path_match("/usr/**", "/usr/bin"));
+       ASSERT_TRUE(pakfire_path_match("/usr/**", "/usr/bin/bash"));
+
+       // Tripe star matches are invalid
+       ASSERT_ERRNO(pakfire_path_match("/usr/***", "/usr/bin"), EINVAL);
+       ASSERT_ERRNO(pakfire_path_match("/usr/***", "/usr/bin/bash"), EINVAL);
+
+       // Partial paths shouldn't match
+       ASSERT_FALSE(pakfire_path_match("/usr", "/usr/bin/bash"));
+
+       // Check for stars in the middle
+       ASSERT_TRUE(pakfire_path_match("/usr/lib64/*.so.*", "/usr/lib64/libblah.so.1"));
+       ASSERT_TRUE(pakfire_path_match("/usr/lib64/**/*.so", "/usr/lib64/blah/module.so"));
+       ASSERT_FALSE(pakfire_path_match("/usr/lib64/*.so.*", "/usr/lib64/beep"));
+
+       // Relatve vs. absolute?
+       ASSERT_FALSE(pakfire_path_match("/usr/lib/*.so", "relative-path"));
+
+       return EXIT_SUCCESS;
+
+FAIL:
+       return EXIT_FAILURE;
+}
+
 int main(int argc, const char* argv[]) {
        testsuite_add_test(test_path_normalize, 0);
        testsuite_add_test(test_path_append, 0);
@@ -149,6 +185,7 @@ int main(int argc, const char* argv[]) {
        testsuite_add_test(test_path_basename, 0);
        testsuite_add_test(test_path_dirname, 0);
        testsuite_add_test(test_path_relative, 0);
+       testsuite_add_test(test_path_match, 0);
 
        return testsuite_run(argc, argv);
 }
index ca9fde5a72ff64c524a6cedf08b3a35293ad1b12..ea4144d0fa9de7b466e13c089aa3dcbfa0bf5dcf 100644 (file)
@@ -50,42 +50,6 @@ FAIL:
        return EXIT_FAILURE;
 }
 
-static int test_path_match(const struct test* t) {
-       // Simple string match
-       ASSERT_TRUE(pakfire_path_match("/abc", "/abc"));
-
-       // Simple negative string match
-       ASSERT_FALSE(pakfire_path_match("/abc", "/def"));
-
-       // Simple star match
-       ASSERT_TRUE(pakfire_path_match("/usr/*", "/usr/bin"));
-       ASSERT_FALSE(pakfire_path_match("/usr/*", "/usr/bin/bash"));
-
-       // Double star match
-       ASSERT_TRUE(pakfire_path_match("/usr/**", "/usr/bin"));
-       ASSERT_TRUE(pakfire_path_match("/usr/**", "/usr/bin/bash"));
-
-       // Tripe star matches are invalid
-       ASSERT_ERRNO(pakfire_path_match("/usr/***", "/usr/bin"), EINVAL);
-       ASSERT_ERRNO(pakfire_path_match("/usr/***", "/usr/bin/bash"), EINVAL);
-
-       // Partial paths shouldn't match
-       ASSERT_FALSE(pakfire_path_match("/usr", "/usr/bin/bash"));
-
-       // Check for stars in the middle
-       ASSERT_TRUE(pakfire_path_match("/usr/lib64/*.so.*", "/usr/lib64/libblah.so.1"));
-       ASSERT_TRUE(pakfire_path_match("/usr/lib64/**/*.so", "/usr/lib64/blah/module.so"));
-       ASSERT_FALSE(pakfire_path_match("/usr/lib64/*.so.*", "/usr/lib64/beep"));
-
-       // Relatve vs. absolute?
-       ASSERT_FALSE(pakfire_path_match("/usr/lib/*.so", "relative-path"));
-
-       return EXIT_SUCCESS;
-
-FAIL:
-       return EXIT_FAILURE;
-}
-
 static int test_base64(const struct test* t) {
        int r = EXIT_FAILURE;
 
@@ -124,7 +88,6 @@ FAIL:
 
 int main(int argc, const char* argv[]) {
        testsuite_add_test(test_mkdir, TEST_WANTS_PAKFIRE);
-       testsuite_add_test(test_path_match, 0);
        testsuite_add_test(test_base64, 0);
 
        return testsuite_run(argc, argv);