| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "chase.h" |
| 4 |
#include "fd-util.h" |
| 5 |
#include "format-table.h" |
| 6 |
#include "log.h" |
| 7 |
#include "main-func.h" |
| 8 |
#include "options.h" |
| 9 |
#include "strv.h" |
| 10 |
#include "tests.h" |
| 11 |
|
| 12 |
static const char *arg_root = NULL; |
| 13 |
static int arg_flags = 0; |
| 14 |
static bool arg_open = false; |
| 15 |
|
| 16 |
static int help(void) { |
| 17 |
_cleanup_(table_unrefp) Table *options = NULL; |
| 18 |
int r; |
| 19 |
|
| 20 |
r = option_parser_get_help_table(&options); |
| 21 |
if (r < 0) |
| 22 |
return r; |
| 23 |
|
| 24 |
printf("%s [OPTIONS...] path...\n" |
| 25 |
"\nExercise chase() function on specified paths.\n\n", |
| 26 |
program_invocation_short_name); |
| 27 |
|
| 28 |
r = table_print_or_warn(options); |
| 29 |
if (r < 0) |
| 30 |
return r; |
| 31 |
|
| 32 |
return 0; |
| 33 |
} |
| 34 |
|
| 35 |
static int parse_argv(int argc, char *argv[], char ***ret_args) { |
| 36 |
assert(argc >= 0); |
| 37 |
assert(argv); |
| 38 |
assert(ret_args); |
| 39 |
|
| 40 |
OptionParser opts = { argc, argv }; |
| 41 |
|
| 42 |
FOREACH_OPTION_OR_RETURN(c, &opts) |
| 43 |
switch (c) { |
| 44 |
|
| 45 |
OPTION_COMMON_HELP: |
| 46 |
return help(); |
| 47 |
|
| 48 |
OPTION_LONG("root", "PATH", "Operate below specified root directory"): |
| 49 |
arg_root = opts.arg; |
| 50 |
break; |
| 51 |
|
| 52 |
OPTION_LONG("open", NULL, "Open the resolved path"): |
| 53 |
arg_open = true; |
| 54 |
break; |
| 55 |
|
| 56 |
OPTION_LONG_DATA("prefix-root", NULL, CHASE_PREFIX_ROOT, "Prefix path with --root"): {} |
| 57 |
OPTION_LONG_DATA("nonexistent", NULL, CHASE_NONEXISTENT, "Allow path to not exist"): {} |
| 58 |
OPTION_LONG_DATA("no-autofs", NULL, CHASE_NO_AUTOFS, "Return -EREMOTE if autofs mount point found"): {} |
| 59 |
OPTION_LONG_DATA("trigger-autofs", NULL, CHASE_TRIGGER_AUTOFS, "Trigger autofs mounts"): {} |
| 60 |
OPTION_LONG_DATA("safe", NULL, CHASE_SAFE, "Refuse privilege boundary crossings"): {} |
| 61 |
OPTION_LONG_DATA("trail-slash", NULL, CHASE_TRAIL_SLASH, "Preserve trailing slash"): {} |
| 62 |
OPTION_LONG_DATA("step", NULL, CHASE_STEP, "Execute a single normalization step"): {} |
| 63 |
OPTION_LONG_DATA("nofollow", NULL, CHASE_NOFOLLOW, "Do not follow the path's right-most component"): {} |
| 64 |
OPTION_LONG_DATA("warn", NULL, CHASE_WARN, "Emit a warning on error"): |
| 65 |
arg_flags |= opts.opt->data; |
| 66 |
break; |
| 67 |
} |
| 68 |
|
| 69 |
*ret_args = option_parser_get_args(&opts); |
| 70 |
if (strv_isempty(*ret_args)) |
| 71 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "At least one argument is required."); |
| 72 |
|
| 73 |
return 1; |
| 74 |
} |
| 75 |
|
| 76 |
static int run(int argc, char **argv) { |
| 77 |
int r; |
| 78 |
|
| 79 |
test_setup_logging(LOG_DEBUG); |
| 80 |
|
| 81 |
char **args = NULL; |
| 82 |
r = parse_argv(argc, argv, &args); |
| 83 |
if (r <= 0) |
| 84 |
return r; |
| 85 |
|
| 86 |
STRV_FOREACH(a, args) { |
| 87 |
_cleanup_free_ char *p = NULL; |
| 88 |
_cleanup_close_ int fd = -EBADF; |
| 89 |
|
| 90 |
printf("%s ", *a); |
| 91 |
fflush(stdout); |
| 92 |
|
| 93 |
r = chase(*a, arg_root, arg_flags, &p, arg_open ? &fd : NULL); |
| 94 |
if (r < 0) |
| 95 |
log_error_errno(r, "failed: %m"); |
| 96 |
else { |
| 97 |
log_info("→ %s", p); |
| 98 |
if (arg_open) |
| 99 |
assert_se(fd >= 0); |
| 100 |
else |
| 101 |
assert_se(fd == -EBADF); |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
return 0; |
| 106 |
} |
| 107 |
|
| 108 |
DEFINE_MAIN_FUNCTION(run); |