Debian Codegraph @

src/import/export.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <locale.h>
4 #include <unistd.h>
5
6 #include "sd-event.h"
7
8 #include "alloc-util.h"
9 #include "ansi-color.h"
10 #include "build.h"
11 #include "discover-image.h"
12 #include "export-raw.h"
13 #include "export-tar.h"
14 #include "fd-util.h"
15 #include "format-table.h"
16 #include "import-common.h"
17 #include "log.h"
18 #include "main-func.h"
19 #include "options.h"
20 #include "runtime-scope.h"
21 #include "signal-util.h"
22 #include "string-util.h"
23 #include "terminal-util.h"
24 #include "verbs.h"
25
26 static ImportFlags arg_import_flags = 0;
27 static Compression arg_compress = _COMPRESSION_INVALID;
28 static ImageClass arg_class = IMAGE_MACHINE;
29 static RuntimeScope arg_runtime_scope = _RUNTIME_SCOPE_INVALID;
30
31 static void determine_compression_from_filename(const char *p) {
32 if (arg_compress >= 0)
33 return;
34
35 arg_compress = p ? compression_from_filename(p) : COMPRESSION_NONE;
36 }
37
38 static void on_tar_finished(TarExport *export, int error, void *userdata) {
39 sd_event *event = userdata;
40 assert(export);
41
42 if (error == 0)
43 log_info("Operation completed successfully.");
44
45 sd_event_exit(event, ABS(error));
46 }
47
48 VERB(verb_export_tar, "tar", "NAME [FILE]", 2, 3, 0,
49 "Export a TAR image");
50 static int verb_export_tar(int argc, char *argv[], uintptr_t _data, void *userdata) {
51 _cleanup_(tar_export_unrefp) TarExport *export = NULL;
52 _cleanup_(sd_event_unrefp) sd_event *event = NULL;
53 _cleanup_(image_unrefp) Image *image = NULL;
54 const char *path = NULL, *local = NULL;
55 _cleanup_close_ int open_fd = -EBADF;
56 int r, fd;
57
58 local = argv[1];
59 if (image_name_is_valid(local)) {
60 r = image_find(arg_runtime_scope, arg_class, local, NULL, &image);
61 if (r == -ENOENT)
62 return log_error_errno(r, "Image %s not found.", local);
63 if (r < 0)
64 return log_error_errno(r, "Failed to look for image %s: %m", local);
65
66 local = image->path;
67 } else
68 local = argv[1];
69
70 if (argc >= 3)
71 path = argv[2];
72 path = empty_or_dash_to_null(path);
73
74 determine_compression_from_filename(path);
75
76 if (path) {
77 open_fd = open(path, O_WRONLY|O_CREAT|O_TRUNC|O_CLOEXEC|O_NOCTTY, 0666);
78 if (open_fd < 0)
79 return log_error_errno(errno, "Failed to open tar image for export: %m");
80
81 fd = open_fd;
82
83 log_info("Exporting '%s', saving to '%s' with compression '%s'.", local, path, compression_to_string(arg_compress));
84 } else {
85 _cleanup_free_ char *pretty = NULL;
86
87 if (isatty_safe(STDOUT_FILENO))
88 return log_error_errno(SYNTHETIC_ERRNO(EBADF), "Refusing to write archive to TTY.");
89
90 fd = STDOUT_FILENO;
91
92 (void) fd_get_path(fd, &pretty);
93 log_info("Exporting '%s', saving to '%s' with compression '%s'.", local, strna(pretty), compression_to_string(arg_compress));
94 }
95
96 r = import_allocate_event_with_signals(&event);
97 if (r < 0)
98 return r;
99
100 r = tar_export_new(&export, event, on_tar_finished, event);
101 if (r < 0)
102 return log_error_errno(r, "Failed to allocate exporter: %m");
103
104 r = tar_export_start(
105 export,
106 local,
107 fd,
108 arg_compress,
109 arg_import_flags & IMPORT_FLAGS_MASK_TAR);
110 if (r < 0)
111 return log_error_errno(r, "Failed to export image: %m");
112
113 r = sd_event_loop(event);
114 if (r < 0)
115 return log_error_errno(r, "Failed to run event loop: %m");
116
117 log_info("Exiting.");
118 return -r;
119 }
120
121 static void on_raw_finished(RawExport *export, int error, void *userdata) {
122 sd_event *event = userdata;
123 assert(export);
124
125 if (error == 0)
126 log_info("Operation completed successfully.");
127
128 sd_event_exit(event, ABS(error));
129 }
130
131 VERB(verb_export_raw, "raw", "NAME [FILE]", 2, 3, 0,
132 "Export a RAW image");
133 static int verb_export_raw(int argc, char *argv[], uintptr_t _data, void *userdata) {
134 _cleanup_(raw_export_unrefp) RawExport *export = NULL;
135 _cleanup_(sd_event_unrefp) sd_event *event = NULL;
136 _cleanup_(image_unrefp) Image *image = NULL;
137 const char *path = NULL, *local = NULL;
138 _cleanup_close_ int open_fd = -EBADF;
139 int r, fd;
140
141 local = argv[1];
142 if (image_name_is_valid(local)) {
143 r = image_find(arg_runtime_scope, arg_class, local, NULL, &image);
144 if (r == -ENOENT)
145 return log_error_errno(r, "Image %s not found.", local);
146 if (r < 0)
147 return log_error_errno(r, "Failed to look for image %s: %m", local);
148
149 local = image->path;
150 } else
151 local = argv[1];
152
153 if (argc >= 3)
154 path = argv[2];
155 path = empty_or_dash_to_null(path);
156
157 determine_compression_from_filename(path);
158
159 if (path) {
160 open_fd = open(path, O_WRONLY|O_CREAT|O_TRUNC|O_CLOEXEC|O_NOCTTY, 0666);
161 if (open_fd < 0)
162 return log_error_errno(errno, "Failed to open raw image for export: %m");
163
164 fd = open_fd;
165
166 log_info("Exporting '%s', saving to '%s' with compression '%s'.", local, path, compression_to_string(arg_compress));
167 } else {
168 _cleanup_free_ char *pretty = NULL;
169
170 fd = STDOUT_FILENO;
171
172 (void) fd_get_path(fd, &pretty);
173 log_info("Exporting '%s', saving to '%s' with compression '%s'.", local, strna(pretty), compression_to_string(arg_compress));
174 }
175
176 r = import_allocate_event_with_signals(&event);
177 if (r < 0)
178 return r;
179
180 r = raw_export_new(&export, event, on_raw_finished, event);
181 if (r < 0)
182 return log_error_errno(r, "Failed to allocate exporter: %m");
183
184 r = raw_export_start(export, local, fd, arg_compress);
185 if (r < 0)
186 return log_error_errno(r, "Failed to export image: %m");
187
188 r = sd_event_loop(event);
189 if (r < 0)
190 return log_error_errno(r, "Failed to run event loop: %m");
191
192 log_info("Exiting.");
193 return -r;
194 }
195
196 static int help(void) {
197 _cleanup_(table_unrefp) Table *options = NULL, *verbs = NULL;
198 int r;
199
200 r = verbs_get_help_table(&verbs);
201 if (r < 0)
202 return r;
203
204 r = option_parser_get_help_table(&options);
205 if (r < 0)
206 return r;
207
208 (void) table_sync_column_widths(0, verbs, options);
209
210 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
211 "%sExport disk images.%s\n"
212 "\n%sCommands:%s\n",
213 program_invocation_short_name,
214 ansi_highlight(),
215 ansi_normal(),
216 ansi_underline(),
217 ansi_normal());
218
219 r = table_print_or_warn(verbs);
220 if (r < 0)
221 return r;
222
223 printf("\n%sOptions:%s\n",
224 ansi_underline(),
225 ansi_normal());
226
227 r = table_print_or_warn(options);
228 if (r < 0)
229 return r;
230
231 return 0;
232 }
233
234 VERB_COMMON_HELP_HIDDEN(help);
235
236 static int parse_argv(int argc, char *argv[], char ***ret_args) {
237 assert(argc >= 0);
238 assert(argv);
239 assert(ret_args);
240
241 OptionParser opts = { argc, argv };
242
243 FOREACH_OPTION_OR_RETURN(c, &opts)
244 switch (c) {
245
246 OPTION_COMMON_HELP:
247 return help();
248
249 OPTION_COMMON_VERSION:
250 return version();
251
252 OPTION_LONG("format", "FORMAT", "Select format"):
253 arg_compress = compression_from_string_harder(opts.arg);
254 if (arg_compress < 0)
255 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
256 "Unknown format: %s", opts.arg);
257 break;
258
259 OPTION_LONG("class", "CLASS",
260 "Select image class (machine, sysext, confext, portable)"):
261 arg_class = image_class_from_string(opts.arg);
262 if (arg_class < 0)
263 return log_error_errno(arg_class, "Failed to parse --class= argument: %s", opts.arg);
264 break;
265
266 OPTION_COMMON_SYSTEM:
267 arg_runtime_scope = RUNTIME_SCOPE_SYSTEM;
268 break;
269
270 OPTION_COMMON_USER:
271 arg_runtime_scope = RUNTIME_SCOPE_USER;
272 break;
273 }
274
275 if (arg_runtime_scope == RUNTIME_SCOPE_USER)
276 arg_import_flags |= IMPORT_FOREIGN_UID;
277
278 *ret_args = option_parser_get_args(&opts);
279 return 1;
280 }
281
282 static int run(int argc, char *argv[]) {
283 int r;
284
285 setlocale(LC_ALL, "");
286 log_setup();
287
288 char **args = NULL;
289 r = parse_argv(argc, argv, &args);
290 if (r <= 0)
291 return r;
292
293 (void) ignore_signals(SIGPIPE);
294
295 return dispatch_verb(args, NULL);
296 }
297
298 DEFINE_MAIN_FUNCTION(run);