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.27 2011/07/13 06:25:50 zy Exp $";
16 #include <sys/types.h>
17 #include <sys/queue.h>
20 #include <bitstring.h>
32 * The log consists of records, each containing a type byte and a variable
33 * length byte string, as follows:
35 * LOG_CURSOR_INIT MARK
37 * LOG_LINE_APPEND recno_t char *
38 * LOG_LINE_DELETE recno_t char *
39 * LOG_LINE_INSERT recno_t char *
40 * LOG_LINE_RESET_F recno_t char *
41 * LOG_LINE_RESET_B recno_t char *
44 * We do before image physical logging. This means that the editor layer
45 * MAY NOT modify records in place, even if simply deleting or overwriting
46 * characters. Since the smallest unit of logging is a line, we're using
47 * up lots of space. This may eventually have to be reduced, probably by
48 * doing logical logging, which is a much cooler database phrase.
50 * The implementation of the historic vi 'u' command, using roll-forward and
51 * roll-back, is simple. Each set of changes has a LOG_CURSOR_INIT record,
52 * followed by a number of other records, followed by a LOG_CURSOR_END record.
53 * LOG_LINE_RESET records come in pairs. The first is a LOG_LINE_RESET_B
54 * record, and is the line before the change. The second is LOG_LINE_RESET_F,
55 * and is the line after the change. Roll-back is done by backing up to the
56 * first LOG_CURSOR_INIT record before a change. Roll-forward is done in a
59 * The 'U' command is implemented by rolling backward to a LOG_CURSOR_END
60 * record for a line different from the current one. It should be noted that
61 * this means that a subsequent 'u' command will make a change based on the
62 * new position of the log's cursor. This is okay, and, in fact, historic vi
66 static int log_cursor1(SCR *, int);
67 static void log_err(SCR *, char *, int);
68 #if defined(DEBUG) && 0
69 static void log_trace(SCR *, char *, recno_t, u_char *);
71 static int apply_with(int (*)(SCR *, recno_t, CHAR_T *, size_t),
72 SCR *, recno_t, u_char *, size_t);
74 /* Try and restart the log on failure, i.e. if we run out of memory. */
76 log_err(sp, __FILE__, __LINE__); \
80 /* offset of CHAR_T string in log needs to be aligned on some systems
81 * because it is passed to db_set as a string
84 char data[sizeof(u_char) /* type */ + sizeof(recno_t)];
87 #define CHAR_T_OFFSET ((char *)(((log_t*)0)->str) - (char *)0)
91 * Initialize the logging subsystem.
93 * PUBLIC: int log_init(SCR *, EXF *);
102 * ep MAY NOT BE THE SAME AS sp->ep, DON'T USE THE LATTER.
104 * Initialize the buffer. The logging subsystem has its own
105 * buffers because the global ones are almost by definition
106 * going to be in use when the log runs.
110 ep->l_cursor.lno = 1; /* XXX Any valid recno. */
111 ep->l_cursor.cno = 0;
112 ep->l_high = ep->l_cur = 1;
114 ep->log = dbopen(NULL, O_CREAT | O_NONBLOCK | O_RDWR,
115 S_IRUSR | S_IWUSR, DB_RECNO, NULL);
116 if (ep->log == NULL) {
117 msgq(sp, M_SYSERR, "009|Log file");
127 * Close the logging subsystem.
129 * PUBLIC: int log_end(SCR *, EXF *);
138 * ep MAY NOT BE THE SAME AS sp->ep, DON'T USE THE LATTER.
140 if (ep->log != NULL) {
141 (void)(ep->log->close)(ep->log);
144 if (ep->l_lp != NULL) {
149 ep->l_cursor.lno = 1; /* XXX Any valid recno. */
150 ep->l_cursor.cno = 0;
151 ep->l_high = ep->l_cur = 1;
157 * Log the current cursor position, starting an event.
159 * PUBLIC: int log_cursor(SCR *);
167 if (F_ISSET(ep, F_NOLOG))
171 * If any changes were made since the last cursor init,
172 * put out the ending cursor record.
174 if (ep->l_cursor.lno == OOBLNO) {
175 ep->l_cursor.lno = sp->lno;
176 ep->l_cursor.cno = sp->cno;
177 return (log_cursor1(sp, LOG_CURSOR_END));
179 ep->l_cursor.lno = sp->lno;
180 ep->l_cursor.cno = sp->cno;
186 * Actually push a cursor record out.
198 BINC_RETC(sp, ep->l_lp, ep->l_len, sizeof(u_char) + sizeof(MARK));
200 memmove(ep->l_lp + sizeof(u_char), &ep->l_cursor, sizeof(MARK));
202 key.data = &ep->l_cur;
203 key.size = sizeof(recno_t);
204 data.data = ep->l_lp;
205 data.size = sizeof(u_char) + sizeof(MARK);
206 if (ep->log->put(ep->log, &key, &data, 0) == -1)
209 #if defined(DEBUG) && 0
210 TRACE(sp, "%lu: %s: %u/%u\n", ep->l_cur,
211 type == LOG_CURSOR_INIT ? "log_cursor_init" : "log_cursor_end",
214 /* Reset high water mark. */
215 ep->l_high = ++ep->l_cur;
224 * PUBLIC: int log_line(SCR *, recno_t, u_int);
239 if (F_ISSET(ep, F_NOLOG))
245 * Kluge for vi. Clear the EXF undo flag so that the
246 * next 'u' command does a roll-back, regardless.
250 /* Put out one initial cursor record per set of changes. */
251 if (ep->l_cursor.lno != OOBLNO) {
252 if (log_cursor1(sp, LOG_CURSOR_INIT))
254 ep->l_cursor.lno = OOBLNO;
258 * Put out the changes. If it's a LOG_LINE_RESET_B call, it's a
259 * special case, avoid the caches. Also, if it fails and it's
260 * line 1, it just means that the user started with an empty file,
261 * so fake an empty length line.
263 if (action == LOG_LINE_RESET_B) {
264 if (db_get(sp, lno, DBG_NOCACHE, &lp, &len)) {
273 if (db_get(sp, lno, DBG_FATAL, &lp, &len))
277 len * sizeof(CHAR_T) + CHAR_T_OFFSET);
278 ep->l_lp[0] = action;
279 memmove(ep->l_lp + sizeof(u_char), &lno, sizeof(recno_t));
280 memmove(ep->l_lp + CHAR_T_OFFSET, lp, len * sizeof(CHAR_T));
284 key.size = sizeof(recno_t);
285 data.data = ep->l_lp;
286 data.size = len * sizeof(CHAR_T) + CHAR_T_OFFSET;
287 if (ep->log->put(ep->log, &key, &data, 0) == -1)
290 #if defined(DEBUG) && 0
292 case LOG_LINE_APPEND:
293 TRACE(sp, "%lu: log_line: append: %lu {%u}\n",
294 ep->l_cur, lno, len);
296 case LOG_LINE_DELETE:
297 TRACE(sp, "%lu: log_line: delete: %lu {%u}\n",
298 ep->l_cur, lno, len);
300 case LOG_LINE_INSERT:
301 TRACE(sp, "%lu: log_line: insert: %lu {%u}\n",
302 ep->l_cur, lno, len);
304 case LOG_LINE_RESET_F:
305 TRACE(sp, "%lu: log_line: reset_f: %lu {%u}\n",
306 ep->l_cur, lno, len);
308 case LOG_LINE_RESET_B:
309 TRACE(sp, "%lu: log_line: reset_b: %lu {%u}\n",
310 ep->l_cur, lno, len);
314 /* Reset high water mark. */
315 ep->l_high = ++ep->l_cur;
322 * Log a mark position. For the log to work, we assume that there
323 * aren't any operations that just put out a log record -- this
324 * would mean that undo operations would only reset marks, and not
325 * cause any other change.
327 * PUBLIC: int log_mark(SCR *, LMARK *);
338 if (F_ISSET(ep, F_NOLOG))
341 /* Put out one initial cursor record per set of changes. */
342 if (ep->l_cursor.lno != OOBLNO) {
343 if (log_cursor1(sp, LOG_CURSOR_INIT))
345 ep->l_cursor.lno = OOBLNO;
348 BINC_RETC(sp, ep->l_lp,
349 ep->l_len, sizeof(u_char) + sizeof(LMARK));
350 ep->l_lp[0] = LOG_MARK;
351 memmove(ep->l_lp + sizeof(u_char), lmp, sizeof(LMARK));
353 key.data = &ep->l_cur;
354 key.size = sizeof(recno_t);
355 data.data = ep->l_lp;
356 data.size = sizeof(u_char) + sizeof(LMARK);
357 if (ep->log->put(ep->log, &key, &data, 0) == -1)
360 #if defined(DEBUG) && 0
361 TRACE(sp, "%lu: mark %c: %lu/%u\n",
362 ep->l_cur, lmp->name, lmp->lno, lmp->cno);
364 /* Reset high water mark. */
365 ep->l_high = ++ep->l_cur;
371 * Roll the log backward one operation.
373 * PUBLIC: int log_backward(SCR *, MARK *);
389 if (F_ISSET(ep, F_NOLOG)) {
391 "010|Logging not being performed, undo not possible");
395 if (ep->l_cur == 1) {
396 msgq(sp, M_BERR, "011|No changes to undo");
400 F_SET(ep, F_NOLOG); /* Turn off logging. */
402 key.data = &ep->l_cur; /* Initialize db request. */
403 key.size = sizeof(recno_t);
406 if (ep->log->get(ep->log, &key, &data, 0))
408 #if defined(DEBUG) && 0
409 log_trace(sp, "log_backward", ep->l_cur, data.data);
411 switch (*(p = (u_char *)data.data)) {
412 case LOG_CURSOR_INIT:
414 memmove(rp, p + sizeof(u_char), sizeof(MARK));
421 case LOG_LINE_APPEND:
422 case LOG_LINE_INSERT:
424 memmove(&lno, p + sizeof(u_char), sizeof(recno_t));
425 if (db_delete(sp, lno))
427 ++sp->rptlines[L_DELETED];
429 case LOG_LINE_DELETE:
431 memmove(&lno, p + sizeof(u_char), sizeof(recno_t));
432 if (apply_with(db_insert, sp, lno,
433 p + CHAR_T_OFFSET, data.size - CHAR_T_OFFSET))
435 ++sp->rptlines[L_ADDED];
437 case LOG_LINE_RESET_F:
439 case LOG_LINE_RESET_B:
441 memmove(&lno, p + sizeof(u_char), sizeof(recno_t));
442 if (apply_with(db_set, sp, lno,
443 p + CHAR_T_OFFSET, data.size - CHAR_T_OFFSET))
445 if (sp->rptlchange != lno) {
446 sp->rptlchange = lno;
447 ++sp->rptlines[L_CHANGED];
452 memmove(&lm, p + sizeof(u_char), sizeof(LMARK));
455 if (mark_set(sp, lm.name, &m, 0))
463 err: F_CLR(ep, F_NOLOG);
469 * Reset the line to its original appearance.
472 * There's a bug in this code due to our not logging cursor movements
473 * unless a change was made. If you do a change, move off the line,
474 * then move back on and do a 'U', the line will be restored to the way
475 * it was before the original change.
477 * PUBLIC: int log_setline(SCR *);
490 if (F_ISSET(ep, F_NOLOG)) {
492 "012|Logging not being performed, undo not possible");
499 F_SET(ep, F_NOLOG); /* Turn off logging. */
501 key.data = &ep->l_cur; /* Initialize db request. */
502 key.size = sizeof(recno_t);
505 if (ep->log->get(ep->log, &key, &data, 0))
507 #if defined(DEBUG) && 0
508 log_trace(sp, "log_setline", ep->l_cur, data.data);
510 switch (*(p = (u_char *)data.data)) {
511 case LOG_CURSOR_INIT:
512 memmove(&m, p + sizeof(u_char), sizeof(MARK));
513 if (m.lno != sp->lno || ep->l_cur == 1) {
519 memmove(&m, p + sizeof(u_char), sizeof(MARK));
520 if (m.lno != sp->lno) {
526 case LOG_LINE_APPEND:
527 case LOG_LINE_INSERT:
528 case LOG_LINE_DELETE:
529 case LOG_LINE_RESET_F:
531 case LOG_LINE_RESET_B:
532 memmove(&lno, p + sizeof(u_char), sizeof(recno_t));
533 if (lno == sp->lno &&
534 apply_with(db_set, sp, lno,
535 p + CHAR_T_OFFSET, data.size - CHAR_T_OFFSET))
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(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 key.data = &ep->l_cur; /* Initialize db request. */
591 key.size = sizeof(recno_t);
594 if (ep->log->get(ep->log, &key, &data, 0))
596 #if defined(DEBUG) && 0
597 log_trace(sp, "log_forward", ep->l_cur, data.data);
599 switch (*(p = (u_char *)data.data)) {
603 memmove(rp, p + sizeof(u_char), sizeof(MARK));
608 case LOG_CURSOR_INIT:
610 case LOG_LINE_APPEND:
611 case LOG_LINE_INSERT:
613 memmove(&lno, p + sizeof(u_char), sizeof(recno_t));
614 if (apply_with(db_insert, sp, lno,
615 p + CHAR_T_OFFSET, data.size - CHAR_T_OFFSET))
617 ++sp->rptlines[L_ADDED];
619 case LOG_LINE_DELETE:
621 memmove(&lno, p + sizeof(u_char), sizeof(recno_t));
622 if (db_delete(sp, lno))
624 ++sp->rptlines[L_DELETED];
626 case LOG_LINE_RESET_B:
628 case LOG_LINE_RESET_F:
630 memmove(&lno, p + sizeof(u_char), sizeof(recno_t));
631 if (apply_with(db_set, sp, lno,
632 p + CHAR_T_OFFSET, data.size - CHAR_T_OFFSET))
634 if (sp->rptlchange != lno) {
635 sp->rptlchange = lno;
636 ++sp->rptlines[L_CHANGED];
641 memmove(&lm, p + sizeof(u_char), sizeof(LMARK));
644 if (mark_set(sp, lm.name, &m, 0))
652 err: F_CLR(ep, F_NOLOG);
658 * Try and restart the log on failure, i.e. if we run out of memory.
668 msgq(sp, M_SYSERR, "015|%s/%d: log put error", tail(file), line);
670 (void)ep->log->close(ep->log);
671 if (!log_init(sp, ep))
672 msgq(sp, M_ERR, "267|Log restarted");
675 #if defined(DEBUG) && 0
688 case LOG_CURSOR_INIT:
689 memmove(&m, p + sizeof(u_char), sizeof(MARK));
690 TRACE(sp, "%lu: %s: C_INIT: %u/%u\n", rno, msg, m.lno, m.cno);
693 memmove(&m, p + sizeof(u_char), sizeof(MARK));
694 TRACE(sp, "%lu: %s: C_END: %u/%u\n", rno, msg, m.lno, m.cno);
696 case LOG_LINE_APPEND:
697 memmove(&lno, p + sizeof(u_char), sizeof(recno_t));
698 TRACE(sp, "%lu: %s: APPEND: %lu\n", rno, msg, lno);
700 case LOG_LINE_INSERT:
701 memmove(&lno, p + sizeof(u_char), sizeof(recno_t));
702 TRACE(sp, "%lu: %s: INSERT: %lu\n", rno, msg, lno);
704 case LOG_LINE_DELETE:
705 memmove(&lno, p + sizeof(u_char), sizeof(recno_t));
706 TRACE(sp, "%lu: %s: DELETE: %lu\n", rno, msg, lno);
708 case LOG_LINE_RESET_F:
709 memmove(&lno, p + sizeof(u_char), sizeof(recno_t));
710 TRACE(sp, "%lu: %s: RESET_F: %lu\n", rno, msg, lno);
712 case LOG_LINE_RESET_B:
713 memmove(&lno, p + sizeof(u_char), sizeof(recno_t));
714 TRACE(sp, "%lu: %s: RESET_B: %lu\n", rno, msg, lno);
717 memmove(&lm, p + sizeof(u_char), sizeof(LMARK));
719 "%lu: %s: MARK: %u/%u\n", rno, msg, lm.lno, lm.cno);
729 * Apply a realigned line from the log db to the file db.
733 int (*db_func)(SCR *, recno_t, CHAR_T *, size_t),
740 typedef unsigned long nword;
744 nword *lp = (nword *)((uintptr_t)p / sizeof(nword) * sizeof(nword));
746 if (lp != (nword *)p) {
747 int offl = ((uintptr_t)p - (uintptr_t)lp) << 3;
748 int offr = (sizeof(nword) << 3) - offl;
749 size_t i, cnt = (len + sizeof(nword) / 2) / sizeof(nword);
752 blen = p2roundup(MAX(len, 512));
753 REALLOC(sp, bp, nword *, blen);
757 for (i = 0; i < cnt; ++i)
758 #if BYTE_ORDER == BIG_ENDIAN
759 bp[i] = (lp[i] << offl) ^ (lp[i+1] >> offr);
761 bp[i] = (lp[i] >> offl) ^ (lp[i+1] << offr);
766 return db_func(sp, lno, (CHAR_T *)p, len / sizeof(CHAR_T));