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