HALF ECHO LEARN / REFERENCE COLLECTION

v262.2

src/basic/path-util.c

46,163 bytesGit mode 100644Download original fileLGPL-2.1-or-later

Git blob: 71486cc9e061a662704e026568623443193a52b7

Lines 1–1000 of 1,518

/* SPDX-License-Identifier: LGPL-2.1-or-later */
 
#include <fnmatch.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
 
#include "alloc-util.h"
#include "chase.h"
#include "errno-util.h"
#include "extract-word.h"
#include "fd-util.h"
#include "fs-util.h"
#include "glob-util.h"
#include "log.h"
#include "path-util.h"
#include "stat-util.h"
#include "string-util.h"
#include "strv.h"
 
bool is_path(const char *p) {
        if (!p) /* A NULL pointer is definitely not a path */
                return false;
 
        return strchr(p, '/');
}
 
int path_split_and_make_absolute(const char *p, char ***ret) {
        _cleanup_strv_free_ char **l = NULL;
        int r;
 
        assert(p);
        assert(ret);
 
        l = strv_split(p, ":");
        if (!l)
                return -ENOMEM;
 
        r = path_strv_make_absolute_cwd(l);
        if (r < 0)
                return r;
 
        *ret = TAKE_PTR(l);
        return r;
}
 
char* path_make_absolute(const char *p, const char *prefix) {
        assert(p);
 
        /* Makes every item in the list an absolute path by prepending
         * the prefix, if specified and necessary */
 
        if (path_is_absolute(p) || isempty(prefix))
                return strdup(p);
 
        return path_join(prefix, p);
}
 
int safe_getcwd(char **ret) {
        _cleanup_free_ char *cwd = NULL;
 
        cwd = get_current_dir_name();
        if (!cwd)
                return negative_errno();
 
        /* Let's make sure the directory is really absolute, to protect us from the logic behind
         * CVE-2018-1000001 */
        if (cwd[0] != '/')
                return -ENOMEDIUM;
 
        if (ret)
                *ret = TAKE_PTR(cwd);
 
        return 0;
}
 
int path_make_absolute_cwd(const char *p, char **ret) {
        char *c;
        int r;
 
        assert(p);
        assert(ret);
 
        /* Similar to path_make_absolute(), but prefixes with the
         * current working directory. */
 
        if (path_is_absolute(p))
                c = strdup(p);
        else {
                _cleanup_free_ char *cwd = NULL;
 
                r = safe_getcwd(&cwd);
                if (r < 0)
                        return r;
 
                c = path_join(cwd, p);
        }
        if (!c)
                return -ENOMEM;
 
        *ret = c;
        return 0;
}
 
int path_make_relative(const char *from, const char *to, char **ret) {
        _cleanup_free_ char *result = NULL;
        unsigned n_parents;
        const char *f, *t;
        int r, k;
        char *p;
 
        assert(from);
        assert(to);
        assert(ret);
 
        /* Strips the common part, and adds ".." elements as necessary. */
 
        if (!path_is_absolute(from) || !path_is_absolute(to))
                return -EINVAL;
 
        for (;;) {
                r = path_find_first_component(&from, true, &f);
                if (r < 0)
                        return r;
 
                k = path_find_first_component(&to, true, &t);
                if (k < 0)
                        return k;
 
                if (r == 0) {
                        /* end of 'from' */
                        if (k == 0) {
                                /* from and to are equivalent. */
                                result = strdup(".");
                                if (!result)
                                        return -ENOMEM;
                        } else {
                                /* 'to' is inside of 'from'. */
                                r = path_simplify_alloc(t, &result);
                                if (r < 0)
                                        return r;
 
                                if (!path_is_valid(result))
                                        return -EINVAL;
                        }
 
                        *ret = TAKE_PTR(result);
                        return 0;
                }
 
                if (r != k || !strneq(f, t, r))
                        break;
        }
 
        /* If we're here, then "from_dir" has one or more elements that need to
         * be replaced with "..". */
 
        for (n_parents = 1;; n_parents++) {
                /* If this includes ".." we can't do a simple series of "..". */
                r = path_find_first_component(&from, false, &f);
                if (r < 0)
                        return r;
                if (r == 0)
                        break;
        }
 
        if (isempty(t) && n_parents * 3 > PATH_MAX)
                /* PATH_MAX is counted *with* the trailing NUL byte */
                return -EINVAL;
 
        result = new(char, n_parents * 3 + !isempty(t) + strlen_ptr(t));
        if (!result)
                return -ENOMEM;
 
        for (p = result; n_parents > 0; n_parents--)
                p = mempcpy(p, "../", 3);
 
        if (isempty(t)) {
                /* Remove trailing slash and terminate string. */
                *(--p) = '\0';
                *ret = TAKE_PTR(result);
                return 0;
        }
 
        strcpy(p, t);
 
        path_simplify(result);
 
        if (!path_is_valid(result))
                return -EINVAL;
 
        *ret = TAKE_PTR(result);
        return 0;
}
 
