xref: /freebsd-src/bin/pax/ar_io.c (revision e043f37205ffbde5627ff299ad25cd532f2956f0)
14b88c807SRodney W. Grimes /*-
28a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
38a16b7a1SPedro F. Giffuni  *
44b88c807SRodney W. Grimes  * Copyright (c) 1992 Keith Muller.
54b88c807SRodney W. Grimes  * Copyright (c) 1992, 1993
64b88c807SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
74b88c807SRodney W. Grimes  *
84b88c807SRodney W. Grimes  * This code is derived from software contributed to Berkeley by
94b88c807SRodney W. Grimes  * Keith Muller of the University of California, San Diego.
104b88c807SRodney W. Grimes  *
114b88c807SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
124b88c807SRodney W. Grimes  * modification, are permitted provided that the following conditions
134b88c807SRodney W. Grimes  * are met:
144b88c807SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
154b88c807SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
164b88c807SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
174b88c807SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
184b88c807SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
19fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
204b88c807SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
214b88c807SRodney W. Grimes  *    without specific prior written permission.
224b88c807SRodney W. Grimes  *
234b88c807SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
244b88c807SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
254b88c807SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
264b88c807SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
274b88c807SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
284b88c807SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
294b88c807SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
304b88c807SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
314b88c807SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
324b88c807SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
334b88c807SRodney W. Grimes  * SUCH DAMAGE.
344b88c807SRodney W. Grimes  */
354b88c807SRodney W. Grimes 
364b88c807SRodney W. Grimes #include <sys/types.h>
374b88c807SRodney W. Grimes #include <sys/ioctl.h>
384b88c807SRodney W. Grimes #include <sys/mtio.h>
391192d531SKris Kennaway #include <sys/stat.h>
401192d531SKris Kennaway #include <sys/wait.h>
411192d531SKris Kennaway #include <err.h>
424b88c807SRodney W. Grimes #include <errno.h>
431192d531SKris Kennaway #include <fcntl.h>
441192d531SKris Kennaway #include <signal.h>
455b94264cSTim J. Robbins #include <stdint.h>
461192d531SKris Kennaway #include <stdio.h>
471192d531SKris Kennaway #include <string.h>
484b88c807SRodney W. Grimes #include <stdlib.h>
491192d531SKris Kennaway #include <unistd.h>
504b88c807SRodney W. Grimes #include "pax.h"
51b1787decSKris Kennaway #include "options.h"
524b88c807SRodney W. Grimes #include "extern.h"
534b88c807SRodney W. Grimes 
544b88c807SRodney W. Grimes /*
554b88c807SRodney W. Grimes  * Routines which deal directly with the archive I/O device/file.
564b88c807SRodney W. Grimes  */
574b88c807SRodney W. Grimes 
584b88c807SRodney W. Grimes #define DMOD		0666		/* default mode of created archives */
594b88c807SRodney W. Grimes #define EXT_MODE	O_RDONLY	/* open mode for list/extract */
604b88c807SRodney W. Grimes #define AR_MODE		(O_WRONLY | O_CREAT | O_TRUNC)	/* mode for archive */
614b88c807SRodney W. Grimes #define APP_MODE	O_RDWR		/* mode for append */
6240feca3aSMark Murray 
6340feca3aSMark Murray static char none[] = "<NONE>";		/* pseudo name for no file */
6440feca3aSMark Murray static char stdo[] = "<STDOUT>";	/* pseudo name for stdout */
6540feca3aSMark Murray static char stdn[] = "<STDIN>";		/* pseudo name for stdin */
664b88c807SRodney W. Grimes static int arfd = -1;			/* archive file descriptor */
674b88c807SRodney W. Grimes static int artyp = ISREG;		/* archive type: file/FIFO/tape */
684b88c807SRodney W. Grimes static int arvol = 1;			/* archive volume number */
694b88c807SRodney W. Grimes static int lstrval = -1;		/* return value from last i/o */
704b88c807SRodney W. Grimes static int io_ok;			/* i/o worked on volume after resync */
714b88c807SRodney W. Grimes static int did_io;			/* did i/o ever occur on volume? */
724b88c807SRodney W. Grimes static int done;			/* set via tty termination */
734b88c807SRodney W. Grimes static struct stat arsb;		/* stat of archive device at open */
744b88c807SRodney W. Grimes static int invld_rec;			/* tape has out of spec record size */
754b88c807SRodney W. Grimes static int wr_trail = 1;		/* trailer was rewritten in append */
764b88c807SRodney W. Grimes static int can_unlnk = 0;		/* do we unlink null archives?  */
7740feca3aSMark Murray const char *arcname;		  	/* printable name of archive */
781192d531SKris Kennaway const char *gzip_program;		/* name of gzip program */
791192d531SKris Kennaway static pid_t zpid = -1; 		/* pid of child process */
804b88c807SRodney W. Grimes 
8146251ddeSWarner Losh static int get_phys(void);
8246251ddeSWarner Losh static void ar_start_gzip(int, const char *, int);
834b88c807SRodney W. Grimes 
844b88c807SRodney W. Grimes /*
854b88c807SRodney W. Grimes  * ar_open()
864b88c807SRodney W. Grimes  *	Opens the next archive volume. Determines the type of the device and
874b88c807SRodney W. Grimes  *	sets up block sizes as required by the archive device and the format.
884b88c807SRodney W. Grimes  *	Note: we may be called with name == NULL on the first open only.
894b88c807SRodney W. Grimes  * Return:
904b88c807SRodney W. Grimes  *	-1 on failure, 0 otherwise
914b88c807SRodney W. Grimes  */
924b88c807SRodney W. Grimes 
934b88c807SRodney W. Grimes int
ar_open(const char * name)9440feca3aSMark Murray ar_open(const char *name)
954b88c807SRodney W. Grimes {
964b88c807SRodney W. Grimes 	struct mtget mb;
974b88c807SRodney W. Grimes 
984b88c807SRodney W. Grimes 	if (arfd != -1)
994b88c807SRodney W. Grimes 		(void)close(arfd);
1004b88c807SRodney W. Grimes 	arfd = -1;
1014b88c807SRodney W. Grimes 	can_unlnk = did_io = io_ok = invld_rec = 0;
1024b88c807SRodney W. Grimes 	artyp = ISREG;
1034b88c807SRodney W. Grimes 	flcnt = 0;
1044b88c807SRodney W. Grimes 
1054b88c807SRodney W. Grimes 	/*
1064b88c807SRodney W. Grimes 	 * open based on overall operation mode
1074b88c807SRodney W. Grimes 	 */
1084b88c807SRodney W. Grimes 	switch (act) {
1094b88c807SRodney W. Grimes 	case LIST:
1104b88c807SRodney W. Grimes 	case EXTRACT:
1114b88c807SRodney W. Grimes 		if (name == NULL) {
1124b88c807SRodney W. Grimes 			arfd = STDIN_FILENO;
11340feca3aSMark Murray 			arcname = stdn;
1144b88c807SRodney W. Grimes 		} else if ((arfd = open(name, EXT_MODE, DMOD)) < 0)
115778766feSKris Kennaway 			syswarn(0, errno, "Failed open to read on %s", name);
1161192d531SKris Kennaway 		if (arfd != -1 && gzip_program != NULL)
1171192d531SKris Kennaway 			ar_start_gzip(arfd, gzip_program, 0);
1184b88c807SRodney W. Grimes 		break;
1194b88c807SRodney W. Grimes 	case ARCHIVE:
1204b88c807SRodney W. Grimes 		if (name == NULL) {
1214b88c807SRodney W. Grimes 			arfd = STDOUT_FILENO;
12240feca3aSMark Murray 			arcname = stdo;
1234b88c807SRodney W. Grimes 		} else if ((arfd = open(name, AR_MODE, DMOD)) < 0)
124778766feSKris Kennaway 			syswarn(0, errno, "Failed open to write on %s", name);
1254b88c807SRodney W. Grimes 		else
1264b88c807SRodney W. Grimes 			can_unlnk = 1;
1271192d531SKris Kennaway 		if (arfd != -1 && gzip_program != NULL)
1281192d531SKris Kennaway 			ar_start_gzip(arfd, gzip_program, 1);
1294b88c807SRodney W. Grimes 		break;
1304b88c807SRodney W. Grimes 	case APPND:
1314b88c807SRodney W. Grimes 		if (name == NULL) {
1324b88c807SRodney W. Grimes 			arfd = STDOUT_FILENO;
13340feca3aSMark Murray 			arcname = stdo;
1344b88c807SRodney W. Grimes 		} else if ((arfd = open(name, APP_MODE, DMOD)) < 0)
135778766feSKris Kennaway 			syswarn(0, errno, "Failed open to read/write on %s",
1364b88c807SRodney W. Grimes 				name);
1374b88c807SRodney W. Grimes 		break;
1384b88c807SRodney W. Grimes 	case COPY:
1394b88c807SRodney W. Grimes 		/*
1404b88c807SRodney W. Grimes 		 * arfd not used in COPY mode
1414b88c807SRodney W. Grimes 		 */
14240feca3aSMark Murray 		arcname = none;
1434b88c807SRodney W. Grimes 		lstrval = 1;
1444b88c807SRodney W. Grimes 		return(0);
1454b88c807SRodney W. Grimes 	}
1464b88c807SRodney W. Grimes 	if (arfd < 0)
1474b88c807SRodney W. Grimes 		return(-1);
1484b88c807SRodney W. Grimes 
149b1787decSKris Kennaway 	if (chdname != NULL)
150c33c2570SDavid E. O'Brien 		if (chdir(chdname) != 0) {
151b1787decSKris Kennaway 			syswarn(1, errno, "Failed chdir to %s", chdname);
152c33c2570SDavid E. O'Brien 			return(-1);
153c33c2570SDavid E. O'Brien 		}
1544b88c807SRodney W. Grimes 	/*
1554b88c807SRodney W. Grimes 	 * set up is based on device type
1564b88c807SRodney W. Grimes 	 */
1574b88c807SRodney W. Grimes 	if (fstat(arfd, &arsb) < 0) {
158778766feSKris Kennaway 		syswarn(0, errno, "Failed stat on %s", arcname);
1594b88c807SRodney W. Grimes 		(void)close(arfd);
1604b88c807SRodney W. Grimes 		arfd = -1;
1614b88c807SRodney W. Grimes 		can_unlnk = 0;
1624b88c807SRodney W. Grimes 		return(-1);
1634b88c807SRodney W. Grimes 	}
1644b88c807SRodney W. Grimes 	if (S_ISDIR(arsb.st_mode)) {
165778766feSKris Kennaway 		paxwarn(0, "Cannot write an archive on top of a directory %s",
1664b88c807SRodney W. Grimes 		    arcname);
1674b88c807SRodney W. Grimes 		(void)close(arfd);
1684b88c807SRodney W. Grimes 		arfd = -1;
1694b88c807SRodney W. Grimes 		can_unlnk = 0;
1704b88c807SRodney W. Grimes 		return(-1);
1714b88c807SRodney W. Grimes 	}
1724b88c807SRodney W. Grimes 
1734b88c807SRodney W. Grimes 	if (S_ISCHR(arsb.st_mode))
1744b88c807SRodney W. Grimes 		artyp = ioctl(arfd, MTIOCGET, &mb) ? ISCHR : ISTAPE;
1754b88c807SRodney W. Grimes 	else if (S_ISBLK(arsb.st_mode))
1764b88c807SRodney W. Grimes 		artyp = ISBLK;
1774b88c807SRodney W. Grimes 	else if ((lseek(arfd, (off_t)0L, SEEK_CUR) == -1) && (errno == ESPIPE))
1784b88c807SRodney W. Grimes 		artyp = ISPIPE;
1794b88c807SRodney W. Grimes 	else
1804b88c807SRodney W. Grimes 		artyp = ISREG;
1814b88c807SRodney W. Grimes 
1824b88c807SRodney W. Grimes 	/*
1834b88c807SRodney W. Grimes 	 * make sure we beyond any doubt that we only can unlink regular files
1844b88c807SRodney W. Grimes 	 * we created
1854b88c807SRodney W. Grimes 	 */
1864b88c807SRodney W. Grimes 	if (artyp != ISREG)
1874b88c807SRodney W. Grimes 		can_unlnk = 0;
1884b88c807SRodney W. Grimes 	/*
1894b88c807SRodney W. Grimes 	 * if we are writing, we are done
1904b88c807SRodney W. Grimes 	 */
1914b88c807SRodney W. Grimes 	if (act == ARCHIVE) {
1924b88c807SRodney W. Grimes 		blksz = rdblksz = wrblksz;
1934b88c807SRodney W. Grimes 		lstrval = 1;
1944b88c807SRodney W. Grimes 		return(0);
1954b88c807SRodney W. Grimes 	}
1964b88c807SRodney W. Grimes 
1974b88c807SRodney W. Grimes 	/*
1984b88c807SRodney W. Grimes 	 * set default blksz on read. APPNDs writes rdblksz on the last volume
1994b88c807SRodney W. Grimes 	 * On all new archive volumes, we shift to wrblksz (if the user
20001c99176SUlrich Spörlein 	 * specified one, otherwise we will continue to use rdblksz). We
201*0266a5d6SDag-Erling Smørgrav 	 * must set blocksize based on what kind of device the archive is
2024b88c807SRodney W. Grimes 	 * stored.
2034b88c807SRodney W. Grimes 	 */
2044b88c807SRodney W. Grimes 	switch(artyp) {
2054b88c807SRodney W. Grimes 	case ISTAPE:
2064b88c807SRodney W. Grimes 		/*
2074b88c807SRodney W. Grimes 		 * Tape drives come in at least two flavors. Those that support
2084b88c807SRodney W. Grimes 		 * variable sized records and those that have fixed sized
2094b88c807SRodney W. Grimes 		 * records. They must be treated differently. For tape drives
2104b88c807SRodney W. Grimes 		 * that support variable sized records, we must make large
2114b88c807SRodney W. Grimes 		 * reads to make sure we get the entire record, otherwise we
2124b88c807SRodney W. Grimes 		 * will just get the first part of the record (up to size we
2134b88c807SRodney W. Grimes 		 * asked). Tapes with fixed sized records may or may not return
2144b88c807SRodney W. Grimes 		 * multiple records in a single read. We really do not care
2154b88c807SRodney W. Grimes 		 * what the physical record size is UNLESS we are going to
2164b88c807SRodney W. Grimes 		 * append. (We will need the physical block size to rewrite
2174b88c807SRodney W. Grimes 		 * the trailer). Only when we are appending do we go to the
2184b88c807SRodney W. Grimes 		 * effort to figure out the true PHYSICAL record size.
2194b88c807SRodney W. Grimes 		 */
2204b88c807SRodney W. Grimes 		blksz = rdblksz = MAXBLK;
2214b88c807SRodney W. Grimes 		break;
2224b88c807SRodney W. Grimes 	case ISPIPE:
2234b88c807SRodney W. Grimes 	case ISBLK:
2244b88c807SRodney W. Grimes 	case ISCHR:
2254b88c807SRodney W. Grimes 		/*
2264b88c807SRodney W. Grimes 		 * Blocksize is not a major issue with these devices (but must
2274b88c807SRodney W. Grimes 		 * be kept a multiple of 512). If the user specified a write
2284b88c807SRodney W. Grimes 		 * block size, we use that to read. Under append, we must
2294b88c807SRodney W. Grimes 		 * always keep blksz == rdblksz. Otherwise we go ahead and use
2304b88c807SRodney W. Grimes 		 * the device optimal blocksize as (and if) returned by stat
2314b88c807SRodney W. Grimes 		 * and if it is within pax specs.
2324b88c807SRodney W. Grimes 		 */
2334b88c807SRodney W. Grimes 		if ((act == APPND) && wrblksz) {
2344b88c807SRodney W. Grimes 			blksz = rdblksz = wrblksz;
2354b88c807SRodney W. Grimes 			break;
2364b88c807SRodney W. Grimes 		}
2374b88c807SRodney W. Grimes 
2384b88c807SRodney W. Grimes 		if ((arsb.st_blksize > 0) && (arsb.st_blksize < MAXBLK) &&
2394b88c807SRodney W. Grimes 		    ((arsb.st_blksize % BLKMULT) == 0))
2404b88c807SRodney W. Grimes 			rdblksz = arsb.st_blksize;
2414b88c807SRodney W. Grimes 		else
2424b88c807SRodney W. Grimes 			rdblksz = DEVBLK;
2434b88c807SRodney W. Grimes 		/*
2444b88c807SRodney W. Grimes 		 * For performance go for large reads when we can without harm
2454b88c807SRodney W. Grimes 		 */
2464b88c807SRodney W. Grimes 		if ((act == APPND) || (artyp == ISCHR))
2474b88c807SRodney W. Grimes 			blksz = rdblksz;
2484b88c807SRodney W. Grimes 		else
2494b88c807SRodney W. Grimes 			blksz = MAXBLK;
2504b88c807SRodney W. Grimes 		break;
2514b88c807SRodney W. Grimes 	case ISREG:
2524b88c807SRodney W. Grimes 		/*
2534b88c807SRodney W. Grimes 		 * if the user specified wrblksz works, use it. Under appends
2544b88c807SRodney W. Grimes 		 * we must always keep blksz == rdblksz
2554b88c807SRodney W. Grimes 		 */
2564b88c807SRodney W. Grimes 		if ((act == APPND) && wrblksz && ((arsb.st_size%wrblksz)==0)){
2574b88c807SRodney W. Grimes 			blksz = rdblksz = wrblksz;
2584b88c807SRodney W. Grimes 			break;
2594b88c807SRodney W. Grimes 		}
2604b88c807SRodney W. Grimes 		/*
2614b88c807SRodney W. Grimes 		 * See if we can find the blocking factor from the file size
2624b88c807SRodney W. Grimes 		 */
2634b88c807SRodney W. Grimes 		for (rdblksz = MAXBLK; rdblksz > 0; rdblksz -= BLKMULT)
2644b88c807SRodney W. Grimes 			if ((arsb.st_size % rdblksz) == 0)
2654b88c807SRodney W. Grimes 				break;
2664b88c807SRodney W. Grimes 		/*
26746be34b9SKris Kennaway 		 * When we cannot find a match, we may have a flawed archive.
2684b88c807SRodney W. Grimes 		 */
2694b88c807SRodney W. Grimes 		if (rdblksz <= 0)
2704b88c807SRodney W. Grimes 			rdblksz = FILEBLK;
2714b88c807SRodney W. Grimes 		/*
2724b88c807SRodney W. Grimes 		 * for performance go for large reads when we can
2734b88c807SRodney W. Grimes 		 */
2744b88c807SRodney W. Grimes 		if (act == APPND)
2754b88c807SRodney W. Grimes 			blksz = rdblksz;
2764b88c807SRodney W. Grimes 		else
2774b88c807SRodney W. Grimes 			blksz = MAXBLK;
2784b88c807SRodney W. Grimes 		break;
2794b88c807SRodney W. Grimes 	default:
2804b88c807SRodney W. Grimes 		/*
281*0266a5d6SDag-Erling Smørgrav 		 * should never happen, worst case, slow...
2824b88c807SRodney W. Grimes 		 */
2834b88c807SRodney W. Grimes 		blksz = rdblksz = BLKMULT;
2844b88c807SRodney W. Grimes 		break;
2854b88c807SRodney W. Grimes 	}
2864b88c807SRodney W. Grimes 	lstrval = 1;
2874b88c807SRodney W. Grimes 	return(0);
2884b88c807SRodney W. Grimes }
2894b88c807SRodney W. Grimes 
2904b88c807SRodney W. Grimes /*
2914b88c807SRodney W. Grimes  * ar_close()
2924b88c807SRodney W. Grimes  *	closes archive device, increments volume number, and prints i/o summary
2934b88c807SRodney W. Grimes  */
2944b88c807SRodney W. Grimes void
ar_close(void)2954b88c807SRodney W. Grimes ar_close(void)
2964b88c807SRodney W. Grimes {
2975f44e053SDavid E. O'Brien 	int status;
2984b88c807SRodney W. Grimes 
2994b88c807SRodney W. Grimes 	if (arfd < 0) {
3004b88c807SRodney W. Grimes 		did_io = io_ok = flcnt = 0;
3014b88c807SRodney W. Grimes 		return;
3024b88c807SRodney W. Grimes 	}
3034b88c807SRodney W. Grimes 
3044b88c807SRodney W. Grimes 	/*
3054b88c807SRodney W. Grimes 	 * Close archive file. This may take a LONG while on tapes (we may be
3064b88c807SRodney W. Grimes 	 * forced to wait for the rewind to complete) so tell the user what is
3074b88c807SRodney W. Grimes 	 * going on (this avoids the user hitting control-c thinking pax is
3084b88c807SRodney W. Grimes 	 * broken).
3094b88c807SRodney W. Grimes 	 */
3104b88c807SRodney W. Grimes 	if (vflag && (artyp == ISTAPE)) {
3114b88c807SRodney W. Grimes 		if (vfpart)
312b1787decSKris Kennaway 			(void)putc('\n', listf);
313b1787decSKris Kennaway 		(void)fprintf(listf,
3144b88c807SRodney W. Grimes 			"%s: Waiting for tape drive close to complete...",
3154b88c807SRodney W. Grimes 			argv0);
316b1787decSKris Kennaway 		(void)fflush(listf);
3174b88c807SRodney W. Grimes 	}
3184b88c807SRodney W. Grimes 
3194b88c807SRodney W. Grimes 	/*
3204b88c807SRodney W. Grimes 	 * if nothing was written to the archive (and we created it), we remove
3214b88c807SRodney W. Grimes 	 * it
3224b88c807SRodney W. Grimes 	 */
3234b88c807SRodney W. Grimes 	if (can_unlnk && (fstat(arfd, &arsb) == 0) && (S_ISREG(arsb.st_mode)) &&
3244b88c807SRodney W. Grimes 	    (arsb.st_size == 0)) {
3254b88c807SRodney W. Grimes 		(void)unlink(arcname);
3264b88c807SRodney W. Grimes 		can_unlnk = 0;
3274b88c807SRodney W. Grimes 	}
3284b88c807SRodney W. Grimes 
3291192d531SKris Kennaway 	/*
3301192d531SKris Kennaway 	 * for a quick extract/list, pax frequently exits before the child
3311192d531SKris Kennaway 	 * process is done
3321192d531SKris Kennaway 	 */
3335f44e053SDavid E. O'Brien 	if ((act == LIST || act == EXTRACT) && nflag && zpid > 0)
3341192d531SKris Kennaway 		kill(zpid, SIGINT);
3351192d531SKris Kennaway 
3364b88c807SRodney W. Grimes 	(void)close(arfd);
3374b88c807SRodney W. Grimes 
3385f44e053SDavid E. O'Brien 	/* Do not exit before child to ensure data integrity */
3395f44e053SDavid E. O'Brien 	if (zpid > 0)
3405f44e053SDavid E. O'Brien 		waitpid(zpid, &status, 0);
3415f44e053SDavid E. O'Brien 
3424b88c807SRodney W. Grimes 	if (vflag && (artyp == ISTAPE)) {
343b1787decSKris Kennaway 		(void)fputs("done.\n", listf);
3444b88c807SRodney W. Grimes 		vfpart = 0;
345b1787decSKris Kennaway 		(void)fflush(listf);
3464b88c807SRodney W. Grimes 	}
3474b88c807SRodney W. Grimes 	arfd = -1;
3484b88c807SRodney W. Grimes 
3494b88c807SRodney W. Grimes 	if (!io_ok && !did_io) {
3504b88c807SRodney W. Grimes 		flcnt = 0;
3514b88c807SRodney W. Grimes 		return;
3524b88c807SRodney W. Grimes 	}
3534b88c807SRodney W. Grimes 	did_io = io_ok = 0;
3544b88c807SRodney W. Grimes 
3554b88c807SRodney W. Grimes 	/*
3564b88c807SRodney W. Grimes 	 * The volume number is only increased when the last device has data
3574b88c807SRodney W. Grimes 	 * and we have already determined the archive format.
3584b88c807SRodney W. Grimes 	 */
3594b88c807SRodney W. Grimes 	if (frmt != NULL)
3604b88c807SRodney W. Grimes 		++arvol;
3614b88c807SRodney W. Grimes 
3624b88c807SRodney W. Grimes 	if (!vflag) {
3634b88c807SRodney W. Grimes 		flcnt = 0;
3644b88c807SRodney W. Grimes 		return;
3654b88c807SRodney W. Grimes 	}
3664b88c807SRodney W. Grimes 
3674b88c807SRodney W. Grimes 	/*
3684b88c807SRodney W. Grimes 	 * Print out a summary of I/O for this archive volume.
3694b88c807SRodney W. Grimes 	 */
3704b88c807SRodney W. Grimes 	if (vfpart) {
371b1787decSKris Kennaway 		(void)putc('\n', listf);
3724b88c807SRodney W. Grimes 		vfpart = 0;
3734b88c807SRodney W. Grimes 	}
3744b88c807SRodney W. Grimes 
3754b88c807SRodney W. Grimes 	/*
3764b88c807SRodney W. Grimes 	 * If we have not determined the format yet, we just say how many bytes
37701c99176SUlrich Spörlein 	 * we have skipped over looking for a header to id. There is no way we
3784b88c807SRodney W. Grimes 	 * could have written anything yet.
3794b88c807SRodney W. Grimes 	 */
3804b88c807SRodney W. Grimes 	if (frmt == NULL) {
3815b94264cSTim J. Robbins 		(void)fprintf(listf, "%s: unknown format, %ju bytes skipped.\n",
3825b94264cSTim J. Robbins 		    argv0, (uintmax_t)rdcnt);
383b1787decSKris Kennaway 		(void)fflush(listf);
3844b88c807SRodney W. Grimes 		flcnt = 0;
3854b88c807SRodney W. Grimes 		return;
3864b88c807SRodney W. Grimes 	}
3874b88c807SRodney W. Grimes 
388b1787decSKris Kennaway 	if (strcmp(NM_CPIO, argv0) == 0)
3895b94264cSTim J. Robbins 		(void)fprintf(listf, "%llu blocks\n",
3905b94264cSTim J. Robbins 		    (unsigned long long)((rdcnt ? rdcnt : wrcnt) / 5120));
391b1787decSKris Kennaway 	else if (strcmp(NM_TAR, argv0) != 0)
392b1787decSKris Kennaway 		(void)fprintf(listf,
3935b94264cSTim J. Robbins 		    "%s: %s vol %d, %ju files, %ju bytes read, %ju bytes written.\n",
3945b94264cSTim J. Robbins 		    argv0, frmt->name, arvol-1, (uintmax_t)flcnt,
3955b94264cSTim J. Robbins 		    (uintmax_t)rdcnt, (uintmax_t)wrcnt);
396b1787decSKris Kennaway 	(void)fflush(listf);
3974b88c807SRodney W. Grimes 	flcnt = 0;
3984b88c807SRodney W. Grimes }
3994b88c807SRodney W. Grimes 
4004b88c807SRodney W. Grimes /*
4014b88c807SRodney W. Grimes  * ar_drain()
4024b88c807SRodney W. Grimes  *	drain any archive format independent padding from an archive read
4034b88c807SRodney W. Grimes  *	from a socket or a pipe. This is to prevent the process on the
4044b88c807SRodney W. Grimes  *	other side of the pipe from getting a SIGPIPE (pax will stop
4054b88c807SRodney W. Grimes  *	reading an archive once a format dependent trailer is detected).
4064b88c807SRodney W. Grimes  */
4074b88c807SRodney W. Grimes void
ar_drain(void)4084b88c807SRodney W. Grimes ar_drain(void)
4094b88c807SRodney W. Grimes {
410f789b261SWarner Losh 	int res;
4114b88c807SRodney W. Grimes 	char drbuf[MAXBLK];
4124b88c807SRodney W. Grimes 
4134b88c807SRodney W. Grimes 	/*
4144b88c807SRodney W. Grimes 	 * we only drain from a pipe/socket. Other devices can be closed
4154b88c807SRodney W. Grimes 	 * without reading up to end of file. We sure hope that pipe is closed
4164b88c807SRodney W. Grimes 	 * on the other side so we will get an EOF.
4174b88c807SRodney W. Grimes 	 */
4184b88c807SRodney W. Grimes 	if ((artyp != ISPIPE) || (lstrval <= 0))
4194b88c807SRodney W. Grimes 		return;
4204b88c807SRodney W. Grimes 
4214b88c807SRodney W. Grimes 	/*
4224b88c807SRodney W. Grimes 	 * keep reading until pipe is drained
4234b88c807SRodney W. Grimes 	 */
4244b88c807SRodney W. Grimes 	while ((res = read(arfd, drbuf, sizeof(drbuf))) > 0)
4254b88c807SRodney W. Grimes 		;
4264b88c807SRodney W. Grimes 	lstrval = res;
4274b88c807SRodney W. Grimes }
4284b88c807SRodney W. Grimes 
4294b88c807SRodney W. Grimes /*
4304b88c807SRodney W. Grimes  * ar_set_wr()
4314b88c807SRodney W. Grimes  *	Set up device right before switching from read to write in an append.
4324b88c807SRodney W. Grimes  *	device dependent code (if required) to do this should be added here.
4334b88c807SRodney W. Grimes  *	For all archive devices we are already positioned at the place we want
4344b88c807SRodney W. Grimes  *	to start writing when this routine is called.
4354b88c807SRodney W. Grimes  * Return:
4364b88c807SRodney W. Grimes  *	0 if all ready to write, -1 otherwise
4374b88c807SRodney W. Grimes  */
4384b88c807SRodney W. Grimes 
4394b88c807SRodney W. Grimes int
ar_set_wr(void)4404b88c807SRodney W. Grimes ar_set_wr(void)
4414b88c807SRodney W. Grimes {
4424b88c807SRodney W. Grimes 	off_t cpos;
4434b88c807SRodney W. Grimes 
4444b88c807SRodney W. Grimes 	/*
4454b88c807SRodney W. Grimes 	 * we must make sure the trailer is rewritten on append, ar_next()
4464b88c807SRodney W. Grimes 	 * will stop us if the archive containing the trailer was not written
4474b88c807SRodney W. Grimes 	 */
4484b88c807SRodney W. Grimes 	wr_trail = 0;
4494b88c807SRodney W. Grimes 
4504b88c807SRodney W. Grimes 	/*
4514b88c807SRodney W. Grimes 	 * Add any device dependent code as required here
4524b88c807SRodney W. Grimes 	 */
4534b88c807SRodney W. Grimes 	if (artyp != ISREG)
4544b88c807SRodney W. Grimes 		return(0);
4554b88c807SRodney W. Grimes 	/*
4564b88c807SRodney W. Grimes 	 * Ok we have an archive in a regular file. If we were rewriting a
4574b88c807SRodney W. Grimes 	 * file, we must get rid of all the stuff after the current offset
4584b88c807SRodney W. Grimes 	 * (it was not written by pax).
4594b88c807SRodney W. Grimes 	 */
4604b88c807SRodney W. Grimes 	if (((cpos = lseek(arfd, (off_t)0L, SEEK_CUR)) < 0) ||
4614b88c807SRodney W. Grimes 	    (ftruncate(arfd, cpos) < 0)) {
462778766feSKris Kennaway 		syswarn(1, errno, "Unable to truncate archive file");
4634b88c807SRodney W. Grimes 		return(-1);
4644b88c807SRodney W. Grimes 	}
4654b88c807SRodney W. Grimes 	return(0);
4664b88c807SRodney W. Grimes }
4674b88c807SRodney W. Grimes 
4684b88c807SRodney W. Grimes /*
4694b88c807SRodney W. Grimes  * ar_app_ok()
4704b88c807SRodney W. Grimes  *	check if the last volume in the archive allows appends. We cannot check
4714b88c807SRodney W. Grimes  *	this until we are ready to write since there is no spec that says all
4724b88c807SRodney W. Grimes  *	volumes in a single archive have to be of the same type...
4734b88c807SRodney W. Grimes  * Return:
4744b88c807SRodney W. Grimes  *	0 if we can append, -1 otherwise.
4754b88c807SRodney W. Grimes  */
4764b88c807SRodney W. Grimes 
4774b88c807SRodney W. Grimes int
ar_app_ok(void)4784b88c807SRodney W. Grimes ar_app_ok(void)
4794b88c807SRodney W. Grimes {
4804b88c807SRodney W. Grimes 	if (artyp == ISPIPE) {
481778766feSKris Kennaway 		paxwarn(1, "Cannot append to an archive obtained from a pipe.");
4824b88c807SRodney W. Grimes 		return(-1);
4834b88c807SRodney W. Grimes 	}
4844b88c807SRodney W. Grimes 
4854b88c807SRodney W. Grimes 	if (!invld_rec)
4864b88c807SRodney W. Grimes 		return(0);
487778766feSKris Kennaway 	paxwarn(1,"Cannot append, device record size %d does not support %s spec",
4884b88c807SRodney W. Grimes 		rdblksz, argv0);
4894b88c807SRodney W. Grimes 	return(-1);
4904b88c807SRodney W. Grimes }
4914b88c807SRodney W. Grimes 
4924b88c807SRodney W. Grimes /*
4934b88c807SRodney W. Grimes  * ar_read()
4944b88c807SRodney W. Grimes  *	read up to a specified number of bytes from the archive into the
4954b88c807SRodney W. Grimes  *	supplied buffer. When dealing with tapes we may not always be able to
4964b88c807SRodney W. Grimes  *	read what we want.
4974b88c807SRodney W. Grimes  * Return:
4984b88c807SRodney W. Grimes  *	Number of bytes in buffer. 0 for end of file, -1 for a read error.
4994b88c807SRodney W. Grimes  */
5004b88c807SRodney W. Grimes 
5014b88c807SRodney W. Grimes int
ar_read(char * buf,int cnt)502f789b261SWarner Losh ar_read(char *buf, int cnt)
5034b88c807SRodney W. Grimes {
504f789b261SWarner Losh 	int res = 0;
5054b88c807SRodney W. Grimes 
5064b88c807SRodney W. Grimes 	/*
5074b88c807SRodney W. Grimes 	 * if last i/o was in error, no more reads until reset or new volume
5084b88c807SRodney W. Grimes 	 */
5094b88c807SRodney W. Grimes 	if (lstrval <= 0)
5104b88c807SRodney W. Grimes 		return(lstrval);
5114b88c807SRodney W. Grimes 
5124b88c807SRodney W. Grimes 	/*
5134b88c807SRodney W. Grimes 	 * how we read must be based on device type
5144b88c807SRodney W. Grimes 	 */
5154b88c807SRodney W. Grimes 	switch (artyp) {
5164b88c807SRodney W. Grimes 	case ISTAPE:
5174b88c807SRodney W. Grimes 		if ((res = read(arfd, buf, cnt)) > 0) {
5184b88c807SRodney W. Grimes 			/*
5194b88c807SRodney W. Grimes 			 * CAUTION: tape systems may not always return the same
5204b88c807SRodney W. Grimes 			 * sized records so we leave blksz == MAXBLK. The
5214b88c807SRodney W. Grimes 			 * physical record size that a tape drive supports is
5224b88c807SRodney W. Grimes 			 * very hard to determine in a uniform and portable
5234b88c807SRodney W. Grimes 			 * manner.
5244b88c807SRodney W. Grimes 			 */
5254b88c807SRodney W. Grimes 			io_ok = 1;
5264b88c807SRodney W. Grimes 			if (res != rdblksz) {
5274b88c807SRodney W. Grimes 				/*
528*0266a5d6SDag-Erling Smørgrav 				 * Record size changed. If this happens on
5294b88c807SRodney W. Grimes 				 * any record after the first, we probably have
5304b88c807SRodney W. Grimes 				 * a tape drive which has a fixed record size
531*0266a5d6SDag-Erling Smørgrav 				 * (we are getting multiple records in a single
5324b88c807SRodney W. Grimes 				 * read). Watch out for record blocking that
5334b88c807SRodney W. Grimes 				 * violates pax spec (must be a multiple of
5344b88c807SRodney W. Grimes 				 * BLKMULT).
5354b88c807SRodney W. Grimes 				 */
5364b88c807SRodney W. Grimes 				rdblksz = res;
5374b88c807SRodney W. Grimes 				if (rdblksz % BLKMULT)
5384b88c807SRodney W. Grimes 					invld_rec = 1;
5394b88c807SRodney W. Grimes 			}
5404b88c807SRodney W. Grimes 			return(res);
5414b88c807SRodney W. Grimes 		}
5424b88c807SRodney W. Grimes 		break;
5434b88c807SRodney W. Grimes 	case ISREG:
5444b88c807SRodney W. Grimes 	case ISBLK:
5454b88c807SRodney W. Grimes 	case ISCHR:
5464b88c807SRodney W. Grimes 	case ISPIPE:
5474b88c807SRodney W. Grimes 	default:
5484b88c807SRodney W. Grimes 		/*
5494b88c807SRodney W. Grimes 		 * Files are so easy to deal with. These other things cannot
5504b88c807SRodney W. Grimes 		 * be trusted at all. So when we are dealing with character
5514b88c807SRodney W. Grimes 		 * devices and pipes we just take what they have ready for us
5524b88c807SRodney W. Grimes 		 * and return. Trying to do anything else with them runs the
5534b88c807SRodney W. Grimes 		 * risk of failure.
5544b88c807SRodney W. Grimes 		 */
5554b88c807SRodney W. Grimes 		if ((res = read(arfd, buf, cnt)) > 0) {
5564b88c807SRodney W. Grimes 			io_ok = 1;
5574b88c807SRodney W. Grimes 			return(res);
5584b88c807SRodney W. Grimes 		}
5594b88c807SRodney W. Grimes 		break;
5604b88c807SRodney W. Grimes 	}
5614b88c807SRodney W. Grimes 
5624b88c807SRodney W. Grimes 	/*
5634b88c807SRodney W. Grimes 	 * We are in trouble at this point, something is broken...
5644b88c807SRodney W. Grimes 	 */
5654b88c807SRodney W. Grimes 	lstrval = res;
5664b88c807SRodney W. Grimes 	if (res < 0)
567778766feSKris Kennaway 		syswarn(1, errno, "Failed read on archive volume %d", arvol);
5684b88c807SRodney W. Grimes 	else
569778766feSKris Kennaway 		paxwarn(0, "End of archive volume %d reached", arvol);
5704b88c807SRodney W. Grimes 	return(res);
5714b88c807SRodney W. Grimes }
5724b88c807SRodney W. Grimes 
5734b88c807SRodney W. Grimes /*
5744b88c807SRodney W. Grimes  * ar_write()
5754b88c807SRodney W. Grimes  *	Write a specified number of bytes in supplied buffer to the archive
5764b88c807SRodney W. Grimes  *	device so it appears as a single "block". Deals with errors and tries
5774b88c807SRodney W. Grimes  *	to recover when faced with short writes.
5784b88c807SRodney W. Grimes  * Return:
5794b88c807SRodney W. Grimes  *	Number of bytes written. 0 indicates end of volume reached and with no
5804b88c807SRodney W. Grimes  *	flaws (as best that can be detected). A -1 indicates an unrecoverable
58101c99176SUlrich Spörlein  *	error in the archive occurred.
5824b88c807SRodney W. Grimes  */
5834b88c807SRodney W. Grimes 
5844b88c807SRodney W. Grimes int
ar_write(char * buf,int bsz)585f789b261SWarner Losh ar_write(char *buf, int bsz)
5864b88c807SRodney W. Grimes {
587f789b261SWarner Losh 	int res;
5884b88c807SRodney W. Grimes 	off_t cpos;
5894b88c807SRodney W. Grimes 
5904b88c807SRodney W. Grimes 	/*
5914b88c807SRodney W. Grimes 	 * do not allow pax to create a "bad" archive. Once a write fails on
5924b88c807SRodney W. Grimes 	 * an archive volume prevent further writes to it.
5934b88c807SRodney W. Grimes 	 */
5944b88c807SRodney W. Grimes 	if (lstrval <= 0)
5954b88c807SRodney W. Grimes 		return(lstrval);
5964b88c807SRodney W. Grimes 
5974b88c807SRodney W. Grimes 	if ((res = write(arfd, buf, bsz)) == bsz) {
5984b88c807SRodney W. Grimes 		wr_trail = 1;
5994b88c807SRodney W. Grimes 		io_ok = 1;
6004b88c807SRodney W. Grimes 		return(bsz);
6014b88c807SRodney W. Grimes 	}
6024b88c807SRodney W. Grimes 	/*
6034b88c807SRodney W. Grimes 	 * write broke, see what we can do with it. We try to send any partial
6044b88c807SRodney W. Grimes 	 * writes that may violate pax spec to the next archive volume.
6054b88c807SRodney W. Grimes 	 */
6064b88c807SRodney W. Grimes 	if (res < 0)
6074b88c807SRodney W. Grimes 		lstrval = res;
6084b88c807SRodney W. Grimes 	else
6094b88c807SRodney W. Grimes 		lstrval = 0;
6104b88c807SRodney W. Grimes 
6114b88c807SRodney W. Grimes 	switch (artyp) {
6124b88c807SRodney W. Grimes 	case ISREG:
6134b88c807SRodney W. Grimes 		if ((res > 0) && (res % BLKMULT)) {
6144b88c807SRodney W. Grimes 			/*
6154b88c807SRodney W. Grimes 		 	 * try to fix up partial writes which are not BLKMULT
6164b88c807SRodney W. Grimes 			 * in size by forcing the runt record to next archive
6174b88c807SRodney W. Grimes 			 * volume
6184b88c807SRodney W. Grimes 		 	 */
6194b88c807SRodney W. Grimes 			if ((cpos = lseek(arfd, (off_t)0L, SEEK_CUR)) < 0)
6204b88c807SRodney W. Grimes 				break;
6214b88c807SRodney W. Grimes 			cpos -= (off_t)res;
6224b88c807SRodney W. Grimes 			if (ftruncate(arfd, cpos) < 0)
6234b88c807SRodney W. Grimes 				break;
6244b88c807SRodney W. Grimes 			res = lstrval = 0;
6254b88c807SRodney W. Grimes 			break;
6264b88c807SRodney W. Grimes 		}
6274b88c807SRodney W. Grimes 		if (res >= 0)
6284b88c807SRodney W. Grimes 			break;
6294b88c807SRodney W. Grimes 		/*
6304b88c807SRodney W. Grimes 		 * if file is out of space, handle it like a return of 0
6314b88c807SRodney W. Grimes 		 */
6324b88c807SRodney W. Grimes 		if ((errno == ENOSPC) || (errno == EFBIG) || (errno == EDQUOT))
6334b88c807SRodney W. Grimes 			res = lstrval = 0;
6344b88c807SRodney W. Grimes 		break;
6354b88c807SRodney W. Grimes 	case ISTAPE:
6364b88c807SRodney W. Grimes 	case ISCHR:
6374b88c807SRodney W. Grimes 	case ISBLK:
6384b88c807SRodney W. Grimes 		if (res >= 0)
6394b88c807SRodney W. Grimes 			break;
6404b88c807SRodney W. Grimes 		if (errno == EACCES) {
641778766feSKris Kennaway 			paxwarn(0, "Write failed, archive is write protected.");
6424b88c807SRodney W. Grimes 			res = lstrval = 0;
6434b88c807SRodney W. Grimes 			return(0);
6444b88c807SRodney W. Grimes 		}
6454b88c807SRodney W. Grimes 		/*
6464b88c807SRodney W. Grimes 		 * see if we reached the end of media, if so force a change to
6474b88c807SRodney W. Grimes 		 * the next volume
6484b88c807SRodney W. Grimes 		 */
6494b88c807SRodney W. Grimes 		if ((errno == ENOSPC) || (errno == EIO) || (errno == ENXIO))
6504b88c807SRodney W. Grimes 			res = lstrval = 0;
6514b88c807SRodney W. Grimes 		break;
6524b88c807SRodney W. Grimes 	case ISPIPE:
6534b88c807SRodney W. Grimes 	default:
6544b88c807SRodney W. Grimes 		/*
6554b88c807SRodney W. Grimes 		 * we cannot fix errors to these devices
6564b88c807SRodney W. Grimes 		 */
6574b88c807SRodney W. Grimes 		break;
6584b88c807SRodney W. Grimes 	}
6594b88c807SRodney W. Grimes 
6604b88c807SRodney W. Grimes 	/*
6614b88c807SRodney W. Grimes 	 * Better tell the user the bad news...
6624b88c807SRodney W. Grimes 	 * if this is a block aligned archive format, we may have a bad archive
66346be34b9SKris Kennaway 	 * if the format wants the header to start at a BLKMULT boundary. While
6644b88c807SRodney W. Grimes 	 * we can deal with the mis-aligned data, it violates spec and other
66501c99176SUlrich Spörlein 	 * archive readers will likely fail. If the format is not block
6664b88c807SRodney W. Grimes 	 * aligned, the user may be lucky (and the archive is ok).
6674b88c807SRodney W. Grimes 	 */
6684b88c807SRodney W. Grimes 	if (res >= 0) {
6694b88c807SRodney W. Grimes 		if (res > 0)
6704b88c807SRodney W. Grimes 			wr_trail = 1;
6714b88c807SRodney W. Grimes 		io_ok = 1;
6724b88c807SRodney W. Grimes 	}
6734b88c807SRodney W. Grimes 
6744b88c807SRodney W. Grimes 	/*
6754b88c807SRodney W. Grimes 	 * If we were trying to rewrite the trailer and it didn't work, we
6764b88c807SRodney W. Grimes 	 * must quit right away.
6774b88c807SRodney W. Grimes 	 */
6784b88c807SRodney W. Grimes 	if (!wr_trail && (res <= 0)) {
679778766feSKris Kennaway 		paxwarn(1,"Unable to append, trailer re-write failed. Quitting.");
6804b88c807SRodney W. Grimes 		return(res);
6814b88c807SRodney W. Grimes 	}
6824b88c807SRodney W. Grimes 
6834b88c807SRodney W. Grimes 	if (res == 0)
684778766feSKris Kennaway 		paxwarn(0, "End of archive volume %d reached", arvol);
6854b88c807SRodney W. Grimes 	else if (res < 0)
686778766feSKris Kennaway 		syswarn(1, errno, "Failed write to archive volume: %d", arvol);
6874b88c807SRodney W. Grimes 	else if (!frmt->blkalgn || ((res % frmt->blkalgn) == 0))
688778766feSKris Kennaway 		paxwarn(0,"WARNING: partial archive write. Archive MAY BE FLAWED");
6894b88c807SRodney W. Grimes 	else
690778766feSKris Kennaway 		paxwarn(1,"WARNING: partial archive write. Archive IS FLAWED");
6914b88c807SRodney W. Grimes 	return(res);
6924b88c807SRodney W. Grimes }
6934b88c807SRodney W. Grimes 
6944b88c807SRodney W. Grimes /*
6954b88c807SRodney W. Grimes  * ar_rdsync()
6964b88c807SRodney W. Grimes  *	Try to move past a bad spot on a flawed archive as needed to continue
6974b88c807SRodney W. Grimes  *	I/O. Clears error flags to allow I/O to continue.
6984b88c807SRodney W. Grimes  * Return:
6994b88c807SRodney W. Grimes  *	0 when ok to try i/o again, -1 otherwise.
7004b88c807SRodney W. Grimes  */
7014b88c807SRodney W. Grimes 
7024b88c807SRodney W. Grimes int
ar_rdsync(void)7034b88c807SRodney W. Grimes ar_rdsync(void)
7044b88c807SRodney W. Grimes {
7054b88c807SRodney W. Grimes 	long fsbz;
7064b88c807SRodney W. Grimes 	off_t cpos;
7074b88c807SRodney W. Grimes 	off_t mpos;
7084b88c807SRodney W. Grimes 	struct mtop mb;
7094b88c807SRodney W. Grimes 
7104b88c807SRodney W. Grimes 	/*
711*0266a5d6SDag-Erling Smørgrav 	 * Fail resync attempts at user request (done) or if this is going to be
7129d5abbddSJens Schweikhardt 	 * an update/append to an existing archive. If last i/o hit media end,
7139d5abbddSJens Schweikhardt 	 * we need to go to the next volume not try a resync.
7144b88c807SRodney W. Grimes 	 */
7154b88c807SRodney W. Grimes 	if ((done > 0) || (lstrval == 0))
7164b88c807SRodney W. Grimes 		return(-1);
7174b88c807SRodney W. Grimes 
7184b88c807SRodney W. Grimes 	if ((act == APPND) || (act == ARCHIVE)) {
719778766feSKris Kennaway 		paxwarn(1, "Cannot allow updates to an archive with flaws.");
7204b88c807SRodney W. Grimes 		return(-1);
7214b88c807SRodney W. Grimes 	}
7224b88c807SRodney W. Grimes 	if (io_ok)
7234b88c807SRodney W. Grimes 		did_io = 1;
7244b88c807SRodney W. Grimes 
7254b88c807SRodney W. Grimes 	switch(artyp) {
7264b88c807SRodney W. Grimes 	case ISTAPE:
7274b88c807SRodney W. Grimes 		/*
7284b88c807SRodney W. Grimes 		 * if the last i/o was a successful data transfer, we assume
7294b88c807SRodney W. Grimes 		 * the fault is just a bad record on the tape that we are now
7304b88c807SRodney W. Grimes 		 * past. If we did not get any data since the last resync try
73146be34b9SKris Kennaway 		 * to move the tape forward one PHYSICAL record past any
7324b88c807SRodney W. Grimes 		 * damaged tape section. Some tape drives are stubborn and need
7334b88c807SRodney W. Grimes 		 * to be pushed.
7344b88c807SRodney W. Grimes 		 */
7354b88c807SRodney W. Grimes 		if (io_ok) {
7364b88c807SRodney W. Grimes 			io_ok = 0;
7374b88c807SRodney W. Grimes 			lstrval = 1;
7384b88c807SRodney W. Grimes 			break;
7394b88c807SRodney W. Grimes 		}
7404b88c807SRodney W. Grimes 		mb.mt_op = MTFSR;
7414b88c807SRodney W. Grimes 		mb.mt_count = 1;
7424b88c807SRodney W. Grimes 		if (ioctl(arfd, MTIOCTOP, &mb) < 0)
7434b88c807SRodney W. Grimes 			break;
7444b88c807SRodney W. Grimes 		lstrval = 1;
7454b88c807SRodney W. Grimes 		break;
7464b88c807SRodney W. Grimes 	case ISREG:
7474b88c807SRodney W. Grimes 	case ISCHR:
7484b88c807SRodney W. Grimes 	case ISBLK:
7494b88c807SRodney W. Grimes 		/*
7504b88c807SRodney W. Grimes 		 * try to step over the bad part of the device.
7514b88c807SRodney W. Grimes 		 */
7524b88c807SRodney W. Grimes 		io_ok = 0;
7534b88c807SRodney W. Grimes 		if (((fsbz = arsb.st_blksize) <= 0) || (artyp != ISREG))
7544b88c807SRodney W. Grimes 			fsbz = BLKMULT;
7554b88c807SRodney W. Grimes 		if ((cpos = lseek(arfd, (off_t)0L, SEEK_CUR)) < 0)
7564b88c807SRodney W. Grimes 			break;
7574b88c807SRodney W. Grimes 		mpos = fsbz - (cpos % (off_t)fsbz);
7584b88c807SRodney W. Grimes 		if (lseek(arfd, mpos, SEEK_CUR) < 0)
7594b88c807SRodney W. Grimes 			break;
7604b88c807SRodney W. Grimes 		lstrval = 1;
7614b88c807SRodney W. Grimes 		break;
7624b88c807SRodney W. Grimes 	case ISPIPE:
7634b88c807SRodney W. Grimes 	default:
7644b88c807SRodney W. Grimes 		/*
7654b88c807SRodney W. Grimes 		 * cannot recover on these archive device types
7664b88c807SRodney W. Grimes 		 */
7674b88c807SRodney W. Grimes 		io_ok = 0;
7684b88c807SRodney W. Grimes 		break;
7694b88c807SRodney W. Grimes 	}
7704b88c807SRodney W. Grimes 	if (lstrval <= 0) {
771778766feSKris Kennaway 		paxwarn(1, "Unable to recover from an archive read failure.");
7724b88c807SRodney W. Grimes 		return(-1);
7734b88c807SRodney W. Grimes 	}
774778766feSKris Kennaway 	paxwarn(0, "Attempting to recover from an archive read failure.");
7754b88c807SRodney W. Grimes 	return(0);
7764b88c807SRodney W. Grimes }
7774b88c807SRodney W. Grimes 
7784b88c807SRodney W. Grimes /*
7794b88c807SRodney W. Grimes  * ar_fow()
78001c99176SUlrich Spörlein  *	Move the I/O position within the archive forward the specified number of
7814b88c807SRodney W. Grimes  *	bytes as supported by the device. If we cannot move the requested
7824b88c807SRodney W. Grimes  *	number of bytes, return the actual number of bytes moved in skipped.
7834b88c807SRodney W. Grimes  * Return:
7844b88c807SRodney W. Grimes  *	0 if moved the requested distance, -1 on complete failure, 1 on
7854b88c807SRodney W. Grimes  *	partial move (the amount moved is in skipped)
7864b88c807SRodney W. Grimes  */
7874b88c807SRodney W. Grimes 
7884b88c807SRodney W. Grimes int
ar_fow(off_t sksz,off_t * skipped)7894b88c807SRodney W. Grimes ar_fow(off_t sksz, off_t *skipped)
7904b88c807SRodney W. Grimes {
7914b88c807SRodney W. Grimes 	off_t cpos;
7924b88c807SRodney W. Grimes 	off_t mpos;
7934b88c807SRodney W. Grimes 
7944b88c807SRodney W. Grimes 	*skipped = 0;
7954b88c807SRodney W. Grimes 	if (sksz <= 0)
7964b88c807SRodney W. Grimes 		return(0);
7974b88c807SRodney W. Grimes 
7984b88c807SRodney W. Grimes 	/*
79901c99176SUlrich Spörlein 	 * we cannot move forward at EOF or error
8004b88c807SRodney W. Grimes 	 */
8014b88c807SRodney W. Grimes 	if (lstrval <= 0)
8024b88c807SRodney W. Grimes 		return(lstrval);
8034b88c807SRodney W. Grimes 
8044b88c807SRodney W. Grimes 	/*
8054b88c807SRodney W. Grimes 	 * Safer to read forward on devices where it is hard to find the end of
8064b88c807SRodney W. Grimes 	 * the media without reading to it. With tapes we cannot be sure of the
8074b88c807SRodney W. Grimes 	 * number of physical blocks to skip (we do not know physical block
80801c99176SUlrich Spörlein 	 * size at this point), so we must only read forward on tapes!
8094b88c807SRodney W. Grimes 	 */
8104b88c807SRodney W. Grimes 	if (artyp != ISREG)
8114b88c807SRodney W. Grimes 		return(0);
8124b88c807SRodney W. Grimes 
8134b88c807SRodney W. Grimes 	/*
8144b88c807SRodney W. Grimes 	 * figure out where we are in the archive
8154b88c807SRodney W. Grimes 	 */
8164b88c807SRodney W. Grimes 	if ((cpos = lseek(arfd, (off_t)0L, SEEK_CUR)) >= 0) {
8174b88c807SRodney W. Grimes 		/*
8184b88c807SRodney W. Grimes 	 	 * we can be asked to move farther than there are bytes in this
8194b88c807SRodney W. Grimes 		 * volume, if so, just go to file end and let normal buf_fill()
8204b88c807SRodney W. Grimes 		 * deal with the end of file (it will go to next volume by
8214b88c807SRodney W. Grimes 		 * itself)
8224b88c807SRodney W. Grimes 	 	 */
8234b88c807SRodney W. Grimes 		if ((mpos = cpos + sksz) > arsb.st_size) {
8244b88c807SRodney W. Grimes 			*skipped = arsb.st_size - cpos;
8254b88c807SRodney W. Grimes 			mpos = arsb.st_size;
8264b88c807SRodney W. Grimes 		} else
8274b88c807SRodney W. Grimes 			*skipped = sksz;
8284b88c807SRodney W. Grimes 		if (lseek(arfd, mpos, SEEK_SET) >= 0)
8294b88c807SRodney W. Grimes 			return(0);
8304b88c807SRodney W. Grimes 	}
831778766feSKris Kennaway 	syswarn(1, errno, "Forward positioning operation on archive failed");
8324b88c807SRodney W. Grimes 	lstrval = -1;
8334b88c807SRodney W. Grimes 	return(-1);
8344b88c807SRodney W. Grimes }
8354b88c807SRodney W. Grimes 
8364b88c807SRodney W. Grimes /*
8374b88c807SRodney W. Grimes  * ar_rev()
8384b88c807SRodney W. Grimes  *	move the i/o position within the archive backwards the specified byte
8394b88c807SRodney W. Grimes  *	count as supported by the device. With tapes drives we RESET rdblksz to
8404b88c807SRodney W. Grimes  *	the PHYSICAL blocksize.
8414b88c807SRodney W. Grimes  *	NOTE: We should only be called to move backwards so we can rewrite the
8424b88c807SRodney W. Grimes  *	last records (the trailer) of an archive (APPEND).
8434b88c807SRodney W. Grimes  * Return:
8444b88c807SRodney W. Grimes  *	0 if moved the requested distance, -1 on complete failure
8454b88c807SRodney W. Grimes  */
8464b88c807SRodney W. Grimes 
8474b88c807SRodney W. Grimes int
ar_rev(off_t sksz)8484b88c807SRodney W. Grimes ar_rev(off_t sksz)
8494b88c807SRodney W. Grimes {
8504b88c807SRodney W. Grimes 	off_t cpos;
8514b88c807SRodney W. Grimes 	struct mtop mb;
852f789b261SWarner Losh 	int phyblk;
8534b88c807SRodney W. Grimes 
8544b88c807SRodney W. Grimes 	/*
8554b88c807SRodney W. Grimes 	 * make sure we do not have try to reverse on a flawed archive
8564b88c807SRodney W. Grimes 	 */
8574b88c807SRodney W. Grimes 	if (lstrval < 0)
8584b88c807SRodney W. Grimes 		return(lstrval);
8594b88c807SRodney W. Grimes 
8604b88c807SRodney W. Grimes 	switch(artyp) {
8614b88c807SRodney W. Grimes 	case ISPIPE:
8624b88c807SRodney W. Grimes 		if (sksz <= 0)
8634b88c807SRodney W. Grimes 			break;
8644b88c807SRodney W. Grimes 		/*
8654b88c807SRodney W. Grimes 		 * cannot go backwards on these critters
8664b88c807SRodney W. Grimes 		 */
867778766feSKris Kennaway 		paxwarn(1, "Reverse positioning on pipes is not supported.");
8684b88c807SRodney W. Grimes 		lstrval = -1;
8694b88c807SRodney W. Grimes 		return(-1);
8704b88c807SRodney W. Grimes 	case ISREG:
8714b88c807SRodney W. Grimes 	case ISBLK:
8724b88c807SRodney W. Grimes 	case ISCHR:
8734b88c807SRodney W. Grimes 	default:
8744b88c807SRodney W. Grimes 		if (sksz <= 0)
8754b88c807SRodney W. Grimes 			break;
8764b88c807SRodney W. Grimes 
8774b88c807SRodney W. Grimes 		/*
8784b88c807SRodney W. Grimes 		 * For things other than files, backwards movement has a very
8794b88c807SRodney W. Grimes 		 * high probability of failure as we really do not know the
8804b88c807SRodney W. Grimes 		 * true attributes of the device we are talking to (the device
8814b88c807SRodney W. Grimes 		 * may not even have the ability to lseek() in any direction).
8824b88c807SRodney W. Grimes 		 * First we figure out where we are in the archive.
8834b88c807SRodney W. Grimes 		 */
8844b88c807SRodney W. Grimes 		if ((cpos = lseek(arfd, (off_t)0L, SEEK_CUR)) < 0) {
885778766feSKris Kennaway 			syswarn(1, errno,
8864b88c807SRodney W. Grimes 			   "Unable to obtain current archive byte offset");
8874b88c807SRodney W. Grimes 			lstrval = -1;
8884b88c807SRodney W. Grimes 			return(-1);
8894b88c807SRodney W. Grimes 		}
8904b88c807SRodney W. Grimes 
8914b88c807SRodney W. Grimes 		/*
8924b88c807SRodney W. Grimes 		 * we may try to go backwards past the start when the archive
89301c99176SUlrich Spörlein 		 * is only a single record. If this happens and we are on a
8944b88c807SRodney W. Grimes 		 * multi volume archive, we need to go to the end of the
8954b88c807SRodney W. Grimes 		 * previous volume and continue our movement backwards from
8964b88c807SRodney W. Grimes 		 * there.
8974b88c807SRodney W. Grimes 		 */
8984b88c807SRodney W. Grimes 		if ((cpos -= sksz) < (off_t)0L) {
8994b88c807SRodney W. Grimes 			if (arvol > 1) {
9004b88c807SRodney W. Grimes 				/*
9014b88c807SRodney W. Grimes 				 * this should never happen
9024b88c807SRodney W. Grimes 				 */
903778766feSKris Kennaway 				paxwarn(1,"Reverse position on previous volume.");
9044b88c807SRodney W. Grimes 				lstrval = -1;
9054b88c807SRodney W. Grimes 				return(-1);
9064b88c807SRodney W. Grimes 			}
9074b88c807SRodney W. Grimes 			cpos = (off_t)0L;
9084b88c807SRodney W. Grimes 		}
9094b88c807SRodney W. Grimes 		if (lseek(arfd, cpos, SEEK_SET) < 0) {
910778766feSKris Kennaway 			syswarn(1, errno, "Unable to seek archive backwards");
9114b88c807SRodney W. Grimes 			lstrval = -1;
9124b88c807SRodney W. Grimes 			return(-1);
9134b88c807SRodney W. Grimes 		}
9144b88c807SRodney W. Grimes 		break;
9154b88c807SRodney W. Grimes 	case ISTAPE:
9164b88c807SRodney W. Grimes 		/*
9174b88c807SRodney W. Grimes 		 * Calculate and move the proper number of PHYSICAL tape
9184b88c807SRodney W. Grimes 		 * blocks. If the sksz is not an even multiple of the physical
9194b88c807SRodney W. Grimes 		 * tape size, we cannot do the move (this should never happen).
920*0266a5d6SDag-Erling Smørgrav 		 * (We also cannot handle trailers spread over two vols).
9214b88c807SRodney W. Grimes 		 * get_phys() also makes sure we are in front of the filemark.
9224b88c807SRodney W. Grimes 		 */
9234b88c807SRodney W. Grimes 		if ((phyblk = get_phys()) <= 0) {
9244b88c807SRodney W. Grimes 			lstrval = -1;
9254b88c807SRodney W. Grimes 			return(-1);
9264b88c807SRodney W. Grimes 		}
9274b88c807SRodney W. Grimes 
9284b88c807SRodney W. Grimes 		/*
9294b88c807SRodney W. Grimes 		 * make sure future tape reads only go by physical tape block
9304b88c807SRodney W. Grimes 		 * size (set rdblksz to the real size).
9314b88c807SRodney W. Grimes 		 */
9324b88c807SRodney W. Grimes 		rdblksz = phyblk;
9334b88c807SRodney W. Grimes 
9344b88c807SRodney W. Grimes 		/*
9354b88c807SRodney W. Grimes 		 * if no movement is required, just return (we must be after
9364b88c807SRodney W. Grimes 		 * get_phys() so the physical blocksize is properly set)
9374b88c807SRodney W. Grimes 		 */
9384b88c807SRodney W. Grimes 		if (sksz <= 0)
9394b88c807SRodney W. Grimes 			break;
9404b88c807SRodney W. Grimes 
9414b88c807SRodney W. Grimes 		/*
9424b88c807SRodney W. Grimes 		 * ok we have to move. Make sure the tape drive can do it.
9434b88c807SRodney W. Grimes 		 */
9444b88c807SRodney W. Grimes 		if (sksz % phyblk) {
945778766feSKris Kennaway 			paxwarn(1,
9464b88c807SRodney W. Grimes 			    "Tape drive unable to backspace requested amount");
9474b88c807SRodney W. Grimes 			lstrval = -1;
9484b88c807SRodney W. Grimes 			return(-1);
9494b88c807SRodney W. Grimes 		}
9504b88c807SRodney W. Grimes 
9514b88c807SRodney W. Grimes 		/*
9524b88c807SRodney W. Grimes 		 * move backwards the requested number of bytes
9534b88c807SRodney W. Grimes 		 */
9544b88c807SRodney W. Grimes 		mb.mt_op = MTBSR;
9554b88c807SRodney W. Grimes 		mb.mt_count = sksz/phyblk;
9564b88c807SRodney W. Grimes 		if (ioctl(arfd, MTIOCTOP, &mb) < 0) {
957778766feSKris Kennaway 			syswarn(1,errno, "Unable to backspace tape %d blocks.",
9584b88c807SRodney W. Grimes 			    mb.mt_count);
9594b88c807SRodney W. Grimes 			lstrval = -1;
9604b88c807SRodney W. Grimes 			return(-1);
9614b88c807SRodney W. Grimes 		}
9624b88c807SRodney W. Grimes 		break;
9634b88c807SRodney W. Grimes 	}
9644b88c807SRodney W. Grimes 	lstrval = 1;
9654b88c807SRodney W. Grimes 	return(0);
9664b88c807SRodney W. Grimes }
9674b88c807SRodney W. Grimes 
9684b88c807SRodney W. Grimes /*
9694b88c807SRodney W. Grimes  * get_phys()
9704b88c807SRodney W. Grimes  *	Determine the physical block size on a tape drive. We need the physical
9714b88c807SRodney W. Grimes  *	block size so we know how many bytes we skip over when we move with
9724b88c807SRodney W. Grimes  *	mtio commands. We also make sure we are BEFORE THE TAPE FILEMARK when
9734b88c807SRodney W. Grimes  *	return.
9744b88c807SRodney W. Grimes  *	This is one really SLOW routine...
9754b88c807SRodney W. Grimes  * Return:
9764b88c807SRodney W. Grimes  *	physical block size if ok (ok > 0), -1 otherwise
9774b88c807SRodney W. Grimes  */
9784b88c807SRodney W. Grimes 
9794b88c807SRodney W. Grimes static int
get_phys(void)9804b88c807SRodney W. Grimes get_phys(void)
9814b88c807SRodney W. Grimes {
982f789b261SWarner Losh 	int padsz = 0;
983f789b261SWarner Losh 	int res;
984f789b261SWarner Losh 	int phyblk;
9854b88c807SRodney W. Grimes 	struct mtop mb;
9864b88c807SRodney W. Grimes 	char scbuf[MAXBLK];
9874b88c807SRodney W. Grimes 
9884b88c807SRodney W. Grimes 	/*
9894b88c807SRodney W. Grimes 	 * move to the file mark, and then back up one record and read it.
9904b88c807SRodney W. Grimes 	 * this should tell us the physical record size the tape is using.
9914b88c807SRodney W. Grimes 	 */
9924b88c807SRodney W. Grimes 	if (lstrval == 1) {
9934b88c807SRodney W. Grimes 		/*
9944b88c807SRodney W. Grimes 		 * we know we are at file mark when we get back a 0 from
9954b88c807SRodney W. Grimes 		 * read()
9964b88c807SRodney W. Grimes 		 */
9974b88c807SRodney W. Grimes 		while ((res = read(arfd, scbuf, sizeof(scbuf))) > 0)
9984b88c807SRodney W. Grimes 			padsz += res;
9994b88c807SRodney W. Grimes 		if (res < 0) {
1000778766feSKris Kennaway 			syswarn(1, errno, "Unable to locate tape filemark.");
10014b88c807SRodney W. Grimes 			return(-1);
10024b88c807SRodney W. Grimes 		}
10034b88c807SRodney W. Grimes 	}
10044b88c807SRodney W. Grimes 
10054b88c807SRodney W. Grimes 	/*
10064b88c807SRodney W. Grimes 	 * move backwards over the file mark so we are at the end of the
10074b88c807SRodney W. Grimes 	 * last record.
10084b88c807SRodney W. Grimes 	 */
10094b88c807SRodney W. Grimes 	mb.mt_op = MTBSF;
10104b88c807SRodney W. Grimes 	mb.mt_count = 1;
10114b88c807SRodney W. Grimes 	if (ioctl(arfd, MTIOCTOP, &mb) < 0) {
1012778766feSKris Kennaway 		syswarn(1, errno, "Unable to backspace over tape filemark.");
10134b88c807SRodney W. Grimes 		return(-1);
10144b88c807SRodney W. Grimes 	}
10154b88c807SRodney W. Grimes 
10164b88c807SRodney W. Grimes 	/*
10174b88c807SRodney W. Grimes 	 * move backwards so we are in front of the last record and read it to
10184b88c807SRodney W. Grimes 	 * get physical tape blocksize.
10194b88c807SRodney W. Grimes 	 */
10204b88c807SRodney W. Grimes 	mb.mt_op = MTBSR;
10214b88c807SRodney W. Grimes 	mb.mt_count = 1;
10224b88c807SRodney W. Grimes 	if (ioctl(arfd, MTIOCTOP, &mb) < 0) {
1023778766feSKris Kennaway 		syswarn(1, errno, "Unable to backspace over last tape block.");
10244b88c807SRodney W. Grimes 		return(-1);
10254b88c807SRodney W. Grimes 	}
10264b88c807SRodney W. Grimes 	if ((phyblk = read(arfd, scbuf, sizeof(scbuf))) <= 0) {
1027778766feSKris Kennaway 		syswarn(1, errno, "Cannot determine archive tape blocksize.");
10284b88c807SRodney W. Grimes 		return(-1);
10294b88c807SRodney W. Grimes 	}
10304b88c807SRodney W. Grimes 
10314b88c807SRodney W. Grimes 	/*
103201c99176SUlrich Spörlein 	 * read forward to the file mark, then back up in front of the filemark
10334b88c807SRodney W. Grimes 	 * (this is a bit paranoid, but should be safe to do).
10344b88c807SRodney W. Grimes 	 */
10354b88c807SRodney W. Grimes 	while ((res = read(arfd, scbuf, sizeof(scbuf))) > 0)
10364b88c807SRodney W. Grimes 		;
10374b88c807SRodney W. Grimes 	if (res < 0) {
1038778766feSKris Kennaway 		syswarn(1, errno, "Unable to locate tape filemark.");
10394b88c807SRodney W. Grimes 		return(-1);
10404b88c807SRodney W. Grimes 	}
10414b88c807SRodney W. Grimes 	mb.mt_op = MTBSF;
10424b88c807SRodney W. Grimes 	mb.mt_count = 1;
10434b88c807SRodney W. Grimes 	if (ioctl(arfd, MTIOCTOP, &mb) < 0) {
1044778766feSKris Kennaway 		syswarn(1, errno, "Unable to backspace over tape filemark.");
10454b88c807SRodney W. Grimes 		return(-1);
10464b88c807SRodney W. Grimes 	}
10474b88c807SRodney W. Grimes 
10484b88c807SRodney W. Grimes 	/*
10494b88c807SRodney W. Grimes 	 * set lstrval so we know that the filemark has not been seen
10504b88c807SRodney W. Grimes 	 */
10514b88c807SRodney W. Grimes 	lstrval = 1;
10524b88c807SRodney W. Grimes 
10534b88c807SRodney W. Grimes 	/*
10544b88c807SRodney W. Grimes 	 * return if there was no padding
10554b88c807SRodney W. Grimes 	 */
10564b88c807SRodney W. Grimes 	if (padsz == 0)
10574b88c807SRodney W. Grimes 		return(phyblk);
10584b88c807SRodney W. Grimes 
10594b88c807SRodney W. Grimes 	/*
10604b88c807SRodney W. Grimes 	 * make sure we can move backwards over the padding. (this should
10614b88c807SRodney W. Grimes 	 * never fail).
10624b88c807SRodney W. Grimes 	 */
10634b88c807SRodney W. Grimes 	if (padsz % phyblk) {
1064778766feSKris Kennaway 		paxwarn(1, "Tape drive unable to backspace requested amount");
10654b88c807SRodney W. Grimes 		return(-1);
10664b88c807SRodney W. Grimes 	}
10674b88c807SRodney W. Grimes 
10684b88c807SRodney W. Grimes 	/*
10694b88c807SRodney W. Grimes 	 * move backwards over the padding so the head is where it was when
10704b88c807SRodney W. Grimes 	 * we were first called (if required).
10714b88c807SRodney W. Grimes 	 */
10724b88c807SRodney W. Grimes 	mb.mt_op = MTBSR;
10734b88c807SRodney W. Grimes 	mb.mt_count = padsz/phyblk;
10744b88c807SRodney W. Grimes 	if (ioctl(arfd, MTIOCTOP, &mb) < 0) {
1075778766feSKris Kennaway 		syswarn(1,errno,"Unable to backspace tape over %d pad blocks",
10764b88c807SRodney W. Grimes 		    mb.mt_count);
10774b88c807SRodney W. Grimes 		return(-1);
10784b88c807SRodney W. Grimes 	}
10794b88c807SRodney W. Grimes 	return(phyblk);
10804b88c807SRodney W. Grimes }
10814b88c807SRodney W. Grimes 
10824b88c807SRodney W. Grimes /*
10834b88c807SRodney W. Grimes  * ar_next()
10844b88c807SRodney W. Grimes  *	prompts the user for the next volume in this archive. For some devices
10854b88c807SRodney W. Grimes  *	we may allow the media to be changed. Otherwise a new archive is
10864b88c807SRodney W. Grimes  *	prompted for. By pax spec, if there is no controlling tty or an eof is
10874b88c807SRodney W. Grimes  *	read on tty input, we must quit pax.
10884b88c807SRodney W. Grimes  * Return:
10894b88c807SRodney W. Grimes  *	0 when ready to continue, -1 when all done
10904b88c807SRodney W. Grimes  */
10914b88c807SRodney W. Grimes 
10924b88c807SRodney W. Grimes int
ar_next(void)10934b88c807SRodney W. Grimes ar_next(void)
10944b88c807SRodney W. Grimes {
1095367787a3SBrian Somers 	static char *arcbuf;
10964b88c807SRodney W. Grimes 	char buf[PAXPATHLEN+2];
10974b88c807SRodney W. Grimes 	sigset_t o_mask;
10984b88c807SRodney W. Grimes 
10994b88c807SRodney W. Grimes 	/*
11004b88c807SRodney W. Grimes 	 * WE MUST CLOSE THE DEVICE. A lot of devices must see last close, (so
11014b88c807SRodney W. Grimes 	 * things like writing EOF etc will be done) (Watch out ar_close() can
11024b88c807SRodney W. Grimes 	 * also be called via a signal handler, so we must prevent a race.
11034b88c807SRodney W. Grimes 	 */
11044b88c807SRodney W. Grimes 	if (sigprocmask(SIG_BLOCK, &s_mask, &o_mask) < 0)
1105778766feSKris Kennaway 		syswarn(0, errno, "Unable to set signal mask");
11064b88c807SRodney W. Grimes 	ar_close();
1107778766feSKris Kennaway 	if (sigprocmask(SIG_SETMASK, &o_mask, NULL) < 0)
1108778766feSKris Kennaway 		syswarn(0, errno, "Unable to restore signal mask");
11094b88c807SRodney W. Grimes 
1110d6e1f8d7SEitan Adler 	if (done || !wr_trail || Oflag || strcmp(NM_TAR, argv0) == 0)
11114b88c807SRodney W. Grimes 		return(-1);
11124b88c807SRodney W. Grimes 
11134b88c807SRodney W. Grimes 	tty_prnt("\nATTENTION! %s archive volume change required.\n", argv0);
11144b88c807SRodney W. Grimes 
11154b88c807SRodney W. Grimes 	/*
11164b88c807SRodney W. Grimes 	 * if i/o is on stdin or stdout, we cannot reopen it (we do not know
11174b88c807SRodney W. Grimes 	 * the name), the user will be forced to type it in.
11184b88c807SRodney W. Grimes 	 */
111940feca3aSMark Murray 	if (strcmp(arcname, stdo) && strcmp(arcname, stdn) && (artyp != ISREG)
11204b88c807SRodney W. Grimes 	    && (artyp != ISPIPE)) {
11214b88c807SRodney W. Grimes 		if (artyp == ISTAPE) {
11224b88c807SRodney W. Grimes 			tty_prnt("%s ready for archive tape volume: %d\n",
11234b88c807SRodney W. Grimes 				arcname, arvol);
11244b88c807SRodney W. Grimes 			tty_prnt("Load the NEXT TAPE on the tape drive");
11254b88c807SRodney W. Grimes 		} else {
11264b88c807SRodney W. Grimes 			tty_prnt("%s ready for archive volume: %d\n",
11274b88c807SRodney W. Grimes 				arcname, arvol);
11284b88c807SRodney W. Grimes 			tty_prnt("Load the NEXT STORAGE MEDIA (if required)");
11294b88c807SRodney W. Grimes 		}
11304b88c807SRodney W. Grimes 
11314b88c807SRodney W. Grimes 		if ((act == ARCHIVE) || (act == APPND))
11324b88c807SRodney W. Grimes 			tty_prnt(" and make sure it is WRITE ENABLED.\n");
11334b88c807SRodney W. Grimes 		else
11344b88c807SRodney W. Grimes 			tty_prnt("\n");
11354b88c807SRodney W. Grimes 
11364b88c807SRodney W. Grimes 		for(;;) {
11374b88c807SRodney W. Grimes 			tty_prnt("Type \"y\" to continue, \".\" to quit %s,",
11384b88c807SRodney W. Grimes 				argv0);
11394b88c807SRodney W. Grimes 			tty_prnt(" or \"s\" to switch to new device.\nIf you");
11404b88c807SRodney W. Grimes 			tty_prnt(" cannot change storage media, type \"s\"\n");
11414b88c807SRodney W. Grimes 			tty_prnt("Is the device ready and online? > ");
11424b88c807SRodney W. Grimes 
11434b88c807SRodney W. Grimes 			if ((tty_read(buf,sizeof(buf))<0) || !strcmp(buf,".")){
11444b88c807SRodney W. Grimes 				done = 1;
11454b88c807SRodney W. Grimes 				lstrval = -1;
11464b88c807SRodney W. Grimes 				tty_prnt("Quitting %s!\n", argv0);
11474b88c807SRodney W. Grimes 				vfpart = 0;
11484b88c807SRodney W. Grimes 				return(-1);
11494b88c807SRodney W. Grimes 			}
11504b88c807SRodney W. Grimes 
11514b88c807SRodney W. Grimes 			if ((buf[0] == '\0') || (buf[1] != '\0')) {
11524b88c807SRodney W. Grimes 				tty_prnt("%s unknown command, try again\n",buf);
11534b88c807SRodney W. Grimes 				continue;
11544b88c807SRodney W. Grimes 			}
11554b88c807SRodney W. Grimes 
11564b88c807SRodney W. Grimes 			switch (buf[0]) {
11574b88c807SRodney W. Grimes 			case 'y':
11584b88c807SRodney W. Grimes 			case 'Y':
11594b88c807SRodney W. Grimes 				/*
11604b88c807SRodney W. Grimes 				 * we are to continue with the same device
11614b88c807SRodney W. Grimes 				 */
11624b88c807SRodney W. Grimes 				if (ar_open(arcname) >= 0)
11634b88c807SRodney W. Grimes 					return(0);
11644b88c807SRodney W. Grimes 				tty_prnt("Cannot re-open %s, try again\n",
11654b88c807SRodney W. Grimes 					arcname);
11664b88c807SRodney W. Grimes 				continue;
11674b88c807SRodney W. Grimes 			case 's':
11684b88c807SRodney W. Grimes 			case 'S':
11694b88c807SRodney W. Grimes 				/*
11704b88c807SRodney W. Grimes 				 * user wants to open a different device
11714b88c807SRodney W. Grimes 				 */
11724b88c807SRodney W. Grimes 				tty_prnt("Switching to a different archive\n");
11734b88c807SRodney W. Grimes 				break;
11744b88c807SRodney W. Grimes 			default:
11754b88c807SRodney W. Grimes 				tty_prnt("%s unknown command, try again\n",buf);
11764b88c807SRodney W. Grimes 				continue;
11774b88c807SRodney W. Grimes 			}
11784b88c807SRodney W. Grimes 			break;
11794b88c807SRodney W. Grimes 		}
11804b88c807SRodney W. Grimes 	} else
11814b88c807SRodney W. Grimes 		tty_prnt("Ready for archive volume: %d\n", arvol);
11824b88c807SRodney W. Grimes 
11834b88c807SRodney W. Grimes 	/*
11844b88c807SRodney W. Grimes 	 * have to go to a different archive
11854b88c807SRodney W. Grimes 	 */
11864b88c807SRodney W. Grimes 	for (;;) {
11874b88c807SRodney W. Grimes 		tty_prnt("Input archive name or \".\" to quit %s.\n", argv0);
11884b88c807SRodney W. Grimes 		tty_prnt("Archive name > ");
11894b88c807SRodney W. Grimes 
11904b88c807SRodney W. Grimes 		if ((tty_read(buf, sizeof(buf)) < 0) || !strcmp(buf, ".")) {
11914b88c807SRodney W. Grimes 			done = 1;
11924b88c807SRodney W. Grimes 			lstrval = -1;
11934b88c807SRodney W. Grimes 			tty_prnt("Quitting %s!\n", argv0);
11944b88c807SRodney W. Grimes 			vfpart = 0;
11954b88c807SRodney W. Grimes 			return(-1);
11964b88c807SRodney W. Grimes 		}
11974b88c807SRodney W. Grimes 		if (buf[0] == '\0') {
11984b88c807SRodney W. Grimes 			tty_prnt("Empty file name, try again\n");
11994b88c807SRodney W. Grimes 			continue;
12004b88c807SRodney W. Grimes 		}
12014b88c807SRodney W. Grimes 		if (!strcmp(buf, "..")) {
12024b88c807SRodney W. Grimes 			tty_prnt("Illegal file name: .. try again\n");
12034b88c807SRodney W. Grimes 			continue;
12044b88c807SRodney W. Grimes 		}
12054b88c807SRodney W. Grimes 		if (strlen(buf) > PAXPATHLEN) {
12064b88c807SRodney W. Grimes 			tty_prnt("File name too long, try again\n");
12074b88c807SRodney W. Grimes 			continue;
12084b88c807SRodney W. Grimes 		}
12094b88c807SRodney W. Grimes 
12104b88c807SRodney W. Grimes 		/*
12114b88c807SRodney W. Grimes 		 * try to open new archive
12124b88c807SRodney W. Grimes 		 */
12134b88c807SRodney W. Grimes 		if (ar_open(buf) >= 0) {
1214367787a3SBrian Somers 			free(arcbuf);
1215367787a3SBrian Somers 			if ((arcbuf = strdup(buf)) == NULL) {
12164b88c807SRodney W. Grimes 				done = 1;
12174b88c807SRodney W. Grimes 				lstrval = -1;
1218778766feSKris Kennaway 				paxwarn(0, "Cannot save archive name.");
12194b88c807SRodney W. Grimes 				return(-1);
12204b88c807SRodney W. Grimes 			}
1221367787a3SBrian Somers 			arcname = arcbuf;
12224b88c807SRodney W. Grimes 			break;
12234b88c807SRodney W. Grimes 		}
12244b88c807SRodney W. Grimes 		tty_prnt("Cannot open %s, try again\n", buf);
12254b88c807SRodney W. Grimes 		continue;
12264b88c807SRodney W. Grimes 	}
12274b88c807SRodney W. Grimes 	return(0);
12284b88c807SRodney W. Grimes }
12291192d531SKris Kennaway 
12301192d531SKris Kennaway /*
12311192d531SKris Kennaway  * ar_start_gzip()
12321192d531SKris Kennaway  * starts the gzip compression/decompression process as a child, using magic
12331192d531SKris Kennaway  * to keep the fd the same in the calling function (parent).
12341192d531SKris Kennaway  */
12351192d531SKris Kennaway void
ar_start_gzip(int fd,const char * gzip_prog,int wr)123640feca3aSMark Murray ar_start_gzip(int fd, const char *gzip_prog, int wr)
12371192d531SKris Kennaway {
12381192d531SKris Kennaway 	int fds[2];
123940feca3aSMark Murray 	const char *gzip_flags;
12401192d531SKris Kennaway 
12411192d531SKris Kennaway 	if (pipe(fds) < 0)
12421192d531SKris Kennaway 		err(1, "could not pipe");
12431192d531SKris Kennaway 	zpid = fork();
12441192d531SKris Kennaway 	if (zpid < 0)
12451192d531SKris Kennaway 		err(1, "could not fork");
12461192d531SKris Kennaway 
12471192d531SKris Kennaway 	/* parent */
12481192d531SKris Kennaway 	if (zpid) {
12491192d531SKris Kennaway 		if (wr)
12501192d531SKris Kennaway 			dup2(fds[1], fd);
12511192d531SKris Kennaway 		else
12521192d531SKris Kennaway 			dup2(fds[0], fd);
12531192d531SKris Kennaway 		close(fds[0]);
12541192d531SKris Kennaway 		close(fds[1]);
12551192d531SKris Kennaway 	} else {
12561192d531SKris Kennaway 		if (wr) {
12571192d531SKris Kennaway 			dup2(fds[0], STDIN_FILENO);
12581192d531SKris Kennaway 			dup2(fd, STDOUT_FILENO);
12591192d531SKris Kennaway 			gzip_flags = "-c";
12601192d531SKris Kennaway 		} else {
12611192d531SKris Kennaway 			dup2(fds[1], STDOUT_FILENO);
12621192d531SKris Kennaway 			dup2(fd, STDIN_FILENO);
12631192d531SKris Kennaway 			gzip_flags = "-dc";
12641192d531SKris Kennaway 		}
12651192d531SKris Kennaway 		close(fds[0]);
12661192d531SKris Kennaway 		close(fds[1]);
126740feca3aSMark Murray 		if (execlp(gzip_prog, gzip_prog, gzip_flags,
12687bc6d015SBrian Somers 		    (char *)NULL) < 0)
12691192d531SKris Kennaway 			err(1, "could not exec");
12701192d531SKris Kennaway 		/* NOTREACHED */
12711192d531SKris Kennaway 	}
12721192d531SKris Kennaway }
1273