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. All advertising materials mentioning features or use of this software 18 * must display the following acknowledgement: 19 * This product includes software developed by the University of 20 * California, Berkeley and its contributors. 21 * 4. Neither the name of the University nor the names of its contributors 22 * may be used to endorse or promote products derived from this software 23 * without specific prior written permission. 24 * 25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 35 * SUCH DAMAGE. 36 * 37 * @(#)ar_io.c 8.2 (Berkeley) 4/18/94 38 * $FreeBSD: src/bin/pax/ar_io.c,v 1.12.2.1 2001/08/01 05:03:11 obrien Exp $ 39 * $DragonFly: src/bin/pax/ar_io.c,v 1.4 2003/09/21 04:24:16 drhodus Exp $ 40 */ 41 42 #include <sys/types.h> 43 #include <sys/ioctl.h> 44 #include <sys/mtio.h> 45 #include <sys/stat.h> 46 #include <sys/wait.h> 47 #include <err.h> 48 #include <errno.h> 49 #include <fcntl.h> 50 #include <signal.h> 51 #include <stdio.h> 52 #include <string.h> 53 #include <stdlib.h> 54 #include <unistd.h> 55 #include "pax.h" 56 #include "options.h" 57 #include "extern.h" 58 59 /* 60 * Routines which deal directly with the archive I/O device/file. 61 */ 62 63 #define DMOD 0666 /* default mode of created archives */ 64 #define EXT_MODE O_RDONLY /* open mode for list/extract */ 65 #define AR_MODE (O_WRONLY | O_CREAT | O_TRUNC) /* mode for archive */ 66 #define APP_MODE O_RDWR /* mode for append */ 67 #define STDO "<STDOUT>" /* pseudo name for stdout */ 68 #define STDN "<STDIN>" /* pseudo name for stdin */ 69 static int arfd = -1; /* archive file descriptor */ 70 static int artyp = ISREG; /* archive type: file/FIFO/tape */ 71 static int arvol = 1; /* archive volume number */ 72 static int lstrval = -1; /* return value from last i/o */ 73 static int io_ok; /* i/o worked on volume after resync */ 74 static int did_io; /* did i/o ever occur on volume? */ 75 static int done; /* set via tty termination */ 76 static struct stat arsb; /* stat of archive device at open */ 77 static int invld_rec; /* tape has out of spec record size */ 78 static int wr_trail = 1; /* trailer was rewritten in append */ 79 static int can_unlnk = 0; /* do we unlink null archives? */ 80 char *arcname; /* printable name of archive */ 81 const char *gzip_program; /* name of gzip program */ 82 static pid_t zpid = -1; /* pid of child process */ 83 84 static int get_phys (void); 85 extern sigset_t s_mask; 86 static void ar_start_gzip (int, const char *, int); 87 88 /* 89 * ar_open() 90 * Opens the next archive volume. Determines the type of the device and 91 * sets up block sizes as required by the archive device and the format. 92 * Note: we may be called with name == NULL on the first open only. 93 * Return: 94 * -1 on failure, 0 otherwise 95 */ 96 97 #ifdef __STDC__ 98 int 99 ar_open(char *name) 100 #else 101 int 102 ar_open(name) 103 char *name; 104 #endif 105 { 106 struct mtget mb; 107 108 if (arfd != -1) 109 (void)close(arfd); 110 arfd = -1; 111 can_unlnk = did_io = io_ok = invld_rec = 0; 112 artyp = ISREG; 113 flcnt = 0; 114 115 /* 116 * open based on overall operation mode 117 */ 118 switch (act) { 119 case LIST: 120 case EXTRACT: 121 if (name == NULL) { 122 arfd = STDIN_FILENO; 123 arcname = STDN; 124 } else if ((arfd = open(name, EXT_MODE, DMOD)) < 0) 125 syswarn(0, errno, "Failed open to read on %s", name); 126 if (arfd != -1 && gzip_program != NULL) 127 ar_start_gzip(arfd, gzip_program, 0); 128 break; 129 case ARCHIVE: 130 if (name == NULL) { 131 arfd = STDOUT_FILENO; 132 arcname = STDO; 133 } else if ((arfd = open(name, AR_MODE, DMOD)) < 0) 134 syswarn(0, errno, "Failed open to write on %s", name); 135 else 136 can_unlnk = 1; 137 if (arfd != -1 && gzip_program != NULL) 138 ar_start_gzip(arfd, gzip_program, 1); 139 break; 140 case APPND: 141 if (name == NULL) { 142 arfd = STDOUT_FILENO; 143 arcname = STDO; 144 } else if ((arfd = open(name, APP_MODE, DMOD)) < 0) 145 syswarn(0, errno, "Failed open to read/write on %s", 146 name); 147 break; 148 case COPY: 149 /* 150 * arfd not used in COPY mode 151 */ 152 arcname = "<NONE>"; 153 lstrval = 1; 154 return(0); 155 } 156 if (arfd < 0) 157 return(-1); 158 159 if (chdname != NULL) 160 if (chdir(chdname) != 0) 161 syswarn(1, errno, "Failed chdir to %s", chdname); 162 /* 163 * set up is based on device type 164 */ 165 if (fstat(arfd, &arsb) < 0) { 166 syswarn(0, errno, "Failed stat on %s", arcname); 167 (void)close(arfd); 168 arfd = -1; 169 can_unlnk = 0; 170 return(-1); 171 } 172 if (S_ISDIR(arsb.st_mode)) { 173 paxwarn(0, "Cannot write an archive on top of a directory %s", 174 arcname); 175 (void)close(arfd); 176 arfd = -1; 177 can_unlnk = 0; 178 return(-1); 179 } 180 181 if (S_ISCHR(arsb.st_mode)) 182 artyp = ioctl(arfd, MTIOCGET, &mb) ? ISCHR : ISTAPE; 183 else if (S_ISBLK(arsb.st_mode)) 184 artyp = ISBLK; 185 else if ((lseek(arfd, (off_t)0L, SEEK_CUR) == -1) && (errno == ESPIPE)) 186 artyp = ISPIPE; 187 else 188 artyp = ISREG; 189 190 /* 191 * make sure we beyond any doubt that we only can unlink regular files 192 * we created 193 */ 194 if (artyp != ISREG) 195 can_unlnk = 0; 196 /* 197 * if we are writing, we are done 198 */ 199 if (act == ARCHIVE) { 200 blksz = rdblksz = wrblksz; 201 lstrval = 1; 202 return(0); 203 } 204 205 /* 206 * set default blksz on read. APPNDs writes rdblksz on the last volume 207 * On all new archive volumes, we shift to wrblksz (if the user 208 * specified one, otherwize we will continue to use rdblksz). We 209 * must to set blocksize based on what kind of device the archive is 210 * stored. 211 */ 212 switch(artyp) { 213 case ISTAPE: 214 /* 215 * Tape drives come in at least two flavors. Those that support 216 * variable sized records and those that have fixed sized 217 * records. They must be treated differently. For tape drives 218 * that support variable sized records, we must make large 219 * reads to make sure we get the entire record, otherwise we 220 * will just get the first part of the record (up to size we 221 * asked). Tapes with fixed sized records may or may not return 222 * multiple records in a single read. We really do not care 223 * what the physical record size is UNLESS we are going to 224 * append. (We will need the physical block size to rewrite 225 * the trailer). Only when we are appending do we go to the 226 * effort to figure out the true PHYSICAL record size. 227 */ 228 blksz = rdblksz = MAXBLK; 229 break; 230 case ISPIPE: 231 case ISBLK: 232 case ISCHR: 233 /* 234 * Blocksize is not a major issue with these devices (but must 235 * be kept a multiple of 512). If the user specified a write 236 * block size, we use that to read. Under append, we must 237 * always keep blksz == rdblksz. Otherwise we go ahead and use 238 * the device optimal blocksize as (and if) returned by stat 239 * and if it is within pax specs. 240 */ 241 if ((act == APPND) && wrblksz) { 242 blksz = rdblksz = wrblksz; 243 break; 244 } 245 246 if ((arsb.st_blksize > 0) && (arsb.st_blksize < MAXBLK) && 247 ((arsb.st_blksize % BLKMULT) == 0)) 248 rdblksz = arsb.st_blksize; 249 else 250 rdblksz = DEVBLK; 251 /* 252 * For performance go for large reads when we can without harm 253 */ 254 if ((act == APPND) || (artyp == ISCHR)) 255 blksz = rdblksz; 256 else 257 blksz = MAXBLK; 258 break; 259 case ISREG: 260 /* 261 * if the user specified wrblksz works, use it. Under appends 262 * we must always keep blksz == rdblksz 263 */ 264 if ((act == APPND) && wrblksz && ((arsb.st_size%wrblksz)==0)){ 265 blksz = rdblksz = wrblksz; 266 break; 267 } 268 /* 269 * See if we can find the blocking factor from the file size 270 */ 271 for (rdblksz = MAXBLK; rdblksz > 0; rdblksz -= BLKMULT) 272 if ((arsb.st_size % rdblksz) == 0) 273 break; 274 /* 275 * When we cannot find a match, we may have a flawed archive. 276 */ 277 if (rdblksz <= 0) 278 rdblksz = FILEBLK; 279 /* 280 * for performance go for large reads when we can 281 */ 282 if (act == APPND) 283 blksz = rdblksz; 284 else 285 blksz = MAXBLK; 286 break; 287 default: 288 /* 289 * should never happen, worse case, slow... 290 */ 291 blksz = rdblksz = BLKMULT; 292 break; 293 } 294 lstrval = 1; 295 return(0); 296 } 297 298 /* 299 * ar_close() 300 * closes archive device, increments volume number, and prints i/o summary 301 */ 302 #ifdef __STDC__ 303 void 304 ar_close(void) 305 #else 306 void 307 ar_close() 308 #endif 309 { 310 int status; 311 312 if (arfd < 0) { 313 did_io = io_ok = flcnt = 0; 314 return; 315 } 316 317 /* 318 * Close archive file. This may take a LONG while on tapes (we may be 319 * forced to wait for the rewind to complete) so tell the user what is 320 * going on (this avoids the user hitting control-c thinking pax is 321 * broken). 322 */ 323 if (vflag && (artyp == ISTAPE)) { 324 if (vfpart) 325 (void)putc('\n', listf); 326 (void)fprintf(listf, 327 "%s: Waiting for tape drive close to complete...", 328 argv0); 329 (void)fflush(listf); 330 } 331 332 /* 333 * if nothing was written to the archive (and we created it), we remove 334 * it 335 */ 336 if (can_unlnk && (fstat(arfd, &arsb) == 0) && (S_ISREG(arsb.st_mode)) && 337 (arsb.st_size == 0)) { 338 (void)unlink(arcname); 339 can_unlnk = 0; 340 } 341 342 /* 343 * for a quick extract/list, pax frequently exits before the child 344 * process is done 345 */ 346 if ((act == LIST || act == EXTRACT) && nflag && zpid > 0) 347 kill(zpid, SIGINT); 348 349 (void)close(arfd); 350 351 /* Do not exit before child to ensure data integrity */ 352 if (zpid > 0) 353 waitpid(zpid, &status, 0); 354 355 if (vflag && (artyp == ISTAPE)) { 356 (void)fputs("done.\n", listf); 357 vfpart = 0; 358 (void)fflush(listf); 359 } 360 arfd = -1; 361 362 if (!io_ok && !did_io) { 363 flcnt = 0; 364 return; 365 } 366 did_io = io_ok = 0; 367 368 /* 369 * The volume number is only increased when the last device has data 370 * and we have already determined the archive format. 371 */ 372 if (frmt != NULL) 373 ++arvol; 374 375 if (!vflag) { 376 flcnt = 0; 377 return; 378 } 379 380 /* 381 * Print out a summary of I/O for this archive volume. 382 */ 383 if (vfpart) { 384 (void)putc('\n', listf); 385 vfpart = 0; 386 } 387 388 /* 389 * If we have not determined the format yet, we just say how many bytes 390 * we have skipped over looking for a header to id. there is no way we 391 * could have written anything yet. 392 */ 393 if (frmt == NULL) { 394 # ifdef NET2_STAT 395 (void)fprintf(listf, "%s: unknown format, %lu bytes skipped.\n", 396 # else 397 (void)fprintf(listf, "%s: unknown format, %qu bytes skipped.\n", 398 # endif 399 argv0, rdcnt); 400 (void)fflush(listf); 401 flcnt = 0; 402 return; 403 } 404 405 if (strcmp(NM_CPIO, argv0) == 0) 406 (void)fprintf(listf, "%qu blocks\n", (rdcnt ? rdcnt : wrcnt) / 5120); 407 else if (strcmp(NM_TAR, argv0) != 0) 408 (void)fprintf(listf, 409 # ifdef NET2_STAT 410 "%s: %s vol %d, %lu files, %lu bytes read, %lu bytes written.\n", 411 # else 412 "%s: %s vol %d, %lu files, %qu bytes read, %qu bytes written.\n", 413 # endif 414 argv0, frmt->name, arvol-1, flcnt, rdcnt, wrcnt); 415 (void)fflush(listf); 416 flcnt = 0; 417 } 418 419 /* 420 * ar_drain() 421 * drain any archive format independent padding from an archive read 422 * from a socket or a pipe. This is to prevent the process on the 423 * other side of the pipe from getting a SIGPIPE (pax will stop 424 * reading an archive once a format dependent trailer is detected). 425 */ 426 #ifdef __STDC__ 427 void 428 ar_drain(void) 429 #else 430 void 431 ar_drain() 432 #endif 433 { 434 register int res; 435 char drbuf[MAXBLK]; 436 437 /* 438 * we only drain from a pipe/socket. Other devices can be closed 439 * without reading up to end of file. We sure hope that pipe is closed 440 * on the other side so we will get an EOF. 441 */ 442 if ((artyp != ISPIPE) || (lstrval <= 0)) 443 return; 444 445 /* 446 * keep reading until pipe is drained 447 */ 448 while ((res = read(arfd, drbuf, sizeof(drbuf))) > 0) 449 ; 450 lstrval = res; 451 } 452 453 /* 454 * ar_set_wr() 455 * Set up device right before switching from read to write in an append. 456 * device dependent code (if required) to do this should be added here. 457 * For all archive devices we are already positioned at the place we want 458 * to start writing when this routine is called. 459 * Return: 460 * 0 if all ready to write, -1 otherwise 461 */ 462 463 #ifdef __STDC__ 464 int 465 ar_set_wr(void) 466 #else 467 int 468 ar_set_wr() 469 #endif 470 { 471 off_t cpos; 472 473 /* 474 * we must make sure the trailer is rewritten on append, ar_next() 475 * will stop us if the archive containing the trailer was not written 476 */ 477 wr_trail = 0; 478 479 /* 480 * Add any device dependent code as required here 481 */ 482 if (artyp != ISREG) 483 return(0); 484 /* 485 * Ok we have an archive in a regular file. If we were rewriting a 486 * file, we must get rid of all the stuff after the current offset 487 * (it was not written by pax). 488 */ 489 if (((cpos = lseek(arfd, (off_t)0L, SEEK_CUR)) < 0) || 490 (ftruncate(arfd, cpos) < 0)) { 491 syswarn(1, errno, "Unable to truncate archive file"); 492 return(-1); 493 } 494 return(0); 495 } 496 497 /* 498 * ar_app_ok() 499 * check if the last volume in the archive allows appends. We cannot check 500 * this until we are ready to write since there is no spec that says all 501 * volumes in a single archive have to be of the same type... 502 * Return: 503 * 0 if we can append, -1 otherwise. 504 */ 505 506 #ifdef __STDC__ 507 int 508 ar_app_ok(void) 509 #else 510 int 511 ar_app_ok() 512 #endif 513 { 514 if (artyp == ISPIPE) { 515 paxwarn(1, "Cannot append to an archive obtained from a pipe."); 516 return(-1); 517 } 518 519 if (!invld_rec) 520 return(0); 521 paxwarn(1,"Cannot append, device record size %d does not support %s spec", 522 rdblksz, argv0); 523 return(-1); 524 } 525 526 /* 527 * ar_read() 528 * read up to a specified number of bytes from the archive into the 529 * supplied buffer. When dealing with tapes we may not always be able to 530 * read what we want. 531 * Return: 532 * Number of bytes in buffer. 0 for end of file, -1 for a read error. 533 */ 534 535 #ifdef __STDC__ 536 int 537 ar_read(register char *buf, register int cnt) 538 #else 539 int 540 ar_read(buf, cnt) 541 register char *buf; 542 register int cnt; 543 #endif 544 { 545 register int res = 0; 546 547 /* 548 * if last i/o was in error, no more reads until reset or new volume 549 */ 550 if (lstrval <= 0) 551 return(lstrval); 552 553 /* 554 * how we read must be based on device type 555 */ 556 switch (artyp) { 557 case ISTAPE: 558 if ((res = read(arfd, buf, cnt)) > 0) { 559 /* 560 * CAUTION: tape systems may not always return the same 561 * sized records so we leave blksz == MAXBLK. The 562 * physical record size that a tape drive supports is 563 * very hard to determine in a uniform and portable 564 * manner. 565 */ 566 io_ok = 1; 567 if (res != rdblksz) { 568 /* 569 * Record size changed. If this is happens on 570 * any record after the first, we probably have 571 * a tape drive which has a fixed record size 572 * we are getting multiple records in a single 573 * read). Watch out for record blocking that 574 * violates pax spec (must be a multiple of 575 * BLKMULT). 576 */ 577 rdblksz = res; 578 if (rdblksz % BLKMULT) 579 invld_rec = 1; 580 } 581 return(res); 582 } 583 break; 584 case ISREG: 585 case ISBLK: 586 case ISCHR: 587 case ISPIPE: 588 default: 589 /* 590 * Files are so easy to deal with. These other things cannot 591 * be trusted at all. So when we are dealing with character 592 * devices and pipes we just take what they have ready for us 593 * and return. Trying to do anything else with them runs the 594 * risk of failure. 595 */ 596 if ((res = read(arfd, buf, cnt)) > 0) { 597 io_ok = 1; 598 return(res); 599 } 600 break; 601 } 602 603 /* 604 * We are in trouble at this point, something is broken... 605 */ 606 lstrval = res; 607 if (res < 0) 608 syswarn(1, errno, "Failed read on archive volume %d", arvol); 609 else 610 paxwarn(0, "End of archive volume %d reached", arvol); 611 return(res); 612 } 613 614 /* 615 * ar_write() 616 * Write a specified number of bytes in supplied buffer to the archive 617 * device so it appears as a single "block". Deals with errors and tries 618 * to recover when faced with short writes. 619 * Return: 620 * Number of bytes written. 0 indicates end of volume reached and with no 621 * flaws (as best that can be detected). A -1 indicates an unrecoverable 622 * error in the archive occured. 623 */ 624 625 #ifdef __STDC__ 626 int 627 ar_write(register char *buf, register int bsz) 628 #else 629 int 630 ar_write(buf, bsz) 631 register char *buf; 632 register int bsz; 633 #endif 634 { 635 register int res; 636 off_t cpos; 637 638 /* 639 * do not allow pax to create a "bad" archive. Once a write fails on 640 * an archive volume prevent further writes to it. 641 */ 642 if (lstrval <= 0) 643 return(lstrval); 644 645 if ((res = write(arfd, buf, bsz)) == bsz) { 646 wr_trail = 1; 647 io_ok = 1; 648 return(bsz); 649 } 650 /* 651 * write broke, see what we can do with it. We try to send any partial 652 * writes that may violate pax spec to the next archive volume. 653 */ 654 if (res < 0) 655 lstrval = res; 656 else 657 lstrval = 0; 658 659 switch (artyp) { 660 case ISREG: 661 if ((res > 0) && (res % BLKMULT)) { 662 /* 663 * try to fix up partial writes which are not BLKMULT 664 * in size by forcing the runt record to next archive 665 * volume 666 */ 667 if ((cpos = lseek(arfd, (off_t)0L, SEEK_CUR)) < 0) 668 break; 669 cpos -= (off_t)res; 670 if (ftruncate(arfd, cpos) < 0) 671 break; 672 res = lstrval = 0; 673 break; 674 } 675 if (res >= 0) 676 break; 677 /* 678 * if file is out of space, handle it like a return of 0 679 */ 680 if ((errno == ENOSPC) || (errno == EFBIG) || (errno == EDQUOT)) 681 res = lstrval = 0; 682 break; 683 case ISTAPE: 684 case ISCHR: 685 case ISBLK: 686 if (res >= 0) 687 break; 688 if (errno == EACCES) { 689 paxwarn(0, "Write failed, archive is write protected."); 690 res = lstrval = 0; 691 return(0); 692 } 693 /* 694 * see if we reached the end of media, if so force a change to 695 * the next volume 696 */ 697 if ((errno == ENOSPC) || (errno == EIO) || (errno == ENXIO)) 698 res = lstrval = 0; 699 break; 700 case ISPIPE: 701 default: 702 /* 703 * we cannot fix errors to these devices 704 */ 705 break; 706 } 707 708 /* 709 * Better tell the user the bad news... 710 * if this is a block aligned archive format, we may have a bad archive 711 * if the format wants the header to start at a BLKMULT boundary. While 712 * we can deal with the mis-aligned data, it violates spec and other 713 * archive readers will likely fail. if the format is not block 714 * aligned, the user may be lucky (and the archive is ok). 715 */ 716 if (res >= 0) { 717 if (res > 0) 718 wr_trail = 1; 719 io_ok = 1; 720 } 721 722 /* 723 * If we were trying to rewrite the trailer and it didn't work, we 724 * must quit right away. 725 */ 726 if (!wr_trail && (res <= 0)) { 727 paxwarn(1,"Unable to append, trailer re-write failed. Quitting."); 728 return(res); 729 } 730 731 if (res == 0) 732 paxwarn(0, "End of archive volume %d reached", arvol); 733 else if (res < 0) 734 syswarn(1, errno, "Failed write to archive volume: %d", arvol); 735 else if (!frmt->blkalgn || ((res % frmt->blkalgn) == 0)) 736 paxwarn(0,"WARNING: partial archive write. Archive MAY BE FLAWED"); 737 else 738 paxwarn(1,"WARNING: partial archive write. Archive IS FLAWED"); 739 return(res); 740 } 741 742 /* 743 * ar_rdsync() 744 * Try to move past a bad spot on a flawed archive as needed to continue 745 * I/O. Clears error flags to allow I/O to continue. 746 * Return: 747 * 0 when ok to try i/o again, -1 otherwise. 748 */ 749 750 #ifdef __STDC__ 751 int 752 ar_rdsync(void) 753 #else 754 int 755 ar_rdsync() 756 #endif 757 { 758 long fsbz; 759 off_t cpos; 760 off_t mpos; 761 struct mtop mb; 762 763 /* 764 * Fail resync attempts at user request (done) or this is going to be 765 * an update/append to a existing archive. if last i/o hit media end, 766 * we need to go to the next volume not try a resync 767 */ 768 if ((done > 0) || (lstrval == 0)) 769 return(-1); 770 771 if ((act == APPND) || (act == ARCHIVE)) { 772 paxwarn(1, "Cannot allow updates to an archive with flaws."); 773 return(-1); 774 } 775 if (io_ok) 776 did_io = 1; 777 778 switch(artyp) { 779 case ISTAPE: 780 /* 781 * if the last i/o was a successful data transfer, we assume 782 * the fault is just a bad record on the tape that we are now 783 * past. If we did not get any data since the last resync try 784 * to move the tape forward one PHYSICAL record past any 785 * damaged tape section. Some tape drives are stubborn and need 786 * to be pushed. 787 */ 788 if (io_ok) { 789 io_ok = 0; 790 lstrval = 1; 791 break; 792 } 793 mb.mt_op = MTFSR; 794 mb.mt_count = 1; 795 if (ioctl(arfd, MTIOCTOP, &mb) < 0) 796 break; 797 lstrval = 1; 798 break; 799 case ISREG: 800 case ISCHR: 801 case ISBLK: 802 /* 803 * try to step over the bad part of the device. 804 */ 805 io_ok = 0; 806 if (((fsbz = arsb.st_blksize) <= 0) || (artyp != ISREG)) 807 fsbz = BLKMULT; 808 if ((cpos = lseek(arfd, (off_t)0L, SEEK_CUR)) < 0) 809 break; 810 mpos = fsbz - (cpos % (off_t)fsbz); 811 if (lseek(arfd, mpos, SEEK_CUR) < 0) 812 break; 813 lstrval = 1; 814 break; 815 case ISPIPE: 816 default: 817 /* 818 * cannot recover on these archive device types 819 */ 820 io_ok = 0; 821 break; 822 } 823 if (lstrval <= 0) { 824 paxwarn(1, "Unable to recover from an archive read failure."); 825 return(-1); 826 } 827 paxwarn(0, "Attempting to recover from an archive read failure."); 828 return(0); 829 } 830 831 /* 832 * ar_fow() 833 * Move the I/O position within the archive foward the specified number of 834 * bytes as supported by the device. If we cannot move the requested 835 * number of bytes, return the actual number of bytes moved in skipped. 836 * Return: 837 * 0 if moved the requested distance, -1 on complete failure, 1 on 838 * partial move (the amount moved is in skipped) 839 */ 840 841 #ifdef __STDC__ 842 int 843 ar_fow(off_t sksz, off_t *skipped) 844 #else 845 int 846 ar_fow(sksz, skipped) 847 off_t sksz; 848 off_t *skipped; 849 #endif 850 { 851 off_t cpos; 852 off_t mpos; 853 854 *skipped = 0; 855 if (sksz <= 0) 856 return(0); 857 858 /* 859 * we cannot move foward at EOF or error 860 */ 861 if (lstrval <= 0) 862 return(lstrval); 863 864 /* 865 * Safer to read forward on devices where it is hard to find the end of 866 * the media without reading to it. With tapes we cannot be sure of the 867 * number of physical blocks to skip (we do not know physical block 868 * size at this point), so we must only read foward on tapes! 869 */ 870 if (artyp != ISREG) 871 return(0); 872 873 /* 874 * figure out where we are in the archive 875 */ 876 if ((cpos = lseek(arfd, (off_t)0L, SEEK_CUR)) >= 0) { 877 /* 878 * we can be asked to move farther than there are bytes in this 879 * volume, if so, just go to file end and let normal buf_fill() 880 * deal with the end of file (it will go to next volume by 881 * itself) 882 */ 883 if ((mpos = cpos + sksz) > arsb.st_size) { 884 *skipped = arsb.st_size - cpos; 885 mpos = arsb.st_size; 886 } else 887 *skipped = sksz; 888 if (lseek(arfd, mpos, SEEK_SET) >= 0) 889 return(0); 890 } 891 syswarn(1, errno, "Forward positioning operation on archive failed"); 892 lstrval = -1; 893 return(-1); 894 } 895 896 /* 897 * ar_rev() 898 * move the i/o position within the archive backwards the specified byte 899 * count as supported by the device. With tapes drives we RESET rdblksz to 900 * the PHYSICAL blocksize. 901 * NOTE: We should only be called to move backwards so we can rewrite the 902 * last records (the trailer) of an archive (APPEND). 903 * Return: 904 * 0 if moved the requested distance, -1 on complete failure 905 */ 906 907 #ifdef __STDC__ 908 int 909 ar_rev(off_t sksz) 910 #else 911 int 912 ar_rev(sksz) 913 off_t sksz; 914 #endif 915 { 916 off_t cpos; 917 struct mtop mb; 918 register int phyblk; 919 920 /* 921 * make sure we do not have try to reverse on a flawed archive 922 */ 923 if (lstrval < 0) 924 return(lstrval); 925 926 switch(artyp) { 927 case ISPIPE: 928 if (sksz <= 0) 929 break; 930 /* 931 * cannot go backwards on these critters 932 */ 933 paxwarn(1, "Reverse positioning on pipes is not supported."); 934 lstrval = -1; 935 return(-1); 936 case ISREG: 937 case ISBLK: 938 case ISCHR: 939 default: 940 if (sksz <= 0) 941 break; 942 943 /* 944 * For things other than files, backwards movement has a very 945 * high probability of failure as we really do not know the 946 * true attributes of the device we are talking to (the device 947 * may not even have the ability to lseek() in any direction). 948 * First we figure out where we are in the archive. 949 */ 950 if ((cpos = lseek(arfd, (off_t)0L, SEEK_CUR)) < 0) { 951 syswarn(1, errno, 952 "Unable to obtain current archive byte offset"); 953 lstrval = -1; 954 return(-1); 955 } 956 957 /* 958 * we may try to go backwards past the start when the archive 959 * is only a single record. If this hapens and we are on a 960 * multi volume archive, we need to go to the end of the 961 * previous volume and continue our movement backwards from 962 * there. 963 */ 964 if ((cpos -= sksz) < (off_t)0L) { 965 if (arvol > 1) { 966 /* 967 * this should never happen 968 */ 969 paxwarn(1,"Reverse position on previous volume."); 970 lstrval = -1; 971 return(-1); 972 } 973 cpos = (off_t)0L; 974 } 975 if (lseek(arfd, cpos, SEEK_SET) < 0) { 976 syswarn(1, errno, "Unable to seek archive backwards"); 977 lstrval = -1; 978 return(-1); 979 } 980 break; 981 case ISTAPE: 982 /* 983 * Calculate and move the proper number of PHYSICAL tape 984 * blocks. If the sksz is not an even multiple of the physical 985 * tape size, we cannot do the move (this should never happen). 986 * (We also cannot handler trailers spread over two vols). 987 * get_phys() also makes sure we are in front of the filemark. 988 */ 989 if ((phyblk = get_phys()) <= 0) { 990 lstrval = -1; 991 return(-1); 992 } 993 994 /* 995 * make sure future tape reads only go by physical tape block 996 * size (set rdblksz to the real size). 997 */ 998 rdblksz = phyblk; 999 1000 /* 1001 * if no movement is required, just return (we must be after 1002 * get_phys() so the physical blocksize is properly set) 1003 */ 1004 if (sksz <= 0) 1005 break; 1006 1007 /* 1008 * ok we have to move. Make sure the tape drive can do it. 1009 */ 1010 if (sksz % phyblk) { 1011 paxwarn(1, 1012 "Tape drive unable to backspace requested amount"); 1013 lstrval = -1; 1014 return(-1); 1015 } 1016 1017 /* 1018 * move backwards the requested number of bytes 1019 */ 1020 mb.mt_op = MTBSR; 1021 mb.mt_count = sksz/phyblk; 1022 if (ioctl(arfd, MTIOCTOP, &mb) < 0) { 1023 syswarn(1,errno, "Unable to backspace tape %d blocks.", 1024 mb.mt_count); 1025 lstrval = -1; 1026 return(-1); 1027 } 1028 break; 1029 } 1030 lstrval = 1; 1031 return(0); 1032 } 1033 1034 /* 1035 * get_phys() 1036 * Determine the physical block size on a tape drive. We need the physical 1037 * block size so we know how many bytes we skip over when we move with 1038 * mtio commands. We also make sure we are BEFORE THE TAPE FILEMARK when 1039 * return. 1040 * This is one really SLOW routine... 1041 * Return: 1042 * physical block size if ok (ok > 0), -1 otherwise 1043 */ 1044 1045 #ifdef __STDC__ 1046 static int 1047 get_phys(void) 1048 #else 1049 static int 1050 get_phys() 1051 #endif 1052 { 1053 register int padsz = 0; 1054 register int res; 1055 register int phyblk; 1056 struct mtop mb; 1057 char scbuf[MAXBLK]; 1058 1059 /* 1060 * move to the file mark, and then back up one record and read it. 1061 * this should tell us the physical record size the tape is using. 1062 */ 1063 if (lstrval == 1) { 1064 /* 1065 * we know we are at file mark when we get back a 0 from 1066 * read() 1067 */ 1068 while ((res = read(arfd, scbuf, sizeof(scbuf))) > 0) 1069 padsz += res; 1070 if (res < 0) { 1071 syswarn(1, errno, "Unable to locate tape filemark."); 1072 return(-1); 1073 } 1074 } 1075 1076 /* 1077 * move backwards over the file mark so we are at the end of the 1078 * last record. 1079 */ 1080 mb.mt_op = MTBSF; 1081 mb.mt_count = 1; 1082 if (ioctl(arfd, MTIOCTOP, &mb) < 0) { 1083 syswarn(1, errno, "Unable to backspace over tape filemark."); 1084 return(-1); 1085 } 1086 1087 /* 1088 * move backwards so we are in front of the last record and read it to 1089 * get physical tape blocksize. 1090 */ 1091 mb.mt_op = MTBSR; 1092 mb.mt_count = 1; 1093 if (ioctl(arfd, MTIOCTOP, &mb) < 0) { 1094 syswarn(1, errno, "Unable to backspace over last tape block."); 1095 return(-1); 1096 } 1097 if ((phyblk = read(arfd, scbuf, sizeof(scbuf))) <= 0) { 1098 syswarn(1, errno, "Cannot determine archive tape blocksize."); 1099 return(-1); 1100 } 1101 1102 /* 1103 * read foward to the file mark, then back up in front of the filemark 1104 * (this is a bit paranoid, but should be safe to do). 1105 */ 1106 while ((res = read(arfd, scbuf, sizeof(scbuf))) > 0) 1107 ; 1108 if (res < 0) { 1109 syswarn(1, errno, "Unable to locate tape filemark."); 1110 return(-1); 1111 } 1112 mb.mt_op = MTBSF; 1113 mb.mt_count = 1; 1114 if (ioctl(arfd, MTIOCTOP, &mb) < 0) { 1115 syswarn(1, errno, "Unable to backspace over tape filemark."); 1116 return(-1); 1117 } 1118 1119 /* 1120 * set lstrval so we know that the filemark has not been seen 1121 */ 1122 lstrval = 1; 1123 1124 /* 1125 * return if there was no padding 1126 */ 1127 if (padsz == 0) 1128 return(phyblk); 1129 1130 /* 1131 * make sure we can move backwards over the padding. (this should 1132 * never fail). 1133 */ 1134 if (padsz % phyblk) { 1135 paxwarn(1, "Tape drive unable to backspace requested amount"); 1136 return(-1); 1137 } 1138 1139 /* 1140 * move backwards over the padding so the head is where it was when 1141 * we were first called (if required). 1142 */ 1143 mb.mt_op = MTBSR; 1144 mb.mt_count = padsz/phyblk; 1145 if (ioctl(arfd, MTIOCTOP, &mb) < 0) { 1146 syswarn(1,errno,"Unable to backspace tape over %d pad blocks", 1147 mb.mt_count); 1148 return(-1); 1149 } 1150 return(phyblk); 1151 } 1152 1153 /* 1154 * ar_next() 1155 * prompts the user for the next volume in this archive. For some devices 1156 * we may allow the media to be changed. Otherwise a new archive is 1157 * prompted for. By pax spec, if there is no controlling tty or an eof is 1158 * read on tty input, we must quit pax. 1159 * Return: 1160 * 0 when ready to continue, -1 when all done 1161 */ 1162 1163 #ifdef __STDC__ 1164 int 1165 ar_next(void) 1166 #else 1167 int 1168 ar_next() 1169 #endif 1170 { 1171 char buf[PAXPATHLEN+2]; 1172 static int freeit = 0; 1173 sigset_t o_mask; 1174 1175 /* 1176 * WE MUST CLOSE THE DEVICE. A lot of devices must see last close, (so 1177 * things like writing EOF etc will be done) (Watch out ar_close() can 1178 * also be called via a signal handler, so we must prevent a race. 1179 */ 1180 if (sigprocmask(SIG_BLOCK, &s_mask, &o_mask) < 0) 1181 syswarn(0, errno, "Unable to set signal mask"); 1182 ar_close(); 1183 if (sigprocmask(SIG_SETMASK, &o_mask, NULL) < 0) 1184 syswarn(0, errno, "Unable to restore signal mask"); 1185 1186 if (done || !wr_trail || strcmp(NM_TAR, argv0) == 0) 1187 return(-1); 1188 1189 tty_prnt("\nATTENTION! %s archive volume change required.\n", argv0); 1190 1191 /* 1192 * if i/o is on stdin or stdout, we cannot reopen it (we do not know 1193 * the name), the user will be forced to type it in. 1194 */ 1195 if (strcmp(arcname, STDO) && strcmp(arcname, STDN) && (artyp != ISREG) 1196 && (artyp != ISPIPE)) { 1197 if (artyp == ISTAPE) { 1198 tty_prnt("%s ready for archive tape volume: %d\n", 1199 arcname, arvol); 1200 tty_prnt("Load the NEXT TAPE on the tape drive"); 1201 } else { 1202 tty_prnt("%s ready for archive volume: %d\n", 1203 arcname, arvol); 1204 tty_prnt("Load the NEXT STORAGE MEDIA (if required)"); 1205 } 1206 1207 if ((act == ARCHIVE) || (act == APPND)) 1208 tty_prnt(" and make sure it is WRITE ENABLED.\n"); 1209 else 1210 tty_prnt("\n"); 1211 1212 for(;;) { 1213 tty_prnt("Type \"y\" to continue, \".\" to quit %s,", 1214 argv0); 1215 tty_prnt(" or \"s\" to switch to new device.\nIf you"); 1216 tty_prnt(" cannot change storage media, type \"s\"\n"); 1217 tty_prnt("Is the device ready and online? > "); 1218 1219 if ((tty_read(buf,sizeof(buf))<0) || !strcmp(buf,".")){ 1220 done = 1; 1221 lstrval = -1; 1222 tty_prnt("Quitting %s!\n", argv0); 1223 vfpart = 0; 1224 return(-1); 1225 } 1226 1227 if ((buf[0] == '\0') || (buf[1] != '\0')) { 1228 tty_prnt("%s unknown command, try again\n",buf); 1229 continue; 1230 } 1231 1232 switch (buf[0]) { 1233 case 'y': 1234 case 'Y': 1235 /* 1236 * we are to continue with the same device 1237 */ 1238 if (ar_open(arcname) >= 0) 1239 return(0); 1240 tty_prnt("Cannot re-open %s, try again\n", 1241 arcname); 1242 continue; 1243 case 's': 1244 case 'S': 1245 /* 1246 * user wants to open a different device 1247 */ 1248 tty_prnt("Switching to a different archive\n"); 1249 break; 1250 default: 1251 tty_prnt("%s unknown command, try again\n",buf); 1252 continue; 1253 } 1254 break; 1255 } 1256 } else 1257 tty_prnt("Ready for archive volume: %d\n", arvol); 1258 1259 /* 1260 * have to go to a different archive 1261 */ 1262 for (;;) { 1263 tty_prnt("Input archive name or \".\" to quit %s.\n", argv0); 1264 tty_prnt("Archive name > "); 1265 1266 if ((tty_read(buf, sizeof(buf)) < 0) || !strcmp(buf, ".")) { 1267 done = 1; 1268 lstrval = -1; 1269 tty_prnt("Quitting %s!\n", argv0); 1270 vfpart = 0; 1271 return(-1); 1272 } 1273 if (buf[0] == '\0') { 1274 tty_prnt("Empty file name, try again\n"); 1275 continue; 1276 } 1277 if (!strcmp(buf, "..")) { 1278 tty_prnt("Illegal file name: .. try again\n"); 1279 continue; 1280 } 1281 if (strlen(buf) > PAXPATHLEN) { 1282 tty_prnt("File name too long, try again\n"); 1283 continue; 1284 } 1285 1286 /* 1287 * try to open new archive 1288 */ 1289 if (ar_open(buf) >= 0) { 1290 if (freeit) { 1291 (void)free(arcname); 1292 freeit = 0; 1293 } 1294 if ((arcname = strdup(buf)) == NULL) { 1295 done = 1; 1296 lstrval = -1; 1297 paxwarn(0, "Cannot save archive name."); 1298 return(-1); 1299 } 1300 freeit = 1; 1301 break; 1302 } 1303 tty_prnt("Cannot open %s, try again\n", buf); 1304 continue; 1305 } 1306 return(0); 1307 } 1308 1309 /* 1310 * ar_start_gzip() 1311 * starts the gzip compression/decompression process as a child, using magic 1312 * to keep the fd the same in the calling function (parent). 1313 */ 1314 void 1315 ar_start_gzip(int fd, const char *gzip_program, int wr) 1316 { 1317 int fds[2]; 1318 char *gzip_flags; 1319 1320 if (pipe(fds) < 0) 1321 err(1, "could not pipe"); 1322 zpid = fork(); 1323 if (zpid < 0) 1324 err(1, "could not fork"); 1325 1326 /* parent */ 1327 if (zpid) { 1328 if (wr) 1329 dup2(fds[1], fd); 1330 else 1331 dup2(fds[0], fd); 1332 close(fds[0]); 1333 close(fds[1]); 1334 } else { 1335 if (wr) { 1336 dup2(fds[0], STDIN_FILENO); 1337 dup2(fd, STDOUT_FILENO); 1338 gzip_flags = "-c"; 1339 } else { 1340 dup2(fds[1], STDOUT_FILENO); 1341 dup2(fd, STDIN_FILENO); 1342 gzip_flags = "-dc"; 1343 } 1344 close(fds[0]); 1345 close(fds[1]); 1346 if (execlp(gzip_program, gzip_program, gzip_flags, 1347 (char *)NULL) < 0) 1348 err(1, "could not exec"); 1349 /* NOTREACHED */ 1350 } 1351 } 1352