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