Debian Codegraph @

src/import/export-raw.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <sys/sendfile.h>
4
5 #include "sd-daemon.h"
6 #include "sd-event.h"
7
8 #include "alloc-util.h"
9 #include "copy.h"
10 #include "export-raw.h"
11 #include "fd-util.h"
12 #include "format-util.h"
13 #include "fs-util.h"
14 #include "log.h"
15 #include "pretty-print.h"
16 #include "ratelimit.h"
17 #include "stat-util.h"
18 #include "string-util.h"
19 #include "terminal-util.h"
20 #include "time-util.h"
21 #include "tmpfile-util.h"
22
23 typedef struct RawExport {
24 sd_event *event;
25
26 RawExportFinished on_finished;
27 void *userdata;
28
29 char *path;
30
31 int input_fd;
32 int output_fd;
33
34 Compressor *compress;
35
36 sd_event_source *output_event_source;
37
38 void *buffer;
39 size_t buffer_size;
40 size_t buffer_allocated;
41
42 uint64_t written_compressed;
43 uint64_t written_uncompressed;
44
45 unsigned last_percent;
46 RateLimit progress_ratelimit;
47
48 struct stat st;
49
50 bool eof;
51 bool tried_reflink;
52 bool tried_sendfile;
53 } RawExport;
54
55 RawExport *raw_export_unref(RawExport *e) {
56 if (!e)
57 return NULL;
58
59 sd_event_source_unref(e->output_event_source);
60
61 e->compress = compressor_free(e->compress);
62
63 sd_event_unref(e->event);
64
65 safe_close(e->input_fd);
66
67 free(e->buffer);
68 free(e->path);
69 return mfree(e);
70 }
71
72 int raw_export_new(
73 RawExport **ret,
74 sd_event *event,
75 RawExportFinished on_finished,
76 void *userdata) {
77
78 _cleanup_(raw_export_unrefp) RawExport *e = NULL;
79 int r;
80
81 assert(ret);
82
83 e = new(RawExport, 1);
84 if (!e)
85 return -ENOMEM;
86
87 *e = (RawExport) {
88 .output_fd = -EBADF,
89 .input_fd = -EBADF,
90 .on_finished = on_finished,
91 .userdata = userdata,
92 .last_percent = UINT_MAX,
93 .progress_ratelimit = { 100 * USEC_PER_MSEC, 1 },
94 };
95
96 if (event)
97 e->event = sd_event_ref(event);
98 else {
99 r = sd_event_default(&e->event);
100 if (r < 0)
101 return r;
102 }
103
104 *ret = TAKE_PTR(e);
105
106 return 0;
107 }
108
109 static void raw_export_report_progress(RawExport *e) {
110 unsigned percent;
111 assert(e);
112
113 if (e->written_uncompressed >= (uint64_t) e->st.st_size)
114 percent = 100;
115 else
116 percent = (unsigned) ((e->written_uncompressed * UINT64_C(100)) / (uint64_t) e->st.st_size);
117
118 if (percent == e->last_percent)
119 return;
120
121 if (!ratelimit_below(&e->progress_ratelimit))
122 return;
123
124 sd_notifyf(false, "X_IMPORT_PROGRESS=%u%%", percent);
125
126 if (isatty_safe(STDERR_FILENO))
127 (void) draw_progress_barf(
128 percent,
129 "%s %s/%s",
130 glyph(GLYPH_ARROW_RIGHT),
131 FORMAT_BYTES(e->written_uncompressed),
132 FORMAT_BYTES(e->st.st_size));
133 else
134 log_info("Exported %u%%.", percent);
135
136 e->last_percent = percent;
137 }
138
139 static int raw_export_process(RawExport *e) {
140 ssize_t l;
141 int r;
142
143 assert(e);
144
145 if (!e->tried_reflink && compressor_type(e->compress) == COMPRESSION_NONE) {
146
147 /* If we shall take an uncompressed snapshot we can
148 * reflink source to destination directly. Let's see
149 * if this works. */
150
151 r = reflink(e->input_fd, e->output_fd);
152 if (r >= 0) {
153 r = 0;
154 goto finish;
155 }
156
157 e->tried_reflink = true;
158 }
159
160 if (!e->tried_sendfile && compressor_type(e->compress) == COMPRESSION_NONE) {
161
162 l = sendfile(e->output_fd, e->input_fd, NULL, COMPRESS_PIPE_BUFFER_SIZE);
163 if (l < 0) {
164 if (errno == EAGAIN)
165 return 0;
166
167 e->tried_sendfile = true;
168 } else if (l == 0) {
169 r = 0;
170 goto finish;
171 } else {
172 e->written_uncompressed += l;
173 e->written_compressed += l;
174
175 raw_export_report_progress(e);
176
177 return 0;
178 }
179 }
180
181 while (e->buffer_size <= 0) {
182 uint8_t input[COMPRESS_PIPE_BUFFER_SIZE];
183
184 if (e->eof) {
185 r = 0;
186 goto finish;
187 }
188
189 l = read(e->input_fd, input, sizeof(input));
190 if (l < 0) {
191 r = log_error_errno(errno, "Failed to read raw file: %m");
192 goto finish;
193 }
194
195 if (l == 0) {
196 e->eof = true;
197 r = compressor_finish(e->compress, &e->buffer, &e->buffer_size, &e->buffer_allocated);
198 } else {
199 e->written_uncompressed += l;
200 r = compressor_start(e->compress, input, l, &e->buffer, &e->buffer_size, &e->buffer_allocated);
201 }
202 if (r < 0) {
203 r = log_error_errno(r, "Failed to encode: %m");
204 goto finish;
205 }
206 }
207
208 l = write(e->output_fd, e->buffer, e->buffer_size);
209 if (l < 0) {
210 if (errno == EAGAIN)
211 return 0;
212
213 r = log_error_errno(errno, "Failed to write output file: %m");
214 goto finish;
215 }
216
217 assert((size_t) l <= e->buffer_size);
218 memmove(e->buffer, (uint8_t*) e->buffer + l, e->buffer_size - l);
219 e->buffer_size -= l;
220 e->written_compressed += l;
221
222 raw_export_report_progress(e);
223
224 return 0;
225
226 finish:
227 if (r >= 0) {
228 if (isatty_safe(STDERR_FILENO))
229 clear_progress_bar(/* prefix= */ NULL);
230
231 (void) copy_times(e->input_fd, e->output_fd, COPY_CRTIME);
232 (void) copy_xattr(e->input_fd, NULL, e->output_fd, NULL, 0);
233 }
234
235 if (e->on_finished)
236 e->on_finished(e, r, e->userdata);
237 else
238 sd_event_exit(e->event, r);
239
240 return 0;
241 }
242
243 static int raw_export_on_output(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
244 RawExport *i = userdata;
245
246 return raw_export_process(i);
247 }
248
249 static int raw_export_on_defer(sd_event_source *s, void *userdata) {
250 RawExport *i = userdata;
251
252 return raw_export_process(i);
253 }
254
255 static int reflink_snapshot(int fd, const char *path) {
256 int new_fd, r;
257
258 new_fd = open_parent(path, O_TMPFILE|O_CLOEXEC|O_RDWR, 0600);
259 if (new_fd < 0) {
260 _cleanup_free_ char *t = NULL;
261
262 r = tempfn_random(path, NULL, &t);
263 if (r < 0)
264 return r;
265
266 new_fd = open(t, O_CLOEXEC|O_CREAT|O_NOCTTY|O_RDWR, 0600);
267 if (new_fd < 0)
268 return -errno;
269
270 (void) unlink(t);
271 }
272
273 r = reflink(fd, new_fd);
274 if (r < 0) {
275 safe_close(new_fd);
276 return r;
277 }
278
279 return new_fd;
280 }
281
282 int raw_export_start(RawExport *e, const char *path, int fd, Compression compress) {
283 _cleanup_close_ int sfd = -EBADF, tfd = -EBADF;
284 int r;
285
286 assert(e);
287 assert(path);
288 assert(fd >= 0);
289 assert(compress >= 0);
290 assert(compress < _COMPRESSION_MAX);
291
292 if (e->output_fd >= 0)
293 return -EBUSY;
294
295 r = fd_nonblock(fd, true);
296 if (r < 0)
297 return r;
298
299 r = free_and_strdup(&e->path, path);
300 if (r < 0)
301 return r;
302
303 sfd = open(path, O_RDONLY|O_CLOEXEC|O_NOCTTY);
304 if (sfd < 0)
305 return -errno;
306
307 if (fstat(sfd, &e->st) < 0)
308 return -errno;
309 r = stat_verify_regular(&e->st);
310 if (r < 0)
311 return r;
312
313 /* Try to take a reflink snapshot of the file, if we can t make the export atomic */
314 tfd = reflink_snapshot(sfd, path);
315 if (tfd >= 0)
316 e->input_fd = TAKE_FD(tfd);
317 else
318 e->input_fd = TAKE_FD(sfd);
319
320 r = compressor_new(&e->compress, compress);
321 if (r < 0)
322 return r;
323
324 r = sd_event_add_io(e->event, &e->output_event_source, fd, EPOLLOUT, raw_export_on_output, e);
325 if (r == -EPERM) {
326 r = sd_event_add_defer(e->event, &e->output_event_source, raw_export_on_defer, e);
327 if (r < 0)
328 return r;
329
330 r = sd_event_source_set_enabled(e->output_event_source, SD_EVENT_ON);
331 }
332 if (r < 0)
333 return r;
334
335 e->output_fd = fd;
336 return r;
337 }