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