fixed : detection of non-existing file

better error message
with test
dev
Yann Collet 2018-12-19 18:30:57 -08:00
parent 0f2d443e10
commit 173ef9dea2
4 changed files with 35 additions and 18 deletions

View File

@ -388,6 +388,12 @@ static FILE* FIO_openSrcFile(const char* srcFileName)
return stdin; return stdin;
} }
if (!UTIL_fileExist(srcFileName)) {
DISPLAYLEVEL(1, "zstd: %s : No such file or directory (can't stat) -- ignored \n",
srcFileName);
return NULL;
}
if (!UTIL_isRegularFile(srcFileName)) { if (!UTIL_isRegularFile(srcFileName)) {
DISPLAYLEVEL(1, "zstd: %s is not a regular file -- ignored \n", DISPLAYLEVEL(1, "zstd: %s is not a regular file -- ignored \n",
srcFileName); srcFileName);
@ -803,26 +809,28 @@ FIO_compressLz4Frame(cRess_t* ress,
/* Main Loop */ /* Main Loop */
while (readSize>0) { while (readSize>0) {
size_t outSize; size_t const outSize = LZ4F_compressUpdate(ctx,
ress->dstBuffer, ress->dstBufferSize,
/* Compress Block */ ress->srcBuffer, readSize, NULL);
outSize = LZ4F_compressUpdate(ctx, ress->dstBuffer, ress->dstBufferSize, ress->srcBuffer, readSize, NULL);
if (LZ4F_isError(outSize)) if (LZ4F_isError(outSize))
EXM_THROW(35, "zstd: %s: lz4 compression failed : %s", EXM_THROW(35, "zstd: %s: lz4 compression failed : %s",
srcFileName, LZ4F_getErrorName(outSize)); srcFileName, LZ4F_getErrorName(outSize));
outFileSize += outSize; outFileSize += outSize;
if (srcFileSize == UTIL_FILESIZE_UNKNOWN) if (srcFileSize == UTIL_FILESIZE_UNKNOWN) {
DISPLAYUPDATE(2, "\rRead : %u MB ==> %.2f%%", DISPLAYUPDATE(2, "\rRead : %u MB ==> %.2f%%",
(U32)(inFileSize>>20), (U32)(inFileSize>>20),
(double)outFileSize/inFileSize*100) (double)outFileSize/inFileSize*100)
else } else {
DISPLAYUPDATE(2, "\rRead : %u / %u MB ==> %.2f%%", DISPLAYUPDATE(2, "\rRead : %u / %u MB ==> %.2f%%",
(U32)(inFileSize>>20), (U32)(srcFileSize>>20), (U32)(inFileSize>>20), (U32)(srcFileSize>>20),
(double)outFileSize/inFileSize*100); (double)outFileSize/inFileSize*100);
}
/* Write Block */ /* Write Block */
{ size_t const sizeCheck = fwrite(ress->dstBuffer, 1, outSize, ress->dstFile); { size_t const sizeCheck = fwrite(ress->dstBuffer, 1, outSize, ress->dstFile);
if (sizeCheck!=outSize) EXM_THROW(36, "Write error : %s", strerror(errno)); } if (sizeCheck != outSize)
EXM_THROW(36, "Write error : %s", strerror(errno));
}
/* Read next block */ /* Read next block */
readSize = fread(ress->srcBuffer, (size_t)1, (size_t)blockSize, ress->srcFile); readSize = fread(ress->srcBuffer, (size_t)1, (size_t)blockSize, ress->srcFile);

View File

@ -16,8 +16,18 @@ extern "C" {
/*-**************************************** /*-****************************************
* Dependencies * Dependencies
******************************************/ ******************************************/
#include <string.h> /* strncmp */
#include <errno.h>
#include "util.h" #include "util.h"
int UTIL_fileExist(const char* filename)
{
stat_t statbuf;
int const stat_success = stat(filename, &statbuf);
return !stat_success;
}
int UTIL_isRegularFile(const char* infilename) int UTIL_isRegularFile(const char* infilename)
{ {
stat_t statbuf; stat_t statbuf;
@ -651,4 +661,3 @@ int UTIL_countPhysicalCores(void)
#if defined (__cplusplus) #if defined (__cplusplus)
} }
#endif #endif

View File

@ -20,10 +20,9 @@ extern "C" {
* Dependencies * Dependencies
******************************************/ ******************************************/
#include "platform.h" /* PLATFORM_POSIX_VERSION, ZSTD_NANOSLEEP_SUPPORT, ZSTD_SETPRIORITY_SUPPORT */ #include "platform.h" /* PLATFORM_POSIX_VERSION, ZSTD_NANOSLEEP_SUPPORT, ZSTD_SETPRIORITY_SUPPORT */
#include <stdlib.h> /* malloc */ #include <stdlib.h> /* malloc, realloc, free */
#include <stddef.h> /* size_t, ptrdiff_t */ #include <stddef.h> /* size_t, ptrdiff_t */
#include <stdio.h> /* fprintf */ #include <stdio.h> /* fprintf */
#include <string.h> /* strncmp */
#include <sys/types.h> /* stat, utime */ #include <sys/types.h> /* stat, utime */
#include <sys/stat.h> /* stat, chmod */ #include <sys/stat.h> /* stat, chmod */
#if defined(_MSC_VER) #if defined(_MSC_VER)
@ -34,7 +33,6 @@ extern "C" {
# include <utime.h> /* utime */ # include <utime.h> /* utime */
#endif #endif
#include <time.h> /* clock_t, clock, CLOCKS_PER_SEC, nanosleep */ #include <time.h> /* clock_t, clock, CLOCKS_PER_SEC, nanosleep */
#include <errno.h>
#include "mem.h" /* U32, U64 */ #include "mem.h" /* U32, U64 */
@ -163,6 +161,7 @@ void UTIL_waitForNextTick(void);
#endif #endif
int UTIL_fileExist(const char* filename);
int UTIL_isRegularFile(const char* infilename); int UTIL_isRegularFile(const char* infilename);
int UTIL_setFileStat(const char* filename, stat_t* statbuf); int UTIL_setFileStat(const char* filename, stat_t* statbuf);
U32 UTIL_isDirectory(const char* infilename); U32 UTIL_isDirectory(const char* infilename);

View File

@ -186,7 +186,8 @@ rm -f tmpro tmpro.zst
$ECHO "test: overwrite input file (must fail)" $ECHO "test: overwrite input file (must fail)"
$ZSTD tmp -fo tmp && die "zstd overwrote the input file" $ZSTD tmp -fo tmp && die "zstd overwrote the input file"
$ZSTD tmp.zst -dfo tmp.zst && die "zstd overwrote the input file" $ZSTD tmp.zst -dfo tmp.zst && die "zstd overwrote the input file"
$ECHO "test: properly detect input file does not exist"
$ZSTD nothere 2>&1 | grep "o such file"
$ECHO "test : file removal" $ECHO "test : file removal"
$ZSTD -f --rm tmp $ZSTD -f --rm tmp