Skip to content

Commit

Permalink
HPCC-32649 Avoid writing any data to an empty compressed file
Browse files Browse the repository at this point in the history
Signed-off-by: Gavin Halliday <[email protected]>
  • Loading branch information
ghalliday committed Sep 23, 2024
1 parent 7513f82 commit 16dcf3b
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 13 deletions.
30 changes: 17 additions & 13 deletions system/jlib/jlzw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2042,7 +2042,7 @@ class CCompressedFile : implements ICompressedFileIO, public CInterface
Owned<IExpander> expander;
MemoryAttr compressedInputBlock;
unsigned compMethod;
offset_t lastFlushPos = (offset_t)-1;
offset_t lastFlushPos = 0;
offset_t nextExpansionPos = (offset_t)-1;
offset_t startBlockPos = (offset_t)-1;
size32_t fullBlockSize = 0;
Expand Down Expand Up @@ -2456,19 +2456,23 @@ class CCompressedFile : implements ICompressedFileIO, public CInterface
throw MakeStringException(-1,"Partial row written at end of file %d of %d",ol,trailer.recordSize);
}
flush();
trailer.datacrc = trailer.crc;
if (setcrc) {
indexbuf.append(sizeof(trailer)-sizeof(trailer.crc),&trailer);
trailer.crc = crc32((const char *)indexbuf.toByteArray(),
indexbuf.length(),trailer.crc);
indexbuf.append(trailer.crc);
}
else {
trailer.datacrc = 0;
trailer.crc = ~0U;
indexbuf.append(sizeof(trailer),&trailer);
//Avoid writing out a header/footer if the file is empty
if (indexbuf.length() != 0)
{
trailer.datacrc = trailer.crc;
if (setcrc) {
indexbuf.append(sizeof(trailer)-sizeof(trailer.crc),&trailer);
trailer.crc = crc32((const char *)indexbuf.toByteArray(),
indexbuf.length(),trailer.crc);
indexbuf.append(trailer.crc);
}
else {
trailer.datacrc = 0;
trailer.crc = ~0U;
indexbuf.append(sizeof(trailer),&trailer);
}
checkedwrite(trailer.indexPos,indexbuf.length(),indexbuf.toByteArray());
}
checkedwrite(trailer.indexPos,indexbuf.length(),indexbuf.toByteArray());
indexbuf.clear();
if (fileio)
fileio->close();
Expand Down
32 changes: 32 additions & 0 deletions testing/regress/ecl/emptycompressed.ecl
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*##############################################################################
HPCC SYSTEMS software Copyright (C) 2012 HPCC Systems®.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
############################################################################## */

import Std.System.Thorlib;
import Std.File AS FileServices;
import Std.Str;
import $.setup;

prefix := setup.Files(false, false).QueryFilePrefix;

filename := prefix+'empty';
r := { string x};

ds := DATASET(0, transform(r, SELF := []));
output(ds,,filename, overwrite,compressed);

iclData0 := DATASET(filename, r, flat);
output(iclData0);
4 changes: 4 additions & 0 deletions testing/regress/ecl/key/emptycompressed.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<Dataset name='Result 1'>
</Dataset>
<Dataset name='Result 2'>
</Dataset>

0 comments on commit 16dcf3b

Please sign in to comment.