rec_open.c revision 1.8 1 /* $NetBSD: rec_open.c,v 1.8 1997/07/13 18:52:12 christos Exp $ */
2
3 /*-
4 * Copyright (c) 1990, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Mike Olson.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
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. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 */
38
39 #include <sys/cdefs.h>
40 #if defined(LIBC_SCCS) && !defined(lint)
41 #if 0
42 static char sccsid[] = "@(#)rec_open.c 8.10 (Berkeley) 9/1/94";
43 #else
44 __RCSID("$NetBSD: rec_open.c,v 1.8 1997/07/13 18:52:12 christos Exp $");
45 #endif
46 #endif /* LIBC_SCCS and not lint */
47
48 #include <sys/types.h>
49 #include <sys/mman.h>
50 #include <sys/stat.h>
51
52 #include <errno.h>
53 #include <fcntl.h>
54 #include <limits.h>
55 #include <stddef.h>
56 #include <stdio.h>
57 #include <unistd.h>
58
59 #include <db.h>
60 #include "recno.h"
61
62 DB *
63 __rec_open(fname, flags, mode, openinfo, dflags)
64 const char *fname;
65 int flags, mode, dflags;
66 const RECNOINFO *openinfo;
67 {
68 BTREE *t;
69 BTREEINFO btopeninfo;
70 DB *dbp;
71 PAGE *h;
72 struct stat sb;
73 int rfd = -1; /* pacify gcc */
74 int sverrno;
75
76 /* Open the user's file -- if this fails, we're done. */
77 if (fname != NULL && (rfd = open(fname, flags, mode)) < 0)
78 return (NULL);
79
80 /* Create a btree in memory (backed by disk). */
81 dbp = NULL;
82 if (openinfo) {
83 if (openinfo->flags & ~(R_FIXEDLEN | R_NOKEY | R_SNAPSHOT))
84 goto einval;
85 btopeninfo.flags = 0;
86 btopeninfo.cachesize = openinfo->cachesize;
87 btopeninfo.maxkeypage = 0;
88 btopeninfo.minkeypage = 0;
89 btopeninfo.psize = openinfo->psize;
90 btopeninfo.compare = NULL;
91 btopeninfo.prefix = NULL;
92 btopeninfo.lorder = openinfo->lorder;
93 dbp = __bt_open(openinfo->bfname,
94 O_RDWR, S_IRUSR | S_IWUSR, &btopeninfo, dflags);
95 } else
96 dbp = __bt_open(NULL, O_RDWR, S_IRUSR | S_IWUSR, NULL, dflags);
97 if (dbp == NULL)
98 goto err;
99
100 /*
101 * Some fields in the tree structure are recno specific. Fill them
102 * in and make the btree structure look like a recno structure. We
103 * don't change the bt_ovflsize value, it's close enough and slightly
104 * bigger.
105 */
106 t = dbp->internal;
107 if (openinfo) {
108 if (openinfo->flags & R_FIXEDLEN) {
109 F_SET(t, R_FIXLEN);
110 t->bt_reclen = openinfo->reclen;
111 if (t->bt_reclen == 0)
112 goto einval;
113 }
114 t->bt_bval = openinfo->bval;
115 } else
116 t->bt_bval = '\n';
117
118 F_SET(t, R_RECNO);
119 if (fname == NULL)
120 F_SET(t, R_EOF | R_INMEM);
121 else
122 t->bt_rfd = rfd;
123
124 if (fname != NULL) {
125 /*
126 * In 4.4BSD, stat(2) returns true for ISSOCK on pipes.
127 * Unfortunately, that's not portable, so we use lseek
128 * and check the errno values.
129 */
130 errno = 0;
131 if (lseek(rfd, (off_t)0, SEEK_CUR) == -1 && errno == ESPIPE) {
132 switch (flags & O_ACCMODE) {
133 case O_RDONLY:
134 F_SET(t, R_RDONLY);
135 break;
136 default:
137 goto einval;
138 }
139 slow: if ((t->bt_rfp = fdopen(rfd, "r")) == NULL)
140 goto err;
141 F_SET(t, R_CLOSEFP);
142 t->bt_irec =
143 F_ISSET(t, R_FIXLEN) ? __rec_fpipe : __rec_vpipe;
144 } else {
145 switch (flags & O_ACCMODE) {
146 case O_RDONLY:
147 F_SET(t, R_RDONLY);
148 break;
149 case O_RDWR:
150 break;
151 default:
152 goto einval;
153 }
154
155 if (fstat(rfd, &sb))
156 goto err;
157 /*
158 * Kluge -- we'd like to test to see if the file is too
159 * big to mmap. Since, we don't know what size or type
160 * off_t's or size_t's are, what the largest unsigned
161 * integral type is, or what random insanity the local
162 * C compiler will perpetrate, doing the comparison in
163 * a portable way is flatly impossible. Hope that mmap
164 * fails if the file is too large.
165 */
166 if (sb.st_size == 0)
167 F_SET(t, R_EOF);
168 else {
169 #ifdef MMAP_NOT_AVAILABLE
170 /*
171 * XXX
172 * Mmap doesn't work correctly on many current
173 * systems. In particular, it can fail subtly,
174 * with cache coherency problems. Don't use it
175 * for now.
176 */
177 t->bt_msize = sb.st_size;
178 if ((t->bt_smap = mmap(NULL, t->bt_msize,
179 PROT_READ, MAP_PRIVATE, rfd,
180 (off_t)0)) == (caddr_t)-1)
181 goto slow;
182 t->bt_cmap = t->bt_smap;
183 t->bt_emap = t->bt_smap + sb.st_size;
184 t->bt_irec = F_ISSET(t, R_FIXLEN) ?
185 __rec_fmap : __rec_vmap;
186 F_SET(t, R_MEMMAPPED);
187 #else
188 goto slow;
189 #endif
190 }
191 }
192 }
193
194 /* Use the recno routines. */
195 dbp->close = __rec_close;
196 dbp->del = __rec_delete;
197 dbp->fd = __rec_fd;
198 dbp->get = __rec_get;
199 dbp->put = __rec_put;
200 dbp->seq = __rec_seq;
201 dbp->sync = __rec_sync;
202
203 /* If the root page was created, reset the flags. */
204 if ((h = mpool_get(t->bt_mp, P_ROOT, 0)) == NULL)
205 goto err;
206 if ((h->flags & P_TYPE) == P_BLEAF) {
207 F_CLR(h, P_TYPE);
208 F_SET(h, P_RLEAF);
209 mpool_put(t->bt_mp, h, MPOOL_DIRTY);
210 } else
211 mpool_put(t->bt_mp, h, 0);
212
213 if (openinfo && openinfo->flags & R_SNAPSHOT &&
214 !F_ISSET(t, R_EOF | R_INMEM) &&
215 t->bt_irec(t, MAX_REC_NUMBER) == RET_ERROR)
216 goto err;
217 return (dbp);
218
219 einval: errno = EINVAL;
220 err: sverrno = errno;
221 if (dbp != NULL)
222 (void)__bt_close(dbp);
223 if (fname != NULL)
224 (void)close(rfd);
225 errno = sverrno;
226 return (NULL);
227 }
228
229 int
230 __rec_fd(dbp)
231 const DB *dbp;
232 {
233 BTREE *t;
234
235 t = dbp->internal;
236
237 /* Toss any page pinned across calls. */
238 if (t->bt_pinned != NULL) {
239 mpool_put(t->bt_mp, t->bt_pinned, 0);
240 t->bt_pinned = NULL;
241 }
242
243 /* In-memory database can't have a file descriptor. */
244 if (F_ISSET(t, R_INMEM)) {
245 errno = ENOENT;
246 return (-1);
247 }
248 return (t->bt_rfd);
249 }
250