]>
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 SP |
4 | * Remy Card <card@Linux.EU.Org>, 1994-1997 |
5 | * Stelian Pop <pop@cybercable.fr>, 1999 | |
1227625a SP |
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. | |
ec387a12 SP |
40 | * |
41 | * $Id: itime.c,v 1.5 1999/10/11 13:31:11 stelian Exp $ | |
1227625a SP |
42 | */ |
43 | ||
1227625a SP |
44 | #include <sys/param.h> |
45 | #include <sys/time.h> | |
46 | #ifdef __linux__ | |
47 | #include <linux/ext2_fs.h> | |
48 | #include <bsdcompat.h> | |
49 | #include <sys/file.h> | |
b45f51d6 | 50 | #include <unistd.h> |
1227625a SP |
51 | #else |
52 | #ifdef sunos | |
53 | #include <sys/vnode.h> | |
54 | ||
55 | #include <ufs/fsdir.h> | |
56 | #include <ufs/inode.h> | |
57 | #include <ufs/fs.h> | |
58 | #else | |
59 | #include <ufs/ufs/dinode.h> | |
60 | #endif | |
61 | #endif | |
62 | ||
63 | #include <protocols/dumprestore.h> | |
64 | ||
65 | #include <errno.h> | |
66 | #include <fcntl.h> | |
67 | #include <stdio.h> | |
68 | #ifdef __STDC__ | |
69 | #include <stdlib.h> | |
70 | #include <string.h> | |
1227625a SP |
71 | #endif |
72 | ||
73 | #ifdef __linux__ | |
74 | #include <ext2fs/ext2fs.h> | |
75 | #endif | |
76 | ||
77 | #include "dump.h" | |
78 | ||
79 | struct dumpdates **ddatev = 0; | |
80 | int nddates = 0; | |
81 | int ddates_in = 0; | |
82 | struct dumptime *dthead = 0; | |
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 | |
8d4197bb | 90 | initdumptimes(int createdumpdates) |
1227625a SP |
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 | } | |
8d4197bb SP |
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 | } | |
1227625a | 116 | } |
8d4197bb SP |
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); | |
1227625a | 123 | (void) fclose(df); |
1227625a | 124 | } |
1227625a SP |
125 | } |
126 | ||
127 | static void | |
ddd2ef55 | 128 | readdumptimes(FILE *df) |
1227625a SP |
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 | |
8d4197bb | 155 | getdumptime(int createdumpdates) |
1227625a SP |
156 | { |
157 | register struct dumpdates *ddp; | |
158 | register int i; | |
159 | char *fname; | |
160 | ||
161 | fname = disk; | |
162 | #ifdef FDEBUG | |
163 | msg("Looking for name %s in dumpdates = %s for level = %c\n", | |
164 | fname, dumpdates, level); | |
165 | #endif | |
166 | spcl.c_ddate = 0; | |
167 | lastlevel = '0'; | |
168 | ||
ddd2ef55 SP |
169 | /* if we're not going to update dumpdates, there's no point in reading |
170 | it, particularly since /var might not be mounted... wait until here | |
171 | to benefit from the initialization of variables needed by parent */ | |
8d4197bb SP |
172 | if (!uflag && level == lastlevel) |
173 | return; | |
174 | initdumptimes(createdumpdates); | |
175 | if (ddatev == NULL) | |
ddd2ef55 | 176 | return; |
1227625a SP |
177 | /* |
178 | * Go find the entry with the same name for a lower increment | |
179 | * and older date | |
180 | */ | |
181 | ITITERATE(i, ddp) { | |
182 | if (strncmp(fname, ddp->dd_name, sizeof (ddp->dd_name)) != 0) | |
183 | continue; | |
184 | if (ddp->dd_level >= level) | |
185 | continue; | |
186 | #ifdef __linux__ | |
187 | if (ddp->dd_ddate <= (time_t)spcl.c_ddate) | |
188 | #else | |
189 | if (ddp->dd_ddate <= spcl.c_ddate) | |
190 | #endif | |
191 | continue; | |
192 | spcl.c_ddate = ddp->dd_ddate; | |
193 | lastlevel = ddp->dd_level; | |
194 | } | |
195 | } | |
196 | ||
197 | void | |
ddd2ef55 | 198 | putdumptime(void) |
1227625a SP |
199 | { |
200 | FILE *df; | |
201 | register struct dumpdates *dtwalk; | |
202 | register int i; | |
203 | int fd; | |
204 | char *fname; | |
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 | (void) flock(fd, LOCK_EX); | |
212 | fname = disk; | |
213 | free((char *)ddatev); | |
214 | ddatev = 0; | |
215 | nddates = 0; | |
216 | dthead = 0; | |
217 | ddates_in = 0; | |
218 | readdumptimes(df); | |
219 | if (fseek(df, 0L, 0) < 0) | |
220 | quit("fseek: %s\n", strerror(errno)); | |
221 | spcl.c_ddate = 0; | |
222 | ITITERATE(i, dtwalk) { | |
223 | if (strncmp(fname, dtwalk->dd_name, | |
224 | sizeof (dtwalk->dd_name)) != 0) | |
225 | continue; | |
226 | if (dtwalk->dd_level != level) | |
227 | continue; | |
228 | goto found; | |
229 | } | |
230 | /* | |
231 | * construct the new upper bound; | |
232 | * Enough room has been allocated. | |
233 | */ | |
234 | dtwalk = ddatev[nddates] = | |
235 | (struct dumpdates *)calloc(1, sizeof (struct dumpdates)); | |
236 | nddates += 1; | |
237 | found: | |
238 | (void) strncpy(dtwalk->dd_name, fname, sizeof (dtwalk->dd_name)); | |
239 | dtwalk->dd_level = level; | |
240 | dtwalk->dd_ddate = spcl.c_date; | |
241 | ||
242 | ITITERATE(i, dtwalk) { | |
243 | dumprecout(df, dtwalk); | |
244 | } | |
245 | if (fflush(df)) | |
246 | quit("%s: %s\n", dumpdates, strerror(errno)); | |
247 | if (ftruncate(fd, ftell(df))) | |
248 | quit("ftruncate (%s): %s\n", dumpdates, strerror(errno)); | |
249 | (void) fclose(df); | |
250 | msg("level %c dump on %s", level, | |
251 | #ifdef __linux__ | |
252 | spcl.c_date == 0 ? "the epoch\n" : ctime4(&spcl.c_date)); | |
253 | #else | |
254 | spcl.c_date == 0 ? "the epoch\n" : ctime(&spcl.c_date)); | |
255 | #endif | |
256 | } | |
257 | ||
258 | static void | |
ddd2ef55 | 259 | dumprecout(FILE *file, struct dumpdates *what) |
1227625a SP |
260 | { |
261 | ||
262 | if (fprintf(file, DUMPOUTFMT, | |
263 | what->dd_name, | |
264 | what->dd_level, | |
265 | ctime(&what->dd_ddate)) < 0) | |
266 | quit("%s: %s\n", dumpdates, strerror(errno)); | |
267 | } | |
268 | ||
269 | int recno; | |
270 | ||
271 | static int | |
ddd2ef55 | 272 | getrecord(FILE *df, struct dumpdates *ddatep) |
1227625a SP |
273 | { |
274 | char tbuf[BUFSIZ]; | |
275 | ||
276 | recno = 0; | |
ddd2ef55 | 277 | if (fgets(tbuf, sizeof (tbuf), df) == NULL) |
1227625a SP |
278 | return(-1); |
279 | recno++; | |
280 | if (makedumpdate(ddatep, tbuf) < 0) | |
281 | msg("Unknown intermediate format in %s, line %d\n", | |
282 | dumpdates, recno); | |
283 | ||
284 | #ifdef FDEBUG | |
285 | msg("getrecord: %s %c %s", ddatep->dd_name, ddatep->dd_level, | |
286 | ddatep->dd_ddate == 0 ? "the epoch\n" : ctime(&ddatep->dd_ddate)); | |
287 | #endif | |
288 | return(0); | |
289 | } | |
290 | ||
291 | static int | |
ddd2ef55 | 292 | makedumpdate(struct dumpdates *ddp, char *tbuf) |
1227625a | 293 | { |
ddd2ef55 | 294 | char un_buf[BUFSIZ]; |
1227625a SP |
295 | |
296 | (void) sscanf(tbuf, DUMPINFMT, ddp->dd_name, &ddp->dd_level, un_buf); | |
297 | ddp->dd_ddate = unctime(un_buf); | |
298 | if (ddp->dd_ddate < 0) | |
299 | return(-1); | |
300 | return(0); | |
301 | } |