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