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