|
|
a4b143 |
From c9f59a78c0016b37730cec831e1eec139ca4a561 Mon Sep 17 00:00:00 2001
|
|
|
a4b143 |
From: Lennart Poettering <lennart@poettering.net>
|
|
|
a4b143 |
Date: Wed, 25 Sep 2013 20:58:23 +0200
|
|
|
a4b143 |
Subject: [PATCH] util: add macro for iterating through all prefixes of a path
|
|
|
a4b143 |
|
|
|
a4b143 |
Syntactic sugar in a macro PATH_FOREACH_PREFIX.
|
|
|
a4b143 |
---
|
|
|
a4b143 |
src/shared/cgroup-util.c | 26 ++++----------------------
|
|
|
a4b143 |
src/shared/path-util.h | 3 +++
|
|
|
a4b143 |
src/test/test-path-util.c | 27 +++++++++++++++++++++++++++
|
|
|
a4b143 |
3 files changed, 34 insertions(+), 22 deletions(-)
|
|
|
a4b143 |
|
|
|
a4b143 |
diff --git a/src/shared/cgroup-util.c b/src/shared/cgroup-util.c
|
|
|
a4b143 |
index 2e630d4..dc0fe85 100644
|
|
|
a4b143 |
--- a/src/shared/cgroup-util.c
|
|
|
a4b143 |
+++ b/src/shared/cgroup-util.c
|
|
|
a4b143 |
@@ -456,23 +456,14 @@ int cg_migrate_recursive_fallback(
|
|
|
a4b143 |
|
|
|
a4b143 |
/* This didn't work? Then let's try all prefixes of the destination */
|
|
|
a4b143 |
|
|
|
a4b143 |
- strcpy(prefix, pto);
|
|
|
a4b143 |
- for (;;) {
|
|
|
a4b143 |
- char *slash;
|
|
|
a4b143 |
-
|
|
|
a4b143 |
- slash = strrchr(prefix, '/');
|
|
|
a4b143 |
- if (!slash)
|
|
|
a4b143 |
- break;
|
|
|
a4b143 |
-
|
|
|
a4b143 |
- *slash = 0;
|
|
|
a4b143 |
-
|
|
|
a4b143 |
+ PATH_FOREACH_PREFIX(prefix, pto) {
|
|
|
a4b143 |
r = cg_migrate_recursive(cfrom, pfrom, cto, prefix, ignore_self, rem);
|
|
|
a4b143 |
if (r >= 0)
|
|
|
a4b143 |
break;
|
|
|
a4b143 |
}
|
|
|
a4b143 |
}
|
|
|
a4b143 |
|
|
|
a4b143 |
- return r;
|
|
|
a4b143 |
+ return 0;
|
|
|
a4b143 |
}
|
|
|
a4b143 |
|
|
|
a4b143 |
static const char *normalize_controller(const char *controller) {
|
|
|
a4b143 |
@@ -661,23 +652,14 @@ int cg_attach_fallback(const char *controller, const char *path, pid_t pid) {
|
|
|
a4b143 |
/* This didn't work? Then let's try all prefixes of
|
|
|
a4b143 |
* the destination */
|
|
|
a4b143 |
|
|
|
a4b143 |
- strcpy(prefix, path);
|
|
|
a4b143 |
- for (;;) {
|
|
|
a4b143 |
- char *slash;
|
|
|
a4b143 |
-
|
|
|
a4b143 |
- slash = strrchr(prefix, '/');
|
|
|
a4b143 |
- if (!slash)
|
|
|
a4b143 |
- break;
|
|
|
a4b143 |
-
|
|
|
a4b143 |
- *slash = 0;
|
|
|
a4b143 |
-
|
|
|
a4b143 |
+ PATH_FOREACH_PREFIX(prefix, path) {
|
|
|
a4b143 |
r = cg_attach(controller, prefix, pid);
|
|
|
a4b143 |
if (r >= 0)
|
|
|
a4b143 |
break;
|
|
|
a4b143 |
}
|
|
|
a4b143 |
}
|
|
|
a4b143 |
|
|
|
a4b143 |
- return r;
|
|
|
a4b143 |
+ return 0;
|
|
|
a4b143 |
}
|
|
|
a4b143 |
|
|
|
a4b143 |
int cg_set_group_access(
|
|
|
a4b143 |
diff --git a/src/shared/path-util.h b/src/shared/path-util.h
|
|
|
a4b143 |
index 9452931..03f2cf2 100644
|
|
|
a4b143 |
--- a/src/shared/path-util.h
|
|
|
a4b143 |
+++ b/src/shared/path-util.h
|
|
|
a4b143 |
@@ -51,3 +51,6 @@ int path_is_read_only_fs(const char *path);
|
|
|
a4b143 |
int path_is_os_tree(const char *path);
|
|
|
a4b143 |
|
|
|
a4b143 |
int find_binary(const char *name, char **filename);
|
|
|
a4b143 |
+
|
|
|
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 |
diff --git a/src/test/test-path-util.c b/src/test/test-path-util.c
|
|
|
a4b143 |
index b0aeb11..e303e48 100644
|
|
|
a4b143 |
--- a/src/test/test-path-util.c
|
|
|
a4b143 |
+++ b/src/test/test-path-util.c
|
|
|
a4b143 |
@@ -106,8 +106,35 @@ static void test_find_binary(void) {
|
|
|
a4b143 |
assert(find_binary("xxxx-xxxx", &p) == -ENOENT);
|
|
|
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 |
+ char s[PATH_MAX];
|
|
|
a4b143 |
+
|
|
|
a4b143 |
+ PATH_FOREACH_PREFIX(s, "/a/b/c/d") {
|
|
|
a4b143 |
+ log_error("---%s---", s);
|
|
|
a4b143 |
+ assert_se(streq(s, values[i++]));
|
|
|
a4b143 |
+ }
|
|
|
a4b143 |
+
|
|
|
a4b143 |
+ assert_se(values[i] == NULL);
|
|
|
a4b143 |
+
|
|
|
a4b143 |
+ i = 0;
|
|
|
a4b143 |
+ PATH_FOREACH_PREFIX(s, "////a////b////c///d///////")
|
|
|
a4b143 |
+ assert_se(streq(s, values[i++]));
|
|
|
a4b143 |
+
|
|
|
a4b143 |
+ assert_se(values[i] == NULL);
|
|
|
a4b143 |
+
|
|
|
a4b143 |
+ PATH_FOREACH_PREFIX(s, "////")
|
|
|
a4b143 |
+ assert_se(streq(s, ""));
|
|
|
a4b143 |
+
|
|
|
a4b143 |
+ PATH_FOREACH_PREFIX(s, "")
|
|
|
a4b143 |
+ assert_not_reached("wut?");
|
|
|
a4b143 |
+
|
|
|
a4b143 |
+}
|
|
|
a4b143 |
+
|
|
|
a4b143 |
int main(void) {
|
|
|
a4b143 |
test_path();
|
|
|
a4b143 |
test_find_binary();
|
|
|
a4b143 |
+ test_prefixes();
|
|
|
a4b143 |
return 0;
|
|
|
a4b143 |
}
|