00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #include "db_config.h"
00011
00012 #ifndef lint
00013 static const char copyright[] =
00014 "Copyright (c) 1996-2005\nSleepycat Software Inc. All rights reserved.\n";
00015 #endif
00016
00017 #ifndef NO_SYSTEM_INCLUDES
00018 #include <sys/types.h>
00019
00020 #include <stdio.h>
00021 #include <stdlib.h>
00022 #include <string.h>
00023 #include <unistd.h>
00024 #endif
00025
00026 #include "db_int.h"
00027
00028 int main __P((int, char *[]));
00029 int usage __P((void));
00030 int version_check __P((void));
00031
00032 const char *progname;
00033
00034 int
00035 main(argc, argv)
00036 int argc;
00037 char *argv[];
00038 {
00039 extern char *optarg;
00040 extern int optind;
00041 DB *dbp, *dbp1;
00042 DB_ENV *dbenv;
00043 u_int32_t flags, cache;
00044 int ch, exitval, nflag, private;
00045 int quiet, resize, ret;
00046 char *home, *passwd;
00047
00048 if ((progname = strrchr(argv[0], '/')) == NULL)
00049 progname = argv[0];
00050 else
00051 ++progname;
00052
00053 if ((ret = version_check()) != 0)
00054 return (ret);
00055
00056 dbenv = NULL;
00057 dbp = NULL;
00058 cache = MEGABYTE;
00059 exitval = nflag = quiet = 0;
00060 flags = 0;
00061 home = passwd = NULL;
00062 while ((ch = getopt(argc, argv, "h:NoP:quV")) != EOF)
00063 switch (ch) {
00064 case 'h':
00065 home = optarg;
00066 break;
00067 case 'N':
00068 nflag = 1;
00069 break;
00070 case 'P':
00071 passwd = strdup(optarg);
00072 memset(optarg, 0, strlen(optarg));
00073 if (passwd == NULL) {
00074 fprintf(stderr, "%s: strdup: %s\n",
00075 progname, strerror(errno));
00076 return (EXIT_FAILURE);
00077 }
00078 break;
00079 case 'o':
00080 LF_SET(DB_NOORDERCHK);
00081 break;
00082 case 'q':
00083 quiet = 1;
00084 break;
00085 case 'u':
00086 LF_SET(DB_UNREF);
00087 break;
00088 case 'V':
00089 printf("%s\n", db_version(NULL, NULL, NULL));
00090 return (EXIT_SUCCESS);
00091 case '?':
00092 default:
00093 return (usage());
00094 }
00095 argc -= optind;
00096 argv += optind;
00097
00098 if (argc <= 0)
00099 return (usage());
00100
00101
00102 __db_util_siginit();
00103
00104
00105
00106
00107
00108 retry: if ((ret = db_env_create(&dbenv, 0)) != 0) {
00109 fprintf(stderr,
00110 "%s: db_env_create: %s\n", progname, db_strerror(ret));
00111 goto shutdown;
00112 }
00113
00114 if (!quiet) {
00115 dbenv->set_errfile(dbenv, stderr);
00116 dbenv->set_errpfx(dbenv, progname);
00117 }
00118
00119 if (nflag) {
00120 if ((ret = dbenv->set_flags(dbenv, DB_NOLOCKING, 1)) != 0) {
00121 dbenv->err(dbenv, ret, "set_flags: DB_NOLOCKING");
00122 goto shutdown;
00123 }
00124 if ((ret = dbenv->set_flags(dbenv, DB_NOPANIC, 1)) != 0) {
00125 dbenv->err(dbenv, ret, "set_flags: DB_NOPANIC");
00126 goto shutdown;
00127 }
00128 }
00129
00130 if (passwd != NULL &&
00131 (ret = dbenv->set_encrypt(dbenv, passwd, DB_ENCRYPT_AES)) != 0) {
00132 dbenv->err(dbenv, ret, "set_passwd");
00133 goto shutdown;
00134 }
00135
00136
00137
00138
00139
00140 private = 0;
00141 if ((ret =
00142 dbenv->open(dbenv, home, DB_INIT_MPOOL | DB_USE_ENVIRON, 0)) != 0) {
00143 if (ret != DB_VERSION_MISMATCH) {
00144 if ((ret =
00145 dbenv->set_cachesize(dbenv, 0, cache, 1)) != 0) {
00146 dbenv->err(dbenv, ret, "set_cachesize");
00147 goto shutdown;
00148 }
00149 private = 1;
00150 ret = dbenv->open(dbenv, home, DB_CREATE |
00151 DB_INIT_MPOOL | DB_PRIVATE | DB_USE_ENVIRON, 0);
00152 }
00153 if (ret != 0) {
00154 dbenv->err(dbenv, ret, "DB_ENV->open");
00155 goto shutdown;
00156 }
00157 }
00158
00159 for (; !__db_util_interrupted() && argv[0] != NULL; ++argv) {
00160 if ((ret = db_create(&dbp, dbenv, 0)) != 0) {
00161 dbenv->err(dbenv, ret, "%s: db_create", progname);
00162 goto shutdown;
00163 }
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173 if (private) {
00174 if ((ret = db_create(&dbp1, dbenv, 0)) != 0) {
00175 dbenv->err(
00176 dbenv, ret, "%s: db_create", progname);
00177 goto shutdown;
00178 }
00179
00180 ret = dbp1->open(dbp1,
00181 NULL, argv[0], NULL, DB_UNKNOWN, DB_RDONLY, 0);
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191 if (ret == 0) {
00192 if (__db_util_cache(
00193 dbp1, &cache, &resize) == 0 && resize) {
00194 (void)dbp1->close(dbp1, 0);
00195 (void)dbp->close(dbp, 0);
00196 dbp = NULL;
00197
00198 (void)dbenv->close(dbenv, 0);
00199 dbenv = NULL;
00200 goto retry;
00201 }
00202 }
00203 (void)dbp1->close(dbp1, 0);
00204 }
00205
00206
00207 ret = dbp->verify(dbp, argv[0], NULL, NULL, flags);
00208 dbp = NULL;
00209 if (ret != 0)
00210 goto shutdown;
00211 }
00212
00213 if (0) {
00214 shutdown: exitval = 1;
00215 }
00216
00217 if (dbp != NULL && (ret = dbp->close(dbp, 0)) != 0) {
00218 exitval = 1;
00219 dbenv->err(dbenv, ret, "close");
00220 }
00221 if (dbenv != NULL && (ret = dbenv->close(dbenv, 0)) != 0) {
00222 exitval = 1;
00223 fprintf(stderr,
00224 "%s: dbenv->close: %s\n", progname, db_strerror(ret));
00225 }
00226
00227 if (passwd != NULL)
00228 free(passwd);
00229
00230
00231 __db_util_sigresend();
00232
00233 return (exitval == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
00234 }
00235
00236 int
00237 usage()
00238 {
00239 fprintf(stderr, "usage: %s %s\n", progname,
00240 "[-NoqV] [-h home] [-P password] db_file ...");
00241 return (EXIT_FAILURE);
00242 }
00243
00244 int
00245 version_check()
00246 {
00247 int v_major, v_minor, v_patch;
00248
00249
00250 (void)db_version(&v_major, &v_minor, &v_patch);
00251 if (v_major != DB_VERSION_MAJOR || v_minor != DB_VERSION_MINOR) {
00252 fprintf(stderr,
00253 "%s: version %d.%d doesn't match library version %d.%d\n",
00254 progname, DB_VERSION_MAJOR, DB_VERSION_MINOR,
00255 v_major, v_minor);
00256 return (EXIT_FAILURE);
00257 }
00258 return (0);
00259 }