xref: /dragonfly/sbin/dump/itime.c (revision 65cc0652)
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 #include <unistd.h>
48 
49 #include "dump.h"
50 
51 struct dumptime {
52 	struct	dumpdates dt_value;
53 	SLIST_ENTRY(dumptime) dt_list;
54 };
55 SLIST_HEAD(dthead, dumptime) dthead = SLIST_HEAD_INITIALIZER(dthead);
56 struct	dumpdates **ddatev = NULL;
57 int	nddates = 0;
58 
59 static	void dumprecout(FILE *, struct dumpdates *);
60 static	int getrecord(FILE *, struct dumpdates *);
61 static	int makedumpdate(struct dumpdates *, char *);
62 static	void readdumptimes(FILE *);
63 
64 void
65 initdumptimes(void)
66 {
67 	FILE *df;
68 
69 	if ((df = fopen(dumpdates, "r")) == NULL) {
70 		if (errno != ENOENT) {
71 			msg("WARNING: cannot read %s: %s\n", dumpdates,
72 			    strerror(errno));
73 			return;
74 		}
75 		/*
76 		 * Dumpdates does not exist, make an empty one.
77 		 */
78 		msg("WARNING: no file `%s', making an empty one\n", dumpdates);
79 		if ((df = fopen(dumpdates, "w")) == NULL) {
80 			msg("WARNING: cannot create %s: %s\n", dumpdates,
81 			    strerror(errno));
82 			return;
83 		}
84 		fclose(df);
85 		if ((df = fopen(dumpdates, "r")) == NULL) {
86 			quit("cannot read %s even after creating it: %s\n",
87 			    dumpdates, strerror(errno));
88 			/* NOTREACHED */
89 		}
90 	}
91 	flock(fileno(df), LOCK_SH);
92 	readdumptimes(df);
93 	fclose(df);
94 }
95 
96 static void
97 readdumptimes(FILE *df)
98 {
99 	int i;
100 	struct dumptime *dtwalk;
101 
102 	for (;;) {
103 		dtwalk = (struct dumptime *)calloc(1, sizeof (struct dumptime));
104 		if (getrecord(df, &(dtwalk->dt_value)) < 0)
105 			break;
106 		nddates++;
107 		SLIST_INSERT_HEAD(&dthead, dtwalk, dt_list);
108 	}
109 
110 	/*
111 	 *	arrayify the list, leaving enough room for the additional
112 	 *	record that we may have to add to the ddate structure
113 	 */
114 	ddatev = (struct dumpdates **)
115 		calloc((unsigned) (nddates + 1), sizeof (struct dumpdates *));
116 	dtwalk = SLIST_FIRST(&dthead);
117 	for (i = nddates - 1; i >= 0; i--, dtwalk = SLIST_NEXT(dtwalk, dt_list))
118 		ddatev[i] = &dtwalk->dt_value;
119 }
120 
121 void
122 getdumptime(void)
123 {
124 	struct dumpdates *ddp;
125 	int i;
126 	char *fname;
127 
128 	fname = disk;
129 #ifdef FDEBUG
130 	msg("Looking for name %s in dumpdates = %s for level = %c\n",
131 		fname, dumpdates, level);
132 #endif
133 	spcl.c_ddate = 0;
134 	lastlevel = '0';
135 
136 	initdumptimes();
137 	/*
138 	 *	Go find the entry with the same name for a lower increment
139 	 *	and older date
140 	 */
141 	ITITERATE(i, ddp) {
142 		if (strncmp(fname, ddp->dd_name, sizeof (ddp->dd_name)) != 0)
143 			continue;
144 		if (ddp->dd_level >= level)
145 			continue;
146 		if (ddp->dd_ddate <= spcl.c_ddate)
147 			continue;
148 		spcl.c_ddate = ddp->dd_ddate;
149 		lastlevel = ddp->dd_level;
150 	}
151 }
152 
153 void
154 putdumptime(void)
155 {
156 	FILE *df;
157 	struct dumpdates *dtwalk;
158 	int i;
159 	int fd;
160 	char *fname;
161 
162 	if(uflag == 0)
163 		return;
164 	if ((df = fopen(dumpdates, "r+")) == NULL)
165 		quit("cannot rewrite %s: %s\n", dumpdates, strerror(errno));
166 	fd = fileno(df);
167 	flock(fd, LOCK_EX);
168 	fname = disk;
169 	free((char *)ddatev);
170 	ddatev = NULL;
171 	nddates = 0;
172 	readdumptimes(df);
173 	if (fseek(df, 0L, 0) < 0)
174 		quit("fseek: %s\n", strerror(errno));
175 	spcl.c_ddate = 0;
176 	ITITERATE(i, dtwalk) {
177 		if (strncmp(fname, dtwalk->dd_name,
178 				sizeof (dtwalk->dd_name)) != 0)
179 			continue;
180 		if (dtwalk->dd_level != level)
181 			continue;
182 		goto found;
183 	}
184 	/*
185 	 *	construct the new upper bound;
186 	 *	Enough room has been allocated.
187 	 */
188 	dtwalk = ddatev[nddates] =
189 		(struct dumpdates *)calloc(1, sizeof (struct dumpdates));
190 	nddates += 1;
191   found:
192 	strncpy(dtwalk->dd_name, fname, sizeof (dtwalk->dd_name));
193 	dtwalk->dd_level = level;
194 	dtwalk->dd_ddate = spcl.c_date;
195 
196 	ITITERATE(i, dtwalk) {
197 		dumprecout(df, dtwalk);
198 	}
199 	if (fflush(df))
200 		quit("%s: %s\n", dumpdates, strerror(errno));
201 	if (ftruncate(fd, ftell(df)))
202 		quit("ftruncate (%s): %s\n", dumpdates, strerror(errno));
203 	fclose(df);
204 	msg("level %c dump on %s", level,
205 		spcl.c_date == 0 ? "the epoch\n" : ctime((const time_t *)&spcl.c_date));
206 }
207 
208 static void
209 dumprecout(FILE *file, struct dumpdates *what)
210 {
211 
212 	if (fprintf(file, DUMPOUTFMT,
213 		    what->dd_name,
214 		    what->dd_level,
215 		    ctime(&what->dd_ddate)) < 0)
216 		quit("%s: %s\n", dumpdates, strerror(errno));
217 }
218 
219 int	recno;
220 
221 static int
222 getrecord(FILE *df, struct dumpdates *ddatep)
223 {
224 	char tbuf[BUFSIZ];
225 
226 	recno = 0;
227 	if ( (fgets(tbuf, sizeof (tbuf), df)) != tbuf)
228 		return(-1);
229 	recno++;
230 	if (makedumpdate(ddatep, tbuf) < 0)
231 		msg("Unknown intermediate format in %s, line %d\n",
232 			dumpdates, recno);
233 
234 #ifdef FDEBUG
235 	msg("getrecord: %s %c %s", ddatep->dd_name, ddatep->dd_level,
236 	    ddatep->dd_ddate == 0 ? "the epoch\n" : ctime(&ddatep->dd_ddate));
237 #endif
238 	return(0);
239 }
240 
241 static int
242 makedumpdate(struct dumpdates *ddp, char *tbuf)
243 {
244 	char un_buf[128];
245 
246 	sscanf(tbuf, DUMPINFMT, ddp->dd_name, &ddp->dd_level, un_buf);
247 	ddp->dd_ddate = unctime(un_buf);
248 	if (ddp->dd_ddate < 0)
249 		return(-1);
250 	return(0);
251 }
252