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.
12 #include <sys/types.h>
13 #include <sys/queue.h>
16 #include <bitstring.h>
29 * The log consists of records, each containing a type byte and a variable
30 * length byte string, as follows:
32 * LOG_CURSOR_INIT MARK
34 * LOG_LINE_APPEND recno_t char *
35 * LOG_LINE_DELETE recno_t char *
36 * LOG_LINE_INSERT recno_t char *
37 * LOG_LINE_RESET_F recno_t char *
38 * LOG_LINE_RESET_B recno_t char *
41 * We do before image physical logging. This means that the editor layer
42 * MAY NOT modify records in place, even if simply deleting or overwriting
43 * characters. Since the smallest unit of logging is a line, we're using
44 * up lots of space. This may eventually have to be reduced, probably by
45 * doing logical logging, which is a much cooler database phrase.
47 * The implementation of the historic vi 'u' command, using roll-forward and
48 * roll-back, is simple. Each set of changes has a LOG_CURSOR_INIT record,
49 * followed by a number of other records, followed by a LOG_CURSOR_END record.
50 * LOG_LINE_RESET records come in pairs. The first is a LOG_LINE_RESET_B
51 * record, and is the line before the change. The second is LOG_LINE_RESET_F,
52 * and is the line after the change. Roll-back is done by backing up to the
53 * first LOG_CURSOR_INIT record before a change. Roll-forward is done in a
56 * The 'U' command is implemented by rolling backward to a LOG_CURSOR_END
57 * record for a line different from the current one. It should be noted that
58 * this means that a subsequent 'u' command will make a change based on the
59 * new position of the log's cursor. This is okay, and, in fact, historic vi
63 static int log_cursor1(SCR *, int);
64 static void log_err(SCR *, char *, int);
65 #if defined(DEBUG) && 0
66 static void log_trace(SCR *, char *, recno_t, u_char *);
68 static int apply_with(int (*)(SCR *, recno_t, CHAR_T *, size_t),
69 SCR *, recno_t, u_char *, size_t);
71 /* Try and restart the log on failure, i.e. if we run out of memory. */
72 #define LOG_ERR do { \
73 log_err(sp, __FILE__, __LINE__); \
77 /* offset of CHAR_T string in log needs to be aligned on some systems
78 * because it is passed to db_set as a string
81 char data[sizeof(u_char) /* type */ + sizeof(recno_t)];
84 #define CHAR_T_OFFSET ((char *)(((log_t*)0)->str) - (char *)0)
88 * Initialize the logging subsystem.
90 * PUBLIC: int log_init(SCR *, EXF *);
93 log_init(SCR *sp, EXF *ep)
97 * ep MAY NOT BE THE SAME AS sp->ep, DON'T USE THE LATTER.
99 * Initialize the buffer. The logging subsystem has its own
100 * buffers because the global ones are almost by definition
101 * going to be in use when the log runs.
105 ep->l_cursor.lno = 1; /* XXX Any valid recno. */
106 ep->l_cursor.cno = 0;
107 ep->l_high = ep->l_cur = 1;
109 ep->log = dbopen(NULL, O_CREAT | O_NONBLOCK | O_RDWR,
110 S_IRUSR | S_IWUSR, DB_RECNO, NULL);
111 if (ep->log == NULL) {
112 msgq(sp, M_SYSERR, "009|Log file");
122 * Close the logging subsystem.
124 * PUBLIC: int log_end(SCR *, EXF *);
127 log_end(SCR *sp, EXF *ep)
131 * ep MAY NOT BE THE SAME AS sp->ep, DON'T USE THE LATTER.
133 if (ep->log != NULL) {
134 (void)(ep->log->close)(ep->log);
140 ep->l_cursor.lno = 1; /* XXX Any valid recno. */
141 ep->l_cursor.cno = 0;
142 ep->l_high = ep->l_cur = 1;
148 * Log the current cursor position, starting an event.
150 * PUBLIC: int log_cursor(SCR *);
158 if (F_ISSET(ep, F_NOLOG))
162 * If any changes were made since the last cursor init,
163 * put out the ending cursor record.
165 if (ep->l_cursor.lno == OOBLNO) {
166 ep->l_cursor.lno = sp->lno;
167 ep->l_cursor.cno = sp->cno;
168 return (log_cursor1(sp, LOG_CURSOR_END));
170 ep->l_cursor.lno = sp->lno;
171 ep->l_cursor.cno = sp->cno;
177 * Actually push a cursor record out.
180 log_cursor1(SCR *sp, int type)
187 BINC_RETC(sp, ep->l_lp, ep->l_len, sizeof(u_char) + sizeof(MARK));
189 memmove(ep->l_lp + sizeof(u_char), &ep->l_cursor, sizeof(MARK));
191 key.data = &ep->l_cur;
192 key.size = sizeof(recno_t);
193 data.data = ep->l_lp;
194 data.size = sizeof(u_char) + sizeof(MARK);
195 if (ep->log->put(ep->log, &key, &data, 0) == -1)
198 #if defined(DEBUG) && 0
199 TRACE(sp, "%lu: %s: %u/%u\n", ep->l_cur,
200 type == LOG_CURSOR_INIT ? "log_cursor_init" : "log_cursor_end",
203 /* Reset high water mark. */
204 ep->l_high = ++ep->l_cur;
213 * PUBLIC: int log_line(SCR *, recno_t, u_int);
216 log_line(SCR *sp, recno_t lno, u_int action)
225 if (F_ISSET(ep, F_NOLOG))
231 * Kluge for vi. Clear the EXF undo flag so that the
232 * next 'u' command does a roll-back, regardless.
236 /* Put out one initial cursor record per set of changes. */
237 if (ep->l_cursor.lno != OOBLNO) {
238 if (log_cursor1(sp, LOG_CURSOR_INIT))
240 ep->l_cursor.lno = OOBLNO;
244 * Put out the changes. If it's a LOG_LINE_RESET_B call, it's a
245 * special case, avoid the caches. Also, if it fails and it's
246 * line 1, it just means that the user started with an empty file,
247 * so fake an empty length line.
249 if (action == LOG_LINE_RESET_B) {
250 if (db_get(sp, lno, DBG_NOCACHE, &lp, &len)) {
259 if (db_get(sp, lno, DBG_FATAL, &lp, &len))
263 len * sizeof(CHAR_T) + CHAR_T_OFFSET);
264 ep->l_lp[0] = action;
265 memmove(ep->l_lp + sizeof(u_char), &lno, sizeof(recno_t));
266 memmove(ep->l_lp + CHAR_T_OFFSET, lp, len * sizeof(CHAR_T));
270 key.size = sizeof(recno_t);
271 data.data = ep->l_lp;
272 data.size = len * sizeof(CHAR_T) + CHAR_T_OFFSET;
273 if (ep->log->put(ep->log, &key, &data, 0) == -1)
276 #if defined(DEBUG) && 0
278 case LOG_LINE_APPEND:
279 TRACE(sp, "%lu: log_line: append: %lu {%u}\n",
280 ep->l_cur, lno, len);
282 case LOG_LINE_DELETE:
283 TRACE(sp, "%lu: log_line: delete: %lu {%u}\n",
284 ep->l_cur, lno, len);
286 case LOG_LINE_INSERT:
287 TRACE(sp, "%lu: log_line: insert: %lu {%u}\n",
288 ep->l_cur, lno, len);
290 case LOG_LINE_RESET_F:
291 TRACE(sp, "%lu: log_line: reset_f: %lu {%u}\n",
292 ep->l_cur, lno, len);
294 case LOG_LINE_RESET_B:
295 TRACE(sp, "%lu: log_line: reset_b: %lu {%u}\n",
296 ep->l_cur, lno, len);
300 /* Reset high water mark. */
301 ep->l_high = ++ep->l_cur;
308 * Log a mark position. For the log to work, we assume that there
309 * aren't any operations that just put out a log record -- this
310 * would mean that undo operations would only reset marks, and not
311 * cause any other change.
313 * PUBLIC: int log_mark(SCR *, LMARK *);
316 log_mark(SCR *sp, LMARK *lmp)
322 if (F_ISSET(ep, F_NOLOG))
325 /* Put out one initial cursor record per set of changes. */
326 if (ep->l_cursor.lno != OOBLNO) {
327 if (log_cursor1(sp, LOG_CURSOR_INIT))
329 ep->l_cursor.lno = OOBLNO;
332 BINC_RETC(sp, ep->l_lp,
333 ep->l_len, sizeof(u_char) + sizeof(LMARK));
334 ep->l_lp[0] = LOG_MARK;
335 memmove(ep->l_lp + sizeof(u_char), lmp, sizeof(LMARK));
337 key.data = &ep->l_cur;
338 key.size = sizeof(recno_t);
339 data.data = ep->l_lp;
340 data.size = sizeof(u_char) + sizeof(LMARK);
341 if (ep->log->put(ep->log, &key, &data, 0) == -1)
344 #if defined(DEBUG) && 0
345 TRACE(sp, "%lu: mark %c: %lu/%u\n",
346 ep->l_cur, lmp->name, lmp->lno, lmp->cno);
348 /* Reset high water mark. */
349 ep->l_high = ++ep->l_cur;
355 * Roll the log backward one operation.
357 * PUBLIC: int log_backward(SCR *, MARK *);
360 log_backward(SCR *sp, MARK *rp)
371 if (F_ISSET(ep, F_NOLOG)) {
373 "010|Logging not being performed, undo not possible");
377 if (ep->l_cur == 1) {
378 msgq(sp, M_BERR, "011|No changes to undo");
382 F_SET(ep, F_NOLOG); /* Turn off logging. */
384 key.data = &ep->l_cur; /* Initialize db request. */
385 key.size = sizeof(recno_t);
388 if (ep->log->get(ep->log, &key, &data, 0))
390 #if defined(DEBUG) && 0
391 log_trace(sp, "log_backward", ep->l_cur, data.data);
393 switch (*(p = (u_char *)data.data)) {
394 case LOG_CURSOR_INIT:
396 memmove(rp, p + sizeof(u_char), sizeof(MARK));
403 case LOG_LINE_APPEND:
404 case LOG_LINE_INSERT:
406 memmove(&lno, p + sizeof(u_char), sizeof(recno_t));
407 if (db_delete(sp, lno))
409 ++sp->rptlines[L_DELETED];
411 case LOG_LINE_DELETE:
413 memmove(&lno, p + sizeof(u_char), sizeof(recno_t));
414 if (apply_with(db_insert, sp, lno,
415 p + CHAR_T_OFFSET, data.size - CHAR_T_OFFSET))
417 ++sp->rptlines[L_ADDED];
419 case LOG_LINE_RESET_F:
421 case LOG_LINE_RESET_B:
423 memmove(&lno, p + sizeof(u_char), sizeof(recno_t));
424 if (apply_with(db_set, sp, lno,
425 p + CHAR_T_OFFSET, data.size - CHAR_T_OFFSET))
427 if (sp->rptlchange != lno) {
428 sp->rptlchange = lno;
429 ++sp->rptlines[L_CHANGED];
434 memmove(&lm, p + sizeof(u_char), sizeof(LMARK));
437 if (mark_set(sp, lm.name, &m, 0))
445 err: F_CLR(ep, F_NOLOG);
451 * Reset the line to its original appearance.
454 * There's a bug in this code due to our not logging cursor movements
455 * unless a change was made. If you do a change, move off the line,
456 * then move back on and do a 'U', the line will be restored to the way
457 * it was before the original change.
459 * PUBLIC: int log_setline(SCR *);
472 if (F_ISSET(ep, F_NOLOG)) {
474 "012|Logging not being performed, undo not possible");
481 F_SET(ep, F_NOLOG); /* Turn off logging. */
483 key.data = &ep->l_cur; /* Initialize db request. */
484 key.size = sizeof(recno_t);
487 if (ep->log->get(ep->log, &key, &data, 0))
489 #if defined(DEBUG) && 0
490 log_trace(sp, "log_setline", ep->l_cur, data.data);
492 switch (*(p = (u_char *)data.data)) {
493 case LOG_CURSOR_INIT:
494 memmove(&m, p + sizeof(u_char), sizeof(MARK));
495 if (m.lno != sp->lno || ep->l_cur == 1) {
501 memmove(&m, p + sizeof(u_char), sizeof(MARK));
502 if (m.lno != sp->lno) {
508 case LOG_LINE_APPEND:
509 case LOG_LINE_INSERT:
510 case LOG_LINE_DELETE:
511 case LOG_LINE_RESET_F:
513 case LOG_LINE_RESET_B:
514 memmove(&lno, p + sizeof(u_char), sizeof(recno_t));
515 if (lno == sp->lno &&
516 apply_with(db_set, sp, lno,
517 p + CHAR_T_OFFSET, data.size - CHAR_T_OFFSET))
519 if (sp->rptlchange != lno) {
520 sp->rptlchange = lno;
521 ++sp->rptlines[L_CHANGED];
524 memmove(&lm, p + sizeof(u_char), sizeof(LMARK));
527 if (mark_set(sp, lm.name, &m, 0))
535 err: F_CLR(ep, F_NOLOG);
541 * Roll the log forward one operation.
543 * PUBLIC: int log_forward(SCR *, MARK *);
546 log_forward(SCR *sp, MARK *rp)
557 if (F_ISSET(ep, F_NOLOG)) {
559 "013|Logging not being performed, roll-forward not possible");
563 if (ep->l_cur == ep->l_high) {
564 msgq(sp, M_BERR, "014|No changes to re-do");
568 F_SET(ep, F_NOLOG); /* Turn off logging. */
570 key.data = &ep->l_cur; /* Initialize db request. */
571 key.size = sizeof(recno_t);
574 if (ep->log->get(ep->log, &key, &data, 0))
576 #if defined(DEBUG) && 0
577 log_trace(sp, "log_forward", ep->l_cur, data.data);
579 switch (*(p = (u_char *)data.data)) {
583 memmove(rp, p + sizeof(u_char), sizeof(MARK));
588 case LOG_CURSOR_INIT:
590 case LOG_LINE_APPEND:
591 case LOG_LINE_INSERT:
593 memmove(&lno, p + sizeof(u_char), sizeof(recno_t));
594 if (apply_with(db_insert, sp, lno,
595 p + CHAR_T_OFFSET, data.size - CHAR_T_OFFSET))
597 ++sp->rptlines[L_ADDED];
599 case LOG_LINE_DELETE:
601 memmove(&lno, p + sizeof(u_char), sizeof(recno_t));
602 if (db_delete(sp, lno))
604 ++sp->rptlines[L_DELETED];
606 case LOG_LINE_RESET_B:
608 case LOG_LINE_RESET_F:
610 memmove(&lno, p + sizeof(u_char), sizeof(recno_t));
611 if (apply_with(db_set, sp, lno,
612 p + CHAR_T_OFFSET, data.size - CHAR_T_OFFSET))
614 if (sp->rptlchange != lno) {
615 sp->rptlchange = lno;
616 ++sp->rptlines[L_CHANGED];
621 memmove(&lm, p + sizeof(u_char), sizeof(LMARK));
624 if (mark_set(sp, lm.name, &m, 0))
632 err: F_CLR(ep, F_NOLOG);
638 * Try and restart the log on failure, i.e. if we run out of memory.
641 log_err(SCR *sp, char *file, int line)
645 msgq(sp, M_SYSERR, "015|%s/%d: log put error", basename(file), line);
647 (void)ep->log->close(ep->log);
648 if (!log_init(sp, ep))
649 msgq(sp, M_ERR, "267|Log restarted");
652 #if defined(DEBUG) && 0
654 log_trace(SCR *sp, char *msg, recno_t rno, u_char *p)
661 case LOG_CURSOR_INIT:
662 memmove(&m, p + sizeof(u_char), sizeof(MARK));
663 TRACE(sp, "%lu: %s: C_INIT: %u/%u\n", rno, msg, m.lno, m.cno);
666 memmove(&m, p + sizeof(u_char), sizeof(MARK));
667 TRACE(sp, "%lu: %s: C_END: %u/%u\n", rno, msg, m.lno, m.cno);
669 case LOG_LINE_APPEND:
670 memmove(&lno, p + sizeof(u_char), sizeof(recno_t));
671 TRACE(sp, "%lu: %s: APPEND: %lu\n", rno, msg, lno);
673 case LOG_LINE_INSERT:
674 memmove(&lno, p + sizeof(u_char), sizeof(recno_t));
675 TRACE(sp, "%lu: %s: INSERT: %lu\n", rno, msg, lno);
677 case LOG_LINE_DELETE:
678 memmove(&lno, p + sizeof(u_char), sizeof(recno_t));
679 TRACE(sp, "%lu: %s: DELETE: %lu\n", rno, msg, lno);
681 case LOG_LINE_RESET_F:
682 memmove(&lno, p + sizeof(u_char), sizeof(recno_t));
683 TRACE(sp, "%lu: %s: RESET_F: %lu\n", rno, msg, lno);
685 case LOG_LINE_RESET_B:
686 memmove(&lno, p + sizeof(u_char), sizeof(recno_t));
687 TRACE(sp, "%lu: %s: RESET_B: %lu\n", rno, msg, lno);
690 memmove(&lm, p + sizeof(u_char), sizeof(LMARK));
692 "%lu: %s: MARK: %u/%u\n", rno, msg, lm.lno, lm.cno);
702 * Apply a realigned line from the log db to the file db.
705 apply_with(int (*db_func)(SCR *, recno_t, CHAR_T *, size_t), SCR *sp,
706 recno_t lno, u_char *p, size_t len)
709 typedef unsigned long nword;
713 nword *lp = (nword *)((uintptr_t)p / sizeof(nword) * sizeof(nword));
715 if (lp != (nword *)p) {
716 int offl = ((uintptr_t)p - (uintptr_t)lp) << 3;
717 int offr = (sizeof(nword) << 3) - offl;
718 size_t i, cnt = (len + sizeof(nword) / 2) / sizeof(nword);
721 blen = p2roundup(MAX(len, 512));
722 REALLOC(sp, bp, nword *, blen);
726 for (i = 0; i < cnt; ++i)
727 #if BYTE_ORDER == BIG_ENDIAN
728 bp[i] = (lp[i] << offl) ^ (lp[i+1] >> offr);
730 bp[i] = (lp[i] >> offl) ^ (lp[i+1] << offr);
735 return db_func(sp, lno, (CHAR_T *)p, len / sizeof(CHAR_T));