itime.c revision 1559
111633Sjoerg/*- 211633Sjoerg * Copyright (c) 1980, 1993 311633Sjoerg * The Regents of the University of California. All rights reserved. 411633Sjoerg * 511633Sjoerg * Redistribution and use in source and binary forms, with or without 650477Speter * modification, are permitted provided that the following conditions 711633Sjoerg * are met: 874858Ssobomax * 1. Redistributions of source code must retain the above copyright 911633Sjoerg * notice, this list of conditions and the following disclaimer. 1011633Sjoerg * 2. Redistributions in binary form must reproduce the above copyright 1111633Sjoerg * notice, this list of conditions and the following disclaimer in the 1211633Sjoerg * documentation and/or other materials provided with the distribution. 1311633Sjoerg * 3. All advertising materials mentioning features or use of this software 1411633Sjoerg * must display the following acknowledgement: 1511633Sjoerg * This product includes software developed by the University of 1611633Sjoerg * California, Berkeley and its contributors. 1711633Sjoerg * 4. Neither the name of the University nor the names of its contributors 1811633Sjoerg * may be used to endorse or promote products derived from this software 1911633Sjoerg * without specific prior written permission. 2011633Sjoerg * 2111633Sjoerg * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2211633Sjoerg * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2311633Sjoerg * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2411633Sjoerg * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2511633Sjoerg * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2611633Sjoerg * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2711633Sjoerg * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2811633Sjoerg * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2911633Sjoerg * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3011633Sjoerg * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3111633Sjoerg * SUCH DAMAGE. 3211633Sjoerg */ 3311633Sjoerg 3411633Sjoerg#ifndef lint 3511633Sjoergstatic char sccsid[] = "@(#)itime.c 8.1 (Berkeley) 6/5/93"; 3611633Sjoerg#endif /* not lint */ 3711633Sjoerg 3811633Sjoerg#include <sys/param.h> 3911633Sjoerg#include <sys/time.h> 4011633Sjoerg#ifdef sunos 4111633Sjoerg#include <sys/vnode.h> 4211633Sjoerg 4311633Sjoerg#include <ufs/fsdir.h> 4411633Sjoerg#include <ufs/inode.h> 4511633Sjoerg#include <ufs/fs.h> 4611633Sjoerg#else 4711633Sjoerg#include <ufs/ufs/dinode.h> 4811633Sjoerg#endif 4911633Sjoerg 5011633Sjoerg#include <protocols/dumprestore.h> 5111633Sjoerg 5211633Sjoerg#include <errno.h> 5311633Sjoerg#include <fcntl.h> 5415940Sjoerg#include <stdio.h> 5511633Sjoerg#ifdef __STDC__ 5611633Sjoerg#include <stdlib.h> 5711633Sjoerg#include <string.h> 5811633Sjoerg#include <unistd.h> 5911633Sjoerg#endif 6011633Sjoerg 6111633Sjoerg#include "dump.h" 6211633Sjoerg 6311633Sjoergstruct dumpdates **ddatev = 0; 6411633Sjoergint nddates = 0; 6574858Ssobomaxint ddates_in = 0; 6611633Sjoergstruct dumptime *dthead = 0; 6711633Sjoerg 6811633Sjoergstatic void dumprecout __P((FILE *, struct dumpdates *)); 6911633Sjoergstatic int getrecord __P((FILE *, struct dumpdates *)); 7011633Sjoergstatic int makedumpdate __P((struct dumpdates *, char *)); 7115940Sjoergstatic void readdumptimes __P((FILE *)); 7211633Sjoerg 7311633Sjoergvoid 7411633Sjoerginitdumptimes() 7511633Sjoerg{ 7611633Sjoerg FILE *df; 7711633Sjoerg 7811633Sjoerg if ((df = fopen(dumpdates, "r")) == NULL) { 7911633Sjoerg if (errno != ENOENT) { 8011633Sjoerg quit("cannot read %s: %s\n", dumpdates, 8111633Sjoerg strerror(errno)); 8211633Sjoerg /* NOTREACHED */ 8311633Sjoerg } 8411633Sjoerg /* 8511633Sjoerg * Dumpdates does not exist, make an empty one. 8611633Sjoerg */ 8711633Sjoerg msg("WARNING: no file `%s', making an empty one\n", dumpdates); 8811633Sjoerg if ((df = fopen(dumpdates, "w")) == NULL) { 8911633Sjoerg quit("cannot create %s: %s\n", dumpdates, 9011633Sjoerg strerror(errno)); 9111633Sjoerg /* NOTREACHED */ 9227153Swosch } 9311633Sjoerg (void) fclose(df); 9411633Sjoerg if ((df = fopen(dumpdates, "r")) == NULL) { 9511633Sjoerg quit("cannot read %s even after creating it: %s\n", 9611633Sjoerg dumpdates, strerror(errno)); 9711633Sjoerg /* NOTREACHED */ 9811633Sjoerg } 9911633Sjoerg } 10011633Sjoerg (void) flock(fileno(df), LOCK_SH); 10111633Sjoerg readdumptimes(df); 10211633Sjoerg (void) fclose(df); 10311633Sjoerg} 10411633Sjoerg 10511633Sjoergstatic void 10611633Sjoergreaddumptimes(df) 10711633Sjoerg FILE *df; 10811633Sjoerg{ 10911633Sjoerg register int i; 11011633Sjoerg register struct dumptime *dtwalk; 11111633Sjoerg 11211633Sjoerg for (;;) { 11332470Scharnier dtwalk = (struct dumptime *)calloc(1, sizeof (struct dumptime)); 11411633Sjoerg if (getrecord(df, &(dtwalk->dt_value)) < 0) 11511633Sjoerg break; 11611633Sjoerg nddates++; 11711633Sjoerg dtwalk->dt_next = dthead; 11811633Sjoerg dthead = dtwalk; 11911633Sjoerg } 12011633Sjoerg 12111633Sjoerg ddates_in = 1; 12211633Sjoerg /* 12311633Sjoerg * arrayify the list, leaving enough room for the additional 12411633Sjoerg * record that we may have to add to the ddate structure 12511633Sjoerg */ 12611633Sjoerg ddatev = (struct dumpdates **) 12711633Sjoerg calloc((unsigned) (nddates + 1), sizeof (struct dumpdates *)); 12811633Sjoerg dtwalk = dthead; 12911633Sjoerg for (i = nddates - 1; i >= 0; i--, dtwalk = dtwalk->dt_next) 13011633Sjoerg ddatev[i] = &dtwalk->dt_value; 13111633Sjoerg} 13211633Sjoerg 13311633Sjoergvoid 13411633Sjoerggetdumptime() 13511633Sjoerg{ 13611633Sjoerg register struct dumpdates *ddp; 13711633Sjoerg register int i; 13811633Sjoerg char *fname; 13911633Sjoerg 14011633Sjoerg fname = disk; 14111633Sjoerg#ifdef FDEBUG 14211633Sjoerg msg("Looking for name %s in dumpdates = %s for level = %c\n", 14311633Sjoerg fname, dumpdates, level); 14411633Sjoerg#endif 14511633Sjoerg spcl.c_ddate = 0; 14611633Sjoerg lastlevel = '0'; 14711633Sjoerg 14811633Sjoerg initdumptimes(); 14911633Sjoerg /* 15011633Sjoerg * Go find the entry with the same name for a lower increment 15111633Sjoerg * and older date 15211633Sjoerg */ 15311633Sjoerg ITITERATE(i, ddp) { 15411633Sjoerg if (strncmp(fname, ddp->dd_name, sizeof (ddp->dd_name)) != 0) 15511633Sjoerg continue; 15611633Sjoerg if (ddp->dd_level >= level) 15711633Sjoerg continue; 15811633Sjoerg if (ddp->dd_ddate <= spcl.c_ddate) 15911633Sjoerg continue; 16011633Sjoerg spcl.c_ddate = ddp->dd_ddate; 16111633Sjoerg lastlevel = ddp->dd_level; 16211633Sjoerg } 16311633Sjoerg} 16411633Sjoerg 16511633Sjoergvoid 16611633Sjoergputdumptime() 16711633Sjoerg{ 16811633Sjoerg FILE *df; 16911633Sjoerg register struct dumpdates *dtwalk; 17011633Sjoerg register int i; 17111633Sjoerg int fd; 17221015Sjoerg char *fname; 17321015Sjoerg 17421015Sjoerg if(uflag == 0) 17521015Sjoerg return; 17621015Sjoerg if ((df = fopen(dumpdates, "r+")) == NULL) 17721015Sjoerg quit("cannot rewrite %s: %s\n", dumpdates, strerror(errno)); 17821015Sjoerg fd = fileno(df); 17921015Sjoerg (void) flock(fd, LOCK_EX); 18021015Sjoerg fname = disk; 18121015Sjoerg free((char *)ddatev); 18221015Sjoerg ddatev = 0; 18374958Sjoerg nddates = 0; 18474858Ssobomax dthead = 0; 18574858Ssobomax ddates_in = 0; 186 readdumptimes(df); 187 if (fseek(df, 0L, 0) < 0) 188 quit("fseek: %s\n", strerror(errno)); 189 spcl.c_ddate = 0; 190 ITITERATE(i, dtwalk) { 191 if (strncmp(fname, dtwalk->dd_name, 192 sizeof (dtwalk->dd_name)) != 0) 193 continue; 194 if (dtwalk->dd_level != level) 195 continue; 196 goto found; 197 } 198 /* 199 * construct the new upper bound; 200 * Enough room has been allocated. 201 */ 202 dtwalk = ddatev[nddates] = 203 (struct dumpdates *)calloc(1, sizeof (struct dumpdates)); 204 nddates += 1; 205 found: 206 (void) strncpy(dtwalk->dd_name, fname, sizeof (dtwalk->dd_name)); 207 dtwalk->dd_level = level; 208 dtwalk->dd_ddate = spcl.c_date; 209 210 ITITERATE(i, dtwalk) { 211 dumprecout(df, dtwalk); 212 } 213 if (fflush(df)) 214 quit("%s: %s\n", dumpdates, strerror(errno)); 215 if (ftruncate(fd, ftell(df))) 216 quit("ftruncate (%s): %s\n", dumpdates, strerror(errno)); 217 (void) fclose(df); 218 msg("level %c dump on %s", level, 219 spcl.c_date == 0 ? "the epoch\n" : ctime(&spcl.c_date)); 220} 221 222static void 223dumprecout(file, what) 224 FILE *file; 225 struct dumpdates *what; 226{ 227 228 if (fprintf(file, DUMPOUTFMT, 229 what->dd_name, 230 what->dd_level, 231 ctime(&what->dd_ddate)) < 0) 232 quit("%s: %s\n", dumpdates, strerror(errno)); 233} 234 235int recno; 236 237static int 238getrecord(df, ddatep) 239 FILE *df; 240 struct dumpdates *ddatep; 241{ 242 char tbuf[BUFSIZ]; 243 244 recno = 0; 245 if ( (fgets(tbuf, sizeof (tbuf), df)) != tbuf) 246 return(-1); 247 recno++; 248 if (makedumpdate(ddatep, tbuf) < 0) 249 msg("Unknown intermediate format in %s, line %d\n", 250 dumpdates, recno); 251 252#ifdef FDEBUG 253 msg("getrecord: %s %c %s", ddatep->dd_name, ddatep->dd_level, 254 ddatep->dd_ddate == 0 ? "the epoch\n" : ctime(&ddatep->dd_ddate)); 255#endif 256 return(0); 257} 258 259static int 260makedumpdate(ddp, tbuf) 261 struct dumpdates *ddp; 262 char *tbuf; 263{ 264 char un_buf[128]; 265 266 (void) sscanf(tbuf, DUMPINFMT, ddp->dd_name, &ddp->dd_level, un_buf); 267 ddp->dd_ddate = unctime(un_buf); 268 if (ddp->dd_ddate < 0) 269 return(-1); 270 return(0); 271} 272