mdb_copy.c 1.91 KB
Newer Older
1
2
/* mdb_copy.c - memory-mapped database backup tool */
/*
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
3
 * Copyright 2012-2018 Howard Chu, Symas Corp.
4
5
6
7
8
9
10
11
12
13
 * 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>.
 */
Howard Chu's avatar
Howard Chu committed
14
15
16
17
18
19
#ifdef _WIN32
#include <windows.h>
#define	MDB_STDOUT	GetStdHandle(STD_OUTPUT_HANDLE)
#else
#define	MDB_STDOUT	1
#endif
20
21
#include <stdio.h>
#include <stdlib.h>
Howard Chu's avatar
Howard Chu committed
22
#include <signal.h>
Howard Chu's avatar
Howard Chu committed
23
#include "lmdb.h"
24

Howard Chu's avatar
Howard Chu committed
25
26
27
28
29
static void
sighandle(int sig)
{
}

30
31
32
33
int main(int argc,char * argv[])
{
	int rc;
	MDB_env *env;
Hallvard Furuseth's avatar
Hallvard Furuseth committed
34
35
	const char *progname = argv[0], *act;
	unsigned flags = MDB_RDONLY;
Howard Chu's avatar
Howard Chu committed
36
	unsigned cpflags = 0;
Hallvard Furuseth's avatar
Hallvard Furuseth committed
37
38
39
40

	for (; argc > 1 && argv[1][0] == '-'; argc--, argv++) {
		if (argv[1][1] == 'n' && argv[1][2] == '\0')
			flags |= MDB_NOSUBDIR;
Howard Chu's avatar
Howard Chu committed
41
		else if (argv[1][1] == 'c' && argv[1][2] == '\0')
Howard Chu's avatar
Howard Chu committed
42
			cpflags |= MDB_CP_COMPACT;
Howard Chu's avatar
Howard Chu committed
43
44
45
46
		else if (argv[1][1] == 'V' && argv[1][2] == '\0') {
			printf("%s\n", MDB_VERSION_STRING);
			exit(0);
		} else
Hallvard Furuseth's avatar
Hallvard Furuseth committed
47
48
			argc = 0;
	}
49

Howard Chu's avatar
Howard Chu committed
50
	if (argc<2 || argc>3) {
Howard Chu's avatar
Howard Chu committed
51
		fprintf(stderr, "usage: %s [-V] [-c] [-n] srcpath [dstpath]\n", progname);
52
53
54
		exit(EXIT_FAILURE);
	}

Howard Chu's avatar
Howard Chu committed
55
56
57
58
59
60
61
62
63
#ifdef SIGPIPE
	signal(SIGPIPE, sighandle);
#endif
#ifdef SIGHUP
	signal(SIGHUP, sighandle);
#endif
	signal(SIGINT, sighandle);
	signal(SIGTERM, sighandle);

Hallvard Furuseth's avatar
Hallvard Furuseth committed
64
	act = "opening environment";
65
	rc = mdb_env_create(&env);
Hallvard Furuseth's avatar
Hallvard Furuseth committed
66
	if (rc == MDB_SUCCESS) {
67
		rc = mdb_env_open(env, argv[1], flags, 0600);
Hallvard Furuseth's avatar
Hallvard Furuseth committed
68
69
70
	}
	if (rc == MDB_SUCCESS) {
		act = "copying";
Howard Chu's avatar
Howard Chu committed
71
72
73
74
		if (argc == 2)
			rc = mdb_env_copyfd2(env, MDB_STDOUT, cpflags);
		else
			rc = mdb_env_copy2(env, argv[2], cpflags);
75
	}
Hallvard Furuseth's avatar
Hallvard Furuseth committed
76
77
78
	if (rc)
		fprintf(stderr, "%s: %s failed, error %d (%s)\n",
			progname, act, rc, mdb_strerror(rc));
79
80
81
82
	mdb_env_close(env);

	return rc ? EXIT_FAILURE : EXIT_SUCCESS;
}