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

systemd / systemd / 14872145375

06 May 2025 09:07PM UTC coverage: 72.232% (+0.02%) from 72.214%
14872145375

push

github

DaanDeMeyer
string-table: annotate _to_string and _from_string with _const_ and _pure_, respectively

Follow-up for c94f6ab1b

297286 of 411572 relevant lines covered (72.23%)

695615.99 hits per line

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

76.83
/src/shared/vpick.c
1
/* SPDX-License-Identifier: LGPL-2.1-or-later */
2

3
#include <sys/stat.h>
4

5
#include "architecture.h"
6
#include "bitfield.h"
7
#include "chase.h"
8
#include "fd-util.h"
9
#include "fs-util.h"
10
#include "log.h"
11
#include "parse-util.h"
12
#include "path-util.h"
13
#include "recurse-dir.h"
14
#include "vpick.h"
15

16
void pick_result_done(PickResult *p) {
14,604✔
17
        assert(p);
14,604✔
18

19
        free(p->path);
14,604✔
20
        safe_close(p->fd);
14,604✔
21
        free(p->version);
14,604✔
22

23
        *p = PICK_RESULT_NULL;
14,604✔
24
}
14,604✔
25

26
static int format_fname(
159✔
27
                const PickFilter *filter,
28
                PickFlags flags,
29
                char **ret) {
30

31
        _cleanup_free_ char *fn = NULL;
159✔
32
        int r;
159✔
33

34
        assert(filter);
159✔
35
        assert(ret);
159✔
36

37
        if (FLAGS_SET(flags, PICK_TRIES) || !filter->version) /* Underspecified? */
159✔
38
                return -ENOEXEC;
39
        if (strv_length(filter->suffix) > 1) /* suffix is not deterministic? */
×
40
                return -ENOEXEC;
41

42
        /* The format for names we match goes like this:
43
         *
44
         *        <basename><suffix>
45
         *  or:
46
         *        <basename>_<version><suffix>
47
         *  or:
48
         *        <basename>_<version>_<architecture><suffix>
49
         *  or:
50
         *        <basename>_<architecture><suffix>
51
         *
52
         * (Note that basename can be empty, in which case the leading "_" is suppressed)
53
         *
54
         * Examples: foo.raw, foo_1.3-7.raw, foo_1.3-7_x86-64.raw, foo_x86-64.raw
55
         *
56
         * Why use "_" as separator here? Primarily because it is not used by Semver 2.0. In RPM it is used
57
         * for "unsortable" versions, i.e. doesn't show up in "sortable" versions, which we matter for this
58
         * usecase here. In Debian the underscore is not allowed (and it uses it itself for separating
59
         * fields).
60
         *
61
         * This is very close to Debian's way to name packages, but allows arbitrary suffixes, and makes the
62
         * architecture field redundant.
63
         *
64
         * Compare with RPM's "NEVRA" concept. Here we have "BVAS" (basename, version, architecture, suffix).
65
         */
66

67
        if (filter->basename) {
×
68
                fn = strdup(filter->basename);
×
69
                if (!fn)
×
70
                        return -ENOMEM;
71
        }
72

73
        if (filter->version) {
×
74
                if (isempty(fn)) {
×
75
                        r = free_and_strdup(&fn, filter->version);
×
76
                        if (r < 0)
×
77
                                return r;
78
                } else if (!strextend(&fn, "_", filter->version))
×
79
                        return -ENOMEM;
80
        }
81

82
        if (FLAGS_SET(flags, PICK_ARCHITECTURE) && filter->architecture >= 0) {
×
83
                const char *as = ASSERT_PTR(architecture_to_string(filter->architecture));
×
84
                if (isempty(fn)) {
×
85
                        r = free_and_strdup(&fn, as);
×
86
                        if (r < 0)
×
87
                                return r;
88
                } else if (!strextend(&fn, "_", as))
×
89
                        return -ENOMEM;
90
        }
91

92
        if (!strv_isempty(filter->suffix))
×
93
                if (!strextend(&fn, filter->suffix[0]))
×
94
                        return -ENOMEM;
95

96
        if (!filename_is_valid(fn))
×
97
                return -EINVAL;
98

99
        *ret = TAKE_PTR(fn);
×
100
        return 0;
×
101
}
102

