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

systemd / systemd / 16687602495

01 Aug 2025 05:41PM UTC coverage: 72.21% (-0.04%) from 72.249%
16687602495

push

github

bluca
build(deps): bump github/codeql-action from 3.29.2 to 3.29.5

Bumps [github/codeql-action](https://github.com/github/codeql-action) from 3.29.2 to 3.29.5.
- [Release notes](https://github.com/github/codeql-action/releases)
- [Changelog](https://github.com/github/codeql-action/blob/main/CHANGELOG.md)
- [Commits](https://github.com/github/codeql-action/compare/181d5eefc...51f77329a)

---
updated-dependencies:
- dependency-name: github/codeql-action
  dependency-version: 3.29.5
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>

302783 of 419308 relevant lines covered (72.21%)

648727.74 hits per line

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

92.69
/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
#include "time-util.h"
21

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

26
        return strchr(p, '/');
122,126✔
27
}
28

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

33
        assert(p);
942✔
34
        assert(ret);
942✔
35

36
        l = strv_split(p, ":");
942✔
37
        if (!l)
942✔
38
                return -ENOMEM;
39

40
        r = path_strv_make_absolute_cwd(l);
942✔
41
        if (r < 0)
942✔
42
                return r;
43

44
        *ret = TAKE_PTR(l);
942✔
45
        return r;
942✔
46
}
47

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

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

54
        if (path_is_absolute(p) || isempty(prefix))
2,235✔
55
                return strdup(p);
278✔
56

57
        return path_join(prefix, p);
1,957✔
58
}
59

60
int safe_getcwd(char **ret) {
5,478✔
61
        _cleanup_free_ char *cwd = NULL;
5,478✔
62

63
        cwd = get_current_dir_name();
5,478✔
64
        if (!cwd)
5,478✔
65
                return negative_errno();
×
66

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

72
        if (ret)
5,478✔
73
                *ret = TAKE_PTR(cwd);
5,478✔
74

75
        return 0;
76
}
77

78
int path_make_absolute_cwd(const char *p, char **ret) {
2,880,886✔
79
        char *c;
2,880,886✔
80
        int r;
2,880,886✔
81

82
        assert(p);
2,880,886✔
83
        assert(ret);
2,880,886✔
84

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

88
        if (path_is_absolute(p))
2,880,886✔
89
                c = strdup(p);
2,880,598✔
90
        else {
91
                _cleanup_free_ char *cwd = NULL;
288✔
92

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

97
                c = path_join(cwd, p);
288✔
98
        }
99
        if (!c)
2,880,886✔
100
                return -ENOMEM;
101

102
        *ret = c;
2,880,886✔
103
        return 0;
2,880,886✔
104
}
105

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

113
        assert(from);
760,068✔
114
        assert(to);
760,068✔
115
        assert(ret);
760,068✔
116

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

119
        if (!path_is_absolute(from) || !path_is_absolute(to))
1,520,132✔
120
                return -EINVAL;
121

122
        for (;;) {
5,005,256✔
123
                r = path_find_first_component(&from, true, &f);
5,005,256✔
124
                if (r < 0)
5,005,256✔
125
                        return r;
126

127
                k = path_find_first_component(&to, true, &t);
5,005,256✔
128
                if (k < 0)
5,005,256✔
129
                        return k;
130

131
                if (r == 0) {
5,005,256✔
132
                        /* end of 'from' */
133
                        if (k == 0) {
611,291✔
134
                                /* from and to are equivalent. */
135
                                result = strdup(".");
4✔
136
                                if (!result)
4✔
137
                                        return -ENOMEM;
138
                        } else {
139
                                /* 'to' is inside of 'from'. */
140
                                r = path_simplify_alloc(t, &result);
611,287✔
141
                                if (r < 0)
611,287✔
142
                                        return r;
143

144
                                if (!path_is_valid(result))
611,287✔
145
                                        return -EINVAL;
146
                        }
147

148
                        *ret = TAKE_PTR(result);
611,291✔
149
                        return 0;
611,291✔
150
                }
151

152
                if (r != k || !strneq(f, t, r))
4,393,965✔
153
                        break;
154
        }
155

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

159
        for (n_parents = 1;; n_parents++) {
183,105✔
160
                /* If this includes ".." we can't do a simple series of "..". */
161
                r = path_find_first_component(&from, false, &f);
331,878✔
162
                if (r < 0)
331,878✔
163
                        return r;
164
                if (r == 0)
331,876✔
165
                        break;
166
        }
167

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

172
        result = new(char, n_parents * 3 + !isempty(t) + strlen_ptr(t));
446,309✔
173
        if (!result)
148,771✔
174
                return -ENOMEM;
175

176
        for (p = result; n_parents > 0; n_parents--)
480,647✔
177
                p = mempcpy(p, "../", 3);
331,876✔
178

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

186
        strcpy(p, t);
148,769✔
187

188
        path_simplify(result);
148,769✔
189

190
        if (!path_is_valid(result))
148,769✔
191
                return -EINVAL;
192

193
        *ret = TAKE_PTR(result);
148,769✔
194
        return 0;
148,769✔
195
}
196

197
int path_make_relative_parent(const char *from_child, const char *to, char **ret) {
148,844✔
198
        _cleanup_free_ char *from = NULL;
148,844✔
199
        int r;
148,844✔
200

201
        assert(from_child);
148,844✔
202
        assert(to);
148,844✔
203
        assert(ret);
148,844✔
204

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

218
        r = path_extract_directory(from_child, &from);
148,844✔
219
        if (r < 0)
148,844✔
220
                return r;
221

222
        return path_make_relative(from, to, ret);
148,843✔
223
}
224

225
char* path_startswith_strv(const char *p, char * const *strv) {
152,347✔
226
        assert(p);
152,347✔
227

228
        STRV_FOREACH(s, strv) {
565,387✔
229
                char *t;
436,753✔
230

231
                t = path_startswith(p, *s);
436,753✔
232
                if (t)
436,753✔
233
                        return t;
234
        }
235

236
        return NULL;
237
}
238

