1#pragma ident	"%Z%%M%	%I%	%E% SMI"
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#if defined(LIBC_SCCS) && !defined(lint)
37static char sccsid[] = "@(#)rec_close.c	8.9 (Berkeley) 11/18/94";
38#endif /* LIBC_SCCS and not lint */
39
40#include <sys/types.h>
41#include <sys/uio.h>
42#ifdef RECNO_USE_MMAP
43#include <sys/mman.h>
44#endif
45
46#include <errno.h>
47#include <limits.h>
48#include <stdio.h>
49#include <unistd.h>
50
51#include "db-int.h"
52#include "recno.h"
53
54/*
55 * __REC_CLOSE -- Close a recno tree.
56 *
57 * Parameters:
58 *	dbp:	pointer to access method
59 *
60 * Returns:
61 *	RET_ERROR, RET_SUCCESS
62 */
63int
64__rec_close(dbp)
65	DB *dbp;
66{
67	BTREE *t;
68	int status;
69
70	t = dbp->internal;
71
72	/* Toss any page pinned across calls. */
73	if (t->bt_pinned != NULL) {
74		mpool_put(t->bt_mp, t->bt_pinned, 0);
75		t->bt_pinned = NULL;
76	}
77
78	if (__rec_sync(dbp, 0) == RET_ERROR)
79		return (RET_ERROR);
80
81	/* Committed to closing. */
82	status = RET_SUCCESS;
83#ifdef	RECNO_USE_MMAP
84	if (F_ISSET(t, R_MEMMAPPED) && munmap(t->bt_smap, t->bt_msize))
85		status = RET_ERROR;
86#endif
87
88	if (!F_ISSET(t, R_INMEM)) {
89		if (F_ISSET(t, R_CLOSEFP)) {
90			if (fclose(t->bt_rfp))
91				status = RET_ERROR;
92		} else
93			if (close(t->bt_rfd))
94				status = RET_ERROR;
95	}
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 */
112int
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 = (char *)&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