From: Christian Brauner Date: Thu, 1 Apr 2021 07:16:14 +0000 (+0200) Subject: string_utils: move to lxc-copy() sources X-Git-Tag: lxc-5.0.0~221^2~3 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=5fba37a1341457363d1b172bfb3453322a1854b0;p=thirdparty%2Flxc.git string_utils: move to lxc-copy() sources It's the only place where it is still used. Signed-off-by: Christian Brauner --- diff --git a/src/lxc/string_utils.c b/src/lxc/string_utils.c index 18c64c11e..e5321d826 100644 --- a/src/lxc/string_utils.c +++ b/src/lxc/string_utils.c @@ -192,41 +192,6 @@ char *lxc_string_join(const char *sep, const char **parts, bool use_as_prefix) 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 */ char *path_simplify(const char *path) { diff --git a/src/lxc/string_utils.h b/src/lxc/string_utils.h index 1bea9a01c..fefb1f496 100644 --- a/src/lxc/string_utils.h +++ b/src/lxc/string_utils.h @@ -30,21 +30,7 @@ __hidden extern char *lxc_string_replace(const char *needle, const char *replace 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); -/* - * 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); /* diff --git a/src/lxc/tools/lxc_copy.c b/src/lxc/tools/lxc_copy.c index c40692927..a786a300c 100644 --- a/src/lxc/tools/lxc_copy.c +++ b/src/lxc/tools/lxc_copy.c @@ -289,6 +289,41 @@ static int mk_rand_ovl_dirs(struct mnts *mnts, unsigned int num, struct lxc_argu 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) { char **components = NULL;