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

db_verify.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_verify.c,v 12.3 2005/06/16 20:21:37 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 *dbp, *dbp1;
00042         DB_ENV *dbenv;
00043         u_int32_t flags, cache;
00044         int ch, exitval, nflag, private;
00045         int quiet, resize, ret;
00046         char *home, *passwd;
00047 
00048         if ((progname = strrchr(argv[0], '/')) == NULL)
00049                 progname = argv[0];
00050         else
00051                 ++progname;
00052 
00053         if ((ret = version_check()) != 0)
00054                 return (ret);
00055 
00056         dbenv = NULL;
00057         dbp = NULL;
00058         cache = MEGABYTE;
00059         exitval = nflag = quiet = 0;
00060         flags = 0;
00061         home = passwd = NULL;
00062         while ((ch = getopt(argc, argv, "h:NoP:quV")) != EOF)
00063                 switch (ch) {
00064                 case 'h':
00065                         home = optarg;
00066                         break;
00067                 case 'N':
00068                         nflag = 1;
00069                         break;
00070                 case 'P':
00071                         passwd = strdup(optarg);
00072                         memset(optarg, 0, strlen(optarg));
00073                         if (passwd == NULL) {
00074                                 fprintf(stderr, "%s: strdup: %s\n",
00075                                     progname, strerror(errno));
00076                                 return (EXIT_FAILURE);
00077                         }
00078                         break;
00079                 case 'o':
00080                         LF_SET(DB_NOORDERCHK);
00081                         break;
00082                 case 'q':
00083                         quiet = 1;
00084                         break;
00085                 case 'u':                       /* Undocumented. */
00086                         LF_SET(DB_UNREF);
00087                         break;
00088                 case 'V':
00089                         printf("%s\n", db_version(NULL, NULL, NULL));
00090                         return (EXIT_SUCCESS);
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 retry:  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         if (!quiet) {
00115                 dbenv->set_errfile(dbenv, stderr);
00116                 dbenv->set_errpfx(dbenv, progname);
00117         }
00118 
00119         if (nflag) {
00120                 if ((ret = dbenv->set_flags(dbenv, DB_NOLOCKING, 1)) != 0) {
00121                         dbenv->err(dbenv, ret, "set_flags: DB_NOLOCKING");
00122                         goto shutdown;
00123                 }
00124                 if ((ret = dbenv->set_flags(dbenv, DB_NOPANIC, 1)) != 0) {
00125                         dbenv->err(dbenv, ret, "set_flags: DB_NOPANIC");
00126                         goto shutdown;
00127                 }
00128         }
00129 
00130         if (passwd != NULL &&
00131             (ret = dbenv->set_encrypt(dbenv, passwd, DB_ENCRYPT_AES)) != 0) {
00132                 dbenv->err(dbenv, ret, "set_passwd");
00133                 goto shutdown;
00134         }
00135         /*
00136          * Attach to an mpool if it exists, but if that fails, attach to a
00137          * private region.  In the latter case, declare a reasonably large
00138          * cache so that we don't fail when verifying large databases.
00139          */
00140         private = 0;
00141         if ((ret =
00142             dbenv->open(dbenv, home, DB_INIT_MPOOL | DB_USE_ENVIRON, 0)) != 0) {
00143                 if (ret != DB_VERSION_MISMATCH) {
00144                         if ((ret =
00145                             dbenv->set_cachesize(dbenv, 0, cache, 1)) != 0) {
00146                                 dbenv->err(dbenv, ret, "set_cachesize");
00147                                 goto shutdown;
00148                         }
00149                         private = 1;
00150                         ret = dbenv->open(dbenv, home, DB_CREATE |
00151                             DB_INIT_MPOOL | DB_PRIVATE | DB_USE_ENVIRON, 0);
00152                 }
00153                 if (ret != 0) {
00154                         dbenv->err(dbenv, ret, "DB_ENV->open");
00155                         goto shutdown;
00156                 }
00157         }
00158 
00159         for (; !__db_util_interrupted() && argv[0] != NULL; ++argv) {
00160                 if ((ret = db_create(&dbp, dbenv, 0)) != 0) {
00161                         dbenv->err(dbenv, ret, "%s: db_create", progname);
00162                         goto shutdown;
00163                 }
00164 
00165                 /*
00166                  * We create a 2nd dbp to this database to get its pagesize
00167                  * because the dbp we're using for verify cannot be opened.
00168                  *
00169                  * If the database is corrupted, we may not be able to open
00170                  * it, of course.  In that case, just continue, using the
00171                  * cache size we have.
00172                  */
00173                 if (private) {
00174                         if ((ret = db_create(&dbp1, dbenv, 0)) != 0) {
00175                                 dbenv->err(
00176                                     dbenv, ret, "%s: db_create", progname);
00177                                 goto shutdown;
00178                         }
00179 
00180                         ret = dbp1->open(dbp1,
00181                             NULL, argv[0], NULL, DB_UNKNOWN, DB_RDONLY, 0);
00182 
00183                         /*
00184                          * If we get here, we can check the cache/page.
00185                          * !!!
00186                          * If we have to retry with an env with a larger
00187                          * cache, we jump out of this loop.  However, we
00188                          * will still be working on the same argv when we
00189                          * get back into the for-loop.
00190                          */
00191                         if (ret == 0) {
00192                                 if (__db_util_cache(
00193                                     dbp1, &cache, &resize) == 0 && resize) {
00194                                         (void)dbp1->close(dbp1, 0);
00195                                         (void)dbp->close(dbp, 0);
00196                                         dbp = NULL;
00197 
00198                                         (void)dbenv->close(dbenv, 0);
00199                                         dbenv = NULL;
00200                                         goto retry;
00201                                 }
00202                         }
00203                         (void)dbp1->close(dbp1, 0);
00204                 }
00205 
00206                 /* The verify method is a destructor. */
00207                 ret = dbp->verify(dbp, argv[0], NULL, NULL, flags);
00208                 dbp = NULL;
00209                 if (ret != 0)
00210                         goto shutdown;
00211         }
00212 
00213         if (0) {
00214 shutdown:       exitval = 1;
00215         }
00216 
00217         if (dbp != NULL && (ret = dbp->close(dbp, 0)) != 0) {
00218                 exitval = 1;
00219                 dbenv->err(dbenv, ret, "close");
00220         }
00221         if (dbenv != NULL && (ret = dbenv->close(dbenv, 0)) != 0) {
00222                 exitval = 1;
00223                 fprintf(stderr,
00224                     "%s: dbenv->close: %s\n", progname, db_strerror(ret));
00225         }
00226 
00227         if (passwd != NULL)
00228                 free(passwd);
00229 
00230         /* Resend any caught signal. */
00231         __db_util_sigresend();
00232 
00233         return (exitval == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
00234 }
00235 
00236 int
00237 usage()
00238 {
00239         fprintf(stderr, "usage: %s %s\n", progname,
00240             "[-NoqV] [-h home] [-P password] db_file ...");
00241         return (EXIT_FAILURE);
00242 }
00243 
00244 int
00245 version_check()
00246 {
00247         int v_major, v_minor, v_patch;
00248 
00249         /* Make sure we're loaded with the right version of the DB library. */
00250         (void)db_version(&v_major, &v_minor, &v_patch);
00251         if (v_major != DB_VERSION_MAJOR || v_minor != DB_VERSION_MINOR) {
00252                 fprintf(stderr,
00253         "%s: version %d.%d doesn't match library version %d.%d\n",
00254                     progname, DB_VERSION_MAJOR, DB_VERSION_MINOR,
00255                     v_major, v_minor);
00256                 return (EXIT_FAILURE);
00257         }
00258         return (0);
00259 }

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