2 * Copyright (c) 1980, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
36 static char sccsid[] = "@(#)itime.c 8.1 (Berkeley) 6/5/93";
38 static const char rcsid[] =
39 "$FreeBSD: src/sbin/dump/itime.c,v 1.3.2.1 2001/08/01 06:29:35 obrien Exp $";
42 #include <sys/param.h>
43 #include <sys/queue.h>
46 #include <sys/vnode.h>
48 #include <ufs/fsdir.h>
49 #include <ufs/inode.h>
52 #include <ufs/ufs/dinode.h>
55 #include <protocols/dumprestore.h>
68 struct dumpdates dt_value;
69 SLIST_ENTRY(dumptime) dt_list;
71 SLIST_HEAD(dthead, dumptime) dthead = SLIST_HEAD_INITIALIZER(dthead);
72 struct dumpdates **ddatev = 0;
76 static void dumprecout __P((FILE *, struct dumpdates *));
77 static int getrecord __P((FILE *, struct dumpdates *));
78 static int makedumpdate __P((struct dumpdates *, char *));
79 static void readdumptimes __P((FILE *));
86 if ((df = fopen(dumpdates, "r")) == NULL) {
87 if (errno != ENOENT) {
88 msg("WARNING: cannot read %s: %s\n", dumpdates,
93 * Dumpdates does not exist, make an empty one.
95 msg("WARNING: no file `%s', making an empty one\n", dumpdates);
96 if ((df = fopen(dumpdates, "w")) == NULL) {
97 msg("WARNING: cannot create %s: %s\n", dumpdates,
102 if ((df = fopen(dumpdates, "r")) == NULL) {
103 quit("cannot read %s even after creating it: %s\n",
104 dumpdates, strerror(errno));
108 (void) flock(fileno(df), LOCK_SH);
118 register struct dumptime *dtwalk;
121 dtwalk = (struct dumptime *)calloc(1, sizeof (struct dumptime));
122 if (getrecord(df, &(dtwalk->dt_value)) < 0)
125 SLIST_INSERT_HEAD(&dthead, dtwalk, dt_list);
130 * arrayify the list, leaving enough room for the additional
131 * record that we may have to add to the ddate structure
133 ddatev = (struct dumpdates **)
134 calloc((unsigned) (nddates + 1), sizeof (struct dumpdates *));
135 dtwalk = SLIST_FIRST(&dthead);
136 for (i = nddates - 1; i >= 0; i--, dtwalk = SLIST_NEXT(dtwalk, dt_list))
137 ddatev[i] = &dtwalk->dt_value;
143 register struct dumpdates *ddp;
149 msg("Looking for name %s in dumpdates = %s for level = %c\n",
150 fname, dumpdates, level);
157 * Go find the entry with the same name for a lower increment
161 if (strncmp(fname, ddp->dd_name, sizeof (ddp->dd_name)) != 0)
163 if (ddp->dd_level >= level)
165 if (ddp->dd_ddate <= spcl.c_ddate)
167 spcl.c_ddate = ddp->dd_ddate;
168 lastlevel = ddp->dd_level;
176 register struct dumpdates *dtwalk;
183 if ((df = fopen(dumpdates, "r+")) == NULL)
184 quit("cannot rewrite %s: %s\n", dumpdates, strerror(errno));
186 (void) flock(fd, LOCK_EX);
188 free((char *)ddatev);
193 if (fseek(df, 0L, 0) < 0)
194 quit("fseek: %s\n", strerror(errno));
196 ITITERATE(i, dtwalk) {
197 if (strncmp(fname, dtwalk->dd_name,
198 sizeof (dtwalk->dd_name)) != 0)
200 if (dtwalk->dd_level != level)
205 * construct the new upper bound;
206 * Enough room has been allocated.
208 dtwalk = ddatev[nddates] =
209 (struct dumpdates *)calloc(1, sizeof (struct dumpdates));
212 (void) strncpy(dtwalk->dd_name, fname, sizeof (dtwalk->dd_name));
213 dtwalk->dd_level = level;
214 dtwalk->dd_ddate = spcl.c_date;
216 ITITERATE(i, dtwalk) {
217 dumprecout(df, dtwalk);
220 quit("%s: %s\n", dumpdates, strerror(errno));
221 if (ftruncate(fd, ftell(df)))
222 quit("ftruncate (%s): %s\n", dumpdates, strerror(errno));
224 msg("level %c dump on %s", level,
225 spcl.c_date == 0 ? "the epoch\n" : ctime(&spcl.c_date));
229 dumprecout(file, what)
231 struct dumpdates *what;
234 if (fprintf(file, DUMPOUTFMT,
237 ctime(&what->dd_ddate)) < 0)
238 quit("%s: %s\n", dumpdates, strerror(errno));
244 getrecord(df, ddatep)
246 struct dumpdates *ddatep;
251 if ( (fgets(tbuf, sizeof (tbuf), df)) != tbuf)
254 if (makedumpdate(ddatep, tbuf) < 0)
255 msg("Unknown intermediate format in %s, line %d\n",
259 msg("getrecord: %s %c %s", ddatep->dd_name, ddatep->dd_level,
260 ddatep->dd_ddate == 0 ? "the epoch\n" : ctime(&ddatep->dd_ddate));
266 makedumpdate(ddp, tbuf)
267 struct dumpdates *ddp;
272 (void) sscanf(tbuf, DUMPINFMT, ddp->dd_name, &ddp->dd_level, un_buf);
273 ddp->dd_ddate = unctime(un_buf);
274 if (ddp->dd_ddate < 0)