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