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. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * $FreeBSD: src/lib/libc/db/recno/rec_open.c,v 1.4 2000/01/27 23:06:11 jasone Exp $
39 #if defined(LIBC_SCCS) && !defined(lint)
40 static char sccsid[] = "@(#)rec_open.c 8.10 (Berkeley) 9/1/94";
41 #endif /* LIBC_SCCS and not lint */
43 #include <sys/types.h>
58 __rec_open(fname, flags, mode, openinfo, dflags)
60 int flags, mode, dflags;
61 const RECNOINFO *openinfo;
70 /* Open the user's file -- if this fails, we're done. */
71 if (fname != NULL && (rfd = _open(fname, flags, mode)) < 0)
74 /* Create a btree in memory (backed by disk). */
77 if (openinfo->flags & ~(R_FIXEDLEN | R_NOKEY | R_SNAPSHOT))
80 btopeninfo.cachesize = openinfo->cachesize;
81 btopeninfo.maxkeypage = 0;
82 btopeninfo.minkeypage = 0;
83 btopeninfo.psize = openinfo->psize;
84 btopeninfo.compare = NULL;
85 btopeninfo.prefix = NULL;
86 btopeninfo.lorder = openinfo->lorder;
87 dbp = __bt_open(openinfo->bfname,
88 O_RDWR, S_IRUSR | S_IWUSR, &btopeninfo, dflags);
90 dbp = __bt_open(NULL, O_RDWR, S_IRUSR | S_IWUSR, NULL, dflags);
95 * Some fields in the tree structure are recno specific. Fill them
96 * in and make the btree structure look like a recno structure. We
97 * don't change the bt_ovflsize value, it's close enough and slightly
102 if (openinfo->flags & R_FIXEDLEN) {
104 t->bt_reclen = openinfo->reclen;
105 if (t->bt_reclen == 0)
108 t->bt_bval = openinfo->bval;
114 F_SET(t, R_EOF | R_INMEM);
120 * In 4.4BSD, stat(2) returns true for ISSOCK on pipes.
121 * Unfortunately, that's not portable, so we use lseek
122 * and check the errno values.
125 if (lseek(rfd, (off_t)0, SEEK_CUR) == -1 && errno == ESPIPE) {
126 switch (flags & O_ACCMODE) {
133 slow: if ((t->bt_rfp = fdopen(rfd, "r")) == NULL)
137 F_ISSET(t, R_FIXLEN) ? __rec_fpipe : __rec_vpipe;
139 switch (flags & O_ACCMODE) {
152 * Kluge -- we'd like to test to see if the file is too
153 * big to mmap. Since, we don't know what size or type
154 * off_t's or size_t's are, what the largest unsigned
155 * integral type is, or what random insanity the local
156 * C compiler will perpetrate, doing the comparison in
157 * a portable way is flatly impossible. Hope that mmap
158 * fails if the file is too large.
163 #ifdef MMAP_NOT_AVAILABLE
166 * Mmap doesn't work correctly on many current
167 * systems. In particular, it can fail subtly,
168 * with cache coherency problems. Don't use it
171 t->bt_msize = sb.st_size;
172 if ((t->bt_smap = mmap(NULL, t->bt_msize,
173 PROT_READ, MAP_PRIVATE, rfd,
174 (off_t)0)) == MAP_FAILED)
176 t->bt_cmap = t->bt_smap;
177 t->bt_emap = t->bt_smap + sb.st_size;
178 t->bt_irec = F_ISSET(t, R_FIXLEN) ?
179 __rec_fmap : __rec_vmap;
180 F_SET(t, R_MEMMAPPED);
188 /* Use the recno routines. */
189 dbp->close = __rec_close;
190 dbp->del = __rec_delete;
192 dbp->get = __rec_get;
193 dbp->put = __rec_put;
194 dbp->seq = __rec_seq;
195 dbp->sync = __rec_sync;
197 /* If the root page was created, reset the flags. */
198 if ((h = mpool_get(t->bt_mp, P_ROOT, 0)) == NULL)
200 if ((h->flags & P_TYPE) == P_BLEAF) {
203 mpool_put(t->bt_mp, h, MPOOL_DIRTY);
205 mpool_put(t->bt_mp, h, 0);
207 if (openinfo && openinfo->flags & R_SNAPSHOT &&
208 !F_ISSET(t, R_EOF | R_INMEM) &&
209 t->bt_irec(t, MAX_REC_NUMBER) == RET_ERROR)
213 einval: errno = EINVAL;
214 err: sverrno = errno;
216 (void)__bt_close(dbp);
231 /* Toss any page pinned across calls. */
232 if (t->bt_pinned != NULL) {
233 mpool_put(t->bt_mp, t->bt_pinned, 0);
237 /* In-memory database can't have a file descriptor. */
238 if (F_ISSET(t, R_INMEM)) {