anitazha / rpms / systemd

Forked from rpms/systemd 3 years ago
Clone

Blame SOURCES/0051-util-properly-handle-the-root-dir-in-PATH_FOREACH_PR.patch

a4b143
From dc5e623cbd6c4250d514ec1c9d67c338cd71934c Mon Sep 17 00:00:00 2001
a4b143
From: Lennart Poettering <lennart@poettering.net>
a4b143
Date: Thu, 26 Sep 2013 19:58:33 +0200
a4b143
Subject: [PATCH] util: properly handle the root dir in PATH_FOREACH_PREFIX
a4b143
a4b143
Also add PATH_FOREACH_PREFIX_MORE which includes the specified dir
a4b143
itself in the iteration
a4b143
---
a4b143
 src/shared/path-util.h    |  9 ++++++++-
a4b143
 src/test/test-path-util.c | 34 ++++++++++++++++++++++++++++++----
a4b143
 2 files changed, 38 insertions(+), 5 deletions(-)
a4b143
a4b143
diff --git a/src/shared/path-util.h b/src/shared/path-util.h
a4b143
index 03f2cf2..0a42de7 100644
a4b143
--- a/src/shared/path-util.h
a4b143
+++ b/src/shared/path-util.h
a4b143
@@ -52,5 +52,12 @@ int path_is_os_tree(const char *path);
a4b143
 
a4b143
 int find_binary(const char *name, char **filename);
a4b143
 
a4b143
+/* Iterates through the path prefixes of the specified path, going up
a4b143
+ * the tree, to root. Also returns "" (and not "/"!) for the root
a4b143
+ * directory. Excludes the specified directory itself */
a4b143
 #define PATH_FOREACH_PREFIX(prefix, path) \
a4b143
-        for (char *_slash = strrchr(path_kill_slashes(strcpy(prefix, path)), '/'); _slash && !(*_slash = 0); _slash = strrchr((prefix), '/'))
a4b143
+        for (char *_slash = ({ path_kill_slashes(strcpy(prefix, path)); streq(prefix, "/") ? NULL : strrchr(prefix, '/'); }); _slash && !(*_slash = 0); _slash = strrchr((prefix), '/'))
a4b143
+
a4b143
+/* Same as PATH_FOREACH_PREFIX but also includes the specified path itself */
a4b143
+#define PATH_FOREACH_PREFIX_MORE(prefix, path) \
a4b143
+        for (char *_slash = ({ path_kill_slashes(strcpy(prefix, path)); if (streq(prefix, "/")) prefix[0] = 0; strrchr(prefix, 0); }); _slash && !(*_slash = 0); _slash = strrchr((prefix), '/'))
a4b143
diff --git a/src/test/test-path-util.c b/src/test/test-path-util.c
a4b143
index e303e48..ed3b315 100644
a4b143
--- a/src/test/test-path-util.c
a4b143
+++ b/src/test/test-path-util.c
a4b143
@@ -107,29 +107,55 @@ static void test_find_binary(void) {
a4b143
 }
a4b143
 
a4b143
 static void test_prefixes(void) {
a4b143
-        static const char* values[] = { "/a/b/c", "/a/b", "/a", "", NULL};
a4b143
-        unsigned i = 0;
a4b143
+        static const char* values[] = { "/a/b/c/d", "/a/b/c", "/a/b", "/a", "", NULL};
a4b143
+        unsigned i;
a4b143
         char s[PATH_MAX];
a4b143
+        bool b;
a4b143
 
a4b143
-        PATH_FOREACH_PREFIX(s, "/a/b/c/d") {
a4b143
+        i = 0;
a4b143
+        PATH_FOREACH_PREFIX_MORE(s, "/a/b/c/d") {
a4b143
                 log_error("---%s---", s);
a4b143
                 assert_se(streq(s, values[i++]));
a4b143
         }
a4b143
+        assert_se(values[i] == NULL);
a4b143
 
a4b143
+        i = 1;
a4b143
+        PATH_FOREACH_PREFIX(s, "/a/b/c/d") {
a4b143
+                log_error("---%s---", s);
a4b143
+                assert_se(streq(s, values[i++]));
a4b143
+        }
a4b143
         assert_se(values[i] == NULL);
a4b143
 
a4b143
         i = 0;
a4b143
-        PATH_FOREACH_PREFIX(s, "////a////b////c///d///////")
a4b143
+        PATH_FOREACH_PREFIX_MORE(s, "////a////b////c///d///////")
a4b143
                 assert_se(streq(s, values[i++]));
a4b143
+        assert_se(values[i] == NULL);
a4b143
 
a4b143
+        i = 1;
a4b143
+        PATH_FOREACH_PREFIX(s, "////a////b////c///d///////")
a4b143
+                assert_se(streq(s, values[i++]));
a4b143
         assert_se(values[i] == NULL);
a4b143
 
a4b143
         PATH_FOREACH_PREFIX(s, "////")
a4b143
+                assert_not_reached("Wut?");
a4b143
+
a4b143
+        b = false;
a4b143
+        PATH_FOREACH_PREFIX_MORE(s, "////") {
a4b143
+                assert_se(!b);
a4b143
                 assert_se(streq(s, ""));
a4b143
+                b = true;
a4b143
+        }
a4b143
+        assert_se(b);
a4b143
 
a4b143
         PATH_FOREACH_PREFIX(s, "")
a4b143
                 assert_not_reached("wut?");
a4b143
 
a4b143
+        b = false;
a4b143
+        PATH_FOREACH_PREFIX_MORE(s, "") {
a4b143
+                assert(!b);
a4b143
+                assert(streq(s, ""));
a4b143
+                b = true;
a4b143
+        }
a4b143
 }
a4b143
 
a4b143
 int main(void) {