1 /* $OpenBSD: itime.c,v 1.5 1998/08/07 17:29:24 millert Exp $ */ 2 /* $NetBSD: itime.c,v 1.4 1997/04/15 01:09:50 lukem Exp $ */ 3 4 /*- 5 * Copyright (c) 1980, 1993 6 * The Regents of the University of California. All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed by the University of 19 * California, Berkeley and its contributors. 20 * 4. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 */ 36 37 #ifndef lint 38 #if 0 39 static char sccsid[] = "@(#)itime.c 8.1 (Berkeley) 6/5/93"; 40 #else 41 static char rcsid[] = "$OpenBSD: itime.c,v 1.5 1998/08/07 17:29:24 millert Exp $"; 42 #endif 43 #endif /* not lint */ 44 45 #include <sys/param.h> 46 #include <sys/time.h> 47 #ifdef sunos 48 #include <sys/vnode.h> 49 50 #include <ufs/fsdir.h> 51 #include <ufs/inode.h> 52 #include <ufs/fs.h> 53 #else 54 #include <ufs/ufs/dinode.h> 55 #endif 56 57 #include <protocols/dumprestore.h> 58 59 #include <errno.h> 60 #include <fcntl.h> 61 #include <stdio.h> 62 #include <time.h> 63 #ifdef __STDC__ 64 #include <stdlib.h> 65 #include <string.h> 66 #include <unistd.h> 67 #endif 68 69 #include "dump.h" 70 71 struct dumpdates **ddatev = 0; 72 int nddates = 0; 73 int ddates_in = 0; 74 struct dumptime *dthead = 0; 75 76 static void dumprecout __P((FILE *, struct dumpdates *)); 77 static int getrecord __P((FILE *, struct dumpdates *)); 78 static int makedumpdate __P((struct dumpdates *, char *)); 79 static void readdumptimes __P((FILE *)); 80 81 void 82 initdumptimes() 83 { 84 FILE *df; 85 86 if ((df = fopen(dumpdates, "r")) == NULL) { 87 if (errno != ENOENT) { 88 quit("cannot read %s: %s\n", dumpdates, 89 strerror(errno)); 90 /* NOTREACHED */ 91 } 92 /* 93 * Dumpdates does not exist, make an empty one. 94 */ 95 msg("WARNING: no file `%s', making an empty one\n", dumpdates); 96 if ((df = fopen(dumpdates, "w")) == NULL) { 97 quit("cannot create %s: %s\n", dumpdates, 98 strerror(errno)); 99 /* NOTREACHED */ 100 } 101 (void) fclose(df); 102 if ((df = fopen(dumpdates, "r")) == NULL) { 103 quit("cannot read %s even after creating it: %s\n", 104 dumpdates, strerror(errno)); 105 /* NOTREACHED */ 106 } 107 } 108 (void) flock(fileno(df), LOCK_SH); 109 readdumptimes(df); 110 (void) fclose(df); 111 } 112 113 static void 114 readdumptimes(df) 115 FILE *df; 116 { 117 register int i; 118 register struct dumptime *dtwalk; 119 120 for (;;) { 121 dtwalk = (struct dumptime *)calloc(1, sizeof(struct dumptime)); 122 if (getrecord(df, &(dtwalk->dt_value)) < 0) 123 break; 124 nddates++; 125 dtwalk->dt_next = dthead; 126 dthead = dtwalk; 127 } 128 129 ddates_in = 1; 130 /* 131 * arrayify the list, leaving enough room for the additional 132 * record that we may have to add to the ddate structure 133 */ 134 ddatev = (struct dumpdates **) 135 calloc((unsigned) (nddates + 1), sizeof(struct dumpdates *)); 136 dtwalk = dthead; 137 for (i = nddates - 1; i >= 0; i--, dtwalk = dtwalk->dt_next) 138 ddatev[i] = &dtwalk->dt_value; 139 } 140 141 void 142 getdumptime() 143 { 144 register struct dumpdates *ddp; 145 register int i; 146 char *fname; 147 148 fname = disk; 149 #ifdef FDEBUG 150 msg("Looking for name %s in dumpdates = %s for level = %c\n", 151 fname, dumpdates, level); 152 #endif 153 spcl.c_ddate = 0; 154 lastlevel = '0'; 155 156 initdumptimes(); 157 /* 158 * Go find the entry with the same name for a lower increment 159 * and older date 160 */ 161 ITITERATE(i, ddp) { 162 if (strncmp(fname, ddp->dd_name, sizeof(ddp->dd_name)) != 0) 163 continue; 164 if (ddp->dd_level >= level) 165 continue; 166 if (ddp->dd_ddate <= spcl.c_ddate) 167 continue; 168 spcl.c_ddate = ddp->dd_ddate; 169 lastlevel = ddp->dd_level; 170 } 171 } 172 173 void 174 putdumptime() 175 { 176 FILE *df; 177 register struct dumpdates *dtwalk; 178 register int i; 179 int fd; 180 char *fname; 181 182 if(uflag == 0) 183 return; 184 if ((df = fopen(dumpdates, "r+")) == NULL) 185 quit("cannot rewrite %s: %s\n", dumpdates, strerror(errno)); 186 fd = fileno(df); 187 (void) flock(fd, LOCK_EX); 188 fname = disk; 189 free((char *)ddatev); 190 ddatev = 0; 191 nddates = 0; 192 dthead = 0; 193 ddates_in = 0; 194 readdumptimes(df); 195 if (fseek(df, 0L, 0) < 0) 196 quit("fseek: %s\n", strerror(errno)); 197 spcl.c_ddate = 0; 198 ITITERATE(i, dtwalk) { 199 if (strncmp(fname, dtwalk->dd_name, 200 sizeof(dtwalk->dd_name)) != 0) 201 continue; 202 if (dtwalk->dd_level != level) 203 continue; 204 goto found; 205 } 206 /* 207 * construct the new upper bound; 208 * Enough room has been allocated. 209 */ 210 dtwalk = ddatev[nddates] = 211 (struct dumpdates *)calloc(1, sizeof(struct dumpdates)); 212 nddates += 1; 213 found: 214 (void) strlcpy(dtwalk->dd_name, fname, sizeof(dtwalk->dd_name)); 215 dtwalk->dd_level = level; 216 dtwalk->dd_ddate = spcl.c_date; 217 218 ITITERATE(i, dtwalk) { 219 dumprecout(df, dtwalk); 220 } 221 if (fflush(df)) 222 quit("%s: %s\n", dumpdates, strerror(errno)); 223 if (ftruncate(fd, ftell(df))) 224 quit("ftruncate (%s): %s\n", dumpdates, strerror(errno)); 225 (void) fclose(df); 226 msg("level %c dump on %s", level, 227 spcl.c_date == 0 ? "the epoch\n" : ctime(&spcl.c_date)); 228 } 229 230 static void 231 dumprecout(file, what) 232 FILE *file; 233 struct dumpdates *what; 234 { 235 236 if (fprintf(file, DUMPOUTFMT, 237 what->dd_name, 238 what->dd_level, 239 ctime(&what->dd_ddate)) < 0) 240 quit("%s: %s\n", dumpdates, strerror(errno)); 241 } 242 243 int recno; 244 245 static int 246 getrecord(df, ddatep) 247 FILE *df; 248 struct dumpdates *ddatep; 249 { 250 char tbuf[BUFSIZ]; 251 252 recno = 0; 253 if (fgets(tbuf, sizeof(tbuf), df) == NULL) 254 return(-1); 255 recno++; 256 if (makedumpdate(ddatep, tbuf) < 0) 257 msg("Unknown intermediate format in %s, line %d\n", 258 dumpdates, recno); 259 260 #ifdef FDEBUG 261 msg("getrecord: %s %c %s", ddatep->dd_name, ddatep->dd_level, 262 ddatep->dd_ddate == 0 ? "the epoch\n" : ctime(&ddatep->dd_ddate)); 263 #endif 264 return(0); 265 } 266 267 static int 268 makedumpdate(ddp, tbuf) 269 struct dumpdates *ddp; 270 char *tbuf; 271 { 272 char un_buf[BUFSIZ], *str; 273 struct tm then; 274 275 if (sscanf(tbuf, DUMPINFMT, ddp->dd_name, &ddp->dd_level, un_buf) != 3) 276 return(-1); 277 str = strptime(un_buf, "%a %b %e %H:%M:%S %Y", &then); 278 then.tm_isdst = -1; 279 if (str == NULL || (*str != '\n' && *str != '\0')) 280 ddp->dd_ddate = (time_t) -1; 281 else 282 ddp->dd_ddate = mktime(&then); 283 if (ddp->dd_ddate < 0) 284 return(-1); 285 return(0); 286 } 287