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