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