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