xref: /original-bsd/lib/libc/db/recno/rec_open.c (revision 48611f03)
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.16 (Berkeley) 03/23/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.maxkeypage = 0;
54 		btopeninfo.minkeypage = 0;
55 		btopeninfo.psize = 0;
56 		btopeninfo.compare = NULL;
57 		btopeninfo.prefix = NULL;
58 		btopeninfo.lorder = openinfo->lorder;
59 		dbp = __bt_open(NULL, O_RDWR, S_IRUSR | S_IWUSR, &btopeninfo);
60 	} else
61 		dbp = __bt_open(NULL, O_RDWR, S_IRUSR | S_IWUSR, NULL);
62 	if (dbp == NULL)
63 		goto err;
64 
65 	/*
66 	 * Some fields in the tree structure are recno specific.  Fill them
67 	 * in and make the btree structure look like a recno structure.  We
68 	 * don't change the bt_ovflsize value, it's close enough and slightly
69 	 * bigger.
70 	 */
71 	t = dbp->internal;
72 	if (openinfo) {
73 		if (openinfo->flags & R_FIXEDLEN) {
74 			SET(t, BTF_FIXEDLEN);
75 			t->bt_reclen = openinfo->reclen;
76 			if (t->bt_reclen == 0)
77 				goto einval;
78 		}
79 		t->bt_bval = openinfo->bval;
80 	} else
81 		t->bt_bval = '\n';
82 
83 	SET(t, BTF_RECNO);
84 	if (fname == NULL)
85 		SET(t, BTF_EOF | BTF_RINMEM);
86 	else
87 		t->bt_rfd = rfd;
88 	t->bt_rcursor = 0;
89 
90 	/*
91 	 * In 4.4BSD stat(2) returns true for ISSOCK on pipes.  Until
92 	 * then, this is fairly close.  Pipes are read-only.
93 	 */
94 	if (fname != NULL) {
95 		if (lseek(rfd, (off_t)0, SEEK_CUR) == -1 && errno == ESPIPE) {
96 			switch (flags & O_ACCMODE) {
97 			case O_RDONLY:
98 				SET(t, BTF_RDONLY);
99 				break;
100 			default:
101 				goto einval;
102 			}
103 slow:			if ((t->bt_rfp = fdopen(rfd, "r")) == NULL)
104 				goto err;
105 			SET(t, BTF_CLOSEFP);
106 			t->bt_irec =
107 			    ISSET(t, BTF_FIXEDLEN) ? __rec_fpipe : __rec_vpipe;
108 		} else {
109 			switch (flags & O_ACCMODE) {
110 			case O_RDONLY:
111 				SET(t, BTF_RDONLY);
112 				break;
113 			case O_RDWR:
114 				break;
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 
142 	/* Use the recno routines. */
143 	dbp->close = __rec_close;
144 	dbp->del = __rec_delete;
145 	dbp->get = __rec_get;
146 	dbp->put = __rec_put;
147 	dbp->seq = __rec_seq;
148 	dbp->sync = __rec_sync;
149 
150 	/* If the root page was created, reset the flags. */
151 	if ((h = mpool_get(t->bt_mp, P_ROOT, 0)) == NULL)
152 		goto err;
153 	if ((h->flags & P_TYPE) == P_BLEAF) {
154 		h->flags = h->flags & ~P_TYPE | P_RLEAF;
155 		mpool_put(t->bt_mp, h, MPOOL_DIRTY);
156 	} else
157 		mpool_put(t->bt_mp, h, 0);
158 
159 	if (openinfo && openinfo->flags & R_SNAPSHOT &&
160 	    !ISSET(t, BTF_EOF | BTF_RINMEM) &&
161 	    t->bt_irec(t, MAX_REC_NUMBER) == RET_ERROR)
162                 goto err;
163 	return (dbp);
164 
165 einval:	errno = EINVAL;
166 err:	sverrno = errno;
167 	if (dbp != NULL)
168 		(void)__bt_close(dbp);
169 	if (fname != NULL)
170 		(void)close(rfd);
171 	errno = sverrno;
172 	return (NULL);
173 }
174