better error handling

This commit is contained in:
Yann Collet 2016-09-09 19:33:56 +02:00
parent 746b51de51
commit a2664649df
8 changed files with 21 additions and 17 deletions

View File

@ -44,7 +44,7 @@ clean:
@$(MAKE) -C $(PRGDIR) $@ > $(VOID)
@$(MAKE) -C $(TESTDIR) $@ > $(VOID)
@$(MAKE) -C $(ZWRAPDIR) $@ > $(VOID)
@rm -f zstd
@$(RM) zstd
@echo Cleaning completed
@ -121,7 +121,7 @@ endif
ifneq (,$(filter $(HOST_OS),MSYS POSIX))
cmaketest:
cmake --version
rm -rf projects/cmake/build
$(RM) -r projects/cmake/build
mkdir projects/cmake/build
cd projects/cmake/build ; cmake -DPREFIX:STRING=~/install_test_dir $(CMAKE_PARAMS) .. ; $(MAKE) install ; $(MAKE) uninstall

View File

@ -61,6 +61,7 @@ clean:
test: all
cp README.md tmp
cp Makefile tmp2
@echo starting simple compression
./simple_compression tmp
./simple_decompression tmp.zst
@ -69,6 +70,6 @@ test: all
./streaming_compression tmp
./streaming_decompression tmp.zst > /dev/null
@echo starting dictionary compression
./dictionary_compression tmp README.md
./dictionary_decompression tmp.zst README.md
./dictionary_compression tmp2 tmp README.md
./dictionary_decompression tmp2.zst tmp.zst README.md
@echo tests completed

View File

