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