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