chiark / gitweb /
timedatectl: remove unused variables
[elogind.git] / src / import / import-raw.c
index ac9e6eb847eff1f225184289bc8175febf168a97..7d1ac2afd700749cf6166b70f1d258050ab1ec6b 100644 (file)
@@ -3,7 +3,7 @@
 /***
   This file is part of systemd.
 
-  Copyright 2014 Lennart Poettering
+  Copyright 2015 Lennart Poettering
 
   systemd is free software; you can redistribute it and/or modify it
   under the terms of the GNU Lesser General Public License as published by
   along with systemd; If not, see <http://www.gnu.org/licenses/>.
 ***/
 
-#include <sys/xattr.h>
 #include <linux/fs.h>
-#include <curl/curl.h>
 
-#include "hashmap.h"
-#include "utf8.h"
-#include "curl-util.h"
-#include "import-raw.h"
-#include "strv.h"
+#include "sd-daemon.h"
+#include "sd-event.h"
+#include "util.h"
+#include "path-util.h"
+#include "btrfs-util.h"
 #include "copy.h"
+#include "mkdir.h"
+#include "ratelimit.h"
+#include "machine-pool.h"
+#include "qcow2-util.h"
+#include "import-compress.h"
+#include "import-common.h"
+#include "import-raw.h"
+
+struct RawImport {
+        sd_event *event;
 
-typedef struct RawImportFile RawImportFile;
+        char *image_root;
 
-struct RawImportFile {
-        RawImport *import;
+        RawImportFinished on_finished;
+        void *userdata;
 
-        char *url;
         char *local;
-
-        CURL *curl;
-        struct curl_slist *request_header;
+        bool force_local;
+        bool read_only;
+        bool grow_machine_directory;
 
         char *temp_path;
         char *final_path;
-        char *etag;
-        char **old_etags;
 
-        uint64_t content_length;
-        uint64_t written;
+        int input_fd;
+        int output_fd;
 
-        usec_t mtime;
+        ImportCompress compress;
 
-        bool force_local;
-        bool done;
+        uint64_t written_since_last_grow;
 
-        int disk_fd;
-};
+        sd_event_source *input_event_source;
 
-struct RawImport {
-        sd_event *event;
-        CurlGlue *glue;
+        uint8_t buffer[16*1024];
+        size_t buffer_size;
 
-        char *image_root;
-        Hashmap *files;
+        uint64_t written_compressed;
+        uint64_t written_uncompressed;
 
-        raw_import_on_finished on_finished;
-        void *userdata;
+        struct stat st;
 
-        bool finished;
+        unsigned last_percent;
+        RateLimit progress_rate_limit;
 };
 
-#define FILENAME_ESCAPE "/.#\"\'"
-
-static RawImportFile *raw_import_file_unref(RawImportFile *f) {
-        if (!f)
+RawImport* raw_import_unref(RawImport *i) {
+        if (!i)
                 return NULL;
 
-        if (f->import)
-                curl_glue_remove_and_free(f->import->glue, f->curl);
-        curl_slist_free_all(f->request_header);
-
-        safe_close(f->disk_fd);
+        sd_event_unref(i->event);
 
-        free(f->final_path);
-
-        if (f->temp_path) {
-                unlink(f->temp_path);
-                free(f->temp_path);
+        if (i->temp_path) {
+                (void) unlink(i->temp_path);
+                free(i->temp_path);
         }
 
-        free(f->url);
-        free(f->local);
-        free(f->etag);
-        strv_free(f->old_etags);
-        free(f);
+        import_compress_free(&i->compress);
 
-        return NULL;
-}
+        sd_event_source_unref(i->input_event_source);
 
-DEFINE_TRIVIAL_CLEANUP_FUNC(RawImportFile*, raw_import_file_unref);
+        safe_close(i->output_fd);
 
-static void raw_import_finish(RawImport *import, int error) {
-        assert(import);
+        free(i->final_path);
+        free(i->image_root);
+        free(i->local);
+        free(i);
 
-        if (import->finished)
-                return;
-
-        import->finished = true;
-
-        if (import->on_finished)
-                import->on_finished(import, error, import->userdata);
-        else
-                sd_event_exit(import->event, error);
+        return NULL;
 }
 
-static int raw_import_file_make_final_path(RawImportFile *f) {
-        _cleanup_free_ char *escaped_url = NULL, *escaped_etag = NULL;
-
-        assert(f);
-
-        if (f->final_path)
-                return 0;
+int raw_import_new(
+                RawImport **ret,
+                sd_event *event,
+                const char *image_root,
+                RawImportFinished on_finished,
+                void *userdata) {
 
-        escaped_url = xescape(f->url, FILENAME_ESCAPE);
-        if (!escaped_url)
-                return -ENOMEM;
+        _cleanup_(raw_import_unrefp) RawImport *i = NULL;
+        int r;
 
-        if (f->etag) {
-                escaped_etag = xescape(f->etag, FILENAME_ESCAPE);
-                if (!escaped_etag)
-                        return -ENOMEM;
+        assert(ret);
 
-                f->final_path = strjoin(f->import->image_root, "/.raw-", escaped_url, ".", escaped_etag, ".raw", NULL);
-        } else
-                f->final_path = strjoin(f->import->image_root, "/.raw-", escaped_url, ".raw", NULL);
-        if (!f->final_path)
+        i = new0(RawImport, 1);
+        if (!i)
                 return -ENOMEM;
 
-        return 0;
-}
-
-static void raw_import_file_success(RawImportFile *f) {
-        int r;
-
-        assert(f);
-
-        f->done = true;
-
-        if (f->local) {
-                _cleanup_free_ char *tp = NULL;
-                _cleanup_close_ int dfd = -1;
-                const char *p;
-
-                if (f->disk_fd >= 0) {
-                        if (lseek(f->disk_fd, SEEK_SET, 0) == (off_t) -1) {
-                                r = log_error_errno(errno, "Failed to seek to beginning of vendor image: %m");
-                                goto finish;
-                        }
-                } else {
-                        r = raw_import_file_make_final_path(f);
-                        if (r < 0) {
-                                log_oom();
-                                goto finish;
-                        }
-
-                        f->disk_fd = open(f->final_path, O_RDONLY|O_NOCTTY|O_CLOEXEC);
-                        if (f->disk_fd < 0) {
-                                r = log_error_errno(errno, "Failed to open vendor image: %m");
-                                goto finish;
-                        }
-                }
+        i->input_fd = i->output_fd = -1;
+        i->on_finished = on_finished;
+        i->userdata = userdata;
 
-                p = strappenda(f->import->image_root, "/", f->local, ".raw");
-                if (f->force_local)
-                        (void) rm_rf_dangerous(p, false, true, false);
+        RATELIMIT_INIT(i->progress_rate_limit, 100 * USEC_PER_MSEC, 1);
+        i->last_percent = (unsigned) -1;
 
-                r = tempfn_random(p, &tp);
-                if (r < 0) {
-                        log_oom();
-                        goto finish;
-                }
+        i->image_root = strdup(image_root ?: "/var/lib/machines");
+        if (!i->image_root)
+                return -ENOMEM;
 
-                dfd = open(tp, O_WRONLY|O_CREAT|O_EXCL|O_NOCTTY|O_CLOEXEC, 0664);
-                if (dfd < 0) {
-                        r = log_error_errno(errno, "Failed to create writable copy of image: %m");
-                        goto finish;
-                }
+        i->grow_machine_directory = path_startswith(i->image_root, "/var/lib/machines");
 
-                /* Turn off COW writing. This should greatly improve
-                 * performance on COW file systems like btrfs, since it
-                 * reduces fragmentation caused by not allowing in-place
-                 * writes. */
-                r = chattr_fd(dfd, true, FS_NOCOW_FL);
+        if (event)
+                i->event = sd_event_ref(event);
+        else {
+                r = sd_event_default(&i->event);
                 if (r < 0)
-                        log_warning_errno(errno, "Failed to set file attributes on %s: %m", f->temp_path);
-
-                r = copy_bytes(f->disk_fd, dfd, (off_t) -1, true);
-                if (r < 0) {
-                        log_error_errno(r, "Failed to make writable copy of image: %m");
-                        unlink(tp);
-                        goto finish;
-                }
-
-                (void) copy_times(f->disk_fd, dfd);
-                (void) copy_xattr(f->disk_fd, dfd);
-
-                dfd = safe_close(dfd);
-
-                r = rename(tp, p);
-                if (r < 0) {
-                        r = log_error_errno(errno, "Failed to move writable image into place: %m");
-                        unlink(tp);
-                        goto finish;
-                }
-
-                log_info("Created new local image %s.", p);
+                        return r;
         }
 
-        f->disk_fd = safe_close(f->disk_fd);
-        r = 0;
+        *ret = i;
+        i = NULL;
 
-finish:
-        raw_import_finish(f->import, r);
+        return 0;
 }
 
-static void raw_import_curl_on_finished(CurlGlue *g, CURL *curl, CURLcode result) {
-        RawImportFile *f = NULL;
-        struct stat st;
-        CURLcode code;
-        long status;
-        int r;
+static void raw_import_report_progress(RawImport *i) {
+        unsigned percent;
+        assert(i);
 
-        if (curl_easy_getinfo(curl, CURLINFO_PRIVATE, &f) != CURLE_OK)
+        /* We have no size information, unless the source is a regular file */
+        if (!S_ISREG(i->st.st_mode))
                 return;
 
-        if (!f || f->done)
-                return;
-
-        f->done = true;
-
-        if (result != CURLE_OK) {
-                log_error("Transfer failed: %s", curl_easy_strerror(result));
-                r = -EIO;
-                goto fail;
-        }
+        if (i->written_compressed >= (uint64_t) i->st.st_size)
+                percent = 100;
+        else
+                percent = (unsigned) ((i->written_compressed * UINT64_C(100)) / (uint64_t) i->st.st_size);
 
-        code = curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &status);
-        if (code != CURLE_OK) {
-                log_error("Failed to retrieve response code: %s", curl_easy_strerror(code));
-                r = -EIO;
-                goto fail;
-        } else if (status == 304) {
-                log_info("Image already downloaded. Skipping download.");
-                raw_import_file_success(f);
+        if (percent == i->last_percent)
                 return;
-        } else if (status >= 300) {
-                log_error("HTTP request to %s failed with code %li.", f->url, status);
-                r = -EIO;
-                goto fail;
-        } else if (status < 200) {
-                log_error("HTTP request to %s finished with unexpected code %li.", f->url, status);
-                r = -EIO;
-                goto fail;
-        }
-
-        if (f->disk_fd < 0) {
-                log_error("No data received.");
-                r = -EIO;
-                goto fail;
-        }
-
-        if (f->content_length != (uint64_t) -1 &&
-            f->content_length != f->written) {
-                log_error("Download truncated.");
-                r = -EIO;
-                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);
+        if (!ratelimit_test(&i->progress_rate_limit))
+                return;
 
-        raw_import_file_success(f);
-        return;
+        sd_notifyf(false, "X_IMPORT_PROGRESS=%u", percent);
+        log_info("Imported %u%%.", percent);
 
-fail:
-        raw_import_finish(f->import, r);
+        i->last_percent = percent;
 }
 
-static int raw_import_file_open_disk_for_write(RawImportFile *f) {
+static int raw_import_maybe_convert_qcow2(RawImport *i) {
+        _cleanup_close_ int converted_fd = -1;
+        _cleanup_free_ char *t = NULL;
         int r;
 
-        assert(f);
+        assert(i);
 
-        if (f->disk_fd >= 0)
+        r = qcow2_detect(i->output_fd);
+        if (r < 0)
+                return log_error_errno(r, "Failed to detect whether this is a QCOW2 image: %m");
+        if (r == 0)
                 return 0;
 
-        r = raw_import_file_make_final_path(f);
+        /* This is a QCOW2 image, let's convert it */
+        r = tempfn_random(i->final_path, &t);
         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);
+        converted_fd = open(t, O_RDWR|O_CREAT|O_EXCL|O_NOCTTY|O_CLOEXEC, 0664);
+        if (converted_fd < 0)
+                return log_error_errno(errno, "Failed to create %s: %m", t);
 
-        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;
-        ssize_t n;
-        int r;
-
-        assert(contents);
-        assert(f);
-
-        if (f->done) {
-                r = -ESTALE;
-                goto fail;
-        }
-
-        r = raw_import_file_open_disk_for_write(f);
+        r = chattr_fd(converted_fd, true, FS_NOCOW_FL);
         if (r < 0)
-                goto fail;
-
-        if (f->written + sz < f->written) {
-                log_error("File too large, overflow");
-                r = -EOVERFLOW;
-                goto fail;
-        }
-
-        if (f->content_length != (uint64_t) -1 &&
-            f->written + sz > f->content_length) {
-                log_error("Content length incorrect.");
-                r = -EFBIG;
-                goto fail;
-        }
+                log_warning_errno(errno, "Failed to set file attributes on %s: %m", t);
 
-        n = write(f->disk_fd, contents, sz);
-        if (n < 0) {
-                log_error_errno(errno, "Failed to write file: %m");
-                goto fail;
-        }
+        log_info("Unpacking QCOW2 file.");
 
-        if ((size_t) n < sz) {
-                log_error("Short write");
-                r = -EIO;
-                goto fail;
+        r = qcow2_convert(i->output_fd, converted_fd);
+        if (r < 0) {
+                unlink(t);
+                return log_error_errno(r, "Failed to convert qcow2 image: %m");
         }
 
-        f->written += sz;
+        (void) unlink(i->temp_path);
+        free(i->temp_path);
+        i->temp_path = t;
+        t = NULL;
 
-        return sz;
+        safe_close(i->output_fd);
+        i->output_fd = converted_fd;
+        converted_fd = -1;
 
-fail:
-        raw_import_finish(f->import, r);
-        return 0;
+        return 1;
 }
 
-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;
+static int raw_import_finish(RawImport *i) {
         int r;
 
-        assert(contents);
-        assert(f);
+        assert(i);
+        assert(i->output_fd >= 0);
+        assert(i->temp_path);
+        assert(i->final_path);
 
-        if (f->done) {
-                r = -ESTALE;
-                goto fail;
+        /* In case this was a sparse file, make sure the file system is right */
+        if (i->written_uncompressed > 0) {
+                if (ftruncate(i->output_fd, i->written_uncompressed) < 0)
+                        return log_error_errno(errno, "Failed to truncate file: %m");
         }
 
-        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;
-                }
+        r = raw_import_maybe_convert_qcow2(i);
+        if (r < 0)
+                return r;
 
-                return sz;
+        if (S_ISREG(i->st.st_mode)) {
+                (void) copy_times(i->input_fd, i->output_fd);
+                (void) copy_xattr(i->input_fd, i->output_fd);
         }
 
-        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);
-                return sz;
+        if (i->read_only) {
+                r = import_make_read_only_fd(i->output_fd);
+                if (r < 0)
+                        return r;
         }
 
-        r = curl_header_strdup(contents, sz, "Last-Modified:", &last_modified);
-        if (r < 0) {
-                log_oom();
-                goto fail;
+        if (i->force_local) {
+                (void) btrfs_subvol_remove(i->final_path);
+                (void) rm_rf_dangerous(i->final_path, false, true, false);
         }
-        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 bool etag_is_valid(const char *etag) {
-
-        if (!endswith(etag, "\""))
-                return false;
+        r = rename_noreplace(AT_FDCWD, i->temp_path, AT_FDCWD, i->final_path);
+        if (r < 0)
+                return log_error_errno(r, "Failed to move image into place: %m");
 
-        if (!startswith(etag, "\"") && !startswith(etag, "W/\""))
-                return false;
+        free(i->temp_path);
+        i->temp_path = NULL;
 
-        return true;
+        return 0;
 }
 
-static int raw_import_file_find_old_etags(RawImportFile *f) {
-        _cleanup_free_ char *escaped_url = NULL;
-        _cleanup_closedir_ DIR *d = NULL;
-        struct dirent *de;
+static int raw_import_open_disk(RawImport *i) {
         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;
+        assert(i);
 
-                if (de->d_type != DT_UNKNOWN &&
-                    de->d_type != DT_REG)
-                        continue;
+        assert(!i->final_path);
+        assert(!i->temp_path);
+        assert(i->output_fd < 0);
 
-                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;
+        i->final_path = strjoin(i->image_root, "/", i->local, ".raw", NULL);
+        if (!i->final_path)
+                return log_oom();
 
-                if (a >= b)
-                        continue;
+        r = tempfn_random(i->final_path, &i->temp_path);
+        if (r < 0)
+                return log_oom();
 
-                u = cunescape_length(a, b - a);
-                if (!u)
-                        return -ENOMEM;
+        (void) mkdir_parents_label(i->temp_path, 0700);
 
-                if (!etag_is_valid(u)) {
-                        free(u);
-                        continue;
-                }
+        i->output_fd = open(i->temp_path, O_RDWR|O_CREAT|O_EXCL|O_NOCTTY|O_CLOEXEC, 0664);
+        if (i->output_fd < 0)
+                return log_error_errno(errno, "Failed to open destination %s: %m", i->temp_path);
 
-                r = strv_consume(&f->old_etags, u);
-                if (r < 0)
-                        return r;
-        }
+        r = chattr_fd(i->output_fd, true, FS_NOCOW_FL);
+        if (r < 0)
+                log_warning_errno(errno, "Failed to set file attributes on %s: %m", i->temp_path);
 
         return 0;
 }
 
-static int raw_import_file_begin(RawImportFile *f) {
+static int raw_import_try_reflink(RawImport *i) {
+        off_t p;
         int r;
 
-        assert(f);
-        assert(!f->curl);
+        assert(i);
+        assert(i->input_fd >= 0);
+        assert(i->output_fd >= 0);
 
-        log_info("Getting %s.", f->url);
+        if (i->compress.type != IMPORT_COMPRESS_UNCOMPRESSED)
+                return 0;
 
-        r = raw_import_file_find_old_etags(f);
-        if (r < 0)
-                return r;
+        if (!S_ISREG(i->st.st_mode))
+                return 0;
 
-        r = curl_glue_make(&f->curl, f->url, f);
-        if (r < 0)
-                return r;
+        p = lseek(i->input_fd, 0, SEEK_CUR);
+        if (p == (off_t) -1)
+                return log_error_errno(errno, "Failed to read file offset of input file: %m");
 
-        if (!strv_isempty(f->old_etags)) {
-                _cleanup_free_ char *cc = NULL, *hdr = NULL;
+        /* Let's only try a btrfs reflink, if we are reading from the beginning of the file */
+        if ((uint64_t) p != (uint64_t) i->buffer_size)
+                return 0;
 
-                cc = strv_join(f->old_etags, ", ");
-                if (!cc)
-                        return -ENOMEM;
+        r = btrfs_reflink(i->input_fd, i->output_fd);
+        if (r >= 0)
+                return 1;
 
-                hdr = strappend("If-None-Match: ", cc);
-                if (!hdr)
-                        return -ENOMEM;
+        return 0;
+}
 
-                f->request_header = curl_slist_new(hdr, NULL);
-                if (!f->request_header)
-                        return -ENOMEM;
+static int raw_import_write(const void *p, size_t sz, void *userdata) {
+        RawImport *i = userdata;
+        ssize_t n;
 
-                if (curl_easy_setopt(f->curl, CURLOPT_HTTPHEADER, f->request_header) != CURLE_OK)
-                        return -EIO;
+        if (i->grow_machine_directory && i->written_since_last_grow >= GROW_INTERVAL_BYTES) {
+                i->written_since_last_grow = 0;
+                grow_machine_directory();
         }
 
-        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)
+        n = sparse_write(i->output_fd, p, sz, 64);
+        if (n < 0)
+                return -errno;
+        if ((size_t) n < sz)
                 return -EIO;
 
-        r = curl_glue_add(f->import->glue, f->curl);
-        if (r < 0)
-                return r;
+        i->written_uncompressed += sz;
+        i->written_since_last_grow += sz;
 
         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;
+static int raw_import_process(RawImport *i) {
+        ssize_t l;
         int r;
 
-        assert(import);
-        assert(image_root);
+        assert(i);
+        assert(i->buffer_size < sizeof(i->buffer));
 
-        i = new0(RawImport, 1);
-        if (!i)
-                return -ENOMEM;
-
-        i->on_finished = on_finished;
-        i->userdata = userdata;
+        l = read(i->input_fd, i->buffer + i->buffer_size, sizeof(i->buffer) - i->buffer_size);
+        if (l < 0) {
+                if (errno == EAGAIN)
+                        return 0;
 
-        i->image_root = strdup(image_root);
-        if (!i->image_root)
-                return -ENOMEM;
+                r = log_error_errno(errno, "Failed to read input file: %m");
+                goto finish;
+        }
+        if (l == 0) {
+                if (i->compress.type == IMPORT_COMPRESS_UNKNOWN) {
+                        log_error("Premature end of file: %m");
+                        r = -EIO;
+                        goto finish;
+                }
 
-        if (event)
-                i->event = sd_event_ref(event);
-        else {
-                r = sd_event_default(&i->event);
-                if (r < 0)
-                        return r;
+                r = raw_import_finish(i);
+                goto finish;
         }
 
-        r = curl_glue_new(&i->glue, i->event);
-        if (r < 0)
-                return r;
+        i->buffer_size += l;
 
-        i->glue->on_finished = raw_import_curl_on_finished;
-        i->glue->userdata = i;
+        if (i->compress.type == IMPORT_COMPRESS_UNKNOWN) {
+                r = import_uncompress_detect(&i->compress, i->buffer, i->buffer_size);
+                if (r < 0) {
+                        log_error("Failed to detect file compression: %m");
+                        goto finish;
+                }
+                if (r == 0) /* Need more data */
+                        return 0;
 
-        *import = i;
-        i = NULL;
+                r = raw_import_open_disk(i);
+                if (r < 0)
+                        goto finish;
 
-        return 0;
-}
+                r = raw_import_try_reflink(i);
+                if (r < 0)
+                        goto finish;
+                if (r > 0) {
+                        r = raw_import_finish(i);
+                        goto finish;
+                }
+        }
 
-RawImport* raw_import_unref(RawImport *import) {
-        RawImportFile *f;
+        r = import_uncompress(&i->compress, i->buffer, i->buffer_size, raw_import_write, i);
+        if (r < 0) {
+                log_error_errno(r, "Failed to decode and write: %m");
+                goto finish;
+        }
 
-        if (!import)
-                return NULL;
+        i->written_compressed += i->buffer_size;
+        i->buffer_size = 0;
 
-        while ((f = hashmap_steal_first(import->files)))
-                raw_import_file_unref(f);
-        hashmap_free(import->files);
+        raw_import_report_progress(i);
 
-        curl_glue_unref(import->glue);
-        sd_event_unref(import->event);
+        return 0;
 
-        free(import->image_root);
-        free(import);
+finish:
+        if (i->on_finished)
+                i->on_finished(i, r, i->userdata);
+        else
+                sd_event_exit(i->event, r);
 
-        return NULL;
+        return 0;
 }
 
-int raw_import_cancel(RawImport *import, const char *url) {
-        RawImportFile *f;
+static int raw_import_on_input(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
+        RawImport *i = userdata;
 
-        assert(import);
-        assert(url);
+        return raw_import_process(i);
+}
 
-        f = hashmap_remove(import->files, url);
-        if (!f)
-                return 0;
+static int raw_import_on_defer(sd_event_source *s, void *userdata) {
+        RawImport *i = userdata;
 
-        raw_import_file_unref(f);
-        return 1;
+        return raw_import_process(i);
 }
 
-int raw_import_pull(RawImport *import, const char *url, const char *local, bool force_local) {
-        _cleanup_(raw_import_file_unrefp) RawImportFile *f = NULL;
+int raw_import_start(RawImport *i, int fd, const char *local, bool force_local, bool read_only) {
         int r;
 
-        assert(import);
-        assert(raw_url_is_valid(url));
-        assert(!local || machine_name_is_valid(local));
+        assert(i);
+        assert(fd >= 0);
+        assert(local);
 
-        if (hashmap_get(import->files, url))
-                return -EEXIST;
+        if (!machine_name_is_valid(local))
+                return -EINVAL;
 
-        r = hashmap_ensure_allocated(&import->files, &string_hash_ops);
+        if (i->input_fd >= 0)
+                return -EBUSY;
+
+        r = fd_nonblock(fd, true);
         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;
+        r = free_and_strdup(&i->local, local);
+        if (r < 0)
+                return r;
+        i->force_local = force_local;
+        i->read_only = read_only;
 
-        f->url = strdup(url);
-        if (!f->url)
-                return -ENOMEM;
+        if (fstat(fd, &i->st) < 0)
+                return -errno;
 
-        if (local) {
-                f->local = strdup(local);
-                if (!f->local)
-                        return -ENOMEM;
+        r = sd_event_add_io(i->event, &i->input_event_source, fd, EPOLLIN, raw_import_on_input, i);
+        if (r == -EPERM) {
+                /* This fd does not support epoll, for example because it is a regular file. Busy read in that case */
+                r = sd_event_add_defer(i->event, &i->input_event_source, raw_import_on_defer, i);
+                if (r < 0)
+                        return r;
 
-                f->force_local = force_local;
+                r = sd_event_source_set_enabled(i->input_event_source, SD_EVENT_ON);
         }
-
-        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);
+        i->input_fd = fd;
+        return r;
 }