From af7b468e634ef6ce1d65c40338a0a38fcf0609fa Mon Sep 17 00:00:00 2001 From: Leo Yuriev Date: Wed, 24 May 2017 18:50:24 +0300 Subject: [PATCH] mdbx: use uint64_t or size_t insted of off_t (buggy on Windows). --- src/bits.h | 11 +++++++++++ src/lck-windows.c | 5 +++-- src/mdbx.c | 16 ++++++++-------- src/osal.c | 23 ++++++++++++++++------- src/osal.h | 14 +++++--------- test/config.cc | 2 +- test/config.h | 6 +++--- 7 files changed, 47 insertions(+), 30 deletions(-) diff --git a/src/bits.h b/src/bits.h index 0a999c02..6d9e1a37 100644 --- a/src/bits.h +++ b/src/bits.h @@ -28,6 +28,17 @@ # define MDBX_DEVEL 0 #endif +/*----------------------------------------------------------------------------*/ + +/* Should be defined before any includes */ +#ifndef _FILE_OFFSET_BITS +#define _FILE_OFFSET_BITS 64 +#endif + +#if defined(_MSC_VER) && !defined(_CRT_SECURE_NO_WARNINGS) +#define _CRT_SECURE_NO_WARNINGS +#endif + #include "../mdbx.h" #include "./defs.h" diff --git a/src/lck-windows.c b/src/lck-windows.c index 5b6551d6..898f7ecd 100644 --- a/src/lck-windows.c +++ b/src/lck-windows.c @@ -98,7 +98,7 @@ void mdbx_rthc_unlock(void) { LeaveCriticalSection(&rthc_critical_section); } #define LCK_WAITFOR 0 #define LCK_DONTWAIT LOCKFILE_FAIL_IMMEDIATELY -static __inline BOOL flock(mdbx_filehandle_t fd, DWORD flags, off_t offset, +static __inline BOOL flock(mdbx_filehandle_t fd, DWORD flags, uint64_t offset, size_t bytes) { OVERLAPPED ov; ov.hEvent = 0; @@ -107,7 +107,8 @@ static __inline BOOL flock(mdbx_filehandle_t fd, DWORD flags, off_t offset, return LockFileEx(fd, flags, 0, (DWORD)bytes, HIGH_DWORD(bytes), &ov); } -static __inline BOOL funlock(mdbx_filehandle_t fd, off_t offset, size_t bytes) { +static __inline BOOL funlock(mdbx_filehandle_t fd, uint64_t offset, + size_t bytes) { return UnlockFile(fd, (DWORD)offset, HIGH_DWORD(offset), (DWORD)bytes, HIGH_DWORD(bytes)); } diff --git a/src/mdbx.c b/src/mdbx.c index 5574cca0..ef93eeaf 100644 --- a/src/mdbx.c +++ b/src/mdbx.c @@ -3368,14 +3368,14 @@ static int mdbx_env_sync_locked(MDBX_env *env, unsigned flags, (pending->mm_txnid == head->mm_txnid || META_IS_WEAK(head)) ? head : mdbx_env_meta_flipflop(env, head); - off_t offset = (char *)target - env->me_map; + size_t offset = (char *)target - env->me_map; MDBX_meta *stay = mdbx_env_meta_flipflop(env, (MDBX_meta *)target); mdbx_debug( "writing meta %d (%s, was %" PRIaTXN "/%s, stay %s %" PRIaTXN "/%s), root %" PRIaPGNO ", " "txn_id %" PRIaTXN ", %s", - offset >= (off_t)env->me_psize, target == head ? "head" : "tail", + offset >= env->me_psize, target == head ? "head" : "tail", target->mm_txnid, META_IS_WEAK(target) ? "Weak" : META_IS_STEADY(target) ? "Steady" : "Legacy", @@ -3716,12 +3716,12 @@ static int __cold mdbx_setup_dxb(MDBX_env *env, MDBX_meta *meta, int lck_rc) { if (unlikely(err != MDBX_SUCCESS)) return err; } else { - off_t size; + uint64_t size; err = mdbx_filesize(env->me_fd, &size); if (unlikely(err != MDBX_SUCCESS)) return err; - if (size != (off_t)env->me_mapsize) { + if (size != env->me_mapsize) { mdbx_trace("filesize mismatch"); if ((env->me_flags & MDBX_RDONLY) || lck_rc != /* lck exclusive */ MDBX_RESULT_TRUE) @@ -3797,15 +3797,15 @@ static int __cold mdbx_setup_lck(MDBX_env *env, char *lck_pathname, int mode) { mdbx_debug("lck-setup: %s ", (rc == MDBX_RESULT_TRUE) ? "exclusive" : "shared"); - off_t size; + uint64_t size; err = mdbx_filesize(env->me_lfd, &size); if (unlikely(err != MDBX_SUCCESS)) return err; if (rc == MDBX_RESULT_TRUE) { - off_t wanna = roundup2((env->me_maxreaders - 1) * sizeof(MDBX_reader) + - sizeof(MDBX_lockinfo), - env->me_os_psize); + uint64_t wanna = roundup2((env->me_maxreaders - 1) * sizeof(MDBX_reader) + + sizeof(MDBX_lockinfo), + env->me_os_psize); #ifndef NDEBUG err = mdbx_ftruncate(env->me_lfd, size = 0); if (unlikely(err != MDBX_SUCCESS)) diff --git a/src/osal.c b/src/osal.c index a8550fef..d2ac9ebb 100644 --- a/src/osal.c +++ b/src/osal.c @@ -391,7 +391,7 @@ int mdbx_closefile(mdbx_filehandle_t fd) { #endif } -int mdbx_pread(mdbx_filehandle_t fd, void *buf, size_t bytes, off_t offset) { +int mdbx_pread(mdbx_filehandle_t fd, void *buf, size_t bytes, uint64_t offset) { if (bytes > MAX_WRITE) return MDBX_EINVAL; #if defined(_WIN32) || defined(_WIN64) @@ -407,6 +407,8 @@ int mdbx_pread(mdbx_filehandle_t fd, void *buf, size_t bytes, off_t offset) { return (rc == MDBX_SUCCESS) ? /* paranoia */ ERROR_READ_FAULT : rc; } #else + STATIC_ASSERT(sizeof(off_t) >= sizeof(size_t), + "libmdbx requires 64-bit file I/O on 64-bit systems"); ssize_t read = pread(fd, buf, bytes, offset); if (read < 0) { int rc = errno; @@ -417,7 +419,7 @@ int mdbx_pread(mdbx_filehandle_t fd, void *buf, size_t bytes, off_t offset) { } int mdbx_pwrite(mdbx_filehandle_t fd, const void *buf, size_t bytes, - off_t offset) { + uint64_t offset) { #if defined(_WIN32) || defined(_WIN64) if (bytes > MAX_WRITE) return ERROR_INVALID_PARAMETER; @@ -435,6 +437,8 @@ int mdbx_pwrite(mdbx_filehandle_t fd, const void *buf, size_t bytes, int rc; ssize_t written; do { + STATIC_ASSERT(sizeof(off_t) >= sizeof(size_t), + "libmdbx requires 64-bit file I/O on 64-bit systems"); written = pwrite(fd, buf, bytes, offset); if (likely(bytes == (size_t)written)) return MDBX_SUCCESS; @@ -445,7 +449,7 @@ int mdbx_pwrite(mdbx_filehandle_t fd, const void *buf, size_t bytes, } int mdbx_pwritev(mdbx_filehandle_t fd, struct iovec *iov, int iovcnt, - off_t offset, size_t expected_written) { + uint64_t offset, size_t expected_written) { #if defined(_WIN32) || defined(_WIN64) size_t written = 0; for (int i = 0; i < iovcnt; ++i) { @@ -461,6 +465,8 @@ int mdbx_pwritev(mdbx_filehandle_t fd, struct iovec *iov, int iovcnt, int rc; ssize_t written; do { + STATIC_ASSERT(sizeof(off_t) >= sizeof(size_t), + "libmdbx requires 64-bit file I/O on 64-bit systems"); written = pwritev(fd, iov, iovcnt, offset); if (likely(expected_written == (size_t)written)) return MDBX_SUCCESS; @@ -494,8 +500,7 @@ int mdbx_write(mdbx_filehandle_t fd, const void *buf, size_t bytes) { #ifdef SIGPIPE if (rc == EPIPE) { /* Collect the pending SIGPIPE, otherwise at least OS X - * gives it to the process on thread-exit (ITS#8504). - */ + * gives it to the process on thread-exit (ITS#8504). */ int tmp; sigwait(&set, &tmp); written = 0; @@ -542,7 +547,7 @@ int mdbx_filesync(mdbx_filehandle_t fd, bool fullsync) { #endif } -int mdbx_filesize(mdbx_filehandle_t fd, off_t *length) { +int mdbx_filesize(mdbx_filehandle_t fd, uint64_t *length) { #if defined(_WIN32) || defined(_WIN64) BY_HANDLE_FILE_INFORMATION info; if (!GetFileInformationByHandle(fd, &info)) @@ -551,6 +556,8 @@ int mdbx_filesize(mdbx_filehandle_t fd, off_t *length) { #else struct stat st; + STATIC_ASSERT(sizeof(off_t) <= sizeof(uint64_t), + "libmdbx requires 64-bit file I/O on 64-bit systems"); if (fstat(fd, &st)) return errno; @@ -559,7 +566,7 @@ int mdbx_filesize(mdbx_filehandle_t fd, off_t *length) { return MDBX_SUCCESS; } -int mdbx_ftruncate(mdbx_filehandle_t fd, off_t length) { +int mdbx_ftruncate(mdbx_filehandle_t fd, uint64_t length) { #if defined(_WIN32) || defined(_WIN64) LARGE_INTEGER li; li.QuadPart = length; @@ -567,6 +574,8 @@ int mdbx_ftruncate(mdbx_filehandle_t fd, off_t length) { ? MDBX_SUCCESS : mdbx_get_errno_checked(); #else + STATIC_ASSERT(sizeof(off_t) >= sizeof(size_t), + "libmdbx requires 64-bit file I/O on 64-bit systems"); return ftruncate(fd, length) == 0 ? MDBX_SUCCESS : errno; #endif } diff --git a/src/osal.h b/src/osal.h index 846e5673..fd050719 100644 --- a/src/osal.h +++ b/src/osal.h @@ -34,10 +34,6 @@ /*----------------------------------------------------------------------------*/ /* C99 includes */ -#ifndef _FILE_OFFSET_BITS -#define _FILE_OFFSET_BITS 64 -#endif - #include #include #include @@ -428,10 +424,10 @@ int mdbx_fastmutex_release(mdbx_fastmutex_t *fastmutex); int mdbx_fastmutex_destroy(mdbx_fastmutex_t *fastmutex); int mdbx_pwritev(mdbx_filehandle_t fd, struct iovec *iov, int iovcnt, - off_t offset, size_t expected_written); -int mdbx_pread(mdbx_filehandle_t fd, void *buf, size_t count, off_t offset); + uint64_t offset, size_t expected_written); +int mdbx_pread(mdbx_filehandle_t fd, void *buf, size_t count, uint64_t offset); int mdbx_pwrite(mdbx_filehandle_t fd, const void *buf, size_t count, - off_t offset); + uint64_t offset); int mdbx_write(mdbx_filehandle_t fd, const void *buf, size_t count); int mdbx_msync(void *addr, size_t length, int async); @@ -447,8 +443,8 @@ void *mdbx_thread_rthc_get(mdbx_thread_key_t key); void mdbx_thread_rthc_set(mdbx_thread_key_t key, const void *value); int mdbx_filesync(mdbx_filehandle_t fd, bool fullsync); -int mdbx_ftruncate(mdbx_filehandle_t fd, off_t length); -int mdbx_filesize(mdbx_filehandle_t fd, off_t *length); +int mdbx_ftruncate(mdbx_filehandle_t fd, uint64_t length); +int mdbx_filesize(mdbx_filehandle_t fd, uint64_t *length); int mdbx_openfile(const char *pathname, int flags, mode_t mode, mdbx_filehandle_t *fd); int mdbx_closefile(mdbx_filehandle_t fd); diff --git a/test/config.cc b/test/config.cc index 02a4f955..3c6ac224 100644 --- a/test/config.cc +++ b/test/config.cc @@ -70,7 +70,7 @@ bool parse_option(int argc, char *const argv[], int &narg, const char *option, } bool parse_option(int argc, char *const argv[], int &narg, const char *option, - size_t &mask, const option_verb *verbs) { + unsigned &mask, const option_verb *verbs) { const char *list; if (!parse_option(argc, argv, narg, option, &list)) return false; diff --git a/test/config.h b/test/config.h index 91ea4a24..483fe9b5 100644 --- a/test/config.h +++ b/test/config.h @@ -64,7 +64,7 @@ struct option_verb { }; bool parse_option(int argc, char *const argv[], int &narg, const char *option, - size_t &mask, const option_verb *verbs); + unsigned &mask, const option_verb *verbs); bool parse_option(int argc, char *const argv[], int &narg, const char *option, uint64_t &value, const scale_mode scale, @@ -194,8 +194,8 @@ struct keygen_params_pod { struct actor_params_pod { unsigned loglevel; - size_t mode_flags; - size_t table_flags; + unsigned mode_flags; + unsigned table_flags; uint64_t size; unsigned test_duration;