xref: /freebsd/sbin/dump/itime.c (revision bdcbfde31e8e9b343f113a1956384bdf30d1ed62)
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 #endif /* not lint */
34 
35 #include <sys/param.h>
36 #include <sys/queue.h>
37 
38 #include <ufs/ufs/dinode.h>
39 
40 #include <protocols/dumprestore.h>
41 
42 #include <errno.h>
43 #include <fcntl.h>
44 #include <limits.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <time.h>
49 #include <timeconv.h>
50 
51 #include "dump.h"
52 
53 struct dumptime {
54 	struct	dumpdates dt_value;
55 	SLIST_ENTRY(dumptime) dt_list;
56 };
57 SLIST_HEAD(dthead, dumptime) dthead = SLIST_HEAD_INITIALIZER(dthead);
58 int	nddates = 0;		/* number of records (might be zero) */
59 struct	dumpdates **ddatev;	/* the arrayfied version */
60 char	*dumpdates;		/* name of the file containing dump date info */
61 int	lastlevel;		/* dump level of previous dump */
62 
63 static	void dumprecout(FILE *, const 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
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 			quit("cannot read %s even after creating it: %s\n",
91 			    dumpdates, strerror(errno));
92 			/* NOTREACHED */
93 		}
94 	}
95 	(void) flock(fileno(df), LOCK_SH);
96 	readdumptimes(df);
97 	(void) fclose(df);
98 }
99 
100 static void
101 readdumptimes(FILE *df)
102 {
103 	int i;
104 	struct	dumptime *dtwalk;
105 
106 	for (;;) {
107 		dtwalk = (struct dumptime *)calloc(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 = calloc((unsigned) (nddates + 1), sizeof (struct dumpdates *));
121 	dtwalk = SLIST_FIRST(&dthead);
122 	for (i = nddates - 1; i >= 0; i--, dtwalk = SLIST_NEXT(dtwalk, dt_list))
123 		ddatev[i] = &dtwalk->dt_value;
124 }
125 
126 void
127 getdumptime(void)
128 {
129 	struct dumpdates *ddp;
130 	int i;
131 	char *fname;
132 
133 	fname = disk;
134 #ifdef FDEBUG
135 	msg("Looking for name %s in dumpdates = %s for level = %d\n",
136 		fname, dumpdates, level);
137 #endif
138 	spcl.c_ddate = 0;
139 	lastlevel = 0;
140 
141 	initdumptimes();
142 	/*
143 	 *	Go find the entry with the same name for a lower increment
144 	 *	and older date
145 	 */
146 	ITITERATE(i, ddp) {
147 		if (strncmp(fname, ddp->dd_name, sizeof (ddp->dd_name)) != 0)
148 			continue;
149 		if (ddp->dd_level >= level)
150 			continue;
151 		if (ddp->dd_ddate <= _time64_to_time(spcl.c_ddate))
152 			continue;
153 		spcl.c_ddate = _time_to_time64(ddp->dd_ddate);
154 		lastlevel = ddp->dd_level;
155 	}
156 }
157 
158 void
159 putdumptime(void)
160 {
161 	FILE *df;
162 	struct dumpdates *dtwalk;
163 	int i;
164 	int fd;
165 	char *fname;
166 	char *tmsg;
167 
168 	if(uflag == 0)
169 		return;
170 	if ((df = fopen(dumpdates, "r+")) == NULL)
171 		quit("cannot rewrite %s: %s\n", dumpdates, strerror(errno));
172 	fd = fileno(df);
173 	(void) flock(fd, LOCK_EX);
174 	fname = disk;
175 	free(ddatev);
176 	ddatev = NULL;
177 	nddates = 0;
178 	readdumptimes(df);
179 	if (fseek(df, 0L, 0) < 0)
180 		quit("fseek: %s\n", strerror(errno));
181 	spcl.c_ddate = 0;
182 	ITITERATE(i, dtwalk) {
183 		if (strncmp(fname, dtwalk->dd_name,
184 				sizeof (dtwalk->dd_name)) != 0)
185 			continue;
186 		if (dtwalk->dd_level != level)
187 			continue;
188 		goto found;
189 	}
190 	/*
191 	 *	construct the new upper bound;
192 	 *	Enough room has been allocated.
193 	 */
194 	dtwalk = ddatev[nddates] =
195 		(struct dumpdates *)calloc(1, sizeof (struct dumpdates));
196 	nddates += 1;
197   found:
198 	(void) strncpy(dtwalk->dd_name, fname, sizeof (dtwalk->dd_name));
199 	dtwalk->dd_level = level;
200 	dtwalk->dd_ddate = _time64_to_time(spcl.c_date);
201 
202 	ITITERATE(i, dtwalk) {
203 		dumprecout(df, dtwalk);
204 	}
205 	if (fflush(df))
206 		quit("%s: %s\n", dumpdates, strerror(errno));
207 	if (ftruncate(fd, ftell(df)))
208 		quit("ftruncate (%s): %s\n", dumpdates, strerror(errno));
209 	(void) fclose(df);
210 	if (spcl.c_date == 0) {
211 		tmsg = "the epoch\n";
212 	} else {
213 		time_t t = _time64_to_time(spcl.c_date);
214 		tmsg = ctime(&t);
215 	}
216 	msg("level %d dump on %s", level, tmsg);
217 }
218 
219 static void
220 dumprecout(FILE *file, const struct dumpdates *what)
221 {
222 
223 	if (strlen(what->dd_name) > DUMPFMTLEN)
224 		quit("Name '%s' exceeds DUMPFMTLEN (%d) bytes\n",
225 		    what->dd_name, DUMPFMTLEN);
226 	if (fprintf(file, DUMPOUTFMT, DUMPFMTLEN, what->dd_name,
227 	      what->dd_level, 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 %d %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, const 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