• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

systemd / systemd / 22742800078

06 Mar 2026 12:17AM UTC coverage: 72.574% (+0.3%) from 72.313%
22742800078

push

github

bluca
zsh: fixup some recent zsh completers

These two completers are written in a stacked _arguments style, and some
generic options are valid before or after the verb. If the toplevel
_arguments is permitted to match options after the verb, it will halt
completion prematurely, so stop toplevel matching after the verb.

This corrects the following error:

$ userdbctl --output=class user <TAB> # completes users
$ userdbctl user --output=class <TAB> # completes nothing

316081 of 435528 relevant lines covered (72.57%)

1223983.74 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.37
/src/basic/path-util.c
1
/* SPDX-License-Identifier: LGPL-2.1-or-later */
2

3
#include <fnmatch.h>
4
#include <stdio.h>
5
#include <stdlib.h>
6
#include <unistd.h>
7

8
#include "alloc-util.h"
9
#include "chase.h"
10
#include "errno-util.h"
11
#include "extract-word.h"
12
#include "fd-util.h"
13
#include "fs-util.h"
14
#include "glob-util.h"
15
#include "log.h"
16
#include "path-util.h"
17
#include "stat-util.h"
18
#include "string-util.h"
19
#include "strv.h"
20

21
bool is_path(const char *p) {
153,074✔
22
        if (!p) /* A NULL pointer is definitely not a path */
153,074✔
23
                return false;
24

25
        return strchr(p, '/');
153,074✔
26
}
27

28
int path_split_and_make_absolute(const char *p, char ***ret) {
1,168✔
29
        _cleanup_strv_free_ char **l = NULL;
1,168✔
30
        int r;
1,168✔
31

32
        assert(p);
1,168✔
33
        assert(ret);
1,168✔
34

35
        l = strv_split(p, ":");
1,168✔
36
        if (!l)
1,168✔
37
                return -ENOMEM;
38

39
        r = path_strv_make_absolute_cwd(l);
1,168✔
40
        if (r < 0)
1,168✔
41
                return r;
42

43
        *ret = TAKE_PTR(l);
1,168✔
44
        return r;
1,168✔
45
}
46

47
char* path_make_absolute(const char *p, const char *prefix) {
2,471✔
48
        assert(p);
2,471✔
49

50
        /* Makes every item in the list an absolute path by prepending
51
         * the prefix, if specified and necessary */
52

53
        if (path_is_absolute(p) || isempty(prefix))
2,471✔
54
                return strdup(p);
319✔
55

56
        return path_join(prefix, p);
2,152✔
57
}
58

59
int safe_getcwd(char **ret) {
381✔
60
        _cleanup_free_ char *cwd = NULL;
381✔
61

62
        cwd = get_current_dir_name();
381✔
63
        if (!cwd)
381✔
64
                return negative_errno();
×
65

66
        /* Let's make sure the directory is really absolute, to protect us from the logic behind
67
         * CVE-2018-1000001 */
68
        if (cwd[0] != '/')
381✔
69
                return -ENOMEDIUM;
70

71
        if (ret)
381✔
72
                *ret = TAKE_PTR(cwd);
381✔
73

74
        return 0;
75
}
76

77
int path_make_absolute_cwd(const char *p, char **ret) {
3,192,566✔
78
        char *c;
3,192,566✔
79
        int r;
3,192,566✔
80

81
        assert(p);
3,192,566✔
82
        assert(ret);
3,192,566✔
83

84
        /* Similar to path_make_absolute(), but prefixes with the
85
         * current working directory. */
86

87
        if (path_is_absolute(p))
3,192,566✔
88
                c = strdup(p);
3,192,277✔
89
        else {
90
                _cleanup_free_ char *cwd = NULL;
289✔
91

92
                r = safe_getcwd(&cwd);
289✔
93
                if (r < 0)
289✔
94
                        return r;
×
95

96
                c = path_join(cwd, p);
289✔
97
        }
98
        if (!c)
3,192,566✔
99
                return -ENOMEM;
100

101
        *ret = c;
3,192,566✔
102
        return 0;
3,192,566✔
103
}
104

105
int path_make_relative(const char *from, const char *to, char **ret) {
856,337✔
106
        _cleanup_free_ char *result = NULL;
856,337✔
107
        unsigned n_parents;
856,337✔
108
        const char *f, *t;
856,337✔
109
        int r, k;
856,337✔
110
        char *p;
856,337✔
111

112
        assert(from);
856,337✔
113
        assert(to);
856,337✔
114
        assert(ret);
856,337✔
115

116
        /* Strips the common part, and adds ".." elements as necessary. */
117

118
        if (!path_is_absolute(from) || !path_is_absolute(to))
1,712,670✔
119
                return -EINVAL;
120

121
        for (;;) {
6,045,817✔
122
                r = path_find_first_component(&from, true, &f);
6,045,817✔
123
                if (r < 0)
6,045,817✔
124
                        return r;
125

126
                k = path_find_first_component(&to, true, &t);
6,045,817✔
127
                if (k < 0)
6,045,817✔
128
                        return k;
129

130
                if (r == 0) {
6,045,817✔
131
                        /* end of 'from' */
132
                        if (k == 0) {
723,103✔
133
                                /* from and to are equivalent. */
134
                                result = strdup(".");
4✔
135
                                if (!result)
4✔
136
                                        return -ENOMEM;
137
                        } else {
138
                                /* 'to' is inside of 'from'. */
139
                                r = path_simplify_alloc(t, &result);
723,099✔
140
                                if (r < 0)
723,099✔
141
                                        return r;
142

143
                                if (!path_is_valid(result))
723,099✔
144
                                        return -EINVAL;
145
                        }
146

147
                        *ret = TAKE_PTR(result);
723,103✔
148
                        return 0;
723,103✔
149
                }
150

151
                if (r != k || !strneq(f, t, r))
5,322,714✔
152
                        break;
153
        }
154

155
        /* If we're here, then "from_dir" has one or more elements that need to
156
         * be replaced with "..". */
157

158
        for (n_parents = 1;; n_parents++) {
139,654✔
159
                /* If this includes ".." we can't do a simple series of "..". */
160
                r = path_find_first_component(&from, false, &f);
272,884✔
161
                if (r < 0)
272,884✔
162
                        return r;
163
                if (r == 0)
272,882✔
164
                        break;
165
        }
166

167
        if (isempty(t) && n_parents * 3 > PATH_MAX)
133,228✔
168
                /* PATH_MAX is counted *with* the trailing NUL byte */
169
                return -EINVAL;
170

171
        result = new(char, n_parents * 3 + !isempty(t) + strlen_ptr(t));
399,680✔
172
        if (!result)
133,228✔
173
                return -ENOMEM;
174

175
        for (p = result; n_parents > 0; n_parents--)
406,110✔
176
                p = mempcpy(p, "../", 3);
272,882✔
177

178
        if (isempty(t)) {
133,228✔
179
                /* Remove trailing slash and terminate string. */
180
                *(--p) = '\0';
2✔
181
                *ret = TAKE_PTR(result);
2✔
182
                return 0;
2✔
183
        }
184

185
        strcpy(p, t);
133,226✔
186

187
        path_simplify(result);
133,226✔
188

189
        if (!path_is_valid(result))
133,226✔
190
                return -EINVAL;
191

192
        *ret = TAKE_PTR(result);
133,226✔
193
        return 0;
133,226✔
194
}
195

