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