mirror of
https://github.com/isar/libmdbx.git
synced 2024-10-30 11:29:19 +08:00
9c5d029417
Change-Id: Ib1b9516ce09497559937f749d520719046c5b7e6
100 lines
2.6 KiB
C
100 lines
2.6 KiB
C
/* mdb_copy.c - memory-mapped database backup tool */
|
|
|
|
/*
|
|
* Copyright (c) 2015,2016 Leonid Yuriev <leo@yuriev.ru>.
|
|
* Copyright (c) 2015,2016 Peter-Service R&D LLC.
|
|
*
|
|
* This file is part of ReOpenMDBX.
|
|
*
|
|
* ReOpenMDBX is free software; you can redistribute it and/or modify it under
|
|
* the terms of the GNU Affero General Public License as published by
|
|
* the Free Software Foundation; either version 3 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* ReOpenMDBX is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU Affero General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
*
|
|
* ---
|
|
*
|
|
* Copyright 2012-2014 Howard Chu, Symas Corp.
|
|
* 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>.
|
|
*/
|
|
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <signal.h>
|
|
#include "mdbx.h"
|
|
|
|
static void
|
|
sighandle(int sig)
|
|
{
|
|
(void) sig;
|
|
}
|
|
|
|
int main(int argc,char * argv[])
|
|
{
|
|
int rc;
|
|
MDB_env *env = NULL;
|
|
const char *progname = argv[0], *act;
|
|
unsigned flags = MDB_RDONLY;
|
|
unsigned cpflags = 0;
|
|
|
|
for (; argc > 1 && argv[1][0] == '-'; argc--, argv++) {
|
|
if (argv[1][1] == 'n' && argv[1][2] == '\0')
|
|
flags |= MDB_NOSUBDIR;
|
|
else if (argv[1][1] == 'c' && argv[1][2] == '\0')
|
|
cpflags |= MDB_CP_COMPACT;
|
|
else if (argv[1][1] == 'V' && argv[1][2] == '\0') {
|
|
printf("%s\n", MDB_VERSION_STRING);
|
|
exit(0);
|
|
} 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 = mdb_env_create(&env);
|
|
if (rc == MDB_SUCCESS) {
|
|
rc = mdb_env_open(env, argv[1], flags, 0640);
|
|
}
|
|
if (rc == MDB_SUCCESS) {
|
|
act = "copying";
|
|
if (argc == 2)
|
|
rc = mdb_env_copyfd2(env, STDOUT_FILENO, cpflags);
|
|
else
|
|
rc = mdb_env_copy2(env, argv[2], cpflags);
|
|
}
|
|
if (rc)
|
|
fprintf(stderr, "%s: %s failed, error %d (%s)\n",
|
|
progname, act, rc, mdb_strerror(rc));
|
|
mdb_env_close(env);
|
|
|
|
return rc ? EXIT_FAILURE : EXIT_SUCCESS;
|
|
}
|