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