string_utils: move to lxc-copy() sources

It's the only place where it is still used. Signed-off-by: 's avatarChristian Brauner <christian.brauner@ubuntu.com>
parent 68dd0ea5
...@@ -192,41 +192,6 @@ char *lxc_string_join(const char *sep, const char **parts, bool use_as_prefix) ...@@ -192,41 +192,6 @@ char *lxc_string_join(const char *sep, const char **parts, bool use_as_prefix)
return result; return result;
} }
char **lxc_normalize_path(const char *path)
{
char **components;
size_t components_len = 0;
size_t pos = 0;
components = lxc_string_split(path, '/');
if (!components)
return NULL;
/* resolve '.' and '..' */
for (pos = 0; pos < components_len;) {
if (strequal(components[pos], ".") ||
(strequal(components[pos], "..") && pos == 0)) {
/* eat this element */
free(components[pos]);
memmove(&components[pos], &components[pos + 1],
sizeof(char *) * (components_len - pos));
components_len--;
} else if (strequal(components[pos], "..")) {
/* eat this and the previous element */
free(components[pos - 1]);
free(components[pos]);
memmove(&components[pos - 1], &components[pos + 1],
sizeof(char *) * (components_len - pos));
components_len -= 2;
pos--;
} else {
pos++;
}
}
return components;
}
/* taken from systemd */ /* taken from systemd */
char *path_simplify(const char *path) char *path_simplify(const char *path)
{ {
......
...@@ -30,21 +30,7 @@ __hidden extern char *lxc_string_replace(const char *needle, const char *replace ...@@ -30,21 +30,7 @@ __hidden extern char *lxc_string_replace(const char *needle, const char *replace
const char *haystack); const char *haystack);
__hidden extern bool lxc_string_in_array(const char *needle, const char **haystack); __hidden extern bool lxc_string_in_array(const char *needle, const char **haystack);
__hidden extern char *lxc_string_join(const char *sep, const char **parts, bool use_as_prefix); __hidden extern char *lxc_string_join(const char *sep, const char **parts, bool use_as_prefix);
/*
* Normalize and split path: Leading and trailing / are removed, multiple
* / are compactified, .. and . are resolved (.. on the top level is considered
* identical to .).
* Examples:
* / -> { NULL }
* foo/../bar -> { bar, NULL }
* ../../ -> { NULL }
* ./bar/baz/.. -> { bar, NULL }
* foo//bar -> { foo, bar, NULL }
*/
__hidden extern char **lxc_normalize_path(const char *path);
/* remove multiple slashes from the path, e.g. ///foo//bar -> /foo/bar */
__hidden extern char *lxc_deslashify(const char *path);
__hidden extern char *lxc_append_paths(const char *first, const char *second); __hidden extern char *lxc_append_paths(const char *first, const char *second);
/* /*
......
...@@ -289,6 +289,41 @@ static int mk_rand_ovl_dirs(struct mnts *mnts, unsigned int num, struct lxc_argu ...@@ -289,6 +289,41 @@ static int mk_rand_ovl_dirs(struct mnts *mnts, unsigned int num, struct lxc_argu
return 0; return 0;
} }
static char **lxc_normalize_path(const char *path)
{
char **components;
size_t components_len = 0;
size_t pos = 0;
components = lxc_string_split(path, '/');
if (!components)
return NULL;
/* resolve '.' and '..' */
for (pos = 0; pos < components_len;) {
if (strequal(components[pos], ".") ||
(strequal(components[pos], "..") && pos == 0)) {
/* eat this element */
free(components[pos]);
memmove(&components[pos], &components[pos + 1],
sizeof(char *) * (components_len - pos));
components_len--;
} else if (strequal(components[pos], "..")) {
/* eat this and the previous element */
free(components[pos - 1]);
free(components[pos]);
memmove(&components[pos - 1], &components[pos + 1],
sizeof(char *) * (components_len - pos));
components_len -= 2;
pos--;
} else {
pos++;
}
}
return components;
}
static char *construct_path(char *path, bool as_prefix) static char *construct_path(char *path, bool as_prefix)
{ {
char **components = NULL; char **components = NULL;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment