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