]> git.ipfire.org Git - thirdparty/zstd.git/commitdiff
cli: add Ctrl-C support, requested by @mike155 in #854
authorYann Collet <cyan@fb.com>
Sun, 1 Oct 2017 19:10:26 +0000 (12:10 -0700)
committerYann Collet <cyan@fb.com>
Sun, 1 Oct 2017 19:10:26 +0000 (12:10 -0700)
Now, pressing Ctrl-C during compression or decompression
will erase operation artefact (unfinished destination file)
before leaving execution.

programs/fileio.c
programs/zstdcli.c

index 507cc5c6001134ac133f2afd25a1d630f9e81da3..83934beef6aa57e0ce7ff1ace5ad6a0d5301f596 100644 (file)
@@ -140,6 +140,21 @@ static clock_t g_time = 0;
 }   }
 
 
+/*-************************************
+*  Signal (Ctrl-C trapping)
+**************************************/
+#include  <signal.h>
+
+const char* g_artefact = NULL;
+void INThandler(int sig)
+{
+    signal(sig, SIG_IGN);
+    remove(g_artefact);
+    DISPLAY("\n");
+    exit(1);
+}
+
+
 /* ************************************************************
 * Avoid fseek()'s 2GiB barrier with MSVC, MacOS, *BSD, MinGW
 ***************************************************************/
@@ -929,6 +944,14 @@ static int FIO_compressFilename_dstFile(cRess_t ress,
     ress.dstFile = FIO_openDstFile(dstFileName);
     if (ress.dstFile==NULL) return 1;  /* could not open dstFileName */
 
+    if (UTIL_isRegularFile(dstFileName)) {
+        g_artefact = dstFileName;
+        signal(SIGINT, INThandler);
+    } else {
+        g_artefact = NULL;
+    }
+
+
     if (strcmp (srcFileName, stdinmark) && UTIL_getFileStat(srcFileName, &statbuf))
         stat_result = 1;
     result = FIO_compressFilename_srcFile(ress, dstFileName, srcFileName, compressionLevel);
@@ -943,6 +966,9 @@ static int FIO_compressFilename_dstFile(cRess_t ress,
     }
     else if (strcmp (dstFileName, stdoutmark) && stat_result)
         UTIL_setFileStat(dstFileName, &statbuf);
+
+    signal(SIGINT, SIG_DFL);
+
     return result;
 }
 
@@ -1629,6 +1655,13 @@ static int FIO_decompressDstFile(dRess_t ress,
     ress.dstFile = FIO_openDstFile(dstFileName);
     if (ress.dstFile==0) return 1;
 
+    if (UTIL_isRegularFile(dstFileName)) {
+        g_artefact = dstFileName;
+        signal(SIGINT, INThandler);
+    } else {
+        g_artefact = NULL;
+    }
+
     if ( strcmp(srcFileName, stdinmark)
       && UTIL_getFileStat(srcFileName, &statbuf) )
         stat_result = 1;
@@ -1649,6 +1682,9 @@ static int FIO_decompressDstFile(dRess_t ress,
           && stat_result )                   /* file permissions correctly extracted from src */
             UTIL_setFileStat(dstFileName, &statbuf);  /* transfer file permissions from src into dst */
     }
+
+    signal(SIGINT, SIG_DFL);
+
     return result;
 }
 
index 801dc448b1bacea1a50b36ea7ac1be03837a91b3..3f8367341885e68a766d39eeefdfe7711e4664a0 100644 (file)
@@ -406,6 +406,7 @@ int main(int argCount, const char* argv[])
     int cover = 1;
 #endif
 
+
     /* init */
     (void)recursive; (void)cLevelLast;    /* not used when ZSTD_NOBENCH set */
     (void)dictCLevel; (void)dictSelect; (void)dictID;  (void)maxDictSize; /* not used when ZSTD_NODICT set */