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