xref: /minix/bin/pax/ar_io.c (revision 0a6a1f1d)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: ar_io.c,v 1.56 2015/03/09 23:38:08 sevan Exp $	*/
285fee539SLionel Sambuc 
385fee539SLionel Sambuc /*-
485fee539SLionel Sambuc  * Copyright (c) 1992 Keith Muller.
585fee539SLionel Sambuc  * Copyright (c) 1992, 1993
685fee539SLionel Sambuc  *	The Regents of the University of California.  All rights reserved.
785fee539SLionel Sambuc  *
885fee539SLionel Sambuc  * This code is derived from software contributed to Berkeley by
985fee539SLionel Sambuc  * Keith Muller of the University of California, San Diego.
1085fee539SLionel Sambuc  *
1185fee539SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
1285fee539SLionel Sambuc  * modification, are permitted provided that the following conditions
1385fee539SLionel Sambuc  * are met:
1485fee539SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
1585fee539SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
1685fee539SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
1785fee539SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
1885fee539SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
1985fee539SLionel Sambuc  * 3. Neither the name of the University nor the names of its contributors
2085fee539SLionel Sambuc  *    may be used to endorse or promote products derived from this software
2185fee539SLionel Sambuc  *    without specific prior written permission.
2285fee539SLionel Sambuc  *
2385fee539SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2485fee539SLionel Sambuc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2585fee539SLionel Sambuc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2685fee539SLionel Sambuc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2785fee539SLionel Sambuc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2885fee539SLionel Sambuc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2985fee539SLionel Sambuc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3085fee539SLionel Sambuc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3185fee539SLionel Sambuc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3285fee539SLionel Sambuc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3385fee539SLionel Sambuc  * SUCH DAMAGE.
3485fee539SLionel Sambuc  */
3585fee539SLionel Sambuc 
3685fee539SLionel Sambuc #if HAVE_NBTOOL_CONFIG_H
3785fee539SLionel Sambuc #include "nbtool_config.h"
3885fee539SLionel Sambuc #endif
3985fee539SLionel Sambuc 
4085fee539SLionel Sambuc #include <sys/cdefs.h>
4185fee539SLionel Sambuc #if !defined(lint)
4285fee539SLionel Sambuc #if 0
4385fee539SLionel Sambuc static char sccsid[] = "@(#)ar_io.c	8.2 (Berkeley) 4/18/94";
4485fee539SLionel Sambuc #else
45*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: ar_io.c,v 1.56 2015/03/09 23:38:08 sevan Exp $");
4685fee539SLionel Sambuc #endif
4785fee539SLionel Sambuc #endif /* not lint */
4885fee539SLionel Sambuc 
4985fee539SLionel Sambuc #include <sys/types.h>
5085fee539SLionel Sambuc #include <sys/param.h>
5185fee539SLionel Sambuc #include <sys/time.h>
5285fee539SLionel Sambuc #include <sys/stat.h>
5385fee539SLionel Sambuc #include <sys/ioctl.h>
5485fee539SLionel Sambuc #ifdef HAVE_SYS_MTIO_H
5585fee539SLionel Sambuc #include <sys/mtio.h>
5685fee539SLionel Sambuc #endif
5785fee539SLionel Sambuc #include <sys/wait.h>
5885fee539SLionel Sambuc #include <signal.h>
5985fee539SLionel Sambuc #include <string.h>
6085fee539SLionel Sambuc #include <fcntl.h>
6185fee539SLionel Sambuc #include <unistd.h>
6285fee539SLionel Sambuc #include <stdio.h>
6385fee539SLionel Sambuc #include <ctype.h>
6485fee539SLionel Sambuc #include <errno.h>
6585fee539SLionel Sambuc #include <stdlib.h>
6685fee539SLionel Sambuc #ifdef SUPPORT_RMT
6785fee539SLionel Sambuc #define __RMTLIB_PRIVATE
6885fee539SLionel Sambuc #include <rmt.h>
6985fee539SLionel Sambuc #endif /* SUPPORT_RMT */
7085fee539SLionel Sambuc #include "pax.h"
7185fee539SLionel Sambuc #include "options.h"
7285fee539SLionel Sambuc #include "extern.h"
7385fee539SLionel Sambuc 
7485fee539SLionel Sambuc /*
7585fee539SLionel Sambuc  * Routines which deal directly with the archive I/O device/file.
7685fee539SLionel Sambuc  */
7785fee539SLionel Sambuc 
7885fee539SLionel Sambuc #define DMOD		0666		/* default mode of created archives */
7985fee539SLionel Sambuc #define EXT_MODE	O_RDONLY	/* open mode for list/extract */
8085fee539SLionel Sambuc #define AR_MODE		(O_WRONLY | O_CREAT | O_TRUNC)	/* mode for archive */
8185fee539SLionel Sambuc #define APP_MODE	O_RDWR		/* mode for append */
8285fee539SLionel Sambuc static char STDO[] =	"<STDOUT>";	/* pseudo name for stdout */
8385fee539SLionel Sambuc static char STDN[] =	"<STDIN>";	/* pseudo name for stdin */
8485fee539SLionel Sambuc static char NONE[] =	"<NONE>";	/* pseudo name for none */
8585fee539SLionel Sambuc static int arfd = -1;			/* archive file descriptor */
8685fee539SLionel Sambuc static int artyp = ISREG;		/* archive type: file/FIFO/tape */
8785fee539SLionel Sambuc static int arvol = 1;			/* archive volume number */
8885fee539SLionel Sambuc static int lstrval = -1;		/* return value from last i/o */
8985fee539SLionel Sambuc static int io_ok;			/* i/o worked on volume after resync */
9085fee539SLionel Sambuc static int did_io;			/* did i/o ever occur on volume? */
9185fee539SLionel Sambuc static int done;			/* set via tty termination */
9285fee539SLionel Sambuc static struct stat arsb;		/* stat of archive device at open */
9385fee539SLionel Sambuc static int invld_rec;			/* tape has out of spec record size */
9485fee539SLionel Sambuc static int wr_trail = 1;		/* trailer was rewritten in append */
9585fee539SLionel Sambuc static int can_unlnk = 0;		/* do we unlink null archives?  */
9685fee539SLionel Sambuc const char *arcname;			/* printable name of archive */
9785fee539SLionel Sambuc const char *gzip_program;		/* name of gzip program */
9885fee539SLionel Sambuc static pid_t zpid = -1;			/* pid of child process */
9985fee539SLionel Sambuc time_t starttime;			/* time the run started */
10085fee539SLionel Sambuc int force_one_volume;			/* 1 if we ignore volume changes */
10185fee539SLionel Sambuc 
10285fee539SLionel Sambuc #ifdef HAVE_SYS_MTIO_H
10385fee539SLionel Sambuc static int get_phys(void);
10485fee539SLionel Sambuc #endif
10585fee539SLionel Sambuc extern sigset_t s_mask;
10685fee539SLionel Sambuc static void ar_start_gzip(int, const char *, int);
10785fee539SLionel Sambuc static const char *timefmt(char *, size_t, off_t, time_t, const char *);
10885fee539SLionel Sambuc static const char *sizefmt(char *, size_t, off_t);
10985fee539SLionel Sambuc 
11085fee539SLionel Sambuc #ifdef SUPPORT_RMT
11185fee539SLionel Sambuc #ifdef SYS_NO_RESTART
11285fee539SLionel Sambuc static int rmtread_with_restart(int, void *, int);
11385fee539SLionel Sambuc static int rmtwrite_with_restart(int, void *, int);
11485fee539SLionel Sambuc #else
11585fee539SLionel Sambuc #define rmtread_with_restart(a, b, c) rmtread((a), (b), (c))
11685fee539SLionel Sambuc #define rmtwrite_with_restart(a, b, c) rmtwrite((a), (b), (c))
11785fee539SLionel Sambuc #endif
11885fee539SLionel Sambuc #endif /* SUPPORT_RMT */
11985fee539SLionel Sambuc 
12085fee539SLionel Sambuc /*
12185fee539SLionel Sambuc  * ar_open()
12285fee539SLionel Sambuc  *	Opens the next archive volume. Determines the type of the device and
12385fee539SLionel Sambuc  *	sets up block sizes as required by the archive device and the format.
12485fee539SLionel Sambuc  *	Note: we may be called with name == NULL on the first open only.
12585fee539SLionel Sambuc  * Return:
12685fee539SLionel Sambuc  *	-1 on failure, 0 otherwise
12785fee539SLionel Sambuc  */
12885fee539SLionel Sambuc 
12985fee539SLionel Sambuc int
ar_open(const char * name)13085fee539SLionel Sambuc ar_open(const char *name)
13185fee539SLionel Sambuc {
13285fee539SLionel Sambuc #ifdef HAVE_SYS_MTIO_H
13385fee539SLionel Sambuc 	struct mtget mb;
13485fee539SLionel Sambuc #endif
13585fee539SLionel Sambuc 
13685fee539SLionel Sambuc 	if (arfd != -1)
13785fee539SLionel Sambuc 		(void)close(arfd);
13885fee539SLionel Sambuc 	arfd = -1;
13985fee539SLionel Sambuc 	can_unlnk = did_io = io_ok = invld_rec = 0;
14085fee539SLionel Sambuc 	artyp = ISREG;
14185fee539SLionel Sambuc 	flcnt = 0;
14285fee539SLionel Sambuc 
14385fee539SLionel Sambuc #ifdef SUPPORT_RMT
14485fee539SLionel Sambuc 	if (name && strchr(name, ':') != NULL && !forcelocal) {
14585fee539SLionel Sambuc 		artyp = ISRMT;
14685fee539SLionel Sambuc 		if ((arfd = rmtopen(name, O_RDWR, DMOD)) == -1) {
14785fee539SLionel Sambuc 			syswarn(0, errno, "Failed open on %s", name);
14885fee539SLionel Sambuc 			return -1;
14985fee539SLionel Sambuc 		}
15085fee539SLionel Sambuc 		if (!isrmt(arfd)) {
15185fee539SLionel Sambuc 			rmtclose(arfd);
15285fee539SLionel Sambuc 			tty_warn(0, "Not a remote file: %s", name);
15385fee539SLionel Sambuc 			return -1;
15485fee539SLionel Sambuc 		}
15585fee539SLionel Sambuc 		blksz = rdblksz = 8192;
15685fee539SLionel Sambuc 		lstrval = 1;
15785fee539SLionel Sambuc 		return 0;
15885fee539SLionel Sambuc 	}
15985fee539SLionel Sambuc #endif /* SUPPORT_RMT */
16085fee539SLionel Sambuc 
16185fee539SLionel Sambuc 	/*
16285fee539SLionel Sambuc 	 * open based on overall operation mode
16385fee539SLionel Sambuc 	 */
16485fee539SLionel Sambuc 	switch (act) {
16585fee539SLionel Sambuc 	case LIST:
16685fee539SLionel Sambuc 	case EXTRACT:
16785fee539SLionel Sambuc 		if (name == NULL) {
16885fee539SLionel Sambuc 			arfd = STDIN_FILENO;
16985fee539SLionel Sambuc 			arcname = STDN;
17085fee539SLionel Sambuc 		} else if ((arfd = open(name, EXT_MODE, DMOD)) < 0)
17185fee539SLionel Sambuc 			syswarn(0, errno, "Failed open to read on %s", name);
17285fee539SLionel Sambuc 		if (arfd != -1 && gzip_program != NULL)
17385fee539SLionel Sambuc 			ar_start_gzip(arfd, gzip_program, 0);
17485fee539SLionel Sambuc 		break;
17585fee539SLionel Sambuc 	case ARCHIVE:
17685fee539SLionel Sambuc 		if (name == NULL) {
17785fee539SLionel Sambuc 			arfd = STDOUT_FILENO;
17885fee539SLionel Sambuc 			arcname = STDO;
17985fee539SLionel Sambuc 		} else if ((arfd = open(name, AR_MODE, DMOD)) < 0)
18085fee539SLionel Sambuc 			syswarn(0, errno, "Failed open to write on %s", name);
18185fee539SLionel Sambuc 		else
18285fee539SLionel Sambuc 			can_unlnk = 1;
18385fee539SLionel Sambuc 		if (arfd != -1 && gzip_program != NULL)
18485fee539SLionel Sambuc 			ar_start_gzip(arfd, gzip_program, 1);
18585fee539SLionel Sambuc 		break;
18685fee539SLionel Sambuc 	case APPND:
18785fee539SLionel Sambuc 		if (name == NULL) {
18885fee539SLionel Sambuc 			arfd = STDOUT_FILENO;
18985fee539SLionel Sambuc 			arcname = STDO;
19085fee539SLionel Sambuc 		} else if ((arfd = open(name, APP_MODE, DMOD)) < 0)
19185fee539SLionel Sambuc 			syswarn(0, errno, "Failed open to read/write on %s",
19285fee539SLionel Sambuc 				name);
19385fee539SLionel Sambuc 		break;
19485fee539SLionel Sambuc 	case COPY:
19585fee539SLionel Sambuc 		/*
19685fee539SLionel Sambuc 		 * arfd not used in COPY mode
19785fee539SLionel Sambuc 		 */
19885fee539SLionel Sambuc 		arcname = NONE;
19985fee539SLionel Sambuc 		lstrval = 1;
20085fee539SLionel Sambuc 		return 0;
20185fee539SLionel Sambuc 	}
20285fee539SLionel Sambuc 	if (arfd < 0)
20385fee539SLionel Sambuc 		return -1;
20485fee539SLionel Sambuc 
20585fee539SLionel Sambuc 	if (chdname != NULL)
20685fee539SLionel Sambuc 		if (dochdir(chdname) == -1)
20785fee539SLionel Sambuc 			return -1;
20885fee539SLionel Sambuc 	/*
20985fee539SLionel Sambuc 	 * set up is based on device type
21085fee539SLionel Sambuc 	 */
21185fee539SLionel Sambuc 	if (fstat(arfd, &arsb) < 0) {
21285fee539SLionel Sambuc 		syswarn(0, errno, "Failed stat on %s", arcname);
21385fee539SLionel Sambuc 		(void)close(arfd);
21485fee539SLionel Sambuc 		arfd = -1;
21585fee539SLionel Sambuc 		can_unlnk = 0;
21685fee539SLionel Sambuc 		return -1;
21785fee539SLionel Sambuc 	}
21885fee539SLionel Sambuc 	if (S_ISDIR(arsb.st_mode)) {
21985fee539SLionel Sambuc 		tty_warn(0, "Cannot write an archive on top of a directory %s",
22085fee539SLionel Sambuc 		    arcname);
22185fee539SLionel Sambuc 		(void)close(arfd);
22285fee539SLionel Sambuc 		arfd = -1;
22385fee539SLionel Sambuc 		can_unlnk = 0;
22485fee539SLionel Sambuc 		return -1;
22585fee539SLionel Sambuc 	}
22685fee539SLionel Sambuc 
22785fee539SLionel Sambuc 	if (S_ISCHR(arsb.st_mode)) {
22885fee539SLionel Sambuc #ifdef HAVE_SYS_MTIO_H
22985fee539SLionel Sambuc 		artyp = ioctl(arfd, MTIOCGET, &mb) ? ISCHR : ISTAPE;
23085fee539SLionel Sambuc #else
23185fee539SLionel Sambuc 		tty_warn(1, "System does not have tape support");
23285fee539SLionel Sambuc 		artyp = ISREG;
23385fee539SLionel Sambuc #endif
23485fee539SLionel Sambuc 	} else if (S_ISBLK(arsb.st_mode))
23585fee539SLionel Sambuc 		artyp = ISBLK;
23685fee539SLionel Sambuc 	else if ((lseek(arfd, (off_t)0L, SEEK_CUR) == -1) && (errno == ESPIPE))
23785fee539SLionel Sambuc 		artyp = ISPIPE;
23885fee539SLionel Sambuc 	else
23985fee539SLionel Sambuc 		artyp = ISREG;
24085fee539SLionel Sambuc 
24185fee539SLionel Sambuc 	/*
24285fee539SLionel Sambuc 	 * Special handling for empty files.
24385fee539SLionel Sambuc 	 */
24485fee539SLionel Sambuc 	if (artyp == ISREG && arsb.st_size == 0) {
24585fee539SLionel Sambuc 		switch (act) {
24685fee539SLionel Sambuc 		case LIST:
24785fee539SLionel Sambuc 		case EXTRACT:
24885fee539SLionel Sambuc 			return -1;
24985fee539SLionel Sambuc 		case APPND:
25085fee539SLionel Sambuc 			act = -ARCHIVE;
25185fee539SLionel Sambuc 			return -1;
25285fee539SLionel Sambuc 		case ARCHIVE:
25385fee539SLionel Sambuc 			break;
25485fee539SLionel Sambuc 		}
25585fee539SLionel Sambuc 	}
25685fee539SLionel Sambuc 
25785fee539SLionel Sambuc 	/*
258*0a6a1f1dSLionel Sambuc 	 * make sure beyond any doubt that we can unlink only regular files
25985fee539SLionel Sambuc 	 * we created
26085fee539SLionel Sambuc 	 */
26185fee539SLionel Sambuc 	if (artyp != ISREG)
26285fee539SLionel Sambuc 		can_unlnk = 0;
26385fee539SLionel Sambuc 
26485fee539SLionel Sambuc 	/*
26585fee539SLionel Sambuc 	 * if we are writing, we are done
26685fee539SLionel Sambuc 	 */
26785fee539SLionel Sambuc 	if (act == ARCHIVE) {
26885fee539SLionel Sambuc 		blksz = rdblksz = wrblksz;
26985fee539SLionel Sambuc 		lstrval = 1;
27085fee539SLionel Sambuc 		return 0;
27185fee539SLionel Sambuc 	}
27285fee539SLionel Sambuc 
27385fee539SLionel Sambuc 	/*
27485fee539SLionel Sambuc 	 * set default blksz on read. APPNDs writes rdblksz on the last volume
27585fee539SLionel Sambuc 	 * On all new archive volumes, we shift to wrblksz (if the user
27685fee539SLionel Sambuc 	 * specified one, otherwize we will continue to use rdblksz). We
27785fee539SLionel Sambuc 	 * must set blocksize based on what kind of device the archive is
27885fee539SLionel Sambuc 	 * stored.
27985fee539SLionel Sambuc 	 */
28085fee539SLionel Sambuc 	switch(artyp) {
28185fee539SLionel Sambuc 	case ISTAPE:
28285fee539SLionel Sambuc 		/*
28385fee539SLionel Sambuc 		 * Tape drives come in at least two flavors. Those that support
28485fee539SLionel Sambuc 		 * variable sized records and those that have fixed sized
28585fee539SLionel Sambuc 		 * records. They must be treated differently. For tape drives
28685fee539SLionel Sambuc 		 * that support variable sized records, we must make large
28785fee539SLionel Sambuc 		 * reads to make sure we get the entire record, otherwise we
28885fee539SLionel Sambuc 		 * will just get the first part of the record (up to size we
28985fee539SLionel Sambuc 		 * asked). Tapes with fixed sized records may or may not return
29085fee539SLionel Sambuc 		 * multiple records in a single read. We really do not care
29185fee539SLionel Sambuc 		 * what the physical record size is UNLESS we are going to
29285fee539SLionel Sambuc 		 * append. (We will need the physical block size to rewrite
29385fee539SLionel Sambuc 		 * the trailer). Only when we are appending do we go to the
29485fee539SLionel Sambuc 		 * effort to figure out the true PHYSICAL record size.
29585fee539SLionel Sambuc 		 */
29685fee539SLionel Sambuc 		blksz = rdblksz = MAXBLK;
29785fee539SLionel Sambuc 		break;
29885fee539SLionel Sambuc 	case ISPIPE:
29985fee539SLionel Sambuc 	case ISBLK:
30085fee539SLionel Sambuc 	case ISCHR:
30185fee539SLionel Sambuc 		/*
30285fee539SLionel Sambuc 		 * Blocksize is not a major issue with these devices (but must
30385fee539SLionel Sambuc 		 * be kept a multiple of 512). If the user specified a write
30485fee539SLionel Sambuc 		 * block size, we use that to read. Under append, we must
30585fee539SLionel Sambuc 		 * always keep blksz == rdblksz. Otherwise we go ahead and use
30685fee539SLionel Sambuc 		 * the device optimal blocksize as (and if) returned by stat
30785fee539SLionel Sambuc 		 * and if it is within pax specs.
30885fee539SLionel Sambuc 		 */
30985fee539SLionel Sambuc 		if ((act == APPND) && wrblksz) {
31085fee539SLionel Sambuc 			blksz = rdblksz = wrblksz;
31185fee539SLionel Sambuc 			break;
31285fee539SLionel Sambuc 		}
31385fee539SLionel Sambuc 
31485fee539SLionel Sambuc 		if ((arsb.st_blksize > 0) && (arsb.st_blksize < MAXBLK) &&
31585fee539SLionel Sambuc 		    ((arsb.st_blksize % BLKMULT) == 0))
31685fee539SLionel Sambuc 			rdblksz = arsb.st_blksize;
31785fee539SLionel Sambuc 		else
31885fee539SLionel Sambuc 			rdblksz = DEVBLK;
31985fee539SLionel Sambuc 		/*
32085fee539SLionel Sambuc 		 * For performance go for large reads when we can without harm
32185fee539SLionel Sambuc 		 */
32285fee539SLionel Sambuc 		if ((act == APPND) || (artyp == ISCHR))
32385fee539SLionel Sambuc 			blksz = rdblksz;
32485fee539SLionel Sambuc 		else
32585fee539SLionel Sambuc 			blksz = MAXBLK;
32685fee539SLionel Sambuc 		break;
32785fee539SLionel Sambuc 	case ISREG:
32885fee539SLionel Sambuc 		/*
32985fee539SLionel Sambuc 		 * if the user specified wrblksz works, use it. Under appends
33085fee539SLionel Sambuc 		 * we must always keep blksz == rdblksz
33185fee539SLionel Sambuc 		 */
33285fee539SLionel Sambuc 		if ((act == APPND) && wrblksz && ((arsb.st_size%wrblksz)==0)){
33385fee539SLionel Sambuc 			blksz = rdblksz = wrblksz;
33485fee539SLionel Sambuc 			break;
33585fee539SLionel Sambuc 		}
33685fee539SLionel Sambuc 		/*
33785fee539SLionel Sambuc 		 * See if we can find the blocking factor from the file size
33885fee539SLionel Sambuc 		 */
33985fee539SLionel Sambuc 		for (rdblksz = MAXBLK; rdblksz > 0; rdblksz -= BLKMULT)
34085fee539SLionel Sambuc 			if ((arsb.st_size % rdblksz) == 0)
34185fee539SLionel Sambuc 				break;
34285fee539SLionel Sambuc 		/*
34385fee539SLionel Sambuc 		 * When we cannot find a match, we may have a flawed archive.
34485fee539SLionel Sambuc 		 */
34585fee539SLionel Sambuc 		if (rdblksz <= 0)
34685fee539SLionel Sambuc 			rdblksz = FILEBLK;
34785fee539SLionel Sambuc 		/*
34885fee539SLionel Sambuc 		 * for performance go for large reads when we can
34985fee539SLionel Sambuc 		 */
35085fee539SLionel Sambuc 		if (act == APPND)
35185fee539SLionel Sambuc 			blksz = rdblksz;
35285fee539SLionel Sambuc 		else
35385fee539SLionel Sambuc 			blksz = MAXBLK;
35485fee539SLionel Sambuc 		break;
35585fee539SLionel Sambuc 	default:
35685fee539SLionel Sambuc 		/*
35785fee539SLionel Sambuc 		 * should never happen, worst case, slow...
35885fee539SLionel Sambuc 		 */
35985fee539SLionel Sambuc 		blksz = rdblksz = BLKMULT;
36085fee539SLionel Sambuc 		break;
36185fee539SLionel Sambuc 	}
36285fee539SLionel Sambuc 	lstrval = 1;
36385fee539SLionel Sambuc 	return 0;
36485fee539SLionel Sambuc }
36585fee539SLionel Sambuc 
36685fee539SLionel Sambuc /*
36785fee539SLionel Sambuc  * ar_close()
36885fee539SLionel Sambuc  *	closes archive device, increments volume number, and prints i/o summary
36985fee539SLionel Sambuc  */
37085fee539SLionel Sambuc void
ar_close(void)37185fee539SLionel Sambuc ar_close(void)
37285fee539SLionel Sambuc {
37385fee539SLionel Sambuc 	int status;
37485fee539SLionel Sambuc 
37585fee539SLionel Sambuc 	if (arfd < 0) {
37685fee539SLionel Sambuc 		did_io = io_ok = flcnt = 0;
37785fee539SLionel Sambuc 		return;
37885fee539SLionel Sambuc 	}
37985fee539SLionel Sambuc 
38085fee539SLionel Sambuc 
38185fee539SLionel Sambuc 	/*
38285fee539SLionel Sambuc 	 * Close archive file. This may take a LONG while on tapes (we may be
38385fee539SLionel Sambuc 	 * forced to wait for the rewind to complete) so tell the user what is
38485fee539SLionel Sambuc 	 * going on (this avoids the user hitting control-c thinking pax is
38585fee539SLionel Sambuc 	 * broken).
38685fee539SLionel Sambuc 	 */
38785fee539SLionel Sambuc 	if ((vflag || Vflag) && (artyp == ISTAPE)) {
38885fee539SLionel Sambuc 		if (vfpart)
38985fee539SLionel Sambuc 			(void)putc('\n', listf);
39085fee539SLionel Sambuc 		(void)fprintf(listf,
39185fee539SLionel Sambuc 			"%s: Waiting for tape drive close to complete...",
39285fee539SLionel Sambuc 			argv0);
39385fee539SLionel Sambuc 		(void)fflush(listf);
39485fee539SLionel Sambuc 	}
39585fee539SLionel Sambuc 
39685fee539SLionel Sambuc 	/*
39785fee539SLionel Sambuc 	 * if nothing was written to the archive (and we created it), we remove
39885fee539SLionel Sambuc 	 * it
39985fee539SLionel Sambuc 	 */
40085fee539SLionel Sambuc 	if (can_unlnk && (fstat(arfd, &arsb) == 0) && (S_ISREG(arsb.st_mode)) &&
40185fee539SLionel Sambuc 	    (arsb.st_size == 0)) {
40285fee539SLionel Sambuc 		(void)unlink(arcname);
40385fee539SLionel Sambuc 		can_unlnk = 0;
40485fee539SLionel Sambuc 	}
40585fee539SLionel Sambuc 
40685fee539SLionel Sambuc 	/*
40785fee539SLionel Sambuc 	 * for a quick extract/list, pax frequently exits before the child
40885fee539SLionel Sambuc 	 * process is done
40985fee539SLionel Sambuc 	 */
41085fee539SLionel Sambuc 	if ((act == LIST || act == EXTRACT) && nflag && zpid > 0)
41185fee539SLionel Sambuc 		kill(zpid, SIGINT);
41285fee539SLionel Sambuc 
41385fee539SLionel Sambuc #ifdef SUPPORT_RMT
41485fee539SLionel Sambuc 	if (artyp == ISRMT)
41585fee539SLionel Sambuc 		(void)rmtclose(arfd);
41685fee539SLionel Sambuc 	else
41785fee539SLionel Sambuc #endif /* SUPPORT_RMT */
41885fee539SLionel Sambuc 		(void)close(arfd);
41985fee539SLionel Sambuc 
42085fee539SLionel Sambuc 	/* Do not exit before child to ensure data integrity */
42185fee539SLionel Sambuc 	if (zpid > 0)
42285fee539SLionel Sambuc 		waitpid(zpid, &status, 0);
42385fee539SLionel Sambuc 
42485fee539SLionel Sambuc 	if ((vflag || Vflag) && (artyp == ISTAPE)) {
42585fee539SLionel Sambuc 		(void)fputs("done.\n", listf);
42685fee539SLionel Sambuc 		vfpart = 0;
42785fee539SLionel Sambuc 		(void)fflush(listf);
42885fee539SLionel Sambuc 	}
42985fee539SLionel Sambuc 	arfd = -1;
43085fee539SLionel Sambuc 
43185fee539SLionel Sambuc 	if (!io_ok && !did_io) {
43285fee539SLionel Sambuc 		flcnt = 0;
43385fee539SLionel Sambuc 		return;
43485fee539SLionel Sambuc 	}
43585fee539SLionel Sambuc 	did_io = io_ok = 0;
43685fee539SLionel Sambuc 
43785fee539SLionel Sambuc 	/*
43885fee539SLionel Sambuc 	 * The volume number is only increased when the last device has data
43985fee539SLionel Sambuc 	 * and we have already determined the archive format.
44085fee539SLionel Sambuc 	 */
44185fee539SLionel Sambuc 	if (frmt != NULL)
44285fee539SLionel Sambuc 		++arvol;
44385fee539SLionel Sambuc 
44485fee539SLionel Sambuc 	if (!vflag && !Vflag) {
44585fee539SLionel Sambuc 		flcnt = 0;
44685fee539SLionel Sambuc 		return;
44785fee539SLionel Sambuc 	}
44885fee539SLionel Sambuc 
44985fee539SLionel Sambuc 	/*
45085fee539SLionel Sambuc 	 * Print out a summary of I/O for this archive volume.
45185fee539SLionel Sambuc 	 */
45285fee539SLionel Sambuc 	if (vfpart) {
45385fee539SLionel Sambuc 		(void)putc('\n', listf);
45485fee539SLionel Sambuc 		vfpart = 0;
45585fee539SLionel Sambuc 	}
45685fee539SLionel Sambuc 
45785fee539SLionel Sambuc 	/* mimic cpio's block count first */
45885fee539SLionel Sambuc 	if (frmt && strcmp(NM_CPIO, argv0) == 0) {
45985fee539SLionel Sambuc 		(void)fprintf(listf, OFFT_F " blocks\n",
46085fee539SLionel Sambuc 		    (rdcnt ? rdcnt : wrcnt) / 5120);
46185fee539SLionel Sambuc 	}
46285fee539SLionel Sambuc 
46385fee539SLionel Sambuc 	ar_summary(0);
46485fee539SLionel Sambuc 
46585fee539SLionel Sambuc 	(void)fflush(listf);
46685fee539SLionel Sambuc 	flcnt = 0;
46785fee539SLionel Sambuc }
46885fee539SLionel Sambuc 
46985fee539SLionel Sambuc /*
47085fee539SLionel Sambuc  * ar_drain()
47185fee539SLionel Sambuc  *	drain any archive format independent padding from an archive read
47285fee539SLionel Sambuc  *	from a socket or a pipe. This is to prevent the process on the
47385fee539SLionel Sambuc  *	other side of the pipe from getting a SIGPIPE (pax will stop
47485fee539SLionel Sambuc  *	reading an archive once a format dependent trailer is detected).
47585fee539SLionel Sambuc  */
47685fee539SLionel Sambuc void
ar_drain(void)47785fee539SLionel Sambuc ar_drain(void)
47885fee539SLionel Sambuc {
47985fee539SLionel Sambuc 	int res;
48085fee539SLionel Sambuc 	char drbuf[MAXBLK];
48185fee539SLionel Sambuc 
48285fee539SLionel Sambuc 	/*
48385fee539SLionel Sambuc 	 * we only drain from a pipe/socket. Other devices can be closed
48485fee539SLionel Sambuc 	 * without reading up to end of file. We sure hope that pipe is closed
48585fee539SLionel Sambuc 	 * on the other side so we will get an EOF.
48685fee539SLionel Sambuc 	 */
48785fee539SLionel Sambuc 	if ((artyp != ISPIPE) || (lstrval <= 0))
48885fee539SLionel Sambuc 		return;
48985fee539SLionel Sambuc 
49085fee539SLionel Sambuc 	/*
49185fee539SLionel Sambuc 	 * keep reading until pipe is drained
49285fee539SLionel Sambuc 	 */
49385fee539SLionel Sambuc #ifdef SUPPORT_RMT
49485fee539SLionel Sambuc 	if (artyp == ISRMT) {
49585fee539SLionel Sambuc 		while ((res = rmtread_with_restart(arfd,
49685fee539SLionel Sambuc 						   drbuf, sizeof(drbuf))) > 0)
49785fee539SLionel Sambuc 			continue;
49885fee539SLionel Sambuc 	} else {
49985fee539SLionel Sambuc #endif /* SUPPORT_RMT */
50085fee539SLionel Sambuc 		while ((res = read_with_restart(arfd,
50185fee539SLionel Sambuc 						drbuf, sizeof(drbuf))) > 0)
50285fee539SLionel Sambuc 			continue;
50385fee539SLionel Sambuc #ifdef SUPPORT_RMT
50485fee539SLionel Sambuc 	}
50585fee539SLionel Sambuc #endif /* SUPPORT_RMT */
50685fee539SLionel Sambuc 	lstrval = res;
50785fee539SLionel Sambuc }
50885fee539SLionel Sambuc 
50985fee539SLionel Sambuc /*
51085fee539SLionel Sambuc  * ar_set_wr()
51185fee539SLionel Sambuc  *	Set up device right before switching from read to write in an append.
51285fee539SLionel Sambuc  *	device dependent code (if required) to do this should be added here.
51385fee539SLionel Sambuc  *	For all archive devices we are already positioned at the place we want
51485fee539SLionel Sambuc  *	to start writing when this routine is called.
51585fee539SLionel Sambuc  * Return:
51685fee539SLionel Sambuc  *	0 if all ready to write, -1 otherwise
51785fee539SLionel Sambuc  */
51885fee539SLionel Sambuc 
51985fee539SLionel Sambuc int
ar_set_wr(void)52085fee539SLionel Sambuc ar_set_wr(void)
52185fee539SLionel Sambuc {
52285fee539SLionel Sambuc 	off_t cpos;
52385fee539SLionel Sambuc 
52485fee539SLionel Sambuc 	/*
52585fee539SLionel Sambuc 	 * we must make sure the trailer is rewritten on append, ar_next()
52685fee539SLionel Sambuc 	 * will stop us if the archive containing the trailer was not written
52785fee539SLionel Sambuc 	 */
52885fee539SLionel Sambuc 	wr_trail = 0;
52985fee539SLionel Sambuc 
53085fee539SLionel Sambuc 	/*
53185fee539SLionel Sambuc 	 * Add any device dependent code as required here
53285fee539SLionel Sambuc 	 */
53385fee539SLionel Sambuc 	if (artyp != ISREG)
53485fee539SLionel Sambuc 		return 0;
53585fee539SLionel Sambuc 	/*
53685fee539SLionel Sambuc 	 * Ok we have an archive in a regular file. If we were rewriting a
53785fee539SLionel Sambuc 	 * file, we must get rid of all the stuff after the current offset
53885fee539SLionel Sambuc 	 * (it was not written by pax).
53985fee539SLionel Sambuc 	 */
54085fee539SLionel Sambuc 	if (((cpos = lseek(arfd, (off_t)0L, SEEK_CUR)) < 0) ||
54185fee539SLionel Sambuc 	    (ftruncate(arfd, cpos) < 0)) {
54285fee539SLionel Sambuc 		syswarn(1, errno, "Unable to truncate archive file");
54385fee539SLionel Sambuc 		return -1;
54485fee539SLionel Sambuc 	}
54585fee539SLionel Sambuc 	return 0;
54685fee539SLionel Sambuc }
54785fee539SLionel Sambuc 
54885fee539SLionel Sambuc /*
54985fee539SLionel Sambuc  * ar_app_ok()
55085fee539SLionel Sambuc  *	check if the last volume in the archive allows appends. We cannot check
55185fee539SLionel Sambuc  *	this until we are ready to write since there is no spec that says all
55285fee539SLionel Sambuc  *	volumes in a single archive have to be of the same type...
55385fee539SLionel Sambuc  * Return:
55485fee539SLionel Sambuc  *	0 if we can append, -1 otherwise.
55585fee539SLionel Sambuc  */
55685fee539SLionel Sambuc 
55785fee539SLionel Sambuc int
ar_app_ok(void)55885fee539SLionel Sambuc ar_app_ok(void)
55985fee539SLionel Sambuc {
56085fee539SLionel Sambuc 	if (artyp == ISPIPE) {
56185fee539SLionel Sambuc 		tty_warn(1,
56285fee539SLionel Sambuc 		    "Cannot append to an archive obtained from a pipe.");
56385fee539SLionel Sambuc 		return -1;
56485fee539SLionel Sambuc 	}
56585fee539SLionel Sambuc 
56685fee539SLionel Sambuc 	if (!invld_rec)
56785fee539SLionel Sambuc 		return 0;
56885fee539SLionel Sambuc 	tty_warn(1,
56985fee539SLionel Sambuc 	    "Cannot append, device record size %d does not support %s spec",
57085fee539SLionel Sambuc 	    rdblksz, argv0);
57185fee539SLionel Sambuc 	return -1;
57285fee539SLionel Sambuc }
57385fee539SLionel Sambuc 
57485fee539SLionel Sambuc #ifdef SYS_NO_RESTART
57585fee539SLionel Sambuc /*
57685fee539SLionel Sambuc  * read_with_restart()
57785fee539SLionel Sambuc  *	Equivalent to read() but does retry on signals.
57885fee539SLionel Sambuc  *	This function is not needed on 4.2BSD and later.
57985fee539SLionel Sambuc  * Return:
58085fee539SLionel Sambuc  *	Number of bytes written.  -1 indicates an error.
58185fee539SLionel Sambuc  */
58285fee539SLionel Sambuc 
58385fee539SLionel Sambuc int
read_with_restart(int fd,void * buf,int bsz)58485fee539SLionel Sambuc read_with_restart(int fd, void *buf, int bsz)
58585fee539SLionel Sambuc {
58685fee539SLionel Sambuc 	int r;
58785fee539SLionel Sambuc 
58885fee539SLionel Sambuc 	while (((r = read(fd, buf, bsz)) < 0) && errno == EINTR)
58985fee539SLionel Sambuc 		continue;
59085fee539SLionel Sambuc 
59185fee539SLionel Sambuc 	return r;
59285fee539SLionel Sambuc }
59385fee539SLionel Sambuc 
59485fee539SLionel Sambuc /*
59585fee539SLionel Sambuc  * rmtread_with_restart()
59685fee539SLionel Sambuc  *	Equivalent to rmtread() but does retry on signals.
59785fee539SLionel Sambuc  *	This function is not needed on 4.2BSD and later.
59885fee539SLionel Sambuc  * Return:
59985fee539SLionel Sambuc  *	Number of bytes written.  -1 indicates an error.
60085fee539SLionel Sambuc  */
60185fee539SLionel Sambuc static int
rmtread_with_restart(int fd,void * buf,int bsz)60285fee539SLionel Sambuc rmtread_with_restart(int fd, void *buf, int bsz)
60385fee539SLionel Sambuc {
60485fee539SLionel Sambuc 	int r;
60585fee539SLionel Sambuc 
60685fee539SLionel Sambuc 	while (((r = rmtread(fd, buf, bsz)) < 0) && errno == EINTR)
60785fee539SLionel Sambuc 		continue;
60885fee539SLionel Sambuc 
60985fee539SLionel Sambuc 	return r;
61085fee539SLionel Sambuc }
61185fee539SLionel Sambuc #endif
61285fee539SLionel Sambuc 
61385fee539SLionel Sambuc /*
61485fee539SLionel Sambuc  * xread()
61585fee539SLionel Sambuc  *	Equivalent to read() but does retry on partial read, which may occur
61685fee539SLionel Sambuc  *	on signals.
61785fee539SLionel Sambuc  * Return:
61885fee539SLionel Sambuc  *	Number of bytes read.  0 for end of file, -1 for an error.
61985fee539SLionel Sambuc  */
62085fee539SLionel Sambuc 
62185fee539SLionel Sambuc int
xread(int fd,void * buf,int bsz)62285fee539SLionel Sambuc xread(int fd, void *buf, int bsz)
62385fee539SLionel Sambuc {
62485fee539SLionel Sambuc 	char *b = buf;
62585fee539SLionel Sambuc 	int nread = 0;
62685fee539SLionel Sambuc 	int r;
62785fee539SLionel Sambuc 
62885fee539SLionel Sambuc 	do {
62985fee539SLionel Sambuc #ifdef SUPPORT_RMT
63085fee539SLionel Sambuc 		if ((r = rmtread_with_restart(fd, b, bsz)) <= 0)
63185fee539SLionel Sambuc 			break;
63285fee539SLionel Sambuc #else
63385fee539SLionel Sambuc 		if ((r = read_with_restart(fd, b, bsz)) <= 0)
63485fee539SLionel Sambuc 			break;
63585fee539SLionel Sambuc #endif /* SUPPORT_RMT */
63685fee539SLionel Sambuc 		b += r;
63785fee539SLionel Sambuc 		bsz -= r;
63885fee539SLionel Sambuc 		nread += r;
63985fee539SLionel Sambuc 	} while (bsz > 0);
64085fee539SLionel Sambuc 
64185fee539SLionel Sambuc 	return nread ? nread : r;
64285fee539SLionel Sambuc }
64385fee539SLionel Sambuc 
64485fee539SLionel Sambuc #ifdef SYS_NO_RESTART
64585fee539SLionel Sambuc /*
64685fee539SLionel Sambuc  * write_with_restart()
64785fee539SLionel Sambuc  *	Equivalent to write() but does retry on signals.
64885fee539SLionel Sambuc  *	This function is not needed on 4.2BSD and later.
64985fee539SLionel Sambuc  * Return:
65085fee539SLionel Sambuc  *	Number of bytes written.  -1 indicates an error.
65185fee539SLionel Sambuc  */
65285fee539SLionel Sambuc 
65385fee539SLionel Sambuc int
write_with_restart(int fd,void * buf,int bsz)65485fee539SLionel Sambuc write_with_restart(int fd, void *buf, int bsz)
65585fee539SLionel Sambuc {
65685fee539SLionel Sambuc 	int r;
65785fee539SLionel Sambuc 
65885fee539SLionel Sambuc 	while (((r = write(fd, buf, bsz)) < 0) && errno == EINTR)
65985fee539SLionel Sambuc 		;
66085fee539SLionel Sambuc 
66185fee539SLionel Sambuc 	return r;
66285fee539SLionel Sambuc }
66385fee539SLionel Sambuc 
66485fee539SLionel Sambuc /*
66585fee539SLionel Sambuc  * rmtwrite_with_restart()
66685fee539SLionel Sambuc  *	Equivalent to write() but does retry on signals.
66785fee539SLionel Sambuc  *	This function is not needed on 4.2BSD and later.
66885fee539SLionel Sambuc  * Return:
66985fee539SLionel Sambuc  *	Number of bytes written.  -1 indicates an error.
67085fee539SLionel Sambuc  */
67185fee539SLionel Sambuc 
67285fee539SLionel Sambuc static int
rmtwrite_with_restart(int fd,void * buf,int bsz)67385fee539SLionel Sambuc rmtwrite_with_restart(int fd, void *buf, int bsz)
67485fee539SLionel Sambuc {
67585fee539SLionel Sambuc 	int r;
67685fee539SLionel Sambuc 
67785fee539SLionel Sambuc 	while (((r = rmtwrite(fd, buf, bsz)) < 0) && errno == EINTR)
67885fee539SLionel Sambuc 		;
67985fee539SLionel Sambuc 
68085fee539SLionel Sambuc 	return r;
68185fee539SLionel Sambuc }
68285fee539SLionel Sambuc #endif
68385fee539SLionel Sambuc 
68485fee539SLionel Sambuc /*
68585fee539SLionel Sambuc  * xwrite()
68685fee539SLionel Sambuc  *	Equivalent to write() but does retry on partial write, which may occur
68785fee539SLionel Sambuc  *	on signals.
68885fee539SLionel Sambuc  * Return:
68985fee539SLionel Sambuc  *	Number of bytes written.  -1 indicates an error.
69085fee539SLionel Sambuc  */
69185fee539SLionel Sambuc 
69285fee539SLionel Sambuc int
xwrite(int fd,void * buf,int bsz)69385fee539SLionel Sambuc xwrite(int fd, void *buf, int bsz)
69485fee539SLionel Sambuc {
69585fee539SLionel Sambuc 	char *b = buf;
69685fee539SLionel Sambuc 	int written = 0;
69785fee539SLionel Sambuc 	int r;
69885fee539SLionel Sambuc 
69985fee539SLionel Sambuc 	do {
70085fee539SLionel Sambuc #ifdef SUPPORT_RMT
70185fee539SLionel Sambuc 		if ((r = rmtwrite_with_restart(fd, b, bsz)) <= 0)
70285fee539SLionel Sambuc 			break;
70385fee539SLionel Sambuc #else
70485fee539SLionel Sambuc 		if ((r = write_with_restart(fd, b, bsz)) <= 0)
70585fee539SLionel Sambuc 			break;
70685fee539SLionel Sambuc #endif /* SUPPORT_RMT */
70785fee539SLionel Sambuc 		b += r;
70885fee539SLionel Sambuc 		bsz -= r;
70985fee539SLionel Sambuc 		written += r;
71085fee539SLionel Sambuc 	} while (bsz > 0);
71185fee539SLionel Sambuc 
71285fee539SLionel Sambuc 	return written ? written : r;
71385fee539SLionel Sambuc }
71485fee539SLionel Sambuc 
71585fee539SLionel Sambuc /*
71685fee539SLionel Sambuc  * ar_read()
71785fee539SLionel Sambuc  *	read up to a specified number of bytes from the archive into the
71885fee539SLionel Sambuc  *	supplied buffer. When dealing with tapes we may not always be able to
71985fee539SLionel Sambuc  *	read what we want.
72085fee539SLionel Sambuc  * Return:
72185fee539SLionel Sambuc  *	Number of bytes in buffer. 0 for end of file, -1 for a read error.
72285fee539SLionel Sambuc  */
72385fee539SLionel Sambuc 
72485fee539SLionel Sambuc int
ar_read(char * buf,int cnt)72585fee539SLionel Sambuc ar_read(char *buf, int cnt)
72685fee539SLionel Sambuc {
72785fee539SLionel Sambuc 	int res = 0;
72885fee539SLionel Sambuc 
72985fee539SLionel Sambuc 	/*
73085fee539SLionel Sambuc 	 * if last i/o was in error, no more reads until reset or new volume
73185fee539SLionel Sambuc 	 */
73285fee539SLionel Sambuc 	if (lstrval <= 0)
73385fee539SLionel Sambuc 		return lstrval;
73485fee539SLionel Sambuc 
73585fee539SLionel Sambuc 	/*
73685fee539SLionel Sambuc 	 * how we read must be based on device type
73785fee539SLionel Sambuc 	 */
73885fee539SLionel Sambuc 	switch (artyp) {
73985fee539SLionel Sambuc #ifdef SUPPORT_RMT
74085fee539SLionel Sambuc 	case ISRMT:
74185fee539SLionel Sambuc 		if ((res = rmtread_with_restart(arfd, buf, cnt)) > 0) {
74285fee539SLionel Sambuc 			io_ok = 1;
74385fee539SLionel Sambuc 			return res;
74485fee539SLionel Sambuc 		}
74585fee539SLionel Sambuc 		break;
74685fee539SLionel Sambuc #endif /* SUPPORT_RMT */
74785fee539SLionel Sambuc 	case ISTAPE:
74885fee539SLionel Sambuc 		if ((res = read_with_restart(arfd, buf, cnt)) > 0) {
74985fee539SLionel Sambuc 			/*
75085fee539SLionel Sambuc 			 * CAUTION: tape systems may not always return the same
75185fee539SLionel Sambuc 			 * sized records so we leave blksz == MAXBLK. The
75285fee539SLionel Sambuc 			 * physical record size that a tape drive supports is
75385fee539SLionel Sambuc 			 * very hard to determine in a uniform and portable
75485fee539SLionel Sambuc 			 * manner.
75585fee539SLionel Sambuc 			 */
75685fee539SLionel Sambuc 			io_ok = 1;
75785fee539SLionel Sambuc 			if (res != rdblksz) {
75885fee539SLionel Sambuc 				/*
75985fee539SLionel Sambuc 				 * Record size changed. If this happens on
76085fee539SLionel Sambuc 				 * any record after the first, we probably have
76185fee539SLionel Sambuc 				 * a tape drive which has a fixed record size
76285fee539SLionel Sambuc 				 * (we are getting multiple records in a single
76385fee539SLionel Sambuc 				 * read). Watch out for record blocking that
76485fee539SLionel Sambuc 				 * violates pax spec (must be a multiple of
76585fee539SLionel Sambuc 				 * BLKMULT).
76685fee539SLionel Sambuc 				 */
76785fee539SLionel Sambuc 				rdblksz = res;
76885fee539SLionel Sambuc 				if (rdblksz % BLKMULT)
76985fee539SLionel Sambuc 					invld_rec = 1;
77085fee539SLionel Sambuc 			}
77185fee539SLionel Sambuc 			return res;
77285fee539SLionel Sambuc 		}
77385fee539SLionel Sambuc 		break;
77485fee539SLionel Sambuc 	case ISREG:
77585fee539SLionel Sambuc 	case ISBLK:
77685fee539SLionel Sambuc 	case ISCHR:
77785fee539SLionel Sambuc 	case ISPIPE:
77885fee539SLionel Sambuc 	default:
77985fee539SLionel Sambuc 		/*
78085fee539SLionel Sambuc 		 * Files are so easy to deal with. These other things cannot
78185fee539SLionel Sambuc 		 * be trusted at all. So when we are dealing with character
78285fee539SLionel Sambuc 		 * devices and pipes we just take what they have ready for us
78385fee539SLionel Sambuc 		 * and return. Trying to do anything else with them runs the
78485fee539SLionel Sambuc 		 * risk of failure.
78585fee539SLionel Sambuc 		 */
78685fee539SLionel Sambuc 		if ((res = read_with_restart(arfd, buf, cnt)) > 0) {
78785fee539SLionel Sambuc 			io_ok = 1;
78885fee539SLionel Sambuc 			return res;
78985fee539SLionel Sambuc 		}
79085fee539SLionel Sambuc 		break;
79185fee539SLionel Sambuc 	}
79285fee539SLionel Sambuc 
79385fee539SLionel Sambuc 	/*
79485fee539SLionel Sambuc 	 * We are in trouble at this point, something is broken...
79585fee539SLionel Sambuc 	 */
79685fee539SLionel Sambuc 	lstrval = res;
79785fee539SLionel Sambuc 	if (res < 0)
79885fee539SLionel Sambuc 		syswarn(1, errno, "Failed read on archive volume %d", arvol);
79985fee539SLionel Sambuc 	else
80085fee539SLionel Sambuc 		tty_warn(0, "End of archive volume %d reached", arvol);
80185fee539SLionel Sambuc 	return res;
80285fee539SLionel Sambuc }
80385fee539SLionel Sambuc 
80485fee539SLionel Sambuc /*
80585fee539SLionel Sambuc  * ar_write()
80685fee539SLionel Sambuc  *	Write a specified number of bytes in supplied buffer to the archive
80785fee539SLionel Sambuc  *	device so it appears as a single "block". Deals with errors and tries
80885fee539SLionel Sambuc  *	to recover when faced with short writes.
80985fee539SLionel Sambuc  * Return:
81085fee539SLionel Sambuc  *	Number of bytes written. 0 indicates end of volume reached and with no
81185fee539SLionel Sambuc  *	flaws (as best that can be detected). A -1 indicates an unrecoverable
81285fee539SLionel Sambuc  *	error in the archive occurred.
81385fee539SLionel Sambuc  */
81485fee539SLionel Sambuc 
81585fee539SLionel Sambuc int
ar_write(char * buf,int bsz)81685fee539SLionel Sambuc ar_write(char *buf, int bsz)
81785fee539SLionel Sambuc {
81885fee539SLionel Sambuc 	int res;
81985fee539SLionel Sambuc 	off_t cpos;
82085fee539SLionel Sambuc 
82185fee539SLionel Sambuc 	/*
82285fee539SLionel Sambuc 	 * do not allow pax to create a "bad" archive. Once a write fails on
82385fee539SLionel Sambuc 	 * an archive volume prevent further writes to it.
82485fee539SLionel Sambuc 	 */
82585fee539SLionel Sambuc 	if (lstrval <= 0)
82685fee539SLionel Sambuc 		return lstrval;
82785fee539SLionel Sambuc 
82885fee539SLionel Sambuc 	if ((res = xwrite(arfd, buf, bsz)) == bsz) {
82985fee539SLionel Sambuc 		wr_trail = 1;
83085fee539SLionel Sambuc 		io_ok = 1;
83185fee539SLionel Sambuc 		return bsz;
83285fee539SLionel Sambuc 	}
83385fee539SLionel Sambuc 	/*
83485fee539SLionel Sambuc 	 * write broke, see what we can do with it. We try to send any partial
83585fee539SLionel Sambuc 	 * writes that may violate pax spec to the next archive volume.
83685fee539SLionel Sambuc 	 */
83785fee539SLionel Sambuc 	if (res < 0)
83885fee539SLionel Sambuc 		lstrval = res;
83985fee539SLionel Sambuc 	else
84085fee539SLionel Sambuc 		lstrval = 0;
84185fee539SLionel Sambuc 
84285fee539SLionel Sambuc 	switch (artyp) {
84385fee539SLionel Sambuc 	case ISREG:
84485fee539SLionel Sambuc 		if ((res > 0) && (res % BLKMULT)) {
84585fee539SLionel Sambuc 			/*
84685fee539SLionel Sambuc 			 * try to fix up partial writes which are not BLKMULT
84785fee539SLionel Sambuc 			 * in size by forcing the runt record to next archive
84885fee539SLionel Sambuc 			 * volume
84985fee539SLionel Sambuc 			 */
85085fee539SLionel Sambuc 			if ((cpos = lseek(arfd, (off_t)0L, SEEK_CUR)) < 0)
85185fee539SLionel Sambuc 				break;
85285fee539SLionel Sambuc 			cpos -= (off_t)res;
85385fee539SLionel Sambuc 			if (ftruncate(arfd, cpos) < 0)
85485fee539SLionel Sambuc 				break;
85585fee539SLionel Sambuc 			res = lstrval = 0;
85685fee539SLionel Sambuc 			break;
85785fee539SLionel Sambuc 		}
85885fee539SLionel Sambuc 		if (res >= 0)
85985fee539SLionel Sambuc 			break;
86085fee539SLionel Sambuc 		/*
86185fee539SLionel Sambuc 		 * if file is out of space, handle it like a return of 0
86285fee539SLionel Sambuc 		 */
86385fee539SLionel Sambuc 		if ((errno == ENOSPC) || (errno == EFBIG))
86485fee539SLionel Sambuc 			res = lstrval = 0;
86585fee539SLionel Sambuc #ifdef EDQUOT
86685fee539SLionel Sambuc 		if (errno == EDQUOT)
86785fee539SLionel Sambuc 			res = lstrval = 0;
86885fee539SLionel Sambuc #endif
86985fee539SLionel Sambuc 		break;
87085fee539SLionel Sambuc 	case ISTAPE:
87185fee539SLionel Sambuc 	case ISCHR:
87285fee539SLionel Sambuc 	case ISBLK:
87385fee539SLionel Sambuc #ifdef SUPPORT_RMT
87485fee539SLionel Sambuc 	case ISRMT:
87585fee539SLionel Sambuc #endif /* SUPPORT_RMT */
87685fee539SLionel Sambuc 		if (res >= 0)
87785fee539SLionel Sambuc 			break;
87885fee539SLionel Sambuc 		if (errno == EACCES) {
87985fee539SLionel Sambuc 			tty_warn(0,
88085fee539SLionel Sambuc 			    "Write failed, archive is write protected.");
88185fee539SLionel Sambuc 			res = lstrval = 0;
88285fee539SLionel Sambuc 			return 0;
88385fee539SLionel Sambuc 		}
88485fee539SLionel Sambuc 		/*
88585fee539SLionel Sambuc 		 * see if we reached the end of media, if so force a change to
88685fee539SLionel Sambuc 		 * the next volume
88785fee539SLionel Sambuc 		 */
88885fee539SLionel Sambuc 		if ((errno == ENOSPC) || (errno == EIO) || (errno == ENXIO))
88985fee539SLionel Sambuc 			res = lstrval = 0;
89085fee539SLionel Sambuc 		break;
89185fee539SLionel Sambuc 	case ISPIPE:
89285fee539SLionel Sambuc 	default:
89385fee539SLionel Sambuc 		/*
89485fee539SLionel Sambuc 		 * we cannot fix errors to these devices
89585fee539SLionel Sambuc 		 */
89685fee539SLionel Sambuc 		break;
89785fee539SLionel Sambuc 	}
89885fee539SLionel Sambuc 
89985fee539SLionel Sambuc 	/*
90085fee539SLionel Sambuc 	 * Better tell the user the bad news...
90185fee539SLionel Sambuc 	 * if this is a block aligned archive format, we may have a bad archive
90285fee539SLionel Sambuc 	 * if the format wants the header to start at a BLKMULT boundary. While
90385fee539SLionel Sambuc 	 * we can deal with the mis-aligned data, it violates spec and other
90485fee539SLionel Sambuc 	 * archive readers will likely fail. if the format is not block
90585fee539SLionel Sambuc 	 * aligned, the user may be lucky (and the archive is ok).
90685fee539SLionel Sambuc 	 */
90785fee539SLionel Sambuc 	if (res >= 0) {
90885fee539SLionel Sambuc 		if (res > 0)
90985fee539SLionel Sambuc 			wr_trail = 1;
91085fee539SLionel Sambuc 		io_ok = 1;
91185fee539SLionel Sambuc 	}
91285fee539SLionel Sambuc 
91385fee539SLionel Sambuc 	/*
91485fee539SLionel Sambuc 	 * If we were trying to rewrite the trailer and it didn't work, we
91585fee539SLionel Sambuc 	 * must quit right away.
91685fee539SLionel Sambuc 	 */
91785fee539SLionel Sambuc 	if (!wr_trail && (res <= 0)) {
91885fee539SLionel Sambuc 		tty_warn(1,
91985fee539SLionel Sambuc 		    "Unable to append, trailer re-write failed. Quitting.");
92085fee539SLionel Sambuc 		return res;
92185fee539SLionel Sambuc 	}
92285fee539SLionel Sambuc 
92385fee539SLionel Sambuc 	if (res == 0)
92485fee539SLionel Sambuc 		tty_warn(0, "End of archive volume %d reached", arvol);
92585fee539SLionel Sambuc 	else if (res < 0)
92685fee539SLionel Sambuc 		syswarn(1, errno, "Failed write to archive volume: %d", arvol);
92785fee539SLionel Sambuc 	else if (!frmt->blkalgn || ((res % frmt->blkalgn) == 0))
92885fee539SLionel Sambuc 		tty_warn(0,
92985fee539SLionel Sambuc 		    "WARNING: partial archive write. Archive MAY BE FLAWED");
93085fee539SLionel Sambuc 	else
93185fee539SLionel Sambuc 		tty_warn(1,"WARNING: partial archive write. Archive IS FLAWED");
93285fee539SLionel Sambuc 	return res;
93385fee539SLionel Sambuc }
93485fee539SLionel Sambuc 
93585fee539SLionel Sambuc /*
93685fee539SLionel Sambuc  * ar_rdsync()
93785fee539SLionel Sambuc  *	Try to move past a bad spot on a flawed archive as needed to continue
93885fee539SLionel Sambuc  *	I/O. Clears error flags to allow I/O to continue.
93985fee539SLionel Sambuc  * Return:
94085fee539SLionel Sambuc  *	0 when ok to try i/o again, -1 otherwise.
94185fee539SLionel Sambuc  */
94285fee539SLionel Sambuc 
94385fee539SLionel Sambuc int
ar_rdsync(void)94485fee539SLionel Sambuc ar_rdsync(void)
94585fee539SLionel Sambuc {
94685fee539SLionel Sambuc 	long fsbz;
94785fee539SLionel Sambuc 	off_t cpos;
94885fee539SLionel Sambuc 	off_t mpos;
94985fee539SLionel Sambuc #ifdef HAVE_SYS_MTIO_H
95085fee539SLionel Sambuc 	struct mtop mb;
95185fee539SLionel Sambuc #endif
95285fee539SLionel Sambuc 
95385fee539SLionel Sambuc 	/*
95485fee539SLionel Sambuc 	 * Fail resync attempts at user request (done) or if this is going to be
95585fee539SLionel Sambuc 	 * an update/append to a existing archive. if last i/o hit media end,
95685fee539SLionel Sambuc 	 * we need to go to the next volume not try a resync
95785fee539SLionel Sambuc 	 */
95885fee539SLionel Sambuc 	if ((done > 0) || (lstrval == 0))
95985fee539SLionel Sambuc 		return -1;
96085fee539SLionel Sambuc 
96185fee539SLionel Sambuc 	if ((act == APPND) || (act == ARCHIVE)) {
96285fee539SLionel Sambuc 		tty_warn(1, "Cannot allow updates to an archive with flaws.");
96385fee539SLionel Sambuc 		return -1;
96485fee539SLionel Sambuc 	}
96585fee539SLionel Sambuc 	if (io_ok)
96685fee539SLionel Sambuc 		did_io = 1;
96785fee539SLionel Sambuc 
96885fee539SLionel Sambuc 	switch(artyp) {
96985fee539SLionel Sambuc #ifdef SUPPORT_RMT
97085fee539SLionel Sambuc 	case ISRMT:
97185fee539SLionel Sambuc #endif /* SUPPORT_RMT */
97285fee539SLionel Sambuc 	case ISTAPE:
97385fee539SLionel Sambuc #ifdef HAVE_SYS_MTIO_H
97485fee539SLionel Sambuc 		/*
97585fee539SLionel Sambuc 		 * if the last i/o was a successful data transfer, we assume
97685fee539SLionel Sambuc 		 * the fault is just a bad record on the tape that we are now
97785fee539SLionel Sambuc 		 * past. If we did not get any data since the last resync try
97885fee539SLionel Sambuc 		 * to move the tape forward one PHYSICAL record past any
97985fee539SLionel Sambuc 		 * damaged tape section. Some tape drives are stubborn and need
98085fee539SLionel Sambuc 		 * to be pushed.
98185fee539SLionel Sambuc 		 */
98285fee539SLionel Sambuc 		if (io_ok) {
98385fee539SLionel Sambuc 			io_ok = 0;
98485fee539SLionel Sambuc 			lstrval = 1;
98585fee539SLionel Sambuc 			break;
98685fee539SLionel Sambuc 		}
98785fee539SLionel Sambuc 		mb.mt_op = MTFSR;
98885fee539SLionel Sambuc 		mb.mt_count = 1;
98985fee539SLionel Sambuc #ifdef SUPPORT_RMT
99085fee539SLionel Sambuc 		if (artyp == ISRMT) {
99185fee539SLionel Sambuc 			if (rmtioctl(arfd, MTIOCTOP, &mb) < 0)
99285fee539SLionel Sambuc 				break;
99385fee539SLionel Sambuc 		} else {
99485fee539SLionel Sambuc #endif /* SUPPORT_RMT */
99585fee539SLionel Sambuc 			if (ioctl(arfd, MTIOCTOP, &mb) < 0)
99685fee539SLionel Sambuc 				break;
99785fee539SLionel Sambuc #ifdef SUPPORT_RMT
99885fee539SLionel Sambuc 		}
99985fee539SLionel Sambuc #endif /* SUPPORT_RMT */
100085fee539SLionel Sambuc 		lstrval = 1;
100185fee539SLionel Sambuc #else
100285fee539SLionel Sambuc 		tty_warn(1, "System does not have tape support");
100385fee539SLionel Sambuc #endif
100485fee539SLionel Sambuc 		break;
100585fee539SLionel Sambuc 	case ISREG:
100685fee539SLionel Sambuc 	case ISCHR:
100785fee539SLionel Sambuc 	case ISBLK:
100885fee539SLionel Sambuc 		/*
100985fee539SLionel Sambuc 		 * try to step over the bad part of the device.
101085fee539SLionel Sambuc 		 */
101185fee539SLionel Sambuc 		io_ok = 0;
101285fee539SLionel Sambuc 		if (((fsbz = arsb.st_blksize) <= 0) || (artyp != ISREG))
101385fee539SLionel Sambuc 			fsbz = BLKMULT;
101485fee539SLionel Sambuc 		if ((cpos = lseek(arfd, (off_t)0L, SEEK_CUR)) < 0)
101585fee539SLionel Sambuc 			break;
101685fee539SLionel Sambuc 		mpos = fsbz - (cpos % (off_t)fsbz);
101785fee539SLionel Sambuc 		if (lseek(arfd, mpos, SEEK_CUR) < 0)
101885fee539SLionel Sambuc 			break;
101985fee539SLionel Sambuc 		lstrval = 1;
102085fee539SLionel Sambuc 		break;
102185fee539SLionel Sambuc 	case ISPIPE:
102285fee539SLionel Sambuc 	default:
102385fee539SLionel Sambuc 		/*
102485fee539SLionel Sambuc 		 * cannot recover on these archive device types
102585fee539SLionel Sambuc 		 */
102685fee539SLionel Sambuc 		io_ok = 0;
102785fee539SLionel Sambuc 		break;
102885fee539SLionel Sambuc 	}
102985fee539SLionel Sambuc 	if (lstrval <= 0) {
103085fee539SLionel Sambuc 		tty_warn(1, "Unable to recover from an archive read failure.");
103185fee539SLionel Sambuc 		return -1;
103285fee539SLionel Sambuc 	}
103385fee539SLionel Sambuc 	tty_warn(0, "Attempting to recover from an archive read failure.");
103485fee539SLionel Sambuc 	return 0;
103585fee539SLionel Sambuc }
103685fee539SLionel Sambuc 
103785fee539SLionel Sambuc /*
103885fee539SLionel Sambuc  * ar_fow()
103985fee539SLionel Sambuc  *	Move the I/O position within the archive forward the specified number of
104085fee539SLionel Sambuc  *	bytes as supported by the device. If we cannot move the requested
104185fee539SLionel Sambuc  *	number of bytes, return the actual number of bytes moved in skipped.
104285fee539SLionel Sambuc  * Return:
104385fee539SLionel Sambuc  *	0 if moved the requested distance, -1 on complete failure, 1 on
104485fee539SLionel Sambuc  *	partial move (the amount moved is in skipped)
104585fee539SLionel Sambuc  */
104685fee539SLionel Sambuc 
104785fee539SLionel Sambuc int
ar_fow(off_t sksz,off_t * skipped)104885fee539SLionel Sambuc ar_fow(off_t sksz, off_t *skipped)
104985fee539SLionel Sambuc {
105085fee539SLionel Sambuc 	off_t cpos;
105185fee539SLionel Sambuc 	off_t mpos;
105285fee539SLionel Sambuc 
105385fee539SLionel Sambuc 	*skipped = 0;
105485fee539SLionel Sambuc 	if (sksz <= 0)
105585fee539SLionel Sambuc 		return 0;
105685fee539SLionel Sambuc 
105785fee539SLionel Sambuc 	/*
105885fee539SLionel Sambuc 	 * we cannot move forward at EOF or error
105985fee539SLionel Sambuc 	 */
106085fee539SLionel Sambuc 	if (lstrval <= 0)
106185fee539SLionel Sambuc 		return lstrval;
106285fee539SLionel Sambuc 
106385fee539SLionel Sambuc 	/*
106485fee539SLionel Sambuc 	 * Safer to read forward on devices where it is hard to find the end of
106585fee539SLionel Sambuc 	 * the media without reading to it. With tapes we cannot be sure of the
106685fee539SLionel Sambuc 	 * number of physical blocks to skip (we do not know physical block
106785fee539SLionel Sambuc 	 * size at this point), so we must only read forward on tapes!
106885fee539SLionel Sambuc 	 */
106985fee539SLionel Sambuc 	if (artyp == ISTAPE || artyp == ISPIPE
107085fee539SLionel Sambuc #ifdef SUPPORT_RMT
107185fee539SLionel Sambuc 	    || artyp == ISRMT
107285fee539SLionel Sambuc #endif /* SUPPORT_RMT */
107385fee539SLionel Sambuc 	    )
107485fee539SLionel Sambuc 		return 0;
107585fee539SLionel Sambuc 
107685fee539SLionel Sambuc 	/*
107785fee539SLionel Sambuc 	 * figure out where we are in the archive
107885fee539SLionel Sambuc 	 */
107985fee539SLionel Sambuc 	if ((cpos = lseek(arfd, (off_t)0L, SEEK_CUR)) >= 0) {
108085fee539SLionel Sambuc 		/*
108185fee539SLionel Sambuc 		 * we can be asked to move farther than there are bytes in this
108285fee539SLionel Sambuc 		 * volume, if so, just go to file end and let normal buf_fill()
108385fee539SLionel Sambuc 		 * deal with the end of file (it will go to next volume by
108485fee539SLionel Sambuc 		 * itself)
108585fee539SLionel Sambuc 		 */
108685fee539SLionel Sambuc 		mpos = cpos + sksz;
108785fee539SLionel Sambuc 		if (artyp == ISREG && mpos > arsb.st_size)
108885fee539SLionel Sambuc 			mpos = arsb.st_size;
108985fee539SLionel Sambuc 		if ((mpos = lseek(arfd, mpos, SEEK_SET)) >= 0) {
109085fee539SLionel Sambuc 			*skipped = mpos - cpos;
109185fee539SLionel Sambuc 			return 0;
109285fee539SLionel Sambuc 		}
109385fee539SLionel Sambuc 	} else {
109485fee539SLionel Sambuc 		if (artyp != ISREG)
109585fee539SLionel Sambuc 			return 0;		/* non-seekable device */
109685fee539SLionel Sambuc 	}
109785fee539SLionel Sambuc 	syswarn(1, errno, "Forward positioning operation on archive failed");
109885fee539SLionel Sambuc 	lstrval = -1;
109985fee539SLionel Sambuc 	return -1;
110085fee539SLionel Sambuc }
110185fee539SLionel Sambuc 
110285fee539SLionel Sambuc /*
110385fee539SLionel Sambuc  * ar_rev()
110485fee539SLionel Sambuc  *	move the i/o position within the archive backwards the specified byte
110585fee539SLionel Sambuc  *	count as supported by the device. With tapes drives we RESET rdblksz to
110685fee539SLionel Sambuc  *	the PHYSICAL blocksize.
110785fee539SLionel Sambuc  *	NOTE: We should only be called to move backwards so we can rewrite the
110885fee539SLionel Sambuc  *	last records (the trailer) of an archive (APPEND).
110985fee539SLionel Sambuc  * Return:
111085fee539SLionel Sambuc  *	0 if moved the requested distance, -1 on complete failure
111185fee539SLionel Sambuc  */
111285fee539SLionel Sambuc 
111385fee539SLionel Sambuc int
ar_rev(off_t sksz)111485fee539SLionel Sambuc ar_rev(off_t sksz)
111585fee539SLionel Sambuc {
111685fee539SLionel Sambuc 	off_t cpos;
111785fee539SLionel Sambuc #ifdef HAVE_SYS_MTIO_H
111885fee539SLionel Sambuc 	int phyblk;
111985fee539SLionel Sambuc 	struct mtop mb;
112085fee539SLionel Sambuc #endif
112185fee539SLionel Sambuc 
112285fee539SLionel Sambuc 	/*
112385fee539SLionel Sambuc 	 * make sure we do not have try to reverse on a flawed archive
112485fee539SLionel Sambuc 	 */
112585fee539SLionel Sambuc 	if (lstrval < 0)
112685fee539SLionel Sambuc 		return lstrval;
112785fee539SLionel Sambuc 
112885fee539SLionel Sambuc 	switch(artyp) {
112985fee539SLionel Sambuc 	case ISPIPE:
113085fee539SLionel Sambuc 		if (sksz <= 0)
113185fee539SLionel Sambuc 			break;
113285fee539SLionel Sambuc 		/*
113385fee539SLionel Sambuc 		 * cannot go backwards on these critters
113485fee539SLionel Sambuc 		 */
113585fee539SLionel Sambuc 		tty_warn(1, "Reverse positioning on pipes is not supported.");
113685fee539SLionel Sambuc 		lstrval = -1;
113785fee539SLionel Sambuc 		return -1;
113885fee539SLionel Sambuc 	case ISREG:
113985fee539SLionel Sambuc 	case ISBLK:
114085fee539SLionel Sambuc 	case ISCHR:
114185fee539SLionel Sambuc 	default:
114285fee539SLionel Sambuc 		if (sksz <= 0)
114385fee539SLionel Sambuc 			break;
114485fee539SLionel Sambuc 
114585fee539SLionel Sambuc 		/*
114685fee539SLionel Sambuc 		 * For things other than files, backwards movement has a very
114785fee539SLionel Sambuc 		 * high probability of failure as we really do not know the
114885fee539SLionel Sambuc 		 * true attributes of the device we are talking to (the device
114985fee539SLionel Sambuc 		 * may not even have the ability to lseek() in any direction).
115085fee539SLionel Sambuc 		 * First we figure out where we are in the archive.
115185fee539SLionel Sambuc 		 */
115285fee539SLionel Sambuc 		if ((cpos = lseek(arfd, (off_t)0L, SEEK_CUR)) < 0) {
115385fee539SLionel Sambuc 			syswarn(1, errno,
115485fee539SLionel Sambuc 			   "Unable to obtain current archive byte offset");
115585fee539SLionel Sambuc 			lstrval = -1;
115685fee539SLionel Sambuc 			return -1;
115785fee539SLionel Sambuc 		}
115885fee539SLionel Sambuc 
115985fee539SLionel Sambuc 		/*
116085fee539SLionel Sambuc 		 * we may try to go backwards past the start when the archive
116185fee539SLionel Sambuc 		 * is only a single record. If this happens and we are on a
116285fee539SLionel Sambuc 		 * multi-volume archive, we need to go to the end of the
116385fee539SLionel Sambuc 		 * previous volume and continue our movement backwards from
116485fee539SLionel Sambuc 		 * there.
116585fee539SLionel Sambuc 		 */
116685fee539SLionel Sambuc 		if ((cpos -= sksz) < (off_t)0L) {
116785fee539SLionel Sambuc 			if (arvol > 1) {
116885fee539SLionel Sambuc 				/*
116985fee539SLionel Sambuc 				 * this should never happen
117085fee539SLionel Sambuc 				 */
117185fee539SLionel Sambuc 				tty_warn(1,
117285fee539SLionel Sambuc 				    "Reverse position on previous volume.");
117385fee539SLionel Sambuc 				lstrval = -1;
117485fee539SLionel Sambuc 				return -1;
117585fee539SLionel Sambuc 			}
117685fee539SLionel Sambuc 			cpos = (off_t)0L;
117785fee539SLionel Sambuc 		}
117885fee539SLionel Sambuc 		if (lseek(arfd, cpos, SEEK_SET) < 0) {
117985fee539SLionel Sambuc 			syswarn(1, errno, "Unable to seek archive backwards");
118085fee539SLionel Sambuc 			lstrval = -1;
118185fee539SLionel Sambuc 			return -1;
118285fee539SLionel Sambuc 		}
118385fee539SLionel Sambuc 		break;
118485fee539SLionel Sambuc 	case ISTAPE:
118585fee539SLionel Sambuc #ifdef SUPPORT_RMT
118685fee539SLionel Sambuc 	case ISRMT:
118785fee539SLionel Sambuc #endif /* SUPPORT_RMT */
118885fee539SLionel Sambuc #ifdef HAVE_SYS_MTIO_H
118985fee539SLionel Sambuc 		/*
119085fee539SLionel Sambuc 		 * Calculate and move the proper number of PHYSICAL tape
119185fee539SLionel Sambuc 		 * blocks. If the sksz is not an even multiple of the physical
119285fee539SLionel Sambuc 		 * tape size, we cannot do the move (this should never happen).
119385fee539SLionel Sambuc 		 * (We also cannot handle trailers spread over two vols).
119485fee539SLionel Sambuc 		 * get_phys() also makes sure we are in front of the filemark.
119585fee539SLionel Sambuc 		 */
119685fee539SLionel Sambuc 		if ((phyblk = get_phys()) <= 0) {
119785fee539SLionel Sambuc 			lstrval = -1;
119885fee539SLionel Sambuc 			return -1;
119985fee539SLionel Sambuc 		}
120085fee539SLionel Sambuc 
120185fee539SLionel Sambuc 		/*
120285fee539SLionel Sambuc 		 * make sure future tape reads only go by physical tape block
120385fee539SLionel Sambuc 		 * size (set rdblksz to the real size).
120485fee539SLionel Sambuc 		 */
120585fee539SLionel Sambuc 		rdblksz = phyblk;
120685fee539SLionel Sambuc 
120785fee539SLionel Sambuc 		/*
120885fee539SLionel Sambuc 		 * if no movement is required, just return (we must be after
120985fee539SLionel Sambuc 		 * get_phys() so the physical blocksize is properly set)
121085fee539SLionel Sambuc 		 */
121185fee539SLionel Sambuc 		if (sksz <= 0)
121285fee539SLionel Sambuc 			break;
121385fee539SLionel Sambuc 
121485fee539SLionel Sambuc 		/*
121585fee539SLionel Sambuc 		 * ok we have to move. Make sure the tape drive can do it.
121685fee539SLionel Sambuc 		 */
121785fee539SLionel Sambuc 		if (sksz % phyblk) {
121885fee539SLionel Sambuc 			tty_warn(1,
121985fee539SLionel Sambuc 			    "Tape drive unable to backspace requested amount");
122085fee539SLionel Sambuc 			lstrval = -1;
122185fee539SLionel Sambuc 			return -1;
122285fee539SLionel Sambuc 		}
122385fee539SLionel Sambuc 
122485fee539SLionel Sambuc 		/*
122585fee539SLionel Sambuc 		 * move backwards the requested number of bytes
122685fee539SLionel Sambuc 		 */
122785fee539SLionel Sambuc 		mb.mt_op = MTBSR;
122885fee539SLionel Sambuc 		mb.mt_count = sksz/phyblk;
122985fee539SLionel Sambuc 		if (
123085fee539SLionel Sambuc #ifdef SUPPORT_RMT
123185fee539SLionel Sambuc 		    rmtioctl(arfd, MTIOCTOP, &mb)
123285fee539SLionel Sambuc #else
123385fee539SLionel Sambuc 		    ioctl(arfd, MTIOCTOP, &mb)
123485fee539SLionel Sambuc #endif /* SUPPORT_RMT */
123585fee539SLionel Sambuc 		    < 0) {
123685fee539SLionel Sambuc 			syswarn(1, errno, "Unable to backspace tape %ld blocks.",
123785fee539SLionel Sambuc 			    (long) mb.mt_count);
123885fee539SLionel Sambuc 			lstrval = -1;
123985fee539SLionel Sambuc 			return -1;
124085fee539SLionel Sambuc 		}
124185fee539SLionel Sambuc #else
124285fee539SLionel Sambuc 		tty_warn(1, "System does not have tape support");
124385fee539SLionel Sambuc #endif
124485fee539SLionel Sambuc 		break;
124585fee539SLionel Sambuc 	}
124685fee539SLionel Sambuc 	lstrval = 1;
124785fee539SLionel Sambuc 	return 0;
124885fee539SLionel Sambuc }
124985fee539SLionel Sambuc 
125085fee539SLionel Sambuc #ifdef HAVE_SYS_MTIO_H
125185fee539SLionel Sambuc /*
125285fee539SLionel Sambuc  * get_phys()
125385fee539SLionel Sambuc  *	Determine the physical block size on a tape drive. We need the physical
125485fee539SLionel Sambuc  *	block size so we know how many bytes we skip over when we move with
125585fee539SLionel Sambuc  *	mtio commands. We also make sure we are BEFORE THE TAPE FILEMARK when
125685fee539SLionel Sambuc  *	return.
125785fee539SLionel Sambuc  *	This is one really SLOW routine...
125885fee539SLionel Sambuc  * Return:
125985fee539SLionel Sambuc  *	physical block size if ok (ok > 0), -1 otherwise
126085fee539SLionel Sambuc  */
126185fee539SLionel Sambuc 
126285fee539SLionel Sambuc static int
get_phys(void)126385fee539SLionel Sambuc get_phys(void)
126485fee539SLionel Sambuc {
126585fee539SLionel Sambuc 	int padsz = 0;
126685fee539SLionel Sambuc 	int res;
126785fee539SLionel Sambuc 	int phyblk;
126885fee539SLionel Sambuc 	struct mtop mb;
126985fee539SLionel Sambuc 	char scbuf[MAXBLK];
127085fee539SLionel Sambuc 
127185fee539SLionel Sambuc 	/*
127285fee539SLionel Sambuc 	 * move to the file mark, and then back up one record and read it.
127385fee539SLionel Sambuc 	 * this should tell us the physical record size the tape is using.
127485fee539SLionel Sambuc 	 */
127585fee539SLionel Sambuc 	if (lstrval == 1) {
127685fee539SLionel Sambuc 		/*
127785fee539SLionel Sambuc 		 * we know we are at file mark when we get back a 0 from
127885fee539SLionel Sambuc 		 * read()
127985fee539SLionel Sambuc 		 */
128085fee539SLionel Sambuc #ifdef SUPPORT_RMT
128185fee539SLionel Sambuc 		while ((res = rmtread_with_restart(arfd,
128285fee539SLionel Sambuc 						   scbuf, sizeof(scbuf))) > 0)
128385fee539SLionel Sambuc #else
128485fee539SLionel Sambuc 		while ((res = read_with_restart(arfd,
128585fee539SLionel Sambuc 						scbuf, sizeof(scbuf))) > 0)
128685fee539SLionel Sambuc #endif /* SUPPORT_RMT */
128785fee539SLionel Sambuc 			padsz += res;
128885fee539SLionel Sambuc 		if (res < 0) {
128985fee539SLionel Sambuc 			syswarn(1, errno, "Unable to locate tape filemark.");
129085fee539SLionel Sambuc 			return -1;
129185fee539SLionel Sambuc 		}
129285fee539SLionel Sambuc 	}
129385fee539SLionel Sambuc 
129485fee539SLionel Sambuc 	/*
129585fee539SLionel Sambuc 	 * move backwards over the file mark so we are at the end of the
129685fee539SLionel Sambuc 	 * last record.
129785fee539SLionel Sambuc 	 */
129885fee539SLionel Sambuc 	mb.mt_op = MTBSF;
129985fee539SLionel Sambuc 	mb.mt_count = 1;
130085fee539SLionel Sambuc 	if (
130185fee539SLionel Sambuc #ifdef SUPPORT_RMT
130285fee539SLionel Sambuc 	    rmtioctl(arfd, MTIOCTOP, &mb)
130385fee539SLionel Sambuc #else
130485fee539SLionel Sambuc 	    ioctl(arfd, MTIOCTOP, &mb)
130585fee539SLionel Sambuc #endif /* SUPPORT_RMT */
130685fee539SLionel Sambuc 	    < 0) {
130785fee539SLionel Sambuc 		syswarn(1, errno, "Unable to backspace over tape filemark.");
130885fee539SLionel Sambuc 		return -1;
130985fee539SLionel Sambuc 	}
131085fee539SLionel Sambuc 
131185fee539SLionel Sambuc 	/*
131285fee539SLionel Sambuc 	 * move backwards so we are in front of the last record and read it to
131385fee539SLionel Sambuc 	 * get physical tape blocksize.
131485fee539SLionel Sambuc 	 */
131585fee539SLionel Sambuc 	mb.mt_op = MTBSR;
131685fee539SLionel Sambuc 	mb.mt_count = 1;
131785fee539SLionel Sambuc 	if (
131885fee539SLionel Sambuc #ifdef SUPPORT_RMT
131985fee539SLionel Sambuc 	    rmtioctl(arfd, MTIOCTOP, &mb)
132085fee539SLionel Sambuc #else
132185fee539SLionel Sambuc 	    ioctl(arfd, MTIOCTOP, &mb)
132285fee539SLionel Sambuc #endif /* SUPPORT_RMT */
132385fee539SLionel Sambuc 	    < 0) {
132485fee539SLionel Sambuc 		syswarn(1, errno, "Unable to backspace over last tape block.");
132585fee539SLionel Sambuc 		return -1;
132685fee539SLionel Sambuc 	}
132785fee539SLionel Sambuc 	if ((phyblk =
132885fee539SLionel Sambuc #ifdef SUPPORT_RMT
132985fee539SLionel Sambuc 	     rmtread_with_restart(arfd, scbuf, sizeof(scbuf))
133085fee539SLionel Sambuc #else
133185fee539SLionel Sambuc 	     read_with_restart(arfd, scbuf, sizeof(scbuf))
133285fee539SLionel Sambuc #endif /* SUPPORT_RMT */
133385fee539SLionel Sambuc 	    ) <= 0) {
133485fee539SLionel Sambuc 		syswarn(1, errno, "Cannot determine archive tape blocksize.");
133585fee539SLionel Sambuc 		return -1;
133685fee539SLionel Sambuc 	}
133785fee539SLionel Sambuc 
133885fee539SLionel Sambuc 	/*
133985fee539SLionel Sambuc 	 * read forward to the file mark, then back up in front of the filemark
134085fee539SLionel Sambuc 	 * (this is a bit paranoid, but should be safe to do).
134185fee539SLionel Sambuc 	 */
134285fee539SLionel Sambuc 	while ((res =
134385fee539SLionel Sambuc #ifdef SUPPORT_RMT
134485fee539SLionel Sambuc 		rmtread_with_restart(arfd, scbuf, sizeof(scbuf))
134585fee539SLionel Sambuc #else
134685fee539SLionel Sambuc 		read_with_restart(arfd, scbuf, sizeof(scbuf))
134785fee539SLionel Sambuc #endif /* SUPPORT_RMT */
134885fee539SLionel Sambuc 	       ) > 0)
134985fee539SLionel Sambuc 		;
135085fee539SLionel Sambuc 	if (res < 0) {
135185fee539SLionel Sambuc 		syswarn(1, errno, "Unable to locate tape filemark.");
135285fee539SLionel Sambuc 		return -1;
135385fee539SLionel Sambuc 	}
135485fee539SLionel Sambuc 	mb.mt_op = MTBSF;
135585fee539SLionel Sambuc 	mb.mt_count = 1;
135685fee539SLionel Sambuc 	if (
135785fee539SLionel Sambuc #ifdef SUPPORT_RMT
135885fee539SLionel Sambuc 	    rmtioctl(arfd, MTIOCTOP, &mb)
135985fee539SLionel Sambuc #else
136085fee539SLionel Sambuc 	    ioctl(arfd, MTIOCTOP, &mb)
136185fee539SLionel Sambuc #endif /* SUPPORT_RMT */
136285fee539SLionel Sambuc 	    < 0) {
136385fee539SLionel Sambuc 		syswarn(1, errno, "Unable to backspace over tape filemark.");
136485fee539SLionel Sambuc 		return -1;
136585fee539SLionel Sambuc 	}
136685fee539SLionel Sambuc 
136785fee539SLionel Sambuc 	/*
136885fee539SLionel Sambuc 	 * set lstrval so we know that the filemark has not been seen
136985fee539SLionel Sambuc 	 */
137085fee539SLionel Sambuc 	lstrval = 1;
137185fee539SLionel Sambuc 
137285fee539SLionel Sambuc 	/*
137385fee539SLionel Sambuc 	 * return if there was no padding
137485fee539SLionel Sambuc 	 */
137585fee539SLionel Sambuc 	if (padsz == 0)
137685fee539SLionel Sambuc 		return phyblk;
137785fee539SLionel Sambuc 
137885fee539SLionel Sambuc 	/*
137985fee539SLionel Sambuc 	 * make sure we can move backwards over the padding. (this should
138085fee539SLionel Sambuc 	 * never fail).
138185fee539SLionel Sambuc 	 */
138285fee539SLionel Sambuc 	if (padsz % phyblk) {
138385fee539SLionel Sambuc 		tty_warn(1, "Tape drive unable to backspace requested amount");
138485fee539SLionel Sambuc 		return -1;
138585fee539SLionel Sambuc 	}
138685fee539SLionel Sambuc 
138785fee539SLionel Sambuc 	/*
138885fee539SLionel Sambuc 	 * move backwards over the padding so the head is where it was when
138985fee539SLionel Sambuc 	 * we were first called (if required).
139085fee539SLionel Sambuc 	 */
139185fee539SLionel Sambuc 	mb.mt_op = MTBSR;
139285fee539SLionel Sambuc 	mb.mt_count = padsz/phyblk;
139385fee539SLionel Sambuc 	if (
139485fee539SLionel Sambuc #ifdef SUPPORT_RMT
139585fee539SLionel Sambuc 	    rmtioctl(arfd, MTIOCTOP, &mb)
139685fee539SLionel Sambuc #else
139785fee539SLionel Sambuc 	    ioctl(arfd, MTIOCTOP, &mb)
139885fee539SLionel Sambuc #endif /* SUPPORT_RMT */
139985fee539SLionel Sambuc 	    < 0) {
140085fee539SLionel Sambuc 		syswarn(1, errno,
140185fee539SLionel Sambuc 		    "Unable to backspace tape over %ld pad blocks",
140285fee539SLionel Sambuc 		    (long)mb.mt_count);
140385fee539SLionel Sambuc 		return -1;
140485fee539SLionel Sambuc 	}
140585fee539SLionel Sambuc 	return phyblk;
140685fee539SLionel Sambuc }
140785fee539SLionel Sambuc #endif
140885fee539SLionel Sambuc 
140985fee539SLionel Sambuc /*
141085fee539SLionel Sambuc  * ar_next()
141185fee539SLionel Sambuc  *	prompts the user for the next volume in this archive. For some devices
141285fee539SLionel Sambuc  *	we may allow the media to be changed. Otherwise a new archive is
141385fee539SLionel Sambuc  *	prompted for. By pax spec, if there is no controlling tty or an eof is
141485fee539SLionel Sambuc  *	read on tty input, we must quit pax.
141585fee539SLionel Sambuc  * Return:
141685fee539SLionel Sambuc  *	0 when ready to continue, -1 when all done
141785fee539SLionel Sambuc  */
141885fee539SLionel Sambuc 
141985fee539SLionel Sambuc int
ar_next(void)142085fee539SLionel Sambuc ar_next(void)
142185fee539SLionel Sambuc {
142285fee539SLionel Sambuc 	char buf[PAXPATHLEN+2];
142385fee539SLionel Sambuc 	static char *arcfree = NULL;
142485fee539SLionel Sambuc 	sigset_t o_mask;
142585fee539SLionel Sambuc 
142685fee539SLionel Sambuc 	/*
142785fee539SLionel Sambuc 	 * WE MUST CLOSE THE DEVICE. A lot of devices must see last close, (so
142885fee539SLionel Sambuc 	 * things like writing EOF etc will be done) (Watch out ar_close() can
142985fee539SLionel Sambuc 	 * also be called via a signal handler, so we must prevent a race.
143085fee539SLionel Sambuc 	 */
143185fee539SLionel Sambuc 	if (sigprocmask(SIG_BLOCK, &s_mask, &o_mask) < 0)
143285fee539SLionel Sambuc 		syswarn(0, errno, "Unable to set signal mask");
143385fee539SLionel Sambuc 	ar_close();
143485fee539SLionel Sambuc 	if (sigprocmask(SIG_SETMASK, &o_mask, NULL) < 0)
143585fee539SLionel Sambuc 		syswarn(0, errno, "Unable to restore signal mask");
143685fee539SLionel Sambuc 
143785fee539SLionel Sambuc 	if (done || !wr_trail || force_one_volume)
143885fee539SLionel Sambuc 		return -1;
143985fee539SLionel Sambuc 
144085fee539SLionel Sambuc 	if (!is_gnutar)
144185fee539SLionel Sambuc 		tty_prnt("\nATTENTION! %s archive volume change required.\n",
144285fee539SLionel Sambuc 		    argv0);
144385fee539SLionel Sambuc 
144485fee539SLionel Sambuc 	/*
144585fee539SLionel Sambuc 	 * if i/o is on stdin or stdout, we cannot reopen it (we do not know
144685fee539SLionel Sambuc 	 * the name), the user will be forced to type it in.
144785fee539SLionel Sambuc 	 */
144885fee539SLionel Sambuc 	if (strcmp(arcname, STDO) && strcmp(arcname, STDN) && (artyp != ISREG)
144985fee539SLionel Sambuc 	    && (artyp != ISPIPE)) {
145085fee539SLionel Sambuc 		if (artyp == ISTAPE
145185fee539SLionel Sambuc #ifdef SUPPORT_RMT
145285fee539SLionel Sambuc 		    || artyp == ISRMT
145385fee539SLionel Sambuc #endif /* SUPPORT_RMT */
145485fee539SLionel Sambuc 		    ) {
145585fee539SLionel Sambuc 			tty_prnt("%s ready for archive tape volume: %d\n",
145685fee539SLionel Sambuc 				arcname, arvol);
145785fee539SLionel Sambuc 			tty_prnt("Load the NEXT TAPE on the tape drive");
145885fee539SLionel Sambuc 		} else {
145985fee539SLionel Sambuc 			tty_prnt("%s ready for archive volume: %d\n",
146085fee539SLionel Sambuc 				arcname, arvol);
146185fee539SLionel Sambuc 			tty_prnt("Load the NEXT STORAGE MEDIA (if required)");
146285fee539SLionel Sambuc 		}
146385fee539SLionel Sambuc 
146485fee539SLionel Sambuc 		if ((act == ARCHIVE) || (act == APPND))
146585fee539SLionel Sambuc 			tty_prnt(" and make sure it is WRITE ENABLED.\n");
146685fee539SLionel Sambuc 		else
146785fee539SLionel Sambuc 			tty_prnt("\n");
146885fee539SLionel Sambuc 
146985fee539SLionel Sambuc 		for(;;) {
147085fee539SLionel Sambuc 			tty_prnt("Type \"y\" to continue, \".\" to quit %s,",
147185fee539SLionel Sambuc 				argv0);
147285fee539SLionel Sambuc 			tty_prnt(" or \"s\" to switch to new device.\nIf you");
147385fee539SLionel Sambuc 			tty_prnt(" cannot change storage media, type \"s\"\n");
147485fee539SLionel Sambuc 			tty_prnt("Is the device ready and online? > ");
147585fee539SLionel Sambuc 
147685fee539SLionel Sambuc 			if ((tty_read(buf,sizeof(buf))<0) || !strcmp(buf,".")){
147785fee539SLionel Sambuc 				done = 1;
147885fee539SLionel Sambuc 				lstrval = -1;
147985fee539SLionel Sambuc 				tty_prnt("Quitting %s!\n", argv0);
148085fee539SLionel Sambuc 				vfpart = 0;
148185fee539SLionel Sambuc 				return -1;
148285fee539SLionel Sambuc 			}
148385fee539SLionel Sambuc 
148485fee539SLionel Sambuc 			if ((buf[0] == '\0') || (buf[1] != '\0')) {
148585fee539SLionel Sambuc 				tty_prnt("%s unknown command, try again\n",buf);
148685fee539SLionel Sambuc 				continue;
148785fee539SLionel Sambuc 			}
148885fee539SLionel Sambuc 
148985fee539SLionel Sambuc 			switch (buf[0]) {
149085fee539SLionel Sambuc 			case 'y':
149185fee539SLionel Sambuc 			case 'Y':
149285fee539SLionel Sambuc 				/*
149385fee539SLionel Sambuc 				 * we are to continue with the same device
149485fee539SLionel Sambuc 				 */
149585fee539SLionel Sambuc 				if (ar_open(arcname) >= 0)
149685fee539SLionel Sambuc 					return 0;
149785fee539SLionel Sambuc 				tty_prnt("Cannot re-open %s, try again\n",
149885fee539SLionel Sambuc 					arcname);
149985fee539SLionel Sambuc 				continue;
150085fee539SLionel Sambuc 			case 's':
150185fee539SLionel Sambuc 			case 'S':
150285fee539SLionel Sambuc 				/*
150385fee539SLionel Sambuc 				 * user wants to open a different device
150485fee539SLionel Sambuc 				 */
150585fee539SLionel Sambuc 				tty_prnt("Switching to a different archive\n");
150685fee539SLionel Sambuc 				break;
150785fee539SLionel Sambuc 			default:
150885fee539SLionel Sambuc 				tty_prnt("%s unknown command, try again\n",buf);
150985fee539SLionel Sambuc 				continue;
151085fee539SLionel Sambuc 			}
151185fee539SLionel Sambuc 			break;
151285fee539SLionel Sambuc 		}
151385fee539SLionel Sambuc 	} else {
151485fee539SLionel Sambuc 		if (is_gnutar) {
151585fee539SLionel Sambuc 			tty_warn(1, "Unexpected EOF on archive file");
151685fee539SLionel Sambuc 			return -1;
151785fee539SLionel Sambuc 		}
151885fee539SLionel Sambuc 		tty_prnt("Ready for archive volume: %d\n", arvol);
151985fee539SLionel Sambuc 	}
152085fee539SLionel Sambuc 
152185fee539SLionel Sambuc 	/*
152285fee539SLionel Sambuc 	 * have to go to a different archive
152385fee539SLionel Sambuc 	 */
152485fee539SLionel Sambuc 	for (;;) {
152585fee539SLionel Sambuc 		tty_prnt("Input archive name or \".\" to quit %s.\n", argv0);
152685fee539SLionel Sambuc 		tty_prnt("Archive name > ");
152785fee539SLionel Sambuc 
152885fee539SLionel Sambuc 		if ((tty_read(buf, sizeof(buf)) < 0) || !strcmp(buf, ".")) {
152985fee539SLionel Sambuc 			done = 1;
153085fee539SLionel Sambuc 			lstrval = -1;
153185fee539SLionel Sambuc 			tty_prnt("Quitting %s!\n", argv0);
153285fee539SLionel Sambuc 			vfpart = 0;
153385fee539SLionel Sambuc 			return -1;
153485fee539SLionel Sambuc 		}
153585fee539SLionel Sambuc 		if (buf[0] == '\0') {
153685fee539SLionel Sambuc 			tty_prnt("Empty file name, try again\n");
153785fee539SLionel Sambuc 			continue;
153885fee539SLionel Sambuc 		}
153985fee539SLionel Sambuc 		if (!strcmp(buf, "..")) {
154085fee539SLionel Sambuc 			tty_prnt("Illegal file name: .. try again\n");
154185fee539SLionel Sambuc 			continue;
154285fee539SLionel Sambuc 		}
154385fee539SLionel Sambuc 		if (strlen(buf) > PAXPATHLEN) {
154485fee539SLionel Sambuc 			tty_prnt("File name too long, try again\n");
154585fee539SLionel Sambuc 			continue;
154685fee539SLionel Sambuc 		}
154785fee539SLionel Sambuc 
154885fee539SLionel Sambuc 		/*
154985fee539SLionel Sambuc 		 * try to open new archive
155085fee539SLionel Sambuc 		 */
155185fee539SLionel Sambuc 		if (ar_open(buf) >= 0) {
155285fee539SLionel Sambuc 			if (arcfree) {
155385fee539SLionel Sambuc 				(void)free(arcfree);
155485fee539SLionel Sambuc 				arcfree = NULL;
155585fee539SLionel Sambuc 			}
155685fee539SLionel Sambuc 			if ((arcfree = strdup(buf)) == NULL) {
155785fee539SLionel Sambuc 				done = 1;
155885fee539SLionel Sambuc 				lstrval = -1;
155985fee539SLionel Sambuc 				tty_warn(0, "Cannot save archive name.");
156085fee539SLionel Sambuc 				return -1;
156185fee539SLionel Sambuc 			}
156285fee539SLionel Sambuc 			arcname = arcfree;
156385fee539SLionel Sambuc 			break;
156485fee539SLionel Sambuc 		}
156585fee539SLionel Sambuc 		tty_prnt("Cannot open %s, try again\n", buf);
156685fee539SLionel Sambuc 		continue;
156785fee539SLionel Sambuc 	}
156885fee539SLionel Sambuc 	return 0;
156985fee539SLionel Sambuc }
157085fee539SLionel Sambuc 
157185fee539SLionel Sambuc /*
157285fee539SLionel Sambuc  * ar_start_gzip()
157385fee539SLionel Sambuc  * starts the compression/decompression process as a child, using magic
157485fee539SLionel Sambuc  * to keep the fd the same in the calling function (parent). possible
157585fee539SLionel Sambuc  * programs are GZIP_CMD, BZIP2_CMD, and COMPRESS_CMD.
157685fee539SLionel Sambuc  */
157785fee539SLionel Sambuc void
ar_start_gzip(int fd,const char * gzp,int wr)157885fee539SLionel Sambuc ar_start_gzip(int fd, const char *gzp, int wr)
157985fee539SLionel Sambuc {
158085fee539SLionel Sambuc 	int fds[2];
158185fee539SLionel Sambuc 	const char *gzip_flags;
158285fee539SLionel Sambuc 
158385fee539SLionel Sambuc 	if (pipe(fds) < 0)
158485fee539SLionel Sambuc 		err(1, "could not pipe");
158585fee539SLionel Sambuc 	zpid = fork();
158685fee539SLionel Sambuc 	if (zpid < 0)
158785fee539SLionel Sambuc 		err(1, "could not fork");
158885fee539SLionel Sambuc 
158985fee539SLionel Sambuc 	/* parent */
159085fee539SLionel Sambuc 	if (zpid) {
159185fee539SLionel Sambuc 		if (wr)
159285fee539SLionel Sambuc 			dup2(fds[1], fd);
159385fee539SLionel Sambuc 		else
159485fee539SLionel Sambuc 			dup2(fds[0], fd);
159585fee539SLionel Sambuc 		close(fds[0]);
159685fee539SLionel Sambuc 		close(fds[1]);
159785fee539SLionel Sambuc 	} else {
159885fee539SLionel Sambuc 		if (wr) {
159985fee539SLionel Sambuc 			dup2(fds[0], STDIN_FILENO);
160085fee539SLionel Sambuc 			dup2(fd, STDOUT_FILENO);
160185fee539SLionel Sambuc 			gzip_flags = "-c";
160285fee539SLionel Sambuc 		} else {
160385fee539SLionel Sambuc 			dup2(fds[1], STDOUT_FILENO);
160485fee539SLionel Sambuc 			dup2(fd, STDIN_FILENO);
160585fee539SLionel Sambuc 			gzip_flags = "-dc";
160685fee539SLionel Sambuc 		}
160785fee539SLionel Sambuc 		close(fds[0]);
160885fee539SLionel Sambuc 		close(fds[1]);
160985fee539SLionel Sambuc 		if (execlp(gzp, gzp, gzip_flags, NULL) < 0)
161085fee539SLionel Sambuc 			err(1, "could not exec");
161185fee539SLionel Sambuc 		/* NOTREACHED */
161285fee539SLionel Sambuc 	}
161385fee539SLionel Sambuc }
161485fee539SLionel Sambuc 
161585fee539SLionel Sambuc static const char *
timefmt(char * buf,size_t size,off_t sz,time_t tm,const char * unitstr)161685fee539SLionel Sambuc timefmt(char *buf, size_t size, off_t sz, time_t tm, const char *unitstr)
161785fee539SLionel Sambuc {
161885fee539SLionel Sambuc 	(void)snprintf(buf, size, "%lu secs (" OFFT_F " %s/sec)",
161985fee539SLionel Sambuc 	    (unsigned long)tm, (OFFT_T)(sz / tm), unitstr);
162085fee539SLionel Sambuc 	return buf;
162185fee539SLionel Sambuc }
162285fee539SLionel Sambuc 
162385fee539SLionel Sambuc static const char *
sizefmt(char * buf,size_t size,off_t sz)162485fee539SLionel Sambuc sizefmt(char *buf, size_t size, off_t sz)
162585fee539SLionel Sambuc {
162685fee539SLionel Sambuc 	(void)snprintf(buf, size, OFFT_F " bytes", (OFFT_T)sz);
162785fee539SLionel Sambuc 	return buf;
162885fee539SLionel Sambuc }
162985fee539SLionel Sambuc 
163085fee539SLionel Sambuc void
ar_summary(int n)163185fee539SLionel Sambuc ar_summary(int n)
163285fee539SLionel Sambuc {
163385fee539SLionel Sambuc 	time_t secs;
163485fee539SLionel Sambuc 	int len;
163585fee539SLionel Sambuc 	char buf[BUFSIZ];
163685fee539SLionel Sambuc 	char tbuf[MAXPATHLEN/4];	/* XXX silly size! */
163785fee539SLionel Sambuc 	char s1buf[MAXPATHLEN/8];	/* XXX very silly size! */
163885fee539SLionel Sambuc 	char s2buf[MAXPATHLEN/8];	/* XXX very silly size! */
163985fee539SLionel Sambuc 	FILE *outf;
164085fee539SLionel Sambuc 
164185fee539SLionel Sambuc 	if (act == LIST)
164285fee539SLionel Sambuc 		outf = stdout;
164385fee539SLionel Sambuc 	else
164485fee539SLionel Sambuc 		outf = stderr;
164585fee539SLionel Sambuc 
164685fee539SLionel Sambuc 	/*
164785fee539SLionel Sambuc 	 * If we are called from a signal (n != 0), use snprintf(3) so that we
164885fee539SLionel Sambuc 	 * don't reenter stdio(3).
164985fee539SLionel Sambuc 	 */
165085fee539SLionel Sambuc 	(void)time(&secs);
165185fee539SLionel Sambuc 	if ((secs -= starttime) == 0)
165285fee539SLionel Sambuc 		secs = 1;
165385fee539SLionel Sambuc 
165485fee539SLionel Sambuc 	/*
165585fee539SLionel Sambuc 	 * If we have not determined the format yet, we just say how many bytes
165685fee539SLionel Sambuc 	 * we have skipped over looking for a header to id. there is no way we
165785fee539SLionel Sambuc 	 * could have written anything yet.
165885fee539SLionel Sambuc 	 */
165985fee539SLionel Sambuc 	if (frmt == NULL && act != COPY) {
166085fee539SLionel Sambuc 		len = snprintf(buf, sizeof(buf),
166185fee539SLionel Sambuc 		    "unknown format, %s skipped in %s\n",
166285fee539SLionel Sambuc 		    sizefmt(s1buf, sizeof(s1buf), rdcnt),
166385fee539SLionel Sambuc 		    timefmt(tbuf, sizeof(tbuf), rdcnt, secs, "bytes"));
166485fee539SLionel Sambuc 		if (n == 0)
166585fee539SLionel Sambuc 			(void)fprintf(outf, "%s: %s", argv0, buf);
166685fee539SLionel Sambuc 		else
166785fee539SLionel Sambuc 			(void)write(STDERR_FILENO, buf, len);
166885fee539SLionel Sambuc 		return;
166985fee539SLionel Sambuc 	}
167085fee539SLionel Sambuc 
167185fee539SLionel Sambuc 
167285fee539SLionel Sambuc 	if (n != 0 && *archd.name) {
167385fee539SLionel Sambuc 		len = snprintf(buf, sizeof(buf), "Working on `%s' (%s)\n",
167485fee539SLionel Sambuc 		    archd.name, sizefmt(s1buf, sizeof(s1buf), archd.sb.st_size));
167585fee539SLionel Sambuc 		(void)write(STDERR_FILENO, buf, len);
167685fee539SLionel Sambuc 		len = 0;
167785fee539SLionel Sambuc 	}
167885fee539SLionel Sambuc 
167985fee539SLionel Sambuc 
168085fee539SLionel Sambuc 	if (act == COPY) {
168185fee539SLionel Sambuc 		len = snprintf(buf, sizeof(buf),
168285fee539SLionel Sambuc 		    "%lu files in %s\n",
168385fee539SLionel Sambuc 		    (unsigned long)flcnt,
168485fee539SLionel Sambuc 		    timefmt(tbuf, sizeof(tbuf), flcnt, secs, "files"));
168585fee539SLionel Sambuc 	} else {
168685fee539SLionel Sambuc 		len = snprintf(buf, sizeof(buf),
168785fee539SLionel Sambuc 		    "%s vol %d, %lu files, %s read, %s written in %s\n",
168885fee539SLionel Sambuc 		    frmt->name, arvol-1, (unsigned long)flcnt,
168985fee539SLionel Sambuc 		    sizefmt(s1buf, sizeof(s1buf), rdcnt),
169085fee539SLionel Sambuc 		    sizefmt(s2buf, sizeof(s2buf), wrcnt),
169185fee539SLionel Sambuc 		    timefmt(tbuf, sizeof(tbuf), rdcnt + wrcnt, secs, "bytes"));
169285fee539SLionel Sambuc 	}
169385fee539SLionel Sambuc 	if (n == 0)
169485fee539SLionel Sambuc 		(void)fprintf(outf, "%s: %s", argv0, buf);
169585fee539SLionel Sambuc 	else
169685fee539SLionel Sambuc 		(void)write(STDERR_FILENO, buf, strlen(buf));
169785fee539SLionel Sambuc }
169885fee539SLionel Sambuc 
169985fee539SLionel Sambuc /*
170085fee539SLionel Sambuc  * ar_dochdir(name)
170185fee539SLionel Sambuc  *	change directory to name, and remember where we came from and
170285fee539SLionel Sambuc  *	where we change to (for ar_open).
170385fee539SLionel Sambuc  *
170485fee539SLionel Sambuc  *	Maybe we could try to be smart and only do the actual chdir
170585fee539SLionel Sambuc  *	when necessary to write a file read from the archive, but this
170685fee539SLionel Sambuc  *	is not easy to get right given the pax code structure.
170785fee539SLionel Sambuc  *
170885fee539SLionel Sambuc  *	Be sure to not leak descriptors!
170985fee539SLionel Sambuc  *
171085fee539SLionel Sambuc  *	We are called N * M times when extracting, and N times when
171185fee539SLionel Sambuc  *	writing archives, where
171285fee539SLionel Sambuc  *	N:	number of -C options
171385fee539SLionel Sambuc  *	M:	number of files in archive
171485fee539SLionel Sambuc  *
171585fee539SLionel Sambuc  * Returns 0 if all went well, else -1.
171685fee539SLionel Sambuc  */
171785fee539SLionel Sambuc 
171885fee539SLionel Sambuc int
ar_dochdir(const char * name)171985fee539SLionel Sambuc ar_dochdir(const char *name)
172085fee539SLionel Sambuc {
172185fee539SLionel Sambuc 	/* First fdochdir() back... */
172285fee539SLionel Sambuc 	if (fdochdir(cwdfd) == -1)
172385fee539SLionel Sambuc 		return -1;
172485fee539SLionel Sambuc 	if (dochdir(name) == -1)
172585fee539SLionel Sambuc 		return -1;
172685fee539SLionel Sambuc 	return 0;
172785fee539SLionel Sambuc }
1728