239
int path_strv_make_absolute_cwd(char **l) {
95,418✔
240
        int r;
95,418✔
241

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

246
        STRV_FOREACH(s, l) {
678,000✔
247
                char *t;
582,582✔
248

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

253
                path_simplify(t);
582,582✔
254
                free_and_replace(*s, t);
582,582✔
255
        }
256

257
        return 0;
258
}
259

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

265
        if (strv_isempty(l))
47,574✔
266
                return l;
267

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

272
        STRV_FOREACH(s, l) {
142,109✔
273
                _cleanup_free_ char *orig = NULL;
118,322✔
274
                char *t, *u;
118,322✔
275

276
                if (!path_is_absolute(*s)) {
118,322✔
277
                        free(*s);
×
278
                        continue;
×
279
                }
280

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

291
                r = chase(t, root, 0, &u, NULL);
118,322✔
292
                if (r == -ENOENT) {
118,322✔
293
                        if (root) {
82,410✔
294
                                u = TAKE_PTR(orig);
2✔
295
                                free(t);
2✔
296
                        } else
297
                                u = t;
82,408✔
298
                } else if (r < 0) {
35,912✔
299
                        free(t);
×
300

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

304
                        continue;
×
305
                } else if (root) {
35,912✔
306
                        char *x;
5✔
307

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

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

330
                l[k++] = u;
118,322✔
331
        }
332

333
        l[k] = NULL;
23,787✔
334

335
        if (enomem)
23,787✔
336
                return NULL;
×
337

338
        return l;
339
}
340

341
char** path_strv_resolve_uniq(char **l, const char *root) {
23,786✔
342

343
        if (strv_isempty(l))
23,786✔
344
                return l;
345

346
        if (!path_strv_resolve(l, root))
23,786✔
347
                return NULL;
348

349
        return strv_uniq(l);
23,786✔
350
}
351

352
char* skip_leading_slash(const char *p) {
100,875✔
353
        return skip_leading_chars(p, "/");
100,875✔
354
}
355

356
char* path_simplify_full(char *path, PathSimplifyFlags flags) {
8,294,999✔
357
        bool add_slash = false, keep_trailing_slash, absolute, beginning = true;
8,294,999✔
358
        char *f = path;
8,294,999✔
359
        int r;
8,294,999✔
360

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

370
        if (isempty(path))
8,294,999✔
371
                return path;
372

373
        keep_trailing_slash = FLAGS_SET(flags, PATH_SIMPLIFY_KEEP_TRAILING_SLASH) && endswith(path, "/");
8,269,685✔
374

375
        absolute = path_is_absolute(path);
8,269,685✔
376
        f += absolute;  /* Keep leading /, if present. */
8,269,685✔
377

378
        for (const char *p = f;;) {
8,269,685✔
379
                const char *e;
34,534,797✔
380

381
                r = path_find_first_component(&p, true, &e);
34,534,797✔
382
                if (r == 0)
34,534,797✔
383
                        break;
384

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

389
                beginning = false;
26,265,037✔
390

391
                if (add_slash)
26,265,037✔
392
                        *f++ = '/';
18,003,562✔
393

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

400
                memmove(f, e, r);
26,265,033✔
401
                f += r;
26,265,033✔
402

403
                add_slash = true;
26,265,033✔
404
        }
405

406
        /* Special rule, if we stripped everything, we need a "." for the current directory. */
407
        if (f == path)
8,269,681✔
408
                *f++ = '.';
7✔
409

410
        if (*(f-1) != '/' && keep_trailing_slash)
8,269,681✔
411
                *f++ = '/';
12✔
412

413
        *f = '\0';
8,269,681✔
414
        return path;
8,269,681✔
415
}
416

417
int path_simplify_alloc(const char *path, char **ret) {
1,539,417✔
418
        assert(ret);
1,539,417✔
419

420
        if (!path) {
1,539,417✔
421
                *ret = NULL;
13,470✔
422
                return 0;
13,470✔
423
        }
424

425
        char *t = strdup(path);
1,525,947✔
426
        if (!t)
1,525,947✔
427
                return -ENOMEM;
428

429
        *ret = path_simplify(t);
1,525,947✔
430
        return 0;
1,525,947✔
431
}
432

