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