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