1 /* $NetBSD: rec_open.c,v 1.16 2008/09/10 17:52:36 joerg Exp $ */
4 * Copyright (c) 1990, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #if HAVE_NBTOOL_CONFIG_H
36 #include "nbtool_config.h"
39 #include <sys/cdefs.h>
40 __RCSID("$NetBSD: rec_open.c,v 1.16 2008/09/10 17:52:36 joerg Exp $");
42 #include "namespace.h"
43 #include <sys/types.h>
59 __rec_open(const char *fname
, int flags
, mode_t mode
, const RECNOINFO
*openinfo
,
67 int rfd
= -1; /* pacify gcc */
71 /* Open the user's file -- if this fails, we're done. */
73 if ((rfd
= open(fname
, flags
, mode
)) == -1)
75 if (fcntl(rfd
, F_SETFD
, FD_CLOEXEC
) == -1)
79 /* Create a btree in memory (backed by disk). */
81 if (openinfo
->flags
& ~(R_FIXEDLEN
| R_NOKEY
| R_SNAPSHOT
))
84 btopeninfo
.cachesize
= openinfo
->cachesize
;
85 btopeninfo
.maxkeypage
= 0;
86 btopeninfo
.minkeypage
= 0;
87 btopeninfo
.psize
= openinfo
->psize
;
88 btopeninfo
.compare
= NULL
;
89 btopeninfo
.prefix
= NULL
;
90 btopeninfo
.lorder
= openinfo
->lorder
;
91 dbp
= __bt_open(openinfo
->bfname
,
92 O_RDWR
, S_IRUSR
| S_IWUSR
, &btopeninfo
, dflags
);
94 dbp
= __bt_open(NULL
, O_RDWR
, S_IRUSR
| S_IWUSR
, NULL
, dflags
);
99 * Some fields in the tree structure are recno specific. Fill them
100 * in and make the btree structure look like a recno structure. We
101 * don't change the bt_ovflsize value, it's close enough and slightly
106 if (openinfo
->flags
& R_FIXEDLEN
) {
108 t
->bt_reclen
= openinfo
->reclen
;
109 if (t
->bt_reclen
== 0)
112 t
->bt_bval
= openinfo
->bval
;
118 F_SET(t
, R_EOF
| R_INMEM
);
124 * In 4.4BSD, stat(2) returns true for ISSOCK on pipes.
125 * Unfortunately, that's not portable, so we use lseek
126 * and check the errno values.
129 if (lseek(rfd
, (off_t
)0, SEEK_CUR
) == -1 && errno
== ESPIPE
) {
130 switch (flags
& O_ACCMODE
) {
137 slow
: if ((t
->bt_rfp
= fdopen(rfd
, "r")) == NULL
)
141 F_ISSET(t
, R_FIXLEN
) ? __rec_fpipe
: __rec_vpipe
;
143 switch (flags
& O_ACCMODE
) {
156 * Kluge -- we'd like to test to see if the file is too
157 * big to mmap. Since, we don't know what size or type
158 * off_t's or size_t's are, what the largest unsigned
159 * integral type is, or what random insanity the local
160 * C compiler will perpetrate, doing the comparison in
161 * a portable way is flatly impossible. Hope that mmap
162 * fails if the file is too large.
167 #ifdef MMAP_NOT_AVAILABLE
170 * Mmap doesn't work correctly on many current
171 * systems. In particular, it can fail subtly,
172 * with cache coherency problems. Don't use it
175 t
->bt_msize
= sb
.st_size
;
176 if ((t
->bt_smap
= mmap(NULL
, t
->bt_msize
,
177 PROT_READ
, MAP_FILE
| MAP_PRIVATE
, rfd
,
178 (off_t
)0)) == (caddr_t
)-1)
180 t
->bt_cmap
= t
->bt_smap
;
181 t
->bt_emap
= t
->bt_smap
+ sb
.st_size
;
182 t
->bt_irec
= F_ISSET(t
, R_FIXLEN
) ?
183 __rec_fmap
: __rec_vmap
;
184 F_SET(t
, R_MEMMAPPED
);
192 /* Use the recno routines. */
193 dbp
->close
= __rec_close
;
194 dbp
->del
= __rec_delete
;
196 dbp
->get
= __rec_get
;
197 dbp
->put
= __rec_put
;
198 dbp
->seq
= __rec_seq
;
199 dbp
->sync
= __rec_sync
;
201 /* If the root page was created, reset the flags. */
202 if ((h
= mpool_get(t
->bt_mp
, P_ROOT
, 0)) == NULL
)
204 if ((h
->flags
& P_TYPE
) == P_BLEAF
) {
207 mpool_put(t
->bt_mp
, h
, MPOOL_DIRTY
);
209 mpool_put(t
->bt_mp
, h
, 0);
211 if (openinfo
&& openinfo
->flags
& R_SNAPSHOT
&&
212 !F_ISSET(t
, R_EOF
| R_INMEM
) &&
213 t
->bt_irec(t
, MAX_REC_NUMBER
) == RET_ERROR
)
217 einval
: errno
= EINVAL
;
218 err
: sverrno
= errno
;
220 (void)__bt_close(dbp
);
228 __rec_fd(const DB
*dbp
)
234 /* Toss any page pinned across calls. */
235 if (t
->bt_pinned
!= NULL
) {
236 mpool_put(t
->bt_mp
, t
->bt_pinned
, 0);
240 /* In-memory database can't have a file descriptor. */
241 if (F_ISSET(t
, R_INMEM
)) {