path-util: get rid of prefix_root()

prefix_root() is equivalent to path_join() in almost all ways, hence
let's remove it.

There are subtle differences though: prefix_root() will try shorten
multiple "/" before and after the prefix. path_join() doesn't do that.
This means prefix_root() might return a string shorter than both its
inputs combined, while path_join() never does that. I like the
path_join() semantics better, hence I think dropping prefix_root() is
totally OK. In the end the strings generated by both functon should
always be identical in terms of path_equal() if not streq().

This leaves prefix_roota() in place. Ideally we'd have path_joina(), but
I don't think we can reasonably implement that as a macro. or maybe we
can? (if so, sounds like something for a later PR)

Also add in a few missing OOM checks
This commit is contained in:
Lennart Poettering
2019-06-21 08:42:55 +09:00
committed by Yu Watanabe
parent f66ad46066
commit c6134d3e2f
14 changed files with 44 additions and 76 deletions
+2 -36
View File
@@ -256,7 +256,7 @@ char **path_strv_resolve(char **l, const char *root) {
if (root) {
orig = *s;
t = prefix_root(root, orig);
t = path_join(root, orig);
if (!t) {
enomem = true;
continue;
@@ -686,40 +686,6 @@ int mkfs_exists(const char *fstype) {
return binary_is_good(mkfs);
}
char *prefix_root(const char *root, const char *path) {
char *n, *p;
size_t l;
/* If root is passed, prefixes path with it. Otherwise returns
* it as is. */
assert(path);
/* First, drop duplicate prefixing slashes from the path */
while (path[0] == '/' && path[1] == '/')
path++;
if (empty_or_root(root))
return strdup(path);
l = strlen(root) + 1 + strlen(path) + 1;
n = new(char, l);
if (!n)
return NULL;
p = stpcpy(n, root);
while (p > n && p[-1] == '/')
p--;
if (path[0] != '/')
*(p++) = '/';
strcpy(p, path);
return n;
}
int parse_path_argument_and_warn(const char *path, bool suppress_root, char **arg) {
char *p;
int r;
@@ -1027,7 +993,7 @@ int systemd_installation_has_version(const char *root, unsigned minimal_version)
_cleanup_free_ char *path = NULL;
char *c, **name;
path = prefix_root(root, pattern);
path = path_join(root, pattern);
if (!path)
return -ENOMEM;