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.13 2000/07/14 14:29:16 skimo Exp $ (Berkeley) $Date: 2000/07/14 14:29:16 $";
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
)) {
254 static CHAR_T nul
= 0;
263 if (db_get(sp
, lno
, DBG_FATAL
, &lp
, &len
))
266 ep
->l_lp
, ep
->l_len
, len
+ sizeof(u_char
) + sizeof(db_recno_t
));
267 ep
->l_lp
[0] = action
;
268 memmove(ep
->l_lp
+ sizeof(u_char
), &lno
, sizeof(db_recno_t
));
269 memmove(ep
->l_lp
+ sizeof(u_char
) + sizeof(db_recno_t
), lp
, len
);
271 memset(&key
, 0, sizeof(key
));
272 key
.data
= &ep
->l_cur
;
273 key
.size
= sizeof(db_recno_t
);
274 memset(&data
, 0, sizeof(data
));
275 data
.data
= ep
->l_lp
;
276 data
.size
= len
+ sizeof(u_char
) + sizeof(db_recno_t
);
277 if (ep
->log
->put(ep
->log
, NULL
, &key
, &data
, 0) == -1)
280 #if defined(DEBUG) && 0
282 case LOG_LINE_APPEND
:
283 vtrace(sp
, "%u: log_line: append: %lu {%u}\n",
284 ep
->l_cur
, lno
, len
);
286 case LOG_LINE_DELETE
:
287 vtrace(sp
, "%lu: log_line: delete: %lu {%u}\n",
288 ep
->l_cur
, lno
, len
);
290 case LOG_LINE_INSERT
:
291 vtrace(sp
, "%lu: log_line: insert: %lu {%u}\n",
292 ep
->l_cur
, lno
, len
);
294 case LOG_LINE_RESET_F
:
295 vtrace(sp
, "%lu: log_line: reset_f: %lu {%u}\n",
296 ep
->l_cur
, lno
, len
);
298 case LOG_LINE_RESET_B
:
299 vtrace(sp
, "%lu: log_line: reset_b: %lu {%u}\n",
300 ep
->l_cur
, lno
, len
);
304 /* Reset high water mark. */
305 ep
->l_high
= ++ep
->l_cur
;
312 * Log a mark position. For the log to work, we assume that there
313 * aren't any operations that just put out a log record -- this
314 * would mean that undo operations would only reset marks, and not
315 * cause any other change.
317 * PUBLIC: int log_mark __P((SCR *, LMARK *));
328 if (F_ISSET(ep
, F_NOLOG
))
331 /* Put out one initial cursor record per set of changes. */
332 if (ep
->l_cursor
.lno
!= OOBLNO
) {
333 if (log_cursor1(sp
, LOG_CURSOR_INIT
))
335 ep
->l_cursor
.lno
= OOBLNO
;
338 BINC_RET(sp
, ep
->l_lp
,
339 ep
->l_len
, sizeof(u_char
) + sizeof(LMARK
));
340 ep
->l_lp
[0] = LOG_MARK
;
341 memmove(ep
->l_lp
+ sizeof(u_char
), lmp
, sizeof(LMARK
));
343 memset(&key
, 0, sizeof(key
));
344 key
.data
= &ep
->l_cur
;
345 key
.size
= sizeof(db_recno_t
);
346 memset(&data
, 0, sizeof(data
));
347 data
.data
= ep
->l_lp
;
348 data
.size
= sizeof(u_char
) + sizeof(LMARK
);
349 if (ep
->log
->put(ep
->log
, NULL
, &key
, &data
, 0) == -1)
352 #if defined(DEBUG) && 0
353 vtrace(sp
, "%lu: mark %c: %lu/%u\n",
354 ep
->l_cur
, lmp
->name
, lmp
->lno
, lmp
->cno
);
356 /* Reset high water mark. */
357 ep
->l_high
= ++ep
->l_cur
;
363 * Roll the log backward one operation.
365 * PUBLIC: int log_backward __P((SCR *, MARK *));
381 if (F_ISSET(ep
, F_NOLOG
)) {
383 "010|Logging not being performed, undo not possible");
387 if (ep
->l_cur
== 1) {
388 msgq(sp
, M_BERR
, "011|No changes to undo");
392 F_SET(ep
, F_NOLOG
); /* Turn off logging. */
394 memset(&key
, 0, sizeof(key
));
395 key
.data
= &ep
->l_cur
; /* Initialize db request. */
396 key
.size
= sizeof(db_recno_t
);
397 memset(&data
, 0, sizeof(data
));
400 if (ep
->log
->get(ep
->log
, NULL
, &key
, &data
, 0))
402 #if defined(DEBUG) && 0
403 log_trace(sp
, "log_backward", ep
->l_cur
, data
.data
);
405 switch (*(p
= (u_char
*)data
.data
)) {
406 case LOG_CURSOR_INIT
:
408 memmove(rp
, p
+ sizeof(u_char
), sizeof(MARK
));
415 case LOG_LINE_APPEND
:
416 case LOG_LINE_INSERT
:
418 memmove(&lno
, p
+ sizeof(u_char
), sizeof(db_recno_t
));
419 if (db_delete(sp
, lno
))
421 ++sp
->rptlines
[L_DELETED
];
423 case LOG_LINE_DELETE
:
425 memmove(&lno
, p
+ sizeof(u_char
), sizeof(db_recno_t
));
426 if (db_insert(sp
, lno
,
427 (CHAR_T
*)(p
+ sizeof(u_char
) +
428 sizeof(db_recno_t
)), data
.size
- sizeof(u_char
) -
431 ++sp
->rptlines
[L_ADDED
];
433 case LOG_LINE_RESET_F
:
435 case LOG_LINE_RESET_B
:
437 memmove(&lno
, p
+ sizeof(u_char
), sizeof(db_recno_t
));
439 (CHAR_T
*)(p
+ sizeof(u_char
) +
440 sizeof(db_recno_t
)), data
.size
- sizeof(u_char
) -
443 if (sp
->rptlchange
!= lno
) {
444 sp
->rptlchange
= lno
;
445 ++sp
->rptlines
[L_CHANGED
];
450 memmove(&lm
, p
+ sizeof(u_char
), sizeof(LMARK
));
453 if (mark_set(sp
, lm
.name
, &m
, 0))
461 err
: F_CLR(ep
, F_NOLOG
);
467 * Reset the line to its original appearance.
470 * There's a bug in this code due to our not logging cursor movements
471 * unless a change was made. If you do a change, move off the line,
472 * then move back on and do a 'U', the line will be restored to the way
473 * it was before the original change.
475 * PUBLIC: int log_setline __P((SCR *));
489 if (F_ISSET(ep
, F_NOLOG
)) {
491 "012|Logging not being performed, undo not possible");
498 F_SET(ep
, F_NOLOG
); /* Turn off logging. */
500 memset(&key
, 0, sizeof(key
));
501 key
.data
= &ep
->l_cur
; /* Initialize db request. */
502 key
.size
= sizeof(db_recno_t
);
503 memset(&data
, 0, sizeof(data
));
507 if (ep
->log
->get(ep
->log
, NULL
, &key
, &data
, 0))
509 #if defined(DEBUG) && 0
510 log_trace(sp
, "log_setline", ep
->l_cur
, data
.data
);
512 switch (*(p
= (u_char
*)data
.data
)) {
513 case LOG_CURSOR_INIT
:
514 memmove(&m
, p
+ sizeof(u_char
), sizeof(MARK
));
515 if (m
.lno
!= sp
->lno
|| ep
->l_cur
== 1) {
521 memmove(&m
, p
+ sizeof(u_char
), sizeof(MARK
));
522 if (m
.lno
!= sp
->lno
) {
528 case LOG_LINE_APPEND
:
529 case LOG_LINE_INSERT
:
530 case LOG_LINE_DELETE
:
531 case LOG_LINE_RESET_F
:
533 case LOG_LINE_RESET_B
:
534 memmove(&lno
, p
+ sizeof(u_char
), sizeof(db_recno_t
));
535 if (lno
== sp
->lno
&&
536 db_set(sp
, lno
, (CHAR_T
*)(p
+ sizeof(u_char
) +
537 sizeof(db_recno_t
)), data
.size
- sizeof(u_char
) -
540 if (sp
->rptlchange
!= lno
) {
541 sp
->rptlchange
= lno
;
542 ++sp
->rptlines
[L_CHANGED
];
545 memmove(&lm
, p
+ sizeof(u_char
), sizeof(LMARK
));
548 if (mark_set(sp
, lm
.name
, &m
, 0))
556 err
: F_CLR(ep
, F_NOLOG
);
562 * Roll the log forward one operation.
564 * PUBLIC: int log_forward __P((SCR *, MARK *));
580 if (F_ISSET(ep
, F_NOLOG
)) {
582 "013|Logging not being performed, roll-forward not possible");
586 if (ep
->l_cur
== ep
->l_high
) {
587 msgq(sp
, M_BERR
, "014|No changes to re-do");
591 F_SET(ep
, F_NOLOG
); /* Turn off logging. */
593 memset(&key
, 0, sizeof(key
));
594 key
.data
= &ep
->l_cur
; /* Initialize db request. */
595 key
.size
= sizeof(db_recno_t
);
596 memset(&data
, 0, sizeof(data
));
599 if (ep
->log
->get(ep
->log
, NULL
, &key
, &data
, 0))
601 #if defined(DEBUG) && 0
602 log_trace(sp
, "log_forward", ep
->l_cur
, data
.data
);
604 switch (*(p
= (u_char
*)data
.data
)) {
608 memmove(rp
, p
+ sizeof(u_char
), sizeof(MARK
));
613 case LOG_CURSOR_INIT
:
615 /* XXXX LOG_LINE_APPEND and LOG_LINE_INSERT split
616 for now, because db_insert won't work for adding
619 case LOG_LINE_APPEND
:
621 memmove(&lno
, p
+ sizeof(u_char
), sizeof(db_recno_t
));
623 if (db_append(sp
, 1, lno
,
624 (CHAR_T
*)(p
+ sizeof(u_char
) +
625 sizeof(db_recno_t
)), data
.size
- sizeof(u_char
) -
628 ++sp
->rptlines
[L_ADDED
];
630 case LOG_LINE_INSERT
:
632 memmove(&lno
, p
+ sizeof(u_char
), sizeof(db_recno_t
));
633 if (db_insert(sp
, lno
,
634 (CHAR_T
*)(p
+ sizeof(u_char
) +
635 sizeof(db_recno_t
)), data
.size
- sizeof(u_char
) -
638 ++sp
->rptlines
[L_ADDED
];
640 case LOG_LINE_DELETE
:
642 memmove(&lno
, p
+ sizeof(u_char
), sizeof(db_recno_t
));
643 if (db_delete(sp
, lno
))
645 ++sp
->rptlines
[L_DELETED
];
647 case LOG_LINE_RESET_B
:
649 case LOG_LINE_RESET_F
:
651 memmove(&lno
, p
+ sizeof(u_char
), sizeof(db_recno_t
));
653 (CHAR_T
*)(p
+ sizeof(u_char
) +
654 sizeof(db_recno_t
)), data
.size
- sizeof(u_char
) -
657 if (sp
->rptlchange
!= lno
) {
658 sp
->rptlchange
= lno
;
659 ++sp
->rptlines
[L_CHANGED
];
664 memmove(&lm
, p
+ sizeof(u_char
), sizeof(LMARK
));
667 if (mark_set(sp
, lm
.name
, &m
, 0))
675 err
: F_CLR(ep
, F_NOLOG
);
681 * Try and restart the log on failure, i.e. if we run out of memory.
684 log_err(sp
, file
, line
)
691 msgq(sp
, M_SYSERR
, "015|%s/%d: log put error", tail(file
), line
);
693 (void)ep
->log
->close(ep
->log
, DB_NOSYNC
);
694 if (!log_init(sp
, ep
))
695 msgq(sp
, M_ERR
, "267|Log restarted");
698 #if defined(DEBUG) && 0
700 log_trace(sp
, msg
, rno
, p
)
711 case LOG_CURSOR_INIT
:
712 memmove(&m
, p
+ sizeof(u_char
), sizeof(MARK
));
713 vtrace(sp
, "%lu: %s: C_INIT: %u/%u\n", rno
, msg
, m
.lno
, m
.cno
);
716 memmove(&m
, p
+ sizeof(u_char
), sizeof(MARK
));
717 vtrace(sp
, "%lu: %s: C_END: %u/%u\n", rno
, msg
, m
.lno
, m
.cno
);
719 case LOG_LINE_APPEND
:
720 memmove(&lno
, p
+ sizeof(u_char
), sizeof(db_recno_t
));
721 vtrace(sp
, "%lu: %s: APPEND: %lu\n", rno
, msg
, lno
);
723 case LOG_LINE_INSERT
:
724 memmove(&lno
, p
+ sizeof(u_char
), sizeof(db_recno_t
));
725 vtrace(sp
, "%lu: %s: INSERT: %lu\n", rno
, msg
, lno
);
727 case LOG_LINE_DELETE
:
728 memmove(&lno
, p
+ sizeof(u_char
), sizeof(db_recno_t
));
729 vtrace(sp
, "%lu: %s: DELETE: %lu\n", rno
, msg
, lno
);
731 case LOG_LINE_RESET_F
:
732 memmove(&lno
, p
+ sizeof(u_char
), sizeof(db_recno_t
));
733 vtrace(sp
, "%lu: %s: RESET_F: %lu\n", rno
, msg
, lno
);
735 case LOG_LINE_RESET_B
:
736 memmove(&lno
, p
+ sizeof(u_char
), sizeof(db_recno_t
));
737 vtrace(sp
, "%lu: %s: RESET_B: %lu\n", rno
, msg
, lno
);
740 memmove(&lm
, p
+ sizeof(u_char
), sizeof(LMARK
));
742 "%lu: %s: MARK: %u/%u\n", rno
, msg
, lm
.lno
, lm
.cno
);