103
static int errno_from_mode(uint32_t type_mask, mode_t found) {
5✔
104
        /* Returns the most appropriate error code if we are lookging for an inode of type of those in the
105
         * 'type_mask' but found 'found' instead.
106
         *
107
         * type_mask is a mask of 1U << DT_REG, 1U << DT_DIR, … flags, while found is a S_IFREG, S_IFDIR, …
108
         * mode value. */
109

110
        if (type_mask == 0) /* type doesn't matter */
5✔
111
                return 0;
112

113
        if (BIT_SET(type_mask, IFTODT(found)))
5✔
114
                return 0;
115

116
        if (type_mask == (UINT32_C(1) << DT_BLK))
5✔
117
                return -ENOTBLK;
118
        if (type_mask == (UINT32_C(1) << DT_DIR))
5✔
119
                return -ENOTDIR;
120
        if (type_mask == (UINT32_C(1) << DT_SOCK))
3✔
121
                return -ENOTSOCK;
122

123
        if (S_ISLNK(found))
2✔
124
                return -ELOOP;
125
        if (S_ISDIR(found))
2✔
126
                return -EISDIR;
1✔
127

128
        return -EBADF;
129
}
130

131
static int pin_choice(
7,315✔
132
                const char *toplevel_path,
133
                int toplevel_fd,
134
                const char *inode_path,
135
                int _inode_fd, /* we always take ownership of the fd, even on failure */
136
                unsigned tries_left,
137
                unsigned tries_done,
138
                const PickFilter *filter,
139
                PickFlags flags,
140
                PickResult *ret) {
141

142
        _cleanup_close_ int inode_fd = TAKE_FD(_inode_fd);
7,315✔
143
        _cleanup_free_ char *resolved_path = NULL;
7,315✔
144
        int r;
7,315✔
145

146
        assert(toplevel_fd >= 0 || toplevel_fd == AT_FDCWD);
7,315✔
147
        assert(inode_path);
7,315✔
148
        assert(filter);
7,315✔
149
        assert(ret);
7,315✔
150

151
        if (inode_fd < 0 || FLAGS_SET(flags, PICK_RESOLVE)) {
7,315✔
152
                r = chaseat(toplevel_fd,
14,566✔
153
                            inode_path,
154
                            CHASE_AT_RESOLVE_IN_ROOT,
155
                            FLAGS_SET(flags, PICK_RESOLVE) ? &resolved_path : NULL,
7,283✔
156
                            inode_fd < 0 ? &inode_fd : NULL);
157
                if (r < 0)
7,283✔
158
                        return r;
159

160
                if (resolved_path)
6,376✔
161
                        inode_path = resolved_path;
5,905✔
162
        }
163

164
        struct stat st;
6,408✔
165
        if (fstat(inode_fd, &st) < 0)
6,408✔
166
                return log_debug_errno(errno, "Failed to stat discovered inode '%s%s': %m",
×
167
                                       empty_to_root(toplevel_path), skip_leading_slash(inode_path));
168

169
        if (filter->type_mask != 0 &&
6,408✔
170
            !BIT_SET(filter->type_mask, IFTODT(st.st_mode)))
6,384✔
171
                return log_debug_errno(
5✔
172
                                SYNTHETIC_ERRNO(errno_from_mode(filter->type_mask, st.st_mode)),
173
                                "Inode '%s/%s' has wrong type, found '%s'.",
174
                                empty_to_root(toplevel_path), skip_leading_slash(inode_path),
175
                                inode_type_to_string(st.st_mode));
176

177
        _cleanup_(pick_result_done) PickResult result = {
6,403✔
178
                .fd = TAKE_FD(inode_fd),
6,403✔
179
                .st = st,
180
                .architecture = filter->architecture,
6,403✔
181
                .tries_left = tries_left,
182
                .tries_done = tries_done,
183
        };
184

185
        result.path = strdup(inode_path);
6,403✔
186
        if (!result.path)
6,403✔
187
                return log_oom_debug();
×
188

189
        r = strdup_to(&result.version, filter->version);
6,403✔
190
        if (r < 0)
6,403✔
191
                return r;
192

193
        *ret = TAKE_PICK_RESULT(result);
6,403✔
194
        return 1;
6,403✔
195
}
196

