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
34static char sccsid[] = "@(#)itime.c	8.1 (Berkeley) 6/5/93";
35#endif
36static 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
58struct dumptime {
59	struct	dumpdates dt_value;
60	SLIST_ENTRY(dumptime) dt_list;
61};
62SLIST_HEAD(dthead, dumptime) dthead = SLIST_HEAD_INITIALIZER(dthead);
63int	nddates = 0;		/* number of records (might be zero) */
64struct	dumpdates **ddatev;	/* the arrayfied version */
65char	*dumpdates;		/* name of the file containing dump date info */
66int	lastlevel;		/* dump level of previous dump */
67
68static	void dumprecout(FILE *, const struct dumpdates *);
69static	int getrecord(FILE *, struct dumpdates *);
70static	int makedumpdate(struct dumpdates *, const char *);
71static	void readdumptimes(FILE *);
72
73void
74initdumptimes(void)
75{
76	FILE *df;
77
78	if ((df = fopen(dumpdates, "r")) == NULL) {
79		if (errno != ENOENT) {
80			msg("WARNING: cannot read %s: %s\n", dumpdates,
81			    strerror(errno));
82			return;
83		}
84		/*
85		 * Dumpdates does not exist, make an empty one.
86		 */
87		msg("WARNING: no file `%s', making an empty one\n", dumpdates);
88		if ((df = fopen(dumpdates, "w")) == NULL) {
89			msg("WARNING: cannot create %s: %s\n", dumpdates,
90			    strerror(errno));
91			return;
92		}
93		(void) fclose(df);
94		if ((df = fopen(dumpdates, "r")) == NULL) {
95			quit("cannot read %s even after creating it: %s\n",
96			    dumpdates, strerror(errno));
97			/* NOTREACHED */
98		}
99	}
100	(void) flock(fileno(df), LOCK_SH);
101	readdumptimes(df);
102	(void) fclose(df);
103}
104
105static void
106readdumptimes(FILE *df)
107{
108	int i;
109	struct	dumptime *dtwalk;
110
111	for (;;) {
112		dtwalk = (struct dumptime *)calloc(1, sizeof (struct dumptime));
113		if (getrecord(df, &(dtwalk->dt_value)) < 0) {
114			free(dtwalk);
115			break;
116		}
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 = calloc((unsigned) (nddates + 1), sizeof (struct dumpdates *));
126	dtwalk = SLIST_FIRST(&dthead);
127	for (i = nddates - 1; i >= 0; i--, dtwalk = SLIST_NEXT(dtwalk, dt_list))
128		ddatev[i] = &dtwalk->dt_value;
129}
130
131void
132getdumptime(void)
133{
134	struct dumpdates *ddp;
135	int i;
136	char *fname;
137
138	fname = disk;
139#ifdef FDEBUG
140	msg("Looking for name %s in dumpdates = %s for level = %d\n",
141		fname, dumpdates, level);
142#endif
143	spcl.c_ddate = 0;
144	lastlevel = 0;
145
146	initdumptimes();
147	/*
148	 *	Go find the entry with the same name for a lower increment
149	 *	and older date
150	 */
151	ITITERATE(i, ddp) {
152		if (strncmp(fname, ddp->dd_name, sizeof (ddp->dd_name)) != 0)
153			continue;
154		if (ddp->dd_level >= level)
155			continue;
156		if (ddp->dd_ddate <= _time64_to_time(spcl.c_ddate))
157			continue;
158		spcl.c_ddate = _time_to_time64(ddp->dd_ddate);
159		lastlevel = ddp->dd_level;
160	}
161}
162
163void
164putdumptime(void)
165{
166	FILE *df;
167	struct dumpdates *dtwalk;
168	int i;
169	int fd;
170	char *fname;
171	char *tmsg;
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(ddatev);
181	ddatev = NULL;
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 *)calloc(1, sizeof (struct dumpdates));
201	nddates += 1;
202  found:
203	(void) strncpy(dtwalk->dd_name, fname, sizeof (dtwalk->dd_name));
204	dtwalk->dd_level = level;
205	dtwalk->dd_ddate = _time64_to_time(spcl.c_date);
206
207	ITITERATE(i, dtwalk) {
208		dumprecout(df, dtwalk);
209	}
210	if (fflush(df))
211		quit("%s: %s\n", dumpdates, strerror(errno));
212	if (ftruncate(fd, ftell(df)))
213		quit("ftruncate (%s): %s\n", dumpdates, strerror(errno));
214	(void) fclose(df);
215	if (spcl.c_date == 0) {
216		tmsg = "the epoch\n";
217	} else {
218		time_t t = _time64_to_time(spcl.c_date);
219		tmsg = ctime(&t);
220	}
221	msg("level %d dump on %s", level, tmsg);
222}
223
224static void
225dumprecout(FILE *file, const struct dumpdates *what)
226{
227
228	if (strlen(what->dd_name) > DUMPFMTLEN)
229		quit("Name '%s' exceeds DUMPFMTLEN (%d) bytes\n",
230		    what->dd_name, DUMPFMTLEN);
231	if (fprintf(file, DUMPOUTFMT, DUMPFMTLEN, what->dd_name,
232	      what->dd_level, ctime(&what->dd_ddate)) < 0)
233		quit("%s: %s\n", dumpdates, strerror(errno));
234}
235
236int	recno;
237
238static int
239getrecord(FILE *df, struct dumpdates *ddatep)
240{
241	char tbuf[BUFSIZ];
242
243	recno = 0;
244	if ( (fgets(tbuf, sizeof (tbuf), df)) != tbuf)
245		return(-1);
246	recno++;
247	if (makedumpdate(ddatep, tbuf) < 0)
248		msg("Unknown intermediate format in %s, line %d\n",
249			dumpdates, recno);
250
251#ifdef FDEBUG
252	msg("getrecord: %s %d %s", ddatep->dd_name, ddatep->dd_level,
253	    ddatep->dd_ddate == 0 ? "the epoch\n" : ctime(&ddatep->dd_ddate));
254#endif
255	return(0);
256}
257
258static int
259makedumpdate(struct dumpdates *ddp, const char *tbuf)
260{
261	char un_buf[128];
262
263	(void) sscanf(tbuf, DUMPINFMT, ddp->dd_name, &ddp->dd_level, un_buf);
264	ddp->dd_ddate = unctime(un_buf);
265	if (ddp->dd_ddate < 0)
266		return(-1);
267	return(0);
268}
269