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