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