1 /* $NetBSD: log.c,v 1.3 2014/01/26 21:43:45 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.
13 #include <sys/cdefs.h>
16 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 ";
19 __RCSID("$NetBSD: log.c,v 1.3 2014/01/26 21:43:45 christos Exp $");
22 #include <sys/types.h>
23 #include <sys/queue.h>
26 #include <bitstring.h>
35 #include "dbinternal.h"
38 * The log consists of records, each containing a type byte and a variable
39 * length byte string, as follows:
41 * LOG_CURSOR_INIT MARK
43 * LOG_LINE_APPEND_F db_recno_t char *
44 * LOG_LINE_APPEND_B db_recno_t char *
45 * LOG_LINE_DELETE_F db_recno_t char *
46 * LOG_LINE_DELETE_B db_recno_t char *
47 * LOG_LINE_RESET_F db_recno_t char *
48 * LOG_LINE_RESET_B db_recno_t char *
51 * We do before image physical logging. This means that the editor layer
52 * MAY NOT modify records in place, even if simply deleting or overwriting
53 * characters. Since the smallest unit of logging is a line, we're using
54 * up lots of space. This may eventually have to be reduced, probably by
55 * doing logical logging, which is a much cooler database phrase.
57 * The implementation of the historic vi 'u' command, using roll-forward and
58 * roll-back, is simple. Each set of changes has a LOG_CURSOR_INIT record,
59 * followed by a number of other records, followed by a LOG_CURSOR_END record.
60 * LOG_LINE_RESET records come in pairs. The first is a LOG_LINE_RESET_B
61 * record, and is the line before the change. The second is LOG_LINE_RESET_F,
62 * and is the line after the change. Roll-back is done by backing up to the
63 * first LOG_CURSOR_INIT record before a change. Roll-forward is done in a
66 * The 'U' command is implemented by rolling backward to a LOG_CURSOR_END
67 * record for a line different from the current one. It should be noted that
68 * this means that a subsequent 'u' command will make a change based on the
69 * new position of the log's cursor. This is okay, and, in fact, historic vi
73 static int vi_log_get
__P((SCR
*sp
, db_recno_t
*lnop
, size_t *size
));
74 static int log_cursor1
__P((SCR
*, int));
75 static void log_err
__P((SCR
*, const char *, int));
76 #if defined(DEBUG) && 0
77 static void log_trace
__P((SCR
*, const char *, db_recno_t
, u_char
*));
80 /* Try and restart the log on failure, i.e. if we run out of memory. */
82 log_err(sp, __FILE__, __LINE__); \
86 /* offset of CHAR_T string in log needs to be aligned on some systems
87 * because it is passed to db_set as a string
90 char data
[sizeof(u_char
) /* type */ + sizeof(db_recno_t
)];
93 #define CHAR_T_OFFSET ((char *)(((log_t*)0)->str) - (char *)0)
97 * Initialize the logging subsystem.
99 * PUBLIC: int log_init __P((SCR *, EXF *));
102 log_init(SCR
*sp
, EXF
*ep
)
106 * ep MAY NOT BE THE SAME AS sp->ep, DON'T USE THE LATTER.
108 * Initialize the buffer. The logging subsystem has its own
109 * buffers because the global ones are almost by definition
110 * going to be in use when the log runs.
114 ep
->l_cursor
.lno
= 1; /* XXX Any valid recno. */
115 ep
->l_cursor
.cno
= 0;
116 ep
->l_high
= ep
->l_cur
= 1;
118 if (db_create(&ep
->log
, 0, 0) != 0 ||
119 db_open(ep
->log
, NULL
, DB_RECNO
,
120 DB_CREATE
| VI_DB_THREAD
, S_IRUSR
| S_IWUSR
) != 0) {
121 msgq(sp
, M_SYSERR
, "009|Log file");
127 /*LOCK_INIT(sp->wp, ep);*/
134 * Close the logging subsystem.
136 * PUBLIC: int log_end __P((SCR *, EXF *));
139 log_end(SCR
*sp
, EXF
*ep
)
143 * ep MAY NOT BE THE SAME AS sp->ep, DON'T USE THE LATTER.
145 /*LOCK_END(sp->wp, ep);*/
146 if (ep
->log
!= NULL
) {
147 (void)(ep
->log
->close
)(ep
->log
, DB_NOSYNC
);
150 if (sp
->wp
->l_lp
!= NULL
) {
155 ep
->l_cursor
.lno
= 1; /* XXX Any valid recno. */
156 ep
->l_cursor
.cno
= 0;
157 ep
->l_high
= ep
->l_cur
= 1;
163 * Log the current cursor position, starting an event.
165 * PUBLIC: int log_cursor __P((SCR *));
173 if (F_ISSET(ep
, F_NOLOG
))
177 * If any changes were made since the last cursor init,
178 * put out the ending cursor record.
180 if (ep
->l_cursor
.lno
== OOBLNO
) {
181 if (ep
->l_win
&& ep
->l_win
!= sp
->wp
)
183 ep
->l_cursor
.lno
= sp
->lno
;
184 ep
->l_cursor
.cno
= sp
->cno
;
186 return (log_cursor1(sp
, LOG_CURSOR_END
));
188 ep
->l_cursor
.lno
= sp
->lno
;
189 ep
->l_cursor
.cno
= sp
->cno
;
195 * Actually push a cursor record out.
198 log_cursor1(SCR
*sp
, int type
)
206 if (type == LOG_CURSOR_INIT &&
207 LOCK_TRY(sp->wp, ep))
211 BINC_RETC(sp
, sp
->wp
->l_lp
, sp
->wp
->l_len
, sizeof(u_char
) + sizeof(MARK
));
212 sp
->wp
->l_lp
[0] = type
;
213 memmove(sp
->wp
->l_lp
+ sizeof(u_char
), &ep
->l_cursor
, sizeof(MARK
));
215 memset(&key
, 0, sizeof(key
));
216 key
.data
= &ep
->l_cur
;
217 key
.size
= sizeof(db_recno_t
);
218 memset(&data
, 0, sizeof(data
));
219 data
.data
= sp
->wp
->l_lp
;
220 data
.size
= sizeof(u_char
) + sizeof(MARK
);
221 if (ep
->log
->put(ep
->log
, NULL
, &key
, &data
, 0) == -1)
224 #if defined(DEBUG) && 0
225 vtrace(sp
, "%lu: %s: %u/%u\n", ep
->l_cur
,
226 type
== LOG_CURSOR_INIT
? "log_cursor_init" : "log_cursor_end",
229 /* Reset high water mark. */
230 ep
->l_high
= ++ep
->l_cur
;
233 if (type == LOG_CURSOR_END)
234 LOCK_UNLOCK(sp->wp, ep);
243 * PUBLIC: int log_line __P((SCR *, db_recno_t, u_int));
246 log_line(SCR
*sp
, db_recno_t lno
, u_int action
)
255 if (F_ISSET(ep
, F_NOLOG
))
261 * Kluge for vi. Clear the EXF undo flag so that the
262 * next 'u' command does a roll-back, regardless.
266 /* Put out one initial cursor record per set of changes. */
267 if (ep
->l_cursor
.lno
!= OOBLNO
) {
268 if (log_cursor1(sp
, LOG_CURSOR_INIT
))
270 ep
->l_cursor
.lno
= OOBLNO
;
272 } /*else if (ep->l_win != sp->wp) {
273 printf("log_line own: %p, this: %p\n", ep->l_win, sp->wp);
278 /* newly added for DB4 logging */
279 case LOG_LINE_APPEND_B
:
280 case LOG_LINE_DELETE_F
:
285 * Put out the changes. If it's a LOG_LINE_RESET_B call, it's a
286 * special case, avoid the caches. Also, if it fails and it's
287 * line 1, it just means that the user started with an empty file,
288 * so fake an empty length line.
290 if (action
== LOG_LINE_RESET_B
) {
291 if (db_get(sp
, lno
, DBG_NOCACHE
, &lp
, &len
)) {
292 static CHAR_T nul
= 0;
301 if (db_get(sp
, lno
, DBG_FATAL
, &lp
, &len
))
304 sp
->wp
->l_lp
, sp
->wp
->l_len
,
305 len
* sizeof(CHAR_T
) + CHAR_T_OFFSET
);
306 sp
->wp
->l_lp
[0] = action
;
307 memmove(sp
->wp
->l_lp
+ sizeof(u_char
), &lno
, sizeof(db_recno_t
));
308 MEMMOVEW(sp
->wp
->l_lp
+ CHAR_T_OFFSET
, lp
, len
);
311 memset(&key
, 0, sizeof(key
));
313 key
.size
= sizeof(db_recno_t
);
314 memset(&data
, 0, sizeof(data
));
315 data
.data
= sp
->wp
->l_lp
;
316 data
.size
= len
* sizeof(CHAR_T
) + CHAR_T_OFFSET
;
317 if (ep
->log
->put(ep
->log
, NULL
, &key
, &data
, 0) == -1)
320 #if defined(DEBUG) && 0
322 case LOG_LINE_APPEND_F
:
323 vtrace(sp
, "%u: log_line: append_f: %lu {%u}\n",
324 ep
->l_cur
, lno
, len
);
326 case LOG_LINE_APPEND_B
:
327 vtrace(sp
, "%u: log_line: append_b: %lu {%u}\n",
328 ep
->l_cur
, lno
, len
);
330 case LOG_LINE_DELETE_F
:
331 vtrace(sp
, "%lu: log_line: delete_f: %lu {%u}\n",
332 ep
->l_cur
, lno
, len
);
334 case LOG_LINE_DELETE_B
:
335 vtrace(sp
, "%lu: log_line: delete_b: %lu {%u}\n",
336 ep
->l_cur
, lno
, len
);
338 case LOG_LINE_RESET_F
:
339 vtrace(sp
, "%lu: log_line: reset_f: %lu {%u}\n",
340 ep
->l_cur
, lno
, len
);
342 case LOG_LINE_RESET_B
:
343 vtrace(sp
, "%lu: log_line: reset_b: %lu {%u}\n",
344 ep
->l_cur
, lno
, len
);
348 /* Reset high water mark. */
349 ep
->l_high
= ++ep
->l_cur
;
356 * Log a mark position. For the log to work, we assume that there
357 * aren't any operations that just put out a log record -- this
358 * would mean that undo operations would only reset marks, and not
359 * cause any other change.
361 * PUBLIC: int log_mark __P((SCR *, LMARK *));
364 log_mark(SCR
*sp
, LMARK
*lmp
)
370 if (F_ISSET(ep
, F_NOLOG
))
373 /* Put out one initial cursor record per set of changes. */
374 if (ep
->l_cursor
.lno
!= OOBLNO
) {
375 if (log_cursor1(sp
, LOG_CURSOR_INIT
))
377 ep
->l_cursor
.lno
= OOBLNO
;
381 BINC_RETC(sp
, sp
->wp
->l_lp
,
382 sp
->wp
->l_len
, sizeof(u_char
) + sizeof(LMARK
));
383 sp
->wp
->l_lp
[0] = LOG_MARK
;
384 memmove(sp
->wp
->l_lp
+ sizeof(u_char
), lmp
, sizeof(LMARK
));
386 memset(&key
, 0, sizeof(key
));
387 key
.data
= &ep
->l_cur
;
388 key
.size
= sizeof(db_recno_t
);
389 memset(&data
, 0, sizeof(data
));
390 data
.data
= sp
->wp
->l_lp
;
391 data
.size
= sizeof(u_char
) + sizeof(LMARK
);
392 if (ep
->log
->put(ep
->log
, NULL
, &key
, &data
, 0) == -1)
395 #if defined(DEBUG) && 0
396 vtrace(sp
, "%lu: mark %c: %lu/%u\n",
397 ep
->l_cur
, lmp
->name
, lmp
->lno
, lmp
->cno
);
399 /* Reset high water mark. */
400 ep
->l_high
= ++ep
->l_cur
;
406 * Get a line from the log in log buffer.
409 vi_log_get(SCR
*sp
, db_recno_t
*lnop
, size_t *size
)
419 BINC_RETC(sp
, sp
->wp
->l_lp
, sp
->wp
->l_len
, nlen
);
421 memset(&key
, 0, sizeof(key
));
422 key
.data
= lnop
; /* Initialize db request. */
423 key
.size
= sizeof(db_recno_t
);
424 memset(&data
, 0, sizeof(data
));
425 data
.data
= sp
->wp
->l_lp
;
426 data
.ulen
= sp
->wp
->l_len
;
427 data
.flags
= DB_DBT_USERMEM
;
428 switch (ep
->log
->get(ep
->log
, NULL
, &key
, &data
, 0)) {
442 * Roll the log backward one operation.
444 * PUBLIC: int log_backward __P((SCR *, MARK *));
447 log_backward(SCR
*sp
, MARK
*rp
)
458 if (F_ISSET(ep
, F_NOLOG
)) {
460 "010|Logging not being performed, undo not possible");
464 if (ep
->l_cur
== 1) {
465 msgq(sp
, M_BERR
, "011|No changes to undo");
469 if (ep
->l_win
&& ep
->l_win
!= sp
->wp
) {
470 ex_emsg(sp
, NULL
, EXM_LOCKED
);
476 F_SET(ep
, F_NOLOG
); /* Turn off logging. */
480 if (vi_log_get(sp
, &ep
->l_cur
, &size
))
482 #if defined(DEBUG) && 0
483 log_trace(sp
, "log_backward", ep
->l_cur
, data
.data
);
485 switch (*(p
= (u_char
*)sp
->wp
->l_lp
)) {
486 case LOG_CURSOR_INIT
:
488 memmove(rp
, p
+ sizeof(u_char
), sizeof(MARK
));
496 case LOG_LINE_APPEND_F
:
498 memmove(&lno
, p
+ sizeof(u_char
), sizeof(db_recno_t
));
499 if (db_delete(sp
, lno
))
501 ++sp
->rptlines
[L_DELETED
];
503 case LOG_LINE_DELETE_B
:
505 memmove(&lno
, p
+ sizeof(u_char
), sizeof(db_recno_t
));
506 if (db_insert(sp
, lno
,
507 (CHAR_T
*)(p
+ CHAR_T_OFFSET
),
508 (size
- CHAR_T_OFFSET
) / sizeof(CHAR_T
)))
510 ++sp
->rptlines
[L_ADDED
];
512 case LOG_LINE_RESET_F
:
514 case LOG_LINE_RESET_B
:
516 memmove(&lno
, p
+ sizeof(u_char
), sizeof(db_recno_t
));
518 (CHAR_T
*)(p
+ CHAR_T_OFFSET
),
519 (size
- CHAR_T_OFFSET
) / sizeof(CHAR_T
)))
521 if (sp
->rptlchange
!= lno
) {
522 sp
->rptlchange
= lno
;
523 ++sp
->rptlines
[L_CHANGED
];
528 memmove(&lm
, p
+ sizeof(u_char
), sizeof(LMARK
));
531 if (mark_set(sp
, lm
.name
, &m
, 0))
539 err
: F_CLR(ep
, F_NOLOG
);
546 * Reset the line to its original appearance.
549 * There's a bug in this code due to our not logging cursor movements
550 * unless a change was made. If you do a change, move off the line,
551 * then move back on and do a 'U', the line will be restored to the way
552 * it was before the original change.
554 * PUBLIC: int log_setline __P((SCR *));
567 if (F_ISSET(ep
, F_NOLOG
)) {
569 "012|Logging not being performed, undo not possible");
576 if (ep
->l_win
&& ep
->l_win
!= sp
->wp
) {
577 ex_emsg(sp
, NULL
, EXM_LOCKED
);
582 F_SET(ep
, F_NOLOG
); /* Turn off logging. */
586 if (vi_log_get(sp
, &ep
->l_cur
, &size
))
588 #if defined(DEBUG) && 0
589 log_trace(sp
, "log_setline", ep
->l_cur
, data
.data
);
591 switch (*(p
= (u_char
*)sp
->wp
->l_lp
)) {
592 case LOG_CURSOR_INIT
:
593 memmove(&m
, p
+ sizeof(u_char
), sizeof(MARK
));
594 if (m
.lno
!= sp
->lno
|| ep
->l_cur
== 1) {
601 memmove(&m
, p
+ sizeof(u_char
), sizeof(MARK
));
602 if (m
.lno
!= sp
->lno
) {
609 case LOG_LINE_APPEND_F
:
610 case LOG_LINE_DELETE_B
:
611 case LOG_LINE_RESET_F
:
613 case LOG_LINE_RESET_B
:
614 memmove(&lno
, p
+ sizeof(u_char
), sizeof(db_recno_t
));
615 if (lno
== sp
->lno
&&
616 db_set(sp
, lno
, (CHAR_T
*)(p
+ CHAR_T_OFFSET
),
617 (size
- CHAR_T_OFFSET
) / sizeof(CHAR_T
)))
619 if (sp
->rptlchange
!= lno
) {
620 sp
->rptlchange
= lno
;
621 ++sp
->rptlines
[L_CHANGED
];
624 memmove(&lm
, p
+ sizeof(u_char
), sizeof(LMARK
));
627 if (mark_set(sp
, lm
.name
, &m
, 0))
635 err
: F_CLR(ep
, F_NOLOG
);
642 * Roll the log forward one operation.
644 * PUBLIC: int log_forward __P((SCR *, MARK *));
647 log_forward(SCR
*sp
, MARK
*rp
)
658 if (F_ISSET(ep
, F_NOLOG
)) {
660 "013|Logging not being performed, roll-forward not possible");
664 if (ep
->l_cur
== ep
->l_high
) {
665 msgq(sp
, M_BERR
, "014|No changes to re-do");
669 if (ep
->l_win
&& ep
->l_win
!= sp
->wp
) {
670 ex_emsg(sp
, NULL
, EXM_LOCKED
);
675 F_SET(ep
, F_NOLOG
); /* Turn off logging. */
679 if (vi_log_get(sp
, &ep
->l_cur
, &size
))
681 #if defined(DEBUG) && 0
682 log_trace(sp
, "log_forward", ep
->l_cur
, data
.data
);
684 switch (*(p
= (u_char
*)sp
->wp
->l_lp
)) {
688 memmove(rp
, p
+ sizeof(u_char
), sizeof(MARK
));
694 case LOG_CURSOR_INIT
:
696 case LOG_LINE_APPEND_F
:
698 memmove(&lno
, p
+ sizeof(u_char
), sizeof(db_recno_t
));
699 if (db_insert(sp
, lno
,
700 (CHAR_T
*)(p
+ CHAR_T_OFFSET
),
701 (size
- CHAR_T_OFFSET
) / sizeof(CHAR_T
)))
703 ++sp
->rptlines
[L_ADDED
];
705 case LOG_LINE_DELETE_B
:
707 memmove(&lno
, p
+ sizeof(u_char
), sizeof(db_recno_t
));
708 if (db_delete(sp
, lno
))
710 ++sp
->rptlines
[L_DELETED
];
712 case LOG_LINE_RESET_B
:
714 case LOG_LINE_RESET_F
:
716 memmove(&lno
, p
+ sizeof(u_char
), sizeof(db_recno_t
));
718 (CHAR_T
*)(p
+ CHAR_T_OFFSET
),
719 (size
- CHAR_T_OFFSET
) / sizeof(CHAR_T
)))
721 if (sp
->rptlchange
!= lno
) {
722 sp
->rptlchange
= lno
;
723 ++sp
->rptlines
[L_CHANGED
];
728 memmove(&lm
, p
+ sizeof(u_char
), sizeof(LMARK
));
731 if (mark_set(sp
, lm
.name
, &m
, 0))
739 err
: F_CLR(ep
, F_NOLOG
);
746 * Try and restart the log on failure, i.e. if we run out of memory.
749 log_err(SCR
*sp
, const char *file
, int line
)
753 msgq(sp
, M_SYSERR
, "015|%s/%d: log put error", tail(file
), line
);
755 (void)ep
->log
->close(ep
->log
, DB_NOSYNC
);
756 if (!log_init(sp
, ep
))
757 msgq(sp
, M_ERR
, "267|Log restarted");
760 #if defined(DEBUG) && 0
762 log_trace(SCR
*sp
, const char *msg
, db_recno_t rno
, u_char
*p
)
769 case LOG_CURSOR_INIT
:
770 memmove(&m
, p
+ sizeof(u_char
), sizeof(MARK
));
771 vtrace(sp
, "%lu: %s: C_INIT: %u/%u\n", rno
, msg
, m
.lno
, m
.cno
);
774 memmove(&m
, p
+ sizeof(u_char
), sizeof(MARK
));
775 vtrace(sp
, "%lu: %s: C_END: %u/%u\n", rno
, msg
, m
.lno
, m
.cno
);
777 case LOG_LINE_APPEND_F
:
778 memmove(&lno
, p
+ sizeof(u_char
), sizeof(db_recno_t
));
779 vtrace(sp
, "%lu: %s: APPEND_F: %lu\n", rno
, msg
, lno
);
781 case LOG_LINE_APPEND_B
:
782 memmove(&lno
, p
+ sizeof(u_char
), sizeof(db_recno_t
));
783 vtrace(sp
, "%lu: %s: APPEND_B: %lu\n", rno
, msg
, lno
);
785 case LOG_LINE_DELETE_F
:
786 memmove(&lno
, p
+ sizeof(u_char
), sizeof(db_recno_t
));
787 vtrace(sp
, "%lu: %s: DELETE_F: %lu\n", rno
, msg
, lno
);
789 case LOG_LINE_DELETE_B
:
790 memmove(&lno
, p
+ sizeof(u_char
), sizeof(db_recno_t
));
791 vtrace(sp
, "%lu: %s: DELETE_B: %lu\n", rno
, msg
, lno
);
793 case LOG_LINE_RESET_F
:
794 memmove(&lno
, p
+ sizeof(u_char
), sizeof(db_recno_t
));
795 vtrace(sp
, "%lu: %s: RESET_F: %lu\n", rno
, msg
, lno
);
797 case LOG_LINE_RESET_B
:
798 memmove(&lno
, p
+ sizeof(u_char
), sizeof(db_recno_t
));
799 vtrace(sp
, "%lu: %s: RESET_B: %lu\n", rno
, msg
, lno
);
802 memmove(&lm
, p
+ sizeof(u_char
), sizeof(LMARK
));
804 "%lu: %s: MARK: %u/%u\n", rno
, msg
, lm
.lno
, lm
.cno
);