itime.c revision 50476
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 50476 1999-08-28 00:22:10Z peter $";
401558Srgrimes#endif /* not lint */
411558Srgrimes
421558Srgrimes#include <sys/param.h>
431558Srgrimes#include <sys/time.h>
441558Srgrimes#ifdef sunos
451558Srgrimes#include <sys/vnode.h>
461558Srgrimes
471558Srgrimes#include <ufs/fsdir.h>
481558Srgrimes#include <ufs/inode.h>
491558Srgrimes#include <ufs/fs.h>
501558Srgrimes#else
511558Srgrimes#include <ufs/ufs/dinode.h>
521558Srgrimes#endif
531558Srgrimes
541558Srgrimes#include <protocols/dumprestore.h>
551558Srgrimes
561558Srgrimes#include <errno.h>
571558Srgrimes#include <fcntl.h>
581558Srgrimes#include <stdio.h>
591558Srgrimes#ifdef __STDC__
601558Srgrimes#include <stdlib.h>
611558Srgrimes#include <string.h>
621558Srgrimes#endif
631558Srgrimes
641558Srgrimes#include "dump.h"
651558Srgrimes
661558Srgrimesstruct	dumpdates **ddatev = 0;
671558Srgrimesint	nddates = 0;
681558Srgrimesint	ddates_in = 0;
691558Srgrimesstruct	dumptime *dthead = 0;
701558Srgrimes
711558Srgrimesstatic	void dumprecout __P((FILE *, struct dumpdates *));
721558Srgrimesstatic	int getrecord __P((FILE *, struct dumpdates *));
731558Srgrimesstatic	int makedumpdate __P((struct dumpdates *, char *));
741558Srgrimesstatic	void readdumptimes __P((FILE *));
751558Srgrimes
761558Srgrimesvoid
771558Srgrimesinitdumptimes()
781558Srgrimes{
791558Srgrimes	FILE *df;
801558Srgrimes
811558Srgrimes	if ((df = fopen(dumpdates, "r")) == NULL) {
821558Srgrimes		if (errno != ENOENT) {
831558Srgrimes			quit("cannot read %s: %s\n", dumpdates,
841558Srgrimes			    strerror(errno));
851558Srgrimes			/* NOTREACHED */
861558Srgrimes		}
871558Srgrimes		/*
881558Srgrimes		 * Dumpdates does not exist, make an empty one.
891558Srgrimes		 */
901558Srgrimes		msg("WARNING: no file `%s', making an empty one\n", dumpdates);
911558Srgrimes		if ((df = fopen(dumpdates, "w")) == NULL) {
921558Srgrimes			quit("cannot create %s: %s\n", dumpdates,
931558Srgrimes			    strerror(errno));
941558Srgrimes			/* NOTREACHED */
951558Srgrimes		}
961558Srgrimes		(void) fclose(df);
971558Srgrimes		if ((df = fopen(dumpdates, "r")) == NULL) {
981558Srgrimes			quit("cannot read %s even after creating it: %s\n",
991558Srgrimes			    dumpdates, strerror(errno));
1001558Srgrimes			/* NOTREACHED */
1011558Srgrimes		}
1021558Srgrimes	}
1031558Srgrimes	(void) flock(fileno(df), LOCK_SH);
1041558Srgrimes	readdumptimes(df);
1051558Srgrimes	(void) fclose(df);
1061558Srgrimes}
1071558Srgrimes
1081558Srgrimesstatic void
1091558Srgrimesreaddumptimes(df)
1101558Srgrimes	FILE *df;
1111558Srgrimes{
1121558Srgrimes	register int i;
1131558Srgrimes	register struct	dumptime *dtwalk;
1141558Srgrimes
1151558Srgrimes	for (;;) {
1161558Srgrimes		dtwalk = (struct dumptime *)calloc(1, sizeof (struct dumptime));
1171558Srgrimes		if (getrecord(df, &(dtwalk->dt_value)) < 0)
1181558Srgrimes			break;
1191558Srgrimes		nddates++;
1201558Srgrimes		dtwalk->dt_next = dthead;
1211558Srgrimes		dthead = dtwalk;
1221558Srgrimes	}
1231558Srgrimes
1241558Srgrimes	ddates_in = 1;
1251558Srgrimes	/*
1261558Srgrimes	 *	arrayify the list, leaving enough room for the additional
1271558Srgrimes	 *	record that we may have to add to the ddate structure
1281558Srgrimes	 */
1291558Srgrimes	ddatev = (struct dumpdates **)
1301558Srgrimes		calloc((unsigned) (nddates + 1), sizeof (struct dumpdates *));
1311558Srgrimes	dtwalk = dthead;
1321558Srgrimes	for (i = nddates - 1; i >= 0; i--, dtwalk = dtwalk->dt_next)
1331558Srgrimes		ddatev[i] = &dtwalk->dt_value;
1341558Srgrimes}
1351558Srgrimes
1361558Srgrimesvoid
1371558Srgrimesgetdumptime()
1381558Srgrimes{
1391558Srgrimes	register struct dumpdates *ddp;
1401558Srgrimes	register int i;
1411558Srgrimes	char *fname;
1421558Srgrimes
1431558Srgrimes	fname = disk;
1441558Srgrimes#ifdef FDEBUG
1451558Srgrimes	msg("Looking for name %s in dumpdates = %s for level = %c\n",
1461558Srgrimes		fname, dumpdates, level);
1471558Srgrimes#endif
1481558Srgrimes	spcl.c_ddate = 0;
1491558Srgrimes	lastlevel = '0';
1501558Srgrimes
1511558Srgrimes	initdumptimes();
1521558Srgrimes	/*
1531558Srgrimes	 *	Go find the entry with the same name for a lower increment
1541558Srgrimes	 *	and older date
1551558Srgrimes	 */
1561558Srgrimes	ITITERATE(i, ddp) {
1571558Srgrimes		if (strncmp(fname, ddp->dd_name, sizeof (ddp->dd_name)) != 0)
1581558Srgrimes			continue;
1591558Srgrimes		if (ddp->dd_level >= level)
1601558Srgrimes			continue;
1611558Srgrimes		if (ddp->dd_ddate <= spcl.c_ddate)
1621558Srgrimes			continue;
1631558Srgrimes		spcl.c_ddate = ddp->dd_ddate;
1641558Srgrimes		lastlevel = ddp->dd_level;
1651558Srgrimes	}
1661558Srgrimes}
1671558Srgrimes
1681558Srgrimesvoid
1691558Srgrimesputdumptime()
1701558Srgrimes{
1711558Srgrimes	FILE *df;
1721558Srgrimes	register struct dumpdates *dtwalk;
1731558Srgrimes	register int i;
1741558Srgrimes	int fd;
1751558Srgrimes	char *fname;
1761558Srgrimes
1771558Srgrimes	if(uflag == 0)
1781558Srgrimes		return;
1791558Srgrimes	if ((df = fopen(dumpdates, "r+")) == NULL)
1801558Srgrimes		quit("cannot rewrite %s: %s\n", dumpdates, strerror(errno));
1811558Srgrimes	fd = fileno(df);
1821558Srgrimes	(void) flock(fd, LOCK_EX);
1831558Srgrimes	fname = disk;
1841558Srgrimes	free((char *)ddatev);
1851558Srgrimes	ddatev = 0;
1861558Srgrimes	nddates = 0;
1871558Srgrimes	dthead = 0;
1881558Srgrimes	ddates_in = 0;
1891558Srgrimes	readdumptimes(df);
1901558Srgrimes	if (fseek(df, 0L, 0) < 0)
1911558Srgrimes		quit("fseek: %s\n", strerror(errno));
1921558Srgrimes	spcl.c_ddate = 0;
1931558Srgrimes	ITITERATE(i, dtwalk) {
1941558Srgrimes		if (strncmp(fname, dtwalk->dd_name,
1951558Srgrimes				sizeof (dtwalk->dd_name)) != 0)
1961558Srgrimes			continue;
1971558Srgrimes		if (dtwalk->dd_level != level)
1981558Srgrimes			continue;
1991558Srgrimes		goto found;
2001558Srgrimes	}
2011558Srgrimes	/*
2021558Srgrimes	 *	construct the new upper bound;
2031558Srgrimes	 *	Enough room has been allocated.
2041558Srgrimes	 */
2051558Srgrimes	dtwalk = ddatev[nddates] =
2061558Srgrimes		(struct dumpdates *)calloc(1, sizeof (struct dumpdates));
2071558Srgrimes	nddates += 1;
2081558Srgrimes  found:
2091558Srgrimes	(void) strncpy(dtwalk->dd_name, fname, sizeof (dtwalk->dd_name));
2101558Srgrimes	dtwalk->dd_level = level;
2111558Srgrimes	dtwalk->dd_ddate = spcl.c_date;
2121558Srgrimes
2131558Srgrimes	ITITERATE(i, dtwalk) {
2141558Srgrimes		dumprecout(df, dtwalk);
2151558Srgrimes	}
2161558Srgrimes	if (fflush(df))
2171558Srgrimes		quit("%s: %s\n", dumpdates, strerror(errno));
2181558Srgrimes	if (ftruncate(fd, ftell(df)))
2191558Srgrimes		quit("ftruncate (%s): %s\n", dumpdates, strerror(errno));
2201558Srgrimes	(void) fclose(df);
2211558Srgrimes	msg("level %c dump on %s", level,
2221558Srgrimes		spcl.c_date == 0 ? "the epoch\n" : ctime(&spcl.c_date));
2231558Srgrimes}
2241558Srgrimes
2251558Srgrimesstatic void
2261558Srgrimesdumprecout(file, what)
2271558Srgrimes	FILE *file;
2281558Srgrimes	struct dumpdates *what;
2291558Srgrimes{
2301558Srgrimes
2311558Srgrimes	if (fprintf(file, DUMPOUTFMT,
2321558Srgrimes		    what->dd_name,
2331558Srgrimes		    what->dd_level,
2341558Srgrimes		    ctime(&what->dd_ddate)) < 0)
2351558Srgrimes		quit("%s: %s\n", dumpdates, strerror(errno));
2361558Srgrimes}
2371558Srgrimes
2381558Srgrimesint	recno;
2391558Srgrimes
2401558Srgrimesstatic int
2411558Srgrimesgetrecord(df, ddatep)
2421558Srgrimes	FILE *df;
2431558Srgrimes	struct dumpdates *ddatep;
2441558Srgrimes{
2451558Srgrimes	char tbuf[BUFSIZ];
2461558Srgrimes
2471558Srgrimes	recno = 0;
2481558Srgrimes	if ( (fgets(tbuf, sizeof (tbuf), df)) != tbuf)
2491558Srgrimes		return(-1);
2501558Srgrimes	recno++;
2511558Srgrimes	if (makedumpdate(ddatep, tbuf) < 0)
2521558Srgrimes		msg("Unknown intermediate format in %s, line %d\n",
2531558Srgrimes			dumpdates, recno);
2541558Srgrimes
2551558Srgrimes#ifdef FDEBUG
2561558Srgrimes	msg("getrecord: %s %c %s", ddatep->dd_name, ddatep->dd_level,
2571558Srgrimes	    ddatep->dd_ddate == 0 ? "the epoch\n" : ctime(&ddatep->dd_ddate));
2581558Srgrimes#endif
2591558Srgrimes	return(0);
2601558Srgrimes}
2611558Srgrimes
2621558Srgrimesstatic int
2631558Srgrimesmakedumpdate(ddp, tbuf)
2641558Srgrimes	struct dumpdates *ddp;
2651558Srgrimes	char *tbuf;
2661558Srgrimes{
2671558Srgrimes	char un_buf[128];
2681558Srgrimes
2691558Srgrimes	(void) sscanf(tbuf, DUMPINFMT, ddp->dd_name, &ddp->dd_level, un_buf);
2701558Srgrimes	ddp->dd_ddate = unctime(un_buf);
2711558Srgrimes	if (ddp->dd_ddate < 0)
2721558Srgrimes		return(-1);
2731558Srgrimes	return(0);
2741558Srgrimes}
275