| 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 |
#if defined(LIBC_SCCS) && !defined(lint) |
| 38 |
static char sccsid[] = "@(#)bt_open.c 8.10 (Berkeley) 8/17/94"; |
| 39 |
#endif |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
#include <sys/param.h> |
| 50 |
#include <sys/stat.h> |
| 51 |
|
| 52 |
#include <errno.h> |
| 53 |
#include <fcntl.h> |
| 54 |
#include <limits.h> |
| 55 |
#include <signal.h> |
| 56 |
#include <stdio.h> |
| 57 |
#include <stdlib.h> |
| 58 |
#include <string.h> |
| 59 |
#include <unistd.h> |
| 60 |
|
| 61 |
#include "../include/db.h" |
| 62 |
#include "btree.h" |
| 63 |
|
| 64 |
#ifdef DEBUG |
| 65 |
#undef MINPSIZE |
| 66 |
#define MINPSIZE 128 |
| 67 |
#endif |
| 68 |
|
| 69 |
static int byteorder __P((void)); |
| 70 |
static int nroot __P((BTREE *)); |
| 71 |
static int tmp __P((void)); |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
DB * |
| 90 |
__bt_open(fname, flags, mode, openinfo, dflags) |
| 91 |
const char *fname; |
| 92 |
int flags, mode, dflags; |
| 93 |
const BTREEINFO *openinfo; |
| 94 |
{ |
| 95 |
struct stat sb; |
| 96 |
BTMETA m; |
| 97 |
BTREE *t; |
| 98 |
BTREEINFO b; |
| 99 |
DB *dbp; |
| 100 |
pgno_t ncache; |
| 101 |
ssize_t nr; |
| 102 |
int machine_lorder; |
| 103 |
|
| 104 |
t = NULL; |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
machine_lorder = byteorder(); |
| 114 |
if (openinfo) { |
| 115 |
b = *openinfo; |
| 116 |
|
| 117 |
|
| 118 |
if (b.flags & ~(R_DUP)) |
| 119 |
goto einval; |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
if (b.psize && |
| 127 |
(b.psize < MINPSIZE || b.psize > MAX_PAGE_OFFSET + 1 || |
| 128 |
b.psize & (sizeof(indx_t) - 1))) |
| 129 |
goto einval; |
| 130 |
|
| 131 |
|
| 132 |
if (b.minkeypage) { |
| 133 |
if (b.minkeypage < 2) |
| 134 |
goto einval; |
| 135 |
} else |
| 136 |
b.minkeypage = DEFMINKEYPAGE; |
| 137 |
|
| 138 |
|
| 139 |
if (b.compare == NULL) { |
| 140 |
b.compare = __bt_defcmp; |
| 141 |
if (b.prefix == NULL) |
| 142 |
b.prefix = __bt_defpfx; |
| 143 |
} |
| 144 |
|
| 145 |
if (b.lorder == 0) |
| 146 |
b.lorder = machine_lorder; |
| 147 |
} else { |
| 148 |
b.compare = __bt_defcmp; |
| 149 |
b.cachesize = 0; |
| 150 |
b.flags = 0; |
| 151 |
b.lorder = machine_lorder; |
| 152 |
b.minkeypage = DEFMINKEYPAGE; |
| 153 |
b.prefix = __bt_defpfx; |
| 154 |
b.psize = 0; |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
if (b.lorder != BIG_ENDIAN && b.lorder != LITTLE_ENDIAN) |
| 159 |
goto einval; |
| 160 |
|
| 161 |
|
| 162 |
if ((t = (BTREE *)malloc(sizeof(BTREE))) == NULL) |
| 163 |
goto err; |
| 164 |
memset(t, 0, sizeof(BTREE)); |
| 165 |
t->bt_fd = -1; |
| 166 |
t->bt_lorder = b.lorder; |
| 167 |
t->bt_order = NOT; |
| 168 |
t->bt_cmp = b.compare; |
| 169 |
t->bt_pfx = b.prefix; |
| 170 |
t->bt_rfd = -1; |
| 171 |
|
| 172 |
if ((t->bt_dbp = dbp = (DB *)malloc(sizeof(DB))) == NULL) |
| 173 |
goto err; |
| 174 |
memset(t->bt_dbp, 0, sizeof(DB)); |
| 175 |
if (t->bt_lorder != machine_lorder) |
| 176 |
F_SET(t, B_NEEDSWAP); |
| 177 |
|
| 178 |
dbp->type = DB_BTREE; |
| 179 |
dbp->internal = t; |
| 180 |
dbp->close = __bt_close; |
| 181 |
dbp->del = __bt_delete; |
| 182 |
dbp->fd = __bt_fd; |
| 183 |
dbp->get = __bt_get; |
| 184 |
dbp->put = __bt_put; |
| 185 |
dbp->seq = __bt_seq; |
| 186 |
dbp->sync = __bt_sync; |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
if (fname) { |
| 193 |
switch (flags & O_ACCMODE) { |
| 194 |
case O_RDONLY: |
| 195 |
F_SET(t, B_RDONLY); |
| 196 |
break; |
| 197 |
case O_RDWR: |
| 198 |
break; |
| 199 |
case O_WRONLY: |
| 200 |
default: |
| 201 |
goto einval; |
| 202 |
} |
| 203 |
|
| 204 |
if ((t->bt_fd = open(fname, flags, mode)) < 0) |
| 205 |
goto err; |
| 206 |
|
| 207 |
} else { |
| 208 |
if ((flags & O_ACCMODE) != O_RDWR) |
| 209 |
goto einval; |
| 210 |
if ((t->bt_fd = tmp()) == -1) |
| 211 |
goto err; |
| 212 |
F_SET(t, B_INMEM); |
| 213 |
} |
| 214 |
|
| 215 |
if (fcntl(t->bt_fd, F_SETFD, 1) == -1) |
| 216 |
goto err; |
| 217 |
|
| 218 |
if (fstat(t->bt_fd, &sb)) |
| 219 |
goto err; |
| 220 |
if (sb.st_size) { |
| 221 |
if ((nr = read(t->bt_fd, &m, sizeof(BTMETA))) < 0) |
| 222 |
goto err; |
| 223 |
if (nr != sizeof(BTMETA)) |
| 224 |
goto eftype; |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
if (m.magic == BTREEMAGIC) |
| 235 |
F_CLR(t, B_NEEDSWAP); |
| 236 |
else { |
| 237 |
F_SET(t, B_NEEDSWAP); |
| 238 |
M_32_SWAP(m.magic); |
| 239 |
M_32_SWAP(m.version); |
| 240 |
M_32_SWAP(m.psize); |
| 241 |
M_32_SWAP(m.free); |
| 242 |
M_32_SWAP(m.nrecs); |
| 243 |
M_32_SWAP(m.flags); |
| 244 |
} |
| 245 |
if (m.magic != BTREEMAGIC || m.version != BTREEVERSION) |
| 246 |
goto eftype; |
| 247 |
if (m.psize < MINPSIZE || m.psize > MAX_PAGE_OFFSET + 1 || |
| 248 |
m.psize & (sizeof(indx_t) - 1)) |
| 249 |
goto eftype; |
| 250 |
if (m.flags & ~SAVEMETA) |
| 251 |
goto eftype; |
| 252 |
b.psize = m.psize; |
| 253 |
F_SET(t, m.flags); |
| 254 |
t->bt_free = m.free; |
| 255 |
t->bt_nrecs = m.nrecs; |
| 256 |
} else { |
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
if (b.psize == 0) { |
| 262 |
#ifdef _STATBUF_ST_BLKSIZE |
| 263 |
b.psize = sb.st_blksize; |
| 264 |
#endif |
| 265 |
if (b.psize < MINPSIZE) |
| 266 |
b.psize = MINPSIZE; |
| 267 |
if (b.psize > MAX_PAGE_OFFSET + 1) |
| 268 |
b.psize = MAX_PAGE_OFFSET + 1; |
| 269 |
} |
| 270 |
|
| 271 |
|
| 272 |
if (!(b.flags & R_DUP)) |
| 273 |
F_SET(t, B_NODUPS); |
| 274 |
|
| 275 |
t->bt_free = P_INVALID; |
| 276 |
t->bt_nrecs = 0; |
| 277 |
F_SET(t, B_METADIRTY); |
| 278 |
} |
| 279 |
|
| 280 |
t->bt_psize = b.psize; |
| 281 |
|
| 282 |
|
| 283 |
if (b.cachesize && b.cachesize & (b.psize - 1)) |
| 284 |
b.cachesize += (~b.cachesize & (b.psize - 1)) + 1; |
| 285 |
if (b.cachesize < b.psize * MINCACHE) |
| 286 |
b.cachesize = b.psize * MINCACHE; |
| 287 |
|
| 288 |
|
| 289 |
ncache = (b.cachesize + t->bt_psize - 1) / t->bt_psize; |
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
t->bt_ovflsize = (t->bt_psize - BTDATAOFF) / b.minkeypage - |
| 303 |
(sizeof(indx_t) + NBLEAFDBT(0, 0)); |
| 304 |
if (t->bt_ovflsize < NBLEAFDBT(NOVFLSIZE, NOVFLSIZE) + sizeof(indx_t)) |
| 305 |
t->bt_ovflsize = |
| 306 |
NBLEAFDBT(NOVFLSIZE, NOVFLSIZE) + sizeof(indx_t); |
| 307 |
|
| 308 |
|
| 309 |
if ((t->bt_mp = |
| 310 |
mpool_open(NULL, t->bt_fd, t->bt_psize, ncache)) == NULL) |
| 311 |
goto err; |
| 312 |
if (!F_ISSET(t, B_INMEM)) |
| 313 |
mpool_filter(t->bt_mp, __bt_pgin, __bt_pgout, t); |
| 314 |
|
| 315 |
|
| 316 |
if (nroot(t) == RET_ERROR) |
| 317 |
goto err; |
| 318 |
|
| 319 |
|
| 320 |
if (dflags & DB_LOCK) |
| 321 |
F_SET(t, B_DB_LOCK); |
| 322 |
if (dflags & DB_SHMEM) |
| 323 |
F_SET(t, B_DB_SHMEM); |
| 324 |
if (dflags & DB_TXN) |
| 325 |
F_SET(t, B_DB_TXN); |
| 326 |
|
| 327 |
return (dbp); |
| 328 |
|
| 329 |
einval: errno = EINVAL; |
| 330 |
goto err; |
| 331 |
|
| 332 |
eftype: errno = EFTYPE; |
| 333 |
goto err; |
| 334 |
|
| 335 |
err: if (t) { |
| 336 |
if (t->bt_dbp) |
| 337 |
free(t->bt_dbp); |
| 338 |
if (t->bt_fd != -1) |
| 339 |
(void)close(t->bt_fd); |
| 340 |
free(t); |
| 341 |
} |
| 342 |
return (NULL); |
| 343 |
} |
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
static int |
| 355 |
nroot(t) |
| 356 |
BTREE *t; |
| 357 |
{ |
| 358 |
PAGE *meta, *root; |
| 359 |
pgno_t npg; |
| 360 |
|
| 361 |
if ((meta = mpool_get(t->bt_mp, 0, 0)) != NULL) { |
| 362 |
mpool_put(t->bt_mp, meta, 0); |
| 363 |
return (RET_SUCCESS); |
| 364 |
} |
| 365 |
if (errno != EINVAL) |
| 366 |
return (RET_ERROR); |
| 367 |
errno = 0; |
| 368 |
|
| 369 |
if ((meta = mpool_new(t->bt_mp, &npg)) == NULL) |
| 370 |
return (RET_ERROR); |
| 371 |
|
| 372 |
if ((root = mpool_new(t->bt_mp, &npg)) == NULL) |
| 373 |
return (RET_ERROR); |
| 374 |
|
| 375 |
if (npg != P_ROOT) |
| 376 |
return (RET_ERROR); |
| 377 |
root->pgno = npg; |
| 378 |
root->prevpg = root->nextpg = P_INVALID; |
| 379 |
root->lower = BTDATAOFF; |
| 380 |
root->upper = t->bt_psize; |
| 381 |
root->flags = P_BLEAF; |
| 382 |
memset(meta, 0, t->bt_psize); |
| 383 |
mpool_put(t->bt_mp, meta, MPOOL_DIRTY); |
| 384 |
mpool_put(t->bt_mp, root, MPOOL_DIRTY); |
| 385 |
return (RET_SUCCESS); |
| 386 |
} |
| 387 |
|
| 388 |
static int |
| 389 |
tmp() |
| 390 |
{ |
| 391 |
sigset_t set, oset; |
| 392 |
int fd; |
| 393 |
const char *envtmp; |
| 394 |
char *path; |
| 395 |
static const char fmt[] = "%s/bt.XXXXXX"; |
| 396 |
size_t n; |
| 397 |
|
| 398 |
envtmp = getenv("TMPDIR"); |
| 399 |
if (!envtmp) |
| 400 |
envtmp = "/tmp"; |
| 401 |
n = strlen (envtmp) + sizeof fmt; |
| 402 |
#ifdef __GNUC__ |
| 403 |
path = __builtin_alloca(n); |
| 404 |
#else |
| 405 |
path = malloc(n); |
| 406 |
#endif |
| 407 |
(void)snprintf(path, n, fmt, envtmp ? envtmp : "/tmp"); |
| 408 |
|
| 409 |
(void)sigfillset(&set); |
| 410 |
(void)sigprocmask(SIG_BLOCK, &set, &oset); |
| 411 |
if ((fd = mkstemp(path)) != -1) |
| 412 |
(void)unlink(path); |
| 413 |
(void)sigprocmask(SIG_SETMASK, &oset, NULL); |
| 414 |
#ifndef __GNUC__ |
| 415 |
free(path); |
| 416 |
#endif |
| 417 |
return(fd); |
| 418 |
} |
| 419 |
|
| 420 |
static int |
| 421 |
byteorder() |
| 422 |
{ |
| 423 |
u_int32_t x; |
| 424 |
u_char *p; |
| 425 |
|
| 426 |
x = 0x01020304; |
| 427 |
p = (u_char *)&x; |
| 428 |
switch (*p) { |
| 429 |
case 1: |
| 430 |
return (BIG_ENDIAN); |
| 431 |
case 4: |
| 432 |
return (LITTLE_ENDIAN); |
| 433 |
default: |
| 434 |
return (0); |
| 435 |
} |
| 436 |
} |
| 437 |
|
| 438 |
int |
| 439 |
__bt_fd(dbp) |
| 440 |
const DB *dbp; |
| 441 |
{ |
| 442 |
BTREE *t; |
| 443 |
|
| 444 |
t = dbp->internal; |
| 445 |
|
| 446 |
|
| 447 |
if (t->bt_pinned != NULL) { |
| 448 |
mpool_put(t->bt_mp, t->bt_pinned, 0); |
| 449 |
t->bt_pinned = NULL; |
| 450 |
} |
| 451 |
|
| 452 |
|
| 453 |
if (F_ISSET(t, B_INMEM)) { |
| 454 |
errno = ENOENT; |
| 455 |
return (-1); |
| 456 |
} |
| 457 |
return (t->bt_fd); |
| 458 |
} |