import-raw.c revision 0716faad4a846d5f3cdce4bf37648d3254b9332f
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmek/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmek
12b42c76672a66c2d4ea7212c14f8f1b5a62b78dTom Gundersen/***
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmek This file is part of systemd.
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmek
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmek Copyright 2014 Lennart Poettering
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmek
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmek systemd is free software; you can redistribute it and/or modify it
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmek under the terms of the GNU Lesser General Public License as published by
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmek the Free Software Foundation; either version 2.1 of the License, or
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmek (at your option) any later version.
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmek
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmek systemd is distributed in the hope that it will be useful, but
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmek WITHOUT ANY WARRANTY; without even the implied warranty of
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmek MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmek Lesser General Public License for more details.
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmek
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmek You should have received a copy of the GNU Lesser General Public License
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmek along with systemd; If not, see <http://www.gnu.org/licenses/>.
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmek***/
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmek
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmek#include <sys/xattr.h>
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmek#include <linux/fs.h>
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering#include <curl/curl.h>
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmek#include <lzma.h>
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmek
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmek#include "hashmap.h"
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmek#include "utf8.h"
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmek#include "curl-util.h"
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmek#include "qcow2-util.h"
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmek#include "import-raw.h"
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmek#include "strv.h"
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmek#include "copy.h"
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmek
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmektypedef struct RawImportFile RawImportFile;
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmek
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmekstruct RawImportFile {
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmek RawImport *import;
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmek
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmek char *url;
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmek char *local;
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmek
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmek CURL *curl;
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmek struct curl_slist *request_header;
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmek
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmek char *temp_path;
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmek char *final_path;
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmek char *etag;
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering char **old_etags;
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering uint64_t content_length;
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering uint64_t written_compressed;
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering uint64_t written_uncompressed;
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering void *payload;
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering size_t payload_size;
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering usec_t mtime;
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmek
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmek bool force_local;
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmek bool done;
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmek
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmek int disk_fd;
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmek
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering lzma_stream lzma;
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering bool compressed;
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering unsigned progress_percent;
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering usec_t start_usec;
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering usec_t last_status_usec;
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering};
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poetteringstruct RawImport {
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering sd_event *event;
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmek CurlGlue *glue;
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmek
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmek char *image_root;
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmek Hashmap *files;
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmek
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmek raw_import_on_finished on_finished;
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmek void *userdata;
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmek
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering bool finished;
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmek};
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmek
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmek#define FILENAME_ESCAPE "/.#\"\'"
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmek
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmek#define RAW_MAX_SIZE (1024LLU*1024LLU*1024LLU*8) /* 8 GB */
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmek
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmekstatic RawImportFile *raw_import_file_unref(RawImportFile *f) {
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering if (!f)
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering return NULL;
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering if (f->import)
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering curl_glue_remove_and_free(f->import->glue, f->curl);
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmek curl_slist_free_all(f->request_header);
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmek
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmek safe_close(f->disk_fd);
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmek
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmek free(f->final_path);
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmek
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering if (f->temp_path) {
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering unlink(f->temp_path);
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering free(f->temp_path);
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering }
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering lzma_end(&f->lzma);
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering free(f->url);
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering free(f->local);
7f3fdb7f19a109fa3d1be92926bfe4cea1817da5Jakub Wilk free(f->etag);
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering strv_free(f->old_etags);
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering free(f->payload);
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmek free(f);
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering return NULL;
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering}
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart PoetteringDEFINE_TRIVIAL_CLEANUP_FUNC(RawImportFile*, raw_import_file_unref);
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poetteringstatic void raw_import_finish(RawImport *import, int error) {
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering assert(import);
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering if (import->finished)
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering return;
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering import->finished = true;
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering if (import->on_finished)
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering import->on_finished(import, error, import->userdata);
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering else
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering sd_event_exit(import->event, error);
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering}
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poetteringstatic int raw_import_file_make_final_path(RawImportFile *f) {
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering _cleanup_free_ char *escaped_url = NULL, *escaped_etag = NULL;
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering assert(f);
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering if (f->final_path)
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering return 0;
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering escaped_url = xescape(f->url, FILENAME_ESCAPE);
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering if (!escaped_url)
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering return -ENOMEM;
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering if (f->etag) {
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering escaped_etag = xescape(f->etag, FILENAME_ESCAPE);
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering if (!escaped_etag)
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering return -ENOMEM;
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering f->final_path = strjoin(f->import->image_root, "/.raw-", escaped_url, ".", escaped_etag, ".raw", NULL);
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering } else
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering f->final_path = strjoin(f->import->image_root, "/.raw-", escaped_url, ".raw", NULL);
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering if (!f->final_path)
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering return -ENOMEM;
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmek
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering return 0;
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering}
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poetteringstatic int raw_import_file_make_local_copy(RawImportFile *f) {
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering _cleanup_free_ char *tp = NULL;
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering _cleanup_close_ int dfd = -1;
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering const char *p;
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering int r;
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering assert(f);
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering if (!f->local)
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering return 0;
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering if (f->disk_fd >= 0) {
7f3fdb7f19a109fa3d1be92926bfe4cea1817da5Jakub Wilk if (lseek(f->disk_fd, SEEK_SET, 0) == (off_t) -1)
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering return log_error_errno(errno, "Failed to seek to beginning of vendor image: %m");
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering } else {
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering r = raw_import_file_make_final_path(f);
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering if (r < 0)
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering return log_oom();
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering f->disk_fd = open(f->final_path, O_RDONLY|O_NOCTTY|O_CLOEXEC);
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering if (f->disk_fd < 0)
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering return log_error_errno(errno, "Failed to open vendor image: %m");
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering }
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering p = strappenda(f->import->image_root, "/", f->local, ".raw");
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering if (f->force_local)
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering (void) rm_rf_dangerous(p, false, true, false);
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering r = tempfn_random(p, &tp);
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering if (r < 0)
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering return log_oom();
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering dfd = open(tp, O_WRONLY|O_CREAT|O_EXCL|O_NOCTTY|O_CLOEXEC, 0664);
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering if (dfd < 0)
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering return log_error_errno(errno, "Failed to create writable copy of image: %m");
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering /* Turn off COW writing. This should greatly improve
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering * performance on COW file systems like btrfs, since it
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering * reduces fragmentation caused by not allowing in-place
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering * writes. */
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering r = chattr_fd(dfd, true, FS_NOCOW_FL);
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering if (r < 0)
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering log_warning_errno(errno, "Failed to set file attributes on %s: %m", tp);
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering r = copy_bytes(f->disk_fd, dfd, (off_t) -1, true);
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering if (r < 0) {
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering unlink(tp);
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering return log_error_errno(r, "Failed to make writable copy of image: %m");
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering }
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering (void) copy_times(f->disk_fd, dfd);
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering (void) copy_xattr(f->disk_fd, dfd);
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering dfd = safe_close(dfd);
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering r = rename(tp, p);
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering if (r < 0) {
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering unlink(tp);
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering return log_error_errno(errno, "Failed to move writable image into place: %m");
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering }
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering log_info("Created new local image %s.", p);
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering return 0;
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering}
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poetteringstatic void raw_import_file_success(RawImportFile *f) {
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering int r;
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering assert(f);
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering f->done = true;
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering r = raw_import_file_make_local_copy(f);
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering if (r < 0)
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering goto finish;
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering f->disk_fd = safe_close(f->disk_fd);
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering r = 0;
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poetteringfinish:
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering raw_import_finish(f->import, r);
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering}
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poetteringstatic int raw_import_maybe_convert_qcow2(RawImportFile *f) {
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering _cleanup_close_ int converted_fd = -1;
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmek _cleanup_free_ char *t = NULL;
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmek int r;
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering assert(f);
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering assert(f->disk_fd);
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering assert(f->temp_path);
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering r = qcow2_detect(f->disk_fd);
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering if (r < 0)
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering return log_error_errno(r, "Failed to detect whether this is a QCOW2 image: %m");
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering if (r == 0)
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering return 0;
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering /* This is a QCOW2 image, let's convert it */
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering r = tempfn_random(f->final_path, &t);
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering if (r < 0)
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering return log_oom();
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering converted_fd = open(t, O_RDWR|O_CREAT|O_EXCL|O_NOCTTY|O_CLOEXEC, 0644);
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering if (converted_fd < 0)
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering return log_error_errno(errno, "Failed to create %s: %m", t);
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering r = qcow2_convert(f->disk_fd, converted_fd);
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmek if (r < 0) {
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmek unlink(t);
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmek return log_error_errno(r, "Failed to convert qcow2 image: %m");
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmek }
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmek
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmek unlink(f->temp_path);
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmek free(f->temp_path);
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmek
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering f->temp_path = t;
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering t = NULL;
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering safe_close(f->disk_fd);
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering f->disk_fd = converted_fd;
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering converted_fd = -1;
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering return 1;
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering}
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmek
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmekstatic void raw_import_curl_on_finished(CurlGlue *g, CURL *curl, CURLcode result) {
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmek RawImportFile *f = NULL;
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmek struct stat st;
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmek CURLcode code;
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmek long status;
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmek int r;
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmek
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmek if (curl_easy_getinfo(curl, CURLINFO_PRIVATE, &f) != CURLE_OK)
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmek return;
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmek
a8eaaee72a2f06e0fb64fb71de3b71ecba31dafbJan Engelhardt if (!f || f->done)
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering return;
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmek
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmek f->done = true;
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmek
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmek if (result != CURLE_OK) {
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmek log_error("Transfer failed: %s", curl_easy_strerror(result));
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmek r = -EIO;
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmek goto fail;
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmek }
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmek
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmek code = curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &status);
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmek if (code != CURLE_OK) {
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmek log_error("Failed to retrieve response code: %s", curl_easy_strerror(code));
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmek r = -EIO;
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmek goto fail;
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmek } else if (status == 304) {
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmek log_info("Image already downloaded. Skipping download.");
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmek raw_import_file_success(f);
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmek return;
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmek } else if (status >= 300) {
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmek log_error("HTTP request to %s failed with code %li.", f->url, status);
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmek r = -EIO;
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmek goto fail;
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmek } else if (status < 200) {
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmek log_error("HTTP request to %s finished with unexpected code %li.", f->url, status);
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmek r = -EIO;
b938cb902c3b5bca807a94b277672c64d6767886Jan Engelhardt goto fail;
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmek }
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmek
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering if (f->disk_fd < 0) {
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmek log_error("No data received.");
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmek r = -EIO;
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmek goto fail;
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmek }
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmek
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmek if (f->content_length != (uint64_t) -1 &&
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmek f->content_length != f->written_compressed) {
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmek log_error("Download truncated.");
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmek r = -EIO;
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmek goto fail;
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmek }
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmek
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering /* Make sure the file size is right, in case the file was
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering * sparse and we just seeked for the last part */
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering if (ftruncate(f->disk_fd, f->written_uncompressed) < 0) {
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering log_error_errno(errno, "Failed to truncate file: %m");
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmek r = -errno;
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmek goto fail;
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmek }
42f1ab5009eed71f0d4f83681b8fdbed8664fca3Zbigniew Jędrzejewski-Szmek
r = raw_import_maybe_convert_qcow2(f);
if (r < 0)
goto fail;
if (f->etag)
(void) fsetxattr(f->disk_fd, "user.source_etag", f->etag, strlen(f->etag), 0);
if (f->url)
(void) fsetxattr(f->disk_fd, "user.source_url", f->url, strlen(f->url), 0);
if (f->mtime != 0) {
struct timespec ut[2];
timespec_store(&ut[0], f->mtime);
ut[1] = ut[0];
(void) futimens(f->disk_fd, ut);
fd_setcrtime(f->disk_fd, f->mtime);
}
if (fstat(f->disk_fd, &st) < 0) {
r = log_error_errno(errno, "Failed to stat file: %m");
goto fail;
}
/* Mark read-only */
(void) fchmod(f->disk_fd, st.st_mode & 07444);
assert(f->temp_path);
assert(f->final_path);
r = rename(f->temp_path, f->final_path);
if (r < 0) {
r = log_error_errno(errno, "Failed to move RAW file into place: %m");
goto fail;
}
free(f->temp_path);
f->temp_path = NULL;
log_info("Completed writing vendor image %s.", f->final_path);
raw_import_file_success(f);
return;
fail:
raw_import_finish(f->import, r);
}
static int raw_import_file_open_disk_for_write(RawImportFile *f) {
int r;
assert(f);
if (f->disk_fd >= 0)
return 0;
r = raw_import_file_make_final_path(f);
if (r < 0)
return log_oom();
if (!f->temp_path) {
r = tempfn_random(f->final_path, &f->temp_path);
if (r < 0)
return log_oom();
}
f->disk_fd = open(f->temp_path, O_RDWR|O_CREAT|O_EXCL|O_NOCTTY|O_CLOEXEC, 0644);
if (f->disk_fd < 0)
return log_error_errno(errno, "Failed to create %s: %m", f->temp_path);
r = chattr_fd(f->disk_fd, true, FS_NOCOW_FL);
if (r < 0)
log_warning_errno(errno, "Failed to set file attributes on %s: %m", f->temp_path);
return 0;
}
static int raw_import_file_write_uncompressed(RawImportFile *f, void *p, size_t sz) {
ssize_t n;
assert(f);
assert(p);
assert(sz > 0);
assert(f->disk_fd >= 0);
if (f->written_uncompressed + sz < f->written_uncompressed) {
log_error("File too large, overflow");
return -EOVERFLOW;
}
if (f->written_uncompressed + sz > RAW_MAX_SIZE) {
log_error("File overly large, refusing");
return -EFBIG;
}
n = sparse_write(f->disk_fd, p, sz, 64);
if (n < 0) {
log_error_errno(errno, "Failed to write file: %m");
return -errno;
}
if ((size_t) n < sz) {
log_error("Short write");
return -EIO;
}
f->written_uncompressed += sz;
return 0;
}
static int raw_import_file_write_compressed(RawImportFile *f, void *p, size_t sz) {
int r;
assert(f);
assert(p);
assert(sz > 0);
assert(f->disk_fd >= 0);
if (f->written_compressed + sz < f->written_compressed) {
log_error("File too large, overflow");
return -EOVERFLOW;
}
if (f->content_length != (uint64_t) -1 &&
f->written_compressed + sz > f->content_length) {
log_error("Content length incorrect.");
return -EFBIG;
}
if (!f->compressed) {
r = raw_import_file_write_uncompressed(f, p, sz);
if (r < 0)
return r;
} else {
f->lzma.next_in = p;
f->lzma.avail_in = sz;
while (f->lzma.avail_in > 0) {
uint8_t buffer[16 * 1024];
lzma_ret lzr;
f->lzma.next_out = buffer;
f->lzma.avail_out = sizeof(buffer);
lzr = lzma_code(&f->lzma, LZMA_RUN);
if (lzr != LZMA_OK && lzr != LZMA_STREAM_END) {
log_error("Decompression error.");
return -EIO;
}
r = raw_import_file_write_uncompressed(f, buffer, sizeof(buffer) - f->lzma.avail_out);
if (r < 0)
return r;
}
}
f->written_compressed += sz;
return 0;
}
static int raw_import_file_detect_xz(RawImportFile *f) {
static const uint8_t xz_signature[] = {
'\xfd', '7', 'z', 'X', 'Z', '\x00'
};
lzma_ret lzr;
int r;
assert(f);
if (f->payload_size < sizeof(xz_signature))
return 0;
f->compressed = memcmp(f->payload, xz_signature, sizeof(xz_signature)) == 0;
log_debug("Stream is XZ compressed: %s", yes_no(f->compressed));
if (f->compressed) {
lzr = lzma_stream_decoder(&f->lzma, UINT64_MAX, LZMA_TELL_UNSUPPORTED_CHECK);
if (lzr != LZMA_OK) {
log_error("Failed to initialize LZMA decoder.");
return -EIO;
}
}
r = raw_import_file_open_disk_for_write(f);
if (r < 0)
return r;
r = raw_import_file_write_compressed(f, f->payload, f->payload_size);
if (r < 0)
return r;
free(f->payload);
f->payload = NULL;
f->payload_size = 0;
return 0;
}
static size_t raw_import_file_write_callback(void *contents, size_t size, size_t nmemb, void *userdata) {
RawImportFile *f = userdata;
size_t sz = size * nmemb;
int r;
assert(contents);
assert(f);
if (f->done) {
r = -ESTALE;
goto fail;
}
if (f->disk_fd < 0) {
uint8_t *p;
/* We haven't opened the file yet, let's first check what it actually is */
p = realloc(f->payload, f->payload_size + sz);
if (!p) {
r = log_oom();
goto fail;
}
memcpy(p + f->payload_size, contents, sz);
f->payload_size = sz;
f->payload = p;
r = raw_import_file_detect_xz(f);
if (r < 0)
goto fail;
return sz;
}
r = raw_import_file_write_compressed(f, contents, sz);
if (r < 0)
goto fail;
return sz;
fail:
raw_import_finish(f->import, r);
return 0;
}
static size_t raw_import_file_header_callback(void *contents, size_t size, size_t nmemb, void *userdata) {
RawImportFile *f = userdata;
size_t sz = size * nmemb;
_cleanup_free_ char *length = NULL, *last_modified = NULL;
char *etag;
int r;
assert(contents);
assert(f);
if (f->done) {
r = -ESTALE;
goto fail;
}
r = curl_header_strdup(contents, sz, "ETag:", &etag);
if (r < 0) {
log_oom();
goto fail;
}
if (r > 0) {
free(f->etag);
f->etag = etag;
if (strv_contains(f->old_etags, f->etag)) {
log_info("Image already downloaded. Skipping download.");
raw_import_file_success(f);
return sz;
}
return sz;
}
r = curl_header_strdup(contents, sz, "Content-Length:", &length);
if (r < 0) {
log_oom();
goto fail;
}
if (r > 0) {
(void) safe_atou64(length, &f->content_length);
if (f->content_length != (uint64_t) -1) {
char bytes[FORMAT_BYTES_MAX];
log_info("Downloading %s.", format_bytes(bytes, sizeof(bytes), f->content_length));
}
return sz;
}
r = curl_header_strdup(contents, sz, "Last-Modified:", &last_modified);
if (r < 0) {
log_oom();
goto fail;
}
if (r > 0) {
(void) curl_parse_http_time(last_modified, &f->mtime);
return sz;
}
return sz;
fail:
raw_import_finish(f->import, r);
return 0;
}
static int raw_import_file_progress_callback(void *userdata, curl_off_t dltotal, curl_off_t dlnow, curl_off_t ultotal, curl_off_t ulnow) {
RawImportFile *f = userdata;
unsigned percent;
usec_t n;
assert(f);
if (dltotal <= 0)
return 0;
percent = ((100 * dlnow) / dltotal);
n = now(CLOCK_MONOTONIC);
if (n > f->last_status_usec + USEC_PER_SEC &&
percent != f->progress_percent) {
char buf[FORMAT_TIMESPAN_MAX];
if (n - f->start_usec > USEC_PER_SEC && dlnow > 0) {
usec_t left, done;
done = n - f->start_usec;
left = (usec_t) (((double) done * (double) dltotal) / dlnow) - done;
log_info("Got %u%%. %s left.", percent, format_timespan(buf, sizeof(buf), left, USEC_PER_SEC));
} else
log_info("Got %u%%.", percent);
f->progress_percent = percent;
f->last_status_usec = n;
}
return 0;
}
static bool etag_is_valid(const char *etag) {
if (!endswith(etag, "\""))
return false;
if (!startswith(etag, "\"") && !startswith(etag, "W/\""))
return false;
return true;
}
static int raw_import_file_find_old_etags(RawImportFile *f) {
_cleanup_free_ char *escaped_url = NULL;
_cleanup_closedir_ DIR *d = NULL;
struct dirent *de;
int r;
escaped_url = xescape(f->url, FILENAME_ESCAPE);
if (!escaped_url)
return -ENOMEM;
d = opendir(f->import->image_root);
if (!d) {
if (errno == ENOENT)
return 0;
return -errno;
}
FOREACH_DIRENT_ALL(de, d, return -errno) {
const char *a, *b;
char *u;
if (de->d_type != DT_UNKNOWN &&
de->d_type != DT_REG)
continue;
a = startswith(de->d_name, ".raw-");
if (!a)
continue;
a = startswith(a, escaped_url);
if (!a)
continue;
a = startswith(a, ".");
if (!a)
continue;
b = endswith(de->d_name, ".raw");
if (!b)
continue;
if (a >= b)
continue;
u = cunescape_length(a, b - a);
if (!u)
return -ENOMEM;
if (!etag_is_valid(u)) {
free(u);
continue;
}
r = strv_consume(&f->old_etags, u);
if (r < 0)
return r;
}
return 0;
}
static int raw_import_file_begin(RawImportFile *f) {
int r;
assert(f);
assert(!f->curl);
log_info("Getting %s.", f->url);
r = raw_import_file_find_old_etags(f);
if (r < 0)
return r;
r = curl_glue_make(&f->curl, f->url, f);
if (r < 0)
return r;
if (!strv_isempty(f->old_etags)) {
_cleanup_free_ char *cc = NULL, *hdr = NULL;
cc = strv_join(f->old_etags, ", ");
if (!cc)
return -ENOMEM;
hdr = strappend("If-None-Match: ", cc);
if (!hdr)
return -ENOMEM;
f->request_header = curl_slist_new(hdr, NULL);
if (!f->request_header)
return -ENOMEM;
if (curl_easy_setopt(f->curl, CURLOPT_HTTPHEADER, f->request_header) != CURLE_OK)
return -EIO;
}
if (curl_easy_setopt(f->curl, CURLOPT_WRITEFUNCTION, raw_import_file_write_callback) != CURLE_OK)
return -EIO;
if (curl_easy_setopt(f->curl, CURLOPT_WRITEDATA, f) != CURLE_OK)
return -EIO;
if (curl_easy_setopt(f->curl, CURLOPT_HEADERFUNCTION, raw_import_file_header_callback) != CURLE_OK)
return -EIO;
if (curl_easy_setopt(f->curl, CURLOPT_HEADERDATA, f) != CURLE_OK)
return -EIO;
if (curl_easy_setopt(f->curl, CURLOPT_XFERINFOFUNCTION, raw_import_file_progress_callback) != CURLE_OK)
return -EIO;
if (curl_easy_setopt(f->curl, CURLOPT_XFERINFODATA, f) != CURLE_OK)
return -EIO;
if (curl_easy_setopt(f->curl, CURLOPT_NOPROGRESS, 0) != CURLE_OK)
return -EIO;
r = curl_glue_add(f->import->glue, f->curl);
if (r < 0)
return r;
return 0;
}
int raw_import_new(RawImport **import, sd_event *event, const char *image_root, raw_import_on_finished on_finished, void *userdata) {
_cleanup_(raw_import_unrefp) RawImport *i = NULL;
int r;
assert(import);
assert(image_root);
i = new0(RawImport, 1);
if (!i)
return -ENOMEM;
i->on_finished = on_finished;
i->userdata = userdata;
i->image_root = strdup(image_root);
if (!i->image_root)
return -ENOMEM;
if (event)
i->event = sd_event_ref(event);
else {
r = sd_event_default(&i->event);
if (r < 0)
return r;
}
r = curl_glue_new(&i->glue, i->event);
if (r < 0)
return r;
i->glue->on_finished = raw_import_curl_on_finished;
i->glue->userdata = i;
*import = i;
i = NULL;
return 0;
}
RawImport* raw_import_unref(RawImport *import) {
RawImportFile *f;
if (!import)
return NULL;
while ((f = hashmap_steal_first(import->files)))
raw_import_file_unref(f);
hashmap_free(import->files);
curl_glue_unref(import->glue);
sd_event_unref(import->event);
free(import->image_root);
free(import);
return NULL;
}
int raw_import_cancel(RawImport *import, const char *url) {
RawImportFile *f;
assert(import);
assert(url);
f = hashmap_remove(import->files, url);
if (!f)
return 0;
raw_import_file_unref(f);
return 1;
}
int raw_import_pull(RawImport *import, const char *url, const char *local, bool force_local) {
_cleanup_(raw_import_file_unrefp) RawImportFile *f = NULL;
int r;
assert(import);
assert(raw_url_is_valid(url));
assert(!local || machine_name_is_valid(local));
if (hashmap_get(import->files, url))
return -EEXIST;
r = hashmap_ensure_allocated(&import->files, &string_hash_ops);
if (r < 0)
return r;
f = new0(RawImportFile, 1);
if (!f)
return -ENOMEM;
f->import = import;
f->disk_fd = -1;
f->content_length = (uint64_t) -1;
f->start_usec = now(CLOCK_MONOTONIC);
f->url = strdup(url);
if (!f->url)
return -ENOMEM;
if (local) {
f->local = strdup(local);
if (!f->local)
return -ENOMEM;
f->force_local = force_local;
}
r = hashmap_put(import->files, f->url, f);
if (r < 0)
return r;
r = raw_import_file_begin(f);
if (r < 0) {
raw_import_cancel(import, f->url);
f = NULL;
return r;
}
f = NULL;
return 0;
}
bool raw_url_is_valid(const char *url) {
if (isempty(url))
return false;
if (!startswith(url, "http://") &&
!startswith(url, "https://"))
return false;
return ascii_is_valid(url);
}