17c478bd9Sstevel@tonic-gate /*-
27c478bd9Sstevel@tonic-gate  * Copyright (c) 1990, 1993, 1994
37c478bd9Sstevel@tonic-gate  *	The Regents of the University of California.  All rights reserved.
47c478bd9Sstevel@tonic-gate  *
57c478bd9Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
67c478bd9Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
77c478bd9Sstevel@tonic-gate  * are met:
87c478bd9Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
97c478bd9Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
107c478bd9Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
117c478bd9Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
127c478bd9Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
137c478bd9Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this software
147c478bd9Sstevel@tonic-gate  *    must display the following acknowledgement:
157c478bd9Sstevel@tonic-gate  *	This product includes software developed by the University of
167c478bd9Sstevel@tonic-gate  *	California, Berkeley and its contributors.
177c478bd9Sstevel@tonic-gate  * 4. Neither the name of the University nor the names of its contributors
187c478bd9Sstevel@tonic-gate  *    may be used to endorse or promote products derived from this software
197c478bd9Sstevel@tonic-gate  *    without specific prior written permission.
207c478bd9Sstevel@tonic-gate  *
217c478bd9Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
227c478bd9Sstevel@tonic-gate  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
237c478bd9Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
247c478bd9Sstevel@tonic-gate  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
257c478bd9Sstevel@tonic-gate  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
267c478bd9Sstevel@tonic-gate  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
277c478bd9Sstevel@tonic-gate  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
287c478bd9Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
297c478bd9Sstevel@tonic-gate  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
307c478bd9Sstevel@tonic-gate  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
317c478bd9Sstevel@tonic-gate  * SUCH DAMAGE.
327c478bd9Sstevel@tonic-gate  */
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate #if defined(LIBC_SCCS) && !defined(lint)
357c478bd9Sstevel@tonic-gate static char sccsid[] = "@(#)rec_get.c	8.9 (Berkeley) 8/18/94";
367c478bd9Sstevel@tonic-gate #endif /* LIBC_SCCS and not lint */
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate #include <sys/types.h>
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate #include <errno.h>
417c478bd9Sstevel@tonic-gate #include <stddef.h>
427c478bd9Sstevel@tonic-gate #include <stdio.h>
437c478bd9Sstevel@tonic-gate #include <stdlib.h>
447c478bd9Sstevel@tonic-gate #include <string.h>
457c478bd9Sstevel@tonic-gate #include <unistd.h>
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate #include "db-int.h"
487c478bd9Sstevel@tonic-gate #include "recno.h"
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate /*
517c478bd9Sstevel@tonic-gate  * __REC_GET -- Get a record from the btree.
527c478bd9Sstevel@tonic-gate  *
537c478bd9Sstevel@tonic-gate  * Parameters:
547c478bd9Sstevel@tonic-gate  *	dbp:	pointer to access method
557c478bd9Sstevel@tonic-gate  *	key:	key to find
567c478bd9Sstevel@tonic-gate  *	data:	data to return
577c478bd9Sstevel@tonic-gate  *	flag:	currently unused
587c478bd9Sstevel@tonic-gate  *
597c478bd9Sstevel@tonic-gate  * Returns:
607c478bd9Sstevel@tonic-gate  *	RET_ERROR, RET_SUCCESS and RET_SPECIAL if the key not found.
617c478bd9Sstevel@tonic-gate  */
627c478bd9Sstevel@tonic-gate int
__rec_get(dbp,key,data,flags)637c478bd9Sstevel@tonic-gate __rec_get(dbp, key, data, flags)
647c478bd9Sstevel@tonic-gate 	const DB *dbp;
657c478bd9Sstevel@tonic-gate 	const DBT *key;
667c478bd9Sstevel@tonic-gate 	DBT *data;
677c478bd9Sstevel@tonic-gate 	u_int flags;
687c478bd9Sstevel@tonic-gate {
697c478bd9Sstevel@tonic-gate 	BTREE *t;
707c478bd9Sstevel@tonic-gate 	EPG *e;
717c478bd9Sstevel@tonic-gate 	recno_t nrec;
727c478bd9Sstevel@tonic-gate 	int status;
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate 	t = dbp->internal;
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate 	/* Toss any page pinned across calls. */
777c478bd9Sstevel@tonic-gate 	if (t->bt_pinned != NULL) {
787c478bd9Sstevel@tonic-gate 		mpool_put(t->bt_mp, t->bt_pinned, 0);
797c478bd9Sstevel@tonic-gate 		t->bt_pinned = NULL;
807c478bd9Sstevel@tonic-gate 	}
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate 	/* Get currently doesn't take any flags, and keys of 0 are illegal. */
837c478bd9Sstevel@tonic-gate 	if (flags || (nrec = *(recno_t *)key->data) == 0) {
847c478bd9Sstevel@tonic-gate 		errno = EINVAL;
857c478bd9Sstevel@tonic-gate 		return (RET_ERROR);
867c478bd9Sstevel@tonic-gate 	}
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate 	/*
897c478bd9Sstevel@tonic-gate 	 * If we haven't seen this record yet, try to find it in the
907c478bd9Sstevel@tonic-gate 	 * original file.
917c478bd9Sstevel@tonic-gate 	 */
927c478bd9Sstevel@tonic-gate 	if (nrec > t->bt_nrecs) {
937c478bd9Sstevel@tonic-gate 		if (F_ISSET(t, R_EOF | R_INMEM))
947c478bd9Sstevel@tonic-gate 			return (RET_SPECIAL);
957c478bd9Sstevel@tonic-gate 		if ((status = t->bt_irec(t, nrec)) != RET_SUCCESS)
967c478bd9Sstevel@tonic-gate 			return (status);
977c478bd9Sstevel@tonic-gate 	}
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate 	--nrec;
1007c478bd9Sstevel@tonic-gate 	if ((e = __rec_search(t, nrec, SEARCH)) == NULL)
1017c478bd9Sstevel@tonic-gate 		return (RET_ERROR);
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate 	status = __rec_ret(t, e, 0, NULL, data);
1047c478bd9Sstevel@tonic-gate 	if (F_ISSET(t, B_DB_LOCK))
1057c478bd9Sstevel@tonic-gate 		mpool_put(t->bt_mp, e->page, 0);
1067c478bd9Sstevel@tonic-gate 	else
1077c478bd9Sstevel@tonic-gate 		t->bt_pinned = e->page;
1087c478bd9Sstevel@tonic-gate 	return (status);
1097c478bd9Sstevel@tonic-gate }
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate /*
1127c478bd9Sstevel@tonic-gate  * __REC_FPIPE -- Get fixed length records from a pipe.
1137c478bd9Sstevel@tonic-gate  *
1147c478bd9Sstevel@tonic-gate  * Parameters:
1157c478bd9Sstevel@tonic-gate  *	t:	tree
1167c478bd9Sstevel@tonic-gate  *	cnt:	records to read
1177c478bd9Sstevel@tonic-gate  *
1187c478bd9Sstevel@tonic-gate  * Returns:
1197c478bd9Sstevel@tonic-gate  *	RET_ERROR, RET_SUCCESS
1207c478bd9Sstevel@tonic-gate  */
1217c478bd9Sstevel@tonic-gate int
__rec_fpipe(t,top)1227c478bd9Sstevel@tonic-gate __rec_fpipe(t, top)
1237c478bd9Sstevel@tonic-gate 	BTREE *t;
1247c478bd9Sstevel@tonic-gate 	recno_t top;
1257c478bd9Sstevel@tonic-gate {
1267c478bd9Sstevel@tonic-gate 	DBT data;
1277c478bd9Sstevel@tonic-gate 	recno_t nrec;
1287c478bd9Sstevel@tonic-gate 	size_t len;
1297c478bd9Sstevel@tonic-gate 	int ch;
1307c478bd9Sstevel@tonic-gate 	u_char *p;
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate 	if (t->bt_rdata.size < t->bt_reclen) {
1337c478bd9Sstevel@tonic-gate 		t->bt_rdata.data = t->bt_rdata.data == NULL ?
1347c478bd9Sstevel@tonic-gate 		    malloc(t->bt_reclen) :
1357c478bd9Sstevel@tonic-gate 		    realloc(t->bt_rdata.data, t->bt_reclen);
1367c478bd9Sstevel@tonic-gate 		if (t->bt_rdata.data == NULL)
1377c478bd9Sstevel@tonic-gate 			return (RET_ERROR);
1387c478bd9Sstevel@tonic-gate 		t->bt_rdata.size = t->bt_reclen;
1397c478bd9Sstevel@tonic-gate 	}
1407c478bd9Sstevel@tonic-gate 	data.data = t->bt_rdata.data;
1417c478bd9Sstevel@tonic-gate 	data.size = t->bt_reclen;
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate 	for (nrec = t->bt_nrecs; nrec < top;) {
1447c478bd9Sstevel@tonic-gate 		len = t->bt_reclen;
1457c478bd9Sstevel@tonic-gate 		for (p = t->bt_rdata.data;; *p++ = ch)
1467c478bd9Sstevel@tonic-gate 			if ((ch = getc(t->bt_rfp)) == EOF || !--len) {
1477c478bd9Sstevel@tonic-gate 				if (ch != EOF)
1487c478bd9Sstevel@tonic-gate 					*p = ch;
1497c478bd9Sstevel@tonic-gate 				if (len != 0)
1507c478bd9Sstevel@tonic-gate 					memset(p, t->bt_bval, len);
1517c478bd9Sstevel@tonic-gate 				if (__rec_iput(t,
1527c478bd9Sstevel@tonic-gate 				    nrec, &data, 0) != RET_SUCCESS)
1537c478bd9Sstevel@tonic-gate 					return (RET_ERROR);
1547c478bd9Sstevel@tonic-gate 				++nrec;
1557c478bd9Sstevel@tonic-gate 				break;
1567c478bd9Sstevel@tonic-gate 			}
1577c478bd9Sstevel@tonic-gate 		if (ch == EOF)
1587c478bd9Sstevel@tonic-gate 			break;
1597c478bd9Sstevel@tonic-gate 	}
1607c478bd9Sstevel@tonic-gate 	if (nrec < top) {
1617c478bd9Sstevel@tonic-gate 		F_SET(t, R_EOF);
1627c478bd9Sstevel@tonic-gate 		return (RET_SPECIAL);
1637c478bd9Sstevel@tonic-gate 	}
1647c478bd9Sstevel@tonic-gate 	return (RET_SUCCESS);
1657c478bd9Sstevel@tonic-gate }
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate /*
1687c478bd9Sstevel@tonic-gate  * __REC_VPIPE -- Get variable length records from a pipe.
1697c478bd9Sstevel@tonic-gate  *
1707c478bd9Sstevel@tonic-gate  * Parameters:
1717c478bd9Sstevel@tonic-gate  *	t:	tree
1727c478bd9Sstevel@tonic-gate  *	cnt:	records to read
1737c478bd9Sstevel@tonic-gate  *
1747c478bd9Sstevel@tonic-gate  * Returns:
1757c478bd9Sstevel@tonic-gate  *	RET_ERROR, RET_SUCCESS
1767c478bd9Sstevel@tonic-gate  */
1777c478bd9Sstevel@tonic-gate int
__rec_vpipe(t,top)1787c478bd9Sstevel@tonic-gate __rec_vpipe(t, top)
1797c478bd9Sstevel@tonic-gate 	BTREE *t;
1807c478bd9Sstevel@tonic-gate 	recno_t top;
1817c478bd9Sstevel@tonic-gate {
1827c478bd9Sstevel@tonic-gate 	DBT data;
1837c478bd9Sstevel@tonic-gate 	recno_t nrec;
1847c478bd9Sstevel@tonic-gate 	indx_t len;
1857c478bd9Sstevel@tonic-gate 	size_t sz;
1867c478bd9Sstevel@tonic-gate 	int bval, ch;
1877c478bd9Sstevel@tonic-gate 	u_char *p;
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate 	bval = t->bt_bval;
1907c478bd9Sstevel@tonic-gate 	for (nrec = t->bt_nrecs; nrec < top; ++nrec) {
1917c478bd9Sstevel@tonic-gate 		for (p = t->bt_rdata.data,
1927c478bd9Sstevel@tonic-gate 		    sz = t->bt_rdata.size;; *p++ = ch, --sz) {
1937c478bd9Sstevel@tonic-gate 			if ((ch = getc(t->bt_rfp)) == EOF || ch == bval) {
1947c478bd9Sstevel@tonic-gate 				data.data = t->bt_rdata.data;
1957c478bd9Sstevel@tonic-gate 				data.size = p - (u_char *)t->bt_rdata.data;
1967c478bd9Sstevel@tonic-gate 				if (ch == EOF && data.size == 0)
1977c478bd9Sstevel@tonic-gate 					break;
1987c478bd9Sstevel@tonic-gate 				if (__rec_iput(t, nrec, &data, 0)
1997c478bd9Sstevel@tonic-gate 				    != RET_SUCCESS)
2007c478bd9Sstevel@tonic-gate 					return (RET_ERROR);
2017c478bd9Sstevel@tonic-gate 				break;
2027c478bd9Sstevel@tonic-gate 			}
2037c478bd9Sstevel@tonic-gate 			if (sz == 0) {
2047c478bd9Sstevel@tonic-gate 				len = p - (u_char *)t->bt_rdata.data;
2057c478bd9Sstevel@tonic-gate 				t->bt_rdata.size += (sz = 256);
2067c478bd9Sstevel@tonic-gate 				t->bt_rdata.data = t->bt_rdata.data == NULL ?
2077c478bd9Sstevel@tonic-gate 				    malloc(t->bt_rdata.size) :
2087c478bd9Sstevel@tonic-gate 				    realloc(t->bt_rdata.data, t->bt_rdata.size);
2097c478bd9Sstevel@tonic-gate 				if (t->bt_rdata.data == NULL)
2107c478bd9Sstevel@tonic-gate 					return (RET_ERROR);
2117c478bd9Sstevel@tonic-gate 				p = (u_char *)t->bt_rdata.data + len;
2127c478bd9Sstevel@tonic-gate 			}
2137c478bd9Sstevel@tonic-gate 		}
2147c478bd9Sstevel@tonic-gate 		if (ch == EOF)
2157c478bd9Sstevel@tonic-gate 			break;
2167c478bd9Sstevel@tonic-gate 	}
2177c478bd9Sstevel@tonic-gate 	if (nrec < top) {
2187c478bd9Sstevel@tonic-gate 		F_SET(t, R_EOF);
2197c478bd9Sstevel@tonic-gate 		return (RET_SPECIAL);
2207c478bd9Sstevel@tonic-gate 	}
2217c478bd9Sstevel@tonic-gate 	return (RET_SUCCESS);
2227c478bd9Sstevel@tonic-gate }
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate /*
2257c478bd9Sstevel@tonic-gate  * __REC_FMAP -- Get fixed length records from a file.
2267c478bd9Sstevel@tonic-gate  *
2277c478bd9Sstevel@tonic-gate  * Parameters:
2287c478bd9Sstevel@tonic-gate  *	t:	tree
2297c478bd9Sstevel@tonic-gate  *	cnt:	records to read
2307c478bd9Sstevel@tonic-gate  *
2317c478bd9Sstevel@tonic-gate  * Returns:
2327c478bd9Sstevel@tonic-gate  *	RET_ERROR, RET_SUCCESS
2337c478bd9Sstevel@tonic-gate  */
2347c478bd9Sstevel@tonic-gate int
__rec_fmap(t,top)2357c478bd9Sstevel@tonic-gate __rec_fmap(t, top)
2367c478bd9Sstevel@tonic-gate 	BTREE *t;
2377c478bd9Sstevel@tonic-gate 	recno_t top;
2387c478bd9Sstevel@tonic-gate {
2397c478bd9Sstevel@tonic-gate 	DBT data;
2407c478bd9Sstevel@tonic-gate 	recno_t nrec;
2417c478bd9Sstevel@tonic-gate 	u_char *sp, *ep, *p;
2427c478bd9Sstevel@tonic-gate 	size_t len;
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate 	if (t->bt_rdata.size < t->bt_reclen) {
2457c478bd9Sstevel@tonic-gate 		t->bt_rdata.data = t->bt_rdata.data == NULL ?
2467c478bd9Sstevel@tonic-gate 		    malloc(t->bt_reclen) :
2477c478bd9Sstevel@tonic-gate 		    realloc(t->bt_rdata.data, t->bt_reclen);
2487c478bd9Sstevel@tonic-gate 		if (t->bt_rdata.data == NULL)
2497c478bd9Sstevel@tonic-gate 			return (RET_ERROR);
2507c478bd9Sstevel@tonic-gate 		t->bt_rdata.size = t->bt_reclen;
2517c478bd9Sstevel@tonic-gate 	}
2527c478bd9Sstevel@tonic-gate 	data.data = t->bt_rdata.data;
2537c478bd9Sstevel@tonic-gate 	data.size = t->bt_reclen;
2547c478bd9Sstevel@tonic-gate 
2557c478bd9Sstevel@tonic-gate 	sp = (u_char *)t->bt_cmap;
2567c478bd9Sstevel@tonic-gate 	ep = (u_char *)t->bt_emap;
2577c478bd9Sstevel@tonic-gate 	for (nrec = t->bt_nrecs; nrec < top; ++nrec) {
2587c478bd9Sstevel@tonic-gate 		if (sp >= ep) {
2597c478bd9Sstevel@tonic-gate 			F_SET(t, R_EOF);
2607c478bd9Sstevel@tonic-gate 			return (RET_SPECIAL);
2617c478bd9Sstevel@tonic-gate 		}
2627c478bd9Sstevel@tonic-gate 		len = t->bt_reclen;
2637c478bd9Sstevel@tonic-gate 		for (p = t->bt_rdata.data;
2647c478bd9Sstevel@tonic-gate 		    sp < ep && len > 0; *p++ = *sp++, --len);
2657c478bd9Sstevel@tonic-gate 		if (len != 0)
2667c478bd9Sstevel@tonic-gate 			memset(p, t->bt_bval, len);
2677c478bd9Sstevel@tonic-gate 		if (__rec_iput(t, nrec, &data, 0) != RET_SUCCESS)
2687c478bd9Sstevel@tonic-gate 			return (RET_ERROR);
2697c478bd9Sstevel@tonic-gate 	}
2707c478bd9Sstevel@tonic-gate 	t->bt_cmap = (caddr_t)sp;
2717c478bd9Sstevel@tonic-gate 	return (RET_SUCCESS);
2727c478bd9Sstevel@tonic-gate }
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate /*
2757c478bd9Sstevel@tonic-gate  * __REC_VMAP -- Get variable length records from a file.
2767c478bd9Sstevel@tonic-gate  *
2777c478bd9Sstevel@tonic-gate  * Parameters:
2787c478bd9Sstevel@tonic-gate  *	t:	tree
2797c478bd9Sstevel@tonic-gate  *	cnt:	records to read
2807c478bd9Sstevel@tonic-gate  *
2817c478bd9Sstevel@tonic-gate  * Returns:
2827c478bd9Sstevel@tonic-gate  *	RET_ERROR, RET_SUCCESS
2837c478bd9Sstevel@tonic-gate  */
2847c478bd9Sstevel@tonic-gate int
__rec_vmap(t,top)2857c478bd9Sstevel@tonic-gate __rec_vmap(t, top)
2867c478bd9Sstevel@tonic-gate 	BTREE *t;
2877c478bd9Sstevel@tonic-gate 	recno_t top;
2887c478bd9Sstevel@tonic-gate {
2897c478bd9Sstevel@tonic-gate 	DBT data;
2907c478bd9Sstevel@tonic-gate 	u_char *sp, *ep;
2917c478bd9Sstevel@tonic-gate 	recno_t nrec;
2927c478bd9Sstevel@tonic-gate 	int bval;
2937c478bd9Sstevel@tonic-gate 
2947c478bd9Sstevel@tonic-gate 	sp = (u_char *)t->bt_cmap;
2957c478bd9Sstevel@tonic-gate 	ep = (u_char *)t->bt_emap;
2967c478bd9Sstevel@tonic-gate 	bval = t->bt_bval;
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate 	for (nrec = t->bt_nrecs; nrec < top; ++nrec) {
2997c478bd9Sstevel@tonic-gate 		if (sp >= ep) {
3007c478bd9Sstevel@tonic-gate 			F_SET(t, R_EOF);
3017c478bd9Sstevel@tonic-gate 			return (RET_SPECIAL);
3027c478bd9Sstevel@tonic-gate 		}
3037c478bd9Sstevel@tonic-gate 		for (data.data = sp; sp < ep && *sp != bval; ++sp);
3047c478bd9Sstevel@tonic-gate 		data.size = sp - (u_char *)data.data;
3057c478bd9Sstevel@tonic-gate 		if (__rec_iput(t, nrec, &data, 0) != RET_SUCCESS)
3067c478bd9Sstevel@tonic-gate 			return (RET_ERROR);
3077c478bd9Sstevel@tonic-gate 		++sp;
3087c478bd9Sstevel@tonic-gate 	}
3097c478bd9Sstevel@tonic-gate 	t->bt_cmap = (caddr_t)sp;
3107c478bd9Sstevel@tonic-gate 	return (RET_SUCCESS);
3117c478bd9Sstevel@tonic-gate }
312