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