]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/import/import-tar.c
b4b9b8dc99789f0e0c86a564e13568caecf1303b
[thirdparty/systemd.git] / src / import / import-tar.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <sys/stat.h>
4
5 #include "sd-daemon.h"
6 #include "sd-event.h"
7
8 #include "alloc-util.h"
9 #include "btrfs-util.h"
10 #include "errno-util.h"
11 #include "fd-util.h"
12 #include "format-util.h"
13 #include "import-common.h"
14 #include "import-compress.h"
15 #include "import-tar.h"
16 #include "import-util.h"
17 #include "install-file.h"
18 #include "io-util.h"
19 #include "log.h"
20 #include "mkdir-label.h"
21 #include "path-util.h"
22 #include "pretty-print.h"
23 #include "process-util.h"
24 #include "ratelimit.h"
25 #include "rm-rf.h"
26 #include "string-util.h"
27 #include "terminal-util.h"
28 #include "time-util.h"
29 #include "tmpfile-util.h"
30
31 typedef struct TarImport {
32 sd_event *event;
33
34 char *image_root;
35
36 TarImportFinished on_finished;
37 void *userdata;
38
39 char *local;
40 ImportFlags flags;
41
42 char *temp_path;
43 char *final_path;
44
45 int input_fd;
46 int tar_fd;
47
48 ImportCompress compress;
49
50 sd_event_source *input_event_source;
51
52 uint8_t buffer[16*1024];
53 size_t buffer_size;
54
55 uint64_t written_compressed;
56 uint64_t written_uncompressed;
57
58 struct stat input_stat;
59
60 pid_t tar_pid;
61
62 unsigned last_percent;
63 RateLimit progress_ratelimit;
64 } TarImport;
65
66 TarImport* tar_import_unref(TarImport *i) {
67 if (!i)
68 return NULL;
69
70 sd_event_source_unref(i->input_event_source);
71
72 if (i->tar_pid > 1)
73 sigkill_wait(i->tar_pid);
74
75 rm_rf_subvolume_and_free(i->temp_path);
76
77 import_compress_free(&i->compress);
78
79 sd_event_unref(i->event);
80
81 safe_close(i->tar_fd);
82
83 free(i->final_path);
84 free(i->image_root);
85 free(i->local);
86 return mfree(i);
87 }
88
89 int tar_import_new(
90 TarImport **ret,
91 sd_event *event,
92 const char *image_root,
93 TarImportFinished on_finished,
94 void *userdata) {
95
96 _cleanup_(tar_import_unrefp) TarImport *i = NULL;
97 _cleanup_free_ char *root = NULL;
98 int r;
99
100 assert(ret);
101 assert(image_root);
102
103 root = strdup(image_root);
104 if (!root)
105 return -ENOMEM;
106
107 i = new(TarImport, 1);
108 if (!i)
109 return -ENOMEM;
110
111 *i = (TarImport) {
112 .input_fd = -EBADF,
113 .tar_fd = -EBADF,
114 .on_finished = on_finished,
115 .userdata = userdata,
116 .last_percent = UINT_MAX,
117 .image_root = TAKE_PTR(root),
118 .progress_ratelimit = { 100 * USEC_PER_MSEC, 1 },
119 };
120
121 if (event)
122 i->event = sd_event_ref(event);
123 else {
124 r = sd_event_default(&i->event);
125 if (r < 0)
126 return r;
127 }
128
129 *ret = TAKE_PTR(i);
130
131 return 0;
132 }
133
134 static void tar_import_report_progress(TarImport *i) {
135 unsigned percent;
136 assert(i);
137
138 /* We have no size information, unless the source is a regular file */
139 if (!S_ISREG(i->input_stat.st_mode))
140 return;
141
142 if (i->written_compressed >= (uint64_t) i->input_stat.st_size)
143 percent = 100;
144 else
145 percent = (unsigned) ((i->written_compressed * UINT64_C(100)) / (uint64_t) i->input_stat.st_size);
146
147 if (percent == i->last_percent)
148 return;
149
150 if (!ratelimit_below(&i->progress_ratelimit))
151 return;
152
153 sd_notifyf(false, "X_IMPORT_PROGRESS=%u%%", percent);
154
155 if (isatty_safe(STDERR_FILENO))
156 (void) draw_progress_barf(
157 percent,
158 "%s %s/%s",
159 glyph(GLYPH_ARROW_RIGHT),
160 FORMAT_BYTES(i->written_compressed),
161 FORMAT_BYTES(i->input_stat.st_size));
162 else
163 log_info("Imported %u%%.", percent);
164
165 i->last_percent = percent;
166 }
167
168 static int tar_import_finish(TarImport *i) {
169 const char *d;
170 int r;
171
172 assert(i);
173 assert(i->tar_fd >= 0);
174
175 i->tar_fd = safe_close(i->tar_fd);
176
177 if (i->tar_pid > 0) {
178 r = wait_for_terminate_and_check("tar", TAKE_PID(i->tar_pid), WAIT_LOG);
179 if (r < 0)
180 return r;
181 if (r != EXIT_SUCCESS)
182 return -EPROTO;
183 }
184
185 assert_se(d = i->temp_path ?: i->local);
186
187 r = import_mangle_os_tree(d);
188 if (r < 0)
189 return r;
190
191 r = install_file(
192 AT_FDCWD, d,
193 AT_FDCWD, i->final_path,
194 (i->flags & IMPORT_FORCE ? INSTALL_REPLACE : 0) |
195 (i->flags & IMPORT_READ_ONLY ? INSTALL_READ_ONLY : 0) |
196 (i->flags & IMPORT_SYNC ? INSTALL_SYNCFS : 0));
197 if (r < 0)
198 return log_error_errno(r, "Failed to move '%s' into place: %m", i->final_path ?: i->local);
199
200 i->temp_path = mfree(i->temp_path);
201
202 return 0;
203 }
204
205 static int tar_import_fork_tar(TarImport *i) {
206 const char *d, *root;
207 int r;
208
209 assert(i);
210 assert(i->local);
211 assert(!i->final_path);
212 assert(!i->temp_path);
213 assert(i->tar_fd < 0);
214
215 if (i->flags & IMPORT_DIRECT) {
216 d = i->local;
217 root = NULL;
218 } else {
219 i->final_path = path_join(i->image_root, i->local);
220 if (!i->final_path)
221 return log_oom();
222
223 r = tempfn_random(i->final_path, NULL, &i->temp_path);
224 if (r < 0)
225 return log_oom();
226
227 d = i->temp_path;
228 root = i->image_root;
229 }
230
231 assert(d);
232
233 (void) mkdir_parents_label(d, 0700);
234
235 if (FLAGS_SET(i->flags, IMPORT_DIRECT|IMPORT_FORCE))
236 (void) rm_rf(d, REMOVE_ROOT|REMOVE_PHYSICAL|REMOVE_SUBVOLUME);
237
238 if (i->flags & IMPORT_BTRFS_SUBVOL)
239 r = btrfs_subvol_make_fallback(AT_FDCWD, d, 0755);
240 else
241 r = RET_NERRNO(mkdir(d, 0755));
242 if (r == -EEXIST && (i->flags & IMPORT_DIRECT)) /* EEXIST is OK if in direct mode, but not otherwise,
243 * because in that case our temporary path collided */
244 r = 0;
245 if (r < 0)
246 return log_error_errno(r, "Failed to create directory/subvolume %s: %m", d);
247 if (r > 0 && (i->flags & IMPORT_BTRFS_QUOTA)) { /* actually btrfs subvol */
248 if (!(i->flags & IMPORT_DIRECT))
249 (void) import_assign_pool_quota_and_warn(root);
250 (void) import_assign_pool_quota_and_warn(d);
251 }
252
253 i->tar_fd = import_fork_tar_x(d, &i->tar_pid);
254 if (i->tar_fd < 0)
255 return i->tar_fd;
256
257 return 0;
258 }
259
260 static int tar_import_write(const void *p, size_t sz, void *userdata) {
261 TarImport *i = userdata;
262 int r;
263
264 r = loop_write(i->tar_fd, p, sz);
265 if (r < 0)
266 return r;
267
268 i->written_uncompressed += sz;
269
270 return 0;
271 }
272
273 static int tar_import_process(TarImport *i) {
274 ssize_t l;
275 int r;
276
277 assert(i);
278 assert(i->buffer_size < sizeof(i->buffer));
279
280 l = read(i->input_fd, i->buffer + i->buffer_size, sizeof(i->buffer) - i->buffer_size);
281 if (l < 0) {
282 if (errno == EAGAIN)
283 return 0;
284
285 r = log_error_errno(errno, "Failed to read input file: %m");
286 goto finish;
287 }
288
289 if ((size_t) l > sizeof(i->buffer) - i->buffer_size) {
290 r = log_error_errno(SYNTHETIC_ERRNO(EBADMSG), "Read input file exceeded maximum size.");
291 goto finish;
292 }
293
294 i->buffer_size += l;
295
296 if (i->compress.type == IMPORT_COMPRESS_UNKNOWN) {
297
298 if (l == 0) { /* EOF */
299 log_debug("File too short to be compressed, as no compression signature fits in, thus assuming uncompressed.");
300 import_uncompress_force_off(&i->compress);
301 } else {
302 r = import_uncompress_detect(&i->compress, i->buffer, i->buffer_size);
303 if (r < 0) {
304 log_error_errno(r, "Failed to detect file compression: %m");
305 goto finish;
306 }
307 if (r == 0) /* Need more data */
308 return 0;
309 }
310
311 r = tar_import_fork_tar(i);
312 if (r < 0)
313 goto finish;
314 }
315
316 r = import_uncompress(&i->compress, i->buffer, i->buffer_size, tar_import_write, i);
317 if (r < 0) {
318 log_error_errno(r, "Failed to decode and write: %m");
319 goto finish;
320 }
321
322 i->written_compressed += i->buffer_size;
323 i->buffer_size = 0;
324
325 if (l == 0) { /* EOF */
326 r = tar_import_finish(i);
327 goto finish;
328 }
329
330 tar_import_report_progress(i);
331
332 return 0;
333
334 finish:
335 if (r >= 0 && isatty_safe(STDERR_FILENO))
336 clear_progress_bar(/* prefix= */ NULL);
337
338 if (i->on_finished)
339 i->on_finished(i, r, i->userdata);
340 else
341 sd_event_exit(i->event, r);
342
343 return 0;
344 }
345
346 static int tar_import_on_input(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
347 TarImport *i = userdata;
348
349 return tar_import_process(i);
350 }
351
352 static int tar_import_on_defer(sd_event_source *s, void *userdata) {
353 TarImport *i = userdata;
354
355 return tar_import_process(i);
356 }
357
358 int tar_import_start(TarImport *i, int fd, const char *local, ImportFlags flags) {
359 int r;
360
361 assert(i);
362 assert(fd >= 0);
363 assert(local);
364 assert(!(flags & ~IMPORT_FLAGS_MASK_TAR));
365
366 if (!import_validate_local(local, flags))
367 return -EINVAL;
368
369 if (i->input_fd >= 0)
370 return -EBUSY;
371
372 r = fd_nonblock(fd, true);
373 if (r < 0)
374 return r;
375
376 r = free_and_strdup(&i->local, local);
377 if (r < 0)
378 return r;
379
380 i->flags = flags;
381
382 if (fstat(fd, &i->input_stat) < 0)
383 return -errno;
384
385 r = sd_event_add_io(i->event, &i->input_event_source, fd, EPOLLIN, tar_import_on_input, i);
386 if (r == -EPERM) {
387 /* This fd does not support epoll, for example because it is a regular file. Busy read in that case */
388 r = sd_event_add_defer(i->event, &i->input_event_source, tar_import_on_defer, i);
389 if (r < 0)
390 return r;
391
392 r = sd_event_source_set_enabled(i->input_event_source, SD_EVENT_ON);
393 }
394 if (r < 0)
395 return r;
396
397 i->input_fd = fd;
398 return 0;
399 }