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