1 /*- 2 * Copyright (c) 1980 The Regents of the University of California. 3 * 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 /* from: static char sccsid[] = "@(#)itime.c 5.15 (Berkeley) 6/18/92"; */ 36 static char *rcsid = "$Id: itime.c,v 1.1 1993/12/22 10:24:44 cgd Exp $"; 37 #endif /* not lint */ 38 39 #ifdef sunos 40 #include <stdio.h> 41 #include <ctype.h> 42 #include <sys/param.h> 43 #include <sys/stat.h> 44 #include <ufs/fs.h> 45 #else 46 #include <sys/param.h> 47 #include <sys/time.h> 48 #endif 49 #include <ufs/dinode.h> 50 #include <fcntl.h> 51 #include <protocols/dumprestore.h> 52 #include <errno.h> 53 #include <stdio.h> 54 #ifdef __STDC__ 55 #include <time.h> 56 #include <unistd.h> 57 #include <stdlib.h> 58 #include <string.h> 59 #endif 60 #include "dump.h" 61 62 struct dumpdates **ddatev = 0; 63 int nddates = 0; 64 int ddates_in = 0; 65 struct dumptime *dthead = 0; 66 67 void readdumptimes(); 68 int getrecord(); 69 int makedumpdate(); 70 71 static void dumprecout(); 72 73 void 74 initdumptimes() 75 { 76 FILE *df; 77 78 if ((df = fopen(dumpdates, "r")) == NULL) { 79 if (errno != ENOENT) { 80 quit("cannot read %s: %s\n", dumpdates, 81 strerror(errno)); 82 /* NOTREACHED */ 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 quit("cannot create %s: %s\n", dumpdates, 90 strerror(errno)); 91 /* NOTREACHED */ 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 105 void 106 readdumptimes(df) 107 FILE *df; 108 { 109 register int i; 110 register struct dumptime *dtwalk; 111 112 for (;;) { 113 dtwalk = (struct dumptime *)calloc(1, sizeof (struct dumptime)); 114 if (getrecord(df, &(dtwalk->dt_value)) < 0) 115 break; 116 nddates++; 117 dtwalk->dt_next = dthead; 118 dthead = dtwalk; 119 } 120 121 ddates_in = 1; 122 /* 123 * arrayify the list, leaving enough room for the additional 124 * record that we may have to add to the ddate structure 125 */ 126 ddatev = (struct dumpdates **) 127 calloc((unsigned) (nddates + 1), sizeof (struct dumpdates *)); 128 dtwalk = dthead; 129 for (i = nddates - 1; i >= 0; i--, dtwalk = dtwalk->dt_next) 130 ddatev[i] = &dtwalk->dt_value; 131 } 132 133 void 134 getdumptime() 135 { 136 register struct dumpdates *ddp; 137 register int i; 138 char *fname; 139 140 fname = disk; 141 #ifdef FDEBUG 142 msg("Looking for name %s in dumpdates = %s for level = %c\n", 143 fname, dumpdates, level); 144 #endif 145 spcl.c_ddate = 0; 146 lastlevel = '0'; 147 148 initdumptimes(); 149 /* 150 * Go find the entry with the same name for a lower increment 151 * and older date 152 */ 153 ITITERATE(i, ddp) { 154 if (strncmp(fname, ddp->dd_name, sizeof (ddp->dd_name)) != 0) 155 continue; 156 if (ddp->dd_level >= level) 157 continue; 158 if (ddp->dd_ddate <= spcl.c_ddate) 159 continue; 160 spcl.c_ddate = ddp->dd_ddate; 161 lastlevel = ddp->dd_level; 162 } 163 } 164 165 void 166 putdumptime() 167 { 168 FILE *df; 169 register struct dumpdates *dtwalk; 170 register int i; 171 int fd; 172 char *fname; 173 174 if(uflag == 0) 175 return; 176 if ((df = fopen(dumpdates, "r+")) == NULL) 177 quit("cannot rewrite %s: %s\n", dumpdates, strerror(errno)); 178 fd = fileno(df); 179 (void) flock(fd, LOCK_EX); 180 fname = disk; 181 free((char *)ddatev); 182 ddatev = 0; 183 nddates = 0; 184 dthead = 0; 185 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 222 static void 223 dumprecout(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 235 int recno; 236 int 237 getrecord(df, ddatep) 238 FILE *df; 239 struct dumpdates *ddatep; 240 { 241 char tbuf[BUFSIZ]; 242 243 recno = 0; 244 if ( (fgets(tbuf, sizeof (tbuf), df)) != tbuf) 245 return(-1); 246 recno++; 247 if (makedumpdate(ddatep, tbuf) < 0) 248 msg("Unknown intermediate format in %s, line %d\n", 249 dumpdates, recno); 250 251 #ifdef FDEBUG 252 msg("getrecord: %s %c %s", ddatep->dd_name, ddatep->dd_level, 253 ddatep->dd_ddate == 0 ? "the epoch\n" : ctime(&ddatep->dd_ddate)); 254 #endif 255 return(0); 256 } 257 258 int 259 makedumpdate(ddp, tbuf) 260 struct dumpdates *ddp; 261 char *tbuf; 262 { 263 char un_buf[32]; 264 265 (void) sscanf(tbuf, DUMPINFMT, ddp->dd_name, &ddp->dd_level, un_buf); 266 ddp->dd_ddate = unctime(un_buf); 267 if (ddp->dd_ddate < 0) 268 return(-1); 269 return(0); 270 } 271