| 1 |
/*- |
| 2 |
* See the file LICENSE for redistribution information. |
| 3 |
* |
| 4 |
* Copyright (c) 1996, 2013 Oracle and/or its affiliates. All rights reserved. |
| 5 |
* |
| 6 |
* $Id$ |
| 7 |
*/ |
| 8 |
|
| 9 |
#include "db_config.h" |
| 10 |
|
| 11 |
#include "db_int.h" |
| 12 |
|
| 13 |
#ifndef lint |
| 14 |
static const char copyright[] = |
| 15 |
"Copyright (c) 1996, 2013 Oracle and/or its affiliates. All rights reserved.\n"; |
| 16 |
#endif |
| 17 |
|
| 18 |
int db_verify_main __P((int, char *[])); |
| 19 |
int db_verify_usage __P((void)); |
| 20 |
int db_verify_version_check __P((void)); |
| 21 |
|
| 22 |
const char *progname; |
| 23 |
|
| 24 |
int |
| 25 |
db_verify(args) |
| 26 |
char *args; |
| 27 |
{ |
| 28 |
int argc; |
| 29 |
char **argv; |
| 30 |
|
| 31 |
__db_util_arg("db_verify", args, &argc, &argv); |
| 32 |
return (db_verify_main(argc, argv) ? EXIT_FAILURE : EXIT_SUCCESS); |
| 33 |
} |
| 34 |
|
| 35 |
#include <stdio.h> |
| 36 |
#define ERROR_RETURN ERROR |
| 37 |
|
| 38 |
int |
| 39 |
db_verify_main(argc, argv) |
| 40 |
int argc; |
| 41 |
char *argv[]; |
| 42 |
{ |
| 43 |
extern char *optarg; |
| 44 |
extern int optind, __db_getopt_reset; |
| 45 |
DB *dbp, *dbp1; |
| 46 |
DB_ENV *dbenv; |
| 47 |
u_int32_t flags, cache; |
| 48 |
int ch, exitval, mflag, nflag, private; |
| 49 |
int quiet, resize, ret; |
| 50 |
char *dname, *fname, *home, *passwd; |
| 51 |
|
| 52 |
if ((progname = __db_rpath(argv[0])) == NULL) |
| 53 |
progname = argv[0]; |
| 54 |
else |
| 55 |
++progname; |
| 56 |
|
| 57 |
if ((ret = db_verify_version_check()) != 0) |
| 58 |
return (ret); |
| 59 |
|
| 60 |
dbenv = NULL; |
| 61 |
dbp = NULL; |
| 62 |
cache = MEGABYTE; |
| 63 |
exitval = mflag = nflag = quiet = 0; |
| 64 |
flags = 0; |
| 65 |
home = passwd = NULL; |
| 66 |
__db_getopt_reset = 1; |
| 67 |
while ((ch = getopt(argc, argv, "h:mNoP:quV")) != EOF) |
| 68 |
switch (ch) { |
| 69 |
case 'h': |
| 70 |
home = optarg; |
| 71 |
break; |
| 72 |
case 'm': |
| 73 |
mflag = 1; |
| 74 |
break; |
| 75 |
case 'N': |
| 76 |
nflag = 1; |
| 77 |
break; |
| 78 |
case 'P': |
| 79 |
if (passwd != NULL) { |
| 80 |
fprintf(stderr, DB_STR("5132", |
| 81 |
"Password may not be specified twice")); |
| 82 |
free(passwd); |
| 83 |
return (EXIT_FAILURE); |
| 84 |
} |
| 85 |
passwd = strdup(optarg); |
| 86 |
memset(optarg, 0, strlen(optarg)); |
| 87 |
if (passwd == NULL) { |
| 88 |
fprintf(stderr, "%s: strdup: %s\n", |
| 89 |
progname, strerror(errno)); |
| 90 |
return (EXIT_FAILURE); |
| 91 |
} |
| 92 |
break; |
| 93 |
case 'o': |
| 94 |
LF_SET(DB_NOORDERCHK); |
| 95 |
break; |
| 96 |
case 'q': |
| 97 |
quiet = 1; |
| 98 |
break; |
| 99 |
case 'u': /* Undocumented. */ |
| 100 |
LF_SET(DB_UNREF); |
| 101 |
break; |
| 102 |
case 'V': |
| 103 |
printf("%s\n", db_version(NULL, NULL, NULL)); |
| 104 |
return (EXIT_SUCCESS); |
| 105 |
case '?': |
| 106 |
default: |
| 107 |
return (db_verify_usage()); |
| 108 |
} |
| 109 |
argc -= optind; |
| 110 |
argv += optind; |
| 111 |
|
| 112 |
if (argc <= 0) |
| 113 |
return (db_verify_usage()); |
| 114 |
|
| 115 |
if (mflag) { |
| 116 |
dname = argv[0]; |
| 117 |
fname = NULL; |
| 118 |
} else { |
| 119 |
fname = argv[0]; |
| 120 |
dname = NULL; |
| 121 |
} |
| 122 |
|
| 123 |
/* Handle possible interruptions. */ |
| 124 |
__db_util_siginit(); |
| 125 |
|
| 126 |
/* |
| 127 |
* Create an environment object and initialize it for error |
| 128 |
* reporting. |
| 129 |
*/ |
| 130 |
retry: if ((ret = db_env_create(&dbenv, 0)) != 0) { |
| 131 |
fprintf(stderr, |
| 132 |
"%s: db_env_create: %s\n", progname, db_strerror(ret)); |
| 133 |
goto err; |
| 134 |
} |
| 135 |
|
| 136 |
if (!quiet) { |
| 137 |
dbenv->set_errfile(dbenv, stderr); |
| 138 |
dbenv->set_errpfx(dbenv, progname); |
| 139 |
} |
| 140 |
|
| 141 |
if (nflag) { |
| 142 |
if ((ret = dbenv->set_flags(dbenv, DB_NOLOCKING, 1)) != 0) { |
| 143 |
dbenv->err(dbenv, ret, "set_flags: DB_NOLOCKING"); |
| 144 |
goto err; |
| 145 |
} |
| 146 |
if ((ret = dbenv->set_flags(dbenv, DB_NOPANIC, 1)) != 0) { |
| 147 |
dbenv->err(dbenv, ret, "set_flags: DB_NOPANIC"); |
| 148 |
goto err; |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
if (passwd != NULL && |
| 153 |
(ret = dbenv->set_encrypt(dbenv, passwd, DB_ENCRYPT_AES)) != 0) { |
| 154 |
dbenv->err(dbenv, ret, "set_passwd"); |
| 155 |
goto err; |
| 156 |
} |
| 157 |
/* |
| 158 |
* Attach to an mpool if it exists, but if that fails, attach to a |
| 159 |
* private region. In the latter case, declare a reasonably large |
| 160 |
* cache so that we don't fail when verifying large databases. |
| 161 |
*/ |
| 162 |
private = 0; |
| 163 |
if ((ret = |
| 164 |
dbenv->open(dbenv, home, DB_INIT_MPOOL | DB_USE_ENVIRON, 0)) != 0) { |
| 165 |
if (ret != DB_VERSION_MISMATCH && ret != DB_REP_LOCKOUT) { |
| 166 |
if ((ret = |
| 167 |
dbenv->set_cachesize(dbenv, 0, cache, 1)) != 0) { |
| 168 |
dbenv->err(dbenv, ret, "set_cachesize"); |
| 169 |
goto err; |
| 170 |
} |
| 171 |
private = 1; |
| 172 |
ret = dbenv->open(dbenv, home, DB_CREATE | |
| 173 |
DB_INIT_MPOOL | DB_PRIVATE | DB_USE_ENVIRON, 0); |
| 174 |
} |
| 175 |
if (ret != 0) { |
| 176 |
dbenv->err(dbenv, ret, "DB_ENV->open"); |
| 177 |
goto err; |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
/* |
| 182 |
* Find out if we have a transactional environment so that we can |
| 183 |
* make sure that we don't open the verify database with logging |
| 184 |
* enabled. |
| 185 |
*/ |
| 186 |
for (; !__db_util_interrupted() && argv[0] != NULL; ++argv) { |
| 187 |
if ((ret = db_create(&dbp, dbenv, 0)) != 0) { |
| 188 |
dbenv->err(dbenv, ret, "%s: db_create", progname); |
| 189 |
goto err; |
| 190 |
} |
| 191 |
|
| 192 |
if (TXN_ON(dbenv->env) && |
| 193 |
(ret = dbp->set_flags(dbp, DB_TXN_NOT_DURABLE)) != 0) { |
| 194 |
dbenv->err( |
| 195 |
dbenv, ret, "%s: db_set_flags", progname); |
| 196 |
goto err; |
| 197 |
} |
| 198 |
|
| 199 |
/* |
| 200 |
* We create a 2nd dbp to this database to get its pagesize |
| 201 |
* because the dbp we're using for verify cannot be opened. |
| 202 |
* |
| 203 |
* If the database is corrupted, we may not be able to open |
| 204 |
* it, of course. In that case, just continue, using the |
| 205 |
* cache size we have. |
| 206 |
*/ |
| 207 |
if (private) { |
| 208 |
if ((ret = db_create(&dbp1, dbenv, 0)) != 0) { |
| 209 |
dbenv->err( |
| 210 |
dbenv, ret, "%s: db_create", progname); |
| 211 |
goto err; |
| 212 |
} |
| 213 |
|
| 214 |
if (TXN_ON(dbenv->env) && (ret = |
| 215 |
dbp1->set_flags(dbp1, DB_TXN_NOT_DURABLE)) != 0) { |
| 216 |
dbenv->err( |
| 217 |
dbenv, ret, "%s: db_set_flags", progname); |
| 218 |
goto err; |
| 219 |
} |
| 220 |
|
| 221 |
ret = dbp1->open(dbp1, |
| 222 |
NULL, fname, dname, DB_UNKNOWN, DB_RDONLY, 0); |
| 223 |
|
| 224 |
/* |
| 225 |
* If we get here, we can check the cache/page. |
| 226 |
* !!! |
| 227 |
* If we have to retry with an env with a larger |
| 228 |
* cache, we jump out of this loop. However, we |
| 229 |
* will still be working on the same argv when we |
| 230 |
* get back into the for-loop. |
| 231 |
*/ |
| 232 |
if (ret == 0) { |
| 233 |
if (__db_util_cache( |
| 234 |
dbp1, &cache, &resize) == 0 && resize) { |
| 235 |
(void)dbp1->close(dbp1, 0); |
| 236 |
(void)dbp->close(dbp, 0); |
| 237 |
dbp = NULL; |
| 238 |
|
| 239 |
(void)dbenv->close(dbenv, 0); |
| 240 |
dbenv = NULL; |
| 241 |
goto retry; |
| 242 |
} |
| 243 |
} |
| 244 |
(void)dbp1->close(dbp1, 0); |
| 245 |
} |
| 246 |
|
| 247 |
/* The verify method is a destructor. */ |
| 248 |
ret = dbp->verify(dbp, fname, dname, NULL, flags); |
| 249 |
dbp = NULL; |
| 250 |
if (ret != 0) |
| 251 |
exitval = 1; |
| 252 |
if (!quiet) |
| 253 |
printf(DB_STR_A("5105", "Verification of %s %s.\n", |
| 254 |
"%s %s\n"), argv[0], ret == 0 ? |
| 255 |
DB_STR_P("succeeded") : DB_STR_P("failed")); |
| 256 |
} |
| 257 |
|
| 258 |
if (0) { |
| 259 |
err: exitval = 1; |
| 260 |
} |
| 261 |
|
| 262 |
if (dbp != NULL && (ret = dbp->close(dbp, 0)) != 0) { |
| 263 |
exitval = 1; |
| 264 |
dbenv->err(dbenv, ret, DB_STR("5106", "close")); |
| 265 |
} |
| 266 |
if (dbenv != NULL && (ret = dbenv->close(dbenv, 0)) != 0) { |
| 267 |
exitval = 1; |
| 268 |
fprintf(stderr, |
| 269 |
"%s: dbenv->close: %s\n", progname, db_strerror(ret)); |
| 270 |
} |
| 271 |
|
| 272 |
if (passwd != NULL) |
| 273 |
free(passwd); |
| 274 |
|
| 275 |
/* Resend any caught signal. */ |
| 276 |
__db_util_sigresend(); |
| 277 |
|
| 278 |
return (exitval == 0 ? EXIT_SUCCESS : EXIT_FAILURE); |
| 279 |
} |
| 280 |
|
| 281 |
int |
| 282 |
db_verify_usage() |
| 283 |
{ |
| 284 |
fprintf(stderr, "usage: %s %s\n", progname, |
| 285 |
"[-NoqV] [-h home] [-P password] db_file ..."); |
| 286 |
return (EXIT_FAILURE); |
| 287 |
} |
| 288 |
|
| 289 |
int |
| 290 |
db_verify_version_check() |
| 291 |
{ |
| 292 |
int v_major, v_minor, v_patch; |
| 293 |
|
| 294 |
/* Make sure we're loaded with the right version of the DB library. */ |
| 295 |
(void)db_version(&v_major, &v_minor, &v_patch); |
| 296 |
if (v_major != DB_VERSION_MAJOR || v_minor != DB_VERSION_MINOR) { |
| 297 |
fprintf(stderr, DB_STR_A("5107", |
| 298 |
"%s: version %d.%d doesn't match library version %d.%d\n", |
| 299 |
"%s %d %d %d %d\n"), progname, DB_VERSION_MAJOR, |
| 300 |
DB_VERSION_MINOR, v_major, v_minor); |
| 301 |
return (EXIT_FAILURE); |
| 302 |
} |
| 303 |
return (0); |
| 304 |
} |