197
static int parse_tries(const char *s, unsigned *ret_tries_left, unsigned *ret_tries_done) {
31✔
198
        unsigned left, done;
31✔
199
        size_t n;
31✔
200

201
        assert(s);
31✔
202
        assert(ret_tries_left);
31✔
203
        assert(ret_tries_done);
31✔
204

205
        if (s[0] != '+')
31✔
206
                goto nomatch;
×
207

208
        s++;
31✔
209

210
        n = strspn(s, DIGITS);
31✔
211
        if (n == 0)
31✔
212
                goto nomatch;
×
213

214
        if (s[n] == 0) {
31✔
215
                if (safe_atou(s, &left) < 0)
×
216
                        goto nomatch;
×
217

218
                done = 0;
×
219
        } else if (s[n] == '-') {
31✔
220
                _cleanup_free_ char *c = NULL;
31✔
221

222
                c = strndup(s, n);
31✔
223
                if (!c)
31✔
224
                        return -ENOMEM;
×
225

226
                if (safe_atou(c, &left) < 0)
31✔
227
                        goto nomatch;
×
228

229
                s += n + 1;
31✔
230

231
                if (!in_charset(s, DIGITS))
31✔
232
                        goto nomatch;
×
233

234
                if (safe_atou(s, &done) < 0)
31✔
235
                        goto nomatch;
×
236
        } else
237
                goto nomatch;
×
238

239
        *ret_tries_left = left;
31✔
240
        *ret_tries_done = done;
31✔
241
        return 1;
31✔
242

243
nomatch:
×
244
        *ret_tries_left = *ret_tries_done = UINT_MAX;
×
245
        return 0;
×
246
}
247

