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
[] = "@(#)log.c 10.8 (Berkeley) 3/6/96";
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 recno_t char *
37 * LOG_LINE_DELETE recno_t char *
38 * LOG_LINE_INSERT recno_t char *
39 * LOG_LINE_RESET_F recno_t char *
40 * LOG_LINE_RESET_B 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 *, 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 ep
->log
= dbopen(NULL
, O_CREAT
| O_NONBLOCK
| O_RDWR
,
103 S_IRUSR
| S_IWUSR
, DB_RECNO
, NULL
);
104 if (ep
->log
== NULL
) {
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
);
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 key
.data
= &ep
->l_cur
;
191 key
.size
= sizeof(recno_t
);
192 data
.data
= ep
->l_lp
;
193 data
.size
= sizeof(u_char
) + sizeof(MARK
);
194 if (ep
->log
->put(ep
->log
, &key
, &data
, 0) == -1)
197 #if defined(DEBUG) && 0
198 TRACE(sp
, "%lu: %s: %u/%u\n", ep
->l_cur
,
199 type
== LOG_CURSOR_INIT
? "log_cursor_init" : "log_cursor_end",
202 /* Reset high water mark. */
203 ep
->l_high
= ++ep
->l_cur
;
212 * PUBLIC: int log_line __P((SCR *, recno_t, u_int));
215 log_line(sp
, lno
, action
)
226 if (F_ISSET(ep
, F_NOLOG
))
232 * Kluge for vi. Clear the EXF undo flag so that the
233 * next 'u' command does a roll-back, regardless.
237 /* Put out one initial cursor record per set of changes. */
238 if (ep
->l_cursor
.lno
!= OOBLNO
) {
239 if (log_cursor1(sp
, LOG_CURSOR_INIT
))
241 ep
->l_cursor
.lno
= OOBLNO
;
245 * Put out the changes. If it's a LOG_LINE_RESET_B call, it's a
246 * special case, avoid the caches. Also, if it fails and it's
247 * line 1, it just means that the user started with an empty file,
248 * so fake an empty length line.
250 if (action
== LOG_LINE_RESET_B
) {
251 if (db_get(sp
, lno
, DBG_NOCACHE
, &lp
, &len
)) {
260 if (db_get(sp
, lno
, DBG_FATAL
, &lp
, &len
))
263 ep
->l_lp
, ep
->l_len
, len
+ sizeof(u_char
) + sizeof(recno_t
));
264 ep
->l_lp
[0] = action
;
265 memmove(ep
->l_lp
+ sizeof(u_char
), &lno
, sizeof(recno_t
));
266 memmove(ep
->l_lp
+ sizeof(u_char
) + sizeof(recno_t
), lp
, len
);
268 key
.data
= &ep
->l_cur
;
269 key
.size
= sizeof(recno_t
);
270 data
.data
= ep
->l_lp
;
271 data
.size
= len
+ sizeof(u_char
) + sizeof(recno_t
);
272 if (ep
->log
->put(ep
->log
, &key
, &data
, 0) == -1)
275 #if defined(DEBUG) && 0
277 case LOG_LINE_APPEND
:
278 TRACE(sp
, "%u: log_line: append: %lu {%u}\n",
279 ep
->l_cur
, lno
, len
);
281 case LOG_LINE_DELETE
:
282 TRACE(sp
, "%lu: log_line: delete: %lu {%u}\n",
283 ep
->l_cur
, lno
, len
);
285 case LOG_LINE_INSERT
:
286 TRACE(sp
, "%lu: log_line: insert: %lu {%u}\n",
287 ep
->l_cur
, lno
, len
);
289 case LOG_LINE_RESET_F
:
290 TRACE(sp
, "%lu: log_line: reset_f: %lu {%u}\n",
291 ep
->l_cur
, lno
, len
);
293 case LOG_LINE_RESET_B
:
294 TRACE(sp
, "%lu: log_line: reset_b: %lu {%u}\n",
295 ep
->l_cur
, lno
, len
);
299 /* Reset high water mark. */
300 ep
->l_high
= ++ep
->l_cur
;
307 * Log a mark position. For the log to work, we assume that there
308 * aren't any operations that just put out a log record -- this
309 * would mean that undo operations would only reset marks, and not
310 * cause any other change.
312 * PUBLIC: int log_mark __P((SCR *, LMARK *));
323 if (F_ISSET(ep
, F_NOLOG
))
326 /* Put out one initial cursor record per set of changes. */
327 if (ep
->l_cursor
.lno
!= OOBLNO
) {
328 if (log_cursor1(sp
, LOG_CURSOR_INIT
))
330 ep
->l_cursor
.lno
= OOBLNO
;
333 BINC_RET(sp
, ep
->l_lp
,
334 ep
->l_len
, sizeof(u_char
) + sizeof(LMARK
));
335 ep
->l_lp
[0] = LOG_MARK
;
336 memmove(ep
->l_lp
+ sizeof(u_char
), lmp
, sizeof(LMARK
));
338 key
.data
= &ep
->l_cur
;
339 key
.size
= sizeof(recno_t
);
340 data
.data
= ep
->l_lp
;
341 data
.size
= sizeof(u_char
) + sizeof(LMARK
);
342 if (ep
->log
->put(ep
->log
, &key
, &data
, 0) == -1)
345 #if defined(DEBUG) && 0
346 TRACE(sp
, "%lu: mark %c: %lu/%u\n",
347 ep
->l_cur
, lmp
->name
, lmp
->lno
, lmp
->cno
);
349 /* Reset high water mark. */
350 ep
->l_high
= ++ep
->l_cur
;
356 * Roll the log backward one operation.
358 * PUBLIC: int log_backward __P((SCR *, MARK *));
374 if (F_ISSET(ep
, F_NOLOG
)) {
376 "010|Logging not being performed, undo not possible");
380 if (ep
->l_cur
== 1) {
381 msgq(sp
, M_BERR
, "011|No changes to undo");
385 F_SET(ep
, F_NOLOG
); /* Turn off logging. */
387 key
.data
= &ep
->l_cur
; /* Initialize db request. */
388 key
.size
= sizeof(recno_t
);
391 if (ep
->log
->get(ep
->log
, &key
, &data
, 0))
393 #if defined(DEBUG) && 0
394 log_trace(sp
, "log_backward", ep
->l_cur
, data
.data
);
396 switch (*(p
= (u_char
*)data
.data
)) {
397 case LOG_CURSOR_INIT
:
399 memmove(rp
, p
+ sizeof(u_char
), sizeof(MARK
));
406 case LOG_LINE_APPEND
:
407 case LOG_LINE_INSERT
:
409 memmove(&lno
, p
+ sizeof(u_char
), sizeof(recno_t
));
410 if (db_delete(sp
, lno
))
412 ++sp
->rptlines
[L_DELETED
];
414 case LOG_LINE_DELETE
:
416 memmove(&lno
, p
+ sizeof(u_char
), sizeof(recno_t
));
417 if (db_insert(sp
, lno
, p
+ sizeof(u_char
) +
418 sizeof(recno_t
), data
.size
- sizeof(u_char
) -
421 ++sp
->rptlines
[L_ADDED
];
423 case LOG_LINE_RESET_F
:
425 case LOG_LINE_RESET_B
:
427 memmove(&lno
, p
+ sizeof(u_char
), sizeof(recno_t
));
428 if (db_set(sp
, lno
, p
+ sizeof(u_char
) +
429 sizeof(recno_t
), data
.size
- sizeof(u_char
) -
432 if (sp
->rptlchange
!= lno
) {
433 sp
->rptlchange
= lno
;
434 ++sp
->rptlines
[L_CHANGED
];
439 memmove(&lm
, p
+ sizeof(u_char
), sizeof(LMARK
));
442 if (mark_set(sp
, lm
.name
, &m
, 0))
450 err
: F_CLR(ep
, F_NOLOG
);
456 * Reset the line to its original appearance.
459 * There's a bug in this code due to our not logging cursor movements
460 * unless a change was made. If you do a change, move off the line,
461 * then move back on and do a 'U', the line will be restored to the way
462 * it was before the original change.
464 * PUBLIC: int log_setline __P((SCR *));
478 if (F_ISSET(ep
, F_NOLOG
)) {
480 "012|Logging not being performed, undo not possible");
487 F_SET(ep
, F_NOLOG
); /* Turn off logging. */
489 key
.data
= &ep
->l_cur
; /* Initialize db request. */
490 key
.size
= sizeof(recno_t
);
494 if (ep
->log
->get(ep
->log
, &key
, &data
, 0))
496 #if defined(DEBUG) && 0
497 log_trace(sp
, "log_setline", ep
->l_cur
, data
.data
);
499 switch (*(p
= (u_char
*)data
.data
)) {
500 case LOG_CURSOR_INIT
:
501 memmove(&m
, p
+ sizeof(u_char
), sizeof(MARK
));
502 if (m
.lno
!= sp
->lno
|| ep
->l_cur
== 1) {
508 memmove(&m
, p
+ sizeof(u_char
), sizeof(MARK
));
509 if (m
.lno
!= sp
->lno
) {
515 case LOG_LINE_APPEND
:
516 case LOG_LINE_INSERT
:
517 case LOG_LINE_DELETE
:
518 case LOG_LINE_RESET_F
:
520 case LOG_LINE_RESET_B
:
521 memmove(&lno
, p
+ sizeof(u_char
), sizeof(recno_t
));
522 if (lno
== sp
->lno
&&
523 db_set(sp
, lno
, p
+ sizeof(u_char
) +
524 sizeof(recno_t
), data
.size
- sizeof(u_char
) -
527 if (sp
->rptlchange
!= lno
) {
528 sp
->rptlchange
= lno
;
529 ++sp
->rptlines
[L_CHANGED
];
532 memmove(&lm
, p
+ sizeof(u_char
), sizeof(LMARK
));
535 if (mark_set(sp
, lm
.name
, &m
, 0))
543 err
: F_CLR(ep
, F_NOLOG
);
549 * Roll the log forward one operation.
551 * PUBLIC: int log_forward __P((SCR *, MARK *));
567 if (F_ISSET(ep
, F_NOLOG
)) {
569 "013|Logging not being performed, roll-forward not possible");
573 if (ep
->l_cur
== ep
->l_high
) {
574 msgq(sp
, M_BERR
, "014|No changes to re-do");
578 F_SET(ep
, F_NOLOG
); /* Turn off logging. */
580 key
.data
= &ep
->l_cur
; /* Initialize db request. */
581 key
.size
= sizeof(recno_t
);
584 if (ep
->log
->get(ep
->log
, &key
, &data
, 0))
586 #if defined(DEBUG) && 0
587 log_trace(sp
, "log_forward", ep
->l_cur
, data
.data
);
589 switch (*(p
= (u_char
*)data
.data
)) {
593 memmove(rp
, p
+ sizeof(u_char
), sizeof(MARK
));
598 case LOG_CURSOR_INIT
:
600 case LOG_LINE_APPEND
:
601 case LOG_LINE_INSERT
:
603 memmove(&lno
, p
+ sizeof(u_char
), sizeof(recno_t
));
604 if (db_insert(sp
, lno
, p
+ sizeof(u_char
) +
605 sizeof(recno_t
), data
.size
- sizeof(u_char
) -
608 ++sp
->rptlines
[L_ADDED
];
610 case LOG_LINE_DELETE
:
612 memmove(&lno
, p
+ sizeof(u_char
), sizeof(recno_t
));
613 if (db_delete(sp
, lno
))
615 ++sp
->rptlines
[L_DELETED
];
617 case LOG_LINE_RESET_B
:
619 case LOG_LINE_RESET_F
:
621 memmove(&lno
, p
+ sizeof(u_char
), sizeof(recno_t
));
622 if (db_set(sp
, lno
, p
+ sizeof(u_char
) +
623 sizeof(recno_t
), data
.size
- sizeof(u_char
) -
626 if (sp
->rptlchange
!= lno
) {
627 sp
->rptlchange
= lno
;
628 ++sp
->rptlines
[L_CHANGED
];
633 memmove(&lm
, p
+ sizeof(u_char
), sizeof(LMARK
));
636 if (mark_set(sp
, lm
.name
, &m
, 0))
644 err
: F_CLR(ep
, F_NOLOG
);
650 * Try and restart the log on failure, i.e. if we run out of memory.
653 log_err(sp
, file
, line
)
660 msgq(sp
, M_SYSERR
, "015|%s/%d: log put error", tail(file
), line
);
662 (void)ep
->log
->close(ep
->log
);
663 if (!log_init(sp
, ep
))
664 msgq(sp
, M_ERR
, "267|Log restarted");
667 #if defined(DEBUG) && 0
669 log_trace(sp
, msg
, rno
, p
)
680 case LOG_CURSOR_INIT
:
681 memmove(&m
, p
+ sizeof(u_char
), sizeof(MARK
));
682 TRACE(sp
, "%lu: %s: C_INIT: %u/%u\n", rno
, msg
, m
.lno
, m
.cno
);
685 memmove(&m
, p
+ sizeof(u_char
), sizeof(MARK
));
686 TRACE(sp
, "%lu: %s: C_END: %u/%u\n", rno
, msg
, m
.lno
, m
.cno
);
688 case LOG_LINE_APPEND
:
689 memmove(&lno
, p
+ sizeof(u_char
), sizeof(recno_t
));
690 TRACE(sp
, "%lu: %s: APPEND: %lu\n", rno
, msg
, lno
);
692 case LOG_LINE_INSERT
:
693 memmove(&lno
, p
+ sizeof(u_char
), sizeof(recno_t
));
694 TRACE(sp
, "%lu: %s: INSERT: %lu\n", rno
, msg
, lno
);
696 case LOG_LINE_DELETE
:
697 memmove(&lno
, p
+ sizeof(u_char
), sizeof(recno_t
));
698 TRACE(sp
, "%lu: %s: DELETE: %lu\n", rno
, msg
, lno
);
700 case LOG_LINE_RESET_F
:
701 memmove(&lno
, p
+ sizeof(u_char
), sizeof(recno_t
));
702 TRACE(sp
, "%lu: %s: RESET_F: %lu\n", rno
, msg
, lno
);
704 case LOG_LINE_RESET_B
:
705 memmove(&lno
, p
+ sizeof(u_char
), sizeof(recno_t
));
706 TRACE(sp
, "%lu: %s: RESET_B: %lu\n", rno
, msg
, lno
);
709 memmove(&lm
, p
+ sizeof(u_char
), sizeof(LMARK
));
711 "%lu: %s: MARK: %u/%u\n", rno
, msg
, lm
.lno
, lm
.cno
);