1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
7 #include "alloc-util.h"
9 #include "format-util.h"
10 #include "gcrypt-util.h"
11 #include "hexdecoct.h"
12 #include "import-util.h"
14 #include "machine-pool.h"
15 #include "parse-util.h"
16 #include "pull-common.h"
18 #include "string-util.h"
20 #include "xattr-util.h"
22 PullJob
* pull_job_unref(PullJob
*j
) {
26 curl_glue_remove_and_free(j
->glue
, j
->curl
);
27 curl_slist_free_all(j
->request_header
);
29 safe_close(j
->disk_fd
);
31 import_compress_free(&j
->compress
);
33 if (j
->checksum_context
)
34 gcry_md_close(j
->checksum_context
);
38 strv_free(j
->old_etags
);
45 static void pull_job_finish(PullJob
*j
, int ret
) {
48 if (IN_SET(j
->state
, PULL_JOB_DONE
, PULL_JOB_FAILED
))
52 j
->state
= PULL_JOB_DONE
;
53 j
->progress_percent
= 100;
54 log_info("Download of %s complete.", j
->url
);
56 j
->state
= PULL_JOB_FAILED
;
64 static int pull_job_restart(PullJob
*j
, const char *new_url
) {
70 r
= free_and_strdup(&j
->url
, new_url
);
74 j
->state
= PULL_JOB_INIT
;
76 j
->payload
= mfree(j
->payload
);
78 j
->written_compressed
= 0;
79 j
->written_uncompressed
= 0;
80 j
->content_length
= UINT64_MAX
;
81 j
->etag
= mfree(j
->etag
);
82 j
->etag_exists
= false;
84 j
->checksum
= mfree(j
->checksum
);
86 curl_glue_remove_and_free(j
->glue
, j
->curl
);
89 curl_slist_free_all(j
->request_header
);
90 j
->request_header
= NULL
;
92 import_compress_free(&j
->compress
);
94 if (j
->checksum_context
) {
95 gcry_md_close(j
->checksum_context
);
96 j
->checksum_context
= NULL
;
99 r
= pull_job_begin(j
);
106 void pull_job_curl_on_finished(CurlGlue
*g
, CURL
*curl
, CURLcode result
) {
112 if (curl_easy_getinfo(curl
, CURLINFO_PRIVATE
, (char **)&j
) != CURLE_OK
)
115 if (!j
|| IN_SET(j
->state
, PULL_JOB_DONE
, PULL_JOB_FAILED
))
118 if (result
!= CURLE_OK
) {
119 log_error("Transfer failed: %s", curl_easy_strerror(result
));
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
));
129 } else if (status
== 304) {
130 log_info("Image already downloaded. Skipping download.");
131 j
->etag_exists
= true;
134 } else if (status
>= 300) {
136 if (status
== 404 && j
->on_not_found
) {
137 _cleanup_free_
char *new_url
= NULL
;
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
);
144 if (r
> 0) { /* A new url to use */
147 r
= pull_job_restart(j
, new_url
);
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
));
163 log_error("HTTP request to %s failed with code %li.", j
->url
, status
);
166 } else if (status
< 200) {
167 log_error("HTTP request to %s finished with unexpected code %li.", j
->url
, status
);
172 if (j
->state
!= PULL_JOB_RUNNING
) {
173 log_error("Premature connection termination.");
178 if (j
->content_length
!= UINT64_MAX
&&
179 j
->content_length
!= j
->written_compressed
) {
180 log_error("Download truncated.");
185 if (j
->checksum_context
) {
188 k
= gcry_md_read(j
->checksum_context
, GCRY_MD_SHA256
);
190 log_error("Failed to get checksum.");
195 j
->checksum
= hexmem(k
, gcry_md_get_algo_dlen(GCRY_MD_SHA256
));
201 log_debug("SHA256 of %s is %s.", j
->url
, j
->checksum
);
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 */
208 if (ftruncate(j
->disk_fd
, j
->written_uncompressed
) < 0) {
209 r
= log_error_errno(errno
, "Failed to truncate file: %m");
214 (void) fsetxattr(j
->disk_fd
, "user.source_etag", j
->etag
, strlen(j
->etag
), 0);
216 (void) fsetxattr(j
->disk_fd
, "user.source_url", j
->url
, strlen(j
->url
), 0);
219 struct timespec ut
[2];
221 timespec_store(&ut
[0], j
->mtime
);
223 (void) futimens(j
->disk_fd
, ut
);
225 (void) fd_setcrtime(j
->disk_fd
, j
->mtime
);
232 pull_job_finish(j
, r
);
235 static int pull_job_write_uncompressed(const void *p
, size_t sz
, void *userdata
) {
236 PullJob
*j
= userdata
;
245 if (j
->written_uncompressed
+ sz
< j
->written_uncompressed
)
246 return log_error_errno(SYNTHETIC_ERRNO(EOVERFLOW
),
247 "File too large, overflow");
249 if (j
->written_uncompressed
+ sz
> j
->uncompressed_max
)
250 return log_error_errno(SYNTHETIC_ERRNO(EFBIG
),
251 "File overly large, refusing");
253 if (j
->disk_fd
>= 0) {
256 n
= sparse_write(j
->disk_fd
, p
, sz
, 64);
258 n
= write(j
->disk_fd
, p
, sz
);
263 return log_error_errno((int) n
, "Failed to write file: %m");
265 return log_error_errno(SYNTHETIC_ERRNO(EIO
), "Short write");
268 if (!GREEDY_REALLOC(j
->payload
, j
->payload_size
+ sz
))
271 memcpy(j
->payload
+ j
->payload_size
, p
, sz
);
272 j
->payload_size
+= sz
;
275 j
->written_uncompressed
+= sz
;
280 static int pull_job_write_compressed(PullJob
*j
, void *p
, size_t sz
) {
289 if (j
->written_compressed
+ sz
< j
->written_compressed
)
290 return log_error_errno(SYNTHETIC_ERRNO(EOVERFLOW
), "File too large, overflow");
292 if (j
->written_compressed
+ sz
> j
->compressed_max
)
293 return log_error_errno(SYNTHETIC_ERRNO(EFBIG
), "File overly large, refusing.");
295 if (j
->content_length
!= UINT64_MAX
&&
296 j
->written_compressed
+ sz
> j
->content_length
)
297 return log_error_errno(SYNTHETIC_ERRNO(EFBIG
),
298 "Content length incorrect.");
300 if (j
->checksum_context
)
301 gcry_md_write(j
->checksum_context
, p
, sz
);
303 r
= import_uncompress(&j
->compress
, p
, sz
, pull_job_write_uncompressed
, j
);
307 j
->written_compressed
+= sz
;
312 static int pull_job_open_disk(PullJob
*j
) {
317 if (j
->on_open_disk
) {
318 r
= j
->on_open_disk(j
);
323 if (j
->disk_fd
>= 0) {
324 /* Check if we can do sparse files */
326 if (lseek(j
->disk_fd
, SEEK_SET
, 0) == 0)
327 j
->allow_sparse
= true;
330 return log_error_errno(errno
, "Failed to seek on file descriptor: %m");
332 j
->allow_sparse
= false;
336 if (j
->calc_checksum
) {
337 initialize_libgcrypt(false);
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.");
347 static int pull_job_detect_compression(PullJob
*j
) {
348 _cleanup_free_
uint8_t *stub
= NULL
;
355 r
= import_uncompress_detect(&j
->compress
, j
->payload
, j
->payload_size
);
357 return log_error_errno(r
, "Failed to initialize compressor: %m");
361 log_debug("Stream is compressed: %s", import_compress_type_to_string(j
->compress
.type
));
363 r
= pull_job_open_disk(j
);
367 /* Now, take the payload we read so far, and decompress it */
369 stub_size
= j
->payload_size
;
374 j
->state
= PULL_JOB_RUNNING
;
376 r
= pull_job_write_compressed(j
, stub
, stub_size
);
383 static size_t pull_job_write_callback(void *contents
, size_t size
, size_t nmemb
, void *userdata
) {
384 PullJob
*j
= userdata
;
385 size_t sz
= size
* nmemb
;
393 case PULL_JOB_ANALYZING
:
394 /* Let's first check what it actually is */
396 if (!GREEDY_REALLOC(j
->payload
, j
->payload_size
+ sz
)) {
401 memcpy(j
->payload
+ j
->payload_size
, contents
, sz
);
402 j
->payload_size
+= sz
;
404 r
= pull_job_detect_compression(j
);
410 case PULL_JOB_RUNNING
:
412 r
= pull_job_write_compressed(j
, contents
, sz
);
419 case PULL_JOB_FAILED
:
424 assert_not_reached("Impossible state.");
430 pull_job_finish(j
, r
);
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;
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;
444 static size_t pull_job_header_callback(void *contents
, size_t size
, size_t nmemb
, void *userdata
) {
445 _cleanup_free_
char *length
= NULL
, *last_modified
= NULL
, *etag
= NULL
;
446 size_t sz
= size
* nmemb
;
447 PullJob
*j
= userdata
;
455 if (IN_SET(j
->state
, PULL_JOB_DONE
, PULL_JOB_FAILED
)) {
460 assert(j
->state
== PULL_JOB_ANALYZING
);
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
));
469 if (http_status_ok(status
) || http_status_etag_exists(status
)) {
470 /* Check Etag on OK and etag exists responses. */
472 r
= curl_header_strdup(contents
, sz
, "ETag:", &etag
);
478 free_and_replace(j
->etag
, etag
);
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);
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 */
495 r
= curl_header_strdup(contents
, sz
, "Content-Length:", &length
);
501 (void) safe_atou64(length
, &j
->content_length
);
503 if (j
->content_length
!= UINT64_MAX
) {
504 char bytes
[FORMAT_BYTES_MAX
];
506 if (j
->content_length
> j
->compressed_max
) {
507 log_error("Content too large.");
512 log_info("Downloading %s for %s.", format_bytes(bytes
, sizeof(bytes
), j
->content_length
), j
->url
);
518 r
= curl_header_strdup(contents
, sz
, "Last-Modified:", &last_modified
);
524 (void) curl_parse_http_time(last_modified
, &j
->mtime
);
529 r
= j
->on_header(j
, contents
, sz
);
537 pull_job_finish(j
, r
);
541 static int pull_job_progress_callback(void *userdata
, curl_off_t dltotal
, curl_off_t dlnow
, curl_off_t ultotal
, curl_off_t ulnow
) {
542 PullJob
*j
= userdata
;
551 percent
= ((100 * dlnow
) / dltotal
);
552 n
= now(CLOCK_MONOTONIC
);
554 if (n
> j
->last_status_usec
+ USEC_PER_SEC
&&
555 percent
!= j
->progress_percent
&&
557 char buf
[FORMAT_TIMESPAN_MAX
];
559 if (n
- j
->start_usec
> USEC_PER_SEC
&& dlnow
> 0) {
560 char y
[FORMAT_BYTES_MAX
];
563 done
= n
- j
->start_usec
;
564 left
= (usec_t
) (((double) done
* (double) dltotal
) / dlnow
) - done
;
566 log_info("Got %u%% of %s. %s left at %s/s.",
569 format_timespan(buf
, sizeof(buf
), left
, USEC_PER_SEC
),
570 format_bytes(y
, sizeof(y
), (uint64_t) ((double) dlnow
/ ((double) done
/ (double) USEC_PER_SEC
))));
572 log_info("Got %u%% of %s.", percent
, j
->url
);
574 j
->progress_percent
= percent
;
575 j
->last_status_usec
= n
;
584 int pull_job_new(PullJob
**ret
, const char *url
, CurlGlue
*glue
, void *userdata
) {
585 _cleanup_(pull_job_unrefp
) PullJob
*j
= NULL
;
586 _cleanup_free_
char *u
= NULL
;
601 .state
= PULL_JOB_INIT
,
603 .userdata
= userdata
,
605 .content_length
= UINT64_MAX
,
606 .start_usec
= now(CLOCK_MONOTONIC
),
607 .compressed_max
= 64LLU * 1024LLU * 1024LLU * 1024LLU, /* 64GB safety limit */
608 .uncompressed_max
= 64LLU * 1024LLU * 1024LLU * 1024LLU, /* 64GB safety limit */
617 int pull_job_begin(PullJob
*j
) {
622 if (j
->state
!= PULL_JOB_INIT
)
625 r
= curl_glue_make(&j
->curl
, j
->url
, j
);
629 if (!strv_isempty(j
->old_etags
)) {
630 _cleanup_free_
char *cc
= NULL
, *hdr
= NULL
;
632 cc
= strv_join(j
->old_etags
, ", ");
636 hdr
= strjoin("If-None-Match: ", cc
);
640 if (!j
->request_header
) {
641 j
->request_header
= curl_slist_new(hdr
, NULL
);
642 if (!j
->request_header
)
645 struct curl_slist
*l
;
647 l
= curl_slist_append(j
->request_header
, hdr
);
651 j
->request_header
= l
;
655 if (j
->request_header
) {
656 if (curl_easy_setopt(j
->curl
, CURLOPT_HTTPHEADER
, j
->request_header
) != CURLE_OK
)
660 if (curl_easy_setopt(j
->curl
, CURLOPT_WRITEFUNCTION
, pull_job_write_callback
) != CURLE_OK
)
663 if (curl_easy_setopt(j
->curl
, CURLOPT_WRITEDATA
, j
) != CURLE_OK
)
666 if (curl_easy_setopt(j
->curl
, CURLOPT_HEADERFUNCTION
, pull_job_header_callback
) != CURLE_OK
)
669 if (curl_easy_setopt(j
->curl
, CURLOPT_HEADERDATA
, j
) != CURLE_OK
)
672 if (curl_easy_setopt(j
->curl
, CURLOPT_XFERINFOFUNCTION
, pull_job_progress_callback
) != CURLE_OK
)
675 if (curl_easy_setopt(j
->curl
, CURLOPT_XFERINFODATA
, j
) != CURLE_OK
)
678 if (curl_easy_setopt(j
->curl
, CURLOPT_NOPROGRESS
, 0) != CURLE_OK
)
681 r
= curl_glue_add(j
->glue
, j
->curl
);
685 j
->state
= PULL_JOB_ANALYZING
;