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.2 2001-12-10 07:20:38 jmarcus Exp $
15 #endif /* HAVE_CONFIG_H */
23 #endif /* HAVE_UNISTD_H */
24 #include <sys/param.h>
28 #include <atalk/cnid.h>
29 #include <atalk/util.h>
32 #define MIN(a, b) ((a) < (b) ? (a) : (b))
35 int main __P((int, char *[]));
36 void usage __P((void));
37 void version_check __P((void));
40 *progname = "cnid_didname_verify"; /* Program name. */
42 static __inline__ int compare_did(const DBT *a, const DBT *b) {
45 memcpy(&dida, a->data, sizeof(dida));
46 memcpy(&didb, b->data, sizeof(didb));
50 #if DB_VERSION_MINOR > 1
51 static int compare_unix(DB *db, const DBT *a, const DBT *b)
53 static int compare_unix(const DBT *a, const DBT *b)
59 if ((ret = compare_did(a, b)))
62 sa = (u_int8_t *) a->data + 4;
63 sb = (u_int8_t *) b->data + 4;
64 for (len = MIN(a->size, b->size); len-- > 4; sa++, sb++)
65 if ((ret = (*sa - *sb)))
67 return a->size - b->size;
79 int ch, e_close, exitval, nflag, quiet, ret, t_ret;
81 char *dbname = "didname.db";
86 e_close = exitval = nflag = quiet = 0;
88 while ((ch = getopt(argc, argv, "h:NqV")) != EOF)
95 if ((ret = db_env_set_panicstate(0)) != 0) {
97 "%s: db_env_set_panicstate: %s\n",
98 progname, db_strerror(ret));
107 printf("%s\n", db_version(NULL, NULL, NULL));
117 * Create an environment object and initialize it for error
120 if ((ret = db_env_create(&dbenv, 0)) != 0) {
121 fprintf(stderr, "%s: db_env_create: %s\n",
122 progname, db_strerror(ret));
129 * We'd prefer to have error output configured while calling
130 * db_env_create, but there's no way to turn it off once it's
134 dbenv->set_errfile(dbenv, stderr);
135 dbenv->set_errpfx(dbenv, progname);
138 if (nflag && (ret = dbenv->set_mutexlocks(dbenv, 0)) != 0) {
139 dbenv->err(dbenv, ret, "set_mutexlocks");
144 * Attach to an mpool if it exists, but if that fails, attach
145 * to a private region.
147 if ((ret = dbenv->open(dbenv,
148 home, DB_INIT_MPOOL | DB_USE_ENVIRON, 0)) != 0 &&
149 (ret = dbenv->open(dbenv, home,
150 DB_CREATE | DB_INIT_MPOOL | DB_PRIVATE | DB_USE_ENVIRON, 0)) != 0) {
151 dbenv->err(dbenv, ret, "open");
155 if ((ret = db_create(&dbp, dbenv, 0)) != 0) {
157 "%s: db_create: %s\n", progname, db_strerror(ret));
161 /* This is the crux of the program. We need to make sure we verify
162 * using the correct sort routine. Else, the database will report
164 dbp->set_bt_compare(dbp, &compare_unix);
167 dbp->set_errfile(dbp, stderr);
168 dbp->set_errpfx(dbp, progname);
170 if ((ret = dbp->verify(dbp, dbname, NULL, NULL, 0)) != 0)
171 dbp->err(dbp, ret, "DB->verify: %s", dbname);
172 if ((t_ret = dbp->close(dbp, 0)) != 0 && ret == 0) {
173 dbp->err(dbp, ret, "DB->close: %s", dbname);
180 shutdown: exitval = 1;
182 if (e_close && (ret = dbenv->close(dbenv, 0)) != 0) {
185 "%s: dbenv->close: %s\n", progname, db_strerror(ret));
194 fprintf(stderr, "usage: db_verify [-NqV] [-h home] ...\n");
201 int v_major, v_minor, v_patch;
203 /* Make sure we're loaded with the right version of the DB library. */
204 (void)db_version(&v_major, &v_minor, &v_patch);
205 if (v_major != DB_VERSION_MAJOR ||
206 v_minor != DB_VERSION_MINOR || v_patch != DB_VERSION_PATCH) {
208 "%s: version %d.%d.%d doesn't match library version %d.%d.%d\n",
209 progname, DB_VERSION_MAJOR, DB_VERSION_MINOR,
210 DB_VERSION_PATCH, v_major, v_minor, v_patch);