1/*- 2 * See the file LICENSE for redistribution information. 3 * 4 * Copyright (c) 1996,2008 Oracle. All rights reserved. 5 * 6 * $Id: db_dump.c,v 12.16 2008/01/08 20:58:13 bostic Exp $ 7 */ 8 9#include "db_config.h" 10 11#include "db_int.h" 12#include "dbinc/db_page.h" 13#include "dbinc/db_am.h" 14 15#ifndef lint 16static const char copyright[] = 17 "Copyright (c) 1996,2008 Oracle. All rights reserved.\n"; 18#endif 19 20int db_init __P((DB_ENV *, char *, int, u_int32_t, int *)); 21int dump_sub __P((DB_ENV *, DB *, char *, int, int)); 22int main __P((int, char *[])); 23int show_subs __P((DB *)); 24int usage __P((void)); 25int version_check __P((void)); 26 27const char *progname; 28 29int 30main(argc, argv) 31 int argc; 32 char *argv[]; 33{ 34 extern char *optarg; 35 extern int optind; 36 DB_ENV *dbenv; 37 DB *dbp; 38 u_int32_t cache; 39 int ch; 40 int exitval, keyflag, lflag, nflag, pflag, private; 41 int ret, Rflag, rflag, resize; 42 char *dopt, *home, *passwd, *subname; 43 44 if ((progname = __db_rpath(argv[0])) == NULL) 45 progname = argv[0]; 46 else 47 ++progname; 48 49 if ((ret = version_check()) != 0) 50 return (ret); 51 52 dbenv = NULL; 53 dbp = NULL; 54 exitval = lflag = nflag = pflag = rflag = Rflag = 0; 55 keyflag = 0; 56 cache = MEGABYTE; 57 private = 0; 58 dopt = home = passwd = subname = NULL; 59 while ((ch = getopt(argc, argv, "d:f:h:klNpP:rRs:V")) != EOF) 60 switch (ch) { 61 case 'd': 62 dopt = optarg; 63 break; 64 case 'f': 65 if (freopen(optarg, "w", stdout) == NULL) { 66 fprintf(stderr, "%s: %s: reopen: %s\n", 67 progname, optarg, strerror(errno)); 68 return (EXIT_FAILURE); 69 } 70 break; 71 case 'h': 72 home = optarg; 73 break; 74 case 'k': 75 keyflag = 1; 76 break; 77 case 'l': 78 lflag = 1; 79 break; 80 case 'N': 81 nflag = 1; 82 break; 83 case 'P': 84 passwd = strdup(optarg); 85 memset(optarg, 0, strlen(optarg)); 86 if (passwd == NULL) { 87 fprintf(stderr, "%s: strdup: %s\n", 88 progname, strerror(errno)); 89 return (EXIT_FAILURE); 90 } 91 break; 92 case 'p': 93 pflag = 1; 94 break; 95 case 's': 96 subname = optarg; 97 break; 98 case 'R': 99 Rflag = 1; 100 /* DB_AGGRESSIVE requires DB_SALVAGE */ 101 /* FALLTHROUGH */ 102 case 'r': 103 rflag = 1; 104 break; 105 case 'V': 106 printf("%s\n", db_version(NULL, NULL, NULL)); 107 return (EXIT_SUCCESS); 108 case '?': 109 default: 110 return (usage()); 111 } 112 argc -= optind; 113 argv += optind; 114 115 if (argc != 1) 116 return (usage()); 117 118 if (dopt != NULL && pflag) { 119 fprintf(stderr, 120 "%s: the -d and -p options may not both be specified\n", 121 progname); 122 return (EXIT_FAILURE); 123 } 124 if (lflag && subname != NULL) { 125 fprintf(stderr, 126 "%s: the -l and -s options may not both be specified\n", 127 progname); 128 return (EXIT_FAILURE); 129 } 130 131 if (keyflag && rflag) { 132 fprintf(stderr, "%s: %s", 133 "the -k and -r or -R options may not both be specified\n", 134 progname); 135 return (EXIT_FAILURE); 136 } 137 138 if (subname != NULL && rflag) { 139 fprintf(stderr, "%s: %s", 140 "the -s and -r or R options may not both be specified\n", 141 progname); 142 return (EXIT_FAILURE); 143 } 144 145 /* Handle possible interruptions. */ 146 __db_util_siginit(); 147 148 /* 149 * Create an environment object and initialize it for error 150 * reporting. 151 */ 152retry: if ((ret = db_env_create(&dbenv, 0)) != 0) { 153 fprintf(stderr, 154 "%s: db_env_create: %s\n", progname, db_strerror(ret)); 155 goto err; 156 } 157 158 dbenv->set_errfile(dbenv, stderr); 159 dbenv->set_errpfx(dbenv, progname); 160 if (nflag) { 161 if ((ret = dbenv->set_flags(dbenv, DB_NOLOCKING, 1)) != 0) { 162 dbenv->err(dbenv, ret, "set_flags: DB_NOLOCKING"); 163 goto err; 164 } 165 if ((ret = dbenv->set_flags(dbenv, DB_NOPANIC, 1)) != 0) { 166 dbenv->err(dbenv, ret, "set_flags: DB_NOPANIC"); 167 goto err; 168 } 169 } 170 if (passwd != NULL && (ret = dbenv->set_encrypt(dbenv, 171 passwd, DB_ENCRYPT_AES)) != 0) { 172 dbenv->err(dbenv, ret, "set_passwd"); 173 goto err; 174 } 175 176 /* Initialize the environment. */ 177 if (db_init(dbenv, home, rflag, cache, &private) != 0) 178 goto err; 179 180 /* Create the DB object and open the file. */ 181 if ((ret = db_create(&dbp, dbenv, 0)) != 0) { 182 dbenv->err(dbenv, ret, "db_create"); 183 goto err; 184 } 185 186 /* 187 * If we're salvaging, don't do an open; it might not be safe. 188 * Dispatch now into the salvager. 189 */ 190 if (rflag) { 191 /* The verify method is a destructor. */ 192 ret = dbp->verify(dbp, argv[0], NULL, stdout, 193 DB_SALVAGE | 194 (Rflag ? DB_AGGRESSIVE : 0) | 195 (pflag ? DB_PRINTABLE : 0)); 196 dbp = NULL; 197 if (ret != 0) 198 goto err; 199 goto done; 200 } 201 202 if ((ret = dbp->open(dbp, NULL, 203 argv[0], subname, DB_UNKNOWN, DB_RDONLY, 0)) != 0) { 204 dbp->err(dbp, ret, "open: %s", argv[0]); 205 goto err; 206 } 207 if (private != 0) { 208 if ((ret = __db_util_cache(dbp, &cache, &resize)) != 0) 209 goto err; 210 if (resize) { 211 (void)dbp->close(dbp, 0); 212 dbp = NULL; 213 214 (void)dbenv->close(dbenv, 0); 215 dbenv = NULL; 216 goto retry; 217 } 218 } 219 220 if (dopt != NULL) { 221 if ((ret = __db_dumptree(dbp, NULL, dopt, NULL)) != 0) { 222 dbp->err(dbp, ret, "__db_dumptree: %s", argv[0]); 223 goto err; 224 } 225 } else if (lflag) { 226 if (dbp->get_multiple(dbp)) { 227 if (show_subs(dbp)) 228 goto err; 229 } else { 230 dbp->errx(dbp, 231 "%s: does not contain multiple databases", argv[0]); 232 goto err; 233 } 234 } else { 235 if (subname == NULL && dbp->get_multiple(dbp)) { 236 if (dump_sub(dbenv, dbp, argv[0], pflag, keyflag)) 237 goto err; 238 } else 239 if (dbp->dump(dbp, NULL, 240 __db_pr_callback, stdout, pflag, keyflag)) 241 goto err; 242 } 243 244 if (0) { 245err: exitval = 1; 246 } 247done: if (dbp != NULL && (ret = dbp->close(dbp, 0)) != 0) { 248 exitval = 1; 249 dbenv->err(dbenv, ret, "close"); 250 } 251 if (dbenv != NULL && (ret = dbenv->close(dbenv, 0)) != 0) { 252 exitval = 1; 253 fprintf(stderr, 254 "%s: dbenv->close: %s\n", progname, db_strerror(ret)); 255 } 256 257 if (passwd != NULL) 258 free(passwd); 259 260 /* Resend any caught signal. */ 261 __db_util_sigresend(); 262 263 return (exitval == 0 ? EXIT_SUCCESS : EXIT_FAILURE); 264} 265 266/* 267 * db_init -- 268 * Initialize the environment. 269 */ 270int 271db_init(dbenv, home, is_salvage, cache, is_privatep) 272 DB_ENV *dbenv; 273 char *home; 274 int is_salvage; 275 u_int32_t cache; 276 int *is_privatep; 277{ 278 int ret; 279 280 /* 281 * Try and use the underlying environment when opening a database. 282 * We wish to use the buffer pool so our information is as up-to-date 283 * as possible, even if the mpool cache hasn't been flushed. 284 * 285 * If we are not doing a salvage, we want to join the environment; 286 * if a locking system is present, this will let us use it and be 287 * safe to run concurrently with other threads of control. (We never 288 * need to use transactions explicitly, as we're read-only.) Note 289 * that in CDB, too, this will configure our environment 290 * appropriately, and our cursors will (correctly) do locking as CDB 291 * read cursors. 292 * 293 * If we are doing a salvage, the verification code will protest 294 * if we initialize transactions, logging, or locking; do an 295 * explicit DB_INIT_MPOOL to try to join any existing environment 296 * before we create our own. 297 */ 298 *is_privatep = 0; 299 if ((ret = dbenv->open(dbenv, home, 300 DB_USE_ENVIRON | (is_salvage ? DB_INIT_MPOOL : 0), 0)) == 0) 301 return (0); 302 if (ret == DB_VERSION_MISMATCH) 303 goto err; 304 305 /* 306 * An environment is required because we may be trying to look at 307 * databases in directories other than the current one. We could 308 * avoid using an environment iff the -h option wasn't specified, 309 * but that seems like more work than it's worth. 310 * 311 * No environment exists (or, at least no environment that includes 312 * an mpool region exists). Create one, but make it private so that 313 * no files are actually created. 314 */ 315 *is_privatep = 1; 316 if ((ret = dbenv->set_cachesize(dbenv, 0, cache, 1)) == 0 && 317 (ret = dbenv->open(dbenv, home, 318 DB_CREATE | DB_INIT_MPOOL | DB_PRIVATE | DB_USE_ENVIRON, 0)) == 0) 319 return (0); 320 321 /* An environment is required. */ 322err: dbenv->err(dbenv, ret, "DB_ENV->open"); 323 return (1); 324} 325 326/* 327 * dump_sub -- 328 * Dump out the records for a DB containing subdatabases. 329 */ 330int 331dump_sub(dbenv, parent_dbp, parent_name, pflag, keyflag) 332 DB_ENV *dbenv; 333 DB *parent_dbp; 334 char *parent_name; 335 int pflag, keyflag; 336{ 337 DB *dbp; 338 DBC *dbcp; 339 DBT key, data; 340 int ret; 341 char *subdb; 342 343 /* 344 * Get a cursor and step through the database, dumping out each 345 * subdatabase. 346 */ 347 if ((ret = parent_dbp->cursor(parent_dbp, NULL, &dbcp, 0)) != 0) { 348 dbenv->err(dbenv, ret, "DB->cursor"); 349 return (1); 350 } 351 352 memset(&key, 0, sizeof(key)); 353 memset(&data, 0, sizeof(data)); 354 while ((ret = dbcp->get(dbcp, &key, &data, 355 DB_IGNORE_LEASE | DB_NEXT)) == 0) { 356 /* Nul terminate the subdatabase name. */ 357 if ((subdb = malloc(key.size + 1)) == NULL) { 358 dbenv->err(dbenv, ENOMEM, NULL); 359 return (1); 360 } 361 memcpy(subdb, key.data, key.size); 362 subdb[key.size] = '\0'; 363 364 /* Create the DB object and open the file. */ 365 if ((ret = db_create(&dbp, dbenv, 0)) != 0) { 366 dbenv->err(dbenv, ret, "db_create"); 367 free(subdb); 368 return (1); 369 } 370 if ((ret = dbp->open(dbp, NULL, 371 parent_name, subdb, DB_UNKNOWN, DB_RDONLY, 0)) != 0) 372 dbp->err(dbp, ret, 373 "DB->open: %s:%s", parent_name, subdb); 374 if (ret == 0 && dbp->dump( 375 dbp, subdb, __db_pr_callback, stdout, pflag, keyflag)) 376 ret = 1; 377 (void)dbp->close(dbp, 0); 378 free(subdb); 379 if (ret != 0) 380 return (1); 381 } 382 if (ret != DB_NOTFOUND) { 383 parent_dbp->err(parent_dbp, ret, "DBcursor->get"); 384 return (1); 385 } 386 387 if ((ret = dbcp->close(dbcp)) != 0) { 388 parent_dbp->err(parent_dbp, ret, "DBcursor->close"); 389 return (1); 390 } 391 392 return (0); 393} 394 395/* 396 * show_subs -- 397 * Display the subdatabases for a database. 398 */ 399int 400show_subs(dbp) 401 DB *dbp; 402{ 403 DBC *dbcp; 404 DBT key, data; 405 int ret; 406 407 /* 408 * Get a cursor and step through the database, printing out the key 409 * of each key/data pair. 410 */ 411 if ((ret = dbp->cursor(dbp, NULL, &dbcp, 0)) != 0) { 412 dbp->err(dbp, ret, "DB->cursor"); 413 return (1); 414 } 415 416 memset(&key, 0, sizeof(key)); 417 memset(&data, 0, sizeof(data)); 418 while ((ret = dbcp->get(dbcp, &key, &data, 419 DB_IGNORE_LEASE | DB_NEXT)) == 0) { 420 if ((ret = dbp->dbenv->prdbt( 421 &key, 1, NULL, stdout, __db_pr_callback, 0)) != 0) { 422 dbp->errx(dbp, NULL); 423 return (1); 424 } 425 } 426 if (ret != DB_NOTFOUND) { 427 dbp->err(dbp, ret, "DBcursor->get"); 428 return (1); 429 } 430 431 if ((ret = dbcp->close(dbcp)) != 0) { 432 dbp->err(dbp, ret, "DBcursor->close"); 433 return (1); 434 } 435 return (0); 436} 437 438/* 439 * usage -- 440 * Display the usage message. 441 */ 442int 443usage() 444{ 445 (void)fprintf(stderr, "usage: %s [-klNprRV]\n\t%s\n", 446 progname, 447 "[-d ahr] [-f output] [-h home] [-P password] [-s database] db_file"); 448 return (EXIT_FAILURE); 449} 450 451int 452version_check() 453{ 454 int v_major, v_minor, v_patch; 455 456 /* Make sure we're loaded with the right version of the DB library. */ 457 (void)db_version(&v_major, &v_minor, &v_patch); 458 if (v_major != DB_VERSION_MAJOR || v_minor != DB_VERSION_MINOR) { 459 fprintf(stderr, 460 "%s: version %d.%d doesn't match library version %d.%d\n", 461 progname, DB_VERSION_MAJOR, DB_VERSION_MINOR, 462 v_major, v_minor); 463 return (EXIT_FAILURE); 464 } 465 return (0); 466} 467