xref: /onnv-gate/usr/src/cmd/backup/restore/restore.c (revision 2753:2c3b0f170201)
10Sstevel@tonic-gate /*
2*2753Smishra  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
30Sstevel@tonic-gate  * Use is subject to license terms.
40Sstevel@tonic-gate  */
50Sstevel@tonic-gate 
60Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
70Sstevel@tonic-gate /*	  All Rights Reserved	*/
80Sstevel@tonic-gate 
90Sstevel@tonic-gate /*
100Sstevel@tonic-gate  * Copyright (c) 1983 Regents of the University of California.
110Sstevel@tonic-gate  * All rights reserved.  The Berkeley software License Agreement
120Sstevel@tonic-gate  * specifies the terms and conditions for redistribution.
130Sstevel@tonic-gate  */
140Sstevel@tonic-gate 
150Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
160Sstevel@tonic-gate 
170Sstevel@tonic-gate #include "restore.h"
180Sstevel@tonic-gate /* undef MAXNAMLEN to prevent compiler warnings about redef in dirent.h */
190Sstevel@tonic-gate #undef MAXNAMLEN
200Sstevel@tonic-gate #include <dirent.h>
210Sstevel@tonic-gate 
220Sstevel@tonic-gate #ifdef __STDC__
230Sstevel@tonic-gate static char *keyval(int);
240Sstevel@tonic-gate static void removexattrs(struct entry *);
250Sstevel@tonic-gate static void movexattrs(char *, char *);
260Sstevel@tonic-gate #else
270Sstevel@tonic-gate static char *keyval();
280Sstevel@tonic-gate static void removexattrs();
290Sstevel@tonic-gate static void movexattrs();
300Sstevel@tonic-gate #endif
310Sstevel@tonic-gate 
320Sstevel@tonic-gate /*
330Sstevel@tonic-gate  * This implements the 't' option.
340Sstevel@tonic-gate  * List entries on the tape.
350Sstevel@tonic-gate  */
360Sstevel@tonic-gate long
listfile(name,ino,type)370Sstevel@tonic-gate listfile(name, ino, type)
380Sstevel@tonic-gate 	char *name;
390Sstevel@tonic-gate 	ino_t ino;
400Sstevel@tonic-gate 	int type;
410Sstevel@tonic-gate {
420Sstevel@tonic-gate 	long descend = hflag ? GOOD : FAIL;
430Sstevel@tonic-gate 
440Sstevel@tonic-gate 	if (BIT(ino, dumpmap) == 0) {
450Sstevel@tonic-gate 		return (descend);
460Sstevel@tonic-gate 	}
470Sstevel@tonic-gate 	vprintf(stdout, "%s", type == LEAF ? gettext("leaf") : gettext("dir "));
480Sstevel@tonic-gate 	(void) fprintf(stdout, "%10lu\t%s\n", ino, name);
490Sstevel@tonic-gate 	return (descend);
500Sstevel@tonic-gate }
510Sstevel@tonic-gate 
520Sstevel@tonic-gate /*
530Sstevel@tonic-gate  * This implements the 'x' option.
540Sstevel@tonic-gate  * Request that new entries be extracted.
550Sstevel@tonic-gate  */
560Sstevel@tonic-gate long
addfile(name,ino,type)570Sstevel@tonic-gate addfile(name, ino, type)
580Sstevel@tonic-gate 	char *name;
590Sstevel@tonic-gate 	ino_t ino;
600Sstevel@tonic-gate 	int type;
610Sstevel@tonic-gate {
620Sstevel@tonic-gate 	struct entry *ep;
630Sstevel@tonic-gate 	long descend = hflag ? GOOD : FAIL;
640Sstevel@tonic-gate 	char buf[100];
650Sstevel@tonic-gate 
660Sstevel@tonic-gate 	/* Don't know if ino_t is long or long long, so be safe w/ *printf() */
670Sstevel@tonic-gate 
680Sstevel@tonic-gate 	if (BIT(ino, dumpmap) == 0) {
690Sstevel@tonic-gate 		if (mflag) {
700Sstevel@tonic-gate 			dprintf(stdout, gettext(
710Sstevel@tonic-gate 			    "%s: not on the volume\n"), name);
720Sstevel@tonic-gate 		} else {
730Sstevel@tonic-gate 			dprintf(stdout, gettext(
740Sstevel@tonic-gate 			    "inode %llu: not on the volume\n"),
750Sstevel@tonic-gate 			    (u_longlong_t)ino);
760Sstevel@tonic-gate 		}
770Sstevel@tonic-gate 		return (descend);
780Sstevel@tonic-gate 	}
790Sstevel@tonic-gate 	if (!mflag) {
800Sstevel@tonic-gate 		(void) snprintf(buf, sizeof (buf), "./%llu", (u_longlong_t)ino);
810Sstevel@tonic-gate 		buf[sizeof (buf) - 1] = '\0';
820Sstevel@tonic-gate 		name = buf;
830Sstevel@tonic-gate 		if (type == NODE) {
840Sstevel@tonic-gate 			(void) genliteraldir(name, ino);
850Sstevel@tonic-gate 			return (descend);
860Sstevel@tonic-gate 		}
870Sstevel@tonic-gate 	}
880Sstevel@tonic-gate 	ep = lookupino(ino);
890Sstevel@tonic-gate 	if (ep != NIL) {
900Sstevel@tonic-gate 		if (strcmp(name, myname(ep)) == 0) {
910Sstevel@tonic-gate 			/* LINTED: result fits into a short */
920Sstevel@tonic-gate 			ep->e_flags |= NEW;
930Sstevel@tonic-gate 			return (descend);
940Sstevel@tonic-gate 		}
950Sstevel@tonic-gate 		type |= LINK;
960Sstevel@tonic-gate 	}
970Sstevel@tonic-gate 	ep = addentry(name, ino, type);
980Sstevel@tonic-gate 	if (type == NODE)
990Sstevel@tonic-gate 		newnode(ep);
1000Sstevel@tonic-gate 	/* LINTED: result fits into a short */
1010Sstevel@tonic-gate 	ep->e_flags |= NEW;
1020Sstevel@tonic-gate 	return (descend);
1030Sstevel@tonic-gate }
1040Sstevel@tonic-gate 
1050Sstevel@tonic-gate /*
1060Sstevel@tonic-gate  * This is used by the 'i' option to undo previous requests made by addfile.
1070Sstevel@tonic-gate  * Delete entries from the request queue.
1080Sstevel@tonic-gate  */
1090Sstevel@tonic-gate /* ARGSUSED */
1100Sstevel@tonic-gate long
deletefile(name,ino,type)1110Sstevel@tonic-gate deletefile(name, ino, type)
1120Sstevel@tonic-gate 	char *name;
1130Sstevel@tonic-gate 	ino_t ino;
1140Sstevel@tonic-gate 	int type;
1150Sstevel@tonic-gate {
1160Sstevel@tonic-gate 	long descend = hflag ? GOOD : FAIL;
1170Sstevel@tonic-gate 	struct entry *ep;
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate 	if (BIT(ino, dumpmap) == 0) {
1200Sstevel@tonic-gate 		return (descend);
1210Sstevel@tonic-gate 	}
1220Sstevel@tonic-gate 	ep = lookupino(ino);
1230Sstevel@tonic-gate 	if (ep != NIL) {
1240Sstevel@tonic-gate 		/* LINTED: result fits into a short */
1250Sstevel@tonic-gate 		ep->e_flags &= ~NEW;
1260Sstevel@tonic-gate 	}
1270Sstevel@tonic-gate 	return (descend);
1280Sstevel@tonic-gate }
1290Sstevel@tonic-gate 
1300Sstevel@tonic-gate /*
1310Sstevel@tonic-gate  * The following four routines implement the incremental
1320Sstevel@tonic-gate  * restore algorithm. The first removes old entries, the second
1330Sstevel@tonic-gate  * does renames and calculates the extraction list, the third
1340Sstevel@tonic-gate  * cleans up link names missed by the first two, and the final
1350Sstevel@tonic-gate  * one deletes old directories.
1360Sstevel@tonic-gate  *
1370Sstevel@tonic-gate  * Directories cannot be immediately deleted, as they may have
1380Sstevel@tonic-gate  * other files in them which need to be moved out first. As
1390Sstevel@tonic-gate  * directories to be deleted are found, they are put on the
1400Sstevel@tonic-gate  * following deletion list. After all deletions and renames
1410Sstevel@tonic-gate  * are done, this list is actually deleted.
1420Sstevel@tonic-gate  */
1430Sstevel@tonic-gate static struct entry *removelist;
1440Sstevel@tonic-gate 
1450Sstevel@tonic-gate /*
1460Sstevel@tonic-gate  *	Remove unneeded leaves from the old tree.
1470Sstevel@tonic-gate  *	Remove directories from the lookup chains.
1480Sstevel@tonic-gate  */
1490Sstevel@tonic-gate void
1500Sstevel@tonic-gate #ifdef __STDC__
removeoldleaves(void)1510Sstevel@tonic-gate removeoldleaves(void)
1520Sstevel@tonic-gate #else
1530Sstevel@tonic-gate removeoldleaves()
1540Sstevel@tonic-gate #endif
1550Sstevel@tonic-gate {
1560Sstevel@tonic-gate 	struct entry *ep;
1570Sstevel@tonic-gate 	ino_t i;
1580Sstevel@tonic-gate 
1590Sstevel@tonic-gate 	vprintf(stdout, gettext("Mark entries to be removed.\n"));
1600Sstevel@tonic-gate 	for (i = ROOTINO + 1; i < maxino; i++) {
1610Sstevel@tonic-gate 		if (BIT(i, clrimap))
1620Sstevel@tonic-gate 			continue;
1630Sstevel@tonic-gate 		ep = lookupino(i);
1640Sstevel@tonic-gate 		if (ep == NIL)
1650Sstevel@tonic-gate 			continue;
1660Sstevel@tonic-gate 		while (ep != NIL) {
1670Sstevel@tonic-gate 			dprintf(stdout, gettext("%s: REMOVE\n"), myname(ep));
1680Sstevel@tonic-gate 			removexattrs(ep->e_xattrs);
1690Sstevel@tonic-gate 			if (ep->e_type == LEAF) {
1700Sstevel@tonic-gate 				removeleaf(ep);
1710Sstevel@tonic-gate 				freeentry(ep);
1720Sstevel@tonic-gate 			} else {
1730Sstevel@tonic-gate 				mktempname(ep);
1740Sstevel@tonic-gate 				deleteino(ep->e_ino);
1750Sstevel@tonic-gate 				/*
1760Sstevel@tonic-gate 				 * once the inode is deleted from the symbol
1770Sstevel@tonic-gate 				 * table, the e_next field is reusable
1780Sstevel@tonic-gate 				 */
1790Sstevel@tonic-gate 				ep->e_next = removelist;
1800Sstevel@tonic-gate 				removelist = ep;
1810Sstevel@tonic-gate 			}
1820Sstevel@tonic-gate 			ep = ep->e_links;
1830Sstevel@tonic-gate 		}
1840Sstevel@tonic-gate 	}
1850Sstevel@tonic-gate }
1860Sstevel@tonic-gate 
1870Sstevel@tonic-gate /*
1880Sstevel@tonic-gate  *	For each directory entry on the incremental tape, determine which
1890Sstevel@tonic-gate  *	category it falls into as follows:
1900Sstevel@tonic-gate  *	KEEP - entries that are to be left alone.
1910Sstevel@tonic-gate  *	NEW - new entries to be added.
1920Sstevel@tonic-gate  *	EXTRACT - files that must be updated with new contents.
1930Sstevel@tonic-gate  *	LINK - new links to be added.
1940Sstevel@tonic-gate  *	Renames are done at the same time.
1950Sstevel@tonic-gate  */
1960Sstevel@tonic-gate long
nodeupdates(name,ino,type)1970Sstevel@tonic-gate nodeupdates(name, ino, type)
1980Sstevel@tonic-gate 	char *name;
1990Sstevel@tonic-gate 	ino_t ino;
2000Sstevel@tonic-gate 	int type;
2010Sstevel@tonic-gate {
2020Sstevel@tonic-gate 	struct entry *ep, *np, *ip;
2030Sstevel@tonic-gate 	long descend = GOOD;
2040Sstevel@tonic-gate 	int lookuptype = 0;
2050Sstevel@tonic-gate 	int key = 0;
2060Sstevel@tonic-gate 		/* key values */
2070Sstevel@tonic-gate #define	ONTAPE	0x1	/* inode is on the tape */
2080Sstevel@tonic-gate #define	INOFND	0x2	/* inode already exists */
2090Sstevel@tonic-gate #define	NAMEFND	0x4	/* name already exists */
2100Sstevel@tonic-gate #define	MODECHG	0x8	/* mode of inode changed */
2110Sstevel@tonic-gate 
2120Sstevel@tonic-gate 	/*
2130Sstevel@tonic-gate 	 * This routine is called once for each element in the
2140Sstevel@tonic-gate 	 * directory hierarchy, with a full path name.
2150Sstevel@tonic-gate 	 * The "type" value is incorrectly specified as LEAF for
2160Sstevel@tonic-gate 	 * directories that are not on the dump tape.
2170Sstevel@tonic-gate 	 *
2180Sstevel@tonic-gate 	 * Check to see if the file is on the tape.
2190Sstevel@tonic-gate 	 */
2200Sstevel@tonic-gate 	if (BIT(ino, dumpmap))
2210Sstevel@tonic-gate 		key |= ONTAPE;
2220Sstevel@tonic-gate 	/*
2230Sstevel@tonic-gate 	 * Check to see if the name exists, and if the name is a link.
2240Sstevel@tonic-gate 	 */
2250Sstevel@tonic-gate 	np = lookupname(name);
2260Sstevel@tonic-gate 	if (np != NIL) {
2270Sstevel@tonic-gate 		key |= NAMEFND;
2280Sstevel@tonic-gate 		ip = lookupino(np->e_ino);
2290Sstevel@tonic-gate 		if (ip == NULL) {
2300Sstevel@tonic-gate 			(void) fprintf(stderr,
2310Sstevel@tonic-gate 			    gettext("corrupted symbol table\n"));
2320Sstevel@tonic-gate 			done(1);
2330Sstevel@tonic-gate 		}
2340Sstevel@tonic-gate 		if (ip != np)
2350Sstevel@tonic-gate 			lookuptype = LINK;
2360Sstevel@tonic-gate 	}
2370Sstevel@tonic-gate 	/*
2380Sstevel@tonic-gate 	 * Check to see if the inode exists, and if one of its links
2390Sstevel@tonic-gate 	 * corresponds to the name (if one was found).
2400Sstevel@tonic-gate 	 */
2410Sstevel@tonic-gate 	ip = lookupino(ino);
2420Sstevel@tonic-gate 	if (ip != NIL) {
2430Sstevel@tonic-gate 		key |= INOFND;
2440Sstevel@tonic-gate 		for (ep = ip->e_links; ep != NIL; ep = ep->e_links) {
2450Sstevel@tonic-gate 			if (ep == np) {
2461022Sswilcox 				/*
2471022Sswilcox 				 * Need to set the NEW flag on the hard link
2481022Sswilcox 				 * so it gets created because we extract the
2491022Sswilcox 				 * "parent".  If the NAMEFND key is set, remove
2501022Sswilcox 				 * the leaf.
2511022Sswilcox 				 */
2521022Sswilcox 				if (ip->e_flags & EXTRACT) {
2531022Sswilcox 					if (key & NAMEFND) {
2541022Sswilcox 						removeleaf(np);
2551022Sswilcox 						freeentry(np);
2561022Sswilcox 						np = NIL;
2571022Sswilcox 						key &= ~NAMEFND;
2581022Sswilcox 					}
2591022Sswilcox 					ep->e_flags |= NEW;
2601022Sswilcox 				} else {
2611022Sswilcox 					ip = ep;
2621022Sswilcox 				}
2630Sstevel@tonic-gate 				break;
2640Sstevel@tonic-gate 			}
2650Sstevel@tonic-gate 		}
2660Sstevel@tonic-gate 	}
2670Sstevel@tonic-gate 	/*
2680Sstevel@tonic-gate 	 * If both a name and an inode are found, but they do not
2690Sstevel@tonic-gate 	 * correspond to the same file, then both the inode that has
2700Sstevel@tonic-gate 	 * been found and the inode corresponding to the name that
2710Sstevel@tonic-gate 	 * has been found need to be renamed. The current pathname
2720Sstevel@tonic-gate 	 * is the new name for the inode that has been found. Since
2730Sstevel@tonic-gate 	 * all files to be deleted have already been removed, the
2740Sstevel@tonic-gate 	 * named file is either a now-unneeded link, or it must live
2750Sstevel@tonic-gate 	 * under a new name in this dump level. If it is a link, it
2760Sstevel@tonic-gate 	 * can be removed. If it is not a link, it is given a
2770Sstevel@tonic-gate 	 * temporary name in anticipation that it will be renamed
2780Sstevel@tonic-gate 	 * when it is later found by inode number.
2790Sstevel@tonic-gate 	 */
2800Sstevel@tonic-gate 	if (((key & (INOFND|NAMEFND)) == (INOFND|NAMEFND)) && ip != np) {
2810Sstevel@tonic-gate 		if (lookuptype == LINK) {
2820Sstevel@tonic-gate 			removeleaf(np);
2830Sstevel@tonic-gate 			freeentry(np);
2840Sstevel@tonic-gate 		} else {
2850Sstevel@tonic-gate 			dprintf(stdout,
2860Sstevel@tonic-gate 			    gettext("name/inode conflict, mktempname %s\n"),
2870Sstevel@tonic-gate 				myname(np));
2880Sstevel@tonic-gate 			mktempname(np);
2890Sstevel@tonic-gate 		}
2900Sstevel@tonic-gate 		np = NIL;
2910Sstevel@tonic-gate 		key &= ~NAMEFND;
2920Sstevel@tonic-gate 	}
2930Sstevel@tonic-gate 	if ((key & ONTAPE) &&
2940Sstevel@tonic-gate 	    (((key & INOFND) && ip->e_type != type) ||
2950Sstevel@tonic-gate 	    ((key & NAMEFND) && np->e_type != type)))
2960Sstevel@tonic-gate 		key |= MODECHG;
2970Sstevel@tonic-gate 
2980Sstevel@tonic-gate 	/*
2990Sstevel@tonic-gate 	 * Decide on the disposition of the file based on its flags.
3000Sstevel@tonic-gate 	 * Note that we have already handled the case in which
3010Sstevel@tonic-gate 	 * a name and inode are found that correspond to different files.
3020Sstevel@tonic-gate 	 * Thus if both NAMEFND and INOFND are set then ip == np.
3030Sstevel@tonic-gate 	 */
3040Sstevel@tonic-gate 	switch (key) {
3050Sstevel@tonic-gate 
3060Sstevel@tonic-gate 	/*
3070Sstevel@tonic-gate 	 * A previously existing file has been found.
3080Sstevel@tonic-gate 	 * Mark it as KEEP so that other links to the inode can be
3090Sstevel@tonic-gate 	 * detected, and so that it will not be reclaimed by the search
3100Sstevel@tonic-gate 	 * for unreferenced names.
3110Sstevel@tonic-gate 	 */
3120Sstevel@tonic-gate 	case INOFND|NAMEFND:
3130Sstevel@tonic-gate 		/* LINTED: result fits into a short */
3140Sstevel@tonic-gate 		ip->e_flags |= KEEP;
3150Sstevel@tonic-gate 		dprintf(stdout, "[%s] %s: %s\n", keyval(key), name,
3160Sstevel@tonic-gate 		    flagvalues(ip));
3170Sstevel@tonic-gate 		break;
3180Sstevel@tonic-gate 
3190Sstevel@tonic-gate 	/*
3200Sstevel@tonic-gate 	 * A file on the tape has a name which is the same as a name
3210Sstevel@tonic-gate 	 * corresponding to a different file in the previous dump.
3220Sstevel@tonic-gate 	 * Since all files to be deleted have already been removed,
3230Sstevel@tonic-gate 	 * this file is either a now-unneeded link, or it must live
3240Sstevel@tonic-gate 	 * under a new name in this dump level. If it is a link, it
3250Sstevel@tonic-gate 	 * can simply be removed. If it is not a link, it is given a
3260Sstevel@tonic-gate 	 * temporary name in anticipation that it will be renamed
3270Sstevel@tonic-gate 	 * when it is later found by inode number (see INOFND case
3280Sstevel@tonic-gate 	 * below). The entry is then treated as a new file.
3290Sstevel@tonic-gate 	 */
3300Sstevel@tonic-gate 	case ONTAPE|NAMEFND:
3310Sstevel@tonic-gate 	case ONTAPE|NAMEFND|MODECHG:
332*2753Smishra 		if (lookuptype == LINK || key == (ONTAPE|NAMEFND)) {
3330Sstevel@tonic-gate 			removeleaf(np);
3340Sstevel@tonic-gate 			freeentry(np);
3350Sstevel@tonic-gate 		} else {
336*2753Smishra 			/*
337*2753Smishra 			 * Create a temporary node only if MODECHG.
338*2753Smishra 			 */
3390Sstevel@tonic-gate 			mktempname(np);
3400Sstevel@tonic-gate 		}
3410Sstevel@tonic-gate 		/*FALLTHROUGH*/
3420Sstevel@tonic-gate 
3430Sstevel@tonic-gate 	/*
3440Sstevel@tonic-gate 	 * A previously non-existent file.
3450Sstevel@tonic-gate 	 * Add it to the file system, and request its extraction.
3460Sstevel@tonic-gate 	 * If it is a directory, create it immediately.
3470Sstevel@tonic-gate 	 * (Since the name is unused there can be no conflict)
3480Sstevel@tonic-gate 	 */
3490Sstevel@tonic-gate 	case ONTAPE:
3500Sstevel@tonic-gate 		ep = addentry(name, ino, type);
3510Sstevel@tonic-gate 		if (type == NODE)
3520Sstevel@tonic-gate 			newnode(ep);
3530Sstevel@tonic-gate 		/* LINTED: result fits into a short */
3540Sstevel@tonic-gate 		ep->e_flags |= NEW|KEEP;
3550Sstevel@tonic-gate 		dprintf(stdout, "[%s] %s: %s\n", keyval(key), name,
3560Sstevel@tonic-gate 			flagvalues(ep));
3570Sstevel@tonic-gate 		break;
3580Sstevel@tonic-gate 
3590Sstevel@tonic-gate 	/*
3600Sstevel@tonic-gate 	 * A file with the same inode number, but a different
3610Sstevel@tonic-gate 	 * name has been found. If the other name has not already
3620Sstevel@tonic-gate 	 * been found (indicated by the KEEP flag, see above) then
3630Sstevel@tonic-gate 	 * this must be a new name for the file, and it is renamed.
3640Sstevel@tonic-gate 	 * If the other name has been found then this must be a
3650Sstevel@tonic-gate 	 * link to the file. Hard links to directories are not
3660Sstevel@tonic-gate 	 * permitted, and are either deleted or converted to
3670Sstevel@tonic-gate 	 * symbolic links. Finally, if the file is on the tape,
3680Sstevel@tonic-gate 	 * a request is made to extract it.
3690Sstevel@tonic-gate 	 */
3700Sstevel@tonic-gate 	case ONTAPE|INOFND:
3710Sstevel@tonic-gate 		if (type == LEAF && (ip->e_flags & KEEP) == 0) {
3720Sstevel@tonic-gate 			/* LINTED: result fits into a short */
3730Sstevel@tonic-gate 			ip->e_flags |= EXTRACT;
3740Sstevel@tonic-gate 		}
3750Sstevel@tonic-gate 		/*FALLTHROUGH*/
3760Sstevel@tonic-gate 	case INOFND:
3770Sstevel@tonic-gate 		if ((ip->e_flags & KEEP) == 0) {
3780Sstevel@tonic-gate 			renameit(myname(ip), name);
3790Sstevel@tonic-gate 			moveentry(ip, name);
3800Sstevel@tonic-gate 			/* LINTED: result fits into a short */
3810Sstevel@tonic-gate 			ip->e_flags |= KEEP;
3820Sstevel@tonic-gate 			dprintf(stdout, "[%s] %s: %s\n", keyval(key), name,
3830Sstevel@tonic-gate 			    flagvalues(ip));
3840Sstevel@tonic-gate 			break;
3850Sstevel@tonic-gate 		}
3860Sstevel@tonic-gate 		if (ip->e_type == NODE) {
3870Sstevel@tonic-gate 			descend = FAIL;
3880Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
3890Sstevel@tonic-gate 			    "deleted hard link %s to directory %s\n"),
3900Sstevel@tonic-gate 			    name, myname(ip));
3910Sstevel@tonic-gate 			break;
3920Sstevel@tonic-gate 		}
3930Sstevel@tonic-gate 		ep = addentry(name, ino, type|LINK);
3940Sstevel@tonic-gate 		/* LINTED: result fits into a short */
3950Sstevel@tonic-gate 		ep->e_flags |= NEW;
3960Sstevel@tonic-gate 		dprintf(stdout, "[%s] %s: %s|LINK\n", keyval(key), name,
3970Sstevel@tonic-gate 		    flagvalues(ep));
3980Sstevel@tonic-gate 		break;
3990Sstevel@tonic-gate 
4000Sstevel@tonic-gate 	/*
4010Sstevel@tonic-gate 	 * A previously known file which is to be updated.
4020Sstevel@tonic-gate 	 */
4030Sstevel@tonic-gate 	case ONTAPE|INOFND|NAMEFND:
404*2753Smishra 		/*
405*2753Smishra 		 * Extract leaf nodes.
406*2753Smishra 		 */
407*2753Smishra 		if (type == LEAF) {
4080Sstevel@tonic-gate 			/* LINTED: result fits into a short */
4090Sstevel@tonic-gate 			np->e_flags |= EXTRACT;
4100Sstevel@tonic-gate 		}
4110Sstevel@tonic-gate 		/* LINTED: result fits into a short */
4120Sstevel@tonic-gate 		np->e_flags |= KEEP;
4130Sstevel@tonic-gate 		dprintf(stdout, "[%s] %s: %s\n", keyval(key), name,
4140Sstevel@tonic-gate 			flagvalues(np));
4150Sstevel@tonic-gate 		break;
4160Sstevel@tonic-gate 
4170Sstevel@tonic-gate 	/*
4180Sstevel@tonic-gate 	 * An inode is being reused in a completely different way.
4190Sstevel@tonic-gate 	 * Normally an extract can simply do an "unlink" followed
4200Sstevel@tonic-gate 	 * by a "creat". Here we must do effectively the same
4210Sstevel@tonic-gate 	 * thing. The complications arise because we cannot really
4220Sstevel@tonic-gate 	 * delete a directory since it may still contain files
4230Sstevel@tonic-gate 	 * that we need to rename, so we delete it from the symbol
4240Sstevel@tonic-gate 	 * table, and put it on the list to be deleted eventually.
4250Sstevel@tonic-gate 	 * Conversely if a directory is to be created, it must be
4260Sstevel@tonic-gate 	 * done immediately, rather than waiting until the
4270Sstevel@tonic-gate 	 * extraction phase.
4280Sstevel@tonic-gate 	 */
4290Sstevel@tonic-gate 	case ONTAPE|INOFND|MODECHG:
4300Sstevel@tonic-gate 	case ONTAPE|INOFND|NAMEFND|MODECHG:
4310Sstevel@tonic-gate 		if (ip->e_flags & KEEP) {
4320Sstevel@tonic-gate 			badentry(ip, gettext("cannot KEEP and change modes"));
4330Sstevel@tonic-gate 			break;
4340Sstevel@tonic-gate 		}
4350Sstevel@tonic-gate 		if (ip->e_type == LEAF) {
4360Sstevel@tonic-gate 			/* changing from leaf to node */
4370Sstevel@tonic-gate 			removeleaf(ip);
4380Sstevel@tonic-gate 			freeentry(ip);
4390Sstevel@tonic-gate 			ip = addentry(name, ino, type);
4400Sstevel@tonic-gate 			newnode(ip);
4410Sstevel@tonic-gate 		} else {
4420Sstevel@tonic-gate 			/* changing from node to leaf */
4430Sstevel@tonic-gate 			if ((ip->e_flags & TMPNAME) == 0)
4440Sstevel@tonic-gate 				mktempname(ip);
4450Sstevel@tonic-gate 			deleteino(ip->e_ino);
4460Sstevel@tonic-gate 			ip->e_next = removelist;
4470Sstevel@tonic-gate 			removelist = ip;
4480Sstevel@tonic-gate 			ip = addentry(name, ino, type);
4490Sstevel@tonic-gate 		}
4500Sstevel@tonic-gate 		/* LINTED: result fits into a short */
4510Sstevel@tonic-gate 		ip->e_flags |= NEW|KEEP;
4520Sstevel@tonic-gate 		dprintf(stdout, "[%s] %s: %s\n", keyval(key), name,
4530Sstevel@tonic-gate 			flagvalues(ip));
4540Sstevel@tonic-gate 		break;
4550Sstevel@tonic-gate 
4560Sstevel@tonic-gate 	/*
4570Sstevel@tonic-gate 	 * A hard link to a directory that has been removed.
4580Sstevel@tonic-gate 	 * Ignore it.
4590Sstevel@tonic-gate 	 */
4600Sstevel@tonic-gate 	case NAMEFND:
4610Sstevel@tonic-gate 		dprintf(stdout, gettext("[%s] %s: Extraneous name\n"),
4620Sstevel@tonic-gate 			keyval(key),
4630Sstevel@tonic-gate 			name);
4640Sstevel@tonic-gate 		descend = FAIL;
4650Sstevel@tonic-gate 		break;
4660Sstevel@tonic-gate 
4670Sstevel@tonic-gate 	/*
4680Sstevel@tonic-gate 	 * If we find a directory entry for a file that is not on
4690Sstevel@tonic-gate 	 * the tape, then we must have found a file that was created
4700Sstevel@tonic-gate 	 * while the dump was in progress. Since we have no contents
4710Sstevel@tonic-gate 	 * for it, we discard the name knowing that it will be on the
4720Sstevel@tonic-gate 	 * next incremental tape.
4730Sstevel@tonic-gate 	 */
4740Sstevel@tonic-gate 	case 0:
4750Sstevel@tonic-gate 		(void) fprintf(stderr,
4760Sstevel@tonic-gate 		    gettext("%s: (inode %lu) not found on volume\n"),
4770Sstevel@tonic-gate 		    name, ino);
4780Sstevel@tonic-gate 		break;
4790Sstevel@tonic-gate 
4800Sstevel@tonic-gate 	/*
4810Sstevel@tonic-gate 	 * If any of these arise, something is grievously wrong with
4820Sstevel@tonic-gate 	 * the current state of the symbol table.
4830Sstevel@tonic-gate 	 */
4840Sstevel@tonic-gate 	case INOFND|NAMEFND|MODECHG:
4850Sstevel@tonic-gate 	case NAMEFND|MODECHG:
4860Sstevel@tonic-gate 	case INOFND|MODECHG:
4870Sstevel@tonic-gate 		(void) fprintf(stderr, "[%s] %s: %s\n",
4880Sstevel@tonic-gate 		    keyval(key), name, gettext("inconsistent state"));
4890Sstevel@tonic-gate 		done(1);
4900Sstevel@tonic-gate 		/*NOTREACHED*/
4910Sstevel@tonic-gate 
4920Sstevel@tonic-gate 	/*
4930Sstevel@tonic-gate 	 * These states "cannot" arise for any state of the symbol table.
4940Sstevel@tonic-gate 	 */
4950Sstevel@tonic-gate 	case ONTAPE|MODECHG:
4960Sstevel@tonic-gate 	case MODECHG:
4970Sstevel@tonic-gate 	default:
4980Sstevel@tonic-gate 		(void) fprintf(stderr, "[%s] %s: %s\n",
4990Sstevel@tonic-gate 		    keyval(key), name, gettext("impossible state"));
5000Sstevel@tonic-gate 		done(1);
5010Sstevel@tonic-gate 		/*NOTREACHED*/
5020Sstevel@tonic-gate 	}
5030Sstevel@tonic-gate 	return (descend);
5040Sstevel@tonic-gate }
5050Sstevel@tonic-gate 
5060Sstevel@tonic-gate /*
5070Sstevel@tonic-gate  * Calculate the active flags in a key.
5080Sstevel@tonic-gate  */
5090Sstevel@tonic-gate static char *
keyval(key)5100Sstevel@tonic-gate keyval(key)
5110Sstevel@tonic-gate 	int key;
5120Sstevel@tonic-gate {
5130Sstevel@tonic-gate 	static char keybuf[32];
5140Sstevel@tonic-gate 
5150Sstevel@tonic-gate 	/* Note longest case is everything except |NIL */
5160Sstevel@tonic-gate 
5170Sstevel@tonic-gate 	(void) strcpy(keybuf, "|NIL");
5180Sstevel@tonic-gate 	keybuf[0] = '\0';
5190Sstevel@tonic-gate 	if (key & ONTAPE)
5200Sstevel@tonic-gate 		(void) strcat(keybuf, "|ONTAPE");
5210Sstevel@tonic-gate 	if (key & INOFND)
5220Sstevel@tonic-gate 		(void) strcat(keybuf, "|INOFND");
5230Sstevel@tonic-gate 	if (key & NAMEFND)
5240Sstevel@tonic-gate 		(void) strcat(keybuf, "|NAMEFND");
5250Sstevel@tonic-gate 	if (key & MODECHG)
5260Sstevel@tonic-gate 		(void) strcat(keybuf, "|MODECHG");
5270Sstevel@tonic-gate 	return (&keybuf[1]);
5280Sstevel@tonic-gate }
5290Sstevel@tonic-gate 
5300Sstevel@tonic-gate /*
5310Sstevel@tonic-gate  * Find unreferenced link names.
5320Sstevel@tonic-gate  */
5330Sstevel@tonic-gate void
5340Sstevel@tonic-gate #ifdef __STDC__
findunreflinks(void)5350Sstevel@tonic-gate findunreflinks(void)
5360Sstevel@tonic-gate #else
5370Sstevel@tonic-gate findunreflinks()
5380Sstevel@tonic-gate #endif
5390Sstevel@tonic-gate {
5400Sstevel@tonic-gate 	struct entry *ep, *np;
5410Sstevel@tonic-gate 	ino_t i;
5420Sstevel@tonic-gate 
5430Sstevel@tonic-gate 	vprintf(stdout, gettext("Find unreferenced names.\n"));
5440Sstevel@tonic-gate 	for (i = ROOTINO; i < maxino; i++) {
5450Sstevel@tonic-gate 		ep = lookupino(i);
5460Sstevel@tonic-gate 		if (ep == NIL || ep->e_type == LEAF || BIT(i, dumpmap) == 0)
5470Sstevel@tonic-gate 			continue;
5480Sstevel@tonic-gate 		for (np = ep->e_entries; np != NIL; np = np->e_sibling) {
5490Sstevel@tonic-gate 			if (np->e_flags == 0) {
5500Sstevel@tonic-gate 				dprintf(stdout, gettext(
5510Sstevel@tonic-gate 				    "%s: remove unreferenced name\n"),
5520Sstevel@tonic-gate 				    myname(np));
5530Sstevel@tonic-gate 				removeleaf(np);
5540Sstevel@tonic-gate 				freeentry(np);
5550Sstevel@tonic-gate 			}
5560Sstevel@tonic-gate 		}
5570Sstevel@tonic-gate 	}
5580Sstevel@tonic-gate 	/*
5590Sstevel@tonic-gate 	 * Any leaves remaining in removed directories are unreferenced.
5600Sstevel@tonic-gate 	 */
5610Sstevel@tonic-gate 	for (ep = removelist; ep != NIL; ep = ep->e_next) {
5620Sstevel@tonic-gate 		for (np = ep->e_entries; np != NIL; np = np->e_sibling) {
5630Sstevel@tonic-gate 			if (np->e_type == LEAF) {
5640Sstevel@tonic-gate 				if (np->e_flags != 0)
5650Sstevel@tonic-gate 					badentry(np, gettext(
5660Sstevel@tonic-gate 						"unreferenced with flags"));
5670Sstevel@tonic-gate 				dprintf(stdout, gettext(
5680Sstevel@tonic-gate 				    "%s: remove unreferenced name\n"),
5690Sstevel@tonic-gate 				    myname(np));
5700Sstevel@tonic-gate 				removeleaf(np);
5710Sstevel@tonic-gate 				freeentry(np);
5720Sstevel@tonic-gate 			}
5730Sstevel@tonic-gate 		}
5740Sstevel@tonic-gate 	}
5750Sstevel@tonic-gate }
5760Sstevel@tonic-gate 
5770Sstevel@tonic-gate /*
5780Sstevel@tonic-gate  * Remove old nodes (directories).
5790Sstevel@tonic-gate  * Note that this routine runs in O(N*D) where:
5800Sstevel@tonic-gate  *	N is the number of directory entries to be removed.
5810Sstevel@tonic-gate  *	D is the maximum depth of the tree.
5820Sstevel@tonic-gate  * If N == D this can be quite slow. If the list were
5830Sstevel@tonic-gate  * topologically sorted, the deletion could be done in
5840Sstevel@tonic-gate  * time O(N).
5850Sstevel@tonic-gate  */
5860Sstevel@tonic-gate void
5870Sstevel@tonic-gate #ifdef __STDC__
removeoldnodes(void)5880Sstevel@tonic-gate removeoldnodes(void)
5890Sstevel@tonic-gate #else
5900Sstevel@tonic-gate removeoldnodes()
5910Sstevel@tonic-gate #endif
5920Sstevel@tonic-gate {
5930Sstevel@tonic-gate 	struct entry *ep, **prev;
5940Sstevel@tonic-gate 	long change;
5950Sstevel@tonic-gate 
5960Sstevel@tonic-gate 	vprintf(stdout, gettext("Remove old nodes (directories).\n"));
5970Sstevel@tonic-gate 	do	{
5980Sstevel@tonic-gate 		change = 0;
5990Sstevel@tonic-gate 		prev = &removelist;
6000Sstevel@tonic-gate 		for (ep = removelist; ep != NIL; ep = *prev) {
6010Sstevel@tonic-gate 			if (ep->e_entries != NIL) {
6020Sstevel@tonic-gate 				prev = &ep->e_next;
6030Sstevel@tonic-gate 				continue;
6040Sstevel@tonic-gate 			}
6050Sstevel@tonic-gate 			*prev = ep->e_next;
6060Sstevel@tonic-gate 			removenode(ep);
6070Sstevel@tonic-gate 			freeentry(ep);
6080Sstevel@tonic-gate 			change++;
6090Sstevel@tonic-gate 		}
6100Sstevel@tonic-gate 	} while (change);
6110Sstevel@tonic-gate 	for (ep = removelist; ep != NIL; ep = ep->e_next)
6120Sstevel@tonic-gate 		badentry(ep, gettext("cannot remove, non-empty"));
6130Sstevel@tonic-gate }
6140Sstevel@tonic-gate 
6150Sstevel@tonic-gate /*
6160Sstevel@tonic-gate  * This is the routine used to extract files for the 'r' command.
6170Sstevel@tonic-gate  * Extract new leaves.
6180Sstevel@tonic-gate  */
6190Sstevel@tonic-gate void
createleaves(symtabfile)6200Sstevel@tonic-gate createleaves(symtabfile)
6210Sstevel@tonic-gate 	char *symtabfile;
6220Sstevel@tonic-gate {
6230Sstevel@tonic-gate 	struct entry *ep;
6240Sstevel@tonic-gate 	char name[MAXCOMPLEXLEN];
6250Sstevel@tonic-gate 	ino_t first;
6260Sstevel@tonic-gate 	int curvol;
6270Sstevel@tonic-gate 
6280Sstevel@tonic-gate 	if (command == 'R') {
6290Sstevel@tonic-gate 		vprintf(stdout, gettext("Continue extraction of new leaves\n"));
6300Sstevel@tonic-gate 	} else {
6310Sstevel@tonic-gate 		vprintf(stdout, gettext("Extract new leaves.\n"));
6320Sstevel@tonic-gate 		dumpsymtable(symtabfile, volno);
6330Sstevel@tonic-gate 	}
6340Sstevel@tonic-gate 	first = lowerbnd(ROOTINO);
6350Sstevel@tonic-gate 	curvol = volno;
6360Sstevel@tonic-gate 	while (curfile.ino < maxino) {
6370Sstevel@tonic-gate 		first = lowerbnd(first);
6380Sstevel@tonic-gate 		/*
6390Sstevel@tonic-gate 		 * If the next available file is not the one which we
6400Sstevel@tonic-gate 		 * expect then we have missed one or more files. Since
6410Sstevel@tonic-gate 		 * we do not request files that were not on the tape,
6420Sstevel@tonic-gate 		 * the lost files must have been due to a tape read error,
6430Sstevel@tonic-gate 		 * or a file that was removed while the dump was in progress.
6440Sstevel@tonic-gate 		 *
6450Sstevel@tonic-gate 		 * The loop will terminate with first == maxino, if not
6460Sstevel@tonic-gate 		 * sooner.  Due to the e_flags manipulation, lowerbnd()
6470Sstevel@tonic-gate 		 * will never return its argument.
6480Sstevel@tonic-gate 		 */
6490Sstevel@tonic-gate 		while (first < curfile.ino) {
6500Sstevel@tonic-gate 			ep = lookupino(first);
6510Sstevel@tonic-gate 			if (ep == NIL) {
6520Sstevel@tonic-gate 				(void) fprintf(stderr,
6530Sstevel@tonic-gate 				    gettext("%d: bad first\n"), first);
6540Sstevel@tonic-gate 				done(1);
6550Sstevel@tonic-gate 			}
6560Sstevel@tonic-gate 			(void) fprintf(stderr,
6570Sstevel@tonic-gate 			    gettext("%s: not found on volume\n"),
6580Sstevel@tonic-gate 			    myname(ep));
6590Sstevel@tonic-gate 			/* LINTED: result fits into a short */
6600Sstevel@tonic-gate 			ep->e_flags &= ~(NEW|EXTRACT);
6610Sstevel@tonic-gate 			first = lowerbnd(first);
6620Sstevel@tonic-gate 		}
6630Sstevel@tonic-gate 		/*
6640Sstevel@tonic-gate 		 * If we find files on the tape that have no corresponding
6650Sstevel@tonic-gate 		 * directory entries, then we must have found a file that
6660Sstevel@tonic-gate 		 * was created while the dump was in progress. Since we have
6670Sstevel@tonic-gate 		 * no name for it, we discard it knowing that it will be
6680Sstevel@tonic-gate 		 * on the next incremental tape.
6690Sstevel@tonic-gate 		 */
6700Sstevel@tonic-gate 		if (first != curfile.ino) {
6710Sstevel@tonic-gate 			(void) fprintf(stderr,
6720Sstevel@tonic-gate 			    gettext("expected next file %d, got %d\n"),
6730Sstevel@tonic-gate 				first, curfile.ino);
6740Sstevel@tonic-gate 			skipfile();
6750Sstevel@tonic-gate 			goto next;
6760Sstevel@tonic-gate 		}
6770Sstevel@tonic-gate 		ep = lookupino(curfile.ino);
6780Sstevel@tonic-gate 		if (ep == NIL) {
6790Sstevel@tonic-gate 			(void) fprintf(stderr,
6800Sstevel@tonic-gate 			    gettext("unknown file on volume\n"));
6810Sstevel@tonic-gate 			done(1);
6820Sstevel@tonic-gate 		}
6830Sstevel@tonic-gate 		if ((ep->e_flags & (NEW|EXTRACT)) == 0)
6840Sstevel@tonic-gate 			badentry(ep, gettext("unexpected file on volume"));
6850Sstevel@tonic-gate 		/*
6860Sstevel@tonic-gate 		 * If the file is to be extracted, then the old file must
6870Sstevel@tonic-gate 		 * be removed since its type may change from one leaf type
6880Sstevel@tonic-gate 		 * to another (eg "file" to "character special"). But we
6890Sstevel@tonic-gate 		 * also need to preserve any existing extended attributes;
6900Sstevel@tonic-gate 		 * so first rename the file, then move its attributes, then
6910Sstevel@tonic-gate 		 * remove it.
6920Sstevel@tonic-gate 		 */
6930Sstevel@tonic-gate 		if ((ep->e_flags & EXTRACT) != 0) {
6940Sstevel@tonic-gate 			char *sname = savename(ep->e_name);
6950Sstevel@tonic-gate 			complexcpy(name, myname(ep), MAXCOMPLEXLEN);
6960Sstevel@tonic-gate 			mktempname(ep);
6970Sstevel@tonic-gate 			(void) extractfile(name);
6980Sstevel@tonic-gate 			movexattrs(myname(ep), name);
6990Sstevel@tonic-gate 			removeleaf(ep);
7000Sstevel@tonic-gate 			freename(ep->e_name);
7010Sstevel@tonic-gate 			ep->e_name = sname;
7020Sstevel@tonic-gate 			ep->e_namlen = strlen(ep->e_name);
7030Sstevel@tonic-gate 			/* LINTED: result fits into a short */
7040Sstevel@tonic-gate 			ep->e_flags &= ~REMOVED;
7050Sstevel@tonic-gate 		} else {
7060Sstevel@tonic-gate 			(void) extractfile(myname(ep));
7070Sstevel@tonic-gate 		}
7080Sstevel@tonic-gate 		/* LINTED: result fits into a short */
7090Sstevel@tonic-gate 		ep->e_flags &= ~(NEW|EXTRACT);
7100Sstevel@tonic-gate 		/*
7110Sstevel@tonic-gate 		 * We checkpoint the restore after every tape reel, so
7120Sstevel@tonic-gate 		 * as to simplify the amount of work required by the
7130Sstevel@tonic-gate 		 * 'R' command.
7140Sstevel@tonic-gate 		 */
7150Sstevel@tonic-gate 	next:
7160Sstevel@tonic-gate 		if (curvol != volno) {
7170Sstevel@tonic-gate 			dumpsymtable(symtabfile, volno);
7180Sstevel@tonic-gate 			skipmaps();
7190Sstevel@tonic-gate 			curvol = volno;
7200Sstevel@tonic-gate 		}
7210Sstevel@tonic-gate 	}
7220Sstevel@tonic-gate }
7230Sstevel@tonic-gate 
7240Sstevel@tonic-gate /*
7250Sstevel@tonic-gate  * This is the routine used to extract files for the 'x' and 'i' commands.
7260Sstevel@tonic-gate  * Efficiently extract a subset of the files on a tape.
7270Sstevel@tonic-gate  */
7280Sstevel@tonic-gate void
7290Sstevel@tonic-gate #ifdef __STDC__
createfiles(void)7300Sstevel@tonic-gate createfiles(void)
7310Sstevel@tonic-gate #else
7320Sstevel@tonic-gate createfiles()
7330Sstevel@tonic-gate #endif
7340Sstevel@tonic-gate {
7350Sstevel@tonic-gate 	ino_t first, next, last;
7360Sstevel@tonic-gate 	struct entry *ep;
7370Sstevel@tonic-gate 	int curvol, nextvol;
7380Sstevel@tonic-gate 
7390Sstevel@tonic-gate 	vprintf(stdout, gettext("Extract requested files\n"));
7400Sstevel@tonic-gate 	first = lowerbnd(ROOTINO);
7410Sstevel@tonic-gate 	last = upperbnd(maxino - 1);
7420Sstevel@tonic-gate 	nextvol = volnumber(first);
7430Sstevel@tonic-gate 	if (nextvol == 0) {
7440Sstevel@tonic-gate 		curfile.action = SKIP;
7450Sstevel@tonic-gate 		getvol(1);
7460Sstevel@tonic-gate 		skipmaps();
7470Sstevel@tonic-gate 		skipdirs();
7480Sstevel@tonic-gate 	}
7490Sstevel@tonic-gate 	for (;;) {
7500Sstevel@tonic-gate 		first = lowerbnd(first);
7510Sstevel@tonic-gate 		last = upperbnd(last);
7520Sstevel@tonic-gate 		/*
7530Sstevel@tonic-gate 		 * Check to see if any files remain to be extracted
7540Sstevel@tonic-gate 		 */
7550Sstevel@tonic-gate 		if (first > last)
7560Sstevel@tonic-gate 			return;
7570Sstevel@tonic-gate 		/*
7580Sstevel@tonic-gate 		 * If a map of inode numbers to tape volumes is
7590Sstevel@tonic-gate 		 * available, then select the next volume to be read.
7600Sstevel@tonic-gate 		 */
7610Sstevel@tonic-gate 		if (nextvol > 0) {
7620Sstevel@tonic-gate 			nextvol = volnumber(first);
7630Sstevel@tonic-gate 			if (nextvol != volno) {
7640Sstevel@tonic-gate 				curfile.action = UNKNOWN;
7650Sstevel@tonic-gate 				getvol(nextvol);
7660Sstevel@tonic-gate 				skipmaps();
7670Sstevel@tonic-gate 			}
7680Sstevel@tonic-gate 		}
7690Sstevel@tonic-gate 		/*
7700Sstevel@tonic-gate 		 * Reject any volumes with inodes greater than
7710Sstevel@tonic-gate 		 * the last one needed. This will only be true
7720Sstevel@tonic-gate 		 * if the above code has not selected a volume.
7730Sstevel@tonic-gate 		 */
7740Sstevel@tonic-gate 		while (curfile.ino > last) {
7750Sstevel@tonic-gate 			curfile.action = SKIP;
7760Sstevel@tonic-gate 			getvol(0);
7770Sstevel@tonic-gate 			skipmaps();
7780Sstevel@tonic-gate 			skipdirs();
7790Sstevel@tonic-gate 		}
7800Sstevel@tonic-gate 		/*
7810Sstevel@tonic-gate 		 * Decide on the next inode needed.
7820Sstevel@tonic-gate 		 * Skip across the inodes until it is found
7830Sstevel@tonic-gate 		 * or an out of order volume change is encountered
7840Sstevel@tonic-gate 		 */
7850Sstevel@tonic-gate 		next = lowerbnd(curfile.ino);
7860Sstevel@tonic-gate 		do	{
7870Sstevel@tonic-gate 			curvol = volno;
7880Sstevel@tonic-gate 			while (next > curfile.ino && volno == curvol)
7890Sstevel@tonic-gate 				skipfile();
7900Sstevel@tonic-gate 			skipmaps();
7910Sstevel@tonic-gate 			skipdirs();
7920Sstevel@tonic-gate 		} while (volno == curvol + 1);
7930Sstevel@tonic-gate 		/*
7940Sstevel@tonic-gate 		 * If volume change out of order occurred the
7950Sstevel@tonic-gate 		 * current state must be recalculated
7960Sstevel@tonic-gate 		 */
7970Sstevel@tonic-gate 		if (volno != curvol)
7980Sstevel@tonic-gate 			continue;
7990Sstevel@tonic-gate 		/*
8000Sstevel@tonic-gate 		 * If the current inode is greater than the one we were
8010Sstevel@tonic-gate 		 * looking for then we missed the one we were looking for.
8020Sstevel@tonic-gate 		 * Since we only attempt to extract files listed in the
8030Sstevel@tonic-gate 		 * dump map, the lost files must have been due to a tape
8040Sstevel@tonic-gate 		 * read error, or a file that was removed while the dump
8050Sstevel@tonic-gate 		 * was in progress. Thus we report all requested files
8060Sstevel@tonic-gate 		 * between the one we were looking for, and the one we
8070Sstevel@tonic-gate 		 * found as missing, and delete their request flags.
8080Sstevel@tonic-gate 		 */
8090Sstevel@tonic-gate 		while (next < curfile.ino) {
8100Sstevel@tonic-gate 			ep = lookupino(next);
8110Sstevel@tonic-gate 			if (ep == NIL) {
8120Sstevel@tonic-gate 				(void) fprintf(stderr,
8130Sstevel@tonic-gate 				    gettext("corrupted symbol table\n"));
8140Sstevel@tonic-gate 				done(1);
8150Sstevel@tonic-gate 			}
8160Sstevel@tonic-gate 			(void) fprintf(stderr,
8170Sstevel@tonic-gate 			    gettext("%s: not found on volume\n"),
8180Sstevel@tonic-gate 			    myname(ep));
8190Sstevel@tonic-gate 			/* LINTED: result fits into a short */
8200Sstevel@tonic-gate 			ep->e_flags &= ~NEW;
8210Sstevel@tonic-gate 			next = lowerbnd(next);
8220Sstevel@tonic-gate 		}
8230Sstevel@tonic-gate 		/*
8240Sstevel@tonic-gate 		 * The current inode is the one that we are looking for,
8250Sstevel@tonic-gate 		 * so extract it per its requested name.
8260Sstevel@tonic-gate 		 */
8270Sstevel@tonic-gate 		if (next == curfile.ino && next <= last) {
8280Sstevel@tonic-gate 			ep = lookupino(next);
8290Sstevel@tonic-gate 			if (ep == NIL) {
8300Sstevel@tonic-gate 				(void) fprintf(stderr,
8310Sstevel@tonic-gate 				    gettext("corrupted symbol table\n"));
8320Sstevel@tonic-gate 				done(1);
8330Sstevel@tonic-gate 			}
8340Sstevel@tonic-gate 			(void) extractfile(myname(ep));
8350Sstevel@tonic-gate 			/* LINTED: result fits into a short */
8360Sstevel@tonic-gate 			ep->e_flags &= ~NEW;
8370Sstevel@tonic-gate 			if (volno != curvol)
8380Sstevel@tonic-gate 				skipmaps();
8390Sstevel@tonic-gate 		}
8400Sstevel@tonic-gate 	}
8410Sstevel@tonic-gate }
8420Sstevel@tonic-gate 
8430Sstevel@tonic-gate /*
8440Sstevel@tonic-gate  * Add links.
8450Sstevel@tonic-gate  */
8460Sstevel@tonic-gate void
8470Sstevel@tonic-gate #ifdef __STDC__
createlinks(void)8480Sstevel@tonic-gate createlinks(void)
8490Sstevel@tonic-gate #else
8500Sstevel@tonic-gate createlinks()
8510Sstevel@tonic-gate #endif
8520Sstevel@tonic-gate {
8530Sstevel@tonic-gate 	struct entry *np, *ep;
8540Sstevel@tonic-gate 	ino_t i;
8550Sstevel@tonic-gate 	int dfd;
8560Sstevel@tonic-gate 	char *to, *from;
8570Sstevel@tonic-gate 	int saverr;
8580Sstevel@tonic-gate 
8590Sstevel@tonic-gate 	vprintf(stdout, gettext("Add links\n"));
8600Sstevel@tonic-gate 	for (i = ROOTINO; i < maxino; i++) {
8610Sstevel@tonic-gate 		ep = lookupino(i);
8620Sstevel@tonic-gate 		if (ep == NIL)
8630Sstevel@tonic-gate 			continue;
8640Sstevel@tonic-gate 		to = savename(myname(ep));
8650Sstevel@tonic-gate 		for (np = ep->e_links; np != NIL; np = np->e_links) {
8660Sstevel@tonic-gate 			if ((np->e_flags & NEW) == 0)
8670Sstevel@tonic-gate 				continue;
8680Sstevel@tonic-gate 			resolve(myname(np), &dfd, &from);
8690Sstevel@tonic-gate 			if (dfd != AT_FDCWD) {
8700Sstevel@tonic-gate 				if (fchdir(dfd) < 0) {
8710Sstevel@tonic-gate 					saverr = errno;
8720Sstevel@tonic-gate 					(void) fprintf(stderr,
8730Sstevel@tonic-gate 					gettext("%s->%s: link failed: %s\n"),
8740Sstevel@tonic-gate 						from, to, strerror(saverr));
8750Sstevel@tonic-gate 					(void) close(dfd);
8760Sstevel@tonic-gate 					continue;
8770Sstevel@tonic-gate 				}
8780Sstevel@tonic-gate 			}
8790Sstevel@tonic-gate 			if (ep->e_type == NODE) {
8800Sstevel@tonic-gate 				(void) lf_linkit(to, from, SYMLINK);
8810Sstevel@tonic-gate 			} else {
8820Sstevel@tonic-gate 				(void) lf_linkit(to, from, HARDLINK);
8830Sstevel@tonic-gate 			}
8840Sstevel@tonic-gate 			/* LINTED: result fits into a short */
8850Sstevel@tonic-gate 			np->e_flags &= ~NEW;
8860Sstevel@tonic-gate 			if (dfd != AT_FDCWD) {
8870Sstevel@tonic-gate 				fchdir(savepwd);
8880Sstevel@tonic-gate 				(void) close(dfd);
8890Sstevel@tonic-gate 			}
8900Sstevel@tonic-gate 		}
8910Sstevel@tonic-gate 		freename(to);
8920Sstevel@tonic-gate 	}
8930Sstevel@tonic-gate }
8940Sstevel@tonic-gate 
8950Sstevel@tonic-gate /*
8960Sstevel@tonic-gate  * Check the symbol table.
8970Sstevel@tonic-gate  * We do this to insure that all the requested work was done, and
8980Sstevel@tonic-gate  * that no temporary names remain.
8990Sstevel@tonic-gate  */
9000Sstevel@tonic-gate void
9010Sstevel@tonic-gate #ifdef __STDC__
checkrestore(void)9020Sstevel@tonic-gate checkrestore(void)
9030Sstevel@tonic-gate #else
9040Sstevel@tonic-gate checkrestore()
9050Sstevel@tonic-gate #endif
9060Sstevel@tonic-gate {
9070Sstevel@tonic-gate 	struct entry *ep;
9080Sstevel@tonic-gate 	ino_t i;
9090Sstevel@tonic-gate 
9100Sstevel@tonic-gate 	vprintf(stdout, gettext("Check the symbol table.\n"));
9110Sstevel@tonic-gate 	for (i = ROOTINO; i < maxino; i++) {
9120Sstevel@tonic-gate 		for (ep = lookupino(i); ep != NIL; ep = ep->e_links) {
9130Sstevel@tonic-gate 			/* LINTED: result fits into a short */
9140Sstevel@tonic-gate 			ep->e_flags &= ~KEEP;
9150Sstevel@tonic-gate 			if (ep->e_type == NODE) {
9160Sstevel@tonic-gate 				/* LINTED: result fits into a short */
9170Sstevel@tonic-gate 				ep->e_flags &= ~(NEW|EXISTED);
9180Sstevel@tonic-gate 			}
9190Sstevel@tonic-gate 			if ((ep->e_flags & ~(XATTR|XATTRROOT)) != 0)
9200Sstevel@tonic-gate 				badentry(ep, gettext("incomplete operations"));
9210Sstevel@tonic-gate 		}
9220Sstevel@tonic-gate 	}
9230Sstevel@tonic-gate }
9240Sstevel@tonic-gate 
9250Sstevel@tonic-gate /*
9260Sstevel@tonic-gate  * Compare with the directory structure on the tape
9270Sstevel@tonic-gate  * A paranoid check that things are as they should be.
9280Sstevel@tonic-gate  */
9290Sstevel@tonic-gate long
verifyfile(name,ino,type)9300Sstevel@tonic-gate verifyfile(name, ino, type)
9310Sstevel@tonic-gate 	char *name;
9320Sstevel@tonic-gate 	ino_t ino;
9330Sstevel@tonic-gate 	int type;
9340Sstevel@tonic-gate {
9350Sstevel@tonic-gate 	struct entry *np, *ep;
9360Sstevel@tonic-gate 	long descend = GOOD;
9370Sstevel@tonic-gate 
9380Sstevel@tonic-gate 	ep = lookupname(name);
9390Sstevel@tonic-gate 	if (ep == NIL) {
9400Sstevel@tonic-gate 		(void) fprintf(stderr,
9410Sstevel@tonic-gate 		    gettext("Warning: missing name %s\n"), name);
9420Sstevel@tonic-gate 		return (FAIL);
9430Sstevel@tonic-gate 	}
9440Sstevel@tonic-gate 	np = lookupino(ino);
9450Sstevel@tonic-gate 	if (np != ep)
9460Sstevel@tonic-gate 		descend = FAIL;
9470Sstevel@tonic-gate 	for (; np != NIL; np = np->e_links)
9480Sstevel@tonic-gate 		if (np == ep)
9490Sstevel@tonic-gate 			break;
9500Sstevel@tonic-gate 	if (np == NIL) {
9510Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("missing inumber %d\n"), ino);
9520Sstevel@tonic-gate 		done(1);
9530Sstevel@tonic-gate 	}
9540Sstevel@tonic-gate 	if (ep->e_type == LEAF && type != LEAF)
9550Sstevel@tonic-gate 		badentry(ep, gettext("type should be LEAF"));
9560Sstevel@tonic-gate 	return (descend);
9570Sstevel@tonic-gate }
9580Sstevel@tonic-gate 
9590Sstevel@tonic-gate /*
9600Sstevel@tonic-gate  * This routine does not actually remove any attribute files, it
9610Sstevel@tonic-gate  * just removes entries from the symbol table.  The attribute files
9620Sstevel@tonic-gate  * themselves are assumed to be removed automatically when the
9630Sstevel@tonic-gate  * parent file is removed.
9640Sstevel@tonic-gate  */
9650Sstevel@tonic-gate static void
removexattrs(ep)9660Sstevel@tonic-gate removexattrs(ep)
9670Sstevel@tonic-gate 	struct entry *ep;
9680Sstevel@tonic-gate {
9690Sstevel@tonic-gate 	struct entry *np = ep;
9700Sstevel@tonic-gate 
9710Sstevel@tonic-gate 	if (ep == NIL)
9720Sstevel@tonic-gate 		return;
9730Sstevel@tonic-gate 	for (np = ep->e_entries; np != NIL; np = np->e_sibling) {
9740Sstevel@tonic-gate 		if (np->e_type == NODE) {
9750Sstevel@tonic-gate 			removexattrs(np);
9760Sstevel@tonic-gate 		} else {
9770Sstevel@tonic-gate 			np->e_flags |= REMOVED;
9780Sstevel@tonic-gate 			freeentry(np);
9790Sstevel@tonic-gate 		}
9800Sstevel@tonic-gate 	}
9810Sstevel@tonic-gate 	ep->e_flags |= REMOVED;
9820Sstevel@tonic-gate 	freeentry(ep);
9830Sstevel@tonic-gate }
9840Sstevel@tonic-gate 
9850Sstevel@tonic-gate /*
9860Sstevel@tonic-gate  * Move all the extended attributes associated with orig to
9870Sstevel@tonic-gate  * the file named by the second argument (targ).
9880Sstevel@tonic-gate  */
9890Sstevel@tonic-gate static void
movexattrs(orig,targ)9900Sstevel@tonic-gate movexattrs(orig, targ)
9910Sstevel@tonic-gate 	char *orig;
9920Sstevel@tonic-gate 	char *targ;
9930Sstevel@tonic-gate {
9940Sstevel@tonic-gate 	char *to, *from;
9950Sstevel@tonic-gate 	int fromfd, fromdir, tofd, todir, tfd;
9960Sstevel@tonic-gate 	DIR *dirp = NULL;
9970Sstevel@tonic-gate 	struct dirent *dp = NULL;
9980Sstevel@tonic-gate 
9990Sstevel@tonic-gate 	fromfd = tofd = fromdir = todir = tfd = -1;
10000Sstevel@tonic-gate 
10010Sstevel@tonic-gate 	resolve(orig, &tfd, &from);
10020Sstevel@tonic-gate 	if (tfd == AT_FDCWD && pathconf(orig, _PC_XATTR_EXISTS) != 1) {
10030Sstevel@tonic-gate 		/* no attributes to move */
10040Sstevel@tonic-gate 		return;
10050Sstevel@tonic-gate 	}
10060Sstevel@tonic-gate 	if ((fromfd = openat64(tfd, from, O_RDONLY|O_NONBLOCK)) == -1) {
10070Sstevel@tonic-gate 		fprintf(stderr, gettext("%s: cannot move attributes: "), from);
10080Sstevel@tonic-gate 		perror("");
10090Sstevel@tonic-gate 		if (tfd != AT_FDCWD) (void) close(tfd);
10100Sstevel@tonic-gate 		goto out;
10110Sstevel@tonic-gate 	}
10120Sstevel@tonic-gate 
10130Sstevel@tonic-gate 	if (fpathconf(fromfd, _PC_XATTR_EXISTS) != 1) {
10140Sstevel@tonic-gate 		/* no attributes to move */
10150Sstevel@tonic-gate 		if (tfd != AT_FDCWD) (void) close(tfd);
10160Sstevel@tonic-gate 		goto out;
10170Sstevel@tonic-gate 	}
10180Sstevel@tonic-gate 	if ((fromdir = openat64(fromfd, ".",
10190Sstevel@tonic-gate 				O_RDONLY|O_NONBLOCK|O_XATTR)) == -1) {
10200Sstevel@tonic-gate 		fprintf(stderr, gettext("%s: cannot access attributes: "),
10210Sstevel@tonic-gate 			from);
10220Sstevel@tonic-gate 		perror("");
10230Sstevel@tonic-gate 		if (tfd != AT_FDCWD) (void) close(tfd);
10240Sstevel@tonic-gate 		goto out;
10250Sstevel@tonic-gate 	}
10260Sstevel@tonic-gate 	if (tfd != AT_FDCWD) (void) close(tfd);
10270Sstevel@tonic-gate 
10280Sstevel@tonic-gate 	resolve(targ, &tfd, &to);
10290Sstevel@tonic-gate 	if ((tofd = openat64(tfd, to, O_RDONLY|O_NONBLOCK)) == -1 ||
10300Sstevel@tonic-gate 	    (todir = openat64(tofd, ".", O_RDONLY|O_NONBLOCK|O_XATTR)) == -1) {
10310Sstevel@tonic-gate 		fprintf(stderr, gettext("%s: cannot create attributes: "), to);
10320Sstevel@tonic-gate 		perror("");
10330Sstevel@tonic-gate 		goto out;
10340Sstevel@tonic-gate 	}
10350Sstevel@tonic-gate 	if (tfd != AT_FDCWD) (void) close(tfd);
10360Sstevel@tonic-gate 	(void) close(tofd);
10370Sstevel@tonic-gate 
10380Sstevel@tonic-gate 	if ((tfd = dup(fromdir)) == -1 ||
10390Sstevel@tonic-gate 	    (dirp = fdopendir(tfd)) == NULL) {
10400Sstevel@tonic-gate 		fprintf(stderr,
10410Sstevel@tonic-gate 	gettext("%s: cannot allocate DIR structure to attribute directory: "),
10420Sstevel@tonic-gate 			from);
10430Sstevel@tonic-gate 		perror("");
10440Sstevel@tonic-gate 		if (tfd != -1) (void) close(tfd);
10450Sstevel@tonic-gate 		goto out;
10460Sstevel@tonic-gate 	}
10470Sstevel@tonic-gate 
10480Sstevel@tonic-gate 	while ((dp = readdir(dirp)) != NULL) {
10490Sstevel@tonic-gate 		if ((dp->d_name[0] == '.' && dp->d_name[1] == '\0') ||
10500Sstevel@tonic-gate 			(dp->d_name[0] == '.' && dp->d_name[1] == '.' &&
10510Sstevel@tonic-gate 			dp->d_name[2] == '\0'))
10520Sstevel@tonic-gate 			continue;
10530Sstevel@tonic-gate 		if ((renameat(fromdir, dp->d_name, todir, dp->d_name)) == -1) {
10540Sstevel@tonic-gate 			fprintf(stderr,
10550Sstevel@tonic-gate 				gettext("%s: cannot move attribute %s: "),
10560Sstevel@tonic-gate 				from, dp->d_name);
10570Sstevel@tonic-gate 			goto out;
10580Sstevel@tonic-gate 		}
10590Sstevel@tonic-gate 	}
10600Sstevel@tonic-gate out:
10610Sstevel@tonic-gate 	if (fromfd != -1)
10620Sstevel@tonic-gate 		(void) close(fromfd);
10630Sstevel@tonic-gate 	if (tofd != -1)
10640Sstevel@tonic-gate 		(void) close(tofd);
10650Sstevel@tonic-gate 	if (dirp != NULL)
10660Sstevel@tonic-gate 		(void) closedir(dirp);
10670Sstevel@tonic-gate 	if (fromdir != -1)
10680Sstevel@tonic-gate 		(void) close(fromdir);
10690Sstevel@tonic-gate 	if (todir != -1)
10700Sstevel@tonic-gate 		(void) close(todir);
10710Sstevel@tonic-gate }
1072