]> git.wh0rd.org Git - dump.git/blob - dump/itime.c
Version 0.4b7.
[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.4 1999/10/11 13:08:07 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(int createdumpdates)
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                 if (createdumpdates) {
107                         /*
108                          * Dumpdates does not exist, make an empty one.
109                          */
110                         msg("WARNING: no file `%s', making an empty one\n", dumpdates);
111                         if ((df = fopen(dumpdates, "w")) == NULL) {
112                                 quit("cannot create %s: %s\n", dumpdates,
113                                 strerror(errno));
114                                 /* NOTREACHED */
115                         }
116                         (void) fclose(df);
117                         if ((df = fopen(dumpdates, "r")) == NULL) {
118                                 quit("cannot read %s even after creating it: %s\n",
119                                 dumpdates, strerror(errno));
120                                 /* NOTREACHED */
121                         }
122                 }
123                 else
124                         msg("WARNING: no file `%s'\n", dumpdates);
125         }
126         if (df != NULL) {
127                 (void) flock(fileno(df), LOCK_SH);
128                 readdumptimes(df);
129                 (void) fclose(df);
130         }
131 }
132
133 static void
134 readdumptimes(FILE *df)
135 {
136         register int i;
137         register struct dumptime *dtwalk;
138
139         for (;;) {
140                 dtwalk = (struct dumptime *)calloc(1, sizeof (struct dumptime));
141                 if (getrecord(df, &(dtwalk->dt_value)) < 0)
142                         break;
143                 nddates++;
144                 dtwalk->dt_next = dthead;
145                 dthead = dtwalk;
146         }
147
148         ddates_in = 1;
149         /*
150          *      arrayify the list, leaving enough room for the additional
151          *      record that we may have to add to the ddate structure
152          */
153         ddatev = (struct dumpdates **)
154                 calloc((unsigned) (nddates + 1), sizeof (struct dumpdates *));
155         dtwalk = dthead;
156         for (i = nddates - 1; i >= 0; i--, dtwalk = dtwalk->dt_next)
157                 ddatev[i] = &dtwalk->dt_value;
158 }
159
160 void
161 getdumptime(int createdumpdates)
162 {
163         register struct dumpdates *ddp;
164         register int i;
165         char *fname;
166
167         fname = disk;
168 #ifdef FDEBUG
169         msg("Looking for name %s in dumpdates = %s for level = %c\n",
170                 fname, dumpdates, level);
171 #endif
172         spcl.c_ddate = 0;
173         lastlevel = '0';
174
175         /* if we're not going to update dumpdates, there's no point in reading
176            it, particularly since /var might not be mounted... wait until here
177            to benefit from the initialization of variables needed by parent */
178         if (!uflag && level == lastlevel)
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, DUMPOUTFMT,
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 un_buf[BUFSIZ];
301
302         (void) sscanf(tbuf, DUMPINFMT, ddp->dd_name, &ddp->dd_level, un_buf);
303         ddp->dd_ddate = unctime(un_buf);
304         if (ddp->dd_ddate < 0)
305                 return(-1);
306         return(0);
307 }