summaryrefslogtreecommitdiffstats
path: root/programs
diff options
context:
space:
mode:
authorYann Collet <yann.collet.73@gmail.com>2023-02-26 18:23:05 (GMT)
committerYann Collet <yann.collet.73@gmail.com>2023-02-26 18:40:28 (GMT)
commit45a4880b9cbb47ddc6c1758744629b5835d5dd19 (patch)
treec9ae72bd0d6a6864f061db33cab3848c92337600 /programs
parentecf92d0897587c0f641df9db83c910fd236cb18a (diff)
downloadlz4-45a4880b9cbb47ddc6c1758744629b5835d5dd19.zip
lz4-45a4880b9cbb47ddc6c1758744629b5835d5dd19.tar.gz
lz4-45a4880b9cbb47ddc6c1758744629b5835d5dd19.tar.bz2
refuse to compress directories
fix #1211, reported by @imba-tjd
Diffstat (limited to 'programs')
-rw-r--r--programs/lz4io.c26
-rw-r--r--programs/util.h20
2 files changed, 34 insertions, 12 deletions
diff --git a/programs/lz4io.c b/programs/lz4io.c
index 8b70b91..09d906e 100644
--- a/programs/lz4io.c
+++ b/programs/lz4io.c
@@ -325,11 +325,16 @@ static FILE* LZ4IO_openSrcFile(const char* srcFileName)
DISPLAYLEVEL(4,"Using stdin for input \n");
f = stdin;
SET_BINARY_MODE(stdin);
- } else {
- f = fopen(srcFileName, "rb");
- if (f==NULL) DISPLAYLEVEL(1, "%s: %s \n", srcFileName, strerror(errno));
+ return f;
+ }
+
+ if (UTIL_isDir(srcFileName)) {
+ DISPLAYLEVEL(1, "lz4: %s is a directory -- ignored \n", srcFileName);
+ return NULL;
}
+ f = fopen(srcFileName, "rb");
+ if (f==NULL) DISPLAYLEVEL(1, "%s: %s \n", srcFileName, strerror(errno));
return f;
}
@@ -337,7 +342,8 @@ static FILE* LZ4IO_openSrcFile(const char* srcFileName)
* prefs is writable, because sparseFileSupport might be updated.
* condition : `dstFileName` must be non-NULL.
* @result : FILE* to `dstFileName`, or NULL if it fails */
-static FILE* LZ4IO_openDstFile(const char* dstFileName, const LZ4IO_prefs_t* const prefs)
+static FILE*
+LZ4IO_openDstFile(const char* dstFileName, const LZ4IO_prefs_t* const prefs)
{
FILE* f;
assert(dstFileName != NULL);
@@ -563,11 +569,14 @@ static void* LZ4IO_createDict(size_t* dictSize, const char* const dictFilename)
char* dictBuf;
FILE* dictFile;
- if (!circularBuf) END_PROCESS(25, "Allocation error : not enough memory for circular buffer");
- if (!dictFilename) END_PROCESS(26, "Dictionary error : no filename provided");
+ if (!dictFilename)
+ END_PROCESS(26, "Dictionary error : no filename provided");
+ if (!circularBuf)
+ END_PROCESS(25, "Allocation error : not enough memory for circular buffer");
dictFile = LZ4IO_openSrcFile(dictFilename);
- if (!dictFile) END_PROCESS(27, "Dictionary error : could not open dictionary file");
+ if (!dictFile)
+ END_PROCESS(27, "Dictionary error : could not open dictionary file");
/* opportunistically seek to the part of the file we care about.
* If this fails it's not a problem since we'll just read everything anyways. */
@@ -1311,7 +1320,8 @@ LZ4IO_decompressSrcFile(dRess_t ress,
static int
LZ4IO_decompressDstFile(dRess_t ress,
- const char* input_filename, const char* output_filename,
+ const char* input_filename,
+ const char* output_filename,
const LZ4IO_prefs_t* const prefs)
{
int result;
diff --git a/programs/util.h b/programs/util.h
index 3192ddc..f1d8a92 100644
--- a/programs/util.h
+++ b/programs/util.h
@@ -195,10 +195,7 @@ UTIL_STATIC void* UTIL_realloc(void* ptr, size_t size)
/*-****************************************
* String functions
******************************************/
-/*
- * A modified version of realloc().
- * If UTIL_realloc() fails the original block is freed.
-*/
+/* supports a==NULL or b==NULL */
UTIL_STATIC int UTIL_sameString(const char* a, const char* b)
{
assert(a!=NULL && b!=NULL); /* unsupported scenario */
@@ -430,6 +427,21 @@ UTIL_STATIC int UTIL_isRegFile(const char* infilename)
return UTIL_getFileStat(infilename, &statbuf); /* Only need to know whether it is a regular file */
}
+UTIL_STATIC int UTIL_isDir(const char* infilename)
+{
+ stat_t statbuf;
+ int r;
+#if defined(_MSC_VER)
+ r = _stat64(infilename, &statbuf);
+ if (r) return 0;
+ return (statbuf.st_mode & S_IFDIR);
+#else
+ r = stat(infilename, &statbuf);
+ if (r) return 0;
+ return (S_ISDIR(statbuf.st_mode));
+#endif
+}
+
UTIL_STATIC U32 UTIL_isDirectory(const char* infilename)
{