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. 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.
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
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 $
33 #include <sys/param.h>
34 #include <sys/queue.h>
37 #include <vfs/ufs/dinode.h>
38 #include <vfs/ufs/fs.h>
40 #include <protocols/dumprestore.h>
51 struct dumpdates dt_value;
52 SLIST_ENTRY(dumptime) dt_list;
54 SLIST_HEAD(dthead, dumptime) dthead = SLIST_HEAD_INITIALIZER(dthead);
55 struct dumpdates **ddatev = NULL;
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 *);
68 if ((df = fopen(dumpdates, "r")) == NULL) {
69 if (errno != ENOENT) {
70 msg("WARNING: cannot read %s: %s\n", dumpdates,
75 * Dumpdates does not exist, make an empty one.
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,
84 if ((df = fopen(dumpdates, "r")) == NULL) {
85 quit("cannot read %s even after creating it: %s\n",
86 dumpdates, strerror(errno));
90 flock(fileno(df), LOCK_SH);
96 readdumptimes(FILE *df)
99 struct dumptime *dtwalk;
102 dtwalk = (struct dumptime *)calloc(1, sizeof (struct dumptime));
103 if (getrecord(df, &(dtwalk->dt_value)) < 0)
106 SLIST_INSERT_HEAD(&dthead, dtwalk, dt_list);
110 * arrayify the list, leaving enough room for the additional
111 * record that we may have to add to the ddate structure
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;
123 struct dumpdates *ddp;
129 msg("Looking for name %s in dumpdates = %s for level = %c\n",
130 fname, dumpdates, level);
137 * Go find the entry with the same name for a lower increment
141 if (strncmp(fname, ddp->dd_name, sizeof (ddp->dd_name)) != 0)
143 if (ddp->dd_level >= level)
145 if (ddp->dd_ddate <= spcl.c_ddate)
147 spcl.c_ddate = ddp->dd_ddate;
148 lastlevel = ddp->dd_level;
156 struct dumpdates *dtwalk;
163 if ((df = fopen(dumpdates, "r+")) == NULL)
164 quit("cannot rewrite %s: %s\n", dumpdates, strerror(errno));
168 free((char *)ddatev);
172 if (fseek(df, 0L, 0) < 0)
173 quit("fseek: %s\n", strerror(errno));
175 ITITERATE(i, dtwalk) {
176 if (strncmp(fname, dtwalk->dd_name,
177 sizeof (dtwalk->dd_name)) != 0)
179 if (dtwalk->dd_level != level)
184 * construct the new upper bound;
185 * Enough room has been allocated.
187 dtwalk = ddatev[nddates] =
188 (struct dumpdates *)calloc(1, sizeof (struct dumpdates));
191 strncpy(dtwalk->dd_name, fname, sizeof (dtwalk->dd_name));
192 dtwalk->dd_level = level;
193 dtwalk->dd_ddate = spcl.c_date;
195 ITITERATE(i, dtwalk) {
196 dumprecout(df, dtwalk);
199 quit("%s: %s\n", dumpdates, strerror(errno));
200 if (ftruncate(fd, ftell(df)))
201 quit("ftruncate (%s): %s\n", dumpdates, strerror(errno));
203 msg("level %c dump on %s", level,
204 spcl.c_date == 0 ? "the epoch\n" : ctime((const time_t *)&spcl.c_date));
208 dumprecout(FILE *file, struct dumpdates *what)
211 if (fprintf(file, DUMPOUTFMT,
214 ctime(&what->dd_ddate)) < 0)
215 quit("%s: %s\n", dumpdates, strerror(errno));
221 getrecord(FILE *df, struct dumpdates *ddatep)
226 if ( (fgets(tbuf, sizeof (tbuf), df)) != tbuf)
229 if (makedumpdate(ddatep, tbuf) < 0)
230 msg("Unknown intermediate format in %s, line %d\n",
234 msg("getrecord: %s %c %s", ddatep->dd_name, ddatep->dd_level,
235 ddatep->dd_ddate == 0 ? "the epoch\n" : ctime(&ddatep->dd_ddate));
241 makedumpdate(struct dumpdates *ddp, char *tbuf)
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)