From c8928675c61ec55ccac565cb1d4258fa13e1468a Mon Sep 17 00:00:00 2001 From: Leonid Yuriev Date: Thu, 12 Sep 2019 14:21:43 +0300 Subject: [PATCH] mdbx: automatically create database directory. Change-Id: Ic1d4c9ce6f29924f5c112afc3065f08584307d39 --- src/elements/core.c | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/src/elements/core.c b/src/elements/core.c index b3a264bf..23b14f3e 100644 --- a/src/elements/core.c +++ b/src/elements/core.c @@ -6954,9 +6954,30 @@ int __cold mdbx_env_open(MDBX_env *env, const char *path, unsigned flags, int oflags; if (F_ISSET(flags, MDBX_RDONLY)) oflags = O_RDONLY; - else if (mode != 0) + else if (mode != 0) { + if ((flags & MDBX_NOSUBDIR) == 0) { +#if defined(_WIN32) || defined(_WIN64) + if (!CreateDirectoryA(path, nullptr)) { + rc = GetLastError(); + if (rc != ERROR_ALREADY_EXISTS) + goto bailout; + } +#else + const mode_t dir_mode = + (/* inherit read/write permissions for group and others */ mode & + (S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH)) | + /* always add read/write/search for owner */ S_IRWXU | + ((mode & S_IRGRP) ? /* +search if readable by group */ S_IXGRP : 0) | + ((mode & S_IROTH) ? /* +search if readable by others */ S_IXOTH : 0); + if (mkdir(path, dir_mode)) { + rc = errno; + if (rc != EEXIST) + goto bailout; + } +#endif + } oflags = O_RDWR | O_CREAT; - else + } else oflags = O_RDWR; rc = mdbx_openfile(dxb_pathname, oflags, mode, &env->me_fd,