xref: /csrg-svn/sbin/dump/itime.c (revision 12947)
1*12947Smckusick static	char *sccsid = "@(#)itime.c	1.10 (Berkeley) 06/09/83";
212589Smckusick 
31422Sroot #include "dump.h"
412589Smckusick #include <sys/file.h>
51422Sroot 
61422Sroot char *prdate(d)
71422Sroot 	time_t d;
81422Sroot {
91422Sroot 	char *p;
101422Sroot 
111422Sroot 	if(d == 0)
121422Sroot 		return("the epoch");
131422Sroot 	p = ctime(&d);
141422Sroot 	p[24] = 0;
151422Sroot 	return(p);
161422Sroot }
171422Sroot 
181422Sroot struct	idates	**idatev = 0;
191422Sroot int	nidates = 0;
201422Sroot int	idates_in = 0;
211422Sroot struct	itime	*ithead = 0;
221422Sroot 
231422Sroot inititimes()
241422Sroot {
251422Sroot 			FILE	*df;
261422Sroot 	register	int	i;
271422Sroot 	register	struct	itime	*itwalk;
2812589Smckusick 			int fd;
291422Sroot 
301422Sroot 	if (idates_in)
311422Sroot 		return;
3212589Smckusick 	if ((fd = open(increm, FSHLOCK|FRDONLY, 0600)) < 0) {
3312589Smckusick 		perror(increm);
3412589Smckusick 		return;
3512589Smckusick 	}
3612589Smckusick 	if ((df = fdopen(fd, "r")) == NULL) {
371422Sroot 		nidates = 0;
381422Sroot 		ithead = 0;
391422Sroot 	} else {
401422Sroot 		do{
411422Sroot 			itwalk=(struct itime *)calloc(1,sizeof (struct itime));
421422Sroot 			if (getrecord(df, &(itwalk->it_value)) < 0)
431422Sroot 				break;
441422Sroot 			nidates++;
451422Sroot 			itwalk->it_next = ithead;
461422Sroot 			ithead = itwalk;
471422Sroot 		} while (1);
481422Sroot 		fclose(df);
491422Sroot 	}
501422Sroot 
511422Sroot 	idates_in = 1;
521422Sroot 	/*
531422Sroot 	 *	arrayify the list, leaving enough room for the additional
541422Sroot 	 *	record that we may have to add to the idate structure
551422Sroot 	 */
561422Sroot 	idatev = (struct idates **)calloc(nidates + 1,sizeof (struct idates *));
571422Sroot 	for (i = nidates-1, itwalk = ithead; i >= 0; i--, itwalk = itwalk->it_next)
581422Sroot 		idatev[i] = &itwalk->it_value;
591422Sroot }
601422Sroot 
611422Sroot getitime()
621422Sroot {
631422Sroot 	register	struct	idates	*ip;
641422Sroot 	register	int	i;
651422Sroot 			char	*fname;
661422Sroot 
671422Sroot 	fname = disk;
681422Sroot #ifdef FDEBUG
691422Sroot 	msg("Looking for name %s in increm = %s for delta = %c\n",
701422Sroot 		fname, increm, incno);
711422Sroot #endif
721422Sroot 	spcl.c_ddate = 0;
731422Sroot 
741422Sroot 	inititimes();
751422Sroot 	/*
761422Sroot 	 *	Go find the entry with the same name for a lower increment
771422Sroot 	 *	and older date
781422Sroot 	 */
791422Sroot 	ITITERATE(i, ip){
801422Sroot 		if(strncmp(fname, ip->id_name,
811422Sroot 				sizeof (ip->id_name)) != 0)
821422Sroot 			continue;
831422Sroot 		if (ip->id_incno >= incno)
841422Sroot 			continue;
851422Sroot 		if (ip->id_ddate <= spcl.c_ddate)
861422Sroot 			continue;
871422Sroot 		spcl.c_ddate = ip->id_ddate;
88*12947Smckusick  		lastincno = ip->id_incno;
891422Sroot 	}
901422Sroot }
911422Sroot 
921422Sroot putitime()
931422Sroot {
941422Sroot 	FILE		*df;
951422Sroot 	register	struct	idates	*itwalk;
961422Sroot 	register	int	i;
9712589Smckusick 	int		fd;
981422Sroot 	char		*fname;
991422Sroot 
1001422Sroot 	if(uflag == 0)
1011422Sroot 		return;
10212589Smckusick 	if ((fd = open(temp, FCREATE|FEXLOCK|FRDWR, 0600)) < 0) {
10312589Smckusick 		perror(temp);
10412589Smckusick 		dumpabort();
10512589Smckusick 	}
10612589Smckusick 	if ((df = fdopen(fd, "w")) == NULL) {
10712589Smckusick 		perror(temp);
10812589Smckusick 		dumpabort();
10912589Smckusick 	}
1101422Sroot 	fname = disk;
11112584Smckusick 	free(idatev);
11212584Smckusick 	idatev = 0;
11312584Smckusick 	nidates = 0;
11412584Smckusick 	ithead = 0;
11512584Smckusick 	idates_in = 0;
11612584Smckusick 	inititimes();
1171422Sroot 
1181422Sroot 	spcl.c_ddate = 0;
1191422Sroot 	ITITERATE(i, itwalk){
1201422Sroot 		if (strncmp(fname, itwalk->id_name,
1211422Sroot 				sizeof (itwalk->id_name)) != 0)
1221422Sroot 			continue;
1231422Sroot 		if (itwalk->id_incno != incno)
1241422Sroot 			continue;
1251422Sroot 		goto found;
1261422Sroot 	}
1271422Sroot 	/*
1281422Sroot 	 *	construct the new upper bound;
1291422Sroot 	 *	Enough room has been allocated.
1301422Sroot 	 */
1311422Sroot 	itwalk = idatev[nidates] =
1321422Sroot 		(struct idates *)calloc(1, sizeof(struct idates));
1331422Sroot 	nidates += 1;
1341422Sroot   found:
1351422Sroot 	strncpy(itwalk->id_name, fname, sizeof (itwalk->id_name));
1361422Sroot 	itwalk->id_incno = incno;
1371422Sroot 	itwalk->id_ddate = spcl.c_date;
1381422Sroot 
1391422Sroot 	ITITERATE(i, itwalk){
1401422Sroot 		recout(df, itwalk);
1411422Sroot 	}
14212589Smckusick 	if (rename(temp, increm) < 0) {
14312589Smckusick 		perror("rename");
14412589Smckusick 		(void) unlink(temp);
14512589Smckusick 		dumpabort();
14612589Smckusick 	}
14712589Smckusick 	(void) chmod(increm, 0644);
14812589Smckusick 	(void) fclose(df);
1491422Sroot 	msg("level %c dump on %s\n", incno, prdate(spcl.c_date));
1501422Sroot }
1511422Sroot 
1521422Sroot recout(file, what)
1531422Sroot 	FILE	*file;
1541422Sroot 	struct	idates	*what;
1551422Sroot {
1561422Sroot 	fprintf(file, DUMPOUTFMT,
1571422Sroot 		what->id_name,
1581422Sroot 		what->id_incno,
1591422Sroot 		ctime(&(what->id_ddate))
1601422Sroot 	);
1611422Sroot }
1621422Sroot 
1631422Sroot int	recno;
1641422Sroot int getrecord(df, idatep)
1651422Sroot 	FILE	*df;
1661422Sroot 	struct	idates	*idatep;
1671422Sroot {
1681422Sroot 	char		buf[BUFSIZ];
1691422Sroot 
1701422Sroot 	recno = 0;
1711422Sroot 	if ( (fgets(buf, BUFSIZ, df)) != buf)
1721422Sroot 		return(-1);
1731422Sroot 	recno++;
1741422Sroot 	if (makeidate(idatep, buf) < 0)
1751422Sroot 		msg("Unknown intermediate format in %s, line %d\n",
17612589Smckusick 			increm, recno);
1771422Sroot 
1781422Sroot #ifdef FDEBUG
1791422Sroot 	msg("getrecord: %s %c %s\n",
1801422Sroot 		idatep->id_name, idatep->id_incno, prdate(idatep->id_ddate));
1811422Sroot #endif
1821422Sroot 	return(0);
1831422Sroot }
1841422Sroot 
1851422Sroot time_t	unctime();
1861422Sroot 
1871422Sroot int makeidate(ip, buf)
1881422Sroot 	struct	idates	*ip;
1891422Sroot 	char	*buf;
1901422Sroot {
1911422Sroot 	char	un_buf[128];
1921422Sroot 
1931422Sroot 	sscanf(buf, DUMPINFMT, ip->id_name, &ip->id_incno, un_buf);
1941422Sroot 	ip->id_ddate = unctime(un_buf);
1951422Sroot 	if (ip->id_ddate < 0)
1961422Sroot 		return(-1);
1971422Sroot 	return(0);
1981422Sroot }
1991422Sroot 
2004701Smckusic /*
2015328Smckusic  * This is an estimation of the number of TP_BSIZE blocks in the file.
2024701Smckusic  * It assumes that there are no unallocated blocks; hence
2034701Smckusic  * the estimate may be high
2044701Smckusic  */
2051422Sroot est(ip)
2061422Sroot 	struct dinode *ip;
2071422Sroot {
2081422Sroot 	long s;
2091422Sroot 
2101422Sroot 	esize++;
2115328Smckusic 	/* calc number of TP_BSIZE blocks */
2129403Smckusick 	s = howmany(ip->di_size, TP_BSIZE);
2135328Smckusic 	if (ip->di_size > sblock->fs_bsize * NDADDR) {
2145328Smckusic 		/* calc number of indirect blocks on the dump tape */
2158536Smckusick 		s += howmany(s - NDADDR * sblock->fs_bsize / TP_BSIZE,
2165328Smckusic 			TP_NINDIR);
2175328Smckusic 	}
2181422Sroot 	esize += s;
2191422Sroot }
2201422Sroot 
2211422Sroot bmapest(map)
2225328Smckusic 	char *map;
2231422Sroot {
2241422Sroot 	register i, n;
2251422Sroot 
2261422Sroot 	n = -1;
2275328Smckusic 	for (i = 0; i < msiz; i++)
2281422Sroot 		if(map[i])
2291422Sroot 			n = i;
2301422Sroot 	if(n < 0)
2311422Sroot 		return;
2325328Smckusic 	n++;
2331422Sroot 	esize++;
2345328Smckusic 	esize += howmany(n * sizeof map[0], TP_BSIZE);
2351422Sroot }
236