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