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