xref: /freebsd/sbin/dump/itime.c (revision 8a16b7a18f5d0b031f09832fd7752fba717e2a97)
18fae3551SRodney W. Grimes /*-
2*8a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
3*8a16b7a1SPedro F. Giffuni  *
48fae3551SRodney W. Grimes  * Copyright (c) 1980, 1993
58fae3551SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
68fae3551SRodney W. Grimes  *
78fae3551SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
88fae3551SRodney W. Grimes  * modification, are permitted provided that the following conditions
98fae3551SRodney W. Grimes  * are met:
108fae3551SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
118fae3551SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
128fae3551SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
138fae3551SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
148fae3551SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
15fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
168fae3551SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
178fae3551SRodney W. Grimes  *    without specific prior written permission.
188fae3551SRodney W. Grimes  *
198fae3551SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
208fae3551SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
218fae3551SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
228fae3551SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
238fae3551SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
248fae3551SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
258fae3551SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
268fae3551SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
278fae3551SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
288fae3551SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
298fae3551SRodney W. Grimes  * SUCH DAMAGE.
308fae3551SRodney W. Grimes  */
318fae3551SRodney W. Grimes 
328fae3551SRodney W. Grimes #ifndef lint
337580ffbbSPhilippe Charnier #if 0
348fae3551SRodney W. Grimes static char sccsid[] = "@(#)itime.c	8.1 (Berkeley) 6/5/93";
357580ffbbSPhilippe Charnier #endif
367580ffbbSPhilippe Charnier static const char rcsid[] =
377f3dea24SPeter Wemm   "$FreeBSD$";
388fae3551SRodney W. Grimes #endif /* not lint */
398fae3551SRodney W. Grimes 
408fae3551SRodney W. Grimes #include <sys/param.h>
41941ee632SPoul-Henning Kamp #include <sys/queue.h>
428fae3551SRodney W. Grimes 
438fae3551SRodney W. Grimes #include <ufs/ufs/dinode.h>
448fae3551SRodney W. Grimes 
458fae3551SRodney W. Grimes #include <protocols/dumprestore.h>
468fae3551SRodney W. Grimes 
478fae3551SRodney W. Grimes #include <errno.h>
488fae3551SRodney W. Grimes #include <fcntl.h>
4989fdc4e1SMike Barcroft #include <limits.h>
508fae3551SRodney W. Grimes #include <stdio.h>
518fae3551SRodney W. Grimes #include <stdlib.h>
528fae3551SRodney W. Grimes #include <string.h>
537649cb00SKirk McKusick #include <time.h>
54617dbd3cSIan Dowse #include <timeconv.h>
558fae3551SRodney W. Grimes 
568fae3551SRodney W. Grimes #include "dump.h"
578fae3551SRodney W. Grimes 
58941ee632SPoul-Henning Kamp struct dumptime {
59941ee632SPoul-Henning Kamp 	struct	dumpdates dt_value;
60941ee632SPoul-Henning Kamp 	SLIST_ENTRY(dumptime) dt_list;
61941ee632SPoul-Henning Kamp };
62941ee632SPoul-Henning Kamp SLIST_HEAD(dthead, dumptime) dthead = SLIST_HEAD_INITIALIZER(dthead);
634c8762f0SPedro F. Giffuni struct	dumpdates **ddatev = NULL;
648fae3551SRodney W. Grimes int	nddates = 0;
658fae3551SRodney W. Grimes 
662db673abSWarner Losh static	void dumprecout(FILE *, const struct dumpdates *);
672db673abSWarner Losh static	int getrecord(FILE *, struct dumpdates *);
682db673abSWarner Losh static	int makedumpdate(struct dumpdates *, const char *);
692db673abSWarner Losh static	void readdumptimes(FILE *);
708fae3551SRodney W. Grimes 
718fae3551SRodney W. Grimes void
722db673abSWarner Losh initdumptimes(void)
738fae3551SRodney W. Grimes {
748fae3551SRodney W. Grimes 	FILE *df;
758fae3551SRodney W. Grimes 
768fae3551SRodney W. Grimes 	if ((df = fopen(dumpdates, "r")) == NULL) {
778fae3551SRodney W. Grimes 		if (errno != ENOENT) {
78b2f6bdeeSDavid E. O'Brien 			msg("WARNING: cannot read %s: %s\n", dumpdates,
798fae3551SRodney W. Grimes 			    strerror(errno));
808cc6e4d8SDavid E. O'Brien 			return;
818fae3551SRodney W. Grimes 		}
828fae3551SRodney W. Grimes 		/*
838fae3551SRodney W. Grimes 		 * Dumpdates does not exist, make an empty one.
848fae3551SRodney W. Grimes 		 */
858fae3551SRodney W. Grimes 		msg("WARNING: no file `%s', making an empty one\n", dumpdates);
868fae3551SRodney W. Grimes 		if ((df = fopen(dumpdates, "w")) == NULL) {
87b2f6bdeeSDavid E. O'Brien 			msg("WARNING: cannot create %s: %s\n", dumpdates,
888fae3551SRodney W. Grimes 			    strerror(errno));
898cc6e4d8SDavid E. O'Brien 			return;
908fae3551SRodney W. Grimes 		}
918fae3551SRodney W. Grimes 		(void) fclose(df);
928fae3551SRodney W. Grimes 		if ((df = fopen(dumpdates, "r")) == NULL) {
938fae3551SRodney W. Grimes 			quit("cannot read %s even after creating it: %s\n",
948fae3551SRodney W. Grimes 			    dumpdates, strerror(errno));
958fae3551SRodney W. Grimes 			/* NOTREACHED */
968fae3551SRodney W. Grimes 		}
978fae3551SRodney W. Grimes 	}
988fae3551SRodney W. Grimes 	(void) flock(fileno(df), LOCK_SH);
998fae3551SRodney W. Grimes 	readdumptimes(df);
1008fae3551SRodney W. Grimes 	(void) fclose(df);
1018fae3551SRodney W. Grimes }
1028fae3551SRodney W. Grimes 
1038fae3551SRodney W. Grimes static void
1042db673abSWarner Losh readdumptimes(FILE *df)
1058fae3551SRodney W. Grimes {
106d2334e27SIan Dowse 	int i;
107d2334e27SIan Dowse 	struct	dumptime *dtwalk;
1088fae3551SRodney W. Grimes 
1098fae3551SRodney W. Grimes 	for (;;) {
1108fae3551SRodney W. Grimes 		dtwalk = (struct dumptime *)calloc(1, sizeof (struct dumptime));
11180ca1f34SXin LI 		if (getrecord(df, &(dtwalk->dt_value)) < 0) {
11280ca1f34SXin LI 			free(dtwalk);
1138fae3551SRodney W. Grimes 			break;
11480ca1f34SXin LI 		}
1158fae3551SRodney W. Grimes 		nddates++;
116941ee632SPoul-Henning Kamp 		SLIST_INSERT_HEAD(&dthead, dtwalk, dt_list);
1178fae3551SRodney W. Grimes 	}
1188fae3551SRodney W. Grimes 
1198fae3551SRodney W. Grimes 	/*
1208fae3551SRodney W. Grimes 	 *	arrayify the list, leaving enough room for the additional
1218fae3551SRodney W. Grimes 	 *	record that we may have to add to the ddate structure
1228fae3551SRodney W. Grimes 	 */
1234c8762f0SPedro F. Giffuni 	ddatev = calloc((unsigned) (nddates + 1), sizeof (struct dumpdates *));
124941ee632SPoul-Henning Kamp 	dtwalk = SLIST_FIRST(&dthead);
125941ee632SPoul-Henning Kamp 	for (i = nddates - 1; i >= 0; i--, dtwalk = SLIST_NEXT(dtwalk, dt_list))
1268fae3551SRodney W. Grimes 		ddatev[i] = &dtwalk->dt_value;
1278fae3551SRodney W. Grimes }
1288fae3551SRodney W. Grimes 
1298fae3551SRodney W. Grimes void
1302db673abSWarner Losh getdumptime(void)
1318fae3551SRodney W. Grimes {
132d2334e27SIan Dowse 	struct dumpdates *ddp;
133d2334e27SIan Dowse 	int i;
1348fae3551SRodney W. Grimes 	char *fname;
1358fae3551SRodney W. Grimes 
1368fae3551SRodney W. Grimes 	fname = disk;
1378fae3551SRodney W. Grimes #ifdef FDEBUG
138f72ab793SKirk McKusick 	msg("Looking for name %s in dumpdates = %s for level = %d\n",
1398fae3551SRodney W. Grimes 		fname, dumpdates, level);
1408fae3551SRodney W. Grimes #endif
1418fae3551SRodney W. Grimes 	spcl.c_ddate = 0;
142f72ab793SKirk McKusick 	lastlevel = 0;
1438fae3551SRodney W. Grimes 
1448fae3551SRodney W. Grimes 	initdumptimes();
1458fae3551SRodney W. Grimes 	/*
1468fae3551SRodney W. Grimes 	 *	Go find the entry with the same name for a lower increment
1478fae3551SRodney W. Grimes 	 *	and older date
1488fae3551SRodney W. Grimes 	 */
1498fae3551SRodney W. Grimes 	ITITERATE(i, ddp) {
1508fae3551SRodney W. Grimes 		if (strncmp(fname, ddp->dd_name, sizeof (ddp->dd_name)) != 0)
1518fae3551SRodney W. Grimes 			continue;
1528fae3551SRodney W. Grimes 		if (ddp->dd_level >= level)
1538fae3551SRodney W. Grimes 			continue;
1541c85e6a3SKirk McKusick 		if (ddp->dd_ddate <= _time64_to_time(spcl.c_ddate))
1558fae3551SRodney W. Grimes 			continue;
1561c85e6a3SKirk McKusick 		spcl.c_ddate = _time_to_time64(ddp->dd_ddate);
1578fae3551SRodney W. Grimes 		lastlevel = ddp->dd_level;
1588fae3551SRodney W. Grimes 	}
1598fae3551SRodney W. Grimes }
1608fae3551SRodney W. Grimes 
1618fae3551SRodney W. Grimes void
1622db673abSWarner Losh putdumptime(void)
1638fae3551SRodney W. Grimes {
1648fae3551SRodney W. Grimes 	FILE *df;
165d2334e27SIan Dowse 	struct dumpdates *dtwalk;
166d2334e27SIan Dowse 	int i;
1678fae3551SRodney W. Grimes 	int fd;
1688fae3551SRodney W. Grimes 	char *fname;
1695b3817c6SMatthew Dillon 	char *tmsg;
1708fae3551SRodney W. Grimes 
1718fae3551SRodney W. Grimes 	if(uflag == 0)
1728fae3551SRodney W. Grimes 		return;
1738fae3551SRodney W. Grimes 	if ((df = fopen(dumpdates, "r+")) == NULL)
1748fae3551SRodney W. Grimes 		quit("cannot rewrite %s: %s\n", dumpdates, strerror(errno));
1758fae3551SRodney W. Grimes 	fd = fileno(df);
1768fae3551SRodney W. Grimes 	(void) flock(fd, LOCK_EX);
1778fae3551SRodney W. Grimes 	fname = disk;
1784c8762f0SPedro F. Giffuni 	free(ddatev);
1794c8762f0SPedro F. Giffuni 	ddatev = NULL;
1808fae3551SRodney W. Grimes 	nddates = 0;
1818fae3551SRodney W. Grimes 	readdumptimes(df);
1828fae3551SRodney W. Grimes 	if (fseek(df, 0L, 0) < 0)
1838fae3551SRodney W. Grimes 		quit("fseek: %s\n", strerror(errno));
1848fae3551SRodney W. Grimes 	spcl.c_ddate = 0;
1858fae3551SRodney W. Grimes 	ITITERATE(i, dtwalk) {
1868fae3551SRodney W. Grimes 		if (strncmp(fname, dtwalk->dd_name,
1878fae3551SRodney W. Grimes 				sizeof (dtwalk->dd_name)) != 0)
1888fae3551SRodney W. Grimes 			continue;
1898fae3551SRodney W. Grimes 		if (dtwalk->dd_level != level)
1908fae3551SRodney W. Grimes 			continue;
1918fae3551SRodney W. Grimes 		goto found;
1928fae3551SRodney W. Grimes 	}
1938fae3551SRodney W. Grimes 	/*
1948fae3551SRodney W. Grimes 	 *	construct the new upper bound;
1958fae3551SRodney W. Grimes 	 *	Enough room has been allocated.
1968fae3551SRodney W. Grimes 	 */
1978fae3551SRodney W. Grimes 	dtwalk = ddatev[nddates] =
1988fae3551SRodney W. Grimes 		(struct dumpdates *)calloc(1, sizeof (struct dumpdates));
1998fae3551SRodney W. Grimes 	nddates += 1;
2008fae3551SRodney W. Grimes   found:
2018fae3551SRodney W. Grimes 	(void) strncpy(dtwalk->dd_name, fname, sizeof (dtwalk->dd_name));
2028fae3551SRodney W. Grimes 	dtwalk->dd_level = level;
2031c85e6a3SKirk McKusick 	dtwalk->dd_ddate = _time64_to_time(spcl.c_date);
2048fae3551SRodney W. Grimes 
2058fae3551SRodney W. Grimes 	ITITERATE(i, dtwalk) {
2068fae3551SRodney W. Grimes 		dumprecout(df, dtwalk);
2078fae3551SRodney W. Grimes 	}
2088fae3551SRodney W. Grimes 	if (fflush(df))
2098fae3551SRodney W. Grimes 		quit("%s: %s\n", dumpdates, strerror(errno));
2108fae3551SRodney W. Grimes 	if (ftruncate(fd, ftell(df)))
2118fae3551SRodney W. Grimes 		quit("ftruncate (%s): %s\n", dumpdates, strerror(errno));
2128fae3551SRodney W. Grimes 	(void) fclose(df);
2135b3817c6SMatthew Dillon 	if (spcl.c_date == 0) {
2145b3817c6SMatthew Dillon 		tmsg = "the epoch\n";
2155b3817c6SMatthew Dillon 	} else {
2161c85e6a3SKirk McKusick 		time_t t = _time64_to_time(spcl.c_date);
2175b3817c6SMatthew Dillon 		tmsg = ctime(&t);
2185b3817c6SMatthew Dillon 	}
219f72ab793SKirk McKusick 	msg("level %d dump on %s", level, tmsg);
2208fae3551SRodney W. Grimes }
2218fae3551SRodney W. Grimes 
2228fae3551SRodney W. Grimes static void
2232db673abSWarner Losh dumprecout(FILE *file, const struct dumpdates *what)
2248fae3551SRodney W. Grimes {
2258fae3551SRodney W. Grimes 
22623ad9069SKirk McKusick 	if (strlen(what->dd_name) > DUMPFMTLEN)
22723ad9069SKirk McKusick 		quit("Name '%s' exceeds DUMPFMTLEN (%d) bytes\n",
22823ad9069SKirk McKusick 		    what->dd_name, DUMPFMTLEN);
22923ad9069SKirk McKusick 	if (fprintf(file, DUMPOUTFMT, DUMPFMTLEN, what->dd_name,
2302db673abSWarner Losh 	      what->dd_level, ctime(&what->dd_ddate)) < 0)
2318fae3551SRodney W. Grimes 		quit("%s: %s\n", dumpdates, strerror(errno));
2328fae3551SRodney W. Grimes }
2338fae3551SRodney W. Grimes 
2348fae3551SRodney W. Grimes int	recno;
2358fae3551SRodney W. Grimes 
2368fae3551SRodney W. Grimes static int
2372db673abSWarner Losh getrecord(FILE *df, struct dumpdates *ddatep)
2388fae3551SRodney W. Grimes {
2398fae3551SRodney W. Grimes 	char tbuf[BUFSIZ];
2408fae3551SRodney W. Grimes 
2418fae3551SRodney W. Grimes 	recno = 0;
2428fae3551SRodney W. Grimes 	if ( (fgets(tbuf, sizeof (tbuf), df)) != tbuf)
2438fae3551SRodney W. Grimes 		return(-1);
2448fae3551SRodney W. Grimes 	recno++;
2458fae3551SRodney W. Grimes 	if (makedumpdate(ddatep, tbuf) < 0)
2468fae3551SRodney W. Grimes 		msg("Unknown intermediate format in %s, line %d\n",
2478fae3551SRodney W. Grimes 			dumpdates, recno);
2488fae3551SRodney W. Grimes 
2498fae3551SRodney W. Grimes #ifdef FDEBUG
250f72ab793SKirk McKusick 	msg("getrecord: %s %d %s", ddatep->dd_name, ddatep->dd_level,
2518fae3551SRodney W. Grimes 	    ddatep->dd_ddate == 0 ? "the epoch\n" : ctime(&ddatep->dd_ddate));
2528fae3551SRodney W. Grimes #endif
2538fae3551SRodney W. Grimes 	return(0);
2548fae3551SRodney W. Grimes }
2558fae3551SRodney W. Grimes 
2568fae3551SRodney W. Grimes static int
2572db673abSWarner Losh makedumpdate(struct dumpdates *ddp, const char *tbuf)
2588fae3551SRodney W. Grimes {
2598fae3551SRodney W. Grimes 	char un_buf[128];
2608fae3551SRodney W. Grimes 
2618fae3551SRodney W. Grimes 	(void) sscanf(tbuf, DUMPINFMT, ddp->dd_name, &ddp->dd_level, un_buf);
2628fae3551SRodney W. Grimes 	ddp->dd_ddate = unctime(un_buf);
2638fae3551SRodney W. Grimes 	if (ddp->dd_ddate < 0)
2648fae3551SRodney W. Grimes 		return(-1);
2658fae3551SRodney W. Grimes 	return(0);
2668fae3551SRodney W. Grimes }
267