From cccc947b4ab1139d808eee01df2b828e3f0183ee Mon Sep 17 00:00:00 2001 From: Hallvard Furuseth Date: Mon, 8 Dec 2014 09:56:36 +0100 Subject: [PATCH] Try to avoid an invalid datafile after failed init --- libraries/liblmdb/mdb.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/libraries/liblmdb/mdb.c b/libraries/liblmdb/mdb.c index 32e24c0..cc6eaa2 100644 --- a/libraries/liblmdb/mdb.c +++ b/libraries/liblmdb/mdb.c @@ -3965,6 +3965,20 @@ mdb_env_open2(MDB_env *env) } meta.mm_mapsize = env->me_mapsize; + if (newenv && !(flags & MDB_FIXEDMAP)) { + /* mdb_env_map() may grow the datafile. Write the metapages + * first, so the file will be valid if initialization fails. + * Except with FIXEDMAP, since we do not yet know mm_address. + * We could fill in mm_address later, but then a different + * program might end up doing that - one with a memory layout + * and map address which does not suit the main program. + */ + rc = mdb_env_init_meta(env, &meta); + if (rc) + return rc; + newenv = 0; + } + rc = mdb_env_map(env, (flags & MDB_FIXEDMAP) ? meta.mm_address : NULL); if (rc) return rc; -- 2.7.4