#include <linux/fs.h>
#include <curl/curl.h>
-#include "hashmap.h"
+#include "sd-daemon.h"
#include "utf8.h"
-#include "curl-util.h"
-#include "import-raw.h"
#include "strv.h"
#include "copy.h"
+#include "btrfs-util.h"
+#include "util.h"
+#include "macro.h"
+#include "mkdir.h"
+#include "import-util.h"
+#include "curl-util.h"
+#include "qcow2-util.h"
+#include "import-job.h"
+#include "import-common.h"
+#include "import-raw.h"
-typedef struct RawImportFile RawImportFile;
-
-struct RawImportFile {
- RawImport *import;
-
- char *url;
- char *local;
-
- CURL *curl;
- struct curl_slist *request_header;
-
- char *temp_path;
- char *final_path;
- char *etag;
- char **old_etags;
-
- uint64_t content_length;
- uint64_t written;
-
- usec_t mtime;
-
- bool force_local;
- bool done;
-
- int disk_fd;
-};
+typedef enum RawProgress {
+ RAW_DOWNLOADING,
+ RAW_VERIFYING,
+ RAW_UNPACKING,
+ RAW_FINALIZING,
+ RAW_COPYING,
+} RawProgress;
struct RawImport {
sd_event *event;
CurlGlue *glue;
char *image_root;
- Hashmap *files;
- raw_import_on_finished on_finished;
+ ImportJob *raw_job;
+ ImportJob *checksum_job;
+ ImportJob *signature_job;
+
+ RawImportFinished on_finished;
void *userdata;
- bool finished;
-};
+ char *local;
+ bool force_local;
+
+ char *temp_path;
+ char *final_path;
-#define FILENAME_ESCAPE "/.#\"\'"
+ ImportVerify verify;
+};
-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);
+ import_job_unref(i->raw_job);
+ import_job_unref(i->checksum_job);
+ import_job_unref(i->signature_job);
- safe_close(f->disk_fd);
+ curl_glue_unref(i->glue);
+ 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);
+ free(i->final_path);
+ free(i->image_root);
+ free(i->local);
+ free(i);
return NULL;
}
-DEFINE_TRIVIAL_CLEANUP_FUNC(RawImportFile*, raw_import_file_unref);
-
-static void raw_import_finish(RawImport *import, int error) {
- assert(import);
+int raw_import_new(
+ RawImport **ret,
+ sd_event *event,
+ const char *image_root,
+ RawImportFinished on_finished,
+ void *userdata) {
- if (import->finished)
- return;
+ _cleanup_(raw_import_unrefp) RawImport *i = NULL;
+ int r;
- import->finished = true;
+ assert(ret);
- if (import->on_finished)
- import->on_finished(import, error, import->userdata);
- else
- sd_event_exit(import->event, error);
-}
+ i = new0(RawImport, 1);
+ if (!i)
+ return -ENOMEM;
-static int raw_import_file_make_final_path(RawImportFile *f) {
- _cleanup_free_ char *escaped_url = NULL, *escaped_etag = NULL;
+ i->on_finished = on_finished;
+ i->userdata = userdata;
- assert(f);
+ i->image_root = strdup(image_root ?: "/var/lib/machines");
+ if (!i->image_root)
+ return -ENOMEM;
- if (f->final_path)
- return 0;
+ if (event)
+ i->event = sd_event_ref(event);
+ else {
+ r = sd_event_default(&i->event);
+ if (r < 0)
+ return r;
+ }
- escaped_url = xescape(f->url, FILENAME_ESCAPE);
- if (!escaped_url)
- return -ENOMEM;
+ r = curl_glue_new(&i->glue, i->event);
+ if (r < 0)
+ return r;
- if (f->etag) {
- escaped_etag = xescape(f->etag, FILENAME_ESCAPE);
- if (!escaped_etag)
- return -ENOMEM;
+ i->glue->on_finished = import_job_curl_on_finished;
+ i->glue->userdata = i;
- 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)
- return -ENOMEM;
+ *ret = i;
+ i = NULL;
return 0;
}
-static void raw_import_file_success(RawImportFile *f) {
- int r;
+static void raw_import_report_progress(RawImport *i, RawProgress p) {
+ unsigned percent;
- 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;
- }
- }
+ assert(i);
- p = strappenda(f->import->image_root, "/", f->local, ".raw");
- if (f->force_local)
- (void) rm_rf_dangerous(p, false, true, false);
+ switch (p) {
- r = tempfn_random(p, &tp);
- if (r < 0) {
- log_oom();
- goto finish;
- }
-
- 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;
- }
+ case RAW_DOWNLOADING: {
+ unsigned remain = 80;
- /* 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 (r < 0)
- log_warning_errno(errno, "Failed to set file attributes on %s: %m", f->temp_path);
+ percent = 0;
- 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;
+ if (i->checksum_job) {
+ percent += i->checksum_job->progress_percent * 5 / 100;
+ remain -= 5;
}
- (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;
+ if (i->signature_job) {
+ percent += i->signature_job->progress_percent * 5 / 100;
+ remain -= 5;
}
- log_info("Created new local image %s.", p);
+ if (i->raw_job)
+ percent += i->raw_job->progress_percent * remain / 100;
+ break;
}
- f->disk_fd = safe_close(f->disk_fd);
- r = 0;
+ case RAW_VERIFYING:
+ percent = 80;
+ break;
-finish:
- raw_import_finish(f->import, r);
-}
+ case RAW_UNPACKING:
+ percent = 85;
+ break;
-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;
-
- if (curl_easy_getinfo(curl, CURLINFO_PRIVATE, &f) != CURLE_OK)
- 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;
- }
+ case RAW_FINALIZING:
+ percent = 90;
+ break;
- 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);
- 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;
- }
+ case RAW_COPYING:
+ percent = 95;
+ break;
- if (f->disk_fd < 0) {
- log_error("No data received.");
- r = -EIO;
- goto fail;
+ default:
+ assert_not_reached("Unknown progress state");
}
- if (f->content_length != (uint64_t) -1 &&
- f->content_length != f->written) {
- log_error("Download truncated.");
- r = -EIO;
- goto fail;
- }
+ sd_notifyf(false, "X_IMPORT_PROGRESS=%u", percent);
+ log_debug("Combined progress %u%%", percent);
+}
- 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);
+static int raw_import_maybe_convert_qcow2(RawImport *i) {
+ _cleanup_close_ int converted_fd = -1;
+ _cleanup_free_ char *t = NULL;
+ int r;
- if (f->mtime != 0) {
- struct timespec ut[2];
+ assert(i);
+ assert(i->raw_job);
- timespec_store(&ut[0], f->mtime);
- ut[1] = ut[0];
- (void) futimens(f->disk_fd, ut);
+ r = qcow2_detect(i->raw_job->disk_fd);
+ if (r < 0)
+ return log_error_errno(r, "Failed to detect whether this is a QCOW2 image: %m");
+ if (r == 0)
+ return 0;
- fd_setcrtime(f->disk_fd, f->mtime);
- }
+ /* This is a QCOW2 image, let's convert it */
+ r = tempfn_random(i->final_path, &t);
+ if (r < 0)
+ return log_oom();
- if (fstat(f->disk_fd, &st) < 0) {
- r = log_error_errno(errno, "Failed to stat file: %m");
- goto fail;
- }
+ converted_fd = open(t, O_RDWR|O_CREAT|O_EXCL|O_NOCTTY|O_CLOEXEC, 0644);
+ if (converted_fd < 0)
+ return log_error_errno(errno, "Failed to create %s: %m", t);
- /* Mark read-only */
- (void) fchmod(f->disk_fd, st.st_mode & 07444);
+ r = chattr_fd(converted_fd, true, FS_NOCOW_FL);
+ if (r < 0)
+ log_warning_errno(errno, "Failed to set file attributes on %s: %m", t);
- assert(f->temp_path);
- assert(f->final_path);
+ log_info("Unpacking QCOW2 file.");
- r = rename(f->temp_path, f->final_path);
+ r = qcow2_convert(i->raw_job->disk_fd, converted_fd);
if (r < 0) {
- r = log_error_errno(errno, "Failed to move RAW file into place: %m");
- goto fail;
+ unlink(t);
+ return log_error_errno(r, "Failed to convert qcow2 image: %m");
}
- free(f->temp_path);
- f->temp_path = NULL;
+ unlink(i->temp_path);
+ free(i->temp_path);
- log_info("Completed writing vendor image %s.", f->final_path);
+ i->temp_path = t;
+ t = NULL;
- raw_import_file_success(f);
- return;
+ safe_close(i->raw_job->disk_fd);
+ i->raw_job->disk_fd = converted_fd;
+ converted_fd = -1;
-fail:
- raw_import_finish(f->import, r);
+ return 1;
}
-static int raw_import_file_open_disk_for_write(RawImportFile *f) {
+static int raw_import_make_local_copy(RawImport *i) {
+ _cleanup_free_ char *tp = NULL;
+ _cleanup_close_ int dfd = -1;
+ const char *p;
int r;
- assert(f);
+ assert(i);
+ assert(i->raw_job);
- if (f->disk_fd >= 0)
+ if (!i->local)
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);
-
- 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;
+ if (i->raw_job->etag_exists) {
+ /* We have downloaded this one previously, reopen it */
- assert(contents);
- assert(f);
+ assert(i->raw_job->disk_fd < 0);
- if (f->done) {
- r = -ESTALE;
- goto fail;
- }
+ if (!i->final_path) {
+ r = import_make_path(i->raw_job->url, i->raw_job->etag, i->image_root, ".raw-", ".raw", &i->final_path);
+ if (r < 0)
+ return log_oom();
+ }
- r = raw_import_file_open_disk_for_write(f);
- if (r < 0)
- goto fail;
+ i->raw_job->disk_fd = open(i->final_path, O_RDONLY|O_NOCTTY|O_CLOEXEC);
+ if (i->raw_job->disk_fd < 0)
+ return log_error_errno(errno, "Failed to open vendor image: %m");
+ } else {
+ /* We freshly downloaded the image, use it */
- if (f->written + sz < f->written) {
- log_error("File too large, overflow");
- r = -EOVERFLOW;
- goto fail;
- }
+ assert(i->raw_job->disk_fd >= 0);
- if (f->content_length != (uint64_t) -1 &&
- f->written + sz > f->content_length) {
- log_error("Content length incorrect.");
- r = -EFBIG;
- goto fail;
+ if (lseek(i->raw_job->disk_fd, SEEK_SET, 0) == (off_t) -1)
+ return log_error_errno(errno, "Failed to seek to beginning of vendor image: %m");
}
- n = write(f->disk_fd, contents, sz);
- if (n < 0) {
- log_error_errno(errno, "Failed to write file: %m");
- goto fail;
- }
+ p = strappenda(i->image_root, "/", i->local, ".raw");
- if ((size_t) n < sz) {
- log_error("Short write");
- r = -EIO;
- goto fail;
+ if (i->force_local) {
+ (void) btrfs_subvol_remove(p);
+ (void) rm_rf_dangerous(p, false, true, false);
}
- f->written += sz;
-
- 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;
+ r = tempfn_random(p, &tp);
+ if (r < 0)
+ return log_oom();
- assert(contents);
- assert(f);
+ dfd = open(tp, O_WRONLY|O_CREAT|O_EXCL|O_NOCTTY|O_CLOEXEC, 0664);
+ if (dfd < 0)
+ return log_error_errno(errno, "Failed to create writable copy of image: %m");
- if (f->done) {
- r = -ESTALE;
- goto fail;
- }
+ /* 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 (r < 0)
+ log_warning_errno(errno, "Failed to set file attributes on %s: %m", tp);
- r = curl_header_strdup(contents, sz, "ETag:", &etag);
+ r = copy_bytes(i->raw_job->disk_fd, dfd, (off_t) -1, true);
if (r < 0) {
- log_oom();
- goto fail;
+ unlink(tp);
+ return log_error_errno(r, "Failed to make writable copy of image: %m");
}
- 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;
- }
+ (void) copy_times(i->raw_job->disk_fd, dfd);
+ (void) copy_xattr(i->raw_job->disk_fd, dfd);
- 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;
- }
+ dfd = safe_close(dfd);
- 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;
+ r = rename(tp, p);
+ if (r < 0) {
+ unlink(tp);
+ return log_error_errno(errno, "Failed to move writable image into place: %m");
}
- return sz;
-
-fail:
- raw_import_finish(f->import, r);
+ log_info("Created new local image '%s'.", i->local);
return 0;
}
-static bool etag_is_valid(const char *etag) {
+static bool raw_import_is_done(RawImport *i) {
+ assert(i);
+ assert(i->raw_job);
- if (!endswith(etag, "\""))
+ if (i->raw_job->state != IMPORT_JOB_DONE)
return false;
-
- if (!startswith(etag, "\"") && !startswith(etag, "W/\""))
+ if (i->checksum_job && i->checksum_job->state != IMPORT_JOB_DONE)
+ return false;
+ if (i->signature_job && i->signature_job->state != IMPORT_JOB_DONE)
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;
+static void raw_import_job_on_finished(ImportJob *j) {
+ RawImport *i;
int r;
- escaped_url = xescape(f->url, FILENAME_ESCAPE);
- if (!escaped_url)
- return -ENOMEM;
+ assert(j);
+ assert(j->userdata);
- d = opendir(f->import->image_root);
- if (!d) {
- if (errno == ENOENT)
- return 0;
+ i = j->userdata;
+ if (j->error != 0) {
+ if (j == i->checksum_job)
+ log_error_errno(j->error, "Failed to retrieve SHA256 checksum, cannot verify. (Try --verify=no?)");
+ else if (j == i->signature_job)
+ log_error_errno(j->error, "Failed to retrieve signature file, cannot verify. (Try --verify=no?)");
+ else
+ log_error_errno(j->error, "Failed to retrieve image file. (Wrong URL?)");
- return -errno;
+ r = j->error;
+ goto finish;
}
- 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;
+ /* This is invoked if either the download completed
+ * successfully, or the download was skipped because we
+ * already have the etag. In this case ->etag_exists is
+ * true.
+ *
+ * We only do something when we got all three files */
- a = startswith(a, ".");
- if (!a)
- continue;
-
- b = endswith(de->d_name, ".raw");
- if (!b)
- continue;
-
- if (a >= b)
- continue;
+ if (!raw_import_is_done(i))
+ return;
- u = cunescape_length(a, b - a);
- if (!u)
- return -ENOMEM;
+ if (!i->raw_job->etag_exists) {
+ /* This is a new download, verify it, and move it into place */
+ assert(i->raw_job->disk_fd >= 0);
- if (!etag_is_valid(u)) {
- free(u);
- continue;
- }
+ raw_import_report_progress(i, RAW_VERIFYING);
- r = strv_consume(&f->old_etags, u);
+ r = import_verify(i->raw_job, i->checksum_job, i->signature_job);
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;
+ goto finish;
- r = curl_glue_make(&f->curl, f->url, f);
- if (r < 0)
- return r;
+ raw_import_report_progress(i, RAW_UNPACKING);
- if (!strv_isempty(f->old_etags)) {
- _cleanup_free_ char *cc = NULL, *hdr = NULL;
+ r = raw_import_maybe_convert_qcow2(i);
+ if (r < 0)
+ goto finish;
- cc = strv_join(f->old_etags, ", ");
- if (!cc)
- return -ENOMEM;
+ raw_import_report_progress(i, RAW_FINALIZING);
- hdr = strappend("If-None-Match: ", cc);
- if (!hdr)
- return -ENOMEM;
+ r = import_make_read_only_fd(i->raw_job->disk_fd);
+ if (r < 0)
+ goto finish;
- f->request_header = curl_slist_new(hdr, NULL);
- if (!f->request_header)
- return -ENOMEM;
+ r = rename(i->temp_path, i->final_path);
+ if (r < 0) {
+ r = log_error_errno(errno, "Failed to move RAW file into place: %m");
+ goto finish;
+ }
- if (curl_easy_setopt(f->curl, CURLOPT_HTTPHEADER, f->request_header) != CURLE_OK)
- return -EIO;
+ free(i->temp_path);
+ i->temp_path = NULL;
}
- 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;
+ raw_import_report_progress(i, RAW_COPYING);
- if (curl_easy_setopt(f->curl, CURLOPT_HEADERDATA, f) != CURLE_OK)
- return -EIO;
-
- r = curl_glue_add(f->import->glue, f->curl);
+ r = raw_import_make_local_copy(i);
if (r < 0)
- return r;
+ goto finish;
- return 0;
+ r = 0;
+
+finish:
+ if (i->on_finished)
+ i->on_finished(i, r, i->userdata);
+ else
+ sd_event_exit(i->event, r);
}
-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_job_on_open_disk(ImportJob *j) {
+ RawImport *i;
int r;
- assert(import);
- assert(image_root);
-
- i = new0(RawImport, 1);
- if (!i)
- return -ENOMEM;
+ assert(j);
+ assert(j->userdata);
- i->on_finished = on_finished;
- i->userdata = userdata;
+ i = j->userdata;
+ assert(i->raw_job == j);
+ assert(!i->final_path);
+ assert(!i->temp_path);
- i->image_root = strdup(image_root);
- if (!i->image_root)
- return -ENOMEM;
+ r = import_make_path(j->url, j->etag, i->image_root, ".raw-", ".raw", &i->final_path);
+ if (r < 0)
+ return log_oom();
- if (event)
- i->event = sd_event_ref(event);
- else {
- r = sd_event_default(&i->event);
- if (r < 0)
- return r;
- }
+ r = tempfn_random(i->final_path, &i->temp_path);
+ if (r <0)
+ return log_oom();
- r = curl_glue_new(&i->glue, i->event);
- if (r < 0)
- return r;
+ mkdir_parents_label(i->temp_path, 0700);
- i->glue->on_finished = raw_import_curl_on_finished;
- i->glue->userdata = i;
+ j->disk_fd = open(i->temp_path, O_RDWR|O_CREAT|O_EXCL|O_NOCTTY|O_CLOEXEC, 0644);
+ if (j->disk_fd < 0)
+ return log_error_errno(errno, "Failed to create %s: %m", i->temp_path);
- *import = i;
- i = NULL;
+ r = chattr_fd(j->disk_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;
}
-RawImport* raw_import_unref(RawImport *import) {
- RawImportFile *f;
+static void raw_import_job_on_progress(ImportJob *j) {
+ RawImport *i;
- if (!import)
- return NULL;
-
- while ((f = hashmap_steal_first(import->files)))
- raw_import_file_unref(f);
- hashmap_free(import->files);
+ assert(j);
+ assert(j->userdata);
- curl_glue_unref(import->glue);
- sd_event_unref(import->event);
+ i = j->userdata;
- free(import->image_root);
- free(import);
-
- return NULL;
+ raw_import_report_progress(i, RAW_DOWNLOADING);
}
-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;
+int raw_import_pull(RawImport *i, const char *url, const char *local, bool force_local, ImportVerify verify) {
+ int r;
- raw_import_file_unref(f);
- return 1;
-}
+ assert(i);
+ assert(verify < _IMPORT_VERIFY_MAX);
+ assert(verify >= 0);
-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;
+ if (!http_url_is_valid(url))
+ return -EINVAL;
- assert(import);
- assert(raw_url_is_valid(url));
- assert(!local || machine_name_is_valid(local));
+ if (local && !machine_name_is_valid(local))
+ return -EINVAL;
- if (hashmap_get(import->files, url))
- return -EEXIST;
+ if (i->raw_job)
+ return -EBUSY;
- r = hashmap_ensure_allocated(&import->files, &string_hash_ops);
+ r = free_and_strdup(&i->local, local);
if (r < 0)
return r;
+ i->force_local = force_local;
+ i->verify = verify;
- f = new0(RawImportFile, 1);
- if (!f)
- return -ENOMEM;
-
- f->import = import;
- f->disk_fd = -1;
- f->content_length = (uint64_t) -1;
-
- f->url = strdup(url);
- if (!f->url)
- return -ENOMEM;
+ /* Queue job for the image itself */
+ r = import_job_new(&i->raw_job, url, i->glue, i);
+ if (r < 0)
+ return r;
- if (local) {
- f->local = strdup(local);
- if (!f->local)
- return -ENOMEM;
+ i->raw_job->on_finished = raw_import_job_on_finished;
+ i->raw_job->on_open_disk = raw_import_job_on_open_disk;
+ i->raw_job->on_progress = raw_import_job_on_progress;
+ i->raw_job->calc_checksum = verify != IMPORT_VERIFY_NO;
- f->force_local = force_local;
- }
+ r = import_find_old_etags(url, i->image_root, DT_REG, ".raw-", ".raw", &i->raw_job->old_etags);
+ if (r < 0)
+ return r;
- r = hashmap_put(import->files, f->url, f);
+ r = import_make_verification_jobs(&i->checksum_job, &i->signature_job, verify, url, i->glue, raw_import_job_on_finished, i);
if (r < 0)
return r;
- r = raw_import_file_begin(f);
- if (r < 0) {
- raw_import_cancel(import, f->url);
- f = NULL;
+ r = import_job_begin(i->raw_job);
+ if (r < 0)
return r;
- }
- f = NULL;
- return 0;
-}
+ if (i->checksum_job) {
+ i->checksum_job->on_progress = raw_import_job_on_progress;
-bool raw_url_is_valid(const char *url) {
- if (isempty(url))
- return false;
+ r = import_job_begin(i->checksum_job);
+ if (r < 0)
+ return r;
+ }
- if (!startswith(url, "http://") &&
- !startswith(url, "https://"))
- return false;
+ if (i->signature_job) {
+ i->signature_job->on_progress = raw_import_job_on_progress;
- return ascii_is_valid(url);
+ r = import_job_begin(i->signature_job);
+ if (r < 0)
+ return r;
+ }
+
+ return 0;
}