rec_open.c revision 1.2 1 /*-
2 * Copyright (c) 1990, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Mike Olson.
7 *
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.
35 */
36
37 #if defined(LIBC_SCCS) && !defined(lint)
38 /*static char sccsid[] = "from: @(#)rec_open.c 8.1 (Berkeley) 6/4/93";*/
39 static char rcsid[] = "$Id: rec_open.c,v 1.2 1993/08/01 18:43:13 mycroft Exp $";
40 #endif /* LIBC_SCCS and not lint */
41
42 #include <sys/types.h>
43 #include <sys/mman.h>
44 #include <sys/stat.h>
45
46 #include <errno.h>
47 #include <fcntl.h>
48 #include <limits.h>
49 #include <stddef.h>
50 #include <stdio.h>
51 #include <unistd.h>
52
53 #include <db.h>
54 #include "recno.h"
55
56 DB *
57 __rec_open(fname, flags, mode, openinfo)
58 const char *fname;
59 int flags, mode;
60 const RECNOINFO *openinfo;
61 {
62 BTREE *t;
63 BTREEINFO btopeninfo;
64 DB *dbp;
65 PAGE *h;
66 struct stat sb;
67 int rfd, sverrno;
68
69 /* Open the user's file -- if this fails, we're done. */
70 if (fname != NULL && (rfd = open(fname, flags, mode)) < 0)
71 return (NULL);
72
73 /* Create a btree in memory (backed by disk). */
74 dbp = NULL;
75 if (openinfo) {
76 if (openinfo->flags & ~(R_FIXEDLEN | R_NOKEY | R_SNAPSHOT))
77 goto einval;
78 btopeninfo.flags = 0;
79 btopeninfo.cachesize = openinfo->cachesize;
80 btopeninfo.maxkeypage = 0;
81 btopeninfo.minkeypage = 0;
82 btopeninfo.psize = openinfo->psize;
83 btopeninfo.compare = NULL;
84 btopeninfo.prefix = NULL;
85 btopeninfo.lorder = openinfo->lorder;
86 dbp = __bt_open(openinfo->bfname,
87 O_RDWR, S_IRUSR | S_IWUSR, &btopeninfo);
88 } else
89 dbp = __bt_open(NULL, O_RDWR, S_IRUSR | S_IWUSR, NULL);
90 if (dbp == NULL)
91 goto err;
92
93 /*
94 * Some fields in the tree structure are recno specific. Fill them
95 * in and make the btree structure look like a recno structure. We
96 * don't change the bt_ovflsize value, it's close enough and slightly
97 * bigger.
98 */
99 t = dbp->internal;
100 if (openinfo) {
101 if (openinfo->flags & R_FIXEDLEN) {
102 SET(t, R_FIXLEN);
103 t->bt_reclen = openinfo->reclen;
104 if (t->bt_reclen == 0)
105 goto einval;
106 }
107 t->bt_bval = openinfo->bval;
108 } else
109 t->bt_bval = '\n';
110
111 SET(t, R_RECNO);
112 if (fname == NULL)
113 SET(t, R_EOF | R_INMEM);
114 else
115 t->bt_rfd = rfd;
116 t->bt_rcursor = 0;
117
118 /*
119 * In 4.4BSD stat(2) returns true for ISSOCK on pipes. Until
120 * then, this is fairly close. Pipes are read-only.
121 */
122 if (fname != NULL) {
123 if (lseek(rfd, (off_t)0, SEEK_CUR) == -1 && errno == ESPIPE) {
124 switch (flags & O_ACCMODE) {
125 case O_RDONLY:
126 SET(t, R_RDONLY);
127 break;
128 default:
129 goto einval;
130 }
131 slow: if ((t->bt_rfp = fdopen(rfd, "r")) == NULL)
132 goto err;
133 SET(t, R_CLOSEFP);
134 t->bt_irec =
135 ISSET(t, R_FIXLEN) ? __rec_fpipe : __rec_vpipe;
136 } else {
137 switch (flags & O_ACCMODE) {
138 case O_RDONLY:
139 SET(t, R_RDONLY);
140 break;
141 case O_RDWR:
142 break;
143 default:
144 goto einval;
145 }
146
147 if (fstat(rfd, &sb))
148 goto err;
149 /*
150 * Kluge -- we'd like to test to see if the file is too
151 * big to mmap. Since, we don't know what size or type
152 * off_t's or size_t's are, what the largest unsigned
153 * integral type is, or what random insanity the local
154 * C compiler will perpetrate, doing the comparison in
155 * a portable way is flatly impossible. Hope that mmap
156 * fails if the file is too large.
157 */
158 if (sb.st_size == 0)
159 SET(t, R_EOF);
160 else {
161 t->bt_msize = sb.st_size;
162 if ((t->bt_smap =
163 mmap(NULL, t->bt_msize, PROT_READ, 0, rfd,
164 (off_t)0)) == (caddr_t)-1)
165 goto slow;
166 t->bt_cmap = t->bt_smap;
167 t->bt_emap = t->bt_smap + sb.st_size;
168 t->bt_irec = ISSET(t, R_FIXLEN) ?
169 __rec_fmap : __rec_vmap;
170 SET(t, R_MEMMAPPED);
171 }
172 }
173 }
174
175 /* Use the recno routines. */
176 dbp->close = __rec_close;
177 dbp->del = __rec_delete;
178 dbp->fd = __rec_fd;
179 dbp->get = __rec_get;
180 dbp->put = __rec_put;
181 dbp->seq = __rec_seq;
182 dbp->sync = __rec_sync;
183
184 /* If the root page was created, reset the flags. */
185 if ((h = mpool_get(t->bt_mp, P_ROOT, 0)) == NULL)
186 goto err;
187 if ((h->flags & P_TYPE) == P_BLEAF) {
188 h->flags = h->flags & ~P_TYPE | P_RLEAF;
189 mpool_put(t->bt_mp, h, MPOOL_DIRTY);
190 } else
191 mpool_put(t->bt_mp, h, 0);
192
193 if (openinfo && openinfo->flags & R_SNAPSHOT &&
194 !ISSET(t, R_EOF | R_INMEM) &&
195 t->bt_irec(t, MAX_REC_NUMBER) == RET_ERROR)
196 goto err;
197 return (dbp);
198
199 einval: errno = EINVAL;
200 err: sverrno = errno;
201 if (dbp != NULL)
202 (void)__bt_close(dbp);
203 if (fname != NULL)
204 (void)close(rfd);
205 errno = sverrno;
206 return (NULL);
207 }
208
209 int
210 __rec_fd(dbp)
211 const DB *dbp;
212 {
213 BTREE *t;
214
215 t = dbp->internal;
216
217 if (ISSET(t, R_INMEM)) {
218 errno = ENOENT;
219 return (-1);
220 }
221 return (t->bt_rfd);
222 }
223