2017-02-21 20:16:54 +03:00
|
|
|
/* mdbx_copy.c - memory-mapped database backup tool */
|
|
|
|
|
|
|
|
/*
|
2017-03-16 18:09:27 +03:00
|
|
|
* Copyright 2015-2017 Leonid Yuriev <leo@yuriev.ru>
|
|
|
|
* and other libmdbx authors: please see AUTHORS file.
|
2017-02-21 20:16:54 +03:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted only as authorized by the OpenLDAP
|
|
|
|
* Public License.
|
|
|
|
*
|
|
|
|
* A copy of this license is available in the file LICENSE in the
|
|
|
|
* top-level directory of the distribution or, alternatively, at
|
2017-05-23 14:44:53 +03:00
|
|
|
* <http://www.OpenLDAP.org/license.html>. */
|
2017-02-21 20:16:54 +03:00
|
|
|
|
2017-03-16 18:09:27 +03:00
|
|
|
#include "../../mdbx.h"
|
2017-02-21 20:16:54 +03:00
|
|
|
#include <signal.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2017-03-16 18:09:27 +03:00
|
|
|
#include <unistd.h>
|
2017-02-21 20:16:54 +03:00
|
|
|
|
|
|
|
static void sighandle(int sig) { (void)sig; }
|
|
|
|
|
|
|
|
int main(int argc, char *argv[]) {
|
|
|
|
int rc;
|
2017-05-24 01:42:10 +03:00
|
|
|
MDBX_env *env = NULL;
|
2017-02-21 20:16:54 +03:00
|
|
|
const char *progname = argv[0], *act;
|
2017-05-24 01:42:10 +03:00
|
|
|
unsigned flags = MDBX_RDONLY;
|
2017-02-21 20:16:54 +03:00
|
|
|
unsigned cpflags = 0;
|
|
|
|
|
|
|
|
for (; argc > 1 && argv[1][0] == '-'; argc--, argv++) {
|
|
|
|
if (argv[1][1] == 'n' && argv[1][2] == '\0')
|
2017-05-24 01:42:10 +03:00
|
|
|
flags |= MDBX_NOSUBDIR;
|
2017-02-21 20:16:54 +03:00
|
|
|
else if (argv[1][1] == 'c' && argv[1][2] == '\0')
|
2017-05-24 01:42:10 +03:00
|
|
|
cpflags |= MDBX_CP_COMPACT;
|
2017-02-21 20:16:54 +03:00
|
|
|
else if (argv[1][1] == 'V' && argv[1][2] == '\0') {
|
2017-05-24 01:07:15 +03:00
|
|
|
printf("%s (%s, build %s)\n", mdbx_version.git.describe,
|
|
|
|
mdbx_version.git.datetime, mdbx_build.datetime);
|
|
|
|
exit(EXIT_SUCCESS);
|
2017-02-21 20:16:54 +03:00
|
|
|
} else
|
|
|
|
argc = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (argc < 2 || argc > 3) {
|
|
|
|
fprintf(stderr, "usage: %s [-V] [-c] [-n] srcpath [dstpath]\n", progname);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef SIGPIPE
|
|
|
|
signal(SIGPIPE, sighandle);
|
|
|
|
#endif
|
|
|
|
#ifdef SIGHUP
|
|
|
|
signal(SIGHUP, sighandle);
|
|
|
|
#endif
|
|
|
|
signal(SIGINT, sighandle);
|
|
|
|
signal(SIGTERM, sighandle);
|
|
|
|
|
|
|
|
act = "opening environment";
|
|
|
|
rc = mdbx_env_create(&env);
|
2017-05-24 01:42:10 +03:00
|
|
|
if (rc == MDBX_SUCCESS) {
|
2017-02-21 20:16:54 +03:00
|
|
|
rc = mdbx_env_open(env, argv[1], flags, 0640);
|
|
|
|
}
|
2017-05-24 01:42:10 +03:00
|
|
|
if (rc == MDBX_SUCCESS) {
|
2017-02-21 20:16:54 +03:00
|
|
|
act = "copying";
|
|
|
|
if (argc == 2)
|
2017-05-22 20:02:23 +03:00
|
|
|
rc = mdbx_env_copy2fd(env, STDOUT_FILENO, cpflags);
|
2017-02-21 20:16:54 +03:00
|
|
|
else
|
2017-05-22 20:02:23 +03:00
|
|
|
rc = mdbx_env_copy(env, argv[2], cpflags);
|
2017-02-21 20:16:54 +03:00
|
|
|
}
|
|
|
|
if (rc)
|
|
|
|
fprintf(stderr, "%s: %s failed, error %d (%s)\n", progname, act, rc,
|
|
|
|
mdbx_strerror(rc));
|
|
|
|
mdbx_env_close(env);
|
|
|
|
|
|
|
|
return rc ? EXIT_FAILURE : EXIT_SUCCESS;
|
|
|
|
}
|