mirror of
https://github.com/facebook/zstd.git
synced 2025-10-18 00:03:50 -04:00
Merge pull request #1559 from shakeelrao/reject-dict
[CLI] ensure dictionary and input file are different
This commit is contained in:
commit
7186a50775
@ -515,29 +515,11 @@ static FILE* FIO_openDstFile(FIO_prefs_t* const prefs, const char* srcFileName,
|
|||||||
return stdout;
|
return stdout;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ensure dst is not the same file as src */
|
/* ensure dst is not the same as src */
|
||||||
if (srcFileName != NULL) {
|
if (srcFileName != NULL && UTIL_isSameFile(srcFileName, dstFileName)) {
|
||||||
#ifdef _MSC_VER
|
DISPLAYLEVEL(1, "zstd: Refusing to open an output file which will overwrite the input file \n");
|
||||||
/* note : Visual does not support file identification by inode.
|
|
||||||
* The following work-around is limited to detecting exact name repetition only,
|
|
||||||
* aka `filename` is considered different from `subdir/../filename` */
|
|
||||||
if (!strcmp(srcFileName, dstFileName)) {
|
|
||||||
DISPLAYLEVEL(1, "zstd: Refusing to open a output file which will overwrite the input file \n");
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
#else
|
|
||||||
stat_t srcStat;
|
|
||||||
stat_t dstStat;
|
|
||||||
if (UTIL_getFileStat(srcFileName, &srcStat)
|
|
||||||
&& UTIL_getFileStat(dstFileName, &dstStat)) {
|
|
||||||
if (srcStat.st_dev == dstStat.st_dev
|
|
||||||
&& srcStat.st_ino == dstStat.st_ino) {
|
|
||||||
DISPLAYLEVEL(1, "zstd: Refusing to open a output file which will overwrite the input file \n");
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
if (prefs->sparseFileSupport == 1) {
|
if (prefs->sparseFileSupport == 1) {
|
||||||
prefs->sparseFileSupport = ZSTD_SPARSE_DEFAULT;
|
prefs->sparseFileSupport = ZSTD_SPARSE_DEFAULT;
|
||||||
@ -628,6 +610,7 @@ typedef struct {
|
|||||||
size_t srcBufferSize;
|
size_t srcBufferSize;
|
||||||
void* dstBuffer;
|
void* dstBuffer;
|
||||||
size_t dstBufferSize;
|
size_t dstBufferSize;
|
||||||
|
const char* dictFileName;
|
||||||
ZSTD_CStream* cctx;
|
ZSTD_CStream* cctx;
|
||||||
} cRess_t;
|
} cRess_t;
|
||||||
|
|
||||||
@ -655,6 +638,7 @@ static cRess_t FIO_createCResources(FIO_prefs_t* const prefs,
|
|||||||
size_t const dictBuffSize = FIO_createDictBuffer(&dictBuffer, dictFileName); /* works with dictFileName==NULL */
|
size_t const dictBuffSize = FIO_createDictBuffer(&dictBuffer, dictFileName); /* works with dictFileName==NULL */
|
||||||
if (dictFileName && (dictBuffer==NULL))
|
if (dictFileName && (dictBuffer==NULL))
|
||||||
EXM_THROW(32, "allocation error : can't create dictBuffer");
|
EXM_THROW(32, "allocation error : can't create dictBuffer");
|
||||||
|
ress.dictFileName = dictFileName;
|
||||||
|
|
||||||
if (prefs->adaptiveMode && !prefs->ldmFlag && !comprParams.windowLog)
|
if (prefs->adaptiveMode && !prefs->ldmFlag && !comprParams.windowLog)
|
||||||
comprParams.windowLog = ADAPT_WINDOWLOG_DEFAULT;
|
comprParams.windowLog = ADAPT_WINDOWLOG_DEFAULT;
|
||||||
@ -1308,12 +1292,18 @@ FIO_compressFilename_srcFile(FIO_prefs_t* const prefs,
|
|||||||
{
|
{
|
||||||
int result;
|
int result;
|
||||||
|
|
||||||
/* File check */
|
/* ensure src is not a directory */
|
||||||
if (UTIL_isDirectory(srcFileName)) {
|
if (UTIL_isDirectory(srcFileName)) {
|
||||||
DISPLAYLEVEL(1, "zstd: %s is a directory -- ignored \n", srcFileName);
|
DISPLAYLEVEL(1, "zstd: %s is a directory -- ignored \n", srcFileName);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* ensure src is not the same as dict (if present) */
|
||||||
|
if (ress.dictFileName != NULL && UTIL_isSameFile(srcFileName, ress.dictFileName)) {
|
||||||
|
DISPLAYLEVEL(1, "zstd: cannot use %s as an input file and dictionary \n", srcFileName);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
ress.srcFile = FIO_openSrcFile(srcFileName);
|
ress.srcFile = FIO_openSrcFile(srcFileName);
|
||||||
if (ress.srcFile == NULL) return 1; /* srcFile could not be opened */
|
if (ress.srcFile == NULL) return 1; /* srcFile could not be opened */
|
||||||
|
|
||||||
|
@ -87,6 +87,23 @@ U32 UTIL_isDirectory(const char* infilename)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int UTIL_isSameFile(const char* file1, const char* file2)
|
||||||
|
{
|
||||||
|
#if defined(_MSC_VER)
|
||||||
|
/* note : Visual does not support file identification by inode.
|
||||||
|
* The following work-around is limited to detecting exact name repetition only,
|
||||||
|
* aka `filename` is considered different from `subdir/../filename` */
|
||||||
|
return !strcmp(file1, file2);
|
||||||
|
#else
|
||||||
|
stat_t file1Stat;
|
||||||
|
stat_t file2Stat;
|
||||||
|
return UTIL_getFileStat(file1, &file1Stat)
|
||||||
|
&& UTIL_getFileStat(file2, &file2Stat)
|
||||||
|
&& (file1Stat.st_dev == file2Stat.st_dev)
|
||||||
|
&& (file1Stat.st_ino == file2Stat.st_ino);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
U32 UTIL_isLink(const char* infilename)
|
U32 UTIL_isLink(const char* infilename)
|
||||||
{
|
{
|
||||||
/* macro guards, as defined in : https://linux.die.net/man/2/lstat */
|
/* macro guards, as defined in : https://linux.die.net/man/2/lstat */
|
||||||
|
@ -174,6 +174,7 @@ 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);
|
||||||
int UTIL_getFileStat(const char* infilename, stat_t* statbuf);
|
int UTIL_getFileStat(const char* infilename, stat_t* statbuf);
|
||||||
|
int UTIL_isSameFile(const char* file1, const char* file2);
|
||||||
|
|
||||||
U32 UTIL_isLink(const char* infilename);
|
U32 UTIL_isLink(const char* infilename);
|
||||||
#define UTIL_FILESIZE_UNKNOWN ((U64)(-1))
|
#define UTIL_FILESIZE_UNKNOWN ((U64)(-1))
|
||||||
|
@ -410,6 +410,8 @@ $ECHO "- Create first dictionary "
|
|||||||
TESTFILE=../programs/zstdcli.c
|
TESTFILE=../programs/zstdcli.c
|
||||||
$ZSTD --train *.c ../programs/*.c -o tmpDict
|
$ZSTD --train *.c ../programs/*.c -o tmpDict
|
||||||
cp $TESTFILE tmp
|
cp $TESTFILE tmp
|
||||||
|
$ECHO "- Test dictionary compression with tmpDict as an input file and dictionary"
|
||||||
|
$ZSTD -f tmpDict -D tmpDict && die "compression error not detected!"
|
||||||
$ECHO "- Dictionary compression roundtrip"
|
$ECHO "- Dictionary compression roundtrip"
|
||||||
$ZSTD -f tmp -D tmpDict
|
$ZSTD -f tmp -D tmpDict
|
||||||
$ZSTD -d tmp.zst -D tmpDict -fo result
|
$ZSTD -d tmp.zst -D tmpDict -fo result
|
||||||
|
Loading…
x
Reference in New Issue
Block a user