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