log.c revision 1.3 1 1.3 christos /* $NetBSD: log.c,v 1.3 2014/01/26 21:43:45 christos Exp $ */
2 1.1 christos /*-
3 1.1 christos * Copyright (c) 1992, 1993, 1994
4 1.1 christos * The Regents of the University of California. All rights reserved.
5 1.1 christos * Copyright (c) 1992, 1993, 1994, 1995, 1996
6 1.1 christos * Keith Bostic. All rights reserved.
7 1.1 christos *
8 1.1 christos * See the LICENSE file for redistribution information.
9 1.1 christos */
10 1.1 christos
11 1.1 christos #include "config.h"
12 1.1 christos
13 1.3 christos #include <sys/cdefs.h>
14 1.3 christos #if 0
15 1.1 christos #ifndef lint
16 1.1 christos 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 ";
17 1.1 christos #endif /* not lint */
18 1.3 christos #else
19 1.3 christos __RCSID("$NetBSD: log.c,v 1.3 2014/01/26 21:43:45 christos Exp $");
20 1.3 christos #endif
21 1.1 christos
22 1.1 christos #include <sys/types.h>
23 1.1 christos #include <sys/queue.h>
24 1.1 christos #include <sys/stat.h>
25 1.1 christos
26 1.1 christos #include <bitstring.h>
27 1.1 christos #include <errno.h>
28 1.1 christos #include <fcntl.h>
29 1.1 christos #include <limits.h>
30 1.1 christos #include <stdio.h>
31 1.1 christos #include <stdlib.h>
32 1.1 christos #include <string.h>
33 1.1 christos
34 1.1 christos #include "common.h"
35 1.2 christos #include "dbinternal.h"
36 1.1 christos
37 1.1 christos /*
38 1.1 christos * The log consists of records, each containing a type byte and a variable
39 1.1 christos * length byte string, as follows:
40 1.1 christos *
41 1.1 christos * LOG_CURSOR_INIT MARK
42 1.1 christos * LOG_CURSOR_END MARK
43 1.1 christos * LOG_LINE_APPEND_F db_recno_t char *
44 1.1 christos * LOG_LINE_APPEND_B db_recno_t char *
45 1.1 christos * LOG_LINE_DELETE_F db_recno_t char *
46 1.1 christos * LOG_LINE_DELETE_B db_recno_t char *
47 1.1 christos * LOG_LINE_RESET_F db_recno_t char *
48 1.1 christos * LOG_LINE_RESET_B db_recno_t char *
49 1.1 christos * LOG_MARK LMARK
50 1.1 christos *
51 1.1 christos * We do before image physical logging. This means that the editor layer
52 1.1 christos * MAY NOT modify records in place, even if simply deleting or overwriting
53 1.1 christos * characters. Since the smallest unit of logging is a line, we're using
54 1.1 christos * up lots of space. This may eventually have to be reduced, probably by
55 1.1 christos * doing logical logging, which is a much cooler database phrase.
56 1.1 christos *
57 1.1 christos * The implementation of the historic vi 'u' command, using roll-forward and
58 1.1 christos * roll-back, is simple. Each set of changes has a LOG_CURSOR_INIT record,
59 1.1 christos * followed by a number of other records, followed by a LOG_CURSOR_END record.
60 1.1 christos * LOG_LINE_RESET records come in pairs. The first is a LOG_LINE_RESET_B
61 1.1 christos * record, and is the line before the change. The second is LOG_LINE_RESET_F,
62 1.1 christos * and is the line after the change. Roll-back is done by backing up to the
63 1.1 christos * first LOG_CURSOR_INIT record before a change. Roll-forward is done in a
64 1.1 christos * similar fashion.
65 1.1 christos *
66 1.1 christos * The 'U' command is implemented by rolling backward to a LOG_CURSOR_END
67 1.1 christos * record for a line different from the current one. It should be noted that
68 1.1 christos * this means that a subsequent 'u' command will make a change based on the
69 1.1 christos * new position of the log's cursor. This is okay, and, in fact, historic vi
70 1.1 christos * behaved that way.
71 1.1 christos */
72 1.1 christos
73 1.1 christos static int vi_log_get __P((SCR *sp, db_recno_t *lnop, size_t *size));
74 1.1 christos static int log_cursor1 __P((SCR *, int));
75 1.2 christos static void log_err __P((SCR *, const char *, int));
76 1.1 christos #if defined(DEBUG) && 0
77 1.2 christos static void log_trace __P((SCR *, const char *, db_recno_t, u_char *));
78 1.1 christos #endif
79 1.1 christos
80 1.1 christos /* Try and restart the log on failure, i.e. if we run out of memory. */
81 1.1 christos #define LOG_ERR { \
82 1.1 christos log_err(sp, __FILE__, __LINE__); \
83 1.1 christos return (1); \
84 1.1 christos }
85 1.1 christos
86 1.1 christos /* offset of CHAR_T string in log needs to be aligned on some systems
87 1.1 christos * because it is passed to db_set as a string
88 1.1 christos */
89 1.1 christos typedef struct {
90 1.1 christos char data[sizeof(u_char) /* type */ + sizeof(db_recno_t)];
91 1.1 christos CHAR_T str[1];
92 1.1 christos } log_t;
93 1.1 christos #define CHAR_T_OFFSET ((char *)(((log_t*)0)->str) - (char *)0)
94 1.1 christos
95 1.1 christos /*
96 1.1 christos * log_init --
97 1.1 christos * Initialize the logging subsystem.
98 1.1 christos *
99 1.1 christos * PUBLIC: int log_init __P((SCR *, EXF *));
100 1.1 christos */
101 1.1 christos int
102 1.1 christos log_init(SCR *sp, EXF *ep)
103 1.1 christos {
104 1.1 christos /*
105 1.1 christos * !!!
106 1.1 christos * ep MAY NOT BE THE SAME AS sp->ep, DON'T USE THE LATTER.
107 1.1 christos *
108 1.1 christos * Initialize the buffer. The logging subsystem has its own
109 1.1 christos * buffers because the global ones are almost by definition
110 1.1 christos * going to be in use when the log runs.
111 1.1 christos */
112 1.1 christos sp->wp->l_lp = NULL;
113 1.1 christos sp->wp->l_len = 0;
114 1.1 christos ep->l_cursor.lno = 1; /* XXX Any valid recno. */
115 1.1 christos ep->l_cursor.cno = 0;
116 1.1 christos ep->l_high = ep->l_cur = 1;
117 1.1 christos
118 1.1 christos if (db_create(&ep->log, 0, 0) != 0 ||
119 1.1 christos db_open(ep->log, NULL, DB_RECNO,
120 1.1 christos DB_CREATE | VI_DB_THREAD, S_IRUSR | S_IWUSR) != 0) {
121 1.1 christos msgq(sp, M_SYSERR, "009|Log file");
122 1.1 christos F_SET(ep, F_NOLOG);
123 1.1 christos return (1);
124 1.1 christos }
125 1.1 christos
126 1.1 christos ep->l_win = NULL;
127 1.1 christos /*LOCK_INIT(sp->wp, ep);*/
128 1.1 christos
129 1.1 christos return (0);
130 1.1 christos }
131 1.1 christos
132 1.1 christos /*
133 1.1 christos * log_end --
134 1.1 christos * Close the logging subsystem.
135 1.1 christos *
136 1.1 christos * PUBLIC: int log_end __P((SCR *, EXF *));
137 1.1 christos */
138 1.1 christos int
139 1.1 christos log_end(SCR *sp, EXF *ep)
140 1.1 christos {
141 1.1 christos /*
142 1.1 christos * !!!
143 1.1 christos * ep MAY NOT BE THE SAME AS sp->ep, DON'T USE THE LATTER.
144 1.1 christos */
145 1.1 christos /*LOCK_END(sp->wp, ep);*/
146 1.1 christos if (ep->log != NULL) {
147 1.1 christos (void)(ep->log->close)(ep->log, DB_NOSYNC);
148 1.1 christos ep->log = NULL;
149 1.1 christos }
150 1.1 christos if (sp->wp->l_lp != NULL) {
151 1.1 christos free(sp->wp->l_lp);
152 1.1 christos sp->wp->l_lp = NULL;
153 1.1 christos }
154 1.1 christos sp->wp->l_len = 0;
155 1.1 christos ep->l_cursor.lno = 1; /* XXX Any valid recno. */
156 1.1 christos ep->l_cursor.cno = 0;
157 1.1 christos ep->l_high = ep->l_cur = 1;
158 1.1 christos return (0);
159 1.1 christos }
160 1.1 christos
161 1.1 christos /*
162 1.1 christos * log_cursor --
163 1.1 christos * Log the current cursor position, starting an event.
164 1.1 christos *
165 1.1 christos * PUBLIC: int log_cursor __P((SCR *));
166 1.1 christos */
167 1.1 christos int
168 1.1 christos log_cursor(SCR *sp)
169 1.1 christos {
170 1.1 christos EXF *ep;
171 1.1 christos
172 1.1 christos ep = sp->ep;
173 1.1 christos if (F_ISSET(ep, F_NOLOG))
174 1.1 christos return (0);
175 1.1 christos
176 1.1 christos /*
177 1.1 christos * If any changes were made since the last cursor init,
178 1.1 christos * put out the ending cursor record.
179 1.1 christos */
180 1.1 christos if (ep->l_cursor.lno == OOBLNO) {
181 1.1 christos if (ep->l_win && ep->l_win != sp->wp)
182 1.1 christos return 0;
183 1.1 christos ep->l_cursor.lno = sp->lno;
184 1.1 christos ep->l_cursor.cno = sp->cno;
185 1.1 christos ep->l_win = NULL;
186 1.1 christos return (log_cursor1(sp, LOG_CURSOR_END));
187 1.1 christos }
188 1.1 christos ep->l_cursor.lno = sp->lno;
189 1.1 christos ep->l_cursor.cno = sp->cno;
190 1.1 christos return (0);
191 1.1 christos }
192 1.1 christos
193 1.1 christos /*
194 1.1 christos * log_cursor1 --
195 1.1 christos * Actually push a cursor record out.
196 1.1 christos */
197 1.1 christos static int
198 1.1 christos log_cursor1(SCR *sp, int type)
199 1.1 christos {
200 1.1 christos DBT data, key;
201 1.1 christos EXF *ep;
202 1.1 christos
203 1.1 christos ep = sp->ep;
204 1.1 christos
205 1.1 christos /*
206 1.1 christos if (type == LOG_CURSOR_INIT &&
207 1.1 christos LOCK_TRY(sp->wp, ep))
208 1.1 christos return 1;
209 1.1 christos */
210 1.1 christos
211 1.1 christos BINC_RETC(sp, sp->wp->l_lp, sp->wp->l_len, sizeof(u_char) + sizeof(MARK));
212 1.1 christos sp->wp->l_lp[0] = type;
213 1.1 christos memmove(sp->wp->l_lp + sizeof(u_char), &ep->l_cursor, sizeof(MARK));
214 1.1 christos
215 1.1 christos memset(&key, 0, sizeof(key));
216 1.1 christos key.data = &ep->l_cur;
217 1.1 christos key.size = sizeof(db_recno_t);
218 1.1 christos memset(&data, 0, sizeof(data));
219 1.1 christos data.data = sp->wp->l_lp;
220 1.1 christos data.size = sizeof(u_char) + sizeof(MARK);
221 1.1 christos if (ep->log->put(ep->log, NULL, &key, &data, 0) == -1)
222 1.1 christos LOG_ERR;
223 1.1 christos
224 1.1 christos #if defined(DEBUG) && 0
225 1.1 christos vtrace(sp, "%lu: %s: %u/%u\n", ep->l_cur,
226 1.1 christos type == LOG_CURSOR_INIT ? "log_cursor_init" : "log_cursor_end",
227 1.1 christos sp->lno, sp->cno);
228 1.1 christos #endif
229 1.1 christos /* Reset high water mark. */
230 1.1 christos ep->l_high = ++ep->l_cur;
231 1.1 christos
232 1.1 christos /*
233 1.1 christos if (type == LOG_CURSOR_END)
234 1.1 christos LOCK_UNLOCK(sp->wp, ep);
235 1.1 christos */
236 1.1 christos return (0);
237 1.1 christos }
238 1.1 christos
239 1.1 christos /*
240 1.1 christos * log_line --
241 1.1 christos * Log a line change.
242 1.1 christos *
243 1.1 christos * PUBLIC: int log_line __P((SCR *, db_recno_t, u_int));
244 1.1 christos */
245 1.1 christos int
246 1.1 christos log_line(SCR *sp, db_recno_t lno, u_int action)
247 1.1 christos {
248 1.1 christos DBT data, key;
249 1.1 christos EXF *ep;
250 1.1 christos size_t len;
251 1.1 christos CHAR_T *lp;
252 1.1 christos db_recno_t lcur;
253 1.1 christos
254 1.1 christos ep = sp->ep;
255 1.1 christos if (F_ISSET(ep, F_NOLOG))
256 1.1 christos return (0);
257 1.1 christos
258 1.1 christos /*
259 1.1 christos * XXX
260 1.1 christos *
261 1.1 christos * Kluge for vi. Clear the EXF undo flag so that the
262 1.1 christos * next 'u' command does a roll-back, regardless.
263 1.1 christos */
264 1.1 christos F_CLR(ep, F_UNDO);
265 1.1 christos
266 1.1 christos /* Put out one initial cursor record per set of changes. */
267 1.1 christos if (ep->l_cursor.lno != OOBLNO) {
268 1.1 christos if (log_cursor1(sp, LOG_CURSOR_INIT))
269 1.1 christos return (1);
270 1.1 christos ep->l_cursor.lno = OOBLNO;
271 1.1 christos ep->l_win = sp->wp;
272 1.1 christos } /*else if (ep->l_win != sp->wp) {
273 1.1 christos printf("log_line own: %p, this: %p\n", ep->l_win, sp->wp);
274 1.1 christos return 1;
275 1.1 christos }*/
276 1.1 christos
277 1.1 christos switch (action) {
278 1.1 christos /* newly added for DB4 logging */
279 1.1 christos case LOG_LINE_APPEND_B:
280 1.1 christos case LOG_LINE_DELETE_F:
281 1.1 christos return 0;
282 1.1 christos }
283 1.1 christos
284 1.1 christos /*
285 1.1 christos * Put out the changes. If it's a LOG_LINE_RESET_B call, it's a
286 1.1 christos * special case, avoid the caches. Also, if it fails and it's
287 1.1 christos * line 1, it just means that the user started with an empty file,
288 1.1 christos * so fake an empty length line.
289 1.1 christos */
290 1.1 christos if (action == LOG_LINE_RESET_B) {
291 1.1 christos if (db_get(sp, lno, DBG_NOCACHE, &lp, &len)) {
292 1.1 christos static CHAR_T nul = 0;
293 1.1 christos if (lno != 1) {
294 1.1 christos db_err(sp, lno);
295 1.1 christos return (1);
296 1.1 christos }
297 1.1 christos len = 0;
298 1.1 christos lp = &nul;
299 1.1 christos }
300 1.1 christos } else
301 1.1 christos if (db_get(sp, lno, DBG_FATAL, &lp, &len))
302 1.1 christos return (1);
303 1.1 christos BINC_RETC(sp,
304 1.1 christos sp->wp->l_lp, sp->wp->l_len,
305 1.1 christos len * sizeof(CHAR_T) + CHAR_T_OFFSET);
306 1.1 christos sp->wp->l_lp[0] = action;
307 1.1 christos memmove(sp->wp->l_lp + sizeof(u_char), &lno, sizeof(db_recno_t));
308 1.1 christos MEMMOVEW(sp->wp->l_lp + CHAR_T_OFFSET, lp, len);
309 1.1 christos
310 1.1 christos lcur = ep->l_cur;
311 1.1 christos memset(&key, 0, sizeof(key));
312 1.1 christos key.data = &lcur;
313 1.1 christos key.size = sizeof(db_recno_t);
314 1.1 christos memset(&data, 0, sizeof(data));
315 1.1 christos data.data = sp->wp->l_lp;
316 1.1 christos data.size = len * sizeof(CHAR_T) + CHAR_T_OFFSET;
317 1.1 christos if (ep->log->put(ep->log, NULL, &key, &data, 0) == -1)
318 1.1 christos LOG_ERR;
319 1.1 christos
320 1.1 christos #if defined(DEBUG) && 0
321 1.1 christos switch (action) {
322 1.1 christos case LOG_LINE_APPEND_F:
323 1.1 christos vtrace(sp, "%u: log_line: append_f: %lu {%u}\n",
324 1.1 christos ep->l_cur, lno, len);
325 1.1 christos break;
326 1.1 christos case LOG_LINE_APPEND_B:
327 1.1 christos vtrace(sp, "%u: log_line: append_b: %lu {%u}\n",
328 1.1 christos ep->l_cur, lno, len);
329 1.1 christos break;
330 1.1 christos case LOG_LINE_DELETE_F:
331 1.1 christos vtrace(sp, "%lu: log_line: delete_f: %lu {%u}\n",
332 1.1 christos ep->l_cur, lno, len);
333 1.1 christos break;
334 1.1 christos case LOG_LINE_DELETE_B:
335 1.1 christos vtrace(sp, "%lu: log_line: delete_b: %lu {%u}\n",
336 1.1 christos ep->l_cur, lno, len);
337 1.1 christos break;
338 1.1 christos case LOG_LINE_RESET_F:
339 1.1 christos vtrace(sp, "%lu: log_line: reset_f: %lu {%u}\n",
340 1.1 christos ep->l_cur, lno, len);
341 1.1 christos break;
342 1.1 christos case LOG_LINE_RESET_B:
343 1.1 christos vtrace(sp, "%lu: log_line: reset_b: %lu {%u}\n",
344 1.1 christos ep->l_cur, lno, len);
345 1.1 christos break;
346 1.1 christos }
347 1.1 christos #endif
348 1.1 christos /* Reset high water mark. */
349 1.1 christos ep->l_high = ++ep->l_cur;
350 1.1 christos
351 1.1 christos return (0);
352 1.1 christos }
353 1.1 christos
354 1.1 christos /*
355 1.1 christos * log_mark --
356 1.1 christos * Log a mark position. For the log to work, we assume that there
357 1.1 christos * aren't any operations that just put out a log record -- this
358 1.1 christos * would mean that undo operations would only reset marks, and not
359 1.1 christos * cause any other change.
360 1.1 christos *
361 1.1 christos * PUBLIC: int log_mark __P((SCR *, LMARK *));
362 1.1 christos */
363 1.1 christos int
364 1.1 christos log_mark(SCR *sp, LMARK *lmp)
365 1.1 christos {
366 1.1 christos DBT data, key;
367 1.1 christos EXF *ep;
368 1.1 christos
369 1.1 christos ep = sp->ep;
370 1.1 christos if (F_ISSET(ep, F_NOLOG))
371 1.1 christos return (0);
372 1.1 christos
373 1.1 christos /* Put out one initial cursor record per set of changes. */
374 1.1 christos if (ep->l_cursor.lno != OOBLNO) {
375 1.1 christos if (log_cursor1(sp, LOG_CURSOR_INIT))
376 1.1 christos return (1);
377 1.1 christos ep->l_cursor.lno = OOBLNO;
378 1.1 christos ep->l_win = sp->wp;
379 1.1 christos }
380 1.1 christos
381 1.1 christos BINC_RETC(sp, sp->wp->l_lp,
382 1.1 christos sp->wp->l_len, sizeof(u_char) + sizeof(LMARK));
383 1.1 christos sp->wp->l_lp[0] = LOG_MARK;
384 1.1 christos memmove(sp->wp->l_lp + sizeof(u_char), lmp, sizeof(LMARK));
385 1.1 christos
386 1.1 christos memset(&key, 0, sizeof(key));
387 1.1 christos key.data = &ep->l_cur;
388 1.1 christos key.size = sizeof(db_recno_t);
389 1.1 christos memset(&data, 0, sizeof(data));
390 1.1 christos data.data = sp->wp->l_lp;
391 1.1 christos data.size = sizeof(u_char) + sizeof(LMARK);
392 1.1 christos if (ep->log->put(ep->log, NULL, &key, &data, 0) == -1)
393 1.1 christos LOG_ERR;
394 1.1 christos
395 1.1 christos #if defined(DEBUG) && 0
396 1.1 christos vtrace(sp, "%lu: mark %c: %lu/%u\n",
397 1.1 christos ep->l_cur, lmp->name, lmp->lno, lmp->cno);
398 1.1 christos #endif
399 1.1 christos /* Reset high water mark. */
400 1.1 christos ep->l_high = ++ep->l_cur;
401 1.1 christos return (0);
402 1.1 christos }
403 1.1 christos
404 1.1 christos /*
405 1.1 christos * vi_log_get --
406 1.1 christos * Get a line from the log in log buffer.
407 1.1 christos */
408 1.1 christos static int
409 1.1 christos vi_log_get(SCR *sp, db_recno_t *lnop, size_t *size)
410 1.1 christos {
411 1.1 christos DBT key, data;
412 1.1 christos size_t nlen;
413 1.1 christos EXF *ep;
414 1.1 christos
415 1.1 christos ep = sp->ep;
416 1.1 christos
417 1.1 christos nlen = 1024;
418 1.1 christos retry:
419 1.1 christos BINC_RETC(sp, sp->wp->l_lp, sp->wp->l_len, nlen);
420 1.1 christos
421 1.1 christos memset(&key, 0, sizeof(key));
422 1.1 christos key.data = lnop; /* Initialize db request. */
423 1.1 christos key.size = sizeof(db_recno_t);
424 1.1 christos memset(&data, 0, sizeof(data));
425 1.1 christos data.data = sp->wp->l_lp;
426 1.1 christos data.ulen = sp->wp->l_len;
427 1.1 christos data.flags = DB_DBT_USERMEM;
428 1.1 christos switch (ep->log->get(ep->log, NULL, &key, &data, 0)) {
429 1.1 christos case ENOMEM:
430 1.1 christos nlen = data.size;
431 1.1 christos goto retry;
432 1.1 christos case 0:
433 1.1 christos *size = data.size;
434 1.1 christos return 0;
435 1.1 christos default:
436 1.1 christos return 1;
437 1.1 christos }
438 1.1 christos }
439 1.1 christos
440 1.1 christos /*
441 1.1 christos * Log_backward --
442 1.1 christos * Roll the log backward one operation.
443 1.1 christos *
444 1.1 christos * PUBLIC: int log_backward __P((SCR *, MARK *));
445 1.1 christos */
446 1.1 christos int
447 1.1 christos log_backward(SCR *sp, MARK *rp)
448 1.1 christos {
449 1.1 christos EXF *ep;
450 1.1 christos LMARK lm;
451 1.1 christos MARK m;
452 1.1 christos db_recno_t lno;
453 1.1 christos int didop;
454 1.1 christos u_char *p;
455 1.1 christos size_t size;
456 1.1 christos
457 1.1 christos ep = sp->ep;
458 1.1 christos if (F_ISSET(ep, F_NOLOG)) {
459 1.1 christos msgq(sp, M_ERR,
460 1.1 christos "010|Logging not being performed, undo not possible");
461 1.1 christos return (1);
462 1.1 christos }
463 1.1 christos
464 1.1 christos if (ep->l_cur == 1) {
465 1.1 christos msgq(sp, M_BERR, "011|No changes to undo");
466 1.1 christos return (1);
467 1.1 christos }
468 1.1 christos
469 1.1 christos if (ep->l_win && ep->l_win != sp->wp) {
470 1.1 christos ex_emsg(sp, NULL, EXM_LOCKED);
471 1.1 christos return 1;
472 1.1 christos }
473 1.1 christos ep->l_win = sp->wp;
474 1.1 christos
475 1.1 christos
476 1.1 christos F_SET(ep, F_NOLOG); /* Turn off logging. */
477 1.1 christos
478 1.1 christos for (didop = 0;;) {
479 1.1 christos --ep->l_cur;
480 1.1 christos if (vi_log_get(sp, &ep->l_cur, &size))
481 1.1 christos LOG_ERR;
482 1.1 christos #if defined(DEBUG) && 0
483 1.1 christos log_trace(sp, "log_backward", ep->l_cur, data.data);
484 1.1 christos #endif
485 1.1 christos switch (*(p = (u_char *)sp->wp->l_lp)) {
486 1.1 christos case LOG_CURSOR_INIT:
487 1.1 christos if (didop) {
488 1.1 christos memmove(rp, p + sizeof(u_char), sizeof(MARK));
489 1.1 christos F_CLR(ep, F_NOLOG);
490 1.1 christos ep->l_win = NULL;
491 1.1 christos return (0);
492 1.1 christos }
493 1.1 christos break;
494 1.1 christos case LOG_CURSOR_END:
495 1.1 christos break;
496 1.1 christos case LOG_LINE_APPEND_F:
497 1.1 christos didop = 1;
498 1.1 christos memmove(&lno, p + sizeof(u_char), sizeof(db_recno_t));
499 1.1 christos if (db_delete(sp, lno))
500 1.1 christos goto err;
501 1.1 christos ++sp->rptlines[L_DELETED];
502 1.1 christos break;
503 1.1 christos case LOG_LINE_DELETE_B:
504 1.1 christos didop = 1;
505 1.1 christos memmove(&lno, p + sizeof(u_char), sizeof(db_recno_t));
506 1.1 christos if (db_insert(sp, lno,
507 1.1 christos (CHAR_T *)(p + CHAR_T_OFFSET),
508 1.1 christos (size - CHAR_T_OFFSET) / sizeof(CHAR_T)))
509 1.1 christos goto err;
510 1.1 christos ++sp->rptlines[L_ADDED];
511 1.1 christos break;
512 1.1 christos case LOG_LINE_RESET_F:
513 1.1 christos break;
514 1.1 christos case LOG_LINE_RESET_B:
515 1.1 christos didop = 1;
516 1.1 christos memmove(&lno, p + sizeof(u_char), sizeof(db_recno_t));
517 1.1 christos if (db_set(sp, lno,
518 1.1 christos (CHAR_T *)(p + CHAR_T_OFFSET),
519 1.1 christos (size - CHAR_T_OFFSET) / sizeof(CHAR_T)))
520 1.1 christos goto err;
521 1.1 christos if (sp->rptlchange != lno) {
522 1.1 christos sp->rptlchange = lno;
523 1.1 christos ++sp->rptlines[L_CHANGED];
524 1.1 christos }
525 1.1 christos break;
526 1.1 christos case LOG_MARK:
527 1.1 christos didop = 1;
528 1.1 christos memmove(&lm, p + sizeof(u_char), sizeof(LMARK));
529 1.1 christos m.lno = lm.lno;
530 1.1 christos m.cno = lm.cno;
531 1.1 christos if (mark_set(sp, lm.name, &m, 0))
532 1.1 christos goto err;
533 1.1 christos break;
534 1.1 christos default:
535 1.1 christos abort();
536 1.1 christos }
537 1.1 christos }
538 1.1 christos
539 1.1 christos err: F_CLR(ep, F_NOLOG);
540 1.1 christos ep->l_win = NULL;
541 1.1 christos return (1);
542 1.1 christos }
543 1.1 christos
544 1.1 christos /*
545 1.1 christos * Log_setline --
546 1.1 christos * Reset the line to its original appearance.
547 1.1 christos *
548 1.1 christos * XXX
549 1.1 christos * There's a bug in this code due to our not logging cursor movements
550 1.1 christos * unless a change was made. If you do a change, move off the line,
551 1.1 christos * then move back on and do a 'U', the line will be restored to the way
552 1.1 christos * it was before the original change.
553 1.1 christos *
554 1.1 christos * PUBLIC: int log_setline __P((SCR *));
555 1.1 christos */
556 1.1 christos int
557 1.1 christos log_setline(SCR *sp)
558 1.1 christos {
559 1.1 christos EXF *ep;
560 1.1 christos LMARK lm;
561 1.1 christos MARK m;
562 1.1 christos db_recno_t lno;
563 1.1 christos u_char *p;
564 1.1 christos size_t size;
565 1.1 christos
566 1.1 christos ep = sp->ep;
567 1.1 christos if (F_ISSET(ep, F_NOLOG)) {
568 1.1 christos msgq(sp, M_ERR,
569 1.1 christos "012|Logging not being performed, undo not possible");
570 1.1 christos return (1);
571 1.1 christos }
572 1.1 christos
573 1.1 christos if (ep->l_cur == 1)
574 1.1 christos return (1);
575 1.1 christos
576 1.1 christos if (ep->l_win && ep->l_win != sp->wp) {
577 1.1 christos ex_emsg(sp, NULL, EXM_LOCKED);
578 1.1 christos return 1;
579 1.1 christos }
580 1.1 christos ep->l_win = sp->wp;
581 1.1 christos
582 1.1 christos F_SET(ep, F_NOLOG); /* Turn off logging. */
583 1.1 christos
584 1.1 christos for (;;) {
585 1.1 christos --ep->l_cur;
586 1.1 christos if (vi_log_get(sp, &ep->l_cur, &size))
587 1.1 christos LOG_ERR;
588 1.1 christos #if defined(DEBUG) && 0
589 1.1 christos log_trace(sp, "log_setline", ep->l_cur, data.data);
590 1.1 christos #endif
591 1.1 christos switch (*(p = (u_char *)sp->wp->l_lp)) {
592 1.1 christos case LOG_CURSOR_INIT:
593 1.1 christos memmove(&m, p + sizeof(u_char), sizeof(MARK));
594 1.1 christos if (m.lno != sp->lno || ep->l_cur == 1) {
595 1.1 christos F_CLR(ep, F_NOLOG);
596 1.1 christos ep->l_win = NULL;
597 1.1 christos return (0);
598 1.1 christos }
599 1.1 christos break;
600 1.1 christos case LOG_CURSOR_END:
601 1.1 christos memmove(&m, p + sizeof(u_char), sizeof(MARK));
602 1.1 christos if (m.lno != sp->lno) {
603 1.1 christos ++ep->l_cur;
604 1.1 christos F_CLR(ep, F_NOLOG);
605 1.1 christos ep->l_win = NULL;
606 1.1 christos return (0);
607 1.1 christos }
608 1.1 christos break;
609 1.1 christos case LOG_LINE_APPEND_F:
610 1.1 christos case LOG_LINE_DELETE_B:
611 1.1 christos case LOG_LINE_RESET_F:
612 1.1 christos break;
613 1.1 christos case LOG_LINE_RESET_B:
614 1.1 christos memmove(&lno, p + sizeof(u_char), sizeof(db_recno_t));
615 1.1 christos if (lno == sp->lno &&
616 1.1 christos db_set(sp, lno, (CHAR_T *)(p + CHAR_T_OFFSET),
617 1.1 christos (size - CHAR_T_OFFSET) / sizeof(CHAR_T)))
618 1.1 christos goto err;
619 1.1 christos if (sp->rptlchange != lno) {
620 1.1 christos sp->rptlchange = lno;
621 1.1 christos ++sp->rptlines[L_CHANGED];
622 1.1 christos }
623 1.1 christos case LOG_MARK:
624 1.1 christos memmove(&lm, p + sizeof(u_char), sizeof(LMARK));
625 1.1 christos m.lno = lm.lno;
626 1.1 christos m.cno = lm.cno;
627 1.1 christos if (mark_set(sp, lm.name, &m, 0))
628 1.1 christos goto err;
629 1.1 christos break;
630 1.1 christos default:
631 1.1 christos abort();
632 1.1 christos }
633 1.1 christos }
634 1.1 christos
635 1.1 christos err: F_CLR(ep, F_NOLOG);
636 1.1 christos ep->l_win = NULL;
637 1.1 christos return (1);
638 1.1 christos }
639 1.1 christos
640 1.1 christos /*
641 1.1 christos * Log_forward --
642 1.1 christos * Roll the log forward one operation.
643 1.1 christos *
644 1.1 christos * PUBLIC: int log_forward __P((SCR *, MARK *));
645 1.1 christos */
646 1.1 christos int
647 1.1 christos log_forward(SCR *sp, MARK *rp)
648 1.1 christos {
649 1.1 christos EXF *ep;
650 1.1 christos LMARK lm;
651 1.1 christos MARK m;
652 1.1 christos db_recno_t lno;
653 1.1 christos int didop;
654 1.1 christos u_char *p;
655 1.1 christos size_t size;
656 1.1 christos
657 1.1 christos ep = sp->ep;
658 1.1 christos if (F_ISSET(ep, F_NOLOG)) {
659 1.1 christos msgq(sp, M_ERR,
660 1.1 christos "013|Logging not being performed, roll-forward not possible");
661 1.1 christos return (1);
662 1.1 christos }
663 1.1 christos
664 1.1 christos if (ep->l_cur == ep->l_high) {
665 1.1 christos msgq(sp, M_BERR, "014|No changes to re-do");
666 1.1 christos return (1);
667 1.1 christos }
668 1.1 christos
669 1.1 christos if (ep->l_win && ep->l_win != sp->wp) {
670 1.1 christos ex_emsg(sp, NULL, EXM_LOCKED);
671 1.1 christos return 1;
672 1.1 christos }
673 1.1 christos ep->l_win = sp->wp;
674 1.1 christos
675 1.1 christos F_SET(ep, F_NOLOG); /* Turn off logging. */
676 1.1 christos
677 1.1 christos for (didop = 0;;) {
678 1.1 christos ++ep->l_cur;
679 1.1 christos if (vi_log_get(sp, &ep->l_cur, &size))
680 1.1 christos LOG_ERR;
681 1.1 christos #if defined(DEBUG) && 0
682 1.1 christos log_trace(sp, "log_forward", ep->l_cur, data.data);
683 1.1 christos #endif
684 1.1 christos switch (*(p = (u_char *)sp->wp->l_lp)) {
685 1.1 christos case LOG_CURSOR_END:
686 1.1 christos if (didop) {
687 1.1 christos ++ep->l_cur;
688 1.1 christos memmove(rp, p + sizeof(u_char), sizeof(MARK));
689 1.1 christos F_CLR(ep, F_NOLOG);
690 1.1 christos ep->l_win = NULL;
691 1.1 christos return (0);
692 1.1 christos }
693 1.1 christos break;
694 1.1 christos case LOG_CURSOR_INIT:
695 1.1 christos break;
696 1.1 christos case LOG_LINE_APPEND_F:
697 1.1 christos didop = 1;
698 1.1 christos memmove(&lno, p + sizeof(u_char), sizeof(db_recno_t));
699 1.1 christos if (db_insert(sp, lno,
700 1.1 christos (CHAR_T *)(p + CHAR_T_OFFSET),
701 1.1 christos (size - CHAR_T_OFFSET) / sizeof(CHAR_T)))
702 1.1 christos goto err;
703 1.1 christos ++sp->rptlines[L_ADDED];
704 1.1 christos break;
705 1.1 christos case LOG_LINE_DELETE_B:
706 1.1 christos didop = 1;
707 1.1 christos memmove(&lno, p + sizeof(u_char), sizeof(db_recno_t));
708 1.1 christos if (db_delete(sp, lno))
709 1.1 christos goto err;
710 1.1 christos ++sp->rptlines[L_DELETED];
711 1.1 christos break;
712 1.1 christos case LOG_LINE_RESET_B:
713 1.1 christos break;
714 1.1 christos case LOG_LINE_RESET_F:
715 1.1 christos didop = 1;
716 1.1 christos memmove(&lno, p + sizeof(u_char), sizeof(db_recno_t));
717 1.1 christos if (db_set(sp, lno,
718 1.1 christos (CHAR_T *)(p + CHAR_T_OFFSET),
719 1.1 christos (size - CHAR_T_OFFSET) / sizeof(CHAR_T)))
720 1.1 christos goto err;
721 1.1 christos if (sp->rptlchange != lno) {
722 1.1 christos sp->rptlchange = lno;
723 1.1 christos ++sp->rptlines[L_CHANGED];
724 1.1 christos }
725 1.1 christos break;
726 1.1 christos case LOG_MARK:
727 1.1 christos didop = 1;
728 1.1 christos memmove(&lm, p + sizeof(u_char), sizeof(LMARK));
729 1.1 christos m.lno = lm.lno;
730 1.1 christos m.cno = lm.cno;
731 1.1 christos if (mark_set(sp, lm.name, &m, 0))
732 1.1 christos goto err;
733 1.1 christos break;
734 1.1 christos default:
735 1.1 christos abort();
736 1.1 christos }
737 1.1 christos }
738 1.1 christos
739 1.1 christos err: F_CLR(ep, F_NOLOG);
740 1.1 christos ep->l_win = NULL;
741 1.1 christos return (1);
742 1.1 christos }
743 1.1 christos
744 1.1 christos /*
745 1.1 christos * log_err --
746 1.1 christos * Try and restart the log on failure, i.e. if we run out of memory.
747 1.1 christos */
748 1.1 christos static void
749 1.2 christos log_err(SCR *sp, const char *file, int line)
750 1.1 christos {
751 1.1 christos EXF *ep;
752 1.1 christos
753 1.1 christos msgq(sp, M_SYSERR, "015|%s/%d: log put error", tail(file), line);
754 1.1 christos ep = sp->ep;
755 1.1 christos (void)ep->log->close(ep->log, DB_NOSYNC);
756 1.1 christos if (!log_init(sp, ep))
757 1.1 christos msgq(sp, M_ERR, "267|Log restarted");
758 1.1 christos }
759 1.1 christos
760 1.1 christos #if defined(DEBUG) && 0
761 1.1 christos static void
762 1.2 christos log_trace(SCR *sp, const char *msg, db_recno_t rno, u_char *p)
763 1.1 christos {
764 1.1 christos LMARK lm;
765 1.1 christos MARK m;
766 1.1 christos db_recno_t lno;
767 1.1 christos
768 1.1 christos switch (*p) {
769 1.1 christos case LOG_CURSOR_INIT:
770 1.1 christos memmove(&m, p + sizeof(u_char), sizeof(MARK));
771 1.1 christos vtrace(sp, "%lu: %s: C_INIT: %u/%u\n", rno, msg, m.lno, m.cno);
772 1.1 christos break;
773 1.1 christos case LOG_CURSOR_END:
774 1.1 christos memmove(&m, p + sizeof(u_char), sizeof(MARK));
775 1.1 christos vtrace(sp, "%lu: %s: C_END: %u/%u\n", rno, msg, m.lno, m.cno);
776 1.1 christos break;
777 1.1 christos case LOG_LINE_APPEND_F:
778 1.1 christos memmove(&lno, p + sizeof(u_char), sizeof(db_recno_t));
779 1.1 christos vtrace(sp, "%lu: %s: APPEND_F: %lu\n", rno, msg, lno);
780 1.1 christos break;
781 1.1 christos case LOG_LINE_APPEND_B:
782 1.1 christos memmove(&lno, p + sizeof(u_char), sizeof(db_recno_t));
783 1.1 christos vtrace(sp, "%lu: %s: APPEND_B: %lu\n", rno, msg, lno);
784 1.1 christos break;
785 1.1 christos case LOG_LINE_DELETE_F:
786 1.1 christos memmove(&lno, p + sizeof(u_char), sizeof(db_recno_t));
787 1.1 christos vtrace(sp, "%lu: %s: DELETE_F: %lu\n", rno, msg, lno);
788 1.1 christos break;
789 1.1 christos case LOG_LINE_DELETE_B:
790 1.1 christos memmove(&lno, p + sizeof(u_char), sizeof(db_recno_t));
791 1.1 christos vtrace(sp, "%lu: %s: DELETE_B: %lu\n", rno, msg, lno);
792 1.1 christos break;
793 1.1 christos case LOG_LINE_RESET_F:
794 1.1 christos memmove(&lno, p + sizeof(u_char), sizeof(db_recno_t));
795 1.1 christos vtrace(sp, "%lu: %s: RESET_F: %lu\n", rno, msg, lno);
796 1.1 christos break;
797 1.1 christos case LOG_LINE_RESET_B:
798 1.1 christos memmove(&lno, p + sizeof(u_char), sizeof(db_recno_t));
799 1.1 christos vtrace(sp, "%lu: %s: RESET_B: %lu\n", rno, msg, lno);
800 1.1 christos break;
801 1.1 christos case LOG_MARK:
802 1.1 christos memmove(&lm, p + sizeof(u_char), sizeof(LMARK));
803 1.1 christos vtrace(sp,
804 1.1 christos "%lu: %s: MARK: %u/%u\n", rno, msg, lm.lno, lm.cno);
805 1.1 christos break;
806 1.1 christos default:
807 1.1 christos abort();
808 1.1 christos }
809 1.1 christos }
810 1.1 christos #endif
811