]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/import/import-tar.c
core/scope: drop effectively unused unit_watch_pidref() calls (#38186)
[thirdparty/systemd.git] / src / import / import-tar.c
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
b6e676ce 2
9412e9e9
DDM
3#include <sys/stat.h>
4
b6e676ce
LP
5#include "sd-daemon.h"
6#include "sd-event.h"
07630cea 7
b5efdb8a 8#include "alloc-util.h"
b6e676ce 9#include "btrfs-util.h"
dc7b1512 10#include "errno-util.h"
3ffd4af2 11#include "fd-util.h"
9412e9e9 12#include "format-util.h"
07630cea
LP
13#include "import-common.h"
14#include "import-compress.h"
3ffd4af2 15#include "import-tar.h"
401a22bf 16#include "import-util.h"
d32a5841 17#include "install-file.h"
c004493c 18#include "io-util.h"
93a1f792 19#include "log.h"
35cd0ba5 20#include "mkdir-label.h"
07630cea 21#include "path-util.h"
3dd0389b 22#include "pretty-print.h"
07630cea 23#include "process-util.h"
07630cea
LP
24#include "ratelimit.h"
25#include "rm-rf.h"
26#include "string-util.h"
9412e9e9
DDM
27#include "terminal-util.h"
28#include "time-util.h"
e4de7287 29#include "tmpfile-util.h"
b6e676ce 30
9412e9e9 31typedef struct TarImport {
b6e676ce
LP
32 sd_event *event;
33
34 char *image_root;
35
36 TarImportFinished on_finished;
37 void *userdata;
38
39 char *local;
5183c50a 40 ImportFlags flags;
b6e676ce
LP
41
42 char *temp_path;
43 char *final_path;
44
45 int input_fd;
46 int tar_fd;
47
48 ImportCompress compress;
49
b6e676ce
LP
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
d32a5841 58 struct stat input_stat;
b6e676ce
LP
59
60 pid_t tar_pid;
61
62 unsigned last_percent;
5ac1530e 63 RateLimit progress_ratelimit;
9412e9e9 64} TarImport;
b6e676ce
LP
65
66TarImport* tar_import_unref(TarImport *i) {
67 if (!i)
68 return NULL;
69
587fec42 70 sd_event_source_unref(i->input_event_source);
b6e676ce 71
7950211d
LP
72 if (i->tar_pid > 1)
73 sigkill_wait(i->tar_pid);
b6e676ce 74
0dfb6503 75 rm_rf_subvolume_and_free(i->temp_path);
b6e676ce
LP
76
77 import_compress_free(&i->compress);
78
587fec42 79 sd_event_unref(i->event);
b6e676ce
LP
80
81 safe_close(i->tar_fd);
82
83 free(i->final_path);
84 free(i->image_root);
85 free(i->local);
6b430fdb 86 return mfree(i);
b6e676ce
LP
87}
88
89int 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;
0d94088e 97 _cleanup_free_ char *root = NULL;
b6e676ce
LP
98 int r;
99
100 assert(ret);
7af5785d 101 assert(image_root);
b6e676ce 102
7af5785d 103 root = strdup(image_root);
0d94088e 104 if (!root)
b6e676ce
LP
105 return -ENOMEM;
106
0d94088e
YW
107 i = new(TarImport, 1);
108 if (!i)
b6e676ce
LP
109 return -ENOMEM;
110
0d94088e 111 *i = (TarImport) {
254d1313
ZJS
112 .input_fd = -EBADF,
113 .tar_fd = -EBADF,
0d94088e
YW
114 .on_finished = on_finished,
115 .userdata = userdata,
f5fbe71d 116 .last_percent = UINT_MAX,
0d94088e 117 .image_root = TAKE_PTR(root),
5ac1530e 118 .progress_ratelimit = { 100 * USEC_PER_MSEC, 1 },
0d94088e
YW
119 };
120
b6e676ce
LP
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
1cc6c93a 129 *ret = TAKE_PTR(i);
b6e676ce
LP
130
131 return 0;
132}
133
134static 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 */
d32a5841 139 if (!S_ISREG(i->input_stat.st_mode))
b6e676ce
LP
140 return;
141
d32a5841 142 if (i->written_compressed >= (uint64_t) i->input_stat.st_size)
b6e676ce
LP
143 percent = 100;
144 else
d32a5841 145 percent = (unsigned) ((i->written_compressed * UINT64_C(100)) / (uint64_t) i->input_stat.st_size);
b6e676ce
LP
146
147 if (percent == i->last_percent)
148 return;
149
5ac1530e 150 if (!ratelimit_below(&i->progress_ratelimit))
b6e676ce
LP
151 return;
152
a986de68 153 sd_notifyf(false, "X_IMPORT_PROGRESS=%u%%", percent);
3dd0389b 154
91d64043
LP
155 if (isatty_safe(STDERR_FILENO))
156 (void) draw_progress_barf(
157 percent,
158 "%s %s/%s",
1ae9b0cf 159 glyph(GLYPH_ARROW_RIGHT),
91d64043
LP
160 FORMAT_BYTES(i->written_compressed),
161 FORMAT_BYTES(i->input_stat.st_size));
162 else
3dd0389b 163 log_info("Imported %u%%.", percent);
b6e676ce
LP
164
165 i->last_percent = percent;
166}
167
168static int tar_import_finish(TarImport *i) {
d32a5841 169 const char *d;
b6e676ce
LP
170 int r;
171
172 assert(i);
173 assert(i->tar_fd >= 0);
b6e676ce
LP
174
175 i->tar_fd = safe_close(i->tar_fd);
176
177 if (i->tar_pid > 0) {
8f03de53 178 r = wait_for_terminate_and_check("tar", TAKE_PID(i->tar_pid), WAIT_LOG);
b6e676ce
LP
179 if (r < 0)
180 return r;
d02bfa50
LP
181 if (r != EXIT_SUCCESS)
182 return -EPROTO;
b6e676ce
LP
183 }
184
d32a5841
LP
185 assert_se(d = i->temp_path ?: i->local);
186
187 r = import_mangle_os_tree(d);
e21b7229
LP
188 if (r < 0)
189 return r;
190
d32a5841
LP
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));
f85ef957 197 if (r < 0)
d32a5841 198 return log_error_errno(r, "Failed to move '%s' into place: %m", i->final_path ?: i->local);
b6e676ce 199
a1e58e8e 200 i->temp_path = mfree(i->temp_path);
b6e676ce
LP
201
202 return 0;
203}
204
205static int tar_import_fork_tar(TarImport *i) {
d32a5841 206 const char *d, *root;
b6e676ce
LP
207 int r;
208
209 assert(i);
d32a5841 210 assert(i->local);
b6e676ce
LP
211 assert(!i->final_path);
212 assert(!i->temp_path);
213 assert(i->tar_fd < 0);
214
d32a5841
LP
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();
b6e676ce 222
d32a5841
LP
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);
b6e676ce 232
d32a5841 233 (void) mkdir_parents_label(d, 0700);
b6e676ce 234
d32a5841
LP
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)
e54c79cc 239 r = btrfs_subvol_make_fallback(AT_FDCWD, d, 0755);
d32a5841 240 else
7c248223 241 r = RET_NERRNO(mkdir(d, 0755));
d32a5841
LP
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;
82c4440d 245 if (r < 0)
d32a5841
LP
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);
052ba0eb 251 }
b6e676ce 252
d32a5841 253 i->tar_fd = import_fork_tar_x(d, &i->tar_pid);
b6e676ce
LP
254 if (i->tar_fd < 0)
255 return i->tar_fd;
256
257 return 0;
258}
259
260static int tar_import_write(const void *p, size_t sz, void *userdata) {
261 TarImport *i = userdata;
262 int r;
263
e22c60a9 264 r = loop_write(i->tar_fd, p, sz);
b6e676ce
LP
265 if (r < 0)
266 return r;
267
268 i->written_uncompressed += sz;
b6e676ce
LP
269
270 return 0;
271}
272
273static 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) {
587fec42
LP
282 if (errno == EAGAIN)
283 return 0;
284
b6e676ce
LP
285 r = log_error_errno(errno, "Failed to read input file: %m");
286 goto finish;
287 }
b6e676ce 288
f7012a93
YW
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
b6e676ce
LP
294 i->buffer_size += l;
295
296 if (i->compress.type == IMPORT_COMPRESS_UNKNOWN) {
c9b6ebef
LP
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;
b6e676ce 309 }
b6e676ce
LP
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
c9b6ebef
LP
325 if (l == 0) { /* EOF */
326 r = tar_import_finish(i);
327 goto finish;
328 }
329
b6e676ce
LP
330 tar_import_report_progress(i);
331
332 return 0;
333
334finish:
3dd0389b
DDM
335 if (r >= 0 && isatty_safe(STDERR_FILENO))
336 clear_progress_bar(/* prefix= */ NULL);
337
b6e676ce
LP
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
346static 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
352static int tar_import_on_defer(sd_event_source *s, void *userdata) {
353 TarImport *i = userdata;
354
355 return tar_import_process(i);
356}
357
5183c50a 358int tar_import_start(TarImport *i, int fd, const char *local, ImportFlags flags) {
b6e676ce
LP
359 int r;
360
361 assert(i);
362 assert(fd >= 0);
363 assert(local);
d32a5841 364 assert(!(flags & ~IMPORT_FLAGS_MASK_TAR));
b6e676ce 365
d32a5841 366 if (!import_validate_local(local, flags))
b6e676ce
LP
367 return -EINVAL;
368
369 if (i->input_fd >= 0)
370 return -EBUSY;
371
587fec42
LP
372 r = fd_nonblock(fd, true);
373 if (r < 0)
374 return r;
375
b6e676ce
LP
376 r = free_and_strdup(&i->local, local);
377 if (r < 0)
378 return r;
5183c50a
LP
379
380 i->flags = flags;
b6e676ce 381
d32a5841 382 if (fstat(fd, &i->input_stat) < 0)
b6e676ce
LP
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;
d32a5841 398 return 0;
b6e676ce 399}