xref: /dragonfly/sbin/dump/itime.c (revision 2c603719)
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  * @(#)itime.c	8.1 (Berkeley) 6/5/93
34  * $FreeBSD: src/sbin/dump/itime.c,v 1.3.2.1 2001/08/01 06:29:35 obrien Exp $
35  * $DragonFly: src/sbin/dump/itime.c,v 1.5 2003/11/01 17:15:58 drhodus Exp $
36  */
37 
38 #include <sys/param.h>
39 #include <sys/queue.h>
40 #include <sys/time.h>
41 #ifdef sunos
42 #include <sys/vnode.h>
43 
44 #include <ufs/fsdir.h>
45 #include <ufs/inode.h>
46 #include <ufs/fs.h>
47 #else
48 #include <vfs/ufs/dinode.h>
49 #endif
50 
51 #include <protocols/dumprestore.h>
52 
53 #include <errno.h>
54 #include <fcntl.h>
55 #include <stdio.h>
56 #ifdef __STDC__
57 #include <stdlib.h>
58 #include <string.h>
59 #endif
60 
61 #include "dump.h"
62 
63 struct dumptime {
64 	struct	dumpdates dt_value;
65 	SLIST_ENTRY(dumptime) dt_list;
66 };
67 SLIST_HEAD(dthead, dumptime) dthead = SLIST_HEAD_INITIALIZER(dthead);
68 struct	dumpdates **ddatev = 0;
69 int	nddates = 0;
70 int	ddates_in = 0;
71 
72 static	void dumprecout(FILE *, struct dumpdates *);
73 static	int getrecord(FILE *, struct dumpdates *);
74 static	int makedumpdate(struct dumpdates *, char *);
75 static	void readdumptimes(FILE *);
76 
77 void
78 initdumptimes(void)
79 {
80 	FILE *df;
81 
82 	if ((df = fopen(dumpdates, "r")) == NULL) {
83 		if (errno != ENOENT) {
84 			msg("WARNING: cannot read %s: %s\n", dumpdates,
85 			    strerror(errno));
86 			return;
87 		}
88 		/*
89 		 * Dumpdates does not exist, make an empty one.
90 		 */
91 		msg("WARNING: no file `%s', making an empty one\n", dumpdates);
92 		if ((df = fopen(dumpdates, "w")) == NULL) {
93 			msg("WARNING: cannot create %s: %s\n", dumpdates,
94 			    strerror(errno));
95 			return;
96 		}
97 		(void) fclose(df);
98 		if ((df = fopen(dumpdates, "r")) == NULL) {
99 			quit("cannot read %s even after creating it: %s\n",
100 			    dumpdates, strerror(errno));
101 			/* NOTREACHED */
102 		}
103 	}
104 	(void) flock(fileno(df), LOCK_SH);
105 	readdumptimes(df);
106 	(void) fclose(df);
107 }
108 
109 static void
110 readdumptimes(FILE *df)
111 {
112 	register int i;
113 	register struct	dumptime *dtwalk;
114 
115 	for (;;) {
116 		dtwalk = (struct dumptime *)calloc(1, sizeof (struct dumptime));
117 		if (getrecord(df, &(dtwalk->dt_value)) < 0)
118 			break;
119 		nddates++;
120 		SLIST_INSERT_HEAD(&dthead, dtwalk, dt_list);
121 	}
122 
123 	ddates_in = 1;
124 	/*
125 	 *	arrayify the list, leaving enough room for the additional
126 	 *	record that we may have to add to the ddate structure
127 	 */
128 	ddatev = (struct dumpdates **)
129 		calloc((unsigned) (nddates + 1), sizeof (struct dumpdates *));
130 	dtwalk = SLIST_FIRST(&dthead);
131 	for (i = nddates - 1; i >= 0; i--, dtwalk = SLIST_NEXT(dtwalk, dt_list))
132 		ddatev[i] = &dtwalk->dt_value;
133 }
134 
135 void
136 getdumptime(void)
137 {
138 	register struct dumpdates *ddp;
139 	register int i;
140 	char *fname;
141 
142 	fname = disk;
143 #ifdef FDEBUG
144 	msg("Looking for name %s in dumpdates = %s for level = %c\n",
145 		fname, dumpdates, level);
146 #endif
147 	spcl.c_ddate = 0;
148 	lastlevel = '0';
149 
150 	initdumptimes();
151 	/*
152 	 *	Go find the entry with the same name for a lower increment
153 	 *	and older date
154 	 */
155 	ITITERATE(i, ddp) {
156 		if (strncmp(fname, ddp->dd_name, sizeof (ddp->dd_name)) != 0)
157 			continue;
158 		if (ddp->dd_level >= level)
159 			continue;
160 		if (ddp->dd_ddate <= spcl.c_ddate)
161 			continue;
162 		spcl.c_ddate = ddp->dd_ddate;
163 		lastlevel = ddp->dd_level;
164 	}
165 }
166 
167 void
168 putdumptime(void)
169 {
170 	FILE *df;
171 	register struct dumpdates *dtwalk;
172 	register int i;
173 	int fd;
174 	char *fname;
175 
176 	if(uflag == 0)
177 		return;
178 	if ((df = fopen(dumpdates, "r+")) == NULL)
179 		quit("cannot rewrite %s: %s\n", dumpdates, strerror(errno));
180 	fd = fileno(df);
181 	(void) flock(fd, LOCK_EX);
182 	fname = disk;
183 	free((char *)ddatev);
184 	ddatev = 0;
185 	nddates = 0;
186 	ddates_in = 0;
187 	readdumptimes(df);
188 	if (fseek(df, 0L, 0) < 0)
189 		quit("fseek: %s\n", strerror(errno));
190 	spcl.c_ddate = 0;
191 	ITITERATE(i, dtwalk) {
192 		if (strncmp(fname, dtwalk->dd_name,
193 				sizeof (dtwalk->dd_name)) != 0)
194 			continue;
195 		if (dtwalk->dd_level != level)
196 			continue;
197 		goto found;
198 	}
199 	/*
200 	 *	construct the new upper bound;
201 	 *	Enough room has been allocated.
202 	 */
203 	dtwalk = ddatev[nddates] =
204 		(struct dumpdates *)calloc(1, sizeof (struct dumpdates));
205 	nddates += 1;
206   found:
207 	(void) strncpy(dtwalk->dd_name, fname, sizeof (dtwalk->dd_name));
208 	dtwalk->dd_level = level;
209 	dtwalk->dd_ddate = spcl.c_date;
210 
211 	ITITERATE(i, dtwalk) {
212 		dumprecout(df, dtwalk);
213 	}
214 	if (fflush(df))
215 		quit("%s: %s\n", dumpdates, strerror(errno));
216 	if (ftruncate(fd, ftell(df)))
217 		quit("ftruncate (%s): %s\n", dumpdates, strerror(errno));
218 	(void) fclose(df);
219 	msg("level %c dump on %s", level,
220 		spcl.c_date == 0 ? "the epoch\n" : ctime(&spcl.c_date));
221 }
222 
223 static void
224 dumprecout(FILE *file, struct dumpdates *what)
225 {
226 
227 	if (fprintf(file, DUMPOUTFMT,
228 		    what->dd_name,
229 		    what->dd_level,
230 		    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 %c %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, 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