xref: /titanic_51/usr/src/cmd/backup/restore/restore.c (revision d11200d12d856cee89b917790f31062907110834)
17c478bd9Sstevel@tonic-gate /*
2*d11200d1Smishra  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
37c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
47c478bd9Sstevel@tonic-gate  */
57c478bd9Sstevel@tonic-gate 
67c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
77c478bd9Sstevel@tonic-gate /*	  All Rights Reserved	*/
87c478bd9Sstevel@tonic-gate 
97c478bd9Sstevel@tonic-gate /*
107c478bd9Sstevel@tonic-gate  * Copyright (c) 1983 Regents of the University of California.
117c478bd9Sstevel@tonic-gate  * All rights reserved.  The Berkeley software License Agreement
127c478bd9Sstevel@tonic-gate  * specifies the terms and conditions for redistribution.
137c478bd9Sstevel@tonic-gate  */
147c478bd9Sstevel@tonic-gate 
157c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
167c478bd9Sstevel@tonic-gate 
177c478bd9Sstevel@tonic-gate #include "restore.h"
187c478bd9Sstevel@tonic-gate /* undef MAXNAMLEN to prevent compiler warnings about redef in dirent.h */
197c478bd9Sstevel@tonic-gate #undef MAXNAMLEN
207c478bd9Sstevel@tonic-gate #include <dirent.h>
217c478bd9Sstevel@tonic-gate 
227c478bd9Sstevel@tonic-gate #ifdef __STDC__
237c478bd9Sstevel@tonic-gate static char *keyval(int);
247c478bd9Sstevel@tonic-gate static void removexattrs(struct entry *);
257c478bd9Sstevel@tonic-gate static void movexattrs(char *, char *);
267c478bd9Sstevel@tonic-gate #else
277c478bd9Sstevel@tonic-gate static char *keyval();
287c478bd9Sstevel@tonic-gate static void removexattrs();
297c478bd9Sstevel@tonic-gate static void movexattrs();
307c478bd9Sstevel@tonic-gate #endif
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate /*
337c478bd9Sstevel@tonic-gate  * This implements the 't' option.
347c478bd9Sstevel@tonic-gate  * List entries on the tape.
357c478bd9Sstevel@tonic-gate  */
367c478bd9Sstevel@tonic-gate long
377c478bd9Sstevel@tonic-gate listfile(name, ino, type)
387c478bd9Sstevel@tonic-gate 	char *name;
397c478bd9Sstevel@tonic-gate 	ino_t ino;
407c478bd9Sstevel@tonic-gate 	int type;
417c478bd9Sstevel@tonic-gate {
427c478bd9Sstevel@tonic-gate 	long descend = hflag ? GOOD : FAIL;
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate 	if (BIT(ino, dumpmap) == 0) {
457c478bd9Sstevel@tonic-gate 		return (descend);
467c478bd9Sstevel@tonic-gate 	}
477c478bd9Sstevel@tonic-gate 	vprintf(stdout, "%s", type == LEAF ? gettext("leaf") : gettext("dir "));
487c478bd9Sstevel@tonic-gate 	(void) fprintf(stdout, "%10lu\t%s\n", ino, name);
497c478bd9Sstevel@tonic-gate 	return (descend);
507c478bd9Sstevel@tonic-gate }
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate /*
537c478bd9Sstevel@tonic-gate  * This implements the 'x' option.
547c478bd9Sstevel@tonic-gate  * Request that new entries be extracted.
557c478bd9Sstevel@tonic-gate  */
567c478bd9Sstevel@tonic-gate long
577c478bd9Sstevel@tonic-gate addfile(name, ino, type)
587c478bd9Sstevel@tonic-gate 	char *name;
597c478bd9Sstevel@tonic-gate 	ino_t ino;
607c478bd9Sstevel@tonic-gate 	int type;
617c478bd9Sstevel@tonic-gate {
627c478bd9Sstevel@tonic-gate 	struct entry *ep;
637c478bd9Sstevel@tonic-gate 	long descend = hflag ? GOOD : FAIL;
647c478bd9Sstevel@tonic-gate 	char buf[100];
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate 	/* Don't know if ino_t is long or long long, so be safe w/ *printf() */
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate 	if (BIT(ino, dumpmap) == 0) {
697c478bd9Sstevel@tonic-gate 		if (mflag) {
707c478bd9Sstevel@tonic-gate 			dprintf(stdout, gettext(
717c478bd9Sstevel@tonic-gate 			    "%s: not on the volume\n"), name);
727c478bd9Sstevel@tonic-gate 		} else {
737c478bd9Sstevel@tonic-gate 			dprintf(stdout, gettext(
747c478bd9Sstevel@tonic-gate 			    "inode %llu: not on the volume\n"),
757c478bd9Sstevel@tonic-gate 			    (u_longlong_t)ino);
767c478bd9Sstevel@tonic-gate 		}
777c478bd9Sstevel@tonic-gate 		return (descend);
787c478bd9Sstevel@tonic-gate 	}
797c478bd9Sstevel@tonic-gate 	if (!mflag) {
807c478bd9Sstevel@tonic-gate 		(void) snprintf(buf, sizeof (buf), "./%llu", (u_longlong_t)ino);
817c478bd9Sstevel@tonic-gate 		buf[sizeof (buf) - 1] = '\0';
827c478bd9Sstevel@tonic-gate 		name = buf;
837c478bd9Sstevel@tonic-gate 		if (type == NODE) {
847c478bd9Sstevel@tonic-gate 			(void) genliteraldir(name, ino);
857c478bd9Sstevel@tonic-gate 			return (descend);
867c478bd9Sstevel@tonic-gate 		}
877c478bd9Sstevel@tonic-gate 	}
887c478bd9Sstevel@tonic-gate 	ep = lookupino(ino);
897c478bd9Sstevel@tonic-gate 	if (ep != NIL) {
907c478bd9Sstevel@tonic-gate 		if (strcmp(name, myname(ep)) == 0) {
917c478bd9Sstevel@tonic-gate 			/* LINTED: result fits into a short */
927c478bd9Sstevel@tonic-gate 			ep->e_flags |= NEW;
937c478bd9Sstevel@tonic-gate 			return (descend);
947c478bd9Sstevel@tonic-gate 		}
957c478bd9Sstevel@tonic-gate 		type |= LINK;
967c478bd9Sstevel@tonic-gate 	}
977c478bd9Sstevel@tonic-gate 	ep = addentry(name, ino, type);
987c478bd9Sstevel@tonic-gate 	if (type == NODE)
997c478bd9Sstevel@tonic-gate 		newnode(ep);
1007c478bd9Sstevel@tonic-gate 	/* LINTED: result fits into a short */
1017c478bd9Sstevel@tonic-gate 	ep->e_flags |= NEW;
1027c478bd9Sstevel@tonic-gate 	return (descend);
1037c478bd9Sstevel@tonic-gate }
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate /*
1067c478bd9Sstevel@tonic-gate  * This is used by the 'i' option to undo previous requests made by addfile.
1077c478bd9Sstevel@tonic-gate  * Delete entries from the request queue.
1087c478bd9Sstevel@tonic-gate  */
1097c478bd9Sstevel@tonic-gate /* ARGSUSED */
1107c478bd9Sstevel@tonic-gate long
1117c478bd9Sstevel@tonic-gate deletefile(name, ino, type)
1127c478bd9Sstevel@tonic-gate 	char *name;
1137c478bd9Sstevel@tonic-gate 	ino_t ino;
1147c478bd9Sstevel@tonic-gate 	int type;
1157c478bd9Sstevel@tonic-gate {
1167c478bd9Sstevel@tonic-gate 	long descend = hflag ? GOOD : FAIL;
1177c478bd9Sstevel@tonic-gate 	struct entry *ep;
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate 	if (BIT(ino, dumpmap) == 0) {
1207c478bd9Sstevel@tonic-gate 		return (descend);
1217c478bd9Sstevel@tonic-gate 	}
1227c478bd9Sstevel@tonic-gate 	ep = lookupino(ino);
1237c478bd9Sstevel@tonic-gate 	if (ep != NIL) {
1247c478bd9Sstevel@tonic-gate 		/* LINTED: result fits into a short */
1257c478bd9Sstevel@tonic-gate 		ep->e_flags &= ~NEW;
1267c478bd9Sstevel@tonic-gate 	}
1277c478bd9Sstevel@tonic-gate 	return (descend);
1287c478bd9Sstevel@tonic-gate }
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate /*
1317c478bd9Sstevel@tonic-gate  * The following four routines implement the incremental
1327c478bd9Sstevel@tonic-gate  * restore algorithm. The first removes old entries, the second
1337c478bd9Sstevel@tonic-gate  * does renames and calculates the extraction list, the third
1347c478bd9Sstevel@tonic-gate  * cleans up link names missed by the first two, and the final
1357c478bd9Sstevel@tonic-gate  * one deletes old directories.
1367c478bd9Sstevel@tonic-gate  *
1377c478bd9Sstevel@tonic-gate  * Directories cannot be immediately deleted, as they may have
1387c478bd9Sstevel@tonic-gate  * other files in them which need to be moved out first. As
1397c478bd9Sstevel@tonic-gate  * directories to be deleted are found, they are put on the
1407c478bd9Sstevel@tonic-gate  * following deletion list. After all deletions and renames
1417c478bd9Sstevel@tonic-gate  * are done, this list is actually deleted.
1427c478bd9Sstevel@tonic-gate  */
1437c478bd9Sstevel@tonic-gate static struct entry *removelist;
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate /*
1467c478bd9Sstevel@tonic-gate  *	Remove unneeded leaves from the old tree.
1477c478bd9Sstevel@tonic-gate  *	Remove directories from the lookup chains.
1487c478bd9Sstevel@tonic-gate  */
1497c478bd9Sstevel@tonic-gate void
1507c478bd9Sstevel@tonic-gate #ifdef __STDC__
1517c478bd9Sstevel@tonic-gate removeoldleaves(void)
1527c478bd9Sstevel@tonic-gate #else
1537c478bd9Sstevel@tonic-gate removeoldleaves()
1547c478bd9Sstevel@tonic-gate #endif
1557c478bd9Sstevel@tonic-gate {
1567c478bd9Sstevel@tonic-gate 	struct entry *ep;
1577c478bd9Sstevel@tonic-gate 	ino_t i;
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate 	vprintf(stdout, gettext("Mark entries to be removed.\n"));
1607c478bd9Sstevel@tonic-gate 	for (i = ROOTINO + 1; i < maxino; i++) {
1617c478bd9Sstevel@tonic-gate 		if (BIT(i, clrimap))
1627c478bd9Sstevel@tonic-gate 			continue;
1637c478bd9Sstevel@tonic-gate 		ep = lookupino(i);
1647c478bd9Sstevel@tonic-gate 		if (ep == NIL)
1657c478bd9Sstevel@tonic-gate 			continue;
1667c478bd9Sstevel@tonic-gate 		while (ep != NIL) {
1677c478bd9Sstevel@tonic-gate 			dprintf(stdout, gettext("%s: REMOVE\n"), myname(ep));
1687c478bd9Sstevel@tonic-gate 			removexattrs(ep->e_xattrs);
1697c478bd9Sstevel@tonic-gate 			if (ep->e_type == LEAF) {
1707c478bd9Sstevel@tonic-gate 				removeleaf(ep);
1717c478bd9Sstevel@tonic-gate 				freeentry(ep);
1727c478bd9Sstevel@tonic-gate 			} else {
1737c478bd9Sstevel@tonic-gate 				mktempname(ep);
1747c478bd9Sstevel@tonic-gate 				deleteino(ep->e_ino);
1757c478bd9Sstevel@tonic-gate 				/*
1767c478bd9Sstevel@tonic-gate 				 * once the inode is deleted from the symbol
1777c478bd9Sstevel@tonic-gate 				 * table, the e_next field is reusable
1787c478bd9Sstevel@tonic-gate 				 */
1797c478bd9Sstevel@tonic-gate 				ep->e_next = removelist;
1807c478bd9Sstevel@tonic-gate 				removelist = ep;
1817c478bd9Sstevel@tonic-gate 			}
1827c478bd9Sstevel@tonic-gate 			ep = ep->e_links;
1837c478bd9Sstevel@tonic-gate 		}
1847c478bd9Sstevel@tonic-gate 	}
1857c478bd9Sstevel@tonic-gate }
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate /*
1887c478bd9Sstevel@tonic-gate  *	For each directory entry on the incremental tape, determine which
1897c478bd9Sstevel@tonic-gate  *	category it falls into as follows:
1907c478bd9Sstevel@tonic-gate  *	KEEP - entries that are to be left alone.
1917c478bd9Sstevel@tonic-gate  *	NEW - new entries to be added.
1927c478bd9Sstevel@tonic-gate  *	EXTRACT - files that must be updated with new contents.
1937c478bd9Sstevel@tonic-gate  *	LINK - new links to be added.
1947c478bd9Sstevel@tonic-gate  *	Renames are done at the same time.
1957c478bd9Sstevel@tonic-gate  */
1967c478bd9Sstevel@tonic-gate long
1977c478bd9Sstevel@tonic-gate nodeupdates(name, ino, type)
1987c478bd9Sstevel@tonic-gate 	char *name;
1997c478bd9Sstevel@tonic-gate 	ino_t ino;
2007c478bd9Sstevel@tonic-gate 	int type;
2017c478bd9Sstevel@tonic-gate {
2027c478bd9Sstevel@tonic-gate 	struct entry *ep, *np, *ip;
2037c478bd9Sstevel@tonic-gate 	long descend = GOOD;
2047c478bd9Sstevel@tonic-gate 	int lookuptype = 0;
2057c478bd9Sstevel@tonic-gate 	int key = 0;
2067c478bd9Sstevel@tonic-gate 		/* key values */
2077c478bd9Sstevel@tonic-gate #define	ONTAPE	0x1	/* inode is on the tape */
2087c478bd9Sstevel@tonic-gate #define	INOFND	0x2	/* inode already exists */
2097c478bd9Sstevel@tonic-gate #define	NAMEFND	0x4	/* name already exists */
2107c478bd9Sstevel@tonic-gate #define	MODECHG	0x8	/* mode of inode changed */
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate 	/*
2137c478bd9Sstevel@tonic-gate 	 * This routine is called once for each element in the
2147c478bd9Sstevel@tonic-gate 	 * directory hierarchy, with a full path name.
2157c478bd9Sstevel@tonic-gate 	 * The "type" value is incorrectly specified as LEAF for
2167c478bd9Sstevel@tonic-gate 	 * directories that are not on the dump tape.
2177c478bd9Sstevel@tonic-gate 	 *
2187c478bd9Sstevel@tonic-gate 	 * Check to see if the file is on the tape.
2197c478bd9Sstevel@tonic-gate 	 */
2207c478bd9Sstevel@tonic-gate 	if (BIT(ino, dumpmap))
2217c478bd9Sstevel@tonic-gate 		key |= ONTAPE;
2227c478bd9Sstevel@tonic-gate 	/*
2237c478bd9Sstevel@tonic-gate 	 * Check to see if the name exists, and if the name is a link.
2247c478bd9Sstevel@tonic-gate 	 */
2257c478bd9Sstevel@tonic-gate 	np = lookupname(name);
2267c478bd9Sstevel@tonic-gate 	if (np != NIL) {
2277c478bd9Sstevel@tonic-gate 		key |= NAMEFND;
2287c478bd9Sstevel@tonic-gate 		ip = lookupino(np->e_ino);
2297c478bd9Sstevel@tonic-gate 		if (ip == NULL) {
2307c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
2317c478bd9Sstevel@tonic-gate 			    gettext("corrupted symbol table\n"));
2327c478bd9Sstevel@tonic-gate 			done(1);
2337c478bd9Sstevel@tonic-gate 		}
2347c478bd9Sstevel@tonic-gate 		if (ip != np)
2357c478bd9Sstevel@tonic-gate 			lookuptype = LINK;
2367c478bd9Sstevel@tonic-gate 	}
2377c478bd9Sstevel@tonic-gate 	/*
2387c478bd9Sstevel@tonic-gate 	 * Check to see if the inode exists, and if one of its links
2397c478bd9Sstevel@tonic-gate 	 * corresponds to the name (if one was found).
2407c478bd9Sstevel@tonic-gate 	 */
2417c478bd9Sstevel@tonic-gate 	ip = lookupino(ino);
2427c478bd9Sstevel@tonic-gate 	if (ip != NIL) {
2437c478bd9Sstevel@tonic-gate 		key |= INOFND;
2447c478bd9Sstevel@tonic-gate 		for (ep = ip->e_links; ep != NIL; ep = ep->e_links) {
2457c478bd9Sstevel@tonic-gate 			if (ep == np) {
2466c70fc64Sswilcox 				/*
2476c70fc64Sswilcox 				 * Need to set the NEW flag on the hard link
2486c70fc64Sswilcox 				 * so it gets created because we extract the
2496c70fc64Sswilcox 				 * "parent".  If the NAMEFND key is set, remove
2506c70fc64Sswilcox 				 * the leaf.
2516c70fc64Sswilcox 				 */
2526c70fc64Sswilcox 				if (ip->e_flags & EXTRACT) {
2536c70fc64Sswilcox 					if (key & NAMEFND) {
2546c70fc64Sswilcox 						removeleaf(np);
2556c70fc64Sswilcox 						freeentry(np);
2566c70fc64Sswilcox 						np = NIL;
2576c70fc64Sswilcox 						key &= ~NAMEFND;
2586c70fc64Sswilcox 					}
2596c70fc64Sswilcox 					ep->e_flags |= NEW;
2606c70fc64Sswilcox 				} else {
2617c478bd9Sstevel@tonic-gate 					ip = ep;
2626c70fc64Sswilcox 				}
2637c478bd9Sstevel@tonic-gate 				break;
2647c478bd9Sstevel@tonic-gate 			}
2657c478bd9Sstevel@tonic-gate 		}
2667c478bd9Sstevel@tonic-gate 	}
2677c478bd9Sstevel@tonic-gate 	/*
2687c478bd9Sstevel@tonic-gate 	 * If both a name and an inode are found, but they do not
2697c478bd9Sstevel@tonic-gate 	 * correspond to the same file, then both the inode that has
2707c478bd9Sstevel@tonic-gate 	 * been found and the inode corresponding to the name that
2717c478bd9Sstevel@tonic-gate 	 * has been found need to be renamed. The current pathname
2727c478bd9Sstevel@tonic-gate 	 * is the new name for the inode that has been found. Since
2737c478bd9Sstevel@tonic-gate 	 * all files to be deleted have already been removed, the
2747c478bd9Sstevel@tonic-gate 	 * named file is either a now-unneeded link, or it must live
2757c478bd9Sstevel@tonic-gate 	 * under a new name in this dump level. If it is a link, it
2767c478bd9Sstevel@tonic-gate 	 * can be removed. If it is not a link, it is given a
2777c478bd9Sstevel@tonic-gate 	 * temporary name in anticipation that it will be renamed
2787c478bd9Sstevel@tonic-gate 	 * when it is later found by inode number.
2797c478bd9Sstevel@tonic-gate 	 */
2807c478bd9Sstevel@tonic-gate 	if (((key & (INOFND|NAMEFND)) == (INOFND|NAMEFND)) && ip != np) {
2817c478bd9Sstevel@tonic-gate 		if (lookuptype == LINK) {
2827c478bd9Sstevel@tonic-gate 			removeleaf(np);
2837c478bd9Sstevel@tonic-gate 			freeentry(np);
2847c478bd9Sstevel@tonic-gate 		} else {
2857c478bd9Sstevel@tonic-gate 			dprintf(stdout,
2867c478bd9Sstevel@tonic-gate 			    gettext("name/inode conflict, mktempname %s\n"),
2877c478bd9Sstevel@tonic-gate 				myname(np));
2887c478bd9Sstevel@tonic-gate 			mktempname(np);
2897c478bd9Sstevel@tonic-gate 		}
2907c478bd9Sstevel@tonic-gate 		np = NIL;
2917c478bd9Sstevel@tonic-gate 		key &= ~NAMEFND;
2927c478bd9Sstevel@tonic-gate 	}
2937c478bd9Sstevel@tonic-gate 	if ((key & ONTAPE) &&
2947c478bd9Sstevel@tonic-gate 	    (((key & INOFND) && ip->e_type != type) ||
2957c478bd9Sstevel@tonic-gate 	    ((key & NAMEFND) && np->e_type != type)))
2967c478bd9Sstevel@tonic-gate 		key |= MODECHG;
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate 	/*
2997c478bd9Sstevel@tonic-gate 	 * Decide on the disposition of the file based on its flags.
3007c478bd9Sstevel@tonic-gate 	 * Note that we have already handled the case in which
3017c478bd9Sstevel@tonic-gate 	 * a name and inode are found that correspond to different files.
3027c478bd9Sstevel@tonic-gate 	 * Thus if both NAMEFND and INOFND are set then ip == np.
3037c478bd9Sstevel@tonic-gate 	 */
3047c478bd9Sstevel@tonic-gate 	switch (key) {
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate 	/*
3077c478bd9Sstevel@tonic-gate 	 * A previously existing file has been found.
3087c478bd9Sstevel@tonic-gate 	 * Mark it as KEEP so that other links to the inode can be
3097c478bd9Sstevel@tonic-gate 	 * detected, and so that it will not be reclaimed by the search
3107c478bd9Sstevel@tonic-gate 	 * for unreferenced names.
3117c478bd9Sstevel@tonic-gate 	 */
3127c478bd9Sstevel@tonic-gate 	case INOFND|NAMEFND:
3137c478bd9Sstevel@tonic-gate 		/* LINTED: result fits into a short */
3147c478bd9Sstevel@tonic-gate 		ip->e_flags |= KEEP;
3157c478bd9Sstevel@tonic-gate 		dprintf(stdout, "[%s] %s: %s\n", keyval(key), name,
3167c478bd9Sstevel@tonic-gate 		    flagvalues(ip));
3177c478bd9Sstevel@tonic-gate 		break;
3187c478bd9Sstevel@tonic-gate 
3197c478bd9Sstevel@tonic-gate 	/*
3207c478bd9Sstevel@tonic-gate 	 * A file on the tape has a name which is the same as a name
3217c478bd9Sstevel@tonic-gate 	 * corresponding to a different file in the previous dump.
3227c478bd9Sstevel@tonic-gate 	 * Since all files to be deleted have already been removed,
3237c478bd9Sstevel@tonic-gate 	 * this file is either a now-unneeded link, or it must live
3247c478bd9Sstevel@tonic-gate 	 * under a new name in this dump level. If it is a link, it
3257c478bd9Sstevel@tonic-gate 	 * can simply be removed. If it is not a link, it is given a
3267c478bd9Sstevel@tonic-gate 	 * temporary name in anticipation that it will be renamed
3277c478bd9Sstevel@tonic-gate 	 * when it is later found by inode number (see INOFND case
3287c478bd9Sstevel@tonic-gate 	 * below). The entry is then treated as a new file.
3297c478bd9Sstevel@tonic-gate 	 */
3307c478bd9Sstevel@tonic-gate 	case ONTAPE|NAMEFND:
3317c478bd9Sstevel@tonic-gate 	case ONTAPE|NAMEFND|MODECHG:
332*d11200d1Smishra 		if (lookuptype == LINK || key == (ONTAPE|NAMEFND)) {
3337c478bd9Sstevel@tonic-gate 			removeleaf(np);
3347c478bd9Sstevel@tonic-gate 			freeentry(np);
3357c478bd9Sstevel@tonic-gate 		} else {
336*d11200d1Smishra 			/*
337*d11200d1Smishra 			 * Create a temporary node only if MODECHG.
338*d11200d1Smishra 			 */
3397c478bd9Sstevel@tonic-gate 			mktempname(np);
3407c478bd9Sstevel@tonic-gate 		}
3417c478bd9Sstevel@tonic-gate 		/*FALLTHROUGH*/
3427c478bd9Sstevel@tonic-gate 
3437c478bd9Sstevel@tonic-gate 	/*
3447c478bd9Sstevel@tonic-gate 	 * A previously non-existent file.
3457c478bd9Sstevel@tonic-gate 	 * Add it to the file system, and request its extraction.
3467c478bd9Sstevel@tonic-gate 	 * If it is a directory, create it immediately.
3477c478bd9Sstevel@tonic-gate 	 * (Since the name is unused there can be no conflict)
3487c478bd9Sstevel@tonic-gate 	 */
3497c478bd9Sstevel@tonic-gate 	case ONTAPE:
3507c478bd9Sstevel@tonic-gate 		ep = addentry(name, ino, type);
3517c478bd9Sstevel@tonic-gate 		if (type == NODE)
3527c478bd9Sstevel@tonic-gate 			newnode(ep);
3537c478bd9Sstevel@tonic-gate 		/* LINTED: result fits into a short */
3547c478bd9Sstevel@tonic-gate 		ep->e_flags |= NEW|KEEP;
3557c478bd9Sstevel@tonic-gate 		dprintf(stdout, "[%s] %s: %s\n", keyval(key), name,
3567c478bd9Sstevel@tonic-gate 			flagvalues(ep));
3577c478bd9Sstevel@tonic-gate 		break;
3587c478bd9Sstevel@tonic-gate 
3597c478bd9Sstevel@tonic-gate 	/*
3607c478bd9Sstevel@tonic-gate 	 * A file with the same inode number, but a different
3617c478bd9Sstevel@tonic-gate 	 * name has been found. If the other name has not already
3627c478bd9Sstevel@tonic-gate 	 * been found (indicated by the KEEP flag, see above) then
3637c478bd9Sstevel@tonic-gate 	 * this must be a new name for the file, and it is renamed.
3647c478bd9Sstevel@tonic-gate 	 * If the other name has been found then this must be a
3657c478bd9Sstevel@tonic-gate 	 * link to the file. Hard links to directories are not
3667c478bd9Sstevel@tonic-gate 	 * permitted, and are either deleted or converted to
3677c478bd9Sstevel@tonic-gate 	 * symbolic links. Finally, if the file is on the tape,
3687c478bd9Sstevel@tonic-gate 	 * a request is made to extract it.
3697c478bd9Sstevel@tonic-gate 	 */
3707c478bd9Sstevel@tonic-gate 	case ONTAPE|INOFND:
3717c478bd9Sstevel@tonic-gate 		if (type == LEAF && (ip->e_flags & KEEP) == 0) {
3727c478bd9Sstevel@tonic-gate 			/* LINTED: result fits into a short */
3737c478bd9Sstevel@tonic-gate 			ip->e_flags |= EXTRACT;
3747c478bd9Sstevel@tonic-gate 		}
3757c478bd9Sstevel@tonic-gate 		/*FALLTHROUGH*/
3767c478bd9Sstevel@tonic-gate 	case INOFND:
3777c478bd9Sstevel@tonic-gate 		if ((ip->e_flags & KEEP) == 0) {
3787c478bd9Sstevel@tonic-gate 			renameit(myname(ip), name);
3797c478bd9Sstevel@tonic-gate 			moveentry(ip, name);
3807c478bd9Sstevel@tonic-gate 			/* LINTED: result fits into a short */
3817c478bd9Sstevel@tonic-gate 			ip->e_flags |= KEEP;
3827c478bd9Sstevel@tonic-gate 			dprintf(stdout, "[%s] %s: %s\n", keyval(key), name,
3837c478bd9Sstevel@tonic-gate 			    flagvalues(ip));
3847c478bd9Sstevel@tonic-gate 			break;
3857c478bd9Sstevel@tonic-gate 		}
3867c478bd9Sstevel@tonic-gate 		if (ip->e_type == NODE) {
3877c478bd9Sstevel@tonic-gate 			descend = FAIL;
3887c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
3897c478bd9Sstevel@tonic-gate 			    "deleted hard link %s to directory %s\n"),
3907c478bd9Sstevel@tonic-gate 			    name, myname(ip));
3917c478bd9Sstevel@tonic-gate 			break;
3927c478bd9Sstevel@tonic-gate 		}
3937c478bd9Sstevel@tonic-gate 		ep = addentry(name, ino, type|LINK);
3947c478bd9Sstevel@tonic-gate 		/* LINTED: result fits into a short */
3957c478bd9Sstevel@tonic-gate 		ep->e_flags |= NEW;
3967c478bd9Sstevel@tonic-gate 		dprintf(stdout, "[%s] %s: %s|LINK\n", keyval(key), name,
3977c478bd9Sstevel@tonic-gate 		    flagvalues(ep));
3987c478bd9Sstevel@tonic-gate 		break;
3997c478bd9Sstevel@tonic-gate 
4007c478bd9Sstevel@tonic-gate 	/*
4017c478bd9Sstevel@tonic-gate 	 * A previously known file which is to be updated.
4027c478bd9Sstevel@tonic-gate 	 */
4037c478bd9Sstevel@tonic-gate 	case ONTAPE|INOFND|NAMEFND:
404*d11200d1Smishra 		/*
405*d11200d1Smishra 		 * Extract leaf nodes.
406*d11200d1Smishra 		 */
407*d11200d1Smishra 		if (type == LEAF) {
4087c478bd9Sstevel@tonic-gate 			/* LINTED: result fits into a short */
4097c478bd9Sstevel@tonic-gate 			np->e_flags |= EXTRACT;
4107c478bd9Sstevel@tonic-gate 		}
4117c478bd9Sstevel@tonic-gate 		/* LINTED: result fits into a short */
4127c478bd9Sstevel@tonic-gate 		np->e_flags |= KEEP;
4137c478bd9Sstevel@tonic-gate 		dprintf(stdout, "[%s] %s: %s\n", keyval(key), name,
4147c478bd9Sstevel@tonic-gate 			flagvalues(np));
4157c478bd9Sstevel@tonic-gate 		break;
4167c478bd9Sstevel@tonic-gate 
4177c478bd9Sstevel@tonic-gate 	/*
4187c478bd9Sstevel@tonic-gate 	 * An inode is being reused in a completely different way.
4197c478bd9Sstevel@tonic-gate 	 * Normally an extract can simply do an "unlink" followed
4207c478bd9Sstevel@tonic-gate 	 * by a "creat". Here we must do effectively the same
4217c478bd9Sstevel@tonic-gate 	 * thing. The complications arise because we cannot really
4227c478bd9Sstevel@tonic-gate 	 * delete a directory since it may still contain files
4237c478bd9Sstevel@tonic-gate 	 * that we need to rename, so we delete it from the symbol
4247c478bd9Sstevel@tonic-gate 	 * table, and put it on the list to be deleted eventually.
4257c478bd9Sstevel@tonic-gate 	 * Conversely if a directory is to be created, it must be
4267c478bd9Sstevel@tonic-gate 	 * done immediately, rather than waiting until the
4277c478bd9Sstevel@tonic-gate 	 * extraction phase.
4287c478bd9Sstevel@tonic-gate 	 */
4297c478bd9Sstevel@tonic-gate 	case ONTAPE|INOFND|MODECHG:
4307c478bd9Sstevel@tonic-gate 	case ONTAPE|INOFND|NAMEFND|MODECHG:
4317c478bd9Sstevel@tonic-gate 		if (ip->e_flags & KEEP) {
4327c478bd9Sstevel@tonic-gate 			badentry(ip, gettext("cannot KEEP and change modes"));
4337c478bd9Sstevel@tonic-gate 			break;
4347c478bd9Sstevel@tonic-gate 		}
4357c478bd9Sstevel@tonic-gate 		if (ip->e_type == LEAF) {
4367c478bd9Sstevel@tonic-gate 			/* changing from leaf to node */
4377c478bd9Sstevel@tonic-gate 			removeleaf(ip);
4387c478bd9Sstevel@tonic-gate 			freeentry(ip);
4397c478bd9Sstevel@tonic-gate 			ip = addentry(name, ino, type);
4407c478bd9Sstevel@tonic-gate 			newnode(ip);
4417c478bd9Sstevel@tonic-gate 		} else {
4427c478bd9Sstevel@tonic-gate 			/* changing from node to leaf */
4437c478bd9Sstevel@tonic-gate 			if ((ip->e_flags & TMPNAME) == 0)
4447c478bd9Sstevel@tonic-gate 				mktempname(ip);
4457c478bd9Sstevel@tonic-gate 			deleteino(ip->e_ino);
4467c478bd9Sstevel@tonic-gate 			ip->e_next = removelist;
4477c478bd9Sstevel@tonic-gate 			removelist = ip;
4487c478bd9Sstevel@tonic-gate 			ip = addentry(name, ino, type);
4497c478bd9Sstevel@tonic-gate 		}
4507c478bd9Sstevel@tonic-gate 		/* LINTED: result fits into a short */
4517c478bd9Sstevel@tonic-gate 		ip->e_flags |= NEW|KEEP;
4527c478bd9Sstevel@tonic-gate 		dprintf(stdout, "[%s] %s: %s\n", keyval(key), name,
4537c478bd9Sstevel@tonic-gate 			flagvalues(ip));
4547c478bd9Sstevel@tonic-gate 		break;
4557c478bd9Sstevel@tonic-gate 
4567c478bd9Sstevel@tonic-gate 	/*
4577c478bd9Sstevel@tonic-gate 	 * A hard link to a directory that has been removed.
4587c478bd9Sstevel@tonic-gate 	 * Ignore it.
4597c478bd9Sstevel@tonic-gate 	 */
4607c478bd9Sstevel@tonic-gate 	case NAMEFND:
4617c478bd9Sstevel@tonic-gate 		dprintf(stdout, gettext("[%s] %s: Extraneous name\n"),
4627c478bd9Sstevel@tonic-gate 			keyval(key),
4637c478bd9Sstevel@tonic-gate 			name);
4647c478bd9Sstevel@tonic-gate 		descend = FAIL;
4657c478bd9Sstevel@tonic-gate 		break;
4667c478bd9Sstevel@tonic-gate 
4677c478bd9Sstevel@tonic-gate 	/*
4687c478bd9Sstevel@tonic-gate 	 * If we find a directory entry for a file that is not on
4697c478bd9Sstevel@tonic-gate 	 * the tape, then we must have found a file that was created
4707c478bd9Sstevel@tonic-gate 	 * while the dump was in progress. Since we have no contents
4717c478bd9Sstevel@tonic-gate 	 * for it, we discard the name knowing that it will be on the
4727c478bd9Sstevel@tonic-gate 	 * next incremental tape.
4737c478bd9Sstevel@tonic-gate 	 */
4747c478bd9Sstevel@tonic-gate 	case 0:
4757c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
4767c478bd9Sstevel@tonic-gate 		    gettext("%s: (inode %lu) not found on volume\n"),
4777c478bd9Sstevel@tonic-gate 		    name, ino);
4787c478bd9Sstevel@tonic-gate 		break;
4797c478bd9Sstevel@tonic-gate 
4807c478bd9Sstevel@tonic-gate 	/*
4817c478bd9Sstevel@tonic-gate 	 * If any of these arise, something is grievously wrong with
4827c478bd9Sstevel@tonic-gate 	 * the current state of the symbol table.
4837c478bd9Sstevel@tonic-gate 	 */
4847c478bd9Sstevel@tonic-gate 	case INOFND|NAMEFND|MODECHG:
4857c478bd9Sstevel@tonic-gate 	case NAMEFND|MODECHG:
4867c478bd9Sstevel@tonic-gate 	case INOFND|MODECHG:
4877c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "[%s] %s: %s\n",
4887c478bd9Sstevel@tonic-gate 		    keyval(key), name, gettext("inconsistent state"));
4897c478bd9Sstevel@tonic-gate 		done(1);
4907c478bd9Sstevel@tonic-gate 		/*NOTREACHED*/
4917c478bd9Sstevel@tonic-gate 
4927c478bd9Sstevel@tonic-gate 	/*
4937c478bd9Sstevel@tonic-gate 	 * These states "cannot" arise for any state of the symbol table.
4947c478bd9Sstevel@tonic-gate 	 */
4957c478bd9Sstevel@tonic-gate 	case ONTAPE|MODECHG:
4967c478bd9Sstevel@tonic-gate 	case MODECHG:
4977c478bd9Sstevel@tonic-gate 	default:
4987c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "[%s] %s: %s\n",
4997c478bd9Sstevel@tonic-gate 		    keyval(key), name, gettext("impossible state"));
5007c478bd9Sstevel@tonic-gate 		done(1);
5017c478bd9Sstevel@tonic-gate 		/*NOTREACHED*/
5027c478bd9Sstevel@tonic-gate 	}
5037c478bd9Sstevel@tonic-gate 	return (descend);
5047c478bd9Sstevel@tonic-gate }
5057c478bd9Sstevel@tonic-gate 
5067c478bd9Sstevel@tonic-gate /*
5077c478bd9Sstevel@tonic-gate  * Calculate the active flags in a key.
5087c478bd9Sstevel@tonic-gate  */
5097c478bd9Sstevel@tonic-gate static char *
5107c478bd9Sstevel@tonic-gate keyval(key)
5117c478bd9Sstevel@tonic-gate 	int key;
5127c478bd9Sstevel@tonic-gate {
5137c478bd9Sstevel@tonic-gate 	static char keybuf[32];
5147c478bd9Sstevel@tonic-gate 
5157c478bd9Sstevel@tonic-gate 	/* Note longest case is everything except |NIL */
5167c478bd9Sstevel@tonic-gate 
5177c478bd9Sstevel@tonic-gate 	(void) strcpy(keybuf, "|NIL");
5187c478bd9Sstevel@tonic-gate 	keybuf[0] = '\0';
5197c478bd9Sstevel@tonic-gate 	if (key & ONTAPE)
5207c478bd9Sstevel@tonic-gate 		(void) strcat(keybuf, "|ONTAPE");
5217c478bd9Sstevel@tonic-gate 	if (key & INOFND)
5227c478bd9Sstevel@tonic-gate 		(void) strcat(keybuf, "|INOFND");
5237c478bd9Sstevel@tonic-gate 	if (key & NAMEFND)
5247c478bd9Sstevel@tonic-gate 		(void) strcat(keybuf, "|NAMEFND");
5257c478bd9Sstevel@tonic-gate 	if (key & MODECHG)
5267c478bd9Sstevel@tonic-gate 		(void) strcat(keybuf, "|MODECHG");
5277c478bd9Sstevel@tonic-gate 	return (&keybuf[1]);
5287c478bd9Sstevel@tonic-gate }
5297c478bd9Sstevel@tonic-gate 
5307c478bd9Sstevel@tonic-gate /*
5317c478bd9Sstevel@tonic-gate  * Find unreferenced link names.
5327c478bd9Sstevel@tonic-gate  */
5337c478bd9Sstevel@tonic-gate void
5347c478bd9Sstevel@tonic-gate #ifdef __STDC__
5357c478bd9Sstevel@tonic-gate findunreflinks(void)
5367c478bd9Sstevel@tonic-gate #else
5377c478bd9Sstevel@tonic-gate findunreflinks()
5387c478bd9Sstevel@tonic-gate #endif
5397c478bd9Sstevel@tonic-gate {
5407c478bd9Sstevel@tonic-gate 	struct entry *ep, *np;
5417c478bd9Sstevel@tonic-gate 	ino_t i;
5427c478bd9Sstevel@tonic-gate 
5437c478bd9Sstevel@tonic-gate 	vprintf(stdout, gettext("Find unreferenced names.\n"));
5447c478bd9Sstevel@tonic-gate 	for (i = ROOTINO; i < maxino; i++) {
5457c478bd9Sstevel@tonic-gate 		ep = lookupino(i);
5467c478bd9Sstevel@tonic-gate 		if (ep == NIL || ep->e_type == LEAF || BIT(i, dumpmap) == 0)
5477c478bd9Sstevel@tonic-gate 			continue;
5487c478bd9Sstevel@tonic-gate 		for (np = ep->e_entries; np != NIL; np = np->e_sibling) {
5497c478bd9Sstevel@tonic-gate 			if (np->e_flags == 0) {
5507c478bd9Sstevel@tonic-gate 				dprintf(stdout, gettext(
5517c478bd9Sstevel@tonic-gate 				    "%s: remove unreferenced name\n"),
5527c478bd9Sstevel@tonic-gate 				    myname(np));
5537c478bd9Sstevel@tonic-gate 				removeleaf(np);
5547c478bd9Sstevel@tonic-gate 				freeentry(np);
5557c478bd9Sstevel@tonic-gate 			}
5567c478bd9Sstevel@tonic-gate 		}
5577c478bd9Sstevel@tonic-gate 	}
5587c478bd9Sstevel@tonic-gate 	/*
5597c478bd9Sstevel@tonic-gate 	 * Any leaves remaining in removed directories are unreferenced.
5607c478bd9Sstevel@tonic-gate 	 */
5617c478bd9Sstevel@tonic-gate 	for (ep = removelist; ep != NIL; ep = ep->e_next) {
5627c478bd9Sstevel@tonic-gate 		for (np = ep->e_entries; np != NIL; np = np->e_sibling) {
5637c478bd9Sstevel@tonic-gate 			if (np->e_type == LEAF) {
5647c478bd9Sstevel@tonic-gate 				if (np->e_flags != 0)
5657c478bd9Sstevel@tonic-gate 					badentry(np, gettext(
5667c478bd9Sstevel@tonic-gate 						"unreferenced with flags"));
5677c478bd9Sstevel@tonic-gate 				dprintf(stdout, gettext(
5687c478bd9Sstevel@tonic-gate 				    "%s: remove unreferenced name\n"),
5697c478bd9Sstevel@tonic-gate 				    myname(np));
5707c478bd9Sstevel@tonic-gate 				removeleaf(np);
5717c478bd9Sstevel@tonic-gate 				freeentry(np);
5727c478bd9Sstevel@tonic-gate 			}
5737c478bd9Sstevel@tonic-gate 		}
5747c478bd9Sstevel@tonic-gate 	}
5757c478bd9Sstevel@tonic-gate }
5767c478bd9Sstevel@tonic-gate 
5777c478bd9Sstevel@tonic-gate /*
5787c478bd9Sstevel@tonic-gate  * Remove old nodes (directories).
5797c478bd9Sstevel@tonic-gate  * Note that this routine runs in O(N*D) where:
5807c478bd9Sstevel@tonic-gate  *	N is the number of directory entries to be removed.
5817c478bd9Sstevel@tonic-gate  *	D is the maximum depth of the tree.
5827c478bd9Sstevel@tonic-gate  * If N == D this can be quite slow. If the list were
5837c478bd9Sstevel@tonic-gate  * topologically sorted, the deletion could be done in
5847c478bd9Sstevel@tonic-gate  * time O(N).
5857c478bd9Sstevel@tonic-gate  */
5867c478bd9Sstevel@tonic-gate void
5877c478bd9Sstevel@tonic-gate #ifdef __STDC__
5887c478bd9Sstevel@tonic-gate removeoldnodes(void)
5897c478bd9Sstevel@tonic-gate #else
5907c478bd9Sstevel@tonic-gate removeoldnodes()
5917c478bd9Sstevel@tonic-gate #endif
5927c478bd9Sstevel@tonic-gate {
5937c478bd9Sstevel@tonic-gate 	struct entry *ep, **prev;
5947c478bd9Sstevel@tonic-gate 	long change;
5957c478bd9Sstevel@tonic-gate 
5967c478bd9Sstevel@tonic-gate 	vprintf(stdout, gettext("Remove old nodes (directories).\n"));
5977c478bd9Sstevel@tonic-gate 	do	{
5987c478bd9Sstevel@tonic-gate 		change = 0;
5997c478bd9Sstevel@tonic-gate 		prev = &removelist;
6007c478bd9Sstevel@tonic-gate 		for (ep = removelist; ep != NIL; ep = *prev) {
6017c478bd9Sstevel@tonic-gate 			if (ep->e_entries != NIL) {
6027c478bd9Sstevel@tonic-gate 				prev = &ep->e_next;
6037c478bd9Sstevel@tonic-gate 				continue;
6047c478bd9Sstevel@tonic-gate 			}
6057c478bd9Sstevel@tonic-gate 			*prev = ep->e_next;
6067c478bd9Sstevel@tonic-gate 			removenode(ep);
6077c478bd9Sstevel@tonic-gate 			freeentry(ep);
6087c478bd9Sstevel@tonic-gate 			change++;
6097c478bd9Sstevel@tonic-gate 		}
6107c478bd9Sstevel@tonic-gate 	} while (change);
6117c478bd9Sstevel@tonic-gate 	for (ep = removelist; ep != NIL; ep = ep->e_next)
6127c478bd9Sstevel@tonic-gate 		badentry(ep, gettext("cannot remove, non-empty"));
6137c478bd9Sstevel@tonic-gate }
6147c478bd9Sstevel@tonic-gate 
6157c478bd9Sstevel@tonic-gate /*
6167c478bd9Sstevel@tonic-gate  * This is the routine used to extract files for the 'r' command.
6177c478bd9Sstevel@tonic-gate  * Extract new leaves.
6187c478bd9Sstevel@tonic-gate  */
6197c478bd9Sstevel@tonic-gate void
6207c478bd9Sstevel@tonic-gate createleaves(symtabfile)
6217c478bd9Sstevel@tonic-gate 	char *symtabfile;
6227c478bd9Sstevel@tonic-gate {
6237c478bd9Sstevel@tonic-gate 	struct entry *ep;
6247c478bd9Sstevel@tonic-gate 	char name[MAXCOMPLEXLEN];
6257c478bd9Sstevel@tonic-gate 	ino_t first;
6267c478bd9Sstevel@tonic-gate 	int curvol;
6277c478bd9Sstevel@tonic-gate 
6287c478bd9Sstevel@tonic-gate 	if (command == 'R') {
6297c478bd9Sstevel@tonic-gate 		vprintf(stdout, gettext("Continue extraction of new leaves\n"));
6307c478bd9Sstevel@tonic-gate 	} else {
6317c478bd9Sstevel@tonic-gate 		vprintf(stdout, gettext("Extract new leaves.\n"));
6327c478bd9Sstevel@tonic-gate 		dumpsymtable(symtabfile, volno);
6337c478bd9Sstevel@tonic-gate 	}
6347c478bd9Sstevel@tonic-gate 	first = lowerbnd(ROOTINO);
6357c478bd9Sstevel@tonic-gate 	curvol = volno;
6367c478bd9Sstevel@tonic-gate 	while (curfile.ino < maxino) {
6377c478bd9Sstevel@tonic-gate 		first = lowerbnd(first);
6387c478bd9Sstevel@tonic-gate 		/*
6397c478bd9Sstevel@tonic-gate 		 * If the next available file is not the one which we
6407c478bd9Sstevel@tonic-gate 		 * expect then we have missed one or more files. Since
6417c478bd9Sstevel@tonic-gate 		 * we do not request files that were not on the tape,
6427c478bd9Sstevel@tonic-gate 		 * the lost files must have been due to a tape read error,
6437c478bd9Sstevel@tonic-gate 		 * or a file that was removed while the dump was in progress.
6447c478bd9Sstevel@tonic-gate 		 *
6457c478bd9Sstevel@tonic-gate 		 * The loop will terminate with first == maxino, if not
6467c478bd9Sstevel@tonic-gate 		 * sooner.  Due to the e_flags manipulation, lowerbnd()
6477c478bd9Sstevel@tonic-gate 		 * will never return its argument.
6487c478bd9Sstevel@tonic-gate 		 */
6497c478bd9Sstevel@tonic-gate 		while (first < curfile.ino) {
6507c478bd9Sstevel@tonic-gate 			ep = lookupino(first);
6517c478bd9Sstevel@tonic-gate 			if (ep == NIL) {
6527c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
6537c478bd9Sstevel@tonic-gate 				    gettext("%d: bad first\n"), first);
6547c478bd9Sstevel@tonic-gate 				done(1);
6557c478bd9Sstevel@tonic-gate 			}
6567c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
6577c478bd9Sstevel@tonic-gate 			    gettext("%s: not found on volume\n"),
6587c478bd9Sstevel@tonic-gate 			    myname(ep));
6597c478bd9Sstevel@tonic-gate 			/* LINTED: result fits into a short */
6607c478bd9Sstevel@tonic-gate 			ep->e_flags &= ~(NEW|EXTRACT);
6617c478bd9Sstevel@tonic-gate 			first = lowerbnd(first);
6627c478bd9Sstevel@tonic-gate 		}
6637c478bd9Sstevel@tonic-gate 		/*
6647c478bd9Sstevel@tonic-gate 		 * If we find files on the tape that have no corresponding
6657c478bd9Sstevel@tonic-gate 		 * directory entries, then we must have found a file that
6667c478bd9Sstevel@tonic-gate 		 * was created while the dump was in progress. Since we have
6677c478bd9Sstevel@tonic-gate 		 * no name for it, we discard it knowing that it will be
6687c478bd9Sstevel@tonic-gate 		 * on the next incremental tape.
6697c478bd9Sstevel@tonic-gate 		 */
6707c478bd9Sstevel@tonic-gate 		if (first != curfile.ino) {
6717c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
6727c478bd9Sstevel@tonic-gate 			    gettext("expected next file %d, got %d\n"),
6737c478bd9Sstevel@tonic-gate 				first, curfile.ino);
6747c478bd9Sstevel@tonic-gate 			skipfile();
6757c478bd9Sstevel@tonic-gate 			goto next;
6767c478bd9Sstevel@tonic-gate 		}
6777c478bd9Sstevel@tonic-gate 		ep = lookupino(curfile.ino);
6787c478bd9Sstevel@tonic-gate 		if (ep == NIL) {
6797c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
6807c478bd9Sstevel@tonic-gate 			    gettext("unknown file on volume\n"));
6817c478bd9Sstevel@tonic-gate 			done(1);
6827c478bd9Sstevel@tonic-gate 		}
6837c478bd9Sstevel@tonic-gate 		if ((ep->e_flags & (NEW|EXTRACT)) == 0)
6847c478bd9Sstevel@tonic-gate 			badentry(ep, gettext("unexpected file on volume"));
6857c478bd9Sstevel@tonic-gate 		/*
6867c478bd9Sstevel@tonic-gate 		 * If the file is to be extracted, then the old file must
6877c478bd9Sstevel@tonic-gate 		 * be removed since its type may change from one leaf type
6887c478bd9Sstevel@tonic-gate 		 * to another (eg "file" to "character special"). But we
6897c478bd9Sstevel@tonic-gate 		 * also need to preserve any existing extended attributes;
6907c478bd9Sstevel@tonic-gate 		 * so first rename the file, then move its attributes, then
6917c478bd9Sstevel@tonic-gate 		 * remove it.
6927c478bd9Sstevel@tonic-gate 		 */
6937c478bd9Sstevel@tonic-gate 		if ((ep->e_flags & EXTRACT) != 0) {
6947c478bd9Sstevel@tonic-gate 			char *sname = savename(ep->e_name);
6957c478bd9Sstevel@tonic-gate 			complexcpy(name, myname(ep), MAXCOMPLEXLEN);
6967c478bd9Sstevel@tonic-gate 			mktempname(ep);
6977c478bd9Sstevel@tonic-gate 			(void) extractfile(name);
6987c478bd9Sstevel@tonic-gate 			movexattrs(myname(ep), name);
6997c478bd9Sstevel@tonic-gate 			removeleaf(ep);
7007c478bd9Sstevel@tonic-gate 			freename(ep->e_name);
7017c478bd9Sstevel@tonic-gate 			ep->e_name = sname;
7027c478bd9Sstevel@tonic-gate 			ep->e_namlen = strlen(ep->e_name);
7037c478bd9Sstevel@tonic-gate 			/* LINTED: result fits into a short */
7047c478bd9Sstevel@tonic-gate 			ep->e_flags &= ~REMOVED;
7057c478bd9Sstevel@tonic-gate 		} else {
7067c478bd9Sstevel@tonic-gate 			(void) extractfile(myname(ep));
7077c478bd9Sstevel@tonic-gate 		}
7087c478bd9Sstevel@tonic-gate 		/* LINTED: result fits into a short */
7097c478bd9Sstevel@tonic-gate 		ep->e_flags &= ~(NEW|EXTRACT);
7107c478bd9Sstevel@tonic-gate 		/*
7117c478bd9Sstevel@tonic-gate 		 * We checkpoint the restore after every tape reel, so
7127c478bd9Sstevel@tonic-gate 		 * as to simplify the amount of work required by the
7137c478bd9Sstevel@tonic-gate 		 * 'R' command.
7147c478bd9Sstevel@tonic-gate 		 */
7157c478bd9Sstevel@tonic-gate 	next:
7167c478bd9Sstevel@tonic-gate 		if (curvol != volno) {
7177c478bd9Sstevel@tonic-gate 			dumpsymtable(symtabfile, volno);
7187c478bd9Sstevel@tonic-gate 			skipmaps();
7197c478bd9Sstevel@tonic-gate 			curvol = volno;
7207c478bd9Sstevel@tonic-gate 		}
7217c478bd9Sstevel@tonic-gate 	}
7227c478bd9Sstevel@tonic-gate }
7237c478bd9Sstevel@tonic-gate 
7247c478bd9Sstevel@tonic-gate /*
7257c478bd9Sstevel@tonic-gate  * This is the routine used to extract files for the 'x' and 'i' commands.
7267c478bd9Sstevel@tonic-gate  * Efficiently extract a subset of the files on a tape.
7277c478bd9Sstevel@tonic-gate  */
7287c478bd9Sstevel@tonic-gate void
7297c478bd9Sstevel@tonic-gate #ifdef __STDC__
7307c478bd9Sstevel@tonic-gate createfiles(void)
7317c478bd9Sstevel@tonic-gate #else
7327c478bd9Sstevel@tonic-gate createfiles()
7337c478bd9Sstevel@tonic-gate #endif
7347c478bd9Sstevel@tonic-gate {
7357c478bd9Sstevel@tonic-gate 	ino_t first, next, last;
7367c478bd9Sstevel@tonic-gate 	struct entry *ep;
7377c478bd9Sstevel@tonic-gate 	int curvol, nextvol;
7387c478bd9Sstevel@tonic-gate 
7397c478bd9Sstevel@tonic-gate 	vprintf(stdout, gettext("Extract requested files\n"));
7407c478bd9Sstevel@tonic-gate 	first = lowerbnd(ROOTINO);
7417c478bd9Sstevel@tonic-gate 	last = upperbnd(maxino - 1);
7427c478bd9Sstevel@tonic-gate 	nextvol = volnumber(first);
7437c478bd9Sstevel@tonic-gate 	if (nextvol == 0) {
7447c478bd9Sstevel@tonic-gate 		curfile.action = SKIP;
7457c478bd9Sstevel@tonic-gate 		getvol(1);
7467c478bd9Sstevel@tonic-gate 		skipmaps();
7477c478bd9Sstevel@tonic-gate 		skipdirs();
7487c478bd9Sstevel@tonic-gate 	}
7497c478bd9Sstevel@tonic-gate 	for (;;) {
7507c478bd9Sstevel@tonic-gate 		first = lowerbnd(first);
7517c478bd9Sstevel@tonic-gate 		last = upperbnd(last);
7527c478bd9Sstevel@tonic-gate 		/*
7537c478bd9Sstevel@tonic-gate 		 * Check to see if any files remain to be extracted
7547c478bd9Sstevel@tonic-gate 		 */
7557c478bd9Sstevel@tonic-gate 		if (first > last)
7567c478bd9Sstevel@tonic-gate 			return;
7577c478bd9Sstevel@tonic-gate 		/*
7587c478bd9Sstevel@tonic-gate 		 * If a map of inode numbers to tape volumes is
7597c478bd9Sstevel@tonic-gate 		 * available, then select the next volume to be read.
7607c478bd9Sstevel@tonic-gate 		 */
7617c478bd9Sstevel@tonic-gate 		if (nextvol > 0) {
7627c478bd9Sstevel@tonic-gate 			nextvol = volnumber(first);
7637c478bd9Sstevel@tonic-gate 			if (nextvol != volno) {
7647c478bd9Sstevel@tonic-gate 				curfile.action = UNKNOWN;
7657c478bd9Sstevel@tonic-gate 				getvol(nextvol);
7667c478bd9Sstevel@tonic-gate 				skipmaps();
7677c478bd9Sstevel@tonic-gate 			}
7687c478bd9Sstevel@tonic-gate 		}
7697c478bd9Sstevel@tonic-gate 		/*
7707c478bd9Sstevel@tonic-gate 		 * Reject any volumes with inodes greater than
7717c478bd9Sstevel@tonic-gate 		 * the last one needed. This will only be true
7727c478bd9Sstevel@tonic-gate 		 * if the above code has not selected a volume.
7737c478bd9Sstevel@tonic-gate 		 */
7747c478bd9Sstevel@tonic-gate 		while (curfile.ino > last) {
7757c478bd9Sstevel@tonic-gate 			curfile.action = SKIP;
7767c478bd9Sstevel@tonic-gate 			getvol(0);
7777c478bd9Sstevel@tonic-gate 			skipmaps();
7787c478bd9Sstevel@tonic-gate 			skipdirs();
7797c478bd9Sstevel@tonic-gate 		}
7807c478bd9Sstevel@tonic-gate 		/*
7817c478bd9Sstevel@tonic-gate 		 * Decide on the next inode needed.
7827c478bd9Sstevel@tonic-gate 		 * Skip across the inodes until it is found
7837c478bd9Sstevel@tonic-gate 		 * or an out of order volume change is encountered
7847c478bd9Sstevel@tonic-gate 		 */
7857c478bd9Sstevel@tonic-gate 		next = lowerbnd(curfile.ino);
7867c478bd9Sstevel@tonic-gate 		do	{
7877c478bd9Sstevel@tonic-gate 			curvol = volno;
7887c478bd9Sstevel@tonic-gate 			while (next > curfile.ino && volno == curvol)
7897c478bd9Sstevel@tonic-gate 				skipfile();
7907c478bd9Sstevel@tonic-gate 			skipmaps();
7917c478bd9Sstevel@tonic-gate 			skipdirs();
7927c478bd9Sstevel@tonic-gate 		} while (volno == curvol + 1);
7937c478bd9Sstevel@tonic-gate 		/*
7947c478bd9Sstevel@tonic-gate 		 * If volume change out of order occurred the
7957c478bd9Sstevel@tonic-gate 		 * current state must be recalculated
7967c478bd9Sstevel@tonic-gate 		 */
7977c478bd9Sstevel@tonic-gate 		if (volno != curvol)
7987c478bd9Sstevel@tonic-gate 			continue;
7997c478bd9Sstevel@tonic-gate 		/*
8007c478bd9Sstevel@tonic-gate 		 * If the current inode is greater than the one we were
8017c478bd9Sstevel@tonic-gate 		 * looking for then we missed the one we were looking for.
8027c478bd9Sstevel@tonic-gate 		 * Since we only attempt to extract files listed in the
8037c478bd9Sstevel@tonic-gate 		 * dump map, the lost files must have been due to a tape
8047c478bd9Sstevel@tonic-gate 		 * read error, or a file that was removed while the dump
8057c478bd9Sstevel@tonic-gate 		 * was in progress. Thus we report all requested files
8067c478bd9Sstevel@tonic-gate 		 * between the one we were looking for, and the one we
8077c478bd9Sstevel@tonic-gate 		 * found as missing, and delete their request flags.
8087c478bd9Sstevel@tonic-gate 		 */
8097c478bd9Sstevel@tonic-gate 		while (next < curfile.ino) {
8107c478bd9Sstevel@tonic-gate 			ep = lookupino(next);
8117c478bd9Sstevel@tonic-gate 			if (ep == NIL) {
8127c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
8137c478bd9Sstevel@tonic-gate 				    gettext("corrupted symbol table\n"));
8147c478bd9Sstevel@tonic-gate 				done(1);
8157c478bd9Sstevel@tonic-gate 			}
8167c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
8177c478bd9Sstevel@tonic-gate 			    gettext("%s: not found on volume\n"),
8187c478bd9Sstevel@tonic-gate 			    myname(ep));
8197c478bd9Sstevel@tonic-gate 			/* LINTED: result fits into a short */
8207c478bd9Sstevel@tonic-gate 			ep->e_flags &= ~NEW;
8217c478bd9Sstevel@tonic-gate 			next = lowerbnd(next);
8227c478bd9Sstevel@tonic-gate 		}
8237c478bd9Sstevel@tonic-gate 		/*
8247c478bd9Sstevel@tonic-gate 		 * The current inode is the one that we are looking for,
8257c478bd9Sstevel@tonic-gate 		 * so extract it per its requested name.
8267c478bd9Sstevel@tonic-gate 		 */
8277c478bd9Sstevel@tonic-gate 		if (next == curfile.ino && next <= last) {
8287c478bd9Sstevel@tonic-gate 			ep = lookupino(next);
8297c478bd9Sstevel@tonic-gate 			if (ep == NIL) {
8307c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
8317c478bd9Sstevel@tonic-gate 				    gettext("corrupted symbol table\n"));
8327c478bd9Sstevel@tonic-gate 				done(1);
8337c478bd9Sstevel@tonic-gate 			}
8347c478bd9Sstevel@tonic-gate 			(void) extractfile(myname(ep));
8357c478bd9Sstevel@tonic-gate 			/* LINTED: result fits into a short */
8367c478bd9Sstevel@tonic-gate 			ep->e_flags &= ~NEW;
8377c478bd9Sstevel@tonic-gate 			if (volno != curvol)
8387c478bd9Sstevel@tonic-gate 				skipmaps();
8397c478bd9Sstevel@tonic-gate 		}
8407c478bd9Sstevel@tonic-gate 	}
8417c478bd9Sstevel@tonic-gate }
8427c478bd9Sstevel@tonic-gate 
8437c478bd9Sstevel@tonic-gate /*
8447c478bd9Sstevel@tonic-gate  * Add links.
8457c478bd9Sstevel@tonic-gate  */
8467c478bd9Sstevel@tonic-gate void
8477c478bd9Sstevel@tonic-gate #ifdef __STDC__
8487c478bd9Sstevel@tonic-gate createlinks(void)
8497c478bd9Sstevel@tonic-gate #else
8507c478bd9Sstevel@tonic-gate createlinks()
8517c478bd9Sstevel@tonic-gate #endif
8527c478bd9Sstevel@tonic-gate {
8537c478bd9Sstevel@tonic-gate 	struct entry *np, *ep;
8547c478bd9Sstevel@tonic-gate 	ino_t i;
8557c478bd9Sstevel@tonic-gate 	int dfd;
8567c478bd9Sstevel@tonic-gate 	char *to, *from;
8577c478bd9Sstevel@tonic-gate 	int saverr;
8587c478bd9Sstevel@tonic-gate 
8597c478bd9Sstevel@tonic-gate 	vprintf(stdout, gettext("Add links\n"));
8607c478bd9Sstevel@tonic-gate 	for (i = ROOTINO; i < maxino; i++) {
8617c478bd9Sstevel@tonic-gate 		ep = lookupino(i);
8627c478bd9Sstevel@tonic-gate 		if (ep == NIL)
8637c478bd9Sstevel@tonic-gate 			continue;
8647c478bd9Sstevel@tonic-gate 		to = savename(myname(ep));
8657c478bd9Sstevel@tonic-gate 		for (np = ep->e_links; np != NIL; np = np->e_links) {
8667c478bd9Sstevel@tonic-gate 			if ((np->e_flags & NEW) == 0)
8677c478bd9Sstevel@tonic-gate 				continue;
8687c478bd9Sstevel@tonic-gate 			resolve(myname(np), &dfd, &from);
8697c478bd9Sstevel@tonic-gate 			if (dfd != AT_FDCWD) {
8707c478bd9Sstevel@tonic-gate 				if (fchdir(dfd) < 0) {
8717c478bd9Sstevel@tonic-gate 					saverr = errno;
8727c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
8737c478bd9Sstevel@tonic-gate 					gettext("%s->%s: link failed: %s\n"),
8747c478bd9Sstevel@tonic-gate 						from, to, strerror(saverr));
8757c478bd9Sstevel@tonic-gate 					(void) close(dfd);
8767c478bd9Sstevel@tonic-gate 					continue;
8777c478bd9Sstevel@tonic-gate 				}
8787c478bd9Sstevel@tonic-gate 			}
8797c478bd9Sstevel@tonic-gate 			if (ep->e_type == NODE) {
8807c478bd9Sstevel@tonic-gate 				(void) lf_linkit(to, from, SYMLINK);
8817c478bd9Sstevel@tonic-gate 			} else {
8827c478bd9Sstevel@tonic-gate 				(void) lf_linkit(to, from, HARDLINK);
8837c478bd9Sstevel@tonic-gate 			}
8847c478bd9Sstevel@tonic-gate 			/* LINTED: result fits into a short */
8857c478bd9Sstevel@tonic-gate 			np->e_flags &= ~NEW;
8867c478bd9Sstevel@tonic-gate 			if (dfd != AT_FDCWD) {
8877c478bd9Sstevel@tonic-gate 				fchdir(savepwd);
8887c478bd9Sstevel@tonic-gate 				(void) close(dfd);
8897c478bd9Sstevel@tonic-gate 			}
8907c478bd9Sstevel@tonic-gate 		}
8917c478bd9Sstevel@tonic-gate 		freename(to);
8927c478bd9Sstevel@tonic-gate 	}
8937c478bd9Sstevel@tonic-gate }
8947c478bd9Sstevel@tonic-gate 
8957c478bd9Sstevel@tonic-gate /*
8967c478bd9Sstevel@tonic-gate  * Check the symbol table.
8977c478bd9Sstevel@tonic-gate  * We do this to insure that all the requested work was done, and
8987c478bd9Sstevel@tonic-gate  * that no temporary names remain.
8997c478bd9Sstevel@tonic-gate  */
9007c478bd9Sstevel@tonic-gate void
9017c478bd9Sstevel@tonic-gate #ifdef __STDC__
9027c478bd9Sstevel@tonic-gate checkrestore(void)
9037c478bd9Sstevel@tonic-gate #else
9047c478bd9Sstevel@tonic-gate checkrestore()
9057c478bd9Sstevel@tonic-gate #endif
9067c478bd9Sstevel@tonic-gate {
9077c478bd9Sstevel@tonic-gate 	struct entry *ep;
9087c478bd9Sstevel@tonic-gate 	ino_t i;
9097c478bd9Sstevel@tonic-gate 
9107c478bd9Sstevel@tonic-gate 	vprintf(stdout, gettext("Check the symbol table.\n"));
9117c478bd9Sstevel@tonic-gate 	for (i = ROOTINO; i < maxino; i++) {
9127c478bd9Sstevel@tonic-gate 		for (ep = lookupino(i); ep != NIL; ep = ep->e_links) {
9137c478bd9Sstevel@tonic-gate 			/* LINTED: result fits into a short */
9147c478bd9Sstevel@tonic-gate 			ep->e_flags &= ~KEEP;
9157c478bd9Sstevel@tonic-gate 			if (ep->e_type == NODE) {
9167c478bd9Sstevel@tonic-gate 				/* LINTED: result fits into a short */
9177c478bd9Sstevel@tonic-gate 				ep->e_flags &= ~(NEW|EXISTED);
9187c478bd9Sstevel@tonic-gate 			}
9197c478bd9Sstevel@tonic-gate 			if ((ep->e_flags & ~(XATTR|XATTRROOT)) != 0)
9207c478bd9Sstevel@tonic-gate 				badentry(ep, gettext("incomplete operations"));
9217c478bd9Sstevel@tonic-gate 		}
9227c478bd9Sstevel@tonic-gate 	}
9237c478bd9Sstevel@tonic-gate }
9247c478bd9Sstevel@tonic-gate 
9257c478bd9Sstevel@tonic-gate /*
9267c478bd9Sstevel@tonic-gate  * Compare with the directory structure on the tape
9277c478bd9Sstevel@tonic-gate  * A paranoid check that things are as they should be.
9287c478bd9Sstevel@tonic-gate  */
9297c478bd9Sstevel@tonic-gate long
9307c478bd9Sstevel@tonic-gate verifyfile(name, ino, type)
9317c478bd9Sstevel@tonic-gate 	char *name;
9327c478bd9Sstevel@tonic-gate 	ino_t ino;
9337c478bd9Sstevel@tonic-gate 	int type;
9347c478bd9Sstevel@tonic-gate {
9357c478bd9Sstevel@tonic-gate 	struct entry *np, *ep;
9367c478bd9Sstevel@tonic-gate 	long descend = GOOD;
9377c478bd9Sstevel@tonic-gate 
9387c478bd9Sstevel@tonic-gate 	ep = lookupname(name);
9397c478bd9Sstevel@tonic-gate 	if (ep == NIL) {
9407c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
9417c478bd9Sstevel@tonic-gate 		    gettext("Warning: missing name %s\n"), name);
9427c478bd9Sstevel@tonic-gate 		return (FAIL);
9437c478bd9Sstevel@tonic-gate 	}
9447c478bd9Sstevel@tonic-gate 	np = lookupino(ino);
9457c478bd9Sstevel@tonic-gate 	if (np != ep)
9467c478bd9Sstevel@tonic-gate 		descend = FAIL;
9477c478bd9Sstevel@tonic-gate 	for (; np != NIL; np = np->e_links)
9487c478bd9Sstevel@tonic-gate 		if (np == ep)
9497c478bd9Sstevel@tonic-gate 			break;
9507c478bd9Sstevel@tonic-gate 	if (np == NIL) {
9517c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("missing inumber %d\n"), ino);
9527c478bd9Sstevel@tonic-gate 		done(1);
9537c478bd9Sstevel@tonic-gate 	}
9547c478bd9Sstevel@tonic-gate 	if (ep->e_type == LEAF && type != LEAF)
9557c478bd9Sstevel@tonic-gate 		badentry(ep, gettext("type should be LEAF"));
9567c478bd9Sstevel@tonic-gate 	return (descend);
9577c478bd9Sstevel@tonic-gate }
9587c478bd9Sstevel@tonic-gate 
9597c478bd9Sstevel@tonic-gate /*
9607c478bd9Sstevel@tonic-gate  * This routine does not actually remove any attribute files, it
9617c478bd9Sstevel@tonic-gate  * just removes entries from the symbol table.  The attribute files
9627c478bd9Sstevel@tonic-gate  * themselves are assumed to be removed automatically when the
9637c478bd9Sstevel@tonic-gate  * parent file is removed.
9647c478bd9Sstevel@tonic-gate  */
9657c478bd9Sstevel@tonic-gate static void
9667c478bd9Sstevel@tonic-gate removexattrs(ep)
9677c478bd9Sstevel@tonic-gate 	struct entry *ep;
9687c478bd9Sstevel@tonic-gate {
9697c478bd9Sstevel@tonic-gate 	struct entry *np = ep;
9707c478bd9Sstevel@tonic-gate 
9717c478bd9Sstevel@tonic-gate 	if (ep == NIL)
9727c478bd9Sstevel@tonic-gate 		return;
9737c478bd9Sstevel@tonic-gate 	for (np = ep->e_entries; np != NIL; np = np->e_sibling) {
9747c478bd9Sstevel@tonic-gate 		if (np->e_type == NODE) {
9757c478bd9Sstevel@tonic-gate 			removexattrs(np);
9767c478bd9Sstevel@tonic-gate 		} else {
9777c478bd9Sstevel@tonic-gate 			np->e_flags |= REMOVED;
9787c478bd9Sstevel@tonic-gate 			freeentry(np);
9797c478bd9Sstevel@tonic-gate 		}
9807c478bd9Sstevel@tonic-gate 	}
9817c478bd9Sstevel@tonic-gate 	ep->e_flags |= REMOVED;
9827c478bd9Sstevel@tonic-gate 	freeentry(ep);
9837c478bd9Sstevel@tonic-gate }
9847c478bd9Sstevel@tonic-gate 
9857c478bd9Sstevel@tonic-gate /*
9867c478bd9Sstevel@tonic-gate  * Move all the extended attributes associated with orig to
9877c478bd9Sstevel@tonic-gate  * the file named by the second argument (targ).
9887c478bd9Sstevel@tonic-gate  */
9897c478bd9Sstevel@tonic-gate static void
9907c478bd9Sstevel@tonic-gate movexattrs(orig, targ)
9917c478bd9Sstevel@tonic-gate 	char *orig;
9927c478bd9Sstevel@tonic-gate 	char *targ;
9937c478bd9Sstevel@tonic-gate {
9947c478bd9Sstevel@tonic-gate 	char *to, *from;
9957c478bd9Sstevel@tonic-gate 	int fromfd, fromdir, tofd, todir, tfd;
9967c478bd9Sstevel@tonic-gate 	DIR *dirp = NULL;
9977c478bd9Sstevel@tonic-gate 	struct dirent *dp = NULL;
9987c478bd9Sstevel@tonic-gate 
9997c478bd9Sstevel@tonic-gate 	fromfd = tofd = fromdir = todir = tfd = -1;
10007c478bd9Sstevel@tonic-gate 
10017c478bd9Sstevel@tonic-gate 	resolve(orig, &tfd, &from);
10027c478bd9Sstevel@tonic-gate 	if (tfd == AT_FDCWD && pathconf(orig, _PC_XATTR_EXISTS) != 1) {
10037c478bd9Sstevel@tonic-gate 		/* no attributes to move */
10047c478bd9Sstevel@tonic-gate 		return;
10057c478bd9Sstevel@tonic-gate 	}
10067c478bd9Sstevel@tonic-gate 	if ((fromfd = openat64(tfd, from, O_RDONLY|O_NONBLOCK)) == -1) {
10077c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext("%s: cannot move attributes: "), from);
10087c478bd9Sstevel@tonic-gate 		perror("");
10097c478bd9Sstevel@tonic-gate 		if (tfd != AT_FDCWD) (void) close(tfd);
10107c478bd9Sstevel@tonic-gate 		goto out;
10117c478bd9Sstevel@tonic-gate 	}
10127c478bd9Sstevel@tonic-gate 
10137c478bd9Sstevel@tonic-gate 	if (fpathconf(fromfd, _PC_XATTR_EXISTS) != 1) {
10147c478bd9Sstevel@tonic-gate 		/* no attributes to move */
10157c478bd9Sstevel@tonic-gate 		if (tfd != AT_FDCWD) (void) close(tfd);
10167c478bd9Sstevel@tonic-gate 		goto out;
10177c478bd9Sstevel@tonic-gate 	}
10187c478bd9Sstevel@tonic-gate 	if ((fromdir = openat64(fromfd, ".",
10197c478bd9Sstevel@tonic-gate 				O_RDONLY|O_NONBLOCK|O_XATTR)) == -1) {
10207c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext("%s: cannot access attributes: "),
10217c478bd9Sstevel@tonic-gate 			from);
10227c478bd9Sstevel@tonic-gate 		perror("");
10237c478bd9Sstevel@tonic-gate 		if (tfd != AT_FDCWD) (void) close(tfd);
10247c478bd9Sstevel@tonic-gate 		goto out;
10257c478bd9Sstevel@tonic-gate 	}
10267c478bd9Sstevel@tonic-gate 	if (tfd != AT_FDCWD) (void) close(tfd);
10277c478bd9Sstevel@tonic-gate 
10287c478bd9Sstevel@tonic-gate 	resolve(targ, &tfd, &to);
10297c478bd9Sstevel@tonic-gate 	if ((tofd = openat64(tfd, to, O_RDONLY|O_NONBLOCK)) == -1 ||
10307c478bd9Sstevel@tonic-gate 	    (todir = openat64(tofd, ".", O_RDONLY|O_NONBLOCK|O_XATTR)) == -1) {
10317c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext("%s: cannot create attributes: "), to);
10327c478bd9Sstevel@tonic-gate 		perror("");
10337c478bd9Sstevel@tonic-gate 		goto out;
10347c478bd9Sstevel@tonic-gate 	}
10357c478bd9Sstevel@tonic-gate 	if (tfd != AT_FDCWD) (void) close(tfd);
10367c478bd9Sstevel@tonic-gate 	(void) close(tofd);
10377c478bd9Sstevel@tonic-gate 
10387c478bd9Sstevel@tonic-gate 	if ((tfd = dup(fromdir)) == -1 ||
10397c478bd9Sstevel@tonic-gate 	    (dirp = fdopendir(tfd)) == NULL) {
10407c478bd9Sstevel@tonic-gate 		fprintf(stderr,
10417c478bd9Sstevel@tonic-gate 	gettext("%s: cannot allocate DIR structure to attribute directory: "),
10427c478bd9Sstevel@tonic-gate 			from);
10437c478bd9Sstevel@tonic-gate 		perror("");
10447c478bd9Sstevel@tonic-gate 		if (tfd != -1) (void) close(tfd);
10457c478bd9Sstevel@tonic-gate 		goto out;
10467c478bd9Sstevel@tonic-gate 	}
10477c478bd9Sstevel@tonic-gate 
10487c478bd9Sstevel@tonic-gate 	while ((dp = readdir(dirp)) != NULL) {
10497c478bd9Sstevel@tonic-gate 		if ((dp->d_name[0] == '.' && dp->d_name[1] == '\0') ||
10507c478bd9Sstevel@tonic-gate 			(dp->d_name[0] == '.' && dp->d_name[1] == '.' &&
10517c478bd9Sstevel@tonic-gate 			dp->d_name[2] == '\0'))
10527c478bd9Sstevel@tonic-gate 			continue;
10537c478bd9Sstevel@tonic-gate 		if ((renameat(fromdir, dp->d_name, todir, dp->d_name)) == -1) {
10547c478bd9Sstevel@tonic-gate 			fprintf(stderr,
10557c478bd9Sstevel@tonic-gate 				gettext("%s: cannot move attribute %s: "),
10567c478bd9Sstevel@tonic-gate 				from, dp->d_name);
10577c478bd9Sstevel@tonic-gate 			goto out;
10587c478bd9Sstevel@tonic-gate 		}
10597c478bd9Sstevel@tonic-gate 	}
10607c478bd9Sstevel@tonic-gate out:
10617c478bd9Sstevel@tonic-gate 	if (fromfd != -1)
10627c478bd9Sstevel@tonic-gate 		(void) close(fromfd);
10637c478bd9Sstevel@tonic-gate 	if (tofd != -1)
10647c478bd9Sstevel@tonic-gate 		(void) close(tofd);
10657c478bd9Sstevel@tonic-gate 	if (dirp != NULL)
10667c478bd9Sstevel@tonic-gate 		(void) closedir(dirp);
10677c478bd9Sstevel@tonic-gate 	if (fromdir != -1)
10687c478bd9Sstevel@tonic-gate 		(void) close(fromdir);
10697c478bd9Sstevel@tonic-gate 	if (todir != -1)
10707c478bd9Sstevel@tonic-gate 		(void) close(todir);
10717c478bd9Sstevel@tonic-gate }
1072