cli: add Ctrl-C support, requested by @mike155 in #854
Now, pressing Ctrl-C during compression or decompression will erase operation artefact (unfinished destination file) before leaving execution.
This commit is contained in:
parent
dc404119e5
commit
00fc1ba8dd
@ -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
|
* 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);
|
ress.dstFile = FIO_openDstFile(dstFileName);
|
||||||
if (ress.dstFile==NULL) return 1; /* could not open 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))
|
if (strcmp (srcFileName, stdinmark) && UTIL_getFileStat(srcFileName, &statbuf))
|
||||||
stat_result = 1;
|
stat_result = 1;
|
||||||
result = FIO_compressFilename_srcFile(ress, dstFileName, srcFileName, compressionLevel);
|
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)
|
else if (strcmp (dstFileName, stdoutmark) && stat_result)
|
||||||
UTIL_setFileStat(dstFileName, &statbuf);
|
UTIL_setFileStat(dstFileName, &statbuf);
|
||||||
|
|
||||||
|
signal(SIGINT, SIG_DFL);
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1629,6 +1655,13 @@ static int FIO_decompressDstFile(dRess_t ress,
|
|||||||
ress.dstFile = FIO_openDstFile(dstFileName);
|
ress.dstFile = FIO_openDstFile(dstFileName);
|
||||||
if (ress.dstFile==0) return 1;
|
if (ress.dstFile==0) return 1;
|
||||||
|
|
||||||
|
if (UTIL_isRegularFile(dstFileName)) {
|
||||||
|
g_artefact = dstFileName;
|
||||||
|
signal(SIGINT, INThandler);
|
||||||
|
} else {
|
||||||
|
g_artefact = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
if ( strcmp(srcFileName, stdinmark)
|
if ( strcmp(srcFileName, stdinmark)
|
||||||
&& UTIL_getFileStat(srcFileName, &statbuf) )
|
&& UTIL_getFileStat(srcFileName, &statbuf) )
|
||||||
stat_result = 1;
|
stat_result = 1;
|
||||||
@ -1649,6 +1682,9 @@ static int FIO_decompressDstFile(dRess_t ress,
|
|||||||
&& stat_result ) /* file permissions correctly extracted from src */
|
&& stat_result ) /* file permissions correctly extracted from src */
|
||||||
UTIL_setFileStat(dstFileName, &statbuf); /* transfer file permissions from src into dst */
|
UTIL_setFileStat(dstFileName, &statbuf); /* transfer file permissions from src into dst */
|
||||||
}
|
}
|
||||||
|
|
||||||
|
signal(SIGINT, SIG_DFL);
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -406,6 +406,7 @@ int main(int argCount, const char* argv[])
|
|||||||
int cover = 1;
|
int cover = 1;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
/* init */
|
/* init */
|
||||||
(void)recursive; (void)cLevelLast; /* not used when ZSTD_NOBENCH set */
|
(void)recursive; (void)cLevelLast; /* not used when ZSTD_NOBENCH set */
|
||||||
(void)dictCLevel; (void)dictSelect; (void)dictID; (void)maxDictSize; /* not used when ZSTD_NODICT set */
|
(void)dictCLevel; (void)dictSelect; (void)dictID; (void)maxDictSize; /* not used when ZSTD_NODICT set */
|
||||||
|
Loading…
x
Reference in New Issue
Block a user