oname[PAXPATHLEN] = '\0';
static DEVT *chk_dev __P((dev_t, int));/* * hard link table routines * * The hard link table tries to detect hard links to files using the device and  * inode values. We do this when writing an archive, so we can tell the format * write routine that this file is a hard link to another file. The format * write routine then can store this file in whatever way it wants (as a hard * link if the format supports that like tar, or ignore this info like cpio). * (Actually a field in the format driver table tells us if the format wants * hard link info. if not, we do not waste time looking for them). We also use * the same table when reading an archive. In that situation, this table is * used by the format read routine to detect hard links from stored dev and * inode numbers (like cpio). This will allow pax to create a link when one * can be detected by the archive format. *//* * lnk_start *	Creates the hard link table. * Return: *	0 if created, -1 if failure */#if __STDC__intlnk_start(void)#elseintlnk_start()#endif{	if (ltab != NULL)		return(0); 	if ((ltab = (HRDLNK **)calloc(L_TAB_SZ, sizeof(HRDLNK *))) == NULL) {                paxwarn(1, "Cannot allocate memory for hard link table");                return(-1);        }	return(0);}/* * chk_lnk() *	Looks up entry in hard link hash table. If found, it copies the name *	of the file it is linked to (we already saw that file) into ln_name. *	lnkcnt is decremented and if goes to 1 the node is deleted from the *	database. (We have seen all the links to this file). If not found, *	we add the file to the database if it has the potential for having *	hard links to other files we may process (it has a link count > 1) * Return: *	if found returns 1; if not found returns 0; -1 on error */#if __STDC__intchk_lnk(register ARCHD *arcn)#elseintchk_lnk(arcn)	register ARCHD *arcn;#endif{	register HRDLNK *pt;	register HRDLNK **ppt;	register u_int indx;	if (ltab == NULL)		return(-1);	/*	 * ignore those nodes that cannot have hard links	 */	if ((arcn->type == PAX_DIR) || (arcn->sb.st_nlink <= 1))		return(0);	/*	 * hash inode number and look for this file	 */	indx = ((unsigned)arcn->sb.st_ino) % L_TAB_SZ;	if ((pt = ltab[indx]) != NULL) {		/*		 * it's hash chain in not empty, walk down looking for it		 */		ppt = &(ltab[indx]);		while (pt != NULL) {			if ((pt->ino == arcn->sb.st_ino) &&			    (pt->dev == arcn->sb.st_dev))				break;			ppt = &(pt->fow);			pt = pt->fow;		}		if (pt != NULL) {			/*			 * found a link. set the node type and copy in the			 * name of the file it is to link to. we need to			 * handle hardlinks to regular files differently than			 * other links.			 */			arcn->ln_nlen = l_strncpy(arcn->ln_name, pt->name,				PAXPATHLEN+1);			if (arcn->type == PAX_REG)				arcn->type = PAX_HRG;			else				arcn->type = PAX_HLK;			/*			 * if we have found all the links to this file, remove			 * it from the database			 */			if (--pt->nlink <= 1) {				*ppt = pt->fow;				(void)free((char *)pt->name);				(void)free((char *)pt);			}			return(1);		}	}	/*	 * we never saw this file before. It has links so we add it to the	 * front of this hash chain	 */	if ((pt = (HRDLNK *)malloc(sizeof(HRDLNK))) != NULL) {		if ((pt->name = strdup(arcn->name)) != NULL) {			pt->dev = arcn->sb.st_dev;			pt->ino = arcn->sb.st_ino;			pt->nlink = arcn->sb.st_nlink;			pt->fow = ltab[indx];			ltab[indx] = pt;			return(0);		}		(void)free((char *)pt);	}	paxwarn(1, "Hard link table out of memory");	return(-1);}/* * purg_lnk *	remove reference for a file that we may have added to the data base as *	a potential source for hard links. We ended up not using the file, so *	we do not want to accidently point another file at it later on. */#if __STDC__voidpurg_lnk(register ARCHD *arcn)#elsevoidpurg_lnk(arcn)	register ARCHD *arcn;#endif{	register HRDLNK *pt;	register HRDLNK **ppt;	register u_int indx;	if (ltab == NULL)		return;	/*	 * do not bother to look if it could not be in the database	 */	if ((arcn->sb.st_nlink <= 1) || (arcn->type == PAX_DIR) ||	    (arcn->type == PAX_HLK) || (arcn->type == PAX_HRG))		return;	/*	 * find the hash chain for this inode value, if empty return	 */	indx = ((unsigned)arcn->sb.st_ino) % L_TAB_SZ;	if ((pt = ltab[indx]) == NULL)		return;	/*	 * walk down the list looking for the inode/dev pair, unlink and	 * free if found	 */	ppt = &(ltab[indx]);	while (pt != NULL) {		if ((pt->ino == arcn->sb.st_ino) &&		    (pt->dev == arcn->sb.st_dev))			break;		ppt = &(pt->fow);		pt = pt->fow;	}	if (pt == NULL)		return;	/*	 * remove and free it	 */	*ppt = pt->fow;	(void)free((char *)pt->name);	(void)free((char *)pt);}/* * lnk_end() *	pull apart a existing link table so we can reuse it. We do this between *	read and write phases of append with update. (The format may have *	used the link table, and we need to start with a fresh table for the *	write phase */#if __STDC__voidlnk_end(void)#elsevoidlnk_end()#endif{	register int i;	register HRDLNK *pt;	register HRDLNK *ppt;	if (ltab == NULL)		return;	for (i = 0; i < L_TAB_SZ; ++i) {		if (ltab[i] == NULL)			continue;		pt = ltab[i];		ltab[i] = NULL;		/*		 * free up each entry on this chain		 */		while (pt != NULL) {			ppt = pt;			pt = ppt->fow;			(void)free((char *)ppt->name);			(void)free((char *)ppt);		}	}	return;}/* * modification time table routines * * The modification time table keeps track of last modification times for all * files stored in an archive during a write phase when -u is set. We only * add a file to the archive if it is newer than a file with the same name * already stored on the archive (if there is no other file with the same * name on the archive it is added). This applies to writes and appends. * An append with an -u must read the archive and store the modification time * for every file on that archive before starting the write phase. It is clear * that this is one HUGE database. To save memory space, the actual file names * are stored in a scatch file and indexed by an in memory hash table. The * hash table is indexed by hashing the file path. The nodes in the table store * the length of the filename and the lseek offset within the scratch file  * where the actual name is stored. Since there are never any deletions to this * table, fragmentation of the scratch file is never a issue. Lookups seem to * not exhibit any locality at all (files in the database are rarely  * looked up more than once...). So caching is just a waste of memory. The * only limitation is the amount of scatch file space available to store the * path names. *//* * ftime_start() *	create the file time hash table and open for read/write the scratch *	file. (after created it is unlinked, so when we exit we leave *	no witnesses). * Return: *	0 if the table and file was created ok, -1 otherwise */#if __STDC__intftime_start(void)#elseintftime_start()#endif{	char *pt;	if (ftab != NULL)		return(0); 	if ((ftab = (FTM **)calloc(F_TAB_SZ, sizeof(FTM *))) == NULL) {                paxwarn(1, "Cannot allocate memory for file time table");                return(-1);        }	/*	 * get random name and create temporary scratch file, unlink name	 * so it will get removed on exit	 */	if ((pt = tempnam((char *)NULL, (char *)NULL)) == NULL)		return(-1);	(void)unlink(pt);	if ((ffd = open(pt, O_RDWR | O_CREAT,  S_IRWXU)) < 0) {		syswarn(1, errno, "Unable to open temporary file: %s", pt);		return(-1);	}	(void)unlink(pt);	return(0);}/* * chk_ftime() *	looks up entry in file time hash table. If not found, the file is *	added to the hash table and the file named stored in the scratch file. *	If a file with the same name is found, the file times are compared and *	the most recent file time is retained. If the new file was younger (or *	was not in the database) the new file is selected for storage. * Return: *	0 if file should be added to the archive, 1 if it should be skipped, *	-1 on error */#if __STDC__intchk_ftime(register ARCHD *arcn)#elseintchk_ftime(arcn)	register ARCHD *arcn;#endif{	register FTM *pt;	register int namelen;	register u_int indx;	char ckname[PAXPATHLEN+1];	/*	 * no info, go ahead and add to archive	 */	if (ftab == NULL)		return(0);	/*	 * hash the pathname and look up in table	 */	namelen = arcn->nlen;	indx = st_hash(arcn->name, namelen, F_TAB_SZ);	if ((pt = ftab[indx]) != NULL) {		/*		 * the hash chain is not empty, walk down looking for match		 * only read up the path names if the lengths match, speeds		 * up the search a lot		 */		while (pt != NULL) {			if (pt->namelen == namelen) {				/*				 * potential match, have to read the name				 * from the scratch file.				 */				if (lseek(ffd,pt->seek,SEEK_SET) != pt->seek) {					syswarn(1, errno,					    "Failed ftime table seek");					return(-1);				}				if (read(ffd, ckname, namelen) != namelen) {					syswarn(1, errno,					    "Failed ftime table read");					return(-1);				}				/*				 * if the names match, we are done				 */				if (!strncmp(ckname, arcn->name, namelen))					break;			}			/*			 * try the next entry on the chain			 */			pt = pt->fow;		}		if (pt != NULL) {			/*			 * found the file, compare the times, save the newer			 */			if (arcn->sb.st_mtime > pt->mtime) {				/*				 * file is newer				 */				pt->mtime = arcn->sb.st_mtime;				return(0);			} 			/*			 * file is older			 */			return(1);		}	}	/*	 * not in table, add it	 */	if ((pt = (FTM *)malloc(sizeof(FTM))) != NULL) {		/*		 * add the name at the end of the scratch file, saving the		 * offset. add the file to the head of the hash chain		 */		if ((pt->seek = lseek(ffd, (off_t)0, SEEK_END)) >= 0) {			if (write(ffd, arcn->name, namelen) == namelen) {				pt->mtime = arcn->sb.st_mtime;				pt->namelen = namelen;				pt->fow = ftab[indx];				ftab[indx] = pt;				return(0);			}			syswarn(1, errno, "Failed write to file time table");		} else 			syswarn(1, errno, "Failed seek on file time table");	} else		paxwarn(1, "File time table ran out of memory");	if (pt != NULL)		(void)free((char *)pt);	return(-1);}/* * Interactive rename table routines * * The interactive rename table keeps track of the new names that the user * assignes to files from tty input. Since this map is unique for each file * we must store it in case there is a reference to the file later in archive * (a link). Otherwise we will be unable to find the file we know was * extracted. The remapping of these files is stored in a memory based hash * table (it is assumed since input must come from /dev/tty, it is unlikely to * be a very large table). *//* * name_start() *	create the interactive rename table * Return: *	0 if successful, -1 otherwise */#if __STDC__intname_start(void)#elseintname_start()#endif{	if (ntab != NULL)		return(0); 	if ((ntab = (NAMT **)calloc(N_TAB_SZ, sizeof(NAMT *))) == NULL) {                paxwarn(1, "Cannot allocate memory for interactive rename table");                return(-1);        }	return(0);}/* * add_name() *	add the new name to old name mapping just created by the user. *	If an old name mapping is found (there may be duplicate names on an *	archive) only the most recent is kept. * Return: *	0 if added, -1 otherwise */#if __STDC__intadd_name(register char *oname, int onamelen, char *nname)#elseintadd_name(oname, onamelen, nname)	register char *oname;	int onamelen;	char *nname;#endif{	register NAMT *pt;	register u_int indx;	if (ntab == NULL) {		/*		 * should never happen		 */		paxwarn(0, "No interactive rename table, links may fail\n");		return(0); 	}	/*	 * look to see if we have already mapped this file, if so we	 * will update it	 */	indx = st_hash(oname, onamelen, N_TAB_SZ);	if ((pt = ntab[indx]) != NULL) {		/*		 * look down the has chain for the file		 */		while ((pt != NULL) && (strcmp(oname, pt->oname) != 0))			pt = pt->fow;		if (pt != NULL) {			/*			 * found an old mapping, replace it with the new one			 * the user just input (if it is different)			 */			if (strcmp(nname, pt->nname) == 0)				return(0);			(void)free((char *)pt->nname);			if ((pt->nname = strdup(nname)) == NULL) {				paxwarn(1, "Cannot update rename table");				return(-1);			}			return(0);		}	}	/*	 * this is a new mapping, add it to the table	 */	if ((pt = (NAMT *)malloc(sizeof(NAMT))) != NULL) {		if ((pt->oname = strdup(oname)) != NULL) {			if ((pt->nname = strdup(nname)) != NULL) {				pt->fow = ntab[indx];				ntab[indx] = pt;				return(0);			}			(void)free((char *)pt->oname);		}		(void)free((char *)pt);	}	paxwarn(1, "Interactive rename table out of memory");	return(-1);}/* * sub_name() *	look up a link name to see if it points at a file that has been *	remapped by the user. If found, the link is adjusted to contain the *	new name (oname is the link to name) */#if __STDC__voidsub_name(register char *oname, int *onamelen)#elsevoidsub_name(oname, onamelen)	register char *oname;	int *onamelen;#endif{	register NAMT *pt;	register u_int indx;	if (ntab == NULL)		return;	/*	 * look the name up in the hash table	 */	indx = st_hash(oname, *onamelen, N_TAB_SZ);	if ((pt = ntab[indx]) == NULL)		return;	while (pt != NULL) {		/*		 * walk down the hash cahin looking for a match		 */		if (strcmp(oname, pt->oname) == 0) {			/*			 * found it, replace it with the new name			 * and return (we know that oname has enough space)			 */			*onamelen = l_strncpy(oname, pt->nname, PAXPATHLEN+1);			return;		}		pt = pt->fow;	}	/*	 * no match, just return	 */	return;}    /* * device/inode mapping table routines * (used with formats that store device and inodes fields) * * device/inode mapping tables remap the device field in a archive header. The * device/inode fields are used to determine when files are hard links to each * other. However these values have very little meaning outside of that. This * database is used to solve one of two different problems. * * 1) when files are appended to an archive, while the new files may have hard * links to each other, you cannot determine if they have hard links to any * file already stored on the archive from a prior run of pax. We must assume * that these inode/device pairs are unique only within a SINGLE run of pax * (which adds a set of files to an archive). So we have to make sure the * inode/dev pairs we add each time are always unique. We do this by observing * while the inode field is very dense, the use of the dev field is fairly * sparse. Within each run of pax, we remap any device number of a new archive * member that has a device number used in a prior run and already stored in a * file on the archive. During the read phase of the append, we store the * device numbers used and mark them to not be used by any file during the * write phase. If during write we go to use one of those old device numbers, * we remap it to a new value. * * 2) Often the fields in the archive header used to store these values are * too small to store the entire value. The result is an inode or device value * which can be truncated. This really can foul up an archive. With truncation * we end up creating links between files that are really not links (after * truncation the inodes are the same value). We address that by detecting * truncation and forcing a remap of the device field to split truncated * inodes away from each other. Each truncation creates a pattern of bits that * are removed. We use this pattern of truncated bits to partition the inodes * on a single device to many different devices (each one represented by the * truncated bit pattern). All inodes on the same device that have the same * truncation pattern are mapped to the same new device. Two inodes that * truncate to the same value clearly will always have different truncation * bit patterns, so they will be split from away each other. When we spot * device truncation we remap the device number to a non truncated value. * (for more info see table.h for the data structures involved). *//* * dev_start() *	create the device mapping table * Return: *	0 if successful, -1 otherwise */#if __STDC__intdev_start(void)#elseintdev_start()#endif{	if (dtab != NULL)		return(0); 	if ((dtab = (DEVT **)calloc(D_TAB_SZ, sizeof(DEVT *))) == NULL) {                paxwarn(1, "Cannot allocate memory for device mapping table");                return(-1);        }	return(0);}/* * add_dev() *	add a device number to the table. this will force the device to be *	remapped to a new value if it be used during a write phase. This *	function is called during the read phase of an append to prohibit the *	use of any device number already in the archive. * Return: *	0 if added ok, -1 otherwise */#if __STDC__intadd_dev(register ARCHD *arcn)#elseintadd_dev(arcn)	register ARCHD *arcn;#endif{	if (chk_dev(arcn->sb.st_dev, 1) == NULL)		return(-1);	return(0);}/* * chk_dev() *	check for a device value in the device table. If not found and the add *	flag is set, it is added. This does NOT assign any mapping values, just *	adds the device number as one that need to be remapped. If this device *	is alread mapped, just return with a pointer to that entry. * Return: *	pointer to the entry for this device in the device map table. Null *	if the add flag is not set and the device is not in the table (it is *	not been seen yet). If add is set and the device cannot be added, null *	is returned (indicates an error). */#if __STDC__static DEVT *chk_dev(dev_t dev, int add)#elsestatic DEVT *chk_dev(dev, add)	dev_t dev;	int add;#endif{	register DEVT *pt;	register u_int indx;	if (dtab == NULL)		return(NULL);	/*	 * look to see if this device is already in the table	 */	indx = ((unsigned)dev) % D_TAB_SZ;	if ((pt = dtab[indx]) != NULL) {		while ((pt != NULL) && (pt->dev != dev))			pt = pt->fow;		/*		 * found it, return a pointer to it		 */		if (pt != NULL)			return(pt);	}	/*	 * not in table, we add it only if told to as this may just be a check	 * to see if a device number is being used.	 */	if (add == 0)		return(NULL);	/*	 * allocate a node for this device and add it to the front of the hash	 * chain. Note we do not assign remaps values here, so the pt->list	 * list must be NULL.	 */	if ((pt = (DEVT *)malloc(sizeof(DEVT))) == NULL) {		paxwarn(1, "Device map table out of memory");		return(NULL);	}	pt->dev = dev;	pt->list = NULL;	pt->fow = dtab[indx];	dtab[indx] = pt;	return(pt);}