xref: /netbsd/lib/libc/db/recno/rec_close.c (revision d3595ddf)
1*d3595ddfSjoerg /*	$NetBSD: rec_close.c,v 1.15 2008/09/11 12:58:00 joerg Exp $	*/
22c84ad3aScgd 
39f0aa214Scgd /*-
4738330daScgd  * Copyright (c) 1990, 1993, 1994
59f0aa214Scgd  *	The Regents of the University of California.  All rights reserved.
69f0aa214Scgd  *
79f0aa214Scgd  * Redistribution and use in source and binary forms, with or without
89f0aa214Scgd  * modification, are permitted provided that the following conditions
99f0aa214Scgd  * are met:
109f0aa214Scgd  * 1. Redistributions of source code must retain the above copyright
119f0aa214Scgd  *    notice, this list of conditions and the following disclaimer.
129f0aa214Scgd  * 2. Redistributions in binary form must reproduce the above copyright
139f0aa214Scgd  *    notice, this list of conditions and the following disclaimer in the
149f0aa214Scgd  *    documentation and/or other materials provided with the distribution.
15eb7c1594Sagc  * 3. Neither the name of the University nor the names of its contributors
169f0aa214Scgd  *    may be used to endorse or promote products derived from this software
179f0aa214Scgd  *    without specific prior written permission.
189f0aa214Scgd  *
199f0aa214Scgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
209f0aa214Scgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
219f0aa214Scgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
229f0aa214Scgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
239f0aa214Scgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
249f0aa214Scgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
259f0aa214Scgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
269f0aa214Scgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
279f0aa214Scgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
289f0aa214Scgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
299f0aa214Scgd  * SUCH DAMAGE.
309f0aa214Scgd  */
319f0aa214Scgd 
32*d3595ddfSjoerg #if HAVE_NBTOOL_CONFIG_H
33*d3595ddfSjoerg #include "nbtool_config.h"
34*d3595ddfSjoerg #endif
35*d3595ddfSjoerg 
3600ae392dSchristos #include <sys/cdefs.h>
37*d3595ddfSjoerg __RCSID("$NetBSD: rec_close.c,v 1.15 2008/09/11 12:58:00 joerg Exp $");
389f0aa214Scgd 
3943fa6fe3Sjtc #include "namespace.h"
409f0aa214Scgd #include <sys/types.h>
419f0aa214Scgd #include <sys/uio.h>
429f0aa214Scgd #include <sys/mman.h>
439f0aa214Scgd 
44cb9daf8fSchristos #include <assert.h>
459f0aa214Scgd #include <errno.h>
469f0aa214Scgd #include <limits.h>
479f0aa214Scgd #include <stdio.h>
489f0aa214Scgd #include <unistd.h>
499f0aa214Scgd 
509f0aa214Scgd #include <db.h>
519f0aa214Scgd #include "recno.h"
529f0aa214Scgd 
539f0aa214Scgd /*
549f0aa214Scgd  * __REC_CLOSE -- Close a recno tree.
559f0aa214Scgd  *
569f0aa214Scgd  * Parameters:
579f0aa214Scgd  *	dbp:	pointer to access method
589f0aa214Scgd  *
599f0aa214Scgd  * Returns:
609f0aa214Scgd  *	RET_ERROR, RET_SUCCESS
619f0aa214Scgd  */
629f0aa214Scgd int
__rec_close(DB * dbp)63cb9daf8fSchristos __rec_close(DB *dbp)
649f0aa214Scgd {
659f0aa214Scgd 	BTREE *t;
66a6d14e36Scgd 	int status;
679f0aa214Scgd 
6845e27c80Scgd 	t = dbp->internal;
6945e27c80Scgd 
7045e27c80Scgd 	/* Toss any page pinned across calls. */
7145e27c80Scgd 	if (t->bt_pinned != NULL) {
7245e27c80Scgd 		mpool_put(t->bt_mp, t->bt_pinned, 0);
7345e27c80Scgd 		t->bt_pinned = NULL;
7445e27c80Scgd 	}
7545e27c80Scgd 
769f0aa214Scgd 	if (__rec_sync(dbp, 0) == RET_ERROR)
779f0aa214Scgd 		return (RET_ERROR);
789f0aa214Scgd 
799f0aa214Scgd 	/* Committed to closing. */
80a6d14e36Scgd 	status = RET_SUCCESS;
81738330daScgd 	if (F_ISSET(t, R_MEMMAPPED) && munmap(t->bt_smap, t->bt_msize))
82a6d14e36Scgd 		status = RET_ERROR;
839f0aa214Scgd 
84e79648d0Sthorpej 	if (!F_ISSET(t, R_INMEM)) {
85738330daScgd 		if (F_ISSET(t, R_CLOSEFP)) {
869f0aa214Scgd 			if (fclose(t->bt_rfp))
87a6d14e36Scgd 				status = RET_ERROR;
88e79648d0Sthorpej 		} else {
899f0aa214Scgd 			if (close(t->bt_rfd))
90a6d14e36Scgd 				status = RET_ERROR;
91e79648d0Sthorpej 		}
92e79648d0Sthorpej 	}
939f0aa214Scgd 
949f0aa214Scgd 	if (__bt_close(dbp) == RET_ERROR)
95a6d14e36Scgd 		status = RET_ERROR;
969f0aa214Scgd 
97a6d14e36Scgd 	return (status);
989f0aa214Scgd }
999f0aa214Scgd 
1009f0aa214Scgd /*
1019f0aa214Scgd  * __REC_SYNC -- sync the recno tree to disk.
1029f0aa214Scgd  *
1039f0aa214Scgd  * Parameters:
1049f0aa214Scgd  *	dbp:	pointer to access method
1059f0aa214Scgd  *
1069f0aa214Scgd  * Returns:
1079f0aa214Scgd  *	RET_SUCCESS, RET_ERROR.
1089f0aa214Scgd  */
1099f0aa214Scgd int
__rec_sync(const DB * dbp,u_int flags)110cb9daf8fSchristos __rec_sync(const DB *dbp, u_int flags)
1119f0aa214Scgd {
1129f0aa214Scgd 	struct iovec iov[2];
1139f0aa214Scgd 	BTREE *t;
1149f0aa214Scgd 	DBT data, key;
1159f0aa214Scgd 	off_t off;
1169f0aa214Scgd 	recno_t scursor, trec;
1179f0aa214Scgd 	int status;
1189f0aa214Scgd 
1199f0aa214Scgd 	t = dbp->internal;
1209f0aa214Scgd 
12145e27c80Scgd 	/* Toss any page pinned across calls. */
12245e27c80Scgd 	if (t->bt_pinned != NULL) {
12345e27c80Scgd 		mpool_put(t->bt_mp, t->bt_pinned, 0);
12445e27c80Scgd 		t->bt_pinned = NULL;
12545e27c80Scgd 	}
12645e27c80Scgd 
1279f0aa214Scgd 	if (flags == R_RECNOSYNC)
1289f0aa214Scgd 		return (__bt_sync(dbp, 0));
1299f0aa214Scgd 
130738330daScgd 	if (F_ISSET(t, R_RDONLY | R_INMEM) || !F_ISSET(t, R_MODIFIED))
1319f0aa214Scgd 		return (RET_SUCCESS);
1329f0aa214Scgd 
1339f0aa214Scgd 	/* Read any remaining records into the tree. */
134738330daScgd 	if (!F_ISSET(t, R_EOF) && t->bt_irec(t, MAX_REC_NUMBER) == RET_ERROR)
1359f0aa214Scgd 		return (RET_ERROR);
1369f0aa214Scgd 
1379f0aa214Scgd 	/* Rewind the file descriptor. */
1389f0aa214Scgd 	if (lseek(t->bt_rfd, (off_t)0, SEEK_SET) != 0)
1399f0aa214Scgd 		return (RET_ERROR);
1409f0aa214Scgd 
141738330daScgd 	/* Save the cursor. */
142738330daScgd 	scursor = t->bt_cursor.rcursor;
1439f0aa214Scgd 
1449f0aa214Scgd 	key.size = sizeof(recno_t);
1459f0aa214Scgd 	key.data = &trec;
1469f0aa214Scgd 
147738330daScgd 	if (F_ISSET(t, R_FIXLEN)) {
148738330daScgd 		/*
149738330daScgd 		 * We assume that fixed length records are all fixed length.
150738330daScgd 		 * Any that aren't are either EINVAL'd or corrected by the
151738330daScgd 		 * record put code.
152738330daScgd 		 */
153738330daScgd 		status = (dbp->seq)(dbp, &key, &data, R_FIRST);
154738330daScgd 		while (status == RET_SUCCESS) {
155e5538ceaSthorpej 			if (write(t->bt_rfd, data.data, data.size) !=
156e5538ceaSthorpej 			    (ssize_t) data.size)
157738330daScgd 				return (RET_ERROR);
158738330daScgd 			status = (dbp->seq)(dbp, &key, &data, R_NEXT);
159738330daScgd 		}
160738330daScgd 	} else {
161738330daScgd 		iov[1].iov_base = &t->bt_bval;
162738330daScgd 		iov[1].iov_len = 1;
163738330daScgd 
1649f0aa214Scgd 		status = (dbp->seq)(dbp, &key, &data, R_FIRST);
1659f0aa214Scgd 		while (status == RET_SUCCESS) {
1669f0aa214Scgd 			iov[0].iov_base = data.data;
1679f0aa214Scgd 			iov[0].iov_len = data.size;
168e5538ceaSthorpej 			if (writev(t->bt_rfd, iov, 2) !=
169e5538ceaSthorpej 			    (ssize_t) (data.size + 1))
1709f0aa214Scgd 				return (RET_ERROR);
1719f0aa214Scgd 			status = (dbp->seq)(dbp, &key, &data, R_NEXT);
1729f0aa214Scgd 		}
173738330daScgd 	}
174738330daScgd 
175738330daScgd 	/* Restore the cursor. */
176738330daScgd 	t->bt_cursor.rcursor = scursor;
177738330daScgd 
1789f0aa214Scgd 	if (status == RET_ERROR)
1799f0aa214Scgd 		return (RET_ERROR);
1809f0aa214Scgd 	if ((off = lseek(t->bt_rfd, (off_t)0, SEEK_CUR)) == -1)
1819f0aa214Scgd 		return (RET_ERROR);
1829f0aa214Scgd 	if (ftruncate(t->bt_rfd, off))
1839f0aa214Scgd 		return (RET_ERROR);
184738330daScgd 	F_CLR(t, R_MODIFIED);
1859f0aa214Scgd 	return (RET_SUCCESS);
1869f0aa214Scgd }
187