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