196
int path_make_relative_parent(const char *from_child, const char *to, char **ret) {
133,303✔
197
        _cleanup_free_ char *from = NULL;
133,303✔
198
        int r;
133,303✔
199

200
        assert(from_child);
133,303✔
201
        assert(to);
133,303✔
202
        assert(ret);
133,303✔
203

204
        /* Similar to path_make_relative(), but provides the relative path from the parent directory of
205
         * 'from_child'. This may be useful when creating relative symlink.
206
         *
207
         * E.g.
208
         * - from = "/path/to/aaa", to = "/path/to/bbb"
209
         *      path_make_relative(from, to) = "../bbb"
210
         *      path_make_relative_parent(from, to) = "bbb"
211
         *
212
         * - from = "/path/to/aaa/bbb", to = "/path/to/ccc/ddd"
213
         *      path_make_relative(from, to) = "../../ccc/ddd"
214
         *      path_make_relative_parent(from, to) = "../ccc/ddd"
215
         */
216

217
        r = path_extract_directory(from_child, &from);
133,303✔
218
        if (r < 0)
133,303✔
219
                return r;
220

221
        return path_make_relative(from, to, ret);
133,302✔
222
}
223

224
char* path_startswith_strv(const char *p, char * const *strv) {
189,201✔
225
        assert(p);
189,201✔
226

227
        STRV_FOREACH(s, strv) {
716,410✔
228
                char *t;
558,620✔
229

230
                t = path_startswith(p, *s);
558,620✔
231
                if (t)
558,620✔
232
                        return t;
233
        }
234

235
        return NULL;
236
}
237

238
int path_strv_make_absolute_cwd(char **l) {
63,385✔
239
        int r;
63,385✔
240

241
        /* Goes through every item in the string list and makes it
242
         * absolute. This works in place and won't rollback any
243
         * changes on failure. */
244

245
        STRV_FOREACH(s, l) {
501,925✔
246
                char *t;
438,540✔
247

248
                r = path_make_absolute_cwd(*s, &t);
438,540✔
249
                if (r < 0)
438,540✔
250
                        return r;
×
251

252
                path_simplify(t);
438,540✔
253
                free_and_replace(*s, t);
438,540✔
254
        }
255

256
        return 0;
257
}
258

259
char** path_strv_resolve(char **l, const char *root) {
27,221✔
260
        unsigned k = 0;
27,221✔
261
        bool enomem = false;
27,221✔
262
        int r;
27,221✔
263

264
        if (strv_isempty(l))
54,442✔
265
                return l;
266

267
        /* Goes through every item in the string list and canonicalize
268
         * the path. This works in place and won't rollback any
269
         * changes on failure. */
270

271
        STRV_FOREACH(s, l) {
162,490✔
272
                _cleanup_free_ char *orig = NULL;
135,269✔
273
                char *t, *u;
135,269✔
274

275
                if (!path_is_absolute(*s)) {
135,269✔
276
                        free(*s);
×
277
                        continue;
×
278
                }
279

280
                if (root) {
135,269✔
281
                        orig = *s;
7✔
282
                        t = path_join(root, orig);
7✔
283
                        if (!t) {
7✔
284
                                enomem = true;
×
285
                                continue;
×
286
                        }
287
                } else
288
                        t = *s;
289

290
                r = chase(t, root, 0, &u, NULL);
135,269✔
291
                if (r == -ENOENT) {
135,269✔
292
                        if (root) {
113,693✔
293
                                u = TAKE_PTR(orig);
2✔
294
                                free(t);
2✔
295
                        } else
296
                                u = t;
113,691✔
297
                } else if (r < 0) {
21,576✔
298
                        free(t);
×
299

300
                        if (r == -ENOMEM)
×
301
                                enomem = true;
×
302

303
                        continue;
×
304
                } else if (root) {
21,576✔
305
                        char *x;
5✔
306

307
                        free(t);
5✔
308
                        x = path_startswith(u, root);
5✔
309
                        if (x) {
5✔
310
                                /* restore the slash if it was lost */
311
                                if (!startswith(x, "/"))
5✔
312
                                        *(--x) = '/';
5✔
313

314
                                t = strdup(x);
5✔
315
                                free(u);
5✔
316
                                if (!t) {
5✔
317
                                        enomem = true;
×
318
                                        continue;
×
319
                                }
320
                                u = t;
5✔
321
                        } else {
322
                                /* canonicalized path goes outside of
323
                                 * prefix, keep the original path instead */
324
                                free_and_replace(u, orig);
×
325
                        }
326
                } else
327
                        free(t);
21,571✔
328

329
                l[k++] = u;
135,269✔
330
        }
331

332
        l[k] = NULL;
27,221✔
333

334
        if (enomem)
27,221✔
335
                return NULL;
×
336

337
        return l;
338
}
339

340
char** path_strv_resolve_uniq(char **l, const char *root) {
27,220✔
341

342
        if (strv_isempty(l))
27,220✔
343
                return l;
344

345
        if (!path_strv_resolve(l, root))
27,220✔
346
                return NULL;
347

348
        return strv_uniq(l);
27,220✔
349
}
350

351
char* skip_leading_slash(const char *p) {
114,496✔
352
        return skip_leading_chars(p, "/");
114,496✔
353
}
354

355
char* path_simplify_full(char *path, PathSimplifyFlags flags) {
9,144,899✔
356
        bool add_slash = false, keep_trailing_slash, absolute, beginning = true;
9,144,899✔
357
        char *f = path;
9,144,899✔
358
        int r;
9,144,899✔
359

360
        /* Removes redundant inner and trailing slashes. Also removes unnecessary dots.
361
         * Modifies the passed string in-place.
362
         *
363
         * ///foo//./bar/.   becomes /foo/bar
364
         * .//./foo//./bar/. becomes foo/bar
365
         * /../foo/bar       becomes /foo/bar
366
         * /../foo/bar/..    becomes /foo/bar/..
367
         */
368

369
        if (isempty(path))
9,144,899✔
370
                return path;
371

372
        keep_trailing_slash = FLAGS_SET(flags, PATH_SIMPLIFY_KEEP_TRAILING_SLASH) && endswith(path, "/");
9,112,351✔
373

374
        absolute = path_is_absolute(path);
9,112,351✔
375
        f += absolute;  /* Keep leading /, if present. */
9,112,351✔
376

377
        for (const char *p = f;;) {
9,112,351✔
378
                const char *e;
37,543,283✔
379

380
                r = path_find_first_component(&p, true, &e);
37,543,283✔
381
                if (r == 0)
37,543,283✔
382
                        break;
383

384
                if (r > 0 && absolute && beginning && path_startswith(e, ".."))
28,430,936✔
385
                        /* If we're at the beginning of an absolute path, we can safely skip ".." */
386
                        continue;
79✔
387

388
                beginning = false;
28,430,857✔
389

390
                if (add_slash)
28,430,857✔
391
                        *f++ = '/';
19,328,246✔
392

393
                if (r < 0) {
28,430,857✔
394
                        /* if path is invalid, then refuse to simplify the remaining part. */
395
                        memmove(f, p, strlen(p) + 1);
4✔
396
                        return path;
4✔
397
                }
398

399
                memmove(f, e, r);
28,430,853✔
400
                f += r;
28,430,853✔
401

402
                add_slash = true;
28,430,853✔
403
        }
404

405
        /* Special rule, if we stripped everything, we need a "." for the current directory. */
406
        if (f == path)
9,112,347✔
407
                *f++ = '.';
7✔
408

409
        if (*(f-1) != '/' && keep_trailing_slash)
9,112,347✔
410
                *f++ = '/';
44✔
411

412
        *f = '\0';
9,112,347✔
413
        return path;
9,112,347✔
414
}
415

