Skip to content

Commit 8028c28

Browse files
committed
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.
1 parent 5ab62e7 commit 8028c28

File tree

2 files changed

+23
-25
lines changed

2 files changed

+23
-25
lines changed

cmake/DaemonFlags.cmake

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -693,6 +693,13 @@ if (APPLE)
693693
set(CMAKE_BUILD_WITH_INSTALL_RPATH ON)
694694
endif()
695695

696+
# Glibc-specific definitions
697+
if (LINUX)
698+
# QUIRK: It does nothing when it is not needed on non-glibc Linux
699+
# systems so it's harmless to always set it.
700+
add_definitions(-D_FILE_OFFSET_BITS=64)
701+
endif()
702+
696703
# Configuration specific definitions
697704

698705
set_property(DIRECTORY APPEND PROPERTY COMPILE_DEFINITIONS $<${DEBUG_GENEXP_COND}:DEBUG_BUILD>)

src/common/FileSystem.cpp

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3232
#include "minizip/unzip.h"
3333
#endif
3434

35+
#if defined(__GLIBC__)
36+
#if !defined(_FILE_OFFSET_BITS) || (_FILE_OFFSET_BITS != 64)
37+
#error _FILE_OFFSET_BITS should be set to 64 on glibc
38+
#endif
39+
#endif
40+
3541
#ifdef BUILD_VM
3642
#include "shared/VMMain.h"
3743
#else
@@ -175,6 +181,7 @@ enum class openMode_t {
175181
MODE_APPEND,
176182
MODE_EDIT
177183
};
184+
178185
inline int my_open(Str::StringRef path, openMode_t mode)
179186
{
180187
int mode_ = Util::ordinal(mode);
@@ -192,14 +199,10 @@ inline int my_open(Str::StringRef path, openMode_t mode)
192199
int fd = _open_osfhandle(reinterpret_cast<intptr_t>(h), modes[mode_] | O_BINARY | O_NOINHERIT);
193200
if (fd == -1)
194201
CloseHandle(h);
195-
#elif defined(__FreeBSD__) || defined(__APPLE__)
196-
// O_CLOEXEC is supported in macOS from 10.7 onwards
202+
#else
203+
// This doesn't actually work in Native Client, but it's not used anyways.
204+
// O_CLOEXEC is supported in macOS from 10.7 onwards.
197205
int fd = open(path.c_str(), modes[mode_] | O_CLOEXEC, 0666);
198-
#elif defined(__linux__)
199-
int fd = open64(path.c_str(), modes[mode_] | O_CLOEXEC | O_LARGEFILE, 0666);
200-
#elif defined(__native_client__)
201-
// This doesn't actually work, but it's not used anyways
202-
int fd = open(path.c_str(), modes[mode_], 0666);
203206
#endif
204207

205208
#ifndef _WIN32
@@ -238,47 +241,37 @@ inline offset_t my_ftell(FILE* fd)
238241
{
239242
#ifdef _WIN32
240243
return _ftelli64(fd);
241-
#elif defined(__FreeBSD__) || defined(__APPLE__) || defined(__native_client__)
244+
#else
242245
return ftello(fd);
243-
#elif defined(__linux__)
244-
return ftello64(fd);
245246
#endif
246247
}
247248
inline int my_fseek(FILE* fd, offset_t off, int whence)
248249
{
249250
#ifdef _WIN32
250251
return _fseeki64(fd, off, whence);
251-
#elif defined(__FreeBSD__) || defined(__APPLE__) || defined(__native_client__)
252+
#else
252253
return fseeko(fd, off, whence);
253-
#elif defined(__linux__)
254-
return fseeko64(fd, off, whence);
255254
#endif
256255
}
257256
#ifdef _WIN32
258257
typedef struct _stati64 my_stat_t;
259-
#elif defined(__FreeBSD__) || defined(__APPLE__) || defined(__native_client__)
258+
#else
260259
using my_stat_t = struct stat;
261-
#elif defined(__linux__)
262-
using my_stat_t = struct stat64;
263260
#endif
264261
inline int my_fstat(int fd, my_stat_t* st)
265262
{
266263
#ifdef _WIN32
267264
return _fstati64(fd, st);
268-
#elif defined(__FreeBSD__) || defined(__APPLE__) || defined(__native_client__)
265+
#else
269266
return fstat(fd, st);
270-
#elif defined(__linux__)
271-
return fstat64(fd, st);
272267
#endif
273268
}
274269
inline int my_stat(Str::StringRef path, my_stat_t* st)
275270
{
276271
#ifdef _WIN32
277272
return _wstati64(Str::UTF8To16(path).c_str(), st);
278-
#elif defined(__FreeBSD__) || defined(__APPLE__) || defined(__native_client__)
273+
#else
279274
return stat(path.c_str(), st);
280-
#elif defined(__linux__)
281-
return stat64(path.c_str(), st);
282275
#endif
283276
}
284277
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)
294287
return -1;
295288
}
296289
return bytesRead;
297-
#elif defined(__FreeBSD__) || defined(__APPLE__) || defined(__native_client__)
290+
#else
298291
return pread(fd, buf, count, offset);
299-
#elif defined(__linux__)
300-
return pread64(fd, buf, count, offset);
301292
#endif
302293
}
303294

0 commit comments

Comments
 (0)