itime.c revision 85635
1/*- 2 * Copyright (c) 1980, 1993 3 * The Regents of the University of California. 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#if 0 36static char sccsid[] = "@(#)itime.c 8.1 (Berkeley) 6/5/93"; 37#endif 38static const char rcsid[] = 39 "$FreeBSD: head/sbin/dump/itime.c 85635 2001-10-28 20:01:38Z dillon $"; 40#endif /* not lint */ 41 42#include <sys/param.h> 43#include <sys/queue.h> 44#include <sys/time.h> 45#ifdef sunos 46#include <sys/vnode.h> 47 48#include <ufs/fsdir.h> 49#include <ufs/inode.h> 50#include <ufs/fs.h> 51#else 52#include <ufs/ufs/dinode.h> 53#endif 54 55#include <protocols/dumprestore.h> 56 57#include <errno.h> 58#include <fcntl.h> 59#include <stdio.h> 60#ifdef __STDC__ 61#include <stdlib.h> 62#include <string.h> 63#endif 64 65#include "dump.h" 66 67struct dumptime { 68 struct dumpdates dt_value; 69 SLIST_ENTRY(dumptime) dt_list; 70}; 71SLIST_HEAD(dthead, dumptime) dthead = SLIST_HEAD_INITIALIZER(dthead); 72struct dumpdates **ddatev = 0; 73int nddates = 0; 74int ddates_in = 0; 75 76static void dumprecout __P((FILE *, struct dumpdates *)); 77static int getrecord __P((FILE *, struct dumpdates *)); 78static int makedumpdate __P((struct dumpdates *, char *)); 79static void readdumptimes __P((FILE *)); 80 81void 82initdumptimes() 83{ 84 FILE *df; 85 86 if ((df = fopen(dumpdates, "r")) == NULL) { 87 if (errno != ENOENT) { 88 msg("WARNING: cannot read %s: %s\n", dumpdates, 89 strerror(errno)); 90 return; 91 } 92 /* 93 * Dumpdates does not exist, make an empty one. 94 */ 95 msg("WARNING: no file `%s', making an empty one\n", dumpdates); 96 if ((df = fopen(dumpdates, "w")) == NULL) { 97 msg("WARNING: cannot create %s: %s\n", dumpdates, 98 strerror(errno)); 99 return; 100 } 101 (void) fclose(df); 102 if ((df = fopen(dumpdates, "r")) == NULL) { 103 quit("cannot read %s even after creating it: %s\n", 104 dumpdates, strerror(errno)); 105 /* NOTREACHED */ 106 } 107 } 108 (void) flock(fileno(df), LOCK_SH); 109 readdumptimes(df); 110 (void) fclose(df); 111} 112 113static void 114readdumptimes(df) 115 FILE *df; 116{ 117 register int i; 118 register struct dumptime *dtwalk; 119 120 for (;;) { 121 dtwalk = (struct dumptime *)calloc(1, sizeof (struct dumptime)); 122 if (getrecord(df, &(dtwalk->dt_value)) < 0) 123 break; 124 nddates++; 125 SLIST_INSERT_HEAD(&dthead, dtwalk, dt_list); 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 = SLIST_FIRST(&dthead); 136 for (i = nddates - 1; i >= 0; i--, dtwalk = SLIST_NEXT(dtwalk, dt_list)) 137 ddatev[i] = &dtwalk->dt_value; 138} 139 140void 141getdumptime() 142{ 143 register struct dumpdates *ddp; 144 register 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 <= time32_to_time(spcl.c_ddate)) 166 continue; 167 spcl.c_ddate = time_to_time32(ddp->dd_ddate); 168 lastlevel = ddp->dd_level; 169 } 170} 171 172void 173putdumptime() 174{ 175 FILE *df; 176 register struct dumpdates *dtwalk; 177 register int i; 178 int fd; 179 char *fname; 180 char *tmsg; 181 182 if(uflag == 0) 183 return; 184 if ((df = fopen(dumpdates, "r+")) == NULL) 185 quit("cannot rewrite %s: %s\n", dumpdates, strerror(errno)); 186 fd = fileno(df); 187 (void) flock(fd, LOCK_EX); 188 fname = disk; 189 free((char *)ddatev); 190 ddatev = 0; 191 nddates = 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 = time32_to_time(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 if (spcl.c_date == 0) { 226 tmsg = "the epoch\n"; 227 } else { 228 time_t t = time32_to_time(spcl.c_date); 229 tmsg = ctime(&t); 230 } 231 msg("level %c dump on %s", level, tmsg); 232} 233 234static void 235dumprecout(file, what) 236 FILE *file; 237 struct dumpdates *what; 238{ 239 240 if (fprintf(file, DUMPOUTFMT, 241 what->dd_name, 242 what->dd_level, 243 ctime(&what->dd_ddate)) < 0) 244 quit("%s: %s\n", dumpdates, strerror(errno)); 245} 246 247int recno; 248 249static int 250getrecord(df, ddatep) 251 FILE *df; 252 struct dumpdates *ddatep; 253{ 254 char tbuf[BUFSIZ]; 255 256 recno = 0; 257 if ( (fgets(tbuf, sizeof (tbuf), df)) != tbuf) 258 return(-1); 259 recno++; 260 if (makedumpdate(ddatep, tbuf) < 0) 261 msg("Unknown intermediate format in %s, line %d\n", 262 dumpdates, recno); 263 264#ifdef FDEBUG 265 msg("getrecord: %s %c %s", ddatep->dd_name, ddatep->dd_level, 266 ddatep->dd_ddate == 0 ? "the epoch\n" : ctime(&ddatep->dd_ddate)); 267#endif 268 return(0); 269} 270 271static int 272makedumpdate(ddp, tbuf) 273 struct dumpdates *ddp; 274 char *tbuf; 275{ 276 char un_buf[128]; 277 278 (void) sscanf(tbuf, DUMPINFMT, ddp->dd_name, &ddp->dd_level, un_buf); 279 ddp->dd_ddate = unctime(un_buf); 280 if (ddp->dd_ddate < 0) 281 return(-1); 282 return(0); 283} 284