37 #if defined(LIBC_SCCS) && !defined(lint)
38 static char sccsid[] =
"@(#)bt_open.c 8.10 (Berkeley) 8/17/94";
49 #include <sys/param.h>
61 #include "../include/db.h"
69 static int byteorder __P((
void));
70 static int nroot __P((
BTREE *));
71 static int tmp __P((
void));
90 __bt_open(fname, flags, mode, openinfo, dflags)
92 int flags, mode, dflags;
113 machine_lorder = byteorder();
118 if (b.flags & ~(R_DUP))
127 (b.psize < MINPSIZE || b.psize > MAX_PAGE_OFFSET + 1 ||
128 b.psize & (
sizeof(indx_t) - 1)))
133 if (b.minkeypage < 2)
136 b.minkeypage = DEFMINKEYPAGE;
139 if (b.compare == NULL) {
140 b.compare = __bt_defcmp;
141 if (b.prefix == NULL)
142 b.prefix = __bt_defpfx;
146 b.lorder = machine_lorder;
148 b.compare = __bt_defcmp;
151 b.lorder = machine_lorder;
152 b.minkeypage = DEFMINKEYPAGE;
153 b.prefix = __bt_defpfx;
158 if (b.lorder != BIG_ENDIAN && b.lorder != LITTLE_ENDIAN)
162 if ((t = (
BTREE *)malloc(
sizeof(
BTREE))) == NULL)
164 memset(t, 0,
sizeof(
BTREE));
166 t->bt_lorder = b.lorder;
168 t->bt_cmp = b.compare;
169 t->bt_pfx = b.prefix;
172 if ((t->bt_dbp = dbp = (
DB *)malloc(
sizeof(
DB))) == NULL)
174 memset(t->bt_dbp, 0,
sizeof(
DB));
175 if (t->bt_lorder != machine_lorder)
176 F_SET(t, B_NEEDSWAP);
178 dbp->type = DB_BTREE;
180 dbp->close = __bt_close;
181 dbp->del = __bt_delete;
186 dbp->sync = __bt_sync;
193 switch (flags & O_ACCMODE) {
204 if ((t->bt_fd = open(fname, flags, mode)) < 0)
208 if ((flags & O_ACCMODE) != O_RDWR)
210 if ((t->bt_fd = tmp()) == -1)
215 if (fcntl(t->bt_fd, F_SETFD, 1) == -1)
218 if (fstat(t->bt_fd, &sb))
221 if ((nr = read(t->bt_fd, &m,
sizeof(
BTMETA))) < 0)
234 if (m.magic == BTREEMAGIC)
235 F_CLR(t, B_NEEDSWAP);
237 F_SET(t, B_NEEDSWAP);
239 M_32_SWAP(m.version);
245 if (m.magic != BTREEMAGIC || m.version != BTREEVERSION)
247 if (m.psize < MINPSIZE || m.psize > MAX_PAGE_OFFSET + 1 ||
248 m.psize & (
sizeof(indx_t) - 1))
250 if (m.flags & ~SAVEMETA)
255 t->bt_nrecs = m.nrecs;
262 #ifdef _STATBUF_ST_BLKSIZE
263 b.psize = sb.st_blksize;
265 if (b.psize < MINPSIZE)
267 if (b.psize > MAX_PAGE_OFFSET + 1)
268 b.psize = MAX_PAGE_OFFSET + 1;
272 if (!(b.flags & R_DUP))
275 t->bt_free = P_INVALID;
277 F_SET(t, B_METADIRTY);
280 t->bt_psize = b.psize;
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;
289 ncache = (b.cachesize + t->bt_psize - 1) / t->bt_psize;
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))
306 NBLEAFDBT(NOVFLSIZE, NOVFLSIZE) +
sizeof(indx_t);
310 mpool_open(NULL, t->bt_fd, t->bt_psize, ncache)) == NULL)
312 if (!F_ISSET(t, B_INMEM))
313 mpool_filter(t->bt_mp, __bt_pgin, __bt_pgout, t);
316 if (nroot(t) == RET_ERROR)
320 if (dflags & DB_LOCK)
322 if (dflags & DB_SHMEM)
323 F_SET(t, B_DB_SHMEM);
329 einval: errno = EINVAL;
332 eftype: errno = EFTYPE;
339 (void)close(t->bt_fd);
361 if ((meta = mpool_get(t->bt_mp, 0, 0)) != NULL) {
362 mpool_put(t->bt_mp, meta, 0);
363 return (RET_SUCCESS);
369 if ((meta = mpool_new(t->bt_mp, &npg)) == NULL)
372 if ((root = mpool_new(t->bt_mp, &npg)) == NULL)
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);
395 static const char fmt[] =
"%s/bt.XXXXXX";
398 envtmp = getenv(
"TMPDIR");
401 n = strlen (envtmp) +
sizeof fmt;
403 path = __builtin_alloca(n);
407 (void)snprintf(path, n, fmt, envtmp ? envtmp :
"/tmp");
409 (void)sigfillset(&set);
410 (void)sigprocmask(SIG_BLOCK, &set, &oset);
411 if ((fd = mkstemp(path)) != -1)
413 (void)sigprocmask(SIG_SETMASK, &oset, NULL);
432 return (LITTLE_ENDIAN);
447 if (t->bt_pinned != NULL) {
448 mpool_put(t->bt_mp, t->bt_pinned, 0);
453 if (F_ISSET(t, B_INMEM)) {