2 This file is part of systemd.
4 Copyright 2015 Lennart Poettering
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 #include <sys/xattr.h>
22 #include "alloc-util.h"
24 #include "hexdecoct.h"
25 #include "import-util.h"
27 #include "machine-pool.h"
28 #include "parse-util.h"
29 #include "pull-common.h"
31 #include "string-util.h"
33 #include "xattr-util.h"
35 PullJob
* pull_job_unref(PullJob
*j
) {
39 curl_glue_remove_and_free(j
->glue
, j
->curl
);
40 curl_slist_free_all(j
->request_header
);
42 safe_close(j
->disk_fd
);
44 import_compress_free(&j
->compress
);
46 if (j
->checksum_context
)
47 gcry_md_close(j
->checksum_context
);
51 strv_free(j
->old_etags
);
58 static void pull_job_finish(PullJob
*j
, int ret
) {
61 if (IN_SET(j
->state
, PULL_JOB_DONE
, PULL_JOB_FAILED
))
65 j
->state
= PULL_JOB_DONE
;
66 j
->progress_percent
= 100;
67 log_info("Download of %s complete.", j
->url
);
69 j
->state
= PULL_JOB_FAILED
;
77 static int pull_job_restart(PullJob
*j
) {
79 char *chksum_url
= NULL
;
81 r
= import_url_change_last_component(j
->url
, "SHA256SUMS", &chksum_url
);
87 j
->state
= PULL_JOB_INIT
;
88 j
->payload
= mfree(j
->payload
);
90 j
->payload_allocated
= 0;
91 j
->written_compressed
= 0;
92 j
->written_uncompressed
= 0;
93 j
->written_since_last_grow
= 0;
95 r
= pull_job_begin(j
);
102 void pull_job_curl_on_finished(CurlGlue
*g
, CURL
*curl
, CURLcode result
) {
108 if (curl_easy_getinfo(curl
, CURLINFO_PRIVATE
, (char **)&j
) != CURLE_OK
)
111 if (!j
|| IN_SET(j
->state
, PULL_JOB_DONE
, PULL_JOB_FAILED
))
114 if (result
!= CURLE_OK
) {
115 log_error("Transfer failed: %s", curl_easy_strerror(result
));
120 code
= curl_easy_getinfo(curl
, CURLINFO_RESPONSE_CODE
, &status
);
121 if (code
!= CURLE_OK
) {
122 log_error("Failed to retrieve response code: %s", curl_easy_strerror(code
));
125 } else if (status
== 304) {
126 log_info("Image already downloaded. Skipping download.");
127 j
->etag_exists
= true;
130 } else if (status
>= 300) {
131 if (status
== 404 && j
->style
== VERIFICATION_PER_FILE
) {
133 /* retry pull job with SHA256SUMS file */
134 r
= pull_job_restart(j
);
138 code
= curl_easy_getinfo(j
->curl
, CURLINFO_RESPONSE_CODE
, &status
);
139 if (code
!= CURLE_OK
) {
140 log_error("Failed to retrieve response code: %s", curl_easy_strerror(code
));
146 j
->style
= VERIFICATION_PER_DIRECTORY
;
151 log_error("HTTP request to %s failed with code %li.", j
->url
, status
);
154 } else if (status
< 200) {
155 log_error("HTTP request to %s finished with unexpected code %li.", j
->url
, status
);
160 if (j
->state
!= PULL_JOB_RUNNING
) {
161 log_error("Premature connection termination.");
166 if (j
->content_length
!= (uint64_t) -1 &&
167 j
->content_length
!= j
->written_compressed
) {
168 log_error("Download truncated.");
173 if (j
->checksum_context
) {
176 k
= gcry_md_read(j
->checksum_context
, GCRY_MD_SHA256
);
178 log_error("Failed to get checksum.");
183 j
->checksum
= hexmem(k
, gcry_md_get_algo_dlen(GCRY_MD_SHA256
));
189 log_debug("SHA256 of %s is %s.", j
->url
, j
->checksum
);
192 if (j
->disk_fd
>= 0 && j
->allow_sparse
) {
193 /* Make sure the file size is right, in case the file was
194 * sparse and we just seeked for the last part */
196 if (ftruncate(j
->disk_fd
, j
->written_uncompressed
) < 0) {
197 r
= log_error_errno(errno
, "Failed to truncate file: %m");
202 (void) fsetxattr(j
->disk_fd
, "user.source_etag", j
->etag
, strlen(j
->etag
), 0);
204 (void) fsetxattr(j
->disk_fd
, "user.source_url", j
->url
, strlen(j
->url
), 0);
207 struct timespec ut
[2];
209 timespec_store(&ut
[0], j
->mtime
);
211 (void) futimens(j
->disk_fd
, ut
);
213 (void) fd_setcrtime(j
->disk_fd
, j
->mtime
);
220 pull_job_finish(j
, r
);
223 static int pull_job_write_uncompressed(const void *p
, size_t sz
, void *userdata
) {
224 PullJob
*j
= userdata
;
233 if (j
->written_uncompressed
+ sz
< j
->written_uncompressed
) {
234 log_error("File too large, overflow");
238 if (j
->written_uncompressed
+ sz
> j
->uncompressed_max
) {
239 log_error("File overly large, refusing");
243 if (j
->disk_fd
>= 0) {
245 if (j
->grow_machine_directory
&& j
->written_since_last_grow
>= GROW_INTERVAL_BYTES
) {
246 j
->written_since_last_grow
= 0;
247 grow_machine_directory();
251 n
= sparse_write(j
->disk_fd
, p
, sz
, 64);
253 n
= write(j
->disk_fd
, p
, sz
);
255 return log_error_errno(errno
, "Failed to write file: %m");
256 if ((size_t) n
< sz
) {
257 log_error("Short write");
262 if (!GREEDY_REALLOC(j
->payload
, j
->payload_allocated
, j
->payload_size
+ sz
))
265 memcpy(j
->payload
+ j
->payload_size
, p
, sz
);
266 j
->payload_size
+= sz
;
269 j
->written_uncompressed
+= sz
;
270 j
->written_since_last_grow
+= sz
;
275 static int pull_job_write_compressed(PullJob
*j
, void *p
, size_t sz
) {
284 if (j
->written_compressed
+ sz
< j
->written_compressed
) {
285 log_error("File too large, overflow");
289 if (j
->written_compressed
+ sz
> j
->compressed_max
) {
290 log_error("File overly large, refusing.");
294 if (j
->content_length
!= (uint64_t) -1 &&
295 j
->written_compressed
+ sz
> j
->content_length
) {
296 log_error("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 if (gcry_md_open(&j
->checksum_context
, GCRY_MD_SHA256
, 0) != 0) {
338 log_error("Failed to initialize hash context.");
346 static int pull_job_detect_compression(PullJob
*j
) {
347 _cleanup_free_
uint8_t *stub
= NULL
;
354 r
= import_uncompress_detect(&j
->compress
, j
->payload
, j
->payload_size
);
356 return log_error_errno(r
, "Failed to initialize compressor: %m");
360 log_debug("Stream is compressed: %s", import_compress_type_to_string(j
->compress
.type
));
362 r
= pull_job_open_disk(j
);
366 /* Now, take the payload we read so far, and decompress it */
368 stub_size
= j
->payload_size
;
372 j
->payload_allocated
= 0;
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_allocated
, 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 size_t pull_job_header_callback(void *contents
, size_t size
, size_t nmemb
, void *userdata
) {
435 PullJob
*j
= userdata
;
436 size_t sz
= size
* nmemb
;
437 _cleanup_free_
char *length
= NULL
, *last_modified
= NULL
;
444 if (IN_SET(j
->state
, PULL_JOB_DONE
, PULL_JOB_FAILED
)) {
449 assert(j
->state
== PULL_JOB_ANALYZING
);
451 r
= curl_header_strdup(contents
, sz
, "ETag:", &etag
);
460 if (strv_contains(j
->old_etags
, j
->etag
)) {
461 log_info("Image already downloaded. Skipping download.");
462 j
->etag_exists
= true;
463 pull_job_finish(j
, 0);
470 r
= curl_header_strdup(contents
, sz
, "Content-Length:", &length
);
476 (void) safe_atou64(length
, &j
->content_length
);
478 if (j
->content_length
!= (uint64_t) -1) {
479 char bytes
[FORMAT_BYTES_MAX
];
481 if (j
->content_length
> j
->compressed_max
) {
482 log_error("Content too large.");
487 log_info("Downloading %s for %s.", format_bytes(bytes
, sizeof(bytes
), j
->content_length
), j
->url
);
493 r
= curl_header_strdup(contents
, sz
, "Last-Modified:", &last_modified
);
499 (void) curl_parse_http_time(last_modified
, &j
->mtime
);
504 r
= j
->on_header(j
, contents
, sz
);
512 pull_job_finish(j
, r
);
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
;
526 percent
= ((100 * dlnow
) / dltotal
);
527 n
= now(CLOCK_MONOTONIC
);
529 if (n
> j
->last_status_usec
+ USEC_PER_SEC
&&
530 percent
!= j
->progress_percent
&&
532 char buf
[FORMAT_TIMESPAN_MAX
];
534 if (n
- j
->start_usec
> USEC_PER_SEC
&& dlnow
> 0) {
535 char y
[FORMAT_BYTES_MAX
];
538 done
= n
- j
->start_usec
;
539 left
= (usec_t
) (((double) done
* (double) dltotal
) / dlnow
) - done
;
541 log_info("Got %u%% of %s. %s left at %s/s.",
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
))));
547 log_info("Got %u%% of %s.", percent
, j
->url
);
549 j
->progress_percent
= percent
;
550 j
->last_status_usec
= n
;
559 int pull_job_new(PullJob
**ret
, const char *url
, CurlGlue
*glue
, void *userdata
) {
560 _cleanup_(pull_job_unrefp
) PullJob
*j
= NULL
;
566 j
= new0(PullJob
, 1);
570 j
->state
= PULL_JOB_INIT
;
572 j
->userdata
= userdata
;
574 j
->content_length
= (uint64_t) -1;
575 j
->start_usec
= now(CLOCK_MONOTONIC
);
576 j
->compressed_max
= j
->uncompressed_max
= 64LLU * 1024LLU * 1024LLU * 1024LLU; /* 64GB safety limit */
577 j
->style
= VERIFICATION_STYLE_UNSET
;
579 j
->url
= strdup(url
);
589 int pull_job_begin(PullJob
*j
) {
594 if (j
->state
!= PULL_JOB_INIT
)
597 if (j
->grow_machine_directory
)
598 grow_machine_directory();
600 r
= curl_glue_make(&j
->curl
, j
->url
, j
);
604 if (!strv_isempty(j
->old_etags
)) {
605 _cleanup_free_
char *cc
= NULL
, *hdr
= NULL
;
607 cc
= strv_join(j
->old_etags
, ", ");
611 hdr
= strappend("If-None-Match: ", cc
);
615 if (!j
->request_header
) {
616 j
->request_header
= curl_slist_new(hdr
, NULL
);
617 if (!j
->request_header
)
620 struct curl_slist
*l
;
622 l
= curl_slist_append(j
->request_header
, hdr
);
626 j
->request_header
= l
;
630 if (j
->request_header
) {
631 if (curl_easy_setopt(j
->curl
, CURLOPT_HTTPHEADER
, j
->request_header
) != CURLE_OK
)
635 if (curl_easy_setopt(j
->curl
, CURLOPT_WRITEFUNCTION
, pull_job_write_callback
) != CURLE_OK
)
638 if (curl_easy_setopt(j
->curl
, CURLOPT_WRITEDATA
, j
) != CURLE_OK
)
641 if (curl_easy_setopt(j
->curl
, CURLOPT_HEADERFUNCTION
, pull_job_header_callback
) != CURLE_OK
)
644 if (curl_easy_setopt(j
->curl
, CURLOPT_HEADERDATA
, j
) != CURLE_OK
)
647 if (curl_easy_setopt(j
->curl
, CURLOPT_XFERINFOFUNCTION
, pull_job_progress_callback
) != CURLE_OK
)
650 if (curl_easy_setopt(j
->curl
, CURLOPT_XFERINFODATA
, j
) != CURLE_OK
)
653 if (curl_easy_setopt(j
->curl
, CURLOPT_NOPROGRESS
, 0) != CURLE_OK
)
656 r
= curl_glue_add(j
->glue
, j
->curl
);
660 j
->state
= PULL_JOB_ANALYZING
;