From 142b4e2ee8f7a4c83e24c906f7ec1f120dc173ea Mon Sep 17 00:00:00 2001 From: Neil Mayhew Date: Mon, 16 May 2016 15:09:13 -0600 Subject: [PATCH] Handle compressed empty files correctly This reverts some of 63584a0 and fixes #1 --- source/archive.d | 19 +++++++++++++++++++ source/backends/debian/tagfile.d | 7 +------ source/bindings/libarchive.d | 1 + 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/source/archive.d b/source/archive.d index 6c7c20e6..579b2e51 100644 --- a/source/archive.d +++ b/source/archive.d @@ -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) == ""); +} diff --git a/source/backends/debian/tagfile.d b/source/backends/debian/tagfile.d index 37acbb73..7795ad22 100644 --- a/source/backends/debian/tagfile.d +++ b/source/backends/debian/tagfile.d @@ -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); diff --git a/source/bindings/libarchive.d b/source/bindings/libarchive.d index cf427cbf..ce8d919f 100644 --- a/source/bindings/libarchive.d +++ b/source/bindings/libarchive.d @@ -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*);