xref: /netbsd-src/sbin/dump/itime.c (revision ae9172d6cd9432a6a1a56760d86b32c57a66c39c)
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 /*static char sccsid[] = "from: @(#)itime.c	8.1 (Berkeley) 6/5/93";*/
36 static char *rcsid = "$Id: itime.c,v 1.2 1994/06/08 18:57:35 mycroft Exp $";
37 #endif /* not lint */
38 
39 #include <sys/param.h>
40 #include <sys/time.h>
41 #ifdef sunos
42 #include <sys/vnode.h>
43 
44 #include <ufs/fsdir.h>
45 #include <ufs/inode.h>
46 #include <ufs/fs.h>
47 #else
48 #include <ufs/ufs/dinode.h>
49 #endif
50 
51 #include <protocols/dumprestore.h>
52 
53 #include <errno.h>
54 #include <fcntl.h>
55 #include <stdio.h>
56 #ifdef __STDC__
57 #include <stdlib.h>
58 #include <string.h>
59 #include <unistd.h>
60 #endif
61 
62 #include "dump.h"
63 
64 struct	dumpdates **ddatev = 0;
65 int	nddates = 0;
66 int	ddates_in = 0;
67 struct	dumptime *dthead = 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 			quit("cannot read %s: %s\n", dumpdates,
82 			    strerror(errno));
83 			/* NOTREACHED */
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 			quit("cannot create %s: %s\n", dumpdates,
91 			    strerror(errno));
92 			/* NOTREACHED */
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 	register int i;
111 	register 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 		dtwalk->dt_next = dthead;
119 		dthead = dtwalk;
120 	}
121 
122 	ddates_in = 1;
123 	/*
124 	 *	arrayify the list, leaving enough room for the additional
125 	 *	record that we may have to add to the ddate structure
126 	 */
127 	ddatev = (struct dumpdates **)
128 		calloc((unsigned) (nddates + 1), sizeof (struct dumpdates *));
129 	dtwalk = dthead;
130 	for (i = nddates - 1; i >= 0; i--, dtwalk = dtwalk->dt_next)
131 		ddatev[i] = &dtwalk->dt_value;
132 }
133 
134 void
135 getdumptime()
136 {
137 	register struct dumpdates *ddp;
138 	register int i;
139 	char *fname;
140 
141 	fname = disk;
142 #ifdef FDEBUG
143 	msg("Looking for name %s in dumpdates = %s for level = %c\n",
144 		fname, dumpdates, level);
145 #endif
146 	spcl.c_ddate = 0;
147 	lastlevel = '0';
148 
149 	initdumptimes();
150 	/*
151 	 *	Go find the entry with the same name for a lower increment
152 	 *	and older date
153 	 */
154 	ITITERATE(i, ddp) {
155 		if (strncmp(fname, ddp->dd_name, sizeof (ddp->dd_name)) != 0)
156 			continue;
157 		if (ddp->dd_level >= level)
158 			continue;
159 		if (ddp->dd_ddate <= spcl.c_ddate)
160 			continue;
161 		spcl.c_ddate = ddp->dd_ddate;
162 		lastlevel = ddp->dd_level;
163 	}
164 }
165 
166 void
167 putdumptime()
168 {
169 	FILE *df;
170 	register struct dumpdates *dtwalk;
171 	register int i;
172 	int fd;
173 	char *fname;
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 	dthead = 0;
186 	ddates_in = 0;
187 	readdumptimes(df);
188 	if (fseek(df, 0L, 0) < 0)
189 		quit("fseek: %s\n", strerror(errno));
190 	spcl.c_ddate = 0;
191 	ITITERATE(i, dtwalk) {
192 		if (strncmp(fname, dtwalk->dd_name,
193 				sizeof (dtwalk->dd_name)) != 0)
194 			continue;
195 		if (dtwalk->dd_level != level)
196 			continue;
197 		goto found;
198 	}
199 	/*
200 	 *	construct the new upper bound;
201 	 *	Enough room has been allocated.
202 	 */
203 	dtwalk = ddatev[nddates] =
204 		(struct dumpdates *)calloc(1, sizeof (struct dumpdates));
205 	nddates += 1;
206   found:
207 	(void) strncpy(dtwalk->dd_name, fname, sizeof (dtwalk->dd_name));
208 	dtwalk->dd_level = level;
209 	dtwalk->dd_ddate = spcl.c_date;
210 
211 	ITITERATE(i, dtwalk) {
212 		dumprecout(df, dtwalk);
213 	}
214 	if (fflush(df))
215 		quit("%s: %s\n", dumpdates, strerror(errno));
216 	if (ftruncate(fd, ftell(df)))
217 		quit("ftruncate (%s): %s\n", dumpdates, strerror(errno));
218 	(void) fclose(df);
219 	msg("level %c dump on %s", level,
220 		spcl.c_date == 0 ? "the epoch\n" : ctime(&spcl.c_date));
221 }
222 
223 static void
224 dumprecout(file, what)
225 	FILE *file;
226 	struct dumpdates *what;
227 {
228 
229 	if (fprintf(file, DUMPOUTFMT,
230 		    what->dd_name,
231 		    what->dd_level,
232 		    ctime(&what->dd_ddate)) < 0)
233 		quit("%s: %s\n", dumpdates, strerror(errno));
234 }
235 
236 int	recno;
237 
238 static int
239 getrecord(df, ddatep)
240 	FILE *df;
241 	struct dumpdates *ddatep;
242 {
243 	char tbuf[BUFSIZ];
244 
245 	recno = 0;
246 	if ( (fgets(tbuf, sizeof (tbuf), df)) != tbuf)
247 		return(-1);
248 	recno++;
249 	if (makedumpdate(ddatep, tbuf) < 0)
250 		msg("Unknown intermediate format in %s, line %d\n",
251 			dumpdates, recno);
252 
253 #ifdef FDEBUG
254 	msg("getrecord: %s %c %s", ddatep->dd_name, ddatep->dd_level,
255 	    ddatep->dd_ddate == 0 ? "the epoch\n" : ctime(&ddatep->dd_ddate));
256 #endif
257 	return(0);
258 }
259 
260 static int
261 makedumpdate(ddp, tbuf)
262 	struct dumpdates *ddp;
263 	char *tbuf;
264 {
265 	char un_buf[128];
266 
267 	(void) sscanf(tbuf, DUMPINFMT, ddp->dd_name, &ddp->dd_level, un_buf);
268 	ddp->dd_ddate = unctime(un_buf);
269 	if (ddp->dd_ddate < 0)
270 		return(-1);
271 	return(0);
272 }
273