]>
Commit | Line | Data |
---|---|---|
9eb977db KS |
1 | /*** |
2 | This file is part of systemd. | |
3 | ||
4 | Copyright 2010-2012 Lennart Poettering | |
5 | ||
6 | systemd is free software; you can redistribute it and/or modify it | |
7 | under the terms of the GNU Lesser General Public License as published by | |
8 | the Free Software Foundation; either version 2.1 of the License, or | |
9 | (at your option) any later version. | |
10 | ||
11 | systemd is distributed in the hope that it will be useful, but | |
12 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | Lesser General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU Lesser General Public License | |
17 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
18 | ***/ | |
19 | ||
9eb977db | 20 | #include <errno.h> |
11c3a366 | 21 | #include <limits.h> |
07630cea LP |
22 | #include <stdio.h> |
23 | #include <stdlib.h> | |
24 | #include <string.h> | |
11c3a366 | 25 | #include <sys/stat.h> |
07630cea | 26 | #include <unistd.h> |
9eb977db | 27 | |
5f311f8c LP |
28 | /* When we include libgen.h because we need dirname() we immediately |
29 | * undefine basename() since libgen.h defines it as a macro to the | |
30 | * POSIX version which is really broken. We prefer GNU basename(). */ | |
31 | #include <libgen.h> | |
32 | #undef basename | |
33 | ||
b5efdb8a | 34 | #include "alloc-util.h" |
93cc7779 | 35 | #include "extract-word.h" |
f4f15635 | 36 | #include "fs-util.h" |
5a46d55f | 37 | #include "glob-util.h" |
9eb977db | 38 | #include "log.h" |
07630cea LP |
39 | #include "macro.h" |
40 | #include "missing.h" | |
5a46d55f | 41 | #include "parse-util.h" |
3ffd4af2 | 42 | #include "path-util.h" |
8fcde012 | 43 | #include "stat-util.h" |
07630cea | 44 | #include "string-util.h" |
9eb977db | 45 | #include "strv.h" |
93cc7779 | 46 | #include "time-util.h" |
9eb977db KS |
47 | |
48 | bool path_is_absolute(const char *p) { | |
49 | return p[0] == '/'; | |
50 | } | |
51 | ||
52 | bool is_path(const char *p) { | |
53 | return !!strchr(p, '/'); | |
54 | } | |
55 | ||
0f474365 | 56 | int path_split_and_make_absolute(const char *p, char ***ret) { |
9eb977db | 57 | char **l; |
0f474365 LP |
58 | int r; |
59 | ||
9eb977db | 60 | assert(p); |
0f474365 | 61 | assert(ret); |
9eb977db | 62 | |
116cc028 ZJS |
63 | l = strv_split(p, ":"); |
64 | if (!l) | |
ce9d6bcf | 65 | return -ENOMEM; |
9eb977db | 66 | |
0f474365 LP |
67 | r = path_strv_make_absolute_cwd(l); |
68 | if (r < 0) { | |
9eb977db | 69 | strv_free(l); |
0f474365 | 70 | return r; |
9eb977db KS |
71 | } |
72 | ||
0f474365 LP |
73 | *ret = l; |
74 | return r; | |
9eb977db KS |
75 | } |
76 | ||
77 | char *path_make_absolute(const char *p, const char *prefix) { | |
78 | assert(p); | |
79 | ||
80 | /* Makes every item in the list an absolute path by prepending | |
81 | * the prefix, if specified and necessary */ | |
82 | ||
83 | if (path_is_absolute(p) || !prefix) | |
84 | return strdup(p); | |
85 | ||
605405c6 | 86 | return strjoin(prefix, "/", p); |
9eb977db KS |
87 | } |
88 | ||
0f474365 LP |
89 | int path_make_absolute_cwd(const char *p, char **ret) { |
90 | char *c; | |
9eb977db KS |
91 | |
92 | assert(p); | |
0f474365 | 93 | assert(ret); |
9eb977db KS |
94 | |
95 | /* Similar to path_make_absolute(), but prefixes with the | |
96 | * current working directory. */ | |
97 | ||
98 | if (path_is_absolute(p)) | |
0f474365 LP |
99 | c = strdup(p); |
100 | else { | |
101 | _cleanup_free_ char *cwd = NULL; | |
9eb977db | 102 | |
0f474365 LP |
103 | cwd = get_current_dir_name(); |
104 | if (!cwd) | |
9c4615fb | 105 | return negative_errno(); |
0f474365 | 106 | |
605405c6 | 107 | c = strjoin(cwd, "/", p); |
0f474365 LP |
108 | } |
109 | if (!c) | |
110 | return -ENOMEM; | |
9eb977db | 111 | |
0f474365 LP |
112 | *ret = c; |
113 | return 0; | |
9eb977db KS |
114 | } |
115 | ||
7cb9c51c TK |
116 | int path_make_relative(const char *from_dir, const char *to_path, char **_r) { |
117 | char *r, *p; | |
118 | unsigned n_parents; | |
7cb9c51c TK |
119 | |
120 | assert(from_dir); | |
121 | assert(to_path); | |
122 | assert(_r); | |
123 | ||
124 | /* Strips the common part, and adds ".." elements as necessary. */ | |
125 | ||
126 | if (!path_is_absolute(from_dir)) | |
127 | return -EINVAL; | |
128 | ||
129 | if (!path_is_absolute(to_path)) | |
130 | return -EINVAL; | |
131 | ||
132 | /* Skip the common part. */ | |
133 | for (;;) { | |
2a5beb66 | 134 | size_t a, b; |
7cb9c51c TK |
135 | |
136 | from_dir += strspn(from_dir, "/"); | |
137 | to_path += strspn(to_path, "/"); | |
138 | ||
139 | if (!*from_dir) { | |
140 | if (!*to_path) | |
141 | /* from_dir equals to_path. */ | |
142 | r = strdup("."); | |
143 | else | |
144 | /* from_dir is a parent directory of to_path. */ | |
145 | r = strdup(to_path); | |
7cb9c51c TK |
146 | if (!r) |
147 | return -ENOMEM; | |
148 | ||
5216f599 TK |
149 | path_kill_slashes(r); |
150 | ||
7cb9c51c TK |
151 | *_r = r; |
152 | return 0; | |
153 | } | |
154 | ||
155 | if (!*to_path) | |
156 | break; | |
157 | ||
158 | a = strcspn(from_dir, "/"); | |
159 | b = strcspn(to_path, "/"); | |
160 | ||
161 | if (a != b) | |
162 | break; | |
163 | ||
164 | if (memcmp(from_dir, to_path, a) != 0) | |
165 | break; | |
166 | ||
167 | from_dir += a; | |
168 | to_path += b; | |
169 | } | |
170 | ||
171 | /* If we're here, then "from_dir" has one or more elements that need to | |
172 | * be replaced with "..". */ | |
173 | ||
174 | /* Count the number of necessary ".." elements. */ | |
175 | for (n_parents = 0;;) { | |
2a5beb66 LP |
176 | size_t w; |
177 | ||
7cb9c51c TK |
178 | from_dir += strspn(from_dir, "/"); |
179 | ||
180 | if (!*from_dir) | |
181 | break; | |
182 | ||
2a5beb66 LP |
183 | w = strcspn(from_dir, "/"); |
184 | ||
185 | /* If this includes ".." we can't do a simple series of "..", refuse */ | |
186 | if (w == 2 && from_dir[0] == '.' && from_dir[1] == '.') | |
187 | return -EINVAL; | |
188 | ||
189 | /* Count number of elements, except if they are "." */ | |
190 | if (w != 1 || from_dir[0] != '.') | |
191 | n_parents++; | |
192 | ||
193 | from_dir += w; | |
7cb9c51c TK |
194 | } |
195 | ||
2a5beb66 | 196 | r = new(char, n_parents * 3 + strlen(to_path) + 1); |
7cb9c51c TK |
197 | if (!r) |
198 | return -ENOMEM; | |
199 | ||
2a5beb66 LP |
200 | for (p = r; n_parents > 0; n_parents--) |
201 | p = mempcpy(p, "../", 3); | |
7cb9c51c | 202 | |
5216f599 TK |
203 | strcpy(p, to_path); |
204 | path_kill_slashes(r); | |
7cb9c51c TK |
205 | |
206 | *_r = r; | |
207 | return 0; | |
208 | } | |
209 | ||
0f474365 | 210 | int path_strv_make_absolute_cwd(char **l) { |
9eb977db | 211 | char **s; |
0f474365 | 212 | int r; |
9eb977db KS |
213 | |
214 | /* Goes through every item in the string list and makes it | |
215 | * absolute. This works in place and won't rollback any | |
216 | * changes on failure. */ | |
217 | ||
218 | STRV_FOREACH(s, l) { | |
219 | char *t; | |
220 | ||
0f474365 LP |
221 | r = path_make_absolute_cwd(*s, &t); |
222 | if (r < 0) | |
223 | return r; | |
9eb977db KS |
224 | |
225 | free(*s); | |
226 | *s = t; | |
227 | } | |
228 | ||
0f474365 | 229 | return 0; |
9eb977db KS |
230 | } |
231 | ||
e1873695 | 232 | char **path_strv_resolve(char **l, const char *root) { |
9eb977db KS |
233 | char **s; |
234 | unsigned k = 0; | |
235 | bool enomem = false; | |
e1873695 | 236 | int r; |
9eb977db KS |
237 | |
238 | if (strv_isempty(l)) | |
239 | return l; | |
240 | ||
241 | /* Goes through every item in the string list and canonicalize | |
242 | * the path. This works in place and won't rollback any | |
243 | * changes on failure. */ | |
244 | ||
245 | STRV_FOREACH(s, l) { | |
12ed81d9 | 246 | _cleanup_free_ char *orig = NULL; |
e1873695 | 247 | char *t, *u; |
9eb977db | 248 | |
12ed81d9 ZJS |
249 | if (!path_is_absolute(*s)) { |
250 | free(*s); | |
9eb977db | 251 | continue; |
12ed81d9 | 252 | } |
112cfb18 | 253 | |
e1873695 | 254 | if (root) { |
12ed81d9 | 255 | orig = *s; |
e1873695 | 256 | t = prefix_root(root, orig); |
112cfb18 MM |
257 | if (!t) { |
258 | enomem = true; | |
259 | continue; | |
260 | } | |
12ed81d9 | 261 | } else |
112cfb18 | 262 | t = *s; |
9eb977db | 263 | |
c4f4fce7 | 264 | r = chase_symlinks(t, root, 0, &u); |
e1873695 LP |
265 | if (r == -ENOENT) { |
266 | if (root) { | |
267 | u = orig; | |
268 | orig = NULL; | |
874310b7 | 269 | free(t); |
e1873695 LP |
270 | } else |
271 | u = t; | |
272 | } else if (r < 0) { | |
273 | free(t); | |
874310b7 | 274 | |
e1873695 LP |
275 | if (r == -ENOMEM) |
276 | enomem = true; | |
277 | ||
278 | continue; | |
279 | } else if (root) { | |
12ed81d9 ZJS |
280 | char *x; |
281 | ||
282 | free(t); | |
e1873695 | 283 | x = path_startswith(u, root); |
12ed81d9 ZJS |
284 | if (x) { |
285 | /* restore the slash if it was lost */ | |
286 | if (!startswith(x, "/")) | |
287 | *(--x) = '/'; | |
288 | ||
289 | t = strdup(x); | |
290 | free(u); | |
291 | if (!t) { | |
292 | enomem = true; | |
293 | continue; | |
294 | } | |
295 | u = t; | |
296 | } else { | |
297 | /* canonicalized path goes outside of | |
298 | * prefix, keep the original path instead */ | |
3b319885 | 299 | free_and_replace(u, orig); |
12ed81d9 | 300 | } |
91a6489d LP |
301 | } else |
302 | free(t); | |
9eb977db KS |
303 | |
304 | l[k++] = u; | |
305 | } | |
306 | ||
307 | l[k] = NULL; | |
308 | ||
309 | if (enomem) | |
310 | return NULL; | |
311 | ||
312 | return l; | |
313 | } | |
314 | ||
e1873695 | 315 | char **path_strv_resolve_uniq(char **l, const char *root) { |
112cfb18 | 316 | |
fabe5c0e LP |
317 | if (strv_isempty(l)) |
318 | return l; | |
319 | ||
e1873695 | 320 | if (!path_strv_resolve(l, root)) |
fabe5c0e LP |
321 | return NULL; |
322 | ||
323 | return strv_uniq(l); | |
324 | } | |
325 | ||
9eb977db KS |
326 | char *path_kill_slashes(char *path) { |
327 | char *f, *t; | |
328 | bool slash = false; | |
329 | ||
330 | /* Removes redundant inner and trailing slashes. Modifies the | |
331 | * passed string in-place. | |
332 | * | |
333 | * ///foo///bar/ becomes /foo/bar | |
334 | */ | |
335 | ||
336 | for (f = path, t = path; *f; f++) { | |
337 | ||
338 | if (*f == '/') { | |
339 | slash = true; | |
340 | continue; | |
341 | } | |
342 | ||
343 | if (slash) { | |
344 | slash = false; | |
345 | *(t++) = '/'; | |
346 | } | |
347 | ||
348 | *(t++) = *f; | |
349 | } | |
350 | ||
351 | /* Special rule, if we are talking of the root directory, a | |
352 | trailing slash is good */ | |
353 | ||
354 | if (t == path && slash) | |
355 | *(t++) = '/'; | |
356 | ||
357 | *t = 0; | |
358 | return path; | |
359 | } | |
360 | ||
424a19f8 | 361 | char* path_startswith(const char *path, const char *prefix) { |
9eb977db KS |
362 | assert(path); |
363 | assert(prefix); | |
364 | ||
0470289b ZJS |
365 | /* Returns a pointer to the start of the first component after the parts matched by |
366 | * the prefix, iff | |
367 | * - both paths are absolute or both paths are relative, | |
368 | * and | |
369 | * - each component in prefix in turn matches a component in path at the same position. | |
370 | * An empty string will be returned when the prefix and path are equivalent. | |
371 | * | |
372 | * Returns NULL otherwise. | |
373 | */ | |
374 | ||
9eb977db | 375 | if ((path[0] == '/') != (prefix[0] == '/')) |
424a19f8 | 376 | return NULL; |
9eb977db KS |
377 | |
378 | for (;;) { | |
379 | size_t a, b; | |
380 | ||
381 | path += strspn(path, "/"); | |
382 | prefix += strspn(prefix, "/"); | |
383 | ||
384 | if (*prefix == 0) | |
424a19f8 | 385 | return (char*) path; |
9eb977db KS |
386 | |
387 | if (*path == 0) | |
424a19f8 | 388 | return NULL; |
9eb977db KS |
389 | |
390 | a = strcspn(path, "/"); | |
391 | b = strcspn(prefix, "/"); | |
392 | ||
393 | if (a != b) | |
424a19f8 | 394 | return NULL; |
9eb977db KS |
395 | |
396 | if (memcmp(path, prefix, a) != 0) | |
424a19f8 | 397 | return NULL; |
9eb977db KS |
398 | |
399 | path += a; | |
400 | prefix += b; | |
401 | } | |
402 | } | |
403 | ||
2230852b MS |
404 | int path_compare(const char *a, const char *b) { |
405 | int d; | |
406 | ||
9eb977db KS |
407 | assert(a); |
408 | assert(b); | |
409 | ||
2230852b MS |
410 | /* A relative path and an abolute path must not compare as equal. |
411 | * Which one is sorted before the other does not really matter. | |
412 | * Here a relative path is ordered before an absolute path. */ | |
413 | d = (a[0] == '/') - (b[0] == '/'); | |
373cd63a | 414 | if (d != 0) |
2230852b | 415 | return d; |
9eb977db KS |
416 | |
417 | for (;;) { | |
418 | size_t j, k; | |
419 | ||
420 | a += strspn(a, "/"); | |
421 | b += strspn(b, "/"); | |
422 | ||
423 | if (*a == 0 && *b == 0) | |
2230852b | 424 | return 0; |
9eb977db | 425 | |
2230852b MS |
426 | /* Order prefixes first: "/foo" before "/foo/bar" */ |
427 | if (*a == 0) | |
428 | return -1; | |
429 | if (*b == 0) | |
430 | return 1; | |
9eb977db KS |
431 | |
432 | j = strcspn(a, "/"); | |
433 | k = strcspn(b, "/"); | |
434 | ||
2230852b MS |
435 | /* Alphabetical sort: "/foo/aaa" before "/foo/b" */ |
436 | d = memcmp(a, b, MIN(j, k)); | |
373cd63a | 437 | if (d != 0) |
2230852b | 438 | return (d > 0) - (d < 0); /* sign of d */ |
9eb977db | 439 | |
2230852b MS |
440 | /* Sort "/foo/a" before "/foo/aaa" */ |
441 | d = (j > k) - (j < k); /* sign of (j - k) */ | |
373cd63a | 442 | if (d != 0) |
2230852b | 443 | return d; |
9eb977db KS |
444 | |
445 | a += j; | |
446 | b += k; | |
447 | } | |
448 | } | |
449 | ||
2230852b MS |
450 | bool path_equal(const char *a, const char *b) { |
451 | return path_compare(a, b) == 0; | |
452 | } | |
453 | ||
e3f791a2 ZJS |
454 | bool path_equal_or_files_same(const char *a, const char *b, int flags) { |
455 | return path_equal(a, b) || files_same(a, b, flags) > 0; | |
c78e47a6 MS |
456 | } |
457 | ||
0c6ea3a4 ZJS |
458 | char* path_join(const char *root, const char *path, const char *rest) { |
459 | assert(path); | |
460 | ||
461 | if (!isempty(root)) | |
bc854dc7 | 462 | return strjoin(root, endswith(root, "/") ? "" : "/", |
0c6ea3a4 | 463 | path[0] == '/' ? path+1 : path, |
bc854dc7 | 464 | rest ? (endswith(path, "/") ? "" : "/") : NULL, |
605405c6 | 465 | rest && rest[0] == '/' ? rest+1 : rest); |
0c6ea3a4 ZJS |
466 | else |
467 | return strjoin(path, | |
bc854dc7 | 468 | rest ? (endswith(path, "/") ? "" : "/") : NULL, |
605405c6 | 469 | rest && rest[0] == '/' ? rest+1 : rest); |
0c6ea3a4 ZJS |
470 | } |
471 | ||
85eca92e LP |
472 | int find_binary(const char *name, char **ret) { |
473 | int last_error, r; | |
474 | const char *p; | |
475 | ||
c9d954b2 | 476 | assert(name); |
4087cb9e | 477 | |
571d0134 | 478 | if (is_path(name)) { |
85eca92e | 479 | if (access(name, X_OK) < 0) |
b972115c ZJS |
480 | return -errno; |
481 | ||
85eca92e | 482 | if (ret) { |
0f474365 LP |
483 | r = path_make_absolute_cwd(name, ret); |
484 | if (r < 0) | |
485 | return r; | |
eb66db55 | 486 | } |
c9d954b2 | 487 | |
c9d954b2 | 488 | return 0; |
85eca92e | 489 | } |
c9d954b2 | 490 | |
85eca92e LP |
491 | /** |
492 | * Plain getenv, not secure_getenv, because we want | |
493 | * to actually allow the user to pick the binary. | |
494 | */ | |
495 | p = getenv("PATH"); | |
496 | if (!p) | |
497 | p = DEFAULT_PATH; | |
498 | ||
499 | last_error = -ENOENT; | |
500 | ||
501 | for (;;) { | |
502 | _cleanup_free_ char *j = NULL, *element = NULL; | |
503 | ||
504 | r = extract_first_word(&p, &element, ":", EXTRACT_RELAX|EXTRACT_DONT_COALESCE_SEPARATORS); | |
505 | if (r < 0) | |
506 | return r; | |
507 | if (r == 0) | |
508 | break; | |
509 | ||
0f474365 LP |
510 | if (!path_is_absolute(element)) |
511 | continue; | |
512 | ||
605405c6 | 513 | j = strjoin(element, "/", name); |
85eca92e LP |
514 | if (!j) |
515 | return -ENOMEM; | |
516 | ||
517 | if (access(j, X_OK) >= 0) { | |
518 | /* Found it! */ | |
c9d954b2 | 519 | |
85eca92e LP |
520 | if (ret) { |
521 | *ret = path_kill_slashes(j); | |
522 | j = NULL; | |
eb66db55 | 523 | } |
c9d954b2 ZJS |
524 | |
525 | return 0; | |
526 | } | |
527 | ||
85eca92e | 528 | last_error = -errno; |
c9d954b2 | 529 | } |
85eca92e LP |
530 | |
531 | return last_error; | |
c9d954b2 | 532 | } |
8e184852 | 533 | |
2ad8416d | 534 | bool paths_check_timestamp(const char* const* paths, usec_t *timestamp, bool update) { |
8e184852 | 535 | bool changed = false; |
2ad8416d | 536 | const char* const* i; |
8e184852 | 537 | |
97f2d76d TG |
538 | assert(timestamp); |
539 | ||
8e184852 | 540 | if (paths == NULL) |
4087cb9e | 541 | return false; |
8e184852 | 542 | |
4087cb9e | 543 | STRV_FOREACH(i, paths) { |
8e184852 | 544 | struct stat stats; |
4087cb9e | 545 | usec_t u; |
8e184852 | 546 | |
4087cb9e | 547 | if (stat(*i, &stats) < 0) |
8e184852 TG |
548 | continue; |
549 | ||
4087cb9e LP |
550 | u = timespec_load(&stats.st_mtim); |
551 | ||
97f2d76d | 552 | /* first check */ |
4087cb9e | 553 | if (*timestamp >= u) |
8e184852 TG |
554 | continue; |
555 | ||
9f6445e3 | 556 | log_debug("timestamp of '%s' changed", *i); |
8e184852 TG |
557 | |
558 | /* update timestamp */ | |
4087cb9e LP |
559 | if (update) { |
560 | *timestamp = u; | |
561 | changed = true; | |
562 | } else | |
563 | return true; | |
8e184852 | 564 | } |
4087cb9e | 565 | |
8e184852 TG |
566 | return changed; |
567 | } | |
eb66db55 | 568 | |
5bcd08db | 569 | static int binary_is_good(const char *binary) { |
571d0134 | 570 | _cleanup_free_ char *p = NULL, *d = NULL; |
571d0134 | 571 | int r; |
eb66db55 | 572 | |
85eca92e LP |
573 | r = find_binary(binary, &p); |
574 | if (r == -ENOENT) | |
575 | return 0; | |
571d0134 LP |
576 | if (r < 0) |
577 | return r; | |
578 | ||
061df014 | 579 | /* An fsck that is linked to /bin/true is a non-existent |
571d0134 LP |
580 | * fsck */ |
581 | ||
582 | r = readlink_malloc(p, &d); | |
85eca92e LP |
583 | if (r == -EINVAL) /* not a symlink */ |
584 | return 1; | |
585 | if (r < 0) | |
586 | return r; | |
571d0134 | 587 | |
3ae5990c ZJS |
588 | return !PATH_IN_SET(d, "true" |
589 | "/bin/true", | |
590 | "/usr/bin/true", | |
591 | "/dev/null"); | |
eb66db55 | 592 | } |
1d13f648 | 593 | |
5bcd08db LP |
594 | int fsck_exists(const char *fstype) { |
595 | const char *checker; | |
596 | ||
85eca92e | 597 | assert(fstype); |
5bcd08db | 598 | |
85eca92e LP |
599 | if (streq(fstype, "auto")) |
600 | return -EINVAL; | |
601 | ||
602 | checker = strjoina("fsck.", fstype); | |
5bcd08db LP |
603 | return binary_is_good(checker); |
604 | } | |
605 | ||
606 | int mkfs_exists(const char *fstype) { | |
607 | const char *mkfs; | |
608 | ||
85eca92e | 609 | assert(fstype); |
5bcd08db | 610 | |
85eca92e LP |
611 | if (streq(fstype, "auto")) |
612 | return -EINVAL; | |
613 | ||
614 | mkfs = strjoina("mkfs.", fstype); | |
5bcd08db LP |
615 | return binary_is_good(mkfs); |
616 | } | |
617 | ||
1d13f648 LP |
618 | char *prefix_root(const char *root, const char *path) { |
619 | char *n, *p; | |
620 | size_t l; | |
621 | ||
622 | /* If root is passed, prefixes path with it. Otherwise returns | |
623 | * it as is. */ | |
624 | ||
625 | assert(path); | |
626 | ||
627 | /* First, drop duplicate prefixing slashes from the path */ | |
628 | while (path[0] == '/' && path[1] == '/') | |
629 | path++; | |
630 | ||
631 | if (isempty(root) || path_equal(root, "/")) | |
632 | return strdup(path); | |
633 | ||
634 | l = strlen(root) + 1 + strlen(path) + 1; | |
635 | ||
636 | n = new(char, l); | |
637 | if (!n) | |
638 | return NULL; | |
639 | ||
640 | p = stpcpy(n, root); | |
641 | ||
642 | while (p > n && p[-1] == '/') | |
643 | p--; | |
644 | ||
645 | if (path[0] != '/') | |
646 | *(p++) = '/'; | |
647 | ||
648 | strcpy(p, path); | |
649 | return n; | |
650 | } | |
0f03c2a4 LP |
651 | |
652 | int parse_path_argument_and_warn(const char *path, bool suppress_root, char **arg) { | |
653 | char *p; | |
654 | int r; | |
655 | ||
656 | /* | |
657 | * This function is intended to be used in command line | |
658 | * parsers, to handle paths that are passed in. It makes the | |
659 | * path absolute, and reduces it to NULL if omitted or | |
660 | * root (the latter optionally). | |
661 | * | |
662 | * NOTE THAT THIS WILL FREE THE PREVIOUS ARGUMENT POINTER ON | |
663 | * SUCCESS! Hence, do not pass in uninitialized pointers. | |
664 | */ | |
665 | ||
666 | if (isempty(path)) { | |
667 | *arg = mfree(*arg); | |
668 | return 0; | |
669 | } | |
670 | ||
671 | r = path_make_absolute_cwd(path, &p); | |
672 | if (r < 0) | |
673 | return log_error_errno(r, "Failed to parse path \"%s\" and make it absolute: %m", path); | |
674 | ||
675 | path_kill_slashes(p); | |
676 | if (suppress_root && path_equal(p, "/")) | |
677 | p = mfree(p); | |
678 | ||
679 | free(*arg); | |
680 | *arg = p; | |
681 | return 0; | |
682 | } | |
5f311f8c LP |
683 | |
684 | char* dirname_malloc(const char *path) { | |
685 | char *d, *dir, *dir2; | |
686 | ||
687 | assert(path); | |
688 | ||
689 | d = strdup(path); | |
690 | if (!d) | |
691 | return NULL; | |
692 | ||
693 | dir = dirname(d); | |
694 | assert(dir); | |
695 | ||
696 | if (dir == d) | |
697 | return d; | |
698 | ||
699 | dir2 = strdup(dir); | |
700 | free(d); | |
701 | ||
702 | return dir2; | |
703 | } | |
bb15fafe LP |
704 | |
705 | bool filename_is_valid(const char *p) { | |
706 | const char *e; | |
707 | ||
708 | if (isempty(p)) | |
709 | return false; | |
710 | ||
49bfc877 | 711 | if (dot_or_dot_dot(p)) |
bb15fafe LP |
712 | return false; |
713 | ||
714 | e = strchrnul(p, '/'); | |
715 | if (*e != 0) | |
716 | return false; | |
717 | ||
718 | if (e - p > FILENAME_MAX) | |
719 | return false; | |
720 | ||
721 | return true; | |
722 | } | |
723 | ||
724 | bool path_is_safe(const char *p) { | |
725 | ||
726 | if (isempty(p)) | |
727 | return false; | |
728 | ||
49bfc877 LP |
729 | if (dot_or_dot_dot(p)) |
730 | return false; | |
731 | ||
732 | if (startswith(p, "../") || endswith(p, "/..") || strstr(p, "/../")) | |
bb15fafe LP |
733 | return false; |
734 | ||
735 | if (strlen(p)+1 > PATH_MAX) | |
736 | return false; | |
737 | ||
738 | /* The following two checks are not really dangerous, but hey, they still are confusing */ | |
49bfc877 | 739 | if (startswith(p, "./") || endswith(p, "/.") || strstr(p, "/./")) |
bb15fafe LP |
740 | return false; |
741 | ||
742 | if (strstr(p, "//")) | |
743 | return false; | |
744 | ||
745 | return true; | |
746 | } | |
a0956174 LP |
747 | |
748 | char *file_in_same_dir(const char *path, const char *filename) { | |
749 | char *e, *ret; | |
750 | size_t k; | |
751 | ||
752 | assert(path); | |
753 | assert(filename); | |
754 | ||
755 | /* This removes the last component of path and appends | |
756 | * filename, unless the latter is absolute anyway or the | |
757 | * former isn't */ | |
758 | ||
759 | if (path_is_absolute(filename)) | |
760 | return strdup(filename); | |
761 | ||
762 | e = strrchr(path, '/'); | |
763 | if (!e) | |
764 | return strdup(filename); | |
765 | ||
766 | k = strlen(filename); | |
767 | ret = new(char, (e + 1 - path) + k + 1); | |
768 | if (!ret) | |
769 | return NULL; | |
770 | ||
771 | memcpy(mempcpy(ret, path, e + 1 - path), filename, k + 1); | |
772 | return ret; | |
773 | } | |
774 | ||
55cdd057 ZJS |
775 | bool hidden_or_backup_file(const char *filename) { |
776 | const char *p; | |
a0956174 | 777 | |
a0956174 LP |
778 | assert(filename); |
779 | ||
55cdd057 ZJS |
780 | if (filename[0] == '.' || |
781 | streq(filename, "lost+found") || | |
782 | streq(filename, "aquota.user") || | |
783 | streq(filename, "aquota.group") || | |
784 | endswith(filename, "~")) | |
a0956174 LP |
785 | return true; |
786 | ||
55cdd057 ZJS |
787 | p = strrchr(filename, '.'); |
788 | if (!p) | |
789 | return false; | |
790 | ||
941060bf LP |
791 | /* Please, let's not add more entries to the list below. If external projects think it's a good idea to come up |
792 | * with always new suffixes and that everybody else should just adjust to that, then it really should be on | |
793 | * them. Hence, in future, let's not add any more entries. Instead, let's ask those packages to instead adopt | |
794 | * one of the generic suffixes/prefixes for hidden files or backups, possibly augmented with an additional | |
795 | * string. Specifically: there's now: | |
796 | * | |
797 | * The generic suffixes "~" and ".bak" for backup files | |
798 | * The generic prefix "." for hidden files | |
799 | * | |
94a0ef6e ZJS |
800 | * Thus, if a new package manager "foopkg" wants its own set of ".foopkg-new", ".foopkg-old", ".foopkg-dist" |
801 | * or so registered, let's refuse that and ask them to use ".foopkg.new", ".foopkg.old" or ".foopkg~" instead. | |
941060bf LP |
802 | */ |
803 | ||
55cdd057 ZJS |
804 | return STR_IN_SET(p + 1, |
805 | "rpmnew", | |
806 | "rpmsave", | |
807 | "rpmorig", | |
808 | "dpkg-old", | |
809 | "dpkg-new", | |
810 | "dpkg-tmp", | |
811 | "dpkg-dist", | |
812 | "dpkg-bak", | |
813 | "dpkg-backup", | |
814 | "dpkg-remove", | |
815 | "ucf-new", | |
816 | "ucf-old", | |
817 | "ucf-dist", | |
941060bf | 818 | "swp", |
94a0ef6e ZJS |
819 | "bak", |
820 | "old", | |
821 | "new"); | |
a0956174 LP |
822 | } |
823 | ||
824 | bool is_device_path(const char *path) { | |
825 | ||
826 | /* Returns true on paths that refer to a device, either in | |
827 | * sysfs or in /dev */ | |
828 | ||
3ccb8862 ZJS |
829 | return path_startswith(path, "/dev/") || |
830 | path_startswith(path, "/sys/"); | |
831 | } | |
832 | ||
833 | bool is_deviceallow_pattern(const char *path) { | |
834 | return path_startswith(path, "/dev/") || | |
835 | startswith(path, "block-") || | |
836 | startswith(path, "char-"); | |
a0956174 | 837 | } |
5a46d55f ZJS |
838 | |
839 | int systemd_installation_has_version(const char *root, unsigned minimal_version) { | |
840 | const char *pattern; | |
841 | int r; | |
842 | ||
843 | /* Try to guess if systemd installation is later than the specified version. This | |
844 | * is hacky and likely to yield false negatives, particularly if the installation | |
845 | * is non-standard. False positives should be relatively rare. | |
846 | */ | |
847 | ||
848 | NULSTR_FOREACH(pattern, | |
849 | /* /lib works for systems without usr-merge, and for systems with a sane | |
850 | * usr-merge, where /lib is a symlink to /usr/lib. /usr/lib is necessary | |
851 | * for Gentoo which does a merge without making /lib a symlink. | |
852 | */ | |
853 | "lib/systemd/libsystemd-shared-*.so\0" | |
854 | "usr/lib/systemd/libsystemd-shared-*.so\0") { | |
855 | ||
856 | _cleanup_strv_free_ char **names = NULL; | |
857 | _cleanup_free_ char *path = NULL; | |
858 | char *c, **name; | |
859 | ||
860 | path = prefix_root(root, pattern); | |
861 | if (!path) | |
862 | return -ENOMEM; | |
863 | ||
864 | r = glob_extend(&names, path); | |
865 | if (r == -ENOENT) | |
866 | continue; | |
867 | if (r < 0) | |
868 | return r; | |
869 | ||
870 | assert_se((c = endswith(path, "*.so"))); | |
871 | *c = '\0'; /* truncate the glob part */ | |
872 | ||
873 | STRV_FOREACH(name, names) { | |
874 | /* This is most likely to run only once, hence let's not optimize anything. */ | |
875 | char *t, *t2; | |
876 | unsigned version; | |
877 | ||
878 | t = startswith(*name, path); | |
879 | if (!t) | |
880 | continue; | |
881 | ||
882 | t2 = endswith(t, ".so"); | |
883 | if (!t2) | |
884 | continue; | |
885 | ||
886 | t2[0] = '\0'; /* truncate the suffix */ | |
887 | ||
888 | r = safe_atou(t, &version); | |
889 | if (r < 0) { | |
890 | log_debug_errno(r, "Found libsystemd shared at \"%s.so\", but failed to parse version: %m", *name); | |
891 | continue; | |
892 | } | |
893 | ||
894 | log_debug("Found libsystemd shared at \"%s.so\", version %u (%s).", | |
895 | *name, version, | |
896 | version >= minimal_version ? "OK" : "too old"); | |
897 | if (version >= minimal_version) | |
898 | return true; | |
899 | } | |
900 | } | |
901 | ||
902 | return false; | |
903 | } | |
49bfc877 LP |
904 | |
905 | bool dot_or_dot_dot(const char *path) { | |
906 | if (!path) | |
907 | return false; | |
908 | if (path[0] != '.') | |
909 | return false; | |
910 | if (path[1] == 0) | |
911 | return true; | |
912 | if (path[1] != '.') | |
913 | return false; | |
914 | ||
915 | return path[2] == 0; | |
916 | } |