xref: /csrg-svn/lib/libc/db/recno/rec_open.c (revision 58311)
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*58311Sbostic static char sccsid[] = "@(#)rec_open.c	5.14 (Berkeley) 02/28/93";
1350995Sbostic #endif /* LIBC_SCCS and not lint */
1450995Sbostic 
1550995Sbostic #include <sys/types.h>
1650995Sbostic #include <sys/mman.h>
1750995Sbostic #include <sys/stat.h>
1854362Sbostic 
1956757Sbostic #include <errno.h>
2050995Sbostic #include <fcntl.h>
2150995Sbostic #include <limits.h>
2256757Sbostic #include <stddef.h>
2356757Sbostic #include <stdio.h>
2450995Sbostic #include <unistd.h>
2556757Sbostic 
2657933Sbostic #include <db.h>
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. */
4356757Sbostic 	if (fname != NULL && (rfd = open(fname, flags, mode)) < 0)
4450995Sbostic 		return (NULL);
4550995Sbostic 
4650995Sbostic 	/* Create a btree in memory (backed by disk). */
4756757Sbostic 	dbp = NULL;
4850995Sbostic 	if (openinfo) {
4956757Sbostic 		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) {
7156757Sbostic 			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 
8056757Sbostic 	SET(t, BTF_RECNO);
8156757Sbostic 	if (fname == NULL) {
8256757Sbostic 		SET(t, BTF_RINMEM);
8356757Sbostic 		t->bt_reof = 1;
8456757Sbostic 	} else
8556757Sbostic 		t->bt_reof = 0;
8657007Sbostic 	t->bt_rcursor = 0;
8750995Sbostic 
8850995Sbostic 	/*
8954286Sbostic 	 * In 4.4BSD stat(2) returns true for ISSOCK on pipes.  Until
9054286Sbostic 	 * then, this is fairly close.  Pipes are read-only.
9154286Sbostic 	 */
9256757Sbostic 	if (fname != NULL)
9356757Sbostic 		if (lseek(rfd, (off_t)0, SEEK_CUR) == -1 && errno == ESPIPE) {
9450995Sbostic 			SET(t, BTF_RDONLY);
9557227Sbostic slow:			if ((t->bt_rfp = fdopen(rfd, "r")) == NULL)
9656757Sbostic 				goto err;
9756757Sbostic 			t->bt_irec =
9856757Sbostic 			    ISSET(t, BTF_FIXEDLEN) ? __rec_fpipe : __rec_vpipe;
9956757Sbostic 		} else {
10056757Sbostic 			if (fstat(rfd, &sb))
10156757Sbostic 				goto err;
10256757Sbostic 			switch(flags & O_ACCMODE) {
10356757Sbostic 			case O_RDONLY:
10456757Sbostic 				SET(t, BTF_RDONLY);
10556757Sbostic 				break;
10656757Sbostic 			case O_RDWR:
10756757Sbostic 				break;
10856757Sbostic 			case O_WRONLY:
10956757Sbostic 			default:
11056757Sbostic 				goto einval;
11156757Sbostic 			}
11256757Sbostic 
113*58311Sbostic 			if (sb.st_size > (off_t)SIZE_T_MAX) {
11456757Sbostic 				errno = EFBIG;
11556757Sbostic 				goto err;
11656757Sbostic 			}
11756757Sbostic 			if ((t->bt_smap = mmap(NULL, (size_t)sb.st_size,
11856757Sbostic 			    PROT_READ, 0, rfd, (off_t)0)) == (caddr_t)-1)
11957227Sbostic 				goto slow;
12056757Sbostic 			t->bt_emap = t->bt_smap + sb.st_size;
12156757Sbostic 			t->bt_rfd = rfd;
12256757Sbostic 			t->bt_rfp = NULL;
12356757Sbostic 			t->bt_irec =
12456757Sbostic 			    ISSET(t, BTF_FIXEDLEN) ? __rec_fmap : __rec_vmap;
12556703Sbostic 		}
12650995Sbostic 
12750995Sbostic 	/* Use the recno routines. */
12850995Sbostic 	dbp->close = __rec_close;
12950995Sbostic 	dbp->del = __rec_delete;
13050995Sbostic 	dbp->get = __rec_get;
13150995Sbostic 	dbp->put = __rec_put;
13250995Sbostic 	dbp->seq = __rec_seq;
13350995Sbostic 	dbp->sync = __rec_sync;
13450995Sbostic 
13550995Sbostic 	/* If the root page was created, reset the flags. */
13650995Sbostic 	if ((h = mpool_get(t->bt_mp, P_ROOT, 0)) == NULL)
13750995Sbostic 		goto err;
13850995Sbostic 	if ((h->flags & P_TYPE) == P_BLEAF) {
13950995Sbostic 		h->flags = h->flags & ~P_TYPE | P_RLEAF;
14050995Sbostic 		mpool_put(t->bt_mp, h, MPOOL_DIRTY);
14150995Sbostic 	} else
14250995Sbostic 		mpool_put(t->bt_mp, h, 0);
14350995Sbostic 
14450995Sbostic 	if (openinfo && openinfo->flags & R_SNAPSHOT &&
14556757Sbostic 	    !ISSET(t, BTF_RINMEM) &&
14650995Sbostic 	    t->bt_irec(t, MAX_REC_NUMBER) == RET_ERROR)
14750995Sbostic                 goto err;
14850995Sbostic 	return (dbp);
14950995Sbostic 
15056703Sbostic einval:	errno = EINVAL;
15156757Sbostic err:	if (dbp != NULL)
15251090Sbostic 		__bt_close(dbp);
15356757Sbostic 	if (fname != NULL)
15456757Sbostic 		(void)close(rfd);
15550995Sbostic 	return (NULL);
15650995Sbostic }
157