xref: /openbsd/sbin/dump/itime.c (revision a7b9eedc)
1*a7b9eedcSflorian /*	$OpenBSD: itime.c,v 1.27 2024/05/09 08:35:40 florian 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 int	nddates = 0;
519be7043fSmortimer struct	dumpdates **ddatev = NULL;
529be7043fSmortimer char	*dumpdates = NULL;
539be7043fSmortimer char	lastlevel = 0;
54df930be7Sderaadt int	ddates_in = 0;
551ee01421Smmcc struct	dumptime *dthead = NULL;
56df930be7Sderaadt 
57c72b5b24Smillert static	void dumprecout(FILE *, struct dumpdates *);
58c72b5b24Smillert static	int getrecord(FILE *, struct dumpdates *);
59c72b5b24Smillert static	int makedumpdate(struct dumpdates *, char *);
60c72b5b24Smillert static	void readdumptimes(FILE *);
61df930be7Sderaadt 
62df930be7Sderaadt void
initdumptimes(void)6353fedb49Sderaadt initdumptimes(void)
64df930be7Sderaadt {
65df930be7Sderaadt 	FILE *df;
66df930be7Sderaadt 
67df930be7Sderaadt 	if ((df = fopen(dumpdates, "r")) == NULL) {
68df930be7Sderaadt 		if (errno != ENOENT) {
69df930be7Sderaadt 			quit("cannot read %s: %s\n", dumpdates,
70df930be7Sderaadt 			    strerror(errno));
71df930be7Sderaadt 			/* NOTREACHED */
72df930be7Sderaadt 		}
73df930be7Sderaadt 		/*
74df930be7Sderaadt 		 * Dumpdates does not exist, make an empty one.
75df930be7Sderaadt 		 */
76df930be7Sderaadt 		msg("WARNING: no file `%s', making an empty one\n", dumpdates);
77df930be7Sderaadt 		if ((df = fopen(dumpdates, "w")) == NULL) {
78df930be7Sderaadt 			quit("cannot create %s: %s\n", dumpdates,
79df930be7Sderaadt 			    strerror(errno));
80df930be7Sderaadt 			/* NOTREACHED */
81df930be7Sderaadt 		}
82df930be7Sderaadt 		(void) fclose(df);
83df930be7Sderaadt 		if ((df = fopen(dumpdates, "r")) == NULL) {
84df930be7Sderaadt 			quit("cannot read %s even after creating it: %s\n",
85df930be7Sderaadt 			    dumpdates, strerror(errno));
86df930be7Sderaadt 			/* NOTREACHED */
87df930be7Sderaadt 		}
88df930be7Sderaadt 	}
89df930be7Sderaadt 	(void) flock(fileno(df), LOCK_SH);
90df930be7Sderaadt 	readdumptimes(df);
91df930be7Sderaadt 	(void) fclose(df);
92df930be7Sderaadt }
93df930be7Sderaadt 
94df930be7Sderaadt static void
readdumptimes(FILE * df)9553fedb49Sderaadt readdumptimes(FILE *df)
96df930be7Sderaadt {
97e073c79dSmpech 	int i;
98e073c79dSmpech 	struct	dumptime *dtwalk;
99df930be7Sderaadt 
100df930be7Sderaadt 	for (;;) {
1015ae94ef8Sderaadt 		dtwalk = calloc(1, sizeof(struct dumptime));
102fd6a1eb9Sotto 		if (dtwalk == NULL)
103fd6a1eb9Sotto 			quit("allocation failed");
1048b448031Sderaadt 		if (getrecord(df, &(dtwalk->dt_value)) < 0) {
1058b448031Sderaadt 			free(dtwalk);
106df930be7Sderaadt 			break;
1078b448031Sderaadt 		}
108df930be7Sderaadt 		nddates++;
109df930be7Sderaadt 		dtwalk->dt_next = dthead;
110df930be7Sderaadt 		dthead = dtwalk;
111df930be7Sderaadt 	}
112df930be7Sderaadt 
113df930be7Sderaadt 	ddates_in = 1;
114df930be7Sderaadt 	/*
115df930be7Sderaadt 	 *	arrayify the list, leaving enough room for the additional
116df930be7Sderaadt 	 *	record that we may have to add to the ddate structure
117df930be7Sderaadt 	 */
1185ae94ef8Sderaadt 	ddatev = calloc((unsigned) (nddates + 1), sizeof(struct dumpdates *));
119fd6a1eb9Sotto 	if (ddatev == NULL)
120fd6a1eb9Sotto 		quit("allocation failed");
121df930be7Sderaadt 	dtwalk = dthead;
122df930be7Sderaadt 	for (i = nddates - 1; i >= 0; i--, dtwalk = dtwalk->dt_next)
123df930be7Sderaadt 		ddatev[i] = &dtwalk->dt_value;
124df930be7Sderaadt }
125df930be7Sderaadt 
126df930be7Sderaadt void
getdumptime(void)12753fedb49Sderaadt getdumptime(void)
128df930be7Sderaadt {
129e073c79dSmpech 	struct dumpdates *ddp;
130e073c79dSmpech 	int i;
131df930be7Sderaadt 	char *fname;
132df930be7Sderaadt 
133352e3770Sguenther 	fname = duid ? duid : disk;
134df930be7Sderaadt #ifdef FDEBUG
135df930be7Sderaadt 	msg("Looking for name %s in dumpdates = %s for level = %c\n",
136df930be7Sderaadt 		fname, dumpdates, level);
137df930be7Sderaadt #endif
138df930be7Sderaadt 	spcl.c_ddate = 0;
139df930be7Sderaadt 	lastlevel = '0';
140df930be7Sderaadt 
141df930be7Sderaadt 	initdumptimes();
142df930be7Sderaadt 	/*
143df930be7Sderaadt 	 *	Go find the entry with the same name for a lower increment
144df930be7Sderaadt 	 *	and older date
145df930be7Sderaadt 	 */
146df930be7Sderaadt 	ITITERATE(i, ddp) {
147352e3770Sguenther 		if ((strncmp(fname, ddp->dd_name, sizeof(ddp->dd_name)) != 0) &&
148352e3770Sguenther 		    (strncmp(disk, ddp->dd_name, sizeof(ddp->dd_name)) != 0))
149df930be7Sderaadt 			continue;
150df930be7Sderaadt 		if (ddp->dd_level >= level)
151df930be7Sderaadt 			continue;
1520155e653Smillert 		if (ddp->dd_ddate <= (time_t)spcl.c_ddate)
153df930be7Sderaadt 			continue;
1540155e653Smillert 		spcl.c_ddate = (int64_t)ddp->dd_ddate;
155df930be7Sderaadt 		lastlevel = ddp->dd_level;
156df930be7Sderaadt 	}
157df930be7Sderaadt }
158df930be7Sderaadt 
159df930be7Sderaadt void
putdumptime(void)16053fedb49Sderaadt putdumptime(void)
161df930be7Sderaadt {
162df930be7Sderaadt 	FILE *df;
163e073c79dSmpech 	struct dumpdates *dtwalk;
1640155e653Smillert 	int fd, i;
165*a7b9eedcSflorian 	char *fname, *ct;
1660155e653Smillert 	time_t t;
167df930be7Sderaadt 
168df930be7Sderaadt 	if(uflag == 0)
169df930be7Sderaadt 		return;
170df930be7Sderaadt 	if ((df = fopen(dumpdates, "r+")) == NULL)
171df930be7Sderaadt 		quit("cannot rewrite %s: %s\n", dumpdates, strerror(errno));
172df930be7Sderaadt 	fd = fileno(df);
173df930be7Sderaadt 	(void) flock(fd, LOCK_EX);
174352e3770Sguenther 	fname = duid ? duid : disk;
1751ee01421Smmcc 	free(ddatev);
1761ee01421Smmcc 	ddatev = NULL;
177df930be7Sderaadt 	nddates = 0;
1781ee01421Smmcc 	dthead = NULL;
179df930be7Sderaadt 	ddates_in = 0;
180df930be7Sderaadt 	readdumptimes(df);
181df69c215Sderaadt 	if (fseek(df, 0L, SEEK_SET) == -1)
182df930be7Sderaadt 		quit("fseek: %s\n", strerror(errno));
183df930be7Sderaadt 	spcl.c_ddate = 0;
184df930be7Sderaadt 	ITITERATE(i, dtwalk) {
185352e3770Sguenther 		if ((strncmp(fname, dtwalk->dd_name,
186352e3770Sguenther 			     sizeof(dtwalk->dd_name)) != 0) &&
187352e3770Sguenther 		    (strncmp(disk, dtwalk->dd_name,
188352e3770Sguenther 			     sizeof(dtwalk->dd_name)) != 0))
189df930be7Sderaadt 			continue;
190df930be7Sderaadt 		if (dtwalk->dd_level != level)
191df930be7Sderaadt 			continue;
192df930be7Sderaadt 		goto found;
193df930be7Sderaadt 	}
194df930be7Sderaadt 	/*
195df930be7Sderaadt 	 *	construct the new upper bound;
196df930be7Sderaadt 	 *	Enough room has been allocated.
197df930be7Sderaadt 	 */
1985ae94ef8Sderaadt 	dtwalk = ddatev[nddates] = calloc(1, sizeof(struct dumpdates));
199fd6a1eb9Sotto 	if (dtwalk == NULL)
200fd6a1eb9Sotto 		quit("allocation failed");
201df930be7Sderaadt 	nddates += 1;
202df930be7Sderaadt   found:
2030c2b89afSmillert 	(void) strlcpy(dtwalk->dd_name, fname, sizeof(dtwalk->dd_name));
204df930be7Sderaadt 	dtwalk->dd_level = level;
2050155e653Smillert 	dtwalk->dd_ddate = (time_t)spcl.c_date;
206df930be7Sderaadt 
207df930be7Sderaadt 	ITITERATE(i, dtwalk) {
208df930be7Sderaadt 		dumprecout(df, dtwalk);
209df930be7Sderaadt 	}
210df930be7Sderaadt 	if (fflush(df))
211df930be7Sderaadt 		quit("%s: %s\n", dumpdates, strerror(errno));
2120e419460Sderaadt 	if (ftruncate(fd, ftello(df)))
213df930be7Sderaadt 		quit("ftruncate (%s): %s\n", dumpdates, strerror(errno));
214df930be7Sderaadt 	(void) fclose(df);
2150155e653Smillert 	t = (time_t)spcl.c_date;
216*a7b9eedcSflorian 	if (t == 0)
217*a7b9eedcSflorian 		ct = "the epoch\n";
218*a7b9eedcSflorian 	else if ((ct = ctime(&t)) == NULL)
219*a7b9eedcSflorian 		ct = "?\n";
220*a7b9eedcSflorian 	msg("level %c dump on %s", level, ct);
221df930be7Sderaadt }
222df930be7Sderaadt 
223df930be7Sderaadt static void
dumprecout(FILE * file,struct dumpdates * what)22453fedb49Sderaadt dumprecout(FILE *file, struct dumpdates *what)
225df930be7Sderaadt {
226*a7b9eedcSflorian 	char *ct;
227*a7b9eedcSflorian 
228*a7b9eedcSflorian 	ct = ctime(&what->dd_ddate);
229*a7b9eedcSflorian 	if (ct == NULL)
230*a7b9eedcSflorian 		quit("Cannot convert date\n");
231df930be7Sderaadt 
232df930be7Sderaadt 	if (fprintf(file, DUMPOUTFMT,
233df930be7Sderaadt 		    what->dd_name,
234df930be7Sderaadt 		    what->dd_level,
235df930be7Sderaadt 		    ctime(&what->dd_ddate)) < 0)
236df930be7Sderaadt 		quit("%s: %s\n", dumpdates, strerror(errno));
237df930be7Sderaadt }
238df930be7Sderaadt 
239df930be7Sderaadt int	recno;
240df930be7Sderaadt 
241df930be7Sderaadt static int
getrecord(FILE * df,struct dumpdates * ddatep)24253fedb49Sderaadt getrecord(FILE *df, struct dumpdates *ddatep)
243df930be7Sderaadt {
244df930be7Sderaadt 	char tbuf[BUFSIZ];
245df930be7Sderaadt 
246df930be7Sderaadt 	recno = 0;
2472904a520Smillert 	if (fgets(tbuf, sizeof(tbuf), df) == NULL)
248df930be7Sderaadt 		return(-1);
249df930be7Sderaadt 	recno++;
250df930be7Sderaadt 	if (makedumpdate(ddatep, tbuf) < 0)
251df930be7Sderaadt 		msg("Unknown intermediate format in %s, line %d\n",
252df930be7Sderaadt 			dumpdates, recno);
253df930be7Sderaadt 
254df930be7Sderaadt #ifdef FDEBUG
255*a7b9eedcSflorian 	{
256*a7b9eedcSflorian 		char *ct;
257*a7b9eedcSflorian 
258*a7b9eedcSflorian 		if (ddatep->dd_ddate == 0)
259*a7b9eedcSflorian 			ct = "the epoch\n";
260*a7b9eedcSflorian 		else
261*a7b9eedcSflorian 			ct = ctime(&ddatep->dd_ddate);
262*a7b9eedcSflorian 
263*a7b9eedcSflorian 		if (ct)
264*a7b9eedcSflorian 			msg("getrecord: %s %c %s", ddatep->dd_name,
265*a7b9eedcSflorian 			    ddatep->dd_level, ct);
266*a7b9eedcSflorian 		else
267*a7b9eedcSflorian 			msg("getrecord: %s %c %lld seconds after the epoch\n",
268*a7b9eedcSflorian 			    ddatep->dd_name, ddatep->dd_level,
269*a7b9eedcSflorian 			    ddatep->dd_ddate);
270*a7b9eedcSflorian 	}
271df930be7Sderaadt #endif
272df930be7Sderaadt 	return(0);
273df930be7Sderaadt }
274df930be7Sderaadt 
275df930be7Sderaadt static int
makedumpdate(struct dumpdates * ddp,char * tbuf)27653fedb49Sderaadt makedumpdate(struct dumpdates *ddp, char *tbuf)
277df930be7Sderaadt {
27800e3001fSderaadt 	char un_buf[BUFSIZ], *str;
27900e3001fSderaadt 	struct tm then;
280df930be7Sderaadt 
28100e3001fSderaadt 	if (sscanf(tbuf, DUMPINFMT, ddp->dd_name, &ddp->dd_level, un_buf) != 3)
28200e3001fSderaadt 		return(-1);
2837833183cSguenther 	str = getduid(ddp->dd_name);
2847833183cSguenther 	if (str != NULL) {
2857833183cSguenther 		strlcpy(ddp->dd_name, str, sizeof(ddp->dd_name));
2867833183cSguenther 		free(str);
2877833183cSguenther 	}
28800e3001fSderaadt 	str = strptime(un_buf, "%a %b %e %H:%M:%S %Y", &then);
28900e3001fSderaadt 	then.tm_isdst = -1;
29000e3001fSderaadt 	if (str == NULL || (*str != '\n' && *str != '\0'))
29100e3001fSderaadt 		ddp->dd_ddate = (time_t) -1;
29200e3001fSderaadt 	else
29300e3001fSderaadt 		ddp->dd_ddate = mktime(&then);
294df930be7Sderaadt 	if (ddp->dd_ddate < 0)
295df930be7Sderaadt 		return(-1);
296df930be7Sderaadt 	return(0);
297df930be7Sderaadt }
298