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