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