xref: /dragonfly/lib/libc/db/recno/rec_get.c (revision e3146d3a)
1 /*-
2  * Copyright (c) 1990, 1993, 1994
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * @(#)rec_get.c	8.9 (Berkeley) 8/18/94
30  * $FreeBSD: src/lib/libc/db/recno/rec_get.c,v 1.2.6.2 2001/01/02 09:11:10 obrien Exp $
31  * $DragonFly: src/lib/libc/db/recno/rec_get.c,v 1.3 2005/09/19 09:20:37 asmodai Exp $
32  */
33 
34 #include <sys/types.h>
35 
36 #include <errno.h>
37 #include <stddef.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <unistd.h>
42 
43 #include <db.h>
44 #include "recno.h"
45 
46 /*
47  * __REC_GET -- Get a record from the btree.
48  *
49  * Parameters:
50  *	dbp:	pointer to access method
51  *	key:	key to find
52  *	data:	data to return
53  *	flag:	currently unused
54  *
55  * Returns:
56  *	RET_ERROR, RET_SUCCESS and RET_SPECIAL if the key not found.
57  */
58 int
59 __rec_get(dbp, key, data, flags)
60 	const DB *dbp;
61 	const DBT *key;
62 	DBT *data;
63 	u_int flags;
64 {
65 	BTREE *t;
66 	EPG *e;
67 	recno_t nrec;
68 	int status;
69 
70 	t = dbp->internal;
71 
72 	/* Toss any page pinned across calls. */
73 	if (t->bt_pinned != NULL) {
74 		mpool_put(t->bt_mp, t->bt_pinned, 0);
75 		t->bt_pinned = NULL;
76 	}
77 
78 	/* Get currently doesn't take any flags, and keys of 0 are illegal. */
79 	if (flags || (nrec = *(recno_t *)key->data) == 0) {
80 		errno = EINVAL;
81 		return (RET_ERROR);
82 	}
83 
84 	/*
85 	 * If we haven't seen this record yet, try to find it in the
86 	 * original file.
87 	 */
88 	if (nrec > t->bt_nrecs) {
89 		if (F_ISSET(t, R_EOF | R_INMEM))
90 			return (RET_SPECIAL);
91 		if ((status = t->bt_irec(t, nrec)) != RET_SUCCESS)
92 			return (status);
93 	}
94 
95 	--nrec;
96 	if ((e = __rec_search(t, nrec, SEARCH)) == NULL)
97 		return (RET_ERROR);
98 
99 	status = __rec_ret(t, e, 0, NULL, data);
100 	if (F_ISSET(t, B_DB_LOCK))
101 		mpool_put(t->bt_mp, e->page, 0);
102 	else
103 		t->bt_pinned = e->page;
104 	return (status);
105 }
106 
107 /*
108  * __REC_FPIPE -- Get fixed length records from a pipe.
109  *
110  * Parameters:
111  *	t:	tree
112  *	cnt:	records to read
113  *
114  * Returns:
115  *	RET_ERROR, RET_SUCCESS
116  */
117 int
118 __rec_fpipe(t, top)
119 	BTREE *t;
120 	recno_t top;
121 {
122 	DBT data;
123 	recno_t nrec;
124 	size_t len;
125 	int ch;
126 	u_char *p;
127 
128 	if (t->bt_rdata.size < t->bt_reclen) {
129 		t->bt_rdata.data = t->bt_rdata.data == NULL ?
130 		    malloc(t->bt_reclen) :
131 		    reallocf(t->bt_rdata.data, t->bt_reclen);
132 		if (t->bt_rdata.data == NULL)
133 			return (RET_ERROR);
134 		t->bt_rdata.size = t->bt_reclen;
135 	}
136 	data.data = t->bt_rdata.data;
137 	data.size = t->bt_reclen;
138 
139 	for (nrec = t->bt_nrecs; nrec < top;) {
140 		len = t->bt_reclen;
141 		for (p = t->bt_rdata.data;; *p++ = ch)
142 			if ((ch = getc(t->bt_rfp)) == EOF || !--len) {
143 				if (ch != EOF)
144 					*p = ch;
145 				if (len != 0)
146 					memset(p, t->bt_bval, len);
147 				if (__rec_iput(t,
148 				    nrec, &data, 0) != RET_SUCCESS)
149 					return (RET_ERROR);
150 				++nrec;
151 				break;
152 			}
153 		if (ch == EOF)
154 			break;
155 	}
156 	if (nrec < top) {
157 		F_SET(t, R_EOF);
158 		return (RET_SPECIAL);
159 	}
160 	return (RET_SUCCESS);
161 }
162 
163 /*
164  * __REC_VPIPE -- Get variable length records from a pipe.
165  *
166  * Parameters:
167  *	t:	tree
168  *	cnt:	records to read
169  *
170  * Returns:
171  *	RET_ERROR, RET_SUCCESS
172  */
173 int
174 __rec_vpipe(t, top)
175 	BTREE *t;
176 	recno_t top;
177 {
178 	DBT data;
179 	recno_t nrec;
180 	size_t len;
181 	size_t sz;
182 	int bval, ch;
183 	u_char *p;
184 
185 	bval = t->bt_bval;
186 	for (nrec = t->bt_nrecs; nrec < top; ++nrec) {
187 		for (p = t->bt_rdata.data,
188 		    sz = t->bt_rdata.size;; *p++ = ch, --sz) {
189 			if ((ch = getc(t->bt_rfp)) == EOF || ch == bval) {
190 				data.data = t->bt_rdata.data;
191 				data.size = p - (u_char *)t->bt_rdata.data;
192 				if (ch == EOF && data.size == 0)
193 					break;
194 				if (__rec_iput(t, nrec, &data, 0)
195 				    != RET_SUCCESS)
196 					return (RET_ERROR);
197 				break;
198 			}
199 			if (sz == 0) {
200 				len = p - (u_char *)t->bt_rdata.data;
201 				t->bt_rdata.size += (sz = 256);
202 				t->bt_rdata.data = t->bt_rdata.data == NULL ?
203 				    malloc(t->bt_rdata.size) :
204 				    reallocf(t->bt_rdata.data, t->bt_rdata.size);
205 				if (t->bt_rdata.data == NULL)
206 					return (RET_ERROR);
207 				p = (u_char *)t->bt_rdata.data + len;
208 			}
209 		}
210 		if (ch == EOF)
211 			break;
212 	}
213 	if (nrec < top) {
214 		F_SET(t, R_EOF);
215 		return (RET_SPECIAL);
216 	}
217 	return (RET_SUCCESS);
218 }
219 
220 /*
221  * __REC_FMAP -- Get fixed length records from a file.
222  *
223  * Parameters:
224  *	t:	tree
225  *	cnt:	records to read
226  *
227  * Returns:
228  *	RET_ERROR, RET_SUCCESS
229  */
230 int
231 __rec_fmap(t, top)
232 	BTREE *t;
233 	recno_t top;
234 {
235 	DBT data;
236 	recno_t nrec;
237 	u_char *sp, *ep, *p;
238 	size_t len;
239 
240 	if (t->bt_rdata.size < t->bt_reclen) {
241 		t->bt_rdata.data = t->bt_rdata.data == NULL ?
242 		    malloc(t->bt_reclen) :
243 		    reallocf(t->bt_rdata.data, t->bt_reclen);
244 		if (t->bt_rdata.data == NULL)
245 			return (RET_ERROR);
246 		t->bt_rdata.size = t->bt_reclen;
247 	}
248 	data.data = t->bt_rdata.data;
249 	data.size = t->bt_reclen;
250 
251 	sp = (u_char *)t->bt_cmap;
252 	ep = (u_char *)t->bt_emap;
253 	for (nrec = t->bt_nrecs; nrec < top; ++nrec) {
254 		if (sp >= ep) {
255 			F_SET(t, R_EOF);
256 			return (RET_SPECIAL);
257 		}
258 		len = t->bt_reclen;
259 		for (p = t->bt_rdata.data;
260 		    sp < ep && len > 0; *p++ = *sp++, --len);
261 		if (len != 0)
262 			memset(p, t->bt_bval, len);
263 		if (__rec_iput(t, nrec, &data, 0) != RET_SUCCESS)
264 			return (RET_ERROR);
265 	}
266 	t->bt_cmap = (caddr_t)sp;
267 	return (RET_SUCCESS);
268 }
269 
270 /*
271  * __REC_VMAP -- Get variable length records from a file.
272  *
273  * Parameters:
274  *	t:	tree
275  *	cnt:	records to read
276  *
277  * Returns:
278  *	RET_ERROR, RET_SUCCESS
279  */
280 int
281 __rec_vmap(t, top)
282 	BTREE *t;
283 	recno_t top;
284 {
285 	DBT data;
286 	u_char *sp, *ep;
287 	recno_t nrec;
288 	int bval;
289 
290 	sp = (u_char *)t->bt_cmap;
291 	ep = (u_char *)t->bt_emap;
292 	bval = t->bt_bval;
293 
294 	for (nrec = t->bt_nrecs; nrec < top; ++nrec) {
295 		if (sp >= ep) {
296 			F_SET(t, R_EOF);
297 			return (RET_SPECIAL);
298 		}
299 		for (data.data = sp; sp < ep && *sp != bval; ++sp);
300 		data.size = sp - (u_char *)data.data;
301 		if (__rec_iput(t, nrec, &data, 0) != RET_SUCCESS)
302 			return (RET_ERROR);
303 		++sp;
304 	}
305 	t->bt_cmap = (caddr_t)sp;
306 	return (RET_SUCCESS);
307 }
308