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