itime.c revision 86473
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 3511633Sjoerg#if 0 3611633Sjoergstatic char sccsid[] = "@(#)itime.c 8.1 (Berkeley) 6/5/93"; 3711633Sjoerg#endif 3811633Sjoergstatic const char rcsid[] = 3911633Sjoerg "$FreeBSD: head/sbin/dump/itime.c 86473 2001-11-17 00:06:55Z iedowse $"; 4011633Sjoerg#endif /* not lint */ 4111633Sjoerg 4211633Sjoerg#include <sys/param.h> 4311633Sjoerg#include <sys/queue.h> 4411633Sjoerg#include <sys/time.h> 4511633Sjoerg 4611633Sjoerg#include <ufs/ufs/dinode.h> 4711633Sjoerg 4811633Sjoerg#include <protocols/dumprestore.h> 4911633Sjoerg 5011633Sjoerg#include <errno.h> 5111633Sjoerg#include <fcntl.h> 5211633Sjoerg#include <stdio.h> 5311633Sjoerg#ifdef __STDC__ 5415940Sjoerg#include <stdlib.h> 5511633Sjoerg#include <string.h> 5611633Sjoerg#endif 5711633Sjoerg 5811633Sjoerg#include "dump.h" 5911633Sjoerg 6011633Sjoergstruct dumptime { 6111633Sjoerg struct dumpdates dt_value; 6211633Sjoerg SLIST_ENTRY(dumptime) dt_list; 6311633Sjoerg}; 6411633SjoergSLIST_HEAD(dthead, dumptime) dthead = SLIST_HEAD_INITIALIZER(dthead); 6574858Ssobomaxstruct dumpdates **ddatev = 0; 6611633Sjoergint nddates = 0; 6711633Sjoergint ddates_in = 0; 6811633Sjoerg 6911633Sjoergstatic void dumprecout __P((FILE *, struct dumpdates *)); 7011633Sjoergstatic int getrecord __P((FILE *, struct dumpdates *)); 7115940Sjoergstatic int makedumpdate __P((struct dumpdates *, char *)); 7211633Sjoergstatic void readdumptimes __P((FILE *)); 7311633Sjoerg 7411633Sjoergvoid 7511633Sjoerginitdumptimes() 7611633Sjoerg{ 7711633Sjoerg FILE *df; 7811633Sjoerg 7911633Sjoerg if ((df = fopen(dumpdates, "r")) == NULL) { 8011633Sjoerg if (errno != ENOENT) { 8111633Sjoerg msg("WARNING: cannot read %s: %s\n", dumpdates, 8211633Sjoerg strerror(errno)); 8311633Sjoerg return; 8411633Sjoerg } 8511633Sjoerg /* 8611633Sjoerg * Dumpdates does not exist, make an empty one. 8711633Sjoerg */ 8811633Sjoerg msg("WARNING: no file `%s', making an empty one\n", dumpdates); 8911633Sjoerg if ((df = fopen(dumpdates, "w")) == NULL) { 9011633Sjoerg msg("WARNING: cannot create %s: %s\n", dumpdates, 9111633Sjoerg strerror(errno)); 9227153Swosch return; 9311633Sjoerg } 9411633Sjoerg (void) fclose(df); 9511633Sjoerg if ((df = fopen(dumpdates, "r")) == NULL) { 9611633Sjoerg quit("cannot read %s even after creating it: %s\n", 9711633Sjoerg dumpdates, strerror(errno)); 9811633Sjoerg /* NOTREACHED */ 9911633Sjoerg } 10011633Sjoerg } 10111633Sjoerg (void) flock(fileno(df), LOCK_SH); 10211633Sjoerg readdumptimes(df); 10311633Sjoerg (void) fclose(df); 10411633Sjoerg} 10511633Sjoerg 10611633Sjoergstatic void 10711633Sjoergreaddumptimes(df) 10811633Sjoerg FILE *df; 10911633Sjoerg{ 11011633Sjoerg int i; 11111633Sjoerg struct dumptime *dtwalk; 11211633Sjoerg 11332470Scharnier for (;;) { 11411633Sjoerg dtwalk = (struct dumptime *)calloc(1, sizeof (struct dumptime)); 11511633Sjoerg if (getrecord(df, &(dtwalk->dt_value)) < 0) 11611633Sjoerg break; 11711633Sjoerg nddates++; 11811633Sjoerg SLIST_INSERT_HEAD(&dthead, dtwalk, dt_list); 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 = SLIST_FIRST(&dthead); 12911633Sjoerg for (i = nddates - 1; i >= 0; i--, dtwalk = SLIST_NEXT(dtwalk, dt_list)) 13011633Sjoerg ddatev[i] = &dtwalk->dt_value; 13111633Sjoerg} 13211633Sjoerg 13311633Sjoergvoid 13411633Sjoerggetdumptime() 13511633Sjoerg{ 13611633Sjoerg struct dumpdates *ddp; 13711633Sjoerg 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 <= time32_to_time(spcl.c_ddate)) 15911633Sjoerg continue; 16011633Sjoerg spcl.c_ddate = time_to_time32(ddp->dd_ddate); 16111633Sjoerg lastlevel = ddp->dd_level; 16211633Sjoerg } 16311633Sjoerg} 16411633Sjoerg 16511633Sjoergvoid 16611633Sjoergputdumptime() 16711633Sjoerg{ 16811633Sjoerg FILE *df; 16911633Sjoerg struct dumpdates *dtwalk; 17011633Sjoerg int i; 17111633Sjoerg int fd; 17221015Sjoerg char *fname; 17321015Sjoerg char *tmsg; 17421015Sjoerg 17521015Sjoerg if(uflag == 0) 17621015Sjoerg return; 17721015Sjoerg if ((df = fopen(dumpdates, "r+")) == NULL) 17821015Sjoerg quit("cannot rewrite %s: %s\n", dumpdates, strerror(errno)); 17921015Sjoerg fd = fileno(df); 18021015Sjoerg (void) flock(fd, LOCK_EX); 18121015Sjoerg fname = disk; 18221015Sjoerg free((char *)ddatev); 18374858Ssobomax ddatev = 0; 18474858Ssobomax nddates = 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 = time32_to_time(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 if (spcl.c_date == 0) { 219 tmsg = "the epoch\n"; 220 } else { 221 time_t t = time32_to_time(spcl.c_date); 222 tmsg = ctime(&t); 223 } 224 msg("level %c dump on %s", level, tmsg); 225} 226 227static void 228dumprecout(file, what) 229 FILE *file; 230 struct dumpdates *what; 231{ 232 233 if (fprintf(file, DUMPOUTFMT, 234 what->dd_name, 235 what->dd_level, 236 ctime(&what->dd_ddate)) < 0) 237 quit("%s: %s\n", dumpdates, strerror(errno)); 238} 239 240int recno; 241 242static int 243getrecord(df, ddatep) 244 FILE *df; 245 struct dumpdates *ddatep; 246{ 247 char tbuf[BUFSIZ]; 248 249 recno = 0; 250 if ( (fgets(tbuf, sizeof (tbuf), df)) != tbuf) 251 return(-1); 252 recno++; 253 if (makedumpdate(ddatep, tbuf) < 0) 254 msg("Unknown intermediate format in %s, line %d\n", 255 dumpdates, recno); 256 257#ifdef FDEBUG 258 msg("getrecord: %s %c %s", ddatep->dd_name, ddatep->dd_level, 259 ddatep->dd_ddate == 0 ? "the epoch\n" : ctime(&ddatep->dd_ddate)); 260#endif 261 return(0); 262} 263 264static int 265makedumpdate(ddp, tbuf) 266 struct dumpdates *ddp; 267 char *tbuf; 268{ 269 char un_buf[128]; 270 271 (void) sscanf(tbuf, DUMPINFMT, ddp->dd_name, &ddp->dd_level, un_buf); 272 ddp->dd_ddate = unctime(un_buf); 273 if (ddp->dd_ddate < 0) 274 return(-1); 275 return(0); 276} 277