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>
52 struct dumpdates dt_value
;
53 SLIST_ENTRY(dumptime
) dt_list
;
55 SLIST_HEAD(dthead
, dumptime
) dthead
= SLIST_HEAD_INITIALIZER(dthead
);
56 int nddates
= 0; /* number of records (might be zero) */
57 struct dumpdates
**ddatev
; /* the arrayfied version */
58 const char *dumpdates
; /* name of the file containing dump date info */
59 int lastlevel
; /* dump level of previous dump */
61 static void dumprecout(FILE *, struct dumpdates
*);
62 static int getrecord(FILE *, struct dumpdates
*);
63 static int makedumpdate(struct dumpdates
*, char *);
64 static void readdumptimes(FILE *);
71 if ((df
= fopen(dumpdates
, "r")) == NULL
) {
72 if (errno
!= ENOENT
) {
73 msg("WARNING: cannot read %s: %s\n", dumpdates
,
78 * Dumpdates does not exist, make an empty one.
80 msg("WARNING: no file `%s', making an empty one\n", dumpdates
);
81 if ((df
= fopen(dumpdates
, "w")) == NULL
) {
82 msg("WARNING: cannot create %s: %s\n", dumpdates
,
87 if ((df
= fopen(dumpdates
, "r")) == NULL
) {
88 quit("cannot read %s even after creating it: %s\n",
89 dumpdates
, strerror(errno
));
93 flock(fileno(df
), LOCK_SH
);
99 readdumptimes(FILE *df
)
102 struct dumptime
*dtwalk
;
105 dtwalk
= (struct dumptime
*)calloc(1, sizeof (struct dumptime
));
106 if (getrecord(df
, &(dtwalk
->dt_value
)) < 0)
109 SLIST_INSERT_HEAD(&dthead
, dtwalk
, dt_list
);
113 * arrayify the list, leaving enough room for the additional
114 * record that we may have to add to the ddate structure
116 ddatev
= (struct dumpdates
**)
117 calloc((unsigned) (nddates
+ 1), sizeof (struct dumpdates
*));
118 dtwalk
= SLIST_FIRST(&dthead
);
119 for (i
= nddates
- 1; i
>= 0; i
--, dtwalk
= SLIST_NEXT(dtwalk
, dt_list
))
120 ddatev
[i
] = &dtwalk
->dt_value
;
126 struct dumpdates
*ddp
;
132 msg("Looking for name %s in dumpdates = %s for level = %c\n",
133 fname
, dumpdates
, level
);
140 * Go find the entry with the same name for a lower increment
144 if (strncmp(fname
, ddp
->dd_name
, sizeof (ddp
->dd_name
)) != 0)
146 if (ddp
->dd_level
>= level
)
148 if (ddp
->dd_ddate
<= spcl
.c_ddate
)
150 spcl
.c_ddate
= ddp
->dd_ddate
;
151 lastlevel
= ddp
->dd_level
;
159 struct dumpdates
*dtwalk
;
166 if ((df
= fopen(dumpdates
, "r+")) == NULL
)
167 quit("cannot rewrite %s: %s\n", dumpdates
, strerror(errno
));
171 free((char *)ddatev
);
175 if (fseek(df
, 0L, 0) < 0)
176 quit("fseek: %s\n", strerror(errno
));
178 ITITERATE(i
, dtwalk
) {
179 if (strncmp(fname
, dtwalk
->dd_name
,
180 sizeof (dtwalk
->dd_name
)) != 0)
182 if (dtwalk
->dd_level
!= level
)
187 * construct the new upper bound;
188 * Enough room has been allocated.
190 dtwalk
= ddatev
[nddates
] =
191 (struct dumpdates
*)calloc(1, sizeof (struct dumpdates
));
194 strncpy(dtwalk
->dd_name
, fname
, sizeof (dtwalk
->dd_name
));
195 dtwalk
->dd_level
= level
;
196 dtwalk
->dd_ddate
= spcl
.c_date
;
198 ITITERATE(i
, dtwalk
) {
199 dumprecout(df
, dtwalk
);
202 quit("%s: %s\n", dumpdates
, strerror(errno
));
203 if (ftruncate(fd
, ftell(df
)))
204 quit("ftruncate (%s): %s\n", dumpdates
, strerror(errno
));
206 msg("level %c dump on %s", level
,
207 spcl
.c_date
== 0 ? "the epoch\n" : ctime((const time_t *)&spcl
.c_date
));
211 dumprecout(FILE *file
, struct dumpdates
*what
)
214 if (fprintf(file
, DUMPOUTFMT
,
217 ctime(&what
->dd_ddate
)) < 0)
218 quit("%s: %s\n", dumpdates
, strerror(errno
));
224 getrecord(FILE *df
, struct dumpdates
*ddatep
)
229 if ( (fgets(tbuf
, sizeof (tbuf
), df
)) != tbuf
)
232 if (makedumpdate(ddatep
, tbuf
) < 0)
233 msg("Unknown intermediate format in %s, line %d\n",
237 msg("getrecord: %s %c %s", ddatep
->dd_name
, ddatep
->dd_level
,
238 ddatep
->dd_ddate
== 0 ? "the epoch\n" : ctime(&ddatep
->dd_ddate
));
244 makedumpdate(struct dumpdates
*ddp
, char *tbuf
)
248 sscanf(tbuf
, DUMPINFMT
, ddp
->dd_name
, &ddp
->dd_level
, un_buf
);
249 ddp
->dd_ddate
= unctime(un_buf
);
250 if (ddp
->dd_ddate
< 0)