BSD 4_3 release
[unix-history] / usr / src / etc / dump / dumpitime.c
index a8ac318..bbc1eb7 100644 (file)
@@ -1,5 +1,15 @@
-static char *sccsid = "@(#)dumpitime.c 1.7 (Berkeley) 12/2/82";
+/*
+ * Copyright (c) 1980 Regents of the University of California.
+ * All rights reserved.  The Berkeley software License Agreement
+ * specifies the terms and conditions for redistribution.
+ */
+
+#ifndef lint
+static char sccsid[] = "@(#)dumpitime.c        5.2 (Berkeley) 5/28/86";
+#endif not lint
+
 #include "dump.h"
 #include "dump.h"
+#include <sys/file.h>
 
 char *prdate(d)
        time_t d;
 
 char *prdate(d)
        time_t d;
@@ -20,25 +30,30 @@ struct      itime   *ithead = 0;
 
 inititimes()
 {
 
 inititimes()
 {
-                       FILE    *df;
+       FILE *df;
+
+       if ((df = fopen(increm, "r")) == NULL) {
+               perror(increm);
+               return;
+       }
+       (void) flock(fileno(df), LOCK_SH);
+       readitimes(df);
+       fclose(df);
+}
+
+readitimes(df)
+       FILE *df;
+{
        register        int     i;
        register        struct  itime   *itwalk;
 
        register        int     i;
        register        struct  itime   *itwalk;
 
-       if (idates_in)
-               return;
-       if ( (df = fopen(increm, "r")) == NULL){
-               nidates = 0;
-               ithead = 0;
-       } else {
-               do{
-                       itwalk=(struct itime *)calloc(1,sizeof (struct itime));
-                       if (getrecord(df, &(itwalk->it_value)) < 0)
-                               break;
-                       nidates++;
-                       itwalk->it_next = ithead;
-                       ithead = itwalk;
-               } while (1);
-               fclose(df);
+       for (;;) {
+               itwalk = (struct itime *)calloc(1, sizeof (struct itime));
+               if (getrecord(df, &(itwalk->it_value)) < 0)
+                       break;
+               nidates++;
+               itwalk->it_next = ithead;
+               ithead = itwalk;
        }
 
        idates_in = 1;
        }
 
        idates_in = 1;
@@ -47,7 +62,8 @@ inititimes()
         *      record that we may have to add to the idate structure
         */
        idatev = (struct idates **)calloc(nidates + 1,sizeof (struct idates *));
         *      record that we may have to add to the idate structure
         */
        idatev = (struct idates **)calloc(nidates + 1,sizeof (struct idates *));
-       for (i = nidates-1, itwalk = ithead; i >= 0; i--, itwalk = itwalk->it_next)
+       itwalk = ithead;
+       for (i = nidates - 1; i >= 0; i--, itwalk = itwalk->it_next)
                idatev[i] = &itwalk->it_value;
 }
 
                idatev[i] = &itwalk->it_value;
 }
 
@@ -63,22 +79,23 @@ getitime()
                fname, increm, incno);
 #endif
        spcl.c_ddate = 0;
                fname, increm, incno);
 #endif
        spcl.c_ddate = 0;
+       lastincno = '0';
 
        inititimes();
        /*
         *      Go find the entry with the same name for a lower increment
         *      and older date
         */
 
        inititimes();
        /*
         *      Go find the entry with the same name for a lower increment
         *      and older date
         */
-       ITITERATE(i, ip){
-               if(strncmp(fname, ip->id_name,
-                               sizeof (ip->id_name)) != 0)
+       ITITERATE(i, ip) {
+               if (strncmp(fname, ip->id_name, sizeof (ip->id_name)) != 0)
                        continue;
                if (ip->id_incno >= incno)
                        continue;
                if (ip->id_ddate <= spcl.c_ddate)
                        continue;
                spcl.c_ddate = ip->id_ddate;
                        continue;
                if (ip->id_incno >= incno)
                        continue;
                if (ip->id_ddate <= spcl.c_ddate)
                        continue;
                spcl.c_ddate = ip->id_ddate;
-       } 
+               lastincno = ip->id_incno;
+       }
 }
 
 putitime()
 }
 
 putitime()
@@ -86,12 +103,28 @@ putitime()
        FILE            *df;
        register        struct  idates  *itwalk;
        register        int     i;
        FILE            *df;
        register        struct  idates  *itwalk;
        register        int     i;
+       int             fd;
        char            *fname;
 
        if(uflag == 0)
                return;
        char            *fname;
 
        if(uflag == 0)
                return;
+       if ((df = fopen(increm, "r+")) == NULL) {
+               perror(increm);
+               dumpabort();
+       }
+       fd = fileno(df);
+       (void) flock(fd, LOCK_EX);
        fname = disk;
        fname = disk;
