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