Debian Codegraph @

src/vpick/vpick-tool.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <dirent.h>
4
5 #include "alloc-util.h"
6 #include "architecture.h"
7 #include "build.h"
8 #include "format-table.h"
9 #include "log.h"
10 #include "main-func.h"
11 #include "options.h"
12 #include "parse-util.h"
13 #include "path-util.h"
14 #include "pretty-print.h"
15 #include "stat-util.h"
16 #include "string-table.h"
17 #include "string-util.h"
18 #include "strv.h"
19 #include "vpick.h"
20
21 typedef enum {
22 PRINT_PATH,
23 PRINT_FILENAME,
24 PRINT_VERSION,
25 PRINT_TYPE,
26 PRINT_ARCHITECTURE,
27 PRINT_TRIES,
28 PRINT_ALL,
29 _PRINT_MAX,
30 _PRINT_INVALID = -EINVAL,
31 } Print;
32
33 static char *arg_filter_basename = NULL;
34 static char *arg_filter_version = NULL;
35 static Architecture arg_filter_architecture = _ARCHITECTURE_INVALID;
36 static char *arg_filter_suffix = NULL;
37 static uint32_t arg_filter_type_mask = 0;
38 static Print arg_print = _PRINT_INVALID;
39 static PickFlags arg_flags = PICK_ARCHITECTURE|PICK_TRIES;
40
41 STATIC_DESTRUCTOR_REGISTER(arg_filter_basename, freep);
42 STATIC_DESTRUCTOR_REGISTER(arg_filter_version, freep);
43 STATIC_DESTRUCTOR_REGISTER(arg_filter_suffix, freep);
44
45 static const char *print_table[_PRINT_MAX] = {
46 [PRINT_PATH] = "path",
47 [PRINT_FILENAME] = "filename",
48 [PRINT_VERSION] = "version",
49 [PRINT_TYPE] = "type",
50 [PRINT_ARCHITECTURE] = "architecture",
51 [PRINT_TRIES] = "tries",
52 [PRINT_ALL] = "all",
53 };
54
55 DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING(print, Print);
56
57 static int help(void) {
58 _cleanup_free_ char *link = NULL;
59 _cleanup_(table_unrefp) Table *lookup_keys = NULL, *output = NULL;
60 int r;
61
62 r = terminal_urlify_man("systemd-vpick", "1", &link);
63 if (r < 0)
64 return log_oom();
65
66 r = option_parser_get_help_table(&lookup_keys);
67 if (r < 0)
68 return r;
69
70 r = option_parser_get_help_table_group("Output", &output);
71 if (r < 0)
72 return r;
73
74 (void) table_sync_column_widths(0, lookup_keys, output);
75
76 printf("%s [OPTIONS...] PATH...\n"
77 "\n%sPick entry from versioned directory.%s\n",
78 program_invocation_short_name,
79 ansi_highlight(),
80 ansi_normal());
81
82 printf("\n%sLookup Keys:%s\n", ansi_underline(), ansi_normal());
83 r = table_print_or_warn(lookup_keys);
84 if (r < 0)
85 return r;
86
87 printf("\n%sOutput:%s\n", ansi_underline(), ansi_normal());
88 r = table_print_or_warn(output);
89 if (r < 0)
90 return r;
91
92 printf("\nSee the %s for details.\n", link);
93 return 0;
94 }
95
96 static int parse_argv(int argc, char *argv[], char ***ret_args) {
97 int r;
98
99 assert(argc >= 0);
100 assert(argv);
101
102 OptionParser opts = { argc, argv };
103
104 FOREACH_OPTION_OR_RETURN(c, &opts)
105 switch (c) {
106
107 OPTION('B', "basename", "BASENAME", "Look for specified basename"):
108 if (!filename_part_is_valid(opts.arg))
109 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid basename string: %s", opts.arg);
110
111 r = free_and_strdup_warn(&arg_filter_basename, opts.arg);
112 if (r < 0)
113 return r;
114
115 break;
116
117 OPTION_SHORT('V', "VERSION", "Look for specified version"):
118 if (!version_is_valid(opts.arg))
119 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid version string: %s", opts.arg);
120
121 r = free_and_strdup_warn(&arg_filter_version, opts.arg);
122 if (r < 0)
123 return r;
124
125 break;
126
127 OPTION_SHORT('A', "ARCH", "Look for specified architecture"):
128 if (streq(opts.arg, "native"))
129 arg_filter_architecture = native_architecture();
130 else if (streq(opts.arg, "secondary")) {
131 #ifdef ARCHITECTURE_SECONDARY
132 arg_filter_architecture = ARCHITECTURE_SECONDARY;
133 #else
134 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "Local architecture has no secondary architecture.");
135 #endif
136 } else if (streq(opts.arg, "uname"))
137 arg_filter_architecture = uname_architecture();
138 else if (streq(opts.arg, "auto"))
139 arg_filter_architecture = _ARCHITECTURE_INVALID;
140 else {
141 arg_filter_architecture = architecture_from_string(opts.arg);
142 if (arg_filter_architecture < 0)
143 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Unknown architecture: %s", opts.arg);
144 }
145 break;
146
147 OPTION('S', "suffix", "SUFFIX", "Look for specified suffix"):
148 if (!filename_part_is_valid(opts.arg))
149 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid suffix string: %s", opts.arg);
150
151 r = free_and_strdup_warn(&arg_filter_suffix, opts.arg);
152 if (r < 0)
153 return r;
154
155 break;
156
157 OPTION('t', "type", "TYPE", "Look for specified inode type"):
158 if (isempty(opts.arg))
159 arg_filter_type_mask = 0;
160 else {
161 mode_t m;
162
163 m = inode_type_from_string(opts.arg);
164 if (m == MODE_INVALID)
165 return log_error_errno(m, "Unknown inode type: %s", opts.arg);
166
167 arg_filter_type_mask |= UINT32_C(1) << IFTODT(m);
168 }
169
170 break;
171
172 OPTION_GROUP("Output"): {}
173
174 OPTION_COMMON_HELP:
175 return help();
176
177 OPTION_COMMON_VERSION:
178 return version();
179
180 OPTION('p', "print", "WHAT",
181 "Print selected WHAT rather than path"): {}
182 OPTION_LONG_FLAGS(OPTION_HELP_ENTRY, "print", "filename",
183 "... print selected filename"): {}
184 OPTION_LONG_FLAGS(OPTION_HELP_ENTRY, "print", "version",
185 "... print selected version"): {}
186 OPTION_LONG_FLAGS(OPTION_HELP_ENTRY, "print", "type",
187 "... print selected inode type"): {}
188 OPTION_LONG_FLAGS(OPTION_HELP_ENTRY, "print", "arch",
189 "... print selected architecture"): {}
190 OPTION_LONG_FLAGS(OPTION_HELP_ENTRY, "print", "tries",
191 "... print selected tries left/tries done"): {}
192 OPTION_LONG_FLAGS(OPTION_HELP_ENTRY, "print", "all",
193 "... print all of the above"):
194
195 if (streq(opts.arg, "arch")) /* accept abbreviation too */
196 arg_print = PRINT_ARCHITECTURE;
197 else
198 arg_print = print_from_string(opts.arg);
199 if (arg_print < 0)
200 return log_error_errno(arg_print, "Unknown --print= argument: %s", opts.arg);
201
202 break;
203
204 OPTION_LONG("resolve", "BOOL", "Canonicalize the result path"):
205 r = parse_boolean(opts.arg);
206 if (r < 0)
207 return log_error_errno(r, "Failed to parse --resolve= value: %m");
208
209 SET_FLAG(arg_flags, PICK_RESOLVE, r);
210 break;
211 }
212
213 if (arg_print < 0)
214 arg_print = PRINT_PATH;
215
216 *ret_args = option_parser_get_args(&opts);
217 return 1;
218 }
219
220 static int run(int argc, char *argv[]) {
221 int r;
222
223 log_setup();
224
225 char **args = NULL;
226 r = parse_argv(argc, argv, &args);
227 if (r <= 0)
228 return r;
229
230 if (strv_isempty(args))
231 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Path to resolve must be specified.");
232
233 STRV_FOREACH(i, args) {
234 _cleanup_free_ char *p = NULL;
235 r = path_make_absolute_cwd(*i, &p);
236 if (r < 0)
237 return log_error_errno(r, "Failed to make path '%s' absolute: %m", *i);
238
239 _cleanup_(pick_result_done) PickResult result = PICK_RESULT_NULL;
240 r = path_pick(/* toplevel_path= */ NULL,
241 /* toplevel_fd= */ AT_FDCWD,
242 p,
243 &(PickFilter) {
244 .basename = arg_filter_basename,
245 .version = arg_filter_version,
246 .architecture = arg_filter_architecture,
247 .suffix = arg_filter_suffix,
248 .type_mask = arg_filter_type_mask,
249 },
250 /* n_filters= */ 1,
251 arg_flags,
252 &result);
253 if (r < 0)
254 return log_error_errno(r, "Failed to pick version for '%s': %m", p);
255 if (r == 0)
256 return log_error_errno(SYNTHETIC_ERRNO(ENOENT), "No matching version for '%s' found.", p);
257
258 switch (arg_print) {
259
260 case PRINT_PATH:
261 fputs(result.path, stdout);
262 if (result.st.st_mode != MODE_INVALID && S_ISDIR(result.st.st_mode) && !endswith(result.path, "/"))
263 fputc('/', stdout);
264 fputc('\n', stdout);
265 break;
266
267 case PRINT_FILENAME: {
268 _cleanup_free_ char *fname = NULL;
269
270 r = path_extract_filename(result.path, &fname);
271 if (r < 0)
272 return log_error_errno(r, "Failed to extract filename from path '%s': %m", result.path);
273
274 puts(fname);
275 break;
276 }
277
278 case PRINT_VERSION:
279 if (!result.version)
280 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "No version information discovered.");
281
282 puts(result.version);
283 break;
284
285 case PRINT_TYPE:
286 if (result.st.st_mode == MODE_INVALID)
287 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "No inode type information discovered.");
288
289 puts(inode_type_to_string(result.st.st_mode));
290 break;
291
292 case PRINT_ARCHITECTURE:
293 if (result.architecture < 0)
294 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "No architecture information discovered.");
295
296 puts(architecture_to_string(result.architecture));
297 break;
298
299 case PRINT_TRIES:
300 if (result.tries_left == UINT_MAX)
301 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "No tries left/tries done information discovered.");
302
303 printf("+%u-%u", result.tries_left, result.tries_done);
304 break;
305
306 case PRINT_ALL: {
307 _cleanup_(table_unrefp) Table *t = NULL;
308
309 t = table_new_vertical();
310 if (!t)
311 return log_oom();
312
313 table_set_ersatz_string(t, TABLE_ERSATZ_NA);
314
315 r = table_add_many(
316 t,
317 TABLE_FIELD, "Path",
318 TABLE_PATH, result.path,
319 TABLE_FIELD, "Version",
320 TABLE_STRING, result.version,
321 TABLE_FIELD, "Type",
322 TABLE_STRING, result.st.st_mode == MODE_INVALID ? NULL : inode_type_to_string(result.st.st_mode),
323 TABLE_FIELD, "Architecture",
324 TABLE_STRING, result.architecture < 0 ? NULL : architecture_to_string(result.architecture));
325 if (r < 0)
326 return table_log_add_error(r);
327
328 if (result.tries_left != UINT_MAX) {
329 r = table_add_many(
330 t,
331 TABLE_FIELD, "Tries left",
332 TABLE_UINT, result.tries_left,
333 TABLE_FIELD, "Tries done",
334 TABLE_UINT, result.tries_done);
335 if (r < 0)
336 return table_log_add_error(r);
337 }
338
339 r = table_print_or_warn(t);
340 if (r < 0)
341 return r;
342
343 break;
344 }
345
346 default:
347 assert_not_reached();
348 }
349 }
350
351 return 0;
352 }
353
354 DEFINE_MAIN_FUNCTION(run);