| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <fcntl.h> |
| 4 |
#include <sys/stat.h> |
| 5 |
#include <sys/xattr.h> |
| 6 |
#include <unistd.h> |
| 7 |
|
| 8 |
#include "alloc-util.h" |
| 9 |
#include "compress.h" |
| 10 |
#include "crypto-util.h" |
| 11 |
#include "curl-util.h" |
| 12 |
#include "fd-util.h" |
| 13 |
#include "format-util.h" |
| 14 |
#include "hexdecoct.h" |
| 15 |
#include "io-util.h" |
| 16 |
#include "log.h" |
| 17 |
#include "parse-util.h" |
| 18 |
#include "pull-common.h" |
| 19 |
#include "pull-job.h" |
| 20 |
#include "string-util.h" |
| 21 |
#include "strv.h" |
| 22 |
#include "sync-util.h" |
| 23 |
#include "time-util.h" |
| 24 |
#include "xattr-util.h" |
| 25 |
|
| 26 |
static int http_status_ok(CURLcode status) { |
| 27 |
/* Consider all HTTP status code in the 2xx range as OK */ |
| 28 |
return status >= 200 && status <= 299; |
| 29 |
} |
| 30 |
|
| 31 |
static int http_status_etag_exists(CURLcode status) { |
| 32 |
/* This one is special, it's triggered by our etag mgmt logic */ |
| 33 |
return status == 304; |
| 34 |
} |
| 35 |
|
| 36 |
static int http_status_need_authentication(CURLcode status) { |
| 37 |
return status == 401; |
| 38 |
} |
| 39 |
|
| 40 |
void pull_job_close_disk_fd(PullJob *j) { |
| 41 |
if (!j) |
| 42 |
return; |
| 43 |
|
| 44 |
if (j->close_disk_fd) |
| 45 |
safe_close(j->disk_fd); |
| 46 |
|
| 47 |
j->disk_fd = -EBADF; |
| 48 |
} |
| 49 |
|
| 50 |
PullJob* pull_job_unref(PullJob *j) { |
| 51 |
if (!j) |
| 52 |
return NULL; |
| 53 |
|
| 54 |
pull_job_close_disk_fd(j); |
| 55 |
|
| 56 |
curl_slot_unref(j->slot); |
| 57 |
sym_curl_slist_free_all(j->request_header); |
| 58 |
|
| 59 |
j->compress = compressor_free(j->compress); |
| 60 |
|
| 61 |
if (j->checksum_ctx) |
| 62 |
sym_EVP_MD_CTX_free(j->checksum_ctx); |
| 63 |
|
| 64 |
free(j->url); |
| 65 |
free(j->etag); |
| 66 |
strv_free(j->old_etags); |
| 67 |
iovec_done(&j->payload); |
| 68 |
iovec_done(&j->checksum); |
| 69 |
iovec_done(&j->expected_checksum); |
| 70 |
free(j->content_type); |
| 71 |
|
| 72 |
if (j->free_userdata) |
| 73 |
j->free_userdata(j->userdata); |
| 74 |
free(j->description); |
| 75 |
free(j->authentication_challenge); |
| 76 |
|
| 77 |
return mfree(j); |
| 78 |
} |
| 79 |
|
| 80 |
static const char* pull_job_description(PullJob *j) { |
| 81 |
assert(j); |
| 82 |
|
| 83 |
return j->description ?: j->url; |
| 84 |
} |
| 85 |
|
| 86 |
static int pull_job_finish(PullJob *j, int ret) { |
| 87 |
assert(j); |
| 88 |
|
| 89 |
/* Returns 0 so callers in int-returning paths can `return pull_job_finish(...)` directly. */ |
| 90 |
|
| 91 |
if (IN_SET(j->state, PULL_JOB_DONE, PULL_JOB_FAILED)) |
| 92 |
return 0; |
| 93 |
|
| 94 |
if (ret == 0) { |
| 95 |
j->state = PULL_JOB_DONE; |
| 96 |
j->progress_percent = 100; |
| 97 |
log_info("Download of %s complete.", pull_job_description(j)); |
| 98 |
} else { |
| 99 |
j->state = PULL_JOB_FAILED; |
| 100 |
j->error = ret; |
| 101 |
} |
| 102 |
|
| 103 |
if (j->on_finished) |
| 104 |
j->on_finished(j); |
| 105 |
|
| 106 |
return 0; |
| 107 |
} |
| 108 |
|
| 109 |
int pull_job_restart(PullJob *j, const char *new_url) { |
| 110 |
int r; |
| 111 |
|
| 112 |
assert(j); |
| 113 |
|
| 114 |
/* If an URL is specified we retry the same request, just towards a different URL. If the URL is NULL |
| 115 |
* then we'll fire the same request again (which is useful if some parameters have been changed) */ |
| 116 |
|
| 117 |
if (new_url) { |
| 118 |
r = free_and_strdup(&j->url, new_url); |
| 119 |
if (r < 0) |
| 120 |
return r; |
| 121 |
} |
| 122 |
|
| 123 |
j->state = PULL_JOB_INIT; |
| 124 |
j->error = 0; |
| 125 |
iovec_done(&j->payload); |
| 126 |
j->written_compressed = 0; |
| 127 |
j->written_uncompressed = 0; |
| 128 |
j->content_length = UINT64_MAX; |
| 129 |
j->etag = mfree(j->etag); |
| 130 |
j->etag_exists = false; |
| 131 |
j->mtime = 0; |
| 132 |
iovec_done(&j->checksum); |
| 133 |
j->content_type = mfree(j->content_type); |
| 134 |
|
| 135 |
if (new_url) { |
| 136 |
/* Reset expectations if the URL changes */ |
| 137 |
iovec_done(&j->expected_checksum); |
| 138 |
j->expected_content_length = UINT64_MAX; |
| 139 |
} |
| 140 |
|
| 141 |
j->slot = curl_slot_unref(j->slot); |
| 142 |
|
| 143 |
j->compress = compressor_free(j->compress); |
| 144 |
|
| 145 |
if (j->checksum_ctx) { |
| 146 |
sym_EVP_MD_CTX_free(j->checksum_ctx); |
| 147 |
j->checksum_ctx = NULL; |
| 148 |
} |
| 149 |
|
| 150 |
r = pull_job_begin(j); |
| 151 |
if (r < 0) |
| 152 |
return r; |
| 153 |
|
| 154 |
return 0; |
| 155 |
} |
| 156 |
|
| 157 |
static uint64_t pull_job_content_length_effective(PullJob *j) { |
| 158 |
assert(j); |
| 159 |
|
| 160 |
if (j->expected_content_length != UINT64_MAX) |
| 161 |
return j->expected_content_length; |
| 162 |
|
| 163 |
return j->content_length; |
| 164 |
} |
| 165 |
|
| 166 |
static int pull_job_write_uncompressed(const void *p, size_t sz, void *userdata) { |
| 167 |
PullJob *j = ASSERT_PTR(userdata); |
| 168 |
bool too_much = false; |
| 169 |
int r; |
| 170 |
|
| 171 |
assert(p); |
| 172 |
assert(sz > 0); |
| 173 |
|
| 174 |
if (j->written_uncompressed > UINT64_MAX - sz) |
| 175 |
return log_error_errno(SYNTHETIC_ERRNO(EOVERFLOW), "File too large, overflow"); |
| 176 |
|
| 177 |
if (j->written_uncompressed >= j->uncompressed_max) { |
| 178 |
too_much = true; |
| 179 |
goto finish; |
| 180 |
} |
| 181 |
|
| 182 |
if (j->written_uncompressed + sz > j->uncompressed_max) { |
| 183 |
too_much = true; |
| 184 |
sz = j->uncompressed_max - j->written_uncompressed; /* since we have the data in memory |
| 185 |
* already, we might as well write it to |
| 186 |
* disk to the max */ |
| 187 |
} |
| 188 |
|
| 189 |
if (j->disk_fd >= 0) { |
| 190 |
|
| 191 |
if (S_ISREG(j->disk_stat.st_mode) && j->offset == UINT64_MAX) { |
| 192 |
ssize_t n; |
| 193 |
|
| 194 |
n = sparse_write(j->disk_fd, p, sz, 64); |
| 195 |
if (n < 0) |
| 196 |
return log_error_errno((int) n, "Failed to write file: %m"); |
| 197 |
if ((size_t) n < sz) |
| 198 |
return log_error_errno(SYNTHETIC_ERRNO(EIO), "Short write"); |
| 199 |
} else { |
| 200 |
r = loop_write(j->disk_fd, p, sz); |
| 201 |
if (r < 0) |
| 202 |
return log_error_errno(r, "Failed to write file: %m"); |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
if (j->disk_fd < 0 || j->force_memory) { |
| 207 |
uint8_t *a = j->payload.iov_base; |
| 208 |
|
| 209 |
if (!GREEDY_REALLOC(a, j->payload.iov_len + sz + 1)) |
| 210 |
return log_oom(); |
| 211 |
|
| 212 |
*((uint8_t*) mempcpy(a + j->payload.iov_len, p, sz)) = 0; |
| 213 |
j->payload.iov_base = a; |
| 214 |
j->payload.iov_len += sz; |
| 215 |
} |
| 216 |
|
| 217 |
j->written_uncompressed += sz; |
| 218 |
|
| 219 |
finish: |
| 220 |
if (too_much) |
| 221 |
return log_error_errno(SYNTHETIC_ERRNO(EFBIG), "File overly large, refusing."); |
| 222 |
|
| 223 |
return 0; |
| 224 |
} |
| 225 |
|
| 226 |
static int pull_job_write_compressed(PullJob *j, const struct iovec *data) { |
| 227 |
int r; |
| 228 |
|
| 229 |
assert(j); |
| 230 |
assert(iovec_is_valid(data)); |
| 231 |
|
| 232 |
if (!iovec_is_set(data)) |
| 233 |
return 0; |
| 234 |
|
| 235 |
if (j->written_compressed + data->iov_len < j->written_compressed) |
| 236 |
return log_error_errno(SYNTHETIC_ERRNO(EOVERFLOW), "File too large, overflow"); |
| 237 |
|
| 238 |
if (j->written_compressed + data->iov_len > j->compressed_max) |
| 239 |
return log_error_errno(SYNTHETIC_ERRNO(EFBIG), "File overly large, refusing."); |
| 240 |
|
| 241 |
uint64_t cl = pull_job_content_length_effective(j); |
| 242 |
if (cl != UINT64_MAX && |
| 243 |
j->written_compressed + data->iov_len > cl) |
| 244 |
return log_error_errno(SYNTHETIC_ERRNO(EFBIG), |
| 245 |
"Content length incorrect."); |
| 246 |
|
| 247 |
if (j->checksum_ctx) { |
| 248 |
r = sym_EVP_DigestUpdate(j->checksum_ctx, data->iov_base, data->iov_len); |
| 249 |
if (r == 0) |
| 250 |
return log_error_errno(SYNTHETIC_ERRNO(EIO), |
| 251 |
"Could not hash chunk."); |
| 252 |
} |
| 253 |
|
| 254 |
r = decompressor_push(j->compress, data->iov_base, data->iov_len, pull_job_write_uncompressed, j); |
| 255 |
if (r < 0) |
| 256 |
return r; |
| 257 |
|
| 258 |
j->written_compressed += data->iov_len; |
| 259 |
|
| 260 |
return 0; |
| 261 |
} |
| 262 |
|
| 263 |
static int pull_job_open_disk(PullJob *j) { |
| 264 |
int r; |
| 265 |
|
| 266 |
assert(j); |
| 267 |
|
| 268 |
if (j->on_open_disk) { |
| 269 |
r = j->on_open_disk(j); |
| 270 |
if (r < 0) |
| 271 |
return r; |
| 272 |
} |
| 273 |
|
| 274 |
if (j->disk_fd >= 0) { |
| 275 |
if (fstat(j->disk_fd, &j->disk_stat) < 0) |
| 276 |
return log_error_errno(errno, "Failed to stat disk file: %m"); |
| 277 |
|
| 278 |
if (j->offset != UINT64_MAX) { |
| 279 |
if (lseek(j->disk_fd, j->offset, SEEK_SET) < 0) |
| 280 |
return log_error_errno(errno, "Failed to seek on file descriptor: %m"); |
| 281 |
} |
| 282 |
} |
| 283 |
|
| 284 |
if (j->calc_checksum) { |
| 285 |
r = DLOPEN_LIBCRYPTO(LOG_ERR, SD_ELF_NOTE_DLOPEN_PRIORITY_RECOMMENDED); |
| 286 |
if (r < 0) |
| 287 |
return r; |
| 288 |
|
| 289 |
j->checksum_ctx = sym_EVP_MD_CTX_new(); |
| 290 |
if (!j->checksum_ctx) |
| 291 |
return log_oom(); |
| 292 |
|
| 293 |
r = sym_EVP_DigestInit_ex(j->checksum_ctx, sym_EVP_sha256(), NULL); |
| 294 |
if (r == 0) |
| 295 |
return log_error_errno(SYNTHETIC_ERRNO(EIO), |
| 296 |
"Failed to initialize hash context."); |
| 297 |
} |
| 298 |
|
| 299 |
return 0; |
| 300 |
} |
| 301 |
|
| 302 |
static int pull_job_begin_running(PullJob *j) { |
| 303 |
int r; |
| 304 |
|
| 305 |
assert(j); |
| 306 |
assert(j->state == PULL_JOB_ANALYZING); |
| 307 |
assert(j->compress); |
| 308 |
|
| 309 |
r = pull_job_open_disk(j); |
| 310 |
if (r < 0) |
| 311 |
return r; |
| 312 |
|
| 313 |
/* Now, take the payload we read so far, and decompress it */ |
| 314 |
_cleanup_(iovec_done) struct iovec stub = TAKE_STRUCT(j->payload); |
| 315 |
|
| 316 |
j->state = PULL_JOB_RUNNING; |
| 317 |
|
| 318 |
r = pull_job_write_compressed(j, &stub); |
| 319 |
if (r < 0) |
| 320 |
return r; |
| 321 |
|
| 322 |
return 0; |
| 323 |
} |
| 324 |
|
| 325 |
static int pull_job_curl_on_finished(CurlSlot *slot, CURL *curl, CURLcode result, void *userdata) { |
| 326 |
PullJob *j = ASSERT_PTR(userdata); |
| 327 |
char *scheme = NULL; |
| 328 |
CURLcode code; |
| 329 |
int r; |
| 330 |
|
| 331 |
if (IN_SET(j->state, PULL_JOB_DONE, PULL_JOB_FAILED)) |
| 332 |
return 0; |
| 333 |
|
| 334 |
code = sym_curl_easy_getinfo(curl, CURLINFO_SCHEME, &scheme); |
| 335 |
if (code != CURLE_OK || !scheme) |
| 336 |
return pull_job_finish(j, log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to retrieve URL scheme.")); |
| 337 |
|
| 338 |
if (strcaseeq(scheme, "FILE") && result == CURLE_FILE_COULDNT_READ_FILE && j->on_not_found) { |
| 339 |
_cleanup_free_ char *new_url = NULL; |
| 340 |
|
| 341 |
/* This resource wasn't found, but the implementer wants to maybe let us know a new URL, query for it. */ |
| 342 |
r = j->on_not_found(j, &new_url); |
| 343 |
if (r < 0) |
| 344 |
return pull_job_finish(j, r); |
| 345 |
if (r > 0) { /* A new url to use */ |
| 346 |
assert(new_url); |
| 347 |
|
| 348 |
r = pull_job_restart(j, new_url); |
| 349 |
if (r < 0) |
| 350 |
return pull_job_finish(j, r); |
| 351 |
|
| 352 |
return 0; |
| 353 |
} |
| 354 |
|
| 355 |
/* if this didn't work, handle like any other error below */ |
| 356 |
} |
| 357 |
|
| 358 |
if (result != CURLE_OK) |
| 359 |
return pull_job_finish(j, log_error_errno(SYNTHETIC_ERRNO(EIO), "Transfer failed: %s", sym_curl_easy_strerror(result))); |
| 360 |
|
| 361 |
if (STRCASE_IN_SET(scheme, "HTTP", "HTTPS")) { |
| 362 |
long status; |
| 363 |
|
| 364 |
code = sym_curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &status); |
| 365 |
if (code != CURLE_OK) |
| 366 |
return pull_job_finish(j, log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to retrieve response code: %s", sym_curl_easy_strerror(code))); |
| 367 |
|
| 368 |
if (http_status_etag_exists(status)) { |
| 369 |
log_info("Image already downloaded. Skipping download."); |
| 370 |
j->etag_exists = true; |
| 371 |
return pull_job_finish(j, 0); |
| 372 |
} else if (http_status_need_authentication(status)) { |
| 373 |
log_info("Access to image requires authentication."); |
| 374 |
return pull_job_finish(j, -ENOKEY); |
| 375 |
} else if (status >= 300) { |
| 376 |
|
| 377 |
if (status == 404 && j->on_not_found) { |
| 378 |
_cleanup_free_ char *new_url = NULL; |
| 379 |
|
| 380 |
/* This resource wasn't found, but the implementer wants to maybe let us know a new URL, query for it. */ |
| 381 |
r = j->on_not_found(j, &new_url); |
| 382 |
if (r < 0) |
| 383 |
return pull_job_finish(j, r); |
| 384 |
|
| 385 |
if (r > 0) { /* A new url to use */ |
| 386 |
assert(new_url); |
| 387 |
|
| 388 |
r = pull_job_restart(j, new_url); |
| 389 |
if (r < 0) |
| 390 |
return pull_job_finish(j, r); |
| 391 |
|
| 392 |
code = sym_curl_easy_getinfo(curl_slot_get_easy(j->slot), CURLINFO_RESPONSE_CODE, &status); |
| 393 |
if (code != CURLE_OK) |
| 394 |
return pull_job_finish(j, log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to retrieve response code: %s", sym_curl_easy_strerror(code))); |
| 395 |
|
| 396 |
if (status == 0) |
| 397 |
return 0; |
| 398 |
} |
| 399 |
} |
| 400 |
|
| 401 |
return pull_job_finish(j, log_notice_errno( |
| 402 |
status == 404 ? SYNTHETIC_ERRNO(ENOMEDIUM) : SYNTHETIC_ERRNO(EIO), /* Make the most common error recognizable */ |
| 403 |
"HTTP request to %s failed with code %li.", j->url, status)); |
| 404 |
} else if (status < 200) |
| 405 |
return pull_job_finish(j, log_error_errno(SYNTHETIC_ERRNO(EIO), "HTTP request to %s finished with unexpected code %li.", j->url, status)); |
| 406 |
} |
| 407 |
|
| 408 |
if (j->state == PULL_JOB_ANALYZING) { |
| 409 |
/* When curl finished the download while we were still looking for a compression magic |
| 410 |
* header the content isn't compressed but should be written out as is. */ |
| 411 |
assert(result == CURLE_OK); |
| 412 |
|
| 413 |
r = decompressor_force_off(&j->compress); |
| 414 |
if (r < 0) |
| 415 |
return pull_job_finish(j, r); |
| 416 |
|
| 417 |
r = pull_job_begin_running(j); |
| 418 |
if (r < 0) |
| 419 |
return pull_job_finish(j, r); |
| 420 |
} |
| 421 |
|
| 422 |
if (j->state != PULL_JOB_RUNNING) |
| 423 |
return pull_job_finish(j, log_error_errno(SYNTHETIC_ERRNO(EIO), "Premature connection termination.")); |
| 424 |
|
| 425 |
uint64_t cl = pull_job_content_length_effective(j); |
| 426 |
if (cl != UINT64_MAX && |
| 427 |
cl != j->written_compressed) |
| 428 |
return pull_job_finish(j, log_error_errno(SYNTHETIC_ERRNO(EIO), "Download truncated.")); |
| 429 |
|
| 430 |
if (j->checksum_ctx) { |
| 431 |
unsigned checksum_len; |
| 432 |
|
| 433 |
iovec_done(&j->checksum); |
| 434 |
j->checksum.iov_base = malloc(EVP_MAX_MD_SIZE); |
| 435 |
if (!j->checksum.iov_base) |
| 436 |
return pull_job_finish(j, log_oom()); |
| 437 |
|
| 438 |
r = sym_EVP_DigestFinal_ex(j->checksum_ctx, j->checksum.iov_base, &checksum_len); |
| 439 |
if (r == 0) |
| 440 |
return pull_job_finish(j, log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to get checksum.")); |
| 441 |
assert(checksum_len <= EVP_MAX_MD_SIZE); |
| 442 |
j->checksum.iov_len = checksum_len; |
| 443 |
|
| 444 |
if (DEBUG_LOGGING) { |
| 445 |
_cleanup_free_ char *h = hexmem(j->checksum.iov_base, j->checksum.iov_len); |
| 446 |
if (!h) |
| 447 |
return pull_job_finish(j, log_oom()); |
| 448 |
|
| 449 |
log_debug("%s of %s is %s.", sym_EVP_MD_CTX_get0_name(j->checksum_ctx), pull_job_description(j), h); |
| 450 |
} |
| 451 |
|
| 452 |
if (iovec_is_set(&j->expected_checksum) && |
| 453 |
!iovec_equal(&j->checksum, &j->expected_checksum)) |
| 454 |
return pull_job_finish(j, log_error_errno(SYNTHETIC_ERRNO(EBADMSG), "Checksum of downloaded resource does not match expected checksum, yikes.")); |
| 455 |
} |
| 456 |
|
| 457 |
/* Do a couple of finishing disk operations, but only if we are the sole owner of the file (i.e. no |
| 458 |
* offset is specified, which indicates we only own the file partially) */ |
| 459 |
|
| 460 |
if (j->disk_fd >= 0) { |
| 461 |
|
| 462 |
if (S_ISREG(j->disk_stat.st_mode)) { |
| 463 |
|
| 464 |
if (j->offset == UINT64_MAX) { |
| 465 |
|
| 466 |
if (j->written_compressed > 0) { |
| 467 |
/* Make sure the file size is right, in case the file was sparse and |
| 468 |
* we just moved to the last part. */ |
| 469 |
if (ftruncate(j->disk_fd, j->written_uncompressed) < 0) |
| 470 |
return pull_job_finish(j, log_error_errno(errno, "Failed to truncate file: %m")); |
| 471 |
} |
| 472 |
|
| 473 |
if (j->etag) |
| 474 |
(void) fsetxattr(j->disk_fd, "user.source_etag", j->etag, strlen(j->etag), 0); |
| 475 |
if (j->url) |
| 476 |
(void) fsetxattr(j->disk_fd, "user.source_url", j->url, strlen(j->url), 0); |
| 477 |
|
| 478 |
if (j->mtime != 0) { |
| 479 |
struct timespec ut; |
| 480 |
|
| 481 |
timespec_store(&ut, j->mtime); |
| 482 |
|
| 483 |
if (futimens(j->disk_fd, (struct timespec[]) { ut, ut }) < 0) |
| 484 |
log_debug_errno(errno, "Failed to adjust atime/mtime of created image, ignoring: %m"); |
| 485 |
|
| 486 |
r = fd_setcrtime(j->disk_fd, j->mtime); |
| 487 |
if (r < 0) |
| 488 |
log_debug_errno(r, "Failed to adjust crtime of created image, ignoring: %m"); |
| 489 |
} |
| 490 |
} |
| 491 |
|
| 492 |
if (j->sync) { |
| 493 |
r = fsync_full(j->disk_fd); |
| 494 |
if (r < 0) |
| 495 |
return pull_job_finish(j, log_error_errno(r, "Failed to synchronize file to disk: %m")); |
| 496 |
} |
| 497 |
|
| 498 |
} else if (S_ISBLK(j->disk_stat.st_mode) && j->sync) { |
| 499 |
|
| 500 |
if (fsync(j->disk_fd) < 0) |
| 501 |
return pull_job_finish(j, log_error_errno(errno, "Failed to synchronize block device: %m")); |
| 502 |
} |
| 503 |
} |
| 504 |
|
| 505 |
log_info("Acquired %s for %s.", FORMAT_BYTES(j->written_uncompressed), pull_job_description(j)); |
| 506 |
|
| 507 |
return pull_job_finish(j, 0); |
| 508 |
} |
| 509 |
|
| 510 |
static int pull_job_detect_compression(PullJob *j) { |
| 511 |
int r; |
| 512 |
|
| 513 |
assert(j); |
| 514 |
|
| 515 |
r = decompressor_detect(&j->compress, j->payload.iov_base, j->payload.iov_len); |
| 516 |
if (r < 0) |
| 517 |
return log_error_errno(r, "Failed to initialize compressor: %m"); |
| 518 |
if (r == 0) |
| 519 |
return 0; |
| 520 |
|
| 521 |
log_debug("Stream is compressed: %s", compression_to_string(compressor_type(j->compress))); |
| 522 |
|
| 523 |
return pull_job_begin_running(j); |
| 524 |
} |
| 525 |
|
| 526 |
static size_t pull_job_write_callback(void *contents, size_t size, size_t nmemb, void *userdata) { |
| 527 |
PullJob *j = ASSERT_PTR(userdata); |
| 528 |
size_t sz = size * nmemb; |
| 529 |
int r; |
| 530 |
|
| 531 |
assert(contents); |
| 532 |
|
| 533 |
switch (j->state) { |
| 534 |
|
| 535 |
case PULL_JOB_ANALYZING: |
| 536 |
/* Let's first check what it actually is */ |
| 537 |
if (!iovec_append(&j->payload, &IOVEC_MAKE(contents, sz))) { |
| 538 |
r = log_oom(); |
| 539 |
goto fail; |
| 540 |
} |
| 541 |
|
| 542 |
r = pull_job_detect_compression(j); |
| 543 |
if (r < 0) |
| 544 |
goto fail; |
| 545 |
|
| 546 |
break; |
| 547 |
|
| 548 |
case PULL_JOB_RUNNING: |
| 549 |
r = pull_job_write_compressed(j, &IOVEC_MAKE(contents, sz)); |
| 550 |
if (r < 0) |
| 551 |
goto fail; |
| 552 |
|
| 553 |
break; |
| 554 |
|
| 555 |
case PULL_JOB_DONE: |
| 556 |
case PULL_JOB_FAILED: |
| 557 |
r = -ESTALE; |
| 558 |
goto fail; |
| 559 |
|
| 560 |
default: |
| 561 |
assert_not_reached(); |
| 562 |
} |
| 563 |
|
| 564 |
return sz; |
| 565 |
|
| 566 |
fail: |
| 567 |
pull_job_finish(j, r); |
| 568 |
return 0; |
| 569 |
} |
| 570 |
|
| 571 |
static size_t pull_job_header_callback(void *contents, size_t size, size_t nmemb, void *userdata) { |
| 572 |
_cleanup_free_ char *length = NULL, *last_modified = NULL, *etag = NULL, *ct = NULL; |
| 573 |
size_t sz = size * nmemb; |
| 574 |
PullJob *j = ASSERT_PTR(userdata); |
| 575 |
CURLcode code; |
| 576 |
long status; |
| 577 |
int r; |
| 578 |
|
| 579 |
assert(contents); |
| 580 |
|
| 581 |
if (IN_SET(j->state, PULL_JOB_DONE, PULL_JOB_FAILED)) { |
| 582 |
r = -ESTALE; |
| 583 |
goto fail; |
| 584 |
} |
| 585 |
|
| 586 |
assert(j->state == PULL_JOB_ANALYZING); |
| 587 |
|
| 588 |
code = sym_curl_easy_getinfo(curl_slot_get_easy(j->slot), CURLINFO_RESPONSE_CODE, &status); |
| 589 |
if (code != CURLE_OK) { |
| 590 |
r = log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to retrieve response code: %s", sym_curl_easy_strerror(code)); |
| 591 |
goto fail; |
| 592 |
} |
| 593 |
|
| 594 |
if (http_status_need_authentication(status)) { |
| 595 |
_cleanup_free_ char *challenge = NULL; |
| 596 |
|
| 597 |
r = curl_header_strdup(contents, sz, "WWW-Authenticate:", &challenge); |
| 598 |
if (r < 0) { |
| 599 |
log_oom(); |
| 600 |
goto fail; |
| 601 |
} |
| 602 |
if (r > 0) |
| 603 |
free_and_replace(j->authentication_challenge, challenge); |
| 604 |
return sz; |
| 605 |
} |
| 606 |
|
| 607 |
if (http_status_ok(status) || http_status_etag_exists(status)) { |
| 608 |
/* Check Etag on OK and etag exists responses. */ |
| 609 |
|
| 610 |
r = curl_header_strdup(contents, sz, "ETag:", &etag); |
| 611 |
if (r < 0) { |
| 612 |
log_oom(); |
| 613 |
goto fail; |
| 614 |
} |
| 615 |
if (r > 0) { |
| 616 |
free_and_replace(j->etag, etag); |
| 617 |
|
| 618 |
if (strv_contains(j->old_etags, j->etag)) { |
| 619 |
log_info("Image already downloaded. Skipping download. (%s)", j->etag); |
| 620 |
j->etag_exists = true; |
| 621 |
pull_job_finish(j, 0); |
| 622 |
return sz; |
| 623 |
} |
| 624 |
|
| 625 |
return sz; |
| 626 |
} |
| 627 |
} |
| 628 |
|
| 629 |
if (!http_status_ok(status)) /* Let's ignore the rest here, these requests are probably redirects and |
| 630 |
* stuff where the headers aren't interesting to us */ |
| 631 |
return sz; |
| 632 |
|
| 633 |
r = curl_header_strdup(contents, sz, "Content-Length:", &length); |
| 634 |
if (r < 0) { |
| 635 |
log_oom(); |
| 636 |
goto fail; |
| 637 |
} |
| 638 |
if (r > 0) { |
| 639 |
(void) safe_atou64(length, &j->content_length); |
| 640 |
|
| 641 |
if (j->content_length != UINT64_MAX) { |
| 642 |
if (j->content_length > j->compressed_max) { |
| 643 |
r = log_error_errno(SYNTHETIC_ERRNO(EFBIG), "Content too large."); |
| 644 |
goto fail; |
| 645 |
} |
| 646 |
|
| 647 |
if (j->expected_content_length != UINT64_MAX && |
| 648 |
j->expected_content_length != j->content_length) { |
| 649 |
r = log_error_errno(SYNTHETIC_ERRNO(EPROTO), "Content does not have expected size."); |
| 650 |
goto fail; |
| 651 |
} |
| 652 |
|
| 653 |
log_info("Downloading %s for %s.", FORMAT_BYTES(j->content_length), pull_job_description(j)); |
| 654 |
} |
| 655 |
|
| 656 |
return sz; |
| 657 |
} |
| 658 |
|
| 659 |
r = curl_header_strdup(contents, sz, "Last-Modified:", &last_modified); |
| 660 |
if (r < 0) { |
| 661 |
log_oom(); |
| 662 |
goto fail; |
| 663 |
} |
| 664 |
if (r > 0) { |
| 665 |
(void) curl_parse_http_time(last_modified, &j->mtime); |
| 666 |
return sz; |
| 667 |
} |
| 668 |
|
| 669 |
r = curl_header_strdup(contents, sz, "Content-Type:", &ct); |
| 670 |
if (r < 0) { |
| 671 |
log_oom(); |
| 672 |
goto fail; |
| 673 |
} |
| 674 |
if (r > 0) { |
| 675 |
free_and_replace(j->content_type, ct); |
| 676 |
return sz; |
| 677 |
} |
| 678 |
|
| 679 |
if (j->on_header) { |
| 680 |
r = j->on_header(j, contents, sz); |
| 681 |
if (r < 0) |
| 682 |
goto fail; |
| 683 |
} |
| 684 |
|
| 685 |
return sz; |
| 686 |
|
| 687 |
fail: |
| 688 |
pull_job_finish(j, r); |
| 689 |
return 0; |
| 690 |
} |
| 691 |
|
| 692 |
static int pull_job_progress_callback(void *userdata, curl_off_t dltotal, curl_off_t dlnow, curl_off_t ultotal, curl_off_t ulnow) { |
| 693 |
PullJob *j = ASSERT_PTR(userdata); |
| 694 |
unsigned percent; |
| 695 |
usec_t n; |
| 696 |
|
| 697 |
if (dltotal <= 0) |
| 698 |
return 0; |
| 699 |
|
| 700 |
percent = ((100 * dlnow) / dltotal); |
| 701 |
n = now(CLOCK_MONOTONIC); |
| 702 |
|
| 703 |
if (n > j->last_status_usec + USEC_PER_SEC && |
| 704 |
percent != j->progress_percent && |
| 705 |
dlnow < dltotal) { |
| 706 |
|
| 707 |
if (n - j->start_usec > USEC_PER_SEC && dlnow > 0) { |
| 708 |
usec_t left, done; |
| 709 |
|
| 710 |
done = n - j->start_usec; |
| 711 |
left = (usec_t) (((double) done * (double) dltotal) / dlnow) - done; |
| 712 |
|
| 713 |
log_info("Got %u%% of %s. %s left at %s/s.", |
| 714 |
percent, |
| 715 |
pull_job_description(j), |
| 716 |
FORMAT_TIMESPAN(left, USEC_PER_SEC), |
| 717 |
FORMAT_BYTES((uint64_t) ((double) dlnow / ((double) done / (double) USEC_PER_SEC)))); |
| 718 |
} else |
| 719 |
log_info("Got %u%% of %s.", percent, pull_job_description(j)); |
| 720 |
|
| 721 |
j->progress_percent = percent; |
| 722 |
j->last_status_usec = n; |
| 723 |
|
| 724 |
if (j->on_progress) |
| 725 |
j->on_progress(j); |
| 726 |
} |
| 727 |
|
| 728 |
return 0; |
| 729 |
} |
| 730 |
|
| 731 |
int pull_job_new( |
| 732 |
PullJob **ret, |
| 733 |
const char *url, |
| 734 |
CurlGlue *glue, |
| 735 |
void *userdata) { |
| 736 |
|
| 737 |
_cleanup_(pull_job_unrefp) PullJob *j = NULL; |
| 738 |
_cleanup_free_ char *u = NULL; |
| 739 |
|
| 740 |
assert(url); |
| 741 |
assert(glue); |
| 742 |
assert(ret); |
| 743 |
|
| 744 |
u = strdup(url); |
| 745 |
if (!u) |
| 746 |
return -ENOMEM; |
| 747 |
|
| 748 |
j = new(PullJob, 1); |
| 749 |
if (!j) |
| 750 |
return -ENOMEM; |
| 751 |
|
| 752 |
*j = (PullJob) { |
| 753 |
.state = PULL_JOB_INIT, |
| 754 |
.disk_fd = -EBADF, |
| 755 |
.close_disk_fd = true, |
| 756 |
.userdata = userdata, |
| 757 |
.glue = glue, |
| 758 |
.content_length = UINT64_MAX, |
| 759 |
.start_usec = now(CLOCK_MONOTONIC), |
| 760 |
.compressed_max = 64LLU * 1024LLU * 1024LLU * 1024LLU, /* 64GB safety limit */ |
| 761 |
.uncompressed_max = 64LLU * 1024LLU * 1024LLU * 1024LLU, /* 64GB safety limit */ |
| 762 |
.url = TAKE_PTR(u), |
| 763 |
.offset = UINT64_MAX, |
| 764 |
.sync = true, |
| 765 |
.expected_content_length = UINT64_MAX, |
| 766 |
}; |
| 767 |
|
| 768 |
*ret = TAKE_PTR(j); |
| 769 |
|
| 770 |
return 0; |
| 771 |
} |
| 772 |
|
| 773 |
int pull_job_add_request_header(PullJob *j, const char *hdr) { |
| 774 |
assert(j); |
| 775 |
assert(hdr); |
| 776 |
|
| 777 |
if (j->request_header) { |
| 778 |
struct curl_slist *l; |
| 779 |
|
| 780 |
l = sym_curl_slist_append(j->request_header, hdr); |
| 781 |
if (!l) |
| 782 |
return -ENOMEM; |
| 783 |
|
| 784 |
j->request_header = l; |
| 785 |
} else { |
| 786 |
j->request_header = curl_slist_new(hdr, NULL); |
| 787 |
if (!j->request_header) |
| 788 |
return -ENOMEM; |
| 789 |
} |
| 790 |
|
| 791 |
return 0; |
| 792 |
} |
| 793 |
|
| 794 |
int pull_job_begin(PullJob *j) { |
| 795 |
int r; |
| 796 |
|
| 797 |
assert(j); |
| 798 |
|
| 799 |
if (j->state != PULL_JOB_INIT) |
| 800 |
return -EBUSY; |
| 801 |
|
| 802 |
_cleanup_(curl_easy_cleanupp) CURL *easy = NULL; |
| 803 |
r = curl_glue_make(&easy, j->url); |
| 804 |
if (r < 0) |
| 805 |
return r; |
| 806 |
|
| 807 |
if (!strv_isempty(j->old_etags)) { |
| 808 |
_cleanup_free_ char *cc = NULL, *hdr = NULL; |
| 809 |
|
| 810 |
cc = strv_join(j->old_etags, ", "); |
| 811 |
if (!cc) |
| 812 |
return -ENOMEM; |
| 813 |
|
| 814 |
hdr = strjoin("If-None-Match: ", cc); |
| 815 |
if (!hdr) |
| 816 |
return -ENOMEM; |
| 817 |
|
| 818 |
r = pull_job_add_request_header(j, hdr); |
| 819 |
if (r < 0) |
| 820 |
return r; |
| 821 |
} |
| 822 |
|
| 823 |
if (j->request_header) { |
| 824 |
if (sym_curl_easy_setopt(easy, CURLOPT_HTTPHEADER, j->request_header) != CURLE_OK) |
| 825 |
return -EIO; |
| 826 |
} |
| 827 |
|
| 828 |
if (sym_curl_easy_setopt(easy, CURLOPT_WRITEFUNCTION, pull_job_write_callback) != CURLE_OK) |
| 829 |
return -EIO; |
| 830 |
|
| 831 |
if (sym_curl_easy_setopt(easy, CURLOPT_WRITEDATA, j) != CURLE_OK) |
| 832 |
return -EIO; |
| 833 |
|
| 834 |
if (sym_curl_easy_setopt(easy, CURLOPT_HEADERFUNCTION, pull_job_header_callback) != CURLE_OK) |
| 835 |
return -EIO; |
| 836 |
|
| 837 |
if (sym_curl_easy_setopt(easy, CURLOPT_HEADERDATA, j) != CURLE_OK) |
| 838 |
return -EIO; |
| 839 |
|
| 840 |
if (sym_curl_easy_setopt(easy, CURLOPT_XFERINFOFUNCTION, pull_job_progress_callback) != CURLE_OK) |
| 841 |
return -EIO; |
| 842 |
|
| 843 |
if (sym_curl_easy_setopt(easy, CURLOPT_XFERINFODATA, j) != CURLE_OK) |
| 844 |
return -EIO; |
| 845 |
|
| 846 |
if (sym_curl_easy_setopt(easy, CURLOPT_NOPROGRESS, 0L) != CURLE_OK) |
| 847 |
return -EIO; |
| 848 |
|
| 849 |
r = curl_glue_perform_async(j->glue, easy, pull_job_curl_on_finished, j, &j->slot); |
| 850 |
if (r < 0) |
| 851 |
return r; |
| 852 |
TAKE_PTR(easy); |
| 853 |
|
| 854 |
j->state = PULL_JOB_ANALYZING; |
| 855 |
|
| 856 |
return 0; |
| 857 |
} |
| 858 |
|
| 859 |
int pull_job_set_accept(PullJob *j, char * const *l) { |
| 860 |
assert(j); |
| 861 |
|
| 862 |
if (strv_isempty(l)) |
| 863 |
return 0; |
| 864 |
|
| 865 |
_cleanup_free_ char *joined = strv_join(l, ", "); |
| 866 |
if (!joined) |
| 867 |
return -ENOMEM; |
| 868 |
|
| 869 |
_cleanup_free_ char *f = strjoin("Accept: ", joined); |
| 870 |
if (!f) |
| 871 |
return -ENOMEM; |
| 872 |
|
| 873 |
return pull_job_add_request_header(j, f); |
| 874 |
} |
| 875 |
|
| 876 |
int pull_job_set_bearer_token(PullJob *j, const char *token) { |
| 877 |
assert(j); |
| 878 |
|
| 879 |
_cleanup_free_ char *f = strjoin("Authorization: Bearer ", token); |
| 880 |
if (!f) |
| 881 |
return -ENOMEM; |
| 882 |
|
| 883 |
return pull_job_add_request_header(j, f); |
| 884 |
} |