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