xref: /freebsd/sbin/dump/itime.c (revision 0957b409a90fd597c1e9124cbaf3edd2b488f4ac)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
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 #ifndef lint
33 #if 0
34 static char sccsid[] = "@(#)itime.c	8.1 (Berkeley) 6/5/93";
35 #endif
36 static const char rcsid[] =
37   "$FreeBSD$";
38 #endif /* not lint */
39 
40 #include <sys/param.h>
41 #include <sys/queue.h>
42 
43 #include <ufs/ufs/dinode.h>
44 
45 #include <protocols/dumprestore.h>
46 
47 #include <errno.h>
48 #include <fcntl.h>
49 #include <limits.h>
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <string.h>
53 #include <time.h>
54 #include <timeconv.h>
55 
56 #include "dump.h"
57 
58 struct dumptime {
59 	struct	dumpdates dt_value;
60 	SLIST_ENTRY(dumptime) dt_list;
61 };
62 SLIST_HEAD(dthead, dumptime) dthead = SLIST_HEAD_INITIALIZER(dthead);
63 struct	dumpdates **ddatev = NULL;
64 int	nddates = 0;
65 
66 static	void dumprecout(FILE *, const struct dumpdates *);
67 static	int getrecord(FILE *, struct dumpdates *);
68 static	int makedumpdate(struct dumpdates *, const char *);
69 static	void readdumptimes(FILE *);
70 
71 void
72 initdumptimes(void)
73 {
74 	FILE *df;
75 
76 	if ((df = fopen(dumpdates, "r")) == NULL) {
77 		if (errno != ENOENT) {
78 			msg("WARNING: cannot read %s: %s\n", dumpdates,
79 			    strerror(errno));
80 			return;
81 		}
82 		/*
83 		 * Dumpdates does not exist, make an empty one.
84 		 */
85 		msg("WARNING: no file `%s', making an empty one\n", dumpdates);
86 		if ((df = fopen(dumpdates, "w")) == NULL) {
87 			msg("WARNING: cannot create %s: %s\n", dumpdates,
88 			    strerror(errno));
89 			return;
90 		}
91 		(void) fclose(df);
92 		if ((df = fopen(dumpdates, "r")) == NULL) {
93 			quit("cannot read %s even after creating it: %s\n",
94 			    dumpdates, strerror(errno));
95 			/* NOTREACHED */
96 		}
97 	}
98 	(void) flock(fileno(df), LOCK_SH);
99 	readdumptimes(df);
100 	(void) fclose(df);
101 }
102 
103 static void
104 readdumptimes(FILE *df)
105 {
106 	int i;
107 	struct	dumptime *dtwalk;
108 
109 	for (;;) {
110 		dtwalk = (struct dumptime *)calloc(1, sizeof (struct dumptime));
111 		if (getrecord(df, &(dtwalk->dt_value)) < 0) {
112 			free(dtwalk);
113 			break;
114 		}
115 		nddates++;
116 		SLIST_INSERT_HEAD(&dthead, dtwalk, dt_list);
117 	}
118 
119 	/*
120 	 *	arrayify the list, leaving enough room for the additional
121 	 *	record that we may have to add to the ddate structure
122 	 */
123 	ddatev = calloc((unsigned) (nddates + 1), sizeof (struct dumpdates *));
124 	dtwalk = SLIST_FIRST(&dthead);
125 	for (i = nddates - 1; i >= 0; i--, dtwalk = SLIST_NEXT(dtwalk, dt_list))
126 		ddatev[i] = &dtwalk->dt_value;
127 }
128 
129 void
130 getdumptime(void)
131 {
132 	struct dumpdates *ddp;
133 	int i;
134 	char *fname;
135 
136 	fname = disk;
137 #ifdef FDEBUG
138 	msg("Looking for name %s in dumpdates = %s for level = %d\n",
139 		fname, dumpdates, level);
140 #endif
141 	spcl.c_ddate = 0;
142 	lastlevel = 0;
143 
144 	initdumptimes();
145 	/*
146 	 *	Go find the entry with the same name for a lower increment
147 	 *	and older date
148 	 */
149 	ITITERATE(i, ddp) {
150 		if (strncmp(fname, ddp->dd_name, sizeof (ddp->dd_name)) != 0)
151 			continue;
152 		if (ddp->dd_level >= level)
153 			continue;
154 		if (ddp->dd_ddate <= _time64_to_time(spcl.c_ddate))
155 			continue;
156 		spcl.c_ddate = _time_to_time64(ddp->dd_ddate);
157 		lastlevel = ddp->dd_level;
158 	}
159 }
160 
161 void
162 putdumptime(void)
163 {
164 	FILE *df;
165 	struct dumpdates *dtwalk;
166 	int i;
167 	int fd;
168 	char *fname;
169 	char *tmsg;
170 
171 	if(uflag == 0)
172 		return;
173 	if ((df = fopen(dumpdates, "r+")) == NULL)
174 		quit("cannot rewrite %s: %s\n", dumpdates, strerror(errno));
175 	fd = fileno(df);
176 	(void) flock(fd, LOCK_EX);
177 	fname = disk;
178 	free(ddatev);
179 	ddatev = NULL;
180 	nddates = 0;
181 	readdumptimes(df);
182 	if (fseek(df, 0L, 0) < 0)
183 		quit("fseek: %s\n", strerror(errno));
184 	spcl.c_ddate = 0;
185 	ITITERATE(i, dtwalk) {
186 		if (strncmp(fname, dtwalk->dd_name,
187 				sizeof (dtwalk->dd_name)) != 0)
188 			continue;
189 		if (dtwalk->dd_level != level)
190 			continue;
191 		goto found;
192 	}
193 	/*
194 	 *	construct the new upper bound;
195 	 *	Enough room has been allocated.
196 	 */
197 	dtwalk = ddatev[nddates] =
198 		(struct dumpdates *)calloc(1, sizeof (struct dumpdates));
199 	nddates += 1;
200   found:
201 	(void) strncpy(dtwalk->dd_name, fname, sizeof (dtwalk->dd_name));
202 	dtwalk->dd_level = level;
203 	dtwalk->dd_ddate = _time64_to_time(spcl.c_date);
204 
205 	ITITERATE(i, dtwalk) {
206 		dumprecout(df, dtwalk);
207 	}
208 	if (fflush(df))
209 		quit("%s: %s\n", dumpdates, strerror(errno));
210 	if (ftruncate(fd, ftell(df)))
211 		quit("ftruncate (%s): %s\n", dumpdates, strerror(errno));
212 	(void) fclose(df);
213 	if (spcl.c_date == 0) {
214 		tmsg = "the epoch\n";
215 	} else {
216 		time_t t = _time64_to_time(spcl.c_date);
217 		tmsg = ctime(&t);
218 	}
219 	msg("level %d dump on %s", level, tmsg);
220 }
221 
222 static void
223 dumprecout(FILE *file, const struct dumpdates *what)
224 {
225 
226 	if (strlen(what->dd_name) > DUMPFMTLEN)
227 		quit("Name '%s' exceeds DUMPFMTLEN (%d) bytes\n",
228 		    what->dd_name, DUMPFMTLEN);
229 	if (fprintf(file, DUMPOUTFMT, DUMPFMTLEN, what->dd_name,
230 	      what->dd_level, ctime(&what->dd_ddate)) < 0)
231 		quit("%s: %s\n", dumpdates, strerror(errno));
232 }
233 
234 int	recno;
235 
236 static int
237 getrecord(FILE *df, struct dumpdates *ddatep)
238 {
239 	char tbuf[BUFSIZ];
240 
241 	recno = 0;
242 	if ( (fgets(tbuf, sizeof (tbuf), df)) != tbuf)
243 		return(-1);
244 	recno++;
245 	if (makedumpdate(ddatep, tbuf) < 0)
246 		msg("Unknown intermediate format in %s, line %d\n",
247 			dumpdates, recno);
248 
249 #ifdef FDEBUG
250 	msg("getrecord: %s %d %s", ddatep->dd_name, ddatep->dd_level,
251 	    ddatep->dd_ddate == 0 ? "the epoch\n" : ctime(&ddatep->dd_ddate));
252 #endif
253 	return(0);
254 }
255 
256 static int
257 makedumpdate(struct dumpdates *ddp, const char *tbuf)
258 {
259 	char un_buf[128];
260 
261 	(void) sscanf(tbuf, DUMPINFMT, ddp->dd_name, &ddp->dd_level, un_buf);
262 	ddp->dd_ddate = unctime(un_buf);
263 	if (ddp->dd_ddate < 0)
264 		return(-1);
265 	return(0);
266 }
267