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