xref: /openbsd-src/lib/libc/db/recno/rec_close.c (revision b2ea75c1b17e1a9a339660e7ed45cd24946b230e)
1 /*	$OpenBSD: rec_close.c,v 1.6 1999/02/15 05:11:25 millert 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 #if defined(LIBC_SCCS) && !defined(lint)
37 #if 0
38 static char sccsid[] = "@(#)rec_close.c	8.6 (Berkeley) 8/18/94";
39 #else
40 static char rcsid[] = "$OpenBSD: rec_close.c,v 1.6 1999/02/15 05:11:25 millert Exp $";
41 #endif
42 #endif /* LIBC_SCCS and not lint */
43 
44 #include <sys/types.h>
45 #include <sys/uio.h>
46 #include <sys/mman.h>
47 
48 #include <errno.h>
49 #include <limits.h>
50 #include <stdio.h>
51 #include <unistd.h>
52 
53 #include <db.h>
54 #include "recno.h"
55 
56 /*
57  * __REC_CLOSE -- Close a recno tree.
58  *
59  * Parameters:
60  *	dbp:	pointer to access method
61  *
62  * Returns:
63  *	RET_ERROR, RET_SUCCESS
64  */
65 int
66 __rec_close(dbp)
67 	DB *dbp;
68 {
69 	BTREE *t;
70 	int status;
71 
72 	t = dbp->internal;
73 
74 	/* Toss any page pinned across calls. */
75 	if (t->bt_pinned != NULL) {
76 		mpool_put(t->bt_mp, t->bt_pinned, 0);
77 		t->bt_pinned = NULL;
78 	}
79 
80 	if (__rec_sync(dbp, 0) == RET_ERROR)
81 		return (RET_ERROR);
82 
83 	/* Committed to closing. */
84 	status = RET_SUCCESS;
85 	if (F_ISSET(t, R_MEMMAPPED) && munmap(t->bt_smap, t->bt_msize))
86 		status = RET_ERROR;
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 	if (__bt_close(dbp) == RET_ERROR)
97 		status = RET_ERROR;
98 
99 	return (status);
100 }
101 
102 /*
103  * __REC_SYNC -- sync the recno tree to disk.
104  *
105  * Parameters:
106  *	dbp:	pointer to access method
107  *
108  * Returns:
109  *	RET_SUCCESS, RET_ERROR.
110  */
111 int
112 __rec_sync(dbp, flags)
113 	const DB *dbp;
114 	u_int flags;
115 {
116 	struct iovec iov[2];
117 	BTREE *t;
118 	DBT data, key;
119 	off_t off;
120 	recno_t scursor, trec;
121 	int status;
122 
123 	t = dbp->internal;
124 
125 	/* Toss any page pinned across calls. */
126 	if (t->bt_pinned != NULL) {
127 		mpool_put(t->bt_mp, t->bt_pinned, 0);
128 		t->bt_pinned = NULL;
129 	}
130 
131 	if (flags == R_RECNOSYNC)
132 		return (__bt_sync(dbp, 0));
133 
134 	if (F_ISSET(t, R_RDONLY | R_INMEM) || !F_ISSET(t, R_MODIFIED))
135 		return (RET_SUCCESS);
136 
137 	/* Read any remaining records into the tree. */
138 	if (!F_ISSET(t, R_EOF) && t->bt_irec(t, MAX_REC_NUMBER) == RET_ERROR)
139 		return (RET_ERROR);
140 
141 	/* Rewind the file descriptor. */
142 	if (lseek(t->bt_rfd, (off_t)0, SEEK_SET) != 0)
143 		return (RET_ERROR);
144 
145 	/* Save the cursor. */
146 	scursor = t->bt_cursor.rcursor;
147 
148 	key.size = sizeof(recno_t);
149 	key.data = &trec;
150 
151 	if (F_ISSET(t, R_FIXLEN)) {
152 		/*
153 		 * We assume that fixed length records are all fixed length.
154 		 * Any that aren't are either EINVAL'd or corrected by the
155 		 * record put code.
156 		 */
157 		status = (dbp->seq)(dbp, &key, &data, R_FIRST);
158 		while (status == RET_SUCCESS) {
159 			if (write(t->bt_rfd, data.data, data.size) != data.size)
160 				return (RET_ERROR);
161 			status = (dbp->seq)(dbp, &key, &data, R_NEXT);
162 		}
163 	} else {
164 		iov[1].iov_base = (void *) &t->bt_bval;
165 		iov[1].iov_len = 1;
166 
167 		status = (dbp->seq)(dbp, &key, &data, R_FIRST);
168 		while (status == RET_SUCCESS) {
169 			iov[0].iov_base = data.data;
170 			iov[0].iov_len = data.size;
171 			if (writev(t->bt_rfd, iov, 2) != data.size + 1)
172 				return (RET_ERROR);
173 			status = (dbp->seq)(dbp, &key, &data, R_NEXT);
174 		}
175 	}
176 
177 	/* Restore the cursor. */
178 	t->bt_cursor.rcursor = scursor;
179 
180 	if (status == RET_ERROR)
181 		return (RET_ERROR);
182 	if ((off = lseek(t->bt_rfd, (off_t)0, SEEK_CUR)) == -1)
183 		return (RET_ERROR);
184 	if (ftruncate(t->bt_rfd, off))
185 		return (RET_ERROR);
186 	F_CLR(t, R_MODIFIED);
187 	return (RET_SUCCESS);
188 }
189