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