2
0
mirror of git://github.com/lxc/lxc synced 2025-09-01 21:09:29 +00:00

utils: rework lxc_deslashify()

Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
This commit is contained in:
Christian Brauner
2017-07-29 23:10:17 +02:00
parent 3999be0ac0
commit eda0afd4b4
4 changed files with 58 additions and 51 deletions

View File

@@ -263,7 +263,7 @@ static void exec_criu(struct criu_opts *opts)
for (i = 0; i < cgroup_num_hierarchies(); i++) { for (i = 0; i < cgroup_num_hierarchies(); i++) {
char **controllers = NULL, *fullname; char **controllers = NULL, *fullname;
char *path; char *path, *tmp;
if (!cgroup_get_hierarchies(i, &controllers)) { if (!cgroup_get_hierarchies(i, &controllers)) {
ERROR("failed to get hierarchy %d", i); ERROR("failed to get hierarchy %d", i);
@@ -296,11 +296,15 @@ static void exec_criu(struct criu_opts *opts)
} }
} }
if (!lxc_deslashify(&path)) { tmp = lxc_deslashify(path);
ERROR("failed to deslashify %s", path); if (!tmp) {
ERROR("Failed to remove extraneous slashes from \"%s\"",
path);
free(path); free(path);
goto err; goto err;
} }
free(path);
path = tmp;
fullname = lxc_string_join(",", (const char **) controllers, false); fullname = lxc_string_join(",", (const char **) controllers, false);
if (!fullname) { if (!fullname) {

View File

@@ -729,47 +729,46 @@ char **lxc_normalize_path(const char *path)
return components; return components;
} }
bool lxc_deslashify(char **path) char *lxc_deslashify(const char *path)
{ {
bool ret = false; char *dup, *p;
char *p;
char **parts = NULL; char **parts = NULL;
size_t n, len; size_t n, len;
parts = lxc_normalize_path(*path); dup = strdup(path);
if (!parts) if (!dup)
return false; return NULL;
parts = lxc_normalize_path(dup);
if (!parts) {
free(dup);
return NULL;
}
/* We'll end up here if path == "///" or path == "". */ /* We'll end up here if path == "///" or path == "". */
if (!*parts) { if (!*parts) {
len = strlen(*path); len = strlen(dup);
if (!len) { if (!len) {
ret = true; lxc_free_array((void **)parts, free);
goto out; return dup;
} }
n = strcspn(*path, "/"); n = strcspn(dup, "/");
if (n == len) { if (n == len) {
free(dup);
lxc_free_array((void **)parts, free);
p = strdup("/"); p = strdup("/");
if (!p) if (!p)
goto out; return NULL;
free(*path);
*path = p; return p;
ret = true;
goto out;
} }
} }
p = lxc_string_join("/", (const char **)parts, **path == '/'); p = lxc_string_join("/", (const char **)parts, *dup == '/');
if (!p) free(dup);
goto out;
free(*path);
*path = p;
ret = true;
out:
lxc_free_array((void **)parts, free); lxc_free_array((void **)parts, free);
return ret; return p;
} }
char *lxc_append_paths(const char *first, const char *second) char *lxc_append_paths(const char *first, const char *second)

View File

@@ -275,7 +275,7 @@ extern char *lxc_string_join(const char *sep, const char **parts, bool use_as_pr
*/ */
extern char **lxc_normalize_path(const char *path); extern char **lxc_normalize_path(const char *path);
/* remove multiple slashes from the path, e.g. ///foo//bar -> /foo/bar */ /* remove multiple slashes from the path, e.g. ///foo//bar -> /foo/bar */
extern bool lxc_deslashify(char **path); extern char *lxc_deslashify(const char *path);
extern char *lxc_append_paths(const char *first, const char *second); extern char *lxc_append_paths(const char *first, const char *second);
/* Note: the following two functions use strtok(), so they will never /* Note: the following two functions use strtok(), so they will never
* consider an empty element, even if two delimiters are next to * consider an empty element, even if two delimiters are next to

View File

@@ -41,33 +41,37 @@
void test_lxc_deslashify(void) void test_lxc_deslashify(void)
{ {
char *s = strdup("/A///B//C/D/E/"); char *s = "/A///B//C/D/E/";
if (!s) char *t;
exit(EXIT_FAILURE);
lxc_test_assert_abort(lxc_deslashify(&s));
lxc_test_assert_abort(strcmp(s, "/A/B/C/D/E") == 0);
free(s);
s = strdup("/A"); t = lxc_deslashify(s);
if (!s) if (!t)
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
lxc_test_assert_abort(lxc_deslashify(&s)); lxc_test_assert_abort(strcmp(t, "/A/B/C/D/E") == 0);
lxc_test_assert_abort(strcmp(s, "/A") == 0); free(t);
free(s);
s = strdup(""); s = "/A";
if (!s)
exit(EXIT_FAILURE);
lxc_test_assert_abort(lxc_deslashify(&s));
lxc_test_assert_abort(strcmp(s, "") == 0);
free(s);
s = strdup("//"); t = lxc_deslashify(s);
if (!s) if (!t)
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
lxc_test_assert_abort(lxc_deslashify(&s)); lxc_test_assert_abort(strcmp(t, "/A") == 0);
lxc_test_assert_abort(strcmp(s, "/") == 0); free(t);
free(s);
s = "";
t = lxc_deslashify(s);
if (!t)
exit(EXIT_FAILURE);
lxc_test_assert_abort(strcmp(t, "") == 0);
free(t);
s = "//";
t = lxc_deslashify(s);
if (!t)
exit(EXIT_FAILURE);
lxc_test_assert_abort(strcmp(t, "/") == 0);
free(t);
} }
/* /proc/int_as_str/ns/mnt\0 = (5 + 21 + 7 + 1) */ /* /proc/int_as_str/ns/mnt\0 = (5 + 21 + 7 + 1) */