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