433
char* path_startswith_full(const char *original_path, const char *prefix, PathStartWithFlags flags) {
48,972,481✔
434
        assert(original_path);
48,972,481✔
435
        assert(prefix);
48,972,481✔
436

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

447
        const char *path = original_path;
48,972,481✔
448

449
        if ((path[0] == '/') != (prefix[0] == '/'))
48,972,481✔
450
                return NULL;
48,972,481✔
451

452
        for (;;) {
42,869,271✔
453
                const char *p, *q;
91,673,966✔
454
                int m, n;
91,673,966✔
455

456
                m = path_find_first_component(&path, !FLAGS_SET(flags, PATH_STARTSWITH_REFUSE_DOT_DOT), &p);
91,673,966✔
457
                if (m < 0)
91,673,966✔
458
                        return NULL;
48,804,695✔
459

460
                n = path_find_first_component(&prefix, !FLAGS_SET(flags, PATH_STARTSWITH_REFUSE_DOT_DOT), &q);
91,673,966✔
461
                if (n < 0)
91,673,966✔
462
                        return NULL;
463

464
                if (n == 0) {
91,673,966✔
465
                        if (!p)
35,322,326✔
466
                                p = path;
183,328✔
467

468
                        if (FLAGS_SET(flags, PATH_STARTSWITH_RETURN_LEADING_SLASH)) {
35,322,326✔
469

470
                                if (p <= original_path)
7✔
471
                                        return NULL;
472

473
                                p--;
7✔
474

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

479
                        return (char*) p;
35,322,325✔
480
                }
481

482
                if (m != n)
56,351,640✔
483
                        return NULL;
484

485
                if (!strneq(p, q, m))
45,437,459✔
486
                        return NULL;
487
        }
488
}
489

490
int path_compare(const char *a, const char *b) {
20,682,899✔
491
        int r;
20,682,899✔
492

493
        /* Order NULL before non-NULL */
494
        r = CMP(!!a, !!b);
20,682,899✔
495
        if (r != 0)
20,659,077✔
496
                return r;
23,825✔
497

498
        /* A relative path and an absolute path must not compare as equal.
499
         * Which one is sorted before the other does not really matter.
500
         * Here a relative path is ordered before an absolute path. */
501
        r = CMP(path_is_absolute(a), path_is_absolute(b));
41,318,146✔
502
        if (r != 0)
20,414,883✔
503
                return r;
285,112✔
504

505
        for (;;) {
17,699,368✔
506
                const char *aa, *bb;
38,073,330✔
507
                int j, k;
38,073,330✔
508

509
                j = path_find_first_component(&a, true, &aa);
38,073,330✔
510
                k = path_find_first_component(&b, true, &bb);
38,073,330✔
511

512
                if (j < 0 || k < 0) {
38,073,330✔
513
                        /* When one of paths is invalid, order invalid path after valid one. */
514
                        r = CMP(j < 0, k < 0);
×
515
                        if (r != 0)
×
516
                                return r;
20,373,962✔
517

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

522
                /* Order prefixes first: "/foo" before "/foo/bar" */
523
                if (j == 0) {
38,073,330✔
524
                        if (k == 0)
3,311,895✔
525
                                return 0;
526
                        return -1;
125,841✔
527
                }
528
                if (k == 0)
34,761,435✔
529
                        return 1;
530

531
                /* Alphabetical sort: "/foo/aaa" before "/foo/b" */
532
                r = memcmp(aa, bb, MIN(j, k));
32,717,434✔
533
                if (r != 0)
32,717,434✔
534
                        return r;
535

536
                /* Sort "/foo/a" before "/foo/aaa" */
537
                r = CMP(j, k);
17,769,262✔
538
                if (r != 0)
17,726,235✔
539
                        return r;
69,894✔
540
        }
541
}
542

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

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

552
        j = path_extract_filename(a, &fa);
104,746✔
553
        k = path_extract_filename(b, &fb);
104,746✔
554

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

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

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

569
        return strcmp(fa, fb);
104,708✔
570
}
571

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

575
        if (path_equal(a, b))
51,417✔
576
                return 1;
577

578
        if (!a || !b)
285✔
579
                return 0;
580

581
        return inode_same(a, b, flags);
285✔
582
}
583

584
char* path_extend_internal(char **x, ...) {
20,854,476✔
585
        size_t sz, old_sz;
20,854,476✔
586
        char *q, *nx;
20,854,476✔
587
        const char *p;
20,854,476✔
588
        va_list ap;
20,854,476✔
589
        bool slash;
20,854,476✔
590

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

609
        sz = old_sz = x ? strlen_ptr(*x) : 0;
20,854,476✔
610
        va_start(ap, x);
20,854,476✔
611
        while ((p = va_arg(ap, char*)) != POINTER_MAX) {
47,620,883✔
612
                size_t add;
26,766,407✔
613

614
                if (isempty(p))
26,766,407✔
615
                        continue;
453,138✔
616

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

623
                sz += add;
26,313,269✔
624
        }
625
        va_end(ap);
20,854,476✔
626

627
        nx = realloc(x ? *x : NULL, GREEDY_ALLOC_ROUND_UP(sz+1));
20,854,476✔
628
        if (!nx)
20,854,476✔
629
                return NULL;
630
        if (x)
20,854,476✔
631
                *x = nx;
15,289,341✔
632

633
        if (old_sz > 0)
20,854,476✔
634
                slash = nx[old_sz-1] == '/';
15,219,670✔
635
        else {
636
                nx[old_sz] = 0;
5,634,806✔
637
                slash = true; /* no need to generate a slash anymore */
5,634,806✔
638
        }
639

640
        q = nx + old_sz;
20,854,476✔
641

642
        va_start(ap, x);
20,854,476✔
643
        while ((p = va_arg(ap, char*)) != POINTER_MAX) {
47,620,883✔
644
                if (isempty(p))
26,766,407✔
645
                        continue;
453,138✔
646

647
                if (!slash && p[0] != '/')
26,313,269✔
648
                        *(q++) = '/';
15,835,976✔
649

650
                q = stpcpy(q, p);
26,313,269✔
651
                slash = endswith(p, "/");
26,313,269✔
652
        }
653
        va_end(ap);
20,854,476✔
654

655
        return nx;
20,854,476✔
656
}
657

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

663
        assert(name);
28,695✔
664

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

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

684
        r = fd_verify_regular(fd);
24,113✔
685
        if (r < 0)
24,113✔
686
                return r;
687

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

695
        if (ret_path) {
24,109✔
696
                if (resolved)
23,753✔
697
                        *ret_path = TAKE_PTR(resolved);
×
698
                else {
699
                        r = path_make_absolute_cwd(name, ret_path);
23,753✔
700
                        if (r < 0)
23,753✔
701
                                return r;
702

703
                        path_simplify(*ret_path);
23,753✔
704
                }
705
        }
706

707
        if (ret_fd)
24,109✔
708
                *ret_fd = TAKE_FD(fd);
22,355✔
709

710
        return 0;
711
}
712

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

721
        int last_error = -ENOENT, r = 0;
24,119✔
722

723
        assert(name);
24,119✔
724

725
        if (is_path(name))
24,119✔
726
                return open_and_check_executable(name, root, ret_filename, ret_fd);
24,119✔
727

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

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

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

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

750
        const char *p = NULL;
3,514✔
751

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

759
        /* Resolve a single-component name to a full path */
