1*aae80e6bSchristos /* $NetBSD: rec_open.c,v 1.22 2016/09/24 21:31:25 christos Exp $ */
22c84ad3aScgd
39f0aa214Scgd /*-
4738330daScgd * Copyright (c) 1990, 1993, 1994
59f0aa214Scgd * The Regents of the University of California. All rights reserved.
69f0aa214Scgd *
79f0aa214Scgd * This code is derived from software contributed to Berkeley by
89f0aa214Scgd * Mike Olson.
99f0aa214Scgd *
109f0aa214Scgd * Redistribution and use in source and binary forms, with or without
119f0aa214Scgd * modification, are permitted provided that the following conditions
129f0aa214Scgd * are met:
139f0aa214Scgd * 1. Redistributions of source code must retain the above copyright
149f0aa214Scgd * notice, this list of conditions and the following disclaimer.
159f0aa214Scgd * 2. Redistributions in binary form must reproduce the above copyright
169f0aa214Scgd * notice, this list of conditions and the following disclaimer in the
179f0aa214Scgd * documentation and/or other materials provided with the distribution.
18eb7c1594Sagc * 3. Neither the name of the University nor the names of its contributors
199f0aa214Scgd * may be used to endorse or promote products derived from this software
209f0aa214Scgd * without specific prior written permission.
219f0aa214Scgd *
229f0aa214Scgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
239f0aa214Scgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
249f0aa214Scgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
259f0aa214Scgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
269f0aa214Scgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
279f0aa214Scgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
289f0aa214Scgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
299f0aa214Scgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
309f0aa214Scgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
319f0aa214Scgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
329f0aa214Scgd * SUCH DAMAGE.
339f0aa214Scgd */
349f0aa214Scgd
35d3595ddfSjoerg #if HAVE_NBTOOL_CONFIG_H
36d3595ddfSjoerg #include "nbtool_config.h"
37d3595ddfSjoerg #endif
38d3595ddfSjoerg
3900ae392dSchristos #include <sys/cdefs.h>
40*aae80e6bSchristos __RCSID("$NetBSD: rec_open.c,v 1.22 2016/09/24 21:31:25 christos Exp $");
419f0aa214Scgd
4243fa6fe3Sjtc #include "namespace.h"
439f0aa214Scgd #include <sys/types.h>
449f0aa214Scgd #include <sys/mman.h>
459f0aa214Scgd #include <sys/stat.h>
469f0aa214Scgd
47cb9daf8fSchristos #include <assert.h>
489f0aa214Scgd #include <errno.h>
499f0aa214Scgd #include <fcntl.h>
509f0aa214Scgd #include <limits.h>
519f0aa214Scgd #include <stddef.h>
529f0aa214Scgd #include <stdio.h>
539f0aa214Scgd #include <unistd.h>
549f0aa214Scgd
559f0aa214Scgd #include <db.h>
569f0aa214Scgd #include "recno.h"
579f0aa214Scgd
589f0aa214Scgd DB *
__rec_open(const char * fname,int flags,mode_t mode,const RECNOINFO * openinfo,int dflags)59cb9daf8fSchristos __rec_open(const char *fname, int flags, mode_t mode, const RECNOINFO *openinfo,
60cb9daf8fSchristos int dflags)
619f0aa214Scgd {
629f0aa214Scgd BTREE *t;
639f0aa214Scgd BTREEINFO btopeninfo;
649f0aa214Scgd DB *dbp;
659f0aa214Scgd PAGE *h;
669f0aa214Scgd struct stat sb;
6700ae392dSchristos int rfd = -1; /* pacify gcc */
6800ae392dSchristos int sverrno;
699f0aa214Scgd
7063246b1eSlukem dbp = NULL;
719f0aa214Scgd /* Open the user's file -- if this fails, we're done. */
72b7e6351cSmycroft if (fname != NULL) {
73b605a13bSchristos if ((rfd = __dbopen(fname, flags, mode, NULL)) == -1)
749ad6d70cSchristos return NULL;
75b7e6351cSmycroft }
769f0aa214Scgd
779f0aa214Scgd /* Create a btree in memory (backed by disk). */
789f0aa214Scgd if (openinfo) {
799f0aa214Scgd if (openinfo->flags & ~(R_FIXEDLEN | R_NOKEY | R_SNAPSHOT))
809f0aa214Scgd goto einval;
819f0aa214Scgd btopeninfo.flags = 0;
829f0aa214Scgd btopeninfo.cachesize = openinfo->cachesize;
839f0aa214Scgd btopeninfo.maxkeypage = 0;
849f0aa214Scgd btopeninfo.minkeypage = 0;
859f0aa214Scgd btopeninfo.psize = openinfo->psize;
869f0aa214Scgd btopeninfo.compare = NULL;
879f0aa214Scgd btopeninfo.prefix = NULL;
889f0aa214Scgd btopeninfo.lorder = openinfo->lorder;
899f0aa214Scgd dbp = __bt_open(openinfo->bfname,
9045e27c80Scgd O_RDWR, S_IRUSR | S_IWUSR, &btopeninfo, dflags);
919f0aa214Scgd } else
9245e27c80Scgd dbp = __bt_open(NULL, O_RDWR, S_IRUSR | S_IWUSR, NULL, dflags);
939f0aa214Scgd if (dbp == NULL)
949f0aa214Scgd goto err;
959f0aa214Scgd
969f0aa214Scgd /*
979f0aa214Scgd * Some fields in the tree structure are recno specific. Fill them
989f0aa214Scgd * in and make the btree structure look like a recno structure. We
999f0aa214Scgd * don't change the bt_ovflsize value, it's close enough and slightly
1009f0aa214Scgd * bigger.
1019f0aa214Scgd */
1029f0aa214Scgd t = dbp->internal;
1039f0aa214Scgd if (openinfo) {
1049f0aa214Scgd if (openinfo->flags & R_FIXEDLEN) {
105738330daScgd F_SET(t, R_FIXLEN);
1069f0aa214Scgd t->bt_reclen = openinfo->reclen;
1079f0aa214Scgd if (t->bt_reclen == 0)
1089f0aa214Scgd goto einval;
1099f0aa214Scgd }
1109f0aa214Scgd t->bt_bval = openinfo->bval;
1119f0aa214Scgd } else
1129f0aa214Scgd t->bt_bval = '\n';
1139f0aa214Scgd
114738330daScgd F_SET(t, R_RECNO);
1159f0aa214Scgd if (fname == NULL)
116738330daScgd F_SET(t, R_EOF | R_INMEM);
1179f0aa214Scgd else
1189f0aa214Scgd t->bt_rfd = rfd;
1199f0aa214Scgd
1209f0aa214Scgd if (fname != NULL) {
121a6d14e36Scgd /*
122a6d14e36Scgd * In 4.4BSD, stat(2) returns true for ISSOCK on pipes.
123a6d14e36Scgd * Unfortunately, that's not portable, so we use lseek
124a6d14e36Scgd * and check the errno values.
125a6d14e36Scgd */
126a6d14e36Scgd errno = 0;
1279f0aa214Scgd if (lseek(rfd, (off_t)0, SEEK_CUR) == -1 && errno == ESPIPE) {
1289f0aa214Scgd switch (flags & O_ACCMODE) {
1299f0aa214Scgd case O_RDONLY:
130738330daScgd F_SET(t, R_RDONLY);
1319f0aa214Scgd break;
1329f0aa214Scgd default:
1339f0aa214Scgd goto einval;
1349f0aa214Scgd }
1359f0aa214Scgd slow: if ((t->bt_rfp = fdopen(rfd, "r")) == NULL)
1369f0aa214Scgd goto err;
137738330daScgd F_SET(t, R_CLOSEFP);
1389f0aa214Scgd t->bt_irec =
139738330daScgd F_ISSET(t, R_FIXLEN) ? __rec_fpipe : __rec_vpipe;
1409f0aa214Scgd } else {
1419f0aa214Scgd switch (flags & O_ACCMODE) {
1429f0aa214Scgd case O_RDONLY:
143738330daScgd F_SET(t, R_RDONLY);
1449f0aa214Scgd break;
1459f0aa214Scgd case O_RDWR:
1469f0aa214Scgd break;
1479f0aa214Scgd default:
1489f0aa214Scgd goto einval;
1499f0aa214Scgd }
1509f0aa214Scgd
1519f0aa214Scgd if (fstat(rfd, &sb))
1529f0aa214Scgd goto err;
1539f0aa214Scgd /*
1549f0aa214Scgd * Kluge -- we'd like to test to see if the file is too
1559f0aa214Scgd * big to mmap. Since, we don't know what size or type
1569f0aa214Scgd * off_t's or size_t's are, what the largest unsigned
1579f0aa214Scgd * integral type is, or what random insanity the local
1589f0aa214Scgd * C compiler will perpetrate, doing the comparison in
1599f0aa214Scgd * a portable way is flatly impossible. Hope that mmap
1609f0aa214Scgd * fails if the file is too large.
1619f0aa214Scgd */
1629f0aa214Scgd if (sb.st_size == 0)
163738330daScgd F_SET(t, R_EOF);
1649f0aa214Scgd else {
165738330daScgd #ifdef MMAP_NOT_AVAILABLE
166738330daScgd /*
167738330daScgd * XXX
168738330daScgd * Mmap doesn't work correctly on many current
169738330daScgd * systems. In particular, it can fail subtly,
170738330daScgd * with cache coherency problems. Don't use it
171738330daScgd * for now.
172738330daScgd */
1739f0aa214Scgd t->bt_msize = sb.st_size;
17445e27c80Scgd if ((t->bt_smap = mmap(NULL, t->bt_msize,
1754462053aSmycroft PROT_READ, MAP_FILE | MAP_PRIVATE, rfd,
1769f0aa214Scgd (off_t)0)) == (caddr_t)-1)
1779f0aa214Scgd goto slow;
1789f0aa214Scgd t->bt_cmap = t->bt_smap;
1799f0aa214Scgd t->bt_emap = t->bt_smap + sb.st_size;
180738330daScgd t->bt_irec = F_ISSET(t, R_FIXLEN) ?
1819f0aa214Scgd __rec_fmap : __rec_vmap;
182738330daScgd F_SET(t, R_MEMMAPPED);
183738330daScgd #else
184738330daScgd goto slow;
185738330daScgd #endif
1869f0aa214Scgd }
1879f0aa214Scgd }
1889f0aa214Scgd }
1899f0aa214Scgd
1909f0aa214Scgd /* Use the recno routines. */
1919f0aa214Scgd dbp->close = __rec_close;
1929f0aa214Scgd dbp->del = __rec_delete;
1939f0aa214Scgd dbp->fd = __rec_fd;
1949f0aa214Scgd dbp->get = __rec_get;
1959f0aa214Scgd dbp->put = __rec_put;
1969f0aa214Scgd dbp->seq = __rec_seq;
1979f0aa214Scgd dbp->sync = __rec_sync;
1989f0aa214Scgd
1999f0aa214Scgd /* If the root page was created, reset the flags. */
200*aae80e6bSchristos if ((h = mpool_get(t->bt_mp, P_ROOT, 0)) == NULL)
2019f0aa214Scgd goto err;
2029f0aa214Scgd if ((h->flags & P_TYPE) == P_BLEAF) {
203738330daScgd F_CLR(h, P_TYPE);
204738330daScgd F_SET(h, P_RLEAF);
2059f0aa214Scgd mpool_put(t->bt_mp, h, MPOOL_DIRTY);
2069f0aa214Scgd } else
2079f0aa214Scgd mpool_put(t->bt_mp, h, 0);
2089f0aa214Scgd
2099f0aa214Scgd if (openinfo && openinfo->flags & R_SNAPSHOT &&
210738330daScgd !F_ISSET(t, R_EOF | R_INMEM) &&
2119f0aa214Scgd t->bt_irec(t, MAX_REC_NUMBER) == RET_ERROR)
2129f0aa214Scgd goto err;
2139f0aa214Scgd return (dbp);
2149f0aa214Scgd
2159f0aa214Scgd einval: errno = EINVAL;
2169f0aa214Scgd err: sverrno = errno;
2179f0aa214Scgd if (dbp != NULL)
2189f0aa214Scgd (void)__bt_close(dbp);
2199f0aa214Scgd if (fname != NULL)
2209f0aa214Scgd (void)close(rfd);
2219f0aa214Scgd errno = sverrno;
2229f0aa214Scgd return (NULL);
2239f0aa214Scgd }
2249f0aa214Scgd
2259f0aa214Scgd int
__rec_fd(const DB * dbp)226cb9daf8fSchristos __rec_fd(const DB *dbp)
2279f0aa214Scgd {
2289f0aa214Scgd BTREE *t;
2299f0aa214Scgd
2309f0aa214Scgd t = dbp->internal;
2319f0aa214Scgd
23245e27c80Scgd /* Toss any page pinned across calls. */
23345e27c80Scgd if (t->bt_pinned != NULL) {
23445e27c80Scgd mpool_put(t->bt_mp, t->bt_pinned, 0);
23545e27c80Scgd t->bt_pinned = NULL;
23645e27c80Scgd }
23745e27c80Scgd
23845e27c80Scgd /* In-memory database can't have a file descriptor. */
239738330daScgd if (F_ISSET(t, R_INMEM)) {
2409f0aa214Scgd errno = ENOENT;
2419f0aa214Scgd return (-1);
2429f0aa214Scgd }
2439f0aa214Scgd return (t->bt_rfd);
2449f0aa214Scgd }
245