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