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