int path_make_relative_parent(const char *from_child, const char *to, char **ret) {
        _cleanup_free_ char *from = NULL;
        int r;
 
        assert(from_child);
        assert(to);
        assert(ret);
 
        /* Similar to path_make_relative(), but provides the relative path from the parent directory of
         * 'from_child'. This may be useful when creating relative symlink.
         *
         * E.g.
         * - from = "/path/to/aaa", to = "/path/to/bbb"
         *      path_make_relative(from, to) = "../bbb"
         *      path_make_relative_parent(from, to) = "bbb"
         *
         * - from = "/path/to/aaa/bbb", to = "/path/to/ccc/ddd"
         *      path_make_relative(from, to) = "../../ccc/ddd"
         *      path_make_relative_parent(from, to) = "../ccc/ddd"
         */
 
        r = path_extract_directory(from_child, &from);
        if (r < 0)
                return r;
 
        return path_make_relative(from, to, ret);
}
 
char* path_startswith_strv(const char *p, char * const *strv) {
        assert(p);
 
        STRV_FOREACH(s, strv) {
                char *t;
 
                t = path_startswith(p, *s);
                if (t)
                        return t;
        }
 
        return NULL;
}
 
int path_strv_make_absolute_cwd(char **l) {
        int r;
 
        /* Goes through every item in the string list and makes it
         * absolute. This works in place and won't rollback any
         * changes on failure. */
 
        STRV_FOREACH(s, l) {
                char *t;
 
                r = path_make_absolute_cwd(*s, &t);
                if (r < 0)
                        return r;
 
                path_simplify(t);
                free_and_replace(*s, t);
        }
 
        return 0;
}
 
char** path_strv_resolve(char **l, const char *root) {
        unsigned k = 0;
        bool enomem = false;
        int r;
 
        if (strv_isempty(l))
                return l;
 
        /* Go through every item in the string list and canonicalize the path.
         * This works in place and doesn't roll back any changes on failure. */
 
        STRV_FOREACH(s, l) {
                _cleanup_free_ char *orig = NULL;
                char *t, *u;
 
                if (root) {
                        if (!path_is_absolute(*s)) {
                                log_debug("Ignoring non-absolute search path '%s'.", *s);
                                free(*s);
                                continue;
                        }
 
                        orig = *s;
                        t = path_join(root, orig);
                        if (!t) {
                                enomem = true;
                                continue;
                        }
                } else
                        t = *s;
 
                r = chase(t, root, 0, &u, NULL);
                if (r == -ENOENT) {
                        if (root) {
                                u = TAKE_PTR(orig);
                                free(t);
                        } else
                                u = t;
                } else if (r < 0) {
                        free(t);
 
                        if (r == -ENOMEM)
                                enomem = true;
 
                        continue;
                } else if (root) {
                        char *x;
 
                        free(t);
                        x = path_startswith(u, root);
                        if (x) {
                                /* restore the slash if it was lost */
                                if (!startswith(x, "/"))
                                        *(--x) = '/';
 
                                t = strdup(x);
                                free(u);
                                if (!t) {
                                        enomem = true;
                                        continue;
                                }
                                u = t;
                        } else {
                                /* canonicalized path goes outside of
                                 * prefix, keep the original path instead */
                                free_and_replace(u, orig);
                        }
                } else
                        free(t);
 
                l[k++] = u;
        }
 
        l[k] = NULL;
 
        if (enomem)
                return NULL;
 
        return l;
}
 
