mdb_copy.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /* mdb_copy.c - memory-mapped database backup tool */
  2. /*
  3. * Copyright 2012-2021 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. static void
  25. sighandle(int sig)
  26. {
  27. }
  28. int main(int argc,char * argv[])
  29. {
  30. int rc;
  31. MDB_env *env;
  32. const char *progname = argv[0], *act;
  33. unsigned flags = MDB_RDONLY;
  34. unsigned cpflags = 0;
  35. for (; argc > 1 && argv[1][0] == '-'; argc--, argv++) {
  36. if (argv[1][1] == 'n' && argv[1][2] == '\0')
  37. flags |= MDB_NOSUBDIR;
  38. else if (argv[1][1] == 'c' && argv[1][2] == '\0')
  39. cpflags |= MDB_CP_COMPACT;
  40. else if (argv[1][1] == 'V' && argv[1][2] == '\0') {
  41. printf("%s\n", MDB_VERSION_STRING);
  42. exit(0);
  43. } else
  44. argc = 0;
  45. }
  46. if (argc<2 || argc>3) {
  47. fprintf(stderr, "usage: %s [-V] [-c] [-n] srcpath [dstpath]\n", progname);
  48. exit(EXIT_FAILURE);
  49. }
  50. #ifdef SIGPIPE
  51. signal(SIGPIPE, sighandle);
  52. #endif
  53. #ifdef SIGHUP
  54. signal(SIGHUP, sighandle);
  55. #endif
  56. signal(SIGINT, sighandle);
  57. signal(SIGTERM, sighandle);
  58. act = "opening environment";
  59. rc = mdb_env_create(&env);
  60. if (rc == MDB_SUCCESS) {
  61. rc = mdb_env_open(env, argv[1], flags, 0600);
  62. }
  63. if (rc == MDB_SUCCESS) {
  64. act = "copying";
  65. if (argc == 2)
  66. rc = mdb_env_copyfd2(env, MDB_STDOUT, cpflags);
  67. else
  68. rc = mdb_env_copy2(env, argv[2], cpflags);
  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. return rc ? EXIT_FAILURE : EXIT_SUCCESS;
  75. }