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/types.h> 36 37 #include <errno.h> 38 #include <stddef.h> 39 #include <stdio.h> 40 #include <stdlib.h> 41 #include <string.h> 42 #include <unistd.h> 43 44 #include <db.h> 45 #include "recno.h" 46 47 /* 48 * __REC_GET -- Get a record from the btree. 49 * 50 * Parameters: 51 * dbp: pointer to access method 52 * key: key to find 53 * data: data to return 54 * flag: currently unused 55 * 56 * Returns: 57 * RET_ERROR, RET_SUCCESS and RET_SPECIAL if the key not found. 58 */ 59 int 60 __rec_get(const DB *dbp, const DBT *key, DBT *data, u_int flags) 61 { 62 BTREE *t; 63 EPG *e; 64 recno_t nrec; 65 int status; 66 67 t = dbp->internal; 68 69 /* Toss any page pinned across calls. */ 70 if (t->bt_pinned != NULL) { 71 mpool_put(t->bt_mp, t->bt_pinned, 0); 72 t->bt_pinned = NULL; 73 } 74 75 /* Get currently doesn't take any flags, and keys of 0 are illegal. */ 76 if (flags || (nrec = *(recno_t *)key->data) == 0) { 77 errno = EINVAL; 78 return (RET_ERROR); 79 } 80 81 /* 82 * If we haven't seen this record yet, try to find it in the 83 * original file. 84 */ 85 if (nrec > t->bt_nrecs) { 86 if (F_ISSET(t, R_EOF | R_INMEM)) 87 return (RET_SPECIAL); 88 if ((status = t->bt_irec(t, nrec)) != RET_SUCCESS) 89 return (status); 90 } 91 92 --nrec; 93 if ((e = __rec_search(t, nrec, SEARCH)) == NULL) 94 return (RET_ERROR); 95 96 status = __rec_ret(t, e, 0, NULL, data); 97 if (F_ISSET(t, B_DB_LOCK)) 98 mpool_put(t->bt_mp, e->page, 0); 99 else 100 t->bt_pinned = e->page; 101 return (status); 102 } 103 104 /* 105 * __REC_FPIPE -- Get fixed length records from a pipe. 106 * 107 * Parameters: 108 * t: tree 109 * cnt: records to read 110 * 111 * Returns: 112 * RET_ERROR, RET_SUCCESS 113 */ 114 int 115 __rec_fpipe(BTREE *t, recno_t top) 116 { 117 DBT data; 118 recno_t nrec; 119 size_t len; 120 int ch; 121 u_char *p; 122 123 if (t->bt_rdata.size < t->bt_reclen) { 124 t->bt_rdata.data = reallocf(t->bt_rdata.data, t->bt_reclen); 125 if (t->bt_rdata.data == NULL) 126 return (RET_ERROR); 127 t->bt_rdata.size = t->bt_reclen; 128 } 129 data.data = t->bt_rdata.data; 130 data.size = t->bt_reclen; 131 132 for (nrec = t->bt_nrecs; nrec < top;) { 133 len = t->bt_reclen; 134 for (p = t->bt_rdata.data;; *p++ = ch) 135 if ((ch = getc(t->bt_rfp)) == EOF || !--len) { 136 if (ch != EOF) 137 *p = ch; 138 if (len != 0) 139 memset(p, t->bt_bval, len); 140 if (__rec_iput(t, 141 nrec, &data, 0) != RET_SUCCESS) 142 return (RET_ERROR); 143 ++nrec; 144 break; 145 } 146 if (ch == EOF) 147 break; 148 } 149 if (nrec < top) { 150 F_SET(t, R_EOF); 151 return (RET_SPECIAL); 152 } 153 return (RET_SUCCESS); 154 } 155 156 /* 157 * __REC_VPIPE -- Get variable length records from a pipe. 158 * 159 * Parameters: 160 * t: tree 161 * cnt: records to read 162 * 163 * Returns: 164 * RET_ERROR, RET_SUCCESS 165 */ 166 int 167 __rec_vpipe(BTREE *t, recno_t top) 168 { 169 DBT data; 170 recno_t nrec; 171 size_t len; 172 size_t sz; 173 int bval, ch; 174 u_char *p; 175 176 bval = t->bt_bval; 177 for (nrec = t->bt_nrecs; nrec < top; ++nrec) { 178 for (p = t->bt_rdata.data, 179 sz = t->bt_rdata.size;; *p++ = ch, --sz) { 180 if ((ch = getc(t->bt_rfp)) == EOF || ch == bval) { 181 data.data = t->bt_rdata.data; 182 data.size = p - (u_char *)t->bt_rdata.data; 183 if (ch == EOF && data.size == 0) 184 break; 185 if (__rec_iput(t, nrec, &data, 0) 186 != RET_SUCCESS) 187 return (RET_ERROR); 188 break; 189 } 190 if (sz == 0) { 191 len = p - (u_char *)t->bt_rdata.data; 192 t->bt_rdata.size += (sz = 256); 193 t->bt_rdata.data = reallocf(t->bt_rdata.data, t->bt_rdata.size); 194 if (t->bt_rdata.data == NULL) 195 return (RET_ERROR); 196 p = (u_char *)t->bt_rdata.data + len; 197 } 198 } 199 if (ch == EOF) 200 break; 201 } 202 if (nrec < top) { 203 F_SET(t, R_EOF); 204 return (RET_SPECIAL); 205 } 206 return (RET_SUCCESS); 207 } 208 209 /* 210 * __REC_FMAP -- Get fixed length records from a file. 211 * 212 * Parameters: 213 * t: tree 214 * cnt: records to read 215 * 216 * Returns: 217 * RET_ERROR, RET_SUCCESS 218 */ 219 int 220 __rec_fmap(BTREE *t, recno_t top) 221 { 222 DBT data; 223 recno_t nrec; 224 u_char *sp, *ep, *p; 225 size_t len; 226 227 if (t->bt_rdata.size < t->bt_reclen) { 228 t->bt_rdata.data = reallocf(t->bt_rdata.data, t->bt_reclen); 229 if (t->bt_rdata.data == NULL) 230 return (RET_ERROR); 231 t->bt_rdata.size = t->bt_reclen; 232 } 233 data.data = t->bt_rdata.data; 234 data.size = t->bt_reclen; 235 236 sp = (u_char *)t->bt_cmap; 237 ep = (u_char *)t->bt_emap; 238 for (nrec = t->bt_nrecs; nrec < top; ++nrec) { 239 if (sp >= ep) { 240 F_SET(t, R_EOF); 241 return (RET_SPECIAL); 242 } 243 len = t->bt_reclen; 244 for (p = t->bt_rdata.data; 245 sp < ep && len > 0; *p++ = *sp++, --len); 246 if (len != 0) 247 memset(p, t->bt_bval, len); 248 if (__rec_iput(t, nrec, &data, 0) != RET_SUCCESS) 249 return (RET_ERROR); 250 } 251 t->bt_cmap = (caddr_t)sp; 252 return (RET_SUCCESS); 253 } 254 255 /* 256 * __REC_VMAP -- Get variable length records from a file. 257 * 258 * Parameters: 259 * t: tree 260 * cnt: records to read 261 * 262 * Returns: 263 * RET_ERROR, RET_SUCCESS 264 */ 265 int 266 __rec_vmap(BTREE *t, recno_t top) 267 { 268 DBT data; 269 u_char *sp, *ep; 270 recno_t nrec; 271 int bval; 272 273 sp = (u_char *)t->bt_cmap; 274 ep = (u_char *)t->bt_emap; 275 bval = t->bt_bval; 276 277 for (nrec = t->bt_nrecs; nrec < top; ++nrec) { 278 if (sp >= ep) { 279 F_SET(t, R_EOF); 280 return (RET_SPECIAL); 281 } 282 for (data.data = sp; sp < ep && *sp != bval; ++sp); 283 data.size = sp - (u_char *)data.data; 284 if (__rec_iput(t, nrec, &data, 0) != RET_SUCCESS) 285 return (RET_ERROR); 286 ++sp; 287 } 288 t->bt_cmap = (caddr_t)sp; 289 return (RET_SUCCESS); 290 } 291