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