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