xref: /netbsd-src/sbin/dump/itime.c (revision 481fca6e59249d8ffcf24fef7cfbe7b131bfb080)
1 /*	$NetBSD: itime.c,v 1.8 1998/04/22 08:55:18 bouyer 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.8 1998/04/22 08:55:18 bouyer 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 #ifdef __STDC__
63 #include <stdlib.h>
64 #include <string.h>
65 #include <time.h>
66 #include <unistd.h>
67 #endif
68 
69 #include "dump.h"
70 
71 struct	dumpdates **ddatev = 0;
72 int	nddates = 0;
73 int	ddates_in = 0;
74 struct	dumptime *dthead = 0;
75 
76 static	void dumprecout __P((FILE *, struct dumpdates *));
77 static	int getrecord __P((FILE *, struct dumpdates *));
78 static	int makedumpdate __P((struct dumpdates *, char *));
79 static	void readdumptimes __P((FILE *));
80 
81 void
82 initdumptimes()
83 {
84 	FILE *df;
85 
86 	if ((df = fopen(dumpdates, "r")) == NULL) {
87 		if (errno != ENOENT) {
88 			quit("cannot read %s: %s\n", dumpdates,
89 			    strerror(errno));
90 			/* NOTREACHED */
91 		}
92 		/*
93 		 * Dumpdates does not exist, make an empty one.
94 		 */
95 		msg("WARNING: no file `%s', making an empty one\n", dumpdates);
96 		if ((df = fopen(dumpdates, "w")) == NULL) {
97 			quit("cannot create %s: %s\n", dumpdates,
98 			    strerror(errno));
99 			/* NOTREACHED */
100 		}
101 		(void) fclose(df);
102 		if ((df = fopen(dumpdates, "r")) == NULL) {
103 			quit("cannot read %s even after creating it: %s\n",
104 			    dumpdates, strerror(errno));
105 			/* NOTREACHED */
106 		}
107 	}
108 	(void) flock(fileno(df), LOCK_SH);
109 	readdumptimes(df);
110 	(void) fclose(df);
111 }
112 
113 static void
114 readdumptimes(df)
115 	FILE *df;
116 {
117 	int i;
118 	struct	dumptime *dtwalk;
119 
120 	for (;;) {
121 		dtwalk = (struct dumptime *)calloc(1, sizeof (struct dumptime));
122 		if (getrecord(df, &(dtwalk->dt_value)) < 0)
123 			break;
124 		nddates++;
125 		dtwalk->dt_next = dthead;
126 		dthead = dtwalk;
127 	}
128 
129 	ddates_in = 1;
130 	/*
131 	 *	arrayify the list, leaving enough room for the additional
132 	 *	record that we may have to add to the ddate structure
133 	 */
134 	ddatev = (struct dumpdates **)
135 		calloc((unsigned) (nddates + 1), sizeof (struct dumpdates *));
136 	dtwalk = dthead;
137 	for (i = nddates - 1; i >= 0; i--, dtwalk = dtwalk->dt_next)
138 		ddatev[i] = &dtwalk->dt_value;
139 }
140 
141 void
142 getdumptime()
143 {
144 	struct dumpdates *ddp;
145 	int i;
146 	char *fname;
147 
148 	fname = disk;
149 #ifdef FDEBUG
150 	msg("Looking for name %s in dumpdates = %s for level = %c\n",
151 		fname, dumpdates, level);
152 #endif
153 	spcl.c_ddate = 0;
154 	lastlevel = '0';
155 
156 	initdumptimes();
157 	/*
158 	 *	Go find the entry with the same name for a lower increment
159 	 *	and older date
160 	 */
161 	ITITERATE(i, ddp) {
162 		if (strncmp(fname, ddp->dd_name, sizeof (ddp->dd_name)) != 0)
163 			continue;
164 		if (ddp->dd_level >= level)
165 			continue;
166 		if (ddp->dd_ddate <= iswap32(spcl.c_ddate))
167 			continue;
168 		spcl.c_ddate = iswap32(ddp->dd_ddate);
169 		lastlevel = ddp->dd_level;
170 	}
171 }
172 
173 void
174 putdumptime()
175 {
176 	FILE *df;
177 	struct dumpdates *dtwalk, *dtfound;
178 	int i;
179 	int fd;
180 	char *fname;
181 
182 	if(uflag == 0)
183 		return;
184 	if ((df = fopen(dumpdates, "r+")) == NULL)
185 		quit("cannot rewrite %s: %s\n", dumpdates, strerror(errno));
186 	fd = fileno(df);
187 	(void) flock(fd, LOCK_EX);
188 	fname = disk;
189 	free((char *)ddatev);
190 	ddatev = 0;
191 	nddates = 0;
192 	dthead = 0;
193 	ddates_in = 0;
194 	readdumptimes(df);
195 	if (fseek(df, 0L, 0) < 0)
196 		quit("fseek: %s\n", strerror(errno));
197 	spcl.c_ddate = 0;
198 	ITITERATE(i, dtwalk) {
199 		if (strncmp(fname, dtwalk->dd_name,
200 				sizeof (dtwalk->dd_name)) != 0)
201 			continue;
202 		if (dtwalk->dd_level != level)
203 			continue;
204 		goto found;
205 	}
206 	/*
207 	 *	construct the new upper bound;
208 	 *	Enough room has been allocated.
209 	 */
210 	dtwalk = ddatev[nddates] =
211 		(struct dumpdates *)calloc(1, sizeof (struct dumpdates));
212 	nddates += 1;
213   found:
214 	(void) strncpy(dtwalk->dd_name, fname, sizeof (dtwalk->dd_name));
215 	dtwalk->dd_level = level;
216 	dtwalk->dd_ddate = iswap32(spcl.c_date);
217 	dtfound = dtwalk;
218 
219 	ITITERATE(i, dtwalk) {
220 		dumprecout(df, dtwalk);
221 	}
222 	if (fflush(df))
223 		quit("%s: %s\n", dumpdates, strerror(errno));
224 	if (ftruncate(fd, ftell(df)))
225 		quit("ftruncate (%s): %s\n", dumpdates, strerror(errno));
226 	(void) fclose(df);
227 	msg("level %c dump on %s", level,
228 		spcl.c_date == 0 ? "the epoch\n" : ctime(&dtfound->dd_ddate));
229 }
230 
231 static void
232 dumprecout(file, what)
233 	FILE *file;
234 	struct dumpdates *what;
235 {
236 
237 	if (fprintf(file, DUMPOUTFMT,
238 		    what->dd_name,
239 		    what->dd_level,
240 		    ctime(&what->dd_ddate)) < 0)
241 		quit("%s: %s\n", dumpdates, strerror(errno));
242 }
243 
244 int	recno;
245 
246 static int
247 getrecord(df, ddatep)
248 	FILE *df;
249 	struct dumpdates *ddatep;
250 {
251 	char tbuf[BUFSIZ];
252 
253 	recno = 0;
254 	if ( (fgets(tbuf, sizeof (tbuf), df)) != tbuf)
255 		return(-1);
256 	recno++;
257 	if (makedumpdate(ddatep, tbuf) < 0)
258 		msg("Unknown intermediate format in %s, line %d\n",
259 			dumpdates, recno);
260 
261 #ifdef FDEBUG
262 	msg("getrecord: %s %c %s", ddatep->dd_name, ddatep->dd_level,
263 	    ddatep->dd_ddate == 0 ? "the epoch\n" : ctime(&ddatep->dd_ddate));
264 #endif
265 	return(0);
266 }
267 
268 static int
269 makedumpdate(ddp, tbuf)
270 	struct dumpdates *ddp;
271 	char *tbuf;
272 {
273 	char un_buf[128];
274 
275 	(void) sscanf(tbuf, DUMPINFMT, ddp->dd_name, &ddp->dd_level, un_buf);
276 	ddp->dd_ddate = unctime(un_buf);
277 	if (ddp->dd_ddate < 0)
278 		return(-1);
279 	return(0);
280 }
281