summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2021-01-28 18:20:11 +0100
committerLennart Poettering <lennart@poettering.net>2021-01-29 16:40:20 +0100
commitc9b6ebef8c4ed28edc930bcb77906cee74c0cb34 (patch)
tree6ff3b9dcfa4996249bb0177ecdb713554552a8a6
parent9f0b5640bd316cc23234370abd3a8b2db6e709dd (diff)
downloadsystemd-c9b6ebef8c4ed28edc930bcb77906cee74c0cb34.tar.gz
import: make sure we can import empty files
-rw-r--r--src/import/import-compress.c7
-rw-r--r--src/import/import-compress.h1
-rw-r--r--src/import/import-raw.c40
-rw-r--r--src/import/import-tar.c33
4 files changed, 45 insertions, 36 deletions
diff --git a/src/import/import-compress.c b/src/import/import-compress.c
index c016587e14..aa837af565 100644
--- a/src/import/import-compress.c
+++ b/src/import/import-compress.c
@@ -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;
diff --git a/src/import/import-compress.h b/src/import/import-compress.h
index e40f4dba0a..3c35b59104 100644
--- a/src/import/import-compress.h
+++ b/src/import/import-compress.h
@@ -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);
diff --git a/src/import/import-raw.c b/src/import/import-raw.c
index 3b7031daf6..4eb59fbb25 100644
--- a/src/import/import-raw.c
+++ b/src/import/import-raw.c
@@ -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);
diff --git a/src/import/import-tar.c b/src/import/import-tar.c
index c772493ece..e94474389d 100644
--- a/src/import/import-tar.c
+++ b/src/import/import-tar.c
@@ -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;