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