]> arthur.barton.de Git - netatalk.git/blob - bin/cnid/cnid_didname_verify.c
Make dbname into a #define just to make it easier to spot if it ever
[netatalk.git] / bin / cnid / cnid_didname_verify.c
1 /*-
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 1996, 1997, 1998, 1999, 2000
5  *      Sleepycat Software.  All rights reserved.
6  *
7  * Modified to check the consistency of didname.db by
8  * Joe Clarke <marcus@marcuscom.com>
9  *
10  * $Id: cnid_didname_verify.c,v 1.4 2001-12-10 07:34:39 jmarcus Exp $
11  */
12
13 #ifdef HAVE_CONFIG_H
14 #include "config.h"
15 #endif /* HAVE_CONFIG_H */
16
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #ifdef HAVE_UNISTD_H
21 #include <unistd.h>
22 #endif /* HAVE_UNISTD_H */
23 #include <sys/param.h>
24
25 #ifdef CNID_DB
26 #include <db.h>
27
28 #include <atalk/cnid.h>
29 #include <atalk/util.h>
30
31 #ifndef MIN
32 #define MIN(a, b)  ((a) < (b) ? (a) : (b))
33 #endif /* ! MIN */
34
35 #define DBDIDNAME "didname.db"
36
37 int     main __P((int, char *[]));
38 void    usage __P((void));
39 void    version_check __P((void));
40
41 const char
42 *progname = "cnid_didname_verify";                      /* Program name. */
43
44 static __inline__ int compare_did(const DBT *a, const DBT *b) {
45     u_int32_t dida, didb;
46
47     memcpy(&dida, a->data, sizeof(dida));
48     memcpy(&didb, b->data, sizeof(didb));
49     return dida - didb;
50 }
51
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 */
57 {
58     u_int8_t *sa, *sb;
59     int len, ret;
60
61     if ((ret = compare_did(a, b)))
62         return ret;
63
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)))
68             return ret;
69     return a->size - b->size;
70 }
71
72 int
73 main(argc, argv)
74 int argc;
75 char *argv[];
76 {
77     extern char *optarg;
78     extern int optind;
79     DB *dbp;
80     DB_ENV *dbenv;
81     int ch, e_close, exitval, nflag, quiet, ret, t_ret;
82     char *home;
83
84     version_check();
85
86     dbenv = NULL;
87     e_close = exitval = nflag = quiet = 0;
88     home = NULL;
89     while ((ch = getopt(argc, argv, "h:NqV")) != EOF)
90         switch (ch) {
91         case 'h':
92             home = optarg;
93             break;
94         case 'N':
95             nflag = 1;
96             if ((ret = db_env_set_panicstate(0)) != 0) {
97                 fprintf(stderr,
98                         "%s: db_env_set_panicstate: %s\n",
99                         progname, db_strerror(ret));
100                 exit (1);
101             }
102             break;
103             break;
104         case 'q':
105             quiet = 1;
106             break;
107         case 'V':
108             printf("%s\n", db_version(NULL, NULL, NULL));
109             exit(0);
110         case '?':
111         default:
112             usage();
113         }
114     argc -= optind;
115     argv += optind;
116
117     /*
118      * Create an environment object and initialize it for error
119      * reporting.
120      */
121     if ((ret = db_env_create(&dbenv, 0)) != 0) {
122         fprintf(stderr, "%s: db_env_create: %s\n",
123                 progname, db_strerror(ret));
124         goto shutdown;
125     }
126     e_close = 1;
127
128     /*
129      * XXX
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
132      * turned on.
133      */
134     if (!quiet) {
135         dbenv->set_errfile(dbenv, stderr);
136         dbenv->set_errpfx(dbenv, progname);
137     }
138
139     if (nflag && (ret = dbenv->set_mutexlocks(dbenv, 0)) != 0) {
140         dbenv->err(dbenv, ret, "set_mutexlocks");
141         goto shutdown;
142     }
143
144     /*
145      * Attach to an mpool if it exists, but if that fails, attach
146      * to a private region.
147      */
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");
153         goto shutdown;
154     }
155
156     if ((ret = db_create(&dbp, dbenv, 0)) != 0) {
157         fprintf(stderr,
158                 "%s: db_create: %s\n", progname, db_strerror(ret));
159         goto shutdown;
160     }
161
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
164      * corruption. */
165     dbp->set_bt_compare(dbp, &compare_unix);
166
167     if (!quiet) {
168         dbp->set_errfile(dbp, stderr);
169         dbp->set_errpfx(dbp, progname);
170     }
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);
175         ret = t_ret;
176     }
177     if (ret != 0)
178         goto shutdown;
179
180     if (0) {
181 shutdown:       exitval = 1;
182     }
183     if (e_close && (ret = dbenv->close(dbenv, 0)) != 0) {
184         exitval = 1;
185         fprintf(stderr,
186                 "%s: dbenv->close: %s\n", progname, db_strerror(ret));
187     }
188
189     return (exitval);
190 }
191
192 void
193 usage()
194 {
195     fprintf(stderr, "usage: db_verify [-NqV] [-h home] ...\n");
196     exit (1);
197 }
198
199 void
200 version_check()
201 {
202     int v_major, v_minor, v_patch;
203
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) {
208         fprintf(stderr,
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);
212         exit (1);
213     }
214 }
215 #else /* CNID_DB */
216 int
217 main(argc, argv)
218 int argc;
219 char *argv[];
220 {
221     printf("Netatalk must be compiled with Berkeley DB to use this program.\n");
222
223     return 0;
224 }
225 #endif /* CNID_DB */