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