416
int path_simplify_alloc(const char *path, char **ret) {
1,782,559✔
417
        assert(ret);
1,782,559✔
418

419
        if (!path) {
1,782,559✔
420
                *ret = NULL;
17,121✔
421
                return 0;
17,121✔
422
        }
423

424
        char *t = strdup(path);
1,765,438✔
425
        if (!t)
1,765,438✔
426
                return -ENOMEM;
427

428
        *ret = path_simplify(t);
1,765,438✔
429
        return 0;
1,765,438✔
430
}
431

432
char* path_startswith_full(const char *original_path, const char *prefix, PathStartWithFlags flags) {
54,062,953✔
433
        assert(original_path);
54,062,953✔
434
        assert(prefix);
54,062,953✔
435

436
        /* Returns a pointer to the start of the first component after the parts matched by
437
         * the prefix, iff
438
         * - both paths are absolute or both paths are relative,
439
         * and
440
         * - each component in prefix in turn matches a component in path at the same position.
441
         * An empty string will be returned when the prefix and path are equivalent.
442
         *
443
         * Returns NULL otherwise.
444
         */
445

446
        const char *path = original_path;
54,062,953✔
447

448
        if ((path[0] == '/') != (prefix[0] == '/'))
54,062,953✔
449
                return NULL;
54,062,953✔
450

451
        for (;;) {
47,087,283✔
452
                const char *p, *q;
101,002,448✔
453
                int m, n;
101,002,448✔
454

455
                m = path_find_first_component(&path, !FLAGS_SET(flags, PATH_STARTSWITH_REFUSE_DOT_DOT), &p);
101,002,448✔
456
                if (m < 0)
101,002,448✔
457
                        return NULL;
53,915,165✔
458

459
                n = path_find_first_component(&prefix, !FLAGS_SET(flags, PATH_STARTSWITH_REFUSE_DOT_DOT), &q);
101,002,448✔
460
                if (n < 0)
101,002,448✔
461
                        return NULL;
462

463
                if (n == 0) {
101,002,448✔
464
                        if (!p)
39,891,747✔
465
                                p = path;
204,846✔
466

467
                        if (FLAGS_SET(flags, PATH_STARTSWITH_RETURN_LEADING_SLASH)) {
39,891,747✔
468

469
                                if (p <= original_path)
19✔
470
                                        return NULL;
471

472
                                p--;
19✔
473

474
                                if (*p != '/')
19✔
475
                                        return NULL;
476
                        }
477

478
                        return (char*) p;
39,891,746✔
479
                }
480

481
                if (m != n)
61,110,701✔
482
                        return NULL;
483

484
                if (!strneq(p, q, m))
49,528,134✔
485
                        return NULL;
486
        }
487
}
488

489
int path_compare(const char *a, const char *b) {
31,245,488✔
490
        int r;
31,245,488✔
491

492
        /* Order NULL before non-NULL */
493
        r = CMP(!!a, !!b);
31,245,488✔
494
        if (r != 0)
31,205,039✔
495
                return r;
40,455✔
496

497
        /* A relative path and an absolute path must not compare as equal.
498
         * Which one is sorted before the other does not really matter.
499
         * Here a relative path is ordered before an absolute path. */
500
        r = CMP(path_is_absolute(a), path_is_absolute(b));
62,409,992✔
501
        if (r != 0)
30,906,812✔
502
                return r;
347,085✔
503

504
        for (;;) {
25,072,872✔
505
                const char *aa, *bb;
55,930,820✔
506
                int j, k;
55,930,820✔
507

508
                j = path_find_first_component(&a, true, &aa);
55,930,820✔
509
                k = path_find_first_component(&b, true, &bb);
55,930,820✔
510

511
                if (j < 0 || k < 0) {
55,930,820✔
512
                        /* When one of paths is invalid, order invalid path after valid one. */
513
                        r = CMP(j < 0, k < 0);
8✔
514
                        if (r != 0)
×
515
                                return r;
30,857,948✔
516

517
                        /* fallback to use strcmp() if both paths are invalid. */
518
                        return strcmp(a, b);
×
519
                }
520

521
                /* Order prefixes first: "/foo" before "/foo/bar" */
522
                if (j == 0) {
55,930,812✔
523
                        if (k == 0)
6,715,824✔
524
                                return 0;
525
                        return -1;
135,228✔
526
                }
527
                if (k == 0)
49,214,988✔
528
                        return 1;
529

530
                /* Alphabetical sort: "/foo/aaa" before "/foo/b" */
531
                r = memcmp(aa, bb, MIN(j, k));
47,599,815✔
532
                if (r != 0)
47,599,815✔
533
                        return r;
534

535
                /* Sort "/foo/a" before "/foo/aaa" */
536
                r = CMP(j, k);
25,166,993✔
537
                if (r != 0)
25,108,414✔
538
                        return r;
94,121✔
539
        }
540
}
541

542
int path_compare_filename(const char *a, const char *b) {
165,504✔
543
        _cleanup_free_ char *fa = NULL, *fb = NULL;
165,504✔
544
        int r, j, k;
165,504✔
545

546
        /* Order NULL before non-NULL */
547
        r = CMP(!!a, !!b);
165,504✔
548
        if (r != 0)
165,504✔
549
                return r;
×
550

551
        j = path_extract_filename(a, &fa);
165,504✔
552
        k = path_extract_filename(b, &fb);
165,504✔
553

554
        /* When one of paths is "." or root, then order it earlier. */
555
        r = CMP(j != -EADDRNOTAVAIL, k != -EADDRNOTAVAIL);
165,504✔
556
        if (r != 0)
165,500✔
557
                return r;
8✔
558

559
        /* When one of paths is invalid (or we get OOM), order invalid path after valid one. */
560
        r = CMP(j < 0, k < 0);
165,496✔
561
        if (r != 0)
165,496✔
562
                return r;
×
563

564
        /* fallback to use strcmp() if both paths are invalid. */
565
        if (j < 0)
165,496✔
566
                return strcmp(a, b);
30✔
567

568
        return strcmp(fa, fb);
165,466✔
569
}
570

571
int path_equal_or_inode_same_full(const char *a, const char *b, int flags) {
52,328✔
572
        /* Returns true if paths are of the same entry, false if not, <0 on error. */
573

574
        if (path_equal(a, b))
52,328✔
575
                return 1;
576

577
        if (!a || !b)
303✔
578
                return 0;
579

580
        return inode_same(a, b, flags);
303✔
581
}
582

