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