1 /* $OpenBSD: tables.c,v 1.49 2016/08/26 04:23:44 guenther Exp $ */ 2 /* $NetBSD: tables.c,v 1.4 1995/03/21 09:07:45 cgd Exp $ */ 3 4 /*- 5 * Copyright (c) 1992 Keith Muller. 6 * Copyright (c) 1992, 1993 7 * The Regents of the University of California. All rights reserved. 8 * 9 * This code is derived from software contributed to Berkeley by 10 * Keith Muller of the University of California, San Diego. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 3. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 */ 36 37 #include <sys/types.h> 38 #include <sys/stat.h> 39 #include <errno.h> 40 #include <fcntl.h> 41 #include <limits.h> 42 #include <signal.h> 43 #include <stdio.h> 44 #include <stdlib.h> 45 #include <string.h> 46 #include <unistd.h> 47 48 #include "pax.h" 49 #include "extern.h" 50 51 /* 52 * Routines for controlling the contents of all the different databases pax 53 * keeps. Tables are dynamically created only when they are needed. The 54 * goal was speed and the ability to work with HUGE archives. The databases 55 * were kept simple, but do have complex rules for when the contents change. 56 * As of this writing, the posix library functions were more complex than 57 * needed for this application (pax databases have very short lifetimes and 58 * do not survive after pax is finished). Pax is required to handle very 59 * large archives. These database routines carefully combine memory usage and 60 * temporary file storage in ways which will not significantly impact runtime 61 * performance while allowing the largest possible archives to be handled. 62 * Trying to force the fit to the posix database routines was not considered 63 * time well spent. 64 */ 65 66 /* 67 * data structures and constants used by the different databases kept by pax 68 */ 69 70 /* 71 * Hash Table Sizes MUST BE PRIME, if set too small performance suffers. 72 * Probably safe to expect 500000 inodes per tape. Assuming good key 73 * distribution (inodes) chains of under 50 long (worst case) is ok. 74 */ 75 #define L_TAB_SZ 2503 /* hard link hash table size */ 76 #define F_TAB_SZ 50503 /* file time hash table size */ 77 #define N_TAB_SZ 541 /* interactive rename hash table */ 78 #define D_TAB_SZ 317 /* unique device mapping table */ 79 #define A_TAB_SZ 317 /* ftree dir access time reset table */ 80 #define SL_TAB_SZ 317 /* escape symlink tables */ 81 #define MAXKEYLEN 64 /* max number of chars for hash */ 82 #define DIRP_SIZE 64 /* initial size of created dir table */ 83 84 /* 85 * file hard link structure (hashed by dev/ino and chained) used to find the 86 * hard links in a file system or with some archive formats (cpio) 87 */ 88 typedef struct hrdlnk { 89 ino_t ino; /* files inode number */ 90 char *name; /* name of first file seen with this ino/dev */ 91 dev_t dev; /* files device number */ 92 u_long nlink; /* expected link count */ 93 struct hrdlnk *fow; 94 } HRDLNK; 95 96 /* 97 * Archive write update file time table (the -u, -C flag), hashed by filename. 98 * Filenames are stored in a scratch file at seek offset into the file. The 99 * file time (mod time) and the file name length (for a quick check) are 100 * stored in a hash table node. We were forced to use a scratch file because 101 * with -u, the mtime for every node in the archive must always be available 102 * to compare against (and this data can get REALLY large with big archives). 103 * By being careful to read only when we have a good chance of a match, the 104 * performance loss is not measurable (and the size of the archive we can 105 * handle is greatly increased). 106 */ 107 typedef struct ftm { 108 off_t seek; /* location in scratch file */ 109 struct timespec mtim; /* files last modification time */ 110 struct ftm *fow; 111 int namelen; /* file name length */ 112 } FTM; 113 114 /* 115 * Interactive rename table (-i flag), hashed by orig filename. 116 * We assume this will not be a large table as this mapping data can only be 117 * obtained through interactive input by the user. Nobody is going to type in 118 * changes for 500000 files? We use chaining to resolve collisions. 119 */ 120 121 typedef struct namt { 122 char *oname; /* old name */ 123 char *nname; /* new name typed in by the user */ 124 struct namt *fow; 125 } NAMT; 126 127 /* 128 * Unique device mapping tables. Some protocols (e.g. cpio) require that the 129 * <c_dev,c_ino> pair will uniquely identify a file in an archive unless they 130 * are links to the same file. Appending to archives can break this. For those 131 * protocols that have this requirement we map c_dev to a unique value not seen 132 * in the archive when we append. We also try to handle inode truncation with 133 * this table. (When the inode field in the archive header are too small, we 134 * remap the dev on writes to remove accidental collisions). 135 * 136 * The list is hashed by device number using chain collision resolution. Off of 137 * each DEVT are linked the various remaps for this device based on those bits 138 * in the inode which were truncated. For example if we are just remapping to 139 * avoid a device number during an update append, off the DEVT we would have 140 * only a single DLIST that has a truncation id of 0 (no inode bits were 141 * stripped for this device so far). When we spot inode truncation we create 142 * a new mapping based on the set of bits in the inode which were stripped off. 143 * so if the top four bits of the inode are stripped and they have a pattern of 144 * 0110...... (where . are those bits not truncated) we would have a mapping 145 * assigned for all inodes that has the same 0110.... pattern (with this dev 146 * number of course). This keeps the mapping sparse and should be able to store 147 * close to the limit of files which can be represented by the optimal 148 * combination of dev and inode bits, and without creating a fouled up archive. 149 * Note we also remap truncated devs in the same way (an exercise for the 150 * dedicated reader; always wanted to say that...:) 151 */ 152 153 typedef struct devt { 154 dev_t dev; /* the orig device number we now have to map */ 155 struct devt *fow; /* new device map list */ 156 struct dlist *list; /* map list based on inode truncation bits */ 157 } DEVT; 158 159 typedef struct dlist { 160 ino_t trunc_bits; /* truncation pattern for a specific map */ 161 dev_t dev; /* the new device id we use */ 162 struct dlist *fow; 163 } DLIST; 164 165 /* 166 * ftree directory access time reset table. When we are done with a 167 * subtree we reset the access and mod time of the directory when the tflag is 168 * set. Not really explicitly specified in the pax spec, but easy and fast to 169 * do (and this may have even been intended in the spec, it is not clear). 170 * table is hashed by inode with chaining. 171 */ 172 173 typedef struct atdir { 174 struct file_times ft; 175 struct atdir *fow; 176 } ATDIR; 177 178 /* 179 * created directory time and mode storage entry. After pax is finished during 180 * extraction or copy, we must reset directory access modes and times that 181 * may have been modified after creation (they no longer have the specified 182 * times and/or modes). We must reset time in the reverse order of creation, 183 * because entries are added from the top of the file tree to the bottom. 184 * We MUST reset times from leaf to root (it will not work the other 185 * direction). 186 */ 187 188 typedef struct dirdata { 189 struct file_times ft; 190 u_int16_t mode; /* file mode to restore */ 191 u_int16_t frc_mode; /* do we force mode settings? */ 192 } DIRDATA; 193 194 static HRDLNK **ltab = NULL; /* hard link table for detecting hard links */ 195 static FTM **ftab = NULL; /* file time table for updating arch */ 196 static NAMT **ntab = NULL; /* interactive rename storage table */ 197 static DEVT **dtab = NULL; /* device/inode mapping tables */ 198 static ATDIR **atab = NULL; /* file tree directory time reset table */ 199 static DIRDATA *dirp = NULL; /* storage for setting created dir time/mode */ 200 static size_t dirsize; /* size of dirp table */ 201 static size_t dircnt = 0; /* entries in dir time/mode storage */ 202 static int ffd = -1; /* tmp file for file time table name storage */ 203 204 /* 205 * hard link table routines 206 * 207 * The hard link table tries to detect hard links to files using the device and 208 * inode values. We do this when writing an archive, so we can tell the format 209 * write routine that this file is a hard link to another file. The format 210 * write routine then can store this file in whatever way it wants (as a hard 211 * link if the format supports that like tar, or ignore this info like cpio). 212 * (Actually a field in the format driver table tells us if the format wants 213 * hard link info. if not, we do not waste time looking for them). We also use 214 * the same table when reading an archive. In that situation, this table is 215 * used by the format read routine to detect hard links from stored dev and 216 * inode numbers (like cpio). This will allow pax to create a link when one 217 * can be detected by the archive format. 218 */ 219 220 /* 221 * lnk_start 222 * Creates the hard link table. 223 * Return: 224 * 0 if created, -1 if failure 225 */ 226 227 int 228 lnk_start(void) 229 { 230 if (ltab != NULL) 231 return(0); 232 if ((ltab = calloc(L_TAB_SZ, sizeof(HRDLNK *))) == NULL) { 233 paxwarn(1, "Cannot allocate memory for hard link table"); 234 return(-1); 235 } 236 return(0); 237 } 238 239 /* 240 * chk_lnk() 241 * Looks up entry in hard link hash table. If found, it copies the name 242 * of the file it is linked to (we already saw that file) into ln_name. 243 * lnkcnt is decremented and if goes to 1 the node is deleted from the 244 * database. (We have seen all the links to this file). If not found, 245 * we add the file to the database if it has the potential for having 246 * hard links to other files we may process (it has a link count > 1) 247 * Return: 248 * if found returns 1; if not found returns 0; -1 on error 249 */ 250 251 int 252 chk_lnk(ARCHD *arcn) 253 { 254 HRDLNK *pt; 255 HRDLNK **ppt; 256 u_int indx; 257 258 if (ltab == NULL) 259 return(-1); 260 /* 261 * ignore those nodes that cannot have hard links 262 */ 263 if ((arcn->type == PAX_DIR) || (arcn->sb.st_nlink <= 1)) 264 return(0); 265 266 /* 267 * hash inode number and look for this file 268 */ 269 indx = ((unsigned)arcn->sb.st_ino) % L_TAB_SZ; 270 if ((pt = ltab[indx]) != NULL) { 271 /* 272 * its hash chain in not empty, walk down looking for it 273 */ 274 ppt = &(ltab[indx]); 275 while (pt != NULL) { 276 if ((pt->ino == arcn->sb.st_ino) && 277 (pt->dev == arcn->sb.st_dev)) 278 break; 279 ppt = &(pt->fow); 280 pt = pt->fow; 281 } 282 283 if (pt != NULL) { 284 /* 285 * found a link. set the node type and copy in the 286 * name of the file it is to link to. we need to 287 * handle hardlinks to regular files differently than 288 * other links. 289 */ 290 arcn->ln_nlen = strlcpy(arcn->ln_name, pt->name, 291 sizeof(arcn->ln_name)); 292 /* XXX truncate? */ 293 if (arcn->nlen >= sizeof(arcn->name)) 294 arcn->nlen = sizeof(arcn->name) - 1; 295 if (arcn->type == PAX_REG) 296 arcn->type = PAX_HRG; 297 else 298 arcn->type = PAX_HLK; 299 300 /* 301 * if we have found all the links to this file, remove 302 * it from the database 303 */ 304 if (--pt->nlink <= 1) { 305 *ppt = pt->fow; 306 free(pt->name); 307 free(pt); 308 } 309 return(1); 310 } 311 } 312 313 /* 314 * we never saw this file before. It has links so we add it to the 315 * front of this hash chain 316 */ 317 if ((pt = malloc(sizeof(HRDLNK))) != NULL) { 318 if ((pt->name = strdup(arcn->name)) != NULL) { 319 pt->dev = arcn->sb.st_dev; 320 pt->ino = arcn->sb.st_ino; 321 pt->nlink = arcn->sb.st_nlink; 322 pt->fow = ltab[indx]; 323 ltab[indx] = pt; 324 return(0); 325 } 326 free(pt); 327 } 328 329 paxwarn(1, "Hard link table out of memory"); 330 return(-1); 331 } 332 333 /* 334 * purg_lnk 335 * remove reference for a file that we may have added to the data base as 336 * a potential source for hard links. We ended up not using the file, so 337 * we do not want to accidently point another file at it later on. 338 */ 339 340 void 341 purg_lnk(ARCHD *arcn) 342 { 343 HRDLNK *pt; 344 HRDLNK **ppt; 345 u_int indx; 346 347 if (ltab == NULL) 348 return; 349 /* 350 * do not bother to look if it could not be in the database 351 */ 352 if ((arcn->sb.st_nlink <= 1) || (arcn->type == PAX_DIR) || 353 PAX_IS_HARDLINK(arcn->type)) 354 return; 355 356 /* 357 * find the hash chain for this inode value, if empty return 358 */ 359 indx = ((unsigned)arcn->sb.st_ino) % L_TAB_SZ; 360 if ((pt = ltab[indx]) == NULL) 361 return; 362 363 /* 364 * walk down the list looking for the inode/dev pair, unlink and 365 * free if found 366 */ 367 ppt = &(ltab[indx]); 368 while (pt != NULL) { 369 if ((pt->ino == arcn->sb.st_ino) && 370 (pt->dev == arcn->sb.st_dev)) 371 break; 372 ppt = &(pt->fow); 373 pt = pt->fow; 374 } 375 if (pt == NULL) 376 return; 377 378 /* 379 * remove and free it 380 */ 381 *ppt = pt->fow; 382 free(pt->name); 383 free(pt); 384 } 385 386 /* 387 * lnk_end() 388 * pull apart a existing link table so we can reuse it. We do this between 389 * read and write phases of append with update. (The format may have 390 * used the link table, and we need to start with a fresh table for the 391 * write phase 392 */ 393 394 void 395 lnk_end(void) 396 { 397 int i; 398 HRDLNK *pt; 399 HRDLNK *ppt; 400 401 if (ltab == NULL) 402 return; 403 404 for (i = 0; i < L_TAB_SZ; ++i) { 405 if (ltab[i] == NULL) 406 continue; 407 pt = ltab[i]; 408 ltab[i] = NULL; 409 410 /* 411 * free up each entry on this chain 412 */ 413 while (pt != NULL) { 414 ppt = pt; 415 pt = ppt->fow; 416 free(ppt->name); 417 free(ppt); 418 } 419 } 420 } 421 422 /* 423 * modification time table routines 424 * 425 * The modification time table keeps track of last modification times for all 426 * files stored in an archive during a write phase when -u is set. We only 427 * add a file to the archive if it is newer than a file with the same name 428 * already stored on the archive (if there is no other file with the same 429 * name on the archive it is added). This applies to writes and appends. 430 * An append with an -u must read the archive and store the modification time 431 * for every file on that archive before starting the write phase. It is clear 432 * that this is one HUGE database. To save memory space, the actual file names 433 * are stored in a scratch file and indexed by an in-memory hash table. The 434 * hash table is indexed by hashing the file path. The nodes in the table store 435 * the length of the filename and the lseek offset within the scratch file 436 * where the actual name is stored. Since there are never any deletions from 437 * this table, fragmentation of the scratch file is never a issue. Lookups 438 * seem to not exhibit any locality at all (files in the database are rarely 439 * looked up more than once...), so caching is just a waste of memory. The 440 * only limitation is the amount of scratch file space available to store the 441 * path names. 442 */ 443 444 /* 445 * ftime_start() 446 * create the file time hash table and open for read/write the scratch 447 * file. (after created it is unlinked, so when we exit we leave 448 * no witnesses). 449 * Return: 450 * 0 if the table and file was created ok, -1 otherwise 451 */ 452 453 int 454 ftime_start(void) 455 { 456 457 if (ftab != NULL) 458 return(0); 459 if ((ftab = calloc(F_TAB_SZ, sizeof(FTM *))) == NULL) { 460 paxwarn(1, "Cannot allocate memory for file time table"); 461 return(-1); 462 } 463 464 /* 465 * get random name and create temporary scratch file, unlink name 466 * so it will get removed on exit 467 */ 468 memcpy(tempbase, _TFILE_BASE, sizeof(_TFILE_BASE)); 469 if ((ffd = mkstemp(tempfile)) < 0) { 470 syswarn(1, errno, "Unable to create temporary file: %s", 471 tempfile); 472 return(-1); 473 } 474 (void)unlink(tempfile); 475 476 return(0); 477 } 478 479 /* 480 * chk_ftime() 481 * looks up entry in file time hash table. If not found, the file is 482 * added to the hash table and the file named stored in the scratch file. 483 * If a file with the same name is found, the file times are compared and 484 * the most recent file time is retained. If the new file was younger (or 485 * was not in the database) the new file is selected for storage. 486 * Return: 487 * 0 if file should be added to the archive, 1 if it should be skipped, 488 * -1 on error 489 */ 490 491 int 492 chk_ftime(ARCHD *arcn) 493 { 494 FTM *pt; 495 int namelen; 496 u_int indx; 497 char ckname[PAXPATHLEN+1]; 498 499 /* 500 * no info, go ahead and add to archive 501 */ 502 if (ftab == NULL) 503 return(0); 504 505 /* 506 * hash the pathname and look up in table 507 */ 508 namelen = arcn->nlen; 509 indx = st_hash(arcn->name, namelen, F_TAB_SZ); 510 if ((pt = ftab[indx]) != NULL) { 511 /* 512 * the hash chain is not empty, walk down looking for match 513 * only read up the path names if the lengths match, speeds 514 * up the search a lot 515 */ 516 while (pt != NULL) { 517 if (pt->namelen == namelen) { 518 /* 519 * potential match, have to read the name 520 * from the scratch file. 521 */ 522 if (lseek(ffd,pt->seek,SEEK_SET) != pt->seek) { 523 syswarn(1, errno, 524 "Failed ftime table seek"); 525 return(-1); 526 } 527 if (read(ffd, ckname, namelen) != namelen) { 528 syswarn(1, errno, 529 "Failed ftime table read"); 530 return(-1); 531 } 532 533 /* 534 * if the names match, we are done 535 */ 536 if (!strncmp(ckname, arcn->name, namelen)) 537 break; 538 } 539 540 /* 541 * try the next entry on the chain 542 */ 543 pt = pt->fow; 544 } 545 546 if (pt != NULL) { 547 /* 548 * found the file, compare the times, save the newer 549 */ 550 if (timespeccmp(&arcn->sb.st_mtim, &pt->mtim, >)) { 551 /* 552 * file is newer 553 */ 554 pt->mtim = arcn->sb.st_mtim; 555 return(0); 556 } 557 /* 558 * file is older 559 */ 560 return(1); 561 } 562 } 563 564 /* 565 * not in table, add it 566 */ 567 if ((pt = malloc(sizeof(FTM))) != NULL) { 568 /* 569 * add the name at the end of the scratch file, saving the 570 * offset. add the file to the head of the hash chain 571 */ 572 if ((pt->seek = lseek(ffd, 0, SEEK_END)) >= 0) { 573 if (write(ffd, arcn->name, namelen) == namelen) { 574 pt->mtim = arcn->sb.st_mtim; 575 pt->namelen = namelen; 576 pt->fow = ftab[indx]; 577 ftab[indx] = pt; 578 return(0); 579 } 580 syswarn(1, errno, "Failed write to file time table"); 581 } else 582 syswarn(1, errno, "Failed seek on file time table"); 583 } else 584 paxwarn(1, "File time table ran out of memory"); 585 586 if (pt != NULL) 587 free(pt); 588 return(-1); 589 } 590 591 /* 592 * escaping (absolute or w/"..") symlink table routines 593 * 594 * By default, an archive shouldn't be able extract to outside of the 595 * current directory. What should we do if the archive contains a symlink 596 * whose value is either absolute or contains ".." components? What we'll 597 * do is initially create the path as an empty file (to block attempts to 598 * reference _through_ it) and instead record its path and desired 599 * final value and mode. Then once all the other archive 600 * members are created (but before the pass to set timestamps on 601 * directories) we'll process those records, replacing the placeholder with 602 * the correct symlink and setting them to the correct mode, owner, group, 603 * and timestamps. 604 * 605 * Note: we also need to handle hardlinks to symlinks (barf) as well as 606 * hardlinks whose target is replaced by a later entry in the archive (barf^2). 607 * 608 * So we track things by dev+ino of the placeholder file, associating with 609 * that the value and mode of the final symlink and a list of paths that 610 * should all be hardlinks of that. We'll 'store' the symlink's desired 611 * timestamps, owner, and group by setting them on the placeholder file. 612 * 613 * The operations are: 614 * a) create an escaping symlink: create the placeholder file and add an entry 615 * for the new link 616 * b) create a hardlink: do the link. If the target turns out to be a 617 * zero-length file whose dev+ino are in the symlink table, then add this 618 * path to the list of names for that link 619 * c) perform deferred processing: for each entry, check each associated path: 620 * if it's a zero-length file with the correct dev+ino then recreate it as 621 * the specified symlink or hardlink to the first such 622 */ 623 624 struct slpath { 625 char *sp_path; 626 struct slpath *sp_next; 627 }; 628 struct slinode { 629 ino_t sli_ino; 630 char *sli_value; 631 struct slpath sli_paths; 632 struct slinode *sli_fow; /* hash table chain */ 633 dev_t sli_dev; 634 mode_t sli_mode; 635 }; 636 637 static struct slinode **slitab = NULL; 638 639 /* 640 * sltab_start() 641 * create the hash table 642 * Return: 643 * 0 if the table and file was created ok, -1 otherwise 644 */ 645 646 int 647 sltab_start(void) 648 { 649 650 if ((slitab = calloc(SL_TAB_SZ, sizeof *slitab)) == NULL) { 651 syswarn(1, errno, "symlink table"); 652 return(-1); 653 } 654 655 return(0); 656 } 657 658 /* 659 * sltab_add_sym() 660 * Create the placeholder and tracking info for an escaping symlink. 661 * Return: 662 * 0 on success, -1 otherwise 663 */ 664 665 int 666 sltab_add_sym(const char *path0, const char *value0, mode_t mode) 667 { 668 struct stat sb; 669 struct slinode *s; 670 struct slpath *p; 671 char *path, *value; 672 u_int indx; 673 int fd; 674 675 /* create the placeholder */ 676 fd = open(path0, O_WRONLY | O_CREAT | O_EXCL | O_CLOEXEC, 0600); 677 if (fd == -1) 678 return (-1); 679 if (fstat(fd, &sb) == -1) { 680 unlink(path0); 681 close(fd); 682 return (-1); 683 } 684 close(fd); 685 686 if (havechd && *path0 != '/') { 687 if ((path = realpath(path0, NULL)) == NULL) { 688 syswarn(1, errno, "Cannot canonicalize %s", path0); 689 unlink(path0); 690 return (-1); 691 } 692 } else if ((path = strdup(path0)) == NULL) { 693 syswarn(1, errno, "defered symlink path"); 694 unlink(path0); 695 return (-1); 696 } 697 if ((value = strdup(value0)) == NULL) { 698 syswarn(1, errno, "defered symlink value"); 699 unlink(path); 700 free(path); 701 return (-1); 702 } 703 704 /* now check the hash table for conflicting entry */ 705 indx = (sb.st_ino ^ sb.st_dev) % SL_TAB_SZ; 706 for (s = slitab[indx]; s != NULL; s = s->sli_fow) { 707 if (s->sli_ino != sb.st_ino || s->sli_dev != sb.st_dev) 708 continue; 709 710 /* 711 * One of our placeholders got removed behind our back and 712 * we've reused the inode. Weird, but clean up the mess. 713 */ 714 free(s->sli_value); 715 free(s->sli_paths.sp_path); 716 p = s->sli_paths.sp_next; 717 while (p != NULL) { 718 struct slpath *next_p = p->sp_next; 719 720 free(p->sp_path); 721 free(p); 722 p = next_p; 723 } 724 goto set_value; 725 } 726 727 /* Normal case: create a new node */ 728 if ((s = malloc(sizeof *s)) == NULL) { 729 syswarn(1, errno, "defered symlink"); 730 unlink(path); 731 free(path); 732 free(value); 733 return (-1); 734 } 735 s->sli_ino = sb.st_ino; 736 s->sli_dev = sb.st_dev; 737 s->sli_fow = slitab[indx]; 738 slitab[indx] = s; 739 740 set_value: 741 s->sli_paths.sp_path = path; 742 s->sli_paths.sp_next = NULL; 743 s->sli_value = value; 744 s->sli_mode = mode; 745 return (0); 746 } 747 748 /* 749 * sltab_add_link() 750 * A hardlink was created; if it looks like a placeholder, handle the 751 * tracking. 752 * Return: 753 * 0 if things are ok, -1 if something went wrong 754 */ 755 756 int 757 sltab_add_link(const char *path, const struct stat *sb) 758 { 759 struct slinode *s; 760 struct slpath *p; 761 u_int indx; 762 763 if (!S_ISREG(sb->st_mode) || sb->st_size != 0) 764 return (1); 765 766 /* find the hash table entry for this hardlink */ 767 indx = (sb->st_ino ^ sb->st_dev) % SL_TAB_SZ; 768 for (s = slitab[indx]; s != NULL; s = s->sli_fow) { 769 if (s->sli_ino != sb->st_ino || s->sli_dev != sb->st_dev) 770 continue; 771 772 if ((p = malloc(sizeof *p)) == NULL) { 773 syswarn(1, errno, "deferred symlink hardlink"); 774 return (-1); 775 } 776 if (havechd && *path != '/') { 777 if ((p->sp_path = realpath(path, NULL)) == NULL) { 778 syswarn(1, errno, "Cannot canonicalize %s", 779 path); 780 free(p); 781 return (-1); 782 } 783 } else if ((p->sp_path = strdup(path)) == NULL) { 784 syswarn(1, errno, "defered symlink hardlink path"); 785 free(p); 786 return (-1); 787 } 788 789 /* link it in */ 790 p->sp_next = s->sli_paths.sp_next; 791 s->sli_paths.sp_next = p; 792 return (0); 793 } 794 795 /* not found */ 796 return (1); 797 } 798 799 800 static int 801 sltab_process_one(struct slinode *s, struct slpath *p, const char *first, 802 int in_sig) 803 { 804 struct stat sb; 805 char *path = p->sp_path; 806 mode_t mode; 807 int err; 808 809 /* 810 * is it the expected placeholder? This can fail legimately 811 * if the archive overwrote the link with another, later entry, 812 * so don't warn. 813 */ 814 if (stat(path, &sb) != 0 || !S_ISREG(sb.st_mode) || sb.st_size != 0 || 815 sb.st_ino != s->sli_ino || sb.st_dev != s->sli_dev) 816 return (0); 817 818 if (unlink(path) && errno != ENOENT) { 819 if (!in_sig) 820 syswarn(1, errno, "deferred symlink removal"); 821 return (0); 822 } 823 824 err = 0; 825 if (first != NULL) { 826 /* add another hardlink to the existing symlink */ 827 if (linkat(AT_FDCWD, first, AT_FDCWD, path, 0) == 0) 828 return (0); 829 830 /* 831 * Couldn't hardlink the symlink for some reason, so we'll 832 * try creating it as its own symlink, but save the error 833 * for reporting if that fails. 834 */ 835 err = errno; 836 } 837 838 if (symlink(s->sli_value, path)) { 839 if (!in_sig) { 840 const char *qualifier = ""; 841 if (err) 842 qualifier = " hardlink"; 843 else 844 err = errno; 845 846 syswarn(1, err, "deferred symlink%s: %s", 847 qualifier, path); 848 } 849 return (0); 850 } 851 852 /* success, so set the id, mode, and times */ 853 mode = s->sli_mode; 854 if (pids) { 855 /* if can't set the ids, force the set[ug]id bits off */ 856 if (set_ids(path, sb.st_uid, sb.st_gid)) 857 mode &= ~(SETBITS); 858 } 859 860 if (pmode) 861 set_pmode(path, mode); 862 863 if (patime || pmtime) 864 set_ftime(path, &sb.st_mtim, &sb.st_atim, 0); 865 866 /* 867 * If we tried to link to first but failed, then this new symlink 868 * might be a better one to try in the future. Guess from the errno. 869 */ 870 if (err == 0 || err == ENOENT || err == EMLINK || err == EOPNOTSUPP) 871 return (1); 872 return (0); 873 } 874 875 /* 876 * sltab_process() 877 * Do all the delayed process for escape symlinks 878 */ 879 880 void 881 sltab_process(int in_sig) 882 { 883 struct slinode *s; 884 struct slpath *p; 885 char *first; 886 u_int indx; 887 888 if (slitab == NULL) 889 return; 890 891 /* walk across the entire hash table */ 892 for (indx = 0; indx < SL_TAB_SZ; indx++) { 893 while ((s = slitab[indx]) != NULL) { 894 /* pop this entry */ 895 slitab[indx] = s->sli_fow; 896 897 first = NULL; 898 p = &s->sli_paths; 899 while (1) { 900 struct slpath *next_p; 901 902 if (sltab_process_one(s, p, first, in_sig)) { 903 if (!in_sig) 904 free(first); 905 first = p->sp_path; 906 } else if (!in_sig) 907 free(p->sp_path); 908 909 if ((next_p = p->sp_next) == NULL) 910 break; 911 *p = *next_p; 912 if (!in_sig) 913 free(next_p); 914 } 915 if (!in_sig) { 916 free(first); 917 free(s->sli_value); 918 free(s); 919 } 920 } 921 } 922 if (!in_sig) 923 free(slitab); 924 slitab = NULL; 925 } 926 927 928 /* 929 * Interactive rename table routines 930 * 931 * The interactive rename table keeps track of the new names that the user 932 * assigns to files from tty input. Since this map is unique for each file 933 * we must store it in case there is a reference to the file later in archive 934 * (a link). Otherwise we will be unable to find the file we know was 935 * extracted. The remapping of these files is stored in a memory based hash 936 * table (it is assumed since input must come from /dev/tty, it is unlikely to 937 * be a very large table). 938 */ 939 940 /* 941 * name_start() 942 * create the interactive rename table 943 * Return: 944 * 0 if successful, -1 otherwise 945 */ 946 947 int 948 name_start(void) 949 { 950 if (ntab != NULL) 951 return(0); 952 if ((ntab = calloc(N_TAB_SZ, sizeof(NAMT *))) == NULL) { 953 paxwarn(1, "Cannot allocate memory for interactive rename table"); 954 return(-1); 955 } 956 return(0); 957 } 958 959 /* 960 * add_name() 961 * add the new name to old name mapping just created by the user. 962 * If an old name mapping is found (there may be duplicate names on an 963 * archive) only the most recent is kept. 964 * Return: 965 * 0 if added, -1 otherwise 966 */ 967 968 int 969 add_name(char *oname, int onamelen, char *nname) 970 { 971 NAMT *pt; 972 u_int indx; 973 974 if (ntab == NULL) { 975 /* 976 * should never happen 977 */ 978 paxwarn(0, "No interactive rename table, links may fail"); 979 return(0); 980 } 981 982 /* 983 * look to see if we have already mapped this file, if so we 984 * will update it 985 */ 986 indx = st_hash(oname, onamelen, N_TAB_SZ); 987 if ((pt = ntab[indx]) != NULL) { 988 /* 989 * look down the has chain for the file 990 */ 991 while ((pt != NULL) && (strcmp(oname, pt->oname) != 0)) 992 pt = pt->fow; 993 994 if (pt != NULL) { 995 /* 996 * found an old mapping, replace it with the new one 997 * the user just input (if it is different) 998 */ 999 if (strcmp(nname, pt->nname) == 0) 1000 return(0); 1001 1002 free(pt->nname); 1003 if ((pt->nname = strdup(nname)) == NULL) { 1004 paxwarn(1, "Cannot update rename table"); 1005 return(-1); 1006 } 1007 return(0); 1008 } 1009 } 1010 1011 /* 1012 * this is a new mapping, add it to the table 1013 */ 1014 if ((pt = malloc(sizeof(NAMT))) != NULL) { 1015 if ((pt->oname = strdup(oname)) != NULL) { 1016 if ((pt->nname = strdup(nname)) != NULL) { 1017 pt->fow = ntab[indx]; 1018 ntab[indx] = pt; 1019 return(0); 1020 } 1021 free(pt->oname); 1022 } 1023 free(pt); 1024 } 1025 paxwarn(1, "Interactive rename table out of memory"); 1026 return(-1); 1027 } 1028 1029 /* 1030 * sub_name() 1031 * look up a link name to see if it points at a file that has been 1032 * remapped by the user. If found, the link is adjusted to contain the 1033 * new name (oname is the link to name) 1034 */ 1035 1036 void 1037 sub_name(char *oname, int *onamelen, size_t onamesize) 1038 { 1039 NAMT *pt; 1040 u_int indx; 1041 1042 if (ntab == NULL) 1043 return; 1044 /* 1045 * look the name up in the hash table 1046 */ 1047 indx = st_hash(oname, *onamelen, N_TAB_SZ); 1048 if ((pt = ntab[indx]) == NULL) 1049 return; 1050 1051 while (pt != NULL) { 1052 /* 1053 * walk down the hash chain looking for a match 1054 */ 1055 if (strcmp(oname, pt->oname) == 0) { 1056 /* 1057 * found it, replace it with the new name 1058 * and return (we know that oname has enough space) 1059 */ 1060 *onamelen = strlcpy(oname, pt->nname, onamesize); 1061 if (*onamelen >= onamesize) 1062 *onamelen = onamesize - 1; /* XXX truncate? */ 1063 return; 1064 } 1065 pt = pt->fow; 1066 } 1067 1068 /* 1069 * no match, just return 1070 */ 1071 } 1072 1073 #ifndef NOCPIO 1074 /* 1075 * device/inode mapping table routines 1076 * (used with formats that store device and inodes fields) 1077 * 1078 * device/inode mapping tables remap the device field in a archive header. The 1079 * device/inode fields are used to determine when files are hard links to each 1080 * other. However these values have very little meaning outside of that. This 1081 * database is used to solve one of two different problems. 1082 * 1083 * 1) when files are appended to an archive, while the new files may have hard 1084 * links to each other, you cannot determine if they have hard links to any 1085 * file already stored on the archive from a prior run of pax. We must assume 1086 * that these inode/device pairs are unique only within a SINGLE run of pax 1087 * (which adds a set of files to an archive). So we have to make sure the 1088 * inode/dev pairs we add each time are always unique. We do this by observing 1089 * while the inode field is very dense, the use of the dev field is fairly 1090 * sparse. Within each run of pax, we remap any device number of a new archive 1091 * member that has a device number used in a prior run and already stored in a 1092 * file on the archive. During the read phase of the append, we store the 1093 * device numbers used and mark them to not be used by any file during the 1094 * write phase. If during write we go to use one of those old device numbers, 1095 * we remap it to a new value. 1096 * 1097 * 2) Often the fields in the archive header used to store these values are 1098 * too small to store the entire value. The result is an inode or device value 1099 * which can be truncated. This really can foul up an archive. With truncation 1100 * we end up creating links between files that are really not links (after 1101 * truncation the inodes are the same value). We address that by detecting 1102 * truncation and forcing a remap of the device field to split truncated 1103 * inodes away from each other. Each truncation creates a pattern of bits that 1104 * are removed. We use this pattern of truncated bits to partition the inodes 1105 * on a single device to many different devices (each one represented by the 1106 * truncated bit pattern). All inodes on the same device that have the same 1107 * truncation pattern are mapped to the same new device. Two inodes that 1108 * truncate to the same value clearly will always have different truncation 1109 * bit patterns, so they will be split from away each other. When we spot 1110 * device truncation we remap the device number to a non truncated value. 1111 * (for more info see table.h for the data structures involved). 1112 */ 1113 1114 static DEVT *chk_dev(dev_t, int); 1115 1116 /* 1117 * dev_start() 1118 * create the device mapping table 1119 * Return: 1120 * 0 if successful, -1 otherwise 1121 */ 1122 1123 int 1124 dev_start(void) 1125 { 1126 if (dtab != NULL) 1127 return(0); 1128 if ((dtab = calloc(D_TAB_SZ, sizeof(DEVT *))) == NULL) { 1129 paxwarn(1, "Cannot allocate memory for device mapping table"); 1130 return(-1); 1131 } 1132 return(0); 1133 } 1134 1135 /* 1136 * add_dev() 1137 * add a device number to the table. this will force the device to be 1138 * remapped to a new value if it be used during a write phase. This 1139 * function is called during the read phase of an append to prohibit the 1140 * use of any device number already in the archive. 1141 * Return: 1142 * 0 if added ok, -1 otherwise 1143 */ 1144 1145 int 1146 add_dev(ARCHD *arcn) 1147 { 1148 if (chk_dev(arcn->sb.st_dev, 1) == NULL) 1149 return(-1); 1150 return(0); 1151 } 1152 1153 /* 1154 * chk_dev() 1155 * check for a device value in the device table. If not found and the add 1156 * flag is set, it is added. This does NOT assign any mapping values, just 1157 * adds the device number as one that need to be remapped. If this device 1158 * is already mapped, just return with a pointer to that entry. 1159 * Return: 1160 * pointer to the entry for this device in the device map table. Null 1161 * if the add flag is not set and the device is not in the table (it is 1162 * not been seen yet). If add is set and the device cannot be added, null 1163 * is returned (indicates an error). 1164 */ 1165 1166 static DEVT * 1167 chk_dev(dev_t dev, int add) 1168 { 1169 DEVT *pt; 1170 u_int indx; 1171 1172 if (dtab == NULL) 1173 return(NULL); 1174 /* 1175 * look to see if this device is already in the table 1176 */ 1177 indx = ((unsigned)dev) % D_TAB_SZ; 1178 if ((pt = dtab[indx]) != NULL) { 1179 while ((pt != NULL) && (pt->dev != dev)) 1180 pt = pt->fow; 1181 1182 /* 1183 * found it, return a pointer to it 1184 */ 1185 if (pt != NULL) 1186 return(pt); 1187 } 1188 1189 /* 1190 * not in table, we add it only if told to as this may just be a check 1191 * to see if a device number is being used. 1192 */ 1193 if (add == 0) 1194 return(NULL); 1195 1196 /* 1197 * allocate a node for this device and add it to the front of the hash 1198 * chain. Note we do not assign remaps values here, so the pt->list 1199 * list must be NULL. 1200 */ 1201 if ((pt = malloc(sizeof(DEVT))) == NULL) { 1202 paxwarn(1, "Device map table out of memory"); 1203 return(NULL); 1204 } 1205 pt->dev = dev; 1206 pt->list = NULL; 1207 pt->fow = dtab[indx]; 1208 dtab[indx] = pt; 1209 return(pt); 1210 } 1211 /* 1212 * map_dev() 1213 * given an inode and device storage mask (the mask has a 1 for each bit 1214 * the archive format is able to store in a header), we check for inode 1215 * and device truncation and remap the device as required. Device mapping 1216 * can also occur when during the read phase of append a device number was 1217 * seen (and was marked as do not use during the write phase). WE ASSUME 1218 * that unsigned longs are the same size or bigger than the fields used 1219 * for ino_t and dev_t. If not the types will have to be changed. 1220 * Return: 1221 * 0 if all ok, -1 otherwise. 1222 */ 1223 1224 int 1225 map_dev(ARCHD *arcn, u_long dev_mask, u_long ino_mask) 1226 { 1227 DEVT *pt; 1228 DLIST *dpt; 1229 static dev_t lastdev = 0; /* next device number to try */ 1230 int trc_ino = 0; 1231 int trc_dev = 0; 1232 ino_t trunc_bits = 0; 1233 ino_t nino; 1234 1235 if (dtab == NULL) 1236 return(0); 1237 /* 1238 * check for device and inode truncation, and extract the truncated 1239 * bit pattern. 1240 */ 1241 if ((arcn->sb.st_dev & (dev_t)dev_mask) != arcn->sb.st_dev) 1242 ++trc_dev; 1243 if ((nino = arcn->sb.st_ino & (ino_t)ino_mask) != arcn->sb.st_ino) { 1244 ++trc_ino; 1245 trunc_bits = arcn->sb.st_ino & (ino_t)(~ino_mask); 1246 } 1247 1248 /* 1249 * see if this device is already being mapped, look up the device 1250 * then find the truncation bit pattern which applies 1251 */ 1252 if ((pt = chk_dev(arcn->sb.st_dev, 0)) != NULL) { 1253 /* 1254 * this device is already marked to be remapped 1255 */ 1256 for (dpt = pt->list; dpt != NULL; dpt = dpt->fow) 1257 if (dpt->trunc_bits == trunc_bits) 1258 break; 1259 1260 if (dpt != NULL) { 1261 /* 1262 * we are being remapped for this device and pattern 1263 * change the device number to be stored and return 1264 */ 1265 arcn->sb.st_dev = dpt->dev; 1266 arcn->sb.st_ino = nino; 1267 return(0); 1268 } 1269 } else { 1270 /* 1271 * this device is not being remapped YET. if we do not have any 1272 * form of truncation, we do not need a remap 1273 */ 1274 if (!trc_ino && !trc_dev) 1275 return(0); 1276 1277 /* 1278 * we have truncation, have to add this as a device to remap 1279 */ 1280 if ((pt = chk_dev(arcn->sb.st_dev, 1)) == NULL) 1281 goto bad; 1282 1283 /* 1284 * if we just have a truncated inode, we have to make sure that 1285 * all future inodes that do not truncate (they have the 1286 * truncation pattern of all 0's) continue to map to the same 1287 * device number. We probably have already written inodes with 1288 * this device number to the archive with the truncation 1289 * pattern of all 0's. So we add the mapping for all 0's to the 1290 * same device number. 1291 */ 1292 if (!trc_dev && (trunc_bits != 0)) { 1293 if ((dpt = malloc(sizeof(DLIST))) == NULL) 1294 goto bad; 1295 dpt->trunc_bits = 0; 1296 dpt->dev = arcn->sb.st_dev; 1297 dpt->fow = pt->list; 1298 pt->list = dpt; 1299 } 1300 } 1301 1302 /* 1303 * look for a device number not being used. We must watch for wrap 1304 * around on lastdev (so we do not get stuck looking forever!) 1305 */ 1306 while (++lastdev > 0) { 1307 if (chk_dev(lastdev, 0) != NULL) 1308 continue; 1309 /* 1310 * found an unused value. If we have reached truncation point 1311 * for this format we are hosed, so we give up. Otherwise we 1312 * mark it as being used. 1313 */ 1314 if (((lastdev & ((dev_t)dev_mask)) != lastdev) || 1315 (chk_dev(lastdev, 1) == NULL)) 1316 goto bad; 1317 break; 1318 } 1319 1320 if ((lastdev <= 0) || ((dpt = malloc(sizeof(DLIST))) == NULL)) 1321 goto bad; 1322 1323 /* 1324 * got a new device number, store it under this truncation pattern. 1325 * change the device number this file is being stored with. 1326 */ 1327 dpt->trunc_bits = trunc_bits; 1328 dpt->dev = lastdev; 1329 dpt->fow = pt->list; 1330 pt->list = dpt; 1331 arcn->sb.st_dev = lastdev; 1332 arcn->sb.st_ino = nino; 1333 return(0); 1334 1335 bad: 1336 paxwarn(1, "Unable to fix truncated inode/device field when storing %s", 1337 arcn->name); 1338 paxwarn(0, "Archive may create improper hard links when extracted"); 1339 return(0); 1340 } 1341 #endif /* NOCPIO */ 1342 1343 /* 1344 * directory access/mod time reset table routines (for directories READ by pax) 1345 * 1346 * The pax -t flag requires that access times of archive files be the same 1347 * before being read by pax. For regular files, access time is restored after 1348 * the file has been copied. This database provides the same functionality for 1349 * directories read during file tree traversal. Restoring directory access time 1350 * is more complex than files since directories may be read several times until 1351 * all the descendants in their subtree are visited by fts. Directory access 1352 * and modification times are stored during the fts pre-order visit (done 1353 * before any descendants in the subtree are visited) and restored after the 1354 * fts post-order visit (after all the descendants have been visited). In the 1355 * case of premature exit from a subtree (like from the effects of -n), any 1356 * directory entries left in this database are reset during final cleanup 1357 * operations of pax. Entries are hashed by inode number for fast lookup. 1358 */ 1359 1360 /* 1361 * atdir_start() 1362 * create the directory access time database for directories READ by pax. 1363 * Return: 1364 * 0 is created ok, -1 otherwise. 1365 */ 1366 1367 int 1368 atdir_start(void) 1369 { 1370 if (atab != NULL) 1371 return(0); 1372 if ((atab = calloc(A_TAB_SZ, sizeof(ATDIR *))) == NULL) { 1373 paxwarn(1,"Cannot allocate space for directory access time table"); 1374 return(-1); 1375 } 1376 return(0); 1377 } 1378 1379 1380 /* 1381 * atdir_end() 1382 * walk through the directory access time table and reset the access time 1383 * of any directory who still has an entry left in the database. These 1384 * entries are for directories READ by pax 1385 */ 1386 1387 void 1388 atdir_end(void) 1389 { 1390 ATDIR *pt; 1391 int i; 1392 1393 if (atab == NULL) 1394 return; 1395 /* 1396 * for each non-empty hash table entry reset all the directories 1397 * chained there. 1398 */ 1399 for (i = 0; i < A_TAB_SZ; ++i) { 1400 if ((pt = atab[i]) == NULL) 1401 continue; 1402 /* 1403 * remember to force the times, set_ftime() looks at pmtime 1404 * and patime, which only applies to things CREATED by pax, 1405 * not read by pax. Read time reset is controlled by -t. 1406 */ 1407 for (; pt != NULL; pt = pt->fow) 1408 set_attr(&pt->ft, 1, 0, 0, 0); 1409 } 1410 } 1411 1412 /* 1413 * add_atdir() 1414 * add a directory to the directory access time table. Table is hashed 1415 * and chained by inode number. This is for directories READ by pax 1416 */ 1417 1418 void 1419 add_atdir(char *fname, dev_t dev, ino_t ino, const struct timespec *mtimp, 1420 const struct timespec *atimp) 1421 { 1422 ATDIR *pt; 1423 sigset_t allsigs, savedsigs; 1424 u_int indx; 1425 1426 if (atab == NULL) 1427 return; 1428 1429 /* 1430 * make sure this directory is not already in the table, if so just 1431 * return (the older entry always has the correct time). The only 1432 * way this will happen is when the same subtree can be traversed by 1433 * different args to pax and the -n option is aborting fts out of a 1434 * subtree before all the post-order visits have been made. 1435 */ 1436 indx = ((unsigned)ino) % A_TAB_SZ; 1437 if ((pt = atab[indx]) != NULL) { 1438 while (pt != NULL) { 1439 if ((pt->ft.ft_ino == ino) && (pt->ft.ft_dev == dev)) 1440 break; 1441 pt = pt->fow; 1442 } 1443 1444 /* 1445 * oops, already there. Leave it alone. 1446 */ 1447 if (pt != NULL) 1448 return; 1449 } 1450 1451 /* 1452 * add it to the front of the hash chain 1453 */ 1454 sigfillset(&allsigs); 1455 sigprocmask(SIG_BLOCK, &allsigs, &savedsigs); 1456 if ((pt = malloc(sizeof *pt)) != NULL) { 1457 if ((pt->ft.ft_name = strdup(fname)) != NULL) { 1458 pt->ft.ft_dev = dev; 1459 pt->ft.ft_ino = ino; 1460 pt->ft.ft_mtim = *mtimp; 1461 pt->ft.ft_atim = *atimp; 1462 pt->fow = atab[indx]; 1463 atab[indx] = pt; 1464 sigprocmask(SIG_SETMASK, &savedsigs, NULL); 1465 return; 1466 } 1467 free(pt); 1468 } 1469 1470 sigprocmask(SIG_SETMASK, &savedsigs, NULL); 1471 paxwarn(1, "Directory access time reset table ran out of memory"); 1472 } 1473 1474 /* 1475 * get_atdir() 1476 * look up a directory by inode and device number to obtain the access 1477 * and modification time you want to set to. If found, the modification 1478 * and access time parameters are set and the entry is removed from the 1479 * table (as it is no longer needed). These are for directories READ by 1480 * pax 1481 * Return: 1482 * 0 if found, -1 if not found. 1483 */ 1484 1485 int 1486 do_atdir(const char *name, dev_t dev, ino_t ino) 1487 { 1488 ATDIR *pt; 1489 ATDIR **ppt; 1490 sigset_t allsigs, savedsigs; 1491 u_int indx; 1492 1493 if (atab == NULL) 1494 return(-1); 1495 /* 1496 * hash by inode and search the chain for an inode and device match 1497 */ 1498 indx = ((unsigned)ino) % A_TAB_SZ; 1499 if ((pt = atab[indx]) == NULL) 1500 return(-1); 1501 1502 ppt = &(atab[indx]); 1503 while (pt != NULL) { 1504 if ((pt->ft.ft_ino == ino) && (pt->ft.ft_dev == dev)) 1505 break; 1506 /* 1507 * no match, go to next one 1508 */ 1509 ppt = &(pt->fow); 1510 pt = pt->fow; 1511 } 1512 1513 /* 1514 * return if we did not find it. 1515 */ 1516 if (pt == NULL || pt->ft.ft_name == NULL || 1517 strcmp(name, pt->ft.ft_name) == 0) 1518 return(-1); 1519 1520 /* 1521 * found it. set the times and remove the entry from the table. 1522 */ 1523 set_attr(&pt->ft, 1, 0, 0, 0); 1524 sigfillset(&allsigs); 1525 sigprocmask(SIG_BLOCK, &allsigs, &savedsigs); 1526 *ppt = pt->fow; 1527 sigprocmask(SIG_SETMASK, &savedsigs, NULL); 1528 free(pt->ft.ft_name); 1529 free(pt); 1530 return(0); 1531 } 1532 1533 /* 1534 * directory access mode and time storage routines (for directories CREATED 1535 * by pax). 1536 * 1537 * Pax requires that extracted directories, by default, have their access/mod 1538 * times and permissions set to the values specified in the archive. During the 1539 * actions of extracting (and creating the destination subtree during -rw copy) 1540 * directories extracted may be modified after being created. Even worse is 1541 * that these directories may have been created with file permissions which 1542 * prohibits any descendants of these directories from being extracted. When 1543 * directories are created by pax, access rights may be added to permit the 1544 * creation of files in their subtree. Every time pax creates a directory, the 1545 * times and file permissions specified by the archive are stored. After all 1546 * files have been extracted (or copied), these directories have their times 1547 * and file modes reset to the stored values. The directory info is restored in 1548 * reverse order as entries were added from root to leaf: to restore atime 1549 * properly, we must go backwards. 1550 */ 1551 1552 /* 1553 * dir_start() 1554 * set up the directory time and file mode storage for directories CREATED 1555 * by pax. 1556 * Return: 1557 * 0 if ok, -1 otherwise 1558 */ 1559 1560 int 1561 dir_start(void) 1562 { 1563 if (dirp != NULL) 1564 return(0); 1565 1566 dirsize = DIRP_SIZE; 1567 if ((dirp = reallocarray(NULL, dirsize, sizeof(DIRDATA))) == NULL) { 1568 paxwarn(1, "Unable to allocate memory for directory times"); 1569 return(-1); 1570 } 1571 return(0); 1572 } 1573 1574 /* 1575 * add_dir() 1576 * add the mode and times for a newly CREATED directory 1577 * name is name of the directory, psb the stat buffer with the data in it, 1578 * frc_mode is a flag that says whether to force the setting of the mode 1579 * (ignoring the user set values for preserving file mode). Frc_mode is 1580 * for the case where we created a file and found that the resulting 1581 * directory was not writeable and the user asked for file modes to NOT 1582 * be preserved. (we have to preserve what was created by default, so we 1583 * have to force the setting at the end. this is stated explicitly in the 1584 * pax spec) 1585 */ 1586 1587 void 1588 add_dir(char *name, struct stat *psb, int frc_mode) 1589 { 1590 DIRDATA *dblk; 1591 sigset_t allsigs, savedsigs; 1592 char realname[PATH_MAX], *rp; 1593 1594 if (dirp == NULL) 1595 return; 1596 1597 if (havechd && *name != '/') { 1598 if ((rp = realpath(name, realname)) == NULL) { 1599 paxwarn(1, "Cannot canonicalize %s", name); 1600 return; 1601 } 1602 name = rp; 1603 } 1604 if (dircnt == dirsize) { 1605 dblk = reallocarray(dirp, dirsize, 2 * sizeof(DIRDATA)); 1606 if (dblk == NULL) { 1607 paxwarn(1, "Unable to store mode and times for created" 1608 " directory: %s", name); 1609 return; 1610 } 1611 sigprocmask(SIG_BLOCK, &allsigs, &savedsigs); 1612 dirp = dblk; 1613 dirsize *= 2; 1614 sigprocmask(SIG_SETMASK, &savedsigs, NULL); 1615 } 1616 dblk = &dirp[dircnt]; 1617 if ((dblk->ft.ft_name = strdup(name)) == NULL) { 1618 paxwarn(1, "Unable to store mode and times for created" 1619 " directory: %s", name); 1620 return; 1621 } 1622 dblk->ft.ft_mtim = psb->st_mtim; 1623 dblk->ft.ft_atim = psb->st_atim; 1624 dblk->ft.ft_ino = psb->st_ino; 1625 dblk->ft.ft_dev = psb->st_dev; 1626 dblk->mode = psb->st_mode & ABITS; 1627 dblk->frc_mode = frc_mode; 1628 sigprocmask(SIG_BLOCK, &allsigs, &savedsigs); 1629 ++dircnt; 1630 sigprocmask(SIG_SETMASK, &savedsigs, NULL); 1631 } 1632 1633 /* 1634 * delete_dir() 1635 * When we rmdir a directory, we may want to make sure we don't 1636 * later warn about being unable to set its mode and times. 1637 */ 1638 1639 void 1640 delete_dir(dev_t dev, ino_t ino) 1641 { 1642 DIRDATA *dblk; 1643 char *name; 1644 size_t i; 1645 1646 if (dirp == NULL) 1647 return; 1648 for (i = 0; i < dircnt; i++) { 1649 dblk = &dirp[i]; 1650 1651 if (dblk->ft.ft_name == NULL) 1652 continue; 1653 if (dblk->ft.ft_dev == dev && dblk->ft.ft_ino == ino) { 1654 name = dblk->ft.ft_name; 1655 dblk->ft.ft_name = NULL; 1656 free(name); 1657 break; 1658 } 1659 } 1660 } 1661 1662 /* 1663 * proc_dir(int in_sig) 1664 * process all file modes and times stored for directories CREATED 1665 * by pax. If in_sig is set, we're in a signal handler and can't 1666 * free stuff. 1667 */ 1668 1669 void 1670 proc_dir(int in_sig) 1671 { 1672 DIRDATA *dblk; 1673 size_t cnt; 1674 1675 if (dirp == NULL) 1676 return; 1677 /* 1678 * read backwards through the file and process each directory 1679 */ 1680 cnt = dircnt; 1681 while (cnt-- > 0) { 1682 dblk = &dirp[cnt]; 1683 /* 1684 * If we remove a directory we created, we replace the 1685 * ft_name with NULL. Ignore those. 1686 */ 1687 if (dblk->ft.ft_name == NULL) 1688 continue; 1689 1690 /* 1691 * frc_mode set, make sure we set the file modes even if 1692 * the user didn't ask for it (see file_subs.c for more info) 1693 */ 1694 set_attr(&dblk->ft, 0, dblk->mode, pmode || dblk->frc_mode, 1695 in_sig); 1696 if (!in_sig) 1697 free(dblk->ft.ft_name); 1698 } 1699 1700 if (!in_sig) 1701 free(dirp); 1702 dirp = NULL; 1703 dircnt = 0; 1704 } 1705 1706 /* 1707 * database independent routines 1708 */ 1709 1710 /* 1711 * st_hash() 1712 * hashes filenames to a u_int for hashing into a table. Looks at the tail 1713 * end of file, as this provides far better distribution than any other 1714 * part of the name. For performance reasons we only care about the last 1715 * MAXKEYLEN chars (should be at LEAST large enough to pick off the file 1716 * name). Was tested on 500,000 name file tree traversal from the root 1717 * and gave almost a perfectly uniform distribution of keys when used with 1718 * prime sized tables (MAXKEYLEN was 128 in test). Hashes (sizeof int) 1719 * chars at a time and pads with 0 for last addition. 1720 * Return: 1721 * the hash value of the string MOD (%) the table size. 1722 */ 1723 1724 u_int 1725 st_hash(const char *name, int len, int tabsz) 1726 { 1727 const char *pt; 1728 char *dest; 1729 const char *end; 1730 int i; 1731 u_int key = 0; 1732 int steps; 1733 int res; 1734 u_int val; 1735 1736 /* 1737 * only look at the tail up to MAXKEYLEN, we do not need to waste 1738 * time here (remember these are pathnames, the tail is what will 1739 * spread out the keys) 1740 */ 1741 if (len > MAXKEYLEN) { 1742 pt = &(name[len - MAXKEYLEN]); 1743 len = MAXKEYLEN; 1744 } else 1745 pt = name; 1746 1747 /* 1748 * calculate the number of u_int size steps in the string and if 1749 * there is a runt to deal with 1750 */ 1751 steps = len/sizeof(u_int); 1752 res = len % sizeof(u_int); 1753 1754 /* 1755 * add up the value of the string in unsigned integer sized pieces 1756 * too bad we cannot have unsigned int aligned strings, then we 1757 * could avoid the expensive copy. 1758 */ 1759 for (i = 0; i < steps; ++i) { 1760 end = pt + sizeof(u_int); 1761 dest = (char *)&val; 1762 while (pt < end) 1763 *dest++ = *pt++; 1764 key += val; 1765 } 1766 1767 /* 1768 * add in the runt padded with zero to the right 1769 */ 1770 if (res) { 1771 val = 0; 1772 end = pt + res; 1773 dest = (char *)&val; 1774 while (pt < end) 1775 *dest++ = *pt++; 1776 key += val; 1777 } 1778 1779 /* 1780 * return the result mod the table size 1781 */ 1782 return(key % tabsz); 1783 } 1784