| 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 |
typedef struct { /* XXX: Globals. */ |
| 21 |
const char *progname; /* Program name. */ |
| 22 |
char *hdrbuf; /* Input file header. */ |
| 23 |
u_long lineno; /* Input file line number. */ |
| 24 |
u_long origline; /* Original file line number. */ |
| 25 |
int endodata; /* Reached the end of a database. */ |
| 26 |
int endofile; /* Reached the end of the input. */ |
| 27 |
int version; /* Input version. */ |
| 28 |
char *home; /* Env home. */ |
| 29 |
char *passwd; /* Env passwd. */ |
| 30 |
int private; /* Private env. */ |
| 31 |
u_int32_t cache; /* Env cache size. */ |
| 32 |
} LDG; |
| 33 |
|
| 34 |
int db_load_badend __P((DB_ENV *)); |
| 35 |
void db_load_badnum __P((DB_ENV *)); |
| 36 |
int db_load_configure __P((DB_ENV *, DB *, char **, char **, int *)); |
| 37 |
int db_load_convprintable __P((DB_ENV *, char *, char **)); |
| 38 |
int db_load_db_init __P((DB_ENV *, char *, u_int32_t, int *)); |
| 39 |
int db_load_dbt_rdump __P((DB_ENV *, DBT *)); |
| 40 |
int db_load_dbt_rprint __P((DB_ENV *, DBT *)); |
| 41 |
int db_load_dbt_rrecno __P((DB_ENV *, DBT *, int)); |
| 42 |
int db_load_dbt_to_recno __P((DB_ENV *, DBT *, db_recno_t *)); |
| 43 |
int db_load_env_create __P((DB_ENV **, LDG *)); |
| 44 |
void free_keys __P((DBT *part_keys)); |
| 45 |
int db_load_load __P((DB_ENV *, char *, DBTYPE, char **, u_int, LDG *, int *)); |
| 46 |
int db_load_main __P((int, char *[])); |
| 47 |
int db_load_rheader __P((DB_ENV *, DB *, DBTYPE *, char **, int *, int *, DBT **)); |
| 48 |
int db_load_usage __P((void)); |
| 49 |
int db_load_version_check __P((void)); |
| 50 |
|
| 51 |
const char *progname; |
| 52 |
|
| 53 |
#define G(f) ((LDG *)dbenv->app_private)->f |
| 54 |
|
| 55 |
/* Flags to the load function. */ |
| 56 |
#define LDF_NOHEADER 0x01 /* No dump header. */ |
| 57 |
#define LDF_NOOVERWRITE 0x02 /* Don't overwrite existing rows. */ |
| 58 |
#define LDF_PASSWORD 0x04 /* Encrypt created databases. */ |
| 59 |
|
| 60 |
int |
| 61 |
db_load(args) |
| 62 |
char *args; |
| 63 |
{ |
| 64 |
int argc; |
| 65 |
char **argv; |
| 66 |
|
| 67 |
__db_util_arg("db_load", args, &argc, &argv); |
| 68 |
return (db_load_main(argc, argv) ? EXIT_FAILURE : EXIT_SUCCESS); |
| 69 |
} |
| 70 |
|
| 71 |
#include <stdio.h> |
| 72 |
#define ERROR_RETURN ERROR |
| 73 |
|
| 74 |
int |
| 75 |
db_load_main(argc, argv) |
| 76 |
int argc; |
| 77 |
char *argv[]; |
| 78 |
{ |
| 79 |
enum { NOTSET, FILEID_RESET, LSN_RESET, STANDARD_LOAD } mode; |
| 80 |
extern char *optarg; |
| 81 |
extern int optind, __db_getopt_reset; |
| 82 |
DBTYPE dbtype; |
| 83 |
DB_ENV *dbenv; |
| 84 |
LDG ldg; |
| 85 |
u_int ldf; |
| 86 |
int ch, existed, exitval, ret; |
| 87 |
char **clist, **clp; |
| 88 |
|
| 89 |
if ((progname = __db_rpath(argv[0])) == NULL) |
| 90 |
progname = argv[0]; |
| 91 |
else |
| 92 |
++progname; |
| 93 |
|
| 94 |
clist = NULL; |
| 95 |
ldg.progname = progname; |
| 96 |
ldg.lineno = 0; |
| 97 |
ldg.endodata = ldg.endofile = 0; |
| 98 |
ldg.version = 1; |
| 99 |
ldg.cache = MEGABYTE; |
| 100 |
ldg.hdrbuf = NULL; |
| 101 |
ldg.home = NULL; |
| 102 |
ldg.passwd = NULL; |
| 103 |
|
| 104 |
if ((exitval = db_load_version_check()) != 0) |
| 105 |
goto done; |
| 106 |
|
| 107 |
mode = NOTSET; |
| 108 |
ldf = 0; |
| 109 |
exitval = existed = 0; |
| 110 |
dbtype = DB_UNKNOWN; |
| 111 |
|
| 112 |
/* Allocate enough room for configuration arguments. */ |
| 113 |
if ((clp = clist = |
| 114 |
(char **)calloc((size_t)argc + 1, sizeof(char *))) == NULL) { |
| 115 |
fprintf(stderr, "%s: %s\n", ldg.progname, strerror(ENOMEM)); |
| 116 |
exitval = 1; |
| 117 |
goto done; |
| 118 |
} |
| 119 |
|
| 120 |
/* |
| 121 |
* There are two modes for db_load: -r and everything else. The -r |
| 122 |
* option zeroes out the database LSN's or resets the file ID, it |
| 123 |
* doesn't really "load" a new database. The functionality is in |
| 124 |
* db_load because we don't have a better place to put it, and we |
| 125 |
* don't want to create a new utility for just that functionality. |
| 126 |
*/ |
| 127 |
__db_getopt_reset = 1; |
| 128 |
while ((ch = getopt(argc, argv, "c:f:h:nP:r:Tt:V")) != EOF) |
| 129 |
switch (ch) { |
| 130 |
case 'c': |
| 131 |
if (mode != NOTSET && mode != STANDARD_LOAD) { |
| 132 |
exitval = db_load_usage(); |
| 133 |
goto done; |
| 134 |
} |
| 135 |
mode = STANDARD_LOAD; |
| 136 |
|
| 137 |
*clp++ = optarg; |
| 138 |
break; |
| 139 |
case 'f': |
| 140 |
if (mode != NOTSET && mode != STANDARD_LOAD) { |
| 141 |
exitval = db_load_usage(); |
| 142 |
goto done; |
| 143 |
} |
| 144 |
mode = STANDARD_LOAD; |
| 145 |
|
| 146 |
if (freopen(optarg, "r", stdin) == NULL) { |
| 147 |
fprintf(stderr, DB_STR_A("5072", |
| 148 |
"%s: %s: reopen: %s\n", "%s %s %s\n"), |
| 149 |
ldg.progname, optarg, strerror(errno)); |
| 150 |
exitval = db_load_usage(); |
| 151 |
goto done; |
| 152 |
} |
| 153 |
break; |
| 154 |
case 'h': |
| 155 |
ldg.home = optarg; |
| 156 |
break; |
| 157 |
case 'n': |
| 158 |
if (mode != NOTSET && mode != STANDARD_LOAD) { |
| 159 |
exitval = db_load_usage(); |
| 160 |
goto done; |
| 161 |
} |
| 162 |
mode = STANDARD_LOAD; |
| 163 |
|
| 164 |
ldf |= LDF_NOOVERWRITE; |
| 165 |
break; |
| 166 |
case 'P': |
| 167 |
ldg.passwd = strdup(optarg); |
| 168 |
memset(optarg, 0, strlen(optarg)); |
| 169 |
if (ldg.passwd == NULL) { |
| 170 |
fprintf(stderr, DB_STR_A("5073", |
| 171 |
"%s: strdup: %s\n", "%s %s\n"), |
| 172 |
ldg.progname, strerror(errno)); |
| 173 |
exitval = db_load_usage(); |
| 174 |
goto done; |
| 175 |
} |
| 176 |
ldf |= LDF_PASSWORD; |
| 177 |
break; |
| 178 |
case 'r': |
| 179 |
if (mode == STANDARD_LOAD) { |
| 180 |
exitval = db_load_usage(); |
| 181 |
goto done; |
| 182 |
} |
| 183 |
if (strcmp(optarg, "lsn") == 0) |
| 184 |
mode = LSN_RESET; |
| 185 |
else if (strcmp(optarg, "fileid") == 0) |
| 186 |
mode = FILEID_RESET; |
| 187 |
else { |
| 188 |
exitval = db_load_usage(); |
| 189 |
goto done; |
| 190 |
} |
| 191 |
break; |
| 192 |
case 'T': |
| 193 |
if (mode != NOTSET && mode != STANDARD_LOAD) { |
| 194 |
exitval = db_load_usage(); |
| 195 |
goto done; |
| 196 |
} |
| 197 |
mode = STANDARD_LOAD; |
| 198 |
|
| 199 |
ldf |= LDF_NOHEADER; |
| 200 |
break; |
| 201 |
case 't': |
| 202 |
if (mode != NOTSET && mode != STANDARD_LOAD) { |
| 203 |
exitval = db_load_usage(); |
| 204 |
goto done; |
| 205 |
} |
| 206 |
mode = STANDARD_LOAD; |
| 207 |
|
| 208 |
if (strcmp(optarg, "btree") == 0) { |
| 209 |
dbtype = DB_BTREE; |
| 210 |
break; |
| 211 |
} |
| 212 |
if (strcmp(optarg, "hash") == 0) { |
| 213 |
dbtype = DB_HASH; |
| 214 |
break; |
| 215 |
} |
| 216 |
if (strcmp(optarg, "recno") == 0) { |
| 217 |
dbtype = DB_RECNO; |
| 218 |
break; |
| 219 |
} |
| 220 |
if (strcmp(optarg, "queue") == 0) { |
| 221 |
dbtype = DB_QUEUE; |
| 222 |
break; |
| 223 |
} |
| 224 |
exitval = db_load_usage(); |
| 225 |
goto done; |
| 226 |
case 'V': |
| 227 |
printf("%s\n", db_version(NULL, NULL, NULL)); |
| 228 |
exitval = (EXIT_SUCCESS); |
| 229 |
goto done; |
| 230 |
case '?': |
| 231 |
default: |
| 232 |
exitval = db_load_usage(); |
| 233 |
goto done; |
| 234 |
} |
| 235 |
argc -= optind; |
| 236 |
argv += optind; |
| 237 |
|
| 238 |
if (argc != 1) { |
| 239 |
exitval = db_load_usage(); |
| 240 |
goto done; |
| 241 |
} |
| 242 |
|
| 243 |
/* Handle possible interruptions. */ |
| 244 |
__db_util_siginit(); |
| 245 |
|
| 246 |
/* |
| 247 |
* Create an environment object initialized for error reporting, and |
| 248 |
* then open it. |
| 249 |
*/ |
| 250 |
if (db_load_env_create(&dbenv, &ldg) != 0) |
| 251 |
goto err; |
| 252 |
|
| 253 |
/* If we're resetting the LSNs, that's an entirely separate path. */ |
| 254 |
switch (mode) { |
| 255 |
case FILEID_RESET: |
| 256 |
exitval = dbenv->fileid_reset( |
| 257 |
dbenv, argv[0], ldf & LDF_PASSWORD ? DB_ENCRYPT : 0); |
| 258 |
break; |
| 259 |
case LSN_RESET: |
| 260 |
exitval = dbenv->lsn_reset( |
| 261 |
dbenv, argv[0], ldf & LDF_PASSWORD ? DB_ENCRYPT : 0); |
| 262 |
break; |
| 263 |
case NOTSET: |
| 264 |
case STANDARD_LOAD: |
| 265 |
while (!ldg.endofile) |
| 266 |
if (db_load_load(dbenv, argv[0], dbtype, clist, ldf, |
| 267 |
&ldg, &existed) != 0) |
| 268 |
goto err; |
| 269 |
break; |
| 270 |
} |
| 271 |
|
| 272 |
if (0) { |
| 273 |
err: exitval = 1; |
| 274 |
} |
| 275 |
if ((ret = dbenv->close(dbenv, 0)) != 0) { |
| 276 |
exitval = 1; |
| 277 |
fprintf(stderr, |
| 278 |
"%s: dbenv->close: %s\n", ldg.progname, db_strerror(ret)); |
| 279 |
} |
| 280 |
|
| 281 |
/* Resend any caught signal. */ |
| 282 |
__db_util_sigresend(); |
| 283 |
|
| 284 |
done: |
| 285 |
if (clist != NULL) |
| 286 |
free(clist); |
| 287 |
if (ldg.passwd != NULL) |
| 288 |
free(ldg.passwd); |
| 289 |
|
| 290 |
/* |
| 291 |
* Return 0 on success, 1 if keys existed already, and 2 on failure. |
| 292 |
* |
| 293 |
* Technically, this is wrong, because exit of anything other than |
| 294 |
* 0 is implementation-defined by the ANSI C standard. I don't see |
| 295 |
* any good solutions that don't involve API changes. |
| 296 |
*/ |
| 297 |
return (exitval == 0 ? (existed == 0 ? 0 : 1) : 2); |
| 298 |
} |
| 299 |
|
| 300 |
/* |
| 301 |
* load -- |
| 302 |
* Load a database. |
| 303 |
*/ |
| 304 |
int |
| 305 |
db_load_load(dbenv, name, argtype, clist, flags, ldg, existedp) |
| 306 |
DB_ENV *dbenv; |
| 307 |
char *name, **clist; |
| 308 |
DBTYPE argtype; |
| 309 |
u_int flags; |
| 310 |
LDG *ldg; |
| 311 |
int *existedp; |
| 312 |
{ |
| 313 |
DB *dbp; |
| 314 |
DBC *dbc; |
| 315 |
DBT key, rkey, data, *part_keys, *readp, *writep; |
| 316 |
DBTYPE dbtype; |
| 317 |
DB_HEAP_RID rid; |
| 318 |
DB_TXN *ctxn, *txn; |
| 319 |
db_recno_t recno, datarecno; |
| 320 |
u_int32_t put_flags; |
| 321 |
int ascii_recno, checkprint, hexkeys, keyflag, keys, resize, ret, rval; |
| 322 |
char *subdb; |
| 323 |
|
| 324 |
put_flags = LF_ISSET(LDF_NOOVERWRITE) ? DB_NOOVERWRITE : 0; |
| 325 |
G(endodata) = 0; |
| 326 |
|
| 327 |
dbc = NULL; |
| 328 |
subdb = NULL; |
| 329 |
ctxn = txn = NULL; |
| 330 |
part_keys = NULL; |
| 331 |
memset(&key, 0, sizeof(DBT)); |
| 332 |
memset(&data, 0, sizeof(DBT)); |
| 333 |
memset(&rkey, 0, sizeof(DBT)); |
| 334 |
|
| 335 |
retry_db: |
| 336 |
dbtype = DB_UNKNOWN; |
| 337 |
keys = -1; |
| 338 |
hexkeys = -1; |
| 339 |
keyflag = -1; |
| 340 |
|
| 341 |
/* Create the DB object. */ |
| 342 |
if ((ret = db_create(&dbp, dbenv, 0)) != 0) { |
| 343 |
dbenv->err(dbenv, ret, "db_create"); |
| 344 |
goto err; |
| 345 |
} |
| 346 |
|
| 347 |
/* Read the header -- if there's no header, we expect flat text. */ |
| 348 |
if (LF_ISSET(LDF_NOHEADER)) { |
| 349 |
checkprint = 1; |
| 350 |
dbtype = argtype; |
| 351 |
} else { |
| 352 |
if (db_load_rheader(dbenv, |
| 353 |
dbp, &dbtype, &subdb, &checkprint, &keys, &part_keys) != 0) |
| 354 |
goto err; |
| 355 |
if (G(endofile)) |
| 356 |
goto done; |
| 357 |
} |
| 358 |
|
| 359 |
/* |
| 360 |
* Apply command-line configuration changes. (We apply command-line |
| 361 |
* configuration changes to all databases that are loaded, e.g., all |
| 362 |
* subdatabases.) |
| 363 |
*/ |
| 364 |
if (db_load_configure(dbenv, dbp, clist, &subdb, &keyflag)) |
| 365 |
goto err; |
| 366 |
|
| 367 |
if (keys != 1) { |
| 368 |
if (keyflag == 1) { |
| 369 |
dbp->err(dbp, EINVAL, DB_STR("5074", |
| 370 |
"No keys specified in file")); |
| 371 |
goto err; |
| 372 |
} |
| 373 |
} |
| 374 |
else if (keyflag == 0) { |
| 375 |
dbp->err(dbp, EINVAL, DB_STR("5075", |
| 376 |
"Keys specified in file")); |
| 377 |
goto err; |
| 378 |
} |
| 379 |
else |
| 380 |
keyflag = 1; |
| 381 |
|
| 382 |
if (dbtype == DB_BTREE || dbtype == DB_HASH) { |
| 383 |
if (keyflag == 0) |
| 384 |
dbp->err(dbp, |
| 385 |
EINVAL, DB_STR("5076", |
| 386 |
"Btree and Hash must specify keys")); |
| 387 |
else |
| 388 |
keyflag = 1; |
| 389 |
} |
| 390 |
|
| 391 |
if (dbtype == DB_HEAP && keyflag == 1) { |
| 392 |
dbp->err(dbp, |
| 393 |
EINVAL, DB_STR("5127", "Heap must not specify keys")); |
| 394 |
goto err; |
| 395 |
} |
| 396 |
|
| 397 |
if (argtype != DB_UNKNOWN) { |
| 398 |
if (dbtype == DB_HEAP) { |
| 399 |
dbenv->errx(dbenv, DB_STR("5128", |
| 400 |
"improper database type conversion specified")); |
| 401 |
goto err; |
| 402 |
} |
| 403 |
|
| 404 |
if (dbtype == DB_RECNO || dbtype == DB_QUEUE) |
| 405 |
if (keyflag != 1 && argtype != DB_RECNO && |
| 406 |
argtype != DB_QUEUE) { |
| 407 |
dbenv->errx(dbenv, DB_STR("5077", |
| 408 |
"improper database type conversion specified")); |
| 409 |
goto err; |
| 410 |
} |
| 411 |
dbtype = argtype; |
| 412 |
} |
| 413 |
|
| 414 |
if (dbtype == DB_UNKNOWN) { |
| 415 |
dbenv->errx(dbenv, DB_STR("5078", |
| 416 |
"no database type specified")); |
| 417 |
goto err; |
| 418 |
} |
| 419 |
|
| 420 |
if (dbtype == DB_HEAP) |
| 421 |
put_flags = DB_APPEND; |
| 422 |
|
| 423 |
if (keyflag == -1) |
| 424 |
keyflag = 0; |
| 425 |
|
| 426 |
/* |
| 427 |
* Recno keys have only been printed in hexadecimal starting |
| 428 |
* with db_dump format version 3 (DB 3.2). |
| 429 |
* |
| 430 |
* !!! |
| 431 |
* Note that version is set in db_load_rheader(), which must be called before |
| 432 |
* this assignment. |
| 433 |
*/ |
| 434 |
hexkeys = (G(version) >= 3 && keyflag == 1 && checkprint == 0); |
| 435 |
|
| 436 |
if (keyflag == 1 && (dbtype == DB_RECNO || dbtype == DB_QUEUE)) |
| 437 |
ascii_recno = 1; |
| 438 |
else |
| 439 |
ascii_recno = 0; |
| 440 |
|
| 441 |
/* If configured with a password, encrypt databases we create. */ |
| 442 |
if (LF_ISSET(LDF_PASSWORD) && |
| 443 |
(ret = dbp->set_flags(dbp, DB_ENCRYPT)) != 0) { |
| 444 |
dbp->err(dbp, ret, "DB->set_flags: DB_ENCRYPT"); |
| 445 |
goto err; |
| 446 |
} |
| 447 |
|
| 448 |
#if 0 |
| 449 |
Set application-specific btree comparison, compression, or hash |
| 450 |
functions here. For example: |
| 451 |
|
| 452 |
if ((ret = dbp->set_bt_compare(dbp, local_comparison_func)) != 0) { |
| 453 |
dbp->err(dbp, ret, "DB->set_bt_compare"); |
| 454 |
goto err; |
| 455 |
} |
| 456 |
if ((ret = dbp->set_bt_compress(dbp, local_compress_func, |
| 457 |
local_decompress_func)) != 0) { |
| 458 |
dbp->err(dbp, ret, "DB->set_bt_compress"); |
| 459 |
goto err; |
| 460 |
} |
| 461 |
if ((ret = dbp->set_h_hash(dbp, local_hash_func)) != 0) { |
| 462 |
dbp->err(dbp, ret, "DB->set_h_hash"); |
| 463 |
goto err; |
| 464 |
} |
| 465 |
#endif |
| 466 |
|
| 467 |
/* Open the DB file. */ |
| 468 |
if ((ret = dbp->open(dbp, NULL, name, subdb, dbtype, |
| 469 |
DB_CREATE | (TXN_ON(dbenv->env) ? DB_AUTO_COMMIT : 0), |
| 470 |
DB_MODE_666)) != 0) { |
| 471 |
dbp->err(dbp, ret, "DB->open: %s", name); |
| 472 |
goto err; |
| 473 |
} |
| 474 |
if (ldg->private != 0) { |
| 475 |
if ((ret = __db_util_cache(dbp, &ldg->cache, &resize)) != 0) |
| 476 |
goto err; |
| 477 |
if (resize) { |
| 478 |
if ((ret = dbp->close(dbp, 0)) != 0) |
| 479 |
goto err; |
| 480 |
dbp = NULL; |
| 481 |
if ((ret = dbenv->close(dbenv, 0)) != 0) |
| 482 |
goto err; |
| 483 |
if ((ret = db_load_env_create(&dbenv, ldg)) != 0) |
| 484 |
goto err; |
| 485 |
goto retry_db; |
| 486 |
} |
| 487 |
} |
| 488 |
|
| 489 |
/* Initialize the key/data pair. */ |
| 490 |
readp = writep = &key; |
| 491 |
if (dbtype == DB_RECNO || dbtype == DB_QUEUE) { |
| 492 |
key.size = sizeof(recno); |
| 493 |
if (keyflag) { |
| 494 |
key.data = &datarecno; |
| 495 |
if (checkprint) { |
| 496 |
readp = &rkey; |
| 497 |
goto key_data; |
| 498 |
} |
| 499 |
} else |
| 500 |
key.data = &recno; |
| 501 |
} else if (dbtype == DB_HEAP) { |
| 502 |
key.size = sizeof(DB_HEAP_RID); |
| 503 |
key.data = &rid; |
| 504 |
} else |
| 505 |
key_data: if ((readp->data = malloc(readp->ulen = 1024)) == NULL) { |
| 506 |
dbenv->err(dbenv, ENOMEM, NULL); |
| 507 |
goto err; |
| 508 |
} |
| 509 |
if ((data.data = malloc(data.ulen = 1024)) == NULL) { |
| 510 |
dbenv->err(dbenv, ENOMEM, NULL); |
| 511 |
goto err; |
| 512 |
} |
| 513 |
|
| 514 |
if (TXN_ON(dbenv->env) && |
| 515 |
(ret = dbenv->txn_begin(dbenv, NULL, &txn, 0)) != 0) |
| 516 |
goto err; |
| 517 |
|
| 518 |
if (put_flags == 0 && (ret = dbp->cursor(dbp, |
| 519 |
txn, &dbc, DB_CURSOR_BULK)) != 0) |
| 520 |
goto err; |
| 521 |
|
| 522 |
/* Get each key/data pair and add them to the database. */ |
| 523 |
for (recno = 1; !__db_util_interrupted(); ++recno) { |
| 524 |
if (!keyflag) { |
| 525 |
if (checkprint) { |
| 526 |
if (db_load_dbt_rprint(dbenv, &data)) |
| 527 |
goto err; |
| 528 |
} else { |
| 529 |
if (db_load_dbt_rdump(dbenv, &data)) |
| 530 |
goto err; |
| 531 |
} |
| 532 |
} else { |
| 533 |
if (checkprint) { |
| 534 |
if (db_load_dbt_rprint(dbenv, readp)) |
| 535 |
goto err; |
| 536 |
if (ascii_recno && |
| 537 |
db_load_dbt_to_recno(dbenv, readp, &datarecno) != 0) |
| 538 |
goto err; |
| 539 |
|
| 540 |
if (!G(endodata) && db_load_dbt_rprint(dbenv, &data)) |
| 541 |
goto odd_count; |
| 542 |
} else { |
| 543 |
if (ascii_recno) { |
| 544 |
if (db_load_dbt_rrecno(dbenv, readp, hexkeys)) |
| 545 |
goto err; |
| 546 |
} else |
| 547 |
if (db_load_dbt_rdump(dbenv, readp)) |
| 548 |
goto err; |
| 549 |
|
| 550 |
if (!G(endodata) && db_load_dbt_rdump(dbenv, &data)) { |
| 551 |
odd_count: dbenv->errx(dbenv, DB_STR("5079", |
| 552 |
"odd number of key/data pairs")); |
| 553 |
goto err; |
| 554 |
} |
| 555 |
} |
| 556 |
} |
| 557 |
if (G(endodata)) |
| 558 |
break; |
| 559 |
retry: |
| 560 |
if (put_flags != 0 && txn != NULL) |
| 561 |
if ((ret = dbenv->txn_begin(dbenv, txn, &ctxn, 0)) != 0) |
| 562 |
goto err; |
| 563 |
switch (ret = ((put_flags == 0) ? |
| 564 |
dbc->put(dbc, writep, &data, DB_KEYLAST) : |
| 565 |
dbp->put(dbp, ctxn, writep, &data, put_flags))) { |
| 566 |
case 0: |
| 567 |
if (ctxn != NULL) { |
| 568 |
if ((ret = |
| 569 |
ctxn->commit(ctxn, DB_TXN_NOSYNC)) != 0) |
| 570 |
goto err; |
| 571 |
ctxn = NULL; |
| 572 |
} |
| 573 |
break; |
| 574 |
case DB_KEYEXIST: |
| 575 |
*existedp = 1; |
| 576 |
dbenv->errx(dbenv, DB_STR_A("5080", |
| 577 |
"%s: line %d: key already exists, not loaded:", |
| 578 |
"%s %d"), name, |
| 579 |
!keyflag ? recno : recno * 2 - 1); |
| 580 |
|
| 581 |
(void)dbenv->prdbt(&key, |
| 582 |
checkprint, 0, stderr, __db_pr_callback, 0, 0); |
| 583 |
break; |
| 584 |
case DB_LOCK_DEADLOCK: |
| 585 |
/* If we have a child txn, retry--else it's fatal. */ |
| 586 |
if (ctxn != NULL) { |
| 587 |
if ((ret = ctxn->abort(ctxn)) != 0) |
| 588 |
goto err; |
| 589 |
ctxn = NULL; |
| 590 |
goto retry; |
| 591 |
} |
| 592 |
/* FALLTHROUGH */ |
| 593 |
default: |
| 594 |
dbenv->err(dbenv, ret, NULL); |
| 595 |
if (ctxn != NULL) { |
| 596 |
(void)ctxn->abort(ctxn); |
| 597 |
ctxn = NULL; |
| 598 |
} |
| 599 |
goto err; |
| 600 |
} |
| 601 |
if (ctxn != NULL) { |
| 602 |
if ((ret = ctxn->abort(ctxn)) != 0) |
| 603 |
goto err; |
| 604 |
ctxn = NULL; |
| 605 |
} |
| 606 |
} |
| 607 |
done: rval = 0; |
| 608 |
if (dbc != NULL && (ret = dbc->close(dbc)) != 0) { |
| 609 |
dbc = NULL; |
| 610 |
goto err; |
| 611 |
} |
| 612 |
if (txn != NULL && (ret = txn->commit(txn, 0)) != 0) { |
| 613 |
txn = NULL; |
| 614 |
goto err; |
| 615 |
} |
| 616 |
|
| 617 |
if (0) { |
| 618 |
err: rval = 1; |
| 619 |
if (dbc != NULL) |
| 620 |
(void)dbc->close(dbc); |
| 621 |
if (txn != NULL) |
| 622 |
(void)txn->abort(txn); |
| 623 |
} |
| 624 |
|
| 625 |
/* Close the database. */ |
| 626 |
if (dbp != NULL && (ret = dbp->close(dbp, 0)) != 0) { |
| 627 |
dbenv->err(dbenv, ret, "DB->close"); |
| 628 |
rval = 1; |
| 629 |
} |
| 630 |
|
| 631 |
if (G(hdrbuf) != NULL) |
| 632 |
free(G(hdrbuf)); |
| 633 |
G(hdrbuf) = NULL; |
| 634 |
/* Free allocated memory. */ |
| 635 |
if (subdb != NULL) |
| 636 |
free(subdb); |
| 637 |
if (dbtype != DB_HEAP && |
| 638 |
dbtype != DB_RECNO && dbtype != DB_QUEUE && key.data != NULL) |
| 639 |
free(key.data); |
| 640 |
if (rkey.data != NULL) |
| 641 |
free(rkey.data); |
| 642 |
free(data.data); |
| 643 |
free_keys(part_keys); |
| 644 |
return (rval); |
| 645 |
} |
| 646 |
|
| 647 |
/* |
| 648 |
* env_create -- |
| 649 |
* Create the environment and initialize it for error reporting. |
| 650 |
*/ |
| 651 |
int |
| 652 |
db_load_env_create(dbenvp, ldg) |
| 653 |
DB_ENV **dbenvp; |
| 654 |
LDG *ldg; |
| 655 |
{ |
| 656 |
DB_ENV *dbenv; |
| 657 |
int ret; |
| 658 |
|
| 659 |
if ((ret = db_env_create(dbenvp, 0)) != 0) { |
| 660 |
fprintf(stderr, "%s: db_env_create: %s\n", |
| 661 |
ldg->progname, db_strerror(ret)); |
| 662 |
return (ret); |
| 663 |
} |
| 664 |
dbenv = *dbenvp; |
| 665 |
dbenv->set_errfile(dbenv, stderr); |
| 666 |
dbenv->set_errpfx(dbenv, ldg->progname); |
| 667 |
if (ldg->passwd != NULL && (ret = dbenv->set_encrypt(dbenv, |
| 668 |
ldg->passwd, DB_ENCRYPT_AES)) != 0) { |
| 669 |
dbenv->err(dbenv, ret, "set_passwd"); |
| 670 |
return (ret); |
| 671 |
} |
| 672 |
if ((ret = db_load_db_init(dbenv, ldg->home, ldg->cache, &ldg->private)) != 0) |
| 673 |
return (ret); |
| 674 |
dbenv->app_private = ldg; |
| 675 |
|
| 676 |
return (0); |
| 677 |
} |
| 678 |
|
| 679 |
/* |
| 680 |
* db_init -- |
| 681 |
* Initialize the environment. |
| 682 |
*/ |
| 683 |
int |
| 684 |
db_load_db_init(dbenv, home, cache, is_private) |
| 685 |
DB_ENV *dbenv; |
| 686 |
char *home; |
| 687 |
u_int32_t cache; |
| 688 |
int *is_private; |
| 689 |
{ |
| 690 |
u_int32_t flags; |
| 691 |
int ret; |
| 692 |
|
| 693 |
*is_private = 0; |
| 694 |
/* We may be loading into a live environment. Try and join. */ |
| 695 |
flags = DB_USE_ENVIRON | |
| 696 |
DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_MPOOL | DB_INIT_TXN; |
| 697 |
if ((ret = dbenv->open(dbenv, home, flags, 0)) == 0) |
| 698 |
return (0); |
| 699 |
if (ret == DB_VERSION_MISMATCH || ret == DB_REP_LOCKOUT) |
| 700 |
goto err; |
| 701 |
|
| 702 |
/* |
| 703 |
* We're trying to load a database. |
| 704 |
* |
| 705 |
* An environment is required because we may be trying to look at |
| 706 |
* databases in directories other than the current one. We could |
| 707 |
* avoid using an environment iff the -h option wasn't specified, |
| 708 |
* but that seems like more work than it's worth. |
| 709 |
* |
| 710 |
* No environment exists (or, at least no environment that includes |
| 711 |
* an mpool region exists). Create one, but make it private so that |
| 712 |
* no files are actually created. |
| 713 |
*/ |
| 714 |
LF_CLR(DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_TXN); |
| 715 |
LF_SET(DB_CREATE | DB_PRIVATE); |
| 716 |
*is_private = 1; |
| 717 |
if ((ret = dbenv->set_cachesize(dbenv, 0, cache, 1)) != 0) { |
| 718 |
dbenv->err(dbenv, ret, "set_cachesize"); |
| 719 |
return (1); |
| 720 |
} |
| 721 |
if ((ret = dbenv->open(dbenv, home, flags, 0)) == 0) |
| 722 |
return (0); |
| 723 |
|
| 724 |
/* An environment is required. */ |
| 725 |
err: dbenv->err(dbenv, ret, "DB_ENV->open"); |
| 726 |
return (1); |
| 727 |
} |
| 728 |
|
| 729 |
#define FLAG(name, value, keyword, flag) \ |
| 730 |
if (strcmp(name, keyword) == 0) { \ |
| 731 |
switch (*value) { \ |
| 732 |
case '1': \ |
| 733 |
if ((ret = dbp->set_flags(dbp, flag)) != 0) { \ |
| 734 |
dbp->err(dbp, ret, "%s: set_flags: %s", \ |
| 735 |
G(progname), name); \ |
| 736 |
goto err; \ |
| 737 |
} \ |
| 738 |
break; \ |
| 739 |
case '0': \ |
| 740 |
break; \ |
| 741 |
default: \ |
| 742 |
db_load_badnum(dbenv); \ |
| 743 |
goto err; \ |
| 744 |
} \ |
| 745 |
continue; \ |
| 746 |
} |
| 747 |
#define NUMBER(name, value, keyword, func, t) \ |
| 748 |
if (strcmp(name, keyword) == 0) { \ |
| 749 |
if ((ret = __db_getlong(dbenv, \ |
| 750 |
NULL, value, 0, LONG_MAX, &val)) != 0 || \ |
| 751 |
(ret = dbp->func(dbp, (t)val)) != 0) \ |
| 752 |
goto nameerr; \ |
| 753 |
continue; \ |
| 754 |
} |
| 755 |
#define STRING(name, value, keyword, func) \ |
| 756 |
if (strcmp(name, keyword) == 0) { \ |
| 757 |
if ((ret = dbp->func(dbp, value[0])) != 0) \ |
| 758 |
goto nameerr; \ |
| 759 |
continue; \ |
| 760 |
} |
| 761 |
|
| 762 |
/* |
| 763 |
* The code to check a command-line or input header argument against a list |
| 764 |
* of configuration options. It's #defined because it's used in two places |
| 765 |
* and the two places have gotten out of sync more than once. |
| 766 |
*/ |
| 767 |
#define CONFIGURATION_LIST_COMPARE \ |
| 768 |
NUMBER(name, value, "bt_minkey", set_bt_minkey, u_int32_t); \ |
| 769 |
FLAG(name, value, "chksum", DB_CHKSUM); \ |
| 770 |
NUMBER(name, value, "db_lorder", set_lorder, int); \ |
| 771 |
NUMBER(name, value, "db_pagesize", set_pagesize, u_int32_t); \ |
| 772 |
FLAG(name, value, "duplicates", DB_DUP); \ |
| 773 |
FLAG(name, value, "dupsort", DB_DUPSORT); \ |
| 774 |
NUMBER(name, value, "extentsize", set_q_extentsize, u_int32_t); \ |
| 775 |
NUMBER(name, value, "h_ffactor", set_h_ffactor, u_int32_t); \ |
| 776 |
NUMBER(name, value, "h_nelem", set_h_nelem, u_int32_t); \ |
| 777 |
NUMBER(name, value, "heap_regionsize", set_heap_regionsize, u_int32_t);\ |
| 778 |
NUMBER(name, value, "re_len", set_re_len, u_int32_t); \ |
| 779 |
STRING(name, value, "re_pad", set_re_pad); \ |
| 780 |
FLAG(name, value, "recnum", DB_RECNUM); \ |
| 781 |
FLAG(name, value, "renumber", DB_RENUMBER); \ |
| 782 |
if (strcmp(name, "compressed") == 0) { \ |
| 783 |
switch (*value) { \ |
| 784 |
case '1': \ |
| 785 |
if ((ret = dbp->set_bt_compress(dbp, NULL, \ |
| 786 |
NULL)) != 0) \ |
| 787 |
goto nameerr; \ |
| 788 |
break; \ |
| 789 |
case '0': \ |
| 790 |
break; \ |
| 791 |
default: \ |
| 792 |
db_load_badnum(dbenv); \ |
| 793 |
goto err; \ |
| 794 |
} \ |
| 795 |
continue; \ |
| 796 |
} |
| 797 |
|
| 798 |
/* |
| 799 |
* configure -- |
| 800 |
* Handle command-line configuration options. |
| 801 |
*/ |
| 802 |
int |
| 803 |
db_load_configure(dbenv, dbp, clp, subdbp, keysp) |
| 804 |
DB_ENV *dbenv; |
| 805 |
DB *dbp; |
| 806 |
char **clp, **subdbp; |
| 807 |
int *keysp; |
| 808 |
{ |
| 809 |
long val; |
| 810 |
int ret, savech; |
| 811 |
char *name, *value; |
| 812 |
u_int32_t heap_bytes, heap_gbytes; |
| 813 |
|
| 814 |
heap_bytes = heap_gbytes = 0; |
| 815 |
|
| 816 |
for (; (name = *clp) != NULL; *--value = savech, ++clp) { |
| 817 |
if ((value = strchr(name, '=')) == NULL) { |
| 818 |
dbp->errx(dbp, DB_STR("5081", |
| 819 |
"command-line configuration uses name=value format")); |
| 820 |
return (1); |
| 821 |
} |
| 822 |
savech = *value; |
| 823 |
*value++ = '\0'; |
| 824 |
|
| 825 |
if (strcmp(name, "database") == 0 || |
| 826 |
strcmp(name, "subdatabase") == 0) { |
| 827 |
if (*subdbp != NULL) |
| 828 |
free(*subdbp); |
| 829 |
if ((*subdbp = strdup(value)) == NULL) { |
| 830 |
dbp->err(dbp, ENOMEM, NULL); |
| 831 |
return (1); |
| 832 |
} |
| 833 |
continue; |
| 834 |
} |
| 835 |
if (strcmp(name, "keys") == 0) { |
| 836 |
if (strcmp(value, "1") == 0) |
| 837 |
*keysp = 1; |
| 838 |
else if (strcmp(value, "0") == 0) |
| 839 |
*keysp = 0; |
| 840 |
else { |
| 841 |
db_load_badnum(dbenv); |
| 842 |
return (1); |
| 843 |
} |
| 844 |
continue; |
| 845 |
} |
| 846 |
|
| 847 |
CONFIGURATION_LIST_COMPARE; |
| 848 |
|
| 849 |
/* Have to special case heap size, because we need 2 values. */ |
| 850 |
if (strcmp(name, "heap_bytes") == 0) { |
| 851 |
if ((ret = __db_getlong(dbenv, |
| 852 |
NULL, value, 0, LONG_MAX, &val)) != 0) |
| 853 |
goto nameerr; |
| 854 |
heap_bytes = (u_int32_t)val; |
| 855 |
} |
| 856 |
if (strcmp(name, "heap_gbytes") == 0) { |
| 857 |
if ((ret = __db_getlong(dbenv, |
| 858 |
NULL, value, 0, LONG_MAX, &val)) != 0) |
| 859 |
goto nameerr; |
| 860 |
heap_gbytes = (u_int32_t)val; |
| 861 |
} |
| 862 |
|
| 863 |
dbp->errx(dbp, DB_STR_A("5082", |
| 864 |
"unknown command-line configuration keyword \"%s\"", |
| 865 |
"%s"), name); |
| 866 |
return (1); |
| 867 |
} |
| 868 |
if (heap_gbytes != 0 || heap_bytes != 0) |
| 869 |
if ((ret = dbp->set_heapsize(dbp, |
| 870 |
heap_gbytes, heap_bytes, 0)) != 0) |
| 871 |
goto heaperr; |
| 872 |
return (0); |
| 873 |
|
| 874 |
nameerr: |
| 875 |
dbp->err(dbp, ret, "%s: %s=%s", G(progname), name, value); |
| 876 |
if (0) { |
| 877 |
heaperr: dbp->err(dbp, ret, "%s: heap_gbytes/heap_bytes=%lu/%lu", |
| 878 |
G(progname), heap_gbytes, heap_bytes); |
| 879 |
} |
| 880 |
err: return (1); |
| 881 |
} |
| 882 |
|
| 883 |
/* |
| 884 |
* rheader -- |
| 885 |
* Read the header message. |
| 886 |
*/ |
| 887 |
int |
| 888 |
db_load_rheader(dbenv, dbp, dbtypep, subdbp, checkprintp, keysp, part_keyp) |
| 889 |
DB_ENV *dbenv; |
| 890 |
DB *dbp; |
| 891 |
DBTYPE *dbtypep; |
| 892 |
char **subdbp; |
| 893 |
int *checkprintp, *keysp; |
| 894 |
DBT **part_keyp; |
| 895 |
{ |
| 896 |
DBT *keys, *kp; |
| 897 |
size_t buflen, linelen, start; |
| 898 |
long val; |
| 899 |
int ch, first, hdr, ret; |
| 900 |
char *buf, *name, *p, *value; |
| 901 |
u_int32_t heap_bytes, heap_gbytes, i, nparts; |
| 902 |
|
| 903 |
*dbtypep = DB_UNKNOWN; |
| 904 |
*checkprintp = 0; |
| 905 |
name = NULL; |
| 906 |
*part_keyp = NULL; |
| 907 |
keys = NULL; |
| 908 |
heap_bytes = heap_gbytes = 0; |
| 909 |
|
| 910 |
/* |
| 911 |
* We start with a smallish buffer; most headers are small. |
| 912 |
* We may need to realloc it for a large subdatabase name. |
| 913 |
*/ |
| 914 |
buflen = 4096; |
| 915 |
if (G(hdrbuf) == NULL) { |
| 916 |
hdr = 0; |
| 917 |
if ((buf = malloc(buflen)) == NULL) |
| 918 |
goto memerr; |
| 919 |
G(hdrbuf) = buf; |
| 920 |
G(origline) = G(lineno); |
| 921 |
} else { |
| 922 |
hdr = 1; |
| 923 |
buf = G(hdrbuf); |
| 924 |
G(lineno) = G(origline); |
| 925 |
} |
| 926 |
|
| 927 |
start = 0; |
| 928 |
for (first = 1;; first = 0) { |
| 929 |
++G(lineno); |
| 930 |
|
| 931 |
/* Read a line, which may be of arbitrary length, into buf. */ |
| 932 |
linelen = 0; |
| 933 |
buf = &G(hdrbuf)[start]; |
| 934 |
if (hdr == 0) { |
| 935 |
for (;;) { |
| 936 |
if ((ch = getchar()) == EOF) { |
| 937 |
if (!first || ferror(stdin)) |
| 938 |
goto badfmt; |
| 939 |
G(endofile) = 1; |
| 940 |
break; |
| 941 |
} |
| 942 |
|
| 943 |
/* |
| 944 |
* If the buffer is too small, double it. |
| 945 |
*/ |
| 946 |
if (linelen + start == buflen) { |
| 947 |
G(hdrbuf) = |
| 948 |
realloc(G(hdrbuf), buflen *= 2); |
| 949 |
if (G(hdrbuf) == NULL) |
| 950 |
goto memerr; |
| 951 |
buf = &G(hdrbuf)[start]; |
| 952 |
} |
| 953 |
|
| 954 |
if (ch == '\n') |
| 955 |
break; |
| 956 |
|
| 957 |
buf[linelen++] = ch; |
| 958 |
} |
| 959 |
if (G(endofile) == 1) |
| 960 |
break; |
| 961 |
buf[linelen++] = '\0'; |
| 962 |
} else |
| 963 |
linelen = strlen(buf) + 1; |
| 964 |
start += linelen; |
| 965 |
|
| 966 |
if (name != NULL) { |
| 967 |
free(name); |
| 968 |
name = NULL; |
| 969 |
} |
| 970 |
/* If we don't see the expected information, it's an error. */ |
| 971 |
if ((name = strdup(buf)) == NULL) |
| 972 |
goto memerr; |
| 973 |
if ((p = strchr(name, '=')) == NULL) |
| 974 |
goto badfmt; |
| 975 |
*p++ = '\0'; |
| 976 |
|
| 977 |
value = p--; |
| 978 |
|
| 979 |
if (name[0] == '\0') |
| 980 |
goto badfmt; |
| 981 |
|
| 982 |
/* |
| 983 |
* The only values that may be zero-length are database names. |
| 984 |
* In the original Berkeley DB code it was possible to create |
| 985 |
* zero-length database names, and the db_load code was then |
| 986 |
* changed to allow such databases to be be dumped and loaded. |
| 987 |
* [#8204] |
| 988 |
*/ |
| 989 |
if (strcmp(name, "database") == 0 || |
| 990 |
strcmp(name, "subdatabase") == 0) { |
| 991 |
if ((ret = db_load_convprintable(dbenv, value, subdbp)) != 0) { |
| 992 |
dbp->err(dbp, ret, DB_STR("5083", |
| 993 |
"error reading db name")); |
| 994 |
goto err; |
| 995 |
} |
| 996 |
continue; |
| 997 |
} |
| 998 |
|
| 999 |
/* No other values may be zero-length. */ |
| 1000 |
if (value[0] == '\0') |
| 1001 |
goto badfmt; |
| 1002 |
|
| 1003 |
if (strcmp(name, "HEADER") == 0) |
| 1004 |
break; |
| 1005 |
if (strcmp(name, "VERSION") == 0) { |
| 1006 |
/* |
| 1007 |
* Version 1 didn't have a "VERSION" header line. We |
| 1008 |
* only support versions 1, 2, and 3 of the dump format. |
| 1009 |
*/ |
| 1010 |
G(version) = atoi(value); |
| 1011 |
|
| 1012 |
if (G(version) > 3) { |
| 1013 |
dbp->errx(dbp, DB_STR_A("5084", |
| 1014 |
"line %lu: VERSION %d is unsupported", |
| 1015 |
"%lu %d"), G(lineno), G(version)); |
| 1016 |
goto err; |
| 1017 |
} |
| 1018 |
continue; |
| 1019 |
} |
| 1020 |
if (strcmp(name, "format") == 0) { |
| 1021 |
if (strcmp(value, "bytevalue") == 0) { |
| 1022 |
*checkprintp = 0; |
| 1023 |
continue; |
| 1024 |
} |
| 1025 |
if (strcmp(value, "print") == 0) { |
| 1026 |
*checkprintp = 1; |
| 1027 |
continue; |
| 1028 |
} |
| 1029 |
goto badfmt; |
| 1030 |
} |
| 1031 |
if (strcmp(name, "type") == 0) { |
| 1032 |
if (strcmp(value, "btree") == 0) { |
| 1033 |
*dbtypep = DB_BTREE; |
| 1034 |
continue; |
| 1035 |
} |
| 1036 |
if (strcmp(value, "hash") == 0) { |
| 1037 |
*dbtypep = DB_HASH; |
| 1038 |
continue; |
| 1039 |
} |
| 1040 |
if (strcmp(value, "heap") == 0) { |
| 1041 |
*dbtypep = DB_HEAP; |
| 1042 |
continue; |
| 1043 |
} |
| 1044 |
if (strcmp(value, "recno") == 0) { |
| 1045 |
*dbtypep = DB_RECNO; |
| 1046 |
continue; |
| 1047 |
} |
| 1048 |
if (strcmp(value, "queue") == 0) { |
| 1049 |
*dbtypep = DB_QUEUE; |
| 1050 |
continue; |
| 1051 |
} |
| 1052 |
dbp->errx(dbp, DB_STR_A("5085", |
| 1053 |
"line %lu: unknown type", "%lu"), G(lineno)); |
| 1054 |
goto err; |
| 1055 |
} |
| 1056 |
if (strcmp(name, "keys") == 0) { |
| 1057 |
if (strcmp(value, "1") == 0) |
| 1058 |
*keysp = 1; |
| 1059 |
else if (strcmp(value, "0") == 0) |
| 1060 |
*keysp = 0; |
| 1061 |
else { |
| 1062 |
db_load_badnum(dbenv); |
| 1063 |
goto err; |
| 1064 |
} |
| 1065 |
continue; |
| 1066 |
} |
| 1067 |
if (strcmp(name, "nparts") == 0) { |
| 1068 |
if ((ret = __db_getlong(dbenv, |
| 1069 |
NULL, value, 0, LONG_MAX, &val)) != 0) { |
| 1070 |
db_load_badnum(dbenv); |
| 1071 |
goto err; |
| 1072 |
} |
| 1073 |
nparts = (u_int32_t) val; |
| 1074 |
if ((keys = |
| 1075 |
malloc(nparts * sizeof(DBT))) == NULL) { |
| 1076 |
dbenv->err(dbenv, ENOMEM, NULL); |
| 1077 |
goto err; |
| 1078 |
} |
| 1079 |
keys[nparts - 1].data = NULL; |
| 1080 |
kp = keys; |
| 1081 |
for (i = 1; i < nparts; kp++, i++) { |
| 1082 |
if ((kp->data = |
| 1083 |
malloc(kp->ulen = 1024)) == NULL) { |
| 1084 |
dbenv->err(dbenv, ENOMEM, NULL); |
| 1085 |
goto err; |
| 1086 |
} |
| 1087 |
if (*checkprintp) { |
| 1088 |
if (db_load_dbt_rprint(dbenv, kp)) |
| 1089 |
goto err; |
| 1090 |
} else { |
| 1091 |
if (db_load_dbt_rdump(dbenv, kp)) |
| 1092 |
goto err; |
| 1093 |
} |
| 1094 |
} |
| 1095 |
if ((ret = dbp->set_partition( |
| 1096 |
dbp, nparts, keys, NULL)) != 0) |
| 1097 |
goto err; |
| 1098 |
|
| 1099 |
*part_keyp = keys; |
| 1100 |
|
| 1101 |
continue; |
| 1102 |
} |
| 1103 |
|
| 1104 |
CONFIGURATION_LIST_COMPARE; |
| 1105 |
|
| 1106 |
/* Have to special case heap size, because we need 2 values. */ |
| 1107 |
if (strcmp(name, "heap_bytes") == 0) { |
| 1108 |
if ((ret = __db_getlong(dbenv, |
| 1109 |
NULL, value, 0, LONG_MAX, &val)) != 0) |
| 1110 |
goto nameerr; |
| 1111 |
heap_bytes = (u_int32_t)val; |
| 1112 |
} |
| 1113 |
if (strcmp(name, "heap_gbytes") == 0) { |
| 1114 |
if ((ret = __db_getlong(dbenv, |
| 1115 |
NULL, value, 0, LONG_MAX, &val)) != 0) |
| 1116 |
goto nameerr; |
| 1117 |
heap_gbytes = (u_int32_t)val; |
| 1118 |
} |
| 1119 |
|
| 1120 |
dbp->errx(dbp, DB_STR_A("5086", |
| 1121 |
"unknown input-file header configuration keyword \"%s\"", |
| 1122 |
"%s"), name); |
| 1123 |
goto err; |
| 1124 |
} |
| 1125 |
ret = 0; |
| 1126 |
|
| 1127 |
if (heap_gbytes != 0 || heap_bytes != 0) |
| 1128 |
if ((ret = dbp->set_heapsize(dbp, |
| 1129 |
heap_gbytes, heap_bytes, 0)) != 0) |
| 1130 |
goto heaperr; |
| 1131 |
|
| 1132 |
if (0) { |
| 1133 |
nameerr: dbp->err(dbp, ret, "%s: %s=%s", G(progname), name, value); |
| 1134 |
ret = 1; |
| 1135 |
} |
| 1136 |
if (0) { |
| 1137 |
heaperr: dbp->err(dbp, ret, "%s: heap_gbytes/heap_bytes=%lu/%lu", |
| 1138 |
G(progname), (u_long)heap_gbytes, (u_long)heap_bytes); |
| 1139 |
ret = 1; |
| 1140 |
} |
| 1141 |
if (0) { |
| 1142 |
badfmt: dbp->errx(dbp, DB_STR_A("5087", |
| 1143 |
"line %lu: unexpected format", "%lu"), G(lineno)); |
| 1144 |
ret = 1; |
| 1145 |
} |
| 1146 |
if (0) { |
| 1147 |
memerr: dbp->errx(dbp, DB_STR("5088", |
| 1148 |
"unable to allocate memory")); |
| 1149 |
err: ret = 1; |
| 1150 |
} |
| 1151 |
if (name != NULL) |
| 1152 |
free(name); |
| 1153 |
if (ret != 0) { |
| 1154 |
*part_keyp = NULL; |
| 1155 |
free_keys(keys); |
| 1156 |
} |
| 1157 |
return (ret); |
| 1158 |
} |
| 1159 |
|
| 1160 |
void free_keys(part_keys) |
| 1161 |
DBT *part_keys; |
| 1162 |
{ |
| 1163 |
DBT *kp; |
| 1164 |
if (part_keys != NULL) { |
| 1165 |
for (kp = part_keys; kp->data != NULL; kp++) |
| 1166 |
free(kp->data); |
| 1167 |
free(part_keys); |
| 1168 |
} |
| 1169 |
} |
| 1170 |
|
| 1171 |
/* |
| 1172 |
* Macro to convert a pair of hex bytes to a decimal value. |
| 1173 |
* |
| 1174 |
* !!! |
| 1175 |
* Note that this macro is side-effect safe. This was done deliberately, |
| 1176 |
* callers depend on it. |
| 1177 |
*/ |
| 1178 |
#define DIGITIZE(store, v1, v2) { \ |
| 1179 |
char _v1, _v2; \ |
| 1180 |
_v1 = (v1); \ |
| 1181 |
_v2 = (v2); \ |
| 1182 |
if ((_v1) > 'f' || (_v2) > 'f') \ |
| 1183 |
return (db_load_badend(dbenv)); \ |
| 1184 |
(store) = \ |
| 1185 |
((_v1) == '0' ? 0 : \ |
| 1186 |
((_v1) == '1' ? 1 : \ |
| 1187 |
((_v1) == '2' ? 2 : \ |
| 1188 |
((_v1) == '3' ? 3 : \ |
| 1189 |
((_v1) == '4' ? 4 : \ |
| 1190 |
((_v1) == '5' ? 5 : \ |
| 1191 |
((_v1) == '6' ? 6 : \ |
| 1192 |
((_v1) == '7' ? 7 : \ |
| 1193 |
((_v1) == '8' ? 8 : \ |
| 1194 |
((_v1) == '9' ? 9 : \ |
| 1195 |
((_v1) == 'a' ? 10 : \ |
| 1196 |
((_v1) == 'b' ? 11 : \ |
| 1197 |
((_v1) == 'c' ? 12 : \ |
| 1198 |
((_v1) == 'd' ? 13 : \ |
| 1199 |
((_v1) == 'e' ? 14 : 15))))))))))))))) << 4 | \ |
| 1200 |
((_v2) == '0' ? 0 : \ |
| 1201 |
((_v2) == '1' ? 1 : \ |
| 1202 |
((_v2) == '2' ? 2 : \ |
| 1203 |
((_v2) == '3' ? 3 : \ |
| 1204 |
((_v2) == '4' ? 4 : \ |
| 1205 |
((_v2) == '5' ? 5 : \ |
| 1206 |
((_v2) == '6' ? 6 : \ |
| 1207 |
((_v2) == '7' ? 7 : \ |
| 1208 |
((_v2) == '8' ? 8 : \ |
| 1209 |
((_v2) == '9' ? 9 : \ |
| 1210 |
((_v2) == 'a' ? 10 : \ |
| 1211 |
((_v2) == 'b' ? 11 : \ |
| 1212 |
((_v2) == 'c' ? 12 : \ |
| 1213 |
((_v2) == 'd' ? 13 : \ |
| 1214 |
((_v2) == 'e' ? 14 : 15))))))))))))))); \ |
| 1215 |
} |
| 1216 |
|
| 1217 |
/* |
| 1218 |
* convprintable -- |
| 1219 |
* Convert a printable-encoded string into a newly allocated string. |
| 1220 |
* |
| 1221 |
* In an ideal world, this would probably share code with dbt_rprint but |
| 1222 |
* that's set up to read character-by-character (to avoid large memory |
| 1223 |
* allocations that aren't likely to be a problem here), and this has fewer |
| 1224 |
* special cases to deal with. |
| 1225 |
* |
| 1226 |
* Note that despite the printable encoding, the char * interface to this |
| 1227 |
* function (which is, not coincidentally, also used for database naming) |
| 1228 |
* means that outstr cannot contain any nuls. |
| 1229 |
*/ |
| 1230 |
int |
| 1231 |
db_load_convprintable(dbenv, instr, outstrp) |
| 1232 |
DB_ENV *dbenv; |
| 1233 |
char *instr, **outstrp; |
| 1234 |
{ |
| 1235 |
char *outstr; |
| 1236 |
|
| 1237 |
/* |
| 1238 |
* Just malloc a string big enough for the whole input string; |
| 1239 |
* the output string will be smaller (or of equal length). |
| 1240 |
* |
| 1241 |
* Note that we may be passed a zero-length string and need to |
| 1242 |
* be able to duplicate it. |
| 1243 |
*/ |
| 1244 |
if ((outstr = malloc(strlen(instr) + 1)) == NULL) |
| 1245 |
return (ENOMEM); |
| 1246 |
|
| 1247 |
*outstrp = outstr; |
| 1248 |
|
| 1249 |
for ( ; *instr != '\0'; instr++) |
| 1250 |
if (*instr == '\\') { |
| 1251 |
if (*++instr == '\\') { |
| 1252 |
*outstr++ = '\\'; |
| 1253 |
continue; |
| 1254 |
} |
| 1255 |
DIGITIZE(*outstr++, *instr, *++instr); |
| 1256 |
} else |
| 1257 |
*outstr++ = *instr; |
| 1258 |
|
| 1259 |
*outstr = '\0'; |
| 1260 |
|
| 1261 |
return (0); |
| 1262 |
} |
| 1263 |
|
| 1264 |
/* |
| 1265 |
* dbt_rprint -- |
| 1266 |
* Read a printable line into a DBT structure. |
| 1267 |
*/ |
| 1268 |
int |
| 1269 |
db_load_dbt_rprint(dbenv, dbtp) |
| 1270 |
DB_ENV *dbenv; |
| 1271 |
DBT *dbtp; |
| 1272 |
{ |
| 1273 |
u_int32_t len; |
| 1274 |
u_int8_t *p; |
| 1275 |
int c1, c2, escape, first; |
| 1276 |
char buf[32]; |
| 1277 |
|
| 1278 |
++G(lineno); |
| 1279 |
|
| 1280 |
first = 1; |
| 1281 |
escape = 0; |
| 1282 |
for (p = dbtp->data, len = 0; (c1 = getchar()) != '\n';) { |
| 1283 |
if (c1 == EOF) { |
| 1284 |
if (len == 0) { |
| 1285 |
G(endofile) = G(endodata) = 1; |
| 1286 |
return (0); |
| 1287 |
} |
| 1288 |
return (db_load_badend(dbenv)); |
| 1289 |
} |
| 1290 |
if (first) { |
| 1291 |
first = 0; |
| 1292 |
if (G(version) > 1) { |
| 1293 |
if (c1 != ' ') { |
| 1294 |
buf[0] = c1; |
| 1295 |
if (fgets(buf + 1, |
| 1296 |
sizeof(buf) - 1, stdin) == NULL || |
| 1297 |
strcmp(buf, "DATA=END\n") != 0) |
| 1298 |
return (db_load_badend(dbenv)); |
| 1299 |
G(endodata) = 1; |
| 1300 |
return (0); |
| 1301 |
} |
| 1302 |
continue; |
| 1303 |
} |
| 1304 |
} |
| 1305 |
if (escape) { |
| 1306 |
if (c1 != '\\') { |
| 1307 |
if ((c2 = getchar()) == EOF) |
| 1308 |
return (db_load_badend(dbenv)); |
| 1309 |
DIGITIZE(c1, c1, c2); |
| 1310 |
} |
| 1311 |
escape = 0; |
| 1312 |
} else |
| 1313 |
if (c1 == '\\') { |
| 1314 |
escape = 1; |
| 1315 |
continue; |
| 1316 |
} |
| 1317 |
if (len >= dbtp->ulen - 10) { |
| 1318 |
dbtp->ulen *= 2; |
| 1319 |
if ((dbtp->data = |
| 1320 |
realloc(dbtp->data, dbtp->ulen)) == NULL) { |
| 1321 |
dbenv->err(dbenv, ENOMEM, NULL); |
| 1322 |
return (1); |
| 1323 |
} |
| 1324 |
p = (u_int8_t *)dbtp->data + len; |
| 1325 |
} |
| 1326 |
++len; |
| 1327 |
*p++ = c1; |
| 1328 |
} |
| 1329 |
dbtp->size = len; |
| 1330 |
|
| 1331 |
return (0); |
| 1332 |
} |
| 1333 |
|
| 1334 |
/* |
| 1335 |
* dbt_rdump -- |
| 1336 |
* Read a byte dump line into a DBT structure. |
| 1337 |
*/ |
| 1338 |
int |
| 1339 |
db_load_dbt_rdump(dbenv, dbtp) |
| 1340 |
DB_ENV *dbenv; |
| 1341 |
DBT *dbtp; |
| 1342 |
{ |
| 1343 |
u_int32_t len; |
| 1344 |
u_int8_t *p; |
| 1345 |
int c1, c2, first; |
| 1346 |
char buf[32]; |
| 1347 |
|
| 1348 |
++G(lineno); |
| 1349 |
|
| 1350 |
first = 1; |
| 1351 |
for (p = dbtp->data, len = 0; (c1 = getchar()) != '\n';) { |
| 1352 |
if (c1 == EOF) { |
| 1353 |
if (len == 0) { |
| 1354 |
G(endofile) = G(endodata) = 1; |
| 1355 |
return (0); |
| 1356 |
} |
| 1357 |
return (db_load_badend(dbenv)); |
| 1358 |
} |
| 1359 |
if (first) { |
| 1360 |
first = 0; |
| 1361 |
if (G(version) > 1) { |
| 1362 |
if (c1 != ' ') { |
| 1363 |
buf[0] = c1; |
| 1364 |
if (fgets(buf + 1, |
| 1365 |
sizeof(buf) - 1, stdin) == NULL || |
| 1366 |
strcmp(buf, "DATA=END\n") != 0) |
| 1367 |
return (db_load_badend(dbenv)); |
| 1368 |
G(endodata) = 1; |
| 1369 |
return (0); |
| 1370 |
} |
| 1371 |
continue; |
| 1372 |
} |
| 1373 |
} |
| 1374 |
if ((c2 = getchar()) == EOF) |
| 1375 |
return (db_load_badend(dbenv)); |
| 1376 |
if (len >= dbtp->ulen - 10) { |
| 1377 |
dbtp->ulen *= 2; |
| 1378 |
if ((dbtp->data = |
| 1379 |
realloc(dbtp->data, dbtp->ulen)) == NULL) { |
| 1380 |
dbenv->err(dbenv, ENOMEM, NULL); |
| 1381 |
return (1); |
| 1382 |
} |
| 1383 |
p = (u_int8_t *)dbtp->data + len; |
| 1384 |
} |
| 1385 |
++len; |
| 1386 |
DIGITIZE(*p++, c1, c2); |
| 1387 |
} |
| 1388 |
dbtp->size = len; |
| 1389 |
|
| 1390 |
return (0); |
| 1391 |
} |
| 1392 |
|
| 1393 |
/* |
| 1394 |
* dbt_rrecno -- |
| 1395 |
* Read a record number dump line into a DBT structure. |
| 1396 |
*/ |
| 1397 |
int |
| 1398 |
db_load_dbt_rrecno(dbenv, dbtp, ishex) |
| 1399 |
DB_ENV *dbenv; |
| 1400 |
DBT *dbtp; |
| 1401 |
int ishex; |
| 1402 |
{ |
| 1403 |
char buf[32], *p, *q; |
| 1404 |
u_long recno; |
| 1405 |
|
| 1406 |
++G(lineno); |
| 1407 |
|
| 1408 |
if (fgets(buf, sizeof(buf), stdin) == NULL) { |
| 1409 |
G(endofile) = G(endodata) = 1; |
| 1410 |
return (0); |
| 1411 |
} |
| 1412 |
|
| 1413 |
if (strcmp(buf, "DATA=END\n") == 0) { |
| 1414 |
G(endodata) = 1; |
| 1415 |
return (0); |
| 1416 |
} |
| 1417 |
|
| 1418 |
if (buf[0] != ' ') |
| 1419 |
goto err; |
| 1420 |
|
| 1421 |
/* |
| 1422 |
* If we're expecting a hex key, do an in-place conversion |
| 1423 |
* of hex to straight ASCII before calling __db_getulong(). |
| 1424 |
*/ |
| 1425 |
if (ishex) { |
| 1426 |
for (p = q = buf + 1; *q != '\0' && *q != '\n';) { |
| 1427 |
/* |
| 1428 |
* 0-9 in hex are 0x30-0x39, so this is easy. |
| 1429 |
* We should alternate between 3's and [0-9], and |
| 1430 |
* if the [0-9] are something unexpected, |
| 1431 |
* __db_getulong will fail, so we only need to catch |
| 1432 |
* end-of-string conditions. |
| 1433 |
*/ |
| 1434 |
if (*q++ != '3') |
| 1435 |
goto err; |
| 1436 |
if (*q == '\n' || *q == '\0') |
| 1437 |
goto err; |
| 1438 |
*p++ = *q++; |
| 1439 |
} |
| 1440 |
*p = '\0'; |
| 1441 |
} |
| 1442 |
|
| 1443 |
if (__db_getulong(dbenv, G(progname), buf + 1, 0, 0, &recno)) |
| 1444 |
goto err; |
| 1445 |
|
| 1446 |
*((db_recno_t *)dbtp->data) = recno; |
| 1447 |
dbtp->size = sizeof(db_recno_t); |
| 1448 |
return (0); |
| 1449 |
|
| 1450 |
err: return (db_load_badend(dbenv)); |
| 1451 |
} |
| 1452 |
|
| 1453 |
int |
| 1454 |
db_load_dbt_to_recno(dbenv, dbt, recnop) |
| 1455 |
DB_ENV *dbenv; |
| 1456 |
DBT *dbt; |
| 1457 |
db_recno_t *recnop; |
| 1458 |
{ |
| 1459 |
char buf[32]; /* Large enough for 2^64. */ |
| 1460 |
|
| 1461 |
memcpy(buf, dbt->data, dbt->size); |
| 1462 |
buf[dbt->size] = '\0'; |
| 1463 |
|
| 1464 |
return (__db_getulong(dbenv, G(progname), buf, 0, 0, (u_long *)recnop)); |
| 1465 |
} |
| 1466 |
|
| 1467 |
/* |
| 1468 |
* badnum -- |
| 1469 |
* Display the bad number message. |
| 1470 |
*/ |
| 1471 |
void |
| 1472 |
db_load_badnum(dbenv) |
| 1473 |
DB_ENV *dbenv; |
| 1474 |
{ |
| 1475 |
dbenv->errx(dbenv, DB_STR("5089", |
| 1476 |
"boolean name=value pairs require a value of 0 or 1")); |
| 1477 |
} |
| 1478 |
|
| 1479 |
/* |
| 1480 |
* badend -- |
| 1481 |
* Display the bad end to input message. |
| 1482 |
*/ |
| 1483 |
int |
| 1484 |
db_load_badend(dbenv) |
| 1485 |
DB_ENV *dbenv; |
| 1486 |
{ |
| 1487 |
dbenv->errx(dbenv, DB_STR("5090", |
| 1488 |
"unexpected end of input data or key/data pair")); |
| 1489 |
return (1); |
| 1490 |
} |
| 1491 |
|
| 1492 |
/* |
| 1493 |
* usage -- |
| 1494 |
* Display the usage message. |
| 1495 |
*/ |
| 1496 |
int |
| 1497 |
db_load_usage() |
| 1498 |
{ |
| 1499 |
(void)fprintf(stderr, "usage: %s %s\n\t%s\n", progname, |
| 1500 |
"[-nTV] [-c name=value] [-f file]", |
| 1501 |
"[-h home] [-P password] [-t btree | hash | recno | queue] db_file"); |
| 1502 |
(void)fprintf(stderr, "usage: %s %s\n", |
| 1503 |
progname, "-r lsn | fileid [-h home] [-P password] db_file"); |
| 1504 |
return (EXIT_FAILURE); |
| 1505 |
} |
| 1506 |
|
| 1507 |
int |
| 1508 |
db_load_version_check() |
| 1509 |
{ |
| 1510 |
int v_major, v_minor, v_patch; |
| 1511 |
|
| 1512 |
/* Make sure we're loaded with the right version of the DB library. */ |
| 1513 |
(void)db_version(&v_major, &v_minor, &v_patch); |
| 1514 |
if (v_major != DB_VERSION_MAJOR || v_minor != DB_VERSION_MINOR) { |
| 1515 |
fprintf(stderr, DB_STR_A("5091", |
| 1516 |
"%s: version %d.%d doesn't match library version %d.%d\n", |
| 1517 |
"%s %d %d %d %d\n"), progname, DB_VERSION_MAJOR, |
| 1518 |
DB_VERSION_MINOR, v_major, v_minor); |
| 1519 |
return (EXIT_FAILURE); |
| 1520 |
} |
| 1521 |
return (0); |
| 1522 |
} |