X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=src%2Fbasic%2Fpath-util.c;h=acd1007e3c08feb72b8767071574c7698d649d7b;hp=cdbb2d39815f349fd4736fcb05e639126935a312;hb=9742b1e43855b1599bd52ff95af995d9a9d35eac;hpb=a0db25e25b5691e4fc0afa5f5a9a8d1ce586decf diff --git a/src/basic/path-util.c b/src/basic/path-util.c index cdbb2d398..acd1007e3 100644 --- a/src/basic/path-util.c +++ b/src/basic/path-util.c @@ -82,7 +82,7 @@ char *path_make_absolute(const char *p, const char *prefix) { if (path_is_absolute(p) || !prefix) return strdup(p); - return strjoin(prefix, "/", p, NULL); + return strjoin(prefix, "/", p); } #endif // 0 @@ -104,7 +104,7 @@ int path_make_absolute_cwd(const char *p, char **ret) { if (!cwd) return negative_errno(); - c = strjoin(cwd, "/", p, NULL); + c = strjoin(cwd, "/", p); } if (!c) return -ENOMEM; @@ -222,10 +222,11 @@ int path_strv_make_absolute_cwd(char **l) { } #endif // 0 -char **path_strv_resolve(char **l, const char *prefix) { +char **path_strv_resolve(char **l, const char *root) { char **s; unsigned k = 0; bool enomem = false; + int r; if (strv_isempty(l)) return l; @@ -235,17 +236,17 @@ char **path_strv_resolve(char **l, const char *prefix) { * changes on failure. */ STRV_FOREACH(s, l) { - char *t, *u; _cleanup_free_ char *orig = NULL; + char *t, *u; if (!path_is_absolute(*s)) { free(*s); continue; } - if (prefix) { + if (root) { orig = *s; - t = strappend(prefix, orig); + t = prefix_root(root, orig); if (!t) { enomem = true; continue; @@ -253,28 +254,26 @@ char **path_strv_resolve(char **l, const char *prefix) { } else t = *s; - errno = 0; - u = canonicalize_file_name(t); - if (!u) { - if (errno == ENOENT) { - if (prefix) { - u = orig; - orig = NULL; - free(t); - } else - u = t; - } else { + r = chase_symlinks(t, root, 0, &u); + if (r == -ENOENT) { + if (root) { + u = orig; + orig = NULL; free(t); - if (errno == ENOMEM || errno == 0) - enomem = true; + } else + u = t; + } else if (r < 0) { + free(t); - continue; - } - } else if (prefix) { + if (r == -ENOMEM) + enomem = true; + + continue; + } else if (root) { char *x; free(t); - x = path_startswith(u, prefix); + x = path_startswith(u, root); if (x) { /* restore the slash if it was lost */ if (!startswith(x, "/")) @@ -306,12 +305,12 @@ char **path_strv_resolve(char **l, const char *prefix) { return l; } -char **path_strv_resolve_uniq(char **l, const char *prefix) { +char **path_strv_resolve_uniq(char **l, const char *root) { if (strv_isempty(l)) return l; - if (!path_strv_resolve(l, prefix)) + if (!path_strv_resolve(l, root)) return NULL; return strv_uniq(l); @@ -356,6 +355,16 @@ char* path_startswith(const char *path, const char *prefix) { assert(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. + */ + if ((path[0] == '/') != (prefix[0] == '/')) return NULL; @@ -435,11 +444,10 @@ bool path_equal(const char *a, const char *b) { return path_compare(a, b) == 0; } -bool path_equal_or_files_same(const char *a, const char *b) { - return path_equal(a, b) || files_same(a, b) > 0; +bool path_equal_or_files_same(const char *a, const char *b, int flags) { + return path_equal(a, b) || files_same(a, b, flags) > 0; } -#if 0 /// UNNEEDED by elogind char* path_join(const char *root, const char *path, const char *rest) { assert(path); @@ -447,13 +455,11 @@ char* path_join(const char *root, const char *path, const char *rest) { return strjoin(root, endswith(root, "/") ? "" : "/", path[0] == '/' ? path+1 : path, rest ? (endswith(path, "/") ? "" : "/") : NULL, - rest && rest[0] == '/' ? rest+1 : rest, - NULL); + rest && rest[0] == '/' ? rest+1 : rest); else return strjoin(path, rest ? (endswith(path, "/") ? "" : "/") : NULL, - rest && rest[0] == '/' ? rest+1 : rest, - NULL); + rest && rest[0] == '/' ? rest+1 : rest); } int find_binary(const char *name, char **ret) { @@ -497,7 +503,7 @@ int find_binary(const char *name, char **ret) { if (!path_is_absolute(element)) continue; - j = strjoin(element, "/", name, NULL); + j = strjoin(element, "/", name); if (!j) return -ENOMEM; @@ -518,6 +524,7 @@ int find_binary(const char *name, char **ret) { return last_error; } +#if 0 /// UNNEEDED by elogind bool paths_check_timestamp(const char* const* paths, usec_t *timestamp, bool update) { bool changed = false; const char* const* i; @@ -601,6 +608,7 @@ int mkfs_exists(const char *fstype) { mkfs = strjoina("mkfs.", fstype); return binary_is_good(mkfs); } +#endif // 0 char *prefix_root(const char *root, const char *path) { char *n, *p; @@ -636,6 +644,7 @@ char *prefix_root(const char *root, const char *path) { return n; } +#if 0 /// UNNEEDED by elogind int parse_path_argument_and_warn(const char *path, bool suppress_root, char **arg) { char *p; int r; @@ -696,10 +705,7 @@ bool filename_is_valid(const char *p) { if (isempty(p)) return false; - if (streq(p, ".")) - return false; - - if (streq(p, "..")) + if (dot_or_dot_dot(p)) return false; e = strchrnul(p, '/'); @@ -717,14 +723,17 @@ bool path_is_safe(const char *p) { if (isempty(p)) return false; - if (streq(p, "..") || startswith(p, "../") || endswith(p, "/..") || strstr(p, "/../")) + if (dot_or_dot_dot(p)) + return false; + + if (startswith(p, "../") || endswith(p, "/..") || strstr(p, "/../")) return false; if (strlen(p)+1 > PATH_MAX) return false; /* The following two checks are not really dangerous, but hey, they still are confusing */ - if (streq(p, ".") || startswith(p, "./") || endswith(p, "/.") || strstr(p, "/./")) + if (startswith(p, "./") || endswith(p, "/.") || strstr(p, "/./")) return false; if (strstr(p, "//")) @@ -824,7 +833,6 @@ bool is_deviceallow_pattern(const char *path) { startswith(path, "block-") || startswith(path, "char-"); } -#endif // 0 int systemd_installation_has_version(const char *root, unsigned minimal_version) { const char *pattern; @@ -891,3 +899,17 @@ int systemd_installation_has_version(const char *root, unsigned minimal_version) return false; } +#endif // 0 + +bool dot_or_dot_dot(const char *path) { + if (!path) + return false; + if (path[0] != '.') + return false; + if (path[1] == 0) + return true; + if (path[1] != '.') + return false; + + return path[2] == 0; +}