From 54f333cba7e39a9e2fc572764db6cd46a858570a Mon Sep 17 00:00:00 2001 From: Thomas Debesse Date: Thu, 13 Nov 2025 22:14:50 +0100 Subject: [PATCH 1/5] FileSystem: properly detect Linux with glibc to detect what's not --- src/common/FileSystem.cpp | 44 +++++++++++++++++++-------------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/src/common/FileSystem.cpp b/src/common/FileSystem.cpp index 03cd3ef266..7140e4b33f 100644 --- a/src/common/FileSystem.cpp +++ b/src/common/FileSystem.cpp @@ -192,14 +192,14 @@ inline int my_open(Str::StringRef path, openMode_t mode) int fd = _open_osfhandle(reinterpret_cast(h), modes[mode_] | O_BINARY | O_NOINHERIT); if (fd == -1) CloseHandle(h); -#elif defined(__FreeBSD__) || defined(__APPLE__) - // O_CLOEXEC is supported in macOS from 10.7 onwards - int fd = open(path.c_str(), modes[mode_] | O_CLOEXEC, 0666); -#elif defined(__linux__) +#elif defined(__linux__) && defined(__GLIBC__) int fd = open64(path.c_str(), modes[mode_] | O_CLOEXEC | O_LARGEFILE, 0666); #elif defined(__native_client__) // This doesn't actually work, but it's not used anyways int fd = open(path.c_str(), modes[mode_], 0666); +#else + // O_CLOEXEC is supported in macOS from 10.7 onwards + int fd = open(path.c_str(), modes[mode_] | O_CLOEXEC, 0666); #endif #ifndef _WIN32 @@ -238,47 +238,47 @@ inline offset_t my_ftell(FILE* fd) { #ifdef _WIN32 return _ftelli64(fd); -#elif defined(__FreeBSD__) || defined(__APPLE__) || defined(__native_client__) - return ftello(fd); -#elif defined(__linux__) +#elif defined(__linux__) && defined(__GLIBC__) return ftello64(fd); +#else + return ftello(fd); #endif } inline int my_fseek(FILE* fd, offset_t off, int whence) { #ifdef _WIN32 return _fseeki64(fd, off, whence); -#elif defined(__FreeBSD__) || defined(__APPLE__) || defined(__native_client__) - return fseeko(fd, off, whence); -#elif defined(__linux__) +#elif defined(__linux__) && defined(__GLIBC__) return fseeko64(fd, off, whence); +#else + return fseeko(fd, off, whence); #endif } #ifdef _WIN32 typedef struct _stati64 my_stat_t; -#elif defined(__FreeBSD__) || defined(__APPLE__) || defined(__native_client__) -using my_stat_t = struct stat; -#elif defined(__linux__) +#elif defined(__linux__) && defined(__GLIBC__) using my_stat_t = struct stat64; +#else +using my_stat_t = struct stat; #endif inline int my_fstat(int fd, my_stat_t* st) { #ifdef _WIN32 return _fstati64(fd, st); -#elif defined(__FreeBSD__) || defined(__APPLE__) || defined(__native_client__) - return fstat(fd, st); -#elif defined(__linux__) +#elif defined(__linux__) && defined(__GLIBC__) return fstat64(fd, st); +#else + return fstat(fd, st); #endif } inline int my_stat(Str::StringRef path, my_stat_t* st) { #ifdef _WIN32 return _wstati64(Str::UTF8To16(path).c_str(), st); -#elif defined(__FreeBSD__) || defined(__APPLE__) || defined(__native_client__) - return stat(path.c_str(), st); -#elif defined(__linux__) +#elif defined(__linux__) && defined(__GLIBC__) return stat64(path.c_str(), st); +#else + return stat(path.c_str(), st); #endif } inline intptr_t my_pread(int fd, void* buf, size_t count, offset_t offset) @@ -294,10 +294,10 @@ inline intptr_t my_pread(int fd, void* buf, size_t count, offset_t offset) return -1; } return bytesRead; -#elif defined(__FreeBSD__) || defined(__APPLE__) || defined(__native_client__) - return pread(fd, buf, count, offset); -#elif defined(__linux__) +#elif defined(__linux__) && defined(__GLIBC__) return pread64(fd, buf, count, offset); +#else + return pread(fd, buf, count, offset); #endif } From 81d12ce32c397ae233ddc7363a2bc1852e82bf3f Mon Sep 17 00:00:00 2001 From: Thomas Debesse Date: Thu, 13 Nov 2025 22:36:30 +0100 Subject: [PATCH 2/5] FileSystem: let Native Client do open() like others It looks like the O_CLOEXEC symbol isn't missing. --- src/common/FileSystem.cpp | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/common/FileSystem.cpp b/src/common/FileSystem.cpp index 7140e4b33f..1016dadee2 100644 --- a/src/common/FileSystem.cpp +++ b/src/common/FileSystem.cpp @@ -194,11 +194,9 @@ inline int my_open(Str::StringRef path, openMode_t mode) CloseHandle(h); #elif defined(__linux__) && defined(__GLIBC__) int fd = open64(path.c_str(), modes[mode_] | O_CLOEXEC | O_LARGEFILE, 0666); -#elif defined(__native_client__) - // This doesn't actually work, but it's not used anyways - int fd = open(path.c_str(), modes[mode_], 0666); #else - // O_CLOEXEC is supported in macOS from 10.7 onwards + // This doesn't actually work in Native Client, but it's not used anyways. + // O_CLOEXEC is supported in macOS from 10.7 onwards. int fd = open(path.c_str(), modes[mode_] | O_CLOEXEC, 0666); #endif From 6df15175578f290175871cdaf337ec063de728f9 Mon Sep 17 00:00:00 2001 From: Thomas Debesse Date: Thu, 27 Nov 2025 05:03:34 +0100 Subject: [PATCH 3/5] FileSystem: mutualize the open64() selection --- src/common/FileSystem.cpp | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/src/common/FileSystem.cpp b/src/common/FileSystem.cpp index 1016dadee2..900ac2d18b 100644 --- a/src/common/FileSystem.cpp +++ b/src/common/FileSystem.cpp @@ -28,6 +28,11 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. =========================================================================== */ +#if defined(_WIN32) +#elif defined(__linux__) && defined(__GLIBC__) +#define DAEMON_OPEN64 +#endif + #if defined(BUILD_ENGINE) #include "minizip/unzip.h" #endif @@ -175,6 +180,7 @@ enum class openMode_t { MODE_APPEND, MODE_EDIT }; + inline int my_open(Str::StringRef path, openMode_t mode) { int mode_ = Util::ordinal(mode); @@ -192,7 +198,7 @@ inline int my_open(Str::StringRef path, openMode_t mode) int fd = _open_osfhandle(reinterpret_cast(h), modes[mode_] | O_BINARY | O_NOINHERIT); if (fd == -1) CloseHandle(h); -#elif defined(__linux__) && defined(__GLIBC__) +#elif defined(DAEMON_OPEN64) int fd = open64(path.c_str(), modes[mode_] | O_CLOEXEC | O_LARGEFILE, 0666); #else // This doesn't actually work in Native Client, but it's not used anyways. @@ -236,7 +242,7 @@ inline offset_t my_ftell(FILE* fd) { #ifdef _WIN32 return _ftelli64(fd); -#elif defined(__linux__) && defined(__GLIBC__) +#elif defined(DAEMON_OPEN64) return ftello64(fd); #else return ftello(fd); @@ -246,7 +252,7 @@ inline int my_fseek(FILE* fd, offset_t off, int whence) { #ifdef _WIN32 return _fseeki64(fd, off, whence); -#elif defined(__linux__) && defined(__GLIBC__) +#elif defined(DAEMON_OPEN64) return fseeko64(fd, off, whence); #else return fseeko(fd, off, whence); @@ -254,7 +260,7 @@ inline int my_fseek(FILE* fd, offset_t off, int whence) } #ifdef _WIN32 typedef struct _stati64 my_stat_t; -#elif defined(__linux__) && defined(__GLIBC__) +#elif defined(DAEMON_OPEN64) using my_stat_t = struct stat64; #else using my_stat_t = struct stat; @@ -263,7 +269,7 @@ inline int my_fstat(int fd, my_stat_t* st) { #ifdef _WIN32 return _fstati64(fd, st); -#elif defined(__linux__) && defined(__GLIBC__) +#elif defined(DAEMON_OPEN64) return fstat64(fd, st); #else return fstat(fd, st); @@ -273,7 +279,7 @@ inline int my_stat(Str::StringRef path, my_stat_t* st) { #ifdef _WIN32 return _wstati64(Str::UTF8To16(path).c_str(), st); -#elif defined(__linux__) && defined(__GLIBC__) +#elif defined(DAEMON_OPEN64) return stat64(path.c_str(), st); #else return stat(path.c_str(), st); @@ -292,7 +298,7 @@ inline intptr_t my_pread(int fd, void* buf, size_t count, offset_t offset) return -1; } return bytesRead; -#elif defined(__linux__) && defined(__GLIBC__) +#elif defined(DAEMON_OPEN64) return pread64(fd, buf, count, offset); #else return pread(fd, buf, count, offset); From d62d364664a8b17de332296167bcf12d644a47d9 Mon Sep 17 00:00:00 2001 From: Thomas Debesse Date: Thu, 27 Nov 2025 05:13:33 +0100 Subject: [PATCH 4/5] FileSystem: open64() is for large file, O_LARGEFILE is redundant --- src/common/FileSystem.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/common/FileSystem.cpp b/src/common/FileSystem.cpp index 900ac2d18b..227e7f0722 100644 --- a/src/common/FileSystem.cpp +++ b/src/common/FileSystem.cpp @@ -199,7 +199,7 @@ inline int my_open(Str::StringRef path, openMode_t mode) if (fd == -1) CloseHandle(h); #elif defined(DAEMON_OPEN64) - int fd = open64(path.c_str(), modes[mode_] | O_CLOEXEC | O_LARGEFILE, 0666); + int fd = open64(path.c_str(), modes[mode_] | O_CLOEXEC, 0666); #else // This doesn't actually work in Native Client, but it's not used anyways. // O_CLOEXEC is supported in macOS from 10.7 onwards. From 324d13b33756a0daf4131c2b8db52e207f559bf9 Mon Sep 17 00:00:00 2001 From: Thomas Debesse Date: Thu, 27 Nov 2025 05:31:50 +0100 Subject: [PATCH 5/5] FileSystem: drop open64(), glibc remap open() and others with _FILE_OFFSET_BITS 64 --- src/common/FileSystem.cpp | 19 ++----------------- 1 file changed, 2 insertions(+), 17 deletions(-) diff --git a/src/common/FileSystem.cpp b/src/common/FileSystem.cpp index 227e7f0722..5840255793 100644 --- a/src/common/FileSystem.cpp +++ b/src/common/FileSystem.cpp @@ -28,9 +28,8 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. =========================================================================== */ -#if defined(_WIN32) -#elif defined(__linux__) && defined(__GLIBC__) -#define DAEMON_OPEN64 +#if defined(__GLIBC__) +#define _FILE_OFFSET_BITS 64 #endif #if defined(BUILD_ENGINE) @@ -198,8 +197,6 @@ inline int my_open(Str::StringRef path, openMode_t mode) int fd = _open_osfhandle(reinterpret_cast(h), modes[mode_] | O_BINARY | O_NOINHERIT); if (fd == -1) CloseHandle(h); -#elif defined(DAEMON_OPEN64) - int fd = open64(path.c_str(), modes[mode_] | O_CLOEXEC, 0666); #else // This doesn't actually work in Native Client, but it's not used anyways. // O_CLOEXEC is supported in macOS from 10.7 onwards. @@ -242,8 +239,6 @@ inline offset_t my_ftell(FILE* fd) { #ifdef _WIN32 return _ftelli64(fd); -#elif defined(DAEMON_OPEN64) - return ftello64(fd); #else return ftello(fd); #endif @@ -252,16 +247,12 @@ inline int my_fseek(FILE* fd, offset_t off, int whence) { #ifdef _WIN32 return _fseeki64(fd, off, whence); -#elif defined(DAEMON_OPEN64) - return fseeko64(fd, off, whence); #else return fseeko(fd, off, whence); #endif } #ifdef _WIN32 typedef struct _stati64 my_stat_t; -#elif defined(DAEMON_OPEN64) -using my_stat_t = struct stat64; #else using my_stat_t = struct stat; #endif @@ -269,8 +260,6 @@ inline int my_fstat(int fd, my_stat_t* st) { #ifdef _WIN32 return _fstati64(fd, st); -#elif defined(DAEMON_OPEN64) - return fstat64(fd, st); #else return fstat(fd, st); #endif @@ -279,8 +268,6 @@ inline int my_stat(Str::StringRef path, my_stat_t* st) { #ifdef _WIN32 return _wstati64(Str::UTF8To16(path).c_str(), st); -#elif defined(DAEMON_OPEN64) - return stat64(path.c_str(), st); #else return stat(path.c_str(), st); #endif @@ -298,8 +285,6 @@ inline intptr_t my_pread(int fd, void* buf, size_t count, offset_t offset) return -1; } return bytesRead; -#elif defined(DAEMON_OPEN64) - return pread64(fd, buf, count, offset); #else return pread(fd, buf, count, offset); #endif