]>
Commit | Line | Data |
---|---|---|
1227625a SP |
1 | /* |
2 | * Ported to Linux's Second Extended File System as part of the | |
3 | * dump and restore backup suit | |
b45f51d6 | 4 | * Remy Card <card@Linux.EU.Org>, 1994-1997 |
df9ae507 | 5 | * Stelian Pop <pop@cybercable.fr>, 1999 |
1227625a SP |
6 | */ |
7 | ||
8 | /*- | |
9 | * Copyright (c) 1980, 1993 | |
10 | * The Regents of the University of California. All rights reserved. | |
11 | * | |
12 | * Redistribution and use in source and binary forms, with or without | |
13 | * modification, are permitted provided that the following conditions | |
14 | * are met: | |
15 | * 1. Redistributions of source code must retain the above copyright | |
16 | * notice, this list of conditions and the following disclaimer. | |
17 | * 2. Redistributions in binary form must reproduce the above copyright | |
18 | * notice, this list of conditions and the following disclaimer in the | |
19 | * documentation and/or other materials provided with the distribution. | |
20 | * 3. All advertising materials mentioning features or use of this software | |
21 | * must display the following acknowledgement: | |
22 | * This product includes software developed by the University of | |
23 | * California, Berkeley and its contributors. | |
24 | * 4. Neither the name of the University nor the names of its contributors | |
25 | * may be used to endorse or promote products derived from this software | |
26 | * without specific prior written permission. | |
27 | * | |
28 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
29 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
30 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
31 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
32 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
33 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
34 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
35 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
36 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
37 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
38 | * SUCH DAMAGE. | |
39 | */ | |
40 | ||
df9ae507 SP |
41 | #ifndef lint |
42 | static const char rcsid[] = | |
60f5ede9 | 43 | "$Id: itime.c,v 1.7 1999/10/31 19:48:28 tiniou Exp $"; |
df9ae507 SP |
44 | #endif /* not lint */ |
45 | ||
1227625a SP |
46 | #include <sys/param.h> |
47 | #include <sys/time.h> | |
48 | #ifdef __linux__ | |
49 | #include <linux/ext2_fs.h> | |
50 | #include <bsdcompat.h> | |
51 | #include <sys/file.h> | |
b45f51d6 | 52 | #include <unistd.h> |
1227625a SP |
53 | #else |
54 | #ifdef sunos | |
55 | #include <sys/vnode.h> | |
56 | ||
57 | #include <ufs/fsdir.h> | |
58 | #include <ufs/inode.h> | |
59 | #include <ufs/fs.h> | |
60 | #else | |
61 | #include <ufs/ufs/dinode.h> | |
62 | #endif | |
63 | #endif | |
64 | ||
65 | #include <protocols/dumprestore.h> | |
66 | ||
67 | #include <errno.h> | |
68 | #include <fcntl.h> | |
69 | #include <stdio.h> | |
70 | #ifdef __STDC__ | |
71 | #include <stdlib.h> | |
72 | #include <string.h> | |
1227625a SP |
73 | #endif |
74 | ||
75 | #ifdef __linux__ | |
76 | #include <ext2fs/ext2fs.h> | |
77 | #endif | |
78 | ||
79 | #include "dump.h" | |
80 | ||
81 | struct dumpdates **ddatev = 0; | |
82 | int nddates = 0; | |
83 | int ddates_in = 0; | |
84 | struct dumptime *dthead = 0; | |
85 | ||
86 | static void dumprecout __P((FILE *, struct dumpdates *)); | |
87 | static int getrecord __P((FILE *, struct dumpdates *)); | |
88 | static int makedumpdate __P((struct dumpdates *, char *)); | |
89 | static void readdumptimes __P((FILE *)); | |
90 | ||
91 | void | |
8d4197bb | 92 | initdumptimes(int createdumpdates) |
1227625a SP |
93 | { |
94 | FILE *df; | |
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 | } | |
8d4197bb SP |
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 | } | |
1227625a | 118 | } |
8d4197bb SP |
119 | else |
120 | msg("WARNING: no file `%s'\n", dumpdates); | |
121 | } | |
122 | if (df != NULL) { | |
123 | (void) flock(fileno(df), LOCK_SH); | |
124 | readdumptimes(df); | |
1227625a | 125 | (void) fclose(df); |
1227625a | 126 | } |
1227625a SP |
127 | } |
128 | ||
129 | static void | |
ddd2ef55 | 130 | readdumptimes(FILE *df) |
1227625a SP |
131 | { |
132 | register int i; | |
133 | register struct dumptime *dtwalk; | |
134 | ||
135 | for (;;) { | |
136 | dtwalk = (struct dumptime *)calloc(1, sizeof (struct dumptime)); | |
137 | if (getrecord(df, &(dtwalk->dt_value)) < 0) | |
138 | break; | |
139 | nddates++; | |
140 | dtwalk->dt_next = dthead; | |
141 | dthead = dtwalk; | |
142 | } | |
143 | ||
144 | ddates_in = 1; | |
145 | /* | |
146 | * arrayify the list, leaving enough room for the additional | |
147 | * record that we may have to add to the ddate structure | |
148 | */ | |
149 | ddatev = (struct dumpdates **) | |
150 | calloc((unsigned) (nddates + 1), sizeof (struct dumpdates *)); | |
151 | dtwalk = dthead; | |
152 | for (i = nddates - 1; i >= 0; i--, dtwalk = dtwalk->dt_next) | |
153 | ddatev[i] = &dtwalk->dt_value; | |
154 | } | |
155 | ||
156 | void | |
8d4197bb | 157 | getdumptime(int createdumpdates) |
1227625a SP |
158 | { |
159 | register struct dumpdates *ddp; | |
160 | register int i; | |
161 | char *fname; | |
162 | ||
163 | fname = disk; | |
164 | #ifdef FDEBUG | |
165 | msg("Looking for name %s in dumpdates = %s for level = %c\n", | |
166 | fname, dumpdates, level); | |
167 | #endif | |
168 | spcl.c_ddate = 0; | |
169 | lastlevel = '0'; | |
170 | ||
60f5ede9 SP |
171 | /* If this is a level 0 dump, there's no point in trying to read |
172 | dumpdates. It may not exist yet, or may not be mounted. For | |
173 | incrementals, we *must* read dumpdates (fail if it's not there!) */ | |
174 | if (level == lastlevel) | |
8d4197bb SP |
175 | return; |
176 | initdumptimes(createdumpdates); | |
177 | if (ddatev == NULL) | |
ddd2ef55 | 178 | return; |
1227625a SP |
179 | /* |
180 | * Go find the entry with the same name for a lower increment | |
181 | * and older date | |
182 | */ | |
183 | ITITERATE(i, ddp) { | |
184 | if (strncmp(fname, ddp->dd_name, sizeof (ddp->dd_name)) != 0) | |
185 | continue; | |
186 | if (ddp->dd_level >= level) | |
187 | continue; | |
188 | #ifdef __linux__ | |
189 | if (ddp->dd_ddate <= (time_t)spcl.c_ddate) | |
190 | #else | |
191 | if (ddp->dd_ddate <= spcl.c_ddate) | |
192 | #endif | |
193 | continue; | |
194 | spcl.c_ddate = ddp->dd_ddate; | |
195 | lastlevel = ddp->dd_level; | |
196 | } | |
197 | } | |
198 | ||
199 | void | |
ddd2ef55 | 200 | putdumptime(void) |
1227625a SP |
201 | { |
202 | FILE *df; | |
203 | register struct dumpdates *dtwalk; | |
204 | register int i; | |
205 | int fd; | |
206 | char *fname; | |
207 | ||
208 | if(uflag == 0) | |
209 | return; | |
210 | if ((df = fopen(dumpdates, "r+")) == NULL) | |
211 | quit("cannot rewrite %s: %s\n", dumpdates, strerror(errno)); | |
212 | fd = fileno(df); | |
213 | (void) flock(fd, LOCK_EX); | |
214 | fname = disk; | |
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(fname, dtwalk->dd_name, | |
226 | sizeof (dtwalk->dd_name)) != 0) | |
227 | continue; | |
228 | if (dtwalk->dd_level != 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, fname, sizeof (dtwalk->dd_name)); | |
241 | dtwalk->dd_level = 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 | msg("level %c dump on %s", level, | |
253 | #ifdef __linux__ | |
254 | spcl.c_date == 0 ? "the epoch\n" : ctime4(&spcl.c_date)); | |
255 | #else | |
256 | spcl.c_date == 0 ? "the epoch\n" : ctime(&spcl.c_date)); | |
257 | #endif | |
258 | } | |
259 | ||
260 | static void | |
ddd2ef55 | 261 | dumprecout(FILE *file, struct dumpdates *what) |
1227625a SP |
262 | { |
263 | ||
264 | if (fprintf(file, DUMPOUTFMT, | |
265 | what->dd_name, | |
266 | what->dd_level, | |
267 | ctime(&what->dd_ddate)) < 0) | |
268 | quit("%s: %s\n", dumpdates, strerror(errno)); | |
269 | } | |
270 | ||
271 | int recno; | |
272 | ||
273 | static int | |
ddd2ef55 | 274 | getrecord(FILE *df, struct dumpdates *ddatep) |
1227625a SP |
275 | { |
276 | char tbuf[BUFSIZ]; | |
277 | ||
278 | recno = 0; | |
ddd2ef55 | 279 | if (fgets(tbuf, sizeof (tbuf), df) == NULL) |
1227625a SP |
280 | return(-1); |
281 | recno++; | |
282 | if (makedumpdate(ddatep, tbuf) < 0) | |
283 | msg("Unknown intermediate format in %s, line %d\n", | |
284 | dumpdates, recno); | |
285 | ||
286 | #ifdef FDEBUG | |
287 | msg("getrecord: %s %c %s", ddatep->dd_name, ddatep->dd_level, | |
288 | ddatep->dd_ddate == 0 ? "the epoch\n" : ctime(&ddatep->dd_ddate)); | |
289 | #endif | |
290 | return(0); | |
291 | } | |
292 | ||
293 | static int | |
ddd2ef55 | 294 | makedumpdate(struct dumpdates *ddp, char *tbuf) |
1227625a | 295 | { |
ddd2ef55 | 296 | char un_buf[BUFSIZ]; |
1227625a SP |
297 | |
298 | (void) sscanf(tbuf, DUMPINFMT, ddp->dd_name, &ddp->dd_level, un_buf); | |
299 | ddp->dd_ddate = unctime(un_buf); | |
300 | if (ddp->dd_ddate < 0) | |
301 | return(-1); | |
302 | return(0); | |
303 | } |