char** path_strv_resolve_uniq(char **l, const char *root) {
 
        if (strv_isempty(l))
                return l;
 
        if (!path_strv_resolve(l, root))
                return NULL;
 
        return strv_uniq(l);
}
 
char* skip_leading_slash(const char *p) {
        return skip_leading_chars(p, "/");
}
 
char* path_simplify_full(char *path, PathSimplifyFlags flags) {
        bool add_slash = false, keep_trailing_slash, absolute, beginning = true;
        char *f = path;
        int r;
 
        /* Removes redundant inner and trailing slashes. Also removes unnecessary dots.
         * Modifies the passed string in-place.
         *
         * ///foo//./bar/.   becomes /foo/bar
         * .//./foo//./bar/. becomes foo/bar
         * /../foo/bar       becomes /foo/bar
         * /../foo/bar/..    becomes /foo/bar/..
         */
 
        if (isempty(path))
                return path;
 
        keep_trailing_slash = FLAGS_SET(flags, PATH_SIMPLIFY_KEEP_TRAILING_SLASH) && endswith(path, "/");
 
        absolute = path_is_absolute(path);
        f += absolute;  /* Keep leading /, if present. */
 
        for (const char *p = f;;) {
                const char *e;
 
                r = path_find_first_component(&p, true, &e);
                if (r == 0)
                        break;
 
                if (r > 0 && absolute && beginning && path_startswith(e, ".."))
                        /* If we're at the beginning of an absolute path, we can safely skip ".." */
                        continue;
 
                beginning = false;
 
                if (add_slash)
                        *f++ = '/';
 
                if (r < 0) {
                        /* if path is invalid, then refuse to simplify the remaining part. */
                        memmove(f, p, strlen(p) + 1);
                        return path;
                }
 
                memmove(f, e, r);
                f += r;
 
                add_slash = true;
        }
 
        /* Special rule, if we stripped everything, we need a "." for the current directory. */
        if (f == path)
                *f++ = '.';
 
        if (*(f-1) != '/' && keep_trailing_slash)
                *f++ = '/';
 
        *f = '\0';
        return path;
}
 
int path_simplify_alloc(const char *path, char **ret) {
        assert(ret);
 
        if (!path) {
                *ret = NULL;
                return 0;
        }
 
        char *t = strdup(path);
        if (!t)
                return -ENOMEM;
 
        *ret = path_simplify(t);
        return 0;
}
 
