util: add macro for iterating through all prefixes of a path

Syntactic sugar in a macro PATH_FOREACH_PREFIX.
This commit is contained in:
Lennart Poettering
2013-09-25 21:04:35 +02:00
parent bc5fb0809e
commit fecffe5d0a
3 changed files with 34 additions and 22 deletions
+4 -22
View 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(
+3
View 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), '/'))