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