-
+       free(idatev);
+       idatev = 0;
+       nidates = 0;
+       ithead = 0;
+       idates_in = 0;
+       readitimes(df);
+       if (fseek(df,0L,0) < 0) {   /* rewind() was redefined in dumptape.c */
+               perror("fseek");
+               dumpabort();
+       }
        spcl.c_ddate = 0;
        ITITERATE(i, itwalk){
                if (strncmp(fname, itwalk->id_name,
        spcl.c_ddate = 0;
        ITITERATE(i, itwalk){
                if (strncmp(fname, itwalk->id_name,
@@ -113,14 +146,14 @@ putitime()
        itwalk->id_incno = incno;
        itwalk->id_ddate = spcl.c_date;
 
        itwalk->id_incno = incno;
        itwalk->id_ddate = spcl.c_date;
 
-       if ( (df = fopen(increm, "w")) == NULL){
-               msg("Cannot open %s\n", increm);
-               dumpabort();
-       }
        ITITERATE(i, itwalk){
                recout(df, itwalk);
        }
        ITITERATE(i, itwalk){
                recout(df, itwalk);
        }
-       fclose(df);
+       if (ftruncate(fd, ftell(df))) {
+               perror("ftruncate");
+               dumpabort();
+       }
+       (void) fclose(df);
        msg("level %c dump on %s\n", incno, prdate(spcl.c_date));
 }
 
        msg("level %c dump on %s\n", incno, prdate(spcl.c_date));
 }
 
@@ -148,7 +181,7 @@ int getrecord(df, idatep)
        recno++;
        if (makeidate(idatep, buf) < 0)
                msg("Unknown intermediate format in %s, line %d\n",
        recno++;
        if (makeidate(idatep, buf) < 0)
                msg("Unknown intermediate format in %s, line %d\n",
-                       NINCREM, recno);
+                       increm, recno);
 
 #ifdef FDEBUG
        msg("getrecord: %s %c %s\n",
 
 #ifdef FDEBUG
        msg("getrecord: %s %c %s\n",
@@ -157,43 +190,6 @@ int getrecord(df, idatep)
        return(0);
 }
 
        return(0);
 }
 
-/*
- *     Convert from old format to new format
- *     Convert from /etc/ddate to /etc/dumpdates format
- */
-o_nconvert()
-{
-       FILE    *oldfile;
-       FILE    *newfile;
-       struct  idates  idate;
-       struct  idates  idatecopy;
-
-       if( (newfile = fopen(NINCREM, "w")) == NULL){
-               msg("%s: Can not open %s to update.\n", processname, NINCREM);
-               Exit(X_ABORT);
-       }
-       if ( (oldfile = fopen(OINCREM, "r")) != NULL){
-               while(!feof(oldfile)){
-                       if (fread(&idate, sizeof(idate), 1, oldfile) != 1)
-                               break;
-                       /*
-                        *      The old format ddate did not have
-                        *      the full special path name on it;
-                        *      we add the prefix /dev/ to the
-                        *      special name, although this may not be
-                        *      always the right thing to do.
-                        */
-                       idatecopy = idate;
-                       strcpy(idatecopy.id_name, "/dev/");
-                       strncat(idatecopy.id_name, idate.id_name,
-                               sizeof(idate.id_name) - sizeof ("/dev/"));
-                       recout(newfile, &idatecopy);
-               }
-       }
-       fclose(oldfile);
-       fclose(newfile);
-}
-
 time_t unctime();
 
 int makeidate(ip, buf)
 time_t unctime();
 
 int makeidate(ip, buf)
@@ -211,20 +207,37 @@ int makeidate(ip, buf)
 
 /*
  * This is an estimation of the number of TP_BSIZE blocks in the file.
 
 /*
  * This is an estimation of the number of TP_BSIZE blocks in the file.
- * It assumes that there are no unallocated blocks; hence
- * the estimate may be high
+ * It estimates the number of blocks in files with holes by assuming
+ * that all of the blocks accounted for by di_blocks are data blocks
+ * (when some of the blocks are usually used for indirect pointers);
+ * hence the estimate may be high.
  */
 est(ip)
        struct dinode *ip;
 {
  */
 est(ip)
        struct dinode *ip;
 {
-       long s;
+       long s, t;
 
 
+       /*
+        * ip->di_size is the size of the file in bytes.
+        * ip->di_blocks stores the number of sectors actually in the file.
+        * If there are more sectors than the size would indicate, this just
+        *      means that there are indirect blocks in the file or unused
+        *      sectors in the last file block; we can safely ignore these
+        *      (s = t below).
+        * If the file is bigger than the number of sectors would indicate,
+        *      then the file has holes in it.  In this case we must use the
+        *      block count to estimate the number of data blocks used, but
+        *      we use the actual size for estimating the number of indirect
+        *      dump blocks (t vs. s in the indirect block calculation).
+        */
        esize++;
        esize++;
-       /* calc number of TP_BSIZE blocks */
-       s = howmany(ip->di_size, TP_BSIZE);
+       s = howmany(dbtob(ip->di_blocks), TP_BSIZE);
+       t = howmany(ip->di_size, TP_BSIZE);
+       if ( s > t )
+               s = t;
        if (ip->di_size > sblock->fs_bsize * NDADDR) {
        if (ip->di_size > sblock->fs_bsize * NDADDR) {
-               /* calc number of indirect blocks on the dump tape */
-               s += howmany(s - NDADDR * sblock->fs_bsize / TP_BSIZE,
+               /* calculate the number of indirect blocks on the dump tape */
+               s += howmany(t - NDADDR * sblock->fs_bsize / TP_BSIZE,
                        TP_NINDIR);
        }
        esize += s;
                        TP_NINDIR);
        }
        esize += s;