pull-job.c revision 3e2cda698f05d7290a8b9444d2c7d5c2599b2a27
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen This file is part of systemd.
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen Copyright 2015 Lennart Poettering
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen systemd is free software; you can redistribute it and/or modify it
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen under the terms of the GNU Lesser General Public License as published by
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen the Free Software Foundation; either version 2.1 of the License, or
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen (at your option) any later version.
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen systemd is distributed in the hope that it will be useful, but
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen WITHOUT ANY WARRANTY; without even the implied warranty of
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen Lesser General Public License for more details.
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen You should have received a copy of the GNU Lesser General Public License
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen along with systemd; If not, see <http://www.gnu.org/licenses/>.
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen/* Grow the /var/lib/machines directory after each 10MiB written */
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen#define PULL_GROW_INTERVAL_BYTES (UINT64_C(10) * UINT64_C(1024) * UINT64_C(1024))
99634696183dfabae20104e58157c69029a11594Tom Gundersenstatic void pull_job_finish(PullJob *j, int ret) {
99634696183dfabae20104e58157c69029a11594Tom Gundersenvoid pull_job_curl_on_finished(CurlGlue *g, CURL *curl, CURLcode result) {
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen if (curl_easy_getinfo(curl, CURLINFO_PRIVATE, &j) != CURLE_OK)
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen if (!j || j->state == PULL_JOB_DONE || j->state == PULL_JOB_FAILED)
99634696183dfabae20104e58157c69029a11594Tom Gundersen log_error("Transfer failed: %s", curl_easy_strerror(result));
20af7091de0cdf92bf299addfc3f96c3ef805bd8Tom Gundersen code = curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &status);
7c16313f11e3953f3fe4dbf544f2d36f58d14138Tom Gundersen log_error("Failed to retrieve response code: %s", curl_easy_strerror(code));
7c16313f11e3953f3fe4dbf544f2d36f58d14138Tom Gundersen log_info("Image already downloaded. Skipping download.");
3733eec3e292e4ddb4cba5eb8d3bd8cbee7102d8Lennart Poettering log_error("HTTP request to %s failed with code %li.", j->url, status);
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen log_error("HTTP request to %s finished with unexpected code %li.", j->url, status);
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen log_error("Premature connection termination.");
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen k = gcry_md_read(j->checksum_context, GCRY_MD_SHA256);
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen j->checksum = hexmem(k, gcry_md_get_algo_dlen(GCRY_MD_SHA256));
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen log_debug("SHA256 of %s is %s.", j->url, j->checksum);
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen /* Make sure the file size is right, in case the file was
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen * sparse and we just seeked for the last part */
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen if (ftruncate(j->disk_fd, j->written_uncompressed) < 0) {
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen log_error_errno(errno, "Failed to truncate file: %m");
3733eec3e292e4ddb4cba5eb8d3bd8cbee7102d8Lennart Poettering (void) fsetxattr(j->disk_fd, "user.source_etag", j->etag, strlen(j->etag), 0);
3733eec3e292e4ddb4cba5eb8d3bd8cbee7102d8Lennart Poettering (void) fsetxattr(j->disk_fd, "user.source_url", j->url, strlen(j->url), 0);
3bdace9bf779ce051f00c14914b35c3a26164aa9Lennart Poettering if (j->mtime != 0) {
3bdace9bf779ce051f00c14914b35c3a26164aa9Lennart Poettering (void) fd_setcrtime(j->disk_fd, j->mtime);
3bdace9bf779ce051f00c14914b35c3a26164aa9Lennart Poetteringstatic int pull_job_write_uncompressed(const void *p, size_t sz, void *userdata) {
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen if (j->written_uncompressed + sz < j->written_uncompressed) {
3733eec3e292e4ddb4cba5eb8d3bd8cbee7102d8Lennart Poettering if (j->written_uncompressed + sz > j->uncompressed_max) {
586ac6f711e2eccceb12421df22fca4f117226c4Lennart Poettering if (j->grow_machine_directory && j->written_since_last_grow >= PULL_GROW_INTERVAL_BYTES) {
a6f1e036de8f212f33ead7f5387c297afd8be26eTom Gundersen log_error_errno(errno, "Failed to write file: %m");
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen if (!GREEDY_REALLOC(j->payload, j->payload_allocated, j->payload_size + sz))
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersenstatic int pull_job_write_compressed(PullJob *j, void *p, size_t sz) {
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen if (j->written_compressed + sz < j->written_compressed) {
ff734080aa02cd70b13bc0fdeec4a5886166163aTom Gundersen if (j->written_compressed + sz > j->compressed_max) {
ff734080aa02cd70b13bc0fdeec4a5886166163aTom Gundersen j->written_compressed + sz > j->content_length) {
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen r = import_uncompress(&j->compress, p, sz, pull_job_write_uncompressed, j);
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen if (j->disk_fd >= 0) {
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen /* Check if we can do sparse files */
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen return log_error_errno(errno, "Failed to seek on file descriptor: %m");
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen if (gcry_md_open(&j->checksum_context, GCRY_MD_SHA256, 0) != 0) {
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen log_error("Failed to initialize hash context.");
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersenstatic int pull_job_detect_compression(PullJob *j) {
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen r = import_uncompress_detect(&j->compress, j->payload, j->payload_size);
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen return log_error_errno(r, "Failed to initialize compressor: %m");
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen log_debug("Stream is compressed: %s", import_compress_type_to_string(j->compress.type));
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen /* Now, take the payload we read so far, and decompress it */
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen r = pull_job_write_compressed(j, stub, stub_size);
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersenstatic size_t pull_job_write_callback(void *contents, size_t size, size_t nmemb, void *userdata) {
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen switch (j->state) {
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen /* Let's first check what it actually is */
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen if (!GREEDY_REALLOC(j->payload, j->payload_allocated, j->payload_size + sz)) {
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen memcpy(j->payload + j->payload_size, contents, sz);
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen r = pull_job_write_compressed(j, contents, sz);
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersenstatic size_t pull_job_header_callback(void *contents, size_t size, size_t nmemb, void *userdata) {
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen _cleanup_free_ char *length = NULL, *last_modified = NULL;
a6f1e036de8f212f33ead7f5387c297afd8be26eTom Gundersen if (j->state == PULL_JOB_DONE || j->state == PULL_JOB_FAILED) {
4dc355680460fdc8e0d590d8572dff1b6a257d88Tom Gundersen r = curl_header_strdup(contents, sz, "ETag:", &etag);
bd57b45029ff25067704c9538e79f31e71c10045Tom Gundersen log_info("Image already downloaded. Skipping download.");
4dc355680460fdc8e0d590d8572dff1b6a257d88Tom Gundersen r = curl_header_strdup(contents, sz, "Content-Length:", &length);
4dc355680460fdc8e0d590d8572dff1b6a257d88Tom Gundersen (void) safe_atou64(length, &j->content_length);
4dc355680460fdc8e0d590d8572dff1b6a257d88Tom Gundersen log_info("Downloading %s for %s.", format_bytes(bytes, sizeof(bytes), j->content_length), j->url);
4dc355680460fdc8e0d590d8572dff1b6a257d88Tom Gundersen r = curl_header_strdup(contents, sz, "Last-Modified:", &last_modified);
a6f1e036de8f212f33ead7f5387c297afd8be26eTom Gundersen (void) curl_parse_http_time(last_modified, &j->mtime);
4dc355680460fdc8e0d590d8572dff1b6a257d88Tom Gundersenstatic int pull_job_progress_callback(void *userdata, curl_off_t dltotal, curl_off_t dlnow, curl_off_t ultotal, curl_off_t ulnow) {
c7d9ffe6d629cb5b34dd749e4a88b190b11a0f48Tom Gundersen if (n - j->start_usec > USEC_PER_SEC && dlnow > 0) {
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen left = (usec_t) (((double) done * (double) dltotal) / dlnow) - done;
59b8f6b628145586e87b8a4f6e29c755ad7d61edTom Gundersen format_timespan(buf, sizeof(buf), left, USEC_PER_SEC),
59b8f6b628145586e87b8a4f6e29c755ad7d61edTom Gundersen format_bytes(y, sizeof(y), (uint64_t) ((double) dlnow / ((double) done / (double) USEC_PER_SEC))));
1a04db0fc9d08fffe80d6d7b5b60459295922b11Lennart Poetteringint pull_job_new(PullJob **ret, const char *url, CurlGlue *glue, void *userdata) {
1a04db0fc9d08fffe80d6d7b5b60459295922b11Lennart Poettering _cleanup_(pull_job_unrefp) PullJob *j = NULL;
8eb9058dc1f99a5eb9b8726a978fcc0720837a10Lennart Poettering j->compressed_max = j->uncompressed_max = 8LLU * 1024LLU * 1024LLU * 1024LLU; /* 8GB */
527503444ef24ae03c73cf85128c7acbb1146f3cTom Gundersen j->request_header = curl_slist_new(hdr, NULL);
527503444ef24ae03c73cf85128c7acbb1146f3cTom Gundersen l = curl_slist_append(j->request_header, hdr);
527503444ef24ae03c73cf85128c7acbb1146f3cTom Gundersen if (curl_easy_setopt(j->curl, CURLOPT_HTTPHEADER, j->request_header) != CURLE_OK)
816e2e7af96886e4a43194042ef61ba9fec2c77dTom Gundersen if (curl_easy_setopt(j->curl, CURLOPT_WRITEFUNCTION, pull_job_write_callback) != CURLE_OK)
816e2e7af96886e4a43194042ef61ba9fec2c77dTom Gundersen if (curl_easy_setopt(j->curl, CURLOPT_WRITEDATA, j) != CURLE_OK)
c7d9ffe6d629cb5b34dd749e4a88b190b11a0f48Tom Gundersen if (curl_easy_setopt(j->curl, CURLOPT_HEADERFUNCTION, pull_job_header_callback) != CURLE_OK)
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen if (curl_easy_setopt(j->curl, CURLOPT_HEADERDATA, j) != CURLE_OK)
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen if (curl_easy_setopt(j->curl, CURLOPT_XFERINFOFUNCTION, pull_job_progress_callback) != CURLE_OK)
816e2e7af96886e4a43194042ef61ba9fec2c77dTom Gundersen if (curl_easy_setopt(j->curl, CURLOPT_XFERINFODATA, j) != CURLE_OK)