home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / bsd_srcs / sbin / dump / itime.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-20  |  6.6 KB  |  270 lines

  1. /*-
  2.  * Copyright (c) 1980 The Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  * 1. Redistributions of source code must retain the above copyright
  9.  *    notice, this list of conditions and the following disclaimer.
  10.  * 2. Redistributions in binary form must reproduce the above copyright
  11.  *    notice, this list of conditions and the following disclaimer in the
  12.  *    documentation and/or other materials provided with the distribution.
  13.  * 3. All advertising materials mentioning features or use of this software
  14.  *    must display the following acknowledgement:
  15.  *    This product includes software developed by the University of
  16.  *    California, Berkeley and its contributors.
  17.  * 4. Neither the name of the University nor the names of its contributors
  18.  *    may be used to endorse or promote products derived from this software
  19.  *    without specific prior written permission.
  20.  *
  21.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31.  * SUCH DAMAGE.
  32.  */
  33.  
  34. #ifndef lint
  35. static char sccsid[] = "@(#)itime.c    5.15 (Berkeley) 6/18/92";
  36. #endif /* not lint */
  37.  
  38. #ifdef sunos
  39. #include <stdio.h>
  40. #include <ctype.h>
  41. #include <sys/param.h>
  42. #include <sys/stat.h>
  43. #include <ufs/fs.h>
  44. #else
  45. #include <sys/param.h>
  46. #include <sys/time.h>
  47. #endif
  48. #include <ufs/ufs/dinode.h>
  49. #include <fcntl.h>
  50. #include <protocols/dumprestore.h>
  51. #include <errno.h>
  52. #include <stdio.h>
  53. #ifdef __STDC__
  54. #include <time.h>
  55. #include <unistd.h>
  56. #include <stdlib.h>
  57. #include <string.h>
  58. #endif
  59. #include "dump.h"
  60.  
  61. struct    dumpdates **ddatev = 0;
  62. int    nddates = 0;
  63. int    ddates_in = 0;
  64. struct    dumptime *dthead = 0;
  65.  
  66. void    readdumptimes();
  67. int    getrecord();
  68. int    makedumpdate();
  69.  
  70. static void dumprecout();
  71.  
  72. void
  73. initdumptimes()
  74. {
  75.     FILE *df;
  76.  
  77.     if ((df = fopen(dumpdates, "r")) == NULL) {
  78.         if (errno != ENOENT) {
  79.             quit("cannot read %s: %s\n", dumpdates,
  80.                 strerror(errno));
  81.             /* NOTREACHED */
  82.         }
  83.         /*
  84.          * Dumpdates does not exist, make an empty one.
  85.          */
  86.         msg("WARNING: no file `%s', making an empty one\n", dumpdates);
  87.         if ((df = fopen(dumpdates, "w")) == NULL) {
  88.             quit("cannot create %s: %s\n", dumpdates,
  89.                 strerror(errno));
  90.             /* NOTREACHED */
  91.         }
  92.         (void) fclose(df);
  93.         if ((df = fopen(dumpdates, "r")) == NULL) {
  94.             quit("cannot read %s even after creating it: %s\n",
  95.                 dumpdates, strerror(errno));
  96.             /* NOTREACHED */
  97.         }
  98.     }
  99.     (void) flock(fileno(df), LOCK_SH);
  100.     readdumptimes(df);
  101.     (void) fclose(df);
  102. }
  103.  
  104. void
  105. readdumptimes(df)
  106.     FILE *df;
  107. {
  108.     register int i;
  109.     register struct    dumptime *dtwalk;
  110.  
  111.     for (;;) {
  112.         dtwalk = (struct dumptime *)calloc(1, sizeof (struct dumptime));
  113.         if (getrecord(df, &(dtwalk->dt_value)) < 0)
  114.             break;
  115.         nddates++;
  116.         dtwalk->dt_next = dthead;
  117.         dthead = dtwalk;
  118.     }
  119.  
  120.     ddates_in = 1;
  121.     /*
  122.      *    arrayify the list, leaving enough room for the additional
  123.      *    record that we may have to add to the ddate structure
  124.      */
  125.     ddatev = (struct dumpdates **)
  126.         calloc((unsigned) (nddates + 1), sizeof (struct dumpdates *));
  127.     dtwalk = dthead;
  128.     for (i = nddates - 1; i >= 0; i--, dtwalk = dtwalk->dt_next)
  129.         ddatev[i] = &dtwalk->dt_value;
  130. }
  131.  
  132. void
  133. getdumptime()
  134. {
  135.     register struct dumpdates *ddp;
  136.     register int i;
  137.     char *fname;
  138.  
  139.     fname = disk;
  140. #ifdef FDEBUG
  141.     msg("Looking for name %s in dumpdates = %s for level = %c\n",
  142.         fname, dumpdates, level);
  143. #endif
  144.     spcl.c_ddate = 0;
  145.     lastlevel = '0';
  146.  
  147.     initdumptimes();
  148.     /*
  149.      *    Go find the entry with the same name for a lower increment
  150.      *    and older date
  151.      */
  152.     ITITERATE(i, ddp) {
  153.         if (strncmp(fname, ddp->dd_name, sizeof (ddp->dd_name)) != 0)
  154.             continue;
  155.         if (ddp->dd_level >= level)
  156.             continue;
  157.         if (ddp->dd_ddate <= spcl.c_ddate)
  158.             continue;
  159.         spcl.c_ddate = ddp->dd_ddate;
  160.         lastlevel = ddp->dd_level;
  161.     }
  162. }
  163.  
  164. void
  165. putdumptime()
  166. {
  167.     FILE *df;
  168.     register struct dumpdates *dtwalk;
  169.     register int i;
  170.     int fd;
  171.     char *fname;
  172.  
  173.     if(uflag == 0)
  174.         return;
  175.     if ((df = fopen(dumpdates, "r+")) == NULL)
  176.         quit("cannot rewrite %s: %s\n", dumpdates, strerror(errno));
  177.     fd = fileno(df);
  178.     (void) flock(fd, LOCK_EX);
  179.     fname = disk;
  180.     free((char *)ddatev);
  181.     ddatev = 0;
  182.     nddates = 0;
  183.     dthead = 0;
  184.     ddates_in = 0;
  185.     readdumptimes(df);
  186.     if (fseek(df, 0L, 0) < 0)
  187.         quit("fseek: %s\n", strerror(errno));
  188.     spcl.c_ddate = 0;
  189.     ITITERATE(i, dtwalk) {
  190.         if (strncmp(fname, dtwalk->dd_name,
  191.                 sizeof (dtwalk->dd_name)) != 0)
  192.             continue;
  193.         if (dtwalk->dd_level != level)
  194.             continue;
  195.         goto found;
  196.     }
  197.     /*
  198.      *    construct the new upper bound;
  199.      *    Enough room has been allocated.
  200.      */
  201.     dtwalk = ddatev[nddates] =
  202.         (struct dumpdates *)calloc(1, sizeof (struct dumpdates));
  203.     nddates += 1;
  204.   found:
  205.     (void) strncpy(dtwalk->dd_name, fname, sizeof (dtwalk->dd_name));
  206.     dtwalk->dd_level = level;
  207.     dtwalk->dd_ddate = spcl.c_date;
  208.  
  209.     ITITERATE(i, dtwalk) {
  210.         dumprecout(df, dtwalk);
  211.     }
  212.     if (fflush(df))
  213.         quit("%s: %s\n", dumpdates, strerror(errno));
  214.     if (ftruncate(fd, ftell(df)))
  215.         quit("ftruncate (%s): %s\n", dumpdates, strerror(errno));
  216.     (void) fclose(df);
  217.     msg("level %c dump on %s", level,
  218.         spcl.c_date == 0 ? "the epoch\n" : ctime(&spcl.c_date));
  219. }
  220.  
  221. static void
  222. dumprecout(file, what)
  223.     FILE *file;
  224.     struct dumpdates *what;
  225. {
  226.  
  227.     if (fprintf(file, DUMPOUTFMT,
  228.             what->dd_name,
  229.             what->dd_level,
  230.             ctime(&what->dd_ddate)) < 0)
  231.         quit("%s: %s\n", dumpdates, strerror(errno));
  232. }
  233.  
  234. int    recno;
  235. int
  236. getrecord(df, ddatep)
  237.     FILE *df;
  238.     struct dumpdates *ddatep;
  239. {
  240.     char tbuf[BUFSIZ];
  241.  
  242.     recno = 0;
  243.     if ( (fgets(tbuf, sizeof (tbuf), df)) != tbuf)
  244.         return(-1);
  245.     recno++;
  246.     if (makedumpdate(ddatep, tbuf) < 0)
  247.         msg("Unknown intermediate format in %s, line %d\n",
  248.             dumpdates, recno);
  249.  
  250. #ifdef FDEBUG
  251.     msg("getrecord: %s %c %s", ddatep->dd_name, ddatep->dd_level,
  252.         ddatep->dd_ddate == 0 ? "the epoch\n" : ctime(&ddatep->dd_ddate));
  253. #endif
  254.     return(0);
  255. }
  256.  
  257. int
  258. makedumpdate(ddp, tbuf)
  259.     struct dumpdates *ddp;
  260.     char *tbuf;
  261. {
  262.     char un_buf[128];
  263.  
  264.     (void) sscanf(tbuf, DUMPINFMT, ddp->dd_name, &ddp->dd_level, un_buf);
  265.     ddp->dd_ddate = unctime(un_buf);
  266.     if (ddp->dd_ddate < 0)
  267.         return(-1);
  268.     return(0);
  269. }
  270.