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