xref: /netbsd-src/external/bsd/openldap/dist/libraries/liblmdb/mdb_copy.c (revision b7b7574d3bf8eeb51a1fa3977b59142ec6434a55)
1 /*	$NetBSD: mdb_copy.c,v 1.1.1.1 2014/05/28 09:58:42 tron Exp $	*/
2 
3 /* mdb_copy.c - memory-mapped database backup tool */
4 /*
5  * Copyright 2012 Howard Chu, Symas Corp.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
12  * A copy of this license is available in the file LICENSE in the
13  * top-level directory of the distribution or, alternatively, at
14  * <http://www.OpenLDAP.org/license.html>.
15  */
16 #ifdef _WIN32
17 #include <windows.h>
18 #define	MDB_STDOUT	GetStdHandle(STD_OUTPUT_HANDLE)
19 #else
20 #define	MDB_STDOUT	1
21 #endif
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <signal.h>
25 #include "lmdb.h"
26 
27 static void
28 sighandle(int sig)
29 {
30 }
31 
32 int main(int argc,char * argv[])
33 {
34 	int rc;
35 	MDB_env *env;
36 	const char *progname = argv[0], *act;
37 	unsigned flags = MDB_RDONLY;
38 
39 	for (; argc > 1 && argv[1][0] == '-'; argc--, argv++) {
40 		if (argv[1][1] == 'n' && argv[1][2] == '\0')
41 			flags |= MDB_NOSUBDIR;
42 		else
43 			argc = 0;
44 	}
45 
46 	if (argc<2 || argc>3) {
47 		fprintf(stderr, "usage: %s [-n] srcpath [dstpath]\n", progname);
48 		exit(EXIT_FAILURE);
49 	}
50 
51 #ifdef SIGPIPE
52 	signal(SIGPIPE, sighandle);
53 #endif
54 #ifdef SIGHUP
55 	signal(SIGHUP, sighandle);
56 #endif
57 	signal(SIGINT, sighandle);
58 	signal(SIGTERM, sighandle);
59 
60 	act = "opening environment";
61 	rc = mdb_env_create(&env);
62 	if (rc == MDB_SUCCESS) {
63 		rc = mdb_env_open(env, argv[1], flags, 0);
64 	}
65 	if (rc == MDB_SUCCESS) {
66 		act = "copying";
67 		if (argc == 2)
68 			rc = mdb_env_copyfd(env, MDB_STDOUT);
69 		else
70 			rc = mdb_env_copy(env, argv[2]);
71 	}
72 	if (rc)
73 		fprintf(stderr, "%s: %s failed, error %d (%s)\n",
74 			progname, act, rc, mdb_strerror(rc));
75 	mdb_env_close(env);
76 
77 	return rc ? EXIT_FAILURE : EXIT_SUCCESS;
78 }
79