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