]> git.wh0rd.org - dump.git/blob - dump/itime.c
Version 0.4b5.
[dump.git] / dump / itime.c
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-1997
5 * Stelian Pop <pop@cybercable.fr>, 1999
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
43 #if 0
44 static char sccsid[] = "@(#)itime.c 8.1 (Berkeley) 6/5/93";
45 #endif
46 static const char rcsid[] =
47 "$Id: itime.c,v 1.2 1999/10/11 12:53:22 stelian Exp $";
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>
56 #include <unistd.h>
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>
77 #endif
78
79 #ifdef __linux__
80 #include <ext2fs/ext2fs.h>
81 #endif
82
83 #include "dump.h"
84
85 struct dumpdates **ddatev = 0;
86 int nddates = 0;
87 int ddates_in = 0;
88 struct dumptime *dthead = 0;
89
90 static void dumprecout __P((FILE *, struct dumpdates *));
91 static int getrecord __P((FILE *, struct dumpdates *));
92 static int makedumpdate __P((struct dumpdates *, char *));
93 static void readdumptimes __P((FILE *));
94
95 void
96 initdumptimes()
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
127 static void
128 readdumptimes(df)
129 FILE *df;
130 {
131 register int i;
132 register struct dumptime *dtwalk;
133
134 for (;;) {
135 dtwalk = (struct dumptime *)calloc(1, sizeof (struct dumptime));
136 if (getrecord(df, &(dtwalk->dt_value)) < 0)
137 break;
138 nddates++;
139 dtwalk->dt_next = dthead;
140 dthead = dtwalk;
141 }
142
143 ddates_in = 1;
144 /*
145 * arrayify the list, leaving enough room for the additional
146 * record that we may have to add to the ddate structure
147 */
148 ddatev = (struct dumpdates **)
149 calloc((unsigned) (nddates + 1), sizeof (struct dumpdates *));
150 dtwalk = dthead;
151 for (i = nddates - 1; i >= 0; i--, dtwalk = dtwalk->dt_next)
152 ddatev[i] = &dtwalk->dt_value;
153 }
154
155 void
156 getdumptime()
157 {
158 register struct dumpdates *ddp;
159 register int i;
160 char *fname;
161
162 fname = disk;
163 #ifdef FDEBUG
164 msg("Looking for name %s in dumpdates = %s for level = %c\n",
165 fname, dumpdates, level);
166 #endif
167 spcl.c_ddate = 0;
168 lastlevel = '0';
169
170 initdumptimes();
171 /*
172 * Go find the entry with the same name for a lower increment
173 * and older date
174 */
175 ITITERATE(i, ddp) {
176 if (strncmp(fname, ddp->dd_name, sizeof (ddp->dd_name)) != 0)
177 continue;
178 if (ddp->dd_level >= level)
179 continue;
180 #ifdef __linux__
181 if (ddp->dd_ddate <= (time_t)spcl.c_ddate)
182 #else
183 if (ddp->dd_ddate <= spcl.c_ddate)
184 #endif
185 continue;
186 spcl.c_ddate = ddp->dd_ddate;
187 lastlevel = ddp->dd_level;
188 }
189 }
190
191 void
192 putdumptime()
193 {
194 FILE *df;
195 register struct dumpdates *dtwalk;
196 register int i;
197 int fd;
198 char *fname;
199
200 if(uflag == 0)
201 return;
202 if ((df = fopen(dumpdates, "r+")) == NULL)
203 quit("cannot rewrite %s: %s\n", dumpdates, strerror(errno));
204 fd = fileno(df);
205 (void) flock(fd, LOCK_EX);
206 fname = disk;
207 free((char *)ddatev);
208 ddatev = 0;
209 nddates = 0;
210 dthead = 0;
211 ddates_in = 0;
212 readdumptimes(df);
213 if (fseek(df, 0L, 0) < 0)
214 quit("fseek: %s\n", strerror(errno));
215 spcl.c_ddate = 0;
216 ITITERATE(i, dtwalk) {
217 if (strncmp(fname, dtwalk->dd_name,
218 sizeof (dtwalk->dd_name)) != 0)
219 continue;
220 if (dtwalk->dd_level != level)
221 continue;
222 goto found;
223 }
224 /*
225 * construct the new upper bound;
226 * Enough room has been allocated.
227 */
228 dtwalk = ddatev[nddates] =
229 (struct dumpdates *)calloc(1, sizeof (struct dumpdates));
230 nddates += 1;
231 found:
232 (void) strncpy(dtwalk->dd_name, fname, sizeof (dtwalk->dd_name));
233 dtwalk->dd_level = level;
234 dtwalk->dd_ddate = spcl.c_date;
235
236 ITITERATE(i, dtwalk) {
237 dumprecout(df, dtwalk);
238 }
239 if (fflush(df))
240 quit("%s: %s\n", dumpdates, strerror(errno));
241 if (ftruncate(fd, ftell(df)))
242 quit("ftruncate (%s): %s\n", dumpdates, strerror(errno));
243 (void) fclose(df);
244 msg("level %c dump on %s", level,
245 #ifdef __linux__
246 spcl.c_date == 0 ? "the epoch\n" : ctime4(&spcl.c_date));
247 #else
248 spcl.c_date == 0 ? "the epoch\n" : ctime(&spcl.c_date));
249 #endif
250 }
251
252 static void
253 dumprecout(file, what)
254 FILE *file;
255 struct dumpdates *what;
256 {
257
258 if (fprintf(file, DUMPOUTFMT,
259 what->dd_name,
260 what->dd_level,
261 ctime(&what->dd_ddate)) < 0)
262 quit("%s: %s\n", dumpdates, strerror(errno));
263 }
264
265 int recno;
266
267 static int
268 getrecord(df, ddatep)
269 FILE *df;
270 struct dumpdates *ddatep;
271 {
272 char tbuf[BUFSIZ];
273
274 recno = 0;
275 if ( (fgets(tbuf, sizeof (tbuf), df)) != tbuf)
276 return(-1);
277 recno++;
278 if (makedumpdate(ddatep, tbuf) < 0)
279 msg("Unknown intermediate format in %s, line %d\n",
280 dumpdates, recno);
281
282 #ifdef FDEBUG
283 msg("getrecord: %s %c %s", ddatep->dd_name, ddatep->dd_level,
284 ddatep->dd_ddate == 0 ? "the epoch\n" : ctime(&ddatep->dd_ddate));
285 #endif
286 return(0);
287 }
288
289 static int
290 makedumpdate(ddp, tbuf)
291 struct dumpdates *ddp;
292 char *tbuf;
293 {
294 char un_buf[128];
295
296 (void) sscanf(tbuf, DUMPINFMT, ddp->dd_name, &ddp->dd_level, un_buf);
297 ddp->dd_ddate = unctime(un_buf);
298 if (ddp->dd_ddate < 0)
299 return(-1);
300 return(0);
301 }