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