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:
committed by
Yu Watanabe
parent
f66ad46066
commit
c6134d3e2f
@@ -207,7 +207,7 @@ int conf_files_insert(char ***strv, const char *root, char **dirs, const char *p
|
||||
_cleanup_free_ char *rdir = NULL;
|
||||
char *p1, *p2;
|
||||
|
||||
rdir = prefix_root(root, *dir);
|
||||
rdir = path_join(root, *dir);
|
||||
if (!rdir)
|
||||
return -ENOMEM;
|
||||
|
||||
@@ -221,7 +221,7 @@ int conf_files_insert(char ***strv, const char *root, char **dirs, const char *p
|
||||
if (p2) {
|
||||
/* Our new entry has higher priority */
|
||||
|
||||
t = prefix_root(root, path);
|
||||
t = path_join(root, path);
|
||||
if (!t)
|
||||
return log_oom();
|
||||
|
||||
@@ -238,7 +238,7 @@ int conf_files_insert(char ***strv, const char *root, char **dirs, const char *p
|
||||
}
|
||||
|
||||
/* The new file has lower priority than all the existing entries */
|
||||
t = prefix_root(root, path);
|
||||
t = path_join(root, path);
|
||||
if (!t)
|
||||
return -ENOMEM;
|
||||
|
||||
@@ -313,7 +313,7 @@ int conf_files_list_with_replacement(
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Failed to extend config file list: %m");
|
||||
|
||||
p = prefix_root(root, replacement);
|
||||
p = path_join(root, replacement);
|
||||
if (!p)
|
||||
return log_oom();
|
||||
}
|
||||
|
||||
+2
-36
@@ -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;
|
||||
|
||||
|
||||
@@ -116,10 +116,8 @@ int mkfs_exists(const char *fstype);
|
||||
_slash && ((*_slash = 0), true); \
|
||||
_slash = strrchr((prefix), '/'))
|
||||
|
||||
char *prefix_root(const char *root, const char *path);
|
||||
|
||||
/* Similar to prefix_root(), but returns an alloca() buffer, or
|
||||
* possibly a const pointer into the path parameter */
|
||||
/* Similar to path_join(), but only works for two components, and only the first one may be NULL and returns
|
||||
* an alloca() buffer, or possibly a const pointer into the path parameter */
|
||||
#define prefix_roota(root, path) \
|
||||
({ \
|
||||
const char* _path = (path), *_root = (root), *_ret; \
|
||||
|
||||
Reference in New Issue
Block a user