xref: /csrg-svn/lib/libc/db/recno/rec_open.c (revision 58751)
1 /*-
2  * Copyright (c) 1990 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Mike Olson.
7  *
8  * %sccs.include.redist.c%
9  */
10 
11 #if defined(LIBC_SCCS) && !defined(lint)
12 static char sccsid[] = "@(#)rec_open.c	5.15 (Berkeley) 03/19/93";
13 #endif /* LIBC_SCCS and not lint */
14 
15 #include <sys/types.h>
16 #include <sys/mman.h>
17 #include <sys/stat.h>
18 
19 #include <errno.h>
20 #include <fcntl.h>
21 #include <limits.h>
22 #include <stddef.h>
23 #include <stdio.h>
24 #include <unistd.h>
25 
26 #include <db.h>
27 #include "recno.h"
28 
29 DB *
30 __rec_open(fname, flags, mode, openinfo)
31 	const char *fname;
32 	int flags, mode;
33 	const RECNOINFO *openinfo;
34 {
35 	BTREE *t;
36 	BTREEINFO btopeninfo;
37 	DB *dbp;
38 	PAGE *h;
39 	struct stat sb;
40 	int rfd, sverrno;
41 
42 	/* Open the user's file -- if this fails, we're done. */
43 	if (fname != NULL && (rfd = open(fname, flags, mode)) < 0)
44 		return (NULL);
45 
46 	/* Create a btree in memory (backed by disk). */
47 	dbp = NULL;
48 	if (openinfo) {
49 		if (openinfo->flags & ~(R_FIXEDLEN | R_NOKEY | R_SNAPSHOT))
50 			goto einval;
51 		btopeninfo.flags = 0;
52 		btopeninfo.cachesize = openinfo->cachesize;
53 		btopeninfo.psize = 0;
54 		btopeninfo.compare = NULL;
55 		btopeninfo.lorder = openinfo->lorder;
56 		dbp = __bt_open(NULL, O_RDWR, S_IRUSR | S_IWUSR, &btopeninfo);
57 	} else
58 		dbp = __bt_open(NULL, O_RDWR, S_IRUSR | S_IWUSR, NULL);
59 	if (dbp == NULL)
60 		goto err;
61 
62 	/*
63 	 * Some fields in the tree structure are recno specific.  Fill them
64 	 * in and make the btree structure look like a recno structure.  We
65 	 * don't change the bt_ovflsize value, it's close enough and slightly
66 	 * bigger.
67 	 */
68 	t = dbp->internal;
69 	if (openinfo) {
70 		if (openinfo->flags & R_FIXEDLEN) {
71 			SET(t, BTF_FIXEDLEN);
72 			t->bt_reclen = openinfo->reclen;
73 			if (t->bt_reclen == 0)
74 				goto einval;
75 		}
76 		t->bt_bval = openinfo->bval;
77 	} else
78 		t->bt_bval = '\n';
79 
80 	SET(t, BTF_RECNO);
81 	if (fname == NULL)
82 		SET(t, BTF_EOF | BTF_RINMEM);
83 	else
84 		t->bt_rfd = rfd;
85 	t->bt_rcursor = 0;
86 
87 	/*
88 	 * In 4.4BSD stat(2) returns true for ISSOCK on pipes.  Until
89 	 * then, this is fairly close.  Pipes are read-only.
90 	 */
91 	if (fname != NULL)
92 		if (lseek(rfd, (off_t)0, SEEK_CUR) == -1 && errno == ESPIPE) {
93 			switch(flags & O_ACCMODE) {
94 			case O_RDONLY:
95 				SET(t, BTF_RDONLY);
96 				break;
97 			case O_RDWR:
98 			case O_WRONLY:
99 			default:
100 				goto einval;
101 			}
102 slow:			if ((t->bt_rfp = fdopen(rfd, "r")) == NULL)
103 				goto err;
104 			SET(t, BTF_CLOSEFP);
105 			t->bt_irec =
106 			    ISSET(t, BTF_FIXEDLEN) ? __rec_fpipe : __rec_vpipe;
107 		} else {
108 			switch(flags & O_ACCMODE) {
109 			case O_RDONLY:
110 				SET(t, BTF_RDONLY);
111 				break;
112 			case O_RDWR:
113 				break;
114 			case O_WRONLY:
115 			default:
116 				goto einval;
117 			}
118 
119 			if (fstat(rfd, &sb))
120 				goto err;
121 			if (sb.st_size > (off_t)SIZE_T_MAX) {
122 				errno = EFBIG;
123 				goto err;
124 			}
125 			if (sb.st_size == 0)
126 				SET(t, BTF_EOF);
127 			else {
128 				t->bt_msize = sb.st_size;
129 				if ((t->bt_smap =
130 				    mmap(NULL, t->bt_msize, PROT_READ, 0, rfd,
131 				    (off_t)0)) == (caddr_t)-1)
132 					goto slow;
133 				t->bt_cmap = t->bt_smap;
134 				t->bt_emap = t->bt_smap + sb.st_size;
135 				t->bt_irec = ISSET(t, BTF_FIXEDLEN) ?
136 				    __rec_fmap : __rec_vmap;
137 				SET(t, BTF_MEMMAPPED);
138 			}
139 		}
140 
141 	/* Use the recno routines. */
142 	dbp->close = __rec_close;
143 	dbp->del = __rec_delete;
144 	dbp->get = __rec_get;
145 	dbp->put = __rec_put;
146 	dbp->seq = __rec_seq;
147 	dbp->sync = __rec_sync;
148 
149 	/* If the root page was created, reset the flags. */
150 	if ((h = mpool_get(t->bt_mp, P_ROOT, 0)) == NULL)
151 		goto err;
152 	if ((h->flags & P_TYPE) == P_BLEAF) {
153 		h->flags = h->flags & ~P_TYPE | P_RLEAF;
154 		mpool_put(t->bt_mp, h, MPOOL_DIRTY);
155 	} else
156 		mpool_put(t->bt_mp, h, 0);
157 
158 	if (openinfo && openinfo->flags & R_SNAPSHOT &&
159 	    !ISSET(t, BTF_EOF | BTF_RINMEM) &&
160 	    t->bt_irec(t, MAX_REC_NUMBER) == RET_ERROR)
161                 goto err;
162 	return (dbp);
163 
164 einval:	errno = EINVAL;
165 err:	sverrno = errno;
166 	if (dbp != NULL)
167 		(void)__bt_close(dbp);
168 	if (fname != NULL)
169 		(void)close(rfd);
170 	errno = sverrno;
171 	return (NULL);
172 }
173