2 * See the file LICENSE for redistribution information.
4 * Copyright (c) 1996, 1997, 1998, 1999, 2000
5 * Sleepycat Software. All rights reserved.
7 * Modified to check the consistency of didname.db by
8 * Joe Clarke <marcus@marcuscom.com>
10 * $Id: cnid_didname_verify.c,v 1.1 2001-12-10 07:04:27 jmarcus Exp $
17 #include <sys/types.h>
21 #define MIN(a, b) ((a) < (b) ? (a) : (b))
24 int main __P((int, char *[]));
25 void usage __P((void));
26 void version_check __P((void));
29 *progname = "db_verify"; /* Program name. */
31 static __inline__ int compare_did(const DBT *a, const DBT *b) {
34 memcpy(&dida, a->data, sizeof(dida));
35 memcpy(&didb, b->data, sizeof(didb));
39 #if DB_VERSION_MINOR > 1
40 static int compare_unix(DB *db, const DBT *a, const DBT *b)
42 static int compare_unix(const DBT *a, const DBT *b)
48 if ((ret = compare_did(a, b)))
51 sa = (u_int8_t *) a->data + 4;
52 sb = (u_int8_t *) b->data + 4;
53 for (len = MIN(a->size, b->size); len-- > 4; sa++, sb++)
54 if ((ret = (*sa - *sb)))
56 return a->size - b->size;
68 int ch, e_close, exitval, nflag, quiet, ret, t_ret;
70 char *dbname = "didname.db";
75 e_close = exitval = nflag = quiet = 0;
77 while ((ch = getopt(argc, argv, "h:NqV")) != EOF)
84 if ((ret = db_env_set_panicstate(0)) != 0) {
86 "%s: db_env_set_panicstate: %s\n",
87 progname, db_strerror(ret));
96 printf("%s\n", db_version(NULL, NULL, NULL));
106 * Create an environment object and initialize it for error
109 if ((ret = db_env_create(&dbenv, 0)) != 0) {
110 fprintf(stderr, "%s: db_env_create: %s\n",
111 progname, db_strerror(ret));
118 * We'd prefer to have error output configured while calling
119 * db_env_create, but there's no way to turn it off once it's
123 dbenv->set_errfile(dbenv, stderr);
124 dbenv->set_errpfx(dbenv, progname);
127 if (nflag && (ret = dbenv->set_mutexlocks(dbenv, 0)) != 0) {
128 dbenv->err(dbenv, ret, "set_mutexlocks");
133 * Attach to an mpool if it exists, but if that fails, attach
134 * to a private region.
136 if ((ret = dbenv->open(dbenv,
137 home, DB_INIT_MPOOL | DB_USE_ENVIRON, 0)) != 0 &&
138 (ret = dbenv->open(dbenv, home,
139 DB_CREATE | DB_INIT_MPOOL | DB_PRIVATE | DB_USE_ENVIRON, 0)) != 0) {
140 dbenv->err(dbenv, ret, "open");
144 if ((ret = db_create(&dbp, dbenv, 0)) != 0) {
146 "%s: db_create: %s\n", progname, db_strerror(ret));
150 /* This is the crux of the program. We need to make sure we verify
151 * using the correct sort routine. Else, the database will report
153 dbp->set_bt_compare(dbp, &compare_unix);
156 dbp->set_errfile(dbp, stderr);
157 dbp->set_errpfx(dbp, progname);
159 if ((ret = dbp->verify(dbp, dbname, NULL, NULL, 0)) != 0)
160 dbp->err(dbp, ret, "DB->verify: %s", dbname);
161 if ((t_ret = dbp->close(dbp, 0)) != 0 && ret == 0) {
162 dbp->err(dbp, ret, "DB->close: %s", dbname);
169 shutdown: exitval = 1;
171 if (e_close && (ret = dbenv->close(dbenv, 0)) != 0) {
174 "%s: dbenv->close: %s\n", progname, db_strerror(ret));
183 fprintf(stderr, "usage: db_verify [-NqV] [-h home] ...\n");
190 int v_major, v_minor, v_patch;
192 /* Make sure we're loaded with the right version of the DB library. */
193 (void)db_version(&v_major, &v_minor, &v_patch);
194 if (v_major != DB_VERSION_MAJOR ||
195 v_minor != DB_VERSION_MINOR || v_patch != DB_VERSION_PATCH) {
197 "%s: version %d.%d.%d doesn't match library version %d.%d.%d\n",
198 progname, DB_VERSION_MAJOR, DB_VERSION_MINOR,
199 DB_VERSION_PATCH, v_major, v_minor, v_patch);