]> arthur.barton.de Git - netatalk.git/blob - bin/cnid/cnid_didname_verify.c
Fix braino in last commit.
[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.6 2001-12-10 23:45:53 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 DB_VERSION_MINOR > 1
140     if (nflag && (ret = dbenv->set_mutexlocks(dbenv, 0)) != 0) {
141         dbenv->err(dbenv, ret, "set_mutexlocks");
142         goto shutdown;
143     }
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");
147         goto shutdown;
148     }
149 #endif /* DB_VERSION_MINOR */
150
151     /*
152      * Attach to an mpool if it exists, but if that fails, attach
153      * to a private region.
154      */
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");
160         goto shutdown;
161     }
162
163     if ((ret = db_create(&dbp, dbenv, 0)) != 0) {
164         fprintf(stderr,
165                 "%s: db_create: %s\n", progname, db_strerror(ret));
166         goto shutdown;
167     }
168
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
171      * corruption. */
172     dbp->set_bt_compare(dbp, &compare_unix);
173
174     if (!quiet) {
175         dbp->set_errfile(dbp, stderr);
176         dbp->set_errpfx(dbp, progname);
177     }
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);
182         ret = t_ret;
183     }
184     if (ret != 0)
185         goto shutdown;
186
187     if (0) {
188 shutdown:       exitval = 1;
189     }
190     if (e_close && (ret = dbenv->close(dbenv, 0)) != 0) {
191         exitval = 1;
192         fprintf(stderr,
193                 "%s: dbenv->close: %s\n", progname, db_strerror(ret));
194     }
195
196     return (exitval);
197 }
198
199 void
200 usage()
201 {
202     fprintf(stderr, "usage: db_verify [-NqV] [-h home] ...\n");
203     exit (1);
204 }
205
206 void
207 version_check()
208 {
209     int v_major, v_minor, v_patch;
210
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) {
215         fprintf(stderr,
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);
219         exit (1);
220     }
221 }
222 #else /* CNID_DB */
223 int
224 main(argc, argv)
225 int argc;
226 char *argv[];
227 {
228     printf("Netatalk must be compiled with Berkeley DB to use this program.\n");
229
230     return 0;
231 }
232 #endif /* CNID_DB */