Home | History | Annotate | Line # | Download | only in recno
rec_close.c revision 1.8
      1 /*	$NetBSD: rec_close.c,v 1.8 1997/07/13 18:52:10 christos Exp $	*/
      2 
      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. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  */
     35 
     36 #include <sys/cdefs.h>
     37 #if defined(LIBC_SCCS) && !defined(lint)
     38 #if 0
     39 static char sccsid[] = "@(#)rec_close.c	8.6 (Berkeley) 8/18/94";
     40 #else
     41 __RCSID("$NetBSD: rec_close.c,v 1.8 1997/07/13 18:52:10 christos Exp $");
     42 #endif
     43 #endif /* LIBC_SCCS and not lint */
     44 
     45 #include <sys/types.h>
     46 #include <sys/uio.h>
     47 #include <sys/mman.h>
     48 
     49 #include <errno.h>
     50 #include <limits.h>
     51 #include <stdio.h>
     52 #include <unistd.h>
     53 
     54 #include <db.h>
     55 #include "recno.h"
     56 
     57 /*
     58  * __REC_CLOSE -- Close a recno tree.
     59  *
     60  * Parameters:
     61  *	dbp:	pointer to access method
     62  *
     63  * Returns:
     64  *	RET_ERROR, RET_SUCCESS
     65  */
     66 int
     67 __rec_close(dbp)
     68 	DB *dbp;
     69 {
     70 	BTREE *t;
     71 	int status;
     72 
     73 	t = dbp->internal;
     74 
     75 	/* Toss any page pinned across calls. */
     76 	if (t->bt_pinned != NULL) {
     77 		mpool_put(t->bt_mp, t->bt_pinned, 0);
     78 		t->bt_pinned = NULL;
     79 	}
     80 
     81 	if (__rec_sync(dbp, 0) == RET_ERROR)
     82 		return (RET_ERROR);
     83 
     84 	/* Committed to closing. */
     85 	status = RET_SUCCESS;
     86 	if (F_ISSET(t, R_MEMMAPPED) && munmap(t->bt_smap, t->bt_msize))
     87 		status = RET_ERROR;
     88 
     89 	if (!F_ISSET(t, R_INMEM))
     90 		if (F_ISSET(t, R_CLOSEFP)) {
     91 			if (fclose(t->bt_rfp))
     92 				status = RET_ERROR;
     93 		} else
     94 			if (close(t->bt_rfd))
     95 				status = RET_ERROR;
     96 
     97 	if (__bt_close(dbp) == RET_ERROR)
     98 		status = RET_ERROR;
     99 
    100 	return (status);
    101 }
    102 
    103 /*
    104  * __REC_SYNC -- sync the recno tree to disk.
    105  *
    106  * Parameters:
    107  *	dbp:	pointer to access method
    108  *
    109  * Returns:
    110  *	RET_SUCCESS, RET_ERROR.
    111  */
    112 int
    113 __rec_sync(dbp, flags)
    114 	const DB *dbp;
    115 	u_int flags;
    116 {
    117 	struct iovec iov[2];
    118 	BTREE *t;
    119 	DBT data, key;
    120 	off_t off;
    121 	recno_t scursor, trec;
    122 	int status;
    123 
    124 	t = dbp->internal;
    125 
    126 	/* Toss any page pinned across calls. */
    127 	if (t->bt_pinned != NULL) {
    128 		mpool_put(t->bt_mp, t->bt_pinned, 0);
    129 		t->bt_pinned = NULL;
    130 	}
    131 
    132 	if (flags == R_RECNOSYNC)
    133 		return (__bt_sync(dbp, 0));
    134 
    135 	if (F_ISSET(t, R_RDONLY | R_INMEM) || !F_ISSET(t, R_MODIFIED))
    136 		return (RET_SUCCESS);
    137 
    138 	/* Read any remaining records into the tree. */
    139 	if (!F_ISSET(t, R_EOF) && t->bt_irec(t, MAX_REC_NUMBER) == RET_ERROR)
    140 		return (RET_ERROR);
    141 
    142 	/* Rewind the file descriptor. */
    143 	if (lseek(t->bt_rfd, (off_t)0, SEEK_SET) != 0)
    144 		return (RET_ERROR);
    145 
    146 	/* Save the cursor. */
    147 	scursor = t->bt_cursor.rcursor;
    148 
    149 	key.size = sizeof(recno_t);
    150 	key.data = &trec;
    151 
    152 	if (F_ISSET(t, R_FIXLEN)) {
    153 		/*
    154 		 * We assume that fixed length records are all fixed length.
    155 		 * Any that aren't are either EINVAL'd or corrected by the
    156 		 * record put code.
    157 		 */
    158 		status = (dbp->seq)(dbp, &key, &data, R_FIRST);
    159 		while (status == RET_SUCCESS) {
    160 			if (write(t->bt_rfd, data.data, data.size) != data.size)
    161 				return (RET_ERROR);
    162 			status = (dbp->seq)(dbp, &key, &data, R_NEXT);
    163 		}
    164 	} else {
    165 		iov[1].iov_base = &t->bt_bval;
    166 		iov[1].iov_len = 1;
    167 
    168 		status = (dbp->seq)(dbp, &key, &data, R_FIRST);
    169 		while (status == RET_SUCCESS) {
    170 			iov[0].iov_base = data.data;
    171 			iov[0].iov_len = data.size;
    172 			if (writev(t->bt_rfd, iov, 2) != data.size + 1)
    173 				return (RET_ERROR);
    174 			status = (dbp->seq)(dbp, &key, &data, R_NEXT);
    175 		}
    176 	}
    177 
    178 	/* Restore the cursor. */
    179 	t->bt_cursor.rcursor = scursor;
    180 
    181 	if (status == RET_ERROR)
    182 		return (RET_ERROR);
    183 	if ((off = lseek(t->bt_rfd, (off_t)0, SEEK_CUR)) == -1)
    184 		return (RET_ERROR);
    185 	if (ftruncate(t->bt_rfd, off))
    186 		return (RET_ERROR);
    187 	F_CLR(t, R_MODIFIED);
    188 	return (RET_SUCCESS);
    189 }
    190