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