@ -73,12 +73,12 @@ static void saveFile_orDie(const char* fileName, const void* buff, size_t buffSi
/* createDict() :
`dictFileName` is supposed to have been created using `zstd --train` */
static ZSTD_CDict* createCDict_orDie(const char* dictFileName)
static ZSTD_CDict* createCDict_orDie(const char* dictFileName, int cLevel)
{
size_t dictSize;
printf("loading dictionary %s \n", dictFileName);
void* const dictBuffer = loadFile_orDie(dictFileName, &dictSize);
ZSTD_CDict* const cdict = ZSTD_createCDict(dictBuffer, dictSize, 3);
ZSTD_CDict* const cdict = ZSTD_createCDict(dictBuffer, dictSize, cLevel);
if (!cdict) {
fprintf(stderr, "ZSTD_createCDict error \n");
exit(7);
@ -96,6 +96,7 @@ static void compress(const char* fname, const char* oname, const ZSTD_CDict* cdi
void* const cBuff = malloc_orDie(cBuffSize);
ZSTD_CCtx* const cctx = ZSTD_createCCtx();
if (cctx==NULL) { fprintf(stderr, "ZSTD_createCCtx() error \n"); exit(10); }
size_t const cSize = ZSTD_compress_usingCDict(cctx, cBuff, cBuffSize, fBuff, fSize, cdict);
if (ZSTD_isError(cSize)) {
fprintf(stderr, "error compressing %s : %s \n", fname, ZSTD_getErrorName(cSize));
@ -107,7 +108,7 @@ static void compress(const char* fname, const char* oname, const ZSTD_CDict* cdi
/* success */
printf("%25s : %6u -> %7u - %s \n", fname, (unsigned)fSize, (unsigned)cSize, oname);
ZSTD_freeCCtx(cctx);
ZSTD_freeCCtx(cctx); /* never fails */
free(fBuff);
free(cBuff);
}
@ -127,6 +128,7 @@ static char* createOutFilename_orDie(const char* filename)
int main(int argc, const char** argv)
{
const char* const exeName = argv[0];
int const cLevel = 3;
if (argc<3) {
fprintf(stderr, "wrong arguments\n");
@ -137,7 +139,7 @@ int main(int argc, const char** argv)
/* load dictionary only once */
const char* const dictName = argv[argc-1];
ZSTD_CDict* const dictPtr = createCDict_orDie(dictName);
ZSTD_CDict* const dictPtr = createCDict_orDie(dictName, cLevel);
int u;
for (u=1; u<argc-1; u++) {

View File

@ -81,11 +81,11 @@ static void decompress(const char* fname, const ZSTD_DDict* ddict)
fprintf(stderr, "%s : original size unknown \n", fname);
exit(6);
}
void* const rBuff = malloc_orDie(rSize);
void* const rBuff = malloc_orDie((size_t)rSize);
ZSTD_DCtx* const dctx = ZSTD_createDCtx();
if (dctx==NULL) { fprintf(stderr, "ZSTD_createDCtx() error \n"); exit(10); }
size_t const dSize = ZSTD_decompress_usingDDict(dctx, rBuff, rSize, cBuff, cSize, ddict);
if (dSize != rSize) {
fprintf(stderr, "error decoding %s : %s \n", fname, ZSTD_getErrorName(dSize));
exit(7);

View File

@ -53,7 +53,7 @@ static void* loadFile_orDie(const char* fileName, size_t* size)
fprintf(stderr, "fread: %s : %s \n", fileName, strerror(errno));
exit(4);
}
fclose(inFile);
fclose(inFile); /* can't fail, read only */
*size = buffSize;
return buffer;
}
@ -101,7 +101,7 @@ static const char* createOutFilename_orDie(const char* filename)
{
size_t const inL = strlen(filename);
size_t const outL = inL + 5;
void* outSpace = malloc_orDie(outL);
void* const outSpace = malloc_orDie(outL);
memset(outSpace, 0, outL);
strcat(outSpace, filename);
strcat(outSpace, ".zst");

View File

@ -53,7 +53,7 @@ static void* loadFile_X(const char* fileName, size_t* size)
printf("fread: %s : %s \n", fileName, strerror(errno));
exit(4);
}
fclose(inFile);
fclose(inFile); /* can't fail (read only) */
*size = buffSize;
return buffer;
}
@ -68,7 +68,7 @@ static void decompress(const char* fname)
printf("%s : original size unknown \n", fname);
exit(5);
}
void* const rBuff = malloc_X(rSize);
void* const rBuff = malloc_X((size_t)rSize);
size_t const dSize = ZSTD_decompress(rBuff, rSize, cBuff, cSize);

View File

@ -69,13 +69,13 @@ static void compressFile_orDie(const char* fname, const char* outName, int cLeve
void* const buffIn = malloc_orDie(buffInSize);
size_t const buffOutSize = ZSTD_CStreamOutSize();;
void* const buffOut = malloc_orDie(buffOutSize);
size_t read, toRead = buffInSize;
ZSTD_CStream* const cstream = ZSTD_createCStream();
if (cstream==NULL) { fprintf(stderr, "ZSTD_createCStream() error \n"); exit(10); }
size_t const initResult = ZSTD_initCStream(cstream, cLevel);
if (ZSTD_isError(initResult)) { fprintf(stderr, "ZSTD_initCStream() error \n"); exit(11); }
size_t read, toRead = buffInSize;
while( (read = fread_orDie(buffIn, toRead, fin)) ) {
ZSTD_inBuffer input = { buffIn, read, 0 };
while (input.pos < input.size) {
@ -86,10 +86,11 @@ static void compressFile_orDie(const char* fname, const char* outName, int cLeve
}
ZSTD_outBuffer output = { buffOut, buffOutSize, 0 };
size_t const remainingToFlush = ZSTD_endStream(cstream, &output);
size_t const remainingToFlush = ZSTD_endStream(cstream, &output); /* close frame */
if (remainingToFlush) { fprintf(stderr, "not fully flushed"); exit(12); }
fwrite_orDie(buffOut, output.pos, fout);
ZSTD_freeCStream(cstream);
fclose_orDie(fout);
fclose_orDie(fin);
free(buffIn);

View File

@ -51,7 +51,6 @@ static size_t fwrite_orDie(const void* buffer, size_t sizeToWrite, FILE* file)
exit(5);
}
static size_t fclose_orDie(FILE* file)
{
if (!fclose(file)) return 0;
@ -85,6 +84,7 @@ static void decompressFile_orDie(const char* fname)
}
}
ZSTD_freeDStream(dstream);
fclose_orDie(fin);
fclose_orDie(fout);
free(buffIn);