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