]> git.ipfire.org Git - thirdparty/systemd.git/blobdiff - src/import/import-raw.c
tree-wide: check if return value of lseek() and friends is negative
[thirdparty/systemd.git] / src / import / import-raw.c
index 9c2c74527fef57d44792e31f26dfe11e50d8291f..2db3198ba6123fa32c7580ba3e3a9ffe6a15ae9f 100644 (file)
 #include "install-file.h"
 #include "io-util.h"
 #include "machine-pool.h"
-#include "mkdir.h"
+#include "mkdir-label.h"
 #include "path-util.h"
 #include "qcow2-util.h"
 #include "ratelimit.h"
 #include "rm-rf.h"
 #include "string-util.h"
 #include "tmpfile-util.h"
-#include "util.h"
 
 struct RawImport {
         sd_event *event;
@@ -106,8 +105,8 @@ int raw_import_new(
                 return -ENOMEM;
 
         *i = (RawImport) {
-                .input_fd = -1,
-                .output_fd = -1,
+                .input_fd = -EBADF,
+                .output_fd = -EBADF,
                 .on_finished = on_finished,
                 .userdata = userdata,
                 .last_percent = UINT_MAX,
@@ -155,7 +154,7 @@ static void raw_import_report_progress(RawImport *i) {
 }
 
 static int raw_import_maybe_convert_qcow2(RawImport *i) {
-        _cleanup_close_ int converted_fd = -1;
+        _cleanup_close_ int converted_fd = -EBADF;
         _cleanup_(unlink_and_freep) char *t = NULL;
         _cleanup_free_ char *f = NULL;
         int r;
@@ -195,7 +194,7 @@ static int raw_import_maybe_convert_qcow2(RawImport *i) {
 
         unlink_and_free(i->temp_path);
         i->temp_path = TAKE_PTR(t);
-        CLOSE_AND_REPLACE(i->output_fd, converted_fd);
+        close_and_replace(i->output_fd, converted_fd);
 
         return 1;
 }
@@ -235,7 +234,7 @@ static int raw_import_finish(RawImport *i) {
 
                 if (S_ISREG(i->input_stat.st_mode)) {
                         (void) copy_times(i->input_fd, i->output_fd, COPY_CRTIME);
-                        (void) copy_xattr(i->input_fd, i->output_fd, 0);
+                        (void) copy_xattr(i->input_fd, NULL, i->output_fd, NULL, 0);
                 }
         }
 
@@ -304,7 +303,7 @@ static int raw_import_open_disk(RawImport *i) {
                                        "Target file is not a regular file or block device");
 
         if (i->offset != UINT64_MAX) {
-                if (lseek(i->output_fd, i->offset, SEEK_SET) == (off_t) -1)
+                if (lseek(i->output_fd, i->offset, SEEK_SET) < 0)
                         return log_error_errno(errno, "Failed to seek to offset: %m");
         }
 
@@ -322,18 +321,21 @@ static int raw_import_try_reflink(RawImport *i) {
         if (i->compress.type != IMPORT_COMPRESS_UNCOMPRESSED)
                 return 0;
 
+        if (i->offset != UINT64_MAX || i->size_max != UINT64_MAX)
+                return 0;
+
         if (!S_ISREG(i->input_stat.st_mode) || !S_ISREG(i->output_stat.st_mode))
                 return 0;
 
         p = lseek(i->input_fd, 0, SEEK_CUR);
-        if (p == (off_t) -1)
+        if (p < 0)
                 return log_error_errno(errno, "Failed to read file offset of input file: %m");
 
         /* 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;
 
-        r = btrfs_reflink(i->input_fd, i->output_fd);
+        r = reflink(i->input_fd, i->output_fd);
         if (r >= 0)
                 return 1;
 
@@ -342,11 +344,10 @@ static int raw_import_try_reflink(RawImport *i) {
 }
 
 static int raw_import_write(const void *p, size_t sz, void *userdata) {
-        RawImport *i = userdata;
+        RawImport *i = ASSERT_PTR(userdata);
         bool too_much = false;
         int r;
 
-        assert(i);
         assert(p);
         assert(sz > 0);
 
@@ -368,7 +369,7 @@ static int raw_import_write(const void *p, size_t sz, void *userdata) {
         }
 
         /* Generate sparse file if we created/truncated the file */
-        if (S_ISREG(i->output_stat.st_mode)) {
+        if (S_ISREG(i->output_stat.st_mode) && i->offset == UINT64_MAX) {
                 ssize_t n;
 
                 n = sparse_write(i->output_fd, p, sz, 64);
@@ -377,7 +378,7 @@ static int raw_import_write(const void *p, size_t sz, void *userdata) {
                 if ((size_t) n < sz)
                         return log_error_errno(SYNTHETIC_ERRNO(EIO), "Short write");
         } else {
-                r = loop_write(i->output_fd, p, sz, false);
+                r = loop_write(i->output_fd, p, sz);
                 if (r < 0)
                         return log_error_errno(r, "Failed to write file: %m");
         }