xref: /csrg-svn/lib/libc/db/recno/rec_open.c (revision 56757)
150995Sbostic /*-
250995Sbostic  * Copyright (c) 1990 The Regents of the University of California.
350995Sbostic  * All rights reserved.
450995Sbostic  *
550995Sbostic  * This code is derived from software contributed to Berkeley by
650995Sbostic  * Mike Olson.
750995Sbostic  *
850995Sbostic  * %sccs.include.redist.c%
950995Sbostic  */
1050995Sbostic 
1150995Sbostic #if defined(LIBC_SCCS) && !defined(lint)
12*56757Sbostic static char sccsid[] = "@(#)rec_open.c	5.10 (Berkeley) 11/13/92";
1350995Sbostic #endif /* LIBC_SCCS and not lint */
1450995Sbostic 
1550995Sbostic #include <sys/types.h>
1650995Sbostic #include <sys/mman.h>
1750995Sbostic #include <sys/stat.h>
1854362Sbostic 
19*56757Sbostic #include <db.h>
20*56757Sbostic #include <errno.h>
2150995Sbostic #include <fcntl.h>
2250995Sbostic #include <limits.h>
23*56757Sbostic #include <stddef.h>
24*56757Sbostic #include <stdio.h>
2550995Sbostic #include <unistd.h>
26*56757Sbostic 
2751090Sbostic #include "recno.h"
2850995Sbostic 
2950995Sbostic DB *
3050995Sbostic __rec_open(fname, flags, mode, openinfo)
3150995Sbostic 	const char *fname;
3250995Sbostic 	int flags, mode;
3350995Sbostic 	const RECNOINFO *openinfo;
3450995Sbostic {
3550995Sbostic 	BTREE *t;
3650995Sbostic 	BTREEINFO btopeninfo;
3750995Sbostic 	DB *dbp;
3850995Sbostic 	PAGE *h;
3950995Sbostic 	struct stat sb;
4050995Sbostic 	int rfd;
4150995Sbostic 
4250995Sbostic 	/* Open the user's file -- if this fails, we're done. */
43*56757Sbostic 	if (fname != NULL && (rfd = open(fname, flags, mode)) < 0)
4450995Sbostic 		return (NULL);
4550995Sbostic 
4650995Sbostic 	/* Create a btree in memory (backed by disk). */
47*56757Sbostic 	dbp = NULL;
4850995Sbostic 	if (openinfo) {
49*56757Sbostic 		if (openinfo->flags & ~(R_FIXEDLEN | R_NOKEY | R_SNAPSHOT))
5056703Sbostic 			goto einval;
5150995Sbostic 		btopeninfo.flags = 0;
5250995Sbostic 		btopeninfo.cachesize = openinfo->cachesize;
5350995Sbostic 		btopeninfo.psize = 0;
5450995Sbostic 		btopeninfo.compare = NULL;
5550995Sbostic 		btopeninfo.lorder = openinfo->lorder;
5650995Sbostic 		dbp = __bt_open(NULL, O_RDWR, S_IRUSR | S_IWUSR, &btopeninfo);
5750995Sbostic 	} else
5850995Sbostic 		dbp = __bt_open(NULL, O_RDWR, S_IRUSR | S_IWUSR, NULL);
5951090Sbostic 	if (dbp == NULL)
6051090Sbostic 		goto err;
6150995Sbostic 
6250995Sbostic 	/*
6350995Sbostic 	 * Some fields in the tree structure are recno specific.  Fill them
6451090Sbostic 	 * in and make the btree structure look like a recno structure.  We
6551090Sbostic 	 * don't change the bt_ovflsize value, it's close enough and slightly
6651090Sbostic 	 * bigger.
6750995Sbostic 	 */
6850995Sbostic 	t = dbp->internal;
6950995Sbostic 	if (openinfo) {
7055295Sbostic 		if (openinfo->flags & R_FIXEDLEN) {
71*56757Sbostic 			SET(t, BTF_FIXEDLEN);
7255295Sbostic 			t->bt_reclen = openinfo->reclen;
7356703Sbostic 			if (t->bt_reclen == 0)
7456703Sbostic 				goto einval;
7550995Sbostic 		}
7650995Sbostic 		t->bt_bval = openinfo->bval;
7750995Sbostic 	} else
7850995Sbostic 		t->bt_bval = '\n';
7950995Sbostic 
80*56757Sbostic 	SET(t, BTF_RECNO);
81*56757Sbostic 	if (fname == NULL) {
82*56757Sbostic 		SET(t, BTF_RINMEM);
83*56757Sbostic 		t->bt_reof = 1;
84*56757Sbostic 	} else
85*56757Sbostic 		t->bt_reof = 0;
8650995Sbostic 
8750995Sbostic 	/*
8854286Sbostic 	 * In 4.4BSD stat(2) returns true for ISSOCK on pipes.  Until
8954286Sbostic 	 * then, this is fairly close.  Pipes are read-only.
9054286Sbostic 	 */
91*56757Sbostic 	if (fname != NULL)
92*56757Sbostic 		if (lseek(rfd, (off_t)0, SEEK_CUR) == -1 && errno == ESPIPE) {
9350995Sbostic 			SET(t, BTF_RDONLY);
94*56757Sbostic 			if ((t->bt_rfp = fdopen(rfd, "r")) == NULL)
95*56757Sbostic 				goto err;
96*56757Sbostic 			t->bt_irec =
97*56757Sbostic 			    ISSET(t, BTF_FIXEDLEN) ? __rec_fpipe : __rec_vpipe;
98*56757Sbostic 		} else {
99*56757Sbostic 			if (fstat(rfd, &sb))
100*56757Sbostic 				goto err;
101*56757Sbostic 			switch(flags & O_ACCMODE) {
102*56757Sbostic 			case O_RDONLY:
103*56757Sbostic 				SET(t, BTF_RDONLY);
104*56757Sbostic 				break;
105*56757Sbostic 			case O_RDWR:
106*56757Sbostic 				break;
107*56757Sbostic 			case O_WRONLY:
108*56757Sbostic 			default:
109*56757Sbostic 				goto einval;
110*56757Sbostic 			}
111*56757Sbostic 
112*56757Sbostic 			if (sb.st_size > SIZE_T_MAX) {
113*56757Sbostic 				errno = EFBIG;
114*56757Sbostic 				goto err;
115*56757Sbostic 			}
116*56757Sbostic 			if ((t->bt_smap = mmap(NULL, (size_t)sb.st_size,
117*56757Sbostic 			    PROT_READ, 0, rfd, (off_t)0)) == (caddr_t)-1)
118*56757Sbostic 				goto err;
119*56757Sbostic 			t->bt_emap = t->bt_smap + sb.st_size;
120*56757Sbostic 			t->bt_rfd = rfd;
121*56757Sbostic 			t->bt_rfp = NULL;
122*56757Sbostic 			t->bt_irec =
123*56757Sbostic 			    ISSET(t, BTF_FIXEDLEN) ? __rec_fmap : __rec_vmap;
12456703Sbostic 		}
12550995Sbostic 
12650995Sbostic 	/* Use the recno routines. */
12750995Sbostic 	dbp->close = __rec_close;
12850995Sbostic 	dbp->del = __rec_delete;
12950995Sbostic 	dbp->get = __rec_get;
13050995Sbostic 	dbp->put = __rec_put;
13150995Sbostic 	dbp->seq = __rec_seq;
13250995Sbostic 	dbp->sync = __rec_sync;
13350995Sbostic 
13450995Sbostic 	/* If the root page was created, reset the flags. */
13550995Sbostic 	if ((h = mpool_get(t->bt_mp, P_ROOT, 0)) == NULL)
13650995Sbostic 		goto err;
13750995Sbostic 	if ((h->flags & P_TYPE) == P_BLEAF) {
13850995Sbostic 		h->flags = h->flags & ~P_TYPE | P_RLEAF;
13950995Sbostic 		mpool_put(t->bt_mp, h, MPOOL_DIRTY);
14050995Sbostic 	} else
14150995Sbostic 		mpool_put(t->bt_mp, h, 0);
14250995Sbostic 
14350995Sbostic 	if (openinfo && openinfo->flags & R_SNAPSHOT &&
144*56757Sbostic 	    !ISSET(t, BTF_RINMEM) &&
14550995Sbostic 	    t->bt_irec(t, MAX_REC_NUMBER) == RET_ERROR)
14650995Sbostic                 goto err;
14750995Sbostic 	return (dbp);
14850995Sbostic 
14956703Sbostic einval:	errno = EINVAL;
150*56757Sbostic err:	if (dbp != NULL)
15151090Sbostic 		__bt_close(dbp);
152*56757Sbostic 	if (fname != NULL)
153*56757Sbostic 		(void)close(rfd);
15450995Sbostic 	return (NULL);
15550995Sbostic }
156