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.7 2001-12-16 18:39:34 srittau Exp $
15 #endif /* HAVE_CONFIG_H */
22 #endif /* HAVE_UNISTD_H */
23 #include <sys/param.h>
27 #include <atalk/cnid.h>
28 #include <atalk/util.h>
31 #define MIN(a, b) ((a) < (b) ? (a) : (b))
34 #define DBDIDNAME "didname.db"
36 int main __P((int, char *[]));
37 void usage __P((void));
38 void version_check __P((void));
41 *progname = "cnid_didname_verify"; /* Program name. */
43 static __inline__ int compare_did(const DBT *a, const DBT *b) {
46 memcpy(&dida, a->data, sizeof(dida));
47 memcpy(&didb, b->data, sizeof(didb));
51 #if DB_VERSION_MINOR > 1
52 static int compare_unix(DB *db, const DBT *a, const DBT *b)
53 #else /* DB_VERSION_MINOR < 1 */
54 static int compare_unix(const DBT *a, const DBT *b)
55 #endif /* DB_VERSION_MINOR */
60 if ((ret = compare_did(a, b)))
63 sa = (u_int8_t *) a->data + 4;
64 sb = (u_int8_t *) b->data + 4;
65 for (len = MIN(a->size, b->size); len-- > 4; sa++, sb++)
66 if ((ret = (*sa - *sb)))
68 return a->size - b->size;
80 int ch, e_close, exitval, nflag, quiet, ret, t_ret;
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 DB_VERSION_MINOR > 1
139 if (nflag && (ret = dbenv->set_mutexlocks(dbenv, 0)) != 0) {
140 dbenv->err(dbenv, ret, "set_mutexlocks");
143 #else /* DB_VERSION_MINOR < 1 */
144 if (nflag && (ret = db_env_set_mutexlocks(0)) != 0) {
145 dbenv->err(dbenv, ret, "db_env_set_mutexlocks");
148 #endif /* DB_VERSION_MINOR */
151 * Attach to an mpool if it exists, but if that fails, attach
152 * to a private region.
154 if ((ret = dbenv->open(dbenv,
155 home, DB_INIT_MPOOL | DB_USE_ENVIRON, 0)) != 0 &&
156 (ret = dbenv->open(dbenv, home,
157 DB_CREATE | DB_INIT_MPOOL | DB_PRIVATE | DB_USE_ENVIRON, 0)) != 0) {
158 dbenv->err(dbenv, ret, "open");
162 if ((ret = db_create(&dbp, dbenv, 0)) != 0) {
164 "%s: db_create: %s\n", progname, db_strerror(ret));
168 /* This is the crux of the program. We need to make sure we verify
169 * using the correct sort routine. Else, the database will report
171 dbp->set_bt_compare(dbp, &compare_unix);
174 dbp->set_errfile(dbp, stderr);
175 dbp->set_errpfx(dbp, progname);
177 if ((ret = dbp->verify(dbp, DBDIDNAME, NULL, NULL, 0)) != 0)
178 dbp->err(dbp, ret, "DB->verify: %s", DBDIDNAME);
179 if ((t_ret = dbp->close(dbp, 0)) != 0 && ret == 0) {
180 dbp->err(dbp, ret, "DB->close: %s", DBDIDNAME);
187 shutdown: exitval = 1;
189 if (e_close && (ret = dbenv->close(dbenv, 0)) != 0) {
192 "%s: dbenv->close: %s\n", progname, db_strerror(ret));
201 fprintf(stderr, "usage: db_verify [-NqV] [-h home] ...\n");
208 int v_major, v_minor, v_patch;
210 /* Make sure we're loaded with the right version of the DB library. */
211 (void)db_version(&v_major, &v_minor, &v_patch);
212 if (v_major != DB_VERSION_MAJOR ||
213 v_minor != DB_VERSION_MINOR || v_patch != DB_VERSION_PATCH) {
215 "%s: version %d.%d.%d doesn't match library version %d.%d.%d\n",
216 progname, DB_VERSION_MAJOR, DB_VERSION_MINOR,
217 DB_VERSION_PATCH, v_major, v_minor, v_patch);