From e400e605471a5bc6571623b4446c41bfc9eb4942 Mon Sep 17 00:00:00 2001 From: Elliott Hughes Date: Tue, 10 Mar 2020 09:18:22 -0700 Subject: Fix Mac build. The recent re-enablement of the BLKGETSIZE64 code broke the Mac build. Use the equivalent ioctl() pair instead. --- lib/lib.c | 5 +---- lib/portability.c | 20 ++++++++++++++++++++ lib/portability.h | 2 ++ 3 files changed, 23 insertions(+), 4 deletions(-) (limited to 'lib') diff --git a/lib/lib.c b/lib/lib.c index 2a4cd8c5..2ac5c74c 100644 --- a/lib/lib.c +++ b/lib/lib.c @@ -478,14 +478,11 @@ off_t fdlength(int fd) if (!fstat(fd, &st) && S_ISREG(st.st_mode)) return st.st_size; // If the ioctl works for this, return it. - if (ioctl(fd, BLKGETSIZE64, &size) >= 0) return size<<9; + if (get_block_device_size(fd, &size)) return size<<9; // If not, do a binary search for the last location we can read. (Some // block devices don't do BLKGETSIZE right.) This should probably have // a CONFIG option... - - // If not, do a binary search for the last location we can read. - old = lseek(fd, 0, SEEK_CUR); do { char temp; diff --git a/lib/portability.c b/lib/portability.c index 28aaf824..145ac474 100644 --- a/lib/portability.c +++ b/lib/portability.c @@ -575,3 +575,23 @@ char *fs_type_name(struct statfs *statfs) return s; #endif } + +#if defined(__APPLE__) +#include +int get_block_device_size(int fd, unsigned long long* size) +{ + unsigned long block_size, block_count; + + if (!ioctl(fd, DKIOCGETBLOCKSIZE, &block_size) && + !ioctl(fd, DKIOCGETBLOCKCOUNT, &block_count)) { + *size = block_count * block_size; + return 1; + } + return 0; +} +#elif defined(__linux__) +int get_block_device_size(int fd, unsigned long long* size) +{ + return (ioctl(fd, BLKGETSIZE64, &size) >= 0); +} +#endif diff --git a/lib/portability.h b/lib/portability.h index 76790f4d..acc32fd4 100644 --- a/lib/portability.h +++ b/lib/portability.h @@ -359,3 +359,5 @@ int dev_major(int dev); int dev_makedev(int major, int minor); char *fs_type_name(struct statfs *statfs); + +int get_block_device_size(int fd, unsigned long long *size); -- cgit v1.2.3