itime.c revision 98542
11558Srgrimes/*-
21558Srgrimes * Copyright (c) 1980, 1993
31558Srgrimes *	The Regents of the University of California.  All rights reserved.
41558Srgrimes *
51558Srgrimes * Redistribution and use in source and binary forms, with or without
61558Srgrimes * modification, are permitted provided that the following conditions
71558Srgrimes * are met:
81558Srgrimes * 1. Redistributions of source code must retain the above copyright
91558Srgrimes *    notice, this list of conditions and the following disclaimer.
101558Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111558Srgrimes *    notice, this list of conditions and the following disclaimer in the
121558Srgrimes *    documentation and/or other materials provided with the distribution.
131558Srgrimes * 3. All advertising materials mentioning features or use of this software
141558Srgrimes *    must display the following acknowledgement:
151558Srgrimes *	This product includes software developed by the University of
161558Srgrimes *	California, Berkeley and its contributors.
171558Srgrimes * 4. Neither the name of the University nor the names of its contributors
181558Srgrimes *    may be used to endorse or promote products derived from this software
191558Srgrimes *    without specific prior written permission.
201558Srgrimes *
211558Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
221558Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
231558Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
241558Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
251558Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261558Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271558Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281558Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291558Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301558Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311558Srgrimes * SUCH DAMAGE.
321558Srgrimes */
331558Srgrimes
341558Srgrimes#ifndef lint
3536997Scharnier#if 0
361558Srgrimesstatic char sccsid[] = "@(#)itime.c	8.1 (Berkeley) 6/5/93";
3736997Scharnier#endif
3836997Scharnierstatic const char rcsid[] =
3950476Speter  "$FreeBSD: head/sbin/dump/itime.c 98542 2002-06-21 06:18:05Z mckusick $";
401558Srgrimes#endif /* not lint */
411558Srgrimes
421558Srgrimes#include <sys/param.h>
4371787Sphk#include <sys/queue.h>
441558Srgrimes#include <sys/time.h>
451558Srgrimes
461558Srgrimes#include <ufs/ufs/dinode.h>
471558Srgrimes
481558Srgrimes#include <protocols/dumprestore.h>
491558Srgrimes
501558Srgrimes#include <errno.h>
511558Srgrimes#include <fcntl.h>
521558Srgrimes#include <stdio.h>
531558Srgrimes#include <stdlib.h>
541558Srgrimes#include <string.h>
551558Srgrimes
561558Srgrimes#include "dump.h"
571558Srgrimes
5871787Sphkstruct dumptime {
5971787Sphk	struct	dumpdates dt_value;
6071787Sphk	SLIST_ENTRY(dumptime) dt_list;
6171787Sphk};
6271787SphkSLIST_HEAD(dthead, dumptime) dthead = SLIST_HEAD_INITIALIZER(dthead);
631558Srgrimesstruct	dumpdates **ddatev = 0;
641558Srgrimesint	nddates = 0;
651558Srgrimesint	ddates_in = 0;
661558Srgrimes
6792837Simpstatic	void dumprecout(FILE *, const struct dumpdates *);
6892837Simpstatic	int getrecord(FILE *, struct dumpdates *);
6992837Simpstatic	int makedumpdate(struct dumpdates *, const char *);
7092837Simpstatic	void readdumptimes(FILE *);
711558Srgrimes
721558Srgrimesvoid
7392837Simpinitdumptimes(void)
741558Srgrimes{
751558Srgrimes	FILE *df;
761558Srgrimes
771558Srgrimes	if ((df = fopen(dumpdates, "r")) == NULL) {
781558Srgrimes		if (errno != ENOENT) {
7974873Sobrien			msg("WARNING: cannot read %s: %s\n", dumpdates,
801558Srgrimes			    strerror(errno));
8174872Sobrien			return;
821558Srgrimes		}
831558Srgrimes		/*
841558Srgrimes		 * Dumpdates does not exist, make an empty one.
851558Srgrimes		 */
861558Srgrimes		msg("WARNING: no file `%s', making an empty one\n", dumpdates);
871558Srgrimes		if ((df = fopen(dumpdates, "w")) == NULL) {
8874873Sobrien			msg("WARNING: cannot create %s: %s\n", dumpdates,
891558Srgrimes			    strerror(errno));
9074872Sobrien			return;
911558Srgrimes		}
921558Srgrimes		(void) fclose(df);
931558Srgrimes		if ((df = fopen(dumpdates, "r")) == NULL) {
941558Srgrimes			quit("cannot read %s even after creating it: %s\n",
951558Srgrimes			    dumpdates, strerror(errno));
961558Srgrimes			/* NOTREACHED */
971558Srgrimes		}
981558Srgrimes	}
991558Srgrimes	(void) flock(fileno(df), LOCK_SH);
1001558Srgrimes	readdumptimes(df);
1011558Srgrimes	(void) fclose(df);
1021558Srgrimes}
1031558Srgrimes
1041558Srgrimesstatic void
10592837Simpreaddumptimes(FILE *df)
1061558Srgrimes{
10786473Siedowse	int i;
10886473Siedowse	struct	dumptime *dtwalk;
1091558Srgrimes
1101558Srgrimes	for (;;) {
1111558Srgrimes		dtwalk = (struct dumptime *)calloc(1, sizeof (struct dumptime));
1121558Srgrimes		if (getrecord(df, &(dtwalk->dt_value)) < 0)
1131558Srgrimes			break;
1141558Srgrimes		nddates++;
11571787Sphk		SLIST_INSERT_HEAD(&dthead, dtwalk, dt_list);
1161558Srgrimes	}
1171558Srgrimes
1181558Srgrimes	ddates_in = 1;
1191558Srgrimes	/*
1201558Srgrimes	 *	arrayify the list, leaving enough room for the additional
1211558Srgrimes	 *	record that we may have to add to the ddate structure
1221558Srgrimes	 */
1231558Srgrimes	ddatev = (struct dumpdates **)
1241558Srgrimes		calloc((unsigned) (nddates + 1), sizeof (struct dumpdates *));
12571787Sphk	dtwalk = SLIST_FIRST(&dthead);
12671787Sphk	for (i = nddates - 1; i >= 0; i--, dtwalk = SLIST_NEXT(dtwalk, dt_list))
1271558Srgrimes		ddatev[i] = &dtwalk->dt_value;
1281558Srgrimes}
1291558Srgrimes
1301558Srgrimesvoid
13192837Simpgetdumptime(void)
1321558Srgrimes{
13386473Siedowse	struct dumpdates *ddp;
13486473Siedowse	int i;
1351558Srgrimes	char *fname;
1361558Srgrimes
1371558Srgrimes	fname = disk;
1381558Srgrimes#ifdef FDEBUG
1391558Srgrimes	msg("Looking for name %s in dumpdates = %s for level = %c\n",
1401558Srgrimes		fname, dumpdates, level);
1411558Srgrimes#endif
1421558Srgrimes	spcl.c_ddate = 0;
1431558Srgrimes	lastlevel = '0';
1441558Srgrimes
1451558Srgrimes	initdumptimes();
1461558Srgrimes	/*
1471558Srgrimes	 *	Go find the entry with the same name for a lower increment
1481558Srgrimes	 *	and older date
1491558Srgrimes	 */
1501558Srgrimes	ITITERATE(i, ddp) {
1511558Srgrimes		if (strncmp(fname, ddp->dd_name, sizeof (ddp->dd_name)) != 0)
1521558Srgrimes			continue;
1531558Srgrimes		if (ddp->dd_level >= level)
1541558Srgrimes			continue;
15598542Smckusick		if (ddp->dd_ddate <= _time64_to_time(spcl.c_ddate))
1561558Srgrimes			continue;
15798542Smckusick		spcl.c_ddate = _time_to_time64(ddp->dd_ddate);
1581558Srgrimes		lastlevel = ddp->dd_level;
1591558Srgrimes	}
1601558Srgrimes}
1611558Srgrimes
1621558Srgrimesvoid
16392837Simpputdumptime(void)
1641558Srgrimes{
1651558Srgrimes	FILE *df;
16686473Siedowse	struct dumpdates *dtwalk;
16786473Siedowse	int i;
1681558Srgrimes	int fd;
1691558Srgrimes	char *fname;
17085635Sdillon	char *tmsg;
1711558Srgrimes
1721558Srgrimes	if(uflag == 0)
1731558Srgrimes		return;
1741558Srgrimes	if ((df = fopen(dumpdates, "r+")) == NULL)
1751558Srgrimes		quit("cannot rewrite %s: %s\n", dumpdates, strerror(errno));
1761558Srgrimes	fd = fileno(df);
1771558Srgrimes	(void) flock(fd, LOCK_EX);
1781558Srgrimes	fname = disk;
1791558Srgrimes	free((char *)ddatev);
1801558Srgrimes	ddatev = 0;
1811558Srgrimes	nddates = 0;
1821558Srgrimes	ddates_in = 0;
1831558Srgrimes	readdumptimes(df);
1841558Srgrimes	if (fseek(df, 0L, 0) < 0)
1851558Srgrimes		quit("fseek: %s\n", strerror(errno));
1861558Srgrimes	spcl.c_ddate = 0;
1871558Srgrimes	ITITERATE(i, dtwalk) {
1881558Srgrimes		if (strncmp(fname, dtwalk->dd_name,
1891558Srgrimes				sizeof (dtwalk->dd_name)) != 0)
1901558Srgrimes			continue;
1911558Srgrimes		if (dtwalk->dd_level != level)
1921558Srgrimes			continue;
1931558Srgrimes		goto found;
1941558Srgrimes	}
1951558Srgrimes	/*
1961558Srgrimes	 *	construct the new upper bound;
1971558Srgrimes	 *	Enough room has been allocated.
1981558Srgrimes	 */
1991558Srgrimes	dtwalk = ddatev[nddates] =
2001558Srgrimes		(struct dumpdates *)calloc(1, sizeof (struct dumpdates));
2011558Srgrimes	nddates += 1;
2021558Srgrimes  found:
2031558Srgrimes	(void) strncpy(dtwalk->dd_name, fname, sizeof (dtwalk->dd_name));
2041558Srgrimes	dtwalk->dd_level = level;
20598542Smckusick	dtwalk->dd_ddate = _time64_to_time(spcl.c_date);
2061558Srgrimes
2071558Srgrimes	ITITERATE(i, dtwalk) {
2081558Srgrimes		dumprecout(df, dtwalk);
2091558Srgrimes	}
2101558Srgrimes	if (fflush(df))
2111558Srgrimes		quit("%s: %s\n", dumpdates, strerror(errno));
2121558Srgrimes	if (ftruncate(fd, ftell(df)))
2131558Srgrimes		quit("ftruncate (%s): %s\n", dumpdates, strerror(errno));
2141558Srgrimes	(void) fclose(df);
21585635Sdillon	if (spcl.c_date == 0) {
21685635Sdillon		tmsg = "the epoch\n";
21785635Sdillon	} else {
21898542Smckusick		time_t t = _time64_to_time(spcl.c_date);
21985635Sdillon		tmsg = ctime(&t);
22085635Sdillon	}
22185635Sdillon	msg("level %c dump on %s", level, tmsg);
2221558Srgrimes}
2231558Srgrimes
2241558Srgrimesstatic void
22592837Simpdumprecout(FILE *file, const struct dumpdates *what)
2261558Srgrimes{
2271558Srgrimes
22892837Simp	if (fprintf(file, DUMPOUTFMT, what->dd_name,
22992837Simp	      what->dd_level, ctime(&what->dd_ddate)) < 0)
2301558Srgrimes		quit("%s: %s\n", dumpdates, strerror(errno));
2311558Srgrimes}
2321558Srgrimes
2331558Srgrimesint	recno;
2341558Srgrimes
2351558Srgrimesstatic int
23692837Simpgetrecord(FILE *df, struct dumpdates *ddatep)
2371558Srgrimes{
2381558Srgrimes	char tbuf[BUFSIZ];
2391558Srgrimes
2401558Srgrimes	recno = 0;
2411558Srgrimes	if ( (fgets(tbuf, sizeof (tbuf), df)) != tbuf)
2421558Srgrimes		return(-1);
2431558Srgrimes	recno++;
2441558Srgrimes	if (makedumpdate(ddatep, tbuf) < 0)
2451558Srgrimes		msg("Unknown intermediate format in %s, line %d\n",
2461558Srgrimes			dumpdates, recno);
2471558Srgrimes
2481558Srgrimes#ifdef FDEBUG
2491558Srgrimes	msg("getrecord: %s %c %s", ddatep->dd_name, ddatep->dd_level,
2501558Srgrimes	    ddatep->dd_ddate == 0 ? "the epoch\n" : ctime(&ddatep->dd_ddate));
2511558Srgrimes#endif
2521558Srgrimes	return(0);
2531558Srgrimes}
2541558Srgrimes
2551558Srgrimesstatic int
25692837Simpmakedumpdate(struct dumpdates *ddp, const char *tbuf)
2571558Srgrimes{
2581558Srgrimes	char un_buf[128];
2591558Srgrimes
2601558Srgrimes	(void) sscanf(tbuf, DUMPINFMT, ddp->dd_name, &ddp->dd_level, un_buf);
2611558Srgrimes	ddp->dd_ddate = unctime(un_buf);
2621558Srgrimes	if (ddp->dd_ddate < 0)
2631558Srgrimes		return(-1);
2641558Srgrimes	return(0);
2651558Srgrimes}
266