xref: /csrg-svn/lib/libc/db/recno/rec_open.c (revision 51090)
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*51090Sbostic static char sccsid[] = "@(#)rec_open.c	5.2 (Berkeley) 09/11/91";
1350995Sbostic #endif /* LIBC_SCCS and not lint */
1450995Sbostic 
1550995Sbostic #include <sys/types.h>
1650995Sbostic #include <sys/mman.h>
1750995Sbostic #include <sys/stat.h>
1850995Sbostic #include <fcntl.h>
1950995Sbostic #include <errno.h>
2050995Sbostic #include <limits.h>
2150995Sbostic #include <db.h>
2250995Sbostic #include <unistd.h>
2350995Sbostic #include <stdio.h>
2450995Sbostic #include <stddef.h>
25*51090Sbostic #include "recno.h"
2650995Sbostic 
2750995Sbostic DB *
2850995Sbostic __rec_open(fname, flags, mode, openinfo)
2950995Sbostic 	const char *fname;
3050995Sbostic 	int flags, mode;
3150995Sbostic 	const RECNOINFO *openinfo;
3250995Sbostic {
3350995Sbostic 	BTREE *t;
3450995Sbostic 	BTREEINFO btopeninfo;
3550995Sbostic 	DB *dbp;
3650995Sbostic 	PAGE *h;
3750995Sbostic 	struct stat sb;
3850995Sbostic 	int rfd;
3950995Sbostic 
4050995Sbostic 	/* Open the user's file -- if this fails, we're done. */
4150995Sbostic 	if ((rfd = open(fname, flags, mode)) < 0)
4250995Sbostic 		return (NULL);
4350995Sbostic 
4450995Sbostic 	/* Create a btree in memory (backed by disk). */
4550995Sbostic 	if (openinfo) {
46*51090Sbostic 		if (openinfo->flags & ~(R_FIXEDLEN|R_NOKEY|R_SNAPSHOT)) {
47*51090Sbostic 			errno = EINVAL;
48*51090Sbostic 			goto err;
49*51090Sbostic 		}
5050995Sbostic 		btopeninfo.flags = 0;
5150995Sbostic 		btopeninfo.cachesize = openinfo->cachesize;
5250995Sbostic 		btopeninfo.psize = 0;
5350995Sbostic 		btopeninfo.compare = NULL;
5450995Sbostic 		btopeninfo.lorder = openinfo->lorder;
5550995Sbostic 		dbp = __bt_open(NULL, O_RDWR, S_IRUSR | S_IWUSR, &btopeninfo);
5650995Sbostic 	} else
5750995Sbostic 		dbp = __bt_open(NULL, O_RDWR, S_IRUSR | S_IWUSR, NULL);
58*51090Sbostic 	if (dbp == NULL)
59*51090Sbostic 		goto err;
6050995Sbostic 
6150995Sbostic 	/*
6250995Sbostic 	 * Some fields in the tree structure are recno specific.  Fill them
63*51090Sbostic 	 * in and make the btree structure look like a recno structure.  We
64*51090Sbostic 	 * don't change the bt_ovflsize value, it's close enough and slightly
65*51090Sbostic 	 * bigger.
6650995Sbostic 	 */
6750995Sbostic 	t = dbp->internal;
6850995Sbostic 	if (openinfo) {
6950995Sbostic 		if (openinfo->flags & R_FIXEDLEN)
7050995Sbostic 			t->bt_flags |= BTF_FIXEDLEN;
7150995Sbostic 
7250995Sbostic 		t->bt_reclen = openinfo->reclen;
7350995Sbostic 		if (t->bt_reclen == 0) {
7450995Sbostic 			errno = EINVAL;
7550995Sbostic 			goto err;
7650995Sbostic 		}
7750995Sbostic 
7850995Sbostic 		t->bt_bval = openinfo->bval;
7950995Sbostic 	} else
8050995Sbostic 		t->bt_bval = '\n';
8150995Sbostic 
8250995Sbostic 	t->bt_flags = BTF_RECNO;
8350995Sbostic 
8450995Sbostic 	/*
8550995Sbostic  	 * In 4.4BSD stat(2) returns true for ISSOCK on pipes.  Until then,
8650995Sbostic 	 * this is fairly close.  Pipes are read-only.
8750995Sbostic  	 */
8850995Sbostic 	if (lseek(rfd, 0L, SEEK_CUR) == -1 && errno == ESPIPE) {
8950995Sbostic 		SET(t, BTF_RDONLY);
9050995Sbostic 		if ((t->bt_rfp = fdopen(rfd, "r")) == NULL)
9150995Sbostic 			goto err;
9250995Sbostic 		t->bt_irec = ISSET(t, BTF_FIXEDLEN) ? __rec_fpipe : __rec_vpipe;
9350995Sbostic 	} else {
9450995Sbostic 		if (fstat(rfd, &sb))
9550995Sbostic 			goto err;
9650995Sbostic 		if (!(flags & (O_RDWR | O_WRONLY)))
9750995Sbostic 			SET(t, BTF_RDONLY);
9850995Sbostic 		if ((t->bt_smap = mmap(NULL, sb.st_size, PROT_READ, MAP_FILE,
9950995Sbostic 		    rfd, (off_t)0)) == NULL)
10050995Sbostic 			goto err;
10150995Sbostic 		t->bt_emap = t->bt_smap + sb.st_size;
10250995Sbostic 		t->bt_rfd = rfd;
10350995Sbostic 		t->bt_irec = ISSET(t, BTF_FIXEDLEN) ? __rec_fmap : __rec_vmap;
10450995Sbostic 	}
10550995Sbostic 
10650995Sbostic 	/* Use the recno routines. */
10750995Sbostic 	dbp->close = __rec_close;
10850995Sbostic 	dbp->del = __rec_delete;
10950995Sbostic 	dbp->get = __rec_get;
11050995Sbostic 	dbp->put = __rec_put;
11150995Sbostic 	dbp->seq = __rec_seq;
11250995Sbostic 	dbp->sync = __rec_sync;
11350995Sbostic 
11450995Sbostic 	/* If the root page was created, reset the flags. */
11550995Sbostic 	if ((h = mpool_get(t->bt_mp, P_ROOT, 0)) == NULL)
11650995Sbostic 		goto err;
11750995Sbostic 	if ((h->flags & P_TYPE) == P_BLEAF) {
11850995Sbostic 		h->flags = h->flags & ~P_TYPE | P_RLEAF;
11950995Sbostic 		mpool_put(t->bt_mp, h, MPOOL_DIRTY);
12050995Sbostic 	} else
12150995Sbostic 		mpool_put(t->bt_mp, h, 0);
12250995Sbostic 
12350995Sbostic 	if (openinfo && openinfo->flags & R_SNAPSHOT &&
12450995Sbostic 	    t->bt_irec(t, MAX_REC_NUMBER) == RET_ERROR)
12550995Sbostic                 goto err;
12650995Sbostic 	return (dbp);
12750995Sbostic 
128*51090Sbostic err:	if (dbp)
129*51090Sbostic 		__bt_close(dbp);
13050995Sbostic 	(void)close(rfd);
13150995Sbostic 	return (NULL);
13250995Sbostic }
133