lzma_stream lzma;
bool compressed;
+
+ unsigned progress_percent;
+ usec_t start_usec;
+ usec_t last_status_usec;
};
struct RawImport {
free(f->temp_path);
}
+ lzma_end(&f->lzma);
free(f->url);
free(f->local);
free(f->etag);
return 0;
}
-static void raw_import_file_success(RawImportFile *f) {
+static int raw_import_file_make_local_copy(RawImportFile *f) {
+ _cleanup_free_ char *tp = NULL;
+ _cleanup_close_ int dfd = -1;
+ const char *p;
int r;
assert(f);
- f->done = true;
+ if (!f->local)
+ return 0;
- 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)
+ return log_error_errno(errno, "Failed to seek to beginning of vendor image: %m");
+ } else {
+ r = raw_import_file_make_final_path(f);
+ if (r < 0)
+ return log_oom();
- 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)
+ return log_error_errno(errno, "Failed to open vendor image: %m");
+ }
- 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;
- }
- }
+ p = strappenda(f->import->image_root, "/", f->local, ".raw");
+ if (f->force_local)
+ (void) rm_rf_dangerous(p, false, true, false);
- p = strappenda(f->import->image_root, "/", f->local, ".raw");
- if (f->force_local)
- (void) rm_rf_dangerous(p, false, true, false);
+ r = tempfn_random(p, &tp);
+ if (r < 0)
+ return log_oom();
- 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)
+ return log_error_errno(errno, "Failed to create writable copy of image: %m");
- 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;
- }
+ /* 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);
- /* 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 = copy_bytes(f->disk_fd, dfd, (off_t) -1, true);
+ if (r < 0) {
+ unlink(tp);
+ return log_error_errno(r, "Failed to make writable copy of image: %m");
+ }
- 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);
- (void) copy_times(f->disk_fd, dfd);
- (void) copy_xattr(f->disk_fd, dfd);
+ dfd = safe_close(dfd);
- dfd = safe_close(dfd);
+ r = rename(tp, p);
+ if (r < 0) {
+ unlink(tp);
+ return log_error_errno(errno, "Failed to move writable image into place: %m");
+ }
- 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 0;
+}
- log_info("Created new local image %s.", p);
- }
+static void raw_import_file_success(RawImportFile *f) {
+ int r;
+
+ assert(f);
+
+ f->done = true;
+
+ r = raw_import_file_make_local_copy(f);
+ if (r < 0)
+ goto finish;
f->disk_fd = safe_close(f->disk_fd);
r = 0;
goto fail;
}
+ /* Make sure the file size is right, in case the file was
+ * sparse and we just seeked for the last part */
+ if (ftruncate(f->disk_fd, f->written_uncompressed) < 0) {
+ log_error_errno(errno, "Failed to truncate file: %m");
+ r = -errno;
+ goto fail;
+ }
+
r = raw_import_maybe_convert_qcow2(f);
if (r < 0)
goto fail;
return -EFBIG;
}
- n = write(f->disk_fd, p, sz);
+ n = sparse_write(f->disk_fd, p, sz, 64);
if (n < 0) {
log_error_errno(errno, "Failed to write file: %m");
return -errno;
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, "\""))
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;
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)