]>
git.saurik.com Git - apple/libc.git/blob - db/recno/FreeBSD/rec_open.c
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 * 4. 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
33 #if defined(LIBC_SCCS) && !defined(lint)
34 static char sccsid
[] = "@(#)rec_open.c 8.10 (Berkeley) 9/1/94";
35 #endif /* LIBC_SCCS and not lint */
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD: src/lib/libc/db/recno/rec_open.c,v 1.9 2009/03/04 00:58:04 delphij Exp $");
39 #include "namespace.h"
40 #include <sys/types.h>
50 #include "un-namespace.h"
56 __rec_open(const char *fname
, int flags
, int mode
, const RECNOINFO
*openinfo
,
66 /* Open the user's file -- if this fails, we're done. */
67 if (fname
!= NULL
&& (rfd
= _open(fname
, flags
, mode
)) < 0)
70 /* Create a btree in memory (backed by disk). */
73 if (openinfo
->flags
& ~(R_FIXEDLEN
| R_NOKEY
| R_SNAPSHOT
))
76 btopeninfo
.cachesize
= openinfo
->cachesize
;
77 btopeninfo
.maxkeypage
= 0;
78 btopeninfo
.minkeypage
= 0;
79 btopeninfo
.psize
= openinfo
->psize
;
80 btopeninfo
.compare
= NULL
;
81 btopeninfo
.prefix
= NULL
;
82 btopeninfo
.lorder
= openinfo
->lorder
;
83 dbp
= __bt_open(openinfo
->bfname
,
84 O_RDWR
, S_IRUSR
| S_IWUSR
, &btopeninfo
, dflags
);
86 dbp
= __bt_open(NULL
, O_RDWR
, S_IRUSR
| S_IWUSR
, NULL
, dflags
);
91 * Some fields in the tree structure are recno specific. Fill them
92 * in and make the btree structure look like a recno structure. We
93 * don't change the bt_ovflsize value, it's close enough and slightly
98 if (openinfo
->flags
& R_FIXEDLEN
) {
100 t
->bt_reclen
= openinfo
->reclen
;
101 if (t
->bt_reclen
== 0)
104 t
->bt_bval
= openinfo
->bval
;
110 F_SET(t
, R_EOF
| R_INMEM
);
116 * In 4.4BSD, stat(2) returns true for ISSOCK on pipes.
117 * Unfortunately, that's not portable, so we use lseek
118 * and check the errno values.
121 if (lseek(rfd
, (off_t
)0, SEEK_CUR
) == -1 && errno
== ESPIPE
) {
122 switch (flags
& O_ACCMODE
) {
129 slow
: if ((t
->bt_rfp
= fdopen(rfd
, "r")) == NULL
)
133 F_ISSET(t
, R_FIXLEN
) ? __rec_fpipe
: __rec_vpipe
;
135 switch (flags
& O_ACCMODE
) {
145 if (_fstat(rfd
, &sb
))
148 * Kluge -- we'd like to test to see if the file is too
149 * big to mmap. Since, we don't know what size or type
150 * off_t's or size_t's are, what the largest unsigned
151 * integral type is, or what random insanity the local
152 * C compiler will perpetrate, doing the comparison in
153 * a portable way is flatly impossible. Hope that mmap
154 * fails if the file is too large.
159 #ifdef MMAP_NOT_AVAILABLE
162 * Mmap doesn't work correctly on many current
163 * systems. In particular, it can fail subtly,
164 * with cache coherency problems. Don't use it
167 t
->bt_msize
= sb
.st_size
;
168 if ((t
->bt_smap
= mmap(NULL
, t
->bt_msize
,
169 PROT_READ
, MAP_PRIVATE
, rfd
,
170 (off_t
)0)) == MAP_FAILED
)
172 t
->bt_cmap
= t
->bt_smap
;
173 t
->bt_emap
= t
->bt_smap
+ sb
.st_size
;
174 t
->bt_irec
= F_ISSET(t
, R_FIXLEN
) ?
175 __rec_fmap
: __rec_vmap
;
176 F_SET(t
, R_MEMMAPPED
);
184 /* Use the recno routines. */
185 dbp
->close
= __rec_close
;
186 dbp
->del
= __rec_delete
;
188 dbp
->get
= __rec_get
;
189 dbp
->put
= __rec_put
;
190 dbp
->seq
= __rec_seq
;
191 dbp
->sync
= __rec_sync
;
193 /* If the root page was created, reset the flags. */
194 if ((h
= mpool_get(t
->bt_mp
, P_ROOT
, 0)) == NULL
)
196 if ((h
->flags
& P_TYPE
) == P_BLEAF
) {
199 mpool_put(t
->bt_mp
, h
, MPOOL_DIRTY
);
201 mpool_put(t
->bt_mp
, h
, 0);
203 if (openinfo
&& openinfo
->flags
& R_SNAPSHOT
&&
204 !F_ISSET(t
, R_EOF
| R_INMEM
) &&
205 t
->bt_irec(t
, MAX_REC_NUMBER
) == RET_ERROR
)
209 einval
: errno
= EINVAL
;
210 err
: sverrno
= errno
;
212 (void)__bt_close(dbp
);
220 __rec_fd(const DB
*dbp
)
226 /* Toss any page pinned across calls. */
227 if (t
->bt_pinned
!= NULL
) {
228 mpool_put(t
->bt_mp
, t
->bt_pinned
, 0);
232 /* In-memory database can't have a file descriptor. */
233 if (F_ISSET(t
, R_INMEM
)) {