rec_open.c revision 1.9 1 /* $NetBSD: rec_open.c,v 1.9 1997/07/21 14:06:45 jtc 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.9 1997/07/21 14:06:45 jtc Exp $");
45 #endif
46 #endif /* LIBC_SCCS and not lint */
47
48 #include "namespace.h"
49 #include <sys/types.h>
50 #include <sys/mman.h>
51 #include <sys/stat.h>
52
53 #include <errno.h>
54 #include <fcntl.h>
55 #include <limits.h>
56 #include <stddef.h>
57 #include <stdio.h>
58 #include <unistd.h>
59
60 #include <db.h>
61 #include "recno.h"
62
63 DB *
64 __rec_open(fname, flags, mode, openinfo, dflags)
65 const char *fname;
66 int flags, mode, dflags;
67 const RECNOINFO *openinfo;
68 {
69 BTREE *t;
70 BTREEINFO btopeninfo;
71 DB *dbp;
72 PAGE *h;
73 struct stat sb;
74 int rfd = -1; /* pacify gcc */
75 int sverrno;
76
77 /* Open the user's file -- if this fails, we're done. */
78 if (fname != NULL && (rfd = open(fname, flags, mode)) < 0)
79 return (NULL);
80
81 /* Create a btree in memory (backed by disk). */
82 dbp = NULL;
83 if (openinfo) {
84 if (openinfo->flags & ~(R_FIXEDLEN | R_NOKEY | R_SNAPSHOT))
85 goto einval;
86 btopeninfo.flags = 0;
87 btopeninfo.cachesize = openinfo->cachesize;
88 btopeninfo.maxkeypage = 0;
89 btopeninfo.minkeypage = 0;
90 btopeninfo.psize = openinfo->psize;
91 btopeninfo.compare = NULL;
92 btopeninfo.prefix = NULL;
93 btopeninfo.lorder = openinfo->lorder;
94 dbp = __bt_open(openinfo->bfname,
95 O_RDWR, S_IRUSR | S_IWUSR, &btopeninfo, dflags);
96 } else
97 dbp = __bt_open(NULL, O_RDWR, S_IRUSR | S_IWUSR, NULL, dflags);
98 if (dbp == NULL)
99 goto err;
100
101 /*
102 * Some fields in the tree structure are recno specific. Fill them
103 * in and make the btree structure look like a recno structure. We
104 * don't change the bt_ovflsize value, it's close enough and slightly
105 * bigger.
106 */
107 t = dbp->internal;
108 if (openinfo) {
109 if (openinfo->flags & R_FIXEDLEN) {
110 F_SET(t, R_FIXLEN);
111 t->bt_reclen = openinfo->reclen;
112 if (t->bt_reclen == 0)
113 goto einval;
114 }
115 t->bt_bval = openinfo->bval;
116 } else
117 t->bt_bval = '\n';
118
119 F_SET(t, R_RECNO);
120 if (fname == NULL)
121 F_SET(t, R_EOF | R_INMEM);
122 else
123 t->bt_rfd = rfd;
124
125 if (fname != NULL) {
126 /*
127 * In 4.4BSD, stat(2) returns true for ISSOCK on pipes.
128 * Unfortunately, that's not portable, so we use lseek
129 * and check the errno values.
130 */
131 errno = 0;
132 if (lseek(rfd, (off_t)0, SEEK_CUR) == -1 && errno == ESPIPE) {
133 switch (flags & O_ACCMODE) {
134 case O_RDONLY:
135 F_SET(t, R_RDONLY);
136 break;
137 default:
138 goto einval;
139 }
140 slow: if ((t->bt_rfp = fdopen(rfd, "r")) == NULL)
141 goto err;
142 F_SET(t, R_CLOSEFP);
143 t->bt_irec =
144 F_ISSET(t, R_FIXLEN) ? __rec_fpipe : __rec_vpipe;
145 } else {
146 switch (flags & O_ACCMODE) {
147 case O_RDONLY:
148 F_SET(t, R_RDONLY);
149 break;
150 case O_RDWR:
151 break;
152 default:
153 goto einval;
154 }
155
156 if (fstat(rfd, &sb))
157 goto err;
158 /*
159 * Kluge -- we'd like to test to see if the file is too
160 * big to mmap. Since, we don't know what size or type
161 * off_t's or size_t's are, what the largest unsigned
162 * integral type is, or what random insanity the local
163 * C compiler will perpetrate, doing the comparison in
164 * a portable way is flatly impossible. Hope that mmap
165 * fails if the file is too large.
166 */
167 if (sb.st_size == 0)
168 F_SET(t, R_EOF);
169 else {
170 #ifdef MMAP_NOT_AVAILABLE
171 /*
172 * XXX
173 * Mmap doesn't work correctly on many current
174 * systems. In particular, it can fail subtly,
175 * with cache coherency problems. Don't use it
176 * for now.
177 */
178 t->bt_msize = sb.st_size;
179 if ((t->bt_smap = mmap(NULL, t->bt_msize,
180 PROT_READ, MAP_PRIVATE, rfd,
181 (off_t)0)) == (caddr_t)-1)
182 goto slow;
183 t->bt_cmap = t->bt_smap;
184 t->bt_emap = t->bt_smap + sb.st_size;
185 t->bt_irec = F_ISSET(t, R_FIXLEN) ?
186 __rec_fmap : __rec_vmap;
187 F_SET(t, R_MEMMAPPED);
188 #else
189 goto slow;
190 #endif
191 }
192 }
193 }
194
195 /* Use the recno routines. */
196 dbp->close = __rec_close;
197 dbp->del = __rec_delete;
198 dbp->fd = __rec_fd;
199 dbp->get = __rec_get;
200 dbp->put = __rec_put;
201 dbp->seq = __rec_seq;
202 dbp->sync = __rec_sync;
203
204 /* If the root page was created, reset the flags. */
205 if ((h = mpool_get(t->bt_mp, P_ROOT, 0)) == NULL)
206 goto err;
207 if ((h->flags & P_TYPE) == P_BLEAF) {
208 F_CLR(h, P_TYPE);
209 F_SET(h, P_RLEAF);
210 mpool_put(t->bt_mp, h, MPOOL_DIRTY);
211 } else
212 mpool_put(t->bt_mp, h, 0);
213
214 if (openinfo && openinfo->flags & R_SNAPSHOT &&
215 !F_ISSET(t, R_EOF | R_INMEM) &&
216 t->bt_irec(t, MAX_REC_NUMBER) == RET_ERROR)
217 goto err;
218 return (dbp);
219
220 einval: errno = EINVAL;
221 err: sverrno = errno;
222 if (dbp != NULL)
223 (void)__bt_close(dbp);
224 if (fname != NULL)
225 (void)close(rfd);
226 errno = sverrno;
227 return (NULL);
228 }
229
230 int
231 __rec_fd(dbp)
232 const DB *dbp;
233 {
234 BTREE *t;
235
236 t = dbp->internal;
237
238 /* Toss any page pinned across calls. */
239 if (t->bt_pinned != NULL) {
240 mpool_put(t->bt_mp, t->bt_pinned, 0);
241 t->bt_pinned = NULL;
242 }
243
244 /* In-memory database can't have a file descriptor. */
245 if (F_ISSET(t, R_INMEM)) {
246 errno = ENOENT;
247 return (-1);
248 }
249 return (t->bt_rfd);
250 }
251