583
char* path_extend_internal(char **x, ...) {
25,298,642✔
584
        size_t sz, old_sz;
25,298,642✔
585
        char *q, *nx;
25,298,642✔
586
        const char *p;
25,298,642✔
587
        va_list ap;
25,298,642✔
588
        bool slash;
25,298,642✔
589

590
        /* Joins all listed strings until the sentinel and places a "/" between them unless the strings
591
         * end/begin already with one so that it is unnecessary. Note that slashes which are already
592
         * duplicate won't be removed. The string returned is hence always equal to or longer than the sum of
593
         * the lengths of the individual strings.
594
         *
595
         * The first argument may be an already allocated string that is extended via realloc() if
596
         * non-NULL. path_extend() and path_join() are macro wrappers around this function, making use of the
597
         * first parameter to distinguish the two operations.
598
         *
599
         * Note: any listed empty string is simply skipped. This can be useful for concatenating strings of
600
         * which some are optional.
601
         *
602
         * Examples:
603
         *
604
         * path_join("foo", "bar") → "foo/bar"
605
         * path_join("foo/", "bar") → "foo/bar"
606
         * path_join("", "foo", "", "bar", "") → "foo/bar" */
607

608
        sz = old_sz = x ? strlen_ptr(*x) : 0;
25,298,642✔
609
        va_start(ap, x);
25,298,642✔
610
        while ((p = va_arg(ap, char*)) != POINTER_MAX) {
57,612,344✔
611
                size_t add;
32,313,702✔
612

613
                if (isempty(p))
32,313,702✔
614
                        continue;
842,517✔
615

616
                add = 1 + strlen(p);
31,471,185✔
617
                if (sz > SIZE_MAX - add) { /* overflow check */
31,471,185✔
618
                        va_end(ap);
×
619
                        return NULL;
×
620
                }
621

622
                sz += add;
31,471,185✔
623
        }
624
        va_end(ap);
25,298,642✔
625

626
        nx = realloc(x ? *x : NULL, GREEDY_ALLOC_ROUND_UP(sz+1));
25,298,642✔
627
        if (!nx)
25,298,642✔
628
                return NULL;
629
        if (x)
25,298,642✔
630
                *x = nx;
18,817,083✔
631

632
        if (old_sz > 0)
25,298,642✔
633
                slash = nx[old_sz-1] == '/';
18,738,100✔
634
        else {
635
                nx[old_sz] = 0;
6,560,542✔
636
                slash = true; /* no need to generate a slash anymore */
6,560,542✔
637
        }
638

639
        q = nx + old_sz;
25,298,642✔
640

641
        va_start(ap, x);
25,298,642✔
642
        while ((p = va_arg(ap, char*)) != POINTER_MAX) {
57,612,344✔
643
                if (isempty(p))
32,313,702✔
644
                        continue;
842,517✔
645

646
                if (!slash && p[0] != '/')
31,471,185✔
647
                        *(q++) = '/';
19,302,604✔
648

649
                q = stpcpy(q, p);
31,471,185✔
650
                slash = endswith(p, "/");
31,471,185✔
651
        }
652
        va_end(ap);
25,298,642✔
653

654
        return nx;
25,298,642✔
655
}
656

657
int open_and_check_executable(const char *name, const char *root, char **ret_path, int *ret_fd) {
30,330✔
658
        _cleanup_close_ int fd = -EBADF;
30,330✔
659
        _cleanup_free_ char *resolved = NULL;
30,330✔
660
        int r;
30,330✔
661

662
        assert(name);
30,330✔
663

664
        /* Function chase() is invoked only when root is not NULL, as using it regardless of
665
         * root value would alter the behavior of existing callers for example: /bin/sleep would become
666
         * /usr/bin/sleep when find_executables is called. Hence, this function should be invoked when
667
         * needed to avoid unforeseen regression or other complicated changes. */
668
        if (root) {
30,330✔
669
                /* prefix root to name in case full paths are not specified */
670
                r = chase(name, root, CHASE_PREFIX_ROOT, &resolved, &fd);
4✔
671
                if (r < 0)
4✔
672
                        return r;
673

674
                name = resolved;
4✔
675
        } else {
676
                /* We need to use O_PATH because there may be executables for which we have only exec permissions,
677
                 * but not read (usually suid executables). */
678
                fd = open(name, O_PATH|O_CLOEXEC);
30,326✔
679
                if (fd < 0)
30,326✔
680
                        return -errno;
5,260✔
681
        }
682

683
        r = fd_verify_regular(fd);
25,070✔
684
        if (r < 0)
25,070✔
685
                return r;
686

687
        r = access_fd(fd, X_OK);
25,067✔
688
        if (r == -ENOSYS)
25,067✔
689
                /* /proc/ is not mounted. Fall back to access(). */
690
                r = RET_NERRNO(access(name, X_OK));
×
691
        if (r < 0)
25,067✔
692
                return r;
693

694
        if (ret_path) {
25,066✔
695
                if (resolved)
24,657✔
696
                        *ret_path = TAKE_PTR(resolved);
×
697
                else {
698
                        r = path_make_absolute_cwd(name, ret_path);
24,657✔
699
                        if (r < 0)
24,657✔
700
                                return r;
701

702
                        path_simplify(*ret_path);
24,657✔
703
                }
704
        }
705

706
        if (ret_fd)
25,066✔
707
                *ret_fd = TAKE_FD(fd);
23,122✔
708

709
        return 0;
710
}
711

712
int find_executable_full(
25,076✔
713
                const char *name,
714
                const char *root,
715
                char * const *exec_search_path,
716
                bool use_path_envvar,
717
                char **ret_filename,
718
                int *ret_fd) {
719

720
        int last_error = -ENOENT, r = 0;
25,076✔
721

722
        assert(name);
25,076✔
723

724
        if (is_path(name))
25,076✔
725
                return open_and_check_executable(name, root, ret_filename, ret_fd);
25,076✔
726

727
        if (exec_search_path) {
3,980✔
728
                STRV_FOREACH(element, exec_search_path) {
6✔
729
                        _cleanup_free_ char *full_path = NULL;
5✔
730

731
                        if (!path_is_absolute(*element)) {
5✔
732
                                log_debug("Exec search path '%s' isn't absolute, ignoring.", *element);
×
733
                                continue;
×
734
                        }
735

736
                        full_path = path_join(*element, name);
5✔
737
                        if (!full_path)
5✔
738
                                return -ENOMEM;
739

740
                        r = open_and_check_executable(full_path, root, ret_filename, ret_fd);
5✔
741
                        if (r >= 0)
5✔
742
                                return 0;
743
                        if (r != -EACCES)
4✔
744
                                last_error = r;
4✔
745
                }
746
                return last_error;
747
        }
748

749
        const char *p = NULL;
3,978✔
750

751
        if (use_path_envvar)
3,978✔
752
                /* Plain getenv, not secure_getenv, because we want to actually allow the user to pick the
753
                 * binary. */
754
                p = getenv("PATH");
1,283✔
755
        if (!p)
3,978✔
756
                p = default_PATH();
2,705✔
757

758
        /* Resolve a single-component name to a full path */
759
        for (;;) {
9,233✔
760
                _cleanup_free_ char *element = NULL;
9,229✔
761

762
                r = extract_first_word(&p, &element, ":", EXTRACT_RELAX|EXTRACT_DONT_COALESCE_SEPARATORS);
9,233✔
763
                if (r < 0)
9,233✔
764
                        return r;
765
                if (r == 0)
9,233✔
766
                        break;
767

768
                if (!path_is_absolute(element)) {
9,229✔
769
                        log_debug("Exec search path '%s' isn't absolute, ignoring.", element);
×
770
                        continue;
×
771
                }
772

773
                if (!path_extend(&element, name))
9,229✔
774
                        return -ENOMEM;
775

776
                r = open_and_check_executable(element, root, ret_filename, ret_fd);
9,229✔
777
                if (r >= 0) /* Found it! */
9,229✔
778
                        return 0;
779
                /* PATH entries which we don't have access to are ignored, as per tradition. */
780
                if (r != -EACCES)
5,255✔
781
                        last_error = r;
5,255✔
782
        }
783

784
        return last_error;
4✔
785
}
786

