]>
Commit | Line | Data |
---|---|---|
76511c1b LP |
1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
2 | ||
3 | #include <sys/stat.h> | |
4 | ||
5 | #include "architecture.h" | |
6 | #include "chase.h" | |
7 | #include "fd-util.h" | |
8 | #include "fs-util.h" | |
9 | #include "parse-util.h" | |
10 | #include "path-util.h" | |
11 | #include "recurse-dir.h" | |
12 | #include "vpick.h" | |
13 | ||
14 | void pick_result_done(PickResult *p) { | |
15 | assert(p); | |
16 | ||
17 | free(p->path); | |
18 | safe_close(p->fd); | |
19 | free(p->version); | |
20 | ||
21 | *p = PICK_RESULT_NULL; | |
22 | } | |
23 | ||
24 | static int format_fname( | |
25 | const PickFilter *filter, | |
26 | PickFlags flags, | |
27 | char **ret) { | |
28 | ||
29 | _cleanup_free_ char *fn = NULL; | |
30 | int r; | |
31 | ||
32 | assert(filter); | |
33 | assert(ret); | |
34 | ||
35 | if (FLAGS_SET(flags, PICK_TRIES) || !filter->version) /* Underspecified? */ | |
36 | return -ENOEXEC; | |
37 | ||
38 | /* The format for names we match goes like this: | |
39 | * | |
40 | * <basename><suffix> | |
41 | * or: | |
42 | * <basename>_<version><suffix> | |
43 | * or: | |
44 | * <basename>_<version>_<architecture><suffix> | |
45 | * or: | |
46 | * <basename>_<architecture><suffix> | |
47 | * | |
48 | * (Note that basename can be empty, in which case the leading "_" is suppressed) | |
49 | * | |
50 | * Examples: foo.raw, foo_1.3-7.raw, foo_1.3-7_x86-64.raw, foo_x86-64.raw | |
51 | * | |
52 | * Why use "_" as separator here? Primarily because it is not used by Semver 2.0. In RPM it is used | |
53 | * for "unsortable" versions, i.e. doesn't show up in "sortable" versions, which we matter for this | |
54 | * usecase here. In Debian the underscore is not allowed (and it uses it itself for separating | |
55 | * fields). | |
56 | * | |
57 | * This is very close to Debian's way to name packages, but allows arbitrary suffixes, and makes the | |
58 | * architecture field redundant. | |
59 | * | |
60 | * Compare with RPM's "NEVRA" concept. Here we have "BVAS" (basename, version, architecture, suffix). | |
61 | */ | |
62 | ||
63 | if (filter->basename) { | |
64 | fn = strdup(filter->basename); | |
65 | if (!fn) | |
66 | return -ENOMEM; | |
67 | } | |
68 | ||
69 | if (filter->version) { | |
70 | if (isempty(fn)) { | |
71 | r = free_and_strdup(&fn, filter->version); | |
72 | if (r < 0) | |
73 | return r; | |
74 | } else if (!strextend(&fn, "_", filter->version)) | |
75 | return -ENOMEM; | |
76 | } | |
77 | ||
78 | if (FLAGS_SET(flags, PICK_ARCHITECTURE) && filter->architecture >= 0) { | |
79 | const char *as = ASSERT_PTR(architecture_to_string(filter->architecture)); | |
80 | if (isempty(fn)) { | |
81 | r = free_and_strdup(&fn, as); | |
82 | if (r < 0) | |
83 | return r; | |
84 | } else if (!strextend(&fn, "_", as)) | |
85 | return -ENOMEM; | |
86 | } | |
87 | ||
88 | if (filter->suffix && !strextend(&fn, filter->suffix)) | |
89 | return -ENOMEM; | |
90 | ||
91 | if (!filename_is_valid(fn)) | |
92 | return -EINVAL; | |
93 | ||
94 | *ret = TAKE_PTR(fn); | |
95 | return 0; | |
96 | } | |
97 | ||
98 | static int errno_from_mode(uint32_t type_mask, mode_t found) { | |
99 | /* Returns the most appropriate error code if we are lookging for an inode of type of those in the | |
100 | * 'type_mask' but found 'found' instead. | |
101 | * | |
102 | * type_mask is a mask of 1U << DT_REG, 1U << DT_DIR, … flags, while found is a S_IFREG, S_IFDIR, … | |
103 | * mode value. */ | |
104 | ||
105 | if (type_mask == 0) /* type doesn't matter */ | |
106 | return 0; | |
107 | ||
108 | if (FLAGS_SET(type_mask, UINT32_C(1) << IFTODT(found))) | |
109 | return 0; | |
110 | ||
111 | if (type_mask == (UINT32_C(1) << DT_BLK)) | |
112 | return -ENOTBLK; | |
113 | if (type_mask == (UINT32_C(1) << DT_DIR)) | |
114 | return -ENOTDIR; | |
115 | if (type_mask == (UINT32_C(1) << DT_SOCK)) | |
116 | return -ENOTSOCK; | |
117 | ||
118 | if (S_ISLNK(found)) | |
119 | return -ELOOP; | |
120 | if (S_ISDIR(found)) | |
121 | return -EISDIR; | |
122 | ||
123 | return -EBADF; | |
124 | } | |
125 | ||
126 | static int pin_choice( | |
127 | const char *toplevel_path, | |
128 | int toplevel_fd, | |
129 | const char *inode_path, | |
130 | int _inode_fd, /* we always take ownership of the fd, even on failure */ | |
131 | unsigned tries_left, | |
132 | unsigned tries_done, | |
133 | const PickFilter *filter, | |
134 | PickFlags flags, | |
135 | PickResult *ret) { | |
136 | ||
137 | _cleanup_close_ int inode_fd = TAKE_FD(_inode_fd); | |
138 | _cleanup_free_ char *resolved_path = NULL; | |
139 | int r; | |
140 | ||
141 | assert(toplevel_fd >= 0 || toplevel_fd == AT_FDCWD); | |
142 | assert(inode_path); | |
143 | assert(filter); | |
144 | ||
145 | toplevel_path = strempty(toplevel_path); | |
146 | ||
147 | if (inode_fd < 0 || FLAGS_SET(flags, PICK_RESOLVE)) { | |
148 | r = chaseat(toplevel_fd, | |
149 | inode_path, | |
150 | CHASE_AT_RESOLVE_IN_ROOT, | |
151 | FLAGS_SET(flags, PICK_RESOLVE) ? &resolved_path : 0, | |
152 | inode_fd < 0 ? &inode_fd : NULL); | |
153 | if (r < 0) | |
154 | return r; | |
155 | ||
156 | if (resolved_path) | |
157 | inode_path = resolved_path; | |
158 | } | |
159 | ||
160 | struct stat st; | |
161 | if (fstat(inode_fd, &st) < 0) | |
162 | return log_debug_errno(errno, "Failed to stat discovered inode '%s/%s': %m", toplevel_path, inode_path); | |
163 | ||
164 | if (filter->type_mask != 0 && | |
165 | !FLAGS_SET(filter->type_mask, UINT32_C(1) << IFTODT(st.st_mode))) | |
166 | return log_debug_errno( | |
167 | SYNTHETIC_ERRNO(errno_from_mode(filter->type_mask, st.st_mode)), | |
168 | "Inode '%s/%s' has wrong type, found '%s'.", | |
169 | toplevel_path, inode_path, | |
170 | inode_type_to_string(st.st_mode)); | |
171 | ||
172 | _cleanup_(pick_result_done) PickResult result = { | |
173 | .fd = TAKE_FD(inode_fd), | |
174 | .st = st, | |
175 | .architecture = filter->architecture, | |
176 | .tries_left = tries_left, | |
177 | .tries_done = tries_done, | |
178 | }; | |
179 | ||
180 | result.path = strdup(inode_path); | |
181 | if (!result.path) | |
182 | return log_oom_debug(); | |
183 | ||
184 | if (filter->version) { | |
185 | result.version = strdup(filter->version); | |
186 | if (!result.version) | |
187 | return log_oom_debug(); | |
188 | } | |
189 | ||
190 | *ret = TAKE_PICK_RESULT(result); | |
191 | return 1; | |
192 | } | |
193 | ||
194 | static int parse_tries(const char *s, unsigned *ret_tries_left, unsigned *ret_tries_done) { | |
195 | unsigned left, done; | |
196 | size_t n; | |
197 | ||
198 | assert(s); | |
199 | assert(ret_tries_left); | |
200 | assert(ret_tries_done); | |
201 | ||
202 | if (s[0] != '+') | |
203 | goto nomatch; | |
204 | ||
205 | s++; | |
206 | ||
207 | n = strspn(s, DIGITS); | |
208 | if (n == 0) | |
209 | goto nomatch; | |
210 | ||
211 | if (s[n] == 0) { | |
212 | if (safe_atou(s, &left) < 0) | |
213 | goto nomatch; | |
214 | ||
215 | done = 0; | |
216 | } else if (s[n] == '-') { | |
217 | _cleanup_free_ char *c = NULL; | |
218 | ||
219 | c = strndup(s, n); | |
220 | if (!c) | |
221 | return -ENOMEM; | |
222 | ||
223 | if (safe_atou(c, &left) < 0) | |
224 | goto nomatch; | |
225 | ||
226 | s += n + 1; | |
227 | ||
228 | if (!in_charset(s, DIGITS)) | |
229 | goto nomatch; | |
230 | ||
231 | if (safe_atou(s, &done) < 0) | |
232 | goto nomatch; | |
233 | } else | |
234 | goto nomatch; | |
235 | ||
236 | *ret_tries_left = left; | |
237 | *ret_tries_done = done; | |
238 | return 1; | |
239 | ||
240 | nomatch: | |
241 | *ret_tries_left = *ret_tries_done = UINT_MAX; | |
242 | return 0; | |
243 | } | |
244 | ||
245 | static int make_choice( | |
246 | const char *toplevel_path, | |
247 | int toplevel_fd, | |
248 | const char *inode_path, | |
249 | int _inode_fd, /* we always take ownership of the fd, even on failure */ | |
250 | const PickFilter *filter, | |
251 | PickFlags flags, | |
252 | PickResult *ret) { | |
253 | ||
254 | static const Architecture local_architectures[] = { | |
255 | /* In order of preference */ | |
256 | native_architecture(), | |
257 | #ifdef ARCHITECTURE_SECONDARY | |
258 | ARCHITECTURE_SECONDARY, | |
259 | #endif | |
260 | _ARCHITECTURE_INVALID, /* accept any arch, as last resort */ | |
261 | }; | |
262 | ||
263 | _cleanup_free_ DirectoryEntries *de = NULL; | |
264 | _cleanup_free_ char *best_version = NULL, *best_filename = NULL, *p = NULL, *j = NULL; | |
265 | _cleanup_close_ int dir_fd = -EBADF, object_fd = -EBADF, inode_fd = TAKE_FD(_inode_fd); | |
266 | const Architecture *architectures; | |
267 | unsigned best_tries_left = UINT_MAX, best_tries_done = UINT_MAX; | |
268 | size_t n_architectures, best_architecture_index = SIZE_MAX; | |
269 | int r; | |
270 | ||
271 | assert(toplevel_fd >= 0 || toplevel_fd == AT_FDCWD); | |
272 | assert(inode_path); | |
273 | assert(filter); | |
274 | ||
275 | toplevel_path = strempty(toplevel_path); | |
276 | ||
277 | if (inode_fd < 0) { | |
278 | r = chaseat(toplevel_fd, inode_path, CHASE_AT_RESOLVE_IN_ROOT, NULL, &inode_fd); | |
279 | if (r < 0) | |
280 | return r; | |
281 | } | |
282 | ||
283 | /* Maybe the filter is fully specified? Then we can generate the file name directly */ | |
284 | r = format_fname(filter, flags, &j); | |
285 | if (r >= 0) { | |
286 | _cleanup_free_ char *object_path = NULL; | |
287 | ||
288 | /* Yay! This worked! */ | |
289 | p = path_join(inode_path, j); | |
290 | if (!p) | |
291 | return log_oom_debug(); | |
292 | ||
293 | r = chaseat(toplevel_fd, p, CHASE_AT_RESOLVE_IN_ROOT, &object_path, &object_fd); | |
294 | if (r < 0) { | |
295 | if (r != -ENOENT) | |
296 | return log_debug_errno(r, "Failed to open '%s/%s': %m", toplevel_path, p); | |
297 | ||
298 | *ret = PICK_RESULT_NULL; | |
299 | return 0; | |
300 | } | |
301 | ||
302 | return pin_choice( | |
303 | toplevel_path, | |
304 | toplevel_fd, | |
305 | FLAGS_SET(flags, PICK_RESOLVE) ? object_path : p, | |
306 | TAKE_FD(object_fd), /* unconditionally pass ownership of the fd */ | |
307 | /* tries_left= */ UINT_MAX, | |
308 | /* tries_done= */ UINT_MAX, | |
309 | filter, | |
310 | flags & ~PICK_RESOLVE, | |
311 | ret); | |
312 | ||
313 | } else if (r != -ENOEXEC) | |
314 | return log_debug_errno(r, "Failed to format file name: %m"); | |
315 | ||
316 | /* Underspecified, so we do our enumeration dance */ | |
317 | ||
318 | /* Convert O_PATH to a regular directory fd */ | |
319 | dir_fd = fd_reopen(inode_fd, O_DIRECTORY|O_RDONLY|O_CLOEXEC); | |
320 | if (dir_fd < 0) | |
321 | return log_debug_errno(dir_fd, "Failed to reopen '%s/%s' as directory: %m", toplevel_path, inode_path); | |
322 | ||
323 | r = readdir_all(dir_fd, 0, &de); | |
324 | if (r < 0) | |
325 | return log_debug_errno(r, "Failed to read directory '%s/%s': %m", toplevel_path, inode_path); | |
326 | ||
327 | if (filter->architecture < 0) { | |
328 | architectures = local_architectures; | |
329 | n_architectures = ELEMENTSOF(local_architectures); | |
330 | } else { | |
331 | architectures = &filter->architecture; | |
332 | n_architectures = 1; | |
333 | } | |
334 | ||
335 | FOREACH_ARRAY(entry, de->entries, de->n_entries) { | |
336 | unsigned found_tries_done = UINT_MAX, found_tries_left = UINT_MAX; | |
337 | _cleanup_free_ char *chopped = NULL; | |
338 | size_t found_architecture_index = SIZE_MAX; | |
339 | const char *e; | |
340 | ||
341 | if (!isempty(filter->basename)) { | |
342 | e = startswith((*entry)->d_name, filter->basename); | |
343 | if (!e) | |
344 | continue; | |
345 | ||
346 | if (e[0] != '_') | |
347 | continue; | |
348 | ||
349 | e++; | |
350 | } else | |
351 | e = (*entry)->d_name; | |
352 | ||
353 | if (!isempty(filter->suffix)) { | |
354 | const char *sfx; | |
355 | ||
356 | sfx = endswith(e, filter->suffix); | |
357 | if (!sfx) | |
358 | continue; | |
359 | ||
360 | chopped = strndup(e, sfx - e); | |
361 | if (!chopped) | |
362 | return log_oom_debug(); | |
363 | ||
364 | e = chopped; | |
365 | } | |
366 | ||
367 | if (FLAGS_SET(flags, PICK_TRIES)) { | |
368 | char *plus = strrchr(e, '+'); | |
369 | if (plus) { | |
370 | r = parse_tries(plus, &found_tries_left, &found_tries_done); | |
371 | if (r < 0) | |
372 | return r; | |
373 | if (r > 0) /* Found and parsed, now chop off */ | |
374 | *plus = 0; | |
375 | } | |
376 | } | |
377 | ||
378 | if (FLAGS_SET(flags, PICK_ARCHITECTURE)) { | |
379 | char *underscore = strrchr(e, '_'); | |
380 | Architecture a; | |
381 | ||
382 | a = underscore ? architecture_from_string(underscore + 1) : _ARCHITECTURE_INVALID; | |
383 | ||
384 | for (size_t i = 0; i < n_architectures; i++) | |
385 | if (architectures[i] == a) { | |
386 | found_architecture_index = i; | |
387 | break; | |
388 | } | |
389 | ||
390 | if (found_architecture_index == SIZE_MAX) { /* No matching arch found */ | |
391 | log_debug("Found entry with architecture '%s' which is not what we are looking for, ignoring entry.", a < 0 ? "any" : architecture_to_string(a)); | |
392 | continue; | |
393 | } | |
394 | ||
395 | /* Chop off architecture from string */ | |
396 | if (underscore) | |
397 | *underscore = 0; | |
398 | } | |
399 | ||
400 | if (!version_is_valid(e)) { | |
401 | log_debug("Version string '%s' of entry '%s' is invalid, ignoring entry.", e, (*entry)->d_name); | |
402 | continue; | |
403 | } | |
404 | ||
405 | if (filter->version && !streq(filter->version, e)) { | |
406 | log_debug("Found entry with version string '%s', but was looking for '%s', ignoring entry.", e, filter->version); | |
407 | continue; | |
408 | } | |
409 | ||
410 | if (best_filename) { /* Already found one matching entry? Then figure out the better one */ | |
411 | int d = 0; | |
412 | ||
413 | /* First, prefer entries with tries left over those without */ | |
414 | if (FLAGS_SET(flags, PICK_TRIES)) | |
415 | d = CMP(found_tries_left != 0, best_tries_left != 0); | |
416 | ||
417 | /* Second, prefer newer versions */ | |
418 | if (d == 0) | |
419 | d = strverscmp_improved(e, best_version); | |
420 | ||
421 | /* Third, prefer native architectures over secondary architectures */ | |
422 | if (d == 0 && | |
423 | FLAGS_SET(flags, PICK_ARCHITECTURE) && | |
424 | found_architecture_index != SIZE_MAX && best_architecture_index != SIZE_MAX) | |
425 | d = -CMP(found_architecture_index, best_architecture_index); | |
426 | ||
427 | /* Fourth, prefer entries with more tries left */ | |
428 | if (FLAGS_SET(flags, PICK_TRIES)) { | |
429 | if (d == 0) | |
430 | d = CMP(found_tries_left, best_tries_left); | |
431 | ||
432 | /* Fifth, prefer entries with fewer attempts done so far */ | |
433 | if (d == 0) | |
434 | d = -CMP(found_tries_done, best_tries_done); | |
435 | } | |
436 | ||
437 | /* Last, just compare the filenames as strings */ | |
438 | if (d == 0) | |
439 | d = strcmp((*entry)->d_name, best_filename); | |
440 | ||
441 | if (d < 0) { | |
442 | log_debug("Found entry '%s' but previously found entry '%s' matches better, hence skipping entry.", (*entry)->d_name, best_filename); | |
443 | continue; | |
444 | } | |
445 | } | |
446 | ||
447 | r = free_and_strdup_warn(&best_version, e); | |
448 | if (r < 0) | |
449 | return r; | |
450 | ||
451 | r = free_and_strdup_warn(&best_filename, (*entry)->d_name); | |
452 | if (r < 0) | |
453 | return r; | |
454 | ||
455 | best_architecture_index = found_architecture_index; | |
456 | best_tries_left = found_tries_left; | |
457 | best_tries_done = found_tries_done; | |
458 | } | |
459 | ||
460 | if (!best_filename) { /* Everything was good, but we didn't find any suitable entry */ | |
461 | *ret = PICK_RESULT_NULL; | |
462 | return 0; | |
463 | } | |
464 | ||
465 | p = path_join(inode_path, best_filename); | |
466 | if (!p) | |
467 | return log_oom_debug(); | |
468 | ||
469 | object_fd = openat(dir_fd, best_filename, O_CLOEXEC|O_PATH); | |
470 | if (object_fd < 0) | |
471 | return log_debug_errno(errno, "Failed to open '%s/%s': %m", toplevel_path, p); | |
472 | ||
473 | return pin_choice( | |
474 | toplevel_path, | |
475 | toplevel_fd, | |
476 | p, | |
477 | TAKE_FD(object_fd), | |
478 | best_tries_left, | |
479 | best_tries_done, | |
480 | &(const PickFilter) { | |
481 | .type_mask = filter->type_mask, | |
482 | .basename = filter->basename, | |
483 | .version = empty_to_null(best_version), | |
484 | .architecture = best_architecture_index != SIZE_MAX ? architectures[best_architecture_index] : _ARCHITECTURE_INVALID, | |
485 | .suffix = filter->suffix, | |
486 | }, | |
487 | flags, | |
488 | ret); | |
489 | } | |
490 | ||
491 | int path_pick(const char *toplevel_path, | |
492 | int toplevel_fd, | |
493 | const char *path, | |
494 | const PickFilter *filter, | |
495 | PickFlags flags, | |
496 | PickResult *ret) { | |
497 | ||
498 | _cleanup_free_ char *filter_bname = NULL, *dir = NULL, *parent = NULL, *fname = NULL; | |
499 | const char *filter_suffix, *enumeration_path; | |
500 | uint32_t filter_type_mask; | |
501 | int r; | |
502 | ||
503 | assert(toplevel_fd >= 0 || toplevel_fd == AT_FDCWD); | |
504 | assert(path); | |
505 | ||
506 | toplevel_path = strempty(toplevel_path); | |
507 | ||
508 | /* Given a path, resolve .v/ subdir logic (if used!), and returns the choice made. This supports | |
509 | * three ways to be called: | |
510 | * | |
511 | * • with a path referring a directory of any name, and filter→basename *explicitly* specified, in | |
512 | * which case we'll use a pattern "<filter→basename>_*<filter→suffix>" on the directory's files. | |
513 | * | |
514 | * • with no filter→basename explicitly specified and a path referring to a directory named in format | |
515 | * "<somestring><filter→suffix>.v" . In this case the filter basename to search for inside the dir | |
516 | * is derived from the directory name. Example: "/foo/bar/baz.suffix.v" → we'll search for | |
517 | * "/foo/bar/baz.suffix.v/baz_*.suffix". | |
518 | * | |
519 | * • with a path whose penultimate component ends in ".v/". In this case the final component of the | |
520 | * path refers to the pattern. Example: "/foo/bar/baz.v/waldo__.suffix" → we'll search for | |
521 | * "/foo/bar/baz.v/waldo_*.suffix". | |
522 | */ | |
523 | ||
524 | /* Explicit basename specified, then shortcut things and do .v mode regardless of the path name. */ | |
525 | if (filter->basename) | |
526 | return make_choice( | |
527 | toplevel_path, | |
528 | toplevel_fd, | |
529 | path, | |
530 | /* inode_fd= */ -EBADF, | |
531 | filter, | |
532 | flags, | |
533 | ret); | |
534 | ||
535 | r = path_extract_filename(path, &fname); | |
536 | if (r < 0) { | |
537 | if (r != -EADDRNOTAVAIL) /* root dir or "." */ | |
538 | return r; | |
539 | ||
540 | /* If there's no path element we can derive a pattern off, the don't */ | |
541 | goto bypass; | |
542 | } | |
543 | ||
544 | /* Remember if the path ends in a dash suffix */ | |
545 | bool slash_suffix = r == O_DIRECTORY; | |
546 | ||
547 | const char *e = endswith(fname, ".v"); | |
548 | if (e) { | |
549 | /* So a path in the form /foo/bar/baz.v is specified. In this case our search basename is | |
550 | * "baz", possibly with a suffix chopped off if there's one specified. */ | |
551 | filter_bname = strndup(fname, e - fname); | |
552 | if (!filter_bname) | |
553 | return -ENOMEM; | |
554 | ||
555 | if (filter->suffix) { | |
556 | /* Chop off suffix, if specified */ | |
557 | char *f = endswith(filter_bname, filter->suffix); | |
558 | if (f) | |
559 | *f = 0; | |
560 | } | |
561 | ||
562 | filter_suffix = filter->suffix; | |
563 | filter_type_mask = filter->type_mask; | |
564 | ||
565 | enumeration_path = path; | |
566 | } else { | |
567 | /* The path does not end in '.v', hence see if the last element is a pattern. */ | |
568 | ||
569 | char *wildcard = strrstr(fname, "___"); | |
570 | if (!wildcard) | |
571 | goto bypass; /* Not a pattern, then bypass */ | |
572 | ||
573 | /* We found the '___' wildcard, hence evertyhing after it is our filter suffix, and | |
574 | * evertyhing before is our filter basename */ | |
575 | *wildcard = 0; | |
576 | filter_suffix = empty_to_null(wildcard + 3); | |
577 | ||
578 | filter_bname = TAKE_PTR(fname); | |
579 | ||
580 | r = path_extract_directory(path, &dir); | |
581 | if (r < 0) { | |
582 | if (!IN_SET(r, -EDESTADDRREQ, -EADDRNOTAVAIL)) /* only filename specified (no dir), or root or "." */ | |
583 | return r; | |
584 | ||
585 | goto bypass; /* No dir extractable, can't check if parent ends in ".v" */ | |
586 | } | |
587 | ||
588 | r = path_extract_filename(dir, &parent); | |
589 | if (r < 0) { | |
590 | if (r != -EADDRNOTAVAIL) /* root dir or "." */ | |
591 | return r; | |
592 | ||
593 | goto bypass; /* Cannot extract fname from parent dir, can't check if it ends in ".v" */ | |
594 | } | |
595 | ||
596 | e = endswith(parent, ".v"); | |
597 | if (!e) | |
598 | goto bypass; /* Doesn't end in ".v", shortcut */ | |
599 | ||
600 | filter_type_mask = filter->type_mask; | |
601 | if (slash_suffix) { | |
602 | /* If the pattern is suffixed by a / then we are looking for directories apparently. */ | |
603 | if (filter_type_mask != 0 && !FLAGS_SET(filter_type_mask, UINT32_C(1) << DT_DIR)) | |
604 | return log_debug_errno(SYNTHETIC_ERRNO(errno_from_mode(filter_type_mask, S_IFDIR)), | |
605 | "Specified pattern ends in '/', but not looking for directories, refusing."); | |
606 | filter_type_mask = UINT32_C(1) << DT_DIR; | |
607 | } | |
608 | ||
609 | enumeration_path = dir; | |
610 | } | |
611 | ||
612 | return make_choice( | |
613 | toplevel_path, | |
614 | toplevel_fd, | |
615 | enumeration_path, | |
616 | /* inode_fd= */ -EBADF, | |
617 | &(const PickFilter) { | |
618 | .type_mask = filter_type_mask, | |
619 | .basename = filter_bname, | |
620 | .version = filter->version, | |
621 | .architecture = filter->architecture, | |
622 | .suffix = filter_suffix, | |
623 | }, | |
624 | flags, | |
625 | ret); | |
626 | ||
627 | bypass: | |
628 | /* Don't make any choice, but just use the passed path literally */ | |
629 | return pin_choice( | |
630 | toplevel_path, | |
631 | toplevel_fd, | |
632 | path, | |
633 | /* inode_fd= */ -EBADF, | |
634 | /* tries_left= */ UINT_MAX, | |
635 | /* tries_done= */ UINT_MAX, | |
636 | filter, | |
637 | flags, | |
638 | ret); | |
639 | } | |
640 | ||
641 | int path_pick_update_warn( | |
642 | char **path, | |
643 | const PickFilter *filter, | |
644 | PickFlags flags, | |
645 | PickResult *ret_result) { | |
646 | ||
647 | _cleanup_(pick_result_done) PickResult result = PICK_RESULT_NULL; | |
648 | int r; | |
649 | ||
650 | assert(path); | |
651 | assert(*path); | |
652 | ||
653 | /* This updates the first argument if needed! */ | |
654 | ||
655 | r = path_pick(/* toplevel_path= */ NULL, | |
656 | /* toplevel_fd= */ AT_FDCWD, | |
657 | *path, | |
658 | filter, | |
659 | flags, | |
660 | &result); | |
661 | if (r == -ENOENT) { | |
662 | log_debug("Path '%s' doesn't exist, leaving as is.", *path); | |
663 | *ret_result = PICK_RESULT_NULL; | |
664 | return 0; | |
665 | } | |
666 | if (r < 0) | |
667 | return log_error_errno(r, "Failed to pick version on path '%s': %m", *path); | |
668 | if (r == 0) | |
669 | return log_error_errno(SYNTHETIC_ERRNO(ENOENT), "No matching entries in versioned directory '%s' found.", *path); | |
670 | ||
671 | log_debug("Resolved versioned directory pattern '%s' to file '%s' as version '%s'.", result.path, *path, strna(result.version)); | |
672 | ||
673 | if (ret_result) { | |
674 | r = free_and_strdup_warn(path, result.path); | |
675 | if (r < 0) | |
676 | return r; | |
677 | ||
678 | *ret_result = TAKE_PICK_RESULT(result); | |
679 | } else | |
680 | free_and_replace(*path, result.path); | |
681 | ||
682 | return 1; | |
683 | } | |
684 | ||
685 | const PickFilter pick_filter_image_raw = { | |
686 | .type_mask = (UINT32_C(1) << DT_REG) | (UINT32_C(1) << DT_BLK), | |
687 | .architecture = _ARCHITECTURE_INVALID, | |
688 | .suffix = ".raw", | |
689 | }; | |
690 | ||
691 | const PickFilter pick_filter_image_dir = { | |
692 | .type_mask = UINT32_C(1) << DT_DIR, | |
693 | .architecture = _ARCHITECTURE_INVALID, | |
694 | }; |