1 /* $OpenBSD: itime.c,v 1.25 2021/01/21 00:16:36 mortimer 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 int nddates = 0; 51 struct dumpdates **ddatev = NULL; 52 char *dumpdates = NULL; 53 char lastlevel = 0; 54 int ddates_in = 0; 55 struct dumptime *dthead = NULL; 56 57 static void dumprecout(FILE *, struct dumpdates *); 58 static int getrecord(FILE *, struct dumpdates *); 59 static int makedumpdate(struct dumpdates *, char *); 60 static void readdumptimes(FILE *); 61 62 void 63 initdumptimes(void) 64 { 65 FILE *df; 66 67 if ((df = fopen(dumpdates, "r")) == NULL) { 68 if (errno != ENOENT) { 69 quit("cannot read %s: %s\n", dumpdates, 70 strerror(errno)); 71 /* NOTREACHED */ 72 } 73 /* 74 * Dumpdates does not exist, make an empty one. 75 */ 76 msg("WARNING: no file `%s', making an empty one\n", dumpdates); 77 if ((df = fopen(dumpdates, "w")) == NULL) { 78 quit("cannot create %s: %s\n", dumpdates, 79 strerror(errno)); 80 /* NOTREACHED */ 81 } 82 (void) fclose(df); 83 if ((df = fopen(dumpdates, "r")) == NULL) { 84 quit("cannot read %s even after creating it: %s\n", 85 dumpdates, strerror(errno)); 86 /* NOTREACHED */ 87 } 88 } 89 (void) flock(fileno(df), LOCK_SH); 90 readdumptimes(df); 91 (void) fclose(df); 92 } 93 94 static void 95 readdumptimes(FILE *df) 96 { 97 int i; 98 struct dumptime *dtwalk; 99 100 for (;;) { 101 dtwalk = calloc(1, sizeof(struct dumptime)); 102 if (getrecord(df, &(dtwalk->dt_value)) < 0) { 103 free(dtwalk); 104 break; 105 } 106 nddates++; 107 dtwalk->dt_next = dthead; 108 dthead = dtwalk; 109 } 110 111 ddates_in = 1; 112 /* 113 * arrayify the list, leaving enough room for the additional 114 * record that we may have to add to the ddate structure 115 */ 116 ddatev = calloc((unsigned) (nddates + 1), sizeof(struct dumpdates *)); 117 dtwalk = dthead; 118 for (i = nddates - 1; i >= 0; i--, dtwalk = dtwalk->dt_next) 119 ddatev[i] = &dtwalk->dt_value; 120 } 121 122 void 123 getdumptime(void) 124 { 125 struct dumpdates *ddp; 126 int i; 127 char *fname; 128 129 fname = duid ? duid : disk; 130 #ifdef FDEBUG 131 msg("Looking for name %s in dumpdates = %s for level = %c\n", 132 fname, dumpdates, level); 133 #endif 134 spcl.c_ddate = 0; 135 lastlevel = '0'; 136 137 initdumptimes(); 138 /* 139 * Go find the entry with the same name for a lower increment 140 * and older date 141 */ 142 ITITERATE(i, ddp) { 143 if ((strncmp(fname, ddp->dd_name, sizeof(ddp->dd_name)) != 0) && 144 (strncmp(disk, ddp->dd_name, sizeof(ddp->dd_name)) != 0)) 145 continue; 146 if (ddp->dd_level >= level) 147 continue; 148 if (ddp->dd_ddate <= (time_t)spcl.c_ddate) 149 continue; 150 spcl.c_ddate = (int64_t)ddp->dd_ddate; 151 lastlevel = ddp->dd_level; 152 } 153 } 154 155 void 156 putdumptime(void) 157 { 158 FILE *df; 159 struct dumpdates *dtwalk; 160 int fd, i; 161 char *fname; 162 time_t t; 163 164 if(uflag == 0) 165 return; 166 if ((df = fopen(dumpdates, "r+")) == NULL) 167 quit("cannot rewrite %s: %s\n", dumpdates, strerror(errno)); 168 fd = fileno(df); 169 (void) flock(fd, LOCK_EX); 170 fname = duid ? duid : disk; 171 free(ddatev); 172 ddatev = NULL; 173 nddates = 0; 174 dthead = NULL; 175 ddates_in = 0; 176 readdumptimes(df); 177 if (fseek(df, 0L, SEEK_SET) == -1) 178 quit("fseek: %s\n", strerror(errno)); 179 spcl.c_ddate = 0; 180 ITITERATE(i, dtwalk) { 181 if ((strncmp(fname, dtwalk->dd_name, 182 sizeof(dtwalk->dd_name)) != 0) && 183 (strncmp(disk, dtwalk->dd_name, 184 sizeof(dtwalk->dd_name)) != 0)) 185 continue; 186 if (dtwalk->dd_level != level) 187 continue; 188 goto found; 189 } 190 /* 191 * construct the new upper bound; 192 * Enough room has been allocated. 193 */ 194 dtwalk = ddatev[nddates] = 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 = getduid(ddp->dd_name); 255 if (str != NULL) { 256 strlcpy(ddp->dd_name, str, sizeof(ddp->dd_name)); 257 free(str); 258 } 259 str = strptime(un_buf, "%a %b %e %H:%M:%S %Y", &then); 260 then.tm_isdst = -1; 261 if (str == NULL || (*str != '\n' && *str != '\0')) 262 ddp->dd_ddate = (time_t) -1; 263 else 264 ddp->dd_ddate = mktime(&then); 265 if (ddp->dd_ddate < 0) 266 return(-1); 267 return(0); 268 } 269