787
static int executable_is_good(const char *executable) {
190✔
788
        _cleanup_free_ char *p = NULL, *d = NULL;
190✔
789
        int r;
190✔
790

791
        r = find_executable(executable, &p);
190✔
792
        if (r == -ENOENT)
190✔
793
                return 0;
794
        if (r < 0)
188✔
795
                return r;
796

797
        /* An fsck that is linked to /bin/true is a non-existent fsck */
798

799
        r = readlink_malloc(p, &d);
188✔
800
        if (r == -EINVAL) /* not a symlink */
188✔
801
                return 1;
802
        if (r < 0)
71✔
803
                return r;
804

805
        return !PATH_IN_SET(d, "true"
71✔
806
                               "/bin/true",
807
                               "/usr/bin/true",
808
                               "/dev/null");
809
}
810

811
int fsck_exists(void) {
102✔
812
        return executable_is_good("fsck");
102✔
813
}
814

815
int fsck_exists_for_fstype(const char *fstype) {
88✔
816
        const char *checker;
88✔
817
        int r;
88✔
818

819
        assert(fstype);
88✔
820

821
        if (streq(fstype, "auto"))
88✔
822
                return -EINVAL;
823

824
        r = fsck_exists();
88✔
825
        if (r <= 0)
88✔
826
                return r;
827

828
        checker = strjoina("fsck.", fstype);
440✔
829
        return executable_is_good(checker);
88✔
830
}
831

832
static const char* skip_slash_or_dot(const char *p) {
1,096,129,556✔
833
        for (; !isempty(p); p++) {
1,694,444,958✔
834
                if (*p == '/')
1,474,165,099✔
835
                        continue;
598,314,637✔
836
                if (startswith(p, "./")) {
875,850,462✔
837
                        p++;
765✔
838
                        continue;
765✔
839
                }
840
                break;
841
        }
842
        return p;
1,096,129,556✔
843
}
844

845
int path_find_first_component(const char **p, bool accept_dot_dot, const char **ret) {
597,231,686✔
846
        const char *q, *first, *end_first, *next;
597,231,686✔
847
        size_t len;
597,231,686✔
848

849
        assert(p);
597,231,686✔
850

851
        /* When a path is input, then returns the pointer to the first component and its length, and
852
         * move the input pointer to the next component or nul. This skips both over any '/'
853
         * immediately *before* and *after* the first component before returning.
854
         *
855
         * Examples
856
         *   Input:  p: "//.//aaa///bbbbb/cc"
857
         *   Output: p: "bbbbb///cc"
858
         *           ret: "aaa///bbbbb/cc"
859
         *           return value: 3 (== strlen("aaa"))
860
         *
861
         *   Input:  p: "aaa//"
862
         *   Output: p: (pointer to NUL)
863
         *           ret: "aaa//"
864
         *           return value: 3 (== strlen("aaa"))
865
         *
866
         *   Input:  p: "/", ".", ""
867
         *   Output: p: (pointer to NUL)
868
         *           ret: NULL
869
         *           return value: 0
870
         *
871
         *   Input:  p: NULL
872
         *   Output: p: NULL
873
         *           ret: NULL
874
         *           return value: 0
875
         *
876
         *   Input:  p: "(too long component)"
877
         *   Output: return value: -EINVAL
878
         *
879
         *   (when accept_dot_dot is false)
880
         *   Input:  p: "//..//aaa///bbbbb/cc"
881
         *   Output: return value: -EINVAL
882
         */
883

884
        q = *p;
597,231,686✔
885

886
        first = skip_slash_or_dot(q);
597,231,686✔
887
        if (isempty(first)) {
597,231,686✔
888
                *p = first;
98,325,870✔
889
                if (ret)
98,325,870✔
890
                        *ret = NULL;
98,320,691✔
891
                return 0;
98,325,870✔
892
        }
893
        if (streq(first, ".")) {
498,905,816✔
894
                *p = first + 1;
7,735✔
895
                if (ret)
7,735✔
896
                        *ret = NULL;
7,729✔
897
                return 0;
7,735✔
898
        }
899

900
        end_first = strchrnul(first, '/');
498,898,081✔
901
        len = end_first - first;
498,898,081✔
902

903
        if (len > NAME_MAX)
498,898,081✔
904
                return -EINVAL;
905
        if (!accept_dot_dot && len == 2 && first[0] == '.' && first[1] == '.')
498,897,907✔
906
                return -EINVAL;
907

908
        next = skip_slash_or_dot(end_first);
498,897,870✔
909

910
        *p = next + streq(next, ".");
498,897,870✔
911
        if (ret)
498,897,870✔
912
                *ret = first;
462,299,558✔
913
        return len;
498,897,870✔
914
}
915

916
static const char* skip_slash_or_dot_backward(const char *path, const char *q) {
15,282,999✔
917
        assert(path);
15,282,999✔
918
        assert(!q || q >= path);
15,282,999✔
919

920
        for (; q; q = PTR_SUB1(q, path)) {
30,801,593✔
921
                if (*q == '/')
22,715,756✔
922
                        continue;
7,797,253✔
923
                if (q > path && strneq(q - 1, "/.", 2))
14,918,503✔
924
                        continue;
296✔
925
                if (q == path && *q == '.')
14,918,207✔
926
                        continue;
84✔
927
                break;
928
        }
929
        return q;
15,282,999✔
930
}
931

