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