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