248
static int make_choice(
161✔
249
                const char *toplevel_path,
250
                int toplevel_fd,
251
                const char *inode_path,
252
                int _inode_fd, /* we always take ownership of the fd, even on failure */
253
                const PickFilter *filter,
254
                PickFlags flags,
255
                PickResult *ret) {
256

257
        static const Architecture local_architectures[] = {
161✔
258
                /* In order of preference */
259
                native_architecture(),
260
#ifdef ARCHITECTURE_SECONDARY
261
                ARCHITECTURE_SECONDARY,
262
#endif
263
                _ARCHITECTURE_INVALID, /* accept any arch, as last resort */
264
        };
265

266
        _cleanup_free_ DirectoryEntries *de = NULL;
322✔
267
        _cleanup_free_ char *best_version = NULL, *best_filename = NULL, *p = NULL, *j = NULL;
161✔
268
        _cleanup_close_ int dir_fd = -EBADF, object_fd = -EBADF, inode_fd = TAKE_FD(_inode_fd);
483✔
269
        const Architecture *architectures;
161✔
270
        unsigned best_tries_left = UINT_MAX, best_tries_done = UINT_MAX;
161✔
271
        size_t n_architectures, best_architecture_index = SIZE_MAX;
161✔
272
        int r;
161✔
273

274
        assert(toplevel_fd >= 0 || toplevel_fd == AT_FDCWD);
161✔
275
        assert(inode_path);
161✔
276
        assert(filter);
161✔
277
        assert(ret);
161✔
278

279
        if (inode_fd < 0) {
161✔
280
                r = chaseat(toplevel_fd, inode_path, CHASE_AT_RESOLVE_IN_ROOT, NULL, &inode_fd);
161✔
281
                if (r < 0)
161✔
282
                        return r;
283
        }
284

285
        /* Maybe the filter is fully specified? Then we can generate the file name directly */
286
        r = format_fname(filter, flags, &j);
159✔
287
        if (r >= 0) {
159✔
288
                _cleanup_free_ char *object_path = NULL;
×
289

290
                /* Yay! This worked! */
291
                p = path_join(inode_path, j);
×
292
                if (!p)
×
293
                        return log_oom_debug();
×
294

295
                r = chaseat(toplevel_fd, p, CHASE_AT_RESOLVE_IN_ROOT, &object_path, &object_fd);
×
296
                if (r == -ENOENT) {
×
297
                        *ret = PICK_RESULT_NULL;
×
298
                        return 0;
×
299
                }
300
                if (r < 0)
×
301
                        return log_debug_errno(r, "Failed to open '%s/%s': %m",
×
302
                                               empty_to_root(toplevel_path), skip_leading_slash(p));
303

304
                return pin_choice(
×
305
                                toplevel_path,
306
                                toplevel_fd,
307
                                FLAGS_SET(flags, PICK_RESOLVE) ? object_path : p,
×
308
                                TAKE_FD(object_fd), /* unconditionally pass ownership of the fd */
×
309
                                /* tries_left= */ UINT_MAX,
310
                                /* tries_done= */ UINT_MAX,
311
                                filter,
312
                                flags & ~PICK_RESOLVE,
×
313
                                ret);
314

315
        } else if (r != -ENOEXEC)
159✔
316
                return log_debug_errno(r, "Failed to format file name: %m");
×
317

318
        /* Underspecified, so we do our enumeration dance */
319

320
        /* Convert O_PATH to a regular directory fd */
321
        dir_fd = fd_reopen(inode_fd, O_DIRECTORY|O_RDONLY|O_CLOEXEC);
159✔
322
        if (dir_fd < 0)
159✔
323
                return log_debug_errno(dir_fd, "Failed to reopen '%s/%s' as directory: %m",
×
324
                                       empty_to_root(toplevel_path), skip_leading_slash(inode_path));
325

326
        r = readdir_all(dir_fd, 0, &de);
159✔
327
        if (r < 0)
159✔
328
                return log_debug_errno(r, "Failed to read directory '%s/%s': %m",
×
329
                                       empty_to_root(toplevel_path), skip_leading_slash(inode_path));
330

331
        if (filter->architecture < 0) {
159✔
332
                architectures = local_architectures;
333
                n_architectures = ELEMENTSOF(local_architectures);
334
        } else {
335
                architectures = &filter->architecture;
16✔
336
                n_architectures = 1;
16✔
337
        }
338

339
        FOREACH_ARRAY(entry, de->entries, de->n_entries) {
521✔
340
                unsigned found_tries_done = UINT_MAX, found_tries_left = UINT_MAX;
362✔
341
                _cleanup_free_ char *dname = NULL;
362✔
342
                size_t found_architecture_index = SIZE_MAX;
362✔
343
                const char *e;
362✔
344

345
                dname = strdup((*entry)->d_name);
362✔
346
                if (!dname)
362✔
347
                        return log_oom_debug();
×
348

349
                if (!isempty(filter->basename)) {
362✔
350
                        e = startswith(dname, filter->basename);
362✔
351
                        if (!e)
362✔
352
                                continue;
33✔
353

354
                        if (e[0] != '_')
329✔
355
                                continue;
×
356

357
                        e++;
329✔
358
                } else
359
                        e = dname;
360

361
                if (!strv_isempty(filter->suffix)) {
329✔
362
                        char *sfx = endswith_strv(e, filter->suffix);
298✔
363
                        if (!sfx)
298✔
364
                                continue;
×
365

366
                        *sfx = 0;
298✔
367
                }
368

369
                if (FLAGS_SET(flags, PICK_TRIES)) {
329✔
370
                        char *plus = strrchr(e, '+');
329✔
371
                        if (plus) {
329✔
372
                                r = parse_tries(plus, &found_tries_left, &found_tries_done);
31✔
373
                                if (r < 0)
31✔
374
                                        return r;
375
                                if (r > 0) /* Found and parsed, now chop off */
31✔
376
                                        *plus = 0;
31✔
377
                        }
378
                }
379

380
                if (FLAGS_SET(flags, PICK_ARCHITECTURE)) {
329✔
381
                        char *underscore = strrchr(e, '_');
329✔
382
                        Architecture a;
329✔
383

384
                        a = underscore ? architecture_from_string(underscore + 1) : _ARCHITECTURE_INVALID;
329✔
385

386
                        for (size_t i = 0; i < n_architectures; i++)
858✔
387
                                if (architectures[i] == a) {
767✔
388
                                        found_architecture_index = i;
389
                                        break;
390
                                }
391

392
                        if (found_architecture_index == SIZE_MAX) { /* No matching arch found */
329✔
393
                                log_debug("Found entry with architecture '%s' which is not what we are looking for, ignoring entry.", a < 0 ? "any" : architecture_to_string(a));
91✔
394
                                continue;
91✔
395
                        }
396

397
                        /* Chop off architecture from string */
398
                        if (underscore)
238✔
399
                                *underscore = 0;
44✔
400
                }
401

402
                if (!version_is_valid(e)) {
238✔
403
                        log_debug("Version string '%s' of entry '%s' is invalid, ignoring entry.", e, (*entry)->d_name);
×
404
                        continue;
×
405
                }
406

407
                if (filter->version && !streq(filter->version, e)) {
238✔
408
                        log_debug("Found entry with version string '%s', but was looking for '%s', ignoring entry.", e, filter->version);
16✔
409
                        continue;
16✔
410
                }
411

412
                if (best_filename) { /* Already found one matching entry? Then figure out the better one */
222✔
413
                        int d = 0;
70✔
414

415
                        /* First, prefer entries with tries left over those without */
416
                        if (FLAGS_SET(flags, PICK_TRIES))
70✔
417
                                d = CMP(found_tries_left != 0, best_tries_left != 0);
70✔
418

419
                        /* Second, prefer newer versions */
420
                        if (d == 0)
65✔
421
                                d = strverscmp_improved(e, best_version);
62✔
422

423
                        /* Third, prefer native architectures over secondary architectures */
424
                        if (d == 0 &&
62✔
425
                            FLAGS_SET(flags, PICK_ARCHITECTURE) &&
426
                            found_architecture_index != SIZE_MAX && best_architecture_index != SIZE_MAX)
6✔
427
                                d = -CMP(found_architecture_index, best_architecture_index);
6✔
428

429
                        /* Fourth, prefer entries with more tries left */
430
                        if (FLAGS_SET(flags, PICK_TRIES)) {
70✔
431
                                if (d == 0)
70✔
432
                                        d = CMP(found_tries_left, best_tries_left);
×
433

434
                                /* Fifth, prefer entries with fewer attempts done so far */
435
                                if (d == 0)
×
436
                                        d = -CMP(found_tries_done, best_tries_done);
×
437
                        }
438

439
                        /* Last, just compare the filenames as strings */
440
                        if (d == 0)
×
441
                                d = strcmp((*entry)->d_name, best_filename);
×
442

443
                        if (d < 0) {
70✔
444
                                log_debug("Found entry '%s' but previously found entry '%s' matches better, hence skipping entry.", (*entry)->d_name, best_filename);
54✔
445
                                continue;
54✔
446
                        }
447
                }
448

449
                r = free_and_strdup_warn(&best_version, e);
168✔
450
                if (r < 0)
168✔
451
                        return r;
452

453
                r = free_and_strdup_warn(&best_filename, (*entry)->d_name);
168✔
454
                if (r < 0)
168✔
455
                        return r;
456

457
                best_architecture_index = found_architecture_index;
168✔
458
                best_tries_left = found_tries_left;
168✔
459
                best_tries_done = found_tries_done;
168✔
460
        }
461

462
        if (!best_filename) { /* Everything was good, but we didn't find any suitable entry */
159✔
463
                *ret = PICK_RESULT_NULL;
7✔
464
                return 0;
7✔
465
        }
466

467
        p = path_join(inode_path, best_filename);
152✔
468
        if (!p)
152✔
469
                return log_oom_debug();
×
470

471
        object_fd = openat(dir_fd, best_filename, O_CLOEXEC|O_PATH);
152✔
472
        if (object_fd < 0)
152✔
473
                return log_debug_errno(errno, "Failed to open '%s/%s': %m",
×
474
                                       empty_to_root(toplevel_path), skip_leading_slash(inode_path));
475

476
        return pin_choice(
304✔
477
                        toplevel_path,
478
                        toplevel_fd,
479
                        p,
480
                        TAKE_FD(object_fd),
152✔
481
                        best_tries_left,
482
                        best_tries_done,
483
                        &(const PickFilter) {
456✔
484
                                .type_mask = filter->type_mask,
152✔
485
                                .basename = filter->basename,
152✔
486
                                .version = empty_to_null(best_version),
152✔
487
                                .architecture = best_architecture_index != SIZE_MAX ? architectures[best_architecture_index] : _ARCHITECTURE_INVALID,
152✔
488
                                .suffix = filter->suffix,
152✔
489
                        },
490
                        flags,
491
                        ret);
492
}
493

