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