2 * Copyright (c) 1992, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 * Copyright (c) 1992, 1993, 1994, 1995, 1996
5 * Keith Bostic. All rights reserved.
7 * See the LICENSE file for redistribution information.
13 static const char sccsid
[] = "$Id: log.c,v 10.16 2000/07/22 10:20:31 skimo Exp $ (Berkeley) $Date: 2000/07/22 10:20:31 $";
16 #include <sys/types.h>
17 #include <sys/queue.h>
20 #include <bitstring.h>
31 * The log consists of records, each containing a type byte and a variable
32 * length byte string, as follows:
34 * LOG_CURSOR_INIT MARK
36 * LOG_LINE_APPEND db_recno_t char *
37 * LOG_LINE_DELETE db_recno_t char *
38 * LOG_LINE_INSERT db_recno_t char *
39 * LOG_LINE_RESET_F db_recno_t char *
40 * LOG_LINE_RESET_B db_recno_t char *
43 * We do before image physical logging. This means that the editor layer
44 * MAY NOT modify records in place, even if simply deleting or overwriting
45 * characters. Since the smallest unit of logging is a line, we're using
46 * up lots of space. This may eventually have to be reduced, probably by
47 * doing logical logging, which is a much cooler database phrase.
49 * The implementation of the historic vi 'u' command, using roll-forward and
50 * roll-back, is simple. Each set of changes has a LOG_CURSOR_INIT record,
51 * followed by a number of other records, followed by a LOG_CURSOR_END record.
52 * LOG_LINE_RESET records come in pairs. The first is a LOG_LINE_RESET_B
53 * record, and is the line before the change. The second is LOG_LINE_RESET_F,
54 * and is the line after the change. Roll-back is done by backing up to the
55 * first LOG_CURSOR_INIT record before a change. Roll-forward is done in a
58 * The 'U' command is implemented by rolling backward to a LOG_CURSOR_END
59 * record for a line different from the current one. It should be noted that
60 * this means that a subsequent 'u' command will make a change based on the
61 * new position of the log's cursor. This is okay, and, in fact, historic vi
65 static int vi_log_get
__P((SCR
*sp
, db_recno_t
*lnop
, size_t *size
));
66 static int log_cursor1
__P((SCR
*, int));
67 static void log_err
__P((SCR
*, char *, int));
68 #if defined(DEBUG) && 0
69 static void log_trace
__P((SCR
*, char *, db_recno_t
, u_char
*));
72 /* Try and restart the log on failure, i.e. if we run out of memory. */
74 log_err(sp, __FILE__, __LINE__); \
80 * Initialize the logging subsystem.
82 * PUBLIC: int log_init __P((SCR *, EXF *));
91 * ep MAY NOT BE THE SAME AS sp->ep, DON'T USE THE LATTER.
93 * Initialize the buffer. The logging subsystem has its own
94 * buffers because the global ones are almost by definition
95 * going to be in use when the log runs.
99 ep
->l_cursor
.lno
= 1; /* XXX Any valid recno. */
100 ep
->l_cursor
.cno
= 0;
101 ep
->l_high
= ep
->l_cur
= 1;
103 if (db_create(&ep
->log
, sp
->gp
->env
, 0) != 0 ||
104 ep
->log
->open(ep
->log
, NULL
, NULL
, DB_RECNO
, DB_CREATE
| DB_THREAD
,
105 S_IRUSR
| S_IWUSR
) != 0) {
106 msgq(sp
, M_SYSERR
, "009|Log file");
116 * Close the logging subsystem.
118 * PUBLIC: int log_end __P((SCR *, EXF *));
127 * ep MAY NOT BE THE SAME AS sp->ep, DON'T USE THE LATTER.
129 if (ep
->log
!= NULL
) {
130 (void)(ep
->log
->close
)(ep
->log
,DB_NOSYNC
);
133 if (sp
->wp
->l_lp
!= NULL
) {
138 ep
->l_cursor
.lno
= 1; /* XXX Any valid recno. */
139 ep
->l_cursor
.cno
= 0;
140 ep
->l_high
= ep
->l_cur
= 1;
146 * Log the current cursor position, starting an event.
148 * PUBLIC: int log_cursor __P((SCR *));
157 if (F_ISSET(ep
, F_NOLOG
))
161 * If any changes were made since the last cursor init,
162 * put out the ending cursor record.
164 if (ep
->l_cursor
.lno
== OOBLNO
) {
165 ep
->l_cursor
.lno
= sp
->lno
;
166 ep
->l_cursor
.cno
= sp
->cno
;
167 return (log_cursor1(sp
, LOG_CURSOR_END
));
169 ep
->l_cursor
.lno
= sp
->lno
;
170 ep
->l_cursor
.cno
= sp
->cno
;
176 * Actually push a cursor record out.
179 log_cursor1(sp
, type
)
187 BINC_RET(sp
, sp
->wp
->l_lp
, sp
->wp
->l_len
, sizeof(u_char
) + sizeof(MARK
));
188 sp
->wp
->l_lp
[0] = type
;
189 memmove(sp
->wp
->l_lp
+ sizeof(u_char
), &ep
->l_cursor
, sizeof(MARK
));
191 memset(&key
, 0, sizeof(key
));
192 key
.data
= &ep
->l_cur
;
193 key
.size
= sizeof(db_recno_t
);
194 memset(&data
, 0, sizeof(data
));
195 data
.data
= sp
->wp
->l_lp
;
196 data
.size
= sizeof(u_char
) + sizeof(MARK
);
197 if (ep
->log
->put(ep
->log
, NULL
, &key
, &data
, 0) == -1)
200 #if defined(DEBUG) && 0
201 vtrace(sp
, "%lu: %s: %u/%u\n", ep
->l_cur
,
202 type
== LOG_CURSOR_INIT
? "log_cursor_init" : "log_cursor_end",
205 /* Reset high water mark. */
206 ep
->l_high
= ++ep
->l_cur
;
215 * PUBLIC: int log_line __P((SCR *, db_recno_t, u_int));
218 log_line(sp
, lno
, action
)
229 if (F_ISSET(ep
, F_NOLOG
))
235 * Kluge for vi. Clear the EXF undo flag so that the
236 * next 'u' command does a roll-back, regardless.
240 /* Put out one initial cursor record per set of changes. */
241 if (ep
->l_cursor
.lno
!= OOBLNO
) {
242 if (log_cursor1(sp
, LOG_CURSOR_INIT
))
244 ep
->l_cursor
.lno
= OOBLNO
;
248 * Put out the changes. If it's a LOG_LINE_RESET_B call, it's a
249 * special case, avoid the caches. Also, if it fails and it's
250 * line 1, it just means that the user started with an empty file,
251 * so fake an empty length line.
253 if (action
== LOG_LINE_RESET_B
) {
254 if (db_get(sp
, lno
, DBG_NOCACHE
, &lp
, &len
)) {
255 static CHAR_T nul
= 0;
264 if (db_get(sp
, lno
, DBG_FATAL
, &lp
, &len
))
267 sp
->wp
->l_lp
, sp
->wp
->l_len
,
268 len
* sizeof(CHAR_T
) + sizeof(u_char
) + sizeof(db_recno_t
));
269 sp
->wp
->l_lp
[0] = action
;
270 memmove(sp
->wp
->l_lp
+ sizeof(u_char
), &lno
, sizeof(db_recno_t
));
271 MEMMOVEW(sp
->wp
->l_lp
+ sizeof(u_char
) + sizeof(db_recno_t
), lp
, len
);
273 memset(&key
, 0, sizeof(key
));
274 key
.data
= &ep
->l_cur
;
275 key
.size
= sizeof(db_recno_t
);
276 memset(&data
, 0, sizeof(data
));
277 data
.data
= sp
->wp
->l_lp
;
278 data
.size
= len
* sizeof(CHAR_T
) +
279 sizeof(u_char
) + sizeof(db_recno_t
);
280 if (ep
->log
->put(ep
->log
, NULL
, &key
, &data
, 0) == -1)
283 #if defined(DEBUG) && 0
285 case LOG_LINE_APPEND
:
286 vtrace(sp
, "%u: log_line: append: %lu {%u}\n",
287 ep
->l_cur
, lno
, len
);
289 case LOG_LINE_DELETE
:
290 vtrace(sp
, "%lu: log_line: delete: %lu {%u}\n",
291 ep
->l_cur
, lno
, len
);
293 case LOG_LINE_INSERT
:
294 vtrace(sp
, "%lu: log_line: insert: %lu {%u}\n",
295 ep
->l_cur
, lno
, len
);
297 case LOG_LINE_RESET_F
:
298 vtrace(sp
, "%lu: log_line: reset_f: %lu {%u}\n",
299 ep
->l_cur
, lno
, len
);
301 case LOG_LINE_RESET_B
:
302 vtrace(sp
, "%lu: log_line: reset_b: %lu {%u}\n",
303 ep
->l_cur
, lno
, len
);
307 /* Reset high water mark. */
308 ep
->l_high
= ++ep
->l_cur
;
315 * Log a mark position. For the log to work, we assume that there
316 * aren't any operations that just put out a log record -- this
317 * would mean that undo operations would only reset marks, and not
318 * cause any other change.
320 * PUBLIC: int log_mark __P((SCR *, LMARK *));
331 if (F_ISSET(ep
, F_NOLOG
))
334 /* Put out one initial cursor record per set of changes. */
335 if (ep
->l_cursor
.lno
!= OOBLNO
) {
336 if (log_cursor1(sp
, LOG_CURSOR_INIT
))
338 ep
->l_cursor
.lno
= OOBLNO
;
341 BINC_RET(sp
, sp
->wp
->l_lp
,
342 sp
->wp
->l_len
, sizeof(u_char
) + sizeof(LMARK
));
343 sp
->wp
->l_lp
[0] = LOG_MARK
;
344 memmove(sp
->wp
->l_lp
+ sizeof(u_char
), lmp
, sizeof(LMARK
));
346 memset(&key
, 0, sizeof(key
));
347 key
.data
= &ep
->l_cur
;
348 key
.size
= sizeof(db_recno_t
);
349 memset(&data
, 0, sizeof(data
));
350 data
.data
= sp
->wp
->l_lp
;
351 data
.size
= sizeof(u_char
) + sizeof(LMARK
);
352 if (ep
->log
->put(ep
->log
, NULL
, &key
, &data
, 0) == -1)
355 #if defined(DEBUG) && 0
356 vtrace(sp
, "%lu: mark %c: %lu/%u\n",
357 ep
->l_cur
, lmp
->name
, lmp
->lno
, lmp
->cno
);
359 /* Reset high water mark. */
360 ep
->l_high
= ++ep
->l_cur
;
366 * Get a line from the log in log buffer.
369 vi_log_get(SCR
*sp
, db_recno_t
*lnop
, size_t *size
)
379 BINC_RET(sp
, sp
->wp
->l_lp
, sp
->wp
->l_len
, nlen
);
381 memset(&key
, 0, sizeof(key
));
382 key
.data
= lnop
; /* Initialize db request. */
383 key
.size
= sizeof(db_recno_t
);
384 memset(&data
, 0, sizeof(data
));
385 data
.data
= sp
->wp
->l_lp
;
386 data
.ulen
= sp
->wp
->l_len
;
387 data
.flags
= DB_DBT_USERMEM
;
388 switch (ep
->log
->get(ep
->log
, NULL
, &key
, &data
, 0)) {
402 * Roll the log backward one operation.
404 * PUBLIC: int log_backward __P((SCR *, MARK *));
420 if (F_ISSET(ep
, F_NOLOG
)) {
422 "010|Logging not being performed, undo not possible");
426 if (ep
->l_cur
== 1) {
427 msgq(sp
, M_BERR
, "011|No changes to undo");
431 F_SET(ep
, F_NOLOG
); /* Turn off logging. */
435 if (vi_log_get(sp
, &ep
->l_cur
, &size
))
437 #if defined(DEBUG) && 0
438 log_trace(sp
, "log_backward", ep
->l_cur
, data
.data
);
440 switch (*(p
= (u_char
*)sp
->wp
->l_lp
)) {
441 case LOG_CURSOR_INIT
:
443 memmove(rp
, p
+ sizeof(u_char
), sizeof(MARK
));
450 case LOG_LINE_APPEND
:
451 case LOG_LINE_INSERT
:
453 memmove(&lno
, p
+ sizeof(u_char
), sizeof(db_recno_t
));
454 if (db_delete(sp
, lno
))
456 ++sp
->rptlines
[L_DELETED
];
458 case LOG_LINE_DELETE
:
460 memmove(&lno
, p
+ sizeof(u_char
), sizeof(db_recno_t
));
461 if (db_insert(sp
, lno
,
462 (CHAR_T
*)(p
+ sizeof(u_char
) +
464 (size
- sizeof(u_char
) - sizeof(db_recno_t
))
467 ++sp
->rptlines
[L_ADDED
];
469 case LOG_LINE_RESET_F
:
471 case LOG_LINE_RESET_B
:
473 memmove(&lno
, p
+ sizeof(u_char
), sizeof(db_recno_t
));
475 (CHAR_T
*)(p
+ sizeof(u_char
) +
477 (size
- sizeof(u_char
) - sizeof(db_recno_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
);
504 * Reset the line to its original appearance.
507 * There's a bug in this code due to our not logging cursor movements
508 * unless a change was made. If you do a change, move off the line,
509 * then move back on and do a 'U', the line will be restored to the way
510 * it was before the original change.
512 * PUBLIC: int log_setline __P((SCR *));
526 if (F_ISSET(ep
, F_NOLOG
)) {
528 "012|Logging not being performed, undo not possible");
535 F_SET(ep
, F_NOLOG
); /* Turn off logging. */
539 if (vi_log_get(sp
, &ep
->l_cur
, &size
))
541 #if defined(DEBUG) && 0
542 log_trace(sp
, "log_setline", ep
->l_cur
, data
.data
);
544 switch (*(p
= (u_char
*)sp
->wp
->l_lp
)) {
545 case LOG_CURSOR_INIT
:
546 memmove(&m
, p
+ sizeof(u_char
), sizeof(MARK
));
547 if (m
.lno
!= sp
->lno
|| ep
->l_cur
== 1) {
553 memmove(&m
, p
+ sizeof(u_char
), sizeof(MARK
));
554 if (m
.lno
!= sp
->lno
) {
560 case LOG_LINE_APPEND
:
561 case LOG_LINE_INSERT
:
562 case LOG_LINE_DELETE
:
563 case LOG_LINE_RESET_F
:
565 case LOG_LINE_RESET_B
:
566 memmove(&lno
, p
+ sizeof(u_char
), sizeof(db_recno_t
));
567 if (lno
== sp
->lno
&&
568 db_set(sp
, lno
, (CHAR_T
*)(p
+ sizeof(u_char
) +
569 sizeof(db_recno_t
)), size
- sizeof(u_char
) -
572 if (sp
->rptlchange
!= lno
) {
573 sp
->rptlchange
= lno
;
574 ++sp
->rptlines
[L_CHANGED
];
577 memmove(&lm
, p
+ sizeof(u_char
), sizeof(LMARK
));
580 if (mark_set(sp
, lm
.name
, &m
, 0))
588 err
: F_CLR(ep
, F_NOLOG
);
594 * Roll the log forward one operation.
596 * PUBLIC: int log_forward __P((SCR *, MARK *));
612 if (F_ISSET(ep
, F_NOLOG
)) {
614 "013|Logging not being performed, roll-forward not possible");
618 if (ep
->l_cur
== ep
->l_high
) {
619 msgq(sp
, M_BERR
, "014|No changes to re-do");
623 F_SET(ep
, F_NOLOG
); /* Turn off logging. */
627 if (vi_log_get(sp
, &ep
->l_cur
, &size
))
629 #if defined(DEBUG) && 0
630 log_trace(sp
, "log_forward", ep
->l_cur
, data
.data
);
632 switch (*(p
= (u_char
*)sp
->wp
->l_lp
)) {
636 memmove(rp
, p
+ sizeof(u_char
), sizeof(MARK
));
641 case LOG_CURSOR_INIT
:
643 /* XXXX LOG_LINE_APPEND and LOG_LINE_INSERT split
644 for now, because db_insert won't work for adding
647 case LOG_LINE_APPEND
:
649 memmove(&lno
, p
+ sizeof(u_char
), sizeof(db_recno_t
));
651 if (db_append(sp
, 1, lno
,
652 (CHAR_T
*)(p
+ sizeof(u_char
) +
654 (size
- sizeof(u_char
) - sizeof(db_recno_t
))
657 ++sp
->rptlines
[L_ADDED
];
659 case LOG_LINE_INSERT
:
661 memmove(&lno
, p
+ sizeof(u_char
), sizeof(db_recno_t
));
662 if (db_insert(sp
, lno
,
663 (CHAR_T
*)(p
+ sizeof(u_char
) +
665 (size
- sizeof(u_char
) - sizeof(db_recno_t
))
668 ++sp
->rptlines
[L_ADDED
];
670 case LOG_LINE_DELETE
:
672 memmove(&lno
, p
+ sizeof(u_char
), sizeof(db_recno_t
));
673 if (db_delete(sp
, lno
))
675 ++sp
->rptlines
[L_DELETED
];
677 case LOG_LINE_RESET_B
:
679 case LOG_LINE_RESET_F
:
681 memmove(&lno
, p
+ sizeof(u_char
), sizeof(db_recno_t
));
683 (CHAR_T
*)(p
+ sizeof(u_char
) +
685 (size
- sizeof(u_char
) - sizeof(db_recno_t
))
688 if (sp
->rptlchange
!= lno
) {
689 sp
->rptlchange
= lno
;
690 ++sp
->rptlines
[L_CHANGED
];
695 memmove(&lm
, p
+ sizeof(u_char
), sizeof(LMARK
));
698 if (mark_set(sp
, lm
.name
, &m
, 0))
706 err
: F_CLR(ep
, F_NOLOG
);
712 * Try and restart the log on failure, i.e. if we run out of memory.
715 log_err(sp
, file
, line
)
722 msgq(sp
, M_SYSERR
, "015|%s/%d: log put error", tail(file
), line
);
724 (void)ep
->log
->close(ep
->log
, DB_NOSYNC
);
725 if (!log_init(sp
, ep
))
726 msgq(sp
, M_ERR
, "267|Log restarted");
729 #if defined(DEBUG) && 0
731 log_trace(sp
, msg
, rno
, p
)
742 case LOG_CURSOR_INIT
:
743 memmove(&m
, p
+ sizeof(u_char
), sizeof(MARK
));
744 vtrace(sp
, "%lu: %s: C_INIT: %u/%u\n", rno
, msg
, m
.lno
, m
.cno
);
747 memmove(&m
, p
+ sizeof(u_char
), sizeof(MARK
));
748 vtrace(sp
, "%lu: %s: C_END: %u/%u\n", rno
, msg
, m
.lno
, m
.cno
);
750 case LOG_LINE_APPEND
:
751 memmove(&lno
, p
+ sizeof(u_char
), sizeof(db_recno_t
));
752 vtrace(sp
, "%lu: %s: APPEND: %lu\n", rno
, msg
, lno
);
754 case LOG_LINE_INSERT
:
755 memmove(&lno
, p
+ sizeof(u_char
), sizeof(db_recno_t
));
756 vtrace(sp
, "%lu: %s: INSERT: %lu\n", rno
, msg
, lno
);
758 case LOG_LINE_DELETE
:
759 memmove(&lno
, p
+ sizeof(u_char
), sizeof(db_recno_t
));
760 vtrace(sp
, "%lu: %s: DELETE: %lu\n", rno
, msg
, lno
);
762 case LOG_LINE_RESET_F
:
763 memmove(&lno
, p
+ sizeof(u_char
), sizeof(db_recno_t
));
764 vtrace(sp
, "%lu: %s: RESET_F: %lu\n", rno
, msg
, lno
);
766 case LOG_LINE_RESET_B
:
767 memmove(&lno
, p
+ sizeof(u_char
), sizeof(db_recno_t
));
768 vtrace(sp
, "%lu: %s: RESET_B: %lu\n", rno
, msg
, lno
);
771 memmove(&lm
, p
+ sizeof(u_char
), sizeof(LMARK
));
773 "%lu: %s: MARK: %u/%u\n", rno
, msg
, lm
.lno
, lm
.cno
);