]> arthur.barton.de Git - netatalk.git/blob - bin/cnid/cnid_didname_verify.c
Add a utility to check the consistency of didname.db. Using the stock
[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.1 2001-12-10 07:04:27 jmarcus Exp $
11  */
12
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <unistd.h>
17 #include <sys/types.h>
18 #include <db.h>
19
20 #ifndef MIN
21 #define MIN(a, b)  ((a) < (b) ? (a) : (b))
22 #endif /* ! MIN */
23
24 int     main __P((int, char *[]));
25 void    usage __P((void));
26 void    version_check __P((void));
27
28 const char
29         *progname = "db_verify";                        /* Program name. */
30
31 static __inline__ int compare_did(const DBT *a, const DBT *b) {
32         u_int32_t dida, didb;
33
34         memcpy(&dida, a->data, sizeof(dida));
35         memcpy(&didb, b->data, sizeof(didb));
36         return dida - didb;
37 }
38
39 #if DB_VERSION_MINOR > 1
40 static int compare_unix(DB *db, const DBT *a, const DBT *b)
41 #else
42 static int compare_unix(const DBT *a, const DBT *b)
43 #endif
44 {
45         u_int8_t *sa, *sb;
46         int len, ret;
47
48         if ((ret = compare_did(a, b)))
49                 return ret;
50         
51         sa = (u_int8_t *) a->data + 4;
52         sb = (u_int8_t *) b->data + 4;
53         for (len = MIN(a->size, b->size); len-- > 4; sa++, sb++)
54                 if ((ret = (*sa - *sb)))
55                         return ret;
56         return a->size - b->size;
57 }
58
59 int
60 main(argc, argv)
61         int argc;
62         char *argv[];
63 {
64         extern char *optarg;
65         extern int optind;
66         DB *dbp;
67         DB_ENV *dbenv;
68         int ch, e_close, exitval, nflag, quiet, ret, t_ret;
69         char *home;
70         char *dbname = "didname.db";
71
72         version_check();
73
74         dbenv = NULL;
75         e_close = exitval = nflag = quiet = 0;
76         home = NULL;
77         while ((ch = getopt(argc, argv, "h:NqV")) != EOF)
78                 switch (ch) {
79                 case 'h':
80                         home = optarg;
81                         break;
82                 case 'N':
83                         nflag = 1;
84                         if ((ret = db_env_set_panicstate(0)) != 0) {
85                                 fprintf(stderr,
86                                     "%s: db_env_set_panicstate: %s\n",
87                                     progname, db_strerror(ret));
88                                 exit (1);
89                         }
90                         break;
91                         break;
92                 case 'q':
93                         quiet = 1;
94                         break;
95                 case 'V':
96                         printf("%s\n", db_version(NULL, NULL, NULL));
97                         exit(0);
98                 case '?':
99                 default:
100                         usage();
101                 }
102         argc -= optind;
103         argv += optind;
104
105         /*
106          * Create an environment object and initialize it for error
107          * reporting.
108          */
109         if ((ret = db_env_create(&dbenv, 0)) != 0) {
110                 fprintf(stderr, "%s: db_env_create: %s\n",
111                     progname, db_strerror(ret));
112                 goto shutdown;
113         }
114         e_close = 1;
115
116         /*
117          * XXX
118          * We'd prefer to have error output configured while calling
119          * db_env_create, but there's no way to turn it off once it's
120          * turned on.
121          */
122         if (!quiet) {
123                 dbenv->set_errfile(dbenv, stderr);
124                 dbenv->set_errpfx(dbenv, progname);
125         }
126
127         if (nflag && (ret = dbenv->set_mutexlocks(dbenv, 0)) != 0) {
128                 dbenv->err(dbenv, ret, "set_mutexlocks");
129                 goto shutdown;
130         }
131
132         /*
133          * Attach to an mpool if it exists, but if that fails, attach
134          * to a private region.
135          */
136         if ((ret = dbenv->open(dbenv,
137             home, DB_INIT_MPOOL | DB_USE_ENVIRON, 0)) != 0 &&
138             (ret = dbenv->open(dbenv, home,
139             DB_CREATE | DB_INIT_MPOOL | DB_PRIVATE | DB_USE_ENVIRON, 0)) != 0) {
140                 dbenv->err(dbenv, ret, "open");
141                 goto shutdown;
142         }
143
144         if ((ret = db_create(&dbp, dbenv, 0)) != 0) {
145                 fprintf(stderr,
146                     "%s: db_create: %s\n", progname, db_strerror(ret));
147                 goto shutdown;
148         }
149
150         /* This is the crux of the program.  We need to make sure we verify
151          * using the correct sort routine.  Else, the database will report
152          * corruption. */
153         dbp->set_bt_compare(dbp, &compare_unix);
154
155         if (!quiet) {
156                 dbp->set_errfile(dbp, stderr);
157                 dbp->set_errpfx(dbp, progname);
158         }
159         if ((ret = dbp->verify(dbp, dbname, NULL, NULL, 0)) != 0)
160                 dbp->err(dbp, ret, "DB->verify: %s", dbname);
161         if ((t_ret = dbp->close(dbp, 0)) != 0 && ret == 0) {
162                 dbp->err(dbp, ret, "DB->close: %s", dbname);
163                 ret = t_ret;
164         }
165         if (ret != 0)
166                 goto shutdown;
167
168         if (0) {
169 shutdown:       exitval = 1;
170         }
171         if (e_close && (ret = dbenv->close(dbenv, 0)) != 0) {
172                 exitval = 1;
173                 fprintf(stderr,
174                     "%s: dbenv->close: %s\n", progname, db_strerror(ret));
175         }
176
177         return (exitval);
178 }
179
180 void
181 usage()
182 {
183         fprintf(stderr, "usage: db_verify [-NqV] [-h home] ...\n");
184         exit (1);
185 }
186
187 void
188 version_check()
189 {
190         int v_major, v_minor, v_patch;
191
192         /* Make sure we're loaded with the right version of the DB library. */
193         (void)db_version(&v_major, &v_minor, &v_patch);
194         if (v_major != DB_VERSION_MAJOR ||
195             v_minor != DB_VERSION_MINOR || v_patch != DB_VERSION_PATCH) {
196                 fprintf(stderr,
197         "%s: version %d.%d.%d doesn't match library version %d.%d.%d\n",
198                     progname, DB_VERSION_MAJOR, DB_VERSION_MINOR,
199                     DB_VERSION_PATCH, v_major, v_minor, v_patch);
200                 exit (1);
201         }
202 }