teknoraver / rpms / systemd

Forked from rpms/systemd 2 months ago
Clone

Blame SOURCES/0132-shared-add-path_compare-an-ordering-path-comparison.patch

803fb7
From 331328223912b66dd25d5cb6ed250d67419d54de Mon Sep 17 00:00:00 2001
803fb7
From: Michal Schmidt <mschmidt@redhat.com>
803fb7
Date: Mon, 16 Mar 2015 21:58:35 +0100
803fb7
Subject: [PATCH] shared: add path_compare(), an ordering path comparison
803fb7
803fb7
... and make path_equal() a simple wrapper around it.
803fb7
803fb7
(cherry picked from commit 2230852bd9755e1b7bfd1260082471f559b0a005)
803fb7
---
803fb7
 src/shared/path-util.c    | 37 +++++++++++++++++++++++++++----------
803fb7
 src/shared/path-util.h    |  1 +
803fb7
 src/test/test-path-util.c | 36 +++++++++++++++++++++++++-----------
803fb7
 3 files changed, 53 insertions(+), 21 deletions(-)
803fb7
803fb7
diff --git a/src/shared/path-util.c b/src/shared/path-util.c
803fb7
index 70bc1caa2..d5510bf56 100644
803fb7
--- a/src/shared/path-util.c
803fb7
+++ b/src/shared/path-util.c
803fb7
@@ -403,12 +403,18 @@ char* path_startswith(const char *path, const char *prefix) {
803fb7
         }
803fb7
 }
803fb7
 