760
        for (;;) {
8,091✔
761
                _cleanup_free_ char *element = NULL;
8,087✔
762

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

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

774
                if (!path_extend(&element, name))
8,087✔
775
                        return -ENOMEM;
776

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

785
        return last_error;
4✔
786
}
787

788
bool paths_check_timestamp(const char* const* paths, usec_t *timestamp, bool update) {
×
789
        bool changed = false, originally_unset;
×
790

791
        assert(timestamp);
×
792

793
        if (!paths)
×
794
                return false;
795

796
        originally_unset = *timestamp == 0;
×
797

798
        STRV_FOREACH(i, paths) {
×
799
                struct stat stats;
×
800
                usec_t u;
×
801

802
                if (stat(*i, &stats) < 0)
×
803
                        continue;
×
804

805
                u = timespec_load(&stats.st_mtim);
×
806

807
                /* check first */
808
                if (*timestamp >= u)
×
809
                        continue;
×
810

811
                log_debug(originally_unset ? "Loaded timestamp for '%s'." : "Timestamp of '%s' changed.", *i);
×
812

813
                /* update timestamp */
814
                if (update) {
×
815
                        *timestamp = u;
×
816
                        changed = true;
×
817
                } else
818
                        return true;
×
819
        }
820

821
        return changed;
822
}
823

824
static int executable_is_good(const char *executable) {
176✔
825
        _cleanup_free_ char *p = NULL, *d = NULL;
176✔
826
        int r;
176✔
827

828
        r = find_executable(executable, &p);
176✔
829
        if (r == -ENOENT)
176✔
830
                return 0;
831
        if (r < 0)
174✔
832
                return r;
833

834
        /* An fsck that is linked to /bin/true is a non-existent fsck */
835

836
        r = readlink_malloc(p, &d);
174✔
837
        if (r == -EINVAL) /* not a symlink */
174✔
838
                return 1;
839
        if (r < 0)
66✔
840
                return r;
841

842
        return !PATH_IN_SET(d, "true"
66✔
843
                               "/bin/true",
844
                               "/usr/bin/true",
845
                               "/dev/null");
846
}
847

848
int fsck_exists(void) {
95✔
849
        return executable_is_good("fsck");
95✔
850
}
851

852
int fsck_exists_for_fstype(const char *fstype) {
81✔
853
        const char *checker;
81✔
854
        int r;
81✔
855

856
        assert(fstype);
81✔
857

858
        if (streq(fstype, "auto"))
81✔
859
                return -EINVAL;
860

861
        r = fsck_exists();
81✔
862
        if (r <= 0)
81✔
863
                return r;
864

865
        checker = strjoina("fsck.", fstype);
405✔
866
        return executable_is_good(checker);
81✔
867
}
868

869
static const char* skip_slash_or_dot(const char *p) {
883,161,751✔
870
        for (; !isempty(p); p++) {
1,361,069,292✔
871
                if (*p == '/')
1,179,230,796✔
872
                        continue;
477,906,735✔
873
                if (startswith(p, "./")) {
701,324,061✔
874
                        p++;
806✔
875
                        continue;
806✔
876
                }
877
                break;
878
        }
879
        return p;
883,161,751✔
880
}
881

882
int path_find_first_component(const char **p, bool accept_dot_dot, const char **ret) {
480,583,257✔
883
        const char *q, *first, *end_first, *next;
480,583,257✔
884
        size_t len;
480,583,257✔
885

886
        assert(p);
480,583,257✔
887

888
        /* When a path is input, then returns the pointer to the first component and its length, and
889
         * move the input pointer to the next component or nul. This skips both over any '/'
890
         * immediately *before* and *after* the first component before returning.
891
         *
892
         * Examples
893
         *   Input:  p: "//.//aaa///bbbbb/cc"
894
         *   Output: p: "bbbbb///cc"
895
         *           ret: "aaa///bbbbb/cc"
896
         *           return value: 3 (== strlen("aaa"))
897
         *
898
         *   Input:  p: "aaa//"
899
         *   Output: p: (pointer to NUL)
900
         *           ret: "aaa//"
901
         *           return value: 3 (== strlen("aaa"))
902
         *
903
         *   Input:  p: "/", ".", ""
904
         *   Output: p: (pointer to NUL)
905
         *           ret: NULL
906
         *           return value: 0
907
         *
908
         *   Input:  p: NULL
909
         *   Output: p: NULL
910
         *           ret: NULL
911
         *           return value: 0
912
         *
913
         *   Input:  p: "(too long component)"
914
         *   Output: return value: -EINVAL
915
         *
916
         *   (when accept_dot_dot is false)
917
         *   Input:  p: "//..//aaa///bbbbb/cc"
918
         *   Output: return value: -EINVAL
919
         */
920

921
        q = *p;
480,583,257✔
922

923
        first = skip_slash_or_dot(q);
480,583,257✔
924
        if (isempty(first)) {
480,583,257✔
925
                *p = first;
77,998,785✔
926
                if (ret)
77,998,785✔
927
                        *ret = NULL;
77,993,239✔
928
                return 0;
77,998,785✔
929
        }
930
        if (streq(first, ".")) {
402,584,472✔
931
                *p = first + 1;
5,791✔
932
                if (ret)
5,791✔
933
                        *ret = NULL;
5,733✔
934
                return 0;
5,791✔
935
        }
936

937
        end_first = strchrnul(first, '/');
402,578,681✔
938
        len = end_first - first;
402,578,681✔
939

940
        if (len > NAME_MAX)
402,578,681✔
941
                return -EINVAL;
942
        if (!accept_dot_dot && len == 2 && first[0] == '.' && first[1] == '.')
402,578,531✔
943
                return -EINVAL;
944

945
        next = skip_slash_or_dot(end_first);
402,578,494✔
946

947
        *p = next + streq(next, ".");
402,578,494✔
948
        if (ret)
402,578,494✔
949
                *ret = first;
369,201,389✔
950
        return len;
402,578,494✔
951
}
952

