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