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