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