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.12 2000/04/21 21:26:19 skimo Exp $ (Berkeley) $Date: 2000/04/21 21:26:19 $";
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 log_cursor1
__P((SCR
*, int));
66 static void log_err
__P((SCR
*, char *, int));
67 #if defined(DEBUG) && 0
68 static void log_trace
__P((SCR
*, char *, db_recno_t
, u_char
*));
71 /* Try and restart the log on failure, i.e. if we run out of memory. */
73 log_err(sp, __FILE__, __LINE__); \
79 * Initialize the logging subsystem.
81 * PUBLIC: int log_init __P((SCR *, EXF *));
90 * ep MAY NOT BE THE SAME AS sp->ep, DON'T USE THE LATTER.
92 * Initialize the buffer. The logging subsystem has its own
93 * buffers because the global ones are almost by definition
94 * going to be in use when the log runs.
98 ep
->l_cursor
.lno
= 1; /* XXX Any valid recno. */
100 ep
->l_high
= ep
->l_cur
= 1;
102 if (db_create(&ep
->log
, sp
->gp
->env
, 0) != 0 ||
103 ep
->log
->open(ep
->log
, NULL
, NULL
, DB_RECNO
, DB_CREATE
,
104 S_IRUSR
| S_IWUSR
) != 0) {
105 msgq(sp
, M_SYSERR
, "009|Log file");
115 * Close the logging subsystem.
117 * PUBLIC: int log_end __P((SCR *, EXF *));
126 * ep MAY NOT BE THE SAME AS sp->ep, DON'T USE THE LATTER.
128 if (ep
->log
!= NULL
) {
129 (void)(ep
->log
->close
)(ep
->log
,DB_NOSYNC
);
132 if (ep
->l_lp
!= NULL
) {
137 ep
->l_cursor
.lno
= 1; /* XXX Any valid recno. */
138 ep
->l_cursor
.cno
= 0;
139 ep
->l_high
= ep
->l_cur
= 1;
145 * Log the current cursor position, starting an event.
147 * PUBLIC: int log_cursor __P((SCR *));
156 if (F_ISSET(ep
, F_NOLOG
))
160 * If any changes were made since the last cursor init,
161 * put out the ending cursor record.
163 if (ep
->l_cursor
.lno
== OOBLNO
) {
164 ep
->l_cursor
.lno
= sp
->lno
;
165 ep
->l_cursor
.cno
= sp
->cno
;
166 return (log_cursor1(sp
, LOG_CURSOR_END
));
168 ep
->l_cursor
.lno
= sp
->lno
;
169 ep
->l_cursor
.cno
= sp
->cno
;
175 * Actually push a cursor record out.
178 log_cursor1(sp
, type
)
186 BINC_RET(sp
, ep
->l_lp
, ep
->l_len
, sizeof(u_char
) + sizeof(MARK
));
188 memmove(ep
->l_lp
+ sizeof(u_char
), &ep
->l_cursor
, sizeof(MARK
));
190 memset(&key
, 0, sizeof(key
));
191 key
.data
= &ep
->l_cur
;
192 key
.size
= sizeof(db_recno_t
);
193 memset(&data
, 0, sizeof(data
));
194 data
.data
= ep
->l_lp
;
195 data
.size
= sizeof(u_char
) + sizeof(MARK
);
196 if (ep
->log
->put(ep
->log
, NULL
, &key
, &data
, 0) == -1)
199 #if defined(DEBUG) && 0
200 vtrace(sp
, "%lu: %s: %u/%u\n", ep
->l_cur
,
201 type
== LOG_CURSOR_INIT
? "log_cursor_init" : "log_cursor_end",
204 /* Reset high water mark. */
205 ep
->l_high
= ++ep
->l_cur
;
214 * PUBLIC: int log_line __P((SCR *, db_recno_t, u_int));
217 log_line(sp
, lno
, action
)
228 if (F_ISSET(ep
, F_NOLOG
))
234 * Kluge for vi. Clear the EXF undo flag so that the
235 * next 'u' command does a roll-back, regardless.
239 /* Put out one initial cursor record per set of changes. */
240 if (ep
->l_cursor
.lno
!= OOBLNO
) {
241 if (log_cursor1(sp
, LOG_CURSOR_INIT
))
243 ep
->l_cursor
.lno
= OOBLNO
;
247 * Put out the changes. If it's a LOG_LINE_RESET_B call, it's a
248 * special case, avoid the caches. Also, if it fails and it's
249 * line 1, it just means that the user started with an empty file,
250 * so fake an empty length line.
252 if (action
== LOG_LINE_RESET_B
) {
253 if (db_get(sp
, lno
, DBG_NOCACHE
, &lp
, &len
)) {
262 if (db_get(sp
, lno
, DBG_FATAL
, &lp
, &len
))
265 ep
->l_lp
, ep
->l_len
, len
+ sizeof(u_char
) + sizeof(db_recno_t
));
266 ep
->l_lp
[0] = action
;
267 memmove(ep
->l_lp
+ sizeof(u_char
), &lno
, sizeof(db_recno_t
));
268 memmove(ep
->l_lp
+ sizeof(u_char
) + sizeof(db_recno_t
), lp
, len
);
270 memset(&key
, 0, sizeof(key
));
271 key
.data
= &ep
->l_cur
;
272 key
.size
= sizeof(db_recno_t
);
273 memset(&data
, 0, sizeof(data
));
274 data
.data
= ep
->l_lp
;
275 data
.size
= len
+ sizeof(u_char
) + sizeof(db_recno_t
);
276 if (ep
->log
->put(ep
->log
, NULL
, &key
, &data
, 0) == -1)
279 #if defined(DEBUG) && 0
281 case LOG_LINE_APPEND
:
282 vtrace(sp
, "%u: log_line: append: %lu {%u}\n",
283 ep
->l_cur
, lno
, len
);
285 case LOG_LINE_DELETE
:
286 vtrace(sp
, "%lu: log_line: delete: %lu {%u}\n",
287 ep
->l_cur
, lno
, len
);
289 case LOG_LINE_INSERT
:
290 vtrace(sp
, "%lu: log_line: insert: %lu {%u}\n",
291 ep
->l_cur
, lno
, len
);
293 case LOG_LINE_RESET_F
:
294 vtrace(sp
, "%lu: log_line: reset_f: %lu {%u}\n",
295 ep
->l_cur
, lno
, len
);
297 case LOG_LINE_RESET_B
:
298 vtrace(sp
, "%lu: log_line: reset_b: %lu {%u}\n",
299 ep
->l_cur
, lno
, len
);
303 /* Reset high water mark. */
304 ep
->l_high
= ++ep
->l_cur
;
311 * Log a mark position. For the log to work, we assume that there
312 * aren't any operations that just put out a log record -- this
313 * would mean that undo operations would only reset marks, and not
314 * cause any other change.
316 * PUBLIC: int log_mark __P((SCR *, LMARK *));
327 if (F_ISSET(ep
, F_NOLOG
))
330 /* Put out one initial cursor record per set of changes. */
331 if (ep
->l_cursor
.lno
!= OOBLNO
) {
332 if (log_cursor1(sp
, LOG_CURSOR_INIT
))
334 ep
->l_cursor
.lno
= OOBLNO
;
337 BINC_RET(sp
, ep
->l_lp
,
338 ep
->l_len
, sizeof(u_char
) + sizeof(LMARK
));
339 ep
->l_lp
[0] = LOG_MARK
;
340 memmove(ep
->l_lp
+ sizeof(u_char
), lmp
, sizeof(LMARK
));
342 memset(&key
, 0, sizeof(key
));
343 key
.data
= &ep
->l_cur
;
344 key
.size
= sizeof(db_recno_t
);
345 memset(&data
, 0, sizeof(data
));
346 data
.data
= ep
->l_lp
;
347 data
.size
= sizeof(u_char
) + sizeof(LMARK
);
348 if (ep
->log
->put(ep
->log
, NULL
, &key
, &data
, 0) == -1)
351 #if defined(DEBUG) && 0
352 vtrace(sp
, "%lu: mark %c: %lu/%u\n",
353 ep
->l_cur
, lmp
->name
, lmp
->lno
, lmp
->cno
);
355 /* Reset high water mark. */
356 ep
->l_high
= ++ep
->l_cur
;
362 * Roll the log backward one operation.
364 * PUBLIC: int log_backward __P((SCR *, MARK *));
380 if (F_ISSET(ep
, F_NOLOG
)) {
382 "010|Logging not being performed, undo not possible");
386 if (ep
->l_cur
== 1) {
387 msgq(sp
, M_BERR
, "011|No changes to undo");
391 F_SET(ep
, F_NOLOG
); /* Turn off logging. */
393 memset(&key
, 0, sizeof(key
));
394 key
.data
= &ep
->l_cur
; /* Initialize db request. */
395 key
.size
= sizeof(db_recno_t
);
396 memset(&data
, 0, sizeof(data
));
399 if (ep
->log
->get(ep
->log
, NULL
, &key
, &data
, 0))
401 #if defined(DEBUG) && 0
402 log_trace(sp
, "log_backward", ep
->l_cur
, data
.data
);
404 switch (*(p
= (u_char
*)data
.data
)) {
405 case LOG_CURSOR_INIT
:
407 memmove(rp
, p
+ sizeof(u_char
), sizeof(MARK
));
414 case LOG_LINE_APPEND
:
415 case LOG_LINE_INSERT
:
417 memmove(&lno
, p
+ sizeof(u_char
), sizeof(db_recno_t
));
418 if (db_delete(sp
, lno
))
420 ++sp
->rptlines
[L_DELETED
];
422 case LOG_LINE_DELETE
:
424 memmove(&lno
, p
+ sizeof(u_char
), sizeof(db_recno_t
));
425 if (db_insert(sp
, lno
, p
+ sizeof(u_char
) +
426 sizeof(db_recno_t
), data
.size
- sizeof(u_char
) -
429 ++sp
->rptlines
[L_ADDED
];
431 case LOG_LINE_RESET_F
:
433 case LOG_LINE_RESET_B
:
435 memmove(&lno
, p
+ sizeof(u_char
), sizeof(db_recno_t
));
436 if (db_set(sp
, lno
, p
+ sizeof(u_char
) +
437 sizeof(db_recno_t
), data
.size
- sizeof(u_char
) -
440 if (sp
->rptlchange
!= lno
) {
441 sp
->rptlchange
= lno
;
442 ++sp
->rptlines
[L_CHANGED
];
447 memmove(&lm
, p
+ sizeof(u_char
), sizeof(LMARK
));
450 if (mark_set(sp
, lm
.name
, &m
, 0))
458 err
: F_CLR(ep
, F_NOLOG
);
464 * Reset the line to its original appearance.
467 * There's a bug in this code due to our not logging cursor movements
468 * unless a change was made. If you do a change, move off the line,
469 * then move back on and do a 'U', the line will be restored to the way
470 * it was before the original change.
472 * PUBLIC: int log_setline __P((SCR *));
486 if (F_ISSET(ep
, F_NOLOG
)) {
488 "012|Logging not being performed, undo not possible");
495 F_SET(ep
, F_NOLOG
); /* Turn off logging. */
497 memset(&key
, 0, sizeof(key
));
498 key
.data
= &ep
->l_cur
; /* Initialize db request. */
499 key
.size
= sizeof(db_recno_t
);
500 memset(&data
, 0, sizeof(data
));
504 if (ep
->log
->get(ep
->log
, NULL
, &key
, &data
, 0))
506 #if defined(DEBUG) && 0
507 log_trace(sp
, "log_setline", ep
->l_cur
, data
.data
);
509 switch (*(p
= (u_char
*)data
.data
)) {
510 case LOG_CURSOR_INIT
:
511 memmove(&m
, p
+ sizeof(u_char
), sizeof(MARK
));
512 if (m
.lno
!= sp
->lno
|| ep
->l_cur
== 1) {
518 memmove(&m
, p
+ sizeof(u_char
), sizeof(MARK
));
519 if (m
.lno
!= sp
->lno
) {
525 case LOG_LINE_APPEND
:
526 case LOG_LINE_INSERT
:
527 case LOG_LINE_DELETE
:
528 case LOG_LINE_RESET_F
:
530 case LOG_LINE_RESET_B
:
531 memmove(&lno
, p
+ sizeof(u_char
), sizeof(db_recno_t
));
532 if (lno
== sp
->lno
&&
533 db_set(sp
, lno
, p
+ sizeof(u_char
) +
534 sizeof(db_recno_t
), data
.size
- sizeof(u_char
) -
537 if (sp
->rptlchange
!= lno
) {
538 sp
->rptlchange
= lno
;
539 ++sp
->rptlines
[L_CHANGED
];
542 memmove(&lm
, p
+ sizeof(u_char
), sizeof(LMARK
));
545 if (mark_set(sp
, lm
.name
, &m
, 0))
553 err
: F_CLR(ep
, F_NOLOG
);
559 * Roll the log forward one operation.
561 * PUBLIC: int log_forward __P((SCR *, MARK *));
577 if (F_ISSET(ep
, F_NOLOG
)) {
579 "013|Logging not being performed, roll-forward not possible");
583 if (ep
->l_cur
== ep
->l_high
) {
584 msgq(sp
, M_BERR
, "014|No changes to re-do");
588 F_SET(ep
, F_NOLOG
); /* Turn off logging. */
590 memset(&key
, 0, sizeof(key
));
591 key
.data
= &ep
->l_cur
; /* Initialize db request. */
592 key
.size
= sizeof(db_recno_t
);
593 memset(&data
, 0, sizeof(data
));
596 if (ep
->log
->get(ep
->log
, NULL
, &key
, &data
, 0))
598 #if defined(DEBUG) && 0
599 log_trace(sp
, "log_forward", ep
->l_cur
, data
.data
);
601 switch (*(p
= (u_char
*)data
.data
)) {
605 memmove(rp
, p
+ sizeof(u_char
), sizeof(MARK
));
610 case LOG_CURSOR_INIT
:
612 /* XXXX LOG_LINE_APPEND and LOG_LINE_INSERT split
613 for now, because db_insert won't work for adding
616 case LOG_LINE_APPEND
:
618 memmove(&lno
, p
+ sizeof(u_char
), sizeof(db_recno_t
));
620 if (db_append(sp
, 1, lno
, p
+ sizeof(u_char
) +
621 sizeof(db_recno_t
), data
.size
- sizeof(u_char
) -
624 ++sp
->rptlines
[L_ADDED
];
626 case LOG_LINE_INSERT
:
628 memmove(&lno
, p
+ sizeof(u_char
), sizeof(db_recno_t
));
629 if (db_insert(sp
, lno
, p
+ sizeof(u_char
) +
630 sizeof(db_recno_t
), data
.size
- sizeof(u_char
) -
633 ++sp
->rptlines
[L_ADDED
];
635 case LOG_LINE_DELETE
:
637 memmove(&lno
, p
+ sizeof(u_char
), sizeof(db_recno_t
));
638 if (db_delete(sp
, lno
))
640 ++sp
->rptlines
[L_DELETED
];
642 case LOG_LINE_RESET_B
:
644 case LOG_LINE_RESET_F
:
646 memmove(&lno
, p
+ sizeof(u_char
), sizeof(db_recno_t
));
647 if (db_set(sp
, lno
, p
+ sizeof(u_char
) +
648 sizeof(db_recno_t
), data
.size
- sizeof(u_char
) -
651 if (sp
->rptlchange
!= lno
) {
652 sp
->rptlchange
= lno
;
653 ++sp
->rptlines
[L_CHANGED
];
658 memmove(&lm
, p
+ sizeof(u_char
), sizeof(LMARK
));
661 if (mark_set(sp
, lm
.name
, &m
, 0))
669 err
: F_CLR(ep
, F_NOLOG
);
675 * Try and restart the log on failure, i.e. if we run out of memory.
678 log_err(sp
, file
, line
)
685 msgq(sp
, M_SYSERR
, "015|%s/%d: log put error", tail(file
), line
);
687 (void)ep
->log
->close(ep
->log
, DB_NOSYNC
);
688 if (!log_init(sp
, ep
))
689 msgq(sp
, M_ERR
, "267|Log restarted");
692 #if defined(DEBUG) && 0
694 log_trace(sp
, msg
, rno
, p
)
705 case LOG_CURSOR_INIT
:
706 memmove(&m
, p
+ sizeof(u_char
), sizeof(MARK
));
707 vtrace(sp
, "%lu: %s: C_INIT: %u/%u\n", rno
, msg
, m
.lno
, m
.cno
);
710 memmove(&m
, p
+ sizeof(u_char
), sizeof(MARK
));
711 vtrace(sp
, "%lu: %s: C_END: %u/%u\n", rno
, msg
, m
.lno
, m
.cno
);
713 case LOG_LINE_APPEND
:
714 memmove(&lno
, p
+ sizeof(u_char
), sizeof(db_recno_t
));
715 vtrace(sp
, "%lu: %s: APPEND: %lu\n", rno
, msg
, lno
);
717 case LOG_LINE_INSERT
:
718 memmove(&lno
, p
+ sizeof(u_char
), sizeof(db_recno_t
));
719 vtrace(sp
, "%lu: %s: INSERT: %lu\n", rno
, msg
, lno
);
721 case LOG_LINE_DELETE
:
722 memmove(&lno
, p
+ sizeof(u_char
), sizeof(db_recno_t
));
723 vtrace(sp
, "%lu: %s: DELETE: %lu\n", rno
, msg
, lno
);
725 case LOG_LINE_RESET_F
:
726 memmove(&lno
, p
+ sizeof(u_char
), sizeof(db_recno_t
));
727 vtrace(sp
, "%lu: %s: RESET_F: %lu\n", rno
, msg
, lno
);
729 case LOG_LINE_RESET_B
:
730 memmove(&lno
, p
+ sizeof(u_char
), sizeof(db_recno_t
));
731 vtrace(sp
, "%lu: %s: RESET_B: %lu\n", rno
, msg
, lno
);
734 memmove(&lm
, p
+ sizeof(u_char
), sizeof(LMARK
));
736 "%lu: %s: MARK: %u/%u\n", rno
, msg
, lm
.lno
, lm
.cno
);