1 /* $NetBSD: log1.c,v 1.3 2013/11/29 16:36:11 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>
32 * The log consists of records, each containing a type byte and a variable
33 * length byte string, as follows:
35 * LOG_CURSOR_INIT MARK
37 * LOG_LINE_APPEND_F db_recno_t char *
38 * LOG_LINE_APPEND_B db_recno_t char *
39 * LOG_LINE_DELETE_F db_recno_t char *
40 * LOG_LINE_DELETE_B db_recno_t char *
41 * LOG_LINE_RESET_F db_recno_t char *
42 * LOG_LINE_RESET_B db_recno_t char *
45 * We do before image physical logging. This means that the editor layer
46 * MAY NOT modify records in place, even if simply deleting or overwriting
47 * characters. Since the smallest unit of logging is a line, we're using
48 * up lots of space. This may eventually have to be reduced, probably by
49 * doing logical logging, which is a much cooler database phrase.
51 * The implementation of the historic vi 'u' command, using roll-forward and
52 * roll-back, is simple. Each set of changes has a LOG_CURSOR_INIT record,
53 * followed by a number of other records, followed by a LOG_CURSOR_END record.
54 * LOG_LINE_RESET records come in pairs. The first is a LOG_LINE_RESET_B
55 * record, and is the line before the change. The second is LOG_LINE_RESET_F,
56 * and is the line after the change. Roll-back is done by backing up to the
57 * first LOG_CURSOR_INIT record before a change. Roll-forward is done in a
60 * The 'U' command is implemented by rolling backward to a LOG_CURSOR_END
61 * record for a line different from the current one. It should be noted that
62 * this means that a subsequent 'u' command will make a change based on the
63 * new position of the log's cursor. This is okay, and, in fact, historic vi
67 static int log_cursor1
__P((SCR
*, int));
68 static void log_err
__P((SCR
*, const char *, int));
69 #if defined(LOGDEBUG) && defined(TRACE)
70 static void log_trace
__P((SCR
*, const char *, db_recno_t
, u_char
*));
73 /* Try and restart the log on failure, i.e. if we run out of memory. */
75 log_err(sp, __FILE__, __LINE__); \
79 /* offset of CHAR_T string in log needs to be aligned on some systems
80 * because it is passed to db_set as a string
83 char data
[sizeof(u_char
) /* type */ + sizeof(db_recno_t
)];
86 #define CHAR_T_OFFSET ((char *)(((log_t*)0)->str) - (char *)0)
90 * Initialize the logging subsystem.
92 * PUBLIC: int log_init __P((SCR *, EXF *));
95 log_init(SCR
*sp
, EXF
*ep
)
99 * ep MAY NOT BE THE SAME AS sp->ep, DON'T USE THE LATTER.
101 * Initialize the buffer. The logging subsystem has its own
102 * buffers because the global ones are almost by definition
103 * going to be in use when the log runs.
107 ep
->l_cursor
.lno
= 1; /* XXX Any valid recno. */
108 ep
->l_cursor
.cno
= 0;
109 ep
->l_high
= ep
->l_cur
= 1;
111 ep
->log
= dbopen(NULL
, O_CREAT
| O_NONBLOCK
| O_RDWR
,
112 S_IRUSR
| S_IWUSR
, DB_RECNO
, NULL
);
113 if (ep
->log
== NULL
) {
114 msgq(sp
, M_SYSERR
, "009|Log file");
120 /*LOCK_INIT(sp->wp, ep);*/
127 * Close the logging subsystem.
129 * PUBLIC: int log_end __P((SCR *, EXF *));
132 log_end(SCR
*sp
, EXF
*ep
)
136 * ep MAY NOT BE THE SAME AS sp->ep, DON'T USE THE LATTER.
138 /*LOCK_END(sp->wp, ep);*/
139 if (ep
->log
!= NULL
) {
140 (void)(ep
->log
->close
)(ep
->log
);
143 if (sp
->wp
->l_lp
!= NULL
) {
148 ep
->l_cursor
.lno
= 1; /* XXX Any valid recno. */
149 ep
->l_cursor
.cno
= 0;
150 ep
->l_high
= ep
->l_cur
= 1;
156 * Log the current cursor position, starting an event.
158 * PUBLIC: int log_cursor __P((SCR *));
166 if (F_ISSET(ep
, F_NOLOG
))
170 * If any changes were made since the last cursor init,
171 * put out the ending cursor record.
173 if (ep
->l_cursor
.lno
== OOBLNO
) {
174 if (ep
->l_win
&& ep
->l_win
!= sp
->wp
)
176 ep
->l_cursor
.lno
= sp
->lno
;
177 ep
->l_cursor
.cno
= sp
->cno
;
179 return (log_cursor1(sp
, LOG_CURSOR_END
));
181 ep
->l_cursor
.lno
= sp
->lno
;
182 ep
->l_cursor
.cno
= sp
->cno
;
188 * Actually push a cursor record out.
191 log_cursor1(SCR
*sp
, int type
)
199 if (type == LOG_CURSOR_INIT &&
200 LOCK_TRY(sp->wp, ep))
204 BINC_RETC(sp
, sp
->wp
->l_lp
, sp
->wp
->l_len
, sizeof(u_char
) + sizeof(MARK
));
205 sp
->wp
->l_lp
[0] = type
;
206 memmove(sp
->wp
->l_lp
+ sizeof(u_char
), &ep
->l_cursor
, sizeof(MARK
));
208 memset(&key
, 0, sizeof(key
));
209 key
.data
= &ep
->l_cur
;
210 key
.size
= sizeof(db_recno_t
);
211 memset(&data
, 0, sizeof(data
));
212 data
.data
= sp
->wp
->l_lp
;
213 data
.size
= sizeof(u_char
) + sizeof(MARK
);
214 if (ep
->log
->put(ep
->log
, &key
, &data
, 0) == -1)
217 #if defined(LOGDEBUG) && defined(TRACE)
218 vtrace("%lu: %s: %u/%u\n", ep
->l_cur
,
219 type
== LOG_CURSOR_INIT
? "log_cursor_init" : "log_cursor_end",
222 /* Reset high water mark. */
223 ep
->l_high
= ++ep
->l_cur
;
226 if (type == LOG_CURSOR_END)
227 LOCK_UNLOCK(sp->wp, ep);
236 * PUBLIC: int log_line __P((SCR *, db_recno_t, u_int));
239 log_line(SCR
*sp
, db_recno_t lno
, u_int action
)
248 if (F_ISSET(ep
, F_NOLOG
))
254 * Kluge for vi. Clear the EXF undo flag so that the
255 * next 'u' command does a roll-back, regardless.
259 /* Put out one initial cursor record per set of changes. */
260 if (ep
->l_cursor
.lno
!= OOBLNO
) {
261 if (log_cursor1(sp
, LOG_CURSOR_INIT
))
263 ep
->l_cursor
.lno
= OOBLNO
;
265 } /*else if (ep->l_win != sp->wp) {
266 printf("log_line own: %p, this: %p\n", ep->l_win, sp->wp);
271 /* newly added for DB4 logging */
272 case LOG_LINE_APPEND_B
:
273 case LOG_LINE_DELETE_F
:
278 * Put out the changes. If it's a LOG_LINE_RESET_B call, it's a
279 * special case, avoid the caches. Also, if it fails and it's
280 * line 1, it just means that the user started with an empty file,
281 * so fake an empty length line.
283 if (action
== LOG_LINE_RESET_B
) {
284 if (db_get(sp
, lno
, DBG_NOCACHE
, &lp
, &len
)) {
285 static CHAR_T nul
= 0;
294 if (db_get(sp
, lno
, DBG_FATAL
, &lp
, &len
))
297 sp
->wp
->l_lp
, sp
->wp
->l_len
,
298 len
* sizeof(CHAR_T
) + CHAR_T_OFFSET
);
299 sp
->wp
->l_lp
[0] = action
;
300 memmove(sp
->wp
->l_lp
+ sizeof(u_char
), &lno
, sizeof(db_recno_t
));
301 MEMMOVEW(sp
->wp
->l_lp
+ CHAR_T_OFFSET
, lp
, len
);
304 memset(&key
, 0, sizeof(key
));
306 key
.size
= sizeof(db_recno_t
);
307 memset(&data
, 0, sizeof(data
));
308 data
.data
= sp
->wp
->l_lp
;
309 data
.size
= len
* sizeof(CHAR_T
) + CHAR_T_OFFSET
;
310 if (ep
->log
->put(ep
->log
, &key
, &data
, 0) == -1)
313 #if defined(LOGDEBUG) && defined(TRACE)
315 case LOG_LINE_APPEND_F
:
316 vtrace("%u: log_line: append_f: %lu {%u}\n",
317 ep
->l_cur
, lno
, len
);
319 case LOG_LINE_APPEND_B
:
320 vtrace("%u: log_line: append_b: %lu {%u}\n",
321 ep
->l_cur
, lno
, len
);
323 case LOG_LINE_DELETE_F
:
324 vtrace("%lu: log_line: delete_f: %lu {%u}\n",
325 ep
->l_cur
, lno
, len
);
327 case LOG_LINE_DELETE_B
:
328 vtrace("%lu: log_line: delete_b: %lu {%u}\n",
329 ep
->l_cur
, lno
, len
);
331 case LOG_LINE_RESET_F
:
332 vtrace("%lu: log_line: reset_f: %lu {%u}\n",
333 ep
->l_cur
, lno
, len
);
335 case LOG_LINE_RESET_B
:
336 vtrace("%lu: log_line: reset_b: %lu {%u}\n",
337 ep
->l_cur
, lno
, len
);
341 /* Reset high water mark. */
342 ep
->l_high
= ++ep
->l_cur
;
349 * Log a mark position. For the log to work, we assume that there
350 * aren't any operations that just put out a log record -- this
351 * would mean that undo operations would only reset marks, and not
352 * cause any other change.
354 * PUBLIC: int log_mark __P((SCR *, LMARK *));
357 log_mark(SCR
*sp
, LMARK
*lmp
)
363 if (F_ISSET(ep
, F_NOLOG
))
366 /* Put out one initial cursor record per set of changes. */
367 if (ep
->l_cursor
.lno
!= OOBLNO
) {
368 if (log_cursor1(sp
, LOG_CURSOR_INIT
))
370 ep
->l_cursor
.lno
= OOBLNO
;
374 BINC_RETC(sp
, sp
->wp
->l_lp
,
375 sp
->wp
->l_len
, sizeof(u_char
) + sizeof(LMARK
));
376 sp
->wp
->l_lp
[0] = LOG_MARK
;
377 memmove(sp
->wp
->l_lp
+ sizeof(u_char
), lmp
, sizeof(LMARK
));
379 memset(&key
, 0, sizeof(key
));
380 key
.data
= &ep
->l_cur
;
381 key
.size
= sizeof(db_recno_t
);
382 memset(&data
, 0, sizeof(data
));
383 data
.data
= sp
->wp
->l_lp
;
384 data
.size
= sizeof(u_char
) + sizeof(LMARK
);
385 if (ep
->log
->put(ep
->log
, &key
, &data
, 0) == -1)
388 #if defined(LOGDEBUG) && defined(TRACE)
389 vtrace("%lu: mark %c: %lu/%u\n",
390 ep
->l_cur
, lmp
->name
, lmp
->lno
, lmp
->cno
);
392 /* Reset high water mark. */
393 ep
->l_high
= ++ep
->l_cur
;
399 * Roll the log backward one operation.
401 * PUBLIC: int log_backward __P((SCR *, MARK *));
404 log_backward(SCR
*sp
, MARK
*rp
)
415 if (F_ISSET(ep
, F_NOLOG
)) {
417 "010|Logging not being performed, undo not possible");
421 if (ep
->l_cur
== 1) {
422 msgq(sp
, M_BERR
, "011|No changes to undo");
426 if (ep
->l_win
&& ep
->l_win
!= sp
->wp
) {
427 ex_emsg(sp
, NULL
, EXM_LOCKED
);
433 F_SET(ep
, F_NOLOG
); /* Turn off logging. */
435 key
.data
= &ep
->l_cur
; /* Initialize db request. */
436 key
.size
= sizeof(recno_t
);
439 if (ep
->log
->get(ep
->log
, &key
, &data
, 0))
441 #if defined(LOGDEBUG) && defined(TRACE)
442 log_trace(sp
, "log_backward", ep
->l_cur
, data
.data
);
444 switch (*(p
= (u_char
*)data
.data
)) {
445 case LOG_CURSOR_INIT
:
447 memmove(rp
, p
+ sizeof(u_char
), sizeof(MARK
));
455 case LOG_LINE_APPEND_F
:
457 memmove(&lno
, p
+ sizeof(u_char
), sizeof(db_recno_t
));
458 if (db_delete(sp
, lno
))
460 ++sp
->rptlines
[L_DELETED
];
462 case LOG_LINE_DELETE_B
:
464 memmove(&lno
, p
+ sizeof(u_char
), sizeof(db_recno_t
));
465 if (db_insert(sp
, lno
,
466 (CHAR_T
*)(p
+ CHAR_T_OFFSET
),
467 (data
.size
- CHAR_T_OFFSET
) / sizeof(CHAR_T
)))
469 ++sp
->rptlines
[L_ADDED
];
471 case LOG_LINE_RESET_F
:
473 case LOG_LINE_RESET_B
:
475 memmove(&lno
, p
+ sizeof(u_char
), sizeof(db_recno_t
));
477 (CHAR_T
*)(p
+ CHAR_T_OFFSET
),
478 (data
.size
- CHAR_T_OFFSET
) / sizeof(CHAR_T
)))
480 if (sp
->rptlchange
!= lno
) {
481 sp
->rptlchange
= lno
;
482 ++sp
->rptlines
[L_CHANGED
];
487 memmove(&lm
, p
+ sizeof(u_char
), sizeof(LMARK
));
490 if (mark_set(sp
, lm
.name
, &m
, 0))
498 err
: F_CLR(ep
, F_NOLOG
);
505 * Reset the line to its original appearance.
508 * There's a bug in this code due to our not logging cursor movements
509 * unless a change was made. If you do a change, move off the line,
510 * then move back on and do a 'U', the line will be restored to the way
511 * it was before the original change.
513 * PUBLIC: int log_setline __P((SCR *));
526 if (F_ISSET(ep
, F_NOLOG
)) {
528 "012|Logging not being performed, undo not possible");
535 if (ep
->l_win
&& ep
->l_win
!= sp
->wp
) {
536 ex_emsg(sp
, NULL
, EXM_LOCKED
);
541 F_SET(ep
, F_NOLOG
); /* Turn off logging. */
543 key
.data
= &ep
->l_cur
; /* Initialize db request. */
544 key
.size
= sizeof(recno_t
);
548 if (ep
->log
->get(ep
->log
, &key
, &data
, 0))
550 #if defined(LOGDEBUG) && defined(TRACE)
551 log_trace(sp
, "log_setline", ep
->l_cur
, data
.data
);
553 switch (*(p
= (u_char
*)data
.data
)) {
554 case LOG_CURSOR_INIT
:
555 memmove(&m
, p
+ sizeof(u_char
), sizeof(MARK
));
556 if (m
.lno
!= sp
->lno
|| ep
->l_cur
== 1) {
563 memmove(&m
, p
+ sizeof(u_char
), sizeof(MARK
));
564 if (m
.lno
!= sp
->lno
) {
571 case LOG_LINE_APPEND_F
:
572 case LOG_LINE_DELETE_B
:
573 case LOG_LINE_RESET_F
:
575 case LOG_LINE_RESET_B
:
576 memmove(&lno
, p
+ sizeof(u_char
), sizeof(db_recno_t
));
577 if (lno
== sp
->lno
&&
578 db_set(sp
, lno
, (CHAR_T
*)(p
+ CHAR_T_OFFSET
),
579 (data
.size
- CHAR_T_OFFSET
) / sizeof(CHAR_T
)))
581 if (sp
->rptlchange
!= lno
) {
582 sp
->rptlchange
= lno
;
583 ++sp
->rptlines
[L_CHANGED
];
586 memmove(&lm
, p
+ sizeof(u_char
), sizeof(LMARK
));
589 if (mark_set(sp
, lm
.name
, &m
, 0))
597 err
: F_CLR(ep
, F_NOLOG
);
604 * Roll the log forward one operation.
606 * PUBLIC: int log_forward __P((SCR *, MARK *));
609 log_forward(SCR
*sp
, MARK
*rp
)
620 if (F_ISSET(ep
, F_NOLOG
)) {
622 "013|Logging not being performed, roll-forward not possible");
626 if (ep
->l_cur
== ep
->l_high
) {
627 msgq(sp
, M_BERR
, "014|No changes to re-do");
631 if (ep
->l_win
&& ep
->l_win
!= sp
->wp
) {
632 ex_emsg(sp
, NULL
, EXM_LOCKED
);
637 F_SET(ep
, F_NOLOG
); /* Turn off logging. */
639 key
.data
= &ep
->l_cur
; /* Initialize db request. */
640 key
.size
= sizeof(recno_t
);
643 if (ep
->log
->get(ep
->log
, &key
, &data
, 0))
645 #if defined(LOGDEBUG) && defined(TRACE)
646 log_trace(sp
, "log_forward", ep
->l_cur
, data
.data
);
648 switch (*(p
= (u_char
*)data
.data
)) {
652 memmove(rp
, p
+ sizeof(u_char
), sizeof(MARK
));
658 case LOG_CURSOR_INIT
:
660 case LOG_LINE_APPEND_F
:
662 memmove(&lno
, p
+ sizeof(u_char
), sizeof(db_recno_t
));
663 if (db_insert(sp
, lno
,
664 (CHAR_T
*)(p
+ CHAR_T_OFFSET
),
665 (data
.size
- CHAR_T_OFFSET
) / sizeof(CHAR_T
)))
667 ++sp
->rptlines
[L_ADDED
];
669 case LOG_LINE_DELETE_B
:
671 memmove(&lno
, p
+ sizeof(u_char
), sizeof(db_recno_t
));
672 if (db_delete(sp
, lno
))
674 ++sp
->rptlines
[L_DELETED
];
676 case LOG_LINE_RESET_B
:
678 case LOG_LINE_RESET_F
:
680 memmove(&lno
, p
+ sizeof(u_char
), sizeof(db_recno_t
));
682 (CHAR_T
*)(p
+ CHAR_T_OFFSET
),
683 (data
.size
- CHAR_T_OFFSET
) / sizeof(CHAR_T
)))
685 if (sp
->rptlchange
!= lno
) {
686 sp
->rptlchange
= lno
;
687 ++sp
->rptlines
[L_CHANGED
];
692 memmove(&lm
, p
+ sizeof(u_char
), sizeof(LMARK
));
695 if (mark_set(sp
, lm
.name
, &m
, 0))
703 err
: F_CLR(ep
, F_NOLOG
);
710 * Try and restart the log on failure, i.e. if we run out of memory.
713 log_err(SCR
*sp
, const char *file
, int line
)
717 msgq(sp
, M_SYSERR
, "015|%s/%d: log put error", tail(file
), line
);
719 (void)ep
->log
->close(ep
->log
);
720 if (!log_init(sp
, ep
))
721 msgq(sp
, M_ERR
, "267|Log restarted");
724 #if defined(LOGDEBUG) && defined(TRACE)
726 log_trace(sp
, msg
, rno
, p
)
737 case LOG_CURSOR_INIT
:
738 memmove(&m
, p
+ sizeof(u_char
), sizeof(MARK
));
739 vtrace("%lu: %s: C_INIT: %u/%u\n", rno
, msg
, m
.lno
, m
.cno
);
742 memmove(&m
, p
+ sizeof(u_char
), sizeof(MARK
));
743 vtrace("%lu: %s: C_END: %u/%u\n", rno
, msg
, m
.lno
, m
.cno
);
745 case LOG_LINE_APPEND_F
:
746 memmove(&lno
, p
+ sizeof(u_char
), sizeof(db_recno_t
));
747 vtrace("%lu: %s: APPEND_F: %lu\n", rno
, msg
, lno
);
749 case LOG_LINE_APPEND_B
:
750 memmove(&lno
, p
+ sizeof(u_char
), sizeof(db_recno_t
));
751 vtrace("%lu: %s: APPEND_B: %lu\n", rno
, msg
, lno
);
753 case LOG_LINE_DELETE_F
:
754 memmove(&lno
, p
+ sizeof(u_char
), sizeof(db_recno_t
));
755 vtrace("%lu: %s: DELETE_F: %lu\n", rno
, msg
, lno
);
757 case LOG_LINE_DELETE_B
:
758 memmove(&lno
, p
+ sizeof(u_char
), sizeof(db_recno_t
));
759 vtrace("%lu: %s: DELETE_B: %lu\n", rno
, msg
, lno
);
761 case LOG_LINE_RESET_F
:
762 memmove(&lno
, p
+ sizeof(u_char
), sizeof(db_recno_t
));
763 vtrace("%lu: %s: RESET_F: %lu\n", rno
, msg
, lno
);
765 case LOG_LINE_RESET_B
:
766 memmove(&lno
, p
+ sizeof(u_char
), sizeof(db_recno_t
));
767 vtrace("%lu: %s: RESET_B: %lu\n", rno
, msg
, lno
);
770 memmove(&lm
, p
+ sizeof(u_char
), sizeof(LMARK
));
771 vtrace("%lu: %s: MARK: %u/%u\n", rno
, msg
, lm
.lno
, lm
.cno
);