Use fstat() to determine file size.

This allows us to get the file size even when the input file is passed
via stdin. This fixes `--content-size` not working in situations like

    $ lz4 -v --content-size < /tmp/test > /tmp/test.lz4
    Warning : cannot determine input content size

With this change, it works.

Also helps with #904.
This commit is contained in:
Niklas Hambüchen 2020-08-24 06:23:51 +02:00
parent 34fe7c9d7f
commit 9a7658070a
2 changed files with 28 additions and 3 deletions

View File

@ -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.frameInfo.contentChecksumFlag = (LZ4F_contentChecksum_t)io_prefs->streamChecksum;
prefs.favorDecSpeed = io_prefs->favorDecSpeed; prefs.favorDecSpeed = io_prefs->favorDecSpeed;
if (io_prefs->contentSizeFlag) { if (io_prefs->contentSizeFlag) {
U64 const fileSize = UTIL_getFileSize(srcFileName); U64 const fileSize = UTIL_getOpenFileSize(srcFile);
prefs.frameInfo.contentSize = fileSize; /* == 0 if input == stdin */ prefs.frameInfo.contentSize = fileSize; /* == 0 if input == stdin */
if (fileSize==0) if (fileSize==0)
DISPLAYLEVEL(3, "Warning : cannot determine input content size \n"); 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) */ LZ4IO_infoResult result = LZ4IO_format_not_known; /* default result (error) */
unsigned char buffer[LZ4F_HEADER_SIZE_MAX]; unsigned char buffer[LZ4F_HEADER_SIZE_MAX];
FILE* const finput = LZ4IO_openSrcFile(input_filename); FILE* const finput = LZ4IO_openSrcFile(input_filename);
cfinfo->fileSize = UTIL_getFileSize(input_filename); cfinfo->fileSize = (finput == NULL) ? 0 : UTIL_getOpenFileSize(finput);
while (!feof(finput)) { while (!feof(finput)) {
LZ4IO_frameInfo_t frameInfo = LZ4IO_INIT_FRAMEINFO; LZ4IO_frameInfo_t frameInfo = LZ4IO_INIT_FRAMEINFO;

View File

@ -33,7 +33,7 @@ extern "C" {
#include <stddef.h> /* size_t, ptrdiff_t */ #include <stddef.h> /* size_t, ptrdiff_t */
#include <stdlib.h> /* malloc */ #include <stdlib.h> /* malloc */
#include <string.h> /* strlen, strncpy */ #include <string.h> /* strlen, strncpy */
#include <stdio.h> /* fprintf */ #include <stdio.h> /* fprintf, fileno */
#include <assert.h> #include <assert.h>
#include <sys/types.h> /* stat, utime */ #include <sys/types.h> /* stat, utime */
#include <sys/stat.h> /* stat */ #include <sys/stat.h> /* stat */
@ -357,6 +357,31 @@ UTIL_STATIC U32 UTIL_isDirectory(const char* infilename)
} }
UTIL_STATIC U64 UTIL_getOpenFileSize(FILE* file)
{
int r;
int fd = fileno(file);
if (fd < 0) {
perror("fileno");
exit(1);
}
#if defined(_MSC_VER)
struct __stat64 statbuf;
r = _fstat64(fd, &statbuf);
if (r || !(statbuf.st_mode & S_IFREG)) return 0; /* No good... */
#elif defined(__MINGW32__) && defined (__MSVCRT__)
struct _stati64 statbuf;
r = _fstati64(fd, &statbuf);
if (r || !(statbuf.st_mode & S_IFREG)) return 0; /* No good... */
#else
struct stat statbuf;
r = fstat(fd, &statbuf);
if (r || !S_ISREG(statbuf.st_mode)) return 0; /* No good... */
#endif
return (U64)statbuf.st_size;
}
UTIL_STATIC U64 UTIL_getFileSize(const char* infilename) UTIL_STATIC U64 UTIL_getFileSize(const char* infilename)
{ {
int r; int r;