]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
import: make sure we can import empty files
authorLennart Poettering <lennart@poettering.net>
Thu, 28 Jan 2021 17:20:11 +0000 (18:20 +0100)
committerLennart Poettering <lennart@poettering.net>
Fri, 29 Jan 2021 15:40:20 +0000 (16:40 +0100)
src/import/import-compress.c
src/import/import-compress.h
src/import/import-raw.c
src/import/import-tar.c

index c016587e14fe4b695cfeb22c81ecc0f087ce77c8..aa837af5659b61d0b29a47b4eb43454627ccfbf4 100644 (file)
@@ -83,6 +83,13 @@ int import_uncompress_detect(ImportCompress *c, const void *data, size_t size) {
         return 1;
 }
 
+void import_uncompress_force_off(ImportCompress *c) {
+        assert(c);
+
+        c->type = IMPORT_COMPRESS_UNCOMPRESSED;
+        c->encoding = false;
+}
+
 int import_uncompress(ImportCompress *c, const void *data, size_t size, ImportCompressCallback callback, void *userdata) {
         int r;
 
index e40f4dba0a78959bad1782be4afb9070bbd7db9a..3c35b59104d2bde5c20c42b81fb89edb3779c7c7 100644 (file)
@@ -37,6 +37,7 @@ typedef int (*ImportCompressCallback)(const void *data, size_t size, void *userd
 void import_compress_free(ImportCompress *c);
 
 int import_uncompress_detect(ImportCompress *c, const void *data, size_t size);
+void import_uncompress_force_off(ImportCompress *c);
 int import_uncompress(ImportCompress *c, const void *data, size_t size, ImportCompressCallback callback, void *userdata);
 
 int import_compress_init(ImportCompress *c, ImportCompressType t);
index 3b7031daf6ff05e68e5d814fa310945941c514ce..4eb59fbb25c09126a72f5ba83103e0c4fd2eccf0 100644 (file)
@@ -313,27 +313,23 @@ static int raw_import_process(RawImport *i) {
                 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.");
-                        r = -EIO;
-                        goto finish;
-                }
-
-                r = raw_import_finish(i);
-                goto finish;
-        }
 
         i->buffer_size += l;
 
         if (i->compress.type == IMPORT_COMPRESS_UNKNOWN) {
-                r = import_uncompress_detect(&i->compress, i->buffer, i->buffer_size);
-                if (r < 0) {
-                        log_error_errno(r, "Failed to detect file compression: %m");
-                        goto finish;
+
+                if (l == 0) { /* EOF */
+                        log_debug("File too short to be compressed, as no compression signature fits in, thus assuming uncompressed.");
+                        import_uncompress_force_off(&i->compress);
+                } else {
+                        r = import_uncompress_detect(&i->compress, i->buffer, i->buffer_size);
+                        if (r < 0) {
+                                log_error_errno(r, "Failed to detect file compression: %m");
+                                goto finish;
+                        }
+                        if (r == 0) /* Need more data */
+                                return 0;
                 }
-                if (r == 0) /* Need more data */
-                        return 0;
 
                 r = raw_import_open_disk(i);
                 if (r < 0)
@@ -342,10 +338,8 @@ static int raw_import_process(RawImport *i) {
                 r = raw_import_try_reflink(i);
                 if (r < 0)
                         goto finish;
-                if (r > 0) {
-                        r = raw_import_finish(i);
-                        goto finish;
-                }
+                if (r > 0)
+                        goto complete;
         }
 
         r = import_uncompress(&i->compress, i->buffer, i->buffer_size, raw_import_write, i);
@@ -357,10 +351,16 @@ static int raw_import_process(RawImport *i) {
         i->written_compressed += i->buffer_size;
         i->buffer_size = 0;
 
+        if (l == 0) /* EOF */
+                goto complete;
+
         raw_import_report_progress(i);
 
         return 0;
 
+complete:
+        r = raw_import_finish(i);
+
 finish:
         if (i->on_finished)
                 i->on_finished(i, r, i->userdata);
index c772493ece8c5f225421de78e44e715c0b7fdef6..e94474389d5c57bc3c548f33ffdf4286de3e4d2e 100644 (file)
@@ -259,27 +259,23 @@ static int tar_import_process(TarImport *i) {
                 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.");
-                        r = -EIO;
-                        goto finish;
-                }
-
-                r = tar_import_finish(i);
-                goto finish;
-        }
 
         i->buffer_size += l;
 
         if (i->compress.type == IMPORT_COMPRESS_UNKNOWN) {
-                r = import_uncompress_detect(&i->compress, i->buffer, i->buffer_size);
-                if (r < 0) {
-                        log_error_errno(r, "Failed to detect file compression: %m");
-                        goto finish;
+
+                if (l == 0) { /* EOF */
+                        log_debug("File too short to be compressed, as no compression signature fits in, thus assuming uncompressed.");
+                        import_uncompress_force_off(&i->compress);
+                } else {
+                        r = import_uncompress_detect(&i->compress, i->buffer, i->buffer_size);
+                        if (r < 0) {
+                                log_error_errno(r, "Failed to detect file compression: %m");
+                                goto finish;
+                        }
+                        if (r == 0) /* Need more data */
+                                return 0;
                 }
-                if (r == 0) /* Need more data */
-                        return 0;
 
                 r = tar_import_fork_tar(i);
                 if (r < 0)
@@ -295,6 +291,11 @@ static int tar_import_process(TarImport *i) {
         i->written_compressed += i->buffer_size;
         i->buffer_size = 0;
 
+        if (l == 0) { /* EOF */
+                r = tar_import_finish(i);
+                goto finish;
+        }
+
         tar_import_report_progress(i);
 
         return 0;