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