ar_io.c revision 241720
1204076Spjd/*- 2204076Spjd * Copyright (c) 1992 Keith Muller. 3218041Spjd * Copyright (c) 1992, 1993 4204076Spjd * The Regents of the University of California. All rights reserved. 5204076Spjd * 6204076Spjd * This code is derived from software contributed to Berkeley by 7204076Spjd * Keith Muller of the University of California, San Diego. 8204076Spjd * 9204076Spjd * Redistribution and use in source and binary forms, with or without 10204076Spjd * modification, are permitted provided that the following conditions 11204076Spjd * are met: 12204076Spjd * 1. Redistributions of source code must retain the above copyright 13204076Spjd * notice, this list of conditions and the following disclaimer. 14204076Spjd * 2. Redistributions in binary form must reproduce the above copyright 15204076Spjd * notice, this list of conditions and the following disclaimer in the 16204076Spjd * documentation and/or other materials provided with the distribution. 17204076Spjd * 4. Neither the name of the University nor the names of its contributors 18204076Spjd * may be used to endorse or promote products derived from this software 19204076Spjd * without specific prior written permission. 20204076Spjd * 21204076Spjd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22204076Spjd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23204076Spjd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24204076Spjd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25204076Spjd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26204076Spjd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27204076Spjd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28204076Spjd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29204076Spjd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30204076Spjd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31204076Spjd * SUCH DAMAGE. 32204076Spjd */ 33204076Spjd 34204076Spjd#ifndef lint 35204076Spjd#if 0 36204076Spjdstatic char sccsid[] = "@(#)ar_io.c 8.2 (Berkeley) 4/18/94"; 37218044Spjd#endif 38204076Spjd#endif /* not lint */ 39204076Spjd#include <sys/cdefs.h> 40204076Spjd__FBSDID("$FreeBSD: head/bin/pax/ar_io.c 241720 2012-10-19 05:43:38Z ed $"); 41204076Spjd 42204076Spjd#include <sys/types.h> 43204076Spjd#include <sys/ioctl.h> 44204076Spjd#include <sys/mtio.h> 45204076Spjd#include <sys/stat.h> 46204076Spjd#include <sys/wait.h> 47204076Spjd#include <err.h> 48204076Spjd#include <errno.h> 49204076Spjd#include <fcntl.h> 50204076Spjd#include <signal.h> 51204076Spjd#include <stdint.h> 52204076Spjd#include <stdio.h> 53204076Spjd#include <string.h> 54204076Spjd#include <stdlib.h> 55212038Spjd#include <unistd.h> 56204076Spjd#include "pax.h" 57204076Spjd#include "options.h" 58204076Spjd#include "extern.h" 59211977Spjd 60204076Spjd/* 61204076Spjd * Routines which deal directly with the archive I/O device/file. 62204076Spjd */ 63210886Spjd 64204076Spjd#define DMOD 0666 /* default mode of created archives */ 65204076Spjd#define EXT_MODE O_RDONLY /* open mode for list/extract */ 66204076Spjd#define AR_MODE (O_WRONLY | O_CREAT | O_TRUNC) /* mode for archive */ 67204076Spjd#define APP_MODE O_RDWR /* mode for append */ 68204076Spjd 69204076Spjdstatic char none[] = "<NONE>"; /* pseudo name for no file */ 70204076Spjdstatic char stdo[] = "<STDOUT>"; /* pseudo name for stdout */ 71211977Spjdstatic char stdn[] = "<STDIN>"; /* pseudo name for stdin */ 72213430Spjdstatic int arfd = -1; /* archive file descriptor */ 73211977Spjdstatic int artyp = ISREG; /* archive type: file/FIFO/tape */ 74204076Spjdstatic int arvol = 1; /* archive volume number */ 75204076Spjdstatic int lstrval = -1; /* return value from last i/o */ 76204076Spjdstatic int io_ok; /* i/o worked on volume after resync */ 77204076Spjdstatic int did_io; /* did i/o ever occur on volume? */ 78204076Spjdstatic int done; /* set via tty termination */ 79204076Spjdstatic struct stat arsb; /* stat of archive device at open */ 80204076Spjdstatic int invld_rec; /* tape has out of spec record size */ 81204076Spjdstatic int wr_trail = 1; /* trailer was rewritten in append */ 82204076Spjdstatic int can_unlnk = 0; /* do we unlink null archives? */ 83204076Spjdconst char *arcname; /* printable name of archive */ 84204076Spjdconst char *gzip_program; /* name of gzip program */ 85204076Spjdstatic pid_t zpid = -1; /* pid of child process */ 86204076Spjd 87204076Spjdstatic int get_phys(void); 88204076Spjdstatic void ar_start_gzip(int, const char *, int); 89204076Spjd 90204076Spjd/* 91204076Spjd * ar_open() 92204076Spjd * Opens the next archive volume. Determines the type of the device and 93204076Spjd * sets up block sizes as required by the archive device and the format. 94204076Spjd * Note: we may be called with name == NULL on the first open only. 95204076Spjd * Return: 96218041Spjd * -1 on failure, 0 otherwise 97218041Spjd */ 98218041Spjd 99218041Spjdint 100218041Spjdar_open(const char *name) 101218041Spjd{ 102218041Spjd struct mtget mb; 103218041Spjd 104218041Spjd if (arfd != -1) 105218041Spjd (void)close(arfd); 106218041Spjd arfd = -1; 107218041Spjd can_unlnk = did_io = io_ok = invld_rec = 0; 108218041Spjd artyp = ISREG; 109218041Spjd flcnt = 0; 110218041Spjd 111218041Spjd /* 112218370Spjd * open based on overall operation mode 113218370Spjd */ 114218370Spjd switch (act) { 115218370Spjd case LIST: 116218370Spjd case EXTRACT: 117218370Spjd if (name == NULL) { 118218041Spjd arfd = STDIN_FILENO; 119218041Spjd arcname = stdn; 120218041Spjd } else if ((arfd = open(name, EXT_MODE, DMOD)) < 0) 121218041Spjd syswarn(0, errno, "Failed open to read on %s", name); 122218041Spjd if (arfd != -1 && gzip_program != NULL) 123218041Spjd ar_start_gzip(arfd, gzip_program, 0); 124218041Spjd break; 125218041Spjd case ARCHIVE: 126218041Spjd if (name == NULL) { 127218041Spjd arfd = STDOUT_FILENO; 128218044Spjd arcname = stdo; 129218044Spjd } else if ((arfd = open(name, AR_MODE, DMOD)) < 0) 130218044Spjd syswarn(0, errno, "Failed open to write on %s", name); 131218044Spjd else 132218044Spjd can_unlnk = 1; 133218044Spjd if (arfd != -1 && gzip_program != NULL) 134218044Spjd ar_start_gzip(arfd, gzip_program, 1); 135218044Spjd break; 136218044Spjd case APPND: 137218044Spjd if (name == NULL) { 138218044Spjd arfd = STDOUT_FILENO; 139218044Spjd arcname = stdo; 140218044Spjd } else if ((arfd = open(name, APP_MODE, DMOD)) < 0) 141218044Spjd syswarn(0, errno, "Failed open to read/write on %s", 142218044Spjd name); 143218044Spjd break; 144218044Spjd case COPY: 145218044Spjd /* 146218044Spjd * arfd not used in COPY mode 147218044Spjd */ 148218044Spjd arcname = none; 149218044Spjd lstrval = 1; 150218044Spjd return(0); 151218044Spjd } 152218044Spjd if (arfd < 0) 153218044Spjd return(-1); 154218044Spjd 155218044Spjd if (chdname != NULL) 156218044Spjd if (chdir(chdname) != 0) { 157218044Spjd syswarn(1, errno, "Failed chdir to %s", chdname); 158218044Spjd return(-1); 159218044Spjd } 160218044Spjd /* 161218044Spjd * set up is based on device type 162218044Spjd */ 163218044Spjd if (fstat(arfd, &arsb) < 0) { 164218044Spjd syswarn(0, errno, "Failed stat on %s", arcname); 165218044Spjd (void)close(arfd); 166218044Spjd arfd = -1; 167218044Spjd can_unlnk = 0; 168218044Spjd return(-1); 169218044Spjd } 170218044Spjd if (S_ISDIR(arsb.st_mode)) { 171218373Spjd paxwarn(0, "Cannot write an archive on top of a directory %s", 172218373Spjd arcname); 173218373Spjd (void)close(arfd); 174218044Spjd arfd = -1; 175218373Spjd can_unlnk = 0; 176218044Spjd return(-1); 177218044Spjd } 178218044Spjd 179218044Spjd if (S_ISCHR(arsb.st_mode)) 180218044Spjd artyp = ioctl(arfd, MTIOCGET, &mb) ? ISCHR : ISTAPE; 181218044Spjd else if (S_ISBLK(arsb.st_mode)) 182218044Spjd artyp = ISBLK; 183218044Spjd else if ((lseek(arfd, (off_t)0L, SEEK_CUR) == -1) && (errno == ESPIPE)) 184218044Spjd artyp = ISPIPE; 185218044Spjd else 186218044Spjd artyp = ISREG; 187218044Spjd 188218044Spjd /* 189218044Spjd * make sure we beyond any doubt that we only can unlink regular files 190218044Spjd * we created 191218374Spjd */ 192218044Spjd if (artyp != ISREG) 193218044Spjd can_unlnk = 0; 194218044Spjd /* 195218044Spjd * if we are writing, we are done 196218044Spjd */ 197218044Spjd if (act == ARCHIVE) { 198218044Spjd blksz = rdblksz = wrblksz; 199218044Spjd lstrval = 1; 200218044Spjd return(0); 201218044Spjd } 202218044Spjd 203218044Spjd /* 204218044Spjd * set default blksz on read. APPNDs writes rdblksz on the last volume 205218044Spjd * On all new archive volumes, we shift to wrblksz (if the user 206218044Spjd * specified one, otherwise we will continue to use rdblksz). We 207218044Spjd * must to set blocksize based on what kind of device the archive is 208218044Spjd * stored. 209218044Spjd */ 210218044Spjd switch(artyp) { 211218044Spjd case ISTAPE: 212218044Spjd /* 213218044Spjd * Tape drives come in at least two flavors. Those that support 214218044Spjd * variable sized records and those that have fixed sized 215218044Spjd * records. They must be treated differently. For tape drives 216218044Spjd * that support variable sized records, we must make large 217218044Spjd * reads to make sure we get the entire record, otherwise we 218218044Spjd * will just get the first part of the record (up to size we 219218044Spjd * asked). Tapes with fixed sized records may or may not return 220218044Spjd * multiple records in a single read. We really do not care 221218044Spjd * what the physical record size is UNLESS we are going to 222218044Spjd * append. (We will need the physical block size to rewrite 223218044Spjd * the trailer). Only when we are appending do we go to the 224218044Spjd * effort to figure out the true PHYSICAL record size. 225218044Spjd */ 226218044Spjd blksz = rdblksz = MAXBLK; 227218044Spjd break; 228218218Spjd case ISPIPE: 229218218Spjd case ISBLK: 230218218Spjd case ISCHR: 231218218Spjd /* 232218218Spjd * Blocksize is not a major issue with these devices (but must 233218218Spjd * be kept a multiple of 512). If the user specified a write 234218218Spjd * block size, we use that to read. Under append, we must 235218218Spjd * always keep blksz == rdblksz. Otherwise we go ahead and use 236218218Spjd * the device optimal blocksize as (and if) returned by stat 237218218Spjd * and if it is within pax specs. 238218218Spjd */ 239218218Spjd if ((act == APPND) && wrblksz) { 240218218Spjd blksz = rdblksz = wrblksz; 241218044Spjd break; 242218044Spjd } 243218044Spjd 244218044Spjd if ((arsb.st_blksize > 0) && (arsb.st_blksize < MAXBLK) && 245218044Spjd ((arsb.st_blksize % BLKMULT) == 0)) 246218044Spjd rdblksz = arsb.st_blksize; 247218044Spjd else 248218044Spjd rdblksz = DEVBLK; 249218044Spjd /* 250218044Spjd * For performance go for large reads when we can without harm 251218044Spjd */ 252218044Spjd if ((act == APPND) || (artyp == ISCHR)) 253218044Spjd blksz = rdblksz; 254218044Spjd else 255218044Spjd blksz = MAXBLK; 256218044Spjd break; 257218044Spjd case ISREG: 258218044Spjd /* 259218044Spjd * if the user specified wrblksz works, use it. Under appends 260218044Spjd * we must always keep blksz == rdblksz 261218044Spjd */ 262218044Spjd if ((act == APPND) && wrblksz && ((arsb.st_size%wrblksz)==0)){ 263218044Spjd blksz = rdblksz = wrblksz; 264218044Spjd break; 265218044Spjd } 266218044Spjd /* 267218044Spjd * See if we can find the blocking factor from the file size 268218044Spjd */ 269218044Spjd for (rdblksz = MAXBLK; rdblksz > 0; rdblksz -= BLKMULT) 270218044Spjd if ((arsb.st_size % rdblksz) == 0) 271218044Spjd break; 272218044Spjd /* 273218044Spjd * When we cannot find a match, we may have a flawed archive. 274218044Spjd */ 275218044Spjd if (rdblksz <= 0) 276218044Spjd rdblksz = FILEBLK; 277218044Spjd /* 278218044Spjd * for performance go for large reads when we can 279218044Spjd */ 280218044Spjd if (act == APPND) 281218044Spjd blksz = rdblksz; 282218044Spjd else 283218044Spjd blksz = MAXBLK; 284218044Spjd break; 285218044Spjd default: 286204076Spjd /* 287207372Spjd * should never happen, worse case, slow... 288207372Spjd */ 289207372Spjd blksz = rdblksz = BLKMULT; 290207372Spjd break; 291207372Spjd } 292207372Spjd lstrval = 1; 293207372Spjd return(0); 294207372Spjd} 295207372Spjd 296207372Spjd/* 297207372Spjd * ar_close() 298207372Spjd * closes archive device, increments volume number, and prints i/o summary 299207372Spjd */ 300207372Spjdvoid 301207372Spjdar_close(void) 302207372Spjd{ 303204076Spjd int status; 304204076Spjd 305204076Spjd if (arfd < 0) { 306204076Spjd did_io = io_ok = flcnt = 0; 307204076Spjd return; 308204076Spjd } 309204076Spjd 310204076Spjd /* 311204076Spjd * Close archive file. This may take a LONG while on tapes (we may be 312204076Spjd * forced to wait for the rewind to complete) so tell the user what is 313204076Spjd * going on (this avoids the user hitting control-c thinking pax is 314204076Spjd * broken). 315204076Spjd */ 316204076Spjd if (vflag && (artyp == ISTAPE)) { 317204076Spjd if (vfpart) 318211977Spjd (void)putc('\n', listf); 319211977Spjd (void)fprintf(listf, 320204076Spjd "%s: Waiting for tape drive close to complete...", 321211977Spjd argv0); 322204076Spjd (void)fflush(listf); 323204076Spjd } 324204076Spjd 325204076Spjd /* 326207372Spjd * if nothing was written to the archive (and we created it), we remove 327213006Spjd * it 328204076Spjd */ 329207372Spjd if (can_unlnk && (fstat(arfd, &arsb) == 0) && (S_ISREG(arsb.st_mode)) && 330207372Spjd (arsb.st_size == 0)) { 331207372Spjd (void)unlink(arcname); 332207372Spjd can_unlnk = 0; 333207372Spjd } 334207372Spjd 335207372Spjd /* 336207348Spjd * for a quick extract/list, pax frequently exits before the child 337207348Spjd * process is done 338207348Spjd */ 339207348Spjd if ((act == LIST || act == EXTRACT) && nflag && zpid > 0) 340207348Spjd kill(zpid, SIGINT); 341207348Spjd 342207348Spjd (void)close(arfd); 343207348Spjd 344204076Spjd /* Do not exit before child to ensure data integrity */ 345204076Spjd if (zpid > 0) 346204076Spjd waitpid(zpid, &status, 0); 347204076Spjd 348204076Spjd if (vflag && (artyp == ISTAPE)) { 349210886Spjd (void)fputs("done.\n", listf); 350210886Spjd vfpart = 0; 351210886Spjd (void)fflush(listf); 352210886Spjd } 353210886Spjd arfd = -1; 354218138Spjd 355210886Spjd if (!io_ok && !did_io) { 356210886Spjd flcnt = 0; 357210886Spjd return; 358210886Spjd } 359210886Spjd did_io = io_ok = 0; 360210886Spjd 361210886Spjd /* 362210886Spjd * The volume number is only increased when the last device has data 363210886Spjd * and we have already determined the archive format. 364210886Spjd */ 365210886Spjd if (frmt != NULL) 366210886Spjd ++arvol; 367210886Spjd 368211886Spjd if (!vflag) { 369211886Spjd flcnt = 0; 370210886Spjd return; 371210886Spjd } 372210886Spjd 373210886Spjd /* 374210886Spjd * Print out a summary of I/O for this archive volume. 375210886Spjd */ 376210886Spjd if (vfpart) { 377210886Spjd (void)putc('\n', listf); 378210886Spjd vfpart = 0; 379218138Spjd } 380218138Spjd 381218138Spjd /* 382210886Spjd * If we have not determined the format yet, we just say how many bytes 383210886Spjd * we have skipped over looking for a header to id. There is no way we 384210886Spjd * could have written anything yet. 385210886Spjd */ 386210886Spjd if (frmt == NULL) { 387210886Spjd# ifdef NET2_STAT 388210886Spjd (void)fprintf(listf, "%s: unknown format, %lu bytes skipped.\n", 389210886Spjd argv0, rdcnt); 390210886Spjd# else 391210886Spjd (void)fprintf(listf, "%s: unknown format, %ju bytes skipped.\n", 392211886Spjd argv0, (uintmax_t)rdcnt); 393211886Spjd# endif 394210886Spjd (void)fflush(listf); 395210886Spjd flcnt = 0; 396210886Spjd return; 397204076Spjd } 398217784Spjd 399217784Spjd if (strcmp(NM_CPIO, argv0) == 0) 400217784Spjd (void)fprintf(listf, "%llu blocks\n", 401217784Spjd (unsigned long long)((rdcnt ? rdcnt : wrcnt) / 5120)); 402217784Spjd else if (strcmp(NM_TAR, argv0) != 0) 403218138Spjd (void)fprintf(listf, 404217784Spjd# ifdef NET2_STAT 405217784Spjd "%s: %s vol %d, %lu files, %lu bytes read, %lu bytes written.\n", 406217784Spjd argv0, frmt->name, arvol-1, flcnt, rdcnt, wrcnt); 407217784Spjd# else 408217784Spjd "%s: %s vol %d, %ju files, %ju bytes read, %ju bytes written.\n", 409217784Spjd argv0, frmt->name, arvol-1, (uintmax_t)flcnt, 410217784Spjd (uintmax_t)rdcnt, (uintmax_t)wrcnt); 411217784Spjd# endif 412217784Spjd (void)fflush(listf); 413217784Spjd flcnt = 0; 414217784Spjd} 415217784Spjd 416217784Spjd/* 417217784Spjd * ar_drain() 418217784Spjd * drain any archive format independent padding from an archive read 419217784Spjd * from a socket or a pipe. This is to prevent the process on the 420217784Spjd * other side of the pipe from getting a SIGPIPE (pax will stop 421217784Spjd * reading an archive once a format dependent trailer is detected). 422217784Spjd */ 423217784Spjdvoid 424217784Spjdar_drain(void) 425217784Spjd{ 426217784Spjd int res; 427217784Spjd char drbuf[MAXBLK]; 428217784Spjd 429217784Spjd /* 430217784Spjd * we only drain from a pipe/socket. Other devices can be closed 431217784Spjd * without reading up to end of file. We sure hope that pipe is closed 432217784Spjd * on the other side so we will get an EOF. 433217784Spjd */ 434217784Spjd if ((artyp != ISPIPE) || (lstrval <= 0)) 435217784Spjd return; 436217784Spjd 437204076Spjd /* 438204076Spjd * keep reading until pipe is drained 439210886Spjd */ 440210886Spjd while ((res = read(arfd, drbuf, sizeof(drbuf))) > 0) 441210886Spjd ; 442204076Spjd lstrval = res; 443210886Spjd} 444210886Spjd 445210886Spjd/* 446210886Spjd * ar_set_wr() 447210886Spjd * Set up device right before switching from read to write in an append. 448210886Spjd * device dependent code (if required) to do this should be added here. 449210886Spjd * For all archive devices we are already positioned at the place we want 450210886Spjd * to start writing when this routine is called. 451210886Spjd * Return: 452210886Spjd * 0 if all ready to write, -1 otherwise 453210886Spjd */ 454210886Spjd 455210886Spjdint 456210886Spjdar_set_wr(void) 457210886Spjd{ 458210886Spjd off_t cpos; 459210886Spjd 460210886Spjd /* 461210886Spjd * we must make sure the trailer is rewritten on append, ar_next() 462210886Spjd * will stop us if the archive containing the trailer was not written 463210886Spjd */ 464210886Spjd wr_trail = 0; 465210886Spjd 466210886Spjd /* 467210886Spjd * Add any device dependent code as required here 468210886Spjd */ 469210886Spjd if (artyp != ISREG) 470210886Spjd return(0); 471210886Spjd /* 472210886Spjd * Ok we have an archive in a regular file. If we were rewriting a 473210886Spjd * file, we must get rid of all the stuff after the current offset 474210886Spjd * (it was not written by pax). 475210886Spjd */ 476210886Spjd if (((cpos = lseek(arfd, (off_t)0L, SEEK_CUR)) < 0) || 477210886Spjd (ftruncate(arfd, cpos) < 0)) { 478210886Spjd syswarn(1, errno, "Unable to truncate archive file"); 479210886Spjd return(-1); 480210886Spjd } 481210886Spjd return(0); 482210886Spjd} 483210886Spjd 484210886Spjd/* 485210886Spjd * ar_app_ok() 486210886Spjd * check if the last volume in the archive allows appends. We cannot check 487210886Spjd * this until we are ready to write since there is no spec that says all 488210886Spjd * volumes in a single archive have to be of the same type... 489210886Spjd * Return: 490210886Spjd * 0 if we can append, -1 otherwise. 491210886Spjd */ 492210886Spjd 493210886Spjdint 494210886Spjdar_app_ok(void) 495210886Spjd{ 496210886Spjd if (artyp == ISPIPE) { 497210886Spjd paxwarn(1, "Cannot append to an archive obtained from a pipe."); 498210886Spjd return(-1); 499210886Spjd } 500210886Spjd 501210886Spjd if (!invld_rec) 502210886Spjd return(0); 503210886Spjd paxwarn(1,"Cannot append, device record size %d does not support %s spec", 504210886Spjd rdblksz, argv0); 505210886Spjd return(-1); 506210886Spjd} 507210886Spjd 508210886Spjd/* 509210886Spjd * ar_read() 510210886Spjd * read up to a specified number of bytes from the archive into the 511210886Spjd * supplied buffer. When dealing with tapes we may not always be able to 512210886Spjd * read what we want. 513210886Spjd * Return: 514210886Spjd * Number of bytes in buffer. 0 for end of file, -1 for a read error. 515210886Spjd */ 516210886Spjd 517210886Spjdint 518210886Spjdar_read(char *buf, int cnt) 519210886Spjd{ 520210886Spjd int res = 0; 521210886Spjd 522210886Spjd /* 523210886Spjd * if last i/o was in error, no more reads until reset or new volume 524210886Spjd */ 525210886Spjd if (lstrval <= 0) 526210886Spjd return(lstrval); 527210886Spjd 528210886Spjd /* 529210886Spjd * how we read must be based on device type 530210886Spjd */ 531210886Spjd switch (artyp) { 532210886Spjd case ISTAPE: 533210886Spjd if ((res = read(arfd, buf, cnt)) > 0) { 534210886Spjd /* 535210886Spjd * CAUTION: tape systems may not always return the same 536210886Spjd * sized records so we leave blksz == MAXBLK. The 537210886Spjd * physical record size that a tape drive supports is 538217729Spjd * very hard to determine in a uniform and portable 539217729Spjd * manner. 540217729Spjd */ 541210886Spjd io_ok = 1; 542210886Spjd if (res != rdblksz) { 543210886Spjd /* 544210886Spjd * Record size changed. If this is happens on 545210886Spjd * any record after the first, we probably have 546210886Spjd * a tape drive which has a fixed record size 547210886Spjd * we are getting multiple records in a single 548210886Spjd * read). Watch out for record blocking that 549210886Spjd * violates pax spec (must be a multiple of 550218138Spjd * BLKMULT). 551210886Spjd */ 552210886Spjd rdblksz = res; 553210886Spjd if (rdblksz % BLKMULT) 554210886Spjd invld_rec = 1; 555210886Spjd } 556210886Spjd return(res); 557210886Spjd } 558210886Spjd break; 559210886Spjd case ISREG: 560210886Spjd case ISBLK: 561210886Spjd case ISCHR: 562210886Spjd case ISPIPE: 563210886Spjd default: 564210886Spjd /* 565210886Spjd * Files are so easy to deal with. These other things cannot 566210886Spjd * be trusted at all. So when we are dealing with character 567210886Spjd * devices and pipes we just take what they have ready for us 568217729Spjd * and return. Trying to do anything else with them runs the 569217729Spjd * risk of failure. 570217784Spjd */ 571217784Spjd if ((res = read(arfd, buf, cnt)) > 0) { 572210886Spjd io_ok = 1; 573210886Spjd return(res); 574210886Spjd } 575210886Spjd break; 576210886Spjd } 577210886Spjd 578210886Spjd /* 579210886Spjd * We are in trouble at this point, something is broken... 580210886Spjd */ 581210886Spjd lstrval = res; 582210886Spjd if (res < 0) 583210886Spjd syswarn(1, errno, "Failed read on archive volume %d", arvol); 584210886Spjd else 585210886Spjd paxwarn(0, "End of archive volume %d reached", arvol); 586210886Spjd return(res); 587204076Spjd} 588204076Spjd 589204076Spjd/* 590211899Spjd * ar_write() 591211899Spjd * Write a specified number of bytes in supplied buffer to the archive 592211899Spjd * device so it appears as a single "block". Deals with errors and tries 593211899Spjd * to recover when faced with short writes. 594211899Spjd * Return: 595211899Spjd * Number of bytes written. 0 indicates end of volume reached and with no 596211899Spjd * flaws (as best that can be detected). A -1 indicates an unrecoverable 597211899Spjd * error in the archive occurred. 598211899Spjd */ 599211899Spjd 600211899Spjdint 601211899Spjdar_write(char *buf, int bsz) 602211899Spjd{ 603211899Spjd int res; 604211899Spjd off_t cpos; 605211899Spjd 606211899Spjd /* 607211899Spjd * do not allow pax to create a "bad" archive. Once a write fails on 608211899Spjd * an archive volume prevent further writes to it. 609204076Spjd */ 610204076Spjd if (lstrval <= 0) 611204076Spjd return(lstrval); 612204076Spjd 613204076Spjd if ((res = write(arfd, buf, bsz)) == bsz) { 614204076Spjd wr_trail = 1; 615204076Spjd io_ok = 1; 616204076Spjd return(bsz); 617204076Spjd } 618204076Spjd /* 619204076Spjd * write broke, see what we can do with it. We try to send any partial 620204076Spjd * writes that may violate pax spec to the next archive volume. 621204076Spjd */ 622204076Spjd if (res < 0) 623204076Spjd lstrval = res; 624204076Spjd else 625204076Spjd lstrval = 0; 626204076Spjd 627204076Spjd switch (artyp) { 628204076Spjd case ISREG: 629204076Spjd if ((res > 0) && (res % BLKMULT)) { 630204076Spjd /* 631209185Spjd * try to fix up partial writes which are not BLKMULT 632204076Spjd * in size by forcing the runt record to next archive 633207371Spjd * volume 634207371Spjd */ 635207371Spjd if ((cpos = lseek(arfd, (off_t)0L, SEEK_CUR)) < 0) 636207371Spjd break; 637204076Spjd cpos -= (off_t)res; 638204076Spjd if (ftruncate(arfd, cpos) < 0) 639204076Spjd break; 640204076Spjd res = lstrval = 0; 641204076Spjd break; 642204076Spjd } 643204076Spjd if (res >= 0) 644204076Spjd break; 645204076Spjd /* 646204076Spjd * if file is out of space, handle it like a return of 0 647204076Spjd */ 648204076Spjd if ((errno == ENOSPC) || (errno == EFBIG) || (errno == EDQUOT)) 649204076Spjd res = lstrval = 0; 650204076Spjd break; 651204076Spjd case ISTAPE: 652204076Spjd case ISCHR: 653204076Spjd case ISBLK: 654204076Spjd if (res >= 0) 655204076Spjd break; 656204076Spjd if (errno == EACCES) { 657204076Spjd paxwarn(0, "Write failed, archive is write protected."); 658204076Spjd res = lstrval = 0; 659204076Spjd return(0); 660204076Spjd } 661204076Spjd /* 662204076Spjd * see if we reached the end of media, if so force a change to 663204076Spjd * the next volume 664204076Spjd */ 665204076Spjd if ((errno == ENOSPC) || (errno == EIO) || (errno == ENXIO)) 666204076Spjd res = lstrval = 0; 667204076Spjd break; 668204076Spjd case ISPIPE: 669204076Spjd default: 670204076Spjd /* 671204076Spjd * we cannot fix errors to these devices 672204076Spjd */ 673204076Spjd break; 674204076Spjd } 675204076Spjd 676204076Spjd /* 677204076Spjd * Better tell the user the bad news... 678204076Spjd * if this is a block aligned archive format, we may have a bad archive 679204076Spjd * if the format wants the header to start at a BLKMULT boundary. While 680204076Spjd * we can deal with the mis-aligned data, it violates spec and other 681204076Spjd * archive readers will likely fail. If the format is not block 682204076Spjd * aligned, the user may be lucky (and the archive is ok). 683204076Spjd */ 684204076Spjd if (res >= 0) { 685204076Spjd if (res > 0) 686204076Spjd wr_trail = 1; 687204076Spjd io_ok = 1; 688204076Spjd } 689204076Spjd 690204076Spjd /* 691204076Spjd * If we were trying to rewrite the trailer and it didn't work, we 692204076Spjd * must quit right away. 693204076Spjd */ 694204076Spjd if (!wr_trail && (res <= 0)) { 695204076Spjd paxwarn(1,"Unable to append, trailer re-write failed. Quitting."); 696204076Spjd return(res); 697204076Spjd } 698204076Spjd 699204076Spjd if (res == 0) 700204076Spjd paxwarn(0, "End of archive volume %d reached", arvol); 701204076Spjd else if (res < 0) 702204076Spjd syswarn(1, errno, "Failed write to archive volume: %d", arvol); 703204076Spjd else if (!frmt->blkalgn || ((res % frmt->blkalgn) == 0)) 704204076Spjd paxwarn(0,"WARNING: partial archive write. Archive MAY BE FLAWED"); 705204076Spjd else 706204076Spjd paxwarn(1,"WARNING: partial archive write. Archive IS FLAWED"); 707204076Spjd return(res); 708204076Spjd} 709204076Spjd 710204076Spjd/* 711204076Spjd * ar_rdsync() 712204076Spjd * Try to move past a bad spot on a flawed archive as needed to continue 713204076Spjd * I/O. Clears error flags to allow I/O to continue. 714204076Spjd * Return: 715204076Spjd * 0 when ok to try i/o again, -1 otherwise. 716204076Spjd */ 717209185Spjd 718204076Spjdint 719204076Spjdar_rdsync(void) 720204076Spjd{ 721204076Spjd long fsbz; 722204076Spjd off_t cpos; 723204076Spjd off_t mpos; 724204076Spjd struct mtop mb; 725204076Spjd 726218138Spjd /* 727204076Spjd * Fail resync attempts at user request (done) or this is going to be 728204076Spjd * an update/append to an existing archive. If last i/o hit media end, 729218138Spjd * we need to go to the next volume not try a resync. 730204076Spjd */ 731204076Spjd if ((done > 0) || (lstrval == 0)) 732204076Spjd return(-1); 733204076Spjd 734204076Spjd if ((act == APPND) || (act == ARCHIVE)) { 735204076Spjd paxwarn(1, "Cannot allow updates to an archive with flaws."); 736204076Spjd return(-1); 737204076Spjd } 738204076Spjd if (io_ok) 739204076Spjd did_io = 1; 740204076Spjd 741204076Spjd switch(artyp) { 742204076Spjd case ISTAPE: 743204076Spjd /* 744204076Spjd * if the last i/o was a successful data transfer, we assume 745204076Spjd * the fault is just a bad record on the tape that we are now 746207372Spjd * past. If we did not get any data since the last resync try 747204076Spjd * to move the tape forward one PHYSICAL record past any 748204076Spjd * damaged tape section. Some tape drives are stubborn and need 749204076Spjd * to be pushed. 750204076Spjd */ 751207372Spjd if (io_ok) { 752204076Spjd io_ok = 0; 753213006Spjd lstrval = 1; 754204076Spjd break; 755204076Spjd } 756204076Spjd mb.mt_op = MTFSR; 757213981Spjd mb.mt_count = 1; 758213981Spjd if (ioctl(arfd, MTIOCTOP, &mb) < 0) 759204076Spjd break; 760204076Spjd lstrval = 1; 761204076Spjd break; 762204076Spjd case ISREG: 763204076Spjd case ISCHR: 764204076Spjd case ISBLK: 765204076Spjd /* 766204076Spjd * try to step over the bad part of the device. 767204076Spjd */ 768204076Spjd io_ok = 0; 769204076Spjd if (((fsbz = arsb.st_blksize) <= 0) || (artyp != ISREG)) 770204076Spjd fsbz = BLKMULT; 771204076Spjd if ((cpos = lseek(arfd, (off_t)0L, SEEK_CUR)) < 0) 772204076Spjd break; 773204076Spjd mpos = fsbz - (cpos % (off_t)fsbz); 774204076Spjd if (lseek(arfd, mpos, SEEK_CUR) < 0) 775204076Spjd break; 776204076Spjd lstrval = 1; 777204076Spjd break; 778204076Spjd case ISPIPE: 779204076Spjd default: 780204076Spjd /* 781204076Spjd * cannot recover on these archive device types 782204076Spjd */ 783204076Spjd io_ok = 0; 784204076Spjd break; 785204076Spjd } 786204076Spjd if (lstrval <= 0) { 787204076Spjd paxwarn(1, "Unable to recover from an archive read failure."); 788204076Spjd return(-1); 789204076Spjd } 790204076Spjd paxwarn(0, "Attempting to recover from an archive read failure."); 791204076Spjd return(0); 792204076Spjd} 793204076Spjd 794204076Spjd/* 795204076Spjd * ar_fow() 796204076Spjd * Move the I/O position within the archive forward the specified number of 797204076Spjd * bytes as supported by the device. If we cannot move the requested 798204076Spjd * number of bytes, return the actual number of bytes moved in skipped. 799204076Spjd * Return: 800204076Spjd * 0 if moved the requested distance, -1 on complete failure, 1 on 801204076Spjd * partial move (the amount moved is in skipped) 802204076Spjd */ 803204076Spjd 804204076Spjdint 805204076Spjdar_fow(off_t sksz, off_t *skipped) 806204076Spjd{ 807204076Spjd off_t cpos; 808204076Spjd off_t mpos; 809204076Spjd 810204076Spjd *skipped = 0; 811204076Spjd if (sksz <= 0) 812204076Spjd return(0); 813204076Spjd 814204076Spjd /* 815204076Spjd * we cannot move forward at EOF or error 816204076Spjd */ 817204076Spjd if (lstrval <= 0) 818204076Spjd return(lstrval); 819204076Spjd 820204076Spjd /* 821204076Spjd * Safer to read forward on devices where it is hard to find the end of 822204076Spjd * the media without reading to it. With tapes we cannot be sure of the 823204076Spjd * number of physical blocks to skip (we do not know physical block 824204076Spjd * size at this point), so we must only read forward on tapes! 825204076Spjd */ 826204076Spjd if (artyp != ISREG) 827204076Spjd return(0); 828204076Spjd 829218218Spjd /* 830218218Spjd * figure out where we are in the archive 831218218Spjd */ 832218218Spjd if ((cpos = lseek(arfd, (off_t)0L, SEEK_CUR)) >= 0) { 833218218Spjd /* 834218218Spjd * we can be asked to move farther than there are bytes in this 835218218Spjd * volume, if so, just go to file end and let normal buf_fill() 836218218Spjd * deal with the end of file (it will go to next volume by 837218218Spjd * itself) 838218218Spjd */ 839218218Spjd if ((mpos = cpos + sksz) > arsb.st_size) { 840218218Spjd *skipped = arsb.st_size - cpos; 841218218Spjd mpos = arsb.st_size; 842218218Spjd } else 843218218Spjd *skipped = sksz; 844218218Spjd if (lseek(arfd, mpos, SEEK_SET) >= 0) 845218218Spjd return(0); 846218218Spjd } 847218218Spjd syswarn(1, errno, "Forward positioning operation on archive failed"); 848218218Spjd lstrval = -1; 849218218Spjd return(-1); 850218218Spjd} 851218218Spjd 852218218Spjd/* 853218218Spjd * ar_rev() 854218218Spjd * move the i/o position within the archive backwards the specified byte 855218218Spjd * count as supported by the device. With tapes drives we RESET rdblksz to 856218218Spjd * the PHYSICAL blocksize. 857218218Spjd * NOTE: We should only be called to move backwards so we can rewrite the 858218218Spjd * last records (the trailer) of an archive (APPEND). 859218218Spjd * Return: 860218218Spjd * 0 if moved the requested distance, -1 on complete failure 861218218Spjd */ 862218218Spjd 863218218Spjdint 864204076Spjdar_rev(off_t sksz) 865204076Spjd{ 866212038Spjd off_t cpos; 867213009Spjd struct mtop mb; 868213009Spjd int phyblk; 869213009Spjd 870213009Spjd /* 871212037Spjd * make sure we do not have try to reverse on a flawed archive 872204076Spjd */ 873213009Spjd if (lstrval < 0) 874213009Spjd return(lstrval); 875213009Spjd 876213009Spjd switch(artyp) { 877211977Spjd case ISPIPE: 878213009Spjd if (sksz <= 0) 879213009Spjd break; 880213009Spjd /* 881213009Spjd * cannot go backwards on these critters 882213009Spjd */ 883213009Spjd paxwarn(1, "Reverse positioning on pipes is not supported."); 884216477Spjd lstrval = -1; 885216477Spjd return(-1); 886216477Spjd case ISREG: 887204076Spjd case ISBLK: 888213009Spjd case ISCHR: 889213009Spjd default: 890213009Spjd if (sksz <= 0) 891213009Spjd break; 892213009Spjd 893213009Spjd /* 894217967Spjd * For things other than files, backwards movement has a very 895213009Spjd * high probability of failure as we really do not know the 896213009Spjd * true attributes of the device we are talking to (the device 897213009Spjd * may not even have the ability to lseek() in any direction). 898213009Spjd * First we figure out where we are in the archive. 899213009Spjd */ 900213009Spjd if ((cpos = lseek(arfd, (off_t)0L, SEEK_CUR)) < 0) { 901213009Spjd syswarn(1, errno, 902213009Spjd "Unable to obtain current archive byte offset"); 903213009Spjd lstrval = -1; 904218138Spjd return(-1); 905213009Spjd } 906211899Spjd 907204076Spjd /* 908209177Spjd * we may try to go backwards past the start when the archive 909204076Spjd * is only a single record. If this happens and we are on a 910212038Spjd * multi volume archive, we need to go to the end of the 911218138Spjd * previous volume and continue our movement backwards from 912212038Spjd * there. 913212038Spjd */ 914218138Spjd if ((cpos -= sksz) < (off_t)0L) { 915212038Spjd if (arvol > 1) { 916212038Spjd /* 917212038Spjd * this should never happen 918212038Spjd */ 919212038Spjd paxwarn(1,"Reverse position on previous volume."); 920218218Spjd lstrval = -1; 921212038Spjd return(-1); 922218138Spjd } 923212038Spjd cpos = (off_t)0L; 924212038Spjd } 925218218Spjd if (lseek(arfd, cpos, SEEK_SET) < 0) { 926218218Spjd syswarn(1, errno, "Unable to seek archive backwards"); 927218218Spjd lstrval = -1; 928218218Spjd return(-1); 929218218Spjd } 930218218Spjd break; 931218218Spjd case ISTAPE: 932212038Spjd /* 933204076Spjd * Calculate and move the proper number of PHYSICAL tape 934218138Spjd * blocks. If the sksz is not an even multiple of the physical 935213009Spjd * tape size, we cannot do the move (this should never happen). 936211977Spjd * (We also cannot handler trailers spread over two vols). 937213429Spjd * get_phys() also makes sure we are in front of the filemark. 938211977Spjd */ 939204076Spjd if ((phyblk = get_phys()) <= 0) { 940204076Spjd lstrval = -1; 941204076Spjd return(-1); 942204076Spjd } 943204076Spjd 944204076Spjd /* 945212038Spjd * make sure future tape reads only go by physical tape block 946204076Spjd * size (set rdblksz to the real size). 947212038Spjd */ 948204076Spjd rdblksz = phyblk; 949212038Spjd 950212038Spjd /* 951212038Spjd * if no movement is required, just return (we must be after 952218218Spjd * get_phys() so the physical blocksize is properly set) 953212038Spjd */ 954212038Spjd if (sksz <= 0) 955212038Spjd break; 956212038Spjd 957212038Spjd /* 958212038Spjd * ok we have to move. Make sure the tape drive can do it. 959218218Spjd */ 960218218Spjd if (sksz % phyblk) { 961218218Spjd paxwarn(1, 962212038Spjd "Tape drive unable to backspace requested amount"); 963218218Spjd lstrval = -1; 964218218Spjd return(-1); 965218218Spjd } 966218218Spjd 967212038Spjd /* 968204076Spjd * move backwards the requested number of bytes 969204076Spjd */ 970204076Spjd mb.mt_op = MTBSR; 971213428Spjd mb.mt_count = sksz/phyblk; 972213428Spjd if (ioctl(arfd, MTIOCTOP, &mb) < 0) { 973213428Spjd syswarn(1,errno, "Unable to backspace tape %d blocks.", 974213428Spjd mb.mt_count); 975213428Spjd lstrval = -1; 976213428Spjd return(-1); 977204076Spjd } 978204076Spjd break; 979204076Spjd } 980204076Spjd lstrval = 1; 981204076Spjd return(0); 982204076Spjd} 983204076Spjd 984213009Spjd/* 985204076Spjd * get_phys() 986204076Spjd * Determine the physical block size on a tape drive. We need the physical 987204076Spjd * block size so we know how many bytes we skip over when we move with 988204076Spjd * mtio commands. We also make sure we are BEFORE THE TAPE FILEMARK when 989204076Spjd * return. 990204076Spjd * This is one really SLOW routine... 991204076Spjd * Return: 992204076Spjd * physical block size if ok (ok > 0), -1 otherwise 993204076Spjd */ 994204076Spjd 995204076Spjdstatic int 996204076Spjdget_phys(void) 997204076Spjd{ 998204076Spjd int padsz = 0; 999204076Spjd int res; 1000204076Spjd int phyblk; 1001204076Spjd struct mtop mb; 1002204076Spjd char scbuf[MAXBLK]; 1003204076Spjd 1004204076Spjd /* 1005204076Spjd * move to the file mark, and then back up one record and read it. 1006204076Spjd * this should tell us the physical record size the tape is using. 1007204076Spjd */ 1008204076Spjd if (lstrval == 1) { 1009204076Spjd /* 1010204076Spjd * we know we are at file mark when we get back a 0 from 1011204076Spjd * read() 1012204076Spjd */ 1013204076Spjd while ((res = read(arfd, scbuf, sizeof(scbuf))) > 0) 1014204076Spjd padsz += res; 1015204076Spjd if (res < 0) { 1016204076Spjd syswarn(1, errno, "Unable to locate tape filemark."); 1017217965Spjd return(-1); 1018204076Spjd } 1019204076Spjd } 1020214273Spjd 1021214273Spjd /* 1022204076Spjd * move backwards over the file mark so we are at the end of the 1023204076Spjd * last record. 1024204076Spjd */ 1025204076Spjd mb.mt_op = MTBSF; 1026204076Spjd mb.mt_count = 1; 1027204076Spjd if (ioctl(arfd, MTIOCTOP, &mb) < 0) { 1028204076Spjd syswarn(1, errno, "Unable to backspace over tape filemark."); 1029204076Spjd return(-1); 1030210879Spjd } 1031204076Spjd 1032204076Spjd /* 1033210883Spjd * move backwards so we are in front of the last record and read it to 1034218138Spjd * get physical tape blocksize. 1035204076Spjd */ 1036213428Spjd mb.mt_op = MTBSR; 1037217307Spjd mb.mt_count = 1; 1038217307Spjd if (ioctl(arfd, MTIOCTOP, &mb) < 0) { 1039217307Spjd syswarn(1, errno, "Unable to backspace over last tape block."); 1040217307Spjd return(-1); 1041217307Spjd } 1042217307Spjd if ((phyblk = read(arfd, scbuf, sizeof(scbuf))) <= 0) { 1043217307Spjd syswarn(1, errno, "Cannot determine archive tape blocksize."); 1044213428Spjd return(-1); 1045213428Spjd } 1046213428Spjd 1047213428Spjd /* 1048217307Spjd * read forward to the file mark, then back up in front of the filemark 1049213009Spjd * (this is a bit paranoid, but should be safe to do). 1050213009Spjd */ 1051213009Spjd while ((res = read(arfd, scbuf, sizeof(scbuf))) > 0) 1052213009Spjd ; 1053213009Spjd if (res < 0) { 1054213009Spjd syswarn(1, errno, "Unable to locate tape filemark."); 1055204076Spjd return(-1); 1056204076Spjd } 1057204076Spjd mb.mt_op = MTBSF; 1058204076Spjd mb.mt_count = 1; 1059204076Spjd if (ioctl(arfd, MTIOCTOP, &mb) < 0) { 1060204076Spjd syswarn(1, errno, "Unable to backspace over tape filemark."); 1061204076Spjd return(-1); 1062204076Spjd } 1063204076Spjd 1064204076Spjd /* 1065204076Spjd * set lstrval so we know that the filemark has not been seen 1066204076Spjd */ 1067204076Spjd lstrval = 1; 1068204076Spjd 1069204076Spjd /* 1070204076Spjd * return if there was no padding 1071204076Spjd */ 1072204076Spjd if (padsz == 0) 1073204076Spjd return(phyblk); 1074204076Spjd 1075204076Spjd /* 1076204076Spjd * make sure we can move backwards over the padding. (this should 1077204076Spjd * never fail). 1078204076Spjd */ 1079204076Spjd if (padsz % phyblk) { 1080204076Spjd paxwarn(1, "Tape drive unable to backspace requested amount"); 1081204076Spjd return(-1); 1082204076Spjd } 1083204076Spjd 1084204076Spjd /* 1085211977Spjd * move backwards over the padding so the head is where it was when 1086211977Spjd * we were first called (if required). 1087204076Spjd */ 1088204076Spjd mb.mt_op = MTBSR; 1089204076Spjd mb.mt_count = padsz/phyblk; 1090204076Spjd if (ioctl(arfd, MTIOCTOP, &mb) < 0) { 1091 syswarn(1,errno,"Unable to backspace tape over %d pad blocks", 1092 mb.mt_count); 1093 return(-1); 1094 } 1095 return(phyblk); 1096} 1097 1098/* 1099 * ar_next() 1100 * prompts the user for the next volume in this archive. For some devices 1101 * we may allow the media to be changed. Otherwise a new archive is 1102 * prompted for. By pax spec, if there is no controlling tty or an eof is 1103 * read on tty input, we must quit pax. 1104 * Return: 1105 * 0 when ready to continue, -1 when all done 1106 */ 1107 1108int 1109ar_next(void) 1110{ 1111 static char *arcbuf; 1112 char buf[PAXPATHLEN+2]; 1113 sigset_t o_mask; 1114 1115 /* 1116 * WE MUST CLOSE THE DEVICE. A lot of devices must see last close, (so 1117 * things like writing EOF etc will be done) (Watch out ar_close() can 1118 * also be called via a signal handler, so we must prevent a race. 1119 */ 1120 if (sigprocmask(SIG_BLOCK, &s_mask, &o_mask) < 0) 1121 syswarn(0, errno, "Unable to set signal mask"); 1122 ar_close(); 1123 if (sigprocmask(SIG_SETMASK, &o_mask, NULL) < 0) 1124 syswarn(0, errno, "Unable to restore signal mask"); 1125 1126 if (done || !wr_trail || strcmp(NM_TAR, argv0) == 0) 1127 return(-1); 1128 1129 tty_prnt("\nATTENTION! %s archive volume change required.\n", argv0); 1130 1131 /* 1132 * if i/o is on stdin or stdout, we cannot reopen it (we do not know 1133 * the name), the user will be forced to type it in. 1134 */ 1135 if (strcmp(arcname, stdo) && strcmp(arcname, stdn) && (artyp != ISREG) 1136 && (artyp != ISPIPE)) { 1137 if (artyp == ISTAPE) { 1138 tty_prnt("%s ready for archive tape volume: %d\n", 1139 arcname, arvol); 1140 tty_prnt("Load the NEXT TAPE on the tape drive"); 1141 } else { 1142 tty_prnt("%s ready for archive volume: %d\n", 1143 arcname, arvol); 1144 tty_prnt("Load the NEXT STORAGE MEDIA (if required)"); 1145 } 1146 1147 if ((act == ARCHIVE) || (act == APPND)) 1148 tty_prnt(" and make sure it is WRITE ENABLED.\n"); 1149 else 1150 tty_prnt("\n"); 1151 1152 for(;;) { 1153 tty_prnt("Type \"y\" to continue, \".\" to quit %s,", 1154 argv0); 1155 tty_prnt(" or \"s\" to switch to new device.\nIf you"); 1156 tty_prnt(" cannot change storage media, type \"s\"\n"); 1157 tty_prnt("Is the device ready and online? > "); 1158 1159 if ((tty_read(buf,sizeof(buf))<0) || !strcmp(buf,".")){ 1160 done = 1; 1161 lstrval = -1; 1162 tty_prnt("Quitting %s!\n", argv0); 1163 vfpart = 0; 1164 return(-1); 1165 } 1166 1167 if ((buf[0] == '\0') || (buf[1] != '\0')) { 1168 tty_prnt("%s unknown command, try again\n",buf); 1169 continue; 1170 } 1171 1172 switch (buf[0]) { 1173 case 'y': 1174 case 'Y': 1175 /* 1176 * we are to continue with the same device 1177 */ 1178 if (ar_open(arcname) >= 0) 1179 return(0); 1180 tty_prnt("Cannot re-open %s, try again\n", 1181 arcname); 1182 continue; 1183 case 's': 1184 case 'S': 1185 /* 1186 * user wants to open a different device 1187 */ 1188 tty_prnt("Switching to a different archive\n"); 1189 break; 1190 default: 1191 tty_prnt("%s unknown command, try again\n",buf); 1192 continue; 1193 } 1194 break; 1195 } 1196 } else 1197 tty_prnt("Ready for archive volume: %d\n", arvol); 1198 1199 /* 1200 * have to go to a different archive 1201 */ 1202 for (;;) { 1203 tty_prnt("Input archive name or \".\" to quit %s.\n", argv0); 1204 tty_prnt("Archive name > "); 1205 1206 if ((tty_read(buf, sizeof(buf)) < 0) || !strcmp(buf, ".")) { 1207 done = 1; 1208 lstrval = -1; 1209 tty_prnt("Quitting %s!\n", argv0); 1210 vfpart = 0; 1211 return(-1); 1212 } 1213 if (buf[0] == '\0') { 1214 tty_prnt("Empty file name, try again\n"); 1215 continue; 1216 } 1217 if (!strcmp(buf, "..")) { 1218 tty_prnt("Illegal file name: .. try again\n"); 1219 continue; 1220 } 1221 if (strlen(buf) > PAXPATHLEN) { 1222 tty_prnt("File name too long, try again\n"); 1223 continue; 1224 } 1225 1226 /* 1227 * try to open new archive 1228 */ 1229 if (ar_open(buf) >= 0) { 1230 free(arcbuf); 1231 if ((arcbuf = strdup(buf)) == NULL) { 1232 done = 1; 1233 lstrval = -1; 1234 paxwarn(0, "Cannot save archive name."); 1235 return(-1); 1236 } 1237 arcname = arcbuf; 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 */ 1251void 1252ar_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