2 * See the file LICENSE for redistribution information.
4 * Copyright (c) 1996, 2012 Oracle and/or its affiliates. All rights reserved.
14 static const char copyright[] =
15 "Copyright (c) 1996, 2012 Oracle and/or its affiliates. All rights reserved.\n";
18 int main __P((int, char *[]));
19 int usage __P((void));
20 int version_check __P((void));
33 u_int32_t flags, cache;
34 int ch, exitval, mflag, nflag, private;
35 int quiet, resize, ret;
36 char *dname, *fname, *home, *passwd;
38 if ((progname = __db_rpath(argv[0])) == NULL)
43 if ((ret = version_check()) != 0)
49 exitval = mflag = nflag = quiet = 0;
52 while ((ch = getopt(argc, argv, "h:mNoP:quV")) != EOF)
65 fprintf(stderr, DB_STR("5132",
66 "Password may not be specified twice"));
68 return (EXIT_FAILURE);
70 passwd = strdup(optarg);
71 memset(optarg, 0, strlen(optarg));
73 fprintf(stderr, "%s: strdup: %s\n",
74 progname, strerror(errno));
75 return (EXIT_FAILURE);
79 LF_SET(DB_NOORDERCHK);
84 case 'u': /* Undocumented. */
88 printf("%s\n", db_version(NULL, NULL, NULL));
89 return (EXIT_SUCCESS);
108 /* Handle possible interruptions. */
112 * Create an environment object and initialize it for error
115 retry: if ((ret = db_env_create(&dbenv, 0)) != 0) {
117 "%s: db_env_create: %s\n", progname, db_strerror(ret));
122 dbenv->set_errfile(dbenv, stderr);
123 dbenv->set_errpfx(dbenv, progname);
127 if ((ret = dbenv->set_flags(dbenv, DB_NOLOCKING, 1)) != 0) {
128 dbenv->err(dbenv, ret, "set_flags: DB_NOLOCKING");
131 if ((ret = dbenv->set_flags(dbenv, DB_NOPANIC, 1)) != 0) {
132 dbenv->err(dbenv, ret, "set_flags: DB_NOPANIC");
137 if (passwd != NULL &&
138 (ret = dbenv->set_encrypt(dbenv, passwd, DB_ENCRYPT_AES)) != 0) {
139 dbenv->err(dbenv, ret, "set_passwd");
143 * Attach to an mpool if it exists, but if that fails, attach to a
144 * private region. In the latter case, declare a reasonably large
145 * cache so that we don't fail when verifying large databases.
149 dbenv->open(dbenv, home, DB_INIT_MPOOL | DB_USE_ENVIRON, 0)) != 0) {
150 if (ret != DB_VERSION_MISMATCH) {
152 dbenv->set_cachesize(dbenv, 0, cache, 1)) != 0) {
153 dbenv->err(dbenv, ret, "set_cachesize");
157 ret = dbenv->open(dbenv, home, DB_CREATE |
158 DB_INIT_MPOOL | DB_PRIVATE | DB_USE_ENVIRON, 0);
161 dbenv->err(dbenv, ret, "DB_ENV->open");
167 * Find out if we have a transactional environment so that we can
168 * make sure that we don't open the verify database with logging
171 for (; !__db_util_interrupted() && argv[0] != NULL; ++argv) {
172 if ((ret = db_create(&dbp, dbenv, 0)) != 0) {
173 dbenv->err(dbenv, ret, "%s: db_create", progname);
177 if (TXN_ON(dbenv->env) &&
178 (ret = dbp->set_flags(dbp, DB_TXN_NOT_DURABLE)) != 0) {
180 dbenv, ret, "%s: db_set_flags", progname);
185 * We create a 2nd dbp to this database to get its pagesize
186 * because the dbp we're using for verify cannot be opened.
188 * If the database is corrupted, we may not be able to open
189 * it, of course. In that case, just continue, using the
190 * cache size we have.
193 if ((ret = db_create(&dbp1, dbenv, 0)) != 0) {
195 dbenv, ret, "%s: db_create", progname);
199 if (TXN_ON(dbenv->env) && (ret =
200 dbp1->set_flags(dbp1, DB_TXN_NOT_DURABLE)) != 0) {
202 dbenv, ret, "%s: db_set_flags", progname);
206 ret = dbp1->open(dbp1,
207 NULL, fname, dname, DB_UNKNOWN, DB_RDONLY, 0);
210 * If we get here, we can check the cache/page.
212 * If we have to retry with an env with a larger
213 * cache, we jump out of this loop. However, we
214 * will still be working on the same argv when we
215 * get back into the for-loop.
219 dbp1, &cache, &resize) == 0 && resize) {
220 (void)dbp1->close(dbp1, 0);
221 (void)dbp->close(dbp, 0);
224 (void)dbenv->close(dbenv, 0);
229 (void)dbp1->close(dbp1, 0);
232 /* The verify method is a destructor. */
233 ret = dbp->verify(dbp, fname, dname, NULL, flags);
238 printf(DB_STR_A("5105", "Verification of %s %s.\n",
239 "%s %s\n"), argv[0], ret == 0 ?
240 DB_STR_P("succeeded") : DB_STR_P("failed"));
247 if (dbp != NULL && (ret = dbp->close(dbp, 0)) != 0) {
249 dbenv->err(dbenv, ret, DB_STR("5106", "close"));
251 if (dbenv != NULL && (ret = dbenv->close(dbenv, 0)) != 0) {
254 "%s: dbenv->close: %s\n", progname, db_strerror(ret));
260 /* Resend any caught signal. */
261 __db_util_sigresend();
263 return (exitval == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
269 fprintf(stderr, "usage: %s %s\n", progname,
270 "[-NoqV] [-h home] [-P password] db_file ...");
271 return (EXIT_FAILURE);
277 int v_major, v_minor, v_patch;
279 /* Make sure we're loaded with the right version of the DB library. */
280 (void)db_version(&v_major, &v_minor, &v_patch);
281 if (v_major != DB_VERSION_MAJOR || v_minor != DB_VERSION_MINOR) {
282 fprintf(stderr, DB_STR_A("5107",
283 "%s: version %d.%d doesn't match library version %d.%d\n",
284 "%s %d %d %d %d\n"), progname, DB_VERSION_MAJOR,
285 DB_VERSION_MINOR, v_major, v_minor);
286 return (EXIT_FAILURE);