]> git.ipfire.org Git - thirdparty/zstd.git/commitdiff
fixed : detection of non-existing file
authorYann Collet <cyan@fb.com>
Thu, 20 Dec 2018 02:30:57 +0000 (18:30 -0800)
committerYann Collet <cyan@fb.com>
Thu, 20 Dec 2018 02:30:57 +0000 (18:30 -0800)
better error message
with test

programs/fileio.c
programs/util.c
programs/util.h
tests/playTests.sh

index d5cd341612b6a2da8e5cbb54af4fd52d5ca56284..ee9d060b05ba22cdb3892c0e6cfc6584a975215a 100644 (file)
@@ -388,6 +388,12 @@ static FILE* FIO_openSrcFile(const char* srcFileName)
         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)) {
         DISPLAYLEVEL(1, "zstd: %s is not a regular file -- ignored \n",
                         srcFileName);
@@ -803,26 +809,28 @@ FIO_compressLz4Frame(cRess_t* ress,
 
         /* Main Loop */
         while (readSize>0) {
-            size_t outSize;
-
-            /* Compress Block */
-            outSize = LZ4F_compressUpdate(ctx, ress->dstBuffer, ress->dstBufferSize, ress->srcBuffer, readSize, NULL);
+            size_t const outSize = LZ4F_compressUpdate(ctx,
+                                        ress->dstBuffer, ress->dstBufferSize,
+                                        ress->srcBuffer, readSize, NULL);
             if (LZ4F_isError(outSize))
                 EXM_THROW(35, "zstd: %s: lz4 compression failed : %s",
                             srcFileName, LZ4F_getErrorName(outSize));
             outFileSize += outSize;
-            if (srcFileSize == UTIL_FILESIZE_UNKNOWN)
+            if (srcFileSize == UTIL_FILESIZE_UNKNOWN) {
                 DISPLAYUPDATE(2, "\rRead : %u MB ==> %.2f%%",
                                 (U32)(inFileSize>>20),
                                 (double)outFileSize/inFileSize*100)
-            else
+            } else {
                 DISPLAYUPDATE(2, "\rRead : %u / %u MB ==> %.2f%%",
                                 (U32)(inFileSize>>20), (U32)(srcFileSize>>20),
                                 (double)outFileSize/inFileSize*100);
+            }
 
             /* Write Block */
-            { size_t const sizeCheck = fwrite(ress->dstBuffer, 1, outSize, ress->dstFile);
-              if (sizeCheck!=outSize) EXM_THROW(36, "Write error : %s", strerror(errno)); }
+            {   size_t const sizeCheck = fwrite(ress->dstBuffer, 1, outSize, ress->dstFile);
+                if (sizeCheck != outSize)
+                    EXM_THROW(36, "Write error : %s", strerror(errno));
+            }
 
             /* Read next block */
             readSize  = fread(ress->srcBuffer, (size_t)1, (size_t)blockSize, ress->srcFile);
@@ -837,7 +845,7 @@ FIO_compressLz4Frame(cRess_t* ress,
                         srcFileName, LZ4F_getErrorName(headerSize));
 
         {   size_t const sizeCheck = fwrite(ress->dstBuffer, 1, headerSize, ress->dstFile);
-            if (sizeCheck!=headerSize)
+            if (sizeCheck != headerSize)
                 EXM_THROW(39, "Write error : %s (cannot write end of stream)",
                             strerror(errno));
         }
index 5c5e196067d14ace99ff652017c6c95976c6ea74..c8140b7ad286a726a937826761d67b0316f42d6e 100644 (file)
@@ -16,8 +16,18 @@ extern "C" {
 /*-****************************************
 *  Dependencies
 ******************************************/
+#include <string.h>       /* strncmp */
+#include <errno.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)
 {
     stat_t statbuf;
@@ -651,4 +661,3 @@ int UTIL_countPhysicalCores(void)
 #if defined (__cplusplus)
 }
 #endif
-
index d61ff526b7eb310370b92d01ddf1250b66ca86dd..b97c417404d4c15cfef2c7e3b171f6ba44378ab3 100644 (file)
@@ -20,10 +20,9 @@ extern "C" {
 *  Dependencies
 ******************************************/
 #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 <stdio.h>        /* fprintf */
-#include <string.h>       /* strncmp */
 #include <sys/types.h>    /* stat, utime */
 #include <sys/stat.h>     /* stat, chmod */
 #if defined(_MSC_VER)
@@ -34,7 +33,6 @@ extern "C" {
 #  include <utime.h>      /* utime */
 #endif
 #include <time.h>         /* clock_t, clock, CLOCKS_PER_SEC, nanosleep */
-#include <errno.h>
 #include "mem.h"          /* U32, U64 */
 
 
@@ -163,10 +161,11 @@ void UTIL_waitForNextTick(void);
 #endif
 
 
+int UTIL_fileExist(const char* filename);
 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);
-int UTIL_getFileStat(const char* infilename, stat_t *statbuf);
+int UTIL_getFileStat(const char* infilename, stat_tstatbuf);
 
 U32 UTIL_isLink(const char* infilename);
 #define UTIL_FILESIZE_UNKNOWN  ((U64)(-1))
@@ -178,7 +177,7 @@ U64 UTIL_getTotalFileSize(const char* const * const fileNamesTable, unsigned nbF
  * A modified version of realloc().
  * If UTIL_realloc() fails the original block is freed.
 */
-UTIL_STATIC void *UTIL_realloc(void *ptr, size_t size)
+UTIL_STATIC voidUTIL_realloc(void *ptr, size_t size)
 {
     void *newptr = realloc(ptr, size);
     if (newptr) return newptr;
@@ -186,7 +185,7 @@ UTIL_STATIC void *UTIL_realloc(void *ptr, size_t size)
     return NULL;
 }
 
-int UTIL_prepareFileList(const char *dirName, char** bufStart, size_t* pos, char** bufEnd, int followLinks);
+int UTIL_prepareFileList(const chardirName, char** bufStart, size_t* pos, char** bufEnd, int followLinks);
 #ifdef _WIN32
 #  define UTIL_HAS_CREATEFILELIST
 #elif defined(__linux__) || (PLATFORM_POSIX_VERSION >= 200112L)  /* opendir, readdir require POSIX.1-2001 */
index b861391eb8e64ae0aee0c2592dec2d56d3ce6129..a86476abb8e5ce1e57a208ff69c84e8827ced550 100755 (executable)
@@ -186,7 +186,8 @@ rm -f tmpro tmpro.zst
 $ECHO "test: overwrite input file (must fail)"
 $ZSTD tmp -fo tmp && 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"
 $ZSTD -f --rm tmp