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