xref: /freebsd/sbin/dump/itime.c (revision 1c85e6a35d93195e896b030d9a55f7ac4ccee2c3)
18fae3551SRodney W. Grimes /*-
28fae3551SRodney W. Grimes  * Copyright (c) 1980, 1993
38fae3551SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
48fae3551SRodney W. Grimes  *
58fae3551SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
68fae3551SRodney W. Grimes  * modification, are permitted provided that the following conditions
78fae3551SRodney W. Grimes  * are met:
88fae3551SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
98fae3551SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
108fae3551SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
118fae3551SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
128fae3551SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
138fae3551SRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
148fae3551SRodney W. Grimes  *    must display the following acknowledgement:
158fae3551SRodney W. Grimes  *	This product includes software developed by the University of
168fae3551SRodney W. Grimes  *	California, Berkeley and its contributors.
178fae3551SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
188fae3551SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
198fae3551SRodney W. Grimes  *    without specific prior written permission.
208fae3551SRodney W. Grimes  *
218fae3551SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
228fae3551SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
238fae3551SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
248fae3551SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
258fae3551SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
268fae3551SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
278fae3551SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
288fae3551SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
298fae3551SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
308fae3551SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
318fae3551SRodney W. Grimes  * SUCH DAMAGE.
328fae3551SRodney W. Grimes  */
338fae3551SRodney W. Grimes 
348fae3551SRodney W. Grimes #ifndef lint
357580ffbbSPhilippe Charnier #if 0
368fae3551SRodney W. Grimes static char sccsid[] = "@(#)itime.c	8.1 (Berkeley) 6/5/93";
377580ffbbSPhilippe Charnier #endif
387580ffbbSPhilippe Charnier static const char rcsid[] =
397f3dea24SPeter Wemm   "$FreeBSD$";
408fae3551SRodney W. Grimes #endif /* not lint */
418fae3551SRodney W. Grimes 
428fae3551SRodney W. Grimes #include <sys/param.h>
43941ee632SPoul-Henning Kamp #include <sys/queue.h>
448fae3551SRodney W. Grimes #include <sys/time.h>
458fae3551SRodney W. Grimes 
468fae3551SRodney W. Grimes #include <ufs/ufs/dinode.h>
478fae3551SRodney W. Grimes 
488fae3551SRodney W. Grimes #include <protocols/dumprestore.h>
498fae3551SRodney W. Grimes 
508fae3551SRodney W. Grimes #include <errno.h>
518fae3551SRodney W. Grimes #include <fcntl.h>
528fae3551SRodney W. Grimes #include <stdio.h>
538fae3551SRodney W. Grimes #include <stdlib.h>
548fae3551SRodney W. Grimes #include <string.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);
638fae3551SRodney W. Grimes struct	dumpdates **ddatev = 0;
648fae3551SRodney W. Grimes int	nddates = 0;
658fae3551SRodney W. Grimes int	ddates_in = 0;
668fae3551SRodney W. Grimes 
672db673abSWarner Losh static	void dumprecout(FILE *, const struct dumpdates *);
682db673abSWarner Losh static	int getrecord(FILE *, struct dumpdates *);
692db673abSWarner Losh static	int makedumpdate(struct dumpdates *, const char *);
702db673abSWarner Losh static	void readdumptimes(FILE *);
718fae3551SRodney W. Grimes 
728fae3551SRodney W. Grimes void
732db673abSWarner Losh initdumptimes(void)
748fae3551SRodney W. Grimes {
758fae3551SRodney W. Grimes 	FILE *df;
768fae3551SRodney W. Grimes 
778fae3551SRodney W. Grimes 	if ((df = fopen(dumpdates, "r")) == NULL) {
788fae3551SRodney W. Grimes 		if (errno != ENOENT) {
79b2f6bdeeSDavid E. O'Brien 			msg("WARNING: cannot read %s: %s\n", dumpdates,
808fae3551SRodney W. Grimes 			    strerror(errno));
818cc6e4d8SDavid E. O'Brien 			return;
828fae3551SRodney W. Grimes 		}
838fae3551SRodney W. Grimes 		/*
848fae3551SRodney W. Grimes 		 * Dumpdates does not exist, make an empty one.
858fae3551SRodney W. Grimes 		 */
868fae3551SRodney W. Grimes 		msg("WARNING: no file `%s', making an empty one\n", dumpdates);
878fae3551SRodney W. Grimes 		if ((df = fopen(dumpdates, "w")) == NULL) {
88b2f6bdeeSDavid E. O'Brien 			msg("WARNING: cannot create %s: %s\n", dumpdates,
898fae3551SRodney W. Grimes 			    strerror(errno));
908cc6e4d8SDavid E. O'Brien 			return;
918fae3551SRodney W. Grimes 		}
928fae3551SRodney W. Grimes 		(void) fclose(df);
938fae3551SRodney W. Grimes 		if ((df = fopen(dumpdates, "r")) == NULL) {
948fae3551SRodney W. Grimes 			quit("cannot read %s even after creating it: %s\n",
958fae3551SRodney W. Grimes 			    dumpdates, strerror(errno));
968fae3551SRodney W. Grimes 			/* NOTREACHED */
978fae3551SRodney W. Grimes 		}
988fae3551SRodney W. Grimes 	}
998fae3551SRodney W. Grimes 	(void) flock(fileno(df), LOCK_SH);
1008fae3551SRodney W. Grimes 	readdumptimes(df);
1018fae3551SRodney W. Grimes 	(void) fclose(df);
1028fae3551SRodney W. Grimes }
1038fae3551SRodney W. Grimes 
1048fae3551SRodney W. Grimes static void
1052db673abSWarner Losh readdumptimes(FILE *df)
1068fae3551SRodney W. Grimes {
107d2334e27SIan Dowse 	int i;
108d2334e27SIan Dowse 	struct	dumptime *dtwalk;
1098fae3551SRodney W. Grimes 
1108fae3551SRodney W. Grimes 	for (;;) {
1118fae3551SRodney W. Grimes 		dtwalk = (struct dumptime *)calloc(1, sizeof (struct dumptime));
1128fae3551SRodney W. Grimes 		if (getrecord(df, &(dtwalk->dt_value)) < 0)
1138fae3551SRodney W. Grimes 			break;
1148fae3551SRodney W. Grimes 		nddates++;
115941ee632SPoul-Henning Kamp 		SLIST_INSERT_HEAD(&dthead, dtwalk, dt_list);
1168fae3551SRodney W. Grimes 	}
1178fae3551SRodney W. Grimes 
1188fae3551SRodney W. Grimes 	ddates_in = 1;
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 	 */
1238fae3551SRodney W. Grimes 	ddatev = (struct dumpdates **)
1248fae3551SRodney W. Grimes 		calloc((unsigned) (nddates + 1), sizeof (struct dumpdates *));
125941ee632SPoul-Henning Kamp 	dtwalk = SLIST_FIRST(&dthead);
126941ee632SPoul-Henning Kamp 	for (i = nddates - 1; i >= 0; i--, dtwalk = SLIST_NEXT(dtwalk, dt_list))
1278fae3551SRodney W. Grimes 		ddatev[i] = &dtwalk->dt_value;
1288fae3551SRodney W. Grimes }
1298fae3551SRodney W. Grimes 
1308fae3551SRodney W. Grimes void
1312db673abSWarner Losh getdumptime(void)
1328fae3551SRodney W. Grimes {
133d2334e27SIan Dowse 	struct dumpdates *ddp;
134d2334e27SIan Dowse 	int i;
1358fae3551SRodney W. Grimes 	char *fname;
1368fae3551SRodney W. Grimes 
1378fae3551SRodney W. Grimes 	fname = disk;
1388fae3551SRodney W. Grimes #ifdef FDEBUG
1398fae3551SRodney W. Grimes 	msg("Looking for name %s in dumpdates = %s for level = %c\n",
1408fae3551SRodney W. Grimes 		fname, dumpdates, level);
1418fae3551SRodney W. Grimes #endif
1428fae3551SRodney W. Grimes 	spcl.c_ddate = 0;
1438fae3551SRodney W. Grimes 	lastlevel = '0';
1448fae3551SRodney W. Grimes 
1458fae3551SRodney W. Grimes 	initdumptimes();
1468fae3551SRodney W. Grimes 	/*
1478fae3551SRodney W. Grimes 	 *	Go find the entry with the same name for a lower increment
1488fae3551SRodney W. Grimes 	 *	and older date
1498fae3551SRodney W. Grimes 	 */
1508fae3551SRodney W. Grimes 	ITITERATE(i, ddp) {
1518fae3551SRodney W. Grimes 		if (strncmp(fname, ddp->dd_name, sizeof (ddp->dd_name)) != 0)
1528fae3551SRodney W. Grimes 			continue;
1538fae3551SRodney W. Grimes 		if (ddp->dd_level >= level)
1548fae3551SRodney W. Grimes 			continue;
1551c85e6a3SKirk McKusick 		if (ddp->dd_ddate <= _time64_to_time(spcl.c_ddate))
1568fae3551SRodney W. Grimes 			continue;
1571c85e6a3SKirk McKusick 		spcl.c_ddate = _time_to_time64(ddp->dd_ddate);
1588fae3551SRodney W. Grimes 		lastlevel = ddp->dd_level;
1598fae3551SRodney W. Grimes 	}
1608fae3551SRodney W. Grimes }
1618fae3551SRodney W. Grimes 
1628fae3551SRodney W. Grimes void
1632db673abSWarner Losh putdumptime(void)
1648fae3551SRodney W. Grimes {
1658fae3551SRodney W. Grimes 	FILE *df;
166d2334e27SIan Dowse 	struct dumpdates *dtwalk;
167d2334e27SIan Dowse 	int i;
1688fae3551SRodney W. Grimes 	int fd;
1698fae3551SRodney W. Grimes 	char *fname;
1705b3817c6SMatthew Dillon 	char *tmsg;
1718fae3551SRodney W. Grimes 
1728fae3551SRodney W. Grimes 	if(uflag == 0)
1738fae3551SRodney W. Grimes 		return;
1748fae3551SRodney W. Grimes 	if ((df = fopen(dumpdates, "r+")) == NULL)
1758fae3551SRodney W. Grimes 		quit("cannot rewrite %s: %s\n", dumpdates, strerror(errno));
1768fae3551SRodney W. Grimes 	fd = fileno(df);
1778fae3551SRodney W. Grimes 	(void) flock(fd, LOCK_EX);
1788fae3551SRodney W. Grimes 	fname = disk;
1798fae3551SRodney W. Grimes 	free((char *)ddatev);
1808fae3551SRodney W. Grimes 	ddatev = 0;
1818fae3551SRodney W. Grimes 	nddates = 0;
1828fae3551SRodney W. Grimes 	ddates_in = 0;
1838fae3551SRodney W. Grimes 	readdumptimes(df);
1848fae3551SRodney W. Grimes 	if (fseek(df, 0L, 0) < 0)
1858fae3551SRodney W. Grimes 		quit("fseek: %s\n", strerror(errno));
1868fae3551SRodney W. Grimes 	spcl.c_ddate = 0;
1878fae3551SRodney W. Grimes 	ITITERATE(i, dtwalk) {
1888fae3551SRodney W. Grimes 		if (strncmp(fname, dtwalk->dd_name,
1898fae3551SRodney W. Grimes 				sizeof (dtwalk->dd_name)) != 0)
1908fae3551SRodney W. Grimes 			continue;
1918fae3551SRodney W. Grimes 		if (dtwalk->dd_level != level)
1928fae3551SRodney W. Grimes 			continue;
1938fae3551SRodney W. Grimes 		goto found;
1948fae3551SRodney W. Grimes 	}
1958fae3551SRodney W. Grimes 	/*
1968fae3551SRodney W. Grimes 	 *	construct the new upper bound;
1978fae3551SRodney W. Grimes 	 *	Enough room has been allocated.
1988fae3551SRodney W. Grimes 	 */
1998fae3551SRodney W. Grimes 	dtwalk = ddatev[nddates] =
2008fae3551SRodney W. Grimes 		(struct dumpdates *)calloc(1, sizeof (struct dumpdates));
2018fae3551SRodney W. Grimes 	nddates += 1;
2028fae3551SRodney W. Grimes   found:
2038fae3551SRodney W. Grimes 	(void) strncpy(dtwalk->dd_name, fname, sizeof (dtwalk->dd_name));
2048fae3551SRodney W. Grimes 	dtwalk->dd_level = level;
2051c85e6a3SKirk McKusick 	dtwalk->dd_ddate = _time64_to_time(spcl.c_date);
2068fae3551SRodney W. Grimes 
2078fae3551SRodney W. Grimes 	ITITERATE(i, dtwalk) {
2088fae3551SRodney W. Grimes 		dumprecout(df, dtwalk);
2098fae3551SRodney W. Grimes 	}
2108fae3551SRodney W. Grimes 	if (fflush(df))
2118fae3551SRodney W. Grimes 		quit("%s: %s\n", dumpdates, strerror(errno));
2128fae3551SRodney W. Grimes 	if (ftruncate(fd, ftell(df)))
2138fae3551SRodney W. Grimes 		quit("ftruncate (%s): %s\n", dumpdates, strerror(errno));
2148fae3551SRodney W. Grimes 	(void) fclose(df);
2155b3817c6SMatthew Dillon 	if (spcl.c_date == 0) {
2165b3817c6SMatthew Dillon 		tmsg = "the epoch\n";
2175b3817c6SMatthew Dillon 	} else {
2181c85e6a3SKirk McKusick 		time_t t = _time64_to_time(spcl.c_date);
2195b3817c6SMatthew Dillon 		tmsg = ctime(&t);
2205b3817c6SMatthew Dillon 	}
2215b3817c6SMatthew Dillon 	msg("level %c dump on %s", level, tmsg);
2228fae3551SRodney W. Grimes }
2238fae3551SRodney W. Grimes 
2248fae3551SRodney W. Grimes static void
2252db673abSWarner Losh dumprecout(FILE *file, const struct dumpdates *what)
2268fae3551SRodney W. Grimes {
2278fae3551SRodney W. Grimes 
2282db673abSWarner Losh 	if (fprintf(file, DUMPOUTFMT, what->dd_name,
2292db673abSWarner Losh 	      what->dd_level, ctime(&what->dd_ddate)) < 0)
2308fae3551SRodney W. Grimes 		quit("%s: %s\n", dumpdates, strerror(errno));
2318fae3551SRodney W. Grimes }
2328fae3551SRodney W. Grimes 
2338fae3551SRodney W. Grimes int	recno;
2348fae3551SRodney W. Grimes 
2358fae3551SRodney W. Grimes static int
2362db673abSWarner Losh getrecord(FILE *df, struct dumpdates *ddatep)
2378fae3551SRodney W. Grimes {
2388fae3551SRodney W. Grimes 	char tbuf[BUFSIZ];
2398fae3551SRodney W. Grimes 
2408fae3551SRodney W. Grimes 	recno = 0;
2418fae3551SRodney W. Grimes 	if ( (fgets(tbuf, sizeof (tbuf), df)) != tbuf)
2428fae3551SRodney W. Grimes 		return(-1);
2438fae3551SRodney W. Grimes 	recno++;
2448fae3551SRodney W. Grimes 	if (makedumpdate(ddatep, tbuf) < 0)
2458fae3551SRodney W. Grimes 		msg("Unknown intermediate format in %s, line %d\n",
2468fae3551SRodney W. Grimes 			dumpdates, recno);
2478fae3551SRodney W. Grimes 
2488fae3551SRodney W. Grimes #ifdef FDEBUG
2498fae3551SRodney W. Grimes 	msg("getrecord: %s %c %s", ddatep->dd_name, ddatep->dd_level,
2508fae3551SRodney W. Grimes 	    ddatep->dd_ddate == 0 ? "the epoch\n" : ctime(&ddatep->dd_ddate));
2518fae3551SRodney W. Grimes #endif
2528fae3551SRodney W. Grimes 	return(0);
2538fae3551SRodney W. Grimes }
2548fae3551SRodney W. Grimes 
2558fae3551SRodney W. Grimes static int
2562db673abSWarner Losh makedumpdate(struct dumpdates *ddp, const char *tbuf)
2578fae3551SRodney W. Grimes {
2588fae3551SRodney W. Grimes 	char un_buf[128];
2598fae3551SRodney W. Grimes 
2608fae3551SRodney W. Grimes 	(void) sscanf(tbuf, DUMPINFMT, ddp->dd_name, &ddp->dd_level, un_buf);
2618fae3551SRodney W. Grimes 	ddp->dd_ddate = unctime(un_buf);
2628fae3551SRodney W. Grimes 	if (ddp->dd_ddate < 0)
2638fae3551SRodney W. Grimes 		return(-1);
2648fae3551SRodney W. Grimes 	return(0);
2658fae3551SRodney W. Grimes }
266