]> git.wh0rd.org - dump.git/blob - restore/restore.c
Added version in usage text.
[dump.git] / restore / restore.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@cybercable.fr>, 1999
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 const char rcsid[] =
43 "$Id: restore.c,v 1.6 1999/10/13 09:57:21 stelian Exp $";
44 #endif /* not lint */
45
46 #include <sys/types.h>
47
48 #ifdef __linux__
49 #include <sys/param.h>
50 #include <sys/time.h>
51 #include <linux/ext2_fs.h>
52 #include <bsdcompat.h>
53 #else /* __linux__ */
54 #include <ufs/ufs/dinode.h>
55 #endif /* __linux__ */
56
57 #include <stdio.h>
58 #include <string.h>
59
60 #ifdef __linux__
61 #include <ext2fs/ext2fs.h>
62 #endif
63
64 #include "restore.h"
65 #include "extern.h"
66
67 static char *keyval __P((int));
68
69 /*
70 * This implements the 't' option.
71 * List entries on the tape.
72 */
73 long
74 listfile(char *name, ino_t ino, int type)
75 {
76 long descend = hflag ? GOOD : FAIL;
77
78 if (TSTINO(ino, dumpmap) == 0)
79 return (descend);
80 Vprintf(stdout, "%s", type == LEAF ? "leaf" : "dir ");
81 fprintf(stdout, "%10lu\t%s\n", (unsigned long)ino, name);
82 return (descend);
83 }
84
85 /*
86 * This implements the 'x' option.
87 * Request that new entries be extracted.
88 */
89 long
90 addfile(char *name, ino_t ino, int type)
91 {
92 register struct entry *ep;
93 long descend = hflag ? GOOD : FAIL;
94 char buf[100];
95
96 if (TSTINO(ino, dumpmap) == 0) {
97 Dprintf(stdout, "%s: not on the tape\n", name);
98 return (descend);
99 }
100 if (ino == WINO && command == 'i' && !vflag)
101 return (descend);
102 if (!mflag) {
103 (void) snprintf(buf, sizeof(buf), "./%lu", (unsigned long)ino);
104 name = buf;
105 if (type == NODE) {
106 (void) genliteraldir(name, ino);
107 return (descend);
108 }
109 }
110 ep = lookupino(ino);
111 if (ep != NULL) {
112 if (strcmp(name, myname(ep)) == 0) {
113 ep->e_flags |= NEW;
114 return (descend);
115 }
116 type |= LINK;
117 }
118 ep = addentry(name, ino, type);
119 if (type == NODE)
120 newnode(ep);
121 ep->e_flags |= NEW;
122 return (descend);
123 }
124
125 /*
126 * This is used by the 'i' option to undo previous requests made by addfile.
127 * Delete entries from the request queue.
128 */
129 /* ARGSUSED */
130 long
131 deletefile(char *name, ino_t ino, int type)
132 {
133 long descend = hflag ? GOOD : FAIL;
134 struct entry *ep;
135
136 if (TSTINO(ino, dumpmap) == 0)
137 return (descend);
138 ep = lookupname(name);
139 if (ep != NULL) {
140 ep->e_flags &= ~NEW;
141 ep->e_flags |= REMOVED;
142 if (ep->e_type != NODE)
143 freeentry(ep);
144 }
145 return (descend);
146 }
147
148 /*
149 * The following four routines implement the incremental
150 * restore algorithm. The first removes old entries, the second
151 * does renames and calculates the extraction list, the third
152 * cleans up link names missed by the first two, and the final
153 * one deletes old directories.
154 *
155 * Directories cannot be immediately deleted, as they may have
156 * other files in them which need to be moved out first. As
157 * directories to be deleted are found, they are put on the
158 * following deletion list. After all deletions and renames
159 * are done, this list is actually deleted.
160 */
161 static struct entry *removelist;
162
163 /*
164 * Remove invalid whiteouts from the old tree.
165 * Remove unneeded leaves from the old tree.
166 * Remove directories from the lookup chains.
167 */
168 void
169 removeoldleaves(void)
170 {
171 register struct entry *ep, *nextep;
172 register ino_t i, mydirino;
173
174 Vprintf(stdout, "Mark entries to be removed.\n");
175 if ((ep = lookupino(WINO))) {
176 Vprintf(stdout, "Delete whiteouts\n");
177 for ( ; ep != NULL; ep = nextep) {
178 nextep = ep->e_links;
179 mydirino = ep->e_parent->e_ino;
180 /*
181 * We remove all whiteouts that are in directories
182 * that have been removed or that have been dumped.
183 */
184 if (TSTINO(mydirino, usedinomap) &&
185 !TSTINO(mydirino, dumpmap))
186 continue;
187 #ifdef __linux__
188 (void)fprintf(stderr, "BUG! Should call delwhiteout\n");
189 #else
190 delwhiteout(ep);
191 #endif
192 freeentry(ep);
193 }
194 }
195 for (i = ROOTINO + 1; i < maxino; i++) {
196 ep = lookupino(i);
197 if (ep == NULL)
198 continue;
199 if (TSTINO(i, usedinomap))
200 continue;
201 for ( ; ep != NULL; ep = ep->e_links) {
202 Dprintf(stdout, "%s: REMOVE\n", myname(ep));
203 if (ep->e_type == LEAF) {
204 removeleaf(ep);
205 freeentry(ep);
206 } else {
207 mktempname(ep);
208 deleteino(ep->e_ino);
209 ep->e_next = removelist;
210 removelist = ep;
211 }
212 }
213 }
214 }
215
216 /*
217 * For each directory entry on the incremental tape, determine which
218 * category it falls into as follows:
219 * KEEP - entries that are to be left alone.
220 * NEW - new entries to be added.
221 * EXTRACT - files that must be updated with new contents.
222 * LINK - new links to be added.
223 * Renames are done at the same time.
224 */
225 long
226 nodeupdates(char *name, ino_t ino, int type)
227 {
228 register struct entry *ep, *np, *ip;
229 long descend = GOOD;
230 int lookuptype = 0;
231 int key = 0;
232 /* key values */
233 # define ONTAPE 0x1 /* inode is on the tape */
234 # define INOFND 0x2 /* inode already exists */
235 # define NAMEFND 0x4 /* name already exists */
236 # define MODECHG 0x8 /* mode of inode changed */
237
238 /*
239 * This routine is called once for each element in the
240 * directory hierarchy, with a full path name.
241 * The "type" value is incorrectly specified as LEAF for
242 * directories that are not on the dump tape.
243 *
244 * Check to see if the file is on the tape.
245 */
246 if (TSTINO(ino, dumpmap))
247 key |= ONTAPE;
248 /*
249 * Check to see if the name exists, and if the name is a link.
250 */
251 np = lookupname(name);
252 if (np != NULL) {
253 key |= NAMEFND;
254 ip = lookupino(np->e_ino);
255 if (ip == NULL)
256 panic("corrupted symbol table\n");
257 if (ip != np)
258 lookuptype = LINK;
259 }
260 /*
261 * Check to see if the inode exists, and if one of its links
262 * corresponds to the name (if one was found).
263 */
264 ip = lookupino(ino);
265 if (ip != NULL) {
266 key |= INOFND;
267 for (ep = ip->e_links; ep != NULL; ep = ep->e_links) {
268 if (ep == np) {
269 ip = ep;
270 break;
271 }
272 }
273 }
274 /*
275 * If both a name and an inode are found, but they do not
276 * correspond to the same file, then both the inode that has
277 * been found and the inode corresponding to the name that
278 * has been found need to be renamed. The current pathname
279 * is the new name for the inode that has been found. Since
280 * all files to be deleted have already been removed, the
281 * named file is either a now unneeded link, or it must live
282 * under a new name in this dump level. If it is a link, it
283 * can be removed. If it is not a link, it is given a
284 * temporary name in anticipation that it will be renamed
285 * when it is later found by inode number.
286 */
287 if (((key & (INOFND|NAMEFND)) == (INOFND|NAMEFND)) && ip != np) {
288 if (lookuptype == LINK) {
289 removeleaf(np);
290 freeentry(np);
291 } else {
292 Dprintf(stdout, "name/inode conflict, mktempname %s\n",
293 myname(np));
294 mktempname(np);
295 }
296 np = NULL;
297 key &= ~NAMEFND;
298 }
299 if ((key & ONTAPE) &&
300 (((key & INOFND) && ip->e_type != type) ||
301 ((key & NAMEFND) && np->e_type != type)))
302 key |= MODECHG;
303
304 /*
305 * Decide on the disposition of the file based on its flags.
306 * Note that we have already handled the case in which
307 * a name and inode are found that correspond to different files.
308 * Thus if both NAMEFND and INOFND are set then ip == np.
309 */
310 switch (key) {
311
312 /*
313 * A previously existing file has been found.
314 * Mark it as KEEP so that other links to the inode can be
315 * detected, and so that it will not be reclaimed by the search
316 * for unreferenced names.
317 */
318 case INOFND|NAMEFND:
319 ip->e_flags |= KEEP;
320 Dprintf(stdout, "[%s] %s: %s\n", keyval(key), name,
321 flagvalues(ip));
322 break;
323
324 /*
325 * A file on the tape has a name which is the same as a name
326 * corresponding to a different file in the previous dump.
327 * Since all files to be deleted have already been removed,
328 * this file is either a now unneeded link, or it must live
329 * under a new name in this dump level. If it is a link, it
330 * can simply be removed. If it is not a link, it is given a
331 * temporary name in anticipation that it will be renamed
332 * when it is later found by inode number (see INOFND case
333 * below). The entry is then treated as a new file.
334 */
335 case ONTAPE|NAMEFND:
336 case ONTAPE|NAMEFND|MODECHG:
337 if (lookuptype == LINK) {
338 removeleaf(np);
339 freeentry(np);
340 } else {
341 mktempname(np);
342 }
343 /* fall through */
344
345 /*
346 * A previously non-existent file.
347 * Add it to the file system, and request its extraction.
348 * If it is a directory, create it immediately.
349 * (Since the name is unused there can be no conflict)
350 */
351 case ONTAPE:
352 ep = addentry(name, ino, type);
353 if (type == NODE)
354 newnode(ep);
355 ep->e_flags |= NEW|KEEP;
356 Dprintf(stdout, "[%s] %s: %s\n", keyval(key), name,
357 flagvalues(ep));
358 break;
359
360 /*
361 * A file with the same inode number, but a different
362 * name has been found. If the other name has not already
363 * been found (indicated by the KEEP flag, see above) then
364 * this must be a new name for the file, and it is renamed.
365 * If the other name has been found then this must be a
366 * link to the file. Hard links to directories are not
367 * permitted, and are either deleted or converted to
368 * symbolic links. Finally, if the file is on the tape,
369 * a request is made to extract it.
370 */
371 case ONTAPE|INOFND:
372 if (type == LEAF && (ip->e_flags & KEEP) == 0)
373 ip->e_flags |= EXTRACT;
374 /* fall through */
375 case INOFND:
376 if ((ip->e_flags & KEEP) == 0) {
377 renameit(myname(ip), name);
378 moveentry(ip, name);
379 ip->e_flags |= KEEP;
380 Dprintf(stdout, "[%s] %s: %s\n", keyval(key), name,
381 flagvalues(ip));
382 break;
383 }
384 if (ip->e_type == NODE) {
385 descend = FAIL;
386 fprintf(stderr,
387 "deleted hard link %s to directory %s\n",
388 name, myname(ip));
389 break;
390 }
391 ep = addentry(name, ino, type|LINK);
392 ep->e_flags |= NEW;
393 Dprintf(stdout, "[%s] %s: %s|LINK\n", keyval(key), name,
394 flagvalues(ep));
395 break;
396
397 /*
398 * A previously known file which is to be updated. If it is a link,
399 * then all names referring to the previous file must be removed
400 * so that the subset of them that remain can be recreated.
401 */
402 case ONTAPE|INOFND|NAMEFND:
403 if (lookuptype == LINK) {
404 removeleaf(np);
405 freeentry(np);
406 ep = addentry(name, ino, type|LINK);
407 if (type == NODE)
408 newnode(ep);
409 ep->e_flags |= NEW|KEEP;
410 Dprintf(stdout, "[%s] %s: %s|LINK\n", keyval(key), name,
411 flagvalues(ep));
412 break;
413 }
414 if (type == LEAF && lookuptype != LINK)
415 np->e_flags |= EXTRACT;
416 np->e_flags |= KEEP;
417 Dprintf(stdout, "[%s] %s: %s\n", keyval(key), name,
418 flagvalues(np));
419 break;
420
421 /*
422 * An inode is being reused in a completely different way.
423 * Normally an extract can simply do an "unlink" followed
424 * by a "creat". Here we must do effectively the same
425 * thing. The complications arise because we cannot really
426 * delete a directory since it may still contain files
427 * that we need to rename, so we delete it from the symbol
428 * table, and put it on the list to be deleted eventually.
429 * Conversely if a directory is to be created, it must be
430 * done immediately, rather than waiting until the
431 * extraction phase.
432 */
433 case ONTAPE|INOFND|MODECHG:
434 case ONTAPE|INOFND|NAMEFND|MODECHG:
435 if (ip->e_flags & KEEP) {
436 badentry(ip, "cannot KEEP and change modes");
437 break;
438 }
439 if (ip->e_type == LEAF) {
440 /* changing from leaf to node */
441 for ( ; ip != NULL; ip = ip->e_links) {
442 if (ip->e_type != LEAF)
443 badentry(ip, "NODE and LEAF links to same inode");
444 removeleaf(ip);
445 freeentry(ip);
446 }
447 ip = addentry(name, ino, type);
448 newnode(ip);
449 } else {
450 /* changing from node to leaf */
451 if ((ip->e_flags & TMPNAME) == 0)
452 mktempname(ip);
453 deleteino(ip->e_ino);
454 ip->e_next = removelist;
455 removelist = ip;
456 ip = addentry(name, ino, type);
457 }
458 ip->e_flags |= NEW|KEEP;
459 Dprintf(stdout, "[%s] %s: %s\n", keyval(key), name,
460 flagvalues(ip));
461 break;
462
463 /*
464 * A hard link to a directory that has been removed.
465 * Ignore it.
466 */
467 case NAMEFND:
468 Dprintf(stdout, "[%s] %s: Extraneous name\n", keyval(key),
469 name);
470 descend = FAIL;
471 break;
472
473 /*
474 * If we find a directory entry for a file that is not on
475 * the tape, then we must have found a file that was created
476 * while the dump was in progress. Since we have no contents
477 * for it, we discard the name knowing that it will be on the
478 * next incremental tape.
479 */
480 case NULL:
481 if (compare_ignore_not_found) break;
482 fprintf(stderr, "%s: (inode %lu) not found on tape\n",
483 name, (unsigned long)ino);
484 break;
485
486 /*
487 * If any of these arise, something is grievously wrong with
488 * the current state of the symbol table.
489 */
490 case INOFND|NAMEFND|MODECHG:
491 case NAMEFND|MODECHG:
492 case INOFND|MODECHG:
493 fprintf(stderr, "[%s] %s: inconsistent state\n", keyval(key),
494 name);
495 break;
496
497 /*
498 * These states "cannot" arise for any state of the symbol table.
499 */
500 case ONTAPE|MODECHG:
501 case MODECHG:
502 default:
503 panic("[%s] %s: impossible state\n", keyval(key), name);
504 break;
505 }
506 return (descend);
507 }
508
509 /*
510 * Calculate the active flags in a key.
511 */
512 static char *
513 keyval(int key)
514 {
515 static char keybuf[32];
516
517 (void) strcpy(keybuf, "|NIL");
518 keybuf[0] = '\0';
519 if (key & ONTAPE)
520 (void) strcat(keybuf, "|ONTAPE");
521 if (key & INOFND)
522 (void) strcat(keybuf, "|INOFND");
523 if (key & NAMEFND)
524 (void) strcat(keybuf, "|NAMEFND");
525 if (key & MODECHG)
526 (void) strcat(keybuf, "|MODECHG");
527 return (&keybuf[1]);
528 }
529
530 /*
531 * Find unreferenced link names.
532 */
533 void
534 findunreflinks(void)
535 {
536 register struct entry *ep, *np;
537 register ino_t i;
538
539 Vprintf(stdout, "Find unreferenced names.\n");
540 for (i = ROOTINO; i < maxino; i++) {
541 ep = lookupino(i);
542 if (ep == NULL || ep->e_type == LEAF || TSTINO(i, dumpmap) == 0)
543 continue;
544 for (np = ep->e_entries; np != NULL; np = np->e_sibling) {
545 if (np->e_flags == 0) {
546 Dprintf(stdout,
547 "%s: remove unreferenced name\n",
548 myname(np));
549 removeleaf(np);
550 freeentry(np);
551 }
552 }
553 }
554 /*
555 * Any leaves remaining in removed directories is unreferenced.
556 */
557 for (ep = removelist; ep != NULL; ep = ep->e_next) {
558 for (np = ep->e_entries; np != NULL; np = np->e_sibling) {
559 if (np->e_type == LEAF) {
560 if (np->e_flags != 0)
561 badentry(np, "unreferenced with flags");
562 Dprintf(stdout,
563 "%s: remove unreferenced name\n",
564 myname(np));
565 removeleaf(np);
566 freeentry(np);
567 }
568 }
569 }
570 }
571
572 /*
573 * Remove old nodes (directories).
574 * Note that this routine runs in O(N*D) where:
575 * N is the number of directory entries to be removed.
576 * D is the maximum depth of the tree.
577 * If N == D this can be quite slow. If the list were
578 * topologically sorted, the deletion could be done in
579 * time O(N).
580 */
581 void
582 removeoldnodes(void)
583 {
584 register struct entry *ep, **prev;
585 long change;
586
587 Vprintf(stdout, "Remove old nodes (directories).\n");
588 do {
589 change = 0;
590 prev = &removelist;
591 for (ep = removelist; ep != NULL; ep = *prev) {
592 if (ep->e_entries != NULL) {
593 prev = &ep->e_next;
594 continue;
595 }
596 *prev = ep->e_next;
597 removenode(ep);
598 freeentry(ep);
599 change++;
600 }
601 } while (change);
602 for (ep = removelist; ep != NULL; ep = ep->e_next)
603 badentry(ep, "cannot remove, non-empty");
604 }
605
606 /* Compare the file specified in `ep' (which is on tape) to the */
607 /* current copy of this file on disk. If do_compare is 0, then just */
608 /* make our caller think we did it--this is used to handle hard links */
609 /* to files and devices. */
610 static void
611 compare_entry(struct entry *ep, int do_compare)
612 {
613 if ((ep->e_flags & (NEW|EXTRACT)) == 0)
614 badentry(ep, "unexpected file on tape");
615 if (do_compare) (void) comparefile(myname(ep));
616 ep->e_flags &= ~(NEW|EXTRACT);
617 }
618
619 /*
620 * This is the routine used to compare files for the 'C' command.
621 */
622 void
623 compareleaves(void)
624 {
625 register struct entry *ep;
626 ino_t first;
627 long curvol;
628
629 first = lowerbnd(ROOTINO);
630 curvol = volno;
631 while (curfile.ino < maxino) {
632 first = lowerbnd(first);
633 /*
634 * If the next available file is not the one which we
635 * expect then we have missed one or more files. Since
636 * we do not request files that were not on the tape,
637 * the lost files must have been due to a tape read error,
638 * or a file that was removed while the dump was in progress.
639 */
640 while (first < curfile.ino) {
641 ep = lookupino(first);
642 if (ep == NULL)
643 panic("%d: bad first\n", first);
644 fprintf(stderr, "%s: not found on tape\n", myname(ep));
645 ep->e_flags &= ~(NEW|EXTRACT);
646 first = lowerbnd(first);
647 }
648 /*
649 * If we find files on the tape that have no corresponding
650 * directory entries, then we must have found a file that
651 * was created while the dump was in progress. Since we have
652 * no name for it, we discard it knowing that it will be
653 * on the next incremental tape.
654 */
655 if (first != curfile.ino) {
656 fprintf(stderr, "expected next file %ld, got %lu\n",
657 (long)first, (unsigned long)curfile.ino);
658 skipfile();
659 goto next;
660 }
661 ep = lookupino(curfile.ino);
662 if (ep == NULL)
663 panic("unknown file on tape\n");
664 compare_entry(ep, 1);
665 for (ep = ep->e_links; ep != NULL; ep = ep->e_links) {
666 compare_entry(ep, 0);
667 }
668
669 /*
670 * We checkpoint the restore after every tape reel, so
671 * as to simplify the amount of work re quired by the
672 * 'R' command.
673 */
674 next:
675 if (curvol != volno) {
676 skipmaps();
677 curvol = volno;
678 }
679 }
680 }
681
682 /*
683 * This is the routine used to extract files for the 'r' command.
684 * Extract new leaves.
685 */
686 void
687 createleaves(char *symtabfile)
688 {
689 register struct entry *ep;
690 ino_t first;
691 long curvol;
692
693 if (command == 'R') {
694 Vprintf(stdout, "Continue extraction of new leaves\n");
695 } else {
696 Vprintf(stdout, "Extract new leaves.\n");
697 dumpsymtable(symtabfile, volno);
698 }
699 first = lowerbnd(ROOTINO);
700 curvol = volno;
701 while (curfile.ino < maxino) {
702 first = lowerbnd(first);
703 /*
704 * If the next available file is not the one which we
705 * expect then we have missed one or more files. Since
706 * we do not request files that were not on the tape,
707 * the lost files must have been due to a tape read error,
708 * or a file that was removed while the dump was in progress.
709 */
710 while (first < curfile.ino) {
711 ep = lookupino(first);
712 if (ep == NULL)
713 panic("%d: bad first\n", first);
714 fprintf(stderr, "%s: not found on tape\n", myname(ep));
715 ep->e_flags &= ~(NEW|EXTRACT);
716 first = lowerbnd(first);
717 }
718 /*
719 * If we find files on the tape that have no corresponding
720 * directory entries, then we must have found a file that
721 * was created while the dump was in progress. Since we have
722 * no name for it, we discard it knowing that it will be
723 * on the next incremental tape.
724 */
725 if (first != curfile.ino) {
726 fprintf(stderr, "expected next file %ld, got %lu\n",
727 (long)first, (unsigned long)curfile.ino);
728 skipfile();
729 goto next;
730 }
731 ep = lookupino(curfile.ino);
732 if (ep == NULL)
733 panic("unknown file on tape\n");
734 if ((ep->e_flags & (NEW|EXTRACT)) == 0)
735 badentry(ep, "unexpected file on tape");
736 /*
737 * If the file is to be extracted, then the old file must
738 * be removed since its type may change from one leaf type
739 * to another (e.g. "file" to "character special").
740 */
741 if ((ep->e_flags & EXTRACT) != 0) {
742 removeleaf(ep);
743 ep->e_flags &= ~REMOVED;
744 }
745 (void) extractfile(myname(ep));
746 ep->e_flags &= ~(NEW|EXTRACT);
747 /*
748 * We checkpoint the restore after every tape reel, so
749 * as to simplify the amount of work required by the
750 * 'R' command.
751 */
752 next:
753 if (curvol != volno) {
754 dumpsymtable(symtabfile, volno);
755 skipmaps();
756 curvol = volno;
757 }
758 }
759 }
760
761 /*
762 * This is the routine used to extract files for the 'x' and 'i' commands.
763 * Efficiently extract a subset of the files on a tape.
764 */
765 void
766 createfiles(void)
767 {
768 register ino_t first, next, last;
769 register struct entry *ep;
770 long curvol;
771
772 Vprintf(stdout, "Extract requested files\n");
773 curfile.action = SKIP;
774 getvol((long)1);
775 skipmaps();
776 skipdirs();
777 first = lowerbnd(ROOTINO);
778 last = upperbnd(maxino - 1);
779 for (;;) {
780 first = lowerbnd(first);
781 last = upperbnd(last);
782 /*
783 * Check to see if any files remain to be extracted
784 */
785 if (first > last)
786 return;
787 /*
788 * Reject any volumes with inodes greater
789 * than the last one needed
790 */
791 while (curfile.ino > last) {
792 curfile.action = SKIP;
793 getvol((long)0);
794 skipmaps();
795 skipdirs();
796 }
797 /*
798 * Decide on the next inode needed.
799 * Skip across the inodes until it is found
800 * or an out of order volume change is encountered
801 */
802 next = lowerbnd(curfile.ino);
803 do {
804 curvol = volno;
805 while (next > curfile.ino && volno == curvol)
806 skipfile();
807 skipmaps();
808 skipdirs();
809 } while (volno == curvol + 1);
810 /*
811 * If volume change out of order occurred the
812 * current state must be recalculated
813 */
814 if (volno != curvol)
815 continue;
816 /*
817 * If the current inode is greater than the one we were
818 * looking for then we missed the one we were looking for.
819 * Since we only attempt to extract files listed in the
820 * dump map, the lost files must have been due to a tape
821 * read error, or a file that was removed while the dump
822 * was in progress. Thus we report all requested files
823 * between the one we were looking for, and the one we
824 * found as missing, and delete their request flags.
825 */
826 while (next < curfile.ino) {
827 ep = lookupino(next);
828 if (ep == NULL)
829 panic("corrupted symbol table\n");
830 fprintf(stderr, "%s: not found on tape\n", myname(ep));
831 ep->e_flags &= ~NEW;
832 next = lowerbnd(next);
833 }
834 /*
835 * The current inode is the one that we are looking for,
836 * so extract it per its requested name.
837 */
838 if (next == curfile.ino && next <= last) {
839 ep = lookupino(next);
840 if (ep == NULL)
841 panic("corrupted symbol table\n");
842 (void) extractfile(myname(ep));
843 ep->e_flags &= ~NEW;
844 if (volno != curvol)
845 skipmaps();
846 }
847 }
848 }
849
850 /*
851 * Add links.
852 */
853 void
854 createlinks(void)
855 {
856 register struct entry *np, *ep;
857 register ino_t i;
858 char name[BUFSIZ];
859
860 if ((ep = lookupino(WINO))) {
861 Vprintf(stdout, "Add whiteouts\n");
862 for ( ; ep != NULL; ep = ep->e_links) {
863 if ((ep->e_flags & NEW) == 0)
864 continue;
865 #ifdef __linux__
866 (void)fprintf(stderr, "BUG! Should call addwhiteout\n");
867 #else
868 (void) addwhiteout(myname(ep));
869 #endif
870 ep->e_flags &= ~NEW;
871 }
872 }
873 Vprintf(stdout, "Add links\n");
874 for (i = ROOTINO; i < maxino; i++) {
875 ep = lookupino(i);
876 if (ep == NULL)
877 continue;
878 for (np = ep->e_links; np != NULL; np = np->e_links) {
879 if ((np->e_flags & NEW) == 0)
880 continue;
881 (void) strcpy(name, myname(ep));
882 if (ep->e_type == NODE) {
883 (void) linkit(name, myname(np), SYMLINK);
884 } else {
885 (void) linkit(name, myname(np), HARDLINK);
886 }
887 np->e_flags &= ~NEW;
888 }
889 }
890 }
891
892 /*
893 * Check the symbol table.
894 * We do this to insure that all the requested work was done, and
895 * that no temporary names remain.
896 */
897 void
898 checkrestore(void)
899 {
900 register struct entry *ep;
901 register ino_t i;
902
903 Vprintf(stdout, "Check the symbol table.\n");
904 for (i = WINO; i < maxino; i++) {
905 for (ep = lookupino(i); ep != NULL; ep = ep->e_links) {
906 ep->e_flags &= ~KEEP;
907 if (ep->e_type == NODE)
908 ep->e_flags &= ~(NEW|EXISTED);
909 if (ep->e_flags /* != NULL */)
910 badentry(ep, "incomplete operations");
911 }
912 }
913 }
914
915 /*
916 * Compare with the directory structure on the tape
917 * A paranoid check that things are as they should be.
918 */
919 long
920 verifyfile(char *name, ino_t ino, int type)
921 {
922 struct entry *np, *ep;
923 long descend = GOOD;
924
925 ep = lookupname(name);
926 if (ep == NULL) {
927 fprintf(stderr, "Warning: missing name %s\n", name);
928 return (FAIL);
929 }
930 np = lookupino(ino);
931 if (np != ep)
932 descend = FAIL;
933 for ( ; np != NULL; np = np->e_links)
934 if (np == ep)
935 break;
936 if (np == NULL)
937 panic("missing inumber %d\n", ino);
938 if (ep->e_type == LEAF && type != LEAF)
939 badentry(ep, "type should be LEAF");
940 return (descend);
941 }