1 /* $NetBSD: mdb_copy.c,v 1.2 2020/08/11 13:15:38 christos Exp $ */ 2 3 /* mdb_copy.c - memory-mapped database backup tool */ 4 /* 5 * Copyright 2012-2020 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 unsigned cpflags = 0; 39 40 for (; argc > 1 && argv[1][0] == '-'; argc--, argv++) { 41 if (argv[1][1] == 'n' && argv[1][2] == '\0') 42 flags |= MDB_NOSUBDIR; 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] 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