932
int path_find_last_component(const char *path, bool accept_dot_dot, const char **next, const char **ret) {
7,649,255✔
933
        const char *q, *last_end, *last_begin;
7,649,255✔
934
        size_t len;
7,649,255✔
935

936
        /* Similar to path_find_first_component(), but search components from the end.
937
        *
938
        * Examples
939
        *   Input:  path: "//.//aaa///bbbbb/cc//././"
940
        *           next: NULL
941
        *   Output: next: "/cc//././"
942
        *           ret: "cc//././"
943
        *           return value: 2 (== strlen("cc"))
944
        *
945
        *   Input:  path: "//.//aaa///bbbbb/cc//././"
946
        *           next: "/cc//././"
947
        *   Output: next: "///bbbbb/cc//././"
948
        *           ret: "bbbbb/cc//././"
949
        *           return value: 5 (== strlen("bbbbb"))
950
        *
951
        *   Input:  path: "//.//aaa///bbbbb/cc//././"
952
        *           next: "///bbbbb/cc//././"
953
        *   Output: next: "//.//aaa///bbbbb/cc//././" (next == path)
954
        *           ret: "aaa///bbbbb/cc//././"
955
        *           return value: 3 (== strlen("aaa"))
956
        *
957
        *   Input:  path: "/", ".", "", or NULL
958
        *   Output: next: equivalent to path
959
        *           ret: NULL
960
        *           return value: 0
961
        *
962
        *   Input:  path: "(too long component)"
963
        *   Output: return value: -EINVAL
964
        *
965
        *   (when accept_dot_dot is false)
966
        *   Input:  path: "//..//aaa///bbbbb/cc/..//"
967
        *   Output: return value: -EINVAL
968
        */
969

970
        if (isempty(path)) {
7,649,255✔
971
                if (next)
4✔
972
                        *next = path;
4✔
973
                if (ret)
4✔
974
                        *ret = NULL;
4✔
975
                return 0;
4✔
976
        }
977

978
        if (next && *next) {
7,649,251✔
979
                if (*next < path || *next > path + strlen(path))
34,267✔
980
                        return -EINVAL;
981
                if (*next == path) {
34,267✔
982
                        if (ret)
9✔
983
                                *ret = NULL;
9✔
984
                        return 0;
9✔
985
                }
986
                if (!IN_SET(**next, '\0', '/'))
34,258✔
987
                        return -EINVAL;
988
                q = *next - 1;
34,258✔
989
        } else
990
                q = path + strlen(path) - 1;
7,614,984✔
991

992
        q = skip_slash_or_dot_backward(path, q);
7,649,242✔
993
        if (!q) { /* the root directory, "." or "./" */
7,649,242✔
994
                if (next)
15,403✔
995
                        *next = path;
15,403✔
996
                if (ret)
15,403✔
997
                        *ret = NULL;
15,400✔
998
                return 0;
15,403✔
999
        }
1000

1001
        last_end = q + 1;
7,633,839✔
1002

1003
        while (q && *q != '/')
120,836,491✔
1004
                q = PTR_SUB1(q, path);
105,568,813✔
1005

1006
        last_begin = q ? q + 1 : path;
7,633,839✔
1007
        len = last_end - last_begin;
7,633,839✔
1008

1009
        if (len > NAME_MAX)
7,633,839✔
1010
                return -EINVAL;
1011
        if (!accept_dot_dot && len == 2 && strneq(last_begin, "..", 2))
7,633,835✔
1012
                return -EINVAL;
1013

1014
        if (next) {
7,633,802✔
1015
                q = skip_slash_or_dot_backward(path, q);
7,633,757✔
1016
                *next = q ? q + 1 : path;
7,633,757✔
1017
        }
1018

1019
        if (ret)
7,633,802✔
1020
                *ret = last_begin;
6,761,755✔
1021
        return len;
7,633,802✔
1022
}
1023

1024
const char* last_path_component(const char *path) {
132,246✔
1025

1026
        /* Finds the last component of the path, preserving the optional trailing slash that signifies a directory.
1027
         *
1028
         *    a/b/c → c
1029
         *    a/b/c/ → c/
1030
         *    x → x
1031
         *    x/ → x/
1032
         *    /y → y
1033
         *    /y/ → y/
1034
         *    / → /
1035
         *    // → /
1036
         *    /foo/a → a
1037
         *    /foo/a/ → a/
1038
         *
1039
         *    Also, the empty string is mapped to itself.
1040
         *
1041
         * This is different than basename(), which returns "" when a trailing slash is present.
1042
         *
1043
         * This always succeeds (except if you pass NULL in which case it returns NULL, too).
1044
         */
1045

1046
        unsigned l, k;
132,246✔
1047

1048
        if (!path)
132,246✔
1049
                return NULL;
1050

1051
        l = k = strlen(path);
132,245✔
1052
        if (l == 0) /* special case — an empty string */
132,245✔
1053
                return path;
1054

1055
        while (k > 0 && path[k-1] == '/')
132,257✔
1056
                k--;
1057

1058
        if (k == 0) /* the root directory */
132,244✔
1059
                return path + l - 1;
3✔
1060

1061
        while (k > 0 && path[k-1] != '/')
1,086,216✔
1062
                k--;
1063

1064
        return path + k;
132,241✔
1065
}
1066

1067
int path_split_prefix_filename(const char *path, char **ret_dir, char **ret_filename) {
6,742,874✔
1068
        _cleanup_free_ char *d = NULL;
6,742,874✔
1069
        const char *c, *next = NULL;
6,742,874✔
1070
        int r;
6,742,874✔
1071

1072
        /* Split the path into dir prefix/filename pair. Returns:
1073
         *
1074
         * -EINVAL        → if the path is not valid
1075
         * -EADDRNOTAVAIL → if the path refers to the uppermost directory in hierarchy (i.e. has neither
1076
         *                  dir prefix nor filename - the root dir itself or ".")
1077
         * -EDESTADDRREQ  → if only a filename was passed, and caller only specifies ret_dir
1078
         * -ENOMEM        → no memory
1079
         *
1080
         * Returns >= 0 on success. If the input path has a trailing slash, returns O_DIRECTORY, to
1081
         * indicate the referenced file must be a directory.
1082
         *
1083
         * This function guarantees to return a fully valid filename, i.e. one that passes
1084
         * filename_is_valid() – this means "." and ".." are not accepted. */
1085

1086
        if (isempty(path))
13,485,744✔
1087
                return -EINVAL;
1088

1089
        r = path_find_last_component(path, /* accept_dot_dot = */ false, &next, &c);
6,742,861✔
1090
        if (r < 0)
6,742,861✔
1091
                return r;
1092
        if (r == 0) /* root directory or "." */
6,742,833✔
1093
                return -EADDRNOTAVAIL;
1094

1095
        if (ret_dir) {
6,727,443✔
1096
                if (next == path) {
4,225,412✔
1097
                        if (*path != '/') { /* filename only */
52,159✔
1098
                                if (!ret_filename)
31,887✔
1099
                                        return -EDESTADDRREQ;
1100
                        } else {
1101
                                d = strdup("/");
20,272✔
1102
                                if (!d)
20,272✔
1103
                                        return -ENOMEM;
1104
                        }
1105
                } else {
1106
                        d = strndup(path, next - path);
4,173,253✔
1107
                        if (!d)
4,173,253✔
1108
                                return -ENOMEM;
1109

1110
                        path_simplify(d);
4,173,253✔
1111

1112
                        if (!path_is_valid(d))
4,173,253✔
1113
                                return -EINVAL;
1114
                }
1115

1116
        } else if (!path_is_valid(path))
2,502,031✔
1117
                /* We didn't validate the dir prefix, hence check if the whole path is valid now */
1118
                return -EINVAL;
1119

1120
        if (ret_filename) {
6,695,556✔
1121
                char *fn = strndup(c, r);
2,842,033✔
1122
                if (!fn)
2,842,033✔
1123
                        return -ENOMEM;
1124

1125
                *ret_filename = fn;
2,842,033✔
1126
        }
1127

1128
        if (ret_dir)
6,702,317✔
1129
                *ret_dir = TAKE_PTR(d);
4,200,286✔
1130

1131
        return strlen(c) > (size_t) r ? O_DIRECTORY : 0;
6,702,317✔
1132
}
1133

