]> git.wh0rd.org Git - dump.git/blob - restore/restore.c
Error code to restore when compare errors detected.
[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-2000
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.8 2000/05/28 16:52: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                 compare_errors = 1;
616         }
617         if (do_compare) (void) comparefile(myname(ep));
618         ep->e_flags &= ~(NEW|EXTRACT);
619 }
620
621 /*
622  * This is the routine used to compare files for the 'C' command.
623  */
624 void
625 compareleaves(void)
626 {
627         register struct entry *ep;
628         ino_t first;
629         long curvol;
630
631         first = lowerbnd(ROOTINO);
632         curvol = volno;
633         while (curfile.ino < maxino) {
634                 first = lowerbnd(first);
635                 /*
636                  * If the next available file is not the one which we
637                  * expect then we have missed one or more files. Since
638                  * we do not request files that were not on the tape,
639                  * the lost files must have been due to a tape read error,
640                  * or a file that was removed while the dump was in progress.
641                  */
642                 while (first < curfile.ino) {
643                         ep = lookupino(first);
644                         if (ep == NULL)
645                                 panic("%d: bad first\n", first);
646                         fprintf(stderr, "%s: not found on tape\n", myname(ep));
647                         compare_errors = 1;
648                         ep->e_flags &= ~(NEW|EXTRACT);
649                         first = lowerbnd(first);
650                 }
651                 /*
652                  * If we find files on the tape that have no corresponding
653                  * directory entries, then we must have found a file that
654                  * was created while the dump was in progress. Since we have 
655                  * no name for it, we discard it knowing that it will be
656                  * on the next incremental tape.
657                  */
658                 if (first != curfile.ino) {
659                         fprintf(stderr, "expected next file %ld, got %lu\n",
660                                 (long)first, (unsigned long)curfile.ino);
661                         compare_errors = 1;
662                         skipfile();
663                         goto next;
664                 }
665                 ep = lookupino(curfile.ino);
666                 if (ep == NULL) {
667                         panic("unknown file on tape\n");
668                         compare_errors = 1;
669                 }
670                 compare_entry(ep, 1);
671                 for (ep = ep->e_links; ep != NULL; ep = ep->e_links) {
672                         compare_entry(ep, 0);
673                 }
674
675                 /*
676                  * We checkpoint the restore after every tape reel, so
677                  * as to simplify the amount of work re quired by the
678                  * 'R' command.
679                  */
680         next:
681                 if (curvol != volno) {
682                         skipmaps();
683                         curvol = volno;
684                 }
685         }
686 }
687
688 /*
689  * This is the routine used to extract files for the 'r' command.
690  * Extract new leaves.
691  */
692 void
693 createleaves(char *symtabfile)
694 {
695         register struct entry *ep;
696         ino_t first;
697         long curvol;
698
699         if (command == 'R') {
700                 Vprintf(stdout, "Continue extraction of new leaves\n");
701         } else {
702                 Vprintf(stdout, "Extract new leaves.\n");
703                 dumpsymtable(symtabfile, volno);
704         }
705         first = lowerbnd(ROOTINO);
706         curvol = volno;
707         while (curfile.ino < maxino) {
708                 first = lowerbnd(first);
709                 /*
710                  * If the next available file is not the one which we
711                  * expect then we have missed one or more files. Since
712                  * we do not request files that were not on the tape,
713                  * the lost files must have been due to a tape read error,
714                  * or a file that was removed while the dump was in progress.
715                  */
716                 while (first < curfile.ino) {
717                         ep = lookupino(first);
718                         if (ep == NULL)
719                                 panic("%d: bad first\n", first);
720                         fprintf(stderr, "%s: not found on tape\n", myname(ep));
721                         ep->e_flags &= ~(NEW|EXTRACT);
722                         first = lowerbnd(first);
723                 }
724                 /*
725                  * If we find files on the tape that have no corresponding
726                  * directory entries, then we must have found a file that
727                  * was created while the dump was in progress. Since we have
728                  * no name for it, we discard it knowing that it will be
729                  * on the next incremental tape.
730                  */
731                 if (first != curfile.ino) {
732                         fprintf(stderr, "expected next file %ld, got %lu\n",
733                                 (long)first, (unsigned long)curfile.ino);
734                         skipfile();
735                         goto next;
736                 }
737                 ep = lookupino(curfile.ino);
738                 if (ep == NULL)
739                         panic("unknown file on tape\n");
740                 if ((ep->e_flags & (NEW|EXTRACT)) == 0)
741                         badentry(ep, "unexpected file on tape");
742                 /*
743                  * If the file is to be extracted, then the old file must
744                  * be removed since its type may change from one leaf type
745                  * to another (e.g. "file" to "character special").
746                  */
747                 if ((ep->e_flags & EXTRACT) != 0) {
748                         removeleaf(ep);
749                         ep->e_flags &= ~REMOVED;
750                 }
751                 (void) extractfile(myname(ep));
752                 ep->e_flags &= ~(NEW|EXTRACT);
753                 /*
754                  * We checkpoint the restore after every tape reel, so
755                  * as to simplify the amount of work required by the
756                  * 'R' command.
757                  */
758         next:
759                 if (curvol != volno) {
760                         dumpsymtable(symtabfile, volno);
761                         skipmaps();
762                         curvol = volno;
763                 }
764         }
765 }
766
767 /*
768  * This is the routine used to extract files for the 'x' and 'i' commands.
769  * Efficiently extract a subset of the files on a tape.
770  */
771 void
772 createfiles(void)
773 {
774         register ino_t first, next, last;
775         register struct entry *ep;
776         long curvol;
777
778         Vprintf(stdout, "Extract requested files\n");
779         curfile.action = SKIP;
780         getvol((long)1);
781         skipmaps();
782         skipdirs();
783         first = lowerbnd(ROOTINO);
784         last = upperbnd(maxino - 1);
785         for (;;) {
786                 first = lowerbnd(first);
787                 last = upperbnd(last);
788                 /*
789                  * Check to see if any files remain to be extracted
790                  */
791                 if (first > last)
792                         return;
793                 /*
794                  * Reject any volumes with inodes greater
795                  * than the last one needed
796                  */
797                 while (curfile.ino > last) {
798                         curfile.action = SKIP;
799                         getvol((long)0);
800                         skipmaps();
801                         skipdirs();
802                 }
803                 /*
804                  * Decide on the next inode needed.
805                  * Skip across the inodes until it is found
806                  * or an out of order volume change is encountered
807                  */
808                 next = lowerbnd(curfile.ino);
809                 do      {
810                         curvol = volno;
811                         while (next > curfile.ino && volno == curvol)
812                                 skipfile();
813                         skipmaps();
814                         skipdirs();
815                 } while (volno == curvol + 1);
816                 /*
817                  * If volume change out of order occurred the
818                  * current state must be recalculated
819                  */
820                 if (volno != curvol)
821                         continue;
822                 /*
823                  * If the current inode is greater than the one we were
824                  * looking for then we missed the one we were looking for.
825                  * Since we only attempt to extract files listed in the
826                  * dump map, the lost files must have been due to a tape
827                  * read error, or a file that was removed while the dump
828                  * was in progress. Thus we report all requested files
829                  * between the one we were looking for, and the one we
830                  * found as missing, and delete their request flags.
831                  */
832                 while (next < curfile.ino) {
833                         ep = lookupino(next);
834                         if (ep == NULL)
835                                 panic("corrupted symbol table\n");
836                         fprintf(stderr, "%s: not found on tape\n", myname(ep));
837                         ep->e_flags &= ~NEW;
838                         next = lowerbnd(next);
839                 }
840                 /*
841                  * The current inode is the one that we are looking for,
842                  * so extract it per its requested name.
843                  */
844                 if (next == curfile.ino && next <= last) {
845                         ep = lookupino(next);
846                         if (ep == NULL)
847                                 panic("corrupted symbol table\n");
848                         (void) extractfile(myname(ep));
849                         ep->e_flags &= ~NEW;
850                         if (volno != curvol)
851                                 skipmaps();
852                 }
853         }
854 }
855
856 /*
857  * Add links.
858  */
859 void
860 createlinks(void)
861 {
862         register struct entry *np, *ep;
863         register ino_t i;
864         char name[BUFSIZ];
865
866         if ((ep = lookupino(WINO))) {
867                 Vprintf(stdout, "Add whiteouts\n");
868                 for ( ; ep != NULL; ep = ep->e_links) {
869                         if ((ep->e_flags & NEW) == 0)
870                                 continue;
871 #ifdef  __linux__
872                         (void)fprintf(stderr, "BUG! Should call addwhiteout\n");
873 #else
874                         (void) addwhiteout(myname(ep));
875 #endif
876                         ep->e_flags &= ~NEW;
877                 }
878         }
879         Vprintf(stdout, "Add links\n");
880         for (i = ROOTINO; i < maxino; i++) {
881                 ep = lookupino(i);
882                 if (ep == NULL)
883                         continue;
884                 for (np = ep->e_links; np != NULL; np = np->e_links) {
885                         if ((np->e_flags & NEW) == 0)
886                                 continue;
887                         (void) strcpy(name, myname(ep));
888                         if (ep->e_type == NODE) {
889                                 (void) linkit(name, myname(np), SYMLINK);
890                         } else {
891                                 (void) linkit(name, myname(np), HARDLINK);
892                         }
893                         np->e_flags &= ~NEW;
894                 }
895         }
896 }
897
898 /*
899  * Check the symbol table.
900  * We do this to insure that all the requested work was done, and
901  * that no temporary names remain.
902  */
903 void
904 checkrestore(void)
905 {
906         register struct entry *ep;
907         register ino_t i;
908
909         Vprintf(stdout, "Check the symbol table.\n");
910         for (i = WINO; i < maxino; i++) {
911                 for (ep = lookupino(i); ep != NULL; ep = ep->e_links) {
912                         ep->e_flags &= ~KEEP;
913                         if (ep->e_type == NODE)
914                                 ep->e_flags &= ~(NEW|EXISTED);
915                         if (ep->e_flags /* != NULL */)
916                                 badentry(ep, "incomplete operations");
917                 }
918         }
919 }
920
921 /*
922  * Compare with the directory structure on the tape
923  * A paranoid check that things are as they should be.
924  */
925 long
926 verifyfile(char *name, ino_t ino, int type)
927 {
928         struct entry *np, *ep;
929         long descend = GOOD;
930
931         ep = lookupname(name);
932         if (ep == NULL) {
933                 fprintf(stderr, "Warning: missing name %s\n", name);
934                 return (FAIL);
935         }
936         np = lookupino(ino);
937         if (np != ep)
938                 descend = FAIL;
939         for ( ; np != NULL; np = np->e_links)
940                 if (np == ep)
941                         break;
942         if (np == NULL)
943                 panic("missing inumber %d\n", ino);
944         if (ep->e_type == LEAF && type != LEAF)
945                 badentry(ep, "type should be LEAF");
946         return (descend);
947 }