xref: /freebsd/lib/libc/db/recno/rec_get.c (revision b9f654b163bce26de79705e77b872427c9f2afa1)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1990, 1993, 1994
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #if defined(LIBC_SCCS) && !defined(lint)
33 static char sccsid[] = "@(#)rec_get.c	8.9 (Berkeley) 8/18/94";
34 #endif /* LIBC_SCCS and not lint */
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
37 
38 #include <sys/types.h>
39 
40 #include <errno.h>
41 #include <stddef.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <unistd.h>
46 
47 #include <db.h>
48 #include "recno.h"
49 
50 /*
51  * __REC_GET -- Get a record from the btree.
52  *
53  * Parameters:
54  *	dbp:	pointer to access method
55  *	key:	key to find
56  *	data:	data to return
57  *	flag:	currently unused
58  *
59  * Returns:
60  *	RET_ERROR, RET_SUCCESS and RET_SPECIAL if the key not found.
61  */
62 int
63 __rec_get(const DB *dbp, const DBT *key, DBT *data, 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(BTREE *t, recno_t top)
119 {
120 	DBT data;
121 	recno_t nrec;
122 	size_t len;
123 	int ch;
124 	u_char *p;
125 
126 	if (t->bt_rdata.size < t->bt_reclen) {
127 		t->bt_rdata.data = reallocf(t->bt_rdata.data, t->bt_reclen);
128 		if (t->bt_rdata.data == NULL)
129 			return (RET_ERROR);
130 		t->bt_rdata.size = t->bt_reclen;
131 	}
132 	data.data = t->bt_rdata.data;
133 	data.size = t->bt_reclen;
134 
135 	for (nrec = t->bt_nrecs; nrec < top;) {
136 		len = t->bt_reclen;
137 		for (p = t->bt_rdata.data;; *p++ = ch)
138 			if ((ch = getc(t->bt_rfp)) == EOF || !--len) {
139 				if (ch != EOF)
140 					*p = ch;
141 				if (len != 0)
142 					memset(p, t->bt_bval, len);
143 				if (__rec_iput(t,
144 				    nrec, &data, 0) != RET_SUCCESS)
145 					return (RET_ERROR);
146 				++nrec;
147 				break;
148 			}
149 		if (ch == EOF)
150 			break;
151 	}
152 	if (nrec < top) {
153 		F_SET(t, R_EOF);
154 		return (RET_SPECIAL);
155 	}
156 	return (RET_SUCCESS);
157 }
158 
159 /*
160  * __REC_VPIPE -- Get variable length records from a pipe.
161  *
162  * Parameters:
163  *	t:	tree
164  *	cnt:	records to read
165  *
166  * Returns:
167  *	RET_ERROR, RET_SUCCESS
168  */
169 int
170 __rec_vpipe(BTREE *t, recno_t top)
171 {
172 	DBT data;
173 	recno_t nrec;
174 	size_t len;
175 	size_t sz;
176 	int bval, ch;
177 	u_char *p;
178 
179 	bval = t->bt_bval;
180 	for (nrec = t->bt_nrecs; nrec < top; ++nrec) {
181 		for (p = t->bt_rdata.data,
182 		    sz = t->bt_rdata.size;; *p++ = ch, --sz) {
183 			if ((ch = getc(t->bt_rfp)) == EOF || ch == bval) {
184 				data.data = t->bt_rdata.data;
185 				data.size = p - (u_char *)t->bt_rdata.data;
186 				if (ch == EOF && data.size == 0)
187 					break;
188 				if (__rec_iput(t, nrec, &data, 0)
189 				    != RET_SUCCESS)
190 					return (RET_ERROR);
191 				break;
192 			}
193 			if (sz == 0) {
194 				len = p - (u_char *)t->bt_rdata.data;
195 				t->bt_rdata.size += (sz = 256);
196 				t->bt_rdata.data = reallocf(t->bt_rdata.data, t->bt_rdata.size);
197 				if (t->bt_rdata.data == NULL)
198 					return (RET_ERROR);
199 				p = (u_char *)t->bt_rdata.data + len;
200 			}
201 		}
202 		if (ch == EOF)
203 			break;
204 	}
205 	if (nrec < top) {
206 		F_SET(t, R_EOF);
207 		return (RET_SPECIAL);
208 	}
209 	return (RET_SUCCESS);
210 }
211 
212 /*
213  * __REC_FMAP -- Get fixed length records from a file.
214  *
215  * Parameters:
216  *	t:	tree
217  *	cnt:	records to read
218  *
219  * Returns:
220  *	RET_ERROR, RET_SUCCESS
221  */
222 int
223 __rec_fmap(BTREE *t, recno_t top)
224 {
225 	DBT data;
226 	recno_t nrec;
227 	u_char *sp, *ep, *p;
228 	size_t len;
229 
230 	if (t->bt_rdata.size < t->bt_reclen) {
231 		t->bt_rdata.data = reallocf(t->bt_rdata.data, t->bt_reclen);
232 		if (t->bt_rdata.data == NULL)
233 			return (RET_ERROR);
234 		t->bt_rdata.size = t->bt_reclen;
235 	}
236 	data.data = t->bt_rdata.data;
237 	data.size = t->bt_reclen;
238 
239 	sp = (u_char *)t->bt_cmap;
240 	ep = (u_char *)t->bt_emap;
241 	for (nrec = t->bt_nrecs; nrec < top; ++nrec) {
242 		if (sp >= ep) {
243 			F_SET(t, R_EOF);
244 			return (RET_SPECIAL);
245 		}
246 		len = t->bt_reclen;
247 		for (p = t->bt_rdata.data;
248 		    sp < ep && len > 0; *p++ = *sp++, --len);
249 		if (len != 0)
250 			memset(p, t->bt_bval, len);
251 		if (__rec_iput(t, nrec, &data, 0) != RET_SUCCESS)
252 			return (RET_ERROR);
253 	}
254 	t->bt_cmap = (caddr_t)sp;
255 	return (RET_SUCCESS);
256 }
257 
258 /*
259  * __REC_VMAP -- Get variable length records from a file.
260  *
261  * Parameters:
262  *	t:	tree
263  *	cnt:	records to read
264  *
265  * Returns:
266  *	RET_ERROR, RET_SUCCESS
267  */
268 int
269 __rec_vmap(BTREE *t, recno_t top)
270 {
271 	DBT data;
272 	u_char *sp, *ep;
273 	recno_t nrec;
274 	int bval;
275 
276 	sp = (u_char *)t->bt_cmap;
277 	ep = (u_char *)t->bt_emap;
278 	bval = t->bt_bval;
279 
280 	for (nrec = t->bt_nrecs; nrec < top; ++nrec) {
281 		if (sp >= ep) {
282 			F_SET(t, R_EOF);
283 			return (RET_SPECIAL);
284 		}
285 		for (data.data = sp; sp < ep && *sp != bval; ++sp);
286 		data.size = sp - (u_char *)data.data;
287 		if (__rec_iput(t, nrec, &data, 0) != RET_SUCCESS)
288 			return (RET_ERROR);
289 		++sp;
290 	}
291 	t->bt_cmap = (caddr_t)sp;
292 	return (RET_SUCCESS);
293 }
294