xref: /csrg-svn/lib/libc/db/recno/rec_open.c (revision 56703)
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*56703Sbostic static char sccsid[] = "@(#)rec_open.c	5.9 (Berkeley) 11/07/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 
1950995Sbostic #include <fcntl.h>
2050995Sbostic #include <errno.h>
2150995Sbostic #include <limits.h>
2250995Sbostic #include <db.h>
2350995Sbostic #include <unistd.h>
2450995Sbostic #include <stdio.h>
2550995Sbostic #include <stddef.h>
2651090Sbostic #include "recno.h"
2750995Sbostic 
2850995Sbostic DB *
2950995Sbostic __rec_open(fname, flags, mode, openinfo)
3050995Sbostic 	const char *fname;
3150995Sbostic 	int flags, mode;
3250995Sbostic 	const RECNOINFO *openinfo;
3350995Sbostic {
3450995Sbostic 	BTREE *t;
3550995Sbostic 	BTREEINFO btopeninfo;
3650995Sbostic 	DB *dbp;
3750995Sbostic 	PAGE *h;
3850995Sbostic 	struct stat sb;
3950995Sbostic 	int rfd;
4050995Sbostic 
4150995Sbostic 	/* Open the user's file -- if this fails, we're done. */
4250995Sbostic 	if ((rfd = open(fname, flags, mode)) < 0)
4350995Sbostic 		return (NULL);
4450995Sbostic 
4550995Sbostic 	/* Create a btree in memory (backed by disk). */
4650995Sbostic 	if (openinfo) {
47*56703Sbostic 		if (openinfo->flags & ~(R_FIXEDLEN|R_NOKEY|R_SNAPSHOT))
48*56703Sbostic 			goto einval;
4950995Sbostic 		btopeninfo.flags = 0;
5050995Sbostic 		btopeninfo.cachesize = openinfo->cachesize;
5150995Sbostic 		btopeninfo.psize = 0;
5250995Sbostic 		btopeninfo.compare = NULL;
5350995Sbostic 		btopeninfo.lorder = openinfo->lorder;
5450995Sbostic 		dbp = __bt_open(NULL, O_RDWR, S_IRUSR | S_IWUSR, &btopeninfo);
5550995Sbostic 	} else
5650995Sbostic 		dbp = __bt_open(NULL, O_RDWR, S_IRUSR | S_IWUSR, NULL);
5751090Sbostic 	if (dbp == NULL)
5851090Sbostic 		goto err;
5950995Sbostic 
6050995Sbostic 	/*
6150995Sbostic 	 * Some fields in the tree structure are recno specific.  Fill them
6251090Sbostic 	 * in and make the btree structure look like a recno structure.  We
6351090Sbostic 	 * don't change the bt_ovflsize value, it's close enough and slightly
6451090Sbostic 	 * bigger.
6550995Sbostic 	 */
6650995Sbostic 	t = dbp->internal;
6750995Sbostic 	if (openinfo) {
6855295Sbostic 		if (openinfo->flags & R_FIXEDLEN) {
6950995Sbostic 			t->bt_flags |= BTF_FIXEDLEN;
7055295Sbostic 			t->bt_reclen = openinfo->reclen;
71*56703Sbostic 			if (t->bt_reclen == 0)
72*56703Sbostic 				goto einval;
7350995Sbostic 		}
7450995Sbostic 		t->bt_bval = openinfo->bval;
7550995Sbostic 	} else
7650995Sbostic 		t->bt_bval = '\n';
7750995Sbostic 
7850995Sbostic 	t->bt_flags = BTF_RECNO;
7954286Sbostic 	t->bt_reof = 0;
8050995Sbostic 
8150995Sbostic 	/*
8254286Sbostic 	 * In 4.4BSD stat(2) returns true for ISSOCK on pipes.  Until
8354286Sbostic 	 * then, this is fairly close.  Pipes are read-only.
8454286Sbostic 	 */
8555311Sbostic 	if (lseek(rfd, (off_t)0, SEEK_CUR) == -1 && errno == ESPIPE) {
8650995Sbostic 		SET(t, BTF_RDONLY);
8750995Sbostic 		if ((t->bt_rfp = fdopen(rfd, "r")) == NULL)
8850995Sbostic 			goto err;
8950995Sbostic 		t->bt_irec = ISSET(t, BTF_FIXEDLEN) ? __rec_fpipe : __rec_vpipe;
9050995Sbostic 	} else {
9150995Sbostic 		if (fstat(rfd, &sb))
9250995Sbostic 			goto err;
93*56703Sbostic 		switch(flags & O_ACCMODE) {
94*56703Sbostic 		case O_RDONLY:
9550995Sbostic 			SET(t, BTF_RDONLY);
96*56703Sbostic 			break;
97*56703Sbostic 		case O_RDWR:
98*56703Sbostic 			break;
99*56703Sbostic 		case O_WRONLY:
100*56703Sbostic 		default:
101*56703Sbostic 			goto einval;
102*56703Sbostic 		}
103*56703Sbostic 
10454362Sbostic 		if (sb.st_size > SIZE_T_MAX) {
10554286Sbostic 			errno = EFBIG;
10650995Sbostic 			goto err;
10754286Sbostic 		}
10855295Sbostic 		if ((t->bt_smap = mmap(NULL, (size_t)sb.st_size,
10955295Sbostic 		    PROT_READ, 0, rfd, (off_t)0)) == (caddr_t)-1)
11054286Sbostic 			goto err;
11150995Sbostic 		t->bt_emap = t->bt_smap + sb.st_size;
11250995Sbostic 		t->bt_rfd = rfd;
11354286Sbostic 		t->bt_rfp = NULL;
11450995Sbostic 		t->bt_irec = ISSET(t, BTF_FIXEDLEN) ? __rec_fmap : __rec_vmap;
11550995Sbostic 	}
11650995Sbostic 
11750995Sbostic 	/* Use the recno routines. */
11850995Sbostic 	dbp->close = __rec_close;
11950995Sbostic 	dbp->del = __rec_delete;
12050995Sbostic 	dbp->get = __rec_get;
12150995Sbostic 	dbp->put = __rec_put;
12250995Sbostic 	dbp->seq = __rec_seq;
12350995Sbostic 	dbp->sync = __rec_sync;
12450995Sbostic 
12550995Sbostic 	/* If the root page was created, reset the flags. */
12650995Sbostic 	if ((h = mpool_get(t->bt_mp, P_ROOT, 0)) == NULL)
12750995Sbostic 		goto err;
12850995Sbostic 	if ((h->flags & P_TYPE) == P_BLEAF) {
12950995Sbostic 		h->flags = h->flags & ~P_TYPE | P_RLEAF;
13050995Sbostic 		mpool_put(t->bt_mp, h, MPOOL_DIRTY);
13150995Sbostic 	} else
13250995Sbostic 		mpool_put(t->bt_mp, h, 0);
13350995Sbostic 
13450995Sbostic 	if (openinfo && openinfo->flags & R_SNAPSHOT &&
13550995Sbostic 	    t->bt_irec(t, MAX_REC_NUMBER) == RET_ERROR)
13650995Sbostic                 goto err;
13750995Sbostic 	return (dbp);
13850995Sbostic 
139*56703Sbostic einval:	errno = EINVAL;
14051090Sbostic err:	if (dbp)
14151090Sbostic 		__bt_close(dbp);
14250995Sbostic 	(void)close(rfd);
14350995Sbostic 	return (NULL);
14450995Sbostic }
145