itime.c revision 98542
1/*-
2 * Copyright (c) 1980, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 *    must display the following acknowledgement:
15 *	This product includes software developed by the University of
16 *	California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 *    may be used to endorse or promote products derived from this software
19 *    without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#ifndef lint
35#if 0
36static char sccsid[] = "@(#)itime.c	8.1 (Berkeley) 6/5/93";
37#endif
38static const char rcsid[] =
39  "$FreeBSD: head/sbin/dump/itime.c 98542 2002-06-21 06:18:05Z mckusick $";
40#endif /* not lint */
41
42#include <sys/param.h>
43#include <sys/queue.h>
44#include <sys/time.h>
45
46#include <ufs/ufs/dinode.h>
47
48#include <protocols/dumprestore.h>
49
50#include <errno.h>
51#include <fcntl.h>
52#include <stdio.h>
53#include <stdlib.h>
54#include <string.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);
63struct	dumpdates **ddatev = 0;
64int	nddates = 0;
65int	ddates_in = 0;
66
67static	void dumprecout(FILE *, const struct dumpdates *);
68static	int getrecord(FILE *, struct dumpdates *);
69static	int makedumpdate(struct dumpdates *, const char *);
70static	void readdumptimes(FILE *);
71
72void
73initdumptimes(void)
74{
75	FILE *df;
76
77	if ((df = fopen(dumpdates, "r")) == NULL) {
78		if (errno != ENOENT) {
79			msg("WARNING: cannot read %s: %s\n", dumpdates,
80			    strerror(errno));
81			return;
82		}
83		/*
84		 * Dumpdates does not exist, make an empty one.
85		 */
86		msg("WARNING: no file `%s', making an empty one\n", dumpdates);
87		if ((df = fopen(dumpdates, "w")) == NULL) {
88			msg("WARNING: cannot create %s: %s\n", dumpdates,
89			    strerror(errno));
90			return;
91		}
92		(void) fclose(df);
93		if ((df = fopen(dumpdates, "r")) == NULL) {
94			quit("cannot read %s even after creating it: %s\n",
95			    dumpdates, strerror(errno));
96			/* NOTREACHED */
97		}
98	}
99	(void) flock(fileno(df), LOCK_SH);
100	readdumptimes(df);
101	(void) fclose(df);
102}
103
104static void
105readdumptimes(FILE *df)
106{
107	int i;
108	struct	dumptime *dtwalk;
109
110	for (;;) {
111		dtwalk = (struct dumptime *)calloc(1, sizeof (struct dumptime));
112		if (getrecord(df, &(dtwalk->dt_value)) < 0)
113			break;
114		nddates++;
115		SLIST_INSERT_HEAD(&dthead, dtwalk, dt_list);
116	}
117
118	ddates_in = 1;
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 = (struct dumpdates **)
124		calloc((unsigned) (nddates + 1), sizeof (struct dumpdates *));
125	dtwalk = SLIST_FIRST(&dthead);
126	for (i = nddates - 1; i >= 0; i--, dtwalk = SLIST_NEXT(dtwalk, dt_list))
127		ddatev[i] = &dtwalk->dt_value;
128}
129
130void
131getdumptime(void)
132{
133	struct dumpdates *ddp;
134	int i;
135	char *fname;
136
137	fname = disk;
138#ifdef FDEBUG
139	msg("Looking for name %s in dumpdates = %s for level = %c\n",
140		fname, dumpdates, level);
141#endif
142	spcl.c_ddate = 0;
143	lastlevel = '0';
144
145	initdumptimes();
146	/*
147	 *	Go find the entry with the same name for a lower increment
148	 *	and older date
149	 */
150	ITITERATE(i, ddp) {
151		if (strncmp(fname, ddp->dd_name, sizeof (ddp->dd_name)) != 0)
152			continue;
153		if (ddp->dd_level >= level)
154			continue;
155		if (ddp->dd_ddate <= _time64_to_time(spcl.c_ddate))
156			continue;
157		spcl.c_ddate = _time_to_time64(ddp->dd_ddate);
158		lastlevel = ddp->dd_level;
159	}
160}
161
162void
163putdumptime(void)
164{
165	FILE *df;
166	struct dumpdates *dtwalk;
167	int i;
168	int fd;
169	char *fname;
170	char *tmsg;
171
172	if(uflag == 0)
173		return;
174	if ((df = fopen(dumpdates, "r+")) == NULL)
175		quit("cannot rewrite %s: %s\n", dumpdates, strerror(errno));
176	fd = fileno(df);
177	(void) flock(fd, LOCK_EX);
178	fname = disk;
179	free((char *)ddatev);
180	ddatev = 0;
181	nddates = 0;
182	ddates_in = 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 %c dump on %s", level, tmsg);
222}
223
224static void
225dumprecout(FILE *file, const struct dumpdates *what)
226{
227
228	if (fprintf(file, DUMPOUTFMT, what->dd_name,
229	      what->dd_level, ctime(&what->dd_ddate)) < 0)
230		quit("%s: %s\n", dumpdates, strerror(errno));
231}
232
233int	recno;
234
235static int
236getrecord(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
255static int
256makedumpdate(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