xref: /openbsd/sbin/dump/itime.c (revision 352e3770)
1*352e3770Sguenther /*	$OpenBSD: itime.c,v 1.20 2015/05/03 01:44:34 guenther Exp $	*/
22904a520Smillert /*	$NetBSD: itime.c,v 1.4 1997/04/15 01:09:50 lukem Exp $	*/
3df930be7Sderaadt 
4df930be7Sderaadt /*-
5df930be7Sderaadt  * Copyright (c) 1980, 1993
6df930be7Sderaadt  *	The Regents of the University of California.  All rights reserved.
7df930be7Sderaadt  *
8df930be7Sderaadt  * Redistribution and use in source and binary forms, with or without
9df930be7Sderaadt  * modification, are permitted provided that the following conditions
10df930be7Sderaadt  * are met:
11df930be7Sderaadt  * 1. Redistributions of source code must retain the above copyright
12df930be7Sderaadt  *    notice, this list of conditions and the following disclaimer.
13df930be7Sderaadt  * 2. Redistributions in binary form must reproduce the above copyright
14df930be7Sderaadt  *    notice, this list of conditions and the following disclaimer in the
15df930be7Sderaadt  *    documentation and/or other materials provided with the distribution.
161ef0d710Smillert  * 3. Neither the name of the University nor the names of its contributors
17df930be7Sderaadt  *    may be used to endorse or promote products derived from this software
18df930be7Sderaadt  *    without specific prior written permission.
19df930be7Sderaadt  *
20df930be7Sderaadt  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21df930be7Sderaadt  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22df930be7Sderaadt  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23df930be7Sderaadt  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24df930be7Sderaadt  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25df930be7Sderaadt  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26df930be7Sderaadt  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27df930be7Sderaadt  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28df930be7Sderaadt  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29df930be7Sderaadt  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30df930be7Sderaadt  * SUCH DAMAGE.
31df930be7Sderaadt  */
32df930be7Sderaadt 
33b9fc9a72Sderaadt #include <sys/param.h>	/* MAXBSIZE */
34df930be7Sderaadt #include <sys/time.h>
35df930be7Sderaadt #include <ufs/ufs/dinode.h>
36df930be7Sderaadt 
37df930be7Sderaadt #include <protocols/dumprestore.h>
38df930be7Sderaadt 
39df930be7Sderaadt #include <errno.h>
40df930be7Sderaadt #include <fcntl.h>
41df930be7Sderaadt #include <stdio.h>
4200e3001fSderaadt #include <time.h>
43df930be7Sderaadt #include <stdlib.h>
44df930be7Sderaadt #include <string.h>
45df930be7Sderaadt #include <unistd.h>
46b9fc9a72Sderaadt #include <limits.h>
47df930be7Sderaadt 
48df930be7Sderaadt #include "dump.h"
49df930be7Sderaadt 
50df930be7Sderaadt struct	dumpdates **ddatev = 0;
51df930be7Sderaadt int	nddates = 0;
52df930be7Sderaadt int	ddates_in = 0;
53df930be7Sderaadt struct	dumptime *dthead = 0;
54df930be7Sderaadt 
55c72b5b24Smillert static	void dumprecout(FILE *, struct dumpdates *);
56c72b5b24Smillert static	int getrecord(FILE *, struct dumpdates *);
57c72b5b24Smillert static	int makedumpdate(struct dumpdates *, char *);
58c72b5b24Smillert static	void readdumptimes(FILE *);
59df930be7Sderaadt 
60df930be7Sderaadt void
6153fedb49Sderaadt initdumptimes(void)
62df930be7Sderaadt {
63df930be7Sderaadt 	FILE *df;
64df930be7Sderaadt 
65df930be7Sderaadt 	if ((df = fopen(dumpdates, "r")) == NULL) {
66df930be7Sderaadt 		if (errno != ENOENT) {
67df930be7Sderaadt 			quit("cannot read %s: %s\n", dumpdates,
68df930be7Sderaadt 			    strerror(errno));
69df930be7Sderaadt 			/* NOTREACHED */
70df930be7Sderaadt 		}
71df930be7Sderaadt 		/*
72df930be7Sderaadt 		 * Dumpdates does not exist, make an empty one.
73df930be7Sderaadt 		 */
74df930be7Sderaadt 		msg("WARNING: no file `%s', making an empty one\n", dumpdates);
75df930be7Sderaadt 		if ((df = fopen(dumpdates, "w")) == NULL) {
76df930be7Sderaadt 			quit("cannot create %s: %s\n", dumpdates,
77df930be7Sderaadt 			    strerror(errno));
78df930be7Sderaadt 			/* NOTREACHED */
79df930be7Sderaadt 		}
80df930be7Sderaadt 		(void) fclose(df);
81df930be7Sderaadt 		if ((df = fopen(dumpdates, "r")) == NULL) {
82df930be7Sderaadt 			quit("cannot read %s even after creating it: %s\n",
83df930be7Sderaadt 			    dumpdates, strerror(errno));
84df930be7Sderaadt 			/* NOTREACHED */
85df930be7Sderaadt 		}
86df930be7Sderaadt 	}
87df930be7Sderaadt 	(void) flock(fileno(df), LOCK_SH);
88df930be7Sderaadt 	readdumptimes(df);
89df930be7Sderaadt 	(void) fclose(df);
90df930be7Sderaadt }
91df930be7Sderaadt 
92df930be7Sderaadt static void
9353fedb49Sderaadt readdumptimes(FILE *df)
94df930be7Sderaadt {
95e073c79dSmpech 	int i;
96e073c79dSmpech 	struct	dumptime *dtwalk;
97df930be7Sderaadt 
98df930be7Sderaadt 	for (;;) {
99df930be7Sderaadt 		dtwalk = (struct dumptime *)calloc(1, sizeof(struct dumptime));
1008b448031Sderaadt 		if (getrecord(df, &(dtwalk->dt_value)) < 0) {
1018b448031Sderaadt 			free(dtwalk);
102df930be7Sderaadt 			break;
1038b448031Sderaadt 		}
104df930be7Sderaadt 		nddates++;
105df930be7Sderaadt 		dtwalk->dt_next = dthead;
106df930be7Sderaadt 		dthead = dtwalk;
107df930be7Sderaadt 	}
108df930be7Sderaadt 
109df930be7Sderaadt 	ddates_in = 1;
110df930be7Sderaadt 	/*
111df930be7Sderaadt 	 *	arrayify the list, leaving enough room for the additional
112df930be7Sderaadt 	 *	record that we may have to add to the ddate structure
113df930be7Sderaadt 	 */
114df930be7Sderaadt 	ddatev = (struct dumpdates **)
115df930be7Sderaadt 		calloc((unsigned) (nddates + 1), sizeof(struct dumpdates *));
116df930be7Sderaadt 	dtwalk = dthead;
117df930be7Sderaadt 	for (i = nddates - 1; i >= 0; i--, dtwalk = dtwalk->dt_next)
118df930be7Sderaadt 		ddatev[i] = &dtwalk->dt_value;
119df930be7Sderaadt }
120df930be7Sderaadt 
121df930be7Sderaadt void
12253fedb49Sderaadt getdumptime(void)
123df930be7Sderaadt {
124e073c79dSmpech 	struct dumpdates *ddp;
125e073c79dSmpech 	int i;
126df930be7Sderaadt 	char *fname;
127df930be7Sderaadt 
128*352e3770Sguenther 	fname = duid ? duid : disk;
129df930be7Sderaadt #ifdef FDEBUG
130df930be7Sderaadt 	msg("Looking for name %s in dumpdates = %s for level = %c\n",
131df930be7Sderaadt 		fname, dumpdates, level);
132df930be7Sderaadt #endif
133df930be7Sderaadt 	spcl.c_ddate = 0;
134df930be7Sderaadt 	lastlevel = '0';
135df930be7Sderaadt 
136df930be7Sderaadt 	initdumptimes();
137df930be7Sderaadt 	/*
138df930be7Sderaadt 	 *	Go find the entry with the same name for a lower increment
139df930be7Sderaadt 	 *	and older date
140df930be7Sderaadt 	 */
141df930be7Sderaadt 	ITITERATE(i, ddp) {
142*352e3770Sguenther 		if ((strncmp(fname, ddp->dd_name, sizeof(ddp->dd_name)) != 0) &&
143*352e3770Sguenther 		    (strncmp(disk, ddp->dd_name, sizeof(ddp->dd_name)) != 0))
144df930be7Sderaadt 			continue;
145df930be7Sderaadt 		if (ddp->dd_level >= level)
146df930be7Sderaadt 			continue;
1470155e653Smillert 		if (ddp->dd_ddate <= (time_t)spcl.c_ddate)
148df930be7Sderaadt 			continue;
1490155e653Smillert 		spcl.c_ddate = (int64_t)ddp->dd_ddate;
150df930be7Sderaadt 		lastlevel = ddp->dd_level;
151df930be7Sderaadt 	}
152df930be7Sderaadt }
153df930be7Sderaadt 
154df930be7Sderaadt void
15553fedb49Sderaadt putdumptime(void)
156df930be7Sderaadt {
157df930be7Sderaadt 	FILE *df;
158e073c79dSmpech 	struct dumpdates *dtwalk;
1590155e653Smillert 	int fd, i;
160df930be7Sderaadt 	char *fname;
1610155e653Smillert 	time_t t;
162df930be7Sderaadt 
163df930be7Sderaadt 	if(uflag == 0)
164df930be7Sderaadt 		return;
165df930be7Sderaadt 	if ((df = fopen(dumpdates, "r+")) == NULL)
166df930be7Sderaadt 		quit("cannot rewrite %s: %s\n", dumpdates, strerror(errno));
167df930be7Sderaadt 	fd = fileno(df);
168df930be7Sderaadt 	(void) flock(fd, LOCK_EX);
169*352e3770Sguenther 	fname = duid ? duid : disk;
170df930be7Sderaadt 	free((char *)ddatev);
171df930be7Sderaadt 	ddatev = 0;
172df930be7Sderaadt 	nddates = 0;
173df930be7Sderaadt 	dthead = 0;
174df930be7Sderaadt 	ddates_in = 0;
175df930be7Sderaadt 	readdumptimes(df);
176a52c79cdStobias 	if (fseek(df, 0L, SEEK_SET) < 0)
177df930be7Sderaadt 		quit("fseek: %s\n", strerror(errno));
178df930be7Sderaadt 	spcl.c_ddate = 0;
179df930be7Sderaadt 	ITITERATE(i, dtwalk) {
180*352e3770Sguenther 		if ((strncmp(fname, dtwalk->dd_name,
181*352e3770Sguenther 			     sizeof(dtwalk->dd_name)) != 0) &&
182*352e3770Sguenther 		    (strncmp(disk, dtwalk->dd_name,
183*352e3770Sguenther 			     sizeof(dtwalk->dd_name)) != 0))
184df930be7Sderaadt 			continue;
185df930be7Sderaadt 		if (dtwalk->dd_level != level)
186df930be7Sderaadt 			continue;
187df930be7Sderaadt 		goto found;
188df930be7Sderaadt 	}
189df930be7Sderaadt 	/*
190df930be7Sderaadt 	 *	construct the new upper bound;
191df930be7Sderaadt 	 *	Enough room has been allocated.
192df930be7Sderaadt 	 */
193df930be7Sderaadt 	dtwalk = ddatev[nddates] =
194df930be7Sderaadt 		(struct dumpdates *)calloc(1, sizeof(struct dumpdates));
195df930be7Sderaadt 	nddates += 1;
196df930be7Sderaadt   found:
1970c2b89afSmillert 	(void) strlcpy(dtwalk->dd_name, fname, sizeof(dtwalk->dd_name));
198df930be7Sderaadt 	dtwalk->dd_level = level;
1990155e653Smillert 	dtwalk->dd_ddate = (time_t)spcl.c_date;
200df930be7Sderaadt 
201df930be7Sderaadt 	ITITERATE(i, dtwalk) {
202df930be7Sderaadt 		dumprecout(df, dtwalk);
203df930be7Sderaadt 	}
204df930be7Sderaadt 	if (fflush(df))
205df930be7Sderaadt 		quit("%s: %s\n", dumpdates, strerror(errno));
2060e419460Sderaadt 	if (ftruncate(fd, ftello(df)))
207df930be7Sderaadt 		quit("ftruncate (%s): %s\n", dumpdates, strerror(errno));
208df930be7Sderaadt 	(void) fclose(df);
2090155e653Smillert 	t = (time_t)spcl.c_date;
2100155e653Smillert 	msg("level %c dump on %s", level, t == 0 ? "the epoch\n" : ctime(&t));
211df930be7Sderaadt }
212df930be7Sderaadt 
213df930be7Sderaadt static void
21453fedb49Sderaadt dumprecout(FILE *file, struct dumpdates *what)
215df930be7Sderaadt {
216df930be7Sderaadt 
217df930be7Sderaadt 	if (fprintf(file, DUMPOUTFMT,
218df930be7Sderaadt 		    what->dd_name,
219df930be7Sderaadt 		    what->dd_level,
220df930be7Sderaadt 		    ctime(&what->dd_ddate)) < 0)
221df930be7Sderaadt 		quit("%s: %s\n", dumpdates, strerror(errno));
222df930be7Sderaadt }
223df930be7Sderaadt 
224df930be7Sderaadt int	recno;
225df930be7Sderaadt 
226df930be7Sderaadt static int
22753fedb49Sderaadt getrecord(FILE *df, struct dumpdates *ddatep)
228df930be7Sderaadt {
229df930be7Sderaadt 	char tbuf[BUFSIZ];
230df930be7Sderaadt 
231df930be7Sderaadt 	recno = 0;
2322904a520Smillert 	if (fgets(tbuf, sizeof(tbuf), df) == NULL)
233df930be7Sderaadt 		return(-1);
234df930be7Sderaadt 	recno++;
235df930be7Sderaadt 	if (makedumpdate(ddatep, tbuf) < 0)
236df930be7Sderaadt 		msg("Unknown intermediate format in %s, line %d\n",
237df930be7Sderaadt 			dumpdates, recno);
238df930be7Sderaadt 
239df930be7Sderaadt #ifdef FDEBUG
240df930be7Sderaadt 	msg("getrecord: %s %c %s", ddatep->dd_name, ddatep->dd_level,
241df930be7Sderaadt 	    ddatep->dd_ddate == 0 ? "the epoch\n" : ctime(&ddatep->dd_ddate));
242df930be7Sderaadt #endif
243df930be7Sderaadt 	return(0);
244df930be7Sderaadt }
245df930be7Sderaadt 
246df930be7Sderaadt static int
24753fedb49Sderaadt makedumpdate(struct dumpdates *ddp, char *tbuf)
248df930be7Sderaadt {
24900e3001fSderaadt 	char un_buf[BUFSIZ], *str;
25000e3001fSderaadt 	struct tm then;
251df930be7Sderaadt 
25200e3001fSderaadt 	if (sscanf(tbuf, DUMPINFMT, ddp->dd_name, &ddp->dd_level, un_buf) != 3)
25300e3001fSderaadt 		return(-1);
25400e3001fSderaadt 	str = strptime(un_buf, "%a %b %e %H:%M:%S %Y", &then);
25500e3001fSderaadt 	then.tm_isdst = -1;
25600e3001fSderaadt 	if (str == NULL || (*str != '\n' && *str != '\0'))
25700e3001fSderaadt 		ddp->dd_ddate = (time_t) -1;
25800e3001fSderaadt 	else
25900e3001fSderaadt 		ddp->dd_ddate = mktime(&then);
260df930be7Sderaadt 	if (ddp->dd_ddate < 0)
261df930be7Sderaadt 		return(-1);
262df930be7Sderaadt 	return(0);
263df930be7Sderaadt }
264