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