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