Merge pull request #11002 from keszybz/path_join-merging
Path join merging
This commit is contained in:
@@ -216,7 +216,7 @@ int conf_files_insert(char ***strv, const char *root, char **dirs, const char *p
|
||||
p2 = path_startswith(path, *dir);
|
||||
if (p2) {
|
||||
/* Our new entry has higher priority */
|
||||
t = path_join(root, path, NULL);
|
||||
t = path_join(root, path);
|
||||
if (!t)
|
||||
return log_oom();
|
||||
|
||||
@@ -232,7 +232,7 @@ int conf_files_insert(char ***strv, const char *root, char **dirs, const char *p
|
||||
/* … we are not there yet, let's continue */
|
||||
}
|
||||
|
||||
t = path_join(root, path, NULL);
|
||||
t = path_join(root, path);
|
||||
if (!t)
|
||||
return log_oom();
|
||||
|
||||
@@ -318,7 +318,7 @@ int conf_files_list_with_replacement(
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Failed to extend config file list: %m");
|
||||
|
||||
p = path_join(root, replacement, NULL);
|
||||
p = path_join(root, replacement);
|
||||
if (!p)
|
||||
return log_oom();
|
||||
}
|
||||
|
||||
+16
-22
@@ -110,7 +110,7 @@ int path_make_absolute_cwd(const char *p, char **ret) {
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
c = path_join(NULL, cwd, p);
|
||||
c = path_join(cwd, p);
|
||||
}
|
||||
if (!c)
|
||||
return -ENOMEM;
|
||||
@@ -481,44 +481,39 @@ 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;
|
||||
}
|
||||
|
||||
char* path_join_many_internal(const char *first, ...) {
|
||||
char* path_join_internal(const char *first, ...) {
|
||||
char *joined, *q;
|
||||
const char *p;
|
||||
va_list ap;
|
||||
bool slash;
|
||||
size_t sz;
|
||||
|
||||
assert(first);
|
||||
|
||||
/* Joins all listed strings until NULL and places an "/" between them unless the strings end/begin already with
|
||||
* one so that it is unnecessary. Note that "/" which are already duplicate won't be removed. The string
|
||||
* returned is hence always equal or longer than the sum of the lengths of each individual string.
|
||||
/* 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 each
|
||||
* individual string.
|
||||
*
|
||||
* Note: any listed empty string is simply skipped. This can be useful for concatenating strings of which some
|
||||
* are optional.
|
||||
*
|
||||
* Examples:
|
||||
*
|
||||
* path_join_many("foo", "bar") → "foo/bar"
|
||||
* path_join_many("foo/", "bar") → "foo/bar"
|
||||
* path_join_many("", "foo", "", "bar", "") → "foo/bar" */
|
||||
* path_join("foo", "bar") → "foo/bar"
|
||||
* path_join("foo/", "bar") → "foo/bar"
|
||||
* path_join("", "foo", "", "bar", "") → "foo/bar" */
|
||||
|
||||
sz = strlen(first);
|
||||
sz = strlen_ptr(first);
|
||||
va_start(ap, first);
|
||||
while ((p = va_arg(ap, char*))) {
|
||||
|
||||
if (*p == 0) /* Skip empty items */
|
||||
continue;
|
||||
|
||||
sz += 1 + strlen(p);
|
||||
}
|
||||
while ((p = va_arg(ap, char*)) != (const char*) -1)
|
||||
if (!isempty(p))
|
||||
sz += 1 + strlen(p);
|
||||
va_end(ap);
|
||||
|
||||
joined = new(char, sz + 1);
|
||||
if (!joined)
|
||||
return NULL;
|
||||
|
||||
if (first[0] != 0) {
|
||||
if (!isempty(first)) {
|
||||
q = stpcpy(joined, first);
|
||||
slash = endswith(first, "/");
|
||||
} else {
|
||||
@@ -529,9 +524,8 @@ char* path_join_many_internal(const char *first, ...) {
|
||||
}
|
||||
|
||||
va_start(ap, first);
|
||||
while ((p = va_arg(ap, char*))) {
|
||||
|
||||
if (*p == 0) /* Skip empty items */
|
||||
while ((p = va_arg(ap, char*)) != (const char*) -1) {
|
||||
if (isempty(p))
|
||||
continue;
|
||||
|
||||
if (!slash && p[0] != '/')
|
||||
|
||||
@@ -49,13 +49,8 @@ char* path_startswith(const char *path, const char *prefix) _pure_;
|
||||
int path_compare(const char *a, const char *b) _pure_;
|
||||
bool path_equal(const char *a, const char *b) _pure_;
|
||||
bool path_equal_or_files_same(const char *a, const char *b, int flags);
|
||||
char* path_join_many_internal(const char *first, ...) _sentinel_;
|
||||
#define path_join_many(x, ...) path_join_many_internal(x, __VA_ARGS__, NULL)
|
||||
static inline char* path_join(const char *root, const char *path, const char *rest) {
|
||||
assert(path);
|
||||
|
||||
return path_join_many(strempty(root), path, rest);
|
||||
}
|
||||
char* path_join_internal(const char *first, ...);
|
||||
#define path_join(x, ...) path_join_internal(x, __VA_ARGS__, (const char*) -1)
|
||||
|
||||
char* path_simplify(char *path, bool kill_dots);
|
||||
|
||||
|
||||
@@ -366,11 +366,9 @@ int mac_selinux_create_file_prepare_at(int dirfd, const char *path, mode_t mode)
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
abspath = path_join(NULL, p, path);
|
||||
if (!abspath)
|
||||
path = abspath = path_join(p, path);
|
||||
if (!path)
|
||||
return -ENOMEM;
|
||||
|
||||
path = abspath;
|
||||
}
|
||||
|
||||
r = selinux_create_file_prepare_abspath(path, mode);
|
||||
|
||||
Reference in New Issue
Block a user