itime.c revision 1.4 1 1.4 lukem /* $NetBSD: itime.c,v 1.4 1997/04/15 01:09:50 lukem Exp $ */
2 1.3 cgd
3 1.1 cgd /*-
4 1.2 mycroft * Copyright (c) 1980, 1993
5 1.2 mycroft * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.1 cgd * 3. All advertising materials mentioning features or use of this software
16 1.1 cgd * must display the following acknowledgement:
17 1.1 cgd * This product includes software developed by the University of
18 1.1 cgd * California, Berkeley and its contributors.
19 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
20 1.1 cgd * may be used to endorse or promote products derived from this software
21 1.1 cgd * without specific prior written permission.
22 1.1 cgd *
23 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.1 cgd * SUCH DAMAGE.
34 1.1 cgd */
35 1.1 cgd
36 1.1 cgd #ifndef lint
37 1.3 cgd #if 0
38 1.3 cgd static char sccsid[] = "@(#)itime.c 8.1 (Berkeley) 6/5/93";
39 1.3 cgd #else
40 1.4 lukem static char rcsid[] = "$NetBSD: itime.c,v 1.4 1997/04/15 01:09:50 lukem Exp $";
41 1.3 cgd #endif
42 1.1 cgd #endif /* not lint */
43 1.1 cgd
44 1.2 mycroft #include <sys/param.h>
45 1.2 mycroft #include <sys/time.h>
46 1.1 cgd #ifdef sunos
47 1.2 mycroft #include <sys/vnode.h>
48 1.2 mycroft
49 1.2 mycroft #include <ufs/fsdir.h>
50 1.2 mycroft #include <ufs/inode.h>
51 1.1 cgd #include <ufs/fs.h>
52 1.1 cgd #else
53 1.2 mycroft #include <ufs/ufs/dinode.h>
54 1.1 cgd #endif
55 1.2 mycroft
56 1.1 cgd #include <protocols/dumprestore.h>
57 1.2 mycroft
58 1.1 cgd #include <errno.h>
59 1.2 mycroft #include <fcntl.h>
60 1.1 cgd #include <stdio.h>
61 1.1 cgd #ifdef __STDC__
62 1.1 cgd #include <stdlib.h>
63 1.1 cgd #include <string.h>
64 1.2 mycroft #include <unistd.h>
65 1.1 cgd #endif
66 1.2 mycroft
67 1.1 cgd #include "dump.h"
68 1.1 cgd
69 1.1 cgd struct dumpdates **ddatev = 0;
70 1.1 cgd int nddates = 0;
71 1.1 cgd int ddates_in = 0;
72 1.1 cgd struct dumptime *dthead = 0;
73 1.1 cgd
74 1.2 mycroft static void dumprecout __P((FILE *, struct dumpdates *));
75 1.2 mycroft static int getrecord __P((FILE *, struct dumpdates *));
76 1.2 mycroft static int makedumpdate __P((struct dumpdates *, char *));
77 1.2 mycroft static void readdumptimes __P((FILE *));
78 1.1 cgd
79 1.1 cgd void
80 1.1 cgd initdumptimes()
81 1.1 cgd {
82 1.1 cgd FILE *df;
83 1.1 cgd
84 1.1 cgd if ((df = fopen(dumpdates, "r")) == NULL) {
85 1.1 cgd if (errno != ENOENT) {
86 1.1 cgd quit("cannot read %s: %s\n", dumpdates,
87 1.1 cgd strerror(errno));
88 1.1 cgd /* NOTREACHED */
89 1.1 cgd }
90 1.1 cgd /*
91 1.1 cgd * Dumpdates does not exist, make an empty one.
92 1.1 cgd */
93 1.1 cgd msg("WARNING: no file `%s', making an empty one\n", dumpdates);
94 1.1 cgd if ((df = fopen(dumpdates, "w")) == NULL) {
95 1.1 cgd quit("cannot create %s: %s\n", dumpdates,
96 1.1 cgd strerror(errno));
97 1.1 cgd /* NOTREACHED */
98 1.1 cgd }
99 1.1 cgd (void) fclose(df);
100 1.1 cgd if ((df = fopen(dumpdates, "r")) == NULL) {
101 1.1 cgd quit("cannot read %s even after creating it: %s\n",
102 1.1 cgd dumpdates, strerror(errno));
103 1.1 cgd /* NOTREACHED */
104 1.1 cgd }
105 1.1 cgd }
106 1.1 cgd (void) flock(fileno(df), LOCK_SH);
107 1.1 cgd readdumptimes(df);
108 1.1 cgd (void) fclose(df);
109 1.1 cgd }
110 1.1 cgd
111 1.2 mycroft static void
112 1.1 cgd readdumptimes(df)
113 1.1 cgd FILE *df;
114 1.1 cgd {
115 1.4 lukem int i;
116 1.4 lukem struct dumptime *dtwalk;
117 1.1 cgd
118 1.1 cgd for (;;) {
119 1.1 cgd dtwalk = (struct dumptime *)calloc(1, sizeof (struct dumptime));
120 1.1 cgd if (getrecord(df, &(dtwalk->dt_value)) < 0)
121 1.1 cgd break;
122 1.1 cgd nddates++;
123 1.1 cgd dtwalk->dt_next = dthead;
124 1.1 cgd dthead = dtwalk;
125 1.1 cgd }
126 1.1 cgd
127 1.1 cgd ddates_in = 1;
128 1.1 cgd /*
129 1.1 cgd * arrayify the list, leaving enough room for the additional
130 1.1 cgd * record that we may have to add to the ddate structure
131 1.1 cgd */
132 1.1 cgd ddatev = (struct dumpdates **)
133 1.1 cgd calloc((unsigned) (nddates + 1), sizeof (struct dumpdates *));
134 1.1 cgd dtwalk = dthead;
135 1.1 cgd for (i = nddates - 1; i >= 0; i--, dtwalk = dtwalk->dt_next)
136 1.1 cgd ddatev[i] = &dtwalk->dt_value;
137 1.1 cgd }
138 1.1 cgd
139 1.1 cgd void
140 1.1 cgd getdumptime()
141 1.1 cgd {
142 1.4 lukem struct dumpdates *ddp;
143 1.4 lukem int i;
144 1.1 cgd char *fname;
145 1.1 cgd
146 1.1 cgd fname = disk;
147 1.1 cgd #ifdef FDEBUG
148 1.1 cgd msg("Looking for name %s in dumpdates = %s for level = %c\n",
149 1.1 cgd fname, dumpdates, level);
150 1.1 cgd #endif
151 1.1 cgd spcl.c_ddate = 0;
152 1.1 cgd lastlevel = '0';
153 1.1 cgd
154 1.1 cgd initdumptimes();
155 1.1 cgd /*
156 1.1 cgd * Go find the entry with the same name for a lower increment
157 1.1 cgd * and older date
158 1.1 cgd */
159 1.1 cgd ITITERATE(i, ddp) {
160 1.1 cgd if (strncmp(fname, ddp->dd_name, sizeof (ddp->dd_name)) != 0)
161 1.1 cgd continue;
162 1.1 cgd if (ddp->dd_level >= level)
163 1.1 cgd continue;
164 1.1 cgd if (ddp->dd_ddate <= spcl.c_ddate)
165 1.1 cgd continue;
166 1.1 cgd spcl.c_ddate = ddp->dd_ddate;
167 1.1 cgd lastlevel = ddp->dd_level;
168 1.1 cgd }
169 1.1 cgd }
170 1.1 cgd
171 1.1 cgd void
172 1.1 cgd putdumptime()
173 1.1 cgd {
174 1.1 cgd FILE *df;
175 1.4 lukem struct dumpdates *dtwalk;
176 1.4 lukem int i;
177 1.1 cgd int fd;
178 1.1 cgd char *fname;
179 1.1 cgd
180 1.1 cgd if(uflag == 0)
181 1.1 cgd return;
182 1.1 cgd if ((df = fopen(dumpdates, "r+")) == NULL)
183 1.1 cgd quit("cannot rewrite %s: %s\n", dumpdates, strerror(errno));
184 1.1 cgd fd = fileno(df);
185 1.1 cgd (void) flock(fd, LOCK_EX);
186 1.1 cgd fname = disk;
187 1.1 cgd free((char *)ddatev);
188 1.1 cgd ddatev = 0;
189 1.1 cgd nddates = 0;
190 1.1 cgd dthead = 0;
191 1.1 cgd ddates_in = 0;
192 1.1 cgd readdumptimes(df);
193 1.1 cgd if (fseek(df, 0L, 0) < 0)
194 1.1 cgd quit("fseek: %s\n", strerror(errno));
195 1.1 cgd spcl.c_ddate = 0;
196 1.1 cgd ITITERATE(i, dtwalk) {
197 1.1 cgd if (strncmp(fname, dtwalk->dd_name,
198 1.1 cgd sizeof (dtwalk->dd_name)) != 0)
199 1.1 cgd continue;
200 1.1 cgd if (dtwalk->dd_level != level)
201 1.1 cgd continue;
202 1.1 cgd goto found;
203 1.1 cgd }
204 1.1 cgd /*
205 1.1 cgd * construct the new upper bound;
206 1.1 cgd * Enough room has been allocated.
207 1.1 cgd */
208 1.1 cgd dtwalk = ddatev[nddates] =
209 1.1 cgd (struct dumpdates *)calloc(1, sizeof (struct dumpdates));
210 1.1 cgd nddates += 1;
211 1.1 cgd found:
212 1.1 cgd (void) strncpy(dtwalk->dd_name, fname, sizeof (dtwalk->dd_name));
213 1.1 cgd dtwalk->dd_level = level;
214 1.1 cgd dtwalk->dd_ddate = spcl.c_date;
215 1.1 cgd
216 1.1 cgd ITITERATE(i, dtwalk) {
217 1.1 cgd dumprecout(df, dtwalk);
218 1.1 cgd }
219 1.1 cgd if (fflush(df))
220 1.1 cgd quit("%s: %s\n", dumpdates, strerror(errno));
221 1.1 cgd if (ftruncate(fd, ftell(df)))
222 1.1 cgd quit("ftruncate (%s): %s\n", dumpdates, strerror(errno));
223 1.1 cgd (void) fclose(df);
224 1.1 cgd msg("level %c dump on %s", level,
225 1.1 cgd spcl.c_date == 0 ? "the epoch\n" : ctime(&spcl.c_date));
226 1.1 cgd }
227 1.1 cgd
228 1.1 cgd static void
229 1.1 cgd dumprecout(file, what)
230 1.1 cgd FILE *file;
231 1.1 cgd struct dumpdates *what;
232 1.1 cgd {
233 1.1 cgd
234 1.1 cgd if (fprintf(file, DUMPOUTFMT,
235 1.1 cgd what->dd_name,
236 1.1 cgd what->dd_level,
237 1.1 cgd ctime(&what->dd_ddate)) < 0)
238 1.1 cgd quit("%s: %s\n", dumpdates, strerror(errno));
239 1.1 cgd }
240 1.1 cgd
241 1.1 cgd int recno;
242 1.2 mycroft
243 1.2 mycroft static int
244 1.1 cgd getrecord(df, ddatep)
245 1.1 cgd FILE *df;
246 1.1 cgd struct dumpdates *ddatep;
247 1.1 cgd {
248 1.1 cgd char tbuf[BUFSIZ];
249 1.1 cgd
250 1.1 cgd recno = 0;
251 1.1 cgd if ( (fgets(tbuf, sizeof (tbuf), df)) != tbuf)
252 1.1 cgd return(-1);
253 1.1 cgd recno++;
254 1.1 cgd if (makedumpdate(ddatep, tbuf) < 0)
255 1.1 cgd msg("Unknown intermediate format in %s, line %d\n",
256 1.1 cgd dumpdates, recno);
257 1.1 cgd
258 1.1 cgd #ifdef FDEBUG
259 1.1 cgd msg("getrecord: %s %c %s", ddatep->dd_name, ddatep->dd_level,
260 1.1 cgd ddatep->dd_ddate == 0 ? "the epoch\n" : ctime(&ddatep->dd_ddate));
261 1.1 cgd #endif
262 1.1 cgd return(0);
263 1.1 cgd }
264 1.1 cgd
265 1.2 mycroft static int
266 1.1 cgd makedumpdate(ddp, tbuf)
267 1.1 cgd struct dumpdates *ddp;
268 1.1 cgd char *tbuf;
269 1.1 cgd {
270 1.2 mycroft char un_buf[128];
271 1.1 cgd
272 1.1 cgd (void) sscanf(tbuf, DUMPINFMT, ddp->dd_name, &ddp->dd_level, un_buf);
273 1.1 cgd ddp->dd_ddate = unctime(un_buf);
274 1.1 cgd if (ddp->dd_ddate < 0)
275 1.1 cgd return(-1);
276 1.1 cgd return(0);
277 1.1 cgd }
278