803fb7
-bool path_equal(const char *a, const char *b) {
803fb7
+int path_compare(const char *a, const char *b) {
803fb7
+        int d;
803fb7
+
803fb7
         assert(a);
803fb7
         assert(b);
803fb7
 
803fb7
-        if ((a[0] == '/') != (b[0] == '/'))
803fb7
-                return false;
803fb7
+        /* A relative path and an abolute path must not compare as equal.
803fb7
+         * Which one is sorted before the other does not really matter.
803fb7
+         * Here a relative path is ordered before an absolute path. */
803fb7
+        d = (a[0] == '/') - (b[0] == '/');
803fb7
+        if (d)
803fb7
+                return d;
803fb7
 
803fb7
         for (;;) {
803fb7
                 size_t j, k;
803fb7
@@ -417,25 +423,36 @@ bool path_equal(const char *a, const char *b) {
803fb7
                 b += strspn(b, "/");
803fb7
 
803fb7
                 if (*a == 0 && *b == 0)
803fb7
-                        return true;
803fb7
+                        return 0;
803fb7
 
803fb7
-                if (*a == 0 || *b == 0)
803fb7
-                        return false;
803fb7
+                /* Order prefixes first: "/foo" before "/foo/bar" */
803fb7
+                if (*a == 0)
803fb7
+                        return -1;
803fb7
+                if (*b == 0)
803fb7
+                        return 1;
803fb7
 
803fb7
                 j = strcspn(a, "/");
803fb7
                 k = strcspn(b, "/");
803fb7
 
803fb7
-                if (j != k)
803fb7
-                        return false;
803fb7
+                /* Alphabetical sort: "/foo/aaa" before "/foo/b" */
803fb7
+                d = memcmp(a, b, MIN(j, k));
803fb7
+                if (d)
803fb7
+                        return (d > 0) - (d < 0); /* sign of d */
803fb7
 
803fb7
-                if (memcmp(a, b, j) != 0)
803fb7
-                        return false;
803fb7
+                /* Sort "/foo/a" before "/foo/aaa" */
803fb7
+                d = (j > k) - (j < k);  /* sign of (j - k) */
803fb7
+                if (d)
803fb7
+                        return d;
803fb7
 
803fb7
                 a += j;
803fb7
                 b += k;
803fb7
         }
803fb7
 }
803fb7
 
803fb7
+bool path_equal(const char *a, const char *b) {
803fb7
+        return path_compare(a, b) == 0;
803fb7
+}
803fb7
+
803fb7
 bool path_equal_or_files_same(const char *a, const char *b) {
803fb7
         return path_equal(a, b) || files_same(a, b) > 0;
803fb7
 }
803fb7
diff --git a/src/shared/path-util.h b/src/shared/path-util.h
803fb7
index bcf116ed3..ca81b49cb 100644
803fb7
--- a/src/shared/path-util.h
803fb7
+++ b/src/shared/path-util.h
803fb7
@@ -44,6 +44,7 @@ char* path_make_absolute_cwd(const char *p);
803fb7
 int path_make_relative(const char *from_dir, const char *to_path, char **_r);
803fb7
 char* path_kill_slashes(char *path);
803fb7
 char* path_startswith(const char *path, const char *prefix) _pure_;
803fb7
+int path_compare(const char *a, const char *b) _pure_;
803fb7
 bool path_equal(const char *a, const char *b) _pure_;
803fb7
 bool path_equal_or_files_same(const char *a, const char *b);
803fb7
 char* path_join(const char *root, const char *path, const char *rest);
803fb7
diff --git a/src/test/test-path-util.c b/src/test/test-path-util.c
803fb7
index 11aa52aae..6396fcb39 100644
803fb7
--- a/src/test/test-path-util.c
803fb7
+++ b/src/test/test-path-util.c
803fb7
@@ -27,23 +27,37 @@
803fb7
 #include "macro.h"
803fb7
 #include "strv.h"
803fb7
 
803fb7
+#define test_path_compare(a, b, result) {                 \
803fb7
+                assert_se(path_compare(a, b) == result);  \
803fb7
+                assert_se(path_compare(b, a) == -result); \
803fb7
+                assert_se(path_equal(a, b) == !result);   \
803fb7
+                assert_se(path_equal(b, a) == !result);   \
803fb7
+        }
803fb7
 
803fb7
 static void test_path(void) {
803fb7
-        assert_se(path_equal("/goo", "/goo"));
803fb7
-        assert_se(path_equal("//goo", "/goo"));
803fb7
-        assert_se(path_equal("//goo/////", "/goo"));
803fb7
-        assert_se(path_equal("goo/////", "goo"));
803fb7
+        test_path_compare("/goo", "/goo", 0);
803fb7
+        test_path_compare("/goo", "/goo", 0);
803fb7
+        test_path_compare("//goo", "/goo", 0);
803fb7
+        test_path_compare("//goo/////", "/goo", 0);
803fb7
+        test_path_compare("goo/////", "goo", 0);
803fb7
+
803fb7
+        test_path_compare("/goo/boo", "/goo//boo", 0);
803fb7
+        test_path_compare("//goo/boo", "/goo/boo//", 0);
803fb7
 
803fb7
-        assert_se(path_equal("/goo/boo", "/goo//boo"));
803fb7
-        assert_se(path_equal("//goo/boo", "/goo/boo//"));
803fb7
+        test_path_compare("/", "///", 0);
803fb7
 
803fb7
-        assert_se(path_equal("/", "///"));
803fb7
+        test_path_compare("/x", "x/", 1);
803fb7
+        test_path_compare("x/", "/", -1);
803fb7
 
803fb7
-        assert_se(!path_equal("/x", "x/"));
803fb7
-        assert_se(!path_equal("x/", "/"));
803fb7
+        test_path_compare("/x/./y", "x/y", 1);
803fb7
+        test_path_compare("x/.y", "x/y", -1);
803fb7
 
803fb7
-        assert_se(!path_equal("/x/./y", "x/y"));
803fb7
-        assert_se(!path_equal("x/.y", "x/y"));
803fb7
+        test_path_compare("foo", "/foo", -1);
803fb7
+        test_path_compare("/foo", "/foo/bar", -1);
803fb7
+        test_path_compare("/foo/aaa", "/foo/b", -1);
803fb7
+        test_path_compare("/foo/aaa", "/foo/b/a", -1);
803fb7
+        test_path_compare("/foo/a", "/foo/aaa", -1);
803fb7
+        test_path_compare("/foo/a/b", "/foo/aaa", -1);
803fb7
 
803fb7
         assert_se(path_is_absolute("/"));
803fb7
         assert_se(!path_is_absolute("./"));