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