2 * See the file LICENSE for redistribution information.
4 * Copyright (c) 1996, 1997, 1998
5 * Sleepycat Software. All rights reserved.
10 static const char sccsid
[] = "@(#)log_get.c 10.38 (Sleepycat) 10/3/98";
13 #ifndef NO_SYSTEM_INCLUDES
14 #include <sys/types.h>
26 #include "common_ext.h"
33 log_get(dblp
, alsn
, dbt
, flags
)
41 LOG_PANIC_CHECK(dblp
);
43 /* Validate arguments. */
44 if (flags
!= DB_CHECKPOINT
&& flags
!= DB_CURRENT
&&
45 flags
!= DB_FIRST
&& flags
!= DB_LAST
&&
46 flags
!= DB_NEXT
&& flags
!= DB_PREV
&& flags
!= DB_SET
)
47 return (__db_ferr(dblp
->dbenv
, "log_get", 1));
49 if (F_ISSET(dblp
, DB_AM_THREAD
)) {
50 if (flags
== DB_NEXT
|| flags
== DB_PREV
|| flags
== DB_CURRENT
)
51 return (__db_ferr(dblp
->dbenv
, "log_get", 1));
52 if (!F_ISSET(dbt
, DB_DBT_USERMEM
| DB_DBT_MALLOC
))
53 return (__db_ferr(dblp
->dbenv
, "threaded data", 1));
59 * If we get one of the log's header records, repeat the operation.
60 * This assumes that applications don't ever request the log header
61 * records by LSN, but that seems reasonable to me.
63 ret
= __log_get(dblp
, alsn
, dbt
, flags
, 0);
64 if (ret
== 0 && alsn
->offset
== 0) {
73 ret
= __log_get(dblp
, alsn
, dbt
, flags
, 0);
76 UNLOCK_LOGREGION(dblp
);
83 * Get a log record; internal version.
85 * PUBLIC: int __log_get __P((DB_LOG *, DB_LSN *, DBT *, u_int32_t, int));
88 __log_get(dblp
, alsn
, dbt
, flags
, silent
)
106 fail
= np
= tbuf
= NULL
;
111 nlsn
= lp
->chkpt_lsn
;
112 if (IS_ZERO_LSN(nlsn
)) {
113 __db_err(dblp
->dbenv
,
114 "log_get: unable to find checkpoint record: no checkpoint set.");
119 case DB_NEXT
: /* Next log record. */
120 if (!IS_ZERO_LSN(nlsn
)) {
121 /* Increment the cursor by the cursor record size. */
122 nlsn
.offset
+= dblp
->c_len
;
126 case DB_FIRST
: /* Find the first log record. */
127 /* Find the first log file. */
128 if ((ret
= __log_find(dblp
, 1, &cnt
)) != 0)
132 * We may have only entered records in the buffer, and not
133 * yet written a log file. If no log files were found and
134 * there's anything in the buffer, it belongs to file 1.
142 case DB_CURRENT
: /* Current log record. */
144 case DB_PREV
: /* Previous log record. */
145 if (!IS_ZERO_LSN(nlsn
)) {
146 /* If at start-of-file, move to the previous file. */
147 if (nlsn
.offset
== 0) {
148 if (nlsn
.file
== 1 ||
149 __log_valid(dblp
, nlsn
.file
- 1, 0) != 0)
150 return (DB_NOTFOUND
);
153 nlsn
.offset
= dblp
->c_off
;
155 nlsn
.offset
= dblp
->c_off
;
159 case DB_LAST
: /* Last log record. */
160 nlsn
.file
= lp
->lsn
.file
;
161 nlsn
.offset
= lp
->lsn
.offset
- lp
->len
;
163 case DB_SET
: /* Set log record. */
169 /* Return 1 if the request is past end-of-file. */
170 if (nlsn
.file
> lp
->lsn
.file
||
171 (nlsn
.file
== lp
->lsn
.file
&& nlsn
.offset
>= lp
->lsn
.offset
))
172 return (DB_NOTFOUND
);
174 /* If we've switched files, discard the current fd. */
175 if (dblp
->c_lsn
.file
!= nlsn
.file
&& dblp
->c_fd
!= -1) {
176 (void)__os_close(dblp
->c_fd
);
180 /* If the entire record is in the in-memory buffer, copy it out. */
181 if (nlsn
.file
== lp
->lsn
.file
&& nlsn
.offset
>= lp
->w_off
) {
182 /* Copy the header. */
183 p
= lp
->buf
+ (nlsn
.offset
- lp
->w_off
);
184 memcpy(&hdr
, p
, sizeof(HDR
));
186 /* Copy the record. */
187 len
= hdr
.len
- sizeof(HDR
);
188 if ((ret
= __db_retcopy(dbt
, (u_int8_t
*)p
+ sizeof(HDR
),
189 len
, &dblp
->c_dbt
.data
, &dblp
->c_dbt
.ulen
, NULL
)) != 0)
194 /* Acquire a file descriptor. */
195 if (dblp
->c_fd
== -1) {
196 if ((ret
= __log_name(dblp
, nlsn
.file
,
197 &np
, &dblp
->c_fd
, DB_RDONLY
| DB_SEQUENTIAL
)) != 0) {
205 /* Seek to the header offset and read the header. */
207 __os_seek(dblp
->c_fd
, 0, 0, nlsn
.offset
, 0, SEEK_SET
)) != 0) {
211 if ((ret
= __os_read(dblp
->c_fd
, &hdr
, sizeof(HDR
), &nr
)) != 0) {
215 if (nr
== sizeof(HDR
))
218 /* If read returns EOF, try the next file. */
220 if (flags
!= DB_NEXT
|| nlsn
.file
== lp
->lsn
.file
)
223 /* Move to the next file. */
230 * If read returns a short count the rest of the record has
231 * to be in the in-memory buffer.
233 if (lp
->b_off
< sizeof(HDR
) - nr
)
236 /* Get the rest of the header from the in-memory buffer. */
237 memcpy((u_int8_t
*)&hdr
+ nr
, lp
->buf
, sizeof(HDR
) - nr
);
238 shortp
= lp
->buf
+ (sizeof(HDR
) - nr
);
242 * Check for buffers of 0's, that's what we usually see during
243 * recovery, although it's certainly not something on which we
246 if (hdr
.len
<= sizeof(HDR
))
248 len
= hdr
.len
- sizeof(HDR
);
250 /* If we've already moved to the in-memory buffer, fill from there. */
251 if (shortp
!= NULL
) {
252 if (lp
->b_off
< ((u_int8_t
*)shortp
- lp
->buf
) + len
)
254 if ((ret
= __db_retcopy(dbt
, shortp
, len
,
255 &dblp
->c_dbt
.data
, &dblp
->c_dbt
.ulen
, NULL
)) != 0)
261 * Allocate temporary memory to hold the record.
264 * We're calling malloc(3) with a region locked. This isn't
267 if ((ret
= __os_malloc(len
, NULL
, &tbuf
)) != 0)
271 * Read the record into the buffer. If read returns a short count,
272 * there was an error or the rest of the record is in the in-memory
273 * buffer. Note, the information may be garbage if we're in recovery,
274 * so don't read past the end of the buffer's memory.
276 if ((ret
= __os_read(dblp
->c_fd
, tbuf
, len
, &nr
)) != 0) {
280 if (len
- nr
> sizeof(lp
->buf
))
282 if (nr
!= (ssize_t
)len
) {
283 if (lp
->b_off
< len
- nr
)
286 /* Get the rest of the record from the in-memory buffer. */
287 memcpy((u_int8_t
*)tbuf
+ nr
, lp
->buf
, len
- nr
);
290 /* Copy the record into the user's DBT. */
291 if ((ret
= __db_retcopy(dbt
, tbuf
, len
,
292 &dblp
->c_dbt
.data
, &dblp
->c_dbt
.ulen
, NULL
)) != 0)
297 cksum
: if (hdr
.cksum
!= __ham_func4(dbt
->data
, dbt
->size
)) {
299 __db_err(dblp
->dbenv
, "log_get: checksum mismatch");
303 /* Update the cursor and the return lsn. */
304 dblp
->c_off
= hdr
.prev
;
305 dblp
->c_len
= hdr
.len
;
306 dblp
->c_lsn
= *alsn
= nlsn
;
311 * This is the catchall -- for some reason we didn't find enough
312 * information or it wasn't reasonable information, and it wasn't
313 * because a system call failed.
320 __db_err(dblp
->dbenv
, "log_get: %s", strerror(ret
));
322 __db_err(dblp
->dbenv
,
323 "log_get: %s: %s", fail
, strerror(ret
));
325 err2
: if (np
!= NULL
)