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