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