chiark / gitweb /
import-raw: when downloading raw images, generate sparse files if we can
[elogind.git] / src / import / import-raw.c
index c15765d51c0e919abb11064096921c6fb99da803..c82d263787f996ed0a508c5b011fd9534077b84f 100644 (file)
@@ -205,7 +205,7 @@ static void raw_import_file_success(RawImportFile *f) {
                  * 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);
+                        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) {
@@ -332,6 +332,14 @@ static void raw_import_curl_on_finished(CurlGlue *g, CURL *curl, CURLcode result
                 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;
@@ -402,6 +410,10 @@ static int raw_import_file_open_disk_for_write(RawImportFile *f) {
         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;
 }
 
@@ -423,7 +435,7 @@ static int raw_import_file_write_uncompressed(RawImportFile *f, void *p, size_t
                 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;