1134
bool filename_part_is_valid(const char *p) {
1,067,284✔
1135
        const char *e;
1,067,284✔
1136

1137
        /* Checks f the specified string is OK to be *part* of a filename. This is different from
1138
         * filename_is_valid() as "." and ".." and "" are OK by this call, but not by filename_is_valid(). */
1139

1140
        if (!p)
1,067,284✔
1141
                return false;
1142

1143
        e = strchrnul(p, '/');
1,067,284✔
1144
        if (*e != 0)
1,067,284✔
1145
                return false;
1146

1147
        if (e - p > NAME_MAX) /* NAME_MAX is counted *without* the trailing NUL byte */
1,046,179✔
1148
                return false;
3✔
1149

1150
        return true;
1151
}
1152

1153
bool filename_is_valid(const char *p) {
1,048,348✔
1154

1155
        if (isempty(p))
1,048,348✔
1156
                return false;
1157

1158
        if (dot_or_dot_dot(p)) /* Yes, in this context we consider "." and ".." invalid */
1,048,337✔
1159
                return false;
1160

1161
        return filename_part_is_valid(p);
1,048,326✔
1162
}
1163

1164
bool path_is_valid_full(const char *p, bool accept_dot_dot) {
11,674,433✔
1165
        if (isempty(p))
11,674,433✔
1166
                return false;
1167

1168
        for (const char *e = p;;) {
11,674,428✔
1169
                int r;
36,603,533✔
1170

1171
                r = path_find_first_component(&e, accept_dot_dot, NULL);
36,603,533✔
1172
                if (r < 0)
36,603,533✔
1173
                        return false;
11,674,428✔
1174

1175
                if (e - p >= PATH_MAX) /* Already reached the maximum length for a path? (PATH_MAX is counted
36,603,497✔
1176
                                        * *with* the trailing NUL byte) */
1177
                        return false;
1178
                if (*e == 0)           /* End of string? Yay! */
36,603,494✔
1179
                        return true;
1180
        }
1181
}
1182

1183
bool path_is_normalized(const char *p) {
811,492✔
1184
        if (!path_is_safe(p))
811,492✔
1185
                return false;
1186

1187
        if (streq(p, ".") || startswith(p, "./") || endswith(p, "/.") || strstr(p, "/./"))
811,467✔
1188
                return false;
1189

1190
        if (strstr(p, "//"))
811,464✔
1191
                return false;
1✔
1192

1193
        return true;
1194
}
1195

1196
int file_in_same_dir(const char *path, const char *filename, char **ret) {
860✔
1197
        _cleanup_free_ char *b = NULL;
860✔
1198
        int r;
860✔
1199

1200
        assert(path);
860✔
1201
        assert(filename);
860✔
1202
        assert(ret);
860✔
1203

1204
        /* This removes the last component of path and appends filename, unless the latter is absolute anyway
1205
         * or the former isn't */
1206

1207
        if (path_is_absolute(filename))
860✔
1208
                b = strdup(filename);
16✔
1209
        else {
1210
                _cleanup_free_ char *dn = NULL;
844✔
1211

1212
                r = path_extract_directory(path, &dn);
844✔
1213
                if (r == -EDESTADDRREQ) /* no path prefix */
844✔
1214
                        b = strdup(filename);
1✔
1215
                else if (r < 0)
843✔
1216
                        return r;
11✔
1217
                else
1218
                        b = path_join(dn, filename);
832✔
1219
        }
1220
        if (!b)
849✔
1221
                return -ENOMEM;
1222

1223
        *ret = TAKE_PTR(b);
849✔
1224
        return 0;
849✔
1225
}
1226

1227
bool hidden_or_backup_file(const char *filename) {
7,767,515✔
1228
        assert(filename);
7,767,515✔
1229

1230
        if (filename[0] == '.' ||
14,632,235✔
1231
            STR_IN_SET(filename,
6,864,720✔
1232
                       "lost+found",
1233
                       "aquota.user",
1234
                       "aquota.group") ||
6,864,717✔
1235
            endswith(filename, "~"))
6,864,717✔
1236
                return true;
902,800✔
1237

1238
        const char *dot = strrchr(filename, '.');
6,864,715✔
1239
        if (!dot)
6,864,715✔
1240
                return false;
1241

1242
        /* Please, let's not add more entries to the list below. If external projects think it's a good idea
1243
         * to come up with always new suffixes and that everybody else should just adjust to that, then it
1244
         * really should be on them. Hence, in future, let's not add any more entries. Instead, let's ask
1245
         * those packages to instead adopt one of the generic suffixes/prefixes for hidden files or backups,
1246
         * possibly augmented with an additional string. Specifically: there's now:
1247
         *
1248
         *    The generic suffixes "~" and ".bak" for backup files
1249
         *    The generic prefix "." for hidden files
1250
         *
1251
         * Thus, if a new package manager "foopkg" wants its own set of ".foopkg-new", ".foopkg-old",
1252
         * ".foopkg-dist" or so registered, let's refuse that and ask them to use ".foopkg.new",
1253
         * ".foopkg.old" or ".foopkg~" instead.
1254
         */
1255

1256
        return STR_IN_SET(dot + 1,
6,287,243✔
1257
                          "ignore",
1258
                          "rpmnew",
1259
                          "rpmsave",
1260
                          "rpmorig",
1261
                          "dpkg-old",
1262
                          "dpkg-new",
1263
                          "dpkg-tmp",
1264
                          "dpkg-dist",
1265
                          "dpkg-bak",
1266
                          "dpkg-backup",
1267
                          "dpkg-remove",
1268
                          "ucf-new",
1269
                          "ucf-old",
1270
                          "ucf-dist",
1271
                          "swp",
1272
                          "bak",
1273
                          "old",
1274
                          "new");
1275
}
1276

1277
bool is_device_path(const char *path) {
13,799✔
1278

1279
        /* Returns true for paths that likely refer to a device, either by path in sysfs or to something in
1280
         * /dev. This accepts any path that starts with /dev/ or /sys/ and has something after that prefix.
1281
         * It does not actually resolve the path.
1282
         *
1283
         * Examples:
1284
         * /dev/sda, /dev/sda/foo, /sys/class, /dev/.., /sys/.., /./dev/foo → yes.
1285
         * /../dev/sda, /dev, /sys, /usr/path, /usr/../dev/sda → no.
1286
         */
1287

1288
        const char *p = PATH_STARTSWITH_SET(ASSERT_PTR(path), "/dev/", "/sys/");
13,799✔
1289
        return !isempty(p);
13,799✔
1290
}
1291

1292
bool valid_device_node_path(const char *path) {
569✔
1293

1294
        /* Some superficial checks whether the specified path is a valid device node path, all without
1295
         * looking at the actual device node. */
1296

1297
        if (!PATH_STARTSWITH_SET(path, "/dev/", "/run/systemd/inaccessible/"))
569✔
1298
                return false;
×
1299

1300
        if (endswith(path, "/")) /* can't be a device node if it ends in a slash */
569✔
1301
                return false;
1302

1303
        return path_is_normalized(path);
569✔
1304
}
1305

1306
bool valid_device_allow_pattern(const char *path) {
18✔
1307
        assert(path);
18✔
1308

1309
        /* Like valid_device_node_path(), but also allows full-subsystem expressions like those accepted by
1310
         * DeviceAllow= and DeviceDeny=. */
1311

1312
        if (STARTSWITH_SET(path, "block-", "char-"))
18✔
1313
                return true;
6✔
1314

1315
        return valid_device_node_path(path);
12✔
1316
}
1317

