Skip to content

Commit

Permalink
Fixed sparse issue with non seekable streams (#105)
Browse files Browse the repository at this point in the history
  • Loading branch information
Cyan4973 committed May 18, 2015
1 parent 60d657a commit 58b5aad
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
5 changes: 4 additions & 1 deletion programs/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ test-all: test test32
test-travis: $(TRAVIS_TARGET)

test-lz4-sparse: lz4 datagen
@echo ---- test sparse file support ----
@echo "\n ---- test sparse file support ----"
./datagen -g5M -P100 > tmpSrc
./lz4 -B4D tmpSrc | ./lz4 -dv --sparse > tmpB4
diff -s tmpSrc tmpB4
Expand All @@ -167,6 +167,9 @@ test-lz4-sparse: lz4 datagen
./datagen -s1 -g1200007 -P100 | diff -s - tmpOdd
ls -ls tmpOdd
@rm tmp*
@echo "\n Compatibility with Console :"
./lz4 COPYING | ./lz4 -d -c
./lz4 COPYING | ./lz4 -d | cat

test-lz4-contentSize: lz4 datagen
@echo ---- test original size support ----
Expand Down
2 changes: 1 addition & 1 deletion programs/lz4cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ int main(int argc, char** argv)
if (multiple_inputs) input_filename = inFileNames[0], output_filename = (const char*)(inFileNames[0]);
if(!input_filename) { input_filename=stdinmark; }

/* Check if input or output are defined as console; trigger an error in this case */
/* Check if input is defined as console; trigger an error in this case */
if (!strcmp(input_filename, stdinmark) && IS_CONSOLE(stdin) ) badusage();

/* Check if benchmark is selected */
Expand Down
18 changes: 12 additions & 6 deletions programs/lz4io.c
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,12 @@ static int LZ4IO_getFiles(const char* input_filename, const char* output_filenam

if ( *pfoutput==0) EXM_THROW(13, "Pb opening %s", output_filename);

if (g_sparseFileSupport)
{
long int ftr = ftell(*pfoutput);
if (ftr==-1) g_sparseFileSupport = 0;
}

return 0;
}

Expand Down Expand Up @@ -639,15 +645,15 @@ static unsigned LZ4IO_fwriteSparse(FILE* file, const void* buffer, size_t buffer
if (!g_sparseFileSupport) /* normal write */
{
size_t sizeCheck = fwrite(buffer, 1, bufferSize, file);
if (sizeCheck != bufferSize) EXM_THROW(68, "Write error : cannot write decoded block");
if (sizeCheck != bufferSize) EXM_THROW(70, "Write error : cannot write decoded block");
return 0;
}

/* avoid int overflow */
if (storedSkips > 1 GB)
{
int seekResult = fseek(file, 1 GB, SEEK_CUR);
if (seekResult != 0) EXM_THROW(68, "1 GB skip error (sparse file support)");
if (seekResult != 0) EXM_THROW(71, "1 GB skip error (sparse file support)");
storedSkips -= 1 GB;
}

Expand All @@ -667,12 +673,12 @@ static unsigned LZ4IO_fwriteSparse(FILE* file, const void* buffer, size_t buffer
{
size_t sizeCheck;
seekResult = fseek(file, storedSkips, SEEK_CUR);
if (seekResult) EXM_THROW(68, "Skip error (sparse file)");
if (seekResult) EXM_THROW(72, "Skip error (sparse file)");
storedSkips = 0;
seg0SizeT -= nb0T;
ptrT += nb0T;
sizeCheck = fwrite(ptrT, sizeT, seg0SizeT, file);
if (sizeCheck != seg0SizeT) EXM_THROW(68, "Write error : cannot write decoded block");
if (sizeCheck != seg0SizeT) EXM_THROW(73, "Write error : cannot write decoded block");
}
ptrT += seg0SizeT;
}
Expand All @@ -689,10 +695,10 @@ static unsigned LZ4IO_fwriteSparse(FILE* file, const void* buffer, size_t buffer
{
size_t sizeCheck;
int seekResult = fseek(file, storedSkips, SEEK_CUR);
if (seekResult) EXM_THROW(68, "Skip error (end of block)");
if (seekResult) EXM_THROW(74, "Skip error (end of block)");
storedSkips = 0;
sizeCheck = fwrite(restPtr, 1, restEnd - restPtr, file);
if (sizeCheck != (size_t)(restEnd - restPtr)) EXM_THROW(68, "Write error : cannot write decoded end of block");
if (sizeCheck != (size_t)(restEnd - restPtr)) EXM_THROW(75, "Write error : cannot write decoded end of block");
}
}

Expand Down

0 comments on commit 58b5aad

Please sign in to comment.