953
static const char* skip_slash_or_dot_backward(const char *path, const char *q) {
14,176,364✔
954
        assert(path);
14,176,364✔
955
        assert(!q || q >= path);
14,176,364✔
956

957
        for (; q; q = PTR_SUB1(q, path)) {
28,878,307✔
958
                if (*q == '/')
21,328,496✔
959
                        continue;
7,380,467✔
960
                if (q > path && strneq(q - 1, "/.", 2))
13,948,029✔
961
                        continue;
268✔
962
                if (q == path && *q == '.')
13,947,761✔
963
                        continue;
81✔
964
                break;
965
        }
966
        return q;
14,176,364✔
967
}
968

969
int path_find_last_component(const char *path, bool accept_dot_dot, const char **next, const char **ret) {
7,095,697✔
970
        const char *q, *last_end, *last_begin;
7,095,697✔
971
        size_t len;
7,095,697✔
972

973
        /* Similar to path_find_first_component(), but search components from the end.
974
        *
975
        * Examples
976
        *   Input:  path: "//.//aaa///bbbbb/cc//././"
977
        *           next: NULL
978
        *   Output: next: "/cc//././"
979
        *           ret: "cc//././"
980
        *           return value: 2 (== strlen("cc"))
981
        *
982
        *   Input:  path: "//.//aaa///bbbbb/cc//././"
983
        *           next: "/cc//././"
984
        *   Output: next: "///bbbbb/cc//././"
985
        *           ret: "bbbbb/cc//././"
986
        *           return value: 5 (== strlen("bbbbb"))
987
        *
988
        *   Input:  path: "//.//aaa///bbbbb/cc//././"
989
        *           next: "///bbbbb/cc//././"
990
        *   Output: next: "//.//aaa///bbbbb/cc//././" (next == path)
991
        *           ret: "aaa///bbbbb/cc//././"
992
        *           return value: 3 (== strlen("aaa"))
993
        *
994
        *   Input:  path: "/", ".", "", or NULL
995
        *   Output: next: equivalent to path
996
        *           ret: NULL
997
        *           return value: 0
998
        *
999
        *   Input:  path: "(too long component)"
1000
        *   Output: return value: -EINVAL
1001
        *
1002
        *   (when accept_dot_dot is false)
1003
        *   Input:  path: "//..//aaa///bbbbb/cc/..//"
1004
        *   Output: return value: -EINVAL
1005
        */
1006

1007
        if (isempty(path)) {
7,095,697✔
1008
                if (next)
9✔
1009
                        *next = path;
9✔
1010
                if (ret)
9✔
1011
                        *ret = NULL;
9✔
1012
                return 0;
9✔
1013
        }
1014

1015
        if (next && *next) {
7,095,688✔
1016
                if (*next < path || *next > path + strlen(path))
34,466✔
1017
                        return -EINVAL;
1018
                if (*next == path) {
34,466✔
1019
                        if (ret)
9✔
1020
                                *ret = NULL;
9✔
1021
                        return 0;
9✔
1022
                }
1023
                if (!IN_SET(**next, '\0', '/'))
34,457✔
1024
                        return -EINVAL;
1025
                q = *next - 1;
34,457✔
1026
        } else
1027
                q = path + strlen(path) - 1;
7,061,222✔
1028

1029
        q = skip_slash_or_dot_backward(path, q);
7,095,679✔
1030
        if (!q || /* the root directory */
7,095,679✔
1031
            (q == path && *q == '.')) { /* path is "." or "./" */
36✔
1032
                if (next)
14,923✔
1033
                        *next = path;
14,923✔
1034
                if (ret)
14,923✔
1035
                        *ret = NULL;
14,920✔
1036
                return 0;
14,923✔
1037
        }
1038

1039
        last_end = q + 1;
7,080,756✔
1040

1041
        while (q && *q != '/')
119,141,438✔
1042
                q = PTR_SUB1(q, path);
104,979,926✔
1043

1044
        last_begin = q ? q + 1 : path;
7,080,756✔
1045
        len = last_end - last_begin;
7,080,756✔
1046

1047
        if (len > NAME_MAX)
7,080,756✔
1048
                return -EINVAL;
1049
        if (!accept_dot_dot && len == 2 && strneq(last_begin, "..", 2))
7,080,752✔
1050
                return -EINVAL;
1051

1052
        if (next) {
7,080,727✔
1053
                q = skip_slash_or_dot_backward(path, q);
7,080,685✔
1054
                *next = q ? q + 1 : path;
7,080,685✔
1055
        }
1056

1057
        if (ret)
7,080,727✔
1058
                *ret = last_begin;
6,177,458✔
1059
        return len;
7,080,727✔
1060
}
1061

1062
const char* last_path_component(const char *path) {
112,053✔
1063

1064
        /* Finds the last component of the path, preserving the optional trailing slash that signifies a directory.
1065
         *
1066
         *    a/b/c → c
1067
         *    a/b/c/ → c/
1068
         *    x → x
1069
         *    x/ → x/
1070
         *    /y → y
1071
         *    /y/ → y/
1072
         *    / → /
1073
         *    // → /
1074
         *    /foo/a → a
1075
         *    /foo/a/ → a/
1076
         *
1077
         *    Also, the empty string is mapped to itself.
1078
         *
1079
         * This is different than basename(), which returns "" when a trailing slash is present.
1080
         *
1081
         * This always succeeds (except if you pass NULL in which case it returns NULL, too).
1082
         */
1083

1084
        unsigned l, k;
112,053✔
1085

1086
        if (!path)
112,053✔
1087
                return NULL;
1088

1089
        l = k = strlen(path);
112,052✔
1090
        if (l == 0) /* special case — an empty string */
112,052✔
1091
                return path;
1092

1093
        while (k > 0 && path[k-1] == '/')
112,064✔
1094
                k--;
1095

1096
        if (k == 0) /* the root directory */
112,051✔
1097
                return path + l - 1;
3✔
1098

1099
        while (k > 0 && path[k-1] != '/')
922,479✔
1100
                k--;
1101

1102
        return path + k;
112,048✔
1103
}
1104

