2012-10-12 03:21:40 +08:00
|
|
|
/* mdb_copy.c - memory-mapped database backup tool */
|
|
|
|
/*
|
2015-02-13 01:17:59 +08:00
|
|
|
* Copyright 2012-2015 Howard Chu, Symas Corp.
|
2012-10-12 03:21:40 +08: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
|
|
|
|
* <http://www.OpenLDAP.org/license.html>.
|
|
|
|
*/
|
2013-05-31 06:56:30 +08:00
|
|
|
#ifdef _WIN32
|
|
|
|
#include <windows.h>
|
|
|
|
#define MDB_STDOUT GetStdHandle(STD_OUTPUT_HANDLE)
|
|
|
|
#else
|
|
|
|
#define MDB_STDOUT 1
|
|
|
|
#endif
|
2012-10-12 03:21:40 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2013-05-31 06:56:30 +08:00
|
|
|
#include <signal.h>
|
2012-12-01 04:30:51 +08:00
|
|
|
#include "lmdb.h"
|
2012-10-12 03:21:40 +08:00
|
|
|
|
2013-05-31 06:56:30 +08:00
|
|
|
static void
|
|
|
|
sighandle(int sig)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-10-12 03:21:40 +08:00
|
|
|
int main(int argc,char * argv[])
|
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
MDB_env *env;
|
2013-12-11 18:57:12 +08:00
|
|
|
const char *progname = argv[0], *act;
|
|
|
|
unsigned flags = MDB_RDONLY;
|
2014-07-05 22:49:10 +08:00
|
|
|
unsigned cpflags = 0;
|
2013-12-11 18:57:12 +08:00
|
|
|
|
|
|
|
for (; argc > 1 && argv[1][0] == '-'; argc--, argv++) {
|
|
|
|
if (argv[1][1] == 'n' && argv[1][2] == '\0')
|
|
|
|
flags |= MDB_NOSUBDIR;
|
2014-07-02 12:45:43 +08:00
|
|
|
else if (argv[1][1] == 'c' && argv[1][2] == '\0')
|
2014-07-05 22:49:10 +08:00
|
|
|
cpflags |= MDB_CP_COMPACT;
|
2014-06-20 20:33:31 +08:00
|
|
|
else if (argv[1][1] == 'V' && argv[1][2] == '\0') {
|
|
|
|
printf("%s\n", MDB_VERSION_STRING);
|
|
|
|
exit(0);
|
|
|
|
} else
|
2013-12-11 18:57:12 +08:00
|
|
|
argc = 0;
|
|
|
|
}
|
2012-10-12 03:21:40 +08:00
|
|
|
|
2013-05-31 04:06:12 +08:00
|
|
|
if (argc<2 || argc>3) {
|
2014-07-02 12:45:43 +08:00
|
|
|
fprintf(stderr, "usage: %s [-V] [-c] [-n] srcpath [dstpath]\n", progname);
|
2012-10-12 03:21:40 +08:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2013-05-31 06:56:30 +08:00
|
|
|
#ifdef SIGPIPE
|
|
|
|
signal(SIGPIPE, sighandle);
|
|
|
|
#endif
|
|
|
|
#ifdef SIGHUP
|
|
|
|
signal(SIGHUP, sighandle);
|
|
|
|
#endif
|
|
|
|
signal(SIGINT, sighandle);
|
|
|
|
signal(SIGTERM, sighandle);
|
|
|
|
|
2013-12-11 18:57:12 +08:00
|
|
|
act = "opening environment";
|
2012-10-12 03:21:40 +08:00
|
|
|
rc = mdb_env_create(&env);
|
2013-12-11 18:57:12 +08:00
|
|
|
if (rc == MDB_SUCCESS) {
|
2015-01-15 14:19:30 +08:00
|
|
|
rc = mdb_env_open(env, argv[1], flags, 0600);
|
2013-12-11 18:57:12 +08:00
|
|
|
}
|
|
|
|
if (rc == MDB_SUCCESS) {
|
|
|
|
act = "copying";
|
2014-07-05 22:49:10 +08:00
|
|
|
if (argc == 2)
|
|
|
|
rc = mdb_env_copyfd2(env, MDB_STDOUT, cpflags);
|
|
|
|
else
|
|
|
|
rc = mdb_env_copy2(env, argv[2], cpflags);
|
2012-10-12 03:21:40 +08:00
|
|
|
}
|
2013-12-11 18:57:12 +08:00
|
|
|
if (rc)
|
|
|
|
fprintf(stderr, "%s: %s failed, error %d (%s)\n",
|
|
|
|
progname, act, rc, mdb_strerror(rc));
|
2012-10-12 03:21:40 +08:00
|
|
|
mdb_env_close(env);
|
|
|
|
|
|
|
|
return rc ? EXIT_FAILURE : EXIT_SUCCESS;
|
|
|
|
}
|