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