1 /*- 2 * Copyright (c) 1992 Keith Muller. 3 * Copyright (c) 1992 The Regents of the University of California. 4 * All rights reserved. 5 * 6 * This code is derived from software contributed to Berkeley by 7 * Keith Muller of the University of California, San Diego. 8 * 9 * %sccs.include.redist.c% 10 */ 11 12 #ifndef lint 13 static char sccsid[] = "@(#)ar_subs.c 1.3 (Berkeley) 01/16/93"; 14 #endif /* not lint */ 15 16 #include <sys/types.h> 17 #include <sys/time.h> 18 #include <sys/stat.h> 19 #include <sys/param.h> 20 #include <signal.h> 21 #include <string.h> 22 #include <stdio.h> 23 #include <ctype.h> 24 #include <fcntl.h> 25 #include <errno.h> 26 #include <unistd.h> 27 #include <stdlib.h> 28 #include "pax.h" 29 #include "extern.h" 30 31 static void wr_archive __P((register ARCHD *)); 32 static int get_arc __P((void)); 33 static int next_head __P((register ARCHD *)); 34 extern sigset_t s_mask; 35 36 /* 37 * Routines which control the overall operation modes of pax as specified by 38 * the user: list, append, read ... 39 */ 40 41 static char hdbuf[BLKMULT]; /* space for archive header on read */ 42 u_long flcnt; /* number of files processed */ 43 44 /* 45 * list() 46 * list the contents of an archive which match user supplied pattern(s) 47 * (no pattern matches all). 48 */ 49 50 #if __STDC__ 51 void 52 list(void) 53 #else 54 void 55 list() 56 #endif 57 { 58 register ARCHD *arcn; 59 register int res; 60 ARCHD archd; 61 time_t now; 62 63 arcn = &archd; 64 /* 65 * figure out archive type; pass any format specific options to the 66 * archive option processing routine; call the format init routine. We 67 * also save current time for ls_list() so we do not make a system 68 * call for each file we need to print. If verbose (vflag) start up 69 * the name and group caches. 70 */ 71 if ((get_arc() < 0) || ((*frmt->options)() < 0) || 72 ((*frmt->st_rd)() < 0)) 73 return; 74 75 if (vflag && ((uidtb_start() < 0) || (gidtb_start() < 0))) 76 return; 77 78 now = time((time_t *)NULL); 79 80 /* 81 * step through the archive until the format says it is done 82 */ 83 while (next_head(arcn) == 0) { 84 /* 85 * check for pattern, and user specified options match. 86 * When all patterns are matched we are done. 87 */ 88 if ((res = pat_match(arcn)) < 0) 89 break; 90 91 if ((res == 0) && (sel_chk(arcn) == 0)) { 92 /* 93 * pattern resulted in a selected file 94 */ 95 if (pat_sel(arcn) < 0) 96 break; 97 98 /* 99 * modify the name as requested by the user if name 100 * survives modification, do a listing of the file 101 */ 102 if ((res = mod_name(arcn)) < 0) 103 break; 104 if (res == 0) 105 ls_list(arcn, now); 106 } 107 108 /* 109 * skip to next archive format header using values calculated 110 * by the format header read routine 111 */ 112 if (rd_skip(arcn->skip + arcn->pad) == 1) 113 break; 114 } 115 116 /* 117 * all done, let format have a chance to cleanup, and make sure that 118 * the patterns supplied by the user were all matched 119 */ 120 (void)(*frmt->end_rd)(); 121 (void)sigprocmask(SIG_BLOCK, &s_mask, (sigset_t *)NULL); 122 ar_close(); 123 pat_chk(); 124 } 125 126 /* 127 * extract() 128 * extract the member(s) of an archive as specified by user supplied 129 * pattern(s) (no patterns extracts all members) 130 */ 131 132 #if __STDC__ 133 void 134 extract(void) 135 #else 136 void 137 extract() 138 #endif 139 { 140 register ARCHD *arcn; 141 register int res; 142 off_t cnt; 143 ARCHD archd; 144 struct stat sb; 145 int fd; 146 147 arcn = &archd; 148 /* 149 * figure out archive type; pass any format specific options to the 150 * archive option processing routine; call the format init routine; 151 * start up the directory modification time and access mode database 152 */ 153 if ((get_arc() < 0) || ((*frmt->options)() < 0) || 154 ((*frmt->st_rd)() < 0) || (dir_start() < 0)) 155 return; 156 157 /* 158 * When we are doing interactive rename, we store the mapping of names 159 * so we can fix up hard links files later in the archive. 160 */ 161 if (iflag && (name_start() < 0)) 162 return; 163 164 /* 165 * step through each entry on the archive until the format read routine 166 * says it is done 167 */ 168 while (next_head(arcn) == 0) { 169 170 /* 171 * check for pattern, and user specified options match. When 172 * all the patterns are matched we are done 173 */ 174 if ((res = pat_match(arcn)) < 0) 175 break; 176 177 if ((res > 0) || (sel_chk(arcn) != 0)) { 178 /* 179 * file is not selected. skip past any file data and 180 * padding and go back for the next archive member 181 */ 182 (void)rd_skip(arcn->skip + arcn->pad); 183 continue; 184 } 185 186 /* 187 * with -u or -D only extract when the archive member is newer 188 * than the file with the same name in the file system (nos 189 * test of being the same type is required). 190 * NOTE: this test is done BEFORE name modifications as 191 * specified by pax. this operation can be confusing to the 192 * user who might expect the test to be done on an existing 193 * file AFTER the name mod. In honesty the pax spec is probably 194 * flawed in this respect. 195 */ 196 if ((uflag || Dflag) && ((lstat(arcn->name, &sb) == 0))) { 197 if (uflag && Dflag) { 198 if ((arcn->sb.st_mtime <= sb.st_mtime) && 199 (arcn->sb.st_ctime <= sb.st_ctime)) { 200 (void)rd_skip(arcn->skip + arcn->pad); 201 continue; 202 } 203 } else if (Dflag) { 204 if (arcn->sb.st_ctime <= sb.st_ctime) { 205 (void)rd_skip(arcn->skip + arcn->pad); 206 continue; 207 } 208 } else if (arcn->sb.st_mtime <= sb.st_mtime) { 209 (void)rd_skip(arcn->skip + arcn->pad); 210 continue; 211 } 212 } 213 214 /* 215 * this archive member is now been selected. modify the name. 216 */ 217 if ((pat_sel(arcn) < 0) || ((res = mod_name(arcn)) < 0)) 218 break; 219 if (res > 0) { 220 /* 221 * a bad name mod, skip and purge name from link table 222 */ 223 purg_lnk(arcn); 224 (void)rd_skip(arcn->skip + arcn->pad); 225 continue; 226 } 227 228 /* 229 * Non standard -Y and -Z flag. When the exisiting file is 230 * same age or newer skip 231 */ 232 if ((Yflag || Zflag) && ((lstat(arcn->name, &sb) == 0))) { 233 if (Yflag && Zflag) { 234 if ((arcn->sb.st_mtime <= sb.st_mtime) && 235 (arcn->sb.st_ctime <= sb.st_ctime)) { 236 (void)rd_skip(arcn->skip + arcn->pad); 237 continue; 238 } 239 } else if (Yflag) { 240 if (arcn->sb.st_ctime <= sb.st_ctime) { 241 (void)rd_skip(arcn->skip + arcn->pad); 242 continue; 243 } 244 } else if (arcn->sb.st_mtime <= sb.st_mtime) { 245 (void)rd_skip(arcn->skip + arcn->pad); 246 continue; 247 } 248 } 249 250 if (vflag) { 251 (void)fputs(arcn->name, stderr); 252 vfpart = 1; 253 } 254 255 /* 256 * all ok, extract this member based on type 257 */ 258 if ((arcn->type != PAX_REG) && (arcn->type != PAX_CTG)) { 259 /* 260 * process archive members that are not regular files. 261 * throw out padding and any data that might follow the 262 * header (as determined by the format). 263 */ 264 if ((arcn->type == PAX_HLK) || (arcn->type == PAX_HRG)) 265 res = lnk_creat(arcn); 266 else 267 res = node_creat(arcn); 268 269 (void)rd_skip(arcn->skip + arcn->pad); 270 if (res < 0) 271 purg_lnk(arcn); 272 273 if (vflag && vfpart) { 274 (void)putc('\n', stderr); 275 vfpart = 0; 276 } 277 continue; 278 } 279 /* 280 * we have a file with data here. If we can not create it, skip 281 * over the data and purge the name from hard link table 282 */ 283 if ((fd = file_creat(arcn)) < 0) { 284 (void)rd_skip(arcn->skip + arcn->pad); 285 purg_lnk(arcn); 286 continue; 287 } 288 /* 289 * extract the file from the archive and skip over padding and 290 * any unprocessed data 291 */ 292 res = (*frmt->rd_data)(arcn, fd, &cnt); 293 file_close(arcn, fd); 294 if (vflag && vfpart) { 295 (void)putc('\n', stderr); 296 vfpart = 0; 297 } 298 if (!res) 299 (void)rd_skip(cnt + arcn->pad); 300 } 301 302 /* 303 * all done, restore directory modes and times as required; make sure 304 * all patterns supplied by the user were matched; block off signals 305 * to avoid chance for multiple entry into the cleanup code. 306 */ 307 (void)(*frmt->end_rd)(); 308 (void)sigprocmask(SIG_BLOCK, &s_mask, (sigset_t *)NULL); 309 ar_close(); 310 proc_dir(); 311 pat_chk(); 312 } 313 314 /* 315 * wr_archive() 316 * Write an archive. used in both creating a new archive and appends on 317 * previously written archive. 318 */ 319 320 #if __STDC__ 321 static void 322 wr_archive(register ARCHD *arcn) 323 #else 324 static void 325 wr_archive(arcn) 326 register ARCHD *arcn; 327 #endif 328 { 329 register int res; 330 register int hlk; 331 off_t cnt; 332 int (*wrf)(); 333 int fd = -1; 334 335 /* 336 * if this format supports hard link storage, start up the database 337 * that detects them. 338 */ 339 if (((hlk = frmt->hlk) == 1) && (lnk_start() < 0)) 340 return; 341 342 /* 343 * start up the file traversal code and format specific write 344 */ 345 if ((ftree_start() < 0) || ((*frmt->st_wr)() < 0)) 346 return; 347 wrf = frmt->wr; 348 349 /* 350 * When we are doing interactive rename, we store the mapping of names 351 * so we can fix up hard links files later in the archive. 352 */ 353 if (iflag && (name_start() < 0)) 354 return; 355 356 /* 357 * while there are files to archive, process them one at at time 358 */ 359 while (next_file(arcn) == 0) { 360 /* 361 * check if this file meets user specified options match. 362 */ 363 if (sel_chk(arcn) != 0) 364 continue; 365 fd = -1; 366 if (uflag) { 367 /* 368 * only archive if this file is newer than a file with 369 * the same name that is already stored on the archive 370 */ 371 if ((res = chk_ftime(arcn)) < 0) 372 break; 373 if (res > 0) 374 continue; 375 } 376 377 /* 378 * this file is considered selected now. see if this is a hard 379 * link to a file already stored 380 */ 381 ftree_sel(arcn); 382 if (hlk && (chk_lnk(arcn) < 0)) 383 break; 384 385 if ((arcn->type == PAX_REG) || (arcn->type == PAX_HRG) || 386 (arcn->type == PAX_CTG)) { 387 /* 388 * we will have to read this file. by opening it now we 389 * can avoid writing a header to the archive for a file 390 * we were later unable to read (we also purge it from 391 * the link table). 392 */ 393 if ((fd = open(arcn->org_name, O_RDONLY, 0)) < 0) { 394 syswarn(1,errno, "Unable to open %s to read", 395 arcn->org_name); 396 purg_lnk(arcn); 397 continue; 398 } 399 } 400 401 /* 402 * Now modify the name as requested by the user 403 */ 404 if ((res = mod_name(arcn)) < 0) { 405 /* 406 * name modification says to skip this file, close the 407 * file and purge link table entry 408 */ 409 rdfile_close(arcn, &fd); 410 purg_lnk(arcn); 411 break; 412 } 413 414 if ((res > 0) || (docrc && (set_crc(arcn, fd) < 0))) { 415 /* 416 * unable to obtain the crc we need, close the file, 417 * purge link table entry 418 */ 419 rdfile_close(arcn, &fd); 420 purg_lnk(arcn); 421 continue; 422 } 423 424 if (vflag) { 425 (void)fputs(arcn->name, stderr); 426 vfpart = 1; 427 } 428 ++flcnt; 429 430 /* 431 * looks safe to store the file, have the format specific 432 * routine write routine store the file header on the archive 433 */ 434 if ((res = (*wrf)(arcn)) < 0) { 435 rdfile_close(arcn, &fd); 436 break; 437 } 438 if (res > 0) { 439 /* 440 * format write says no file data needs to be stored 441 * so we are done messing with this file 442 */ 443 if (vflag && vfpart) { 444 (void)putc('\n', stderr); 445 vfpart = 0; 446 } 447 rdfile_close(arcn, &fd); 448 continue; 449 } 450 451 /* 452 * Add file data to the archive, quit on write error. if we 453 * cannot write the entire file contents to the archive we 454 * must pad the archive to replace the missing file data 455 * (otherwise during an extract the file header for the file 456 * which FOLLOWS this one will not be where we expect it to 457 * be). 458 */ 459 res = (*frmt->wr_data)(arcn, fd, &cnt); 460 rdfile_close(arcn, &fd); 461 if (vflag && vfpart) { 462 (void)putc('\n', stderr); 463 vfpart = 0; 464 } 465 if (res < 0) 466 break; 467 468 /* 469 * pad as required, cnt is number of bytes not written 470 */ 471 if (((cnt > 0) && (wr_skip(cnt) < 0)) || 472 ((arcn->pad > 0) && (wr_skip(arcn->pad) < 0))) 473 break; 474 } 475 476 /* 477 * tell format to write trailer; pad to block boundry; reset directory 478 * mode/access times, and check if all patterns supplied by the user 479 * were matched. block off signals to avoid chance for multiple entry 480 * into the cleanup code 481 */ 482 (*frmt->end_wr)(); 483 wr_fin(); 484 (void)sigprocmask(SIG_BLOCK, &s_mask, (sigset_t *)NULL); 485 ar_close(); 486 if (tflag) 487 proc_dir(); 488 ftree_chk(); 489 } 490 491 /* 492 * append() 493 * Add file to previously written archive. Archive format specified by the 494 * user must agree with archive. The archive is read first to collect 495 * modification times (if -u) and locate the archive trailer. The archive 496 * is positioned in front of the record with the trailer and wr_archive() 497 * is called to add the new members. 498 * PAX IMPLEMENTATION DETAIL NOTE: 499 * -u is implemented by adding the new members to the end of the archive. 500 * Care is taken so that these do not end up as links to the older 501 * version of the same file already stored in the archive. It is expected 502 * when extraction occurs these newer versions will over-write the older 503 * ones stored "earlier" in the archive (this may be a bad assumption as 504 * it depends on the implementation of the program doing the extraction). 505 * It is really difficult to splice in members without either re-writing 506 * the entire archive (from the point were the old version was), or having 507 * assistance of the format specification in terms of a special update 508 * header that invalidates a previous archive record. The posix spec left 509 * the method used to implement -u unspecified. This pax is able to 510 * over write existing files that it creates. 511 */ 512 513 #if __STDC__ 514 void 515 append(void) 516 #else 517 void 518 append() 519 #endif 520 { 521 register ARCHD *arcn; 522 register int res; 523 ARCHD archd; 524 FSUB *orgfrmt; 525 int udev; 526 off_t tlen; 527 528 arcn = &archd; 529 orgfrmt = frmt; 530 531 /* 532 * Do not allow an append operation if the actual archive is of a 533 * different format than the user specified foramt. 534 */ 535 if (get_arc() < 0) 536 return; 537 if ((orgfrmt != NULL) && (orgfrmt != frmt)) { 538 warn(1, "Cannot mix current archive format %s with %s", 539 frmt->name, orgfrmt->name); 540 return; 541 } 542 543 /* 544 * pass the format any options and start up format 545 */ 546 if (((*frmt->options)() < 0) || ((*frmt->st_rd)() < 0)) 547 return; 548 549 /* 550 * if we only are adding members that are newer, we need to save the 551 * mod times for all files we see. 552 */ 553 if (uflag && (ftime_start() < 0)) 554 return; 555 556 /* 557 * some archive formats encode hard links by recording the device and 558 * file serial number (inode) but copy the file anyway (multiple times) 559 * to the archive. When we append, we run the risk that newly added 560 * files may have the same device and inode numbers as those recorded 561 * on the archive but during a previous run. If this happens, when the 562 * archive is extracted we get INCORRECT hard links. We avoid this by 563 * remapping the device numbers so that newly added files will never 564 * use the same device number as one found on the archive. remapping 565 * allows new members to safely have links among themselves. remapping 566 * also avoids problems with file inode (serial number) truncations 567 * when the inode number is larger than storage space in the archive 568 * header. See the remap routines for more details. 569 */ 570 if ((udev = frmt->udev) && (dev_start() < 0)) 571 return; 572 573 /* 574 * reading the archive may take a long time. If verbose tell the user 575 */ 576 if (vflag) { 577 (void)fputs("pax: Reading archive to position at the end...", 578 stderr); 579 vfpart = 1; 580 } 581 582 /* 583 * step through the archive until the format says it is done 584 */ 585 while (next_head(arcn) == 0) { 586 /* 587 * check if this file meets user specified options. 588 */ 589 if (sel_chk(arcn) != 0) { 590 if (rd_skip(arcn->skip + arcn->pad) == 1) 591 break; 592 continue; 593 } 594 595 if (uflag) { 596 /* 597 * see if this is the newest version of this file has 598 * already been seen, if so skip. 599 */ 600 if ((res = chk_ftime(arcn)) < 0) 601 break; 602 if (res > 0) { 603 if (rd_skip(arcn->skip + arcn->pad) == 1) 604 break; 605 continue; 606 } 607 } 608 609 /* 610 * Store this device number. Device numbers seen during the 611 * read phase of append will cause newly appended files with a 612 * device number seen in the old part of the archive to be 613 * remapped to an unused device number. 614 */ 615 if ((udev && (add_dev(arcn) < 0)) || 616 (rd_skip(arcn->skip + arcn->pad) == 1)) 617 break; 618 } 619 620 /* 621 * done, finish up read and get the number of bytes to back up so we 622 * can add new members. The format might have used the hard link table, 623 * purge it. 624 */ 625 tlen = (*frmt->end_rd)(); 626 lnk_end(); 627 628 /* 629 * try to postion for write, if this fails quit. if any error occurs, 630 * we will refuse to write 631 */ 632 if (appnd_start(tlen) < 0) 633 return; 634 635 /* 636 * tell the user we are done reading. 637 */ 638 if (vflag && vfpart) { 639 (void)fputs("done.\n", stderr); 640 vfpart = 0; 641 } 642 643 /* 644 * go to the writing phase to add the new members 645 */ 646 wr_archive(arcn); 647 } 648 649 /* 650 * archive() 651 * write a new archive 652 */ 653 654 #if __STDC__ 655 void 656 archive(void) 657 #else 658 void 659 archive() 660 #endif 661 { 662 ARCHD archd; 663 664 /* 665 * if we only are adding members that are newer, we need to save the 666 * mod times for all files; set up for writing; pass the format any 667 * options write the archive 668 */ 669 if ((uflag && (ftime_start() < 0)) || (wr_start() < 0)) 670 return; 671 if ((*frmt->options)() < 0) 672 return; 673 674 wr_archive(&archd); 675 } 676 677 /* 678 * copy() 679 * copy files from one part of the file system to another. this does not 680 * use any archive storage. The EFFECT OF THE COPY IS THE SAME as if an 681 * archive was written and then extracted in the destination directory 682 * (except the files are forced to be under the destination directory). 683 */ 684 685 #if __STDC__ 686 void 687 copy(void) 688 #else 689 void 690 copy() 691 #endif 692 { 693 register ARCHD *arcn; 694 register int res; 695 register int fddest; 696 register char *dest_pt; 697 register int dlen; 698 register int drem; 699 int fdsrc = -1; 700 struct stat sb; 701 ARCHD archd; 702 char dirbuf[PAXPATHLEN+1]; 703 704 arcn = &archd; 705 /* 706 * set up the destination dir path and make sure it is a directory. We 707 * make sure we have a trailing / on the destination 708 */ 709 dlen = l_strncpy(dirbuf, dirptr, PAXPATHLEN); 710 dest_pt = dirbuf + dlen; 711 if (*(dest_pt-1) != '/') { 712 *dest_pt++ = '/'; 713 ++dlen; 714 } 715 *dest_pt = '\0'; 716 drem = PAXPATHLEN - dlen; 717 718 if (stat(dirptr, &sb) < 0) { 719 syswarn(1, errno, "Cannot access destination directory %s", 720 dirptr); 721 return; 722 } 723 if (!S_ISDIR(sb.st_mode)) { 724 warn(1, "Destination is not a directory %s", dirptr); 725 return; 726 } 727 728 /* 729 * start up the hard link table; file traversal routines and the 730 * modification time and access mode database 731 */ 732 if ((lnk_start() < 0) || (ftree_start() < 0) || (dir_start() < 0)) 733 return; 734 735 /* 736 * When we are doing interactive rename, we store the mapping of names 737 * so we can fix up hard links files later in the archive. 738 */ 739 if (iflag && (name_start() < 0)) 740 return; 741 742 /* 743 * set up to cp file trees 744 */ 745 cp_start(); 746 747 /* 748 * while there are files to archive, process them 749 */ 750 while (next_file(arcn) == 0) { 751 fdsrc = -1; 752 753 /* 754 * check if this file meets user specified options 755 */ 756 if (sel_chk(arcn) != 0) 757 continue; 758 759 /* 760 * if there is already a file in the destination directory with 761 * the same name and it is newer, skip the one stored on the 762 * archive. 763 * NOTE: this test is done BEFORE name modifications as 764 * specified by pax. this can be confusing to the user who 765 * might expect the test to be done on an existing file AFTER 766 * the name mod. In honesty the pax spec is probably flawed in 767 * this respect 768 */ 769 if (uflag || Dflag) { 770 /* 771 * create the destination name 772 */ 773 if (*(arcn->name) == '/') 774 res = 1; 775 else 776 res = 0; 777 if ((arcn->nlen - res) > drem) { 778 warn(1, "Destination pathname too long %s", 779 arcn->name); 780 continue; 781 } 782 (void)strncpy(dest_pt, arcn->name + res, drem); 783 dirbuf[PAXPATHLEN] = '\0'; 784 785 /* 786 * if existing file is same age or newer skip 787 */ 788 res = lstat(dirbuf, &sb); 789 *dest_pt = '\0'; 790 791 if (res == 0) { 792 if (uflag && Dflag) { 793 if ((arcn->sb.st_mtime<=sb.st_mtime) && 794 (arcn->sb.st_ctime<=sb.st_ctime)) 795 continue; 796 } else if (Dflag) { 797 if (arcn->sb.st_ctime <= sb.st_ctime) 798 continue; 799 } else if (arcn->sb.st_mtime <= sb.st_mtime) 800 continue; 801 } 802 } 803 804 /* 805 * this file is considered selected. See if this is a hard link 806 * to a previous file; modify the name as requested by the 807 * user; set the final destination. 808 */ 809 ftree_sel(arcn); 810 if ((chk_lnk(arcn) < 0) || ((res = mod_name(arcn)) < 0)) 811 break; 812 if ((res > 0) || (set_dest(arcn, dirbuf, dlen) < 0)) { 813 /* 814 * skip file, purge from link table 815 */ 816 purg_lnk(arcn); 817 continue; 818 } 819 820 /* 821 * Non standard -Y and -Z flag. When the exisiting file is 822 * same age or newer skip 823 */ 824 if ((Yflag || Zflag) && ((lstat(arcn->name, &sb) == 0))) { 825 if (Yflag && Zflag) { 826 if ((arcn->sb.st_mtime <= sb.st_mtime) && 827 (arcn->sb.st_ctime <= sb.st_ctime)) 828 continue; 829 } else if (Yflag) { 830 if (arcn->sb.st_ctime <= sb.st_ctime) 831 continue; 832 } else if (arcn->sb.st_mtime <= sb.st_mtime) 833 continue; 834 } 835 836 if (vflag) { 837 (void)fputs(arcn->name, stderr); 838 vfpart = 1; 839 } 840 ++flcnt; 841 842 /* 843 * try to create a hard link to the src file if requested 844 * but make sure we are not trying to overwrite ourselves. 845 */ 846 if (lflag) 847 res = cross_lnk(arcn); 848 else 849 res = chk_same(arcn); 850 if (res <= 0) { 851 if (vflag && vfpart) { 852 (void)putc('\n', stderr); 853 vfpart = 0; 854 } 855 continue; 856 } 857 858 /* 859 * have to create a new file 860 */ 861 if ((arcn->type != PAX_REG) && (arcn->type != PAX_CTG)) { 862 /* 863 * create a link or special file 864 */ 865 if ((arcn->type == PAX_HLK) || (arcn->type == PAX_HRG)) 866 res = lnk_creat(arcn); 867 else 868 res = node_creat(arcn); 869 if (res < 0) 870 purg_lnk(arcn); 871 if (vflag && vfpart) { 872 (void)putc('\n', stderr); 873 vfpart = 0; 874 } 875 continue; 876 } 877 878 /* 879 * have to copy a regular file to the destination directory. 880 * first open source file and then create the destination file 881 */ 882 if ((fdsrc = open(arcn->org_name, O_RDONLY, 0)) < 0) { 883 syswarn(1, errno, "Unable to open %s to read", 884 arcn->org_name); 885 purg_lnk(arcn); 886 continue; 887 } 888 if ((fddest = file_creat(arcn)) < 0) { 889 rdfile_close(arcn, &fdsrc); 890 purg_lnk(arcn); 891 continue; 892 } 893 894 /* 895 * copy source file data to the destination file 896 */ 897 cp_file(arcn, fdsrc, fddest); 898 file_close(arcn, fddest); 899 rdfile_close(arcn, &fdsrc); 900 901 if (vflag && vfpart) { 902 (void)putc('\n', stderr); 903 vfpart = 0; 904 } 905 } 906 907 /* 908 * restore directory modes and times as required; make sure all 909 * patterns were selected block off signals to avoid chance for 910 * multiple entry into the cleanup code. 911 */ 912 (void)sigprocmask(SIG_BLOCK, &s_mask, (sigset_t *)NULL); 913 ar_close(); 914 proc_dir(); 915 ftree_chk(); 916 } 917 918 /* 919 * next_head() 920 * try to find a valid header in the archive. Uses format specific 921 * routines to extract the header and id the trailer. Trailers may be 922 * located within a valid header or in an invalid header (the location 923 * is format specific. The inhead field from the option table tells us 924 * where to look for the trailer). 925 * We keep reading (and resyncing) until we get enough contiguous data 926 * to check for a header. If we cannot find one, we shift by a byte 927 * add a new byte from the archive to the end of the buffer and try again. 928 * If we get a read error, we throw out what we have (as we must have 929 * contiguous data) and start over again. 930 * ASSUMED: headers fit within a BLKMULT header. 931 * Return: 932 * 0 if we got a header, -1 if we are unable to ever find another one 933 * (we reached the end of input, or we reached the limit on retries. see 934 * the specs for rd_wrbuf() for more details) 935 */ 936 937 #if __STDC__ 938 static int 939 next_head(register ARCHD *arcn) 940 #else 941 static int 942 next_head(arcn) 943 register ARCHD *arcn; 944 #endif 945 { 946 register int ret; 947 register char *hdend; 948 register int res; 949 register int shftsz; 950 register int hsz; 951 register int in_resync = 0; /* set when we are in resync mode */ 952 int cnt = 0; /* counter for trailer function */ 953 954 /* 955 * set up initial conditions, we want a whole frmt->hsz block as we 956 * have no data yet. 957 */ 958 res = hsz = frmt->hsz; 959 hdend = hdbuf; 960 shftsz = hsz - 1; 961 for(;;) { 962 /* 963 * keep looping until we get a contiguous FULL buffer 964 * (frmt->hsz is the proper size) 965 */ 966 for (;;) { 967 if ((ret = rd_wrbuf(hdend, res)) == res) 968 break; 969 970 /* 971 * some kind of archive read problem, try to resync the 972 * storage device, better give the user the bad news. 973 */ 974 if ((ret == 0) || (rd_sync() < 0)) { 975 warn(1,"Premature end of file on archive read"); 976 return(-1); 977 } 978 if (!in_resync) { 979 if (act == APPND) { 980 warn(1, 981 "Archive I/O error, cannot continue"); 982 return(-1); 983 } 984 warn(1,"Archive I/O error. Trying to recover."); 985 ++in_resync; 986 } 987 988 /* 989 * oh well, throw it all out and start over 990 */ 991 res = hsz; 992 hdend = hdbuf; 993 } 994 995 /* 996 * ok we have a contiguous buffer of the right size. Call the 997 * format read routine. If this was not a valid header and this 998 * format stores trailers outside of the header, call the 999 * format specific trailer routine to check for a trailer. We 1000 * have to watch out that we do not mis-identify file data or 1001 * block padding as a header or trailer. Format specific 1002 * trailer functions must NOT check for the trailer while we 1003 * are running in resync mode. Some trailer functions may tell 1004 * us that this block cannot contain a valid header either, so 1005 * we then throw out the entire block and start over. 1006 */ 1007 if ((*frmt->rd)(arcn, hdbuf) == 0) 1008 break; 1009 1010 if (!frmt->inhead) { 1011 /* 1012 * this format has trailers outside of valid headers 1013 */ 1014 if ((ret = (*frmt->trail)(hdbuf,in_resync, &cnt)) == 0) 1015 return(-1); 1016 if (ret == 1) { 1017 /* 1018 * we are in resync and we were told to throw 1019 * the whole block out because none of the 1020 * bytes in this block can be used to form a 1021 * valid header 1022 */ 1023 res = hsz; 1024 hdend = hdbuf; 1025 continue; 1026 } 1027 } 1028 1029 /* 1030 * Brute force section. 1031 * not a valid header. We may be able to find a header yet. So 1032 * we shift over by one byte, and set up to read one byte at a 1033 * time from the archive and place it at the end of the buffer. 1034 * We will keep moving byte at a time until we find a header or 1035 * get a read error and have to start over. 1036 */ 1037 if (!in_resync) { 1038 if (act == APPND) { 1039 warn(1,"Unable to append, archive header flaw"); 1040 return(-1); 1041 } 1042 warn(1,"Invalid header, starting valid header search."); 1043 ++in_resync; 1044 } 1045 bcopy(hdbuf+1, hdbuf, shftsz); 1046 res = 1; 1047 hdend = hdbuf + shftsz; 1048 } 1049 1050 /* 1051 * ok got a valid header, check for trailer if format encodes it in the 1052 * the header. NOTE: the parameters are different than trailer routines 1053 * which encode trailers outside of the header! 1054 */ 1055 if (frmt->inhead && ((*frmt->trail)(arcn) == 0)) 1056 return(-1); 1057 ++flcnt; 1058 return(0); 1059 } 1060 1061 /* 1062 * get_arc() 1063 * Figure out what format an archive is. Handles archive with flaws by 1064 * brute force searches for a legal header in any supported format. The 1065 * format id routines have to be careful to NOT mis-identify a format. 1066 * ASSUMED: headers fit within a BLKMULT header. 1067 * Return: 1068 * 0 if archive found -1 otherwise 1069 */ 1070 1071 #if __STDC__ 1072 static int 1073 get_arc(void) 1074 #else 1075 static int 1076 get_arc() 1077 #endif 1078 { 1079 register int i; 1080 register int hdsz = 0; 1081 register int res; 1082 register int minhd = BLKMULT; 1083 char *hdend; 1084 int notice = 0; 1085 1086 /* 1087 * find the smallest header size in all archive formats and then set up 1088 * to read the archive. 1089 */ 1090 for (i = 0; ford[i] >= 0; ++i) { 1091 if (fsub[ford[i]].hsz < minhd) 1092 minhd = fsub[ford[i]].hsz; 1093 } 1094 if (rd_start() < 0) 1095 return(-1); 1096 res = BLKMULT; 1097 hdsz = 0; 1098 hdend = hdbuf; 1099 for(;;) { 1100 for (;;) { 1101 /* 1102 * fill the buffer with at least the smallest header 1103 */ 1104 i = rd_wrbuf(hdend, res); 1105 if (i > 0) 1106 hdsz += i; 1107 if (hdsz >= minhd) 1108 break; 1109 1110 /* 1111 * if we cannot recover from a read error quit 1112 */ 1113 if ((i == 0) || (rd_sync() < 0)) 1114 goto out; 1115 1116 /* 1117 * when we get an error none of the data we already 1118 * have can be used to create a legal header (we just 1119 * got an error in the middle), so we throw it all out 1120 * and refill the buffer with fresh data. 1121 */ 1122 res = BLKMULT; 1123 hdsz = 0; 1124 hdend = hdbuf; 1125 if (!notice) { 1126 if (act == APPND) 1127 return(-1); 1128 warn(1,"Cannot identify format. Searching..."); 1129 ++notice; 1130 } 1131 } 1132 1133 /* 1134 * we have at least the size of the smallest header in any 1135 * archive format. Look to see if we have a match. The array 1136 * ford[] is used to specify the header id order to reduce the 1137 * chance of incorrectly id'ing a valid header (some formats 1138 * may be subsets of each other and the order would then be 1139 * important). 1140 */ 1141 for (i = 0; ford[i] >= 0; ++i) { 1142 if ((*fsub[ford[i]].id)(hdbuf, hdsz) < 0) 1143 continue; 1144 frmt = &(fsub[ford[i]]); 1145 /* 1146 * yuck, to avoid slow special case code in the extract 1147 * routines, just push this header back as if it was 1148 * not seen. We have left extra space at start of the 1149 * buffer for this purpose. This is a bit ugly, but 1150 * adding all the special case code is far worse. 1151 */ 1152 pback(hdbuf, hdsz); 1153 return(0); 1154 } 1155 1156 /* 1157 * We have a flawed archive, no match. we start searching, but 1158 * we never allow additions to flawed archives 1159 */ 1160 if (!notice) { 1161 if (act == APPND) 1162 return(-1); 1163 warn(1, "Cannot identify format. Searching..."); 1164 ++notice; 1165 } 1166 1167 /* 1168 * brute force search for a header that we can id. 1169 * we shift through byte at a time. this is slow, but we cannot 1170 * determine the nature of the flaw in the archive in a 1171 * portable manner 1172 */ 1173 if (--hdsz > 0) { 1174 bcopy(hdbuf+1, hdbuf, hdsz); 1175 res = BLKMULT - hdsz; 1176 hdend = hdbuf + hdsz; 1177 } else { 1178 res = BLKMULT; 1179 hdend = hdbuf; 1180 hdsz = 0; 1181 } 1182 } 1183 1184 out: 1185 /* 1186 * we cannot find a header, bow, apologize and quit 1187 */ 1188 warn(1, "Sorry, unable to determine archive format."); 1189 return(-1); 1190 } 1191