itime.c revision 99562
11573Srgrimes/*- 250476Speter * Copyright (c) 1980, 1993 31573Srgrimes * The Regents of the University of California. All rights reserved. 4114308Speter * 5114308Speter * Redistribution and use in source and binary forms, with or without 6114308Speter * 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 99562 2002-07-08 00:29:23Z iedowse $"; 40#endif /* not lint */ 41 42#include <sys/param.h> 43#include <sys/queue.h> 44#include <sys/time.h> 45 46#include <ufs/ufs/dinode.h> 47 48#include <protocols/dumprestore.h> 49 50#include <errno.h> 51#include <fcntl.h> 52#include <stdio.h> 53#include <stdlib.h> 54#include <string.h> 55#include <timeconv.h> 56 57#include "dump.h" 58 59struct dumptime { 60 struct dumpdates dt_value; 61 SLIST_ENTRY(dumptime) dt_list; 62}; 63SLIST_HEAD(dthead, dumptime) dthead = SLIST_HEAD_INITIALIZER(dthead); 64struct dumpdates **ddatev = 0; 65int nddates = 0; 66int ddates_in = 0; 67 68static void dumprecout(FILE *, const struct dumpdates *); 69static int getrecord(FILE *, struct dumpdates *); 70static int makedumpdate(struct dumpdates *, const char *); 71static void readdumptimes(FILE *); 72 73void 74initdumptimes(void) 75{ 76 FILE *df; 77 78 if ((df = fopen(dumpdates, "r")) == NULL) { 79 if (errno != ENOENT) { 80 msg("WARNING: cannot read %s: %s\n", dumpdates, 81 strerror(errno)); 82 return; 83 } 84 /* 85 * Dumpdates does not exist, make an empty one. 86 */ 87 msg("WARNING: no file `%s', making an empty one\n", dumpdates); 88 if ((df = fopen(dumpdates, "w")) == NULL) { 89 msg("WARNING: cannot create %s: %s\n", dumpdates, 90 strerror(errno)); 91 return; 92 } 93 (void) fclose(df); 94 if ((df = fopen(dumpdates, "r")) == NULL) { 95 quit("cannot read %s even after creating it: %s\n", 96 dumpdates, strerror(errno)); 97 /* NOTREACHED */ 98 } 99 } 100 (void) flock(fileno(df), LOCK_SH); 101 readdumptimes(df); 102 (void) fclose(df); 103} 104 105static void 106readdumptimes(FILE *df) 107{ 108 int i; 109 struct dumptime *dtwalk; 110 111 for (;;) { 112 dtwalk = (struct dumptime *)calloc(1, sizeof (struct dumptime)); 113 if (getrecord(df, &(dtwalk->dt_value)) < 0) 114 break; 115 nddates++; 116 SLIST_INSERT_HEAD(&dthead, dtwalk, dt_list); 117 } 118 119 ddates_in = 1; 120 /* 121 * arrayify the list, leaving enough room for the additional 122 * record that we may have to add to the ddate structure 123 */ 124 ddatev = (struct dumpdates **) 125 calloc((unsigned) (nddates + 1), sizeof (struct dumpdates *)); 126 dtwalk = SLIST_FIRST(&dthead); 127 for (i = nddates - 1; i >= 0; i--, dtwalk = SLIST_NEXT(dtwalk, dt_list)) 128 ddatev[i] = &dtwalk->dt_value; 129} 130 131void 132getdumptime(void) 133{ 134 struct dumpdates *ddp; 135 int i; 136 char *fname; 137 138 fname = disk; 139#ifdef FDEBUG 140 msg("Looking for name %s in dumpdates = %s for level = %c\n", 141 fname, dumpdates, level); 142#endif 143 spcl.c_ddate = 0; 144 lastlevel = '0'; 145 146 initdumptimes(); 147 /* 148 * Go find the entry with the same name for a lower increment 149 * and older date 150 */ 151 ITITERATE(i, ddp) { 152 if (strncmp(fname, ddp->dd_name, sizeof (ddp->dd_name)) != 0) 153 continue; 154 if (ddp->dd_level >= level) 155 continue; 156 if (ddp->dd_ddate <= _time64_to_time(spcl.c_ddate)) 157 continue; 158 spcl.c_ddate = _time_to_time64(ddp->dd_ddate); 159 lastlevel = ddp->dd_level; 160 } 161} 162 163void 164putdumptime(void) 165{ 166 FILE *df; 167 struct dumpdates *dtwalk; 168 int i; 169 int fd; 170 char *fname; 171 char *tmsg; 172 173 if(uflag == 0) 174 return; 175 if ((df = fopen(dumpdates, "r+")) == NULL) 176 quit("cannot rewrite %s: %s\n", dumpdates, strerror(errno)); 177 fd = fileno(df); 178 (void) flock(fd, LOCK_EX); 179 fname = disk; 180 free((char *)ddatev); 181 ddatev = 0; 182 nddates = 0; 183 ddates_in = 0; 184 readdumptimes(df); 185 if (fseek(df, 0L, 0) < 0) 186 quit("fseek: %s\n", strerror(errno)); 187 spcl.c_ddate = 0; 188 ITITERATE(i, dtwalk) { 189 if (strncmp(fname, dtwalk->dd_name, 190 sizeof (dtwalk->dd_name)) != 0) 191 continue; 192 if (dtwalk->dd_level != level) 193 continue; 194 goto found; 195 } 196 /* 197 * construct the new upper bound; 198 * Enough room has been allocated. 199 */ 200 dtwalk = ddatev[nddates] = 201 (struct dumpdates *)calloc(1, sizeof (struct dumpdates)); 202 nddates += 1; 203 found: 204 (void) strncpy(dtwalk->dd_name, fname, sizeof (dtwalk->dd_name)); 205 dtwalk->dd_level = level; 206 dtwalk->dd_ddate = _time64_to_time(spcl.c_date); 207 208 ITITERATE(i, dtwalk) { 209 dumprecout(df, dtwalk); 210 } 211 if (fflush(df)) 212 quit("%s: %s\n", dumpdates, strerror(errno)); 213 if (ftruncate(fd, ftell(df))) 214 quit("ftruncate (%s): %s\n", dumpdates, strerror(errno)); 215 (void) fclose(df); 216 if (spcl.c_date == 0) { 217 tmsg = "the epoch\n"; 218 } else { 219 time_t t = _time64_to_time(spcl.c_date); 220 tmsg = ctime(&t); 221 } 222 msg("level %c dump on %s", level, tmsg); 223} 224 225static void 226dumprecout(FILE *file, const struct dumpdates *what) 227{ 228 229 if (fprintf(file, DUMPOUTFMT, what->dd_name, 230 what->dd_level, ctime(&what->dd_ddate)) < 0) 231 quit("%s: %s\n", dumpdates, strerror(errno)); 232} 233 234int recno; 235 236static int 237getrecord(FILE *df, struct dumpdates *ddatep) 238{ 239 char tbuf[BUFSIZ]; 240 241 recno = 0; 242 if ( (fgets(tbuf, sizeof (tbuf), df)) != tbuf) 243 return(-1); 244 recno++; 245 if (makedumpdate(ddatep, tbuf) < 0) 246 msg("Unknown intermediate format in %s, line %d\n", 247 dumpdates, recno); 248 249#ifdef FDEBUG 250 msg("getrecord: %s %c %s", ddatep->dd_name, ddatep->dd_level, 251 ddatep->dd_ddate == 0 ? "the epoch\n" : ctime(&ddatep->dd_ddate)); 252#endif 253 return(0); 254} 255 256static int 257makedumpdate(struct dumpdates *ddp, const char *tbuf) 258{ 259 char un_buf[128]; 260 261 (void) sscanf(tbuf, DUMPINFMT, ddp->dd_name, &ddp->dd_level, un_buf); 262 ddp->dd_ddate = unctime(un_buf); 263 if (ddp->dd_ddate < 0) 264 return(-1); 265 return(0); 266} 267