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