diff options
author | Niklas Hambüchen <mail@nh2.me> | 2020-08-24 06:23:51 +0200 |
---|---|---|
committer | Niklas Hambüchen <mail@nh2.me> | 2020-08-24 06:27:28 +0200 |
commit | 9a7658070aeca2c0a6c68e5025a80f9e4b847ad7 (patch) | |
tree | 02082f6462611113623f51e56ac3dec07ccaa5d0 /programs/util.h | |
parent | 34fe7c9d7f4fbeeb42c22f4ad1a01c1771d14268 (diff) | |
download | lz4-9a7658070aeca2c0a6c68e5025a80f9e4b847ad7.tar.gz lz4-9a7658070aeca2c0a6c68e5025a80f9e4b847ad7.tar.bz2 lz4-9a7658070aeca2c0a6c68e5025a80f9e4b847ad7.zip |
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.
Diffstat (limited to 'programs/util.h')
-rw-r--r-- | programs/util.h | 27 |
1 files changed, 26 insertions, 1 deletions
diff --git a/programs/util.h b/programs/util.h index 8e361ca..4b2c09a 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 */ @@ -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) { int r; |