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