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