Home | History | Annotate | Line # | Download | only in recno
rec_open.c revision 1.1
      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 static char sccsid[] = "@(#)rec_open.c	8.1 (Berkeley) 6/4/93";
     39 #endif /* LIBC_SCCS and not lint */
     40 
     41 #include <sys/types.h>
     42 #include <sys/mman.h>
     43 #include <sys/stat.h>
     44 
     45 #include <errno.h>
     46 #include <fcntl.h>
     47 #include <limits.h>
     48 #include <stddef.h>
     49 #include <stdio.h>
     50 #include <unistd.h>
     51 
     52 #include <db.h>
     53 #include "recno.h"
     54 
     55 DB *
     56 __rec_open(fname, flags, mode, openinfo)
     57 	const char *fname;
     58 	int flags, mode;
     59 	const RECNOINFO *openinfo;
     60 {
     61 	BTREE *t;
     62 	BTREEINFO btopeninfo;
     63 	DB *dbp;
     64 	PAGE *h;
     65 	struct stat sb;
     66 	int rfd, sverrno;
     67 
     68 	/* Open the user's file -- if this fails, we're done. */
     69 	if (fname != NULL && (rfd = open(fname, flags, mode)) < 0)
     70 		return (NULL);
     71 
     72 	/* Create a btree in memory (backed by disk). */
     73 	dbp = NULL;
     74 	if (openinfo) {
     75 		if (openinfo->flags & ~(R_FIXEDLEN | R_NOKEY | R_SNAPSHOT))
     76 			goto einval;
     77 		btopeninfo.flags = 0;
     78 		btopeninfo.cachesize = openinfo->cachesize;
     79 		btopeninfo.maxkeypage = 0;
     80 		btopeninfo.minkeypage = 0;
     81 		btopeninfo.psize = openinfo->psize;
     82 		btopeninfo.compare = NULL;
     83 		btopeninfo.prefix = NULL;
     84 		btopeninfo.lorder = openinfo->lorder;
     85 		dbp = __bt_open(openinfo->bfname,
     86 		    O_RDWR, S_IRUSR | S_IWUSR, &btopeninfo);
     87 	} else
     88 		dbp = __bt_open(NULL, O_RDWR, S_IRUSR | S_IWUSR, NULL);
     89 	if (dbp == NULL)
     90 		goto err;
     91 
     92 	/*
     93 	 * Some fields in the tree structure are recno specific.  Fill them
     94 	 * in and make the btree structure look like a recno structure.  We
     95 	 * don't change the bt_ovflsize value, it's close enough and slightly
     96 	 * bigger.
     97 	 */
     98 	t = dbp->internal;
     99 	if (openinfo) {
    100 		if (openinfo->flags & R_FIXEDLEN) {
    101 			SET(t, R_FIXLEN);
    102 			t->bt_reclen = openinfo->reclen;
    103 			if (t->bt_reclen == 0)
    104 				goto einval;
    105 		}
    106 		t->bt_bval = openinfo->bval;
    107 	} else
    108 		t->bt_bval = '\n';
    109 
    110 	SET(t, R_RECNO);
    111 	if (fname == NULL)
    112 		SET(t, R_EOF | R_INMEM);
    113 	else
    114 		t->bt_rfd = rfd;
    115 	t->bt_rcursor = 0;
    116 
    117 	/*
    118 	 * In 4.4BSD stat(2) returns true for ISSOCK on pipes.  Until
    119 	 * then, this is fairly close.  Pipes are read-only.
    120 	 */
    121 	if (fname != NULL) {
    122 		if (lseek(rfd, (off_t)0, SEEK_CUR) == -1 && errno == ESPIPE) {
    123 			switch (flags & O_ACCMODE) {
    124 			case O_RDONLY:
    125 				SET(t, R_RDONLY);
    126 				break;
    127 			default:
    128 				goto einval;
    129 			}
    130 slow:			if ((t->bt_rfp = fdopen(rfd, "r")) == NULL)
    131 				goto err;
    132 			SET(t, R_CLOSEFP);
    133 			t->bt_irec =
    134 			    ISSET(t, R_FIXLEN) ? __rec_fpipe : __rec_vpipe;
    135 		} else {
    136 			switch (flags & O_ACCMODE) {
    137 			case O_RDONLY:
    138 				SET(t, R_RDONLY);
    139 				break;
    140 			case O_RDWR:
    141 				break;
    142 			default:
    143 				goto einval;
    144 			}
    145 
    146 			if (fstat(rfd, &sb))
    147 				goto err;
    148 			/*
    149 			 * Kluge -- we'd like to test to see if the file is too
    150 			 * big to mmap.  Since, we don't know what size or type
    151 			 * off_t's or size_t's are, what the largest unsigned
    152 			 * integral type is, or what random insanity the local
    153 			 * C compiler will perpetrate, doing the comparison in
    154 			 * a portable way is flatly impossible.  Hope that mmap
    155 			 * fails if the file is too large.
    156 			 */
    157 			if (sb.st_size == 0)
    158 				SET(t, R_EOF);
    159 			else {
    160 				t->bt_msize = sb.st_size;
    161 				if ((t->bt_smap =
    162 				    mmap(NULL, t->bt_msize, PROT_READ, 0, rfd,
    163 				    (off_t)0)) == (caddr_t)-1)
    164 					goto slow;
    165 				t->bt_cmap = t->bt_smap;
    166 				t->bt_emap = t->bt_smap + sb.st_size;
    167 				t->bt_irec = ISSET(t, R_FIXLEN) ?
    168 				    __rec_fmap : __rec_vmap;
    169 				SET(t, R_MEMMAPPED);
    170 			}
    171 		}
    172 	}
    173 
    174 	/* Use the recno routines. */
    175 	dbp->close = __rec_close;
    176 	dbp->del = __rec_delete;
    177 	dbp->fd = __rec_fd;
    178 	dbp->get = __rec_get;
    179 	dbp->put = __rec_put;
    180 	dbp->seq = __rec_seq;
    181 	dbp->sync = __rec_sync;
    182 
    183 	/* If the root page was created, reset the flags. */
    184 	if ((h = mpool_get(t->bt_mp, P_ROOT, 0)) == NULL)
    185 		goto err;
    186 	if ((h->flags & P_TYPE) == P_BLEAF) {
    187 		h->flags = h->flags & ~P_TYPE | P_RLEAF;
    188 		mpool_put(t->bt_mp, h, MPOOL_DIRTY);
    189 	} else
    190 		mpool_put(t->bt_mp, h, 0);
    191 
    192 	if (openinfo && openinfo->flags & R_SNAPSHOT &&
    193 	    !ISSET(t, R_EOF | R_INMEM) &&
    194 	    t->bt_irec(t, MAX_REC_NUMBER) == RET_ERROR)
    195                 goto err;
    196 	return (dbp);
    197 
    198 einval:	errno = EINVAL;
    199 err:	sverrno = errno;
    200 	if (dbp != NULL)
    201 		(void)__bt_close(dbp);
    202 	if (fname != NULL)
    203 		(void)close(rfd);
    204 	errno = sverrno;
    205 	return (NULL);
    206 }
    207 
    208 int
    209 __rec_fd(dbp)
    210 	const DB *dbp;
    211 {
    212 	BTREE *t;
    213 
    214 	t = dbp->internal;
    215 
    216 	if (ISSET(t, R_INMEM)) {
    217 		errno = ENOENT;
    218 		return (-1);
    219 	}
    220 	return (t->bt_rfd);
    221 }
    222