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