1105
int path_extract_filename(const char *path, char **ret) {
2,279,142✔
1106
        _cleanup_free_ char *a = NULL;
4,558,284✔
1107
        const char *c, *next = NULL;
2,279,142✔
1108
        int r;
2,279,142✔
1109

1110
        /* Extracts the filename part (i.e. right-most component) from a path, i.e. string that passes
1111
         * filename_is_valid(). A wrapper around last_path_component(), but eats up trailing
1112
         * slashes. Returns:
1113
         *
1114
         * -EINVAL        → if the path is not valid
1115
         * -EADDRNOTAVAIL → if only a directory was specified, but no filename, i.e. the root dir
1116
         *                  itself or "." is specified
1117
         * -ENOMEM        → no memory
1118
         *
1119
         * Returns >= 0 on success. If the input path has a trailing slash, returns O_DIRECTORY, to
1120
         * indicate the referenced file must be a directory.
1121
         *
1122
         * This function guarantees to return a fully valid filename, i.e. one that passes
1123
         * filename_is_valid() – this means "." and ".." are not accepted. */
1124

1125
        if (!path_is_valid(path))
2,279,142✔
1126
                return -EINVAL;
1127

1128
        r = path_find_last_component(path, false, &next, &c);
2,279,139✔
1129
        if (r < 0)
2,279,139✔
1130
                return r;
1131
        if (r == 0) /* root directory */
2,279,125✔
1132
                return -EADDRNOTAVAIL;
1133

1134
        a = strndup(c, r);
2,279,039✔
1135
        if (!a)
2,279,039✔
1136
                return -ENOMEM;
1137

1138
        *ret = TAKE_PTR(a);
2,279,039✔
1139
        return strlen(c) > (size_t) r ? O_DIRECTORY : 0;
2,279,039✔
1140
}
1141

1142
int path_extract_directory(const char *path, char **ret) {
3,878,746✔
1143
        const char *c, *next = NULL;
3,878,746✔
1144
        int r;
3,878,746✔
1145

1146
        /* The inverse of path_extract_filename(), i.e. returns the directory path prefix. Returns:
1147
         *
1148
         * -EINVAL        → if the path is not valid
1149
         * -EDESTADDRREQ  → if no directory was specified in the passed in path, i.e. only a filename was passed
1150
         * -EADDRNOTAVAIL → if the passed in parameter had no filename but did have a directory, i.e.
1151
         *                   the root dir itself or "." was specified
1152
         * -ENOMEM        → no memory (surprise!)
1153
         *
1154
         * This function guarantees to return a fully valid path, i.e. one that passes path_is_valid().
1155
         */
1156

1157
        r = path_find_last_component(path, false, &next, &c);
3,878,746✔
1158
        if (r < 0)
3,878,746✔
1159
                return r;
3,878,746✔
1160
        if (r == 0) /* empty or root */
3,878,740✔
1161
                return isempty(path) ? -EINVAL : -EADDRNOTAVAIL;
29,657✔
1162
        if (next == path) {
3,863,911✔
1163
                if (*path != '/') /* filename only */
27,114✔
1164
                        return -EDESTADDRREQ;
1165

1166
                return strdup_to(ret, "/");
19,466✔
1167
        }
1168

1169
        _cleanup_free_ char *a = strndup(path, next - path);
3,836,797✔
1170
        if (!a)
3,836,797✔
1171
                return -ENOMEM;
1172

1173
        path_simplify(a);
3,836,797✔
1174

1175
        if (!path_is_valid(a))
3,836,797✔
1176
                return -EINVAL;
1177

1178
        if (ret)
3,836,797✔
1179
                *ret = TAKE_PTR(a);
3,836,797✔
1180

1181
        return 0;
1182
}
1183

1184
bool filename_part_is_valid(const char *p) {
913,939✔
1185
        const char *e;
913,939✔
1186

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

1190
        if (!p)
913,939✔
1191
                return false;
1192

1193
        e = strchrnul(p, '/');
913,939✔
1194
        if (*e != 0)
913,939✔
1195
                return false;
1196

1197
        if (e - p > NAME_MAX) /* NAME_MAX is counted *without* the trailing NUL byte */
899,953✔
1198
                return false;
3✔
1199

1200
        return true;
1201
}
1202

1203
bool filename_is_valid(const char *p) {
909,818✔
1204

1205
        if (isempty(p))
909,818✔
1206
                return false;
1207

1208
        if (dot_or_dot_dot(p)) /* Yes, in this context we consider "." and ".." invalid */
909,806✔
1209
                return false;
1210

1211
        return filename_part_is_valid(p);
909,793✔
1212
}
1213

1214
bool path_is_valid_full(const char *p, bool accept_dot_dot) {
10,508,635✔
1215
        if (isempty(p))
10,508,635✔
1216
                return false;
1217

1218
        for (const char *e = p;;) {
10,508,627✔
1219
                int r;
33,382,745✔
1220

1221
                r = path_find_first_component(&e, accept_dot_dot, NULL);
33,382,745✔
1222
                if (r < 0)
33,382,745✔
1223
                        return false;
10,508,627✔
1224

1225
                if (e - p >= PATH_MAX) /* Already reached the maximum length for a path? (PATH_MAX is counted
33,382,709✔
1226
                                        * *with* the trailing NUL byte) */
1227
                        return false;
1228
                if (*e == 0)           /* End of string? Yay! */
33,382,706✔
1229
                        return true;
1230
        }
1231
}
1232

