Debian Codegraph @

rec.c

raw
1 /**************************************************************************
2 * This program is Copyright (C) 1986-2002 by Jonathan Payne. JOVE is *
3 * provided by Jonathan and Jovehacks without charge and without *
4 * warranty. You may copy, modify, and/or distribute JOVE, provided that *
5 * this notice is included in all the source files and documentation. *
6 **************************************************************************/
7
8 #include "jove.h"
9
10 #ifdef RECOVER /* the body is the rest of this file */
11
12 #include "fp.h"
13 #include "rec.h"
14 #include "fmt.h"
15 #include "recover.h"
16
17 private int rec_fd = -1;
18 private char *recfname;
19 private File *rec_out;
20
21 #define dmpobj(obj) fputnchar((char *) &obj, (int) sizeof(obj), rec_out)
22
23 #ifndef L_SET
24 # define L_SET 0
25 #endif
26
27 private struct rec_head Header;
28
29 void
30 rectmpname(char *tfname)
31 {
32 if (strlen(tfname) >= sizeof(Header.TmpFileName)) {
33 complain("temporary filename too long; recovery disabled.");
34 /* NOTREACHED */
35 }
36 strcpy(Header.TmpFileName, tfname);
37 }
38
39 private void
40 recinit(void)
41 {
42 char buf[FILESIZE];
43
44 PathCat(buf, sizeof(buf), TmpDir,
45 #ifdef MAC
46 ".jrecXXX" /* must match string in mac.c:Ffilter() */
47 #else
48 "jrXXXXXX"
49 #endif
50 );
51 recfname = copystr(buf);
52 rec_fd = MakeTemp(recfname, "recovery (disabling it)");
53 /* initialize the recovery file */
54 rec_out = fd_open(recfname, F_WRITE|F_LOCKED, rec_fd, iobuff, LBSIZE);
55
56 /* Initialize the record header (TmpFileName initialized by rectmpname). */
57 Header.RecMagic = RECMAGIC;
58 Header.Uid = getuid();
59 Header.Pid = getpid();
60 }
61
62 /* Close recfile before execing a child process.
63 * Since we might be vforking, we must not change any variables
64 * (in particular rec_fd).
65 */
66 void
67 recclose(void)
68 {
69 if (rec_fd != -1)
70 (void) close(rec_fd);
71 }
72
73 /* Close and remove recfile before exiting. */
74
75
76 void
77 recremove(void)
78 {
79 if (rec_fd != -1) {
80 recclose();
81 (void) unlink(recfname);
82 }
83 }
84
85 /* Write out the line pointers for buffer B. */
86
87 private void
88 dmppntrs(Buffer *b)
89 {
90 LinePtr lp;
91
92 for (lp = b->b_first; lp != NULL; lp = lp->l_next)
93 dmpobj(lp->l_dline);
94 }
95
96 /* dump the buffer info and then the actual line pointers. */
97
98 private void
99 dmp_buf_header(Buffer *b)
100 {
101 struct rec_entry record;
102
103 byte_zero(&record, sizeof(struct rec_entry)); /* clean out holes for purify */
104 record.r_dotline = LinesTo(b->b_first, b->b_dot);
105 record.r_dotchar = b->b_char;
106 record.r_nlines = record.r_dotline + LinesTo(b->b_dot, (LinePtr)NULL);
107 strcpy(record.r_fname, b->b_fname ? b->b_fname : NullStr);
108 null_ncpy(record.r_bname, b->b_name, sizeof(record.r_bname) - 1);
109 dmpobj(record);
110 }
111
112 /* Goes through all the buffers and syncs them to the disk. */
113
114 int ModCount = 0; /* number of buffer mods since last sync */
115
116 int SyncFreq = 50; /* VAR: how often to sync the file pointers */
117
118 void
119 SyncRec(void)
120 {
121 Buffer *b;
122 static jbool beenhere = NO;
123 time_t tupd;
124
125 /* Count number of interesting buffers. If none, don't bother syncing. */
126 Header.Nbuffers = 0;
127 for (b = world; b != NULL; b = b->b_next)
128 if (b->b_type != B_SCRATCH && IsModified(b))
129 Header.Nbuffers += 1;
130 if (Header.Nbuffers == 0)
131 return;
132
133 lsave(); /* this makes things really right */
134 SyncTmp(); /* note: this will force rectmpname() */
135
136 if (!beenhere) {
137 beenhere = YES;
138 recinit(); /* Init recover file. */
139 }
140 /* Note: once writing to the recover file fails, we permanently
141 * stop trying. This is to avoid useless thrashing. Perhaps
142 * there should be a way to turn this back on.
143 */
144 if (rec_fd == -1 || (rec_out->f_flags & F_ERR))
145 return;
146
147 f_seek(rec_out, (off_t)0);
148 (void) time(&tupd);
149 Header.UpdTime = tupd;
150 Header.FreePtr = DFree;
151 dmpobj(Header);
152 for (b = world; b != NULL; b = b->b_next)
153 if (b->b_type != B_SCRATCH && IsModified(b))
154 dmp_buf_header(b);
155 for (b = world; b != NULL; b = b->b_next)
156 if (b->b_type != B_SCRATCH && IsModified(b))
157 dmppntrs(b);
158 flushout(rec_out);
159 }
160
161 /* To be implemented:
162 * Full Recover. What we have to do is go find the name of the tmp
163 * file data/rec pair and use those instead of the ones we would have
164 * created eventually. The rec file has a list of buffers, and then
165 * the actual pointers. Stored for each buffer is the buffer name,
166 * the file name, the number of lines, the current line, the current
167 * character. The current modes do not need saving as they will be
168 * saved when the file name is set. If a process was running in a
169 * buffer, it will be lost.
170 */
171
172 #endif /* RECOVER */