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