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.6 2001-12-10 23:45:53 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 DB_VERSION_MINOR > 1
140 if (nflag && (ret = dbenv->set_mutexlocks(dbenv, 0)) != 0) {
141 dbenv->err(dbenv, ret, "set_mutexlocks");
144 #else /* DB_VERSION_MINOR < 1 */
145 if (nflag && (ret = db_env_set_mutexlocks(0)) != 0) {
146 dbenv->err(dbenv, ret, "db_env_set_mutexlocks");
149 #endif /* DB_VERSION_MINOR */
152 * Attach to an mpool if it exists, but if that fails, attach
153 * to a private region.
155 if ((ret = dbenv->open(dbenv,
156 home, DB_INIT_MPOOL | DB_USE_ENVIRON, 0)) != 0 &&
157 (ret = dbenv->open(dbenv, home,
158 DB_CREATE | DB_INIT_MPOOL | DB_PRIVATE | DB_USE_ENVIRON, 0)) != 0) {
159 dbenv->err(dbenv, ret, "open");
163 if ((ret = db_create(&dbp, dbenv, 0)) != 0) {
165 "%s: db_create: %s\n", progname, db_strerror(ret));
169 /* This is the crux of the program. We need to make sure we verify
170 * using the correct sort routine. Else, the database will report
172 dbp->set_bt_compare(dbp, &compare_unix);
175 dbp->set_errfile(dbp, stderr);
176 dbp->set_errpfx(dbp, progname);
178 if ((ret = dbp->verify(dbp, DBDIDNAME, NULL, NULL, 0)) != 0)
179 dbp->err(dbp, ret, "DB->verify: %s", DBDIDNAME);
180 if ((t_ret = dbp->close(dbp, 0)) != 0 && ret == 0) {
181 dbp->err(dbp, ret, "DB->close: %s", DBDIDNAME);
188 shutdown: exitval = 1;
190 if (e_close && (ret = dbenv->close(dbenv, 0)) != 0) {
193 "%s: dbenv->close: %s\n", progname, db_strerror(ret));
202 fprintf(stderr, "usage: db_verify [-NqV] [-h home] ...\n");
209 int v_major, v_minor, v_patch;
211 /* Make sure we're loaded with the right version of the DB library. */
212 (void)db_version(&v_major, &v_minor, &v_patch);
213 if (v_major != DB_VERSION_MAJOR ||
214 v_minor != DB_VERSION_MINOR || v_patch != DB_VERSION_PATCH) {
216 "%s: version %d.%d.%d doesn't match library version %d.%d.%d\n",
217 progname, DB_VERSION_MAJOR, DB_VERSION_MINOR,
218 DB_VERSION_PATCH, v_major, v_minor, v_patch);
228 printf("Netatalk must be compiled with Berkeley DB to use this program.\n");