]> git.wh0rd.org - dump.git/blame - dump/itime.c
Version 0.4b6.
[dump.git] / dump / itime.c
CommitLineData
1227625a
SP
1/*
2 * Ported to Linux's Second Extended File System as part of the
3 * dump and restore backup suit
b45f51d6
SP
4 * Remy Card <card@Linux.EU.Org>, 1994-1997
5 * Stelian Pop <pop@cybercable.fr>, 1999
1227625a
SP
6 *
7 */
8
9/*-
10 * Copyright (c) 1980, 1993
11 * The Regents of the University of California. All rights reserved.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. All advertising materials mentioning features or use of this software
22 * must display the following acknowledgement:
23 * This product includes software developed by the University of
24 * California, Berkeley and its contributors.
25 * 4. Neither the name of the University nor the names of its contributors
26 * may be used to endorse or promote products derived from this software
27 * without specific prior written permission.
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 * SUCH DAMAGE.
40 */
41
42#ifndef lint
b45f51d6 43#if 0
1227625a 44static char sccsid[] = "@(#)itime.c 8.1 (Berkeley) 6/5/93";
b45f51d6
SP
45#endif
46static const char rcsid[] =
ddd2ef55 47 "$Id: itime.c,v 1.3 1999/10/11 12:59:18 stelian Exp $";
1227625a
SP
48#endif /* not lint */
49
50#include <sys/param.h>
51#include <sys/time.h>
52#ifdef __linux__
53#include <linux/ext2_fs.h>
54#include <bsdcompat.h>
55#include <sys/file.h>
b45f51d6 56#include <unistd.h>
1227625a
SP
57#else
58#ifdef sunos
59#include <sys/vnode.h>
60
61#include <ufs/fsdir.h>
62#include <ufs/inode.h>
63#include <ufs/fs.h>
64#else
65#include <ufs/ufs/dinode.h>
66#endif
67#endif
68
69#include <protocols/dumprestore.h>
70
71#include <errno.h>
72#include <fcntl.h>
73#include <stdio.h>
74#ifdef __STDC__
75#include <stdlib.h>
76#include <string.h>
1227625a
SP
77#endif
78
79#ifdef __linux__
80#include <ext2fs/ext2fs.h>
81#endif
82
83#include "dump.h"
84
85struct dumpdates **ddatev = 0;
86int nddates = 0;
87int ddates_in = 0;
88struct dumptime *dthead = 0;
89
90static void dumprecout __P((FILE *, struct dumpdates *));
91static int getrecord __P((FILE *, struct dumpdates *));
92static int makedumpdate __P((struct dumpdates *, char *));
93static void readdumptimes __P((FILE *));
94
95void
ddd2ef55 96initdumptimes(void)
1227625a
SP
97{
98 FILE *df;
99
100 if ((df = fopen(dumpdates, "r")) == NULL) {
101 if (errno != ENOENT) {
102 quit("cannot read %s: %s\n", dumpdates,
103 strerror(errno));
104 /* NOTREACHED */
105 }
106 /*
107 * Dumpdates does not exist, make an empty one.
108 */
109 msg("WARNING: no file `%s', making an empty one\n", dumpdates);
110 if ((df = fopen(dumpdates, "w")) == NULL) {
111 quit("cannot create %s: %s\n", dumpdates,
112 strerror(errno));
113 /* NOTREACHED */
114 }
115 (void) fclose(df);
116 if ((df = fopen(dumpdates, "r")) == NULL) {
117 quit("cannot read %s even after creating it: %s\n",
118 dumpdates, strerror(errno));
119 /* NOTREACHED */
120 }
121 }
122 (void) flock(fileno(df), LOCK_SH);
123 readdumptimes(df);
124 (void) fclose(df);
125}
126
127static void
ddd2ef55 128readdumptimes(FILE *df)
1227625a
SP
129{
130 register int i;
131 register struct dumptime *dtwalk;
132
133 for (;;) {
134 dtwalk = (struct dumptime *)calloc(1, sizeof (struct dumptime));
135 if (getrecord(df, &(dtwalk->dt_value)) < 0)
136 break;
137 nddates++;
138 dtwalk->dt_next = dthead;
139 dthead = dtwalk;
140 }
141
142 ddates_in = 1;
143 /*
144 * arrayify the list, leaving enough room for the additional
145 * record that we may have to add to the ddate structure
146 */
147 ddatev = (struct dumpdates **)
148 calloc((unsigned) (nddates + 1), sizeof (struct dumpdates *));
149 dtwalk = dthead;
150 for (i = nddates - 1; i >= 0; i--, dtwalk = dtwalk->dt_next)
151 ddatev[i] = &dtwalk->dt_value;
152}
153
154void
ddd2ef55 155getdumptime(void)
1227625a
SP
156{
157 register struct dumpdates *ddp;
158 register int i;
159 char *fname;
160
161 fname = disk;
162#ifdef FDEBUG
163 msg("Looking for name %s in dumpdates = %s for level = %c\n",
164 fname, dumpdates, level);
165#endif
166 spcl.c_ddate = 0;
167 lastlevel = '0';
168
ddd2ef55
SP
169 /* if we're not going to update dumpdates, there's no point in reading
170 it, particularly since /var might not be mounted... wait until here
171 to benefit from the initialization of variables needed by parent */
172 if (uflag == 0)
173 return;
174
1227625a
SP
175 initdumptimes();
176 /*
177 * Go find the entry with the same name for a lower increment
178 * and older date
179 */
180 ITITERATE(i, ddp) {
181 if (strncmp(fname, ddp->dd_name, sizeof (ddp->dd_name)) != 0)
182 continue;
183 if (ddp->dd_level >= level)
184 continue;
185#ifdef __linux__
186 if (ddp->dd_ddate <= (time_t)spcl.c_ddate)
187#else
188 if (ddp->dd_ddate <= spcl.c_ddate)
189#endif
190 continue;
191 spcl.c_ddate = ddp->dd_ddate;
192 lastlevel = ddp->dd_level;
193 }
194}
195
196void
ddd2ef55 197putdumptime(void)
1227625a
SP
198{
199 FILE *df;
200 register struct dumpdates *dtwalk;
201 register int i;
202 int fd;
203 char *fname;
204
205 if(uflag == 0)
206 return;
207 if ((df = fopen(dumpdates, "r+")) == NULL)
208 quit("cannot rewrite %s: %s\n", dumpdates, strerror(errno));
209 fd = fileno(df);
210 (void) flock(fd, LOCK_EX);
211 fname = disk;
212 free((char *)ddatev);
213 ddatev = 0;
214 nddates = 0;
215 dthead = 0;
216 ddates_in = 0;
217 readdumptimes(df);
218 if (fseek(df, 0L, 0) < 0)
219 quit("fseek: %s\n", strerror(errno));
220 spcl.c_ddate = 0;
221 ITITERATE(i, dtwalk) {
222 if (strncmp(fname, dtwalk->dd_name,
223 sizeof (dtwalk->dd_name)) != 0)
224 continue;
225 if (dtwalk->dd_level != level)
226 continue;
227 goto found;
228 }
229 /*
230 * construct the new upper bound;
231 * Enough room has been allocated.
232 */
233 dtwalk = ddatev[nddates] =
234 (struct dumpdates *)calloc(1, sizeof (struct dumpdates));
235 nddates += 1;
236 found:
237 (void) strncpy(dtwalk->dd_name, fname, sizeof (dtwalk->dd_name));
238 dtwalk->dd_level = level;
239 dtwalk->dd_ddate = spcl.c_date;
240
241 ITITERATE(i, dtwalk) {
242 dumprecout(df, dtwalk);
243 }
244 if (fflush(df))
245 quit("%s: %s\n", dumpdates, strerror(errno));
246 if (ftruncate(fd, ftell(df)))
247 quit("ftruncate (%s): %s\n", dumpdates, strerror(errno));
248 (void) fclose(df);
249 msg("level %c dump on %s", level,
250#ifdef __linux__
251 spcl.c_date == 0 ? "the epoch\n" : ctime4(&spcl.c_date));
252#else
253 spcl.c_date == 0 ? "the epoch\n" : ctime(&spcl.c_date));
254#endif
255}
256
257static void
ddd2ef55 258dumprecout(FILE *file, struct dumpdates *what)
1227625a
SP
259{
260
261 if (fprintf(file, DUMPOUTFMT,
262 what->dd_name,
263 what->dd_level,
264 ctime(&what->dd_ddate)) < 0)
265 quit("%s: %s\n", dumpdates, strerror(errno));
266}
267
268int recno;
269
270static int
ddd2ef55 271getrecord(FILE *df, struct dumpdates *ddatep)
1227625a
SP
272{
273 char tbuf[BUFSIZ];
274
275 recno = 0;
ddd2ef55 276 if (fgets(tbuf, sizeof (tbuf), df) == NULL)
1227625a
SP
277 return(-1);
278 recno++;
279 if (makedumpdate(ddatep, tbuf) < 0)
280 msg("Unknown intermediate format in %s, line %d\n",
281 dumpdates, recno);
282
283#ifdef FDEBUG
284 msg("getrecord: %s %c %s", ddatep->dd_name, ddatep->dd_level,
285 ddatep->dd_ddate == 0 ? "the epoch\n" : ctime(&ddatep->dd_ddate));
286#endif
287 return(0);
288}
289
290static int
ddd2ef55 291makedumpdate(struct dumpdates *ddp, char *tbuf)
1227625a 292{
ddd2ef55 293 char un_buf[BUFSIZ];
1227625a
SP
294
295 (void) sscanf(tbuf, DUMPINFMT, ddp->dd_name, &ddp->dd_level, un_buf);
296 ddp->dd_ddate = unctime(un_buf);
297 if (ddp->dd_ddate < 0)
298 return(-1);
299 return(0);
300}