Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle compressed empty files correctly #8

Merged
merged 1 commit into from May 16, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -44,6 +44,10 @@ private string readArchiveData (archive *ar, string name = null)
char[BUFFER_SIZE] buff;

ret = archive_read_next_header (ar, &ae);

if (ret == ARCHIVE_EOF)
return data;

if (ret != ARCHIVE_OK) {
if (name is null)
throw new Exception (format ("Unable to read header of compressed data."));
@@ -77,6 +81,7 @@ string decompressFile (string fname)
scope(exit) archive_read_free (ar);

archive_read_support_format_raw (ar);
archive_read_support_format_empty (ar);
archive_read_support_filter_all (ar);

ret = archive_read_open_filename (ar, toStringz (fname), 16384);
@@ -94,6 +99,7 @@ string decompressData (ubyte[] data)
scope(exit) archive_read_free (ar);

archive_read_support_filter_all (ar);
archive_read_support_format_empty (ar);
archive_read_support_format_raw (ar);

auto dSize = ubyte.sizeof * data.length;
@@ -466,3 +472,16 @@ public:
}

}

unittest
{
writeln ("TEST: ", "Compressed empty file");

ubyte emptyGz [] = [
0x1f, 0x8b, 0x08, 0x08, 0x00, 0x00, 0x00, 0x00,
0x00, 0x03, 0x65, 0x6d, 0x70, 0x74, 0x79, 0x00,
0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00,
];
assert (decompressData (emptyGz) == "");
}
@@ -46,12 +46,7 @@ public:
try {
data = decompressFile (fname);
} catch (Exception e) {
// libarchive fails to detect GZip-compressed zero-byte documents proprly and emits an error.
// We don't want this to be a permanent failure, so we ignore errors in that particular case.
if (fname.endsWith (".gz"))
logWarning (e.msg);
else
throw e;
throw e;
}

content = splitLines (data);
@@ -47,6 +47,7 @@ int archive_read_support_filter_gzip (archive*);
int archive_read_support_filter_lzma (archive*);

int archive_read_support_format_raw (archive*);
int archive_read_support_format_empty (archive*);
int archive_read_support_format_all (archive*);
int archive_read_support_format_ar (archive*);
int archive_read_support_format_gnutar (archive*);