1 /* mdb_copy.c - memory-mapped database backup tool */
3 * Copyright 2012 Howard Chu, Symas Corp.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted only as authorized by the OpenLDAP
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>.
16 #define MDB_STDOUT GetStdHandle(STD_OUTPUT_HANDLE)
30 int main(int argc,char * argv[])
34 const char *progname = argv[0], *act;
35 unsigned flags = MDB_RDONLY;
38 for (; argc > 1 && argv[1][0] == '-'; argc--, argv++) {
39 if (argv[1][1] == 'n' && argv[1][2] == '\0')
40 flags |= MDB_NOSUBDIR;
41 else if (argv[1][1] == 'c' && argv[1][2] == '\0')
42 cpflags |= MDB_CP_COMPACT;
43 else if (argv[1][1] == 'V' && argv[1][2] == '\0') {
44 printf("%s\n", MDB_VERSION_STRING);
50 if (argc<2 || argc>3) {
51 fprintf(stderr, "usage: %s [-V] [-c] [-n] srcpath [dstpath]\n", progname);
56 signal(SIGPIPE, sighandle);
59 signal(SIGHUP, sighandle);
61 signal(SIGINT, sighandle);
62 signal(SIGTERM, sighandle);
64 act = "opening environment";
65 rc = mdb_env_create(&env);
66 if (rc == MDB_SUCCESS) {
67 rc = mdb_env_open(env, argv[1], flags, 0600);
69 if (rc == MDB_SUCCESS) {
72 rc = mdb_env_copyfd2(env, MDB_STDOUT, cpflags);
74 rc = mdb_env_copy2(env, argv[2], cpflags);
77 fprintf(stderr, "%s: %s failed, error %d (%s)\n",
78 progname, act, rc, mdb_strerror(rc));
81 return rc ? EXIT_FAILURE : EXIT_SUCCESS;