char* path_startswith_full(const char *original_path, const char *prefix, PathStartWithFlags flags) {
        assert(original_path);
        assert(prefix);
 
        /* Returns a pointer to the start of the first component after the parts matched by
         * the prefix, iff
         * - both paths are absolute or both paths are relative,
         * and
         * - each component in prefix in turn matches a component in path at the same position.
         * An empty string will be returned when the prefix and path are equivalent.
         *
         * Returns NULL otherwise.
         */
 
        const char *path = original_path;
 
        if ((path[0] == '/') != (prefix[0] == '/'))
                return NULL;
 
        for (;;) {
                const char *p, *q;
                int m, n;
 
                m = path_find_first_component(&path, !FLAGS_SET(flags, PATH_STARTSWITH_REFUSE_DOT_DOT), &p);
                if (m < 0)
                        return NULL;
 
                n = path_find_first_component(&prefix, !FLAGS_SET(flags, PATH_STARTSWITH_REFUSE_DOT_DOT), &q);
                if (n < 0)
                        return NULL;
 
                if (n == 0) {
                        if (!p)
                                p = path;
 
                        if (FLAGS_SET(flags, PATH_STARTSWITH_RETURN_LEADING_SLASH)) {
 
                                if (p <= original_path)
                                        return NULL;
 
                                p--;
 
                                if (*p != '/')
                                        return NULL;
                        }
 
                        return (char*) p;
                }
 
                if (m != n)
                        return NULL;
 
                if (!strneq(p, q, m))
                        return NULL;
        }
}
 
int path_compare(const char *a, const char *b) {
        int r;
 
        /* Order NULL before non-NULL */
        r = CMP(!!a, !!b);
        if (r != 0)
                return r;
 
        /* A relative path and an absolute path must not compare as equal.
         * Which one is sorted before the other does not really matter.
         * Here a relative path is ordered before an absolute path. */
        r = CMP(path_is_absolute(a), path_is_absolute(b));
        if (r != 0)
                return r;
 
        for (;;) {
                const char *aa, *bb;
                int j, k;
 
                j = path_find_first_component(&a, true, &aa);
                k = path_find_first_component(&b, true, &bb);
 
                if (j < 0 || k < 0) {
                        /* When one of paths is invalid, order invalid path after valid one. */
                        r = CMP(j < 0, k < 0);
                        if (r != 0)
                                return r;
 
                        /* fallback to use strcmp() if both paths are invalid. */
                        return strcmp(a, b);
                }
 
                /* Order prefixes first: "/foo" before "/foo/bar" */
                if (j == 0) {
                        if (k == 0)
                                return 0;
                        return -1;
                }
                if (k == 0)
                        return 1;
 
                /* Alphabetical sort: "/foo/aaa" before "/foo/b" */
                r = memcmp(aa, bb, MIN(j, k));
                if (r != 0)
                        return r;
 
                /* Sort "/foo/a" before "/foo/aaa" */
                r = CMP(j, k);
                if (r != 0)
                        return r;
        }
}
 
int path_compare_filename(const char *a, const char *b) {
        _cleanup_free_ char *fa = NULL, *fb = NULL;
        int r, j, k;
 
        /* Order NULL before non-NULL */
        r = CMP(!!a, !!b);
        if (r != 0)
                return r;
 
        j = path_extract_filename(a, &fa);
        k = path_extract_filename(b, &fb);
 
        /* When one of paths is "." or root, then order it earlier. */
        r = CMP(j != -EADDRNOTAVAIL, k != -EADDRNOTAVAIL);
        if (r != 0)
                return r;
 
        /* When one of paths is invalid (or we get OOM), order invalid path after valid one. */
        r = CMP(j < 0, k < 0);
        if (r != 0)
                return r;
 
        /* fallback to use strcmp() if both paths are invalid. */
        if (j < 0)
                return strcmp(a, b);
 
        return strcmp(fa, fb);
}
 
int path_equal_or_inode_same_full(const char *a, const char *b, int flags) {
        /* Returns true if paths are of the same entry, false if not, <0 on error. */
 
        if (path_equal(a, b))
                return 1;
 
        if (!a || !b)
                return 0;
 
        return inode_same(a, b, flags);
}
 
