]>
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 | |
4 | * Remy Card <card@Linux.EU.Org>, 1994, 1995, 1996 | |
5 | * | |
6 | */ | |
7 | ||
8 | /* | |
9 | * Copyright (c) 1983, 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 | ||
41 | #ifndef lint | |
42 | static char sccsid[] = "@(#)utilities.c 8.5 (Berkeley) 4/28/95"; | |
43 | #endif /* not lint */ | |
44 | ||
45 | #include <sys/param.h> | |
46 | #include <sys/stat.h> | |
47 | ||
48 | #ifdef __linux__ | |
49 | #include <sys/time.h> | |
50 | #include <linux/ext2_fs.h> | |
51 | #include <bsdcompat.h> | |
52 | #else /* __linux__ */ | |
53 | #include <ufs/ufs/dinode.h> | |
54 | #include <ufs/ufs/dir.h> | |
55 | #endif /* __linux__ */ | |
56 | ||
57 | #include <errno.h> | |
58 | #include <stdio.h> | |
59 | #include <stdlib.h> | |
60 | #include <string.h> | |
61 | #include <unistd.h> | |
62 | ||
63 | #ifdef __linux__ | |
64 | #include <ext2fs/ext2fs.h> | |
65 | #endif | |
66 | ||
67 | #include "restore.h" | |
68 | #include "extern.h" | |
69 | ||
70 | /* | |
71 | * Insure that all the components of a pathname exist. | |
72 | */ | |
73 | void | |
74 | pathcheck(name) | |
75 | char *name; | |
76 | { | |
77 | register char *cp; | |
78 | struct entry *ep; | |
79 | char *start; | |
80 | ||
81 | start = strrchr(name, '/'); | |
82 | if (start == 0) | |
83 | return; | |
84 | for (cp = start; *cp != '\0'; cp++) { | |
85 | if (*cp != '/') | |
86 | continue; | |
87 | *cp = '\0'; | |
88 | ep = lookupname(name); | |
89 | if (ep == NULL) { | |
90 | /* Safe; we know the pathname exists in the dump. */ | |
91 | ep = addentry(name, pathsearch(name)->d_ino, NODE); | |
92 | newnode(ep); | |
93 | } | |
94 | ep->e_flags |= NEW|KEEP; | |
95 | *cp = '/'; | |
96 | } | |
97 | } | |
98 | ||
99 | /* | |
100 | * Change a name to a unique temporary name. | |
101 | */ | |
102 | void | |
103 | mktempname(ep) | |
104 | register struct entry *ep; | |
105 | { | |
106 | char oldname[MAXPATHLEN]; | |
107 | ||
108 | if (ep->e_flags & TMPNAME) | |
109 | badentry(ep, "mktempname: called with TMPNAME"); | |
110 | ep->e_flags |= TMPNAME; | |
111 | (void) strcpy(oldname, myname(ep)); | |
112 | freename(ep->e_name); | |
113 | ep->e_name = savename(gentempname(ep)); | |
114 | ep->e_namlen = strlen(ep->e_name); | |
115 | renameit(oldname, myname(ep)); | |
116 | } | |
117 | ||
118 | /* | |
119 | * Generate a temporary name for an entry. | |
120 | */ | |
121 | char * | |
122 | gentempname(ep) | |
123 | struct entry *ep; | |
124 | { | |
125 | static char name[MAXPATHLEN]; | |
126 | struct entry *np; | |
127 | long i = 0; | |
128 | ||
129 | for (np = lookupino(ep->e_ino); | |
130 | np != NULL && np != ep; np = np->e_links) | |
131 | i++; | |
132 | if (np == NULL) | |
133 | badentry(ep, "not on ino list"); | |
134 | (void) sprintf(name, "%s%d%d", TMPHDR, i, ep->e_ino); | |
135 | return (name); | |
136 | } | |
137 | ||
138 | /* | |
139 | * Rename a file or directory. | |
140 | */ | |
141 | void | |
142 | renameit(from, to) | |
143 | char *from, *to; | |
144 | { | |
145 | if (!Nflag && rename(from, to) < 0) { | |
146 | fprintf(stderr, "warning: cannot rename %s to %s: %s\n", | |
147 | from, to, strerror(errno)); | |
148 | return; | |
149 | } | |
150 | vprintf(stdout, "rename %s to %s\n", from, to); | |
151 | } | |
152 | ||
153 | /* | |
154 | * Create a new node (directory). | |
155 | */ | |
156 | void | |
157 | newnode(np) | |
158 | struct entry *np; | |
159 | { | |
160 | char *cp; | |
161 | ||
162 | if (np->e_type != NODE) | |
163 | badentry(np, "newnode: not a node"); | |
164 | cp = myname(np); | |
165 | if (command == 'C') return; | |
166 | if (!Nflag && mkdir(cp, 0777) < 0) { | |
167 | np->e_flags |= EXISTED; | |
168 | fprintf(stderr, "warning: %s: %s\n", cp, strerror(errno)); | |
169 | return; | |
170 | } | |
171 | vprintf(stdout, "Make node %s\n", cp); | |
172 | } | |
173 | ||
174 | /* | |
175 | * Remove an old node (directory). | |
176 | */ | |
177 | void | |
178 | removenode(ep) | |
179 | register struct entry *ep; | |
180 | { | |
181 | char *cp; | |
182 | ||
183 | if (ep->e_type != NODE) | |
184 | badentry(ep, "removenode: not a node"); | |
185 | if (ep->e_entries != NULL) | |
186 | badentry(ep, "removenode: non-empty directory"); | |
187 | ep->e_flags |= REMOVED; | |
188 | ep->e_flags &= ~TMPNAME; | |
189 | cp = myname(ep); | |
190 | if (!Nflag && rmdir(cp) < 0) { | |
191 | fprintf(stderr, "warning: %s: %s\n", cp, strerror(errno)); | |
192 | return; | |
193 | } | |
194 | vprintf(stdout, "Remove node %s\n", cp); | |
195 | } | |
196 | ||
197 | /* | |
198 | * Remove a leaf. | |
199 | */ | |
200 | void | |
201 | removeleaf(ep) | |
202 | register struct entry *ep; | |
203 | { | |
204 | char *cp; | |
205 | ||
206 | if (command == 'C') return; | |
207 | ||
208 | if (ep->e_type != LEAF) | |
209 | badentry(ep, "removeleaf: not a leaf"); | |
210 | ep->e_flags |= REMOVED; | |
211 | ep->e_flags &= ~TMPNAME; | |
212 | cp = myname(ep); | |
213 | if (!Nflag && unlink(cp) < 0) { | |
214 | fprintf(stderr, "warning: %s: %s\n", cp, strerror(errno)); | |
215 | return; | |
216 | } | |
217 | vprintf(stdout, "Remove leaf %s\n", cp); | |
218 | } | |
219 | ||
220 | /* | |
221 | * Create a link. | |
222 | */ | |
223 | int | |
224 | linkit(existing, new, type) | |
225 | char *existing, *new; | |
226 | int type; | |
227 | { | |
228 | ||
229 | if (type == SYMLINK) { | |
230 | if (!Nflag && symlink(existing, new) < 0) { | |
231 | fprintf(stderr, | |
232 | "warning: cannot create symbolic link %s->%s: %s\n", | |
233 | new, existing, strerror(errno)); | |
234 | return (FAIL); | |
235 | } | |
236 | } else if (type == HARDLINK) { | |
237 | if (!Nflag && link(existing, new) < 0) { | |
238 | fprintf(stderr, | |
239 | "warning: cannot create hard link %s->%s: %s\n", | |
240 | new, existing, strerror(errno)); | |
241 | return (FAIL); | |
242 | } | |
243 | } else { | |
244 | panic("linkit: unknown type %d\n", type); | |
245 | return (FAIL); | |
246 | } | |
247 | vprintf(stdout, "Create %s link %s->%s\n", | |
248 | type == SYMLINK ? "symbolic" : "hard", new, existing); | |
249 | return (GOOD); | |
250 | } | |
251 | ||
252 | #ifndef __linux__ | |
253 | /* | |
254 | * Create a whiteout | |
255 | */ | |
256 | int | |
257 | addwhiteout(name) | |
258 | char *name; | |
259 | { | |
260 | ||
261 | if (!Nflag && mknod(name, S_IFWHT, 0) < 0) { | |
262 | fprintf(stderr, "warning: cannot create whiteout %s: %s\n", | |
263 | name, strerror(errno)); | |
264 | return (FAIL); | |
265 | } | |
266 | vprintf(stdout, "Create whiteout %s\n", name); | |
267 | return (GOOD); | |
268 | } | |
269 | ||
270 | /* | |
271 | * Delete a whiteout. | |
272 | */ | |
273 | void | |
274 | delwhiteout(ep) | |
275 | register struct entry *ep; | |
276 | { | |
277 | char *name; | |
278 | ||
279 | if (ep->e_type != LEAF) | |
280 | badentry(ep, "delwhiteout: not a leaf"); | |
281 | ep->e_flags |= REMOVED; | |
282 | ep->e_flags &= ~TMPNAME; | |
283 | name = myname(ep); | |
284 | if (!Nflag && undelete(name) < 0) { | |
285 | fprintf(stderr, "warning: cannot delete whiteout %s: %s\n", | |
286 | name, strerror(errno)); | |
287 | return; | |
288 | } | |
289 | vprintf(stdout, "Delete whiteout %s\n", name); | |
290 | } | |
291 | #endif | |
292 | ||
293 | /* | |
294 | * find lowest number file (above "start") that needs to be extracted | |
295 | */ | |
296 | ino_t | |
297 | lowerbnd(start) | |
298 | ino_t start; | |
299 | { | |
300 | register struct entry *ep; | |
301 | ||
302 | for ( ; start < maxino; start++) { | |
303 | ep = lookupino(start); | |
304 | if (ep == NULL || ep->e_type == NODE) | |
305 | continue; | |
306 | if (ep->e_flags & (NEW|EXTRACT)) | |
307 | return (start); | |
308 | } | |
309 | return (start); | |
310 | } | |
311 | ||
312 | /* | |
313 | * find highest number file (below "start") that needs to be extracted | |
314 | */ | |
315 | ino_t | |
316 | upperbnd(start) | |
317 | ino_t start; | |
318 | { | |
319 | register struct entry *ep; | |
320 | ||
321 | for ( ; start > ROOTINO; start--) { | |
322 | ep = lookupino(start); | |
323 | if (ep == NULL || ep->e_type == NODE) | |
324 | continue; | |
325 | if (ep->e_flags & (NEW|EXTRACT)) | |
326 | return (start); | |
327 | } | |
328 | return (start); | |
329 | } | |
330 | ||
331 | /* | |
332 | * report on a badly formed entry | |
333 | */ | |
334 | void | |
335 | badentry(ep, msg) | |
336 | register struct entry *ep; | |
337 | char *msg; | |
338 | { | |
339 | ||
340 | fprintf(stderr, "bad entry: %s\n", msg); | |
341 | fprintf(stderr, "name: %s\n", myname(ep)); | |
342 | fprintf(stderr, "parent name %s\n", myname(ep->e_parent)); | |
343 | if (ep->e_sibling != NULL) | |
344 | fprintf(stderr, "sibling name: %s\n", myname(ep->e_sibling)); | |
345 | if (ep->e_entries != NULL) | |
346 | fprintf(stderr, "next entry name: %s\n", myname(ep->e_entries)); | |
347 | if (ep->e_links != NULL) | |
348 | fprintf(stderr, "next link name: %s\n", myname(ep->e_links)); | |
349 | if (ep->e_next != NULL) | |
350 | fprintf(stderr, | |
351 | "next hashchain name: %s\n", myname(ep->e_next)); | |
352 | fprintf(stderr, "entry type: %s\n", | |
353 | ep->e_type == NODE ? "NODE" : "LEAF"); | |
354 | fprintf(stderr, "inode number: %ld\n", ep->e_ino); | |
355 | panic("flags: %s\n", flagvalues(ep)); | |
356 | } | |
357 | ||
358 | /* | |
359 | * Construct a string indicating the active flag bits of an entry. | |
360 | */ | |
361 | char * | |
362 | flagvalues(ep) | |
363 | register struct entry *ep; | |
364 | { | |
365 | static char flagbuf[BUFSIZ]; | |
366 | ||
367 | (void) strcpy(flagbuf, "|NIL"); | |
368 | flagbuf[0] = '\0'; | |
369 | if (ep->e_flags & REMOVED) | |
370 | (void) strcat(flagbuf, "|REMOVED"); | |
371 | if (ep->e_flags & TMPNAME) | |
372 | (void) strcat(flagbuf, "|TMPNAME"); | |
373 | if (ep->e_flags & EXTRACT) | |
374 | (void) strcat(flagbuf, "|EXTRACT"); | |
375 | if (ep->e_flags & NEW) | |
376 | (void) strcat(flagbuf, "|NEW"); | |
377 | if (ep->e_flags & KEEP) | |
378 | (void) strcat(flagbuf, "|KEEP"); | |
379 | if (ep->e_flags & EXISTED) | |
380 | (void) strcat(flagbuf, "|EXISTED"); | |
381 | return (&flagbuf[1]); | |
382 | } | |
383 | ||
384 | /* | |
385 | * Check to see if a name is on a dump tape. | |
386 | */ | |
387 | ino_t | |
388 | dirlookup(name) | |
389 | const char *name; | |
390 | { | |
391 | struct direct *dp; | |
392 | ino_t ino; | |
393 | ||
394 | ino = ((dp = pathsearch(name)) == NULL) ? 0 : dp->d_ino; | |
395 | ||
396 | if (ino == 0 || TSTINO(ino, dumpmap) == 0) | |
397 | fprintf(stderr, "%s is not on the tape\n", name); | |
398 | return (ino); | |
399 | } | |
400 | ||
401 | /* | |
402 | * Elicit a reply. | |
403 | */ | |
404 | int | |
405 | reply(question) | |
406 | char *question; | |
407 | { | |
408 | char c; | |
409 | ||
410 | do { | |
411 | fprintf(stderr, "%s? [yn] ", question); | |
412 | (void) fflush(stderr); | |
413 | c = getc(terminal); | |
414 | while (c != '\n' && getc(terminal) != '\n') | |
415 | if (feof(terminal)) | |
416 | return (FAIL); | |
417 | } while (c != 'y' && c != 'n'); | |
418 | if (c == 'y') | |
419 | return (GOOD); | |
420 | return (FAIL); | |
421 | } | |
422 | ||
423 | /* | |
424 | * handle unexpected inconsistencies | |
425 | */ | |
426 | #if __STDC__ | |
427 | #include <stdarg.h> | |
428 | #else | |
429 | #include <varargs.h> | |
430 | #endif | |
431 | ||
432 | void | |
433 | #if __STDC__ | |
434 | panic(const char *fmt, ...) | |
435 | #else | |
436 | panic(fmt, va_alist) | |
437 | char *fmt; | |
438 | va_dcl | |
439 | #endif | |
440 | { | |
441 | va_list ap; | |
442 | #if __STDC__ | |
443 | va_start(ap, fmt); | |
444 | #else | |
445 | va_start(ap); | |
446 | #endif | |
447 | ||
448 | vfprintf(stderr, fmt, ap); | |
449 | if (yflag) | |
450 | return; | |
451 | if (reply("abort") == GOOD) { | |
452 | if (reply("dump core") == GOOD) | |
453 | abort(); | |
454 | done(1); | |
455 | } | |
456 | } |