Skip to content

Commit 4ede980

Browse files
committed
tree-wide: make use of path_extend() at many places
This is not a comprehensive port, but mostly some low-hanging fruit.
1 parent 7ae2768 commit 4ede980

File tree

6 files changed

+44
-65
lines changed

6 files changed

+44
-65
lines changed

src/basic/path-lookup.c

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -36,35 +36,33 @@ int xdg_user_runtime_dir(char **ret, const char *suffix) {
3636
}
3737

3838
int xdg_user_config_dir(char **ret, const char *suffix) {
39+
_cleanup_free_ char *j = NULL;
3940
const char *e;
40-
char *j;
4141
int r;
4242

4343
assert(ret);
4444

4545
e = getenv("XDG_CONFIG_HOME");
46-
if (e)
46+
if (e) {
4747
j = path_join(e, suffix);
48-
else {
49-
_cleanup_free_ char *home = NULL;
50-
51-
r = get_home_dir(&home);
48+
if (!j)
49+
return -ENOMEM;
50+
} else {
51+
r = get_home_dir(&j);
5252
if (r < 0)
5353
return r;
5454

55-
j = path_join(home, "/.config", suffix);
55+
if (!path_extend(&j, "/.config", suffix))
56+
return -ENOMEM;
5657
}
5758

58-
if (!j)
59-
return -ENOMEM;
60-
61-
*ret = j;
59+
*ret = TAKE_PTR(j);
6260
return 0;
6361
}
6462

6563
int xdg_user_data_dir(char **ret, const char *suffix) {
64+
_cleanup_free_ char *j = NULL;
6665
const char *e;
67-
char *j;
6866
int r;
6967

7068
assert(ret);
@@ -75,21 +73,20 @@ int xdg_user_data_dir(char **ret, const char *suffix) {
7573
* /etc/systemd/ anyway. */
7674

7775
e = getenv("XDG_DATA_HOME");
78-
if (e)
76+
if (e) {
7977
j = path_join(e, suffix);
80-
else {
81-
_cleanup_free_ char *home = NULL;
82-
83-
r = get_home_dir(&home);
78+
if (!j)
79+
return -ENOMEM;
80+
} else {
81+
r = get_home_dir(&j);
8482
if (r < 0)
8583
return r;
8684

87-
j = path_join(home, "/.local/share", suffix);
85+
if (!path_extend(&j, "/.local/share", suffix))
86+
return -ENOMEM;
8887
}
89-
if (!j)
90-
return -ENOMEM;
9188

92-
*ret = j;
89+
*ret = TAKE_PTR(j);
9390
return 1;
9491
}
9592

src/basic/path-util.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -681,7 +681,7 @@ int find_executable_full(const char *name, bool use_path_envvar, char **ret_file
681681

682682
/* Resolve a single-component name to a full path */
683683
for (;;) {
684-
_cleanup_free_ char *j = NULL, *element = NULL;
684+
_cleanup_free_ char *element = NULL;
685685
_cleanup_close_ int fd = -1;
686686

687687
r = extract_first_word(&p, &element, ":", EXTRACT_RELAX|EXTRACT_DONT_COALESCE_SEPARATORS);
@@ -693,11 +693,10 @@ int find_executable_full(const char *name, bool use_path_envvar, char **ret_file
693693
if (!path_is_absolute(element))
694694
continue;
695695

696-
j = path_join(element, name);
697-
if (!j)
696+
if (!path_extend(&element, name))
698697
return -ENOMEM;
699698

700-
r = check_x_access(j, ret_fd ? &fd : NULL);
699+
r = check_x_access(element, ret_fd ? &fd : NULL);
701700
if (r < 0) {
702701
/* PATH entries which we don't have access to are ignored, as per tradition. */
703702
if (r != -EACCES)
@@ -707,7 +706,7 @@ int find_executable_full(const char *name, bool use_path_envvar, char **ret_file
707706

708707
/* Found it! */
709708
if (ret_filename)
710-
*ret_filename = path_simplify(TAKE_PTR(j), false);
709+
*ret_filename = path_simplify(TAKE_PTR(element), false);
711710
if (ret_fd)
712711
*ret_fd = TAKE_FD(fd);
713712

src/basic/selinux-util.c

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -524,18 +524,17 @@ int mac_selinux_create_file_prepare_at(int dirfd, const char *path, mode_t mode)
524524
return 0;
525525

526526
if (!path_is_absolute(path)) {
527-
_cleanup_free_ char *p = NULL;
528-
529527
if (dirfd == AT_FDCWD)
530-
r = safe_getcwd(&p);
528+
r = safe_getcwd(&abspath);
531529
else
532-
r = fd_get_path(dirfd, &p);
530+
r = fd_get_path(dirfd, &abspath);
533531
if (r < 0)
534532
return r;
535533

536-
path = abspath = path_join(p, path);
537-
if (!path)
534+
if (!path_extend(&abspath, path))
538535
return -ENOMEM;
536+
537+
path = abspath;
539538
}
540539

541540
return selinux_create_file_prepare_abspath(path, mode);

src/basic/tmpfile-util.c

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -123,13 +123,10 @@ int tempfn_xxxxxx(const char *p, const char *extra, char **ret) {
123123
return -EINVAL;
124124

125125
if (d) {
126-
char *j;
127-
128-
j = path_join(d, nf);
129-
if (!j)
126+
if (!path_extend(&d, nf))
130127
return -ENOMEM;
131128

132-
*ret = path_simplify(j, false);
129+
*ret = path_simplify(TAKE_PTR(d), false);
133130
} else
134131
*ret = TAKE_PTR(nf);
135132

@@ -168,13 +165,10 @@ int tempfn_random(const char *p, const char *extra, char **ret) {
168165
return -EINVAL;
169166

170167
if (d) {
171-
char *j;
172-
173-
j = path_join(d, nf);
174-
if (!j)
168+
if (!path_extend(&d, nf))
175169
return -ENOMEM;
176170

177-
*ret = path_simplify(j, false);
171+
*ret = path_simplify(TAKE_PTR(d), false);
178172
} else
179173
*ret = TAKE_PTR(nf);
180174

src/core/execute.c

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2287,8 +2287,6 @@ static int setup_exec_directory(
22872287
goto fail;
22882288

22892289
if (exec_directory_is_private(context, type)) {
2290-
_cleanup_free_ char *private_root = NULL;
2291-
22922290
/* So, here's one extra complication when dealing with DynamicUser=1 units. In that
22932291
* case we want to avoid leaving a directory around fully accessible that is owned by
22942292
* a dynamic user whose UID is later on reused. To lock this down we use the same
@@ -2314,19 +2312,18 @@ static int setup_exec_directory(
23142312
* Also, note that we don't do this for EXEC_DIRECTORY_RUNTIME as that's often used
23152313
* for sharing files or sockets with other services. */
23162314

2317-
private_root = path_join(params->prefix[type], "private");
2318-
if (!private_root) {
2315+
pp = path_join(params->prefix[type], "private");
2316+
if (!pp) {
23192317
r = -ENOMEM;
23202318
goto fail;
23212319
}
23222320

23232321
/* First set up private root if it doesn't exist yet, with access mode 0700 and owned by root:root */
2324-
r = mkdir_safe_label(private_root, 0700, 0, 0, MKDIR_WARN_MODE);
2322+
r = mkdir_safe_label(pp, 0700, 0, 0, MKDIR_WARN_MODE);
23252323
if (r < 0)
23262324
goto fail;
23272325

2328-
pp = path_join(private_root, *rt);
2329-
if (!pp) {
2326+
if (!path_extend(&pp, *rt)) {
23302327
r = -ENOMEM;
23312328
goto fail;
23322329
}

src/libsystemd/sd-path/sd-path.c

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ static int from_environment(const char *envname, const char *fallback, const cha
3737

3838
static int from_home_dir(const char *envname, const char *suffix, char **buffer, const char **ret) {
3939
_cleanup_free_ char *h = NULL;
40-
char *cc = NULL;
4140
int r;
4241

4342
assert(suffix);
@@ -58,12 +57,11 @@ static int from_home_dir(const char *envname, const char *suffix, char **buffer,
5857
if (r < 0)
5958
return r;
6059

61-
cc = path_join(h, suffix);
62-
if (!cc)
60+
if (!path_extend(&h, suffix))
6361
return -ENOMEM;
6462

65-
*buffer = cc;
66-
*ret = cc;
63+
*buffer = h;
64+
*ret = TAKE_PTR(h);
6765
return 0;
6866
}
6967

@@ -135,18 +133,16 @@ static int from_user_dir(const char *field, char **buffer, const char **ret) {
135133
/* Three syntaxes permitted: relative to $HOME, $HOME itself, and absolute path */
136134
if (startswith(p, "$HOME/")) {
137135
_cleanup_free_ char *h = NULL;
138-
char *cc;
139136

140137
r = get_home_dir(&h);
141138
if (r < 0)
142139
return r;
143140

144-
cc = path_join(h, p+5);
145-
if (!cc)
141+
if (!path_extend(&h, p+5))
146142
return -ENOMEM;
147143

148-
*buffer = cc;
149-
*ret = cc;
144+
*buffer = h;
145+
*ret = TAKE_PTR(h);
150146
return 0;
151147
} else if (streq(p, "$HOME")) {
152148

@@ -173,20 +169,17 @@ static int from_user_dir(const char *field, char **buffer, const char **ret) {
173169
/* The desktop directory defaults to $HOME/Desktop, the others to $HOME */
174170
if (streq(field, "XDG_DESKTOP_DIR")) {
175171
_cleanup_free_ char *h = NULL;
176-
char *cc;
177172

178173
r = get_home_dir(&h);
179174
if (r < 0)
180175
return r;
181176

182-
cc = path_join(h, "Desktop");
183-
if (!cc)
177+
if (!path_extend(&h, "Desktop"))
184178
return -ENOMEM;
185179

186-
*buffer = cc;
187-
*ret = cc;
180+
*buffer = h;
181+
*ret = TAKE_PTR(h);
188182
} else {
189-
190183
r = get_home_dir(buffer);
191184
if (r < 0)
192185
return r;

0 commit comments

Comments
 (0)