]> git.wh0rd.org Git - dump.git/blob - dump/itime.c
Blocksize default is 32 when using -d HIGHDENSITYTREC.
[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 <stelian@popies.net>, 1999-2000
6  *      Stelian Pop <stelian@popies.net> - AlcĂ´ve <www.alcove.com>, 2000-2002
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. Neither the name of the University nor the names of its contributors
22  *    may be used to endorse or promote products derived from this software
23  *    without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  */
37
38 #ifndef lint
39 static const char rcsid[] =
40         "$Id: itime.c,v 1.25 2003/03/30 15:40:36 stelian Exp $";
41 #endif /* not lint */
42
43 #include <config.h>
44 #include <errno.h>
45 #include <fcntl.h>
46 #include <stdio.h>
47 #ifdef __STDC__
48 #include <stdlib.h>
49 #include <string.h>
50 #endif
51
52 #include <sys/param.h>
53 #include <sys/time.h>
54 #include <time.h>
55 #include <fcntl.h>
56 #ifdef  __linux__
57 #ifdef HAVE_EXT2FS_EXT2_FS_H
58 #include <ext2fs/ext2_fs.h>
59 #else
60 #include <linux/ext2_fs.h>
61 #endif
62 #include <ext2fs/ext2fs.h>
63 #include <bsdcompat.h>
64 #include <sys/file.h>
65 #include <unistd.h>
66 #elif defined sunos
67 #include <sys/vnode.h>
68
69 #include <ufs/fsdir.h>
70 #include <ufs/inode.h>
71 #include <ufs/fs.h>
72 #else
73 #include <ufs/ufs/dinode.h>
74 #endif
75
76 #include <protocols/dumprestore.h>
77
78 #include "dump.h"
79
80 struct  dumpdates **ddatev;
81 int     nddates;
82 int     ddates_in;
83 struct  dumptime *dthead;
84
85 static  void dumprecout __P((FILE *, struct dumpdates *));
86 static  int getrecord __P((FILE *, struct dumpdates *));
87 static  int makedumpdate __P((struct dumpdates *, char *));
88 static  void readdumptimes __P((FILE *));
89
90 void
91 initdumptimes(int createdumpdates)
92 {
93         FILE *df;
94         struct flock lock;
95
96         if ((df = fopen(dumpdates, "r")) == NULL) {
97                 if (errno != ENOENT) {
98                         quit("cannot read %s: %s\n", dumpdates,
99                             strerror(errno));
100                         /* NOTREACHED */
101                 }
102                 if (createdumpdates) {
103                         /*
104                          * Dumpdates does not exist, make an empty one.
105                          */
106                         msg("WARNING: no file `%s', making an empty one\n", dumpdates);
107                         if ((df = fopen(dumpdates, "w")) == NULL) {
108                                 quit("cannot create %s: %s\n", dumpdates,
109                                 strerror(errno));
110                                 /* NOTREACHED */
111                         }
112                         (void) fclose(df);
113                         if ((df = fopen(dumpdates, "r")) == NULL) {
114                                 quit("cannot read %s even after creating it: %s\n",
115                                 dumpdates, strerror(errno));
116                                 /* NOTREACHED */
117                         }
118                 }
119                 else
120                         msg("WARNING: no file `%s'\n", dumpdates);
121         }
122         if (df != NULL) {
123                 memset(&lock, 0, sizeof(lock));
124                 lock.l_type = F_RDLCK;
125                 if (fcntl(fileno(df), F_SETLKW, &lock) < 0)
126                         quit("cannot set read lock on %s: %s\n",
127                                 dumpdates, strerror(errno));
128                 readdumptimes(df);
129                 (void) fclose(df);
130         }
131 }
132
133 static void
134 readdumptimes(FILE *df)
135 {
136         int i;
137         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         struct dumpdates *ddp;
164         int i;
165
166 #ifdef FDEBUG
167         msg("Looking for name %s in dumpdates = %s for level = %c\n",
168                 disk, 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(disk, ddp->dd_name, sizeof (ddp->dd_name)) != 0)
188                         continue;
189                 if (ddp->dd_level >= level)
190                         continue;
191                 if (ddp->dd_ddate <= (time_t)spcl.c_ddate)
192                         continue;
193                 spcl.c_ddate = ddp->dd_ddate;
194                 lastlevel = ddp->dd_level;
195         }
196 }
197
198 void
199 putdumptime(void)
200 {
201         FILE *df;
202         struct dumpdates *dtwalk;
203         int i;
204         int fd;
205         struct flock lock;
206
207         if(uflag == 0)
208                 return;
209         if ((df = fopen(dumpdates, "r+")) == NULL)
210                 quit("cannot rewrite %s: %s\n", dumpdates, strerror(errno));
211         fd = fileno(df);
212         memset(&lock, 0, sizeof(lock));
213         lock.l_type = F_WRLCK;
214         if (fcntl(fd, F_SETLKW, &lock) < 0)
215                 quit("cannot set write lock on %s: %s\n", dumpdates, strerror(errno));
216         free((char *)ddatev);
217         ddatev = 0;
218         nddates = 0;
219         dthead = 0;
220         ddates_in = 0;
221         readdumptimes(df);
222         if (fseek(df, 0L, 0) < 0)
223                 quit("fseek: %s\n", strerror(errno));
224         spcl.c_ddate = 0;
225         ITITERATE(i, dtwalk) {
226                 if (strncmp(disk, dtwalk->dd_name,
227                                 sizeof (dtwalk->dd_name)) != 0)
228                         continue;
229                 if (dtwalk->dd_level != level)
230                         continue;
231                 goto found;
232         }
233         /*
234          *      construct the new upper bound;
235          *      Enough room has been allocated.
236          */
237         dtwalk = ddatev[nddates] =
238                 (struct dumpdates *)calloc(1, sizeof (struct dumpdates));
239         nddates += 1;
240   found:
241         (void) strncpy(dtwalk->dd_name, disk, sizeof (dtwalk->dd_name));
242         dtwalk->dd_level = level;
243         dtwalk->dd_ddate = spcl.c_date;
244
245         ITITERATE(i, dtwalk) {
246                 dumprecout(df, dtwalk);
247         }
248         if (fflush(df))
249                 quit("%s: %s\n", dumpdates, strerror(errno));
250         if (ftruncate(fd, ftell(df)))
251                 quit("ftruncate (%s): %s\n", dumpdates, strerror(errno));
252         (void) fclose(df);
253 }
254
255 static void
256 dumprecout(FILE *file, struct dumpdates *what)
257 {
258         char buf[26];
259         struct tm *tms;
260
261         tms = localtime(&what->dd_ddate);
262         strncpy(buf, asctime(tms), sizeof(buf));
263         if (buf[24] != '\n' || buf[25] != '\0')
264                 quit("asctime returned an unexpected string\n");
265         buf[24] = 0;
266         if (fprintf(file, "%s %c %s %c%2.2d%2.2d\n",
267                     what->dd_name,
268                     what->dd_level,
269                     buf,
270                     (tms->tm_gmtoff < 0 ? '-' : '+'),
271                     abs(tms->tm_gmtoff) / 3600,
272                     abs(tms->tm_gmtoff) % 3600 / 60) < 0)
273                 quit("%s: %s\n", dumpdates, strerror(errno));
274 }
275
276 int     recno;
277
278 static int
279 getrecord(FILE *df, struct dumpdates *ddatep)
280 {
281         char tbuf[BUFSIZ];
282
283         recno = 0;
284         if (fgets(tbuf, sizeof (tbuf), df) == NULL)
285                 return(-1);
286         recno++;
287         if (makedumpdate(ddatep, tbuf) < 0)
288                 msg("Unknown intermediate format in %s, line %d\n",
289                         dumpdates, recno);
290
291 #ifdef FDEBUG
292         msg("getrecord: %s %c %s", ddatep->dd_name, ddatep->dd_level,
293             ddatep->dd_ddate == 0 ? "the epoch\n" : ctime(&ddatep->dd_ddate));
294 #endif
295         return(0);
296 }
297
298 static int
299 makedumpdate(struct dumpdates *ddp, char *tbuf)
300 {
301         char *tok;
302         
303         /* device name */
304         if ( NULL == (tok = strsep( &tbuf, " ")) )
305                 return(-1);
306         if ( strlen(tok) >  MAXPATHLEN )
307                 return(-1);
308         strcpy(ddp->dd_name, tok);
309
310         /* eat whitespace */
311         for( ; *tbuf == ' ' ; tbuf++);
312
313         /* dump level */
314         ddp->dd_level = *tbuf;
315         ++tbuf;
316
317         /* eat whitespace */
318         for( ; *tbuf == ' ' ; tbuf++);
319
320         /* dump date */
321         ddp->dd_ddate = unctime(tbuf);
322         if (ddp->dd_ddate < 0)
323                 return(-1);
324         /* fstab entry */
325         ddp->dd_fstab = fstabsearch(ddp->dd_name);
326         return(0);
327 }