2 * See the file LICENSE for redistribution information.
4 * Copyright (c) 1996, 1997
5 * Sleepycat Software. All rights reserved.
10 static const char sccsid[] = "@(#)log_get.c 10.21 (Sleepycat) 10/25/97";
13 #ifndef NO_SYSTEM_INCLUDES
14 #include <sys/types.h>
28 #include "common_ext.h"
35 log_get(dblp, alsn, dbt, flags)
44 /* Validate arguments. */
45 #define OKFLAGS (DB_CHECKPOINT | \
46 DB_CURRENT | DB_FIRST | DB_LAST | DB_NEXT | DB_PREV | DB_SET)
49 __db_fchk(dblp->dbenv, "log_get", flags, OKFLAGS)) != 0)
62 return (__db_ferr(dblp->dbenv, "log_get", 1));
65 if (F_ISSET(dblp, DB_AM_THREAD)) {
66 if (LF_ISSET(DB_NEXT | DB_PREV | DB_CURRENT))
67 return (__db_ferr(dblp->dbenv, "log_get", 1));
68 if (!F_ISSET(dbt, DB_DBT_USERMEM | DB_DBT_MALLOC))
69 return (__db_ferr(dblp->dbenv, "threaded data", 1));
77 * If we get one of the log's header records, repeat the operation.
78 * This assumes that applications don't ever request the log header
79 * records by LSN, but that seems reasonable to me.
81 ret = __log_get(dblp, alsn, dbt, flags, 0);
82 if (ret == 0 && alsn->offset == 0) {
91 ret = __log_get(dblp, alsn, dbt, flags, 0);
94 UNLOCK_LOGREGION(dblp);
101 * Get a log record; internal version.
103 * PUBLIC: int __log_get __P((DB_LOG *, DB_LSN *, DBT *, int, int));
106 __log_get(dblp, alsn, dbt, flags, silent)
123 fail = np = tbuf = NULL;
128 nlsn = dblp->lp->c_lsn;
129 if (IS_ZERO_LSN(nlsn)) {
130 __db_err(dblp->dbenv,
131 "log_get: unable to find checkpoint record: no checkpoint set.");
136 case DB_NEXT: /* Next log record. */
137 if (!IS_ZERO_LSN(nlsn)) {
138 /* Increment the cursor by the cursor record size. */
139 nlsn.offset += dblp->c_len;
143 case DB_FIRST: /* Find the first log record. */
145 * Find any log file. Note, we may have only entered records
146 * in the buffer, and not yet written a log file.
148 if ((ret = __log_find(dblp, &cnt)) != 0) {
149 __db_err(dblp->dbenv,
150 "log_get: unable to find the first record: no log files found.");
154 /* If there's anything in the buffer, it belongs to file 1. */
158 /* Now go backwards to find the smallest one. */
159 for (; cnt > 1; --cnt)
160 if (__log_valid(dblp, NULL, cnt) != 0) {
167 case DB_CURRENT: /* Current log record. */
169 case DB_PREV: /* Previous log record. */
170 if (!IS_ZERO_LSN(nlsn)) {
171 /* If at start-of-file, move to the previous file. */
172 if (nlsn.offset == 0) {
173 if (nlsn.file == 1 ||
174 __log_valid(dblp, NULL, nlsn.file - 1) != 0)
175 return (DB_NOTFOUND);
178 nlsn.offset = dblp->c_off;
180 nlsn.offset = dblp->c_off;
184 case DB_LAST: /* Last log record. */
185 nlsn.file = lp->lsn.file;
186 nlsn.offset = lp->lsn.offset - lp->len;
188 case DB_SET: /* Set log record. */
194 /* Return 1 if the request is past end-of-file. */
195 if (nlsn.file > lp->lsn.file ||
196 (nlsn.file == lp->lsn.file && nlsn.offset >= lp->lsn.offset))
197 return (DB_NOTFOUND);
199 /* If we've switched files, discard the current fd. */
200 if (dblp->c_lsn.file != nlsn.file && dblp->c_fd != -1) {
201 (void)__db_close(dblp->c_fd);
205 /* If the entire record is in the in-memory buffer, copy it out. */
206 if (nlsn.file == lp->lsn.file && nlsn.offset >= lp->w_off) {
207 /* Copy the header. */
208 p = lp->buf + (nlsn.offset - lp->w_off);
209 memcpy(&hdr, p, sizeof(HDR));
211 /* Copy the record. */
212 len = hdr.len - sizeof(HDR);
213 if ((ret = __db_retcopy(dbt, (u_int8_t *)p + sizeof(HDR),
214 len, &dblp->c_dbt.data, &dblp->c_dbt.ulen, NULL)) != 0)
219 /* Acquire a file descriptor. */
220 if (dblp->c_fd == -1) {
221 if ((ret = __log_name(dblp, nlsn.file, &np)) != 0)
223 if ((ret = __db_open(np, DB_RDONLY | DB_SEQUENTIAL,
224 DB_RDONLY | DB_SEQUENTIAL, 0, &dblp->c_fd)) != 0) {
232 /* Seek to the header offset and read the header. */
233 if ((ret = __db_seek(dblp->c_fd, 0, 0, nlsn.offset, SEEK_SET)) != 0) {
237 if ((ret = __db_read(dblp->c_fd, &hdr, sizeof(HDR), &nr)) != 0) {
241 if (nr == sizeof(HDR))
244 /* If read returns EOF, try the next file. */
246 if (flags != DB_NEXT || nlsn.file == lp->lsn.file)
249 /* Move to the next file. */
256 * If read returns a short count the rest of the record has
257 * to be in the in-memory buffer.
259 if (lp->b_off < sizeof(HDR) - nr)
262 /* Get the rest of the header from the in-memory buffer. */
263 memcpy((u_int8_t *)&hdr + nr, lp->buf, sizeof(HDR) - nr);
264 shortp = lp->buf + (sizeof(HDR) - nr);
268 * Check for buffers of 0's, that's what we usually see during
269 * recovery, although it's certainly not something on which we
272 if (hdr.len <= sizeof(HDR))
274 len = hdr.len - sizeof(HDR);
276 /* If we've already moved to the in-memory buffer, fill from there. */
277 if (shortp != NULL) {
278 if (lp->b_off < ((u_int8_t *)shortp - lp->buf) + len)
280 if ((ret = __db_retcopy(dbt, shortp, len,
281 &dblp->c_dbt.data, &dblp->c_dbt.ulen, NULL)) != 0)
286 /* Allocate temporary memory to hold the record. */
287 if ((tbuf = (char *)__db_malloc(len)) == NULL) {
293 * Read the record into the buffer. If read returns a short count,
294 * there was an error or the rest of the record is in the in-memory
295 * buffer. Note, the information may be garbage if we're in recovery,
296 * so don't read past the end of the buffer's memory.
298 if ((ret = __db_read(dblp->c_fd, tbuf, len, &nr)) != 0) {
302 if (len - nr > sizeof(lp->buf))
304 if (nr != (ssize_t)len) {
305 if (lp->b_off < len - nr)
308 /* Get the rest of the record from the in-memory buffer. */
309 memcpy((u_int8_t *)tbuf + nr, lp->buf, len - nr);
312 /* Copy the record into the user's DBT. */
313 if ((ret = __db_retcopy(dbt, tbuf, len,
314 &dblp->c_dbt.data, &dblp->c_dbt.ulen, NULL)) != 0)
319 cksum: if (hdr.cksum != __ham_func4(dbt->data, dbt->size)) {
321 __db_err(dblp->dbenv, "log_get: checksum mismatch");
325 /* Update the cursor and the return lsn. */
326 dblp->c_off = hdr.prev;
327 dblp->c_len = hdr.len;
328 dblp->c_lsn = *alsn = nlsn;
333 * This is the catchall -- for some reason we didn't find enough
334 * information or it wasn't reasonable information, and it wasn't
335 * because a system call failed.
342 __db_err(dblp->dbenv, "log_get: %s", strerror(ret));
344 __db_err(dblp->dbenv,
345 "log_get: %s: %s", fail, strerror(ret));
346 err2: if (np != NULL)