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