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.4 2001-12-10 07:34:39 jmarcus Exp $
15 #endif /* HAVE_CONFIG_H */
22 #endif /* HAVE_UNISTD_H */
23 #include <sys/param.h>
28 #include <atalk/cnid.h>
29 #include <atalk/util.h>
32 #define MIN(a, b) ((a) < (b) ? (a) : (b))
35 #define DBDIDNAME "didname.db"
37 int main __P((int, char *[]));
38 void usage __P((void));
39 void version_check __P((void));
42 *progname = "cnid_didname_verify"; /* Program name. */
44 static __inline__ int compare_did(const DBT *a, const DBT *b) {
47 memcpy(&dida, a->data, sizeof(dida));
48 memcpy(&didb, b->data, sizeof(didb));
52 #if DB_VERSION_MINOR > 1
53 static int compare_unix(DB *db, const DBT *a, const DBT *b)
54 #else /* DB_VERSION_MINOR < 1 */
55 static int compare_unix(const DBT *a, const DBT *b)
56 #endif /* DB_VERSION_MINOR */
61 if ((ret = compare_did(a, b)))
64 sa = (u_int8_t *) a->data + 4;
65 sb = (u_int8_t *) b->data + 4;
66 for (len = MIN(a->size, b->size); len-- > 4; sa++, sb++)
67 if ((ret = (*sa - *sb)))
69 return a->size - b->size;
81 int ch, e_close, exitval, nflag, quiet, ret, t_ret;
87 e_close = exitval = nflag = quiet = 0;
89 while ((ch = getopt(argc, argv, "h:NqV")) != EOF)
96 if ((ret = db_env_set_panicstate(0)) != 0) {
98 "%s: db_env_set_panicstate: %s\n",
99 progname, db_strerror(ret));
108 printf("%s\n", db_version(NULL, NULL, NULL));
118 * Create an environment object and initialize it for error
121 if ((ret = db_env_create(&dbenv, 0)) != 0) {
122 fprintf(stderr, "%s: db_env_create: %s\n",
123 progname, db_strerror(ret));
130 * We'd prefer to have error output configured while calling
131 * db_env_create, but there's no way to turn it off once it's
135 dbenv->set_errfile(dbenv, stderr);
136 dbenv->set_errpfx(dbenv, progname);
139 if (nflag && (ret = dbenv->set_mutexlocks(dbenv, 0)) != 0) {
140 dbenv->err(dbenv, ret, "set_mutexlocks");
145 * Attach to an mpool if it exists, but if that fails, attach
146 * to a private region.
148 if ((ret = dbenv->open(dbenv,
149 home, DB_INIT_MPOOL | DB_USE_ENVIRON, 0)) != 0 &&
150 (ret = dbenv->open(dbenv, home,
151 DB_CREATE | DB_INIT_MPOOL | DB_PRIVATE | DB_USE_ENVIRON, 0)) != 0) {
152 dbenv->err(dbenv, ret, "open");
156 if ((ret = db_create(&dbp, dbenv, 0)) != 0) {
158 "%s: db_create: %s\n", progname, db_strerror(ret));
162 /* This is the crux of the program. We need to make sure we verify
163 * using the correct sort routine. Else, the database will report
165 dbp->set_bt_compare(dbp, &compare_unix);
168 dbp->set_errfile(dbp, stderr);
169 dbp->set_errpfx(dbp, progname);
171 if ((ret = dbp->verify(dbp, DBDIDNAME, NULL, NULL, 0)) != 0)
172 dbp->err(dbp, ret, "DB->verify: %s", DBDIDNAME);
173 if ((t_ret = dbp->close(dbp, 0)) != 0 && ret == 0) {
174 dbp->err(dbp, ret, "DB->close: %s", DBDIDNAME);
181 shutdown: exitval = 1;
183 if (e_close && (ret = dbenv->close(dbenv, 0)) != 0) {
186 "%s: dbenv->close: %s\n", progname, db_strerror(ret));
195 fprintf(stderr, "usage: db_verify [-NqV] [-h home] ...\n");
202 int v_major, v_minor, v_patch;
204 /* Make sure we're loaded with the right version of the DB library. */
205 (void)db_version(&v_major, &v_minor, &v_patch);
206 if (v_major != DB_VERSION_MAJOR ||
207 v_minor != DB_VERSION_MINOR || v_patch != DB_VERSION_PATCH) {
209 "%s: version %d.%d.%d doesn't match library version %d.%d.%d\n",
210 progname, DB_VERSION_MAJOR, DB_VERSION_MINOR,
211 DB_VERSION_PATCH, v_major, v_minor, v_patch);
221 printf("Netatalk must be compiled with Berkeley DB to use this program.\n");