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