1233
bool path_is_normalized(const char *p) {
698,383✔
1234
        if (!path_is_safe(p))
698,383✔
1235
                return false;
1236

1237
        if (streq(p, ".") || startswith(p, "./") || endswith(p, "/.") || strstr(p, "/./"))
698,358✔
1238
                return false;
1239

1240
        if (strstr(p, "//"))
698,355✔
1241
                return false;
1✔
1242

1243
        return true;
1244
}
1245

1246
int file_in_same_dir(const char *path, const char *filename, char **ret) {
268✔
1247
        _cleanup_free_ char *b = NULL;
268✔
1248
        int r;
268✔
1249

1250
        assert(path);
268✔
1251
        assert(filename);
268✔
1252
        assert(ret);
268✔
1253

1254
        /* This removes the last component of path and appends filename, unless the latter is absolute anyway
1255
         * or the former isn't */
1256

1257
        if (path_is_absolute(filename))
268✔
1258
                b = strdup(filename);
17✔
1259
        else {
1260
                _cleanup_free_ char *dn = NULL;
251✔
1261

1262
                r = path_extract_directory(path, &dn);
251✔
1263
                if (r == -EDESTADDRREQ) /* no path prefix */
251✔
1264
                        b = strdup(filename);
1✔
1265
                else if (r < 0)
250✔
1266
                        return r;
11✔
1267
                else
1268
                        b = path_join(dn, filename);
239✔
1269
        }
1270
        if (!b)
257✔
1271
                return -ENOMEM;
1272

1273
        *ret = TAKE_PTR(b);
257✔
1274
        return 0;
257✔
1275
}
1276

1277
bool hidden_or_backup_file(const char *filename) {
6,974,894✔
1278
        assert(filename);
6,974,894✔
1279

1280
        if (filename[0] == '.' ||
13,040,761✔
1281
            STR_IN_SET(filename,
6,065,867✔
1282
                       "lost+found",
1283
                       "aquota.user",
1284
                       "aquota.group") ||
6,065,864✔
1285
            endswith(filename, "~"))
6,065,864✔
1286
                return true;
909,032✔
1287

1288
        const char *dot = strrchr(filename, '.');
6,065,862✔
1289
        if (!dot)
6,065,862✔
1290
                return false;
1291

1292
        /* Please, let's not add more entries to the list below. If external projects think it's a good idea
1293
         * to come up with always new suffixes and that everybody else should just adjust to that, then it
1294
         * really should be on them. Hence, in future, let's not add any more entries. Instead, let's ask
1295
         * those packages to instead adopt one of the generic suffixes/prefixes for hidden files or backups,
1296
         * possibly augmented with an additional string. Specifically: there's now:
1297
         *
1298
         *    The generic suffixes "~" and ".bak" for backup files
1299
         *    The generic prefix "." for hidden files
1300
         *
1301
         * Thus, if a new package manager "foopkg" wants its own set of ".foopkg-new", ".foopkg-old",
1302
         * ".foopkg-dist" or so registered, let's refuse that and ask them to use ".foopkg.new",
1303
         * ".foopkg.old" or ".foopkg~" instead.
1304
         */
1305

1306
        return STR_IN_SET(dot + 1,
4,987,631✔
1307
                          "rpmnew",
1308
                          "rpmsave",
1309
                          "rpmorig",
1310
                          "dpkg-old",
1311
                          "dpkg-new",
1312
                          "dpkg-tmp",
1313
                          "dpkg-dist",
1314
                          "dpkg-bak",
1315
                          "dpkg-backup",
1316
                          "dpkg-remove",
1317
                          "ucf-new",
1318
                          "ucf-old",
1319
                          "ucf-dist",
1320
                          "swp",
1321
                          "bak",
1322
                          "old",
1323
                          "new");
1324
}
1325

1326
bool is_device_path(const char *path) {
9,475✔
1327

1328
        /* Returns true for paths that likely refer to a device, either by path in sysfs or to something in
1329
         * /dev. This accepts any path that starts with /dev/ or /sys/ and has something after that prefix.
1330
         * It does not actually resolve the path.
1331
         *
1332
         * Examples:
1333
         * /dev/sda, /dev/sda/foo, /sys/class, /dev/.., /sys/.., /./dev/foo → yes.
1334
         * /../dev/sda, /dev, /sys, /usr/path, /usr/../dev/sda → no.
1335
         */
1336

1337
        const char *p = PATH_STARTSWITH_SET(ASSERT_PTR(path), "/dev/", "/sys/");
9,475✔
1338
        return !isempty(p);
9,475✔
1339
}
1340

1341
bool valid_device_node_path(const char *path) {
474✔
1342

1343
        /* Some superficial checks whether the specified path is a valid device node path, all without
1344
         * looking at the actual device node. */
1345

1346
        if (!PATH_STARTSWITH_SET(path, "/dev/", "/run/systemd/inaccessible/"))
474✔
1347
                return false;
×
1348

1349
        if (endswith(path, "/")) /* can't be a device node if it ends in a slash */
474✔
1350
                return false;
1351

1352
        return path_is_normalized(path);
474✔
1353
}
1354

1355
bool valid_device_allow_pattern(const char *path) {
12✔
1356
        assert(path);
12✔
1357

1358
        /* Like valid_device_node_path(), but also allows full-subsystem expressions like those accepted by
1359
         * DeviceAllow= and DeviceDeny=. */
1360

1361
        if (STARTSWITH_SET(path, "block-", "char-"))
12✔
1362
                return true;
4✔
1363

1364
        return valid_device_node_path(path);
8✔
1365
}
1366

1367
bool dot_or_dot_dot(const char *path) {
6,244,033✔
1368
        if (!path)
6,244,033✔
1369
                return false;
1370
        if (path[0] != '.')
6,244,032✔
1371
                return false;
1372
        if (path[1] == 0)
261,855✔
1373
                return true;
1374
        if (path[1] != '.')
131,514✔
1375
                return false;
1376

1377
        return path[2] == 0;
129,347✔
1378
}
1379

