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