xref: /freebsd/bin/pax/ar_io.c (revision 68e7a217f8019b955f87547f218e95ab237597af)
1 /*-
2  * Copyright (c) 1992 Keith Muller.
3  * Copyright (c) 1992, 1993
4  *	The Regents of the University of California.  All rights reserved.
5  *
6  * This code is derived from software contributed to Berkeley by
7  * Keith Muller of the University of California, San Diego.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *	This product includes software developed by the University of
20  *	California, Berkeley and its contributors.
21  * 4. Neither the name of the University nor the names of its contributors
22  *    may be used to endorse or promote products derived from this software
23  *    without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  */
37 
38 #ifndef lint
39 #if 0
40 static char sccsid[] = "@(#)ar_io.c	8.2 (Berkeley) 4/18/94";
41 #endif
42 static const char rcsid[] =
43   "$FreeBSD$";
44 #endif /* not lint */
45 
46 #include <sys/types.h>
47 #include <sys/ioctl.h>
48 #include <sys/mtio.h>
49 #include <sys/stat.h>
50 #include <sys/wait.h>
51 #include <err.h>
52 #include <errno.h>
53 #include <fcntl.h>
54 #include <signal.h>
55 #include <stdio.h>
56 #include <string.h>
57 #include <stdlib.h>
58 #include <unistd.h>
59 #include "pax.h"
60 #include "options.h"
61 #include "extern.h"
62 
63 /*
64  * Routines which deal directly with the archive I/O device/file.
65  */
66 
67 #define DMOD		0666		/* default mode of created archives */
68 #define EXT_MODE	O_RDONLY	/* open mode for list/extract */
69 #define AR_MODE		(O_WRONLY | O_CREAT | O_TRUNC)	/* mode for archive */
70 #define APP_MODE	O_RDWR		/* mode for append */
71 #define STDO		"<STDOUT>"	/* pseudo name for stdout */
72 #define STDN		"<STDIN>"	/* pseudo name for stdin */
73 static int arfd = -1;			/* archive file descriptor */
74 static int artyp = ISREG;		/* archive type: file/FIFO/tape */
75 static int arvol = 1;			/* archive volume number */
76 static int lstrval = -1;		/* return value from last i/o */
77 static int io_ok;			/* i/o worked on volume after resync */
78 static int did_io;			/* did i/o ever occur on volume? */
79 static int done;			/* set via tty termination */
80 static struct stat arsb;		/* stat of archive device at open */
81 static int invld_rec;			/* tape has out of spec record size */
82 static int wr_trail = 1;		/* trailer was rewritten in append */
83 static int can_unlnk = 0;		/* do we unlink null archives?  */
84 char *arcname;		  	/* printable name of archive */
85 const char *gzip_program;		/* name of gzip program */
86 static pid_t zpid = -1;			/* pid of child process */
87 
88 static int get_phys(void);
89 extern sigset_t s_mask;
90 static void ar_start_gzip(int, const char *, int);
91 
92 /*
93  * ar_open()
94  *	Opens the next archive volume. Determines the type of the device and
95  *	sets up block sizes as required by the archive device and the format.
96  *	Note: we may be called with name == NULL on the first open only.
97  * Return:
98  *	-1 on failure, 0 otherwise
99  */
100 
101 int
102 ar_open(char *name)
103 {
104 	struct mtget mb;
105 
106 	if (arfd != -1)
107 		(void)close(arfd);
108 	arfd = -1;
109 	can_unlnk = did_io = io_ok = invld_rec = 0;
110 	artyp = ISREG;
111 	flcnt = 0;
112 
113 	/*
114 	 * open based on overall operation mode
115 	 */
116 	switch (act) {
117 	case LIST:
118 	case EXTRACT:
119 		if (name == NULL) {
120 			arfd = STDIN_FILENO;
121 			arcname = STDN;
122 		} else if ((arfd = open(name, EXT_MODE, DMOD)) < 0)
123 			syswarn(0, errno, "Failed open to read on %s", name);
124 		if (arfd != -1 && gzip_program != NULL)
125 			ar_start_gzip(arfd, gzip_program, 0);
126 		break;
127 	case ARCHIVE:
128 		if (name == NULL) {
129 			arfd = STDOUT_FILENO;
130 			arcname = STDO;
131 		} else if ((arfd = open(name, AR_MODE, DMOD)) < 0)
132 			syswarn(0, errno, "Failed open to write on %s", name);
133 		else
134 			can_unlnk = 1;
135 		if (arfd != -1 && gzip_program != NULL)
136 			ar_start_gzip(arfd, gzip_program, 1);
137 		break;
138 	case APPND:
139 		if (name == NULL) {
140 			arfd = STDOUT_FILENO;
141 			arcname = STDO;
142 		} else if ((arfd = open(name, APP_MODE, DMOD)) < 0)
143 			syswarn(0, errno, "Failed open to read/write on %s",
144 				name);
145 		break;
146 	case COPY:
147 		/*
148 		 * arfd not used in COPY mode
149 		 */
150 		arcname = "<NONE>";
151 		lstrval = 1;
152 		return(0);
153 	}
154 	if (arfd < 0)
155 		return(-1);
156 
157 	if (chdname != NULL)
158 		if (chdir(chdname) != 0)
159 			syswarn(1, errno, "Failed chdir to %s", chdname);
160 	/*
161 	 * set up is based on device type
162 	 */
163 	if (fstat(arfd, &arsb) < 0) {
164 		syswarn(0, errno, "Failed stat on %s", arcname);
165 		(void)close(arfd);
166 		arfd = -1;
167 		can_unlnk = 0;
168 		return(-1);
169 	}
170 	if (S_ISDIR(arsb.st_mode)) {
171 		paxwarn(0, "Cannot write an archive on top of a directory %s",
172 		    arcname);
173 		(void)close(arfd);
174 		arfd = -1;
175 		can_unlnk = 0;
176 		return(-1);
177 	}
178 
179 	if (S_ISCHR(arsb.st_mode))
180 		artyp = ioctl(arfd, MTIOCGET, &mb) ? ISCHR : ISTAPE;
181 	else if (S_ISBLK(arsb.st_mode))
182 		artyp = ISBLK;
183 	else if ((lseek(arfd, (off_t)0L, SEEK_CUR) == -1) && (errno == ESPIPE))
184 		artyp = ISPIPE;
185 	else
186 		artyp = ISREG;
187 
188 	/*
189 	 * make sure we beyond any doubt that we only can unlink regular files
190 	 * we created
191 	 */
192 	if (artyp != ISREG)
193 		can_unlnk = 0;
194 	/*
195 	 * if we are writing, we are done
196 	 */
197 	if (act == ARCHIVE) {
198 		blksz = rdblksz = wrblksz;
199 		lstrval = 1;
200 		return(0);
201 	}
202 
203 	/*
204 	 * set default blksz on read. APPNDs writes rdblksz on the last volume
205 	 * On all new archive volumes, we shift to wrblksz (if the user
206 	 * specified one, otherwize we will continue to use rdblksz). We
207 	 * must to set blocksize based on what kind of device the archive is
208 	 * stored.
209 	 */
210 	switch(artyp) {
211 	case ISTAPE:
212 		/*
213 		 * Tape drives come in at least two flavors. Those that support
214 		 * variable sized records and those that have fixed sized
215 		 * records. They must be treated differently. For tape drives
216 		 * that support variable sized records, we must make large
217 		 * reads to make sure we get the entire record, otherwise we
218 		 * will just get the first part of the record (up to size we
219 		 * asked). Tapes with fixed sized records may or may not return
220 		 * multiple records in a single read. We really do not care
221 		 * what the physical record size is UNLESS we are going to
222 		 * append. (We will need the physical block size to rewrite
223 		 * the trailer). Only when we are appending do we go to the
224 		 * effort to figure out the true PHYSICAL record size.
225 		 */
226 		blksz = rdblksz = MAXBLK;
227 		break;
228 	case ISPIPE:
229 	case ISBLK:
230 	case ISCHR:
231 		/*
232 		 * Blocksize is not a major issue with these devices (but must
233 		 * be kept a multiple of 512). If the user specified a write
234 		 * block size, we use that to read. Under append, we must
235 		 * always keep blksz == rdblksz. Otherwise we go ahead and use
236 		 * the device optimal blocksize as (and if) returned by stat
237 		 * and if it is within pax specs.
238 		 */
239 		if ((act == APPND) && wrblksz) {
240 			blksz = rdblksz = wrblksz;
241 			break;
242 		}
243 
244 		if ((arsb.st_blksize > 0) && (arsb.st_blksize < MAXBLK) &&
245 		    ((arsb.st_blksize % BLKMULT) == 0))
246 			rdblksz = arsb.st_blksize;
247 		else
248 			rdblksz = DEVBLK;
249 		/*
250 		 * For performance go for large reads when we can without harm
251 		 */
252 		if ((act == APPND) || (artyp == ISCHR))
253 			blksz = rdblksz;
254 		else
255 			blksz = MAXBLK;
256 		break;
257 	case ISREG:
258 		/*
259 		 * if the user specified wrblksz works, use it. Under appends
260 		 * we must always keep blksz == rdblksz
261 		 */
262 		if ((act == APPND) && wrblksz && ((arsb.st_size%wrblksz)==0)){
263 			blksz = rdblksz = wrblksz;
264 			break;
265 		}
266 		/*
267 		 * See if we can find the blocking factor from the file size
268 		 */
269 		for (rdblksz = MAXBLK; rdblksz > 0; rdblksz -= BLKMULT)
270 			if ((arsb.st_size % rdblksz) == 0)
271 				break;
272 		/*
273 		 * When we cannot find a match, we may have a flawed archive.
274 		 */
275 		if (rdblksz <= 0)
276 			rdblksz = FILEBLK;
277 		/*
278 		 * for performance go for large reads when we can
279 		 */
280 		if (act == APPND)
281 			blksz = rdblksz;
282 		else
283 			blksz = MAXBLK;
284 		break;
285 	default:
286 		/*
287 		 * should never happen, worse case, slow...
288 		 */
289 		blksz = rdblksz = BLKMULT;
290 		break;
291 	}
292 	lstrval = 1;
293 	return(0);
294 }
295 
296 /*
297  * ar_close()
298  *	closes archive device, increments volume number, and prints i/o summary
299  */
300 void
301 ar_close(void)
302 {
303 
304 	if (arfd < 0) {
305 		did_io = io_ok = flcnt = 0;
306 		return;
307 	}
308 
309 	/*
310 	 * Close archive file. This may take a LONG while on tapes (we may be
311 	 * forced to wait for the rewind to complete) so tell the user what is
312 	 * going on (this avoids the user hitting control-c thinking pax is
313 	 * broken).
314 	 */
315 	if (vflag && (artyp == ISTAPE)) {
316 		if (vfpart)
317 			(void)putc('\n', listf);
318 		(void)fprintf(listf,
319 			"%s: Waiting for tape drive close to complete...",
320 			argv0);
321 		(void)fflush(listf);
322 	}
323 
324 	/*
325 	 * if nothing was written to the archive (and we created it), we remove
326 	 * it
327 	 */
328 	if (can_unlnk && (fstat(arfd, &arsb) == 0) && (S_ISREG(arsb.st_mode)) &&
329 	    (arsb.st_size == 0)) {
330 		(void)unlink(arcname);
331 		can_unlnk = 0;
332 	}
333 
334 	/*
335 	 * for a quick extract/list, pax frequently exits before the child
336 	 * process is done
337 	 */
338 	if ((act == LIST || act == EXTRACT) && nflag && zpid > 0) {
339 		int status;
340 		kill(zpid, SIGINT);
341 		waitpid(zpid, &status, 0);
342 	}
343 
344 	(void)close(arfd);
345 
346 	if (vflag && (artyp == ISTAPE)) {
347 		(void)fputs("done.\n", listf);
348 		vfpart = 0;
349 		(void)fflush(listf);
350 	}
351 	arfd = -1;
352 
353 	if (!io_ok && !did_io) {
354 		flcnt = 0;
355 		return;
356 	}
357 	did_io = io_ok = 0;
358 
359 	/*
360 	 * The volume number is only increased when the last device has data
361 	 * and we have already determined the archive format.
362 	 */
363 	if (frmt != NULL)
364 		++arvol;
365 
366 	if (!vflag) {
367 		flcnt = 0;
368 		return;
369 	}
370 
371 	/*
372 	 * Print out a summary of I/O for this archive volume.
373 	 */
374 	if (vfpart) {
375 		(void)putc('\n', listf);
376 		vfpart = 0;
377 	}
378 
379 	/*
380 	 * If we have not determined the format yet, we just say how many bytes
381 	 * we have skipped over looking for a header to id. there is no way we
382 	 * could have written anything yet.
383 	 */
384 	if (frmt == NULL) {
385 #	ifdef NET2_STAT
386 		(void)fprintf(listf, "%s: unknown format, %lu bytes skipped.\n",
387 #	else
388 		(void)fprintf(listf, "%s: unknown format, %qu bytes skipped.\n",
389 #	endif
390 		    argv0, rdcnt);
391 		(void)fflush(listf);
392 		flcnt = 0;
393 		return;
394 	}
395 
396 	if (strcmp(NM_CPIO, argv0) == 0)
397 		(void)fprintf(listf, "%qu blocks\n", (rdcnt ? rdcnt : wrcnt) / 5120);
398 	else if (strcmp(NM_TAR, argv0) != 0)
399 		(void)fprintf(listf,
400 #	ifdef NET2_STAT
401 		    "%s: %s vol %d, %lu files, %lu bytes read, %lu bytes written.\n",
402 #	else
403 		    "%s: %s vol %d, %lu files, %qu bytes read, %qu bytes written.\n",
404 #	endif
405 		    argv0, frmt->name, arvol-1, flcnt, rdcnt, wrcnt);
406 	(void)fflush(listf);
407 	flcnt = 0;
408 }
409 
410 /*
411  * ar_drain()
412  *	drain any archive format independent padding from an archive read
413  *	from a socket or a pipe. This is to prevent the process on the
414  *	other side of the pipe from getting a SIGPIPE (pax will stop
415  *	reading an archive once a format dependent trailer is detected).
416  */
417 void
418 ar_drain(void)
419 {
420 	int res;
421 	char drbuf[MAXBLK];
422 
423 	/*
424 	 * we only drain from a pipe/socket. Other devices can be closed
425 	 * without reading up to end of file. We sure hope that pipe is closed
426 	 * on the other side so we will get an EOF.
427 	 */
428 	if ((artyp != ISPIPE) || (lstrval <= 0))
429 		return;
430 
431 	/*
432 	 * keep reading until pipe is drained
433 	 */
434 	while ((res = read(arfd, drbuf, sizeof(drbuf))) > 0)
435 		;
436 	lstrval = res;
437 }
438 
439 /*
440  * ar_set_wr()
441  *	Set up device right before switching from read to write in an append.
442  *	device dependent code (if required) to do this should be added here.
443  *	For all archive devices we are already positioned at the place we want
444  *	to start writing when this routine is called.
445  * Return:
446  *	0 if all ready to write, -1 otherwise
447  */
448 
449 int
450 ar_set_wr(void)
451 {
452 	off_t cpos;
453 
454 	/*
455 	 * we must make sure the trailer is rewritten on append, ar_next()
456 	 * will stop us if the archive containing the trailer was not written
457 	 */
458 	wr_trail = 0;
459 
460 	/*
461 	 * Add any device dependent code as required here
462 	 */
463 	if (artyp != ISREG)
464 		return(0);
465 	/*
466 	 * Ok we have an archive in a regular file. If we were rewriting a
467 	 * file, we must get rid of all the stuff after the current offset
468 	 * (it was not written by pax).
469 	 */
470 	if (((cpos = lseek(arfd, (off_t)0L, SEEK_CUR)) < 0) ||
471 	    (ftruncate(arfd, cpos) < 0)) {
472 		syswarn(1, errno, "Unable to truncate archive file");
473 		return(-1);
474 	}
475 	return(0);
476 }
477 
478 /*
479  * ar_app_ok()
480  *	check if the last volume in the archive allows appends. We cannot check
481  *	this until we are ready to write since there is no spec that says all
482  *	volumes in a single archive have to be of the same type...
483  * Return:
484  *	0 if we can append, -1 otherwise.
485  */
486 
487 int
488 ar_app_ok(void)
489 {
490 	if (artyp == ISPIPE) {
491 		paxwarn(1, "Cannot append to an archive obtained from a pipe.");
492 		return(-1);
493 	}
494 
495 	if (!invld_rec)
496 		return(0);
497 	paxwarn(1,"Cannot append, device record size %d does not support %s spec",
498 		rdblksz, argv0);
499 	return(-1);
500 }
501 
502 /*
503  * ar_read()
504  *	read up to a specified number of bytes from the archive into the
505  *	supplied buffer. When dealing with tapes we may not always be able to
506  *	read what we want.
507  * Return:
508  *	Number of bytes in buffer. 0 for end of file, -1 for a read error.
509  */
510 
511 int
512 ar_read(char *buf, int cnt)
513 {
514 	int res = 0;
515 
516 	/*
517 	 * if last i/o was in error, no more reads until reset or new volume
518 	 */
519 	if (lstrval <= 0)
520 		return(lstrval);
521 
522 	/*
523 	 * how we read must be based on device type
524 	 */
525 	switch (artyp) {
526 	case ISTAPE:
527 		if ((res = read(arfd, buf, cnt)) > 0) {
528 			/*
529 			 * CAUTION: tape systems may not always return the same
530 			 * sized records so we leave blksz == MAXBLK. The
531 			 * physical record size that a tape drive supports is
532 			 * very hard to determine in a uniform and portable
533 			 * manner.
534 			 */
535 			io_ok = 1;
536 			if (res != rdblksz) {
537 				/*
538 				 * Record size changed. If this is happens on
539 				 * any record after the first, we probably have
540 				 * a tape drive which has a fixed record size
541 				 * we are getting multiple records in a single
542 				 * read). Watch out for record blocking that
543 				 * violates pax spec (must be a multiple of
544 				 * BLKMULT).
545 				 */
546 				rdblksz = res;
547 				if (rdblksz % BLKMULT)
548 					invld_rec = 1;
549 			}
550 			return(res);
551 		}
552 		break;
553 	case ISREG:
554 	case ISBLK:
555 	case ISCHR:
556 	case ISPIPE:
557 	default:
558 		/*
559 		 * Files are so easy to deal with. These other things cannot
560 		 * be trusted at all. So when we are dealing with character
561 		 * devices and pipes we just take what they have ready for us
562 		 * and return. Trying to do anything else with them runs the
563 		 * risk of failure.
564 		 */
565 		if ((res = read(arfd, buf, cnt)) > 0) {
566 			io_ok = 1;
567 			return(res);
568 		}
569 		break;
570 	}
571 
572 	/*
573 	 * We are in trouble at this point, something is broken...
574 	 */
575 	lstrval = res;
576 	if (res < 0)
577 		syswarn(1, errno, "Failed read on archive volume %d", arvol);
578 	else
579 		paxwarn(0, "End of archive volume %d reached", arvol);
580 	return(res);
581 }
582 
583 /*
584  * ar_write()
585  *	Write a specified number of bytes in supplied buffer to the archive
586  *	device so it appears as a single "block". Deals with errors and tries
587  *	to recover when faced with short writes.
588  * Return:
589  *	Number of bytes written. 0 indicates end of volume reached and with no
590  *	flaws (as best that can be detected). A -1 indicates an unrecoverable
591  *	error in the archive occured.
592  */
593 
594 int
595 ar_write(char *buf, int bsz)
596 {
597 	int res;
598 	off_t cpos;
599 
600 	/*
601 	 * do not allow pax to create a "bad" archive. Once a write fails on
602 	 * an archive volume prevent further writes to it.
603 	 */
604 	if (lstrval <= 0)
605 		return(lstrval);
606 
607 	if ((res = write(arfd, buf, bsz)) == bsz) {
608 		wr_trail = 1;
609 		io_ok = 1;
610 		return(bsz);
611 	}
612 	/*
613 	 * write broke, see what we can do with it. We try to send any partial
614 	 * writes that may violate pax spec to the next archive volume.
615 	 */
616 	if (res < 0)
617 		lstrval = res;
618 	else
619 		lstrval = 0;
620 
621 	switch (artyp) {
622 	case ISREG:
623 		if ((res > 0) && (res % BLKMULT)) {
624 			/*
625 		 	 * try to fix up partial writes which are not BLKMULT
626 			 * in size by forcing the runt record to next archive
627 			 * volume
628 		 	 */
629 			if ((cpos = lseek(arfd, (off_t)0L, SEEK_CUR)) < 0)
630 				break;
631 			cpos -= (off_t)res;
632 			if (ftruncate(arfd, cpos) < 0)
633 				break;
634 			res = lstrval = 0;
635 			break;
636 		}
637 		if (res >= 0)
638 			break;
639 		/*
640 		 * if file is out of space, handle it like a return of 0
641 		 */
642 		if ((errno == ENOSPC) || (errno == EFBIG) || (errno == EDQUOT))
643 			res = lstrval = 0;
644 		break;
645 	case ISTAPE:
646 	case ISCHR:
647 	case ISBLK:
648 		if (res >= 0)
649 			break;
650 		if (errno == EACCES) {
651 			paxwarn(0, "Write failed, archive is write protected.");
652 			res = lstrval = 0;
653 			return(0);
654 		}
655 		/*
656 		 * see if we reached the end of media, if so force a change to
657 		 * the next volume
658 		 */
659 		if ((errno == ENOSPC) || (errno == EIO) || (errno == ENXIO))
660 			res = lstrval = 0;
661 		break;
662 	case ISPIPE:
663 	default:
664 		/*
665 		 * we cannot fix errors to these devices
666 		 */
667 		break;
668 	}
669 
670 	/*
671 	 * Better tell the user the bad news...
672 	 * if this is a block aligned archive format, we may have a bad archive
673 	 * if the format wants the header to start at a BLKMULT boundary. While
674 	 * we can deal with the mis-aligned data, it violates spec and other
675 	 * archive readers will likely fail. if the format is not block
676 	 * aligned, the user may be lucky (and the archive is ok).
677 	 */
678 	if (res >= 0) {
679 		if (res > 0)
680 			wr_trail = 1;
681 		io_ok = 1;
682 	}
683 
684 	/*
685 	 * If we were trying to rewrite the trailer and it didn't work, we
686 	 * must quit right away.
687 	 */
688 	if (!wr_trail && (res <= 0)) {
689 		paxwarn(1,"Unable to append, trailer re-write failed. Quitting.");
690 		return(res);
691 	}
692 
693 	if (res == 0)
694 		paxwarn(0, "End of archive volume %d reached", arvol);
695 	else if (res < 0)
696 		syswarn(1, errno, "Failed write to archive volume: %d", arvol);
697 	else if (!frmt->blkalgn || ((res % frmt->blkalgn) == 0))
698 		paxwarn(0,"WARNING: partial archive write. Archive MAY BE FLAWED");
699 	else
700 		paxwarn(1,"WARNING: partial archive write. Archive IS FLAWED");
701 	return(res);
702 }
703 
704 /*
705  * ar_rdsync()
706  *	Try to move past a bad spot on a flawed archive as needed to continue
707  *	I/O. Clears error flags to allow I/O to continue.
708  * Return:
709  *	0 when ok to try i/o again, -1 otherwise.
710  */
711 
712 int
713 ar_rdsync(void)
714 {
715 	long fsbz;
716 	off_t cpos;
717 	off_t mpos;
718 	struct mtop mb;
719 
720 	/*
721 	 * Fail resync attempts at user request (done) or this is going to be
722 	 * an update/append to a existing archive. if last i/o hit media end,
723 	 * we need to go to the next volume not try a resync
724 	 */
725 	if ((done > 0) || (lstrval == 0))
726 		return(-1);
727 
728 	if ((act == APPND) || (act == ARCHIVE)) {
729 		paxwarn(1, "Cannot allow updates to an archive with flaws.");
730 		return(-1);
731 	}
732 	if (io_ok)
733 		did_io = 1;
734 
735 	switch(artyp) {
736 	case ISTAPE:
737 		/*
738 		 * if the last i/o was a successful data transfer, we assume
739 		 * the fault is just a bad record on the tape that we are now
740 		 * past. If we did not get any data since the last resync try
741 		 * to move the tape forward one PHYSICAL record past any
742 		 * damaged tape section. Some tape drives are stubborn and need
743 		 * to be pushed.
744 		 */
745 		if (io_ok) {
746 			io_ok = 0;
747 			lstrval = 1;
748 			break;
749 		}
750 		mb.mt_op = MTFSR;
751 		mb.mt_count = 1;
752 		if (ioctl(arfd, MTIOCTOP, &mb) < 0)
753 			break;
754 		lstrval = 1;
755 		break;
756 	case ISREG:
757 	case ISCHR:
758 	case ISBLK:
759 		/*
760 		 * try to step over the bad part of the device.
761 		 */
762 		io_ok = 0;
763 		if (((fsbz = arsb.st_blksize) <= 0) || (artyp != ISREG))
764 			fsbz = BLKMULT;
765 		if ((cpos = lseek(arfd, (off_t)0L, SEEK_CUR)) < 0)
766 			break;
767 		mpos = fsbz - (cpos % (off_t)fsbz);
768 		if (lseek(arfd, mpos, SEEK_CUR) < 0)
769 			break;
770 		lstrval = 1;
771 		break;
772 	case ISPIPE:
773 	default:
774 		/*
775 		 * cannot recover on these archive device types
776 		 */
777 		io_ok = 0;
778 		break;
779 	}
780 	if (lstrval <= 0) {
781 		paxwarn(1, "Unable to recover from an archive read failure.");
782 		return(-1);
783 	}
784 	paxwarn(0, "Attempting to recover from an archive read failure.");
785 	return(0);
786 }
787 
788 /*
789  * ar_fow()
790  *	Move the I/O position within the archive foward the specified number of
791  *	bytes as supported by the device. If we cannot move the requested
792  *	number of bytes, return the actual number of bytes moved in skipped.
793  * Return:
794  *	0 if moved the requested distance, -1 on complete failure, 1 on
795  *	partial move (the amount moved is in skipped)
796  */
797 
798 int
799 ar_fow(off_t sksz, off_t *skipped)
800 {
801 	off_t cpos;
802 	off_t mpos;
803 
804 	*skipped = 0;
805 	if (sksz <= 0)
806 		return(0);
807 
808 	/*
809 	 * we cannot move foward at EOF or error
810 	 */
811 	if (lstrval <= 0)
812 		return(lstrval);
813 
814 	/*
815 	 * Safer to read forward on devices where it is hard to find the end of
816 	 * the media without reading to it. With tapes we cannot be sure of the
817 	 * number of physical blocks to skip (we do not know physical block
818 	 * size at this point), so we must only read foward on tapes!
819 	 */
820 	if (artyp != ISREG)
821 		return(0);
822 
823 	/*
824 	 * figure out where we are in the archive
825 	 */
826 	if ((cpos = lseek(arfd, (off_t)0L, SEEK_CUR)) >= 0) {
827 		/*
828 	 	 * we can be asked to move farther than there are bytes in this
829 		 * volume, if so, just go to file end and let normal buf_fill()
830 		 * deal with the end of file (it will go to next volume by
831 		 * itself)
832 	 	 */
833 		if ((mpos = cpos + sksz) > arsb.st_size) {
834 			*skipped = arsb.st_size - cpos;
835 			mpos = arsb.st_size;
836 		} else
837 			*skipped = sksz;
838 		if (lseek(arfd, mpos, SEEK_SET) >= 0)
839 			return(0);
840 	}
841 	syswarn(1, errno, "Forward positioning operation on archive failed");
842 	lstrval = -1;
843 	return(-1);
844 }
845 
846 /*
847  * ar_rev()
848  *	move the i/o position within the archive backwards the specified byte
849  *	count as supported by the device. With tapes drives we RESET rdblksz to
850  *	the PHYSICAL blocksize.
851  *	NOTE: We should only be called to move backwards so we can rewrite the
852  *	last records (the trailer) of an archive (APPEND).
853  * Return:
854  *	0 if moved the requested distance, -1 on complete failure
855  */
856 
857 int
858 ar_rev(off_t sksz)
859 {
860 	off_t cpos;
861 	struct mtop mb;
862 	int phyblk;
863 
864 	/*
865 	 * make sure we do not have try to reverse on a flawed archive
866 	 */
867 	if (lstrval < 0)
868 		return(lstrval);
869 
870 	switch(artyp) {
871 	case ISPIPE:
872 		if (sksz <= 0)
873 			break;
874 		/*
875 		 * cannot go backwards on these critters
876 		 */
877 		paxwarn(1, "Reverse positioning on pipes is not supported.");
878 		lstrval = -1;
879 		return(-1);
880 	case ISREG:
881 	case ISBLK:
882 	case ISCHR:
883 	default:
884 		if (sksz <= 0)
885 			break;
886 
887 		/*
888 		 * For things other than files, backwards movement has a very
889 		 * high probability of failure as we really do not know the
890 		 * true attributes of the device we are talking to (the device
891 		 * may not even have the ability to lseek() in any direction).
892 		 * First we figure out where we are in the archive.
893 		 */
894 		if ((cpos = lseek(arfd, (off_t)0L, SEEK_CUR)) < 0) {
895 			syswarn(1, errno,
896 			   "Unable to obtain current archive byte offset");
897 			lstrval = -1;
898 			return(-1);
899 		}
900 
901 		/*
902 		 * we may try to go backwards past the start when the archive
903 		 * is only a single record. If this hapens and we are on a
904 		 * multi volume archive, we need to go to the end of the
905 		 * previous volume and continue our movement backwards from
906 		 * there.
907 		 */
908 		if ((cpos -= sksz) < (off_t)0L) {
909 			if (arvol > 1) {
910 				/*
911 				 * this should never happen
912 				 */
913 				paxwarn(1,"Reverse position on previous volume.");
914 				lstrval = -1;
915 				return(-1);
916 			}
917 			cpos = (off_t)0L;
918 		}
919 		if (lseek(arfd, cpos, SEEK_SET) < 0) {
920 			syswarn(1, errno, "Unable to seek archive backwards");
921 			lstrval = -1;
922 			return(-1);
923 		}
924 		break;
925 	case ISTAPE:
926 		/*
927 	 	 * Calculate and move the proper number of PHYSICAL tape
928 		 * blocks. If the sksz is not an even multiple of the physical
929 		 * tape size, we cannot do the move (this should never happen).
930 		 * (We also cannot handler trailers spread over two vols).
931 		 * get_phys() also makes sure we are in front of the filemark.
932 	 	 */
933 		if ((phyblk = get_phys()) <= 0) {
934 			lstrval = -1;
935 			return(-1);
936 		}
937 
938 		/*
939 		 * make sure future tape reads only go by physical tape block
940 		 * size (set rdblksz to the real size).
941 		 */
942 		rdblksz = phyblk;
943 
944 		/*
945 		 * if no movement is required, just return (we must be after
946 		 * get_phys() so the physical blocksize is properly set)
947 		 */
948 		if (sksz <= 0)
949 			break;
950 
951 		/*
952 		 * ok we have to move. Make sure the tape drive can do it.
953 		 */
954 		if (sksz % phyblk) {
955 			paxwarn(1,
956 			    "Tape drive unable to backspace requested amount");
957 			lstrval = -1;
958 			return(-1);
959 		}
960 
961 		/*
962 		 * move backwards the requested number of bytes
963 		 */
964 		mb.mt_op = MTBSR;
965 		mb.mt_count = sksz/phyblk;
966 		if (ioctl(arfd, MTIOCTOP, &mb) < 0) {
967 			syswarn(1,errno, "Unable to backspace tape %d blocks.",
968 			    mb.mt_count);
969 			lstrval = -1;
970 			return(-1);
971 		}
972 		break;
973 	}
974 	lstrval = 1;
975 	return(0);
976 }
977 
978 /*
979  * get_phys()
980  *	Determine the physical block size on a tape drive. We need the physical
981  *	block size so we know how many bytes we skip over when we move with
982  *	mtio commands. We also make sure we are BEFORE THE TAPE FILEMARK when
983  *	return.
984  *	This is one really SLOW routine...
985  * Return:
986  *	physical block size if ok (ok > 0), -1 otherwise
987  */
988 
989 static int
990 get_phys(void)
991 {
992 	int padsz = 0;
993 	int res;
994 	int phyblk;
995 	struct mtop mb;
996 	char scbuf[MAXBLK];
997 
998 	/*
999 	 * move to the file mark, and then back up one record and read it.
1000 	 * this should tell us the physical record size the tape is using.
1001 	 */
1002 	if (lstrval == 1) {
1003 		/*
1004 		 * we know we are at file mark when we get back a 0 from
1005 		 * read()
1006 		 */
1007 		while ((res = read(arfd, scbuf, sizeof(scbuf))) > 0)
1008 			padsz += res;
1009 		if (res < 0) {
1010 			syswarn(1, errno, "Unable to locate tape filemark.");
1011 			return(-1);
1012 		}
1013 	}
1014 
1015 	/*
1016 	 * move backwards over the file mark so we are at the end of the
1017 	 * last record.
1018 	 */
1019 	mb.mt_op = MTBSF;
1020 	mb.mt_count = 1;
1021 	if (ioctl(arfd, MTIOCTOP, &mb) < 0) {
1022 		syswarn(1, errno, "Unable to backspace over tape filemark.");
1023 		return(-1);
1024 	}
1025 
1026 	/*
1027 	 * move backwards so we are in front of the last record and read it to
1028 	 * get physical tape blocksize.
1029 	 */
1030 	mb.mt_op = MTBSR;
1031 	mb.mt_count = 1;
1032 	if (ioctl(arfd, MTIOCTOP, &mb) < 0) {
1033 		syswarn(1, errno, "Unable to backspace over last tape block.");
1034 		return(-1);
1035 	}
1036 	if ((phyblk = read(arfd, scbuf, sizeof(scbuf))) <= 0) {
1037 		syswarn(1, errno, "Cannot determine archive tape blocksize.");
1038 		return(-1);
1039 	}
1040 
1041 	/*
1042 	 * read foward to the file mark, then back up in front of the filemark
1043 	 * (this is a bit paranoid, but should be safe to do).
1044 	 */
1045 	while ((res = read(arfd, scbuf, sizeof(scbuf))) > 0)
1046 		;
1047 	if (res < 0) {
1048 		syswarn(1, errno, "Unable to locate tape filemark.");
1049 		return(-1);
1050 	}
1051 	mb.mt_op = MTBSF;
1052 	mb.mt_count = 1;
1053 	if (ioctl(arfd, MTIOCTOP, &mb) < 0) {
1054 		syswarn(1, errno, "Unable to backspace over tape filemark.");
1055 		return(-1);
1056 	}
1057 
1058 	/*
1059 	 * set lstrval so we know that the filemark has not been seen
1060 	 */
1061 	lstrval = 1;
1062 
1063 	/*
1064 	 * return if there was no padding
1065 	 */
1066 	if (padsz == 0)
1067 		return(phyblk);
1068 
1069 	/*
1070 	 * make sure we can move backwards over the padding. (this should
1071 	 * never fail).
1072 	 */
1073 	if (padsz % phyblk) {
1074 		paxwarn(1, "Tape drive unable to backspace requested amount");
1075 		return(-1);
1076 	}
1077 
1078 	/*
1079 	 * move backwards over the padding so the head is where it was when
1080 	 * we were first called (if required).
1081 	 */
1082 	mb.mt_op = MTBSR;
1083 	mb.mt_count = padsz/phyblk;
1084 	if (ioctl(arfd, MTIOCTOP, &mb) < 0) {
1085 		syswarn(1,errno,"Unable to backspace tape over %d pad blocks",
1086 		    mb.mt_count);
1087 		return(-1);
1088 	}
1089 	return(phyblk);
1090 }
1091 
1092 /*
1093  * ar_next()
1094  *	prompts the user for the next volume in this archive. For some devices
1095  *	we may allow the media to be changed. Otherwise a new archive is
1096  *	prompted for. By pax spec, if there is no controlling tty or an eof is
1097  *	read on tty input, we must quit pax.
1098  * Return:
1099  *	0 when ready to continue, -1 when all done
1100  */
1101 
1102 int
1103 ar_next(void)
1104 {
1105 	char buf[PAXPATHLEN+2];
1106 	static int freeit = 0;
1107 	sigset_t o_mask;
1108 
1109 	/*
1110 	 * WE MUST CLOSE THE DEVICE. A lot of devices must see last close, (so
1111 	 * things like writing EOF etc will be done) (Watch out ar_close() can
1112 	 * also be called via a signal handler, so we must prevent a race.
1113 	 */
1114 	if (sigprocmask(SIG_BLOCK, &s_mask, &o_mask) < 0)
1115 		syswarn(0, errno, "Unable to set signal mask");
1116 	ar_close();
1117 	if (sigprocmask(SIG_SETMASK, &o_mask, NULL) < 0)
1118 		syswarn(0, errno, "Unable to restore signal mask");
1119 
1120 	if (done || !wr_trail || strcmp(NM_TAR, argv0) == 0)
1121 		return(-1);
1122 
1123 	tty_prnt("\nATTENTION! %s archive volume change required.\n", argv0);
1124 
1125 	/*
1126 	 * if i/o is on stdin or stdout, we cannot reopen it (we do not know
1127 	 * the name), the user will be forced to type it in.
1128 	 */
1129 	if (strcmp(arcname, STDO) && strcmp(arcname, STDN) && (artyp != ISREG)
1130 	    && (artyp != ISPIPE)) {
1131 		if (artyp == ISTAPE) {
1132 			tty_prnt("%s ready for archive tape volume: %d\n",
1133 				arcname, arvol);
1134 			tty_prnt("Load the NEXT TAPE on the tape drive");
1135 		} else {
1136 			tty_prnt("%s ready for archive volume: %d\n",
1137 				arcname, arvol);
1138 			tty_prnt("Load the NEXT STORAGE MEDIA (if required)");
1139 		}
1140 
1141 		if ((act == ARCHIVE) || (act == APPND))
1142 			tty_prnt(" and make sure it is WRITE ENABLED.\n");
1143 		else
1144 			tty_prnt("\n");
1145 
1146 		for(;;) {
1147 			tty_prnt("Type \"y\" to continue, \".\" to quit %s,",
1148 				argv0);
1149 			tty_prnt(" or \"s\" to switch to new device.\nIf you");
1150 			tty_prnt(" cannot change storage media, type \"s\"\n");
1151 			tty_prnt("Is the device ready and online? > ");
1152 
1153 			if ((tty_read(buf,sizeof(buf))<0) || !strcmp(buf,".")){
1154 				done = 1;
1155 				lstrval = -1;
1156 				tty_prnt("Quitting %s!\n", argv0);
1157 				vfpart = 0;
1158 				return(-1);
1159 			}
1160 
1161 			if ((buf[0] == '\0') || (buf[1] != '\0')) {
1162 				tty_prnt("%s unknown command, try again\n",buf);
1163 				continue;
1164 			}
1165 
1166 			switch (buf[0]) {
1167 			case 'y':
1168 			case 'Y':
1169 				/*
1170 				 * we are to continue with the same device
1171 				 */
1172 				if (ar_open(arcname) >= 0)
1173 					return(0);
1174 				tty_prnt("Cannot re-open %s, try again\n",
1175 					arcname);
1176 				continue;
1177 			case 's':
1178 			case 'S':
1179 				/*
1180 				 * user wants to open a different device
1181 				 */
1182 				tty_prnt("Switching to a different archive\n");
1183 				break;
1184 			default:
1185 				tty_prnt("%s unknown command, try again\n",buf);
1186 				continue;
1187 			}
1188 			break;
1189 		}
1190 	} else
1191 		tty_prnt("Ready for archive volume: %d\n", arvol);
1192 
1193 	/*
1194 	 * have to go to a different archive
1195 	 */
1196 	for (;;) {
1197 		tty_prnt("Input archive name or \".\" to quit %s.\n", argv0);
1198 		tty_prnt("Archive name > ");
1199 
1200 		if ((tty_read(buf, sizeof(buf)) < 0) || !strcmp(buf, ".")) {
1201 			done = 1;
1202 			lstrval = -1;
1203 			tty_prnt("Quitting %s!\n", argv0);
1204 			vfpart = 0;
1205 			return(-1);
1206 		}
1207 		if (buf[0] == '\0') {
1208 			tty_prnt("Empty file name, try again\n");
1209 			continue;
1210 		}
1211 		if (!strcmp(buf, "..")) {
1212 			tty_prnt("Illegal file name: .. try again\n");
1213 			continue;
1214 		}
1215 		if (strlen(buf) > PAXPATHLEN) {
1216 			tty_prnt("File name too long, try again\n");
1217 			continue;
1218 		}
1219 
1220 		/*
1221 		 * try to open new archive
1222 		 */
1223 		if (ar_open(buf) >= 0) {
1224 			if (freeit) {
1225 				(void)free(arcname);
1226 				freeit = 0;
1227 			}
1228 			if ((arcname = strdup(buf)) == NULL) {
1229 				done = 1;
1230 				lstrval = -1;
1231 				paxwarn(0, "Cannot save archive name.");
1232 				return(-1);
1233 			}
1234 			freeit = 1;
1235 			break;
1236 		}
1237 		tty_prnt("Cannot open %s, try again\n", buf);
1238 		continue;
1239 	}
1240 	return(0);
1241 }
1242 
1243 /*
1244  * ar_start_gzip()
1245  * starts the gzip compression/decompression process as a child, using magic
1246  * to keep the fd the same in the calling function (parent).
1247  */
1248 void
1249 ar_start_gzip(int fd, const char *gzip_program, int wr)
1250 {
1251 	int fds[2];
1252 	char *gzip_flags;
1253 
1254 	if (pipe(fds) < 0)
1255 		err(1, "could not pipe");
1256 	zpid = fork();
1257 	if (zpid < 0)
1258 		err(1, "could not fork");
1259 
1260 	/* parent */
1261 	if (zpid) {
1262 		if (wr)
1263 			dup2(fds[1], fd);
1264 		else
1265 			dup2(fds[0], fd);
1266 		close(fds[0]);
1267 		close(fds[1]);
1268 	} else {
1269 		if (wr) {
1270 			dup2(fds[0], STDIN_FILENO);
1271 			dup2(fd, STDOUT_FILENO);
1272 			gzip_flags = "-c";
1273 		} else {
1274 			dup2(fds[1], STDOUT_FILENO);
1275 			dup2(fd, STDIN_FILENO);
1276 			gzip_flags = "-dc";
1277 		}
1278 		close(fds[0]);
1279 		close(fds[1]);
1280 		if (execlp(gzip_program, gzip_program, gzip_flags,
1281 		    (char *)NULL) < 0)
1282 			err(1, "could not exec");
1283 		/* NOTREACHED */
1284 	}
1285 }
1286