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