]> git.ipfire.org Git - thirdparty/systemd.git/blame_incremental - src/import/export-tar.c
openssl-util: allow to build with openssl without UI support (#38041)
[thirdparty/systemd.git] / src / import / export-tar.c
... / ...
CommitLineData
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 "export-tar.h"
11#include "fd-util.h"
12#include "format-util.h"
13#include "import-common.h"
14#include "log.h"
15#include "pretty-print.h"
16#include "process-util.h"
17#include "ratelimit.h"
18#include "string-util.h"
19#include "terminal-util.h"
20#include "time-util.h"
21#include "tmpfile-util.h"
22
23#define COPY_BUFFER_SIZE (16*1024)
24
25typedef struct TarExport {
26 sd_event *event;
27
28 TarExportFinished on_finished;
29 void *userdata;
30
31 char *path;
32 char *temp_path;
33
34 int output_fd;
35 int tar_fd;
36
37 ImportCompress compress;
38
39 sd_event_source *output_event_source;
40
41 void *buffer;
42 size_t buffer_size;
43 size_t buffer_allocated;
44
45 uint64_t written_compressed;
46 uint64_t written_uncompressed;
47
48 pid_t tar_pid;
49
50 struct stat st;
51 uint64_t quota_referenced;
52
53 unsigned last_percent;
54 RateLimit progress_ratelimit;
55
56 bool eof;
57 bool tried_splice;
58} TarExport;
59
60TarExport *tar_export_unref(TarExport *e) {
61 if (!e)
62 return NULL;
63
64 sd_event_source_unref(e->output_event_source);
65
66 if (e->tar_pid > 1)
67 sigkill_wait(e->tar_pid);
68
69 if (e->temp_path) {
70 (void) btrfs_subvol_remove(e->temp_path, BTRFS_REMOVE_QUOTA);
71 free(e->temp_path);
72 }
73
74 import_compress_free(&e->compress);
75
76 sd_event_unref(e->event);
77
78 safe_close(e->tar_fd);
79
80 free(e->buffer);
81 free(e->path);
82 return mfree(e);
83}
84
85int tar_export_new(
86 TarExport **ret,
87 sd_event *event,
88 TarExportFinished on_finished,
89 void *userdata) {
90
91 _cleanup_(tar_export_unrefp) TarExport *e = NULL;
92 int r;
93
94 assert(ret);
95
96 e = new(TarExport, 1);
97 if (!e)
98 return -ENOMEM;
99
100 *e = (TarExport) {
101 .output_fd = -EBADF,
102 .tar_fd = -EBADF,
103 .on_finished = on_finished,
104 .userdata = userdata,
105 .quota_referenced = UINT64_MAX,
106 .last_percent = UINT_MAX,
107 .progress_ratelimit = { 100 * USEC_PER_MSEC, 1 },
108 };
109
110 if (event)
111 e->event = sd_event_ref(event);
112 else {
113 r = sd_event_default(&e->event);
114 if (r < 0)
115 return r;
116 }
117
118 *ret = TAKE_PTR(e);
119
120 return 0;
121}
122
123static void tar_export_report_progress(TarExport *e) {
124 unsigned percent;
125 assert(e);
126
127 /* Do we have any quota info? If not, we don't know anything about the progress */
128 if (e->quota_referenced == UINT64_MAX)
129 return;
130
131 if (e->written_uncompressed >= e->quota_referenced)
132 percent = 100;
133 else
134 percent = (unsigned) ((e->written_uncompressed * UINT64_C(100)) / e->quota_referenced);
135
136 if (percent == e->last_percent)
137 return;
138
139 if (!ratelimit_below(&e->progress_ratelimit))
140 return;
141
142 sd_notifyf(false, "X_IMPORT_PROGRESS=%u%%", percent);
143
144 if (isatty_safe(STDERR_FILENO))
145 (void) draw_progress_barf(
146 percent,
147 "%s %s/%s",
148 glyph(GLYPH_ARROW_RIGHT),
149 FORMAT_BYTES(e->written_uncompressed),
150 FORMAT_BYTES(e->quota_referenced));
151 else
152 log_info("Exported %u%%.", percent);
153
154 e->last_percent = percent;
155}
156
157static int tar_export_finish(TarExport *e) {
158 int r;
159
160 assert(e);
161 assert(e->tar_fd >= 0);
162
163 if (e->tar_pid > 0) {
164 r = wait_for_terminate_and_check("tar", TAKE_PID(e->tar_pid), WAIT_LOG);
165 if (r < 0)
166 return r;
167 if (r != EXIT_SUCCESS)
168 return -EPROTO;
169 }
170
171 e->tar_fd = safe_close(e->tar_fd);
172
173 return 0;
174}
175
176static int tar_export_process(TarExport *e) {
177 ssize_t l;
178 int r;
179
180 assert(e);
181
182 if (!e->tried_splice && e->compress.type == IMPORT_COMPRESS_UNCOMPRESSED) {
183
184 l = splice(e->tar_fd, NULL, e->output_fd, NULL, COPY_BUFFER_SIZE, 0);
185 if (l < 0) {
186 if (errno == EAGAIN)
187 return 0;
188
189 e->tried_splice = true;
190 } else if (l == 0) {
191 r = tar_export_finish(e);
192 goto finish;
193 } else {
194 e->written_uncompressed += l;
195 e->written_compressed += l;
196
197 tar_export_report_progress(e);
198
199 return 0;
200 }
201 }
202
203 while (e->buffer_size <= 0) {
204 uint8_t input[COPY_BUFFER_SIZE];
205
206 if (e->eof) {
207 r = tar_export_finish(e);
208 goto finish;
209 }
210
211 l = read(e->tar_fd, input, sizeof(input));
212 if (l < 0) {
213 r = log_error_errno(errno, "Failed to read tar file: %m");
214 goto finish;
215 }
216
217 if (l == 0) {
218 e->eof = true;
219 r = import_compress_finish(&e->compress, &e->buffer, &e->buffer_size, &e->buffer_allocated);
220 } else {
221 e->written_uncompressed += l;
222 r = import_compress(&e->compress, input, l, &e->buffer, &e->buffer_size, &e->buffer_allocated);
223 }
224 if (r < 0) {
225 r = log_error_errno(r, "Failed to encode: %m");
226 goto finish;
227 }
228 }
229
230 l = write(e->output_fd, e->buffer, e->buffer_size);
231 if (l < 0) {
232 if (errno == EAGAIN)
233 return 0;
234
235 r = log_error_errno(errno, "Failed to write output file: %m");
236 goto finish;
237 }
238
239 assert((size_t) l <= e->buffer_size);
240 memmove(e->buffer, (uint8_t*) e->buffer + l, e->buffer_size - l);
241 e->buffer_size -= l;
242 e->written_compressed += l;
243
244 tar_export_report_progress(e);
245
246 return 0;
247
248finish:
249 if (r >= 0 && isatty_safe(STDERR_FILENO))
250 clear_progress_bar(/* prefix= */ NULL);
251
252 if (e->on_finished)
253 e->on_finished(e, r, e->userdata);
254 else
255 sd_event_exit(e->event, r);
256
257 return 0;
258}
259
260static int tar_export_on_output(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
261 TarExport *i = userdata;
262
263 return tar_export_process(i);
264}
265
266static int tar_export_on_defer(sd_event_source *s, void *userdata) {
267 TarExport *i = userdata;
268
269 return tar_export_process(i);
270}
271
272int tar_export_start(TarExport *e, const char *path, int fd, ImportCompressType compress) {
273 _cleanup_close_ int sfd = -EBADF;
274 int r;
275
276 assert(e);
277 assert(path);
278 assert(fd >= 0);
279 assert(compress < _IMPORT_COMPRESS_TYPE_MAX);
280 assert(compress != IMPORT_COMPRESS_UNKNOWN);
281
282 if (e->output_fd >= 0)
283 return -EBUSY;
284
285 sfd = open(path, O_DIRECTORY|O_RDONLY|O_NOCTTY|O_CLOEXEC);
286 if (sfd < 0)
287 return -errno;
288
289 if (fstat(sfd, &e->st) < 0)
290 return -errno;
291
292 r = fd_nonblock(fd, true);
293 if (r < 0)
294 return r;
295
296 r = free_and_strdup(&e->path, path);
297 if (r < 0)
298 return r;
299
300 e->quota_referenced = UINT64_MAX;
301
302 if (btrfs_might_be_subvol(&e->st)) {
303 BtrfsQuotaInfo q;
304
305 r = btrfs_subvol_get_subtree_quota_fd(sfd, 0, &q);
306 if (r >= 0)
307 e->quota_referenced = q.referenced;
308
309 e->temp_path = mfree(e->temp_path);
310
311 r = tempfn_random(path, NULL, &e->temp_path);
312 if (r < 0)
313 return r;
314
315 /* Let's try to make a snapshot, if we can, so that the export is atomic */
316 r = btrfs_subvol_snapshot_at(sfd, NULL, AT_FDCWD, e->temp_path, BTRFS_SNAPSHOT_READ_ONLY|BTRFS_SNAPSHOT_RECURSIVE);
317 if (r < 0) {
318 log_debug_errno(r, "Couldn't create snapshot %s of %s, not exporting atomically: %m", e->temp_path, path);
319 e->temp_path = mfree(e->temp_path);
320 }
321 }
322
323 r = import_compress_init(&e->compress, compress);
324 if (r < 0)
325 return r;
326
327 r = sd_event_add_io(e->event, &e->output_event_source, fd, EPOLLOUT, tar_export_on_output, e);
328 if (r == -EPERM) {
329 r = sd_event_add_defer(e->event, &e->output_event_source, tar_export_on_defer, e);
330 if (r < 0)
331 return r;
332
333 r = sd_event_source_set_enabled(e->output_event_source, SD_EVENT_ON);
334 }
335 if (r < 0)
336 return r;
337
338 e->tar_fd = import_fork_tar_c(e->temp_path ?: e->path, &e->tar_pid);
339 if (e->tar_fd < 0) {
340 e->output_event_source = sd_event_source_unref(e->output_event_source);
341 return e->tar_fd;
342 }
343
344 e->output_fd = fd;
345 return r;
346}