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