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