494
int path_pick(
7,324✔
495
                const char *toplevel_path,
496
                int toplevel_fd,
497
                const char *path,
498
                const PickFilter *filter,
499
                PickFlags flags,
500
                PickResult *ret) {
501

502
        _cleanup_free_ char *filter_bname = NULL, *dir = NULL, *parent = NULL, *fname = NULL;
7,324✔
503
        char * const *filter_suffix_strv = NULL;
7,324✔
504
        const char *filter_suffix = NULL, *enumeration_path;
7,324✔
505
        uint32_t filter_type_mask;
7,324✔
506
        int r;
7,324✔
507

508
        assert(toplevel_fd >= 0 || toplevel_fd == AT_FDCWD);
7,324✔
509
        assert(path);
7,324✔
510
        assert(filter);
7,324✔
511
        assert(ret);
7,324✔
512

513
        /* Given a path, resolve .v/ subdir logic (if used!), and returns the choice made. This supports
514
         * three ways to be called:
515
         *
516
         * • with a path referring a directory of any name, and filter→basename *explicitly* specified, in
517
         *   which case we'll use a pattern "<filter→basename>_*<filter→suffix>" on the directory's files.
518
         *
519
         * • with no filter→basename explicitly specified and a path referring to a directory named in format
520
         *   "<somestring><filter→suffix>.v" . In this case the filter basename to search for inside the dir
521
         *   is derived from the directory name. Example: "/foo/bar/baz.suffix.v" → we'll search for
522
         *   "/foo/bar/baz.suffix.v/baz_*.suffix".
523
         *
524
         * • with a path whose penultimate component ends in ".v/". In this case the final component of the
525
         *   path refers to the pattern. Example: "/foo/bar/baz.v/waldo__.suffix" → we'll search for
526
         *   "/foo/bar/baz.v/waldo_*.suffix".
527
         */
528

529
        /* Explicit basename specified, then shortcut things and do .v mode regardless of the path name. */
530
        if (filter->basename)
7,324✔
531
                return make_choice(
3✔
532
                                toplevel_path,
533
                                toplevel_fd,
534
                                path,
535
                                /* inode_fd= */ -EBADF,
536
                                filter,
537
                                flags,
538
                                ret);
539

540
        r = path_extract_filename(path, &fname);
7,321✔
541
        if (r < 0) {
7,321✔
542
                if (r != -EADDRNOTAVAIL) /* root dir or "." */
1✔
543
                        return r;
544

545
                /* If there's no path element we can derive a pattern off, the don't */
546
                goto bypass;
1✔
547
        }
548

549
        /* Remember if the path ends in a dash suffix */
550
        bool slash_suffix = r == O_DIRECTORY;
7,320✔
551

552
        const char *e = endswith(fname, ".v");
7,320✔
553
        if (e) {
7,320✔
554
                /* So a path in the form /foo/bar/baz.v is specified. In this case our search basename is
555
                 * "baz", possibly with a suffix chopped off if there's one specified. */
556
                filter_bname = strndup(fname, e - fname);
157✔
557
                if (!filter_bname)
157✔
558
                        return -ENOMEM;
559

560
                /* Chop off suffix, if specified */
561
                char *f = endswith_strv(filter_bname, filter->suffix);
157✔
562
                if (f)
157✔
563
                        *f = 0;
132✔
564

565
                filter_suffix_strv = filter->suffix;
157✔
566
                filter_type_mask = filter->type_mask;
157✔
567

568
                enumeration_path = path;
157✔
569
        } else {
570
                /* The path does not end in '.v', hence see if the last element is a pattern. */
571

572
                char *wildcard = strrstr(fname, "___");
7,163✔
573
                if (!wildcard)
7,163✔
574
                        goto bypass; /* Not a pattern, then bypass */
7,162✔
575

576
                /* We found the '___' wildcard, hence everything after it is our filter suffix, and
577
                 * everything before is our filter basename */
578
                *wildcard = 0;
1✔
579
                filter_suffix = empty_to_null(wildcard + 3);
1✔
580

581
                filter_bname = TAKE_PTR(fname);
1✔
582

583
                r = path_extract_directory(path, &dir);
1✔
584
                if (r < 0) {
1✔
585
                        if (!IN_SET(r, -EDESTADDRREQ, -EADDRNOTAVAIL)) /* only filename specified (no dir), or root or "." */
×
586
                                return r;
587

588
                        goto bypass; /* No dir extractable, can't check if parent ends in ".v" */
×
589
                }
590

591
                r = path_extract_filename(dir, &parent);
1✔
592
                if (r < 0) {
1✔
593
                        if (r != -EADDRNOTAVAIL) /* root dir or "." */
×
594
                                return r;
595

596
                        goto bypass; /* Cannot extract fname from parent dir, can't check if it ends in ".v" */
×
597
                }
598

599
                e = endswith(parent, ".v");
1✔
600
                if (!e)
1✔
601
                        goto bypass; /* Doesn't end in ".v", shortcut */
×
602

603
                filter_type_mask = filter->type_mask;
1✔
604
                if (slash_suffix) {
1✔
605
                        /* If the pattern is suffixed by a / then we are looking for directories apparently. */
606
                        if (filter_type_mask != 0 && !BIT_SET(filter_type_mask, DT_DIR))
×
607
                                return log_debug_errno(SYNTHETIC_ERRNO(errno_from_mode(filter_type_mask, S_IFDIR)),
×
608
                                                       "Specified pattern ends in '/', but not looking for directories, refusing.");
609
                        filter_type_mask = UINT32_C(1) << DT_DIR;
610
                }
611

612
                enumeration_path = dir;
1✔
613
        }
614

615
        return make_choice(
316✔
616
                        toplevel_path,
617
                        toplevel_fd,
618
                        enumeration_path,
619
                        /* inode_fd= */ -EBADF,
620
                        &(const PickFilter) {
158✔
621
                                .type_mask = filter_type_mask,
622
                                .basename = filter_bname,
623
                                .version = filter->version,
158✔
624
                                .architecture = filter->architecture,
158✔
625
                                .suffix = filter_suffix_strv ?: STRV_MAKE(filter_suffix),
158✔
626
                        },
627
                        flags,
628
                        ret);
629

630
bypass:
7,163✔
631
        /* Don't make any choice, but just use the passed path literally */
632
        return pin_choice(
7,163✔
633
                        toplevel_path,
634
                        toplevel_fd,
635
                        path,
636
                        /* inode_fd= */ -EBADF,
637
                        /* tries_left= */ UINT_MAX,
638
                        /* tries_done= */ UINT_MAX,
639
                        filter,
640
                        flags,
641
                        ret);
642
}
643

