| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
#if defined(LIBC_SCCS) && !defined(lint) |
| 35 |
static char sccsid[] = "@(#)rec_close.c 8.6 (Berkeley) 8/18/94"; |
| 36 |
#endif |
| 37 |
|
| 38 |
#include <sys/types.h> |
| 39 |
#include <sys/uio.h> |
| 40 |
#include <sys/mman.h> |
| 41 |
|
| 42 |
#include <errno.h> |
| 43 |
#include <limits.h> |
| 44 |
#include <stdio.h> |
| 45 |
#include <unistd.h> |
| 46 |
|
| 47 |
#include "../include/db.h" |
| 48 |
#include "recno.h" |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
int |
| 60 |
__rec_close(dbp) |
| 61 |
DB *dbp; |
| 62 |
{ |
| 63 |
BTREE *t; |
| 64 |
int status; |
| 65 |
|
| 66 |
t = dbp->internal; |
| 67 |
|
| 68 |
|
| 69 |
if (t->bt_pinned != NULL) { |
| 70 |
mpool_put(t->bt_mp, t->bt_pinned, 0); |
| 71 |
t->bt_pinned = NULL; |
| 72 |
} |
| 73 |
|
| 74 |
if (__rec_sync(dbp, 0) == RET_ERROR) |
| 75 |
return (RET_ERROR); |
| 76 |
|
| 77 |
|
| 78 |
status = RET_SUCCESS; |
| 79 |
if (F_ISSET(t, R_MEMMAPPED) && munmap(t->bt_smap, t->bt_msize)) |
| 80 |
status = RET_ERROR; |
| 81 |
|
| 82 |
if (!F_ISSET(t, R_INMEM)) { |
| 83 |
if (F_ISSET(t, R_CLOSEFP)) { |
| 84 |
if (fclose(t->bt_rfp)) |
| 85 |
status = RET_ERROR; |
| 86 |
} else |
| 87 |
if (close(t->bt_rfd)) |
| 88 |
status = RET_ERROR; |
| 89 |
} |
| 90 |
|
| 91 |
if (__bt_close(dbp) == RET_ERROR) |
| 92 |
status = RET_ERROR; |
| 93 |
|
| 94 |
return (status); |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
int |
| 107 |
__rec_sync(dbp, flags) |
| 108 |
const DB *dbp; |
| 109 |
u_int flags; |
| 110 |
{ |
| 111 |
struct iovec iov[2]; |
| 112 |
BTREE *t; |
| 113 |
DBT data, key; |
| 114 |
off_t off; |
| 115 |
recno_t scursor, trec; |
| 116 |
int status; |
| 117 |
|
| 118 |
t = dbp->internal; |
| 119 |
|
| 120 |
|
| 121 |
if (t->bt_pinned != NULL) { |
| 122 |
mpool_put(t->bt_mp, t->bt_pinned, 0); |
| 123 |
t->bt_pinned = NULL; |
| 124 |
} |
| 125 |
|
| 126 |
if (flags == R_RECNOSYNC) |
| 127 |
return (__bt_sync(dbp, 0)); |
| 128 |
|
| 129 |
if (F_ISSET(t, R_RDONLY | R_INMEM) || !F_ISSET(t, R_MODIFIED)) |
| 130 |
return (RET_SUCCESS); |
| 131 |
|
| 132 |
|
| 133 |
if (!F_ISSET(t, R_EOF) && t->bt_irec(t, MAX_REC_NUMBER) == RET_ERROR) |
| 134 |
return (RET_ERROR); |
| 135 |
|
| 136 |
|
| 137 |
if (lseek(t->bt_rfd, (off_t)0, SEEK_SET) != 0) |
| 138 |
return (RET_ERROR); |
| 139 |
|
| 140 |
|
| 141 |
scursor = t->bt_cursor.rcursor; |
| 142 |
|
| 143 |
key.size = sizeof(recno_t); |
| 144 |
key.data = &trec; |
| 145 |
|
| 146 |
if (F_ISSET(t, R_FIXLEN)) { |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
status = (dbp->seq)(dbp, &key, &data, R_FIRST); |
| 153 |
while (status == RET_SUCCESS) { |
| 154 |
if ((size_t) write(t->bt_rfd, data.data, data.size) != data.size) |
| 155 |
return (RET_ERROR); |
| 156 |
status = (dbp->seq)(dbp, &key, &data, R_NEXT); |
| 157 |
} |
| 158 |
} else { |
| 159 |
iov[1].iov_base = &t->bt_bval; |
| 160 |
iov[1].iov_len = 1; |
| 161 |
|
| 162 |
status = (dbp->seq)(dbp, &key, &data, R_FIRST); |
| 163 |
while (status == RET_SUCCESS) { |
| 164 |
iov[0].iov_base = data.data; |
| 165 |
iov[0].iov_len = data.size; |
| 166 |
if ((size_t) writev(t->bt_rfd, iov, 2) != data.size + 1) |
| 167 |
return (RET_ERROR); |
| 168 |
status = (dbp->seq)(dbp, &key, &data, R_NEXT); |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
t->bt_cursor.rcursor = scursor; |
| 174 |
|
| 175 |
if (status == RET_ERROR) |
| 176 |
return (RET_ERROR); |
| 177 |
if ((off = lseek(t->bt_rfd, (off_t)0, SEEK_CUR)) == -1) |
| 178 |
return (RET_ERROR); |
| 179 |
if (ftruncate(t->bt_rfd, off)) |
| 180 |
return (RET_ERROR); |
| 181 |
F_CLR(t, R_MODIFIED); |
| 182 |
return (RET_SUCCESS); |
| 183 |
} |