2 * Copyright (c) 1990, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * @(#)rec_open.c 8.10 (Berkeley) 9/1/94
33 * $FreeBSD: head/lib/libc/db/recno/rec_open.c 189327 2009-03-04 00:58:04Z delphij $
36 #include "namespace.h"
37 #include <sys/types.h>
47 #include "un-namespace.h"
53 __rec_open(const char *fname, int flags, mode_t mode, const RECNOINFO *openinfo,
63 /* Open the user's file -- if this fails, we're done. */
64 if (fname != NULL && (rfd = _open(fname, flags, mode)) < 0)
67 /* Create a btree in memory (backed by disk). */
70 if (openinfo->flags & ~(R_FIXEDLEN | R_NOKEY | R_SNAPSHOT))
73 btopeninfo.cachesize = openinfo->cachesize;
74 btopeninfo.maxkeypage = 0;
75 btopeninfo.minkeypage = 0;
76 btopeninfo.psize = openinfo->psize;
77 btopeninfo.compare = NULL;
78 btopeninfo.prefix = NULL;
79 btopeninfo.lorder = openinfo->lorder;
80 dbp = __bt_open(openinfo->bfname,
81 O_RDWR, S_IRUSR | S_IWUSR, &btopeninfo, dflags);
83 dbp = __bt_open(NULL, O_RDWR, S_IRUSR | S_IWUSR, NULL, dflags);
88 * Some fields in the tree structure are recno specific. Fill them
89 * in and make the btree structure look like a recno structure. We
90 * don't change the bt_ovflsize value, it's close enough and slightly
95 if (openinfo->flags & R_FIXEDLEN) {
97 t->bt_reclen = openinfo->reclen;
98 if (t->bt_reclen == 0)
101 t->bt_bval = openinfo->bval;
107 F_SET(t, R_EOF | R_INMEM);
113 * In 4.4BSD, stat(2) returns true for ISSOCK on pipes.
114 * Unfortunately, that's not portable, so we use lseek
115 * and check the errno values.
118 if (lseek(rfd, (off_t)0, SEEK_CUR) == -1 && errno == ESPIPE) {
119 switch (flags & O_ACCMODE) {
126 slow: if ((t->bt_rfp = fdopen(rfd, "r")) == NULL)
130 F_ISSET(t, R_FIXLEN) ? __rec_fpipe : __rec_vpipe;
132 switch (flags & O_ACCMODE) {
142 if (_fstat(rfd, &sb))
145 * Kluge -- we'd like to test to see if the file is too
146 * big to mmap. Since, we don't know what size or type
147 * off_t's or size_t's are, what the largest unsigned
148 * integral type is, or what random insanity the local
149 * C compiler will perpetrate, doing the comparison in
150 * a portable way is flatly impossible. Hope that mmap
151 * fails if the file is too large.
156 #ifdef MMAP_NOT_AVAILABLE
159 * Mmap doesn't work correctly on many current
160 * systems. In particular, it can fail subtly,
161 * with cache coherency problems. Don't use it
164 t->bt_msize = sb.st_size;
165 if ((t->bt_smap = mmap(NULL, t->bt_msize,
166 PROT_READ, MAP_PRIVATE, rfd,
167 (off_t)0)) == MAP_FAILED)
169 t->bt_cmap = t->bt_smap;
170 t->bt_emap = t->bt_smap + sb.st_size;
171 t->bt_irec = F_ISSET(t, R_FIXLEN) ?
172 __rec_fmap : __rec_vmap;
173 F_SET(t, R_MEMMAPPED);
181 /* Use the recno routines. */
182 dbp->close = __rec_close;
183 dbp->del = __rec_delete;
185 dbp->get = __rec_get;
186 dbp->put = __rec_put;
187 dbp->seq = __rec_seq;
188 dbp->sync = __rec_sync;
190 /* If the root page was created, reset the flags. */
191 if ((h = mpool_get(t->bt_mp, P_ROOT, 0)) == NULL)
193 if ((h->flags & P_TYPE) == P_BLEAF) {
196 mpool_put(t->bt_mp, h, MPOOL_DIRTY);
198 mpool_put(t->bt_mp, h, 0);
200 if (openinfo && openinfo->flags & R_SNAPSHOT &&
201 !F_ISSET(t, R_EOF | R_INMEM) &&
202 t->bt_irec(t, MAX_REC_NUMBER) == RET_ERROR)
206 einval: errno = EINVAL;
207 err: sverrno = errno;
217 __rec_fd(const DB *dbp)
223 /* Toss any page pinned across calls. */
224 if (t->bt_pinned != NULL) {
225 mpool_put(t->bt_mp, t->bt_pinned, 0);
229 /* In-memory database can't have a file descriptor. */
230 if (F_ISSET(t, R_INMEM)) {