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