1*22027Sdist /*
2*22027Sdist * Copyright (c) 1980 Regents of the University of California.
3*22027Sdist * All rights reserved. The Berkeley software License Agreement
4*22027Sdist * specifies the terms and conditions for redistribution.
5*22027Sdist */
6*22027Sdist
7*22027Sdist #ifndef lint
8*22027Sdist static char sccsid[] = "@(#)dumpitime.c 5.1 (Berkeley) 06/05/85";
9*22027Sdist #endif not lint
10*22027Sdist
111422Sroot #include "dump.h"
121422Sroot
prdate(d)131422Sroot char *prdate(d)
141422Sroot time_t d;
151422Sroot {
161422Sroot char *p;
171422Sroot
181422Sroot if(d == 0)
191422Sroot return("the epoch");
201422Sroot p = ctime(&d);
211422Sroot p[24] = 0;
221422Sroot return(p);
231422Sroot }
241422Sroot
251422Sroot struct idates **idatev = 0;
261422Sroot int nidates = 0;
271422Sroot int idates_in = 0;
281422Sroot struct itime *ithead = 0;
291422Sroot
inititimes()301422Sroot inititimes()
311422Sroot {
321422Sroot FILE *df;
331422Sroot register int i;
341422Sroot register struct itime *itwalk;
351422Sroot
361422Sroot if (idates_in)
371422Sroot return;
381422Sroot if ( (df = fopen(increm, "r")) == NULL){
391422Sroot nidates = 0;
401422Sroot ithead = 0;
411422Sroot } else {
421422Sroot do{
431422Sroot itwalk=(struct itime *)calloc(1,sizeof (struct itime));
441422Sroot if (getrecord(df, &(itwalk->it_value)) < 0)
451422Sroot break;
461422Sroot nidates++;
471422Sroot itwalk->it_next = ithead;
481422Sroot ithead = itwalk;
491422Sroot } while (1);
501422Sroot fclose(df);
511422Sroot }
521422Sroot
531422Sroot idates_in = 1;
541422Sroot /*
551422Sroot * arrayify the list, leaving enough room for the additional
561422Sroot * record that we may have to add to the idate structure
571422Sroot */
581422Sroot idatev = (struct idates **)calloc(nidates + 1,sizeof (struct idates *));
591422Sroot for (i = nidates-1, itwalk = ithead; i >= 0; i--, itwalk = itwalk->it_next)
601422Sroot idatev[i] = &itwalk->it_value;
611422Sroot }
621422Sroot
getitime()631422Sroot getitime()
641422Sroot {
651422Sroot register struct idates *ip;
661422Sroot register int i;
671422Sroot char *fname;
681422Sroot
691422Sroot fname = disk;
701422Sroot #ifdef FDEBUG
711422Sroot msg("Looking for name %s in increm = %s for delta = %c\n",
721422Sroot fname, increm, incno);
731422Sroot #endif
741422Sroot spcl.c_ddate = 0;
751422Sroot
761422Sroot inititimes();
771422Sroot /*
781422Sroot * Go find the entry with the same name for a lower increment
791422Sroot * and older date
801422Sroot */
811422Sroot ITITERATE(i, ip){
821422Sroot if(strncmp(fname, ip->id_name,
831422Sroot sizeof (ip->id_name)) != 0)
841422Sroot continue;
851422Sroot if (ip->id_incno >= incno)
861422Sroot continue;
871422Sroot if (ip->id_ddate <= spcl.c_ddate)
881422Sroot continue;
891422Sroot spcl.c_ddate = ip->id_ddate;
901422Sroot }
911422Sroot }
921422Sroot
putitime()931422Sroot putitime()
941422Sroot {
951422Sroot FILE *df;
961422Sroot register struct idates *itwalk;
971422Sroot register int i;
981422Sroot char *fname;
991422Sroot
1001422Sroot if(uflag == 0)
1011422Sroot return;
1021422Sroot fname = disk;
1031422Sroot
1041422Sroot spcl.c_ddate = 0;
1051422Sroot ITITERATE(i, itwalk){
1061422Sroot if (strncmp(fname, itwalk->id_name,
1071422Sroot sizeof (itwalk->id_name)) != 0)
1081422Sroot continue;
1091422Sroot if (itwalk->id_incno != incno)
1101422Sroot continue;
1111422Sroot goto found;
1121422Sroot }
1131422Sroot /*
1141422Sroot * construct the new upper bound;
1151422Sroot * Enough room has been allocated.
1161422Sroot */
1171422Sroot itwalk = idatev[nidates] =
1181422Sroot (struct idates *)calloc(1, sizeof(struct idates));
1191422Sroot nidates += 1;
1201422Sroot found:
1211422Sroot strncpy(itwalk->id_name, fname, sizeof (itwalk->id_name));
1221422Sroot itwalk->id_incno = incno;
1231422Sroot itwalk->id_ddate = spcl.c_date;
1241422Sroot
1251422Sroot if ( (df = fopen(increm, "w")) == NULL){
1261422Sroot msg("Cannot open %s\n", increm);
1271422Sroot dumpabort();
1281422Sroot }
1291422Sroot ITITERATE(i, itwalk){
1301422Sroot recout(df, itwalk);
1311422Sroot }
1321422Sroot fclose(df);
1331422Sroot msg("level %c dump on %s\n", incno, prdate(spcl.c_date));
1341422Sroot }
1351422Sroot
recout(file,what)1361422Sroot recout(file, what)
1371422Sroot FILE *file;
1381422Sroot struct idates *what;
1391422Sroot {
1401422Sroot fprintf(file, DUMPOUTFMT,
1411422Sroot what->id_name,
1421422Sroot what->id_incno,
1431422Sroot ctime(&(what->id_ddate))
1441422Sroot );
1451422Sroot }
1461422Sroot
1471422Sroot int recno;
getrecord(df,idatep)1481422Sroot int getrecord(df, idatep)
1491422Sroot FILE *df;
1501422Sroot struct idates *idatep;
1511422Sroot {
1521422Sroot char buf[BUFSIZ];
1531422Sroot
1541422Sroot recno = 0;
1551422Sroot if ( (fgets(buf, BUFSIZ, df)) != buf)
1561422Sroot return(-1);
1571422Sroot recno++;
1581422Sroot if (makeidate(idatep, buf) < 0)
1591422Sroot msg("Unknown intermediate format in %s, line %d\n",
1601422Sroot NINCREM, recno);
1611422Sroot
1621422Sroot #ifdef FDEBUG
1631422Sroot msg("getrecord: %s %c %s\n",
1641422Sroot idatep->id_name, idatep->id_incno, prdate(idatep->id_ddate));
1651422Sroot #endif
1661422Sroot return(0);
1671422Sroot }
1681422Sroot
1691422Sroot /*
1701422Sroot * Convert from old format to new format
1711422Sroot * Convert from /etc/ddate to /etc/dumpdates format
1721422Sroot */
o_nconvert()1731422Sroot o_nconvert()
1741422Sroot {
1751422Sroot FILE *oldfile;
1761422Sroot FILE *newfile;
1771422Sroot struct idates idate;
1781422Sroot struct idates idatecopy;
1791422Sroot
1801422Sroot if( (newfile = fopen(NINCREM, "w")) == NULL){
1811422Sroot msg("%s: Can not open %s to update.\n", processname, NINCREM);
1821422Sroot Exit(X_ABORT);
1831422Sroot }
1841422Sroot if ( (oldfile = fopen(OINCREM, "r")) != NULL){
1851422Sroot while(!feof(oldfile)){
1861422Sroot if (fread(&idate, sizeof(idate), 1, oldfile) != 1)
1871422Sroot break;
1881422Sroot /*
1891422Sroot * The old format ddate did not have
1901422Sroot * the full special path name on it;
1911422Sroot * we add the prefix /dev/ to the
1921422Sroot * special name, although this may not be
1931422Sroot * always the right thing to do.
1941422Sroot */
1951422Sroot idatecopy = idate;
1961422Sroot strcpy(idatecopy.id_name, "/dev/");
1971422Sroot strncat(idatecopy.id_name, idate.id_name,
1981422Sroot sizeof(idate.id_name) - sizeof ("/dev/"));
1991422Sroot recout(newfile, &idatecopy);
2001422Sroot }
2011422Sroot }
2021422Sroot fclose(oldfile);
2031422Sroot fclose(newfile);
2041422Sroot }
2051422Sroot
2061422Sroot time_t unctime();
2071422Sroot
makeidate(ip,buf)2081422Sroot int makeidate(ip, buf)
2091422Sroot struct idates *ip;
2101422Sroot char *buf;
2111422Sroot {
2121422Sroot char un_buf[128];
2131422Sroot
2141422Sroot sscanf(buf, DUMPINFMT, ip->id_name, &ip->id_incno, un_buf);
2151422Sroot ip->id_ddate = unctime(un_buf);
2161422Sroot if (ip->id_ddate < 0)
2171422Sroot return(-1);
2181422Sroot return(0);
2191422Sroot }
2201422Sroot
2211422Sroot est(ip)
2221422Sroot struct dinode *ip;
2231422Sroot {
2241422Sroot long s;
2251422Sroot
2261422Sroot esize++;
2271422Sroot s = (ip->di_size + BSIZE-1) / BSIZE;
2281422Sroot esize += s;
2291422Sroot if(s > NADDR-3) {
2301422Sroot /*
2311422Sroot * This code is only appproximate.
2321422Sroot * it totally estimates low on doubly and triply indirect
2331422Sroot * files.
2341422Sroot */
2351422Sroot s -= NADDR-3;
2361422Sroot s = (s + (BSIZE/sizeof(daddr_t))-1) / (BSIZE/sizeof(daddr_t));
2371422Sroot esize += s;
2381422Sroot }
2391422Sroot }
2401422Sroot
bmapest(map)2411422Sroot bmapest(map)
2421422Sroot short *map;
2431422Sroot {
2441422Sroot register i, n;
2451422Sroot
2461422Sroot n = -1;
2471422Sroot for(i=0; i<MSIZ; i++)
2481422Sroot if(map[i])
2491422Sroot n = i;
2501422Sroot if(n < 0)
2511422Sroot return;
2521422Sroot esize++;
2531422Sroot esize += (n + (BSIZE/sizeof(short))-1) / (BSIZE/sizeof(short));
2541422Sroot }
255