tools: lxc_deslashify() handle special cases

Signed-off-by: Christian Brauner <christian.brauner@canonical.com>
This commit is contained in:
Christian Brauner 2016-09-25 23:57:13 +02:00
parent 30498b9e1d
commit c56a9652d7
No known key found for this signature in database
GPG Key ID: 8EB056D53EECB12D
3 changed files with 29 additions and 10 deletions

View File

@ -282,7 +282,7 @@ static void exec_criu(struct criu_opts *opts)
} }
} }
if (!lxc_deslashify(path)) { if (!lxc_deslashify(&path)) {
ERROR("failed to deslashify %s", path); ERROR("failed to deslashify %s", path);
free(path); free(path);
goto err; goto err;

View File

@ -716,21 +716,40 @@ char **lxc_normalize_path(const char *path)
return components; return components;
} }
bool lxc_deslashify(char *path) bool lxc_deslashify(char **path)
{ {
char **parts = NULL, *path2; char *p;
char **parts = NULL;
size_t n, len;
parts = lxc_normalize_path(path); parts = lxc_normalize_path(*path);
if (!parts) if (!parts)
return false; return false;
path2 = lxc_string_join("/", (const char **) parts, *path == '/'); /* We'll end up here if path == "///" or path == "". */
lxc_free_array((void **) parts, free); if (!*parts) {
if (!path2) len = strlen(*path);
if (!len)
return true;
n = strcspn(*path, "/");
if (n == len) {
p = strdup("/");
if (!p)
return false;
free(*path);
*path = p;
return true;
}
}
p = lxc_string_join("/", (const char **)parts, **path == '/');
lxc_free_array((void **)parts, free);
if (!p)
return false; return false;
strncpy(path, path2, strlen(path)); free(*path);
free(path2); *path = p;
return true; return true;
} }

View File

@ -249,7 +249,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 bool lxc_deslashify(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