From 8028c28e82e0ed7d25e658ccd6226ce92d555ac5 Mon Sep 17 00:00:00 2001 From: Thomas Debesse Date: Thu, 13 Nov 2025 22:14:50 +0100 Subject: [PATCH] FileSystem: unify open() wording and deduplicate code - Set _FILE_OFFSET_BITS=64 on Linux so it's set when using glibc, this allows to write open() instead of open64() because when set, glibc uses open64() when calling open(). This definition does nothing when glibc isn't used so it's harmless in such other cases. - let Native Client do open() like others, O_CLOEXEC is available as well. - O_LARGEFILE is redundant with open64() or _FILE_OFFSET_BITS=64. --- cmake/DaemonFlags.cmake | 7 +++++++ src/common/FileSystem.cpp | 41 +++++++++++++++------------------------ 2 files changed, 23 insertions(+), 25 deletions(-) diff --git a/cmake/DaemonFlags.cmake b/cmake/DaemonFlags.cmake index 868be61185..28c2b47281 100644 --- a/cmake/DaemonFlags.cmake +++ b/cmake/DaemonFlags.cmake @@ -693,6 +693,13 @@ if (APPLE) set(CMAKE_BUILD_WITH_INSTALL_RPATH ON) endif() +# Glibc-specific definitions +if (LINUX) + # QUIRK: It does nothing when it is not needed on non-glibc Linux + # systems so it's harmless to always set it. + add_definitions(-D_FILE_OFFSET_BITS=64) +endif() + # Configuration specific definitions set_property(DIRECTORY APPEND PROPERTY COMPILE_DEFINITIONS $<${DEBUG_GENEXP_COND}:DEBUG_BUILD>) diff --git a/src/common/FileSystem.cpp b/src/common/FileSystem.cpp index 03cd3ef266..45bd66b8b0 100644 --- a/src/common/FileSystem.cpp +++ b/src/common/FileSystem.cpp @@ -32,6 +32,12 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "minizip/unzip.h" #endif +#if defined(__GLIBC__) +#if !defined(_FILE_OFFSET_BITS) || (_FILE_OFFSET_BITS != 64) +#error _FILE_OFFSET_BITS should be set to 64 on glibc +#endif +#endif + #ifdef BUILD_VM #include "shared/VMMain.h" #else @@ -175,6 +181,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,14 +199,10 @@ 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 +#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. int fd = open(path.c_str(), modes[mode_] | O_CLOEXEC, 0666); -#elif defined(__linux__) - 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); #endif #ifndef _WIN32 @@ -238,47 +241,37 @@ inline offset_t my_ftell(FILE* fd) { #ifdef _WIN32 return _ftelli64(fd); -#elif defined(__FreeBSD__) || defined(__APPLE__) || defined(__native_client__) +#else return ftello(fd); -#elif defined(__linux__) - return ftello64(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__) +#else return fseeko(fd, off, whence); -#elif defined(__linux__) - return fseeko64(fd, off, whence); #endif } #ifdef _WIN32 typedef struct _stati64 my_stat_t; -#elif defined(__FreeBSD__) || defined(__APPLE__) || defined(__native_client__) +#else using my_stat_t = struct stat; -#elif defined(__linux__) -using my_stat_t = struct stat64; #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__) +#else return fstat(fd, st); -#elif defined(__linux__) - return fstat64(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__) +#else return stat(path.c_str(), st); -#elif defined(__linux__) - return stat64(path.c_str(), st); #endif } inline intptr_t my_pread(int fd, void* buf, size_t count, offset_t offset) @@ -294,10 +287,8 @@ 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__) +#else return pread(fd, buf, count, offset); -#elif defined(__linux__) - return pread64(fd, buf, count, offset); #endif }