]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/import/export-tar.c
import: use structured initializers
[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"
0d39fa9c 9#include "fileio.h"
587fec42 10#include "import-common.h"
0b452006 11#include "process-util.h"
07630cea
LP
12#include "ratelimit.h"
13#include "string-util.h"
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
144static int tar_export_process(TarExport *e) {
145 ssize_t l;
146 int r;
147
148 assert(e);
149
150 if (!e->tried_splice && e->compress.type == IMPORT_COMPRESS_UNCOMPRESSED) {
151
152 l = splice(e->tar_fd, NULL, e->output_fd, NULL, COPY_BUFFER_SIZE, 0);
153 if (l < 0) {
154 if (errno == EAGAIN)
155 return 0;
156
157 e->tried_splice = true;
158 } else if (l == 0) {
159 r = 0;
160 goto finish;
161 } else {
162 e->written_uncompressed += l;
163 e->written_compressed += l;
164
165 tar_export_report_progress(e);
166
167 return 0;
168 }
169 }
170
171 while (e->buffer_size <= 0) {
172 uint8_t input[COPY_BUFFER_SIZE];
173
174 if (e->eof) {
175 r = 0;
176 goto finish;
177 }
178
179 l = read(e->tar_fd, input, sizeof(input));
180 if (l < 0) {
181 r = log_error_errno(errno, "Failed to read tar file: %m");
182 goto finish;
183 }
184
185 if (l == 0) {
186 e->eof = true;
187 r = import_compress_finish(&e->compress, &e->buffer, &e->buffer_size, &e->buffer_allocated);
188 } else {
189 e->written_uncompressed += l;
190 r = import_compress(&e->compress, input, l, &e->buffer, &e->buffer_size, &e->buffer_allocated);
191 }
192 if (r < 0) {
193 r = log_error_errno(r, "Failed to encode: %m");
194 goto finish;
195 }
196 }
197
198 l = write(e->output_fd, e->buffer, e->buffer_size);
199 if (l < 0) {
200 if (errno == EAGAIN)
201 return 0;
202
203 r = log_error_errno(errno, "Failed to write output file: %m");
204 goto finish;
205 }
206
207 assert((size_t) l <= e->buffer_size);
208 memmove(e->buffer, (uint8_t*) e->buffer + l, e->buffer_size - l);
209 e->buffer_size -= l;
210 e->written_compressed += l;
211
212 tar_export_report_progress(e);
213
214 return 0;
215
216finish:
217 if (e->on_finished)
218 e->on_finished(e, r, e->userdata);
219 else
220 sd_event_exit(e->event, r);
221
222 return 0;
223}
224
225static int tar_export_on_output(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
226 TarExport *i = userdata;
227
228 return tar_export_process(i);
229}
230
231static int tar_export_on_defer(sd_event_source *s, void *userdata) {
232 TarExport *i = userdata;
233
234 return tar_export_process(i);
235}
236
237int tar_export_start(TarExport *e, const char *path, int fd, ImportCompressType compress) {
238 _cleanup_close_ int sfd = -1;
239 int r;
240
241 assert(e);
242 assert(path);
243 assert(fd >= 0);
244 assert(compress < _IMPORT_COMPRESS_TYPE_MAX);
245 assert(compress != IMPORT_COMPRESS_UNKNOWN);
246
247 if (e->output_fd >= 0)
248 return -EBUSY;
249
250 sfd = open(path, O_DIRECTORY|O_RDONLY|O_NOCTTY|O_CLOEXEC);
251 if (sfd < 0)
252 return -errno;
253
254 if (fstat(sfd, &e->st) < 0)
255 return -errno;
256
257 r = fd_nonblock(fd, true);
258 if (r < 0)
259 return r;
260
261 r = free_and_strdup(&e->path, path);
262 if (r < 0)
263 return r;
264
265 e->quota_referenced = (uint64_t) -1;
266
267 if (e->st.st_ino == 256) { /* might be a btrfs subvolume? */
268 BtrfsQuotaInfo q;
269
5bcd08db 270 r = btrfs_subvol_get_subtree_quota_fd(sfd, 0, &q);
587fec42 271 if (r >= 0)
cb81cd80 272 e->quota_referenced = q.referenced;
587fec42 273
a1e58e8e 274 e->temp_path = mfree(e->temp_path);
587fec42 275
14bcf25c 276 r = tempfn_random(path, NULL, &e->temp_path);
587fec42
LP
277 if (r < 0)
278 return r;
279
280 /* Let's try to make a snapshot, if we can, so that the export is atomic */
f70a17f8 281 r = btrfs_subvol_snapshot_fd(sfd, e->temp_path, BTRFS_SNAPSHOT_READ_ONLY|BTRFS_SNAPSHOT_RECURSIVE);
587fec42
LP
282 if (r < 0) {
283 log_debug_errno(r, "Couldn't create snapshot %s of %s, not exporting atomically: %m", e->temp_path, path);
a1e58e8e 284 e->temp_path = mfree(e->temp_path);
587fec42
LP
285 }
286 }
287
288 r = import_compress_init(&e->compress, compress);
289 if (r < 0)
290 return r;
291
292 r = sd_event_add_io(e->event, &e->output_event_source, fd, EPOLLOUT, tar_export_on_output, e);
293 if (r == -EPERM) {
294 r = sd_event_add_defer(e->event, &e->output_event_source, tar_export_on_defer, e);
295 if (r < 0)
296 return r;
297
298 r = sd_event_source_set_enabled(e->output_event_source, SD_EVENT_ON);
299 }
300 if (r < 0)
301 return r;
302
303 e->tar_fd = import_fork_tar_c(e->temp_path ?: e->path, &e->tar_pid);
304 if (e->tar_fd < 0) {
305 e->output_event_source = sd_event_source_unref(e->output_event_source);
306 return e->tar_fd;
307 }
308
309 e->output_fd = fd;
310 return r;
311}