1 /* $NetBSD: log.c,v 1.2 2013/11/22 15:52:05 christos Exp $ */
3 * Copyright (c) 1992, 1993, 1994
4 * The Regents of the University of California. All rights reserved.
5 * Copyright (c) 1992, 1993, 1994, 1995, 1996
6 * Keith Bostic. All rights reserved.
8 * See the LICENSE file for redistribution information.
14 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 ";
17 #include <sys/types.h>
18 #include <sys/queue.h>
21 #include <bitstring.h>
30 #include "dbinternal.h"
33 * The log consists of records, each containing a type byte and a variable
34 * length byte string, as follows:
36 * LOG_CURSOR_INIT MARK
38 * LOG_LINE_APPEND_F db_recno_t char *
39 * LOG_LINE_APPEND_B db_recno_t char *
40 * LOG_LINE_DELETE_F db_recno_t char *
41 * LOG_LINE_DELETE_B db_recno_t char *
42 * LOG_LINE_RESET_F db_recno_t char *
43 * LOG_LINE_RESET_B db_recno_t char *
46 * We do before image physical logging. This means that the editor layer
47 * MAY NOT modify records in place, even if simply deleting or overwriting
48 * characters. Since the smallest unit of logging is a line, we're using
49 * up lots of space. This may eventually have to be reduced, probably by
50 * doing logical logging, which is a much cooler database phrase.
52 * The implementation of the historic vi 'u' command, using roll-forward and
53 * roll-back, is simple. Each set of changes has a LOG_CURSOR_INIT record,
54 * followed by a number of other records, followed by a LOG_CURSOR_END record.
55 * LOG_LINE_RESET records come in pairs. The first is a LOG_LINE_RESET_B
56 * record, and is the line before the change. The second is LOG_LINE_RESET_F,
57 * and is the line after the change. Roll-back is done by backing up to the
58 * first LOG_CURSOR_INIT record before a change. Roll-forward is done in a
61 * The 'U' command is implemented by rolling backward to a LOG_CURSOR_END
62 * record for a line different from the current one. It should be noted that
63 * this means that a subsequent 'u' command will make a change based on the
64 * new position of the log's cursor. This is okay, and, in fact, historic vi
68 static int vi_log_get
__P((SCR
*sp
, db_recno_t
*lnop
, size_t *size
));
69 static int log_cursor1
__P((SCR
*, int));
70 static void log_err
__P((SCR
*, const char *, int));
71 #if defined(DEBUG) && 0
72 static void log_trace
__P((SCR
*, const char *, db_recno_t
, u_char
*));
75 /* Try and restart the log on failure, i.e. if we run out of memory. */
77 log_err(sp, __FILE__, __LINE__); \
81 /* offset of CHAR_T string in log needs to be aligned on some systems
82 * because it is passed to db_set as a string
85 char data
[sizeof(u_char
) /* type */ + sizeof(db_recno_t
)];
88 #define CHAR_T_OFFSET ((char *)(((log_t*)0)->str) - (char *)0)
92 * Initialize the logging subsystem.
94 * PUBLIC: int log_init __P((SCR *, EXF *));
97 log_init(SCR
*sp
, EXF
*ep
)
101 * ep MAY NOT BE THE SAME AS sp->ep, DON'T USE THE LATTER.
103 * Initialize the buffer. The logging subsystem has its own
104 * buffers because the global ones are almost by definition
105 * going to be in use when the log runs.
109 ep
->l_cursor
.lno
= 1; /* XXX Any valid recno. */
110 ep
->l_cursor
.cno
= 0;
111 ep
->l_high
= ep
->l_cur
= 1;
113 if (db_create(&ep
->log
, 0, 0) != 0 ||
114 db_open(ep
->log
, NULL
, DB_RECNO
,
115 DB_CREATE
| VI_DB_THREAD
, S_IRUSR
| S_IWUSR
) != 0) {
116 msgq(sp
, M_SYSERR
, "009|Log file");
122 /*LOCK_INIT(sp->wp, ep);*/
129 * Close the logging subsystem.
131 * PUBLIC: int log_end __P((SCR *, EXF *));
134 log_end(SCR
*sp
, EXF
*ep
)
138 * ep MAY NOT BE THE SAME AS sp->ep, DON'T USE THE LATTER.
140 /*LOCK_END(sp->wp, ep);*/
141 if (ep
->log
!= NULL
) {
142 (void)(ep
->log
->close
)(ep
->log
, DB_NOSYNC
);
145 if (sp
->wp
->l_lp
!= NULL
) {
150 ep
->l_cursor
.lno
= 1; /* XXX Any valid recno. */
151 ep
->l_cursor
.cno
= 0;
152 ep
->l_high
= ep
->l_cur
= 1;
158 * Log the current cursor position, starting an event.
160 * PUBLIC: int log_cursor __P((SCR *));
168 if (F_ISSET(ep
, F_NOLOG
))
172 * If any changes were made since the last cursor init,
173 * put out the ending cursor record.
175 if (ep
->l_cursor
.lno
== OOBLNO
) {
176 if (ep
->l_win
&& ep
->l_win
!= sp
->wp
)
178 ep
->l_cursor
.lno
= sp
->lno
;
179 ep
->l_cursor
.cno
= sp
->cno
;
181 return (log_cursor1(sp
, LOG_CURSOR_END
));
183 ep
->l_cursor
.lno
= sp
->lno
;
184 ep
->l_cursor
.cno
= sp
->cno
;
190 * Actually push a cursor record out.
193 log_cursor1(SCR
*sp
, int type
)
201 if (type == LOG_CURSOR_INIT &&
202 LOCK_TRY(sp->wp, ep))
206 BINC_RETC(sp
, sp
->wp
->l_lp
, sp
->wp
->l_len
, sizeof(u_char
) + sizeof(MARK
));
207 sp
->wp
->l_lp
[0] = type
;
208 memmove(sp
->wp
->l_lp
+ sizeof(u_char
), &ep
->l_cursor
, sizeof(MARK
));
210 memset(&key
, 0, sizeof(key
));
211 key
.data
= &ep
->l_cur
;
212 key
.size
= sizeof(db_recno_t
);
213 memset(&data
, 0, sizeof(data
));
214 data
.data
= sp
->wp
->l_lp
;
215 data
.size
= sizeof(u_char
) + sizeof(MARK
);
216 if (ep
->log
->put(ep
->log
, NULL
, &key
, &data
, 0) == -1)
219 #if defined(DEBUG) && 0
220 vtrace(sp
, "%lu: %s: %u/%u\n", ep
->l_cur
,
221 type
== LOG_CURSOR_INIT
? "log_cursor_init" : "log_cursor_end",
224 /* Reset high water mark. */
225 ep
->l_high
= ++ep
->l_cur
;
228 if (type == LOG_CURSOR_END)
229 LOCK_UNLOCK(sp->wp, ep);
238 * PUBLIC: int log_line __P((SCR *, db_recno_t, u_int));
241 log_line(SCR
*sp
, db_recno_t lno
, u_int action
)
250 if (F_ISSET(ep
, F_NOLOG
))
256 * Kluge for vi. Clear the EXF undo flag so that the
257 * next 'u' command does a roll-back, regardless.
261 /* Put out one initial cursor record per set of changes. */
262 if (ep
->l_cursor
.lno
!= OOBLNO
) {
263 if (log_cursor1(sp
, LOG_CURSOR_INIT
))
265 ep
->l_cursor
.lno
= OOBLNO
;
267 } /*else if (ep->l_win != sp->wp) {
268 printf("log_line own: %p, this: %p\n", ep->l_win, sp->wp);
273 /* newly added for DB4 logging */
274 case LOG_LINE_APPEND_B
:
275 case LOG_LINE_DELETE_F
:
280 * Put out the changes. If it's a LOG_LINE_RESET_B call, it's a
281 * special case, avoid the caches. Also, if it fails and it's
282 * line 1, it just means that the user started with an empty file,
283 * so fake an empty length line.
285 if (action
== LOG_LINE_RESET_B
) {
286 if (db_get(sp
, lno
, DBG_NOCACHE
, &lp
, &len
)) {
287 static CHAR_T nul
= 0;
296 if (db_get(sp
, lno
, DBG_FATAL
, &lp
, &len
))
299 sp
->wp
->l_lp
, sp
->wp
->l_len
,
300 len
* sizeof(CHAR_T
) + CHAR_T_OFFSET
);
301 sp
->wp
->l_lp
[0] = action
;
302 memmove(sp
->wp
->l_lp
+ sizeof(u_char
), &lno
, sizeof(db_recno_t
));
303 MEMMOVEW(sp
->wp
->l_lp
+ CHAR_T_OFFSET
, lp
, len
);
306 memset(&key
, 0, sizeof(key
));
308 key
.size
= sizeof(db_recno_t
);
309 memset(&data
, 0, sizeof(data
));
310 data
.data
= sp
->wp
->l_lp
;
311 data
.size
= len
* sizeof(CHAR_T
) + CHAR_T_OFFSET
;
312 if (ep
->log
->put(ep
->log
, NULL
, &key
, &data
, 0) == -1)
315 #if defined(DEBUG) && 0
317 case LOG_LINE_APPEND_F
:
318 vtrace(sp
, "%u: log_line: append_f: %lu {%u}\n",
319 ep
->l_cur
, lno
, len
);
321 case LOG_LINE_APPEND_B
:
322 vtrace(sp
, "%u: log_line: append_b: %lu {%u}\n",
323 ep
->l_cur
, lno
, len
);
325 case LOG_LINE_DELETE_F
:
326 vtrace(sp
, "%lu: log_line: delete_f: %lu {%u}\n",
327 ep
->l_cur
, lno
, len
);
329 case LOG_LINE_DELETE_B
:
330 vtrace(sp
, "%lu: log_line: delete_b: %lu {%u}\n",
331 ep
->l_cur
, lno
, len
);
333 case LOG_LINE_RESET_F
:
334 vtrace(sp
, "%lu: log_line: reset_f: %lu {%u}\n",
335 ep
->l_cur
, lno
, len
);
337 case LOG_LINE_RESET_B
:
338 vtrace(sp
, "%lu: log_line: reset_b: %lu {%u}\n",
339 ep
->l_cur
, lno
, len
);
343 /* Reset high water mark. */
344 ep
->l_high
= ++ep
->l_cur
;
351 * Log a mark position. For the log to work, we assume that there
352 * aren't any operations that just put out a log record -- this
353 * would mean that undo operations would only reset marks, and not
354 * cause any other change.
356 * PUBLIC: int log_mark __P((SCR *, LMARK *));
359 log_mark(SCR
*sp
, LMARK
*lmp
)
365 if (F_ISSET(ep
, F_NOLOG
))
368 /* Put out one initial cursor record per set of changes. */
369 if (ep
->l_cursor
.lno
!= OOBLNO
) {
370 if (log_cursor1(sp
, LOG_CURSOR_INIT
))
372 ep
->l_cursor
.lno
= OOBLNO
;
376 BINC_RETC(sp
, sp
->wp
->l_lp
,
377 sp
->wp
->l_len
, sizeof(u_char
) + sizeof(LMARK
));
378 sp
->wp
->l_lp
[0] = LOG_MARK
;
379 memmove(sp
->wp
->l_lp
+ sizeof(u_char
), lmp
, sizeof(LMARK
));
381 memset(&key
, 0, sizeof(key
));
382 key
.data
= &ep
->l_cur
;
383 key
.size
= sizeof(db_recno_t
);
384 memset(&data
, 0, sizeof(data
));
385 data
.data
= sp
->wp
->l_lp
;
386 data
.size
= sizeof(u_char
) + sizeof(LMARK
);
387 if (ep
->log
->put(ep
->log
, NULL
, &key
, &data
, 0) == -1)
390 #if defined(DEBUG) && 0
391 vtrace(sp
, "%lu: mark %c: %lu/%u\n",
392 ep
->l_cur
, lmp
->name
, lmp
->lno
, lmp
->cno
);
394 /* Reset high water mark. */
395 ep
->l_high
= ++ep
->l_cur
;
401 * Get a line from the log in log buffer.
404 vi_log_get(SCR
*sp
, db_recno_t
*lnop
, size_t *size
)
414 BINC_RETC(sp
, sp
->wp
->l_lp
, sp
->wp
->l_len
, nlen
);
416 memset(&key
, 0, sizeof(key
));
417 key
.data
= lnop
; /* Initialize db request. */
418 key
.size
= sizeof(db_recno_t
);
419 memset(&data
, 0, sizeof(data
));
420 data
.data
= sp
->wp
->l_lp
;
421 data
.ulen
= sp
->wp
->l_len
;
422 data
.flags
= DB_DBT_USERMEM
;
423 switch (ep
->log
->get(ep
->log
, NULL
, &key
, &data
, 0)) {
437 * Roll the log backward one operation.
439 * PUBLIC: int log_backward __P((SCR *, MARK *));
442 log_backward(SCR
*sp
, MARK
*rp
)
453 if (F_ISSET(ep
, F_NOLOG
)) {
455 "010|Logging not being performed, undo not possible");
459 if (ep
->l_cur
== 1) {
460 msgq(sp
, M_BERR
, "011|No changes to undo");
464 if (ep
->l_win
&& ep
->l_win
!= sp
->wp
) {
465 ex_emsg(sp
, NULL
, EXM_LOCKED
);
471 F_SET(ep
, F_NOLOG
); /* Turn off logging. */
475 if (vi_log_get(sp
, &ep
->l_cur
, &size
))
477 #if defined(DEBUG) && 0
478 log_trace(sp
, "log_backward", ep
->l_cur
, data
.data
);
480 switch (*(p
= (u_char
*)sp
->wp
->l_lp
)) {
481 case LOG_CURSOR_INIT
:
483 memmove(rp
, p
+ sizeof(u_char
), sizeof(MARK
));
491 case LOG_LINE_APPEND_F
:
493 memmove(&lno
, p
+ sizeof(u_char
), sizeof(db_recno_t
));
494 if (db_delete(sp
, lno
))
496 ++sp
->rptlines
[L_DELETED
];
498 case LOG_LINE_DELETE_B
:
500 memmove(&lno
, p
+ sizeof(u_char
), sizeof(db_recno_t
));
501 if (db_insert(sp
, lno
,
502 (CHAR_T
*)(p
+ CHAR_T_OFFSET
),
503 (size
- CHAR_T_OFFSET
) / sizeof(CHAR_T
)))
505 ++sp
->rptlines
[L_ADDED
];
507 case LOG_LINE_RESET_F
:
509 case LOG_LINE_RESET_B
:
511 memmove(&lno
, p
+ sizeof(u_char
), sizeof(db_recno_t
));
513 (CHAR_T
*)(p
+ CHAR_T_OFFSET
),
514 (size
- CHAR_T_OFFSET
) / sizeof(CHAR_T
)))
516 if (sp
->rptlchange
!= lno
) {
517 sp
->rptlchange
= lno
;
518 ++sp
->rptlines
[L_CHANGED
];
523 memmove(&lm
, p
+ sizeof(u_char
), sizeof(LMARK
));
526 if (mark_set(sp
, lm
.name
, &m
, 0))
534 err
: F_CLR(ep
, F_NOLOG
);
541 * Reset the line to its original appearance.
544 * There's a bug in this code due to our not logging cursor movements
545 * unless a change was made. If you do a change, move off the line,
546 * then move back on and do a 'U', the line will be restored to the way
547 * it was before the original change.
549 * PUBLIC: int log_setline __P((SCR *));
562 if (F_ISSET(ep
, F_NOLOG
)) {
564 "012|Logging not being performed, undo not possible");
571 if (ep
->l_win
&& ep
->l_win
!= sp
->wp
) {
572 ex_emsg(sp
, NULL
, EXM_LOCKED
);
577 F_SET(ep
, F_NOLOG
); /* Turn off logging. */
581 if (vi_log_get(sp
, &ep
->l_cur
, &size
))
583 #if defined(DEBUG) && 0
584 log_trace(sp
, "log_setline", ep
->l_cur
, data
.data
);
586 switch (*(p
= (u_char
*)sp
->wp
->l_lp
)) {
587 case LOG_CURSOR_INIT
:
588 memmove(&m
, p
+ sizeof(u_char
), sizeof(MARK
));
589 if (m
.lno
!= sp
->lno
|| ep
->l_cur
== 1) {
596 memmove(&m
, p
+ sizeof(u_char
), sizeof(MARK
));
597 if (m
.lno
!= sp
->lno
) {
604 case LOG_LINE_APPEND_F
:
605 case LOG_LINE_DELETE_B
:
606 case LOG_LINE_RESET_F
:
608 case LOG_LINE_RESET_B
:
609 memmove(&lno
, p
+ sizeof(u_char
), sizeof(db_recno_t
));
610 if (lno
== sp
->lno
&&
611 db_set(sp
, lno
, (CHAR_T
*)(p
+ CHAR_T_OFFSET
),
612 (size
- CHAR_T_OFFSET
) / sizeof(CHAR_T
)))
614 if (sp
->rptlchange
!= lno
) {
615 sp
->rptlchange
= lno
;
616 ++sp
->rptlines
[L_CHANGED
];
619 memmove(&lm
, p
+ sizeof(u_char
), sizeof(LMARK
));
622 if (mark_set(sp
, lm
.name
, &m
, 0))
630 err
: F_CLR(ep
, F_NOLOG
);
637 * Roll the log forward one operation.
639 * PUBLIC: int log_forward __P((SCR *, MARK *));
642 log_forward(SCR
*sp
, MARK
*rp
)
653 if (F_ISSET(ep
, F_NOLOG
)) {
655 "013|Logging not being performed, roll-forward not possible");
659 if (ep
->l_cur
== ep
->l_high
) {
660 msgq(sp
, M_BERR
, "014|No changes to re-do");
664 if (ep
->l_win
&& ep
->l_win
!= sp
->wp
) {
665 ex_emsg(sp
, NULL
, EXM_LOCKED
);
670 F_SET(ep
, F_NOLOG
); /* Turn off logging. */
674 if (vi_log_get(sp
, &ep
->l_cur
, &size
))
676 #if defined(DEBUG) && 0
677 log_trace(sp
, "log_forward", ep
->l_cur
, data
.data
);
679 switch (*(p
= (u_char
*)sp
->wp
->l_lp
)) {
683 memmove(rp
, p
+ sizeof(u_char
), sizeof(MARK
));
689 case LOG_CURSOR_INIT
:
691 case LOG_LINE_APPEND_F
:
693 memmove(&lno
, p
+ sizeof(u_char
), sizeof(db_recno_t
));
694 if (db_insert(sp
, lno
,
695 (CHAR_T
*)(p
+ CHAR_T_OFFSET
),
696 (size
- CHAR_T_OFFSET
) / sizeof(CHAR_T
)))
698 ++sp
->rptlines
[L_ADDED
];
700 case LOG_LINE_DELETE_B
:
702 memmove(&lno
, p
+ sizeof(u_char
), sizeof(db_recno_t
));
703 if (db_delete(sp
, lno
))
705 ++sp
->rptlines
[L_DELETED
];
707 case LOG_LINE_RESET_B
:
709 case LOG_LINE_RESET_F
:
711 memmove(&lno
, p
+ sizeof(u_char
), sizeof(db_recno_t
));
713 (CHAR_T
*)(p
+ CHAR_T_OFFSET
),
714 (size
- CHAR_T_OFFSET
) / sizeof(CHAR_T
)))
716 if (sp
->rptlchange
!= lno
) {
717 sp
->rptlchange
= lno
;
718 ++sp
->rptlines
[L_CHANGED
];
723 memmove(&lm
, p
+ sizeof(u_char
), sizeof(LMARK
));
726 if (mark_set(sp
, lm
.name
, &m
, 0))
734 err
: F_CLR(ep
, F_NOLOG
);
741 * Try and restart the log on failure, i.e. if we run out of memory.
744 log_err(SCR
*sp
, const char *file
, int line
)
748 msgq(sp
, M_SYSERR
, "015|%s/%d: log put error", tail(file
), line
);
750 (void)ep
->log
->close(ep
->log
, DB_NOSYNC
);
751 if (!log_init(sp
, ep
))
752 msgq(sp
, M_ERR
, "267|Log restarted");
755 #if defined(DEBUG) && 0
757 log_trace(SCR
*sp
, const char *msg
, db_recno_t rno
, u_char
*p
)
764 case LOG_CURSOR_INIT
:
765 memmove(&m
, p
+ sizeof(u_char
), sizeof(MARK
));
766 vtrace(sp
, "%lu: %s: C_INIT: %u/%u\n", rno
, msg
, m
.lno
, m
.cno
);
769 memmove(&m
, p
+ sizeof(u_char
), sizeof(MARK
));
770 vtrace(sp
, "%lu: %s: C_END: %u/%u\n", rno
, msg
, m
.lno
, m
.cno
);
772 case LOG_LINE_APPEND_F
:
773 memmove(&lno
, p
+ sizeof(u_char
), sizeof(db_recno_t
));
774 vtrace(sp
, "%lu: %s: APPEND_F: %lu\n", rno
, msg
, lno
);
776 case LOG_LINE_APPEND_B
:
777 memmove(&lno
, p
+ sizeof(u_char
), sizeof(db_recno_t
));
778 vtrace(sp
, "%lu: %s: APPEND_B: %lu\n", rno
, msg
, lno
);
780 case LOG_LINE_DELETE_F
:
781 memmove(&lno
, p
+ sizeof(u_char
), sizeof(db_recno_t
));
782 vtrace(sp
, "%lu: %s: DELETE_F: %lu\n", rno
, msg
, lno
);
784 case LOG_LINE_DELETE_B
:
785 memmove(&lno
, p
+ sizeof(u_char
), sizeof(db_recno_t
));
786 vtrace(sp
, "%lu: %s: DELETE_B: %lu\n", rno
, msg
, lno
);
788 case LOG_LINE_RESET_F
:
789 memmove(&lno
, p
+ sizeof(u_char
), sizeof(db_recno_t
));
790 vtrace(sp
, "%lu: %s: RESET_F: %lu\n", rno
, msg
, lno
);
792 case LOG_LINE_RESET_B
:
793 memmove(&lno
, p
+ sizeof(u_char
), sizeof(db_recno_t
));
794 vtrace(sp
, "%lu: %s: RESET_B: %lu\n", rno
, msg
, lno
);
797 memmove(&lm
, p
+ sizeof(u_char
), sizeof(LMARK
));
799 "%lu: %s: MARK: %u/%u\n", rno
, msg
, lm
.lno
, lm
.cno
);