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