Imported Upstream version 0.4b37
[debian/dump] / 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.28 2004/06/17 09:01:15 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 #ifdef  __linux__
56 #ifdef HAVE_EXT2FS_EXT2_FS_H
57 #include <ext2fs/ext2_fs.h>
58 #else
59 #include <linux/ext2_fs.h>
60 #endif
61 #include <ext2fs/ext2fs.h>
62 #include <bsdcompat.h>
63 #include <sys/file.h>
64 #include <unistd.h>
65 #elif defined sunos
66 #include <sys/vnode.h>
67
68 #include <ufs/fsdir.h>
69 #include <ufs/inode.h>
70 #include <ufs/fs.h>
71 #else
72 #include <ufs/ufs/dinode.h>
73 #endif
74
75 #include <protocols/dumprestore.h>
76
77 #include "dump.h"
78
79 struct  dumpdates **ddatev;
80 int     nddates;
81 int     ddates_in;
82 struct  dumptime *dthead;
83
84 static  void dumprecout __P((FILE *, struct dumpdates *));
85 static  int getrecord __P((FILE *, struct dumpdates *));
86 static  int makedumpdate __P((struct dumpdates *, char *));
87 static  void readdumptimes __P((FILE *));
88
89 void
90 initdumptimes(int createdumpdates)
91 {
92         FILE *df;
93         struct flock lock;
94
95         if ((df = fopen(dumpdates, "r")) == NULL) {
96                 if (errno != ENOENT) {
97                         quit("cannot read %s: %s\n", dumpdates,
98                             strerror(errno));
99                         /* NOTREACHED */
100                 }
101                 if (createdumpdates) {
102                         /*
103                          * Dumpdates does not exist, make an empty one.
104                          */
105                         msg("WARNING: no file `%s', making an empty one\n", dumpdates);
106                         if ((df = fopen(dumpdates, "w")) == NULL) {
107                                 quit("cannot create %s: %s\n", dumpdates,
108                                 strerror(errno));
109                                 /* NOTREACHED */
110                         }
111                         (void) fclose(df);
112                         if ((df = fopen(dumpdates, "r")) == NULL) {
113                                 quit("cannot read %s even after creating it: %s\n",
114                                 dumpdates, strerror(errno));
115                                 /* NOTREACHED */
116                         }
117                 }
118                 else
119                         msg("WARNING: no file `%s'\n", dumpdates);
120         }
121         if (df != NULL) {
122                 memset(&lock, 0, sizeof(lock));
123                 lock.l_type = F_RDLCK;
124                 if (fcntl(fileno(df), F_SETLKW, &lock) < 0)
125                         quit("cannot set read lock on %s: %s\n",
126                                 dumpdates, strerror(errno));
127                 readdumptimes(df);
128                 (void) fclose(df);
129         }
130 }
131
132 static void
133 readdumptimes(FILE *df)
134 {
135         int i;
136         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         struct dumpdates *ddp;
163         int i;
164
165 #ifdef FDEBUG
166         msg("Looking for name %s in dumpdates = %s for level = %s\n",
167                 disk, dumpdates, level);
168 #endif
169         spcl.c_ddate = 0;
170         memset(&lastlevel, 0, NUM_STR_SIZE);
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 ( (!strcmp(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(disk, ddp->dd_name, sizeof (ddp->dd_name)) != 0)
187                         continue;
188                 if (ddp->dd_level >= atoi(level))
189                         continue;
190                 if (ddp->dd_ddate <= (time_t)spcl.c_ddate)
191                         continue;
192                 spcl.c_ddate = ddp->dd_ddate;
193                 snprintf(lastlevel, NUM_STR_SIZE, "%d", ddp->dd_level);
194         }
195 }
196
197 void
198 putdumptime(void)
199 {
200         FILE *df;
201         struct dumpdates *dtwalk;
202         int i;
203         int fd;
204         struct flock lock;
205
206         if(uflag == 0)
207                 return;
208         if ((df = fopen(dumpdates, "r+")) == NULL)
209                 quit("cannot rewrite %s: %s\n", dumpdates, strerror(errno));
210         fd = fileno(df);
211         memset(&lock, 0, sizeof(lock));
212         lock.l_type = F_WRLCK;
213         if (fcntl(fd, F_SETLKW, &lock) < 0)
214                 quit("cannot set write lock on %s: %s\n", dumpdates, strerror(errno));
215         free((char *)ddatev);
216         ddatev = 0;
217         nddates = 0;
218         dthead = 0;
219         ddates_in = 0;
220         readdumptimes(df);
221         if (fseek(df, 0L, 0) < 0)
222                 quit("fseek: %s\n", strerror(errno));
223         spcl.c_ddate = 0;
224         ITITERATE(i, dtwalk) {
225                 if (strncmp(disk, dtwalk->dd_name,
226                                 sizeof (dtwalk->dd_name)) != 0)
227                         continue;
228                 if (dtwalk->dd_level != atoi(level))
229                         continue;
230                 goto found;
231         }
232         /*
233          *      construct the new upper bound;
234          *      Enough room has been allocated.
235          */
236         dtwalk = ddatev[nddates] =
237                 (struct dumpdates *)calloc(1, sizeof (struct dumpdates));
238         nddates += 1;
239   found:
240         (void) strncpy(dtwalk->dd_name, disk, sizeof (dtwalk->dd_name));
241         dtwalk->dd_level = atoi(level);
242         dtwalk->dd_ddate = spcl.c_date;
243
244         ITITERATE(i, dtwalk) {
245                 dumprecout(df, dtwalk);
246         }
247         if (fflush(df))
248                 quit("%s: %s\n", dumpdates, strerror(errno));
249         if (ftruncate(fd, ftell(df)))
250                 quit("ftruncate (%s): %s\n", dumpdates, strerror(errno));
251         (void) fclose(df);
252 }
253
254 static void
255 dumprecout(FILE *file, struct dumpdates *what)
256 {
257         char buf[26];
258         struct tm *tms;
259
260         tms = localtime(&what->dd_ddate);
261         strncpy(buf, asctime(tms), sizeof(buf));
262         if (buf[24] != '\n' || buf[25] != '\0')
263                 quit("asctime returned an unexpected string\n");
264         buf[24] = 0;
265         if (fprintf(file, "%s %d %s %c%2.2d%2.2d\n",
266                     what->dd_name,
267                     what->dd_level,
268                     buf,
269                     (tms->tm_gmtoff < 0 ? '-' : '+'),
270                     abs(tms->tm_gmtoff) / 3600,
271                     abs(tms->tm_gmtoff) % 3600 / 60) < 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 format in %s, line %d\n",
288                         dumpdates, recno);
289                 return(-1);
290         }
291
292 #ifdef FDEBUG
293         msg("getrecord: %s %d %s", ddatep->dd_name, ddatep->dd_level,
294             ddatep->dd_ddate == 0 ? "the epoch\n" : ctime(&ddatep->dd_ddate));
295 #endif
296         return(0);
297 }
298
299 static int
300 makedumpdate(struct dumpdates *ddp, char *tbuf)
301 {
302         char *tok;
303         /* device name */
304         if ( NULL == (tok = strsep( &tbuf, " ")) )
305                 return(-1);
306         if ( !tbuf || 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 = atoi(tbuf);
315         /* eat the rest of the numbers*/
316         for( ; *tbuf >= '0' && *tbuf <= '9' ; tbuf++);
317
318         /* eat whitespace */
319         for( ; *tbuf == ' ' ; tbuf++);
320
321         /* dump date */
322         ddp->dd_ddate = unctime(tbuf);
323         if (ddp->dd_ddate < 0)
324                 return(-1);
325         /* fstab entry */
326         ddp->dd_fstab = fstabsearch(ddp->dd_name);
327         return(0);
328 }