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