char* path_extend_internal(char **x, ...) {
        size_t sz, old_sz;
        char *q, *nx;
        const char *p;
        va_list ap;
        bool slash;
 
        POINTER_MAY_BE_NULL(x);
 
        /* Joins all listed strings until the sentinel and places a "/" between them unless the strings
         * end/begin already with one so that it is unnecessary. Note that slashes which are already
         * duplicate won't be removed. The string returned is hence always equal to or longer than the sum of
         * the lengths of the individual strings.
         *
         * The first argument may be an already allocated string that is extended via realloc() if
         * non-NULL. path_extend() and path_join() are macro wrappers around this function, making use of the
         * first parameter to distinguish the two operations.
         *
         * Note: any listed empty string is simply skipped. This can be useful for concatenating strings of
         * which some are optional.
         *
         * Examples:
         *
         * path_join("foo", "bar") → "foo/bar"
         * path_join("foo/", "bar") → "foo/bar"
         * path_join("", "foo", "", "bar", "") → "foo/bar" */
 
        sz = old_sz = x ? strlen_ptr(*x) : 0;
        va_start(ap, x);
        while ((p = va_arg(ap, char*)) != POINTER_MAX) {
                size_t add;
 
                if (isempty(p))
                        continue;
 
                add = 1 + strlen(p);
                if (sz > SIZE_MAX - add) { /* overflow check */
                        va_end(ap);
                        return NULL;
                }
 
                sz += add;
        }
        va_end(ap);
 
        nx = realloc(x ? *x : NULL, GREEDY_ALLOC_ROUND_UP(sz+1));
        if (!nx)
                return NULL;
        if (x)
                *x = nx;
 
        if (old_sz > 0)
                slash = nx[old_sz-1] == '/';
        else {
                nx[old_sz] = 0;
                slash = true; /* no need to generate a slash anymore */
        }
 
        q = nx + old_sz;
 
        va_start(ap, x);
        while ((p = va_arg(ap, char*)) != POINTER_MAX) {
                if (isempty(p))
                        continue;
 
                if (!slash && p[0] != '/')
                        *(q++) = '/';
 
                q = stpcpy(q, p);
                slash = endswith(p, "/");
        }
        va_end(ap);
 
        return nx;
}
 
int open_and_check_executable(const char *name, const char *root, char **ret_path, int *ret_fd) {
        _cleanup_close_ int fd = -EBADF;
        _cleanup_free_ char *resolved = NULL;
        int r;
 
        assert(name);
 
        /* Function chase() is invoked only when root is not NULL, as using it regardless of
         * root value would alter the behavior of existing callers for example: /bin/sleep would become
         * /usr/bin/sleep when find_executables is called. Hence, this function should be invoked when
         * needed to avoid unforeseen regression or other complicated changes. */
        if (root) {
                /* prefix root to name in case full paths are not specified */
                r = chase(name, root, CHASE_PREFIX_ROOT, &resolved, &fd);
                if (r < 0)
                        return r;
 
                name = resolved;
        } else {
                /* We need to use O_PATH because there may be executables for which we have only exec permissions,
                 * but not read (usually suid executables). */
                fd = open(name, O_PATH|O_CLOEXEC);
                if (fd < 0)
                        return -errno;
        }
 
        r = fd_verify_regular(fd);
        if (r < 0)
                return r;
 
        r = access_fd(fd, X_OK);
        if (r == -ENOSYS)
                /* /proc/ is not mounted. Fall back to access(). */
                r = RET_NERRNO(access(name, X_OK));
        if (r < 0)
                return r;
 
        if (ret_path) {
                if (resolved)
                        *ret_path = TAKE_PTR(resolved);
                else {
                        r = path_make_absolute_cwd(name, ret_path);
                        if (r < 0)
                                return r;
 
                        path_simplify(*ret_path);
                }
        }
 
        if (ret_fd)
                *ret_fd = TAKE_FD(fd);
 
        return 0;
}
 
