ar_subs.c revision 1.7 1 /* $NetBSD: ar_subs.c,v 1.7 1997/07/20 20:32:18 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 #ifndef lint
42 #if 0
43 static char sccsid[] = "@(#)ar_subs.c 8.2 (Berkeley) 4/18/94";
44 #else
45 __RCSID("$NetBSD: ar_subs.c,v 1.7 1997/07/20 20:32:18 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/param.h>
53 #include <signal.h>
54 #include <string.h>
55 #include <stdio.h>
56 #include <ctype.h>
57 #include <fcntl.h>
58 #include <errno.h>
59 #include <unistd.h>
60 #include <stdlib.h>
61 #include "pax.h"
62 #include "extern.h"
63
64 static void wr_archive __P((ARCHD *, int is_app));
65 static int get_arc __P((void));
66 static int next_head __P((ARCHD *));
67 extern sigset_t s_mask;
68
69 /*
70 * Routines which control the overall operation modes of pax as specified by
71 * the user: list, append, read ...
72 */
73
74 static char hdbuf[BLKMULT]; /* space for archive header on read */
75 u_long flcnt; /* number of files processed */
76
77 /*
78 * list()
79 * list the contents of an archive which match user supplied pattern(s)
80 * (no pattern matches all).
81 */
82
83 #if __STDC__
84 void
85 list(void)
86 #else
87 void
88 list()
89 #endif
90 {
91 ARCHD *arcn;
92 int res;
93 ARCHD archd;
94 time_t now;
95
96 arcn = &archd;
97 /*
98 * figure out archive type; pass any format specific options to the
99 * archive option processing routine; call the format init routine. We
100 * also save current time for ls_list() so we do not make a system
101 * call for each file we need to print. If verbose (vflag) start up
102 * the name and group caches.
103 */
104 if ((get_arc() < 0) || ((*frmt->options)() < 0) ||
105 ((*frmt->st_rd)() < 0))
106 return;
107
108 if (vflag && ((uidtb_start() < 0) || (gidtb_start() < 0)))
109 return;
110
111 now = time((time_t *)NULL);
112
113 /*
114 * step through the archive until the format says it is done
115 */
116 while (next_head(arcn) == 0) {
117 /*
118 * check for pattern, and user specified options match.
119 * When all patterns are matched we are done.
120 */
121 if ((res = pat_match(arcn)) < 0)
122 break;
123
124 if ((res == 0) && (sel_chk(arcn) == 0)) {
125 /*
126 * pattern resulted in a selected file
127 */
128 if (pat_sel(arcn) < 0)
129 break;
130
131 /*
132 * modify the name as requested by the user if name
133 * survives modification, do a listing of the file
134 */
135 if ((res = mod_name(arcn)) < 0)
136 break;
137 if (res == 0)
138 ls_list(arcn, now);
139 }
140
141 /*
142 * skip to next archive format header using values calculated
143 * by the format header read routine
144 */
145 if (rd_skip(arcn->skip + arcn->pad) == 1)
146 break;
147 }
148
149 /*
150 * all done, let format have a chance to cleanup, and make sure that
151 * the patterns supplied by the user were all matched
152 */
153 (void)(*frmt->end_rd)();
154 (void)sigprocmask(SIG_BLOCK, &s_mask, (sigset_t *)NULL);
155 ar_close();
156 pat_chk();
157 }
158
159 /*
160 * extract()
161 * extract the member(s) of an archive as specified by user supplied
162 * pattern(s) (no patterns extracts all members)
163 */
164
165 #if __STDC__
166 void
167 extract(void)
168 #else
169 void
170 extract()
171 #endif
172 {
173 ARCHD *arcn;
174 int res;
175 off_t cnt;
176 ARCHD archd;
177 struct stat sb;
178 int fd;
179
180 arcn = &archd;
181 /*
182 * figure out archive type; pass any format specific options to the
183 * archive option processing routine; call the format init routine;
184 * start up the directory modification time and access mode database
185 */
186 if ((get_arc() < 0) || ((*frmt->options)() < 0) ||
187 ((*frmt->st_rd)() < 0) || (dir_start() < 0))
188 return;
189
190 /*
191 * When we are doing interactive rename, we store the mapping of names
192 * so we can fix up hard links files later in the archive.
193 */
194 if (iflag && (name_start() < 0))
195 return;
196
197 /*
198 * step through each entry on the archive until the format read routine
199 * says it is done
200 */
201 while (next_head(arcn) == 0) {
202
203 /*
204 * check for pattern, and user specified options match. When
205 * all the patterns are matched we are done
206 */
207 if ((res = pat_match(arcn)) < 0)
208 break;
209
210 if ((res > 0) || (sel_chk(arcn) != 0)) {
211 /*
212 * file is not selected. skip past any file data and
213 * padding and go back for the next archive member
214 */
215 (void)rd_skip(arcn->skip + arcn->pad);
216 continue;
217 }
218
219 /*
220 * with -u or -D only extract when the archive member is newer
221 * than the file with the same name in the file system (nos
222 * test of being the same type is required).
223 * NOTE: this test is done BEFORE name modifications as
224 * specified by pax. this operation can be confusing to the
225 * user who might expect the test to be done on an existing
226 * file AFTER the name mod. In honesty the pax spec is probably
227 * flawed in this respect.
228 */
229 if ((uflag || Dflag) && ((lstat(arcn->name, &sb) == 0))) {
230 if (uflag && Dflag) {
231 if ((arcn->sb.st_mtime <= sb.st_mtime) &&
232 (arcn->sb.st_ctime <= sb.st_ctime)) {
233 (void)rd_skip(arcn->skip + arcn->pad);
234 continue;
235 }
236 } else if (Dflag) {
237 if (arcn->sb.st_ctime <= sb.st_ctime) {
238 (void)rd_skip(arcn->skip + arcn->pad);
239 continue;
240 }
241 } else if (arcn->sb.st_mtime <= sb.st_mtime) {
242 (void)rd_skip(arcn->skip + arcn->pad);
243 continue;
244 }
245 }
246
247 /*
248 * this archive member is now been selected. modify the name.
249 */
250 if ((pat_sel(arcn) < 0) || ((res = mod_name(arcn)) < 0))
251 break;
252 if (res > 0) {
253 /*
254 * a bad name mod, skip and purge name from link table
255 */
256 purg_lnk(arcn);
257 (void)rd_skip(arcn->skip + arcn->pad);
258 continue;
259 }
260
261 /*
262 * Non standard -Y and -Z flag. When the exisiting file is
263 * same age or newer skip
264 */
265 if ((Yflag || Zflag) && ((lstat(arcn->name, &sb) == 0))) {
266 if (Yflag && Zflag) {
267 if ((arcn->sb.st_mtime <= sb.st_mtime) &&
268 (arcn->sb.st_ctime <= sb.st_ctime)) {
269 (void)rd_skip(arcn->skip + arcn->pad);
270 continue;
271 }
272 } else if (Yflag) {
273 if (arcn->sb.st_ctime <= sb.st_ctime) {
274 (void)rd_skip(arcn->skip + arcn->pad);
275 continue;
276 }
277 } else if (arcn->sb.st_mtime <= sb.st_mtime) {
278 (void)rd_skip(arcn->skip + arcn->pad);
279 continue;
280 }
281 }
282
283 if (vflag) {
284 (void)fputs(arcn->name, stderr);
285 vfpart = 1;
286 }
287
288 /*
289 * all ok, extract this member based on type
290 */
291 if ((arcn->type != PAX_REG) && (arcn->type != PAX_CTG)) {
292 /*
293 * process archive members that are not regular files.
294 * throw out padding and any data that might follow the
295 * header (as determined by the format).
296 */
297 if ((arcn->type == PAX_HLK) || (arcn->type == PAX_HRG))
298 res = lnk_creat(arcn);
299 else
300 res = node_creat(arcn);
301
302 (void)rd_skip(arcn->skip + arcn->pad);
303 if (res < 0)
304 purg_lnk(arcn);
305
306 if (vflag && vfpart) {
307 (void)putc('\n', stderr);
308 vfpart = 0;
309 }
310 continue;
311 }
312 /*
313 * we have a file with data here. If we can not create it, skip
314 * over the data and purge the name from hard link table
315 */
316 if ((fd = file_creat(arcn)) < 0) {
317 (void)rd_skip(arcn->skip + arcn->pad);
318 purg_lnk(arcn);
319 continue;
320 }
321 /*
322 * extract the file from the archive and skip over padding and
323 * any unprocessed data
324 */
325 res = (*frmt->rd_data)(arcn, fd, &cnt);
326 file_close(arcn, fd);
327 if (vflag && vfpart) {
328 (void)putc('\n', stderr);
329 vfpart = 0;
330 }
331 if (!res)
332 (void)rd_skip(cnt + arcn->pad);
333 }
334
335 /*
336 * all done, restore directory modes and times as required; make sure
337 * all patterns supplied by the user were matched; block off signals
338 * to avoid chance for multiple entry into the cleanup code.
339 */
340 (void)(*frmt->end_rd)();
341 (void)sigprocmask(SIG_BLOCK, &s_mask, (sigset_t *)NULL);
342 ar_close();
343 proc_dir();
344 pat_chk();
345 }
346
347 /*
348 * wr_archive()
349 * Write an archive. used in both creating a new archive and appends on
350 * previously written archive.
351 */
352
353 #if __STDC__
354 static void
355 wr_archive(ARCHD *arcn, int is_app)
356 #else
357 static void
358 wr_archive(arcn, is_app)
359 ARCHD *arcn;
360 int is_app;
361 #endif
362 {
363 int res;
364 int hlk;
365 int wr_one;
366 off_t cnt;
367 int (*wrf) __P((ARCHD *));
368 int fd = -1;
369
370 /*
371 * if this format supports hard link storage, start up the database
372 * that detects them.
373 */
374 if (((hlk = frmt->hlk) == 1) && (lnk_start() < 0))
375 return;
376
377 /*
378 * start up the file traversal code and format specific write
379 */
380 if ((ftree_start() < 0) || ((*frmt->st_wr)() < 0))
381 return;
382 wrf = frmt->wr;
383
384 /*
385 * When we are doing interactive rename, we store the mapping of names
386 * so we can fix up hard links files later in the archive.
387 */
388 if (iflag && (name_start() < 0))
389 return;
390
391 /*
392 * if this not append, and there are no files, we do no write a trailer
393 */
394 wr_one = is_app;
395
396 /*
397 * while there are files to archive, process them one at at time
398 */
399 while (next_file(arcn) == 0) {
400 /*
401 * check if this file meets user specified options match.
402 */
403 if (sel_chk(arcn) != 0)
404 continue;
405 fd = -1;
406 if (uflag) {
407 /*
408 * only archive if this file is newer than a file with
409 * the same name that is already stored on the archive
410 */
411 if ((res = chk_ftime(arcn)) < 0)
412 break;
413 if (res > 0)
414 continue;
415 }
416
417 /*
418 * this file is considered selected now. see if this is a hard
419 * link to a file already stored
420 */
421 ftree_sel(arcn);
422 if (hlk && (chk_lnk(arcn) < 0))
423 break;
424
425 if ((arcn->type == PAX_REG) || (arcn->type == PAX_HRG) ||
426 (arcn->type == PAX_CTG)) {
427 /*
428 * we will have to read this file. by opening it now we
429 * can avoid writing a header to the archive for a file
430 * we were later unable to read (we also purge it from
431 * the link table).
432 */
433 if ((fd = open(arcn->org_name, O_RDONLY, 0)) < 0) {
434 syswarn(1,errno, "Unable to open %s to read",
435 arcn->org_name);
436 purg_lnk(arcn);
437 continue;
438 }
439 }
440
441 /*
442 * Now modify the name as requested by the user
443 */
444 if ((res = mod_name(arcn)) < 0) {
445 /*
446 * name modification says to skip this file, close the
447 * file and purge link table entry
448 */
449 rdfile_close(arcn, &fd);
450 purg_lnk(arcn);
451 break;
452 }
453
454 if ((res > 0) || (docrc && (set_crc(arcn, fd) < 0))) {
455 /*
456 * unable to obtain the crc we need, close the file,
457 * purge link table entry
458 */
459 rdfile_close(arcn, &fd);
460 purg_lnk(arcn);
461 continue;
462 }
463
464 if (vflag) {
465 (void)fputs(arcn->name, stderr);
466 vfpart = 1;
467 }
468 ++flcnt;
469
470 /*
471 * looks safe to store the file, have the format specific
472 * routine write routine store the file header on the archive
473 */
474 if ((res = (*wrf)(arcn)) < 0) {
475 rdfile_close(arcn, &fd);
476 break;
477 }
478 wr_one = 1;
479 if (res > 0) {
480 /*
481 * format write says no file data needs to be stored
482 * so we are done messing with this file
483 */
484 if (vflag && vfpart) {
485 (void)putc('\n', stderr);
486 vfpart = 0;
487 }
488 rdfile_close(arcn, &fd);
489 continue;
490 }
491
492 /*
493 * Add file data to the archive, quit on write error. if we
494 * cannot write the entire file contents to the archive we
495 * must pad the archive to replace the missing file data
496 * (otherwise during an extract the file header for the file
497 * which FOLLOWS this one will not be where we expect it to
498 * be).
499 */
500 res = (*frmt->wr_data)(arcn, fd, &cnt);
501 rdfile_close(arcn, &fd);
502 if (vflag && vfpart) {
503 (void)putc('\n', stderr);
504 vfpart = 0;
505 }
506 if (res < 0)
507 break;
508
509 /*
510 * pad as required, cnt is number of bytes not written
511 */
512 if (((cnt > 0) && (wr_skip(cnt) < 0)) ||
513 ((arcn->pad > 0) && (wr_skip(arcn->pad) < 0)))
514 break;
515 }
516
517 /*
518 * tell format to write trailer; pad to block boundry; reset directory
519 * mode/access times, and check if all patterns supplied by the user
520 * were matched. block off signals to avoid chance for multiple entry
521 * into the cleanup code
522 */
523 if (wr_one) {
524 (*frmt->end_wr)();
525 wr_fin();
526 }
527 (void)sigprocmask(SIG_BLOCK, &s_mask, (sigset_t *)NULL);
528 ar_close();
529 if (tflag)
530 proc_dir();
531 ftree_chk();
532 }
533
534 /*
535 * append()
536 * Add file to previously written archive. Archive format specified by the
537 * user must agree with archive. The archive is read first to collect
538 * modification times (if -u) and locate the archive trailer. The archive
539 * is positioned in front of the record with the trailer and wr_archive()
540 * is called to add the new members.
541 * PAX IMPLEMENTATION DETAIL NOTE:
542 * -u is implemented by adding the new members to the end of the archive.
543 * Care is taken so that these do not end up as links to the older
544 * version of the same file already stored in the archive. It is expected
545 * when extraction occurs these newer versions will over-write the older
546 * ones stored "earlier" in the archive (this may be a bad assumption as
547 * it depends on the implementation of the program doing the extraction).
548 * It is really difficult to splice in members without either re-writing
549 * the entire archive (from the point were the old version was), or having
550 * assistance of the format specification in terms of a special update
551 * header that invalidates a previous archive record. The posix spec left
552 * the method used to implement -u unspecified. This pax is able to
553 * over write existing files that it creates.
554 */
555
556 #if __STDC__
557 void
558 append(void)
559 #else
560 void
561 append()
562 #endif
563 {
564 ARCHD *arcn;
565 int res;
566 ARCHD archd;
567 FSUB *orgfrmt;
568 int udev;
569 off_t tlen;
570
571 arcn = &archd;
572 orgfrmt = frmt;
573
574 /*
575 * Do not allow an append operation if the actual archive is of a
576 * different format than the user specified foramt.
577 */
578 if (get_arc() < 0)
579 return;
580 if ((orgfrmt != NULL) && (orgfrmt != frmt)) {
581 tty_warn(1, "Cannot mix current archive format %s with %s",
582 frmt->name, orgfrmt->name);
583 return;
584 }
585
586 /*
587 * pass the format any options and start up format
588 */
589 if (((*frmt->options)() < 0) || ((*frmt->st_rd)() < 0))
590 return;
591
592 /*
593 * if we only are adding members that are newer, we need to save the
594 * mod times for all files we see.
595 */
596 if (uflag && (ftime_start() < 0))
597 return;
598
599 /*
600 * some archive formats encode hard links by recording the device and
601 * file serial number (inode) but copy the file anyway (multiple times)
602 * to the archive. When we append, we run the risk that newly added
603 * files may have the same device and inode numbers as those recorded
604 * on the archive but during a previous run. If this happens, when the
605 * archive is extracted we get INCORRECT hard links. We avoid this by
606 * remapping the device numbers so that newly added files will never
607 * use the same device number as one found on the archive. remapping
608 * allows new members to safely have links among themselves. remapping
609 * also avoids problems with file inode (serial number) truncations
610 * when the inode number is larger than storage space in the archive
611 * header. See the remap routines for more details.
612 */
613 if ((udev = frmt->udev) && (dev_start() < 0))
614 return;
615
616 /*
617 * reading the archive may take a long time. If verbose tell the user
618 */
619 if (vflag) {
620 (void)fprintf(stderr,
621 "%s: Reading archive to position at the end...", argv0);
622 vfpart = 1;
623 }
624
625 /*
626 * step through the archive until the format says it is done
627 */
628 while (next_head(arcn) == 0) {
629 /*
630 * check if this file meets user specified options.
631 */
632 if (sel_chk(arcn) != 0) {
633 if (rd_skip(arcn->skip + arcn->pad) == 1)
634 break;
635 continue;
636 }
637
638 if (uflag) {
639 /*
640 * see if this is the newest version of this file has
641 * already been seen, if so skip.
642 */
643 if ((res = chk_ftime(arcn)) < 0)
644 break;
645 if (res > 0) {
646 if (rd_skip(arcn->skip + arcn->pad) == 1)
647 break;
648 continue;
649 }
650 }
651
652 /*
653 * Store this device number. Device numbers seen during the
654 * read phase of append will cause newly appended files with a
655 * device number seen in the old part of the archive to be
656 * remapped to an unused device number.
657 */
658 if ((udev && (add_dev(arcn) < 0)) ||
659 (rd_skip(arcn->skip + arcn->pad) == 1))
660 break;
661 }
662
663 /*
664 * done, finish up read and get the number of bytes to back up so we
665 * can add new members. The format might have used the hard link table,
666 * purge it.
667 */
668 tlen = (*frmt->end_rd)();
669 lnk_end();
670
671 /*
672 * try to postion for write, if this fails quit. if any error occurs,
673 * we will refuse to write
674 */
675 if (appnd_start(tlen) < 0)
676 return;
677
678 /*
679 * tell the user we are done reading.
680 */
681 if (vflag && vfpart) {
682 (void)fputs("done.\n", stderr);
683 vfpart = 0;
684 }
685
686 /*
687 * go to the writing phase to add the new members
688 */
689 wr_archive(arcn, 1);
690 }
691
692 /*
693 * archive()
694 * write a new archive
695 */
696
697 #if __STDC__
698 void
699 archive(void)
700 #else
701 void
702 archive()
703 #endif
704 {
705 ARCHD archd;
706
707 /*
708 * if we only are adding members that are newer, we need to save the
709 * mod times for all files; set up for writing; pass the format any
710 * options write the archive
711 */
712 if ((uflag && (ftime_start() < 0)) || (wr_start() < 0))
713 return;
714 if ((*frmt->options)() < 0)
715 return;
716
717 wr_archive(&archd, 0);
718 }
719
720 /*
721 * copy()
722 * copy files from one part of the file system to another. this does not
723 * use any archive storage. The EFFECT OF THE COPY IS THE SAME as if an
724 * archive was written and then extracted in the destination directory
725 * (except the files are forced to be under the destination directory).
726 */
727
728 #if __STDC__
729 void
730 copy(void)
731 #else
732 void
733 copy()
734 #endif
735 {
736 ARCHD *arcn;
737 int res;
738 int fddest;
739 char *dest_pt;
740 int dlen;
741 int drem;
742 int fdsrc = -1;
743 struct stat sb;
744 ARCHD archd;
745 char dirbuf[PAXPATHLEN+1];
746
747 arcn = &archd;
748 /*
749 * set up the destination dir path and make sure it is a directory. We
750 * make sure we have a trailing / on the destination
751 */
752 dlen = l_strncpy(dirbuf, dirptr, PAXPATHLEN);
753 dest_pt = dirbuf + dlen;
754 if (*(dest_pt-1) != '/') {
755 *dest_pt++ = '/';
756 ++dlen;
757 }
758 *dest_pt = '\0';
759 drem = PAXPATHLEN - dlen;
760
761 if (stat(dirptr, &sb) < 0) {
762 syswarn(1, errno, "Cannot access destination directory %s",
763 dirptr);
764 return;
765 }
766 if (!S_ISDIR(sb.st_mode)) {
767 tty_warn(1, "Destination is not a directory %s", dirptr);
768 return;
769 }
770
771 /*
772 * start up the hard link table; file traversal routines and the
773 * modification time and access mode database
774 */
775 if ((lnk_start() < 0) || (ftree_start() < 0) || (dir_start() < 0))
776 return;
777
778 /*
779 * When we are doing interactive rename, we store the mapping of names
780 * so we can fix up hard links files later in the archive.
781 */
782 if (iflag && (name_start() < 0))
783 return;
784
785 /*
786 * set up to cp file trees
787 */
788 cp_start();
789
790 /*
791 * while there are files to archive, process them
792 */
793 while (next_file(arcn) == 0) {
794 fdsrc = -1;
795
796 /*
797 * check if this file meets user specified options
798 */
799 if (sel_chk(arcn) != 0)
800 continue;
801
802 /*
803 * if there is already a file in the destination directory with
804 * the same name and it is newer, skip the one stored on the
805 * archive.
806 * NOTE: this test is done BEFORE name modifications as
807 * specified by pax. this can be confusing to the user who
808 * might expect the test to be done on an existing file AFTER
809 * the name mod. In honesty the pax spec is probably flawed in
810 * this respect
811 */
812 if (uflag || Dflag) {
813 /*
814 * create the destination name
815 */
816 if (*(arcn->name) == '/')
817 res = 1;
818 else
819 res = 0;
820 if ((arcn->nlen - res) > drem) {
821 tty_warn(1, "Destination pathname too long %s",
822 arcn->name);
823 continue;
824 }
825 (void)strncpy(dest_pt, arcn->name + res, drem);
826 dirbuf[PAXPATHLEN] = '\0';
827
828 /*
829 * if existing file is same age or newer skip
830 */
831 res = lstat(dirbuf, &sb);
832 *dest_pt = '\0';
833
834 if (res == 0) {
835 if (uflag && Dflag) {
836 if ((arcn->sb.st_mtime<=sb.st_mtime) &&
837 (arcn->sb.st_ctime<=sb.st_ctime))
838 continue;
839 } else if (Dflag) {
840 if (arcn->sb.st_ctime <= sb.st_ctime)
841 continue;
842 } else if (arcn->sb.st_mtime <= sb.st_mtime)
843 continue;
844 }
845 }
846
847 /*
848 * this file is considered selected. See if this is a hard link
849 * to a previous file; modify the name as requested by the
850 * user; set the final destination.
851 */
852 ftree_sel(arcn);
853 if ((chk_lnk(arcn) < 0) || ((res = mod_name(arcn)) < 0))
854 break;
855 if ((res > 0) || (set_dest(arcn, dirbuf, dlen) < 0)) {
856 /*
857 * skip file, purge from link table
858 */
859 purg_lnk(arcn);
860 continue;
861 }
862
863 /*
864 * Non standard -Y and -Z flag. When the exisiting file is
865 * same age or newer skip
866 */
867 if ((Yflag || Zflag) && ((lstat(arcn->name, &sb) == 0))) {
868 if (Yflag && Zflag) {
869 if ((arcn->sb.st_mtime <= sb.st_mtime) &&
870 (arcn->sb.st_ctime <= sb.st_ctime))
871 continue;
872 } else if (Yflag) {
873 if (arcn->sb.st_ctime <= sb.st_ctime)
874 continue;
875 } else if (arcn->sb.st_mtime <= sb.st_mtime)
876 continue;
877 }
878
879 if (vflag) {
880 (void)fputs(arcn->name, stderr);
881 vfpart = 1;
882 }
883 ++flcnt;
884
885 /*
886 * try to create a hard link to the src file if requested
887 * but make sure we are not trying to overwrite ourselves.
888 */
889 if (lflag)
890 res = cross_lnk(arcn);
891 else
892 res = chk_same(arcn);
893 if (res <= 0) {
894 if (vflag && vfpart) {
895 (void)putc('\n', stderr);
896 vfpart = 0;
897 }
898 continue;
899 }
900
901 /*
902 * have to create a new file
903 */
904 if ((arcn->type != PAX_REG) && (arcn->type != PAX_CTG)) {
905 /*
906 * create a link or special file
907 */
908 if ((arcn->type == PAX_HLK) || (arcn->type == PAX_HRG))
909 res = lnk_creat(arcn);
910 else
911 res = node_creat(arcn);
912 if (res < 0)
913 purg_lnk(arcn);
914 if (vflag && vfpart) {
915 (void)putc('\n', stderr);
916 vfpart = 0;
917 }
918 continue;
919 }
920
921 /*
922 * have to copy a regular file to the destination directory.
923 * first open source file and then create the destination file
924 */
925 if ((fdsrc = open(arcn->org_name, O_RDONLY, 0)) < 0) {
926 syswarn(1, errno, "Unable to open %s to read",
927 arcn->org_name);
928 purg_lnk(arcn);
929 continue;
930 }
931 if ((fddest = file_creat(arcn)) < 0) {
932 rdfile_close(arcn, &fdsrc);
933 purg_lnk(arcn);
934 continue;
935 }
936
937 /*
938 * copy source file data to the destination file
939 */
940 cp_file(arcn, fdsrc, fddest);
941 file_close(arcn, fddest);
942 rdfile_close(arcn, &fdsrc);
943
944 if (vflag && vfpart) {
945 (void)putc('\n', stderr);
946 vfpart = 0;
947 }
948 }
949
950 /*
951 * restore directory modes and times as required; make sure all
952 * patterns were selected block off signals to avoid chance for
953 * multiple entry into the cleanup code.
954 */
955 (void)sigprocmask(SIG_BLOCK, &s_mask, (sigset_t *)NULL);
956 ar_close();
957 proc_dir();
958 ftree_chk();
959 }
960
961 /*
962 * next_head()
963 * try to find a valid header in the archive. Uses format specific
964 * routines to extract the header and id the trailer. Trailers may be
965 * located within a valid header or in an invalid header (the location
966 * is format specific. The inhead field from the option table tells us
967 * where to look for the trailer).
968 * We keep reading (and resyncing) until we get enough contiguous data
969 * to check for a header. If we cannot find one, we shift by a byte
970 * add a new byte from the archive to the end of the buffer and try again.
971 * If we get a read error, we throw out what we have (as we must have
972 * contiguous data) and start over again.
973 * ASSUMED: headers fit within a BLKMULT header.
974 * Return:
975 * 0 if we got a header, -1 if we are unable to ever find another one
976 * (we reached the end of input, or we reached the limit on retries. see
977 * the specs for rd_wrbuf() for more details)
978 */
979
980 #if __STDC__
981 static int
982 next_head(ARCHD *arcn)
983 #else
984 static int
985 next_head(arcn)
986 ARCHD *arcn;
987 #endif
988 {
989 int ret;
990 char *hdend;
991 int res;
992 int shftsz;
993 int hsz;
994 int in_resync = 0; /* set when we are in resync mode */
995 int cnt = 0; /* counter for trailer function */
996
997 /*
998 * set up initial conditions, we want a whole frmt->hsz block as we
999 * have no data yet.
1000 */
1001 res = hsz = frmt->hsz;
1002 hdend = hdbuf;
1003 shftsz = hsz - 1;
1004 for(;;) {
1005 /*
1006 * keep looping until we get a contiguous FULL buffer
1007 * (frmt->hsz is the proper size)
1008 */
1009 for (;;) {
1010 if ((ret = rd_wrbuf(hdend, res)) == res)
1011 break;
1012
1013 /*
1014 * some kind of archive read problem, try to resync the
1015 * storage device, better give the user the bad news.
1016 */
1017 if ((ret == 0) || (rd_sync() < 0)) {
1018 tty_warn(1,
1019 "Premature end of file on archive read");
1020 return(-1);
1021 }
1022 if (!in_resync) {
1023 if (act == APPND) {
1024 tty_warn(1,
1025 "Archive I/O error, cannot continue");
1026 return(-1);
1027 }
1028 tty_warn(1,
1029 "Archive I/O error. Trying to recover.");
1030 ++in_resync;
1031 }
1032
1033 /*
1034 * oh well, throw it all out and start over
1035 */
1036 res = hsz;
1037 hdend = hdbuf;
1038 }
1039
1040 /*
1041 * ok we have a contiguous buffer of the right size. Call the
1042 * format read routine. If this was not a valid header and this
1043 * format stores trailers outside of the header, call the
1044 * format specific trailer routine to check for a trailer. We
1045 * have to watch out that we do not mis-identify file data or
1046 * block padding as a header or trailer. Format specific
1047 * trailer functions must NOT check for the trailer while we
1048 * are running in resync mode. Some trailer functions may tell
1049 * us that this block cannot contain a valid header either, so
1050 * we then throw out the entire block and start over.
1051 */
1052 if ((*frmt->rd)(arcn, hdbuf) == 0)
1053 break;
1054
1055 if (!frmt->inhead) {
1056 /*
1057 * this format has trailers outside of valid headers
1058 */
1059 if ((ret = (*frmt->trail)(hdbuf,in_resync,&cnt)) == 0){
1060 /*
1061 * valid trailer found, drain input as required
1062 */
1063 ar_drain();
1064 return(-1);
1065 }
1066
1067 if (ret == 1) {
1068 /*
1069 * we are in resync and we were told to throw
1070 * the whole block out because none of the
1071 * bytes in this block can be used to form a
1072 * valid header
1073 */
1074 res = hsz;
1075 hdend = hdbuf;
1076 continue;
1077 }
1078 }
1079
1080 /*
1081 * Brute force section.
1082 * not a valid header. We may be able to find a header yet. So
1083 * we shift over by one byte, and set up to read one byte at a
1084 * time from the archive and place it at the end of the buffer.
1085 * We will keep moving byte at a time until we find a header or
1086 * get a read error and have to start over.
1087 */
1088 if (!in_resync) {
1089 if (act == APPND) {
1090 tty_warn(1,
1091 "Unable to append, archive header flaw");
1092 return(-1);
1093 }
1094 tty_warn(1,
1095 "Invalid header, starting valid header search.");
1096 ++in_resync;
1097 }
1098 memmove(hdbuf, hdbuf+1, shftsz);
1099 res = 1;
1100 hdend = hdbuf + shftsz;
1101 }
1102
1103 /*
1104 * ok got a valid header, check for trailer if format encodes it in the
1105 * the header. NOTE: the parameters are different than trailer routines
1106 * which encode trailers outside of the header!
1107 */
1108 if (frmt->inhead && ((*frmt->subtrail)(arcn) == 0)) {
1109 /*
1110 * valid trailer found, drain input as required
1111 */
1112 ar_drain();
1113 return(-1);
1114 }
1115
1116 ++flcnt;
1117 return(0);
1118 }
1119
1120 /*
1121 * get_arc()
1122 * Figure out what format an archive is. Handles archive with flaws by
1123 * brute force searches for a legal header in any supported format. The
1124 * format id routines have to be careful to NOT mis-identify a format.
1125 * ASSUMED: headers fit within a BLKMULT header.
1126 * Return:
1127 * 0 if archive found -1 otherwise
1128 */
1129
1130 #if __STDC__
1131 static int
1132 get_arc(void)
1133 #else
1134 static int
1135 get_arc()
1136 #endif
1137 {
1138 int i;
1139 int hdsz = 0;
1140 int res;
1141 int minhd = BLKMULT;
1142 char *hdend;
1143 int notice = 0;
1144
1145 /*
1146 * find the smallest header size in all archive formats and then set up
1147 * to read the archive.
1148 */
1149 for (i = 0; ford[i] >= 0; ++i) {
1150 if (fsub[ford[i]].hsz < minhd)
1151 minhd = fsub[ford[i]].hsz;
1152 }
1153 if (rd_start() < 0)
1154 return(-1);
1155 res = BLKMULT;
1156 hdsz = 0;
1157 hdend = hdbuf;
1158 for(;;) {
1159 for (;;) {
1160 /*
1161 * fill the buffer with at least the smallest header
1162 */
1163 i = rd_wrbuf(hdend, res);
1164 if (i > 0)
1165 hdsz += i;
1166 if (hdsz >= minhd)
1167 break;
1168
1169 /*
1170 * if we cannot recover from a read error quit
1171 */
1172 if ((i == 0) || (rd_sync() < 0))
1173 goto out;
1174
1175 /*
1176 * when we get an error none of the data we already
1177 * have can be used to create a legal header (we just
1178 * got an error in the middle), so we throw it all out
1179 * and refill the buffer with fresh data.
1180 */
1181 res = BLKMULT;
1182 hdsz = 0;
1183 hdend = hdbuf;
1184 if (!notice) {
1185 if (act == APPND)
1186 return(-1);
1187 tty_warn(1,
1188 "Cannot identify format. Searching...");
1189 ++notice;
1190 }
1191 }
1192
1193 /*
1194 * we have at least the size of the smallest header in any
1195 * archive format. Look to see if we have a match. The array
1196 * ford[] is used to specify the header id order to reduce the
1197 * chance of incorrectly id'ing a valid header (some formats
1198 * may be subsets of each other and the order would then be
1199 * important).
1200 */
1201 for (i = 0; ford[i] >= 0; ++i) {
1202 if ((*fsub[ford[i]].id)(hdbuf, hdsz) < 0)
1203 continue;
1204 frmt = &(fsub[ford[i]]);
1205 /*
1206 * yuck, to avoid slow special case code in the extract
1207 * routines, just push this header back as if it was
1208 * not seen. We have left extra space at start of the
1209 * buffer for this purpose. This is a bit ugly, but
1210 * adding all the special case code is far worse.
1211 */
1212 pback(hdbuf, hdsz);
1213 return(0);
1214 }
1215
1216 /*
1217 * We have a flawed archive, no match. we start searching, but
1218 * we never allow additions to flawed archives
1219 */
1220 if (!notice) {
1221 if (act == APPND)
1222 return(-1);
1223 tty_warn(1, "Cannot identify format. Searching...");
1224 ++notice;
1225 }
1226
1227 /*
1228 * brute force search for a header that we can id.
1229 * we shift through byte at a time. this is slow, but we cannot
1230 * determine the nature of the flaw in the archive in a
1231 * portable manner
1232 */
1233 if (--hdsz > 0) {
1234 memmove(hdbuf, hdbuf+1, hdsz);
1235 res = BLKMULT - hdsz;
1236 hdend = hdbuf + hdsz;
1237 } else {
1238 res = BLKMULT;
1239 hdend = hdbuf;
1240 hdsz = 0;
1241 }
1242 }
1243
1244 out:
1245 /*
1246 * we cannot find a header, bow, apologize and quit
1247 */
1248 tty_warn(1, "Sorry, unable to determine archive format.");
1249 return(-1);
1250 }
1251