/*-1* SPDX-License-Identifier: BSD-3-Clause2*3* Copyright (c) 1990, 1993, 19944* The Regents of the University of California. All rights reserved.5*6* This code is derived from software contributed to Berkeley by7* Mike Olson.8*9* Redistribution and use in source and binary forms, with or without10* modification, are permitted provided that the following conditions11* are met:12* 1. Redistributions of source code must retain the above copyright13* notice, this list of conditions and the following disclaimer.14* 2. Redistributions in binary form must reproduce the above copyright15* notice, this list of conditions and the following disclaimer in the16* documentation and/or other materials provided with the distribution.17* 3. Neither the name of the University nor the names of its contributors18* may be used to endorse or promote products derived from this software19* without specific prior written permission.20*21* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND22* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE23* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE24* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE25* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL26* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS27* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)28* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT29* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY30* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF31* SUCH DAMAGE.32*/3334#include "namespace.h"35#include <sys/types.h>36#include <sys/mman.h>37#include <sys/stat.h>3839#include <errno.h>40#include <fcntl.h>41#include <limits.h>42#include <stddef.h>43#include <stdio.h>44#include <unistd.h>45#include "un-namespace.h"4647#include <db.h>48#include "recno.h"4950DB *51__rec_open(const char *fname, int flags, int mode, const RECNOINFO *openinfo,52int dflags)53{54BTREE *t;55BTREEINFO btopeninfo;56DB *dbp;57PAGE *h;58struct stat sb;59int rfd, sverrno;6061/* Open the user's file -- if this fails, we're done. */62if (fname != NULL && (rfd = _open(fname, flags | O_CLOEXEC, mode)) < 0)63return (NULL);6465/* Create a btree in memory (backed by disk). */66dbp = NULL;67if (openinfo) {68if (openinfo->flags & ~(R_FIXEDLEN | R_NOKEY | R_SNAPSHOT))69goto einval;70btopeninfo.flags = 0;71btopeninfo.cachesize = openinfo->cachesize;72btopeninfo.maxkeypage = 0;73btopeninfo.minkeypage = 0;74btopeninfo.psize = openinfo->psize;75btopeninfo.compare = NULL;76btopeninfo.prefix = NULL;77btopeninfo.lorder = openinfo->lorder;78dbp = __bt_open(openinfo->bfname,79O_RDWR, S_IRUSR | S_IWUSR, &btopeninfo, dflags);80} else81dbp = __bt_open(NULL, O_RDWR, S_IRUSR | S_IWUSR, NULL, dflags);82if (dbp == NULL)83goto err;8485/*86* Some fields in the tree structure are recno specific. Fill them87* in and make the btree structure look like a recno structure. We88* don't change the bt_ovflsize value, it's close enough and slightly89* bigger.90*/91t = dbp->internal;92if (openinfo) {93if (openinfo->flags & R_FIXEDLEN) {94F_SET(t, R_FIXLEN);95t->bt_reclen = openinfo->reclen;96if (t->bt_reclen == 0)97goto einval;98}99t->bt_bval = openinfo->bval;100} else101t->bt_bval = '\n';102103F_SET(t, R_RECNO);104if (fname == NULL)105F_SET(t, R_EOF | R_INMEM);106else107t->bt_rfd = rfd;108109if (fname != NULL) {110/*111* In 4.4BSD, stat(2) returns true for ISSOCK on pipes.112* Unfortunately, that's not portable, so we use lseek113* and check the errno values.114*/115errno = 0;116if (lseek(rfd, (off_t)0, SEEK_CUR) == -1 && errno == ESPIPE) {117switch (flags & O_ACCMODE) {118case O_RDONLY:119F_SET(t, R_RDONLY);120break;121default:122goto einval;123}124slow: if ((t->bt_rfp = fdopen(rfd, "r")) == NULL)125goto err;126F_SET(t, R_CLOSEFP);127t->bt_irec =128F_ISSET(t, R_FIXLEN) ? __rec_fpipe : __rec_vpipe;129} else {130switch (flags & O_ACCMODE) {131case O_RDONLY:132F_SET(t, R_RDONLY);133break;134case O_RDWR:135break;136default:137goto einval;138}139140if (_fstat(rfd, &sb))141goto err;142/*143* Kluge -- we'd like to test to see if the file is too144* big to mmap. Since, we don't know what size or type145* off_t's or size_t's are, what the largest unsigned146* integral type is, or what random insanity the local147* C compiler will perpetrate, doing the comparison in148* a portable way is flatly impossible. Hope that mmap149* fails if the file is too large.150*/151if (sb.st_size == 0)152F_SET(t, R_EOF);153else {154#ifdef MMAP_NOT_AVAILABLE155/*156* XXX157* Mmap doesn't work correctly on many current158* systems. In particular, it can fail subtly,159* with cache coherency problems. Don't use it160* for now.161*/162t->bt_msize = sb.st_size;163if ((t->bt_smap = mmap(NULL, t->bt_msize,164PROT_READ, MAP_PRIVATE, rfd,165(off_t)0)) == MAP_FAILED)166goto slow;167t->bt_cmap = t->bt_smap;168t->bt_emap = t->bt_smap + sb.st_size;169t->bt_irec = F_ISSET(t, R_FIXLEN) ?170__rec_fmap : __rec_vmap;171F_SET(t, R_MEMMAPPED);172#else173goto slow;174#endif175}176}177}178179/* Use the recno routines. */180dbp->close = __rec_close;181dbp->del = __rec_delete;182dbp->fd = __rec_fd;183dbp->get = __rec_get;184dbp->put = __rec_put;185dbp->seq = __rec_seq;186dbp->sync = __rec_sync;187188/* If the root page was created, reset the flags. */189if ((h = mpool_get(t->bt_mp, P_ROOT, 0)) == NULL)190goto err;191if ((h->flags & P_TYPE) == P_BLEAF) {192F_CLR(h, P_TYPE);193F_SET(h, P_RLEAF);194mpool_put(t->bt_mp, h, MPOOL_DIRTY);195} else196mpool_put(t->bt_mp, h, 0);197198if (openinfo && openinfo->flags & R_SNAPSHOT &&199!F_ISSET(t, R_EOF | R_INMEM) &&200t->bt_irec(t, MAX_REC_NUMBER) == RET_ERROR)201goto err;202return (dbp);203204einval: errno = EINVAL;205err: sverrno = errno;206if (dbp != NULL)207(void)__bt_close(dbp);208if (fname != NULL)209(void)_close(rfd);210errno = sverrno;211return (NULL);212}213214int215__rec_fd(const DB *dbp)216{217BTREE *t;218219t = dbp->internal;220221/* Toss any page pinned across calls. */222if (t->bt_pinned != NULL) {223mpool_put(t->bt_mp, t->bt_pinned, 0);224t->bt_pinned = NULL;225}226227/* In-memory database can't have a file descriptor. */228if (F_ISSET(t, R_INMEM)) {229errno = ENOENT;230return (-1);231}232return (t->bt_rfd);233}234235236