summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorYann Collet <Cyan4973@users.noreply.github.com>2020-08-25 21:19:40 (GMT)
committerGitHub <noreply@github.com>2020-08-25 21:19:40 (GMT)
commit442005ff8e249937d3fb0b2f65b8a03558adb858 (patch)
treea55a97cb20a0f68b9f913750561cb7171bdda089
parentd64962a44bde352e24bf2e7553dc68c13568821e (diff)
parenta58b7ac6ce71819b712d0663f1d37bfb3cdd161c (diff)
downloadlz4-442005ff8e249937d3fb0b2f65b8a03558adb858.zip
lz4-442005ff8e249937d3fb0b2f65b8a03558adb858.tar.gz
lz4-442005ff8e249937d3fb0b2f65b8a03558adb858.tar.bz2
Merge pull request #905 from nh2/use-fstat
Use fstat() to determine file size
-rw-r--r--programs/lz4io.c4
-rw-r--r--programs/util.h66
2 files changed, 54 insertions, 16 deletions
diff --git a/programs/lz4io.c b/programs/lz4io.c
index 7926b20..0f3507e 100644
--- a/programs/lz4io.c
+++ b/programs/lz4io.c
@@ -680,7 +680,7 @@ LZ4IO_compressFilename_extRess(LZ4IO_prefs_t* const io_prefs, cRess_t ress,
prefs.frameInfo.contentChecksumFlag = (LZ4F_contentChecksum_t)io_prefs->streamChecksum;
prefs.favorDecSpeed = io_prefs->favorDecSpeed;
if (io_prefs->contentSizeFlag) {
- U64 const fileSize = UTIL_getFileSize(srcFileName);
+ U64 const fileSize = UTIL_getOpenFileSize(srcFile);
prefs.frameInfo.contentSize = fileSize; /* == 0 if input == stdin */
if (fileSize==0)
DISPLAYLEVEL(3, "Warning : cannot determine input content size \n");
@@ -1472,7 +1472,7 @@ LZ4IO_getCompressedFileInfo(LZ4IO_cFileInfo_t* cfinfo, const char* input_filenam
LZ4IO_infoResult result = LZ4IO_format_not_known; /* default result (error) */
unsigned char buffer[LZ4F_HEADER_SIZE_MAX];
FILE* const finput = LZ4IO_openSrcFile(input_filename);
- cfinfo->fileSize = UTIL_getFileSize(input_filename);
+ cfinfo->fileSize = (finput == NULL) ? 0 : UTIL_getOpenFileSize(finput);
while (!feof(finput)) {
LZ4IO_frameInfo_t frameInfo = LZ4IO_INIT_FRAMEINFO;
diff --git a/programs/util.h b/programs/util.h
index 8e361ca..2f3d0a8 100644
--- a/programs/util.h
+++ b/programs/util.h
@@ -33,7 +33,7 @@ extern "C" {
#include <stddef.h> /* size_t, ptrdiff_t */
#include <stdlib.h> /* malloc */
#include <string.h> /* strlen, strncpy */
-#include <stdio.h> /* fprintf */
+#include <stdio.h> /* fprintf, fileno */
#include <assert.h>
#include <sys/types.h> /* stat, utime */
#include <sys/stat.h> /* stat */
@@ -122,6 +122,36 @@ extern "C" {
#endif
+/*-****************************************
+* stat() functions
+******************************************/
+#if defined(_MSC_VER)
+# define UTIL_TYPE_stat __stat64
+# define UTIL_stat _stat64
+# define UTIL_fstat _fstat64
+# define UTIL_STAT_MODE_ISREG(st_mode) ((st_mode) & S_IFREG)
+#elif defined(__MINGW32__) && defined (__MSVCRT__)
+# define UTIL_TYPE_stat _stati64
+# define UTIL_stat _stati64
+# define UTIL_fstat _fstati64
+# define UTIL_STAT_MODE_ISREG(st_mode) ((st_mode) & S_IFREG)
+#else
+# define UTIL_TYPE_stat stat
+# define UTIL_stat stat
+# define UTIL_fstat fstat
+# define UTIL_STAT_MODE_ISREG(st_mode) (S_ISREG(st_mode))
+#endif
+
+
+/*-****************************************
+* fileno() function
+******************************************/
+#if defined(_MSC_VER)
+# define UTIL_fileno _fileno
+#else
+# define UTIL_fileno fileno
+#endif
+
/* *************************************
* Constants
***************************************/
@@ -357,22 +387,30 @@ UTIL_STATIC U32 UTIL_isDirectory(const char* infilename)
}
+UTIL_STATIC U64 UTIL_getOpenFileSize(FILE* file)
+{
+ int r;
+ int fd;
+ struct UTIL_TYPE_stat statbuf;
+
+ fd = UTIL_fileno(file);
+ if (fd < 0) {
+ perror("fileno");
+ exit(1);
+ }
+ r = UTIL_fstat(fd, &statbuf);
+ if (r || !UTIL_STAT_MODE_ISREG(statbuf.st_mode)) return 0; /* No good... */
+ return (U64)statbuf.st_size;
+}
+
+
UTIL_STATIC U64 UTIL_getFileSize(const char* infilename)
{
int r;
-#if defined(_MSC_VER)
- struct __stat64 statbuf;
- r = _stat64(infilename, &statbuf);
- if (r || !(statbuf.st_mode & S_IFREG)) return 0; /* No good... */
-#elif defined(__MINGW32__) && defined (__MSVCRT__)
- struct _stati64 statbuf;
- r = _stati64(infilename, &statbuf);
- if (r || !(statbuf.st_mode & S_IFREG)) return 0; /* No good... */
-#else
- struct stat statbuf;
- r = stat(infilename, &statbuf);
- if (r || !S_ISREG(statbuf.st_mode)) return 0; /* No good... */
-#endif
+ struct UTIL_TYPE_stat statbuf;
+
+ r = UTIL_stat(infilename, &statbuf);
+ if (r || !UTIL_STAT_MODE_ISREG(statbuf.st_mode)) return 0; /* No good... */
return (U64)statbuf.st_size;
}