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