From 76f0a024859d3907305ec43b2f5826b45987d553 Mon Sep 17 00:00:00 2001 From: Howard Chu Date: Tue, 13 Aug 2013 13:12:47 -0700 Subject: [PATCH] ITS#7664 better fix For RDONLY, don't get lockfile until we know datafile exists. Also, don't try to create a new datafile for me_mfd if someone deleted it after we got me_fd. --- libraries/liblmdb/mdb.c | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/libraries/liblmdb/mdb.c b/libraries/liblmdb/mdb.c index 6460173..ee7f771 100644 --- a/libraries/liblmdb/mdb.c +++ b/libraries/liblmdb/mdb.c @@ -3898,9 +3898,12 @@ mdb_env_open(MDB_env *env, const char *path, unsigned int flags, mdb_mode_t mode goto leave; } - rc = mdb_env_setup_locks(env, lpath, mode, &excl); - if (rc) - goto leave; + /* For RDONLY, get lockfile after we know datafile exists */ + if (!F_ISSET(flags, MDB_RDONLY)) { + rc = mdb_env_setup_locks(env, lpath, mode, &excl); + if (rc) + goto leave; + } #ifdef _WIN32 if (F_ISSET(flags, MDB_RDONLY)) { @@ -3926,6 +3929,12 @@ mdb_env_open(MDB_env *env, const char *path, unsigned int flags, mdb_mode_t mode goto leave; } + if (F_ISSET(flags, MDB_RDONLY)) { + rc = mdb_env_setup_locks(env, lpath, mode, &excl); + if (rc) + goto leave; + } + if ((rc = mdb_env_open2(env)) == MDB_SUCCESS) { if (flags & (MDB_RDONLY|MDB_WRITEMAP)) { env->me_mfd = env->me_fd; @@ -3934,10 +3943,12 @@ mdb_env_open(MDB_env *env, const char *path, unsigned int flags, mdb_mode_t mode * MDB_NOSYNC/MDB_NOMETASYNC, in case these get reset. */ #ifdef _WIN32 + len = OPEN_EXISTING; env->me_mfd = CreateFile(dpath, oflags, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, len, mode | FILE_FLAG_WRITE_THROUGH, NULL); #else + oflags &= ~O_CREAT; env->me_mfd = open(dpath, oflags | MDB_DSYNC, mode); #endif if (env->me_mfd == INVALID_HANDLE_VALUE) {