1318
bool dot_or_dot_dot(const char *path) {
7,239,998✔
1319
        if (!path)
7,239,998✔
1320
                return false;
1321
        if (path[0] != '.')
7,239,997✔
1322
                return false;
1323
        if (path[1] == 0)
304,816✔
1324
                return true;
1325
        if (path[1] != '.')
152,939✔
1326
                return false;
1327

1328
        return path[2] == 0;
150,486✔
1329
}
1330

1331
bool path_implies_directory(const char *path) {
64,645✔
1332

1333
        /* Sometimes, if we look at a path we already know it must refer to a directory, because it is
1334
         * suffixed with a slash, or its last component is "." or ".." */
1335

1336
        if (!path)
64,645✔
1337
                return false;
64,645✔
1338

1339
        if (dot_or_dot_dot(path))
41,900✔
1340
                return true;
1341

1342
        return ENDSWITH_SET(path, "/", "/.", "/..");
41,898✔
1343
}
1344

1345
bool empty_or_root(const char *path) {
11,325,965✔
1346

1347
        /* For operations relative to some root directory, returns true if the specified root directory is
1348
         * redundant, i.e. either / or NULL or the empty string or any equivalent. */
1349

1350
        if (isempty(path))
11,325,965✔
1351
                return true;
1352

1353
        return path_equal(path, "/");
5,256,111✔
1354
}
1355

1356
const char* empty_to_root(const char *path) {
412,229✔
1357
        return isempty(path) ? "/" : path;
418,261✔
1358
}
1359

1360
int empty_or_root_harder_to_null(const char **path) {
2,722,783✔
1361
        int r;
2,722,783✔
1362

1363
        assert(path);
2,722,783✔
1364

1365
        /* This nullifies the input path when the path is empty or points to "/". */
1366

1367
        if (empty_or_root(*path)) {
2,722,783✔
1368
                *path = NULL;
2,666,221✔
1369
                return 0;
2,666,221✔
1370
        }
1371

1372
        r = path_is_root(*path);
56,562✔
1373
        if (r < 0)
56,562✔
1374
                return r;
1375
        if (r > 0)
56,554✔
1376
                *path = NULL;
9✔
1377

1378
        return 0;
1379
}
1380

1381
bool path_strv_contains(char * const *l, const char *path) {
78,917✔
1382
        assert(path);
78,917✔
1383

1384
        STRV_FOREACH(i, l)
1,365,957✔
1385
                if (path_equal(*i, path))
1,299,196✔
1386
                        return true;
1387

1388
        return false;
1389
}
1390

1391
bool prefixed_path_strv_contains(char * const *l, const char *path) {
2,179✔
1392
        assert(path);
2,179✔
1393

1394
        STRV_FOREACH(i, l) {
2,184✔
1395
                const char *j = *i;
6✔
1396

1397
                if (*j == '-')
6✔
1398
                        j++;
×
1399
                if (*j == '+')
6✔
1400
                        j++;
×
1401

1402
                if (path_equal(j, path))
6✔
1403
                        return true;
1404
        }
1405

1406
        return false;
1407
}
1408

1409
int path_glob_can_match(const char *pattern, const char *prefix, char **ret) {
11,756✔
1410
        assert(pattern);
11,756✔
1411
        assert(prefix);
11,756✔
1412

1413
        for (const char *a = pattern, *b = prefix;;) {
11,756✔
1414
                _cleanup_free_ char *g = NULL, *h = NULL;
26,481✔
1415
                const char *p, *q;
35,282✔
1416
                int r, s;
35,282✔
1417

1418
                r = path_find_first_component(&a, /* accept_dot_dot= */ false, &p);
35,282✔
1419
                if (r < 0)
35,282✔
1420
                        return r;
1421

1422
                s = path_find_first_component(&b, /* accept_dot_dot= */ false, &q);
35,282✔
1423
                if (s < 0)
35,282✔
1424
                        return s;
1425

1426
                if (s == 0) {
35,282✔
1427
                        /* The pattern matches the prefix. */
1428
                        if (ret) {
2,955✔
1429
                                char *t;
2,955✔
1430

1431
                                t = path_join(prefix, p);
2,955✔
1432
                                if (!t)
2,955✔
1433
                                        return -ENOMEM;
1434

1435
                                *ret = t;
2,955✔
1436
                        }
1437
                        return true;
2,955✔
1438
                }
1439

1440
                if (r == 0)
32,327✔
1441
                        break;
1442

1443
                if (r == s && strneq(p, q, r))
32,323✔
1444
                        continue; /* common component. Check next. */
20,569✔
1445

1446
                g = strndup(p, r);
11,754✔
1447
                if (!g)
11,754✔
1448
                        return -ENOMEM;
1449

1450
                if (!string_is_glob(g))
11,754✔
1451
                        break;
1452

1453
                /* We found a glob component. Check if the glob pattern matches the prefix component. */
1454

1455
                h = strndup(q, s);
2,957✔
1456
                if (!h)
2,957✔
1457
                        return -ENOMEM;
1458

1459
                r = fnmatch(g, h, 0);
2,957✔
1460
                if (r == FNM_NOMATCH)
2,957✔
1461
                        break;
1462
                if (r != 0) /* Failure to process pattern? */
2,957✔
1463
                        return -EINVAL;
1464
        }
1465

1466
        /* The pattern does not match the prefix. */
1467
        if (ret)
8,801✔
1468
                *ret = NULL;
8,801✔
1469
        return false;
1470
}
1471

1472
#if HAVE_SPLIT_BIN
1473
static bool dir_is_split(const char *a, const char *b) {
1474
        int r;
1475

1476
        r = inode_same(a, b, AT_NO_AUTOMOUNT);
1477
        if (r < 0 && r != -ENOENT) {
1478
                log_debug_errno(r, "Failed to compare \"%s\" and \"%s\", assuming split directories: %m", a, b);
1479
                return true;
1480
        }
1481
        return r == 0;
1482
}
1483
#endif
1484

1485
const char* default_PATH(void) {
3,771✔
1486
#if HAVE_SPLIT_BIN
1487
        static const char *default_path = NULL;
1488

1489
        /* Return one of the three sets of paths:
1490
         * a) split /usr/s?bin, /usr/local/sbin doesn't matter.
1491
         * b) merged /usr/s?bin, /usr/sbin is a symlink, but /usr/local/sbin is not,
1492
         * c) fully merged, neither /usr/sbin nor /usr/local/sbin are symlinks,
1493
         *
1494
         * On error the fallback to the safe value with both directories as configured is returned.
1495
         */
1496

1497
        if (default_path)
1498
                return default_path;
1499

1500
        if (dir_is_split("/usr/sbin", "/usr/bin"))
1501
                return (default_path = DEFAULT_PATH_WITH_FULL_SBIN);  /* a */
1502
        if (dir_is_split("/usr/local/sbin", "/usr/local/bin"))
1503
                return (default_path = DEFAULT_PATH_WITH_LOCAL_SBIN); /* b */
1504
        return (default_path = DEFAULT_PATH_WITHOUT_SBIN);            /* c */
1505
#else
1506
        return DEFAULT_PATH_WITHOUT_SBIN;
3,771✔
1507
#endif
1508
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc