]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/import/export-tar.c
fix: UnsetProperty example in systemd.link.xml
[thirdparty/systemd.git] / src / import / export-tar.c
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
587fec42 2
9412e9e9
DDM
3#include <sys/stat.h>
4
587fec42 5#include "sd-daemon.h"
9412e9e9 6#include "sd-event.h"
07630cea 7
b5efdb8a 8#include "alloc-util.h"
587fec42 9#include "btrfs-util.h"
3ffd4af2
LP
10#include "export-tar.h"
11#include "fd-util.h"
9412e9e9 12#include "format-util.h"
587fec42 13#include "import-common.h"
93a1f792 14#include "log.h"
3dd0389b 15#include "pretty-print.h"
0b452006 16#include "process-util.h"
07630cea
LP
17#include "ratelimit.h"
18#include "string-util.h"
9412e9e9
DDM
19#include "terminal-util.h"
20#include "time-util.h"
e4de7287 21#include "tmpfile-util.h"
587fec42
LP
22
23#define COPY_BUFFER_SIZE (16*1024)
24
9412e9e9 25typedef struct TarExport {
587fec42
LP
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;
5ac1530e 54 RateLimit progress_ratelimit;
587fec42
LP
55
56 bool eof;
57 bool tried_splice;
9412e9e9 58} TarExport;
587fec42
LP
59
60TarExport *tar_export_unref(TarExport *e) {
61 if (!e)
62 return NULL;
63
64 sd_event_source_unref(e->output_event_source);
65
7950211d
LP
66 if (e->tar_pid > 1)
67 sigkill_wait(e->tar_pid);
587fec42
LP
68
69 if (e->temp_path) {
5bcd08db 70 (void) btrfs_subvol_remove(e->temp_path, BTRFS_REMOVE_QUOTA);
587fec42
LP
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);
6b430fdb 82 return mfree(e);
587fec42
LP
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
0d94088e 96 e = new(TarExport, 1);
587fec42
LP
97 if (!e)
98 return -ENOMEM;
99
0d94088e 100 *e = (TarExport) {
254d1313
ZJS
101 .output_fd = -EBADF,
102 .tar_fd = -EBADF,
0d94088e
YW
103 .on_finished = on_finished,
104 .userdata = userdata,
f5fbe71d
YW
105 .quota_referenced = UINT64_MAX,
106 .last_percent = UINT_MAX,
5ac1530e 107 .progress_ratelimit = { 100 * USEC_PER_MSEC, 1 },
0d94088e 108 };
587fec42 109
587fec42
LP
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
1cc6c93a 118 *ret = TAKE_PTR(e);
587fec42
LP
119
120 return 0;
121}
122
123static void tar_export_report_progress(TarExport *e) {
124 unsigned percent;
125 assert(e);
126
e5f270f5 127 /* Do we have any quota info? If not, we don't know anything about the progress */
f5fbe71d 128 if (e->quota_referenced == UINT64_MAX)
587fec42
LP
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
5ac1530e 139 if (!ratelimit_below(&e->progress_ratelimit))
587fec42
LP
140 return;
141
a986de68 142 sd_notifyf(false, "X_IMPORT_PROGRESS=%u%%", percent);
3dd0389b 143
91d64043
LP
144 if (isatty_safe(STDERR_FILENO))
145 (void) draw_progress_barf(
146 percent,
147 "%s %s/%s",
1ae9b0cf 148 glyph(GLYPH_ARROW_RIGHT),
91d64043
LP
149 FORMAT_BYTES(e->written_uncompressed),
150 FORMAT_BYTES(e->quota_referenced));
151 else
3dd0389b 152 log_info("Exported %u%%.", percent);
587fec42
LP
153
154 e->last_percent = percent;
155}
156
82f299e7
LP
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) {
8f03de53 164 r = wait_for_terminate_and_check("tar", TAKE_PID(e->tar_pid), WAIT_LOG);
82f299e7
LP
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
587fec42
LP
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) {
82f299e7 191 r = tar_export_finish(e);
587fec42
LP
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) {
82f299e7 207 r = tar_export_finish(e);
587fec42
LP
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:
3dd0389b
DDM
249 if (r >= 0 && isatty_safe(STDERR_FILENO))
250 clear_progress_bar(/* prefix= */ NULL);
251
587fec42
LP
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) {
254d1313 273 _cleanup_close_ int sfd = -EBADF;
587fec42
LP
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
f5fbe71d 300 e->quota_referenced = UINT64_MAX;
587fec42 301
674b04ff 302 if (btrfs_might_be_subvol(&e->st)) {
587fec42
LP
303 BtrfsQuotaInfo q;
304
5bcd08db 305 r = btrfs_subvol_get_subtree_quota_fd(sfd, 0, &q);
587fec42 306 if (r >= 0)
cb81cd80 307 e->quota_referenced = q.referenced;
587fec42 308
a1e58e8e 309 e->temp_path = mfree(e->temp_path);
587fec42 310
14bcf25c 311 r = tempfn_random(path, NULL, &e->temp_path);
587fec42
LP
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 */
fab4ef72 316 r = btrfs_subvol_snapshot_at(sfd, NULL, AT_FDCWD, e->temp_path, BTRFS_SNAPSHOT_READ_ONLY|BTRFS_SNAPSHOT_RECURSIVE);
587fec42
LP
317 if (r < 0) {
318 log_debug_errno(r, "Couldn't create snapshot %s of %s, not exporting atomically: %m", e->temp_path, path);
a1e58e8e 319 e->temp_path = mfree(e->temp_path);
587fec42
LP
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}