Home | History | Annotate | Line # | Download | only in btree
bt_open.c revision 1.25.4.1
      1  1.25.4.1      yamt /*	$NetBSD: bt_open.c,v 1.25.4.1 2012/04/17 00:05:17 yamt Exp $	*/
      2       1.7       cgd 
      3       1.1       cgd /*-
      4       1.6       cgd  * Copyright (c) 1990, 1993, 1994
      5       1.1       cgd  *	The Regents of the University of California.  All rights reserved.
      6       1.1       cgd  *
      7       1.1       cgd  * This code is derived from software contributed to Berkeley by
      8       1.1       cgd  * Mike Olson.
      9       1.1       cgd  *
     10       1.1       cgd  * Redistribution and use in source and binary forms, with or without
     11       1.1       cgd  * modification, are permitted provided that the following conditions
     12       1.1       cgd  * are met:
     13       1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     14       1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     15       1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     16       1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     17       1.1       cgd  *    documentation and/or other materials provided with the distribution.
     18      1.17       agc  * 3. Neither the name of the University nor the names of its contributors
     19       1.1       cgd  *    may be used to endorse or promote products derived from this software
     20       1.1       cgd  *    without specific prior written permission.
     21       1.1       cgd  *
     22       1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23       1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24       1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25       1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26       1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27       1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28       1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29       1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30       1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31       1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32       1.1       cgd  * SUCH DAMAGE.
     33       1.1       cgd  */
     34       1.1       cgd 
     35      1.24     joerg #if HAVE_NBTOOL_CONFIG_H
     36      1.24     joerg #include "nbtool_config.h"
     37      1.24     joerg #endif
     38      1.24     joerg 
     39       1.9  christos #include <sys/cdefs.h>
     40  1.25.4.1      yamt __RCSID("$NetBSD: bt_open.c,v 1.25.4.1 2012/04/17 00:05:17 yamt Exp $");
     41       1.1       cgd 
     42       1.1       cgd /*
     43       1.1       cgd  * Implementation of btree access method for 4.4BSD.
     44       1.1       cgd  *
     45       1.1       cgd  * The design here was originally based on that of the btree access method
     46       1.1       cgd  * used in the Postgres database system at UC Berkeley.  This implementation
     47       1.1       cgd  * is wholly independent of the Postgres code.
     48       1.1       cgd  */
     49       1.1       cgd 
     50      1.10       jtc #include "namespace.h"
     51       1.1       cgd #include <sys/stat.h>
     52       1.1       cgd 
     53      1.21  christos #include <assert.h>
     54       1.1       cgd #include <errno.h>
     55       1.1       cgd #include <fcntl.h>
     56       1.1       cgd #include <limits.h>
     57       1.1       cgd #include <signal.h>
     58       1.1       cgd #include <stdio.h>
     59       1.1       cgd #include <stdlib.h>
     60       1.1       cgd #include <string.h>
     61       1.1       cgd #include <unistd.h>
     62      1.11      fair #include <paths.h>
     63       1.1       cgd 
     64       1.1       cgd #include <db.h>
     65       1.1       cgd #include "btree.h"
     66       1.1       cgd 
     67       1.8       cgd #ifdef DEBUG
     68       1.8       cgd #undef	MINPSIZE
     69       1.8       cgd #define	MINPSIZE	128
     70       1.8       cgd #endif
     71       1.8       cgd 
     72      1.21  christos static int byteorder(void);
     73      1.21  christos static int nroot(BTREE *);
     74      1.21  christos static int tmp(void);
     75       1.1       cgd 
     76       1.1       cgd /*
     77       1.1       cgd  * __BT_OPEN -- Open a btree.
     78       1.1       cgd  *
     79       1.1       cgd  * Creates and fills a DB struct, and calls the routine that actually
     80       1.1       cgd  * opens the btree.
     81       1.1       cgd  *
     82       1.1       cgd  * Parameters:
     83       1.1       cgd  *	fname:	filename (NULL for in-memory trees)
     84       1.1       cgd  *	flags:	open flag bits
     85       1.1       cgd  *	mode:	open permission bits
     86       1.1       cgd  *	b:	BTREEINFO pointer
     87       1.1       cgd  *
     88       1.1       cgd  * Returns:
     89       1.1       cgd  *	NULL on failure, pointer to DB on success.
     90       1.1       cgd  *
     91       1.1       cgd  */
     92       1.1       cgd DB *
     93      1.21  christos __bt_open(const char *fname, int flags, mode_t mode, const BTREEINFO *openinfo,
     94      1.21  christos     int dflags)
     95       1.1       cgd {
     96       1.6       cgd 	struct stat sb;
     97       1.1       cgd 	BTMETA m;
     98       1.1       cgd 	BTREE *t;
     99       1.1       cgd 	BTREEINFO b;
    100       1.1       cgd 	DB *dbp;
    101       1.1       cgd 	pgno_t ncache;
    102       1.6       cgd 	ssize_t nr;
    103      1.21  christos 	size_t temp;
    104       1.6       cgd 	int machine_lorder;
    105       1.1       cgd 
    106       1.1       cgd 	t = NULL;
    107       1.1       cgd 
    108       1.1       cgd 	/*
    109       1.1       cgd 	 * Intention is to make sure all of the user's selections are okay
    110       1.1       cgd 	 * here and then use them without checking.  Can't be complete, since
    111       1.1       cgd 	 * we don't know the right page size, lorder or flags until the backing
    112       1.1       cgd 	 * file is opened.  Also, the file's page size can cause the cachesize
    113       1.1       cgd 	 * to change.
    114       1.1       cgd 	 */
    115       1.1       cgd 	machine_lorder = byteorder();
    116       1.1       cgd 	if (openinfo) {
    117       1.1       cgd 		b = *openinfo;
    118       1.1       cgd 
    119       1.1       cgd 		/* Flags: R_DUP. */
    120       1.1       cgd 		if (b.flags & ~(R_DUP))
    121       1.1       cgd 			goto einval;
    122       1.1       cgd 
    123       1.1       cgd 		/*
    124       1.1       cgd 		 * Page size must be indx_t aligned and >= MINPSIZE.  Default
    125       1.1       cgd 		 * page size is set farther on, based on the underlying file
    126       1.1       cgd 		 * transfer size.
    127       1.1       cgd 		 */
    128       1.1       cgd 		if (b.psize &&
    129       1.1       cgd 		    (b.psize < MINPSIZE || b.psize > MAX_PAGE_OFFSET + 1 ||
    130       1.9  christos 		    b.psize & (sizeof(indx_t) - 1)))
    131       1.1       cgd 			goto einval;
    132       1.1       cgd 
    133       1.1       cgd 		/* Minimum number of keys per page; absolute minimum is 2. */
    134       1.1       cgd 		if (b.minkeypage) {
    135       1.1       cgd 			if (b.minkeypage < 2)
    136       1.1       cgd 				goto einval;
    137       1.1       cgd 		} else
    138       1.1       cgd 			b.minkeypage = DEFMINKEYPAGE;
    139       1.1       cgd 
    140       1.1       cgd 		/* If no comparison, use default comparison and prefix. */
    141       1.1       cgd 		if (b.compare == NULL) {
    142       1.1       cgd 			b.compare = __bt_defcmp;
    143       1.1       cgd 			if (b.prefix == NULL)
    144       1.1       cgd 				b.prefix = __bt_defpfx;
    145       1.1       cgd 		}
    146       1.1       cgd 
    147       1.1       cgd 		if (b.lorder == 0)
    148       1.1       cgd 			b.lorder = machine_lorder;
    149       1.1       cgd 	} else {
    150       1.1       cgd 		b.compare = __bt_defcmp;
    151       1.1       cgd 		b.cachesize = 0;
    152       1.1       cgd 		b.flags = 0;
    153       1.1       cgd 		b.lorder = machine_lorder;
    154       1.1       cgd 		b.minkeypage = DEFMINKEYPAGE;
    155       1.1       cgd 		b.prefix = __bt_defpfx;
    156       1.1       cgd 		b.psize = 0;
    157       1.1       cgd 	}
    158       1.1       cgd 
    159       1.1       cgd 	/* Check for the ubiquitous PDP-11. */
    160       1.1       cgd 	if (b.lorder != BIG_ENDIAN && b.lorder != LITTLE_ENDIAN)
    161       1.1       cgd 		goto einval;
    162       1.1       cgd 
    163       1.1       cgd 	/* Allocate and initialize DB and BTREE structures. */
    164       1.6       cgd 	if ((t = (BTREE *)malloc(sizeof(BTREE))) == NULL)
    165       1.1       cgd 		goto err;
    166       1.5       cgd 	memset(t, 0, sizeof(BTREE));
    167       1.1       cgd 	t->bt_fd = -1;			/* Don't close unopened fd on error. */
    168       1.1       cgd 	t->bt_lorder = b.lorder;
    169       1.1       cgd 	t->bt_order = NOT;
    170       1.1       cgd 	t->bt_cmp = b.compare;
    171       1.1       cgd 	t->bt_pfx = b.prefix;
    172       1.5       cgd 	t->bt_rfd = -1;
    173       1.5       cgd 
    174       1.6       cgd 	if ((t->bt_dbp = dbp = (DB *)malloc(sizeof(DB))) == NULL)
    175       1.5       cgd 		goto err;
    176       1.8       cgd 	memset(t->bt_dbp, 0, sizeof(DB));
    177       1.1       cgd 	if (t->bt_lorder != machine_lorder)
    178       1.8       cgd 		F_SET(t, B_NEEDSWAP);
    179       1.1       cgd 
    180       1.1       cgd 	dbp->type = DB_BTREE;
    181       1.1       cgd 	dbp->internal = t;
    182       1.1       cgd 	dbp->close = __bt_close;
    183       1.1       cgd 	dbp->del = __bt_delete;
    184       1.1       cgd 	dbp->fd = __bt_fd;
    185       1.1       cgd 	dbp->get = __bt_get;
    186       1.1       cgd 	dbp->put = __bt_put;
    187       1.1       cgd 	dbp->seq = __bt_seq;
    188       1.1       cgd 	dbp->sync = __bt_sync;
    189       1.1       cgd 
    190       1.1       cgd 	/*
    191       1.1       cgd 	 * If no file name was supplied, this is an in-memory btree and we
    192       1.1       cgd 	 * open a backing temporary file.  Otherwise, it's a disk-based tree.
    193       1.1       cgd 	 */
    194       1.1       cgd 	if (fname) {
    195       1.8       cgd 		switch (flags & O_ACCMODE) {
    196       1.1       cgd 		case O_RDONLY:
    197       1.8       cgd 			F_SET(t, B_RDONLY);
    198       1.1       cgd 			break;
    199       1.1       cgd 		case O_RDWR:
    200       1.1       cgd 			break;
    201       1.1       cgd 		case O_WRONLY:
    202       1.1       cgd 		default:
    203       1.1       cgd 			goto einval;
    204       1.1       cgd 		}
    205       1.1       cgd 
    206      1.18   mycroft 		if ((t->bt_fd = open(fname, flags, mode)) == -1)
    207      1.18   mycroft 			goto err;
    208      1.18   mycroft 		if (fcntl(t->bt_fd, F_SETFD, FD_CLOEXEC) == -1)
    209       1.1       cgd 			goto err;
    210       1.1       cgd 	} else {
    211       1.1       cgd 		if ((flags & O_ACCMODE) != O_RDWR)
    212       1.1       cgd 			goto einval;
    213       1.1       cgd 		if ((t->bt_fd = tmp()) == -1)
    214       1.1       cgd 			goto err;
    215       1.8       cgd 		F_SET(t, B_INMEM);
    216       1.1       cgd 	}
    217       1.1       cgd 
    218      1.20  christos 	if (fcntl(t->bt_fd, F_SETFD, FD_CLOEXEC) == -1)
    219       1.1       cgd 		goto err;
    220       1.1       cgd 
    221       1.1       cgd 	if (fstat(t->bt_fd, &sb))
    222       1.1       cgd 		goto err;
    223       1.1       cgd 	if (sb.st_size) {
    224       1.6       cgd 		if ((nr = read(t->bt_fd, &m, sizeof(BTMETA))) < 0)
    225       1.1       cgd 			goto err;
    226       1.1       cgd 		if (nr != sizeof(BTMETA))
    227       1.1       cgd 			goto eftype;
    228       1.1       cgd 
    229       1.1       cgd 		/*
    230       1.1       cgd 		 * Read in the meta-data.  This can change the notion of what
    231       1.1       cgd 		 * the lorder, page size and flags are, and, when the page size
    232       1.1       cgd 		 * changes, the cachesize value can change too.  If the user
    233       1.1       cgd 		 * specified the wrong byte order for an existing database, we
    234       1.1       cgd 		 * don't bother to return an error, we just clear the NEEDSWAP
    235       1.1       cgd 		 * bit.
    236       1.1       cgd 		 */
    237       1.8       cgd 		if (m.magic == BTREEMAGIC)
    238       1.8       cgd 			F_CLR(t, B_NEEDSWAP);
    239       1.1       cgd 		else {
    240       1.8       cgd 			F_SET(t, B_NEEDSWAP);
    241       1.8       cgd 			M_32_SWAP(m.magic);
    242       1.8       cgd 			M_32_SWAP(m.version);
    243       1.8       cgd 			M_32_SWAP(m.psize);
    244       1.8       cgd 			M_32_SWAP(m.free);
    245       1.8       cgd 			M_32_SWAP(m.nrecs);
    246       1.8       cgd 			M_32_SWAP(m.flags);
    247       1.1       cgd 		}
    248       1.8       cgd 		if (m.magic != BTREEMAGIC || m.version != BTREEVERSION)
    249       1.1       cgd 			goto eftype;
    250       1.8       cgd 		if (m.psize < MINPSIZE || m.psize > MAX_PAGE_OFFSET + 1 ||
    251       1.9  christos 		    m.psize & (sizeof(indx_t) - 1))
    252       1.1       cgd 			goto eftype;
    253       1.8       cgd 		if (m.flags & ~SAVEMETA)
    254       1.1       cgd 			goto eftype;
    255       1.8       cgd 		b.psize = m.psize;
    256       1.8       cgd 		F_SET(t, m.flags);
    257       1.8       cgd 		t->bt_free = m.free;
    258       1.8       cgd 		t->bt_nrecs = m.nrecs;
    259       1.1       cgd 	} else {
    260       1.1       cgd 		/*
    261       1.1       cgd 		 * Set the page size to the best value for I/O to this file.
    262       1.1       cgd 		 * Don't overflow the page offset type.
    263       1.1       cgd 		 */
    264       1.1       cgd 		if (b.psize == 0) {
    265       1.1       cgd 			b.psize = sb.st_blksize;
    266       1.1       cgd 			if (b.psize < MINPSIZE)
    267       1.1       cgd 				b.psize = MINPSIZE;
    268       1.1       cgd 			if (b.psize > MAX_PAGE_OFFSET + 1)
    269       1.1       cgd 				b.psize = MAX_PAGE_OFFSET + 1;
    270       1.1       cgd 		}
    271       1.1       cgd 
    272       1.1       cgd 		/* Set flag if duplicates permitted. */
    273       1.1       cgd 		if (!(b.flags & R_DUP))
    274       1.8       cgd 			F_SET(t, B_NODUPS);
    275       1.1       cgd 
    276       1.1       cgd 		t->bt_free = P_INVALID;
    277       1.1       cgd 		t->bt_nrecs = 0;
    278       1.8       cgd 		F_SET(t, B_METADIRTY);
    279       1.1       cgd 	}
    280       1.1       cgd 
    281       1.1       cgd 	t->bt_psize = b.psize;
    282       1.1       cgd 
    283       1.1       cgd 	/* Set the cache size; must be a multiple of the page size. */
    284       1.9  christos 	if (b.cachesize && b.cachesize & (b.psize - 1))
    285       1.9  christos 		b.cachesize += (~b.cachesize & (b.psize - 1)) + 1;
    286       1.1       cgd 	if (b.cachesize < b.psize * MINCACHE)
    287       1.1       cgd 		b.cachesize = b.psize * MINCACHE;
    288       1.1       cgd 
    289       1.1       cgd 	/* Calculate number of pages to cache. */
    290       1.1       cgd 	ncache = (b.cachesize + t->bt_psize - 1) / t->bt_psize;
    291       1.1       cgd 
    292       1.1       cgd 	/*
    293       1.1       cgd 	 * The btree data structure requires that at least two keys can fit on
    294       1.1       cgd 	 * a page, but other than that there's no fixed requirement.  The user
    295       1.1       cgd 	 * specified a minimum number per page, and we translated that into the
    296       1.1       cgd 	 * number of bytes a key/data pair can use before being placed on an
    297       1.1       cgd 	 * overflow page.  This calculation includes the page header, the size
    298       1.1       cgd 	 * of the index referencing the leaf item and the size of the leaf item
    299       1.1       cgd 	 * structure.  Also, don't let the user specify a minkeypage such that
    300       1.1       cgd 	 * a key/data pair won't fit even if both key and data are on overflow
    301       1.1       cgd 	 * pages.
    302       1.1       cgd 	 */
    303      1.21  christos 	temp = (t->bt_psize - BTDATAOFF) / b.minkeypage -
    304       1.1       cgd 	    (sizeof(indx_t) + NBLEAFDBT(0, 0));
    305      1.21  christos 	_DBFIT(temp, indx_t);
    306      1.21  christos 	t->bt_ovflsize = (indx_t)temp;
    307  1.25.4.1      yamt 	if (t->bt_ovflsize < NBLEAFDBT(NOVFLSIZE, NOVFLSIZE) + sizeof(indx_t)) {
    308  1.25.4.1      yamt 		size_t l = NBLEAFDBT(NOVFLSIZE, NOVFLSIZE) + sizeof(indx_t);
    309  1.25.4.1      yamt 		_DBFIT(l, indx_t);
    310  1.25.4.1      yamt 		t->bt_ovflsize = (indx_t)l;
    311  1.25.4.1      yamt 	}
    312       1.1       cgd 
    313       1.1       cgd 	/* Initialize the buffer pool. */
    314       1.1       cgd 	if ((t->bt_mp =
    315       1.1       cgd 	    mpool_open(NULL, t->bt_fd, t->bt_psize, ncache)) == NULL)
    316       1.1       cgd 		goto err;
    317       1.8       cgd 	if (!F_ISSET(t, B_INMEM))
    318       1.1       cgd 		mpool_filter(t->bt_mp, __bt_pgin, __bt_pgout, t);
    319       1.1       cgd 
    320       1.1       cgd 	/* Create a root page if new tree. */
    321       1.1       cgd 	if (nroot(t) == RET_ERROR)
    322       1.1       cgd 		goto err;
    323       1.1       cgd 
    324       1.4       cgd 	/* Global flags. */
    325       1.4       cgd 	if (dflags & DB_LOCK)
    326       1.8       cgd 		F_SET(t, B_DB_LOCK);
    327       1.4       cgd 	if (dflags & DB_SHMEM)
    328       1.8       cgd 		F_SET(t, B_DB_SHMEM);
    329       1.4       cgd 	if (dflags & DB_TXN)
    330       1.8       cgd 		F_SET(t, B_DB_TXN);
    331       1.4       cgd 
    332       1.1       cgd 	return (dbp);
    333       1.1       cgd 
    334       1.1       cgd einval:	errno = EINVAL;
    335       1.1       cgd 	goto err;
    336       1.1       cgd 
    337       1.1       cgd eftype:	errno = EFTYPE;
    338       1.1       cgd 	goto err;
    339       1.1       cgd 
    340       1.1       cgd err:	if (t) {
    341       1.1       cgd 		if (t->bt_dbp)
    342       1.1       cgd 			free(t->bt_dbp);
    343       1.1       cgd 		if (t->bt_fd != -1)
    344       1.1       cgd 			(void)close(t->bt_fd);
    345       1.1       cgd 		free(t);
    346       1.1       cgd 	}
    347       1.1       cgd 	return (NULL);
    348       1.1       cgd }
    349       1.1       cgd 
    350       1.1       cgd /*
    351       1.1       cgd  * NROOT -- Create the root of a new tree.
    352       1.1       cgd  *
    353       1.1       cgd  * Parameters:
    354       1.1       cgd  *	t:	tree
    355       1.1       cgd  *
    356       1.1       cgd  * Returns:
    357       1.1       cgd  *	RET_ERROR, RET_SUCCESS
    358       1.1       cgd  */
    359       1.1       cgd static int
    360      1.21  christos nroot(BTREE *t)
    361       1.1       cgd {
    362       1.1       cgd 	PAGE *meta, *root;
    363       1.1       cgd 	pgno_t npg;
    364       1.1       cgd 
    365       1.1       cgd 	if ((meta = mpool_get(t->bt_mp, 0, 0)) != NULL) {
    366       1.1       cgd 		mpool_put(t->bt_mp, meta, 0);
    367       1.1       cgd 		return (RET_SUCCESS);
    368       1.1       cgd 	}
    369       1.6       cgd 	if (errno != EINVAL)		/* It's OK to not exist. */
    370       1.1       cgd 		return (RET_ERROR);
    371       1.6       cgd 	errno = 0;
    372       1.1       cgd 
    373       1.1       cgd 	if ((meta = mpool_new(t->bt_mp, &npg)) == NULL)
    374       1.1       cgd 		return (RET_ERROR);
    375       1.1       cgd 
    376       1.1       cgd 	if ((root = mpool_new(t->bt_mp, &npg)) == NULL)
    377       1.1       cgd 		return (RET_ERROR);
    378       1.1       cgd 
    379       1.1       cgd 	if (npg != P_ROOT)
    380       1.1       cgd 		return (RET_ERROR);
    381       1.1       cgd 	root->pgno = npg;
    382       1.1       cgd 	root->prevpg = root->nextpg = P_INVALID;
    383       1.1       cgd 	root->lower = BTDATAOFF;
    384       1.1       cgd 	root->upper = t->bt_psize;
    385       1.1       cgd 	root->flags = P_BLEAF;
    386       1.1       cgd 	memset(meta, 0, t->bt_psize);
    387       1.1       cgd 	mpool_put(t->bt_mp, meta, MPOOL_DIRTY);
    388       1.1       cgd 	mpool_put(t->bt_mp, root, MPOOL_DIRTY);
    389       1.1       cgd 	return (RET_SUCCESS);
    390       1.1       cgd }
    391       1.1       cgd 
    392       1.1       cgd static int
    393      1.21  christos tmp(void)
    394       1.1       cgd {
    395       1.1       cgd 	sigset_t set, oset;
    396      1.25  christos 	int len;
    397      1.16   thorpej 	int fd;
    398       1.1       cgd 	char *envtmp;
    399      1.14   thorpej 	char path[PATH_MAX];
    400       1.1       cgd 
    401      1.15      groo 	if (issetugid())
    402      1.15      groo 		envtmp = NULL;
    403      1.15      groo 	else
    404      1.15      groo 		envtmp = getenv("TMPDIR");
    405      1.15      groo 
    406      1.15      groo 	len = snprintf(path,
    407      1.11      fair 	    sizeof(path), "%s/bt.XXXXXX", envtmp ? envtmp : _PATH_TMP);
    408      1.25  christos 	if (len < 0 || (size_t)len >= sizeof(path)) {
    409      1.25  christos 		errno = ENAMETOOLONG;
    410      1.15      groo 		return -1;
    411      1.25  christos 	}
    412      1.15      groo 
    413       1.1       cgd 	(void)sigfillset(&set);
    414       1.1       cgd 	(void)sigprocmask(SIG_BLOCK, &set, &oset);
    415      1.19       rtr 	if ((fd = mkstemp(path)) != -1) {
    416       1.1       cgd 		(void)unlink(path);
    417      1.20  christos 		(void)fcntl(fd, F_SETFD, FD_CLOEXEC);
    418      1.19       rtr 	}
    419       1.1       cgd 	(void)sigprocmask(SIG_SETMASK, &oset, NULL);
    420       1.1       cgd 	return(fd);
    421       1.1       cgd }
    422       1.1       cgd 
    423       1.1       cgd static int
    424      1.21  christos byteorder(void)
    425       1.1       cgd {
    426      1.22     joerg 	uint32_t x;
    427      1.22     joerg 	uint8_t *p;
    428       1.1       cgd 
    429       1.1       cgd 	x = 0x01020304;
    430      1.22     joerg 	p = (uint8_t *)(void *)&x;
    431       1.1       cgd 	switch (*p) {
    432       1.1       cgd 	case 1:
    433       1.1       cgd 		return (BIG_ENDIAN);
    434       1.1       cgd 	case 4:
    435       1.1       cgd 		return (LITTLE_ENDIAN);
    436       1.1       cgd 	default:
    437       1.1       cgd 		return (0);
    438       1.1       cgd 	}
    439       1.1       cgd }
    440       1.1       cgd 
    441       1.1       cgd int
    442      1.21  christos __bt_fd(const DB *dbp)
    443       1.1       cgd {
    444       1.1       cgd 	BTREE *t;
    445       1.1       cgd 
    446       1.1       cgd 	t = dbp->internal;
    447       1.1       cgd 
    448       1.4       cgd 	/* Toss any page pinned across calls. */
    449       1.4       cgd 	if (t->bt_pinned != NULL) {
    450       1.4       cgd 		mpool_put(t->bt_mp, t->bt_pinned, 0);
    451       1.4       cgd 		t->bt_pinned = NULL;
    452       1.4       cgd 	}
    453       1.4       cgd 
    454       1.4       cgd 	/* In-memory database can't have a file descriptor. */
    455       1.8       cgd 	if (F_ISSET(t, B_INMEM)) {
    456       1.1       cgd 		errno = ENOENT;
    457       1.1       cgd 		return (-1);
    458       1.1       cgd 	}
    459       1.1       cgd 	return (t->bt_fd);
    460       1.1       cgd }
    461