]> git.wh0rd.org - dump.git/blob - dump/itime.c
cybercable -> noos.
[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@noos.fr>, 1999-2000
6 * Stelian Pop <pop@noos.fr> - AlcĂ´ve <www.alcove.fr>, 2000
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 static const char rcsid[] =
44 "$Id: itime.c,v 1.13 2000/12/04 15:43:16 stelian Exp $";
45 #endif /* not lint */
46
47 #include <sys/param.h>
48 #include <sys/time.h>
49 #ifdef __linux__
50 #include <linux/ext2_fs.h>
51 #include <bsdcompat.h>
52 #include <sys/file.h>
53 #include <unistd.h>
54 #else
55 #ifdef sunos
56 #include <sys/vnode.h>
57
58 #include <ufs/fsdir.h>
59 #include <ufs/inode.h>
60 #include <ufs/fs.h>
61 #else
62 #include <ufs/ufs/dinode.h>
63 #endif
64 #endif
65
66 #include <protocols/dumprestore.h>
67
68 #include <errno.h>
69 #include <fcntl.h>
70 #include <stdio.h>
71 #ifdef __STDC__
72 #include <stdlib.h>
73 #include <string.h>
74 #endif
75
76 #ifdef __linux__
77 #include <ext2fs/ext2fs.h>
78 #endif
79
80 #include "dump.h"
81
82 struct dumpdates **ddatev;
83 int nddates;
84 int ddates_in;
85 struct dumptime *dthead;
86
87 static void dumprecout __P((FILE *, struct dumpdates *));
88 static int getrecord __P((FILE *, struct dumpdates *));
89 static int makedumpdate __P((struct dumpdates *, char *));
90 static void readdumptimes __P((FILE *));
91
92 void
93 initdumptimes(int createdumpdates)
94 {
95 FILE *df;
96
97 if ((df = fopen(dumpdates, "r")) == NULL) {
98 if (errno != ENOENT) {
99 quit("cannot read %s: %s\n", dumpdates,
100 strerror(errno));
101 /* NOTREACHED */
102 }
103 if (createdumpdates) {
104 /*
105 * Dumpdates does not exist, make an empty one.
106 */
107 msg("WARNING: no file `%s', making an empty one\n", dumpdates);
108 if ((df = fopen(dumpdates, "w")) == NULL) {
109 quit("cannot create %s: %s\n", dumpdates,
110 strerror(errno));
111 /* NOTREACHED */
112 }
113 (void) fclose(df);
114 if ((df = fopen(dumpdates, "r")) == NULL) {
115 quit("cannot read %s even after creating it: %s\n",
116 dumpdates, strerror(errno));
117 /* NOTREACHED */
118 }
119 }
120 else
121 msg("WARNING: no file `%s'\n", dumpdates);
122 }
123 if (df != NULL) {
124 (void) flock(fileno(df), LOCK_SH);
125 readdumptimes(df);
126 (void) fclose(df);
127 }
128 }
129
130 static void
131 readdumptimes(FILE *df)
132 {
133 register int i;
134 register struct dumptime *dtwalk;
135
136 for (;;) {
137 dtwalk = (struct dumptime *)calloc(1, sizeof (struct dumptime));
138 if (getrecord(df, &(dtwalk->dt_value)) < 0)
139 break;
140 nddates++;
141 dtwalk->dt_next = dthead;
142 dthead = dtwalk;
143 }
144
145 ddates_in = 1;
146 /*
147 * arrayify the list, leaving enough room for the additional
148 * record that we may have to add to the ddate structure
149 */
150 ddatev = (struct dumpdates **)
151 calloc((unsigned) (nddates + 1), sizeof (struct dumpdates *));
152 dtwalk = dthead;
153 for (i = nddates - 1; i >= 0; i--, dtwalk = dtwalk->dt_next)
154 ddatev[i] = &dtwalk->dt_value;
155 }
156
157 void
158 getdumptime(int createdumpdates)
159 {
160 register struct dumpdates *ddp;
161 register int i;
162 char *fname;
163
164 fname = disk;
165 #ifdef FDEBUG
166 msg("Looking for name %s in dumpdates = %s for level = %c\n",
167 fname, dumpdates, level);
168 #endif
169 spcl.c_ddate = 0;
170 lastlevel = '0';
171
172 /* If this is a level 0 dump, and we're not updating
173 dumpdates, there's no point in trying to read
174 dumpdates. It may not exist yet, or may not be mounted. For
175 incrementals, we *must* read dumpdates (fail if it's not there!) */
176 if ( (level == lastlevel) && !createdumpdates)
177 return;
178 initdumptimes(createdumpdates);
179 if (ddatev == NULL)
180 return;
181 /*
182 * Go find the entry with the same name for a lower increment
183 * and older date
184 */
185 ITITERATE(i, ddp) {
186 if (strncmp(fname, ddp->dd_name, sizeof (ddp->dd_name)) != 0)
187 continue;
188 if (ddp->dd_level >= level)
189 continue;
190 #ifdef __linux__
191 if (ddp->dd_ddate <= (time_t)spcl.c_ddate)
192 #else
193 if (ddp->dd_ddate <= spcl.c_ddate)
194 #endif
195 continue;
196 spcl.c_ddate = ddp->dd_ddate;
197 lastlevel = ddp->dd_level;
198 }
199 }
200
201 void
202 putdumptime(void)
203 {
204 FILE *df;
205 register struct dumpdates *dtwalk;
206 register int i;
207 int fd;
208 char *fname;
209
210 if(uflag == 0)
211 return;
212 if ((df = fopen(dumpdates, "r+")) == NULL)
213 quit("cannot rewrite %s: %s\n", dumpdates, strerror(errno));
214 fd = fileno(df);
215 (void) flock(fd, LOCK_EX);
216 fname = disk;
217 free((char *)ddatev);
218 ddatev = 0;
219 nddates = 0;
220 dthead = 0;
221 ddates_in = 0;
222 readdumptimes(df);
223 if (fseek(df, 0L, 0) < 0)
224 quit("fseek: %s\n", strerror(errno));
225 spcl.c_ddate = 0;
226 ITITERATE(i, dtwalk) {
227 if (strncmp(fname, dtwalk->dd_name,
228 sizeof (dtwalk->dd_name)) != 0)
229 continue;
230 if (dtwalk->dd_level != level)
231 continue;
232 goto found;
233 }
234 /*
235 * construct the new upper bound;
236 * Enough room has been allocated.
237 */
238 dtwalk = ddatev[nddates] =
239 (struct dumpdates *)calloc(1, sizeof (struct dumpdates));
240 nddates += 1;
241 found:
242 (void) strncpy(dtwalk->dd_name, fname, sizeof (dtwalk->dd_name));
243 dtwalk->dd_level = level;
244 dtwalk->dd_ddate = spcl.c_date;
245
246 ITITERATE(i, dtwalk) {
247 dumprecout(df, dtwalk);
248 }
249 if (fflush(df))
250 quit("%s: %s\n", dumpdates, strerror(errno));
251 if (ftruncate(fd, ftell(df)))
252 quit("ftruncate (%s): %s\n", dumpdates, strerror(errno));
253 (void) fclose(df);
254 msg("level %c dump on %s", level,
255 #ifdef __linux__
256 spcl.c_date == 0 ? "the epoch\n" : ctime4(&spcl.c_date));
257 #else
258 spcl.c_date == 0 ? "the epoch\n" : ctime(&spcl.c_date));
259 #endif
260 }
261
262 static void
263 dumprecout(FILE *file, struct dumpdates *what)
264 {
265
266 if (fprintf(file, "%s %c %s",
267 what->dd_name,
268 what->dd_level,
269 ctime(&what->dd_ddate)) < 0)
270 quit("%s: %s\n", dumpdates, strerror(errno));
271 }
272
273 int recno;
274
275 static int
276 getrecord(FILE *df, struct dumpdates *ddatep)
277 {
278 char tbuf[BUFSIZ];
279
280 recno = 0;
281 if (fgets(tbuf, sizeof (tbuf), df) == NULL)
282 return(-1);
283 recno++;
284 if (makedumpdate(ddatep, tbuf) < 0)
285 msg("Unknown intermediate format in %s, line %d\n",
286 dumpdates, recno);
287
288 #ifdef FDEBUG
289 msg("getrecord: %s %c %s", ddatep->dd_name, ddatep->dd_level,
290 ddatep->dd_ddate == 0 ? "the epoch\n" : ctime(&ddatep->dd_ddate));
291 #endif
292 return(0);
293 }
294
295 static int
296 makedumpdate(struct dumpdates *ddp, char *tbuf)
297 {
298 char *tok;
299
300 /* device name */
301 if ( NULL == (tok = strsep( &tbuf, " ")) )
302 return(-1);
303 if ( strlen(tok) > MAXPATHLEN )
304 return(-1);
305 strcpy(ddp->dd_name, tok);
306
307 /* eat whitespace */
308 for( ; *tbuf == ' ' ; tbuf++);
309
310 /* dump level */
311 ddp->dd_level = *tbuf;
312 ++tbuf;
313
314 /* eat whitespace */
315 for( ; *tbuf == ' ' ; tbuf++);
316
317 /* dump date */
318 ddp->dd_ddate = unctime(tbuf);
319 if (ddp->dd_ddate < 0)
320 return(-1);
321 /* fstab entry */
322 ddp->dd_fstab = fstabsearch(ddp->dd_name);
323 return(0);
324 }