1 /* $NetBSD: log1.c,v 1.4 2014/01/26 21:43:45 christos Exp $ */
3 * Copyright (c) 1992, 1993, 1994
4 * The Regents of the University of California. All rights reserved.
5 * Copyright (c) 1992, 1993, 1994, 1995, 1996
6 * Keith Bostic. All rights reserved.
8 * See the LICENSE file for redistribution information.
13 #include <sys/cdefs.h>
16 static const char sccsid
[] = "Id: log.c,v 10.26 2002/03/02 23:12:13 skimo Exp (Berkeley) Date: 2002/03/02 23:12:13 ";
19 __RCSID("$NetBSD: log1.c,v 1.4 2014/01/26 21:43:45 christos Exp $");
22 #include <sys/types.h>
23 #include <sys/queue.h>
26 #include <bitstring.h>
37 * The log consists of records, each containing a type byte and a variable
38 * length byte string, as follows:
40 * LOG_CURSOR_INIT MARK
42 * LOG_LINE_APPEND_F db_recno_t char *
43 * LOG_LINE_APPEND_B db_recno_t char *
44 * LOG_LINE_DELETE_F db_recno_t char *
45 * LOG_LINE_DELETE_B db_recno_t char *
46 * LOG_LINE_RESET_F db_recno_t char *
47 * LOG_LINE_RESET_B db_recno_t char *
50 * We do before image physical logging. This means that the editor layer
51 * MAY NOT modify records in place, even if simply deleting or overwriting
52 * characters. Since the smallest unit of logging is a line, we're using
53 * up lots of space. This may eventually have to be reduced, probably by
54 * doing logical logging, which is a much cooler database phrase.
56 * The implementation of the historic vi 'u' command, using roll-forward and
57 * roll-back, is simple. Each set of changes has a LOG_CURSOR_INIT record,
58 * followed by a number of other records, followed by a LOG_CURSOR_END record.
59 * LOG_LINE_RESET records come in pairs. The first is a LOG_LINE_RESET_B
60 * record, and is the line before the change. The second is LOG_LINE_RESET_F,
61 * and is the line after the change. Roll-back is done by backing up to the
62 * first LOG_CURSOR_INIT record before a change. Roll-forward is done in a
65 * The 'U' command is implemented by rolling backward to a LOG_CURSOR_END
66 * record for a line different from the current one. It should be noted that
67 * this means that a subsequent 'u' command will make a change based on the
68 * new position of the log's cursor. This is okay, and, in fact, historic vi
72 static int log_cursor1
__P((SCR
*, int));
73 static void log_err
__P((SCR
*, const char *, int));
74 #if defined(LOGDEBUG) && defined(TRACE)
75 static void log_trace
__P((SCR
*, const char *, db_recno_t
, u_char
*));
78 /* Try and restart the log on failure, i.e. if we run out of memory. */
80 log_err(sp, __FILE__, __LINE__); \
84 /* offset of CHAR_T string in log needs to be aligned on some systems
85 * because it is passed to db_set as a string
88 char data
[sizeof(u_char
) /* type */ + sizeof(db_recno_t
)];
91 #define CHAR_T_OFFSET ((char *)(((log_t*)0)->str) - (char *)0)
95 * Initialize the logging subsystem.
97 * PUBLIC: int log_init __P((SCR *, EXF *));
100 log_init(SCR
*sp
, EXF
*ep
)
104 * ep MAY NOT BE THE SAME AS sp->ep, DON'T USE THE LATTER.
106 * Initialize the buffer. The logging subsystem has its own
107 * buffers because the global ones are almost by definition
108 * going to be in use when the log runs.
112 ep
->l_cursor
.lno
= 1; /* XXX Any valid recno. */
113 ep
->l_cursor
.cno
= 0;
114 ep
->l_high
= ep
->l_cur
= 1;
116 ep
->log
= dbopen(NULL
, O_CREAT
| O_NONBLOCK
| O_RDWR
,
117 S_IRUSR
| S_IWUSR
, DB_RECNO
, NULL
);
118 if (ep
->log
== NULL
) {
119 msgq(sp
, M_SYSERR
, "009|Log file");
125 /*LOCK_INIT(sp->wp, ep);*/
132 * Close the logging subsystem.
134 * PUBLIC: int log_end __P((SCR *, EXF *));
137 log_end(SCR
*sp
, EXF
*ep
)
141 * ep MAY NOT BE THE SAME AS sp->ep, DON'T USE THE LATTER.
143 /*LOCK_END(sp->wp, ep);*/
144 if (ep
->log
!= NULL
) {
145 (void)(ep
->log
->close
)(ep
->log
);
148 if (sp
->wp
->l_lp
!= NULL
) {
153 ep
->l_cursor
.lno
= 1; /* XXX Any valid recno. */
154 ep
->l_cursor
.cno
= 0;
155 ep
->l_high
= ep
->l_cur
= 1;
161 * Log the current cursor position, starting an event.
163 * PUBLIC: int log_cursor __P((SCR *));
171 if (F_ISSET(ep
, F_NOLOG
))
175 * If any changes were made since the last cursor init,
176 * put out the ending cursor record.
178 if (ep
->l_cursor
.lno
== OOBLNO
) {
179 if (ep
->l_win
&& ep
->l_win
!= sp
->wp
)
181 ep
->l_cursor
.lno
= sp
->lno
;
182 ep
->l_cursor
.cno
= sp
->cno
;
184 return (log_cursor1(sp
, LOG_CURSOR_END
));
186 ep
->l_cursor
.lno
= sp
->lno
;
187 ep
->l_cursor
.cno
= sp
->cno
;
193 * Actually push a cursor record out.
196 log_cursor1(SCR
*sp
, int type
)
204 if (type == LOG_CURSOR_INIT &&
205 LOCK_TRY(sp->wp, ep))
209 BINC_RETC(sp
, sp
->wp
->l_lp
, sp
->wp
->l_len
, sizeof(u_char
) + sizeof(MARK
));
210 sp
->wp
->l_lp
[0] = type
;
211 memmove(sp
->wp
->l_lp
+ sizeof(u_char
), &ep
->l_cursor
, sizeof(MARK
));
213 memset(&key
, 0, sizeof(key
));
214 key
.data
= &ep
->l_cur
;
215 key
.size
= sizeof(db_recno_t
);
216 memset(&data
, 0, sizeof(data
));
217 data
.data
= sp
->wp
->l_lp
;
218 data
.size
= sizeof(u_char
) + sizeof(MARK
);
219 if (ep
->log
->put(ep
->log
, &key
, &data
, 0) == -1)
222 #if defined(LOGDEBUG) && defined(TRACE)
223 vtrace("%lu: %s: %u/%u\n", ep
->l_cur
,
224 type
== LOG_CURSOR_INIT
? "log_cursor_init" : "log_cursor_end",
227 /* Reset high water mark. */
228 ep
->l_high
= ++ep
->l_cur
;
231 if (type == LOG_CURSOR_END)
232 LOCK_UNLOCK(sp->wp, ep);
241 * PUBLIC: int log_line __P((SCR *, db_recno_t, u_int));
244 log_line(SCR
*sp
, db_recno_t lno
, u_int action
)
253 if (F_ISSET(ep
, F_NOLOG
))
259 * Kluge for vi. Clear the EXF undo flag so that the
260 * next 'u' command does a roll-back, regardless.
264 /* Put out one initial cursor record per set of changes. */
265 if (ep
->l_cursor
.lno
!= OOBLNO
) {
266 if (log_cursor1(sp
, LOG_CURSOR_INIT
))
268 ep
->l_cursor
.lno
= OOBLNO
;
270 } /*else if (ep->l_win != sp->wp) {
271 printf("log_line own: %p, this: %p\n", ep->l_win, sp->wp);
276 /* newly added for DB4 logging */
277 case LOG_LINE_APPEND_B
:
278 case LOG_LINE_DELETE_F
:
283 * Put out the changes. If it's a LOG_LINE_RESET_B call, it's a
284 * special case, avoid the caches. Also, if it fails and it's
285 * line 1, it just means that the user started with an empty file,
286 * so fake an empty length line.
288 if (action
== LOG_LINE_RESET_B
) {
289 if (db_get(sp
, lno
, DBG_NOCACHE
, &lp
, &len
)) {
290 static CHAR_T nul
= 0;
299 if (db_get(sp
, lno
, DBG_FATAL
, &lp
, &len
))
302 sp
->wp
->l_lp
, sp
->wp
->l_len
,
303 len
* sizeof(CHAR_T
) + CHAR_T_OFFSET
);
304 sp
->wp
->l_lp
[0] = action
;
305 memmove(sp
->wp
->l_lp
+ sizeof(u_char
), &lno
, sizeof(db_recno_t
));
306 MEMMOVEW(sp
->wp
->l_lp
+ CHAR_T_OFFSET
, lp
, len
);
309 memset(&key
, 0, sizeof(key
));
311 key
.size
= sizeof(db_recno_t
);
312 memset(&data
, 0, sizeof(data
));
313 data
.data
= sp
->wp
->l_lp
;
314 data
.size
= len
* sizeof(CHAR_T
) + CHAR_T_OFFSET
;
315 if (ep
->log
->put(ep
->log
, &key
, &data
, 0) == -1)
318 #if defined(LOGDEBUG) && defined(TRACE)
320 case LOG_LINE_APPEND_F
:
321 vtrace("%u: log_line: append_f: %lu {%u}\n",
322 ep
->l_cur
, lno
, len
);
324 case LOG_LINE_APPEND_B
:
325 vtrace("%u: log_line: append_b: %lu {%u}\n",
326 ep
->l_cur
, lno
, len
);
328 case LOG_LINE_DELETE_F
:
329 vtrace("%lu: log_line: delete_f: %lu {%u}\n",
330 ep
->l_cur
, lno
, len
);
332 case LOG_LINE_DELETE_B
:
333 vtrace("%lu: log_line: delete_b: %lu {%u}\n",
334 ep
->l_cur
, lno
, len
);
336 case LOG_LINE_RESET_F
:
337 vtrace("%lu: log_line: reset_f: %lu {%u}\n",
338 ep
->l_cur
, lno
, len
);
340 case LOG_LINE_RESET_B
:
341 vtrace("%lu: log_line: reset_b: %lu {%u}\n",
342 ep
->l_cur
, lno
, len
);
346 /* Reset high water mark. */
347 ep
->l_high
= ++ep
->l_cur
;
354 * Log a mark position. For the log to work, we assume that there
355 * aren't any operations that just put out a log record -- this
356 * would mean that undo operations would only reset marks, and not
357 * cause any other change.
359 * PUBLIC: int log_mark __P((SCR *, LMARK *));
362 log_mark(SCR
*sp
, LMARK
*lmp
)
368 if (F_ISSET(ep
, F_NOLOG
))
371 /* Put out one initial cursor record per set of changes. */
372 if (ep
->l_cursor
.lno
!= OOBLNO
) {
373 if (log_cursor1(sp
, LOG_CURSOR_INIT
))
375 ep
->l_cursor
.lno
= OOBLNO
;
379 BINC_RETC(sp
, sp
->wp
->l_lp
,
380 sp
->wp
->l_len
, sizeof(u_char
) + sizeof(LMARK
));
381 sp
->wp
->l_lp
[0] = LOG_MARK
;
382 memmove(sp
->wp
->l_lp
+ sizeof(u_char
), lmp
, sizeof(LMARK
));
384 memset(&key
, 0, sizeof(key
));
385 key
.data
= &ep
->l_cur
;
386 key
.size
= sizeof(db_recno_t
);
387 memset(&data
, 0, sizeof(data
));
388 data
.data
= sp
->wp
->l_lp
;
389 data
.size
= sizeof(u_char
) + sizeof(LMARK
);
390 if (ep
->log
->put(ep
->log
, &key
, &data
, 0) == -1)
393 #if defined(LOGDEBUG) && defined(TRACE)
394 vtrace("%lu: mark %c: %lu/%u\n",
395 ep
->l_cur
, lmp
->name
, lmp
->lno
, lmp
->cno
);
397 /* Reset high water mark. */
398 ep
->l_high
= ++ep
->l_cur
;
404 * Roll the log backward one operation.
406 * PUBLIC: int log_backward __P((SCR *, MARK *));
409 log_backward(SCR
*sp
, MARK
*rp
)
420 if (F_ISSET(ep
, F_NOLOG
)) {
422 "010|Logging not being performed, undo not possible");
426 if (ep
->l_cur
== 1) {
427 msgq(sp
, M_BERR
, "011|No changes to undo");
431 if (ep
->l_win
&& ep
->l_win
!= sp
->wp
) {
432 ex_emsg(sp
, NULL
, EXM_LOCKED
);
438 F_SET(ep
, F_NOLOG
); /* Turn off logging. */
440 key
.data
= &ep
->l_cur
; /* Initialize db request. */
441 key
.size
= sizeof(recno_t
);
444 if (ep
->log
->get(ep
->log
, &key
, &data
, 0))
446 #if defined(LOGDEBUG) && defined(TRACE)
447 log_trace(sp
, "log_backward", ep
->l_cur
, data
.data
);
449 switch (*(p
= (u_char
*)data
.data
)) {
450 case LOG_CURSOR_INIT
:
452 memmove(rp
, p
+ sizeof(u_char
), sizeof(MARK
));
460 case LOG_LINE_APPEND_F
:
462 memmove(&lno
, p
+ sizeof(u_char
), sizeof(db_recno_t
));
463 if (db_delete(sp
, lno
))
465 ++sp
->rptlines
[L_DELETED
];
467 case LOG_LINE_DELETE_B
:
469 memmove(&lno
, p
+ sizeof(u_char
), sizeof(db_recno_t
));
470 if (db_insert(sp
, lno
,
471 (CHAR_T
*)(p
+ CHAR_T_OFFSET
),
472 (data
.size
- CHAR_T_OFFSET
) / sizeof(CHAR_T
)))
474 ++sp
->rptlines
[L_ADDED
];
476 case LOG_LINE_RESET_F
:
478 case LOG_LINE_RESET_B
:
480 memmove(&lno
, p
+ sizeof(u_char
), sizeof(db_recno_t
));
482 (CHAR_T
*)(p
+ CHAR_T_OFFSET
),
483 (data
.size
- CHAR_T_OFFSET
) / sizeof(CHAR_T
)))
485 if (sp
->rptlchange
!= lno
) {
486 sp
->rptlchange
= lno
;
487 ++sp
->rptlines
[L_CHANGED
];
492 memmove(&lm
, p
+ sizeof(u_char
), sizeof(LMARK
));
495 if (mark_set(sp
, lm
.name
, &m
, 0))
503 err
: F_CLR(ep
, F_NOLOG
);
510 * Reset the line to its original appearance.
513 * There's a bug in this code due to our not logging cursor movements
514 * unless a change was made. If you do a change, move off the line,
515 * then move back on and do a 'U', the line will be restored to the way
516 * it was before the original change.
518 * PUBLIC: int log_setline __P((SCR *));
531 if (F_ISSET(ep
, F_NOLOG
)) {
533 "012|Logging not being performed, undo not possible");
540 if (ep
->l_win
&& ep
->l_win
!= sp
->wp
) {
541 ex_emsg(sp
, NULL
, EXM_LOCKED
);
546 F_SET(ep
, F_NOLOG
); /* Turn off logging. */
548 key
.data
= &ep
->l_cur
; /* Initialize db request. */
549 key
.size
= sizeof(recno_t
);
553 if (ep
->log
->get(ep
->log
, &key
, &data
, 0))
555 #if defined(LOGDEBUG) && defined(TRACE)
556 log_trace(sp
, "log_setline", ep
->l_cur
, data
.data
);
558 switch (*(p
= (u_char
*)data
.data
)) {
559 case LOG_CURSOR_INIT
:
560 memmove(&m
, p
+ sizeof(u_char
), sizeof(MARK
));
561 if (m
.lno
!= sp
->lno
|| ep
->l_cur
== 1) {
568 memmove(&m
, p
+ sizeof(u_char
), sizeof(MARK
));
569 if (m
.lno
!= sp
->lno
) {
576 case LOG_LINE_APPEND_F
:
577 case LOG_LINE_DELETE_B
:
578 case LOG_LINE_RESET_F
:
580 case LOG_LINE_RESET_B
:
581 memmove(&lno
, p
+ sizeof(u_char
), sizeof(db_recno_t
));
582 if (lno
== sp
->lno
&&
583 db_set(sp
, lno
, (CHAR_T
*)(p
+ CHAR_T_OFFSET
),
584 (data
.size
- CHAR_T_OFFSET
) / sizeof(CHAR_T
)))
586 if (sp
->rptlchange
!= lno
) {
587 sp
->rptlchange
= lno
;
588 ++sp
->rptlines
[L_CHANGED
];
591 memmove(&lm
, p
+ sizeof(u_char
), sizeof(LMARK
));
594 if (mark_set(sp
, lm
.name
, &m
, 0))
602 err
: F_CLR(ep
, F_NOLOG
);
609 * Roll the log forward one operation.
611 * PUBLIC: int log_forward __P((SCR *, MARK *));
614 log_forward(SCR
*sp
, MARK
*rp
)
625 if (F_ISSET(ep
, F_NOLOG
)) {
627 "013|Logging not being performed, roll-forward not possible");
631 if (ep
->l_cur
== ep
->l_high
) {
632 msgq(sp
, M_BERR
, "014|No changes to re-do");
636 if (ep
->l_win
&& ep
->l_win
!= sp
->wp
) {
637 ex_emsg(sp
, NULL
, EXM_LOCKED
);
642 F_SET(ep
, F_NOLOG
); /* Turn off logging. */
644 key
.data
= &ep
->l_cur
; /* Initialize db request. */
645 key
.size
= sizeof(recno_t
);
648 if (ep
->log
->get(ep
->log
, &key
, &data
, 0))
650 #if defined(LOGDEBUG) && defined(TRACE)
651 log_trace(sp
, "log_forward", ep
->l_cur
, data
.data
);
653 switch (*(p
= (u_char
*)data
.data
)) {
657 memmove(rp
, p
+ sizeof(u_char
), sizeof(MARK
));
663 case LOG_CURSOR_INIT
:
665 case LOG_LINE_APPEND_F
:
667 memmove(&lno
, p
+ sizeof(u_char
), sizeof(db_recno_t
));
668 if (db_insert(sp
, lno
,
669 (CHAR_T
*)(p
+ CHAR_T_OFFSET
),
670 (data
.size
- CHAR_T_OFFSET
) / sizeof(CHAR_T
)))
672 ++sp
->rptlines
[L_ADDED
];
674 case LOG_LINE_DELETE_B
:
676 memmove(&lno
, p
+ sizeof(u_char
), sizeof(db_recno_t
));
677 if (db_delete(sp
, lno
))
679 ++sp
->rptlines
[L_DELETED
];
681 case LOG_LINE_RESET_B
:
683 case LOG_LINE_RESET_F
:
685 memmove(&lno
, p
+ sizeof(u_char
), sizeof(db_recno_t
));
687 (CHAR_T
*)(p
+ CHAR_T_OFFSET
),
688 (data
.size
- CHAR_T_OFFSET
) / sizeof(CHAR_T
)))
690 if (sp
->rptlchange
!= lno
) {
691 sp
->rptlchange
= lno
;
692 ++sp
->rptlines
[L_CHANGED
];
697 memmove(&lm
, p
+ sizeof(u_char
), sizeof(LMARK
));
700 if (mark_set(sp
, lm
.name
, &m
, 0))
708 err
: F_CLR(ep
, F_NOLOG
);
715 * Try and restart the log on failure, i.e. if we run out of memory.
718 log_err(SCR
*sp
, const char *file
, int line
)
722 msgq(sp
, M_SYSERR
, "015|%s/%d: log put error", tail(file
), line
);
724 (void)ep
->log
->close(ep
->log
);
725 if (!log_init(sp
, ep
))
726 msgq(sp
, M_ERR
, "267|Log restarted");
729 #if defined(LOGDEBUG) && defined(TRACE)
731 log_trace(sp
, msg
, rno
, p
)
742 case LOG_CURSOR_INIT
:
743 memmove(&m
, p
+ sizeof(u_char
), sizeof(MARK
));
744 vtrace("%lu: %s: C_INIT: %u/%u\n", rno
, msg
, m
.lno
, m
.cno
);
747 memmove(&m
, p
+ sizeof(u_char
), sizeof(MARK
));
748 vtrace("%lu: %s: C_END: %u/%u\n", rno
, msg
, m
.lno
, m
.cno
);
750 case LOG_LINE_APPEND_F
:
751 memmove(&lno
, p
+ sizeof(u_char
), sizeof(db_recno_t
));
752 vtrace("%lu: %s: APPEND_F: %lu\n", rno
, msg
, lno
);
754 case LOG_LINE_APPEND_B
:
755 memmove(&lno
, p
+ sizeof(u_char
), sizeof(db_recno_t
));
756 vtrace("%lu: %s: APPEND_B: %lu\n", rno
, msg
, lno
);
758 case LOG_LINE_DELETE_F
:
759 memmove(&lno
, p
+ sizeof(u_char
), sizeof(db_recno_t
));
760 vtrace("%lu: %s: DELETE_F: %lu\n", rno
, msg
, lno
);
762 case LOG_LINE_DELETE_B
:
763 memmove(&lno
, p
+ sizeof(u_char
), sizeof(db_recno_t
));
764 vtrace("%lu: %s: DELETE_B: %lu\n", rno
, msg
, lno
);
766 case LOG_LINE_RESET_F
:
767 memmove(&lno
, p
+ sizeof(u_char
), sizeof(db_recno_t
));
768 vtrace("%lu: %s: RESET_F: %lu\n", rno
, msg
, lno
);
770 case LOG_LINE_RESET_B
:
771 memmove(&lno
, p
+ sizeof(u_char
), sizeof(db_recno_t
));
772 vtrace("%lu: %s: RESET_B: %lu\n", rno
, msg
, lno
);
775 memmove(&lm
, p
+ sizeof(u_char
), sizeof(LMARK
));
776 vtrace("%lu: %s: MARK: %u/%u\n", rno
, msg
, lm
.lno
, lm
.cno
);