]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
56ebfaf1 | 2 | |
ca78ad1d ZJS |
3 | #include <fcntl.h> |
4 | #include <sys/stat.h> | |
56ebfaf1 LP |
5 | #include <sys/xattr.h> |
6 | ||
b5efdb8a | 7 | #include "alloc-util.h" |
3ffd4af2 | 8 | #include "fd-util.h" |
aa892669 | 9 | #include "format-util.h" |
c40d82ab | 10 | #include "fs-util.h" |
eef836ed | 11 | #include "gcrypt-util.h" |
89a5a90c | 12 | #include "hexdecoct.h" |
1f0f4f3b | 13 | #include "import-util.h" |
c004493c | 14 | #include "io-util.h" |
26166c88 | 15 | #include "machine-pool.h" |
6bedfcbb | 16 | #include "parse-util.h" |
1f0f4f3b | 17 | #include "pull-common.h" |
3ffd4af2 | 18 | #include "pull-job.h" |
07630cea LP |
19 | #include "string-util.h" |
20 | #include "strv.h" | |
89a5a90c | 21 | #include "xattr-util.h" |
56ebfaf1 | 22 | |
c40d82ab LP |
23 | void pull_job_close_disk_fd(PullJob *j) { |
24 | if (!j) | |
25 | return; | |
26 | ||
27 | if (j->close_disk_fd) | |
28 | safe_close(j->disk_fd); | |
29 | ||
30 | j->disk_fd = -1; | |
31 | } | |
32 | ||
dc2c282b | 33 | PullJob* pull_job_unref(PullJob *j) { |
56ebfaf1 LP |
34 | if (!j) |
35 | return NULL; | |
36 | ||
c40d82ab LP |
37 | pull_job_close_disk_fd(j); |
38 | ||
56ebfaf1 LP |
39 | curl_glue_remove_and_free(j->glue, j->curl); |
40 | curl_slist_free_all(j->request_header); | |
41 | ||
3e2cda69 | 42 | import_compress_free(&j->compress); |
56ebfaf1 | 43 | |
98c38001 LP |
44 | if (j->checksum_context) |
45 | gcry_md_close(j->checksum_context); | |
85dbc41d | 46 | |
56ebfaf1 LP |
47 | free(j->url); |
48 | free(j->etag); | |
49 | strv_free(j->old_etags); | |
50 | free(j->payload); | |
98c38001 | 51 | free(j->checksum); |
56ebfaf1 | 52 | |
6b430fdb | 53 | return mfree(j); |
56ebfaf1 LP |
54 | } |
55 | ||
dc2c282b | 56 | static void pull_job_finish(PullJob *j, int ret) { |
56ebfaf1 LP |
57 | assert(j); |
58 | ||
3742095b | 59 | if (IN_SET(j->state, PULL_JOB_DONE, PULL_JOB_FAILED)) |
56ebfaf1 LP |
60 | return; |
61 | ||
68c913fd | 62 | if (ret == 0) { |
dc2c282b | 63 | j->state = PULL_JOB_DONE; |
7079cfef | 64 | j->progress_percent = 100; |
68c913fd LP |
65 | log_info("Download of %s complete.", j->url); |
66 | } else { | |
dc2c282b | 67 | j->state = PULL_JOB_FAILED; |
56ebfaf1 LP |
68 | j->error = ret; |
69 | } | |
70 | ||
71 | if (j->on_finished) | |
72 | j->on_finished(j); | |
73 | } | |
74 | ||
f14717a7 | 75 | static int pull_job_restart(PullJob *j, const char *new_url) { |
697be0be | 76 | int r; |
697be0be | 77 | |
f14717a7 LP |
78 | assert(j); |
79 | assert(new_url); | |
80 | ||
81 | r = free_and_strdup(&j->url, new_url); | |
697be0be TB |
82 | if (r < 0) |
83 | return r; | |
84 | ||
697be0be | 85 | j->state = PULL_JOB_INIT; |
8bc3f0b8 | 86 | j->error = 0; |
1f0f4f3b | 87 | j->payload = mfree(j->payload); |
697be0be | 88 | j->payload_size = 0; |
697be0be TB |
89 | j->written_compressed = 0; |
90 | j->written_uncompressed = 0; | |
8bc3f0b8 LP |
91 | j->content_length = UINT64_MAX; |
92 | j->etag = mfree(j->etag); | |
93 | j->etag_exists = false; | |
94 | j->mtime = 0; | |
95 | j->checksum = mfree(j->checksum); | |
96 | ||
97 | curl_glue_remove_and_free(j->glue, j->curl); | |
98 | j->curl = NULL; | |
99 | ||
100 | curl_slist_free_all(j->request_header); | |
101 | j->request_header = NULL; | |
102 | ||
103 | import_compress_free(&j->compress); | |
104 | ||
105 | if (j->checksum_context) { | |
106 | gcry_md_close(j->checksum_context); | |
107 | j->checksum_context = NULL; | |
108 | } | |
697be0be TB |
109 | |
110 | r = pull_job_begin(j); | |
111 | if (r < 0) | |
112 | return r; | |
113 | ||
114 | return 0; | |
115 | } | |
116 | ||
dc2c282b LP |
117 | void pull_job_curl_on_finished(CurlGlue *g, CURL *curl, CURLcode result) { |
118 | PullJob *j = NULL; | |
56ebfaf1 | 119 | CURLcode code; |
c456862f | 120 | long protocol; |
56ebfaf1 LP |
121 | int r; |
122 | ||
a7f7d1bd | 123 | if (curl_easy_getinfo(curl, CURLINFO_PRIVATE, (char **)&j) != CURLE_OK) |
56ebfaf1 LP |
124 | return; |
125 | ||
4c701096 | 126 | if (!j || IN_SET(j->state, PULL_JOB_DONE, PULL_JOB_FAILED)) |
56ebfaf1 LP |
127 | return; |
128 | ||
129 | if (result != CURLE_OK) { | |
c40d82ab | 130 | r = log_error_errno(SYNTHETIC_ERRNO(EIO), "Transfer failed: %s", curl_easy_strerror(result)); |
56ebfaf1 LP |
131 | goto finish; |
132 | } | |
133 | ||
c456862f | 134 | code = curl_easy_getinfo(curl, CURLINFO_PROTOCOL, &protocol); |
56ebfaf1 | 135 | if (code != CURLE_OK) { |
c40d82ab | 136 | r = log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to retrieve response code: %s", curl_easy_strerror(code)); |
56ebfaf1 | 137 | goto finish; |
c456862f | 138 | } |
697be0be | 139 | |
c456862f LP |
140 | if (IN_SET(protocol, CURLPROTO_HTTP, CURLPROTO_HTTPS)) { |
141 | long status; | |
f14717a7 | 142 | |
c456862f LP |
143 | code = curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &status); |
144 | if (code != CURLE_OK) { | |
145 | r = log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to retrieve response code: %s", curl_easy_strerror(code)); | |
146 | goto finish; | |
147 | } | |
697be0be | 148 | |
c456862f LP |
149 | if (status == 304) { |
150 | log_info("Image already downloaded. Skipping download."); | |
151 | j->etag_exists = true; | |
152 | r = 0; | |
153 | goto finish; | |
154 | } else if (status >= 300) { | |
f14717a7 | 155 | |
c456862f LP |
156 | if (status == 404 && j->on_not_found) { |
157 | _cleanup_free_ char *new_url = NULL; | |
158 | ||
159 | /* This resource wasn't found, but the implementor wants to maybe let us know a new URL, query for it. */ | |
160 | r = j->on_not_found(j, &new_url); | |
f14717a7 LP |
161 | if (r < 0) |
162 | goto finish; | |
163 | ||
c456862f LP |
164 | if (r > 0) { /* A new url to use */ |
165 | assert(new_url); | |
166 | ||
167 | r = pull_job_restart(j, new_url); | |
168 | if (r < 0) | |
169 | goto finish; | |
697be0be | 170 | |
c456862f LP |
171 | code = curl_easy_getinfo(j->curl, CURLINFO_RESPONSE_CODE, &status); |
172 | if (code != CURLE_OK) { | |
173 | r = log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to retrieve response code: %s", curl_easy_strerror(code)); | |
174 | goto finish; | |
175 | } | |
176 | ||
177 | if (status == 0) | |
178 | return; | |
179 | } | |
697be0be | 180 | } |
697be0be | 181 | |
c456862f LP |
182 | r = log_error_errno( |
183 | status == 404 ? SYNTHETIC_ERRNO(ENOMEDIUM) : SYNTHETIC_ERRNO(EIO), /* Make the most common error recognizable */ | |
184 | "HTTP request to %s failed with code %li.", j->url, status); | |
185 | goto finish; | |
186 | } else if (status < 200) { | |
187 | r = log_error_errno(SYNTHETIC_ERRNO(EIO), "HTTP request to %s finished with unexpected code %li.", j->url, status); | |
188 | goto finish; | |
189 | } | |
56ebfaf1 LP |
190 | } |
191 | ||
dc2c282b | 192 | if (j->state != PULL_JOB_RUNNING) { |
c40d82ab | 193 | r = log_error_errno(SYNTHETIC_ERRNO(EIO), "Premature connection termination."); |
56ebfaf1 LP |
194 | goto finish; |
195 | } | |
196 | ||
f5fbe71d | 197 | if (j->content_length != UINT64_MAX && |
56ebfaf1 | 198 | j->content_length != j->written_compressed) { |
c40d82ab | 199 | r = log_error_errno(SYNTHETIC_ERRNO(EIO), "Download truncated."); |
56ebfaf1 LP |
200 | goto finish; |
201 | } | |
202 | ||
98c38001 | 203 | if (j->checksum_context) { |
85dbc41d LP |
204 | uint8_t *k; |
205 | ||
98c38001 | 206 | k = gcry_md_read(j->checksum_context, GCRY_MD_SHA256); |
85dbc41d | 207 | if (!k) { |
c40d82ab | 208 | r = log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to get checksum."); |
85dbc41d LP |
209 | goto finish; |
210 | } | |
211 | ||
98c38001 LP |
212 | j->checksum = hexmem(k, gcry_md_get_algo_dlen(GCRY_MD_SHA256)); |
213 | if (!j->checksum) { | |
85dbc41d LP |
214 | r = log_oom(); |
215 | goto finish; | |
216 | } | |
217 | ||
98c38001 | 218 | log_debug("SHA256 of %s is %s.", j->url, j->checksum); |
85dbc41d LP |
219 | } |
220 | ||
c40d82ab LP |
221 | /* Do a couple of finishing disk operations, but only if we are the sole owner of the file (i.e. no |
222 | * offset is specified, which indicates we only own the file partially) */ | |
56ebfaf1 | 223 | |
c40d82ab | 224 | if (j->disk_fd >= 0) { |
56ebfaf1 | 225 | |
c40d82ab | 226 | if (S_ISREG(j->disk_stat.st_mode)) { |
56ebfaf1 | 227 | |
c40d82ab | 228 | if (j->offset == UINT64_MAX) { |
56ebfaf1 | 229 | |
c40d82ab LP |
230 | if (j->written_compressed > 0) { |
231 | /* Make sure the file size is right, in case the file was sparse and we just seeked | |
232 | * for the last part */ | |
233 | if (ftruncate(j->disk_fd, j->written_uncompressed) < 0) { | |
234 | r = log_error_errno(errno, "Failed to truncate file: %m"); | |
235 | goto finish; | |
236 | } | |
237 | } | |
238 | ||
239 | if (j->etag) | |
240 | (void) fsetxattr(j->disk_fd, "user.source_etag", j->etag, strlen(j->etag), 0); | |
241 | if (j->url) | |
242 | (void) fsetxattr(j->disk_fd, "user.source_url", j->url, strlen(j->url), 0); | |
243 | ||
244 | if (j->mtime != 0) { | |
245 | struct timespec ut; | |
56ebfaf1 | 246 | |
c40d82ab LP |
247 | timespec_store(&ut, j->mtime); |
248 | ||
249 | if (futimens(j->disk_fd, (struct timespec[]) { ut, ut }) < 0) | |
250 | log_debug_errno(errno, "Failed to adjust atime/mtime of created image, ignoring: %m"); | |
251 | ||
252 | r = fd_setcrtime(j->disk_fd, j->mtime); | |
253 | if (r < 0) | |
254 | log_debug_errno(r, "Failed to adjust crtime of created image, ignoring: %m"); | |
255 | } | |
256 | } | |
257 | ||
258 | if (j->sync) { | |
259 | r = fsync_full(j->disk_fd); | |
260 | if (r < 0) { | |
261 | log_error_errno(r, "Failed to synchronize file to disk: %m"); | |
262 | goto finish; | |
263 | } | |
264 | } | |
265 | ||
266 | } else if (S_ISBLK(j->disk_stat.st_mode) && j->sync) { | |
267 | ||
268 | if (fsync(j->disk_fd) < 0) { | |
269 | r = log_error_errno(errno, "Failed to synchronize block device: %m"); | |
270 | goto finish; | |
271 | } | |
56ebfaf1 LP |
272 | } |
273 | } | |
274 | ||
53e03c96 LP |
275 | log_info("Acquired %s.", FORMAT_BYTES(j->written_uncompressed)); |
276 | ||
56ebfaf1 LP |
277 | r = 0; |
278 | ||
279 | finish: | |
dc2c282b | 280 | pull_job_finish(j, r); |
56ebfaf1 LP |
281 | } |
282 | ||
3e2cda69 LP |
283 | static int pull_job_write_uncompressed(const void *p, size_t sz, void *userdata) { |
284 | PullJob *j = userdata; | |
c40d82ab LP |
285 | bool too_much = false; |
286 | int r; | |
56ebfaf1 LP |
287 | |
288 | assert(j); | |
289 | assert(p); | |
c40d82ab | 290 | assert(sz > 0); |
8af3cf74 | 291 | |
c40d82ab LP |
292 | if (j->written_uncompressed > UINT64_MAX - sz) |
293 | return log_error_errno(SYNTHETIC_ERRNO(EOVERFLOW), "File too large, overflow"); | |
56ebfaf1 | 294 | |
c40d82ab LP |
295 | if (j->written_uncompressed >= j->uncompressed_max) { |
296 | too_much = true; | |
297 | goto finish; | |
298 | } | |
56ebfaf1 | 299 | |
c40d82ab LP |
300 | if (j->written_uncompressed + sz > j->uncompressed_max) { |
301 | too_much = true; | |
302 | sz = j->uncompressed_max - j->written_uncompressed; /* since we have the data in memory | |
303 | * already, we might as well write it to | |
304 | * disk to the max */ | |
305 | } | |
56ebfaf1 LP |
306 | |
307 | if (j->disk_fd >= 0) { | |
308 | ||
bf284aee | 309 | if (S_ISREG(j->disk_stat.st_mode) && j->offset == UINT64_MAX) { |
c40d82ab LP |
310 | ssize_t n; |
311 | ||
56ebfaf1 | 312 | n = sparse_write(j->disk_fd, p, sz, 64); |
e986910e | 313 | if (n < 0) |
c40d82ab LP |
314 | return log_error_errno((int) n, "Failed to write file: %m"); |
315 | if ((size_t) n < sz) | |
316 | return log_error_errno(SYNTHETIC_ERRNO(EIO), "Short write"); | |
317 | } else { | |
318 | r = loop_write(j->disk_fd, p, sz, false); | |
319 | if (r < 0) | |
320 | return log_error_errno(r, "Failed to write file: %m"); | |
e986910e | 321 | } |
c40d82ab | 322 | } |
56ebfaf1 | 323 | |
c40d82ab | 324 | if (j->disk_fd < 0 || j->force_memory) { |
319a4f4b | 325 | if (!GREEDY_REALLOC(j->payload, j->payload_size + sz)) |
56ebfaf1 LP |
326 | return log_oom(); |
327 | ||
8af3cf74 | 328 | memcpy(j->payload + j->payload_size, p, sz); |
56ebfaf1 LP |
329 | j->payload_size += sz; |
330 | } | |
331 | ||
332 | j->written_uncompressed += sz; | |
333 | ||
c40d82ab LP |
334 | finish: |
335 | if (too_much) | |
336 | return log_error_errno(SYNTHETIC_ERRNO(EFBIG), "File overly large, refusing."); | |
337 | ||
56ebfaf1 LP |
338 | return 0; |
339 | } | |
340 | ||
dc2c282b | 341 | static int pull_job_write_compressed(PullJob *j, void *p, size_t sz) { |
56ebfaf1 LP |
342 | int r; |
343 | ||
344 | assert(j); | |
345 | assert(p); | |
8af3cf74 LP |
346 | |
347 | if (sz <= 0) | |
348 | return 0; | |
56ebfaf1 | 349 | |
baaa35ad ZJS |
350 | if (j->written_compressed + sz < j->written_compressed) |
351 | return log_error_errno(SYNTHETIC_ERRNO(EOVERFLOW), "File too large, overflow"); | |
56ebfaf1 | 352 | |
baaa35ad ZJS |
353 | if (j->written_compressed + sz > j->compressed_max) |
354 | return log_error_errno(SYNTHETIC_ERRNO(EFBIG), "File overly large, refusing."); | |
56ebfaf1 | 355 | |
f5fbe71d | 356 | if (j->content_length != UINT64_MAX && |
baaa35ad ZJS |
357 | j->written_compressed + sz > j->content_length) |
358 | return log_error_errno(SYNTHETIC_ERRNO(EFBIG), | |
359 | "Content length incorrect."); | |
56ebfaf1 | 360 | |
98c38001 LP |
361 | if (j->checksum_context) |
362 | gcry_md_write(j->checksum_context, p, sz); | |
85dbc41d | 363 | |
3e2cda69 LP |
364 | r = import_uncompress(&j->compress, p, sz, pull_job_write_uncompressed, j); |
365 | if (r < 0) | |
366 | return r; | |
56ebfaf1 LP |
367 | |
368 | j->written_compressed += sz; | |
369 | ||
370 | return 0; | |
371 | } | |
372 | ||
dc2c282b | 373 | static int pull_job_open_disk(PullJob *j) { |
56ebfaf1 LP |
374 | int r; |
375 | ||
376 | assert(j); | |
377 | ||
378 | if (j->on_open_disk) { | |
379 | r = j->on_open_disk(j); | |
380 | if (r < 0) | |
381 | return r; | |
382 | } | |
383 | ||
384 | if (j->disk_fd >= 0) { | |
c40d82ab LP |
385 | if (fstat(j->disk_fd, &j->disk_stat) < 0) |
386 | return log_error_errno(errno, "Failed to stat disk file: %m"); | |
56ebfaf1 | 387 | |
c40d82ab LP |
388 | if (j->offset != UINT64_MAX) { |
389 | if (lseek(j->disk_fd, j->offset, SEEK_SET) == (off_t) -1) | |
56ebfaf1 | 390 | return log_error_errno(errno, "Failed to seek on file descriptor: %m"); |
56ebfaf1 LP |
391 | } |
392 | } | |
393 | ||
98c38001 | 394 | if (j->calc_checksum) { |
eef836ed YW |
395 | initialize_libgcrypt(false); |
396 | ||
baaa35ad ZJS |
397 | if (gcry_md_open(&j->checksum_context, GCRY_MD_SHA256, 0) != 0) |
398 | return log_error_errno(SYNTHETIC_ERRNO(EIO), | |
399 | "Failed to initialize hash context."); | |
85dbc41d LP |
400 | } |
401 | ||
56ebfaf1 LP |
402 | return 0; |
403 | } | |
404 | ||
dc2c282b | 405 | static int pull_job_detect_compression(PullJob *j) { |
56ebfaf1 LP |
406 | _cleanup_free_ uint8_t *stub = NULL; |
407 | size_t stub_size; | |
408 | ||
409 | int r; | |
410 | ||
411 | assert(j); | |
412 | ||
3e2cda69 LP |
413 | r = import_uncompress_detect(&j->compress, j->payload, j->payload_size); |
414 | if (r < 0) | |
415 | return log_error_errno(r, "Failed to initialize compressor: %m"); | |
416 | if (r == 0) | |
56ebfaf1 LP |
417 | return 0; |
418 | ||
3e2cda69 | 419 | log_debug("Stream is compressed: %s", import_compress_type_to_string(j->compress.type)); |
56ebfaf1 | 420 | |
dc2c282b | 421 | r = pull_job_open_disk(j); |
56ebfaf1 LP |
422 | if (r < 0) |
423 | return r; | |
424 | ||
425 | /* Now, take the payload we read so far, and decompress it */ | |
426 | stub = j->payload; | |
427 | stub_size = j->payload_size; | |
428 | ||
429 | j->payload = NULL; | |
430 | j->payload_size = 0; | |
431 | ||
dc2c282b | 432 | j->state = PULL_JOB_RUNNING; |
56ebfaf1 | 433 | |
dc2c282b | 434 | r = pull_job_write_compressed(j, stub, stub_size); |
56ebfaf1 LP |
435 | if (r < 0) |
436 | return r; | |
437 | ||
438 | return 0; | |
439 | } | |
440 | ||
dc2c282b LP |
441 | static size_t pull_job_write_callback(void *contents, size_t size, size_t nmemb, void *userdata) { |
442 | PullJob *j = userdata; | |
56ebfaf1 LP |
443 | size_t sz = size * nmemb; |
444 | int r; | |
445 | ||
446 | assert(contents); | |
447 | assert(j); | |
448 | ||
449 | switch (j->state) { | |
450 | ||
dc2c282b | 451 | case PULL_JOB_ANALYZING: |
56ebfaf1 LP |
452 | /* Let's first check what it actually is */ |
453 | ||
319a4f4b | 454 | if (!GREEDY_REALLOC(j->payload, j->payload_size + sz)) { |
56ebfaf1 LP |
455 | r = log_oom(); |
456 | goto fail; | |
457 | } | |
458 | ||
8af3cf74 | 459 | memcpy(j->payload + j->payload_size, contents, sz); |
56ebfaf1 LP |
460 | j->payload_size += sz; |
461 | ||
dc2c282b | 462 | r = pull_job_detect_compression(j); |
56ebfaf1 LP |
463 | if (r < 0) |
464 | goto fail; | |
465 | ||
466 | break; | |
467 | ||
dc2c282b | 468 | case PULL_JOB_RUNNING: |
56ebfaf1 | 469 | |
dc2c282b | 470 | r = pull_job_write_compressed(j, contents, sz); |
56ebfaf1 LP |
471 | if (r < 0) |
472 | goto fail; | |
473 | ||
474 | break; | |
475 | ||
dc2c282b LP |
476 | case PULL_JOB_DONE: |
477 | case PULL_JOB_FAILED: | |
56ebfaf1 LP |
478 | r = -ESTALE; |
479 | goto fail; | |
480 | ||
481 | default: | |
04499a70 | 482 | assert_not_reached(); |
56ebfaf1 LP |
483 | } |
484 | ||
485 | return sz; | |
486 | ||
487 | fail: | |
dc2c282b | 488 | pull_job_finish(j, r); |
56ebfaf1 LP |
489 | return 0; |
490 | } | |
491 | ||
5d80583d LP |
492 | static int http_status_ok(CURLcode status) { |
493 | /* Consider all HTTP status code in the 2xx range as OK */ | |
494 | return status >= 200 && status <= 299; | |
495 | } | |
496 | ||
497 | static int http_status_etag_exists(CURLcode status) { | |
498 | /* This one is special, it's triggered by our etag mgmt logic */ | |
499 | return status == 304; | |
500 | } | |
501 | ||
dc2c282b | 502 | static size_t pull_job_header_callback(void *contents, size_t size, size_t nmemb, void *userdata) { |
273cb07d | 503 | _cleanup_free_ char *length = NULL, *last_modified = NULL, *etag = NULL; |
56ebfaf1 | 504 | size_t sz = size * nmemb; |
6792cbbc LP |
505 | PullJob *j = userdata; |
506 | CURLcode code; | |
507 | long status; | |
56ebfaf1 LP |
508 | int r; |
509 | ||
510 | assert(contents); | |
511 | assert(j); | |
512 | ||
3742095b | 513 | if (IN_SET(j->state, PULL_JOB_DONE, PULL_JOB_FAILED)) { |
56ebfaf1 LP |
514 | r = -ESTALE; |
515 | goto fail; | |
516 | } | |
517 | ||
dc2c282b | 518 | assert(j->state == PULL_JOB_ANALYZING); |
56ebfaf1 | 519 | |
6792cbbc LP |
520 | code = curl_easy_getinfo(j->curl, CURLINFO_RESPONSE_CODE, &status); |
521 | if (code != CURLE_OK) { | |
c40d82ab | 522 | r = log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to retrieve response code: %s", curl_easy_strerror(code)); |
6792cbbc LP |
523 | goto fail; |
524 | } | |
525 | ||
5d80583d LP |
526 | if (http_status_ok(status) || http_status_etag_exists(status)) { |
527 | /* Check Etag on OK and etag exists responses. */ | |
6792cbbc | 528 | |
5d80583d LP |
529 | r = curl_header_strdup(contents, sz, "ETag:", &etag); |
530 | if (r < 0) { | |
531 | log_oom(); | |
532 | goto fail; | |
533 | } | |
534 | if (r > 0) { | |
535 | free_and_replace(j->etag, etag); | |
536 | ||
537 | if (strv_contains(j->old_etags, j->etag)) { | |
538 | log_info("Image already downloaded. Skipping download. (%s)", j->etag); | |
539 | j->etag_exists = true; | |
540 | pull_job_finish(j, 0); | |
541 | return sz; | |
542 | } | |
56ebfaf1 | 543 | |
56ebfaf1 LP |
544 | return sz; |
545 | } | |
5d80583d | 546 | } |
56ebfaf1 | 547 | |
5d80583d LP |
548 | if (!http_status_ok(status)) /* Let's ignore the rest here, these requests are probably redirects and |
549 | * stuff where the headers aren't interesting to us */ | |
56ebfaf1 | 550 | return sz; |
56ebfaf1 LP |
551 | |
552 | r = curl_header_strdup(contents, sz, "Content-Length:", &length); | |
553 | if (r < 0) { | |
554 | log_oom(); | |
555 | goto fail; | |
556 | } | |
557 | if (r > 0) { | |
558 | (void) safe_atou64(length, &j->content_length); | |
559 | ||
f5fbe71d | 560 | if (j->content_length != UINT64_MAX) { |
56ebfaf1 | 561 | if (j->content_length > j->compressed_max) { |
22fd4a8f | 562 | r = log_error_errno(SYNTHETIC_ERRNO(EFBIG), "Content too large."); |
56ebfaf1 LP |
563 | goto fail; |
564 | } | |
565 | ||
2b59bf51 | 566 | log_info("Downloading %s for %s.", FORMAT_BYTES(j->content_length), j->url); |
56ebfaf1 LP |
567 | } |
568 | ||
569 | return sz; | |
570 | } | |
571 | ||
572 | r = curl_header_strdup(contents, sz, "Last-Modified:", &last_modified); | |
573 | if (r < 0) { | |
574 | log_oom(); | |
575 | goto fail; | |
576 | } | |
577 | if (r > 0) { | |
578 | (void) curl_parse_http_time(last_modified, &j->mtime); | |
579 | return sz; | |
580 | } | |
581 | ||
ff2670ad LP |
582 | if (j->on_header) { |
583 | r = j->on_header(j, contents, sz); | |
584 | if (r < 0) | |
585 | goto fail; | |
586 | } | |
587 | ||
56ebfaf1 LP |
588 | return sz; |
589 | ||
590 | fail: | |
dc2c282b | 591 | pull_job_finish(j, r); |
56ebfaf1 LP |
592 | return 0; |
593 | } | |
594 | ||
dc2c282b LP |
595 | static int pull_job_progress_callback(void *userdata, curl_off_t dltotal, curl_off_t dlnow, curl_off_t ultotal, curl_off_t ulnow) { |
596 | PullJob *j = userdata; | |
56ebfaf1 LP |
597 | unsigned percent; |
598 | usec_t n; | |
599 | ||
600 | assert(j); | |
601 | ||
602 | if (dltotal <= 0) | |
603 | return 0; | |
604 | ||
605 | percent = ((100 * dlnow) / dltotal); | |
606 | n = now(CLOCK_MONOTONIC); | |
607 | ||
608 | if (n > j->last_status_usec + USEC_PER_SEC && | |
68c913fd LP |
609 | percent != j->progress_percent && |
610 | dlnow < dltotal) { | |
56ebfaf1 LP |
611 | |
612 | if (n - j->start_usec > USEC_PER_SEC && dlnow > 0) { | |
613 | usec_t left, done; | |
614 | ||
615 | done = n - j->start_usec; | |
616 | left = (usec_t) (((double) done * (double) dltotal) / dlnow) - done; | |
617 | ||
90bc083b LP |
618 | log_info("Got %u%% of %s. %s left at %s/s.", |
619 | percent, | |
620 | j->url, | |
5291f26d | 621 | FORMAT_TIMESPAN(left, USEC_PER_SEC), |
2b59bf51 | 622 | FORMAT_BYTES((uint64_t) ((double) dlnow / ((double) done / (double) USEC_PER_SEC)))); |
56ebfaf1 LP |
623 | } else |
624 | log_info("Got %u%% of %s.", percent, j->url); | |
625 | ||
626 | j->progress_percent = percent; | |
627 | j->last_status_usec = n; | |
7079cfef LP |
628 | |
629 | if (j->on_progress) | |
630 | j->on_progress(j); | |
56ebfaf1 LP |
631 | } |
632 | ||
633 | return 0; | |
634 | } | |
635 | ||
c40d82ab LP |
636 | int pull_job_new( |
637 | PullJob **ret, | |
638 | const char *url, | |
639 | CurlGlue *glue, | |
640 | void *userdata) { | |
641 | ||
dc2c282b | 642 | _cleanup_(pull_job_unrefp) PullJob *j = NULL; |
0d94088e | 643 | _cleanup_free_ char *u = NULL; |
56ebfaf1 LP |
644 | |
645 | assert(url); | |
646 | assert(glue); | |
647 | assert(ret); | |
648 | ||
0d94088e | 649 | u = strdup(url); |
9b5b4bed | 650 | if (!u) |
56ebfaf1 LP |
651 | return -ENOMEM; |
652 | ||
0d94088e YW |
653 | j = new(PullJob, 1); |
654 | if (!j) | |
56ebfaf1 LP |
655 | return -ENOMEM; |
656 | ||
0d94088e YW |
657 | *j = (PullJob) { |
658 | .state = PULL_JOB_INIT, | |
659 | .disk_fd = -1, | |
c40d82ab | 660 | .close_disk_fd = true, |
0d94088e YW |
661 | .userdata = userdata, |
662 | .glue = glue, | |
f5fbe71d | 663 | .content_length = UINT64_MAX, |
0d94088e YW |
664 | .start_usec = now(CLOCK_MONOTONIC), |
665 | .compressed_max = 64LLU * 1024LLU * 1024LLU * 1024LLU, /* 64GB safety limit */ | |
666 | .uncompressed_max = 64LLU * 1024LLU * 1024LLU * 1024LLU, /* 64GB safety limit */ | |
0d94088e | 667 | .url = TAKE_PTR(u), |
c40d82ab LP |
668 | .offset = UINT64_MAX, |
669 | .sync = true, | |
0d94088e YW |
670 | }; |
671 | ||
1cc6c93a | 672 | *ret = TAKE_PTR(j); |
56ebfaf1 LP |
673 | |
674 | return 0; | |
675 | } | |
676 | ||
dc2c282b | 677 | int pull_job_begin(PullJob *j) { |
56ebfaf1 LP |
678 | int r; |
679 | ||
680 | assert(j); | |
681 | ||
dc2c282b | 682 | if (j->state != PULL_JOB_INIT) |
56ebfaf1 LP |
683 | return -EBUSY; |
684 | ||
685 | r = curl_glue_make(&j->curl, j->url, j); | |
686 | if (r < 0) | |
687 | return r; | |
688 | ||
689 | if (!strv_isempty(j->old_etags)) { | |
690 | _cleanup_free_ char *cc = NULL, *hdr = NULL; | |
691 | ||
692 | cc = strv_join(j->old_etags, ", "); | |
693 | if (!cc) | |
694 | return -ENOMEM; | |
695 | ||
b910cc72 | 696 | hdr = strjoin("If-None-Match: ", cc); |
56ebfaf1 LP |
697 | if (!hdr) |
698 | return -ENOMEM; | |
699 | ||
ff2670ad LP |
700 | if (!j->request_header) { |
701 | j->request_header = curl_slist_new(hdr, NULL); | |
702 | if (!j->request_header) | |
703 | return -ENOMEM; | |
704 | } else { | |
705 | struct curl_slist *l; | |
706 | ||
707 | l = curl_slist_append(j->request_header, hdr); | |
708 | if (!l) | |
709 | return -ENOMEM; | |
710 | ||
711 | j->request_header = l; | |
712 | } | |
713 | } | |
56ebfaf1 | 714 | |
ff2670ad | 715 | if (j->request_header) { |
56ebfaf1 LP |
716 | if (curl_easy_setopt(j->curl, CURLOPT_HTTPHEADER, j->request_header) != CURLE_OK) |
717 | return -EIO; | |
718 | } | |
719 | ||
dc2c282b | 720 | if (curl_easy_setopt(j->curl, CURLOPT_WRITEFUNCTION, pull_job_write_callback) != CURLE_OK) |
56ebfaf1 LP |
721 | return -EIO; |
722 | ||
723 | if (curl_easy_setopt(j->curl, CURLOPT_WRITEDATA, j) != CURLE_OK) | |
724 | return -EIO; | |
725 | ||
dc2c282b | 726 | if (curl_easy_setopt(j->curl, CURLOPT_HEADERFUNCTION, pull_job_header_callback) != CURLE_OK) |
56ebfaf1 LP |
727 | return -EIO; |
728 | ||
729 | if (curl_easy_setopt(j->curl, CURLOPT_HEADERDATA, j) != CURLE_OK) | |
730 | return -EIO; | |
731 | ||
dc2c282b | 732 | if (curl_easy_setopt(j->curl, CURLOPT_XFERINFOFUNCTION, pull_job_progress_callback) != CURLE_OK) |
56ebfaf1 LP |
733 | return -EIO; |
734 | ||
735 | if (curl_easy_setopt(j->curl, CURLOPT_XFERINFODATA, j) != CURLE_OK) | |
736 | return -EIO; | |
737 | ||
738 | if (curl_easy_setopt(j->curl, CURLOPT_NOPROGRESS, 0) != CURLE_OK) | |
739 | return -EIO; | |
740 | ||
741 | r = curl_glue_add(j->glue, j->curl); | |
742 | if (r < 0) | |
743 | return r; | |
744 | ||
dc2c282b | 745 | j->state = PULL_JOB_ANALYZING; |
56ebfaf1 LP |
746 | |
747 | return 0; | |
748 | } |