| 1 | /* mdb_copy.c - memory-mapped database backup tool */ |
| 2 | /* |
| 3 | * Copyright 2012-2018 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 | unsigned cpflags = 0; |
| 37 | |
| 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] == 'v' && argv[1][2] == '\0') |
| 42 | flags |= MDB_PREVSNAPSHOT; |
| 43 | else if (argv[1][1] == 'c' && argv[1][2] == '\0') |
| 44 | cpflags |= MDB_CP_COMPACT; |
| 45 | else if (argv[1][1] == 'V' && argv[1][2] == '\0') { |
| 46 | printf("%s\n" , MDB_VERSION_STRING); |
| 47 | exit(0); |
| 48 | } else |
| 49 | argc = 0; |
| 50 | } |
| 51 | |
| 52 | if (argc<2 || argc>3) { |
| 53 | fprintf(stderr, "usage: %s [-V] [-c] [-n] [-v] srcpath [dstpath]\n" , progname); |
| 54 | exit(EXIT_FAILURE); |
| 55 | } |
| 56 | |
| 57 | #ifdef SIGPIPE |
| 58 | signal(SIGPIPE, sighandle); |
| 59 | #endif |
| 60 | #ifdef SIGHUP |
| 61 | signal(SIGHUP, sighandle); |
| 62 | #endif |
| 63 | signal(SIGINT, sighandle); |
| 64 | signal(SIGTERM, sighandle); |
| 65 | |
| 66 | act = "opening environment" ; |
| 67 | rc = mdb_env_create(&env); |
| 68 | if (rc == MDB_SUCCESS) { |
| 69 | rc = mdb_env_open(env, argv[1], flags, 0600); |
| 70 | } |
| 71 | if (rc == MDB_SUCCESS) { |
| 72 | act = "copying" ; |
| 73 | if (argc == 2) |
| 74 | rc = mdb_env_copyfd2(env, MDB_STDOUT, cpflags); |
| 75 | else |
| 76 | rc = mdb_env_copy2(env, argv[2], cpflags); |
| 77 | } |
| 78 | if (rc) |
| 79 | fprintf(stderr, "%s: %s failed, error %d (%s)\n" , |
| 80 | progname, act, rc, mdb_strerror(rc)); |
| 81 | mdb_env_close(env); |
| 82 | |
| 83 | return rc ? EXIT_FAILURE : EXIT_SUCCESS; |
| 84 | } |
| 85 | |