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