From 7c2733de69b962708bc40a25e21f8ebdf0733a0c Mon Sep 17 00:00:00 2001 From: sorgelig Date: Sun, 26 May 2019 18:37:51 +0800 Subject: [PATCH] One more fix for getting file size. --- file_io.cpp | 42 ++++++++++++++++++++++++++---------------- 1 file changed, 26 insertions(+), 16 deletions(-) diff --git a/file_io.cpp b/file_io.cpp index 40090d8..f32b84b 100644 --- a/file_io.cpp +++ b/file_io.cpp @@ -322,20 +322,21 @@ int FileOpenEx(fileTYPE *file, const char *name, int mode, char mute) FileClose(file); return 0; } - if ((st.st_rdev != 0) && (st.st_size == 0)) //for special files we need an ioctl call to get the correct size - { - unsigned long long blksize; - int ret = ioctl(fd, BLKGETSIZE64, &blksize); - if (ret < 0 ) - { - if (!mute) printf("FileOpenEx(ioctl) File:%s, error: %d.\n", full_path, ret); - FileClose(file); - return 0; - } - file->size = blksize; - } - else - file->size = st.st_size; + + file->size = st.st_size; + if (st.st_rdev && !st.st_size) //for special files we need an ioctl call to get the correct size + { + unsigned long long blksize; + int ret = ioctl(fd, BLKGETSIZE64, &blksize); + if (ret < 0) + { + if (!mute) printf("FileOpenEx(ioctl) File:%s, error: %d.\n", full_path, ret); + FileClose(file); + return 0; + } + file->size = blksize; + } + file->offset = 0; file->mode = mode; } @@ -350,8 +351,17 @@ __off64_t FileGetSize(fileTYPE *file) if (file->filp) { struct stat64 st; - int ret = fstat64(fileno(file->filp), &st); - return (ret < 0) ? 0 : st.st_size; + if (fstat64(fileno(file->filp), &st) < 0) return 0; + + if (st.st_rdev && !st.st_size) //for special files we need an ioctl call to get the correct size + { + unsigned long long blksize; + int ret = ioctl(fileno(file->filp), BLKGETSIZE64, &blksize); + if (ret < 0) return 0; + return blksize; + } + + return st.st_size; } else if (file->zip) {