1380
bool path_implies_directory(const char *path) {
28,364✔
1381

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

1385
        if (!path)
28,364✔
1386
                return false;
28,364✔
1387

1388
        if (dot_or_dot_dot(path))
21,643✔
1389
                return true;
1390

1391
        return ENDSWITH_SET(path, "/", "/.", "/..");
21,641✔
1392
}
1393

1394
bool empty_or_root(const char *path) {
7,898,170✔
1395

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

1399
        if (isempty(path))
7,898,170✔
1400
                return true;
1401

1402
        return path_equal(path, "/");
2,896,428✔
1403
}
1404

1405
const char* empty_to_root(const char *path) {
244,452✔
1406
        return isempty(path) ? "/" : path;
249,251✔
1407
}
1408

1409
int empty_or_root_harder_to_null(const char **path) {
2,283,634✔
1410
        int r;
2,283,634✔
1411

1412
        assert(path);
2,283,634✔
1413

1414
        /* This nullifies the input path when the path is empty or points to "/". */
1415

1416
        if (empty_or_root(*path)) {
2,283,634✔
1417
                *path = NULL;
2,204,420✔
1418
                return 0;
2,204,420✔
1419
        }
1420

1421
        r = path_is_root(*path);
79,214✔
1422
        if (r < 0)
79,214✔
1423
                return r;
1424
        if (r > 0)
79,206✔
1425
                *path = NULL;
9✔
1426

1427
        return 0;
1428
}
1429

1430
bool path_strv_contains(char * const *l, const char *path) {
54,082✔
1431
        assert(path);
54,082✔
1432

1433
        STRV_FOREACH(i, l)
903,139✔
1434
                if (path_equal(*i, path))
858,153✔
1435
                        return true;
1436

1437
        return false;
1438
}
1439

1440
bool prefixed_path_strv_contains(char * const *l, const char *path) {
71✔
1441
        assert(path);
71✔
1442

1443
        STRV_FOREACH(i, l) {
72✔
1444
                const char *j = *i;
1✔
1445

1446
                if (*j == '-')
1✔
1447
                        j++;
×
1448
                if (*j == '+')
1✔
1449
                        j++;
×
1450

1451
                if (path_equal(j, path))
1✔
1452
                        return true;
1453
        }
1454

1455
        return false;
1456
}
1457

1458
int path_glob_can_match(const char *pattern, const char *prefix, char **ret) {
11,507✔
1459
        assert(pattern);
11,507✔
1460
        assert(prefix);
11,507✔
1461

1462
        for (const char *a = pattern, *b = prefix;;) {
11,507✔
1463
                _cleanup_free_ char *g = NULL, *h = NULL;
25,905✔
1464
                const char *p, *q;
34,523✔
1465
                int r, s;
34,523✔
1466

1467
                r = path_find_first_component(&a, /* accept_dot_dot = */ false, &p);
34,523✔
1468
                if (r < 0)
34,523✔
1469
                        return r;
1470

1471
                s = path_find_first_component(&b, /* accept_dot_dot = */ false, &q);
34,523✔
1472
                if (s < 0)
34,523✔
1473
                        return s;
1474

1475
                if (s == 0) {
34,523✔
1476
                        /* The pattern matches the prefix. */
1477
                        if (ret) {
2,889✔
1478
                                char *t;
2,889✔
1479

1480
                                t = path_join(prefix, p);
2,889✔
1481
                                if (!t)
2,889✔
1482
                                        return -ENOMEM;
1483

1484
                                *ret = t;
2,889✔
1485
                        }
1486
                        return true;
2,889✔
1487
                }
1488

1489
                if (r == 0)
31,634✔
1490
                        break;
1491

1492
                if (r == s && strneq(p, q, r))
31,630✔
1493
                        continue; /* common component. Check next. */
20,128✔
1494

1495
                g = strndup(p, r);
11,502✔
1496
                if (!g)
11,502✔
1497
                        return -ENOMEM;
1498

1499
                if (!string_is_glob(g))
11,502✔
1500
                        break;
1501

1502
                /* We found a glob component. Check if the glob pattern matches the prefix component. */
1503

1504
                h = strndup(q, s);
2,888✔
1505
                if (!h)
2,888✔
1506
                        return -ENOMEM;
1507

1508
                r = fnmatch(g, h, 0);
2,888✔
1509
                if (r == FNM_NOMATCH)
2,888✔
1510
                        break;
1511
                if (r != 0) /* Failure to process pattern? */
2,888✔
1512
                        return -EINVAL;
1513
        }
1514

1515
        /* The pattern does not match the prefix. */
1516
        if (ret)
8,618✔
1517
                *ret = NULL;
8,618✔
1518
        return false;
1519
}
1520

1521
const char* default_PATH(void) {
3,428✔
1522
#if HAVE_SPLIT_BIN
1523
        static int split = -1;
1524
        int r;
1525

1526
        /* Check whether /usr/sbin is not a symlink and return the appropriate $PATH.
1527
         * On error fall back to the safe value with both directories as configured… */
1528

1529
        if (split < 0)
1530
                STRV_FOREACH_PAIR(bin, sbin, STRV_MAKE("/usr/bin", "/usr/sbin",
1531
                                                       "/usr/local/bin", "/usr/local/sbin")) {
1532
                        r = inode_same(*bin, *sbin, AT_NO_AUTOMOUNT);
1533
                        if (r > 0 || r == -ENOENT)
1534
                                continue;
1535
                        if (r < 0)
1536
                                log_debug_errno(r, "Failed to compare \"%s\" and \"%s\", using compat $PATH: %m",
1537
                                                *bin, *sbin);
1538
                        split = true;
1539
                        break;
1540
                }
1541
        if (split < 0)
1542
                split = false;
1543
        if (split)
1544
                return DEFAULT_PATH_WITH_SBIN;
1545
#endif
1546
        return DEFAULT_PATH_WITHOUT_SBIN;
3,428✔
1547
}
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