Debian Codegraph @

src/escape/escape-tool.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <stdio.h>
4
5 #include "alloc-util.h"
6 #include "build.h"
7 #include "format-table.h"
8 #include "log.h"
9 #include "main-func.h"
10 #include "options.h"
11 #include "path-util.h"
12 #include "pretty-print.h"
13 #include "string-util.h"
14 #include "strv.h"
15 #include "unit-def.h"
16 #include "unit-name.h"
17
18 static enum {
19 ACTION_ESCAPE,
20 ACTION_UNESCAPE,
21 ACTION_MANGLE
22 } arg_action = ACTION_ESCAPE;
23 static const char *arg_suffix = NULL;
24 static const char *arg_template = NULL;
25 static bool arg_path = false;
26 static bool arg_instance = false;
27
28 static int help(void) {
29 _cleanup_free_ char *link = NULL;
30 _cleanup_(table_unrefp) Table *options = NULL;
31 int r;
32
33 r = terminal_urlify_man("systemd-escape", "1", &link);
34 if (r < 0)
35 return log_oom();
36
37 r = option_parser_get_help_table(&options);
38 if (r < 0)
39 return r;
40
41 printf("%s [OPTIONS...] [NAME...]\n\n"
42 "%sEscape strings for usage in systemd unit names.%s\n\n",
43 program_invocation_short_name,
44 ansi_highlight(),
45 ansi_normal());
46
47 r = table_print_or_warn(options);
48 if (r < 0)
49 return r;
50
51 printf("\nSee the %s for details.\n", link);
52 return 0;
53 }
54
55 static int parse_argv(int argc, char *argv[], char ***ret_args) {
56 assert(argc >= 0);
57 assert(argv);
58
59 OptionParser opts = { argc, argv };
60
61 FOREACH_OPTION_OR_RETURN(c, &opts)
62 switch (c) {
63
64 OPTION_COMMON_HELP:
65 return help();
66
67 OPTION_COMMON_VERSION:
68 return version();
69
70 OPTION_LONG("suffix", "SUFFIX", "Unit suffix to append to escaped strings"): {
71 UnitType t = unit_type_from_string(opts.arg);
72 if (t < 0)
73 return log_error_errno(t, "Invalid unit suffix type \"%s\".", opts.arg);
74
75 arg_suffix = opts.arg;
76 break;
77 }
78
79 OPTION_LONG("template", "TEMPLATE", "Insert strings as instance into template"):
80 if (!unit_name_is_valid(opts.arg, UNIT_NAME_TEMPLATE))
81 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
82 "Template name %s is not valid.", opts.arg);
83
84 arg_template = opts.arg;
85 break;
86
87 OPTION_LONG("instance", NULL, "With --unescape, show just the instance part"):
88 arg_instance = true;
89 break;
90
91 OPTION('u', "unescape", NULL, "Unescape strings"):
92 arg_action = ACTION_UNESCAPE;
93 break;
94
95 OPTION('m', "mangle", NULL, "Mangle strings"):
96 arg_action = ACTION_MANGLE;
97 break;
98
99 OPTION('p', "path", NULL,
100 "When escaping/unescaping assume the string is a path"):
101 arg_path = true;
102 break;
103 }
104
105 if (option_parser_get_n_args(&opts) == 0)
106 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
107 "Not enough arguments.");
108
109 if (arg_template && arg_suffix)
110 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
111 "--suffix= and --template= may not be combined.");
112
113 if ((arg_template || arg_suffix) && arg_action == ACTION_MANGLE)
114 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
115 "--suffix= and --template= are not compatible with --mangle.");
116
117 if (arg_suffix && arg_action == ACTION_UNESCAPE)
118 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
119 "--suffix is not compatible with --unescape.");
120
121 if (arg_path && !IN_SET(arg_action, ACTION_ESCAPE, ACTION_UNESCAPE))
122 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
123 "--path may not be combined with --mangle.");
124
125 if (arg_instance && arg_action != ACTION_UNESCAPE)
126 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
127 "--instance must be used in conjunction with --unescape.");
128
129 if (arg_instance && arg_template)
130 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
131 "--instance may not be combined with --template.");
132
133 *ret_args = option_parser_get_args(&opts);
134 return 1;
135 }
136
137 static int run(int argc, char *argv[]) {
138 int r;
139
140 log_setup();
141
142 char **args = NULL;
143 r = parse_argv(argc, argv, &args);
144 if (r <= 0)
145 return r;
146
147 STRV_FOREACH(i, args) {
148 _cleanup_free_ char *e = NULL;
149
150 switch (arg_action) {
151
152 case ACTION_ESCAPE:
153 if (arg_path) {
154 r = unit_name_path_escape(*i, &e);
155 if (r == -EINVAL) {
156 /* If escaping failed because the string was invalid, let's print a
157 * friendly message about it. Catch these specific error cases
158 * explicitly. */
159
160 if (!path_is_valid(*i))
161 return log_error_errno(r, "Input '%s' is not a valid file system path, failed to escape.", *i);
162 if (!path_is_absolute(*i))
163 return log_error_errno(r, "Input '%s' is not an absolute file system path, failed to escape.", *i);
164 if (!path_is_normalized(*i))
165 return log_error_errno(r, "Input '%s' is not a normalized file system path, failed to escape.", *i);
166 }
167 if (r < 0)
168 /* All other error cases. */
169 return log_error_errno(r, "Failed to escape string: %m");
170
171 /* If the escaping worked, then still warn if the path is not like we'd like
172 * it. Because that means escaping is not necessarily reversible. */
173
174 if (!path_is_valid(*i))
175 log_warning("Input '%s' is not a valid file system path, escaping is likely not going to be reversible.", *i);
176 else if (!path_is_absolute(*i))
177 log_warning("Input '%s' is not an absolute file system path, escaping is likely not going to be reversible.", *i);
178
179 /* Note that we don't complain about paths not being normalized here, because
180 * some forms of non-normalization is actually OK, such as a series // and
181 * unit_name_path_escape() will clean those up silently, and the reversal is
182 * "close enough" to be OK. */
183 } else {
184 e = unit_name_escape(*i);
185 if (!e)
186 return log_oom();
187 }
188
189 if (arg_template) {
190 char *x;
191
192 r = unit_name_replace_instance(arg_template, e, &x);
193 if (r < 0)
194 return log_error_errno(r, "Failed to replace instance: %m");
195
196 free_and_replace(e, x);
197 } else if (arg_suffix) {
198 if (!strextend(&e, ".", arg_suffix))
199 return log_oom();
200 }
201
202 break;
203
204 case ACTION_UNESCAPE: {
205 _cleanup_free_ char *name = NULL;
206
207 if (arg_template || arg_instance) {
208 _cleanup_free_ char *template = NULL;
209
210 r = unit_name_to_instance(*i, &name);
211 if (r < 0)
212 return log_error_errno(r, "Failed to extract instance: %m");
213 if (isempty(name))
214 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
215 "Unit %s is missing the instance name.", *i);
216
217 r = unit_name_template(*i, &template);
218 if (r < 0)
219 return log_error_errno(r, "Failed to extract template: %m");
220 if (arg_template && !streq(arg_template, template))
221 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
222 "Unit %s template %s does not match specified template %s.",
223 *i, template, arg_template);
224 } else {
225 name = strdup(*i);
226 if (!name)
227 return log_oom();
228 }
229
230 if (arg_path)
231 r = unit_name_path_unescape(name, &e);
232 else
233 r = unit_name_unescape(name, &e);
234 if (r < 0)
235 return log_error_errno(r, "Failed to unescape string: %m");
236
237 break;
238 }
239
240 case ACTION_MANGLE:
241 r = unit_name_mangle(*i, 0, &e);
242 if (r < 0)
243 return log_error_errno(r, "Failed to mangle name: %m");
244
245 break;
246 }
247
248 if (i != args)
249 fputc(' ', stdout);
250
251 fputs(e, stdout);
252 }
253
254 fputc('\n', stdout);
255
256 return 0;
257 }
258
259 DEFINE_MAIN_FUNCTION(run);