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