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 static char sccsid[] = "@(#)itime.c 8.1 (Berkeley) 6/5/93"; 36 #endif /* not lint */ 37 38 #include <sys/param.h> 39 #include <sys/time.h> 40 #ifdef sunos 41 #include <sys/vnode.h> 42 43 #include <ufs/fsdir.h> 44 #include <ufs/inode.h> 45 #include <ufs/fs.h> 46 #else 47 #include <ufs/ufs/dinode.h> 48 #endif 49 50 #include <protocols/dumprestore.h> 51 52 #include <errno.h> 53 #include <fcntl.h> 54 #include <stdio.h> 55 #ifdef __STDC__ 56 #include <stdlib.h> 57 #include <string.h> 58 #include <unistd.h> 59 #endif 60 61 #include "dump.h" 62 63 struct dumpdates **ddatev = 0; 64 int nddates = 0; 65 int ddates_in = 0; 66 struct dumptime *dthead = 0; 67 68 static void dumprecout __P((FILE *, struct dumpdates *)); 69 static int getrecord __P((FILE *, struct dumpdates *)); 70 static int makedumpdate __P((struct dumpdates *, char *)); 71 static void readdumptimes __P((FILE *)); 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 static 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 237 static int 238 getrecord(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 259 static int 260 makedumpdate(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