int find_executable_full(
                const char *name,
                const char *root,
                char * const *exec_search_path,
                bool use_path_envvar,
                char **ret_filename,
                int *ret_fd) {
 
        int last_error = -ENOENT, r = 0;
 
        assert(name);
 
        if (is_path(name))
                return open_and_check_executable(name, root, ret_filename, ret_fd);
 
        if (exec_search_path) {
                STRV_FOREACH(element, exec_search_path) {
                        _cleanup_free_ char *full_path = NULL;
 
                        if (!path_is_absolute(*element)) {
                                log_debug("Exec search path '%s' isn't absolute, ignoring.", *element);
                                continue;
                        }
 
                        full_path = path_join(*element, name);
                        if (!full_path)
                                return -ENOMEM;
 
                        r = open_and_check_executable(full_path, root, ret_filename, ret_fd);
                        if (r >= 0)
                                return 0;
                        if (r != -EACCES)
                                last_error = r;
                }
                return last_error;
        }
 
        const char *p = NULL;
 
        if (use_path_envvar)
                /* Plain getenv, not secure_getenv, because we want to actually allow the user to pick the
                 * binary. */
                p = getenv("PATH");
        if (!p)
                p = default_PATH();
 
        /* Resolve a single-component name to a full path */
        for (;;) {
                _cleanup_free_ char *element = NULL;
 
                r = extract_first_word(&p, &element, ":", EXTRACT_RELAX|EXTRACT_DONT_COALESCE_SEPARATORS);
                if (r < 0)
                        return r;
                if (r == 0)
                        break;
 
                if (!path_is_absolute(element)) {
                        log_debug("Exec search path '%s' isn't absolute, ignoring.", element);
                        continue;
                }
 
                if (!path_extend(&element, name))
                        return -ENOMEM;
 
                r = open_and_check_executable(element, root, ret_filename, ret_fd);
                if (r >= 0) /* Found it! */
                        return 0;
                /* PATH entries which we don't have access to are ignored, as per tradition. */
                if (r != -EACCES)
                        last_error = r;
        }
 
        return last_error;
}
 
static int executable_is_good(const char *executable) {
        _cleanup_free_ char *p = NULL, *d = NULL;
        int r;
 
        r = find_executable(executable, &p);
        if (r == -ENOENT)
                return 0;
        if (r < 0)
                return r;
 
        /* An fsck that is linked to /bin/true is a non-existent fsck */
 
        r = readlink_malloc(p, &d);
        if (r == -EINVAL) /* not a symlink */
                return 1;
        if (r < 0)
                return r;
 
        return !PATH_IN_SET(d, "true",
                               "/bin/true",
                               "/usr/bin/true",
                               "/dev/null");
}
 
int fsck_exists(void) {
        return executable_is_good("fsck");
}
 
int fsck_exists_for_fstype(const char *fstype) {
        const char *checker;
        int r;
 
        assert(fstype);
 
        if (streq(fstype, "auto"))
                return -EINVAL;
 
        r = fsck_exists();
        if (r <= 0)
                return r;
 
        checker = strjoina("fsck.", fstype);
        return executable_is_good(checker);
}
 
static const char* skip_slash_or_dot(const char *p) {
        POINTER_MAY_BE_NULL(p);
 
        for (; !isempty(p); p++) {
                if (*p == '/')
                        continue;
                if (startswith(p, "./")) {
                        p++;
                        continue;
                }
                break;
        }
        return p;
}
 
