Skip to content

Commit 8de0788

Browse files
committed
Only set me_mfd if needed. Drop unused read access.
1 parent e911ad6 commit 8de0788

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

libraries/liblmdb/mdb.c

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1239,7 +1239,7 @@ typedef struct MDB_pgstate {
12391239
struct MDB_env {
12401240
HANDLE me_fd; /**< The main data file */
12411241
HANDLE me_lfd; /**< The lock file */
1242-
HANDLE me_mfd; /**< just for writing the meta pages */
1242+
HANDLE me_mfd; /**< For writing and syncing the meta pages */
12431243
/** Failed to update the meta page. Probably an I/O error. */
12441244
#define MDB_FATAL_ERROR 0x80000000U
12451245
/** Some fields are initialized. */
@@ -3827,7 +3827,10 @@ mdb_env_write_meta(MDB_txn *txn)
38273827
len = sizeof(MDB_meta) - off;
38283828
off += (char *)mp - env->me_map;
38293829

3830-
/* Write to the SYNC fd */
3830+
/* Write to the SYNC fd unless MDB_NOSYNC/MDB_NOMETASYNC.
3831+
* (me_mfd goes to the same file as me_fd, but writing to it
3832+
* also syncs to disk. Avoids a separate fdatasync() call.)
3833+
*/
38313834
mfd = (flags & (MDB_NOSYNC|MDB_NOMETASYNC)) ? env->me_fd : env->me_mfd;
38323835
#ifdef _WIN32
38333836
{
@@ -4156,7 +4159,7 @@ enum mdb_fopen_type {
41564159
/* A comment in mdb_fopen() explains some O_* flag choices. */
41574160
MDB_O_RDONLY= O_RDONLY, /**< for RDONLY me_fd */
41584161
MDB_O_RDWR = O_RDWR |O_CREAT, /**< for me_fd */
4159-
MDB_O_META = O_RDWR |MDB_DSYNC |MDB_CLOEXEC, /**< for me_mfd */
4162+
MDB_O_META = O_WRONLY|MDB_DSYNC |MDB_CLOEXEC, /**< for me_mfd */
41604163
MDB_O_COPY = O_WRONLY|O_CREAT|O_EXCL|MDB_CLOEXEC, /**< for #mdb_env_copy() */
41614164
/** Bitmask for open() flags in enum #mdb_fopen_type. The other bits
41624165
* distinguish otherwise-equal MDB_O_* constants from each other.
@@ -4217,6 +4220,7 @@ mdb_fopen(const MDB_env *env, MDB_name *fname,
42174220
disp = OPEN_EXISTING;
42184221
break;
42194222
case MDB_O_META: /* for writing metapages */
4223+
acc = GENERIC_WRITE;
42204224
disp = OPEN_EXISTING;
42214225
attrs = FILE_ATTRIBUTE_NORMAL|FILE_FLAG_WRITE_THROUGH;
42224226
break;
@@ -4955,9 +4959,7 @@ mdb_env_open(MDB_env *env, const char *path, unsigned int flags, mdb_mode_t mode
49554959
}
49564960

49574961
if ((rc = mdb_env_open2(env)) == MDB_SUCCESS) {
4958-
if (flags & (MDB_RDONLY|MDB_WRITEMAP)) {
4959-
env->me_mfd = env->me_fd;
4960-
} else {
4962+
if (!(flags & (MDB_RDONLY|MDB_WRITEMAP))) {
49614963
/* Synchronous fd for meta writes. Needed even with
49624964
* MDB_NOSYNC/MDB_NOMETASYNC, in case these get reset.
49634965
*/
@@ -5040,7 +5042,7 @@ mdb_env_close0(MDB_env *env, int excl)
50405042
if (env->me_map) {
50415043
munmap(env->me_map, env->me_mapsize);
50425044
}
5043-
if (env->me_mfd != env->me_fd && env->me_mfd != INVALID_HANDLE_VALUE)
5045+
if (env->me_mfd != INVALID_HANDLE_VALUE)
50445046
(void) close(env->me_mfd);
50455047
if (env->me_fd != INVALID_HANDLE_VALUE)
50465048
(void) close(env->me_fd);

0 commit comments

Comments
 (0)