xref: /freebsd/sbin/dump/itime.c (revision 89fdc4e1)
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 <limits.h>
53 #include <stdio.h>
54 #include <stdlib.h>
55 #include <string.h>
56 #include <timeconv.h>
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(FILE *, const struct dumpdates *);
70 static	int getrecord(FILE *, struct dumpdates *);
71 static	int makedumpdate(struct dumpdates *, const char *);
72 static	void readdumptimes(FILE *);
73 
74 void
75 initdumptimes(void)
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(FILE *df)
108 {
109 	int i;
110 	struct	dumptime *dtwalk;
111 
112 	for (;;) {
113 		dtwalk = (struct dumptime *)calloc(1, sizeof (struct dumptime));
114 		if (getrecord(df, &(dtwalk->dt_value)) < 0)
115 			break;
116 		nddates++;
117 		SLIST_INSERT_HEAD(&dthead, dtwalk, dt_list);
118 	}
119 
120 	ddates_in = 1;
121 	/*
122 	 *	arrayify the list, leaving enough room for the additional
123 	 *	record that we may have to add to the ddate structure
124 	 */
125 	ddatev = (struct dumpdates **)
126 		calloc((unsigned) (nddates + 1), sizeof (struct dumpdates *));
127 	dtwalk = SLIST_FIRST(&dthead);
128 	for (i = nddates - 1; i >= 0; i--, dtwalk = SLIST_NEXT(dtwalk, dt_list))
129 		ddatev[i] = &dtwalk->dt_value;
130 }
131 
132 void
133 getdumptime(void)
134 {
135 	struct dumpdates *ddp;
136 	int i;
137 	char *fname;
138 
139 	fname = disk;
140 #ifdef FDEBUG
141 	msg("Looking for name %s in dumpdates = %s for level = %c\n",
142 		fname, dumpdates, level);
143 #endif
144 	spcl.c_ddate = 0;
145 	lastlevel = '0';
146 
147 	initdumptimes();
148 	/*
149 	 *	Go find the entry with the same name for a lower increment
150 	 *	and older date
151 	 */
152 	ITITERATE(i, ddp) {
153 		if (strncmp(fname, ddp->dd_name, sizeof (ddp->dd_name)) != 0)
154 			continue;
155 		if (ddp->dd_level >= level)
156 			continue;
157 		if (ddp->dd_ddate <= _time64_to_time(spcl.c_ddate))
158 			continue;
159 		spcl.c_ddate = _time_to_time64(ddp->dd_ddate);
160 		lastlevel = ddp->dd_level;
161 	}
162 }
163 
164 void
165 putdumptime(void)
166 {
167 	FILE *df;
168 	struct dumpdates *dtwalk;
169 	int i;
170 	int fd;
171 	char *fname;
172 	char *tmsg;
173 
174 	if(uflag == 0)
175 		return;
176 	if ((df = fopen(dumpdates, "r+")) == NULL)
177 		quit("cannot rewrite %s: %s\n", dumpdates, strerror(errno));
178 	fd = fileno(df);
179 	(void) flock(fd, LOCK_EX);
180 	fname = disk;
181 	free((char *)ddatev);
182 	ddatev = 0;
183 	nddates = 0;
184 	ddates_in = 0;
185 	readdumptimes(df);
186 	if (fseek(df, 0L, 0) < 0)
187 		quit("fseek: %s\n", strerror(errno));
188 	spcl.c_ddate = 0;
189 	ITITERATE(i, dtwalk) {
190 		if (strncmp(fname, dtwalk->dd_name,
191 				sizeof (dtwalk->dd_name)) != 0)
192 			continue;
193 		if (dtwalk->dd_level != level)
194 			continue;
195 		goto found;
196 	}
197 	/*
198 	 *	construct the new upper bound;
199 	 *	Enough room has been allocated.
200 	 */
201 	dtwalk = ddatev[nddates] =
202 		(struct dumpdates *)calloc(1, sizeof (struct dumpdates));
203 	nddates += 1;
204   found:
205 	(void) strncpy(dtwalk->dd_name, fname, sizeof (dtwalk->dd_name));
206 	dtwalk->dd_level = level;
207 	dtwalk->dd_ddate = _time64_to_time(spcl.c_date);
208 
209 	ITITERATE(i, dtwalk) {
210 		dumprecout(df, dtwalk);
211 	}
212 	if (fflush(df))
213 		quit("%s: %s\n", dumpdates, strerror(errno));
214 	if (ftruncate(fd, ftell(df)))
215 		quit("ftruncate (%s): %s\n", dumpdates, strerror(errno));
216 	(void) fclose(df);
217 	if (spcl.c_date == 0) {
218 		tmsg = "the epoch\n";
219 	} else {
220 		time_t t = _time64_to_time(spcl.c_date);
221 		tmsg = ctime(&t);
222 	}
223 	msg("level %c dump on %s", level, tmsg);
224 }
225 
226 static void
227 dumprecout(FILE *file, const struct dumpdates *what)
228 {
229 
230 	if (fprintf(file, DUMPOUTFMT, what->dd_name,
231 	      what->dd_level, ctime(&what->dd_ddate)) < 0)
232 		quit("%s: %s\n", dumpdates, strerror(errno));
233 }
234 
235 int	recno;
236 
237 static int
238 getrecord(FILE *df, struct dumpdates *ddatep)
239 {
240 	char tbuf[BUFSIZ];
241 
242 	recno = 0;
243 	if ( (fgets(tbuf, sizeof (tbuf), df)) != tbuf)
244 		return(-1);
245 	recno++;
246 	if (makedumpdate(ddatep, tbuf) < 0)
247 		msg("Unknown intermediate format in %s, line %d\n",
248 			dumpdates, recno);
249 
250 #ifdef FDEBUG
251 	msg("getrecord: %s %c %s", ddatep->dd_name, ddatep->dd_level,
252 	    ddatep->dd_ddate == 0 ? "the epoch\n" : ctime(&ddatep->dd_ddate));
253 #endif
254 	return(0);
255 }
256 
257 static int
258 makedumpdate(struct dumpdates *ddp, const char *tbuf)
259 {
260 	char un_buf[128];
261 
262 	(void) sscanf(tbuf, DUMPINFMT, ddp->dd_name, &ddp->dd_level, un_buf);
263 	ddp->dd_ddate = unctime(un_buf);
264 	if (ddp->dd_ddate < 0)
265 		return(-1);
266 	return(0);
267 }
268