xref: /csrg-svn/sbin/dump/itime.c (revision 1422)
1*1422Sroot static	char *sccsid = "@(#)itime.c	1.1 (Berkeley) 10/13/80";
2*1422Sroot #include "dump.h"
3*1422Sroot 
4*1422Sroot char *prdate(d)
5*1422Sroot 	time_t d;
6*1422Sroot {
7*1422Sroot 	char *p;
8*1422Sroot 
9*1422Sroot 	if(d == 0)
10*1422Sroot 		return("the epoch");
11*1422Sroot 	p = ctime(&d);
12*1422Sroot 	p[24] = 0;
13*1422Sroot 	return(p);
14*1422Sroot }
15*1422Sroot 
16*1422Sroot struct	idates	**idatev = 0;
17*1422Sroot int	nidates = 0;
18*1422Sroot int	idates_in = 0;
19*1422Sroot struct	itime	*ithead = 0;
20*1422Sroot 
21*1422Sroot inititimes()
22*1422Sroot {
23*1422Sroot 			FILE	*df;
24*1422Sroot 	register	int	i;
25*1422Sroot 	register	struct	itime	*itwalk;
26*1422Sroot 
27*1422Sroot 	if (idates_in)
28*1422Sroot 		return;
29*1422Sroot 	if ( (df = fopen(increm, "r")) == NULL){
30*1422Sroot 		nidates = 0;
31*1422Sroot 		ithead = 0;
32*1422Sroot 	} else {
33*1422Sroot 		do{
34*1422Sroot 			itwalk=(struct itime *)calloc(1,sizeof (struct itime));
35*1422Sroot 			if (getrecord(df, &(itwalk->it_value)) < 0)
36*1422Sroot 				break;
37*1422Sroot 			nidates++;
38*1422Sroot 			itwalk->it_next = ithead;
39*1422Sroot 			ithead = itwalk;
40*1422Sroot 		} while (1);
41*1422Sroot 		fclose(df);
42*1422Sroot 	}
43*1422Sroot 
44*1422Sroot 	idates_in = 1;
45*1422Sroot 	/*
46*1422Sroot 	 *	arrayify the list, leaving enough room for the additional
47*1422Sroot 	 *	record that we may have to add to the idate structure
48*1422Sroot 	 */
49*1422Sroot 	idatev = (struct idates **)calloc(nidates + 1,sizeof (struct idates *));
50*1422Sroot 	for (i = nidates-1, itwalk = ithead; i >= 0; i--, itwalk = itwalk->it_next)
51*1422Sroot 		idatev[i] = &itwalk->it_value;
52*1422Sroot }
53*1422Sroot 
54*1422Sroot getitime()
55*1422Sroot {
56*1422Sroot 	register	struct	idates	*ip;
57*1422Sroot 	register	int	i;
58*1422Sroot 			char	*fname;
59*1422Sroot 
60*1422Sroot 	fname = disk;
61*1422Sroot #ifdef FDEBUG
62*1422Sroot 	msg("Looking for name %s in increm = %s for delta = %c\n",
63*1422Sroot 		fname, increm, incno);
64*1422Sroot #endif
65*1422Sroot 	spcl.c_ddate = 0;
66*1422Sroot 
67*1422Sroot 	inititimes();
68*1422Sroot 	/*
69*1422Sroot 	 *	Go find the entry with the same name for a lower increment
70*1422Sroot 	 *	and older date
71*1422Sroot 	 */
72*1422Sroot 	ITITERATE(i, ip){
73*1422Sroot 		if(strncmp(fname, ip->id_name,
74*1422Sroot 				sizeof (ip->id_name)) != 0)
75*1422Sroot 			continue;
76*1422Sroot 		if (ip->id_incno >= incno)
77*1422Sroot 			continue;
78*1422Sroot 		if (ip->id_ddate <= spcl.c_ddate)
79*1422Sroot 			continue;
80*1422Sroot 		spcl.c_ddate = ip->id_ddate;
81*1422Sroot 	}
82*1422Sroot }
83*1422Sroot 
84*1422Sroot putitime()
85*1422Sroot {
86*1422Sroot 	FILE		*df;
87*1422Sroot 	register	struct	idates	*itwalk;
88*1422Sroot 	register	int	i;
89*1422Sroot 	char		*fname;
90*1422Sroot 
91*1422Sroot 	if(uflag == 0)
92*1422Sroot 		return;
93*1422Sroot 	fname = disk;
94*1422Sroot 
95*1422Sroot 	spcl.c_ddate = 0;
96*1422Sroot 	ITITERATE(i, itwalk){
97*1422Sroot 		if (strncmp(fname, itwalk->id_name,
98*1422Sroot 				sizeof (itwalk->id_name)) != 0)
99*1422Sroot 			continue;
100*1422Sroot 		if (itwalk->id_incno != incno)
101*1422Sroot 			continue;
102*1422Sroot 		goto found;
103*1422Sroot 	}
104*1422Sroot 	/*
105*1422Sroot 	 *	construct the new upper bound;
106*1422Sroot 	 *	Enough room has been allocated.
107*1422Sroot 	 */
108*1422Sroot 	itwalk = idatev[nidates] =
109*1422Sroot 		(struct idates *)calloc(1, sizeof(struct idates));
110*1422Sroot 	nidates += 1;
111*1422Sroot   found:
112*1422Sroot 	strncpy(itwalk->id_name, fname, sizeof (itwalk->id_name));
113*1422Sroot 	itwalk->id_incno = incno;
114*1422Sroot 	itwalk->id_ddate = spcl.c_date;
115*1422Sroot 
116*1422Sroot 	if ( (df = fopen(increm, "w")) == NULL){
117*1422Sroot 		msg("Cannot open %s\n", increm);
118*1422Sroot 		dumpabort();
119*1422Sroot 	}
120*1422Sroot 	ITITERATE(i, itwalk){
121*1422Sroot 		recout(df, itwalk);
122*1422Sroot 	}
123*1422Sroot 	fclose(df);
124*1422Sroot 	msg("level %c dump on %s\n", incno, prdate(spcl.c_date));
125*1422Sroot }
126*1422Sroot 
127*1422Sroot recout(file, what)
128*1422Sroot 	FILE	*file;
129*1422Sroot 	struct	idates	*what;
130*1422Sroot {
131*1422Sroot 	fprintf(file, DUMPOUTFMT,
132*1422Sroot 		what->id_name,
133*1422Sroot 		what->id_incno,
134*1422Sroot 		ctime(&(what->id_ddate))
135*1422Sroot 	);
136*1422Sroot }
137*1422Sroot 
138*1422Sroot int	recno;
139*1422Sroot int getrecord(df, idatep)
140*1422Sroot 	FILE	*df;
141*1422Sroot 	struct	idates	*idatep;
142*1422Sroot {
143*1422Sroot 	char		buf[BUFSIZ];
144*1422Sroot 
145*1422Sroot 	recno = 0;
146*1422Sroot 	if ( (fgets(buf, BUFSIZ, df)) != buf)
147*1422Sroot 		return(-1);
148*1422Sroot 	recno++;
149*1422Sroot 	if (makeidate(idatep, buf) < 0)
150*1422Sroot 		msg("Unknown intermediate format in %s, line %d\n",
151*1422Sroot 			NINCREM, recno);
152*1422Sroot 
153*1422Sroot #ifdef FDEBUG
154*1422Sroot 	msg("getrecord: %s %c %s\n",
155*1422Sroot 		idatep->id_name, idatep->id_incno, prdate(idatep->id_ddate));
156*1422Sroot #endif
157*1422Sroot 	return(0);
158*1422Sroot }
159*1422Sroot 
160*1422Sroot /*
161*1422Sroot  *	Convert from old format to new format
162*1422Sroot  *	Convert from /etc/ddate to /etc/dumpdates format
163*1422Sroot  */
164*1422Sroot o_nconvert()
165*1422Sroot {
166*1422Sroot 	FILE	*oldfile;
167*1422Sroot 	FILE	*newfile;
168*1422Sroot 	struct	idates	idate;
169*1422Sroot 	struct	idates	idatecopy;
170*1422Sroot 
171*1422Sroot 	if( (newfile = fopen(NINCREM, "w")) == NULL){
172*1422Sroot 		msg("%s: Can not open %s to update.\n", processname, NINCREM);
173*1422Sroot 		Exit(X_ABORT);
174*1422Sroot 	}
175*1422Sroot 	if ( (oldfile = fopen(OINCREM, "r")) != NULL){
176*1422Sroot 		while(!feof(oldfile)){
177*1422Sroot 			if (fread(&idate, sizeof(idate), 1, oldfile) != 1)
178*1422Sroot 				break;
179*1422Sroot 			/*
180*1422Sroot 			 *	The old format ddate did not have
181*1422Sroot 			 *	the full special path name on it;
182*1422Sroot 			 *	we add the prefix /dev/ to the
183*1422Sroot 			 *	special name, although this may not be
184*1422Sroot 			 *	always the right thing to do.
185*1422Sroot 			 */
186*1422Sroot 			idatecopy = idate;
187*1422Sroot 			strcpy(idatecopy.id_name, "/dev/");
188*1422Sroot 			strncat(idatecopy.id_name, idate.id_name,
189*1422Sroot 				sizeof(idate.id_name) - sizeof ("/dev/"));
190*1422Sroot 			recout(newfile, &idatecopy);
191*1422Sroot 		}
192*1422Sroot 	}
193*1422Sroot 	fclose(oldfile);
194*1422Sroot 	fclose(newfile);
195*1422Sroot }
196*1422Sroot 
197*1422Sroot time_t	unctime();
198*1422Sroot 
199*1422Sroot int makeidate(ip, buf)
200*1422Sroot 	struct	idates	*ip;
201*1422Sroot 	char	*buf;
202*1422Sroot {
203*1422Sroot 	char	un_buf[128];
204*1422Sroot 
205*1422Sroot 	sscanf(buf, DUMPINFMT, ip->id_name, &ip->id_incno, un_buf);
206*1422Sroot 	ip->id_ddate = unctime(un_buf);
207*1422Sroot 	if (ip->id_ddate < 0)
208*1422Sroot 		return(-1);
209*1422Sroot 	return(0);
210*1422Sroot }
211*1422Sroot 
212*1422Sroot est(ip)
213*1422Sroot 	struct dinode *ip;
214*1422Sroot {
215*1422Sroot 	long s;
216*1422Sroot 
217*1422Sroot 	esize++;
218*1422Sroot 	s = (ip->di_size + BSIZE-1) / BSIZE;
219*1422Sroot 	esize += s;
220*1422Sroot 	if(s > NADDR-3) {
221*1422Sroot 		/*
222*1422Sroot 		 *	This code is only appproximate.
223*1422Sroot 		 *	it totally estimates low on doubly and triply indirect
224*1422Sroot 		 *	files.
225*1422Sroot 		 */
226*1422Sroot 		s -= NADDR-3;
227*1422Sroot 		s = (s + (BSIZE/sizeof(daddr_t))-1) / (BSIZE/sizeof(daddr_t));
228*1422Sroot 		esize += s;
229*1422Sroot 	}
230*1422Sroot }
231*1422Sroot 
232*1422Sroot bmapest(map)
233*1422Sroot short *map;
234*1422Sroot {
235*1422Sroot 	register i, n;
236*1422Sroot 
237*1422Sroot 	n = -1;
238*1422Sroot 	for(i=0; i<MSIZ; i++)
239*1422Sroot 		if(map[i])
240*1422Sroot 			n = i;
241*1422Sroot 	if(n < 0)
242*1422Sroot 		return;
243*1422Sroot 	esize++;
244*1422Sroot 	esize += (n + (BSIZE/sizeof(short))-1) / (BSIZE/sizeof(short));
245*1422Sroot }
246