itime.c revision 74872
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 74872 2001-03-27 19:28:00Z obrien $"; 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("cannot read %s: %s\n", dumpdates, 89 strerror(errno)); 90 return; 91 /* NOTREACHED */ 92 } 93 /* 94 * Dumpdates does not exist, make an empty one. 95 */ 96 msg("WARNING: no file `%s', making an empty one\n", dumpdates); 97 if ((df = fopen(dumpdates, "w")) == NULL) { 98 msg("cannot create %s: %s\n", dumpdates, 99 strerror(errno)); 100 return; 101 /* NOTREACHED */ 102 } 103 (void) fclose(df); 104 if ((df = fopen(dumpdates, "r")) == NULL) { 105 quit("cannot read %s even after creating it: %s\n", 106 dumpdates, strerror(errno)); 107 /* NOTREACHED */ 108 } 109 } 110 (void) flock(fileno(df), LOCK_SH); 111 readdumptimes(df); 112 (void) fclose(df); 113} 114 115static void 116readdumptimes(df) 117 FILE *df; 118{ 119 register int i; 120 register struct dumptime *dtwalk; 121 122 for (;;) { 123 dtwalk = (struct dumptime *)calloc(1, sizeof (struct dumptime)); 124 if (getrecord(df, &(dtwalk->dt_value)) < 0) 125 break; 126 nddates++; 127 SLIST_INSERT_HEAD(&dthead, dtwalk, dt_list); 128 } 129 130 ddates_in = 1; 131 /* 132 * arrayify the list, leaving enough room for the additional 133 * record that we may have to add to the ddate structure 134 */ 135 ddatev = (struct dumpdates **) 136 calloc((unsigned) (nddates + 1), sizeof (struct dumpdates *)); 137 dtwalk = SLIST_FIRST(&dthead); 138 for (i = nddates - 1; i >= 0; i--, dtwalk = SLIST_NEXT(dtwalk, dt_list)) 139 ddatev[i] = &dtwalk->dt_value; 140} 141 142void 143getdumptime() 144{ 145 register struct dumpdates *ddp; 146 register int i; 147 char *fname; 148 149 fname = disk; 150#ifdef FDEBUG 151 msg("Looking for name %s in dumpdates = %s for level = %c\n", 152 fname, dumpdates, level); 153#endif 154 spcl.c_ddate = 0; 155 lastlevel = '0'; 156 157 initdumptimes(); 158 /* 159 * Go find the entry with the same name for a lower increment 160 * and older date 161 */ 162 ITITERATE(i, ddp) { 163 if (strncmp(fname, ddp->dd_name, sizeof (ddp->dd_name)) != 0) 164 continue; 165 if (ddp->dd_level >= level) 166 continue; 167 if (ddp->dd_ddate <= spcl.c_ddate) 168 continue; 169 spcl.c_ddate = ddp->dd_ddate; 170 lastlevel = ddp->dd_level; 171 } 172} 173 174void 175putdumptime() 176{ 177 FILE *df; 178 register struct dumpdates *dtwalk; 179 register int i; 180 int fd; 181 char *fname; 182 183 if(uflag == 0) 184 return; 185 if ((df = fopen(dumpdates, "r+")) == NULL) 186 quit("cannot rewrite %s: %s\n", dumpdates, strerror(errno)); 187 fd = fileno(df); 188 (void) flock(fd, LOCK_EX); 189 fname = disk; 190 free((char *)ddatev); 191 ddatev = 0; 192 nddates = 0; 193 ddates_in = 0; 194 readdumptimes(df); 195 if (fseek(df, 0L, 0) < 0) 196 quit("fseek: %s\n", strerror(errno)); 197 spcl.c_ddate = 0; 198 ITITERATE(i, dtwalk) { 199 if (strncmp(fname, dtwalk->dd_name, 200 sizeof (dtwalk->dd_name)) != 0) 201 continue; 202 if (dtwalk->dd_level != level) 203 continue; 204 goto found; 205 } 206 /* 207 * construct the new upper bound; 208 * Enough room has been allocated. 209 */ 210 dtwalk = ddatev[nddates] = 211 (struct dumpdates *)calloc(1, sizeof (struct dumpdates)); 212 nddates += 1; 213 found: 214 (void) strncpy(dtwalk->dd_name, fname, sizeof (dtwalk->dd_name)); 215 dtwalk->dd_level = level; 216 dtwalk->dd_ddate = spcl.c_date; 217 218 ITITERATE(i, dtwalk) { 219 dumprecout(df, dtwalk); 220 } 221 if (fflush(df)) 222 quit("%s: %s\n", dumpdates, strerror(errno)); 223 if (ftruncate(fd, ftell(df))) 224 quit("ftruncate (%s): %s\n", dumpdates, strerror(errno)); 225 (void) fclose(df); 226 msg("level %c dump on %s", level, 227 spcl.c_date == 0 ? "the epoch\n" : ctime(&spcl.c_date)); 228} 229 230static void 231dumprecout(file, what) 232 FILE *file; 233 struct dumpdates *what; 234{ 235 236 if (fprintf(file, DUMPOUTFMT, 237 what->dd_name, 238 what->dd_level, 239 ctime(&what->dd_ddate)) < 0) 240 quit("%s: %s\n", dumpdates, strerror(errno)); 241} 242 243int recno; 244 245static int 246getrecord(df, ddatep) 247 FILE *df; 248 struct dumpdates *ddatep; 249{ 250 char tbuf[BUFSIZ]; 251 252 recno = 0; 253 if ( (fgets(tbuf, sizeof (tbuf), df)) != tbuf) 254 return(-1); 255 recno++; 256 if (makedumpdate(ddatep, tbuf) < 0) 257 msg("Unknown intermediate format in %s, line %d\n", 258 dumpdates, recno); 259 260#ifdef FDEBUG 261 msg("getrecord: %s %c %s", ddatep->dd_name, ddatep->dd_level, 262 ddatep->dd_ddate == 0 ? "the epoch\n" : ctime(&ddatep->dd_ddate)); 263#endif 264 return(0); 265} 266 267static int 268makedumpdate(ddp, tbuf) 269 struct dumpdates *ddp; 270 char *tbuf; 271{ 272 char un_buf[128]; 273 274 (void) sscanf(tbuf, DUMPINFMT, ddp->dd_name, &ddp->dd_level, un_buf); 275 ddp->dd_ddate = unctime(un_buf); 276 if (ddp->dd_ddate < 0) 277 return(-1); 278 return(0); 279} 280