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