mirror of
https://github.com/isar/libmdbx.git
synced 2024-10-30 11:29:19 +08:00
mdbx: minor refine mdbx_env_copy().
Change-Id: I007add822bc4d30b7a8a838981ada6d2812f669a
This commit is contained in:
parent
40d5db2418
commit
b9d3eac12e
3
mdbx.h
3
mdbx.h
@ -695,7 +695,8 @@ LIBMDBX_API int mdbx_env_open(MDBX_env *env, const char *path, unsigned flags,
|
|||||||
* NOTE: Currently it fails if the environment has suffered a page leak.
|
* NOTE: Currently it fails if the environment has suffered a page leak.
|
||||||
*
|
*
|
||||||
* Returns A non-zero error value on failure and 0 on success. */
|
* Returns A non-zero error value on failure and 0 on success. */
|
||||||
LIBMDBX_API int mdbx_env_copy(MDBX_env *env, const char *path, unsigned flags);
|
LIBMDBX_API int mdbx_env_copy(MDBX_env *env, const char *dest_path,
|
||||||
|
unsigned flags);
|
||||||
|
|
||||||
/* Copy an MDBX environment to the specified file descriptor,
|
/* Copy an MDBX environment to the specified file descriptor,
|
||||||
* with options.
|
* with options.
|
||||||
|
20
src/mdbx.c
20
src/mdbx.c
@ -10488,25 +10488,25 @@ int __cold mdbx_env_copy2fd(MDBX_env *env, mdbx_filehandle_t fd,
|
|||||||
return mdbx_env_copy_asis(env, fd);
|
return mdbx_env_copy_asis(env, fd);
|
||||||
}
|
}
|
||||||
|
|
||||||
int __cold mdbx_env_copy(MDBX_env *env, const char *path, unsigned flags) {
|
int __cold mdbx_env_copy(MDBX_env *env, const char *dest_path, unsigned flags) {
|
||||||
char *lck_pathname;
|
char *dxb_pathname;
|
||||||
mdbx_filehandle_t newfd = INVALID_HANDLE_VALUE;
|
mdbx_filehandle_t newfd = INVALID_HANDLE_VALUE;
|
||||||
|
|
||||||
if (env->me_flags & MDBX_NOSUBDIR) {
|
if (env->me_flags & MDBX_NOSUBDIR) {
|
||||||
lck_pathname = (char *)path;
|
dxb_pathname = (char *)dest_path;
|
||||||
} else {
|
} else {
|
||||||
size_t len = strlen(path);
|
size_t len = strlen(dest_path);
|
||||||
len += sizeof(MDBX_DATANAME);
|
len += sizeof(MDBX_DATANAME);
|
||||||
lck_pathname = malloc(len);
|
dxb_pathname = malloc(len);
|
||||||
if (!lck_pathname)
|
if (!dxb_pathname)
|
||||||
return MDBX_ENOMEM;
|
return MDBX_ENOMEM;
|
||||||
sprintf(lck_pathname, "%s" MDBX_DATANAME, path);
|
sprintf(dxb_pathname, "%s" MDBX_DATANAME, dest_path);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* The destination path must exist, but the destination file must not.
|
/* The destination path must exist, but the destination file must not.
|
||||||
* We don't want the OS to cache the writes, since the source data is
|
* We don't want the OS to cache the writes, since the source data is
|
||||||
* already in the OS cache. */
|
* already in the OS cache. */
|
||||||
int rc = mdbx_openfile(lck_pathname, O_WRONLY | O_CREAT | O_EXCL, 0666,
|
int rc = mdbx_openfile(dxb_pathname, O_WRONLY | O_CREAT | O_EXCL, 0666,
|
||||||
&newfd, true);
|
&newfd, true);
|
||||||
if (rc == MDBX_SUCCESS) {
|
if (rc == MDBX_SUCCESS) {
|
||||||
if (env->me_psize >= env->me_os_psize) {
|
if (env->me_psize >= env->me_os_psize) {
|
||||||
@ -10521,8 +10521,8 @@ int __cold mdbx_env_copy(MDBX_env *env, const char *path, unsigned flags) {
|
|||||||
rc = mdbx_env_copy2fd(env, newfd, flags);
|
rc = mdbx_env_copy2fd(env, newfd, flags);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!(env->me_flags & MDBX_NOSUBDIR))
|
if (dxb_pathname != dest_path)
|
||||||
free(lck_pathname);
|
free(dxb_pathname);
|
||||||
|
|
||||||
if (newfd != INVALID_HANDLE_VALUE) {
|
if (newfd != INVALID_HANDLE_VALUE) {
|
||||||
int err = mdbx_closefile(newfd);
|
int err = mdbx_closefile(newfd);
|
||||||
|
Loading…
Reference in New Issue
Block a user