int path_find_first_component(const char **p, bool accept_dot_dot, const char **ret) {
        const char *q, *first, *end_first, *next;
        size_t len;
 
        assert(p);
 
        /* When a path is input, then returns the pointer to the first component and its length, and
         * move the input pointer to the next component or nul. This skips both over any '/'
         * immediately *before* and *after* the first component before returning.
         *
         * Examples
         *   Input:  p: "//.//aaa///bbbbb/cc"
         *   Output: p: "bbbbb///cc"
         *           ret: "aaa///bbbbb/cc"
         *           return value: 3 (== strlen("aaa"))
         *
         *   Input:  p: "aaa//"
         *   Output: p: (pointer to NUL)
         *           ret: "aaa//"
         *           return value: 3 (== strlen("aaa"))
         *
         *   Input:  p: "/", ".", ""
         *   Output: p: (pointer to NUL)
         *           ret: NULL
         *           return value: 0
         *
         *   Input:  p: NULL
         *   Output: p: NULL
         *           ret: NULL
         *           return value: 0
         *
         *   Input:  p: "(too long component)"
         *   Output: return value: -EINVAL
         *
         *   (when accept_dot_dot is false)
         *   Input:  p: "//..//aaa///bbbbb/cc"
         *   Output: return value: -EINVAL
         */
 
        q = *p;
 
        first = skip_slash_or_dot(q);
        if (isempty(first)) {
                *p = first;
                if (ret)
                        *ret = NULL;
                return 0;
        }
        if (streq(first, ".")) {
                *p = first + 1;
                if (ret)
                        *ret = NULL;
                return 0;
        }
 
        end_first = strchrnul(first, '/');
        len = end_first - first;
 
        if (len > NAME_MAX)
                return -EINVAL;
        if (!accept_dot_dot && len == 2 && first[0] == '.' && first[1] == '.')
                return -EINVAL;
 
        next = skip_slash_or_dot(end_first);
 
        *p = next + streq(next, ".");
        if (ret)
                *ret = first;
        return len;
}
 
static const char* skip_slash_or_dot_backward(const char *path, const char *q) {
        assert(path);
        assert(!q || q >= path);
 
        for (; q; q = PTR_SUB1(q, path)) {
                if (*q == '/')
                        continue;
                if (q > path && strneq(q - 1, "/.", 2))
                        continue;
                if (q == path && *q == '.')
                        continue;
                break;
        }
        return q;
}
 
int path_find_last_component(const char *path, bool accept_dot_dot, const char **next, const char **ret) {
        const char *q, *last_end, *last_begin;
        size_t len;
 
        POINTER_MAY_BE_NULL(next);
        POINTER_MAY_BE_NULL(ret);
 
        /* Similar to path_find_first_component(), but search components from the end.
        *
        * Examples
        *   Input:  path: "//.//aaa///bbbbb/cc//././"
        *           next: NULL
        *   Output: next: "/cc//././"
        *           ret: "cc//././"
        *           return value: 2 (== strlen("cc"))
        *
        *   Input:  path: "//.//aaa///bbbbb/cc//././"
        *           next: "/cc//././"
        *   Output: next: "///bbbbb/cc//././"
        *           ret: "bbbbb/cc//././"
        *           return value: 5 (== strlen("bbbbb"))
        *
        *   Input:  path: "//.//aaa///bbbbb/cc//././"
        *           next: "///bbbbb/cc//././"
        *   Output: next: "//.//aaa///bbbbb/cc//././" (next == path)
        *           ret: "aaa///bbbbb/cc//././"
        *           return value: 3 (== strlen("aaa"))
        *
        *   Input:  path: "/", ".", "", or NULL
        *   Output: next: equivalent to path
        *           ret: NULL
        *           return value: 0
        *
        *   Input:  path: "(too long component)"
        *   Output: return value: -EINVAL
        *
        *   (when accept_dot_dot is false)
        *   Input:  path: "//..//aaa///bbbbb/cc/..//"
        *   Output: return value: -EINVAL
        */
 
        if (isempty(path)) {
                if (next)
                        *next = path;
                if (ret)
                        *ret = NULL;
                return 0;
        }
 
        if (next && *next) {
                if (*next < path || *next > path + strlen(path))
                        return -EINVAL;
                if (*next == path) {
                        if (ret)
                                *ret = NULL;
                        return 0;
                }
                if (!IN_SET(**next, '\0', '/'))
                        return -EINVAL;
                q = *next - 1;
        } else
                q = path + strlen(path) - 1;
 
        q = skip_slash_or_dot_backward(path, q);
        if (!q) { /* the root directory, "." or "./" */