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