Home | History | Annotate | Line # | Download | only in pax
tables.c revision 1.21
      1 /*	$NetBSD: tables.c,v 1.21 2003/08/07 09:05:22 agc Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1992, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * This code is derived from software contributed to Berkeley by
      8  * Keith Muller of the University of California, San Diego.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. Neither the name of the University nor the names of its contributors
     19  *    may be used to endorse or promote products derived from this software
     20  *    without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  * SUCH DAMAGE.
     33  */
     34 
     35 /*-
     36  * Copyright (c) 1992 Keith Muller.
     37  *
     38  * This code is derived from software contributed to Berkeley by
     39  * Keith Muller of the University of California, San Diego.
     40  *
     41  * Redistribution and use in source and binary forms, with or without
     42  * modification, are permitted provided that the following conditions
     43  * are met:
     44  * 1. Redistributions of source code must retain the above copyright
     45  *    notice, this list of conditions and the following disclaimer.
     46  * 2. Redistributions in binary form must reproduce the above copyright
     47  *    notice, this list of conditions and the following disclaimer in the
     48  *    documentation and/or other materials provided with the distribution.
     49  * 3. All advertising materials mentioning features or use of this software
     50  *    must display the following acknowledgement:
     51  *	This product includes software developed by the University of
     52  *	California, Berkeley and its contributors.
     53  * 4. Neither the name of the University nor the names of its contributors
     54  *    may be used to endorse or promote products derived from this software
     55  *    without specific prior written permission.
     56  *
     57  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     58  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     59  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     60  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     61  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     62  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     63  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     64  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     65  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     66  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     67  * SUCH DAMAGE.
     68  */
     69 
     70 #include <sys/cdefs.h>
     71 #if defined(__RCSID) && !defined(lint)
     72 #if 0
     73 static char sccsid[] = "@(#)tables.c	8.1 (Berkeley) 5/31/93";
     74 #else
     75 __RCSID("$NetBSD: tables.c,v 1.21 2003/08/07 09:05:22 agc Exp $");
     76 #endif
     77 #endif /* not lint */
     78 
     79 #include <sys/types.h>
     80 #include <sys/time.h>
     81 #include <sys/stat.h>
     82 #include <sys/param.h>
     83 #include <stdio.h>
     84 #include <ctype.h>
     85 #include <fcntl.h>
     86 #include <paths.h>
     87 #include <string.h>
     88 #include <unistd.h>
     89 #include <errno.h>
     90 #include <stdlib.h>
     91 #include "pax.h"
     92 #include "tables.h"
     93 #include "extern.h"
     94 
     95 /*
     96  * Routines for controlling the contents of all the different databases pax
     97  * keeps. Tables are dynamically created only when they are needed. The
     98  * goal was speed and the ability to work with HUGE archives. The databases
     99  * were kept simple, but do have complex rules for when the contents change.
    100  * As of this writing, the posix library functions were more complex than
    101  * needed for this application (pax databases have very short lifetimes and
    102  * do not survive after pax is finished). Pax is required to handle very
    103  * large archives. These database routines carefully combine memory usage and
    104  * temporary file storage in ways which will not significantly impact runtime
    105  * performance while allowing the largest possible archives to be handled.
    106  * Trying to force the fit to the posix database routines was not considered
    107  * time well spent.
    108  */
    109 
    110 static HRDLNK **ltab = NULL;	/* hard link table for detecting hard links */
    111 static FTM **ftab = NULL;	/* file time table for updating arch */
    112 static NAMT **ntab = NULL;	/* interactive rename storage table */
    113 static DEVT **dtab = NULL;	/* device/inode mapping tables */
    114 static ATDIR **atab = NULL;	/* file tree directory time reset table */
    115 #ifdef DIRS_USE_FILE
    116 static int dirfd = -1;		/* storage for setting created dir time/mode */
    117 static u_long dircnt;		/* entries in dir time/mode storage */
    118 #endif
    119 static int ffd = -1;		/* tmp file for file time table name storage */
    120 
    121 static DEVT *chk_dev(dev_t, int);
    122 
    123 /*
    124  * hard link table routines
    125  *
    126  * The hard link table tries to detect hard links to files using the device and
    127  * inode values. We do this when writing an archive, so we can tell the format
    128  * write routine that this file is a hard link to another file. The format
    129  * write routine then can store this file in whatever way it wants (as a hard
    130  * link if the format supports that like tar, or ignore this info like cpio).
    131  * (Actually a field in the format driver table tells us if the format wants
    132  * hard link info. if not, we do not waste time looking for them). We also use
    133  * the same table when reading an archive. In that situation, this table is
    134  * used by the format read routine to detect hard links from stored dev and
    135  * inode numbers (like cpio). This will allow pax to create a link when one
    136  * can be detected by the archive format.
    137  */
    138 
    139 /*
    140  * lnk_start
    141  *	Creates the hard link table.
    142  * Return:
    143  *	0 if created, -1 if failure
    144  */
    145 
    146 int
    147 lnk_start(void)
    148 {
    149 	if (ltab != NULL)
    150 		return(0);
    151 	if ((ltab = (HRDLNK **)calloc(L_TAB_SZ, sizeof(HRDLNK *))) == NULL) {
    152 		tty_warn(1, "Cannot allocate memory for hard link table");
    153 		return(-1);
    154 	}
    155 	return(0);
    156 }
    157 
    158 /*
    159  * chk_lnk()
    160  *	Looks up entry in hard link hash table. If found, it copies the name
    161  *	of the file it is linked to (we already saw that file) into ln_name.
    162  *	lnkcnt is decremented and if goes to 1 the node is deleted from the
    163  *	database. (We have seen all the links to this file). If not found,
    164  *	we add the file to the database if it has the potential for having
    165  *	hard links to other files we may process (it has a link count > 1)
    166  * Return:
    167  *	if found returns 1; if not found returns 0; -1 on error
    168  */
    169 
    170 int
    171 chk_lnk(ARCHD *arcn)
    172 {
    173 	HRDLNK *pt;
    174 	HRDLNK **ppt;
    175 	u_int indx;
    176 
    177 	if (ltab == NULL)
    178 		return(-1);
    179 	/*
    180 	 * ignore those nodes that cannot have hard links
    181 	 */
    182 	if ((arcn->type == PAX_DIR) || (arcn->sb.st_nlink <= 1))
    183 		return(0);
    184 
    185 	/*
    186 	 * hash inode number and look for this file
    187 	 */
    188 	indx = ((unsigned)arcn->sb.st_ino) % L_TAB_SZ;
    189 	if ((pt = ltab[indx]) != NULL) {
    190 		/*
    191 		 * it's hash chain in not empty, walk down looking for it
    192 		 */
    193 		ppt = &(ltab[indx]);
    194 		while (pt != NULL) {
    195 			if ((pt->ino == arcn->sb.st_ino) &&
    196 			    (pt->dev == arcn->sb.st_dev))
    197 				break;
    198 			ppt = &(pt->fow);
    199 			pt = pt->fow;
    200 		}
    201 
    202 		if (pt != NULL) {
    203 			/*
    204 			 * found a link. set the node type and copy in the
    205 			 * name of the file it is to link to. we need to
    206 			 * handle hardlinks to regular files differently than
    207 			 * other links.
    208 			 */
    209 			arcn->ln_nlen = strlcpy(arcn->ln_name, pt->name,
    210 				sizeof(arcn->ln_name));
    211 			if (arcn->type == PAX_REG)
    212 				arcn->type = PAX_HRG;
    213 			else
    214 				arcn->type = PAX_HLK;
    215 
    216 			/*
    217 			 * if we have found all the links to this file, remove
    218 			 * it from the database
    219 			 */
    220 			if (--pt->nlink <= 1) {
    221 				*ppt = pt->fow;
    222 				(void)free((char *)pt->name);
    223 				(void)free((char *)pt);
    224 			}
    225 			return(1);
    226 		}
    227 	}
    228 
    229 	/*
    230 	 * we never saw this file before. It has links so we add it to the
    231 	 * front of this hash chain
    232 	 */
    233 	if ((pt = (HRDLNK *)malloc(sizeof(HRDLNK))) != NULL) {
    234 		if ((pt->name = strdup(arcn->name)) != NULL) {
    235 			pt->dev = arcn->sb.st_dev;
    236 			pt->ino = arcn->sb.st_ino;
    237 			pt->nlink = arcn->sb.st_nlink;
    238 			pt->fow = ltab[indx];
    239 			ltab[indx] = pt;
    240 			return(0);
    241 		}
    242 		(void)free((char *)pt);
    243 	}
    244 
    245 	tty_warn(1, "Hard link table out of memory");
    246 	return(-1);
    247 }
    248 
    249 /*
    250  * purg_lnk
    251  *	remove reference for a file that we may have added to the data base as
    252  *	a potential source for hard links. We ended up not using the file, so
    253  *	we do not want to accidently point another file at it later on.
    254  */
    255 
    256 void
    257 purg_lnk(ARCHD *arcn)
    258 {
    259 	HRDLNK *pt;
    260 	HRDLNK **ppt;
    261 	u_int indx;
    262 
    263 	if (ltab == NULL)
    264 		return;
    265 	/*
    266 	 * do not bother to look if it could not be in the database
    267 	 */
    268 	if ((arcn->sb.st_nlink <= 1) || (arcn->type == PAX_DIR) ||
    269 	    (arcn->type == PAX_HLK) || (arcn->type == PAX_HRG))
    270 		return;
    271 
    272 	/*
    273 	 * find the hash chain for this inode value, if empty return
    274 	 */
    275 	indx = ((unsigned)arcn->sb.st_ino) % L_TAB_SZ;
    276 	if ((pt = ltab[indx]) == NULL)
    277 		return;
    278 
    279 	/*
    280 	 * walk down the list looking for the inode/dev pair, unlink and
    281 	 * free if found
    282 	 */
    283 	ppt = &(ltab[indx]);
    284 	while (pt != NULL) {
    285 		if ((pt->ino == arcn->sb.st_ino) &&
    286 		    (pt->dev == arcn->sb.st_dev))
    287 			break;
    288 		ppt = &(pt->fow);
    289 		pt = pt->fow;
    290 	}
    291 	if (pt == NULL)
    292 		return;
    293 
    294 	/*
    295 	 * remove and free it
    296 	 */
    297 	*ppt = pt->fow;
    298 	(void)free((char *)pt->name);
    299 	(void)free((char *)pt);
    300 }
    301 
    302 /*
    303  * lnk_end()
    304  *	pull apart a existing link table so we can reuse it. We do this between
    305  *	read and write phases of append with update. (The format may have
    306  *	used the link table, and we need to start with a fresh table for the
    307  *	write phase
    308  */
    309 
    310 void
    311 lnk_end(void)
    312 {
    313 	int i;
    314 	HRDLNK *pt;
    315 	HRDLNK *ppt;
    316 
    317 	if (ltab == NULL)
    318 		return;
    319 
    320 	for (i = 0; i < L_TAB_SZ; ++i) {
    321 		if (ltab[i] == NULL)
    322 			continue;
    323 		pt = ltab[i];
    324 		ltab[i] = NULL;
    325 
    326 		/*
    327 		 * free up each entry on this chain
    328 		 */
    329 		while (pt != NULL) {
    330 			ppt = pt;
    331 			pt = ppt->fow;
    332 			(void)free((char *)ppt->name);
    333 			(void)free((char *)ppt);
    334 		}
    335 	}
    336 	return;
    337 }
    338 
    339 /*
    340  * modification time table routines
    341  *
    342  * The modification time table keeps track of last modification times for all
    343  * files stored in an archive during a write phase when -u is set. We only
    344  * add a file to the archive if it is newer than a file with the same name
    345  * already stored on the archive (if there is no other file with the same
    346  * name on the archive it is added). This applies to writes and appends.
    347  * An append with an -u must read the archive and store the modification time
    348  * for every file on that archive before starting the write phase. It is clear
    349  * that this is one HUGE database. To save memory space, the actual file names
    350  * are stored in a scratch file and indexed by an in-memory hash table. The
    351  * hash table is indexed by hashing the file path. The nodes in the table store
    352  * the length of the filename and the lseek offset within the scratch file
    353  * where the actual name is stored. Since there are never any deletions from this
    354  * table, fragmentation of the scratch file is never a issue. Lookups seem to
    355  * not exhibit any locality at all (files in the database are rarely
    356  * looked up more than once...), so caching is just a waste of memory. The
    357  * only limitation is the amount of scratch file space available to store the
    358  * path names.
    359  */
    360 
    361 /*
    362  * ftime_start()
    363  *	create the file time hash table and open for read/write the scratch
    364  *	file. (after created it is unlinked, so when we exit we leave
    365  *	no witnesses).
    366  * Return:
    367  *	0 if the table and file was created ok, -1 otherwise
    368  */
    369 
    370 int
    371 ftime_start(void)
    372 {
    373 	if (ftab != NULL)
    374 		return(0);
    375 	if ((ftab = (FTM **)calloc(F_TAB_SZ, sizeof(FTM *))) == NULL) {
    376 		tty_warn(1, "Cannot allocate memory for file time table");
    377 		return(-1);
    378 	}
    379 
    380 	/*
    381 	 * get random name and create temporary scratch file, unlink name
    382 	 * so it will get removed on exit
    383 	 */
    384 	memcpy(tempbase, _TFILE_BASE, sizeof(_TFILE_BASE));
    385 	if ((ffd = mkstemp(tempfile)) == -1) {
    386 		syswarn(1, errno, "Unable to create temporary file: %s",
    387 		    tempfile);
    388 		return(-1);
    389 	}
    390 
    391 	(void)unlink(tempfile);
    392 	return(0);
    393 }
    394 
    395 /*
    396  * chk_ftime()
    397  *	looks up entry in file time hash table. If not found, the file is
    398  *	added to the hash table and the file named stored in the scratch file.
    399  *	If a file with the same name is found, the file times are compared and
    400  *	the most recent file time is retained. If the new file was younger (or
    401  *	was not in the database) the new file is selected for storage.
    402  * Return:
    403  *	0 if file should be added to the archive, 1 if it should be skipped,
    404  *	-1 on error
    405  */
    406 
    407 int
    408 chk_ftime(ARCHD *arcn)
    409 {
    410 	FTM *pt;
    411 	int namelen;
    412 	u_int indx;
    413 	char ckname[PAXPATHLEN+1];
    414 
    415 	/*
    416 	 * no info, go ahead and add to archive
    417 	 */
    418 	if (ftab == NULL)
    419 		return(0);
    420 
    421 	/*
    422 	 * hash the pathname and look up in table
    423 	 */
    424 	namelen = arcn->nlen;
    425 	indx = st_hash(arcn->name, namelen, F_TAB_SZ);
    426 	if ((pt = ftab[indx]) != NULL) {
    427 		/*
    428 		 * the hash chain is not empty, walk down looking for match
    429 		 * only read up the path names if the lengths match, speeds
    430 		 * up the search a lot
    431 		 */
    432 		while (pt != NULL) {
    433 			if (pt->namelen == namelen) {
    434 				/*
    435 				 * potential match, have to read the name
    436 				 * from the scratch file.
    437 				 */
    438 				if (lseek(ffd,pt->seek,SEEK_SET) != pt->seek) {
    439 					syswarn(1, errno,
    440 					    "Failed ftime table seek");
    441 					return(-1);
    442 				}
    443 				if (xread(ffd, ckname, namelen) != namelen) {
    444 					syswarn(1, errno,
    445 					    "Failed ftime table read");
    446 					return(-1);
    447 				}
    448 
    449 				/*
    450 				 * if the names match, we are done
    451 				 */
    452 				if (!strncmp(ckname, arcn->name, namelen))
    453 					break;
    454 			}
    455 
    456 			/*
    457 			 * try the next entry on the chain
    458 			 */
    459 			pt = pt->fow;
    460 		}
    461 
    462 		if (pt != NULL) {
    463 			/*
    464 			 * found the file, compare the times, save the newer
    465 			 */
    466 			if (arcn->sb.st_mtime > pt->mtime) {
    467 				/*
    468 				 * file is newer
    469 				 */
    470 				pt->mtime = arcn->sb.st_mtime;
    471 				return(0);
    472 			}
    473 			/*
    474 			 * file is older
    475 			 */
    476 			return(1);
    477 		}
    478 	}
    479 
    480 	/*
    481 	 * not in table, add it
    482 	 */
    483 	if ((pt = (FTM *)malloc(sizeof(FTM))) != NULL) {
    484 		/*
    485 		 * add the name at the end of the scratch file, saving the
    486 		 * offset. add the file to the head of the hash chain
    487 		 */
    488 		if ((pt->seek = lseek(ffd, (off_t)0, SEEK_END)) >= 0) {
    489 			if (xwrite(ffd, arcn->name, namelen) == namelen) {
    490 				pt->mtime = arcn->sb.st_mtime;
    491 				pt->namelen = namelen;
    492 				pt->fow = ftab[indx];
    493 				ftab[indx] = pt;
    494 				return(0);
    495 			}
    496 			syswarn(1, errno, "Failed write to file time table");
    497 		} else
    498 			syswarn(1, errno, "Failed seek on file time table");
    499 	} else
    500 		tty_warn(1, "File time table ran out of memory");
    501 
    502 	if (pt != NULL)
    503 		(void)free((char *)pt);
    504 	return(-1);
    505 }
    506 
    507 /*
    508  * Interactive rename table routines
    509  *
    510  * The interactive rename table keeps track of the new names that the user
    511  * assigns to files from tty input. Since this map is unique for each file
    512  * we must store it in case there is a reference to the file later in archive
    513  * (a link). Otherwise we will be unable to find the file we know was
    514  * extracted. The remapping of these files is stored in a memory based hash
    515  * table (it is assumed since input must come from /dev/tty, it is unlikely to
    516  * be a very large table).
    517  */
    518 
    519 /*
    520  * name_start()
    521  *	create the interactive rename table
    522  * Return:
    523  *	0 if successful, -1 otherwise
    524  */
    525 
    526 int
    527 name_start(void)
    528 {
    529 	if (ntab != NULL)
    530 		return(0);
    531 	if ((ntab = (NAMT **)calloc(N_TAB_SZ, sizeof(NAMT *))) == NULL) {
    532 		tty_warn(1,
    533 		    "Cannot allocate memory for interactive rename table");
    534 		return(-1);
    535 	}
    536 	return(0);
    537 }
    538 
    539 /*
    540  * add_name()
    541  *	add the new name to old name mapping just created by the user.
    542  *	If an old name mapping is found (there may be duplicate names on an
    543  *	archive) only the most recent is kept.
    544  * Return:
    545  *	0 if added, -1 otherwise
    546  */
    547 
    548 int
    549 add_name(char *oname, int onamelen, char *nname)
    550 {
    551 	NAMT *pt;
    552 	u_int indx;
    553 
    554 	if (ntab == NULL) {
    555 		/*
    556 		 * should never happen
    557 		 */
    558 		tty_warn(0, "No interactive rename table, links may fail\n");
    559 		return(0);
    560 	}
    561 
    562 	/*
    563 	 * look to see if we have already mapped this file, if so we
    564 	 * will update it
    565 	 */
    566 	indx = st_hash(oname, onamelen, N_TAB_SZ);
    567 	if ((pt = ntab[indx]) != NULL) {
    568 		/*
    569 		 * look down the has chain for the file
    570 		 */
    571 		while ((pt != NULL) && (strcmp(oname, pt->oname) != 0))
    572 			pt = pt->fow;
    573 
    574 		if (pt != NULL) {
    575 			/*
    576 			 * found an old mapping, replace it with the new one
    577 			 * the user just input (if it is different)
    578 			 */
    579 			if (strcmp(nname, pt->nname) == 0)
    580 				return(0);
    581 
    582 			(void)free((char *)pt->nname);
    583 			if ((pt->nname = strdup(nname)) == NULL) {
    584 				tty_warn(1, "Cannot update rename table");
    585 				return(-1);
    586 			}
    587 			return(0);
    588 		}
    589 	}
    590 
    591 	/*
    592 	 * this is a new mapping, add it to the table
    593 	 */
    594 	if ((pt = (NAMT *)malloc(sizeof(NAMT))) != NULL) {
    595 		if ((pt->oname = strdup(oname)) != NULL) {
    596 			if ((pt->nname = strdup(nname)) != NULL) {
    597 				pt->fow = ntab[indx];
    598 				ntab[indx] = pt;
    599 				return(0);
    600 			}
    601 			(void)free((char *)pt->oname);
    602 		}
    603 		(void)free((char *)pt);
    604 	}
    605 	tty_warn(1, "Interactive rename table out of memory");
    606 	return(-1);
    607 }
    608 
    609 /*
    610  * sub_name()
    611  *	look up a link name to see if it points at a file that has been
    612  *	remapped by the user. If found, the link is adjusted to contain the
    613  *	new name (oname is the link to name)
    614  */
    615 
    616 void
    617 sub_name(char *oname, int *onamelen, size_t onamesize)
    618 {
    619 	NAMT *pt;
    620 	u_int indx;
    621 
    622 	if (ntab == NULL)
    623 		return;
    624 	/*
    625 	 * look the name up in the hash table
    626 	 */
    627 	indx = st_hash(oname, *onamelen, N_TAB_SZ);
    628 	if ((pt = ntab[indx]) == NULL)
    629 		return;
    630 
    631 	while (pt != NULL) {
    632 		/*
    633 		 * walk down the hash chain looking for a match
    634 		 */
    635 		if (strcmp(oname, pt->oname) == 0) {
    636 			/*
    637 			 * found it, replace it with the new name
    638 			 * and return (we know that oname has enough space)
    639 			 */
    640 			*onamelen = strlcpy(oname, pt->nname, onamesize);
    641 			return;
    642 		}
    643 		pt = pt->fow;
    644 	}
    645 
    646 	/*
    647 	 * no match, just return
    648 	 */
    649 	return;
    650 }
    651 
    652 /*
    653  * device/inode mapping table routines
    654  * (used with formats that store device and inodes fields)
    655  *
    656  * device/inode mapping tables remap the device field in a archive header. The
    657  * device/inode fields are used to determine when files are hard links to each
    658  * other. However these values have very little meaning outside of that. This
    659  * database is used to solve one of two different problems.
    660  *
    661  * 1) when files are appended to an archive, while the new files may have hard
    662  * links to each other, you cannot determine if they have hard links to any
    663  * file already stored on the archive from a prior run of pax. We must assume
    664  * that these inode/device pairs are unique only within a SINGLE run of pax
    665  * (which adds a set of files to an archive). So we have to make sure the
    666  * inode/dev pairs we add each time are always unique. We do this by observing
    667  * while the inode field is very dense, the use of the dev field is fairly
    668  * sparse. Within each run of pax, we remap any device number of a new archive
    669  * member that has a device number used in a prior run and already stored in a
    670  * file on the archive. During the read phase of the append, we store the
    671  * device numbers used and mark them to not be used by any file during the
    672  * write phase. If during write we go to use one of those old device numbers,
    673  * we remap it to a new value.
    674  *
    675  * 2) Often the fields in the archive header used to store these values are
    676  * too small to store the entire value. The result is an inode or device value
    677  * which can be truncated. This really can foul up an archive. With truncation
    678  * we end up creating links between files that are really not links (after
    679  * truncation the inodes are the same value). We address that by detecting
    680  * truncation and forcing a remap of the device field to split truncated
    681  * inodes away from each other. Each truncation creates a pattern of bits that
    682  * are removed. We use this pattern of truncated bits to partition the inodes
    683  * on a single device to many different devices (each one represented by the
    684  * truncated bit pattern). All inodes on the same device that have the same
    685  * truncation pattern are mapped to the same new device. Two inodes that
    686  * truncate to the same value clearly will always have different truncation
    687  * bit patterns, so they will be split from away each other. When we spot
    688  * device truncation we remap the device number to a non truncated value.
    689  * (for more info see table.h for the data structures involved).
    690  */
    691 
    692 /*
    693  * dev_start()
    694  *	create the device mapping table
    695  * Return:
    696  *	0 if successful, -1 otherwise
    697  */
    698 
    699 int
    700 dev_start(void)
    701 {
    702 	if (dtab != NULL)
    703 		return(0);
    704 	if ((dtab = (DEVT **)calloc(D_TAB_SZ, sizeof(DEVT *))) == NULL) {
    705 		tty_warn(1, "Cannot allocate memory for device mapping table");
    706 		return(-1);
    707 	}
    708 	return(0);
    709 }
    710 
    711 /*
    712  * add_dev()
    713  *	add a device number to the table. this will force the device to be
    714  *	remapped to a new value if it be used during a write phase. This
    715  *	function is called during the read phase of an append to prohibit the
    716  *	use of any device number already in the archive.
    717  * Return:
    718  *	0 if added ok, -1 otherwise
    719  */
    720 
    721 int
    722 add_dev(ARCHD *arcn)
    723 {
    724 	if (chk_dev(arcn->sb.st_dev, 1) == NULL)
    725 		return(-1);
    726 	return(0);
    727 }
    728 
    729 /*
    730  * chk_dev()
    731  *	check for a device value in the device table. If not found and the add
    732  *	flag is set, it is added. This does NOT assign any mapping values, just
    733  *	adds the device number as one that need to be remapped. If this device
    734  *	is already mapped, just return with a pointer to that entry.
    735  * Return:
    736  *	pointer to the entry for this device in the device map table. Null
    737  *	if the add flag is not set and the device is not in the table (it is
    738  *	not been seen yet). If add is set and the device cannot be added, null
    739  *	is returned (indicates an error).
    740  */
    741 
    742 static DEVT *
    743 chk_dev(dev_t dev, int add)
    744 {
    745 	DEVT *pt;
    746 	u_int indx;
    747 
    748 	if (dtab == NULL)
    749 		return(NULL);
    750 	/*
    751 	 * look to see if this device is already in the table
    752 	 */
    753 	indx = ((unsigned)dev) % D_TAB_SZ;
    754 	if ((pt = dtab[indx]) != NULL) {
    755 		while ((pt != NULL) && (pt->dev != dev))
    756 			pt = pt->fow;
    757 
    758 		/*
    759 		 * found it, return a pointer to it
    760 		 */
    761 		if (pt != NULL)
    762 			return(pt);
    763 	}
    764 
    765 	/*
    766 	 * not in table, we add it only if told to as this may just be a check
    767 	 * to see if a device number is being used.
    768 	 */
    769 	if (add == 0)
    770 		return(NULL);
    771 
    772 	/*
    773 	 * allocate a node for this device and add it to the front of the hash
    774 	 * chain. Note we do not assign remaps values here, so the pt->list
    775 	 * list must be NULL.
    776 	 */
    777 	if ((pt = (DEVT *)malloc(sizeof(DEVT))) == NULL) {
    778 		tty_warn(1, "Device map table out of memory");
    779 		return(NULL);
    780 	}
    781 	pt->dev = dev;
    782 	pt->list = NULL;
    783 	pt->fow = dtab[indx];
    784 	dtab[indx] = pt;
    785 	return(pt);
    786 }
    787 /*
    788  * map_dev()
    789  *	given an inode and device storage mask (the mask has a 1 for each bit
    790  *	the archive format is able to store in a header), we check for inode
    791  *	and device truncation and remap the device as required. Device mapping
    792  *	can also occur when during the read phase of append a device number was
    793  *	seen (and was marked as do not use during the write phase). WE ASSUME
    794  *	that unsigned longs are the same size or bigger than the fields used
    795  *	for ino_t and dev_t. If not the types will have to be changed.
    796  * Return:
    797  *	0 if all ok, -1 otherwise.
    798  */
    799 
    800 int
    801 map_dev(ARCHD *arcn, u_long dev_mask, u_long ino_mask)
    802 {
    803 	DEVT *pt;
    804 	DLIST *dpt;
    805 	static dev_t lastdev = 0;	/* next device number to try */
    806 	int trc_ino = 0;
    807 	int trc_dev = 0;
    808 	ino_t trunc_bits = 0;
    809 	ino_t nino;
    810 
    811 	if (dtab == NULL)
    812 		return(0);
    813 	/*
    814 	 * check for device and inode truncation, and extract the truncated
    815 	 * bit pattern.
    816 	 */
    817 	if ((arcn->sb.st_dev & (dev_t)dev_mask) != arcn->sb.st_dev)
    818 		++trc_dev;
    819 	if ((nino = arcn->sb.st_ino & (ino_t)ino_mask) != arcn->sb.st_ino) {
    820 		++trc_ino;
    821 		trunc_bits = arcn->sb.st_ino & (ino_t)(~ino_mask);
    822 	}
    823 
    824 	/*
    825 	 * see if this device is already being mapped, look up the device
    826 	 * then find the truncation bit pattern which applies
    827 	 */
    828 	if ((pt = chk_dev(arcn->sb.st_dev, 0)) != NULL) {
    829 		/*
    830 		 * this device is already marked to be remapped
    831 		 */
    832 		for (dpt = pt->list; dpt != NULL; dpt = dpt->fow)
    833 			if (dpt->trunc_bits == trunc_bits)
    834 				break;
    835 
    836 		if (dpt != NULL) {
    837 			/*
    838 			 * we are being remapped for this device and pattern
    839 			 * change the device number to be stored and return
    840 			 */
    841 			arcn->sb.st_dev = dpt->dev;
    842 			arcn->sb.st_ino = nino;
    843 			return(0);
    844 		}
    845 	} else {
    846 		/*
    847 		 * this device is not being remapped YET. if we do not have any
    848 		 * form of truncation, we do not need a remap
    849 		 */
    850 		if (!trc_ino && !trc_dev)
    851 			return(0);
    852 
    853 		/*
    854 		 * we have truncation, have to add this as a device to remap
    855 		 */
    856 		if ((pt = chk_dev(arcn->sb.st_dev, 1)) == NULL)
    857 			goto bad;
    858 
    859 		/*
    860 		 * if we just have a truncated inode, we have to make sure that
    861 		 * all future inodes that do not truncate (they have the
    862 		 * truncation pattern of all 0's) continue to map to the same
    863 		 * device number. We probably have already written inodes with
    864 		 * this device number to the archive with the truncation
    865 		 * pattern of all 0's. So we add the mapping for all 0's to the
    866 		 * same device number.
    867 		 */
    868 		if (!trc_dev && (trunc_bits != 0)) {
    869 			if ((dpt = (DLIST *)malloc(sizeof(DLIST))) == NULL)
    870 				goto bad;
    871 			dpt->trunc_bits = 0;
    872 			dpt->dev = arcn->sb.st_dev;
    873 			dpt->fow = pt->list;
    874 			pt->list = dpt;
    875 		}
    876 	}
    877 
    878 	/*
    879 	 * look for a device number not being used. We must watch for wrap
    880 	 * around on lastdev (so we do not get stuck looking forever!)
    881 	 */
    882 	while (++lastdev > 0) {
    883 		if (chk_dev(lastdev, 0) != NULL)
    884 			continue;
    885 		/*
    886 		 * found an unused value. If we have reached truncation point
    887 		 * for this format we are hosed, so we give up. Otherwise we
    888 		 * mark it as being used.
    889 		 */
    890 		if (((lastdev & ((dev_t)dev_mask)) != lastdev) ||
    891 		    (chk_dev(lastdev, 1) == NULL))
    892 			goto bad;
    893 		break;
    894 	}
    895 
    896 	if ((lastdev <= 0) || ((dpt = (DLIST *)malloc(sizeof(DLIST))) == NULL))
    897 		goto bad;
    898 
    899 	/*
    900 	 * got a new device number, store it under this truncation pattern.
    901 	 * change the device number this file is being stored with.
    902 	 */
    903 	dpt->trunc_bits = trunc_bits;
    904 	dpt->dev = lastdev;
    905 	dpt->fow = pt->list;
    906 	pt->list = dpt;
    907 	arcn->sb.st_dev = lastdev;
    908 	arcn->sb.st_ino = nino;
    909 	return(0);
    910 
    911     bad:
    912 	tty_warn(1,
    913 	    "Unable to fix truncated inode/device field when storing %s",
    914 	    arcn->name);
    915 	tty_warn(0, "Archive may create improper hard links when extracted");
    916 	return(0);
    917 }
    918 
    919 /*
    920  * directory access/mod time reset table routines (for directories READ by pax)
    921  *
    922  * The pax -t flag requires that access times of archive files to be the same
    923  * as before being read by pax. For regular files, access time is restored after
    924  * the file has been copied. This database provides the same functionality for
    925  * directories read during file tree traversal. Restoring directory access time
    926  * is more complex than files since directories may be read several times until
    927  * all the descendants in their subtree are visited by fts. Directory access
    928  * and modification times are stored during the fts pre-order visit (done
    929  * before any descendants in the subtree is visited) and restored after the
    930  * fts post-order visit (after all the descendants have been visited). In the
    931  * case of premature exit from a subtree (like from the effects of -n), any
    932  * directory entries left in this database are reset during final cleanup
    933  * operations of pax. Entries are hashed by inode number for fast lookup.
    934  */
    935 
    936 /*
    937  * atdir_start()
    938  *	create the directory access time database for directories READ by pax.
    939  * Return:
    940  *	0 is created ok, -1 otherwise.
    941  */
    942 
    943 int
    944 atdir_start(void)
    945 {
    946 	if (atab != NULL)
    947 		return(0);
    948 	if ((atab = (ATDIR **)calloc(A_TAB_SZ, sizeof(ATDIR *))) == NULL) {
    949 		tty_warn(1,
    950 		    "Cannot allocate space for directory access time table");
    951 		return(-1);
    952 	}
    953 	return(0);
    954 }
    955 
    956 
    957 /*
    958  * atdir_end()
    959  *	walk through the directory access time table and reset the access time
    960  *	of any directory who still has an entry left in the database. These
    961  *	entries are for directories READ by pax
    962  */
    963 
    964 void
    965 atdir_end(void)
    966 {
    967 	ATDIR *pt;
    968 	int i;
    969 
    970 	if (atab == NULL)
    971 		return;
    972 	/*
    973 	 * for each non-empty hash table entry reset all the directories
    974 	 * chained there.
    975 	 */
    976 	for (i = 0; i < A_TAB_SZ; ++i) {
    977 		if ((pt = atab[i]) == NULL)
    978 			continue;
    979 		/*
    980 		 * remember to force the times, set_ftime() looks at pmtime
    981 		 * and patime, which only applies to things CREATED by pax,
    982 		 * not read by pax. Read time reset is controlled by -t.
    983 		 */
    984 		for (; pt != NULL; pt = pt->fow)
    985 			set_ftime(pt->name, pt->mtime, pt->atime, 1);
    986 	}
    987 }
    988 
    989 /*
    990  * add_atdir()
    991  *	add a directory to the directory access time table. Table is hashed
    992  *	and chained by inode number. This is for directories READ by pax
    993  */
    994 
    995 void
    996 add_atdir(char *fname, dev_t dev, ino_t ino, time_t mtime, time_t atime)
    997 {
    998 	ATDIR *pt;
    999 	u_int indx;
   1000 
   1001 	if (atab == NULL)
   1002 		return;
   1003 
   1004 	/*
   1005 	 * make sure this directory is not already in the table, if so just
   1006 	 * return (the older entry always has the correct time). The only
   1007 	 * way this will happen is when the same subtree can be traversed by
   1008 	 * different args to pax and the -n option is aborting fts out of a
   1009 	 * subtree before all the post-order visits have been made.
   1010 	 */
   1011 	indx = ((unsigned)ino) % A_TAB_SZ;
   1012 	if ((pt = atab[indx]) != NULL) {
   1013 		while (pt != NULL) {
   1014 			if ((pt->ino == ino) && (pt->dev == dev))
   1015 				break;
   1016 			pt = pt->fow;
   1017 		}
   1018 
   1019 		/*
   1020 		 * oops, already there. Leave it alone.
   1021 		 */
   1022 		if (pt != NULL)
   1023 			return;
   1024 	}
   1025 
   1026 	/*
   1027 	 * add it to the front of the hash chain
   1028 	 */
   1029 	if ((pt = (ATDIR *)malloc(sizeof(ATDIR))) != NULL) {
   1030 		if ((pt->name = strdup(fname)) != NULL) {
   1031 			pt->dev = dev;
   1032 			pt->ino = ino;
   1033 			pt->mtime = mtime;
   1034 			pt->atime = atime;
   1035 			pt->fow = atab[indx];
   1036 			atab[indx] = pt;
   1037 			return;
   1038 		}
   1039 		(void)free((char *)pt);
   1040 	}
   1041 
   1042 	tty_warn(1, "Directory access time reset table ran out of memory");
   1043 	return;
   1044 }
   1045 
   1046 /*
   1047  * get_atdir()
   1048  *	look up a directory by inode and device number to obtain the access
   1049  *	and modification time you want to set to. If found, the modification
   1050  *	and access time parameters are set and the entry is removed from the
   1051  *	table (as it is no longer needed). These are for directories READ by
   1052  *	pax
   1053  * Return:
   1054  *	0 if found, -1 if not found.
   1055  */
   1056 
   1057 int
   1058 get_atdir(dev_t dev, ino_t ino, time_t *mtime, time_t *atime)
   1059 {
   1060 	ATDIR *pt;
   1061 	ATDIR **ppt;
   1062 	u_int indx;
   1063 
   1064 	if (atab == NULL)
   1065 		return(-1);
   1066 	/*
   1067 	 * hash by inode and search the chain for an inode and device match
   1068 	 */
   1069 	indx = ((unsigned)ino) % A_TAB_SZ;
   1070 	if ((pt = atab[indx]) == NULL)
   1071 		return(-1);
   1072 
   1073 	ppt = &(atab[indx]);
   1074 	while (pt != NULL) {
   1075 		if ((pt->ino == ino) && (pt->dev == dev))
   1076 			break;
   1077 		/*
   1078 		 * no match, go to next one
   1079 		 */
   1080 		ppt = &(pt->fow);
   1081 		pt = pt->fow;
   1082 	}
   1083 
   1084 	/*
   1085 	 * return if we did not find it.
   1086 	 */
   1087 	if (pt == NULL)
   1088 		return(-1);
   1089 
   1090 	/*
   1091 	 * found it. return the times and remove the entry from the table.
   1092 	 */
   1093 	*ppt = pt->fow;
   1094 	*mtime = pt->mtime;
   1095 	*atime = pt->atime;
   1096 	(void)free((char *)pt->name);
   1097 	(void)free((char *)pt);
   1098 	return(0);
   1099 }
   1100 
   1101 /*
   1102  * directory access mode and time storage routines (for directories CREATED
   1103  * by pax).
   1104  *
   1105  * Pax requires that extracted directories, by default, have their access/mod
   1106  * times and permissions set to the values specified in the archive. During the
   1107  * actions of extracting (and creating the destination subtree during -rw copy)
   1108  * directories extracted may be modified after being created. Even worse is
   1109  * that these directories may have been created with file permissions which
   1110  * prohibits any descendants of these directories from being extracted. When
   1111  * directories are created by pax, access rights may be added to permit the
   1112  * creation of files in their subtree. Every time pax creates a directory, the
   1113  * times and file permissions specified by the archive are stored. After all
   1114  * files have been extracted (or copied), these directories have their times
   1115  * and file modes reset to the stored values. The directory info is restored in
   1116  * reverse order as entries were added to the data file from root to leaf. To
   1117  * restore atime properly, we must go backwards. The data file consists of
   1118  * records with two parts, the file name followed by a DIRDATA trailer. The
   1119  * fixed sized trailer contains the size of the name plus the off_t location in
   1120  * the file. To restore we work backwards through the file reading the trailer
   1121  * then the file name.
   1122  */
   1123 
   1124 #ifndef DIRS_USE_FILE
   1125 static DIRDATA *dirdata_head;
   1126 #endif
   1127 
   1128 /*
   1129  * dir_start()
   1130  *	set up the directory time and file mode storage for directories CREATED
   1131  *	by pax.
   1132  * Return:
   1133  *	0 if ok, -1 otherwise
   1134  */
   1135 
   1136 int
   1137 dir_start(void)
   1138 {
   1139 #ifdef DIRS_USE_FILE
   1140 	if (dirfd != -1)
   1141 		return(0);
   1142 
   1143 	/*
   1144 	 * unlink the file so it goes away at termination by itself
   1145 	 */
   1146 	memcpy(tempbase, _TFILE_BASE, sizeof(_TFILE_BASE));
   1147 	if ((dirfd = mkstemp(tempfile)) >= 0) {
   1148 		(void)unlink(tempfile);
   1149 		return(0);
   1150 	}
   1151 	tty_warn(1, "Unable to create temporary file for directory times: %s",
   1152 	    tempfile);
   1153 	return(-1);
   1154 #else
   1155 	return (0);
   1156 #endif /* DIRS_USE_FILE */
   1157 }
   1158 
   1159 /*
   1160  * add_dir()
   1161  *	add the mode and times for a newly CREATED directory
   1162  *	name is name of the directory, psb the stat buffer with the data in it,
   1163  *	frc_mode is a flag that says whether to force the setting of the mode
   1164  *	(ignoring the user set values for preserving file mode). Frc_mode is
   1165  *	for the case where we created a file and found that the resulting
   1166  *	directory was not writable and the user asked for file modes to NOT
   1167  *	be preserved. (we have to preserve what was created by default, so we
   1168  *	have to force the setting at the end. this is stated explicitly in the
   1169  *	pax spec)
   1170  */
   1171 
   1172 void
   1173 add_dir(char *name, int nlen, struct stat *psb, int frc_mode)
   1174 {
   1175 #ifdef DIRS_USE_FILE
   1176 	DIRDATA dblk;
   1177 
   1178 	if (dirfd < 0)
   1179 		return;
   1180 
   1181 	/*
   1182 	 * get current position (where file name will start) so we can store it
   1183 	 * in the trailer
   1184 	 */
   1185 	if ((dblk.npos = lseek(dirfd, 0L, SEEK_CUR)) < 0) {
   1186 		tty_warn(1,
   1187 		    "Unable to store mode and times for directory: %s",name);
   1188 		return;
   1189 	}
   1190 
   1191 	/*
   1192 	 * write the file name followed by the trailer
   1193 	 */
   1194 	dblk.nlen = nlen + 1;
   1195 	dblk.mode = psb->st_mode & 0xffff;
   1196 	dblk.mtime = psb->st_mtime;
   1197 	dblk.atime = psb->st_atime;
   1198 #if HAVE_STRUCT_STAT_ST_FLAGS
   1199 	dblk.fflags = psb->st_flags;
   1200 #else
   1201 	dblk.fflags = 0;
   1202 #endif
   1203 	dblk.frc_mode = frc_mode;
   1204 	if ((xwrite(dirfd, name, dblk.nlen) == dblk.nlen) &&
   1205 	    (xwrite(dirfd, (char *)&dblk, sizeof(dblk)) == sizeof(dblk))) {
   1206 		++dircnt;
   1207 		return;
   1208 	}
   1209 
   1210 	tty_warn(1,
   1211 	    "Unable to store mode and times for created directory: %s",name);
   1212 	return;
   1213 #else
   1214 	DIRDATA *dblk;
   1215 
   1216 	if ((dblk = malloc(sizeof(*dblk))) == NULL ||
   1217 	    (dblk->name = strdup(name)) == NULL) {
   1218 		tty_warn(1,
   1219 		    "Unable to store mode and times for directory: %s",name);
   1220 		if (dblk != NULL)
   1221 			free(dblk);
   1222 		return;
   1223 	}
   1224 
   1225 	dblk->mode = psb->st_mode & 0xffff;
   1226 	dblk->mtime = psb->st_mtime;
   1227 	dblk->atime = psb->st_atime;
   1228 #if HAVE_STRUCT_STAT_ST_FLAGS
   1229 	dblk->fflags = psb->st_flags;
   1230 #else
   1231 	dblk->fflags = 0;
   1232 #endif
   1233 	dblk->frc_mode = frc_mode;
   1234 
   1235 	dblk->next = dirdata_head;
   1236 	dirdata_head = dblk;
   1237 	return;
   1238 #endif /* DIRS_USE_FILE */
   1239 }
   1240 
   1241 /*
   1242  * proc_dir()
   1243  *	process all file modes and times stored for directories CREATED
   1244  *	by pax
   1245  */
   1246 
   1247 void
   1248 proc_dir(void)
   1249 {
   1250 #ifdef DIRS_USE_FILE
   1251 	char name[PAXPATHLEN+1];
   1252 	DIRDATA dblk;
   1253 	u_long cnt;
   1254 
   1255 	if (dirfd < 0)
   1256 		return;
   1257 	/*
   1258 	 * read backwards through the file and process each directory
   1259 	 */
   1260 	for (cnt = 0; cnt < dircnt; ++cnt) {
   1261 		/*
   1262 		 * read the trailer, then the file name, if this fails
   1263 		 * just give up.
   1264 		 */
   1265 		if (lseek(dirfd, -((off_t)sizeof(dblk)), SEEK_CUR) < 0)
   1266 			break;
   1267 		if (xread(dirfd,(char *)&dblk, sizeof(dblk)) != sizeof(dblk))
   1268 			break;
   1269 		if (lseek(dirfd, dblk.npos, SEEK_SET) < 0)
   1270 			break;
   1271 		if (xread(dirfd, name, dblk.nlen) != dblk.nlen)
   1272 			break;
   1273 		if (lseek(dirfd, dblk.npos, SEEK_SET) < 0)
   1274 			break;
   1275 
   1276 		/*
   1277 		 * frc_mode set, make sure we set the file modes even if
   1278 		 * the user didn't ask for it (see file_subs.c for more info)
   1279 		 */
   1280 		if (pmode || dblk.frc_mode)
   1281 			set_pmode(name, dblk.mode);
   1282 		if (patime || pmtime)
   1283 			set_ftime(name, dblk.mtime, dblk.atime, 0);
   1284 		if (pfflags)
   1285 			set_chflags(name, dblk.fflags);
   1286 	}
   1287 
   1288 	(void)close(dirfd);
   1289 	dirfd = -1;
   1290 	if (cnt != dircnt)
   1291 		tty_warn(1,
   1292 		    "Unable to set mode and times for created directories");
   1293 	return;
   1294 #else
   1295 	DIRDATA *dblk;
   1296 
   1297 	for (dblk = dirdata_head; dblk != NULL; dblk = dirdata_head) {
   1298 		dirdata_head = dblk->next;
   1299 
   1300 		/*
   1301 		 * frc_mode set, make sure we set the file modes even if
   1302 		 * the user didn't ask for it (see file_subs.c for more info)
   1303 		 */
   1304 		if (pmode || dblk->frc_mode)
   1305 			set_pmode(dblk->name, dblk->mode);
   1306 		if (patime || pmtime)
   1307 			set_ftime(dblk->name, dblk->mtime, dblk->atime, 0);
   1308 		if (pfflags)
   1309 			set_chflags(dblk->name, dblk->fflags);
   1310 
   1311 		free(dblk->name);
   1312 		free(dblk);
   1313 	}
   1314 #endif /* DIRS_USE_FILE */
   1315 }
   1316 
   1317 /*
   1318  * database independent routines
   1319  */
   1320 
   1321 /*
   1322  * st_hash()
   1323  *	hashes filenames to a u_int for hashing into a table. Looks at the tail
   1324  *	end of file, as this provides far better distribution than any other
   1325  *	part of the name. For performance reasons we only care about the last
   1326  *	MAXKEYLEN chars (should be at LEAST large enough to pick off the file
   1327  *	name). Was tested on 500,000 name file tree traversal from the root
   1328  *	and gave almost a perfectly uniform distribution of keys when used with
   1329  *	prime sized tables (MAXKEYLEN was 128 in test). Hashes (sizeof int)
   1330  *	chars at a time and pads with 0 for last addition.
   1331  * Return:
   1332  *	the hash value of the string MOD (%) the table size.
   1333  */
   1334 
   1335 u_int
   1336 st_hash(char *name, int len, int tabsz)
   1337 {
   1338 	char *pt;
   1339 	char *dest;
   1340 	char *end;
   1341 	int i;
   1342 	u_int key = 0;
   1343 	int steps;
   1344 	int res;
   1345 	u_int val;
   1346 
   1347 	/*
   1348 	 * only look at the tail up to MAXKEYLEN, we do not need to waste
   1349 	 * time here (remember these are pathnames, the tail is what will
   1350 	 * spread out the keys)
   1351 	 */
   1352 	if (len > MAXKEYLEN) {
   1353 		pt = &(name[len - MAXKEYLEN]);
   1354 		len = MAXKEYLEN;
   1355 	} else
   1356 		pt = name;
   1357 
   1358 	/*
   1359 	 * calculate the number of u_int size steps in the string and if
   1360 	 * there is a runt to deal with
   1361 	 */
   1362 	steps = len/sizeof(u_int);
   1363 	res = len % sizeof(u_int);
   1364 
   1365 	/*
   1366 	 * add up the value of the string in unsigned integer sized pieces
   1367 	 * too bad we cannot have unsigned int aligned strings, then we
   1368 	 * could avoid the expensive copy.
   1369 	 */
   1370 	for (i = 0; i < steps; ++i) {
   1371 		end = pt + sizeof(u_int);
   1372 		dest = (char *)&val;
   1373 		while (pt < end)
   1374 			*dest++ = *pt++;
   1375 		key += val;
   1376 	}
   1377 
   1378 	/*
   1379 	 * add in the runt padded with zero to the right
   1380 	 */
   1381 	if (res) {
   1382 		val = 0;
   1383 		end = pt + res;
   1384 		dest = (char *)&val;
   1385 		while (pt < end)
   1386 			*dest++ = *pt++;
   1387 		key += val;
   1388 	}
   1389 
   1390 	/*
   1391 	 * return the result mod the table size
   1392 	 */
   1393 	return(key % tabsz);
   1394 }
   1395