Debian Codegraph @

build_vxworks/util/db_dump.c

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