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