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