xref: /original-bsd/lib/libc/db/recno/rec_open.c (revision d250c449)
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.17 (Berkeley) 05/06/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 			/*
122 			 * Kludge -- but we don't know what size an off_t
123 			 * is or what size a size_t is, although we do
124 			 * know that the former is signed and the latter
125 			 * unsigned.
126 			 */
127 			if (sizeof(sb.st_size) > sizeof(size_t)) {
128 				if (sb.st_size > (off_t)SIZE_T_MAX) {
129 					errno = EFBIG;
130 					goto err;
131 				}
132 			} else {
133 				if ((size_t)sb.st_size > SIZE_T_MAX) {
134 					errno = EFBIG;
135 					goto err;
136 				}
137 			}
138 			if (sb.st_size == 0)
139 				SET(t, BTF_EOF);
140 			else {
141 				t->bt_msize = sb.st_size;
142 				if ((t->bt_smap =
143 				    mmap(NULL, t->bt_msize, PROT_READ, 0, rfd,
144 				    (off_t)0)) == (caddr_t)-1)
145 					goto slow;
146 				t->bt_cmap = t->bt_smap;
147 				t->bt_emap = t->bt_smap + sb.st_size;
148 				t->bt_irec = ISSET(t, BTF_FIXEDLEN) ?
149 				    __rec_fmap : __rec_vmap;
150 				SET(t, BTF_MEMMAPPED);
151 			}
152 		}
153 	}
154 
155 	/* Use the recno routines. */
156 	dbp->close = __rec_close;
157 	dbp->del = __rec_delete;
158 	dbp->get = __rec_get;
159 	dbp->put = __rec_put;
160 	dbp->seq = __rec_seq;
161 	dbp->sync = __rec_sync;
162 
163 	/* If the root page was created, reset the flags. */
164 	if ((h = mpool_get(t->bt_mp, P_ROOT, 0)) == NULL)
165 		goto err;
166 	if ((h->flags & P_TYPE) == P_BLEAF) {
167 		h->flags = h->flags & ~P_TYPE | P_RLEAF;
168 		mpool_put(t->bt_mp, h, MPOOL_DIRTY);
169 	} else
170 		mpool_put(t->bt_mp, h, 0);
171 
172 	if (openinfo && openinfo->flags & R_SNAPSHOT &&
173 	    !ISSET(t, BTF_EOF | BTF_RINMEM) &&
174 	    t->bt_irec(t, MAX_REC_NUMBER) == RET_ERROR)
175                 goto err;
176 	return (dbp);
177 
178 einval:	errno = EINVAL;
179 err:	sverrno = errno;
180 	if (dbp != NULL)
181 		(void)__bt_close(dbp);
182 	if (fname != NULL)
183 		(void)close(rfd);
184 	errno = sverrno;
185 	return (NULL);
186 }
187