644
int path_pick_update_warn(
468✔
645
                char **path,
646
                const PickFilter *filter,
647
                PickFlags flags,
648
                PickResult *ret_result) {
649

650
        _cleanup_(pick_result_done) PickResult result = PICK_RESULT_NULL;
468✔
651
        int r;
468✔
652

653
        assert(path);
468✔
654
        assert(*path);
468✔
655
        assert(filter);
468✔
656

657
        /* This updates the first argument if needed! */
658

659
        r = path_pick(/* toplevel_path= */ NULL,
468✔
660
                      /* toplevel_fd= */ AT_FDCWD,
661
                      *path,
662
                      filter,
663
                      flags,
664
                      &result);
665
        if (r == -ENOENT) {
468✔
666
                log_debug("Path '%s' doesn't exist, leaving as is.", *path);
4✔
667

668
                if (ret_result)
4✔
669
                        *ret_result = PICK_RESULT_NULL;
4✔
670
                return 0;
4✔
671
        }
672
        if (r < 0)
464✔
673
                return log_error_errno(r, "Failed to pick version on path '%s': %m", *path);
×
674
        if (r == 0)
464✔
675
                return log_error_errno(SYNTHETIC_ERRNO(ENOENT), "No matching entries in versioned directory '%s' found.", *path);
×
676

677
        log_debug("Resolved versioned directory pattern '%s' to file '%s' as version '%s'.", result.path, *path, strna(result.version));
928✔
678

679
        if (ret_result) {
464✔
680
                r = free_and_strdup_warn(path, result.path);
370✔
681
                if (r < 0)
370✔
682
                        return r;
683

684
                *ret_result = TAKE_PICK_RESULT(result);
370✔
685
        } else
686
                free_and_replace(*path, result.path);
94✔
687

688
        return 1;
689
}
690

691
const PickFilter pick_filter_image_raw = {
692
        .type_mask = (UINT32_C(1) << DT_REG) | (UINT32_C(1) << DT_BLK),
693
        .architecture = _ARCHITECTURE_INVALID,
694
        .suffix = STRV_MAKE(".raw"),
695
};
696

697
const PickFilter pick_filter_image_dir = {
698
        .type_mask = UINT32_C(1) << DT_DIR,
699
        .architecture = _ARCHITECTURE_INVALID,
700
};
701

702
const PickFilter pick_filter_image_any = {
703
        .type_mask = (UINT32_C(1) << DT_REG) | (UINT32_C(1) << DT_BLK) | (UINT32_C(1) << DT_DIR),
704
        .architecture = _ARCHITECTURE_INVALID,
705
        .suffix = STRV_MAKE(".raw", ""),
706
};
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