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