ar_io.c revision 1.15 1 /* $NetBSD: ar_io.c,v 1.15 2000/02/17 03:06:12 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.15 2000/02/17 03:06:12 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>" /* psuedo name for stdout */
76 #define STDN "<STDIN>" /* psuedo 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 cannont 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 boundry. 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 foward 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 foward 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 foward 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 foward on tapes!
993 */
994 if (artyp != ISREG)
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 if ((mpos = cpos + sksz) > arsb.st_size) {
1008 *skipped = arsb.st_size - cpos;
1009 mpos = arsb.st_size;
1010 } else
1011 *skipped = sksz;
1012 if (lseek(arfd, mpos, SEEK_SET) >= 0)
1013 return(0);
1014 }
1015 syswarn(1, errno, "Foward positioning operation on archive failed");
1016 lstrval = -1;
1017 return(-1);
1018 }
1019
1020 /*
1021 * ar_rev()
1022 * move the i/o position within the archive backwards the specified byte
1023 * count as supported by the device. With tapes drives we RESET rdblksz to
1024 * the PHYSICAL blocksize.
1025 * NOTE: We should only be called to move backwards so we can rewrite the
1026 * last records (the trailer) of an archive (APPEND).
1027 * Return:
1028 * 0 if moved the requested distance, -1 on complete failure
1029 */
1030
1031 #if __STDC__
1032 int
1033 ar_rev(off_t sksz)
1034 #else
1035 int
1036 ar_rev(sksz)
1037 off_t sksz;
1038 #endif
1039 {
1040 off_t cpos;
1041 struct mtop mb;
1042 int phyblk;
1043
1044 /*
1045 * make sure we do not have try to reverse on a flawed archive
1046 */
1047 if (lstrval < 0)
1048 return(lstrval);
1049
1050 switch(artyp) {
1051 case ISPIPE:
1052 if (sksz <= 0)
1053 break;
1054 /*
1055 * cannot go backwards on these critters
1056 */
1057 tty_warn(1, "Reverse positioning on pipes is not supported.");
1058 lstrval = -1;
1059 return(-1);
1060 case ISREG:
1061 case ISBLK:
1062 case ISCHR:
1063 default:
1064 if (sksz <= 0)
1065 break;
1066
1067 /*
1068 * For things other than files, backwards movement has a very
1069 * high probability of failure as we really do not know the
1070 * true attributes of the device we are talking to (the device
1071 * may not even have the ability to lseek() in any direction).
1072 * First we figure out where we are in the archive.
1073 */
1074 if ((cpos = lseek(arfd, (off_t)0L, SEEK_CUR)) < 0) {
1075 syswarn(1, errno,
1076 "Unable to obtain current archive byte offset");
1077 lstrval = -1;
1078 return(-1);
1079 }
1080
1081 /*
1082 * we may try to go backwards past the start when the archive
1083 * is only a single record. If this hapens and we are on a
1084 * multi volume archive, we need to go to the end of the
1085 * previous volume and continue our movement backwards from
1086 * there.
1087 */
1088 if ((cpos -= sksz) < (off_t)0L) {
1089 if (arvol > 1) {
1090 /*
1091 * this should never happen
1092 */
1093 tty_warn(1,
1094 "Reverse position on previous volume.");
1095 lstrval = -1;
1096 return(-1);
1097 }
1098 cpos = (off_t)0L;
1099 }
1100 if (lseek(arfd, cpos, SEEK_SET) < 0) {
1101 syswarn(1, errno, "Unable to seek archive backwards");
1102 lstrval = -1;
1103 return(-1);
1104 }
1105 break;
1106 case ISTAPE:
1107 /*
1108 * Calculate and move the proper number of PHYSICAL tape
1109 * blocks. If the sksz is not an even multiple of the physical
1110 * tape size, we cannot do the move (this should never happen).
1111 * (We also cannot handler trailers spread over two vols).
1112 * get_phys() also makes sure we are in front of the filemark.
1113 */
1114 if ((phyblk = get_phys()) <= 0) {
1115 lstrval = -1;
1116 return(-1);
1117 }
1118
1119 /*
1120 * make sure future tape reads only go by physical tape block
1121 * size (set rdblksz to the real size).
1122 */
1123 rdblksz = phyblk;
1124
1125 /*
1126 * if no movement is required, just return (we must be after
1127 * get_phys() so the physical blocksize is properly set)
1128 */
1129 if (sksz <= 0)
1130 break;
1131
1132 /*
1133 * ok we have to move. Make sure the tape drive can do it.
1134 */
1135 if (sksz % phyblk) {
1136 tty_warn(1,
1137 "Tape drive unable to backspace requested amount");
1138 lstrval = -1;
1139 return(-1);
1140 }
1141
1142 /*
1143 * move backwards the requested number of bytes
1144 */
1145 mb.mt_op = MTBSR;
1146 mb.mt_count = sksz/phyblk;
1147 if (ioctl(arfd, MTIOCTOP, &mb) < 0) {
1148 syswarn(1,errno, "Unable to backspace tape %ld blocks.",
1149 (long) mb.mt_count);
1150 lstrval = -1;
1151 return(-1);
1152 }
1153 break;
1154 }
1155 lstrval = 1;
1156 return(0);
1157 }
1158
1159 /*
1160 * get_phys()
1161 * Determine the physical block size on a tape drive. We need the physical
1162 * block size so we know how many bytes we skip over when we move with
1163 * mtio commands. We also make sure we are BEFORE THE TAPE FILEMARK when
1164 * return.
1165 * This is one really SLOW routine...
1166 * Return:
1167 * physical block size if ok (ok > 0), -1 otherwise
1168 */
1169
1170 #if __STDC__
1171 static int
1172 get_phys(void)
1173 #else
1174 static int
1175 get_phys()
1176 #endif
1177 {
1178 int padsz = 0;
1179 int res;
1180 int phyblk;
1181 struct mtop mb;
1182 char scbuf[MAXBLK];
1183
1184 /*
1185 * move to the file mark, and then back up one record and read it.
1186 * this should tell us the physical record size the tape is using.
1187 */
1188 if (lstrval == 1) {
1189 /*
1190 * we know we are at file mark when we get back a 0 from
1191 * read()
1192 */
1193 while ((res = read_with_restart(arfd, scbuf, sizeof(scbuf))) > 0)
1194 padsz += res;
1195 if (res < 0) {
1196 syswarn(1, errno, "Unable to locate tape filemark.");
1197 return(-1);
1198 }
1199 }
1200
1201 /*
1202 * move backwards over the file mark so we are at the end of the
1203 * last record.
1204 */
1205 mb.mt_op = MTBSF;
1206 mb.mt_count = 1;
1207 if (ioctl(arfd, MTIOCTOP, &mb) < 0) {
1208 syswarn(1, errno, "Unable to backspace over tape filemark.");
1209 return(-1);
1210 }
1211
1212 /*
1213 * move backwards so we are in front of the last record and read it to
1214 * get physical tape blocksize.
1215 */
1216 mb.mt_op = MTBSR;
1217 mb.mt_count = 1;
1218 if (ioctl(arfd, MTIOCTOP, &mb) < 0) {
1219 syswarn(1, errno, "Unable to backspace over last tape block.");
1220 return(-1);
1221 }
1222 if ((phyblk = read_with_restart(arfd, scbuf, sizeof(scbuf))) <= 0) {
1223 syswarn(1, errno, "Cannot determine archive tape blocksize.");
1224 return(-1);
1225 }
1226
1227 /*
1228 * read foward to the file mark, then back up in front of the filemark
1229 * (this is a bit paranoid, but should be safe to do).
1230 */
1231 while ((res = read_with_restart(arfd, scbuf, sizeof(scbuf))) > 0)
1232 ;
1233 if (res < 0) {
1234 syswarn(1, errno, "Unable to locate tape filemark.");
1235 return(-1);
1236 }
1237 mb.mt_op = MTBSF;
1238 mb.mt_count = 1;
1239 if (ioctl(arfd, MTIOCTOP, &mb) < 0) {
1240 syswarn(1, errno, "Unable to backspace over tape filemark.");
1241 return(-1);
1242 }
1243
1244 /*
1245 * set lstrval so we know that the filemark has not been seen
1246 */
1247 lstrval = 1;
1248
1249 /*
1250 * return if there was no padding
1251 */
1252 if (padsz == 0)
1253 return(phyblk);
1254
1255 /*
1256 * make sure we can move backwards over the padding. (this should
1257 * never fail).
1258 */
1259 if (padsz % phyblk) {
1260 tty_warn(1, "Tape drive unable to backspace requested amount");
1261 return(-1);
1262 }
1263
1264 /*
1265 * move backwards over the padding so the head is where it was when
1266 * we were first called (if required).
1267 */
1268 mb.mt_op = MTBSR;
1269 mb.mt_count = padsz/phyblk;
1270 if (ioctl(arfd, MTIOCTOP, &mb) < 0) {
1271 syswarn(1,errno,"Unable to backspace tape over %ld pad blocks",
1272 (long)mb.mt_count);
1273 return(-1);
1274 }
1275 return(phyblk);
1276 }
1277
1278 /*
1279 * ar_next()
1280 * prompts the user for the next volume in this archive. For some devices
1281 * we may allow the media to be changed. Otherwise a new archive is
1282 * prompted for. By pax spec, if there is no controlling tty or an eof is
1283 * read on tty input, we must quit pax.
1284 * Return:
1285 * 0 when ready to continue, -1 when all done
1286 */
1287
1288 #if __STDC__
1289 int
1290 ar_next(void)
1291 #else
1292 int
1293 ar_next()
1294 #endif
1295 {
1296 char buf[PAXPATHLEN+2];
1297 static int freeit = 0;
1298 sigset_t o_mask;
1299
1300 /*
1301 * WE MUST CLOSE THE DEVICE. A lot of devices must see last close, (so
1302 * things like writing EOF etc will be done) (Watch out ar_close() can
1303 * also be called via a signal handler, so we must prevent a race.
1304 */
1305 if (sigprocmask(SIG_BLOCK, &s_mask, &o_mask) < 0)
1306 syswarn(0, errno, "Unable to set signal mask");
1307 ar_close();
1308 if (sigprocmask(SIG_SETMASK, &o_mask, (sigset_t *)NULL) < 0)
1309 syswarn(0, errno, "Unable to restore signal mask");
1310
1311 if (done || !wr_trail || is_oldgnutar)
1312 return(-1);
1313
1314 tty_prnt("\nATTENTION! %s archive volume change required.\n", argv0);
1315
1316 /*
1317 * if i/o is on stdin or stdout, we cannot reopen it (we do not know
1318 * the name), the user will be forced to type it in.
1319 */
1320 if (strcmp(arcname, STDO) && strcmp(arcname, STDN) && (artyp != ISREG)
1321 && (artyp != ISPIPE)) {
1322 if (artyp == ISTAPE) {
1323 tty_prnt("%s ready for archive tape volume: %d\n",
1324 arcname, arvol);
1325 tty_prnt("Load the NEXT TAPE on the tape drive");
1326 } else {
1327 tty_prnt("%s ready for archive volume: %d\n",
1328 arcname, arvol);
1329 tty_prnt("Load the NEXT STORAGE MEDIA (if required)");
1330 }
1331
1332 if ((act == ARCHIVE) || (act == APPND))
1333 tty_prnt(" and make sure it is WRITE ENABLED.\n");
1334 else
1335 tty_prnt("\n");
1336
1337 for(;;) {
1338 tty_prnt("Type \"y\" to continue, \".\" to quit %s,",
1339 argv0);
1340 tty_prnt(" or \"s\" to switch to new device.\nIf you");
1341 tty_prnt(" cannot change storage media, type \"s\"\n");
1342 tty_prnt("Is the device ready and online? > ");
1343
1344 if ((tty_read(buf,sizeof(buf))<0) || !strcmp(buf,".")){
1345 done = 1;
1346 lstrval = -1;
1347 tty_prnt("Quitting %s!\n", argv0);
1348 vfpart = 0;
1349 return(-1);
1350 }
1351
1352 if ((buf[0] == '\0') || (buf[1] != '\0')) {
1353 tty_prnt("%s unknown command, try again\n",buf);
1354 continue;
1355 }
1356
1357 switch (buf[0]) {
1358 case 'y':
1359 case 'Y':
1360 /*
1361 * we are to continue with the same device
1362 */
1363 if (ar_open(arcname) >= 0)
1364 return(0);
1365 tty_prnt("Cannot re-open %s, try again\n",
1366 arcname);
1367 continue;
1368 case 's':
1369 case 'S':
1370 /*
1371 * user wants to open a different device
1372 */
1373 tty_prnt("Switching to a different archive\n");
1374 break;
1375 default:
1376 tty_prnt("%s unknown command, try again\n",buf);
1377 continue;
1378 }
1379 break;
1380 }
1381 } else
1382 tty_prnt("Ready for archive volume: %d\n", arvol);
1383
1384 /*
1385 * have to go to a different archive
1386 */
1387 for (;;) {
1388 tty_prnt("Input archive name or \".\" to quit %s.\n", argv0);
1389 tty_prnt("Archive name > ");
1390
1391 if ((tty_read(buf, sizeof(buf)) < 0) || !strcmp(buf, ".")) {
1392 done = 1;
1393 lstrval = -1;
1394 tty_prnt("Quitting %s!\n", argv0);
1395 vfpart = 0;
1396 return(-1);
1397 }
1398 if (buf[0] == '\0') {
1399 tty_prnt("Empty file name, try again\n");
1400 continue;
1401 }
1402 if (!strcmp(buf, "..")) {
1403 tty_prnt("Illegal file name: .. try again\n");
1404 continue;
1405 }
1406 if (strlen(buf) > PAXPATHLEN) {
1407 tty_prnt("File name too long, try again\n");
1408 continue;
1409 }
1410
1411 /*
1412 * try to open new archive
1413 */
1414 if (ar_open(buf) >= 0) {
1415 if (freeit) {
1416 (void)free((char *)arcname);
1417 freeit = 0;
1418 }
1419 if ((arcname = strdup(buf)) == NULL) {
1420 done = 1;
1421 lstrval = -1;
1422 tty_warn(0, "Cannot save archive name.");
1423 return(-1);
1424 }
1425 freeit = 1;
1426 break;
1427 }
1428 tty_prnt("Cannot open %s, try again\n", buf);
1429 continue;
1430 }
1431 return(0);
1432 }
1433
1434 /*
1435 * ar_start_gzip()
1436 * starts the gzip compression/decompression process as a child, using magic
1437 * to keep the fd the same in the calling function (parent).
1438 */
1439 void
1440 #ifdef __STDC__
1441 ar_start_gzip(int fd)
1442 #else
1443 ar_start_gzip(fd)
1444 int fd;
1445 #endif
1446 {
1447 pid_t pid;
1448 int fds[2];
1449 char *gzip_flags;
1450
1451 if (pipe(fds) < 0)
1452 err(1, "could not pipe");
1453 pid = fork();
1454 if (pid < 0)
1455 err(1, "could not fork");
1456
1457 /* parent */
1458 if (pid) {
1459 switch (act) {
1460 case ARCHIVE:
1461 dup2(fds[1], fd);
1462 break;
1463 case LIST:
1464 case EXTRACT:
1465 dup2(fds[0], fd);
1466 break;
1467 default:
1468 errx(1, "ar_start_gzip: impossible");
1469 }
1470 close(fds[0]);
1471 close(fds[1]);
1472 } else {
1473 switch (act) {
1474 case ARCHIVE:
1475 dup2(fds[0], STDIN_FILENO);
1476 dup2(fd, STDOUT_FILENO);
1477 gzip_flags = "-c";
1478 break;
1479 case LIST:
1480 case EXTRACT:
1481 dup2(fds[1], STDOUT_FILENO);
1482 dup2(fd, STDIN_FILENO);
1483 gzip_flags = "-dc";
1484 break;
1485 default:
1486 errx(1, "ar_start_gzip: impossible");
1487 }
1488 close(fds[0]);
1489 close(fds[1]);
1490 if (execlp(gzip_program, gzip_program, gzip_flags, NULL) < 0)
1491 err(1, "could not exec");
1492 /* NOTREACHED */
1493 }
1494 }
1495
1496 static const char *
1497 timefmt(buf, size, sz, tm)
1498 char *buf;
1499 size_t size;
1500 off_t sz;
1501 time_t tm;
1502 {
1503 #ifdef NET2_STAT
1504 (void)snprintf(buf, size, "%lu secs (%lu bytes/sec)",
1505 (unsigned long)tm, (unsigned long)(sz / tm));
1506 #else
1507 (void)snprintf(buf, size, "%lu secs (%llu bytes/sec)",
1508 (unsigned long)tm, (unsigned long long)(sz / tm));
1509 #endif
1510 return buf;
1511 }
1512
1513 static const char *
1514 sizefmt(buf, size, sz)
1515 char *buf;
1516 size_t size;
1517 off_t sz;
1518 {
1519 #ifdef NET2_STAT
1520 (void)snprintf(buf, size, "%lu bytes", (unsigned long)sz);
1521 #else
1522 (void)snprintf(buf, size, "%llu bytes", (unsigned long long)sz);
1523 #endif
1524 return buf;
1525 }
1526
1527 #if __STDC__
1528 void
1529 ar_summary(int n)
1530 #else
1531 void
1532 ar_summary(n)
1533 int n;
1534 #endif
1535 {
1536 time_t secs;
1537 int len;
1538 char buf[MAXPATHLEN];
1539 char tbuf[MAXPATHLEN/4];
1540 char s1buf[MAXPATHLEN/8];
1541 char s2buf[MAXPATHLEN/8];
1542 FILE *outf;
1543
1544 if (act == LIST)
1545 outf = stdout;
1546 else
1547 outf = stderr;
1548
1549 /*
1550 * If we are called from a signal (n != 0), use snprintf(3) so that we
1551 * don't reenter stdio(3).
1552 */
1553 (void)time(&secs);
1554 if ((secs -= starttime) == 0)
1555 secs = 1;
1556
1557 /*
1558 * If we have not determined the format yet, we just say how many bytes
1559 * we have skipped over looking for a header to id. there is no way we
1560 * could have written anything yet.
1561 */
1562 if (frmt == NULL) {
1563 len = snprintf(buf, sizeof(buf),
1564 "unknown format, %s skipped in %s\n",
1565 sizefmt(s1buf, sizeof(s1buf), rdcnt),
1566 timefmt(tbuf, sizeof(tbuf), rdcnt, secs));
1567 if (n == 0)
1568 (void)fprintf(outf, "%s: %s", argv0, buf);
1569 else
1570 (void)write(STDERR_FILENO, buf, len);
1571 return;
1572 }
1573
1574
1575 if (n != 0) {
1576 len = snprintf(buf, sizeof(buf), "Working on `%s' (%s)\n",
1577 archd.name, sizefmt(s1buf, sizeof(s1buf), archd.sb.st_size));
1578 (void)write(STDERR_FILENO, buf, len);
1579 }
1580
1581
1582 len = snprintf(buf, sizeof(buf),
1583 "%s vol %d, %lu files, %s read, %s written in %s\n",
1584 frmt->name, arvol-1, (unsigned long)flcnt,
1585 sizefmt(s1buf, sizeof(s1buf), rdcnt),
1586 sizefmt(s2buf, sizeof(s2buf), wrcnt),
1587 timefmt(tbuf, sizeof(tbuf), rdcnt + wrcnt, secs));
1588 if (n == 0)
1589 (void)fprintf(outf, "%s: %s", argv0, buf);
1590 else
1591 (void)write(STDERR_FILENO, buf, strlen(buf));
1592 }
1593
1594 /*
1595 * ar_dochdir(name)
1596 * change directory to name, and remember where we came from and
1597 * where we change to (for ar_open).
1598 *
1599 * Maybe we could try to be smart and only do the actual chdir
1600 * when necessary to write a file read from the archive, but this
1601 * is not easy to get right given the pax code structure.
1602 *
1603 * Be sure to not leak descriptors!
1604 *
1605 * We are called N * M times when extracting, and N times when
1606 * writing archives, where
1607 * N: number of -C options
1608 * M: number of files in archive
1609 *
1610 * Returns 0 if all went well, else -1.
1611 */
1612
1613 #ifdef __STDC__
1614 int
1615 ar_dochdir(char *name)
1616 #else
1617 int
1618 ar_dochdir(name)
1619 char *name;
1620 #endif
1621 {
1622 if (curdirfd == -1) {
1623 /* first time. remember where we came from */
1624 curdirfd = open(".", O_RDONLY);
1625 if (curdirfd < 0) {
1626 syswarn(0, errno, "failed to open directory .");
1627 return (-1);
1628 }
1629 } else /* XXX if (*name != '/') XXX */ {
1630 /*
1631 * relative chdir. Make sure to get the same directory
1632 * each time by fchdir-ing back first.
1633 */
1634 fchdir(curdirfd);
1635 }
1636
1637 if (minusCfd != -1) {
1638 /* don't leak descriptors */
1639 close(minusCfd);
1640 minusCfd = -1;
1641 }
1642
1643 minusCfd = open(name, O_RDONLY);
1644 if (minusCfd < 0) {
1645 syswarn(0, errno, "failed to open directory %s", name);
1646 return (-1);
1647 }
1648
1649 fchdir(minusCfd);
1650 return (0);
1651 }
1652