Main Page | Class Hierarchy | Data Structures | Directories | File List | Data Fields | Related Pages

db_archive.c

00001 /*-
00002  * See the file LICENSE for redistribution information.
00003  *
00004  * Copyright (c) 1996-2005
00005  *      Sleepycat Software.  All rights reserved.
00006  *
00007  * $Id: db_archive.c,v 12.4 2005/09/09 12:38:30 bostic Exp $
00008  */
00009 
00010 #include "db_config.h"
00011 
00012 #ifndef lint
00013 static const char copyright[] =
00014     "Copyright (c) 1996-2005\nSleepycat Software Inc.  All rights reserved.\n";
00015 #endif
00016 
00017 #ifndef NO_SYSTEM_INCLUDES
00018 #include <sys/types.h>
00019 
00020 #include <stdio.h>
00021 #include <stdlib.h>
00022 #include <string.h>
00023 #include <unistd.h>
00024 #endif
00025 
00026 #include "db_int.h"
00027 
00028 int main __P((int, char *[]));
00029 int usage __P((void));
00030 int version_check __P((void));
00031 
00032 const char *progname;
00033 
00034 int
00035 main(argc, argv)
00036         int argc;
00037         char *argv[];
00038 {
00039         extern char *optarg;
00040         extern int optind;
00041         DB_ENV  *dbenv;
00042         u_int32_t flags;
00043         int ch, exitval, ret, verbose;
00044         char **file, *home, **list, *passwd;
00045 
00046         if ((progname = strrchr(argv[0], '/')) == NULL)
00047                 progname = argv[0];
00048         else
00049                 ++progname;
00050 
00051         if ((ret = version_check()) != 0)
00052                 return (ret);
00053 
00054         dbenv = NULL;
00055         flags = 0;
00056         exitval = verbose = 0;
00057         home = passwd = NULL;
00058         file = list = NULL;
00059         while ((ch = getopt(argc, argv, "adh:lP:sVv")) != EOF)
00060                 switch (ch) {
00061                 case 'a':
00062                         LF_SET(DB_ARCH_ABS);
00063                         break;
00064                 case 'd':
00065                         LF_SET(DB_ARCH_REMOVE);
00066                         break;
00067                 case 'h':
00068                         home = optarg;
00069                         break;
00070                 case 'l':
00071                         LF_SET(DB_ARCH_LOG);
00072                         break;
00073                 case 'P':
00074                         passwd = strdup(optarg);
00075                         memset(optarg, 0, strlen(optarg));
00076                         if (passwd == NULL) {
00077                                 fprintf(stderr, "%s: strdup: %s\n",
00078                                     progname, strerror(errno));
00079                                 return (EXIT_FAILURE);
00080                         }
00081                         break;
00082                 case 's':
00083                         LF_SET(DB_ARCH_DATA);
00084                         break;
00085                 case 'V':
00086                         printf("%s\n", db_version(NULL, NULL, NULL));
00087                         return (EXIT_SUCCESS);
00088                 case 'v':
00089                         verbose = 1;
00090                         break;
00091                 case '?':
00092                 default:
00093                         return (usage());
00094                 }
00095         argc -= optind;
00096         argv += optind;
00097 
00098         if (argc != 0)
00099                 return (usage());
00100 
00101         /* Handle possible interruptions. */
00102         __db_util_siginit();
00103 
00104         /*
00105          * Create an environment object and initialize it for error
00106          * reporting.
00107          */
00108         if ((ret = db_env_create(&dbenv, 0)) != 0) {
00109                 fprintf(stderr,
00110                     "%s: db_env_create: %s\n", progname, db_strerror(ret));
00111                 goto shutdown;
00112         }
00113 
00114         dbenv->set_errfile(dbenv, stderr);
00115         dbenv->set_errpfx(dbenv, progname);
00116 
00117         if (passwd != NULL && (ret = dbenv->set_encrypt(dbenv,
00118             passwd, DB_ENCRYPT_AES)) != 0) {
00119                 dbenv->err(dbenv, ret, "set_passwd");
00120                 goto shutdown;
00121         }
00122         /*
00123          * If attaching to a pre-existing environment fails, create a
00124          * private one and try again.
00125          */
00126         if ((ret = dbenv->open(dbenv, home, DB_USE_ENVIRON, 0)) != 0 &&
00127             (ret == DB_VERSION_MISMATCH ||
00128             (ret = dbenv->open(dbenv, home, DB_CREATE |
00129             DB_INIT_LOG | DB_PRIVATE | DB_USE_ENVIRON, 0)) != 0)) {
00130                 dbenv->err(dbenv, ret, "DB_ENV->open");
00131                 goto shutdown;
00132         }
00133 
00134         /* Get the list of names. */
00135         if ((ret = dbenv->log_archive(dbenv, &list, flags)) != 0) {
00136                 dbenv->err(dbenv, ret, "DB_ENV->log_archive");
00137                 goto shutdown;
00138         }
00139 
00140         /* Print the list of names. */
00141         if (list != NULL) {
00142                 for (file = list; *file != NULL; ++file)
00143                         printf("%s\n", *file);
00144                 free(list);
00145         }
00146 
00147         if (0) {
00148 shutdown:       exitval = 1;
00149         }
00150         if (dbenv != NULL && (ret = dbenv->close(dbenv, 0)) != 0) {
00151                 exitval = 1;
00152                 fprintf(stderr,
00153                     "%s: dbenv->close: %s\n", progname, db_strerror(ret));
00154         }
00155 
00156         if (passwd != NULL)
00157                 free(passwd);
00158 
00159         /* Resend any caught signal. */
00160         __db_util_sigresend();
00161 
00162         return (exitval == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
00163 }
00164 
00165 int
00166 usage()
00167 {
00168         (void)fprintf(stderr,
00169             "usage: %s [-adlsVv] [-h home] [-P password]\n", progname);
00170         return (EXIT_FAILURE);
00171 }
00172 
00173 int
00174 version_check()
00175 {
00176         int v_major, v_minor, v_patch;
00177 
00178         /* Make sure we're loaded with the right version of the DB library. */
00179         (void)db_version(&v_major, &v_minor, &v_patch);
00180         if (v_major != DB_VERSION_MAJOR || v_minor != DB_VERSION_MINOR) {
00181                 fprintf(stderr,
00182         "%s: version %d.%d doesn't match library version %d.%d\n",
00183                     progname, DB_VERSION_MAJOR, DB_VERSION_MINOR,
00184                     v_major, v_minor);
00185                 return (EXIT_FAILURE);
00186         }
00187         return (0);
00188 }

Generated on Sun Dec 25 12:14:15 2005 for Berkeley DB 4.4.16 by  doxygen 1.4.2