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