mariadb/storage/bdb/db_verify/db_verify.c

260 lines
5.7 KiB
C
Raw Normal View History

2001-03-04 19:42:05 -05:00
/*-
* See the file LICENSE for redistribution information.
*
2005-12-05 10:27:46 -08:00
* Copyright (c) 1996-2005
2001-03-04 19:42:05 -05:00
* Sleepycat Software. All rights reserved.
2005-07-20 15:48:22 -07:00
*
2005-12-05 10:27:46 -08:00
* $Id: db_verify.c,v 12.3 2005/06/16 20:21:37 bostic Exp $
2001-03-04 19:42:05 -05:00
*/
#include "db_config.h"
#ifndef lint
static const char copyright[] =
2005-12-05 10:27:46 -08:00
"Copyright (c) 1996-2005\nSleepycat Software Inc. All rights reserved.\n";
2001-03-04 19:42:05 -05:00
#endif
#ifndef NO_SYSTEM_INCLUDES
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#endif
#include "db_int.h"
2002-10-30 15:57:05 +04:00
int main __P((int, char *[]));
int usage __P((void));
2005-12-05 10:27:46 -08:00
int version_check __P((void));
const char *progname;
2001-03-04 19:42:05 -05:00
int
main(argc, argv)
int argc;
char *argv[];
{
extern char *optarg;
extern int optind;
2002-10-30 15:57:05 +04:00
DB *dbp, *dbp1;
2001-03-04 19:42:05 -05:00
DB_ENV *dbenv;
2005-07-20 15:48:22 -07:00
u_int32_t flags, cache;
int ch, exitval, nflag, private;
int quiet, resize, ret;
2002-10-30 15:57:05 +04:00
char *home, *passwd;
2001-03-04 19:42:05 -05:00
2005-12-05 10:27:46 -08:00
if ((progname = strrchr(argv[0], '/')) == NULL)
progname = argv[0];
else
++progname;
if ((ret = version_check()) != 0)
2002-10-30 15:57:05 +04:00
return (ret);
2001-03-04 19:42:05 -05:00
dbenv = NULL;
2005-07-20 15:48:22 -07:00
dbp = NULL;
2002-10-30 15:57:05 +04:00
cache = MEGABYTE;
2005-07-20 15:48:22 -07:00
exitval = nflag = quiet = 0;
flags = 0;
2002-10-30 15:57:05 +04:00
home = passwd = NULL;
2005-07-20 15:48:22 -07:00
while ((ch = getopt(argc, argv, "h:NoP:quV")) != EOF)
2001-03-04 19:42:05 -05:00
switch (ch) {
case 'h':
home = optarg;
break;
case 'N':
nflag = 1;
2002-10-30 15:57:05 +04:00
break;
case 'P':
passwd = strdup(optarg);
memset(optarg, 0, strlen(optarg));
if (passwd == NULL) {
fprintf(stderr, "%s: strdup: %s\n",
progname, strerror(errno));
return (EXIT_FAILURE);
2001-03-04 19:42:05 -05:00
}
break;
2002-10-30 15:57:05 +04:00
case 'o':
2005-07-20 15:48:22 -07:00
LF_SET(DB_NOORDERCHK);
2001-03-04 19:42:05 -05:00
break;
case 'q':
quiet = 1;
break;
2005-07-20 15:48:22 -07:00
case 'u': /* Undocumented. */
LF_SET(DB_UNREF);
break;
2001-03-04 19:42:05 -05:00
case 'V':
printf("%s\n", db_version(NULL, NULL, NULL));
2002-10-30 15:57:05 +04:00
return (EXIT_SUCCESS);
2001-03-04 19:42:05 -05:00
case '?':
default:
2002-10-30 15:57:05 +04:00
return (usage());
2001-03-04 19:42:05 -05:00
}
argc -= optind;
argv += optind;
if (argc <= 0)
2002-10-30 15:57:05 +04:00
return (usage());
2001-03-04 19:42:05 -05:00
/* Handle possible interruptions. */
__db_util_siginit();
/*
* Create an environment object and initialize it for error
* reporting.
*/
2002-10-30 15:57:05 +04:00
retry: if ((ret = db_env_create(&dbenv, 0)) != 0) {
fprintf(stderr,
"%s: db_env_create: %s\n", progname, db_strerror(ret));
2001-03-04 19:42:05 -05:00
goto shutdown;
}
if (!quiet) {
dbenv->set_errfile(dbenv, stderr);
dbenv->set_errpfx(dbenv, progname);
}
2002-10-30 15:57:05 +04:00
if (nflag) {
if ((ret = dbenv->set_flags(dbenv, DB_NOLOCKING, 1)) != 0) {
dbenv->err(dbenv, ret, "set_flags: DB_NOLOCKING");
goto shutdown;
}
if ((ret = dbenv->set_flags(dbenv, DB_NOPANIC, 1)) != 0) {
dbenv->err(dbenv, ret, "set_flags: DB_NOPANIC");
goto shutdown;
}
2001-03-04 19:42:05 -05:00
}
2002-10-30 15:57:05 +04:00
if (passwd != NULL &&
(ret = dbenv->set_encrypt(dbenv, passwd, DB_ENCRYPT_AES)) != 0) {
dbenv->err(dbenv, ret, "set_passwd");
goto shutdown;
}
2001-03-04 19:42:05 -05:00
/*
2002-10-30 15:57:05 +04:00
* Attach to an mpool if it exists, but if that fails, attach to a
* private region. In the latter case, declare a reasonably large
* cache so that we don't fail when verifying large databases.
2001-03-04 19:42:05 -05:00
*/
2002-10-30 15:57:05 +04:00
private = 0;
if ((ret =
dbenv->open(dbenv, home, DB_INIT_MPOOL | DB_USE_ENVIRON, 0)) != 0) {
2005-07-20 15:48:22 -07:00
if (ret != DB_VERSION_MISMATCH) {
if ((ret =
dbenv->set_cachesize(dbenv, 0, cache, 1)) != 0) {
dbenv->err(dbenv, ret, "set_cachesize");
goto shutdown;
}
private = 1;
ret = dbenv->open(dbenv, home, DB_CREATE |
DB_INIT_MPOOL | DB_PRIVATE | DB_USE_ENVIRON, 0);
2002-10-30 15:57:05 +04:00
}
2005-07-20 15:48:22 -07:00
if (ret != 0) {
dbenv->err(dbenv, ret, "DB_ENV->open");
2002-10-30 15:57:05 +04:00
goto shutdown;
}
2001-03-04 19:42:05 -05:00
}
for (; !__db_util_interrupted() && argv[0] != NULL; ++argv) {
if ((ret = db_create(&dbp, dbenv, 0)) != 0) {
2002-10-30 15:57:05 +04:00
dbenv->err(dbenv, ret, "%s: db_create", progname);
2001-03-04 19:42:05 -05:00
goto shutdown;
}
2002-10-30 15:57:05 +04:00
/*
* We create a 2nd dbp to this database to get its pagesize
* because the dbp we're using for verify cannot be opened.
2005-07-20 15:48:22 -07:00
*
* If the database is corrupted, we may not be able to open
* it, of course. In that case, just continue, using the
* cache size we have.
2002-10-30 15:57:05 +04:00
*/
if (private) {
if ((ret = db_create(&dbp1, dbenv, 0)) != 0) {
dbenv->err(
dbenv, ret, "%s: db_create", progname);
goto shutdown;
}
2005-07-20 15:48:22 -07:00
ret = dbp1->open(dbp1,
NULL, argv[0], NULL, DB_UNKNOWN, DB_RDONLY, 0);
2002-10-30 15:57:05 +04:00
/*
* If we get here, we can check the cache/page.
* !!!
* If we have to retry with an env with a larger
* cache, we jump out of this loop. However, we
* will still be working on the same argv when we
* get back into the for-loop.
*/
2005-07-20 15:48:22 -07:00
if (ret == 0) {
if (__db_util_cache(
dbp1, &cache, &resize) == 0 && resize) {
(void)dbp1->close(dbp1, 0);
(void)dbp->close(dbp, 0);
dbp = NULL;
(void)dbenv->close(dbenv, 0);
dbenv = NULL;
goto retry;
}
2002-10-30 15:57:05 +04:00
}
2005-07-20 15:48:22 -07:00
(void)dbp1->close(dbp1, 0);
2001-03-04 19:42:05 -05:00
}
2005-07-20 15:48:22 -07:00
/* The verify method is a destructor. */
ret = dbp->verify(dbp, argv[0], NULL, NULL, flags);
dbp = NULL;
2001-03-04 19:42:05 -05:00
if (ret != 0)
goto shutdown;
}
if (0) {
shutdown: exitval = 1;
}
2002-10-30 15:57:05 +04:00
2005-07-20 15:48:22 -07:00
if (dbp != NULL && (ret = dbp->close(dbp, 0)) != 0) {
2002-10-30 15:57:05 +04:00
exitval = 1;
dbenv->err(dbenv, ret, "close");
}
2005-07-20 15:48:22 -07:00
if (dbenv != NULL && (ret = dbenv->close(dbenv, 0)) != 0) {
2001-03-04 19:42:05 -05:00
exitval = 1;
fprintf(stderr,
"%s: dbenv->close: %s\n", progname, db_strerror(ret));
}
2005-07-20 15:48:22 -07:00
if (passwd != NULL)
free(passwd);
2001-03-04 19:42:05 -05:00
/* Resend any caught signal. */
__db_util_sigresend();
2002-10-30 15:57:05 +04:00
return (exitval == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
2001-03-04 19:42:05 -05:00
}
2002-10-30 15:57:05 +04:00
int
2001-03-04 19:42:05 -05:00
usage()
{
2005-12-05 10:27:46 -08:00
fprintf(stderr, "usage: %s %s\n", progname,
"[-NoqV] [-h home] [-P password] db_file ...");
2002-10-30 15:57:05 +04:00
return (EXIT_FAILURE);
2001-03-04 19:42:05 -05:00
}
2002-10-30 15:57:05 +04:00
int
2005-12-05 10:27:46 -08:00
version_check()
2001-03-04 19:42:05 -05:00
{
int v_major, v_minor, v_patch;
/* Make sure we're loaded with the right version of the DB library. */
(void)db_version(&v_major, &v_minor, &v_patch);
2005-07-20 15:48:22 -07:00
if (v_major != DB_VERSION_MAJOR || v_minor != DB_VERSION_MINOR) {
2001-03-04 19:42:05 -05:00
fprintf(stderr,
2005-07-20 15:48:22 -07:00
"%s: version %d.%d doesn't match library version %d.%d\n",
2001-03-04 19:42:05 -05:00
progname, DB_VERSION_MAJOR, DB_VERSION_MINOR,
2005-07-20 15:48:22 -07:00
v_major, v_minor);
2002-10-30 15:57:05 +04:00
return (EXIT_FAILURE);
2001-03-04 19:42:05 -05:00
}
2002-10-30 15:57:05 +04:00
return (0);
2001-03-04 19:42:05 -05:00
}