]> git.sur5r.net Git - openldap/blob - libraries/liblmdb/mdb_copy.c
tweak mdb_copy, trap signals
[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         char *envname = argv[1];
35
36         if (argc<2 || argc>3) {
37                 fprintf(stderr, "usage: %s srcpath [dstpath]\n", argv[0]);
38                 exit(EXIT_FAILURE);
39         }
40
41 #ifdef SIGPIPE
42         signal(SIGPIPE, sighandle);
43 #endif
44 #ifdef SIGHUP
45         signal(SIGHUP, sighandle);
46 #endif
47         signal(SIGINT, sighandle);
48         signal(SIGTERM, sighandle);
49
50         rc = mdb_env_create(&env);
51
52         rc = mdb_env_open(env, envname, MDB_RDONLY, 0);
53         if (rc) {
54                 printf("mdb_env_open failed, error %d %s\n", rc, mdb_strerror(rc));
55         } else {
56                 if (argc == 2)
57                         rc = mdb_env_copyfd(env, MDB_STDOUT);
58                 else
59                         rc = mdb_env_copy(env, argv[2]);
60                 if (rc)
61                         printf("mdb_env_copy failed, error %d %s\n", rc, mdb_strerror(rc));
62         }
63         mdb_env_close(env);
64
65         return rc ? EXIT_FAILURE : EXIT_SUCCESS;
66 }