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