chiark / gitweb /
util: add macro for iterating through all prefixes of a path
authorLennart Poettering <lennart@poettering.net>
Wed, 25 Sep 2013 18:58:23 +0000 (20:58 +0200)
committerLennart Poettering <lennart@poettering.net>
Wed, 25 Sep 2013 19:04:35 +0000 (21:04 +0200)
Syntactic sugar in a macro PATH_FOREACH_PREFIX.

src/shared/cgroup-util.c
src/shared/path-util.h
src/test/test-path-util.c

index 2e630d4708d6f14814762794d2fe21345322016e..dc0fe85ee22a3fb189253b1c2f044348bf453e2a 100644 (file)
@@ -456,23 +456,14 @@ int cg_migrate_recursive_fallback(
 
                 /* This didn't work? Then let's try all prefixes of the destination */
 
-                strcpy(prefix, pto);
-                for (;;) {
-                        char *slash;
-
-                        slash = strrchr(prefix, '/');
-                        if (!slash)
-                                break;
-
-                        *slash = 0;
-
+                PATH_FOREACH_PREFIX(prefix, pto) {
                         r = cg_migrate_recursive(cfrom, pfrom, cto, prefix, ignore_self, rem);
                         if (r >= 0)
                                 break;
                 }
         }
 
-        return r;
+        return 0;
 }
 
 static const char *normalize_controller(const char *controller) {
@@ -661,23 +652,14 @@ int cg_attach_fallback(const char *controller, const char *path, pid_t pid) {
                 /* This didn't work? Then let's try all prefixes of
                  * the destination */
 
-                strcpy(prefix, path);
-                for (;;) {
-                        char *slash;
-
-                        slash = strrchr(prefix, '/');
-                        if (!slash)
-                                break;
-
-                        *slash = 0;
-
+                PATH_FOREACH_PREFIX(prefix, path) {
                         r = cg_attach(controller, prefix, pid);
                         if (r >= 0)
                                 break;
                 }
         }
 
-        return r;
+        return 0;
 }
 
 int cg_set_group_access(
index 9452931586836e007d19e5dc861e60d5e8a76eb2..03f2cf273cdd0e8de63b21560fc21e2d04e66c38 100644 (file)
@@ -51,3 +51,6 @@ int path_is_read_only_fs(const char *path);
 int path_is_os_tree(const char *path);
 
 int find_binary(const char *name, char **filename);
+
+#define PATH_FOREACH_PREFIX(prefix, path) \
+        for (char *_slash = strrchr(path_kill_slashes(strcpy(prefix, path)), '/'); _slash && !(*_slash = 0); _slash = strrchr((prefix), '/'))
index b0aeb11a63f239e117aef0f16a6ecc8c8cf71f63..e303e488e2a1610ff4602910f97bd3129b99feed 100644 (file)
@@ -106,8 +106,35 @@ static void test_find_binary(void) {
         assert(find_binary("xxxx-xxxx", &p) == -ENOENT);
 }
 
+static void test_prefixes(void) {
+        static const char* values[] = { "/a/b/c", "/a/b", "/a", "", NULL};
+        unsigned i = 0;
+        char s[PATH_MAX];
+
+        PATH_FOREACH_PREFIX(s, "/a/b/c/d") {
+                log_error("---%s---", s);
+                assert_se(streq(s, values[i++]));
+        }
+
+        assert_se(values[i] == NULL);
+
+        i = 0;
+        PATH_FOREACH_PREFIX(s, "////a////b////c///d///////")
+                assert_se(streq(s, values[i++]));
+
+        assert_se(values[i] == NULL);
+
+        PATH_FOREACH_PREFIX(s, "////")
+                assert_se(streq(s, ""));
+
+        PATH_FOREACH_PREFIX(s, "")
+                assert_not_reached("wut?");
+
+}
+
 int main(void) {
         test_path();
         test_find_binary();
+        test_prefixes();
         return 0;
 }