1 /* $NetBSD: log.c,v 1.1.1.2 2008/05/18 14:29:46 aymeric Exp $ */
4 * Copyright (c) 1992, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
6 * Copyright (c) 1992, 1993, 1994, 1995, 1996
7 * Keith Bostic. All rights reserved.
9 * See the LICENSE file for redistribution information.
15 static const char sccsid
[] = "Id: log.c,v 10.26 2002/03/02 23:12:13 skimo Exp (Berkeley) Date: 2002/03/02 23:12:13";
18 #include <sys/types.h>
19 #include <sys/queue.h>
22 #include <bitstring.h>
31 #include "dbinternal.h"
34 * The log consists of records, each containing a type byte and a variable
35 * length byte string, as follows:
37 * LOG_CURSOR_INIT MARK
39 * LOG_LINE_APPEND_F db_recno_t char *
40 * LOG_LINE_APPEND_B db_recno_t char *
41 * LOG_LINE_DELETE_F db_recno_t char *
42 * LOG_LINE_DELETE_B db_recno_t char *
43 * LOG_LINE_RESET_F db_recno_t char *
44 * LOG_LINE_RESET_B db_recno_t char *
47 * We do before image physical logging. This means that the editor layer
48 * MAY NOT modify records in place, even if simply deleting or overwriting
49 * characters. Since the smallest unit of logging is a line, we're using
50 * up lots of space. This may eventually have to be reduced, probably by
51 * doing logical logging, which is a much cooler database phrase.
53 * The implementation of the historic vi 'u' command, using roll-forward and
54 * roll-back, is simple. Each set of changes has a LOG_CURSOR_INIT record,
55 * followed by a number of other records, followed by a LOG_CURSOR_END record.
56 * LOG_LINE_RESET records come in pairs. The first is a LOG_LINE_RESET_B
57 * record, and is the line before the change. The second is LOG_LINE_RESET_F,
58 * and is the line after the change. Roll-back is done by backing up to the
59 * first LOG_CURSOR_INIT record before a change. Roll-forward is done in a
62 * The 'U' command is implemented by rolling backward to a LOG_CURSOR_END
63 * record for a line different from the current one. It should be noted that
64 * this means that a subsequent 'u' command will make a change based on the
65 * new position of the log's cursor. This is okay, and, in fact, historic vi
69 static int vi_log_get
__P((SCR
*sp
, db_recno_t
*lnop
, size_t *size
));
70 static int log_cursor1
__P((SCR
*, int));
71 static void log_err
__P((SCR
*, const char *, int));
72 #if defined(DEBUG) && 0
73 static void log_trace
__P((SCR
*, const char *, db_recno_t
, u_char
*));
76 /* Try and restart the log on failure, i.e. if we run out of memory. */
78 log_err(sp, __FILE__, __LINE__); \
82 /* offset of CHAR_T string in log needs to be aligned on some systems
83 * because it is passed to db_set as a string
86 char data
[sizeof(u_char
) /* type */ + sizeof(db_recno_t
)];
89 #define CHAR_T_OFFSET ((char *)(((log_t*)0)->str) - (char *)0)
93 * Initialize the logging subsystem.
95 * PUBLIC: int log_init __P((SCR *, EXF *));
98 log_init(SCR
*sp
, EXF
*ep
)
102 * ep MAY NOT BE THE SAME AS sp->ep, DON'T USE THE LATTER.
104 * Initialize the buffer. The logging subsystem has its own
105 * buffers because the global ones are almost by definition
106 * going to be in use when the log runs.
110 ep
->l_cursor
.lno
= 1; /* XXX Any valid recno. */
111 ep
->l_cursor
.cno
= 0;
112 ep
->l_high
= ep
->l_cur
= 1;
114 if (db_create(&ep
->log
, 0, 0) != 0 ||
115 db_open(ep
->log
, NULL
, DB_RECNO
,
116 DB_CREATE
| VI_DB_THREAD
, S_IRUSR
| S_IWUSR
) != 0) {
117 msgq(sp
, M_SYSERR
, "009|Log file");
123 /*LOCK_INIT(sp->wp, ep);*/
130 * Close the logging subsystem.
132 * PUBLIC: int log_end __P((SCR *, EXF *));
135 log_end(SCR
*sp
, EXF
*ep
)
139 * ep MAY NOT BE THE SAME AS sp->ep, DON'T USE THE LATTER.
141 /*LOCK_END(sp->wp, ep);*/
142 if (ep
->log
!= NULL
) {
143 (void)(ep
->log
->close
)(ep
->log
,DB_NOSYNC
);
146 if (sp
->wp
->l_lp
!= NULL
) {
151 ep
->l_cursor
.lno
= 1; /* XXX Any valid recno. */
152 ep
->l_cursor
.cno
= 0;
153 ep
->l_high
= ep
->l_cur
= 1;
159 * Log the current cursor position, starting an event.
161 * PUBLIC: int log_cursor __P((SCR *));
169 if (F_ISSET(ep
, F_NOLOG
))
173 * If any changes were made since the last cursor init,
174 * put out the ending cursor record.
176 if (ep
->l_cursor
.lno
== OOBLNO
) {
177 if (ep
->l_win
&& ep
->l_win
!= sp
->wp
)
179 ep
->l_cursor
.lno
= sp
->lno
;
180 ep
->l_cursor
.cno
= sp
->cno
;
182 return (log_cursor1(sp
, LOG_CURSOR_END
));
184 ep
->l_cursor
.lno
= sp
->lno
;
185 ep
->l_cursor
.cno
= sp
->cno
;
191 * Actually push a cursor record out.
194 log_cursor1(SCR
*sp
, int type
)
202 if (type == LOG_CURSOR_INIT &&
203 LOCK_TRY(sp->wp, ep))
207 BINC_RETC(sp
, sp
->wp
->l_lp
, sp
->wp
->l_len
, sizeof(u_char
) + sizeof(MARK
));
208 sp
->wp
->l_lp
[0] = type
;
209 memmove(sp
->wp
->l_lp
+ sizeof(u_char
), &ep
->l_cursor
, sizeof(MARK
));
211 memset(&key
, 0, sizeof(key
));
212 key
.data
= &ep
->l_cur
;
213 key
.size
= sizeof(db_recno_t
);
214 memset(&data
, 0, sizeof(data
));
215 data
.data
= sp
->wp
->l_lp
;
216 data
.size
= sizeof(u_char
) + sizeof(MARK
);
217 if (ep
->log
->put(ep
->log
, NULL
, &key
, &data
, 0) == -1)
220 #if defined(DEBUG) && 0
221 vtrace(sp
, "%lu: %s: %u/%u\n", ep
->l_cur
,
222 type
== LOG_CURSOR_INIT
? "log_cursor_init" : "log_cursor_end",
225 /* Reset high water mark. */
226 ep
->l_high
= ++ep
->l_cur
;
229 if (type == LOG_CURSOR_END)
230 LOCK_UNLOCK(sp->wp, ep);
239 * PUBLIC: int log_line __P((SCR *, db_recno_t, u_int));
242 log_line(SCR
*sp
, db_recno_t lno
, u_int action
)
251 if (F_ISSET(ep
, F_NOLOG
))
257 * Kluge for vi. Clear the EXF undo flag so that the
258 * next 'u' command does a roll-back, regardless.
262 /* Put out one initial cursor record per set of changes. */
263 if (ep
->l_cursor
.lno
!= OOBLNO
) {
264 if (log_cursor1(sp
, LOG_CURSOR_INIT
))
266 ep
->l_cursor
.lno
= OOBLNO
;
268 } /*else if (ep->l_win != sp->wp) {
269 printf("log_line own: %p, this: %p\n", ep->l_win, sp->wp);
274 /* newly added for DB4 logging */
275 case LOG_LINE_APPEND_B
:
276 case LOG_LINE_DELETE_F
:
281 * Put out the changes. If it's a LOG_LINE_RESET_B call, it's a
282 * special case, avoid the caches. Also, if it fails and it's
283 * line 1, it just means that the user started with an empty file,
284 * so fake an empty length line.
286 if (action
== LOG_LINE_RESET_B
) {
287 if (db_get(sp
, lno
, DBG_NOCACHE
, &lp
, &len
)) {
288 static CHAR_T nul
= 0;
297 if (db_get(sp
, lno
, DBG_FATAL
, &lp
, &len
))
300 sp
->wp
->l_lp
, sp
->wp
->l_len
,
301 len
* sizeof(CHAR_T
) + CHAR_T_OFFSET
);
302 sp
->wp
->l_lp
[0] = action
;
303 memmove(sp
->wp
->l_lp
+ sizeof(u_char
), &lno
, sizeof(db_recno_t
));
304 MEMMOVEW(sp
->wp
->l_lp
+ CHAR_T_OFFSET
, lp
, len
);
307 memset(&key
, 0, sizeof(key
));
309 key
.size
= sizeof(db_recno_t
);
310 memset(&data
, 0, sizeof(data
));
311 data
.data
= sp
->wp
->l_lp
;
312 data
.size
= len
* sizeof(CHAR_T
) + CHAR_T_OFFSET
;
313 if (ep
->log
->put(ep
->log
, NULL
, &key
, &data
, 0) == -1)
316 #if defined(DEBUG) && 0
318 case LOG_LINE_APPEND_F
:
319 vtrace(sp
, "%u: log_line: append_f: %lu {%u}\n",
320 ep
->l_cur
, lno
, len
);
322 case LOG_LINE_APPEND_B
:
323 vtrace(sp
, "%u: log_line: append_b: %lu {%u}\n",
324 ep
->l_cur
, lno
, len
);
326 case LOG_LINE_DELETE_F
:
327 vtrace(sp
, "%lu: log_line: delete_f: %lu {%u}\n",
328 ep
->l_cur
, lno
, len
);
330 case LOG_LINE_DELETE_B
:
331 vtrace(sp
, "%lu: log_line: delete_b: %lu {%u}\n",
332 ep
->l_cur
, lno
, len
);
334 case LOG_LINE_RESET_F
:
335 vtrace(sp
, "%lu: log_line: reset_f: %lu {%u}\n",
336 ep
->l_cur
, lno
, len
);
338 case LOG_LINE_RESET_B
:
339 vtrace(sp
, "%lu: log_line: reset_b: %lu {%u}\n",
340 ep
->l_cur
, lno
, len
);
344 /* Reset high water mark. */
345 ep
->l_high
= ++ep
->l_cur
;
352 * Log a mark position. For the log to work, we assume that there
353 * aren't any operations that just put out a log record -- this
354 * would mean that undo operations would only reset marks, and not
355 * cause any other change.
357 * PUBLIC: int log_mark __P((SCR *, LMARK *));
360 log_mark(SCR
*sp
, LMARK
*lmp
)
366 if (F_ISSET(ep
, F_NOLOG
))
369 /* Put out one initial cursor record per set of changes. */
370 if (ep
->l_cursor
.lno
!= OOBLNO
) {
371 if (log_cursor1(sp
, LOG_CURSOR_INIT
))
373 ep
->l_cursor
.lno
= OOBLNO
;
377 BINC_RETC(sp
, sp
->wp
->l_lp
,
378 sp
->wp
->l_len
, sizeof(u_char
) + sizeof(LMARK
));
379 sp
->wp
->l_lp
[0] = LOG_MARK
;
380 memmove(sp
->wp
->l_lp
+ sizeof(u_char
), lmp
, sizeof(LMARK
));
382 memset(&key
, 0, sizeof(key
));
383 key
.data
= &ep
->l_cur
;
384 key
.size
= sizeof(db_recno_t
);
385 memset(&data
, 0, sizeof(data
));
386 data
.data
= sp
->wp
->l_lp
;
387 data
.size
= sizeof(u_char
) + sizeof(LMARK
);
388 if (ep
->log
->put(ep
->log
, NULL
, &key
, &data
, 0) == -1)
391 #if defined(DEBUG) && 0
392 vtrace(sp
, "%lu: mark %c: %lu/%u\n",
393 ep
->l_cur
, lmp
->name
, lmp
->lno
, lmp
->cno
);
395 /* Reset high water mark. */
396 ep
->l_high
= ++ep
->l_cur
;
402 * Get a line from the log in log buffer.
405 vi_log_get(SCR
*sp
, db_recno_t
*lnop
, size_t *size
)
415 BINC_RETC(sp
, sp
->wp
->l_lp
, sp
->wp
->l_len
, nlen
);
417 memset(&key
, 0, sizeof(key
));
418 key
.data
= lnop
; /* Initialize db request. */
419 key
.size
= sizeof(db_recno_t
);
420 memset(&data
, 0, sizeof(data
));
421 data
.data
= sp
->wp
->l_lp
;
422 data
.ulen
= sp
->wp
->l_len
;
423 data
.flags
= DB_DBT_USERMEM
;
424 switch (ep
->log
->get(ep
->log
, NULL
, &key
, &data
, 0)) {
438 * Roll the log backward one operation.
440 * PUBLIC: int log_backward __P((SCR *, MARK *));
443 log_backward(SCR
*sp
, MARK
*rp
)
454 if (F_ISSET(ep
, F_NOLOG
)) {
456 "010|Logging not being performed, undo not possible");
460 if (ep
->l_cur
== 1) {
461 msgq(sp
, M_BERR
, "011|No changes to undo");
465 if (ep
->l_win
&& ep
->l_win
!= sp
->wp
) {
466 ex_emsg(sp
, NULL
, EXM_LOCKED
);
472 F_SET(ep
, F_NOLOG
); /* Turn off logging. */
476 if (vi_log_get(sp
, &ep
->l_cur
, &size
))
478 #if defined(DEBUG) && 0
479 log_trace(sp
, "log_backward", ep
->l_cur
, data
.data
);
481 switch (*(p
= (u_char
*)sp
->wp
->l_lp
)) {
482 case LOG_CURSOR_INIT
:
484 memmove(rp
, p
+ sizeof(u_char
), sizeof(MARK
));
492 case LOG_LINE_APPEND_F
:
494 memmove(&lno
, p
+ sizeof(u_char
), sizeof(db_recno_t
));
495 if (db_delete(sp
, lno
))
497 ++sp
->rptlines
[L_DELETED
];
499 case LOG_LINE_DELETE_B
:
501 memmove(&lno
, p
+ sizeof(u_char
), sizeof(db_recno_t
));
502 if (db_insert(sp
, lno
,
503 (CHAR_T
*)(p
+ CHAR_T_OFFSET
),
504 (size
- CHAR_T_OFFSET
) / sizeof(CHAR_T
)))
506 ++sp
->rptlines
[L_ADDED
];
508 case LOG_LINE_RESET_F
:
510 case LOG_LINE_RESET_B
:
512 memmove(&lno
, p
+ sizeof(u_char
), sizeof(db_recno_t
));
514 (CHAR_T
*)(p
+ CHAR_T_OFFSET
),
515 (size
- CHAR_T_OFFSET
) / sizeof(CHAR_T
)))
517 if (sp
->rptlchange
!= lno
) {
518 sp
->rptlchange
= lno
;
519 ++sp
->rptlines
[L_CHANGED
];
524 memmove(&lm
, p
+ sizeof(u_char
), sizeof(LMARK
));
527 if (mark_set(sp
, lm
.name
, &m
, 0))
535 err
: F_CLR(ep
, F_NOLOG
);
542 * Reset the line to its original appearance.
545 * There's a bug in this code due to our not logging cursor movements
546 * unless a change was made. If you do a change, move off the line,
547 * then move back on and do a 'U', the line will be restored to the way
548 * it was before the original change.
550 * PUBLIC: int log_setline __P((SCR *));
563 if (F_ISSET(ep
, F_NOLOG
)) {
565 "012|Logging not being performed, undo not possible");
572 if (ep
->l_win
&& ep
->l_win
!= sp
->wp
) {
573 ex_emsg(sp
, NULL
, EXM_LOCKED
);
578 F_SET(ep
, F_NOLOG
); /* Turn off logging. */
582 if (vi_log_get(sp
, &ep
->l_cur
, &size
))
584 #if defined(DEBUG) && 0
585 log_trace(sp
, "log_setline", ep
->l_cur
, data
.data
);
587 switch (*(p
= (u_char
*)sp
->wp
->l_lp
)) {
588 case LOG_CURSOR_INIT
:
589 memmove(&m
, p
+ sizeof(u_char
), sizeof(MARK
));
590 if (m
.lno
!= sp
->lno
|| ep
->l_cur
== 1) {
597 memmove(&m
, p
+ sizeof(u_char
), sizeof(MARK
));
598 if (m
.lno
!= sp
->lno
) {
605 case LOG_LINE_APPEND_F
:
606 case LOG_LINE_DELETE_B
:
607 case LOG_LINE_RESET_F
:
609 case LOG_LINE_RESET_B
:
610 memmove(&lno
, p
+ sizeof(u_char
), sizeof(db_recno_t
));
611 if (lno
== sp
->lno
&&
612 db_set(sp
, lno
, (CHAR_T
*)(p
+ CHAR_T_OFFSET
),
613 (size
- CHAR_T_OFFSET
) / sizeof(CHAR_T
)))
615 if (sp
->rptlchange
!= lno
) {
616 sp
->rptlchange
= lno
;
617 ++sp
->rptlines
[L_CHANGED
];
620 memmove(&lm
, p
+ sizeof(u_char
), sizeof(LMARK
));
623 if (mark_set(sp
, lm
.name
, &m
, 0))
631 err
: F_CLR(ep
, F_NOLOG
);
638 * Roll the log forward one operation.
640 * PUBLIC: int log_forward __P((SCR *, MARK *));
643 log_forward(SCR
*sp
, MARK
*rp
)
654 if (F_ISSET(ep
, F_NOLOG
)) {
656 "013|Logging not being performed, roll-forward not possible");
660 if (ep
->l_cur
== ep
->l_high
) {
661 msgq(sp
, M_BERR
, "014|No changes to re-do");
665 if (ep
->l_win
&& ep
->l_win
!= sp
->wp
) {
666 ex_emsg(sp
, NULL
, EXM_LOCKED
);
671 F_SET(ep
, F_NOLOG
); /* Turn off logging. */
675 if (vi_log_get(sp
, &ep
->l_cur
, &size
))
677 #if defined(DEBUG) && 0
678 log_trace(sp
, "log_forward", ep
->l_cur
, data
.data
);
680 switch (*(p
= (u_char
*)sp
->wp
->l_lp
)) {
684 memmove(rp
, p
+ sizeof(u_char
), sizeof(MARK
));
690 case LOG_CURSOR_INIT
:
692 case LOG_LINE_APPEND_F
:
694 memmove(&lno
, p
+ sizeof(u_char
), sizeof(db_recno_t
));
695 if (db_insert(sp
, lno
,
696 (CHAR_T
*)(p
+ CHAR_T_OFFSET
),
697 (size
- CHAR_T_OFFSET
) / sizeof(CHAR_T
)))
699 ++sp
->rptlines
[L_ADDED
];
701 case LOG_LINE_DELETE_B
:
703 memmove(&lno
, p
+ sizeof(u_char
), sizeof(db_recno_t
));
704 if (db_delete(sp
, lno
))
706 ++sp
->rptlines
[L_DELETED
];
708 case LOG_LINE_RESET_B
:
710 case LOG_LINE_RESET_F
:
712 memmove(&lno
, p
+ sizeof(u_char
), sizeof(db_recno_t
));
714 (CHAR_T
*)(p
+ CHAR_T_OFFSET
),
715 (size
- CHAR_T_OFFSET
) / sizeof(CHAR_T
)))
717 if (sp
->rptlchange
!= lno
) {
718 sp
->rptlchange
= lno
;
719 ++sp
->rptlines
[L_CHANGED
];
724 memmove(&lm
, p
+ sizeof(u_char
), sizeof(LMARK
));
727 if (mark_set(sp
, lm
.name
, &m
, 0))
735 err
: F_CLR(ep
, F_NOLOG
);
742 * Try and restart the log on failure, i.e. if we run out of memory.
745 log_err(SCR
*sp
, const char *file
, int line
)
749 msgq(sp
, M_SYSERR
, "015|%s/%d: log put error", tail(file
), line
);
751 (void)ep
->log
->close(ep
->log
, DB_NOSYNC
);
752 if (!log_init(sp
, ep
))
753 msgq(sp
, M_ERR
, "267|Log restarted");
756 #if defined(DEBUG) && 0
758 log_trace(SCR
*sp
, const char *msg
, db_recno_t rno
, u_char
*p
)
765 case LOG_CURSOR_INIT
:
766 memmove(&m
, p
+ sizeof(u_char
), sizeof(MARK
));
767 vtrace(sp
, "%lu: %s: C_INIT: %u/%u\n", rno
, msg
, m
.lno
, m
.cno
);
770 memmove(&m
, p
+ sizeof(u_char
), sizeof(MARK
));
771 vtrace(sp
, "%lu: %s: C_END: %u/%u\n", rno
, msg
, m
.lno
, m
.cno
);
773 case LOG_LINE_APPEND_F
:
774 memmove(&lno
, p
+ sizeof(u_char
), sizeof(db_recno_t
));
775 vtrace(sp
, "%lu: %s: APPEND_F: %lu\n", rno
, msg
, lno
);
777 case LOG_LINE_APPEND_B
:
778 memmove(&lno
, p
+ sizeof(u_char
), sizeof(db_recno_t
));
779 vtrace(sp
, "%lu: %s: APPEND_B: %lu\n", rno
, msg
, lno
);
781 case LOG_LINE_DELETE_F
:
782 memmove(&lno
, p
+ sizeof(u_char
), sizeof(db_recno_t
));
783 vtrace(sp
, "%lu: %s: DELETE_F: %lu\n", rno
, msg
, lno
);
785 case LOG_LINE_DELETE_B
:
786 memmove(&lno
, p
+ sizeof(u_char
), sizeof(db_recno_t
));
787 vtrace(sp
, "%lu: %s: DELETE_B: %lu\n", rno
, msg
, lno
);
789 case LOG_LINE_RESET_F
:
790 memmove(&lno
, p
+ sizeof(u_char
), sizeof(db_recno_t
));
791 vtrace(sp
, "%lu: %s: RESET_F: %lu\n", rno
, msg
, lno
);
793 case LOG_LINE_RESET_B
:
794 memmove(&lno
, p
+ sizeof(u_char
), sizeof(db_recno_t
));
795 vtrace(sp
, "%lu: %s: RESET_B: %lu\n", rno
, msg
, lno
);
798 memmove(&lm
, p
+ sizeof(u_char
), sizeof(LMARK
));
800 "%lu: %s: MARK: %u/%u\n", rno
, msg
, lm
.lno
, lm
.cno
);