Commit | Line | Data |
---|---|---|
91b5c9e5 | 1 | /*- |
fd88f5c5 | 2 | * Copyright (c) 1990, 1993, 1994 |
e659a03d | 3 | * The Regents of the University of California. All rights reserved. |
91b5c9e5 KB |
4 | * |
5 | * This code is derived from software contributed to Berkeley by | |
6 | * Mike Olson. | |
7 | * | |
ad787160 C |
8 | * Redistribution and use in source and binary forms, with or without |
9 | * modification, are permitted provided that the following conditions | |
10 | * are met: | |
11 | * 1. Redistributions of source code must retain the above copyright | |
12 | * notice, this list of conditions and the following disclaimer. | |
13 | * 2. Redistributions in binary form must reproduce the above copyright | |
14 | * notice, this list of conditions and the following disclaimer in the | |
15 | * documentation and/or other materials provided with the distribution. | |
16 | * 3. All advertising materials mentioning features or use of this software | |
17 | * must display the following acknowledgement: | |
18 | * This product includes software developed by the University of | |
19 | * California, Berkeley and its contributors. | |
20 | * 4. Neither the name of the University nor the names of its contributors | |
21 | * may be used to endorse or promote products derived from this software | |
22 | * without specific prior written permission. | |
23 | * | |
24 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
25 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
26 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
27 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
28 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
29 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
30 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
31 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
32 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
33 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
34 | * SUCH DAMAGE. | |
91b5c9e5 KB |
35 | */ |
36 | ||
37 | #if defined(LIBC_SCCS) && !defined(lint) | |
fd88f5c5 | 38 | static char sccsid[] = "@(#)rec_open.c 8.12 (Berkeley) 11/18/94"; |
91b5c9e5 KB |
39 | #endif /* LIBC_SCCS and not lint */ |
40 | ||
41 | #include <sys/types.h> | |
fd88f5c5 | 42 | #ifdef RECNO_USE_MMAP |
91b5c9e5 | 43 | #include <sys/mman.h> |
fd88f5c5 | 44 | #endif |
91b5c9e5 | 45 | #include <sys/stat.h> |
d4bfe2c2 | 46 | |
91b5c9e5 | 47 | #include <errno.h> |
a156e1f1 | 48 | #include <fcntl.h> |
91b5c9e5 | 49 | #include <limits.h> |
91b5c9e5 | 50 | #include <stddef.h> |
a156e1f1 KB |
51 | #include <stdio.h> |
52 | #include <unistd.h> | |
53 | ||
94ac72c5 | 54 | #include <db.h> |
dfe3e67a | 55 | #include "recno.h" |
91b5c9e5 KB |
56 | |
57 | DB * | |
bf1c55e1 | 58 | __rec_open(fname, flags, mode, openinfo, dflags) |
91b5c9e5 | 59 | const char *fname; |
bf1c55e1 | 60 | int flags, mode, dflags; |
91b5c9e5 KB |
61 | const RECNOINFO *openinfo; |
62 | { | |
63 | BTREE *t; | |
64 | BTREEINFO btopeninfo; | |
65 | DB *dbp; | |
66 | PAGE *h; | |
67 | struct stat sb; | |
78393cf4 | 68 | int rfd, sverrno; |
91b5c9e5 KB |
69 | |
70 | /* Open the user's file -- if this fails, we're done. */ | |
a156e1f1 | 71 | if (fname != NULL && (rfd = open(fname, flags, mode)) < 0) |
91b5c9e5 KB |
72 | return (NULL); |
73 | ||
74 | /* Create a btree in memory (backed by disk). */ | |
a156e1f1 | 75 | dbp = NULL; |
91b5c9e5 | 76 | if (openinfo) { |
a156e1f1 | 77 | if (openinfo->flags & ~(R_FIXEDLEN | R_NOKEY | R_SNAPSHOT)) |
46f5b8f4 | 78 | goto einval; |
91b5c9e5 KB |
79 | btopeninfo.flags = 0; |
80 | btopeninfo.cachesize = openinfo->cachesize; | |
a0297b9c RC |
81 | btopeninfo.maxkeypage = 0; |
82 | btopeninfo.minkeypage = 0; | |
a05339f7 | 83 | btopeninfo.psize = openinfo->psize; |
91b5c9e5 | 84 | btopeninfo.compare = NULL; |
a0297b9c | 85 | btopeninfo.prefix = NULL; |
91b5c9e5 | 86 | btopeninfo.lorder = openinfo->lorder; |
ff15fc91 | 87 | dbp = __bt_open(openinfo->bfname, |
bf1c55e1 | 88 | O_RDWR, S_IRUSR | S_IWUSR, &btopeninfo, dflags); |
91b5c9e5 | 89 | } else |
bf1c55e1 | 90 | dbp = __bt_open(NULL, O_RDWR, S_IRUSR | S_IWUSR, NULL, dflags); |
dfe3e67a KB |
91 | if (dbp == NULL) |
92 | goto err; | |
91b5c9e5 KB |
93 | |
94 | /* | |
95 | * Some fields in the tree structure are recno specific. Fill them | |
dfe3e67a KB |
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. | |
91b5c9e5 KB |
99 | */ |
100 | t = dbp->internal; | |
101 | if (openinfo) { | |
a6678652 | 102 | if (openinfo->flags & R_FIXEDLEN) { |
fd88f5c5 | 103 | F_SET(t, R_FIXLEN); |
a6678652 | 104 | t->bt_reclen = openinfo->reclen; |
46f5b8f4 KB |
105 | if (t->bt_reclen == 0) |
106 | goto einval; | |
91b5c9e5 | 107 | } |
91b5c9e5 KB |
108 | t->bt_bval = openinfo->bval; |
109 | } else | |
110 | t->bt_bval = '\n'; | |
111 | ||
fd88f5c5 | 112 | F_SET(t, R_RECNO); |
78393cf4 | 113 | if (fname == NULL) |
fd88f5c5 | 114 | F_SET(t, R_EOF | R_INMEM); |
78393cf4 KB |
115 | else |
116 | t->bt_rfd = rfd; | |
91b5c9e5 | 117 | |
a0297b9c | 118 | if (fname != NULL) { |
22b471c0 KB |
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; | |
a156e1f1 | 125 | if (lseek(rfd, (off_t)0, SEEK_CUR) == -1 && errno == ESPIPE) { |
a0297b9c | 126 | switch (flags & O_ACCMODE) { |
78393cf4 | 127 | case O_RDONLY: |
fd88f5c5 | 128 | F_SET(t, R_RDONLY); |
78393cf4 | 129 | break; |
78393cf4 KB |
130 | default: |
131 | goto einval; | |
132 | } | |
b59a7d42 | 133 | slow: if ((t->bt_rfp = fdopen(rfd, "r")) == NULL) |
a156e1f1 | 134 | goto err; |
fd88f5c5 | 135 | F_SET(t, R_CLOSEFP); |
a156e1f1 | 136 | t->bt_irec = |
fd88f5c5 | 137 | F_ISSET(t, R_FIXLEN) ? __rec_fpipe : __rec_vpipe; |
a156e1f1 | 138 | } else { |
a0297b9c | 139 | switch (flags & O_ACCMODE) { |
a156e1f1 | 140 | case O_RDONLY: |
fd88f5c5 | 141 | F_SET(t, R_RDONLY); |
a156e1f1 KB |
142 | break; |
143 | case O_RDWR: | |
144 | break; | |
a156e1f1 KB |
145 | default: |
146 | goto einval; | |
147 | } | |
a0297b9c | 148 | |
78393cf4 KB |
149 | if (fstat(rfd, &sb)) |
150 | goto err; | |
2daa68f8 | 151 | /* |
fb6b8e52 KB |
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. | |
2daa68f8 | 159 | */ |
78393cf4 | 160 | if (sb.st_size == 0) |
fd88f5c5 | 161 | F_SET(t, R_EOF); |
78393cf4 | 162 | else { |
fd88f5c5 C |
163 | #ifdef RECNO_USE_MMAP |
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 | */ | |
78393cf4 | 171 | t->bt_msize = sb.st_size; |
4dfe198a KB |
172 | if ((t->bt_smap = mmap(NULL, t->bt_msize, |
173 | PROT_READ, MAP_PRIVATE, rfd, | |
78393cf4 KB |
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; | |
fd88f5c5 | 178 | t->bt_irec = F_ISSET(t, R_FIXLEN) ? |
78393cf4 | 179 | __rec_fmap : __rec_vmap; |
fd88f5c5 C |
180 | F_SET(t, R_MEMMAPPED); |
181 | #else | |
182 | goto slow; | |
183 | #endif | |
78393cf4 | 184 | } |
91860078 | 185 | } |
a0297b9c | 186 | } |
91b5c9e5 KB |
187 | |
188 | /* Use the recno routines. */ | |
189 | dbp->close = __rec_close; | |
190 | dbp->del = __rec_delete; | |
83bfc92e | 191 | dbp->fd = __rec_fd; |
91b5c9e5 KB |
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) { | |
fd88f5c5 C |
201 | F_CLR(h, P_TYPE); |
202 | F_SET(h, P_RLEAF); | |
91b5c9e5 KB |
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 && | |
fd88f5c5 | 208 | !F_ISSET(t, R_EOF | R_INMEM) && |
91b5c9e5 KB |
209 | t->bt_irec(t, MAX_REC_NUMBER) == RET_ERROR) |
210 | goto err; | |
211 | return (dbp); | |
212 | ||
46f5b8f4 | 213 | einval: errno = EINVAL; |
78393cf4 KB |
214 | err: sverrno = errno; |
215 | if (dbp != NULL) | |
216 | (void)__bt_close(dbp); | |
a156e1f1 KB |
217 | if (fname != NULL) |
218 | (void)close(rfd); | |
78393cf4 | 219 | errno = sverrno; |
91b5c9e5 KB |
220 | return (NULL); |
221 | } | |
83bfc92e KB |
222 | |
223 | int | |
224 | __rec_fd(dbp) | |
225 | const DB *dbp; | |
226 | { | |
227 | BTREE *t; | |
228 | ||
229 | t = dbp->internal; | |
230 | ||
bf1c55e1 KB |
231 | /* Toss any page pinned across calls. */ |
232 | if (t->bt_pinned != NULL) { | |
233 | mpool_put(t->bt_mp, t->bt_pinned, 0); | |
234 | t->bt_pinned = NULL; | |
235 | } | |
236 | ||
237 | /* In-memory database can't have a file descriptor. */ | |
fd88f5c5 | 238 | if (F_ISSET(t, R_INMEM)) { |
83bfc92e KB |
239 | errno = ENOENT; |
240 | return (-1); | |
241 | } | |
242 | return (t->bt_rfd); | |
243 | } |