1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594
|
/*-
* See the file LICENSE for redistribution information.
*
* Copyright (c) 1996-2002
* Sleepycat Software. All rights reserved.
*/
#include "db_config.h"
#ifndef lint
static const char copyright[] =
"Copyright (c) 1996-2002\nSleepycat Software Inc. All rights reserved.\n";
static const char revid[] =
"$Id: db185.c,v 1.1.1.1 2003/11/20 22:13:15 toshok Exp $";
#endif
#ifndef NO_SYSTEM_INCLUDES
#include <sys/types.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#endif
#include "db_int.h"
#include "db185_int.h"
static int db185_close __P((DB185 *));
static int db185_compare __P((DB *, const DBT *, const DBT *));
static int db185_del __P((const DB185 *, const DBT185 *, u_int));
static int db185_fd __P((const DB185 *));
static int db185_get __P((const DB185 *, const DBT185 *, DBT185 *, u_int));
static u_int32_t
db185_hash __P((DB *, const void *, u_int32_t));
static void db185_openstderr __P((DB_FH *));
static size_t db185_prefix __P((DB *, const DBT *, const DBT *));
static int db185_put __P((const DB185 *, DBT185 *, const DBT185 *, u_int));
static int db185_seq __P((const DB185 *, DBT185 *, DBT185 *, u_int));
static int db185_sync __P((const DB185 *, u_int));
/*
* EXTERN: #ifdef _DB185_INT_H_
* EXTERN: DB185 *__db185_open
* EXTERN: __P((const char *, int, int, DBTYPE, const void *));
* EXTERN: #else
* EXTERN: DB *__db185_open
* EXTERN: __P((const char *, int, int, DBTYPE, const void *));
* EXTERN: #endif
*/
DB185 *
__db185_open(file, oflags, mode, type, openinfo)
const char *file;
int oflags, mode;
DBTYPE type;
const void *openinfo;
{
const BTREEINFO *bi;
const HASHINFO *hi;
const RECNOINFO *ri;
DB *dbp;
DB185 *db185p;
DB_FH fh;
size_t nw;
int ret;
dbp = NULL;
db185p = NULL;
if ((ret = db_create(&dbp, NULL, 0)) != 0)
goto err;
if ((ret = __os_calloc(NULL, 1, sizeof(DB185), &db185p)) != 0)
goto err;
/*
* !!!
* The DBTYPE enum wasn't initialized in DB 185, so it's off-by-one
* from DB 2.0.
*/
switch (type) {
case 0: /* DB_BTREE */
type = DB_BTREE;
if ((bi = openinfo) != NULL) {
if (bi->flags & ~R_DUP)
goto einval;
if (bi->flags & R_DUP)
(void)dbp->set_flags(dbp, DB_DUP);
if (bi->cachesize != 0)
(void)dbp->set_cachesize
(dbp, 0, bi->cachesize, 0);
if (bi->minkeypage != 0)
(void)dbp->set_bt_minkey(dbp, bi->minkeypage);
if (bi->psize != 0)
(void)dbp->set_pagesize(dbp, bi->psize);
/*
* !!!
* Comparisons and prefix calls work because the DBT
* structures in 1.85 and 2.0 have the same initial
* fields.
*/
if (bi->prefix != NULL) {
db185p->prefix = bi->prefix;
dbp->set_bt_prefix(dbp, db185_prefix);
}
if (bi->compare != NULL) {
db185p->compare = bi->compare;
dbp->set_bt_compare(dbp, db185_compare);
}
if (bi->lorder != 0)
dbp->set_lorder(dbp, bi->lorder);
}
break;
case 1: /* DB_HASH */
type = DB_HASH;
if ((hi = openinfo) != NULL) {
if (hi->bsize != 0)
(void)dbp->set_pagesize(dbp, hi->bsize);
if (hi->ffactor != 0)
(void)dbp->set_h_ffactor(dbp, hi->ffactor);
if (hi->nelem != 0)
(void)dbp->set_h_nelem(dbp, hi->nelem);
if (hi->cachesize != 0)
(void)dbp->set_cachesize
(dbp, 0, hi->cachesize, 0);
if (hi->hash != NULL) {
db185p->hash = hi->hash;
(void)dbp->set_h_hash(dbp, db185_hash);
}
if (hi->lorder != 0)
dbp->set_lorder(dbp, hi->lorder);
}
break;
case 2: /* DB_RECNO */
type = DB_RECNO;
/* DB 1.85 did renumbering by default. */
(void)dbp->set_flags(dbp, DB_RENUMBER);
/*
* !!!
* The file name given to DB 1.85 recno is the name of the DB
* 2.0 backing file. If the file doesn't exist, create it if
* the user has the O_CREAT flag set, DB 1.85 did it for you,
* and DB 2.0 doesn't.
*
* !!!
* Setting the file name to NULL specifies that we're creating
* a temporary backing file, in DB 2.X. If we're opening the
* DB file read-only, change the flags to read-write, because
* temporary backing files cannot be opened read-only, and DB
* 2.X will return an error. We are cheating here -- if the
* application does a put on the database, it will succeed --
* although that would be a stupid thing for the application
* to do.
*
* !!!
* Note, the file name in DB 1.85 was a const -- we don't do
* that in DB 2.0, so do that cast.
*/
if (file != NULL) {
if (oflags & O_CREAT && __os_exists(file, NULL) != 0)
if (__os_openhandle(NULL, file,
oflags, mode, &fh) == 0)
(void)__os_closehandle(NULL, &fh);
(void)dbp->set_re_source(dbp, file);
if (O_RDONLY)
oflags &= ~O_RDONLY;
oflags |= O_RDWR;
file = NULL;
}
if ((ri = openinfo) != NULL) {
/*
* !!!
* We can't support the bfname field.
*/
#define BFMSG "DB: DB 1.85's recno bfname field is not supported.\n"
if (ri->bfname != NULL) {
db185_openstderr(&fh);
(void)__os_write(NULL, &fh,
BFMSG, sizeof(BFMSG) - 1, &nw);
goto einval;
}
if (ri->flags & ~(R_FIXEDLEN | R_NOKEY | R_SNAPSHOT))
goto einval;
if (ri->flags & R_FIXEDLEN) {
if (ri->bval != 0)
(void)dbp->set_re_pad(dbp, ri->bval);
if (ri->reclen != 0)
(void)dbp->set_re_len(dbp, ri->reclen);
} else
if (ri->bval != 0)
(void)dbp->set_re_delim(dbp, ri->bval);
/*
* !!!
* We ignore the R_NOKEY flag, but that's okay, it was
* only an optimization that was never implemented.
*/
if (ri->flags & R_SNAPSHOT)
(void)dbp->set_flags(dbp, DB_SNAPSHOT);
if (ri->cachesize != 0)
(void)dbp->set_cachesize
(dbp, 0, ri->cachesize, 0);
if (ri->psize != 0)
(void)dbp->set_pagesize(dbp, ri->psize);
if (ri->lorder != 0)
dbp->set_lorder(dbp, ri->lorder);
}
break;
default:
goto einval;
}
db185p->close = db185_close;
db185p->del = db185_del;
db185p->fd = db185_fd;
db185p->get = db185_get;
db185p->put = db185_put;
db185p->seq = db185_seq;
db185p->sync = db185_sync;
/*
* Store a reference so we can indirect from the DB 1.85 structure
* to the underlying DB structure, and vice-versa. This has to be
* done BEFORE the DB::open method call because the hash callback
* is exercised as part of hash database initialiation.
*/
db185p->dbp = dbp;
dbp->api_internal = db185p;
/* Open the database. */
if ((ret = dbp->open(dbp, NULL,
file, NULL, type, __db_oflags(oflags), mode)) != 0)
goto err;
/* Create the cursor used for sequential ops. */
if ((ret = dbp->cursor(dbp, NULL, &((DB185 *)db185p)->dbc, 0)) != 0)
goto err;
return (db185p);
err: if (ret < 0) /* DB 1.85 can't handle DB 2.0's errors. */
einval: ret = EINVAL;
if (db185p != NULL)
__os_free(NULL, db185p);
if (dbp != NULL)
(void)dbp->close(dbp, 0);
__os_set_errno(ret);
return (NULL);
}
static int
db185_close(db185p)
DB185 *db185p;
{
DB *dbp;
int ret;
dbp = db185p->dbp;
ret = dbp->close(dbp, 0);
__os_free(NULL, db185p);
if (ret == 0)
return (0);
if (ret < 0) /* DB 1.85 can't handle DB 2.0's errors. */
ret = EINVAL;
__os_set_errno(ret);
return (-1);
}
static int
db185_del(db185p, key185, flags)
const DB185 *db185p;
const DBT185 *key185;
u_int flags;
{
DB *dbp;
DBT key;
int ret;
dbp = db185p->dbp;
memset(&key, 0, sizeof(key));
key.data = key185->data;
key.size = key185->size;
if (flags & ~R_CURSOR)
goto einval;
if (flags & R_CURSOR)
ret = db185p->dbc->c_del(db185p->dbc, 0);
else
ret = dbp->del(dbp, NULL, &key, 0);
switch (ret) {
case 0:
return (0);
case DB_NOTFOUND:
return (1);
}
if (ret < 0) /* DB 1.85 can't handle DB 2.0's errors. */
einval: ret = EINVAL;
__os_set_errno(ret);
return (-1);
}
static int
db185_fd(db185p)
const DB185 *db185p;
{
DB *dbp;
int fd, ret;
dbp = db185p->dbp;
if ((ret = dbp->fd(dbp, &fd)) == 0)
return (fd);
if (ret < 0) /* DB 1.85 can't handle DB 2.0's errors. */
ret = EINVAL;
__os_set_errno(ret);
return (-1);
}
static int
db185_get(db185p, key185, data185, flags)
const DB185 *db185p;
const DBT185 *key185;
DBT185 *data185;
u_int flags;
{
DB *dbp;
DBT key, data;
int ret;
dbp = db185p->dbp;
memset(&key, 0, sizeof(key));
key.data = key185->data;
key.size = key185->size;
memset(&data, 0, sizeof(data));
data.data = data185->data;
data.size = data185->size;
if (flags)
goto einval;
switch (ret = dbp->get(dbp, NULL, &key, &data, 0)) {
case 0:
data185->data = data.data;
data185->size = data.size;
return (0);
case DB_NOTFOUND:
return (1);
}
if (ret < 0) /* DB 1.85 can't handle DB 2.0's errors. */
einval: ret = EINVAL;
__os_set_errno(ret);
return (-1);
}
static int
db185_put(db185p, key185, data185, flags)
const DB185 *db185p;
DBT185 *key185;
const DBT185 *data185;
u_int flags;
{
DB *dbp;
DBC *dbcp_put;
DBT key, data;
int ret, t_ret;
dbp = db185p->dbp;
memset(&key, 0, sizeof(key));
key.data = key185->data;
key.size = key185->size;
memset(&data, 0, sizeof(data));
data.data = data185->data;
data.size = data185->size;
switch (flags) {
case 0:
ret = dbp->put(dbp, NULL, &key, &data, 0);
break;
case R_CURSOR:
ret = db185p->dbc->c_put(db185p->dbc, &key, &data, DB_CURRENT);
break;
case R_IAFTER:
case R_IBEFORE:
if (dbp->type != DB_RECNO)
goto einval;
if ((ret = dbp->cursor(dbp, NULL, &dbcp_put, 0)) != 0)
break;
if ((ret =
dbcp_put->c_get(dbcp_put, &key, &data, DB_SET)) == 0) {
memset(&data, 0, sizeof(data));
data.data = data185->data;
data.size = data185->size;
ret = dbcp_put->c_put(dbcp_put, &key, &data,
flags == R_IAFTER ? DB_AFTER : DB_BEFORE);
}
if ((t_ret = dbcp_put->c_close(dbcp_put)) != 0 && ret == 0)
ret = t_ret;
break;
case R_NOOVERWRITE:
ret = dbp->put(dbp, NULL, &key, &data, DB_NOOVERWRITE);
break;
case R_SETCURSOR:
if (dbp->type != DB_BTREE && dbp->type != DB_RECNO)
goto einval;
if ((ret = dbp->put(dbp, NULL, &key, &data, 0)) != 0)
break;
ret =
db185p->dbc->c_get(db185p->dbc, &key, &data, DB_SET_RANGE);
break;
default:
goto einval;
}
switch (ret) {
case 0:
key185->data = key.data;
key185->size = key.size;
return (0);
case DB_KEYEXIST:
return (1);
}
if (ret < 0) /* DB 1.85 can't handle DB 2.0's errors. */
einval: ret = EINVAL;
__os_set_errno(ret);
return (-1);
}
static int
db185_seq(db185p, key185, data185, flags)
const DB185 *db185p;
DBT185 *key185, *data185;
u_int flags;
{
DB *dbp;
DBT key, data;
int ret;
dbp = db185p->dbp;
memset(&key, 0, sizeof(key));
key.data = key185->data;
key.size = key185->size;
memset(&data, 0, sizeof(data));
data.data = data185->data;
data.size = data185->size;
switch (flags) {
case R_CURSOR:
flags = DB_SET_RANGE;
break;
case R_FIRST:
flags = DB_FIRST;
break;
case R_LAST:
if (dbp->type != DB_BTREE && dbp->type != DB_RECNO)
goto einval;
flags = DB_LAST;
break;
case R_NEXT:
flags = DB_NEXT;
break;
case R_PREV:
if (dbp->type != DB_BTREE && dbp->type != DB_RECNO)
goto einval;
flags = DB_PREV;
break;
default:
goto einval;
}
switch (ret = db185p->dbc->c_get(db185p->dbc, &key, &data, flags)) {
case 0:
key185->data = key.data;
key185->size = key.size;
data185->data = data.data;
data185->size = data.size;
return (0);
case DB_NOTFOUND:
return (1);
}
if (ret < 0) /* DB 1.85 can't handle DB 2.0's errors. */
einval: ret = EINVAL;
__os_set_errno(ret);
return (-1);
}
static int
db185_sync(db185p, flags)
const DB185 *db185p;
u_int flags;
{
DB *dbp;
DB_FH fh;
size_t nw;
int ret;
dbp = db185p->dbp;
switch (flags) {
case 0:
break;
case R_RECNOSYNC:
/*
* !!!
* We can't support the R_RECNOSYNC flag.
*/
#define RSMSG "DB: DB 1.85's R_RECNOSYNC sync flag is not supported.\n"
db185_openstderr(&fh);
(void)__os_write(NULL, &fh, RSMSG, sizeof(RSMSG) - 1, &nw);
goto einval;
default:
goto einval;
}
if ((ret = dbp->sync(dbp, 0)) == 0)
return (0);
if (ret < 0) /* DB 1.85 can't handle DB 2.0's errors. */
einval: ret = EINVAL;
__os_set_errno(ret);
return (-1);
}
static void
db185_openstderr(fhp)
DB_FH *fhp;
{
/* Dummy up the results of an __os_openhandle() on stderr. */
memset(fhp, 0, sizeof(*fhp));
F_SET(fhp, DB_FH_VALID);
#ifndef STDERR_FILENO
#define STDERR_FILENO 2
#endif
fhp->fd = STDERR_FILENO;
}
/*
* db185_compare --
* Cutout routine to call the user's Btree comparison function.
*/
static int
db185_compare(dbp, a, b)
DB *dbp;
const DBT *a, *b;
{
return (((DB185 *)dbp->api_internal)->compare(a, b));
}
/*
* db185_prefix --
* Cutout routine to call the user's Btree prefix function.
*/
static size_t
db185_prefix(dbp, a, b)
DB *dbp;
const DBT *a, *b;
{
return (((DB185 *)dbp->api_internal)->prefix(a, b));
}
/*
* db185_hash --
* Cutout routine to call the user's hash function.
*/
static u_int32_t
db185_hash(dbp, key, len)
DB *dbp;
const void *key;
u_int32_t len;
{
return (((DB185 *)dbp->api_internal)->hash(key, (size_t)len));
}
|