1 /* $NetBSD: ar_subs.c,v 1.54 2007/05/04 21:19:36 christos Exp $ */
4 * Copyright (c) 1992 Keith Muller.
5 * Copyright (c) 1992, 1993
6 * The Regents of the University of California. All rights reserved.
8 * This code is derived from software contributed to Berkeley by
9 * Keith Muller of the University of California, San Diego.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 #if HAVE_NBTOOL_CONFIG_H
37 #include "nbtool_config.h"
40 #include <sys/cdefs.h>
43 static char sccsid
[] = "@(#)ar_subs.c 8.2 (Berkeley) 4/18/94";
45 __RCSID("$NetBSD: ar_subs.c,v 1.54 2007/05/04 21:19:36 christos Exp $");
49 #include <sys/types.h>
52 #include <sys/param.h>
66 static int path_check(ARCHD
*, int);
67 static int wr_archive(ARCHD
*, int is_app
);
68 static int get_arc(void);
69 static int next_head(ARCHD
*);
70 #if !HAVE_NBTOOL_CONFIG_H
71 static int fdochroot(int);
73 extern sigset_t s_mask
;
76 * Routines which control the overall operation modes of pax as specified by
77 * the user: list, append, read ...
80 static char hdbuf
[BLKMULT
]; /* space for archive header on read */
81 u_long flcnt
; /* number of files processed */
84 static char cwdpath
[MAXPATHLEN
]; /* current working directory path */
85 static size_t cwdpathlen
; /* current working directory path len */
90 if (getcwd(cwdpath
, sizeof(cwdpath
)) == NULL
) {
91 syswarn(1, errno
, "Cannot get working directory");
94 cwdpathlen
= strlen(cwdpath
);
101 if (fchdir(fcwd
) == -1) {
102 syswarn(1, errno
, "Cannot chdir to `.'");
109 dochdir(const char *name
)
111 if (chdir(name
) == -1)
112 syswarn(1, errno
, "Cannot chdir to `%s'", name
);
116 #if !HAVE_NBTOOL_CONFIG_H
120 if (fchroot(fcwd
) != 0) {
121 syswarn(1, errno
, "Can't fchroot to \".\"");
129 * mkdir(), but if we failed, check if someone else made it for us
130 * already and don't error out.
133 domkdir(const char *fname
, mode_t mode
)
138 if ((error
= mkdir(fname
, mode
)) != -1)
146 case ENOSYS
: /* Grr Solaris */
149 if (stat(fname
, &sb
) != -1 && S_ISDIR(sb
.st_mode
))
159 path_check(ARCHD
*arcn
, int level
)
161 char buf
[MAXPATHLEN
];
164 if ((p
= strrchr(arcn
->name
, '/')) == NULL
)
168 if (realpath(arcn
->name
, buf
) == NULL
) {
170 error
= path_check(arcn
, level
+ 1);
175 syswarn(1, 0, "Cannot resolve `%s'", arcn
->name
);
178 if (strncmp(buf
, cwdpath
, cwdpathlen
) != 0) {
180 syswarn(1, 0, "Attempt to write file `%s' that resolves into "
181 "`%s/%s' outside current working directory `%s' ignored",
182 arcn
->name
, buf
, p
+ 1, cwdpath
);
191 * list the contents of an archive which match user supplied pattern(s)
192 * (if no pattern is supplied, list entire contents).
204 * figure out archive type; pass any format specific options to the
205 * archive option processing routine; call the format init routine. We
206 * also save current time for ls_list() so we do not make a system
207 * call for each file we need to print. If verbose (vflag) start up
208 * the name and group caches.
210 if ((get_arc() < 0) || ((*frmt
->options
)() < 0) ||
211 ((*frmt
->st_rd
)() < 0))
214 now
= time((time_t *)NULL
);
217 * step through the archive until the format says it is done
219 while (next_head(arcn
) == 0) {
220 if (arcn
->type
== PAX_GLL
|| arcn
->type
== PAX_GLF
) {
222 * we need to read, to get the real filename
225 if (!(*frmt
->rd_data
)(arcn
, -arcn
->type
, &cnt
))
226 (void)rd_skip(cnt
+ arcn
->pad
);
231 * check for pattern, and user specified options match.
232 * When all patterns are matched we are done.
234 if ((res
= pat_match(arcn
)) < 0)
237 if ((res
== 0) && (sel_chk(arcn
) == 0)) {
239 * pattern resulted in a selected file
241 if (pat_sel(arcn
) < 0)
245 * modify the name as requested by the user if name
246 * survives modification, do a listing of the file
248 if ((res
= mod_name(arcn
, RENM
)) < 0)
251 if (arcn
->name
[0] == '/' && !check_Aflag()) {
252 memmove(arcn
->name
, arcn
->name
+ 1,
255 ls_list(arcn
, now
, stdout
);
258 * if there's an error writing to stdout then we must
259 * stop now -- we're probably writing to a pipe that
260 * has been closed by the reader.
262 if (ferror(stdout
)) {
263 syswarn(1, errno
, "Listing incomplete.");
268 * skip to next archive format header using values calculated
269 * by the format header read routine
271 if (rd_skip(arcn
->skip
+ arcn
->pad
) == 1)
276 * all done, let format have a chance to cleanup, and make sure that
277 * the patterns supplied by the user were all matched
279 (void)(*frmt
->end_rd
)();
280 (void)sigprocmask(SIG_BLOCK
, &s_mask
, (sigset_t
*)NULL
);
289 * extract the member(s) of an archive as specified by user supplied
290 * pattern(s) (no patterns extracts all members)
305 * figure out archive type; pass any format specific options to the
306 * archive option processing routine; call the format init routine;
307 * start up the directory modification time and access mode database
309 if ((get_arc() < 0) || ((*frmt
->options
)() < 0) ||
310 ((*frmt
->st_rd
)() < 0) || (dir_start() < 0))
313 now
= time((time_t *)NULL
);
314 #if !HAVE_NBTOOL_CONFIG_H
316 (void)fdochroot(cwdfd
);
320 * When we are doing interactive rename, we store the mapping of names
321 * so we can fix up hard links files later in the archive.
323 if (iflag
&& (name_start() < 0))
327 * step through each entry on the archive until the format read routine
330 while (next_head(arcn
) == 0) {
331 int write_to_hard_link
= 0;
333 if (arcn
->type
== PAX_GLL
|| arcn
->type
== PAX_GLF
) {
335 * we need to read, to get the real filename
337 if (!(*frmt
->rd_data
)(arcn
, -arcn
->type
, &cnt
))
338 (void)rd_skip(cnt
+ arcn
->pad
);
343 * check for pattern, and user specified options match. When
344 * all the patterns are matched we are done
346 if ((res
= pat_match(arcn
)) < 0)
349 if ((res
> 0) || (sel_chk(arcn
) != 0)) {
351 * file is not selected. skip past any file
352 * data and padding and go back for the next
355 (void)rd_skip(arcn
->skip
+ arcn
->pad
);
359 if (kflag
&& (lstat(arcn
->name
, &sb
) == 0)) {
360 (void)rd_skip(arcn
->skip
+ arcn
->pad
);
365 * with -u or -D only extract when the archive member is newer
366 * than the file with the same name in the file system (no
367 * test of being the same type is required).
368 * NOTE: this test is done BEFORE name modifications as
369 * specified by pax. this operation can be confusing to the
370 * user who might expect the test to be done on an existing
371 * file AFTER the name mod. In honesty the pax spec is probably
372 * flawed in this respect. ignore this for GNU long links.
374 if ((uflag
|| Dflag
) && ((lstat(arcn
->name
, &sb
) == 0))) {
375 if (uflag
&& Dflag
) {
376 if ((arcn
->sb
.st_mtime
<= sb
.st_mtime
) &&
377 (arcn
->sb
.st_ctime
<= sb
.st_ctime
)) {
378 (void)rd_skip(arcn
->skip
+ arcn
->pad
);
382 if (arcn
->sb
.st_ctime
<= sb
.st_ctime
) {
383 (void)rd_skip(arcn
->skip
+ arcn
->pad
);
386 } else if (arcn
->sb
.st_mtime
<= sb
.st_mtime
) {
387 (void)rd_skip(arcn
->skip
+ arcn
->pad
);
393 * this archive member is now been selected. modify the name.
395 if ((pat_sel(arcn
) < 0) || ((res
= mod_name(arcn
, RENM
)) < 0))
399 * a bad name mod, skip and purge name from link table
402 (void)rd_skip(arcn
->skip
+ arcn
->pad
);
406 if (arcn
->name
[0] == '/' && !check_Aflag()) {
407 memmove(arcn
->name
, arcn
->name
+ 1, strlen(arcn
->name
));
410 * Non standard -Y and -Z flag. When the existing file is
411 * same age or newer skip; ignore this for GNU long links.
413 if ((Yflag
|| Zflag
) && ((lstat(arcn
->name
, &sb
) == 0))) {
414 if (Yflag
&& Zflag
) {
415 if ((arcn
->sb
.st_mtime
<= sb
.st_mtime
) &&
416 (arcn
->sb
.st_ctime
<= sb
.st_ctime
)) {
417 (void)rd_skip(arcn
->skip
+ arcn
->pad
);
421 if (arcn
->sb
.st_ctime
<= sb
.st_ctime
) {
422 (void)rd_skip(arcn
->skip
+ arcn
->pad
);
425 } else if (arcn
->sb
.st_mtime
<= sb
.st_mtime
) {
426 (void)rd_skip(arcn
->skip
+ arcn
->pad
);
433 ls_list(arcn
, now
, listf
);
435 (void)safe_print(arcn
->name
, listf
);
441 * if required, chdir around.
443 if ((arcn
->pat
!= NULL
) && (arcn
->pat
->chdname
!= NULL
) &&
445 dochdir(arcn
->pat
->chdname
);
447 if (secure
&& path_check(arcn
, 0) != 0) {
448 (void)rd_skip(arcn
->skip
+ arcn
->pad
);
454 * all ok, extract this member based on type
456 if ((arcn
->type
!= PAX_REG
) && (arcn
->type
!= PAX_CTG
)) {
458 * process archive members that are not regular files.
459 * throw out padding and any data that might follow the
460 * header (as determined by the format).
462 if ((arcn
->type
== PAX_HLK
) ||
463 (arcn
->type
== PAX_HRG
))
464 res
= lnk_creat(arcn
, &write_to_hard_link
);
466 res
= node_creat(arcn
);
468 if (!write_to_hard_link
) {
469 (void)rd_skip(arcn
->skip
+ arcn
->pad
);
473 if (vflag
&& vfpart
) {
474 (void)putc('\n', listf
);
484 * We have a file with data here. If we cannot create
485 * it, skip over the data and purge the name from hard
488 if ((fd
= file_creat(arcn
, write_to_hard_link
)) < 0) {
490 (void)rd_skip(arcn
->skip
+ arcn
->pad
);
496 * extract the file from the archive and skip over padding and
497 * any unprocessed data
499 res
= (*frmt
->rd_data
)(arcn
, fd
, &cnt
);
501 file_close(arcn
, fd
);
502 if (vflag
&& vfpart
) {
503 (void)putc('\n', listf
);
507 (void)rd_skip(cnt
+ arcn
->pad
);
510 * if required, chdir around.
512 if ((arcn
->pat
!= NULL
) && (arcn
->pat
->chdname
!= NULL
))
517 * all done, restore directory modes and times as required; make sure
518 * all patterns supplied by the user were matched; block off signals
519 * to avoid chance for multiple entry into the cleanup code.
521 (void)(*frmt
->end_rd
)();
522 (void)sigprocmask(SIG_BLOCK
, &s_mask
, (sigset_t
*)NULL
);
532 * Write an archive. used in both creating a new archive and appends on
533 * previously written archive.
537 wr_archive(ARCHD
*arcn
, int is_app
)
548 * if this format supports hard link storage, start up the database
551 if (((hlk
= frmt
->hlk
) == 1) && (lnk_start() < 0))
555 * start up the file traversal code and format specific write
557 if ((ftree_start() < 0) || ((*frmt
->st_wr
)() < 0))
561 now
= time((time_t *)NULL
);
564 * When we are doing interactive rename, we store the mapping of names
565 * so we can fix up hard links files later in the archive.
567 if (iflag
&& (name_start() < 0))
571 * if this is not append, and there are no files, we do no write a trailer
576 * while there are files to archive, process them one at at time
578 while (next_file(arcn
) == 0) {
580 * check if this file meets user specified options match.
582 if (sel_chk(arcn
) != 0)
585 * Here we handle the exclusion -X gnu style patterns which
586 * are implemented like a pattern list. We don't modify the
587 * name as this will be done below again, and we don't want
588 * to double modify it.
590 if ((res
= mod_name(arcn
, 0)) < 0)
597 * only archive if this file is newer than a file with
598 * the same name that is already stored on the archive
600 if ((res
= chk_ftime(arcn
)) < 0)
607 * this file is considered selected now. see if this is a hard
608 * link to a file already stored
611 if (hlk
&& (chk_lnk(arcn
) < 0))
614 if ((arcn
->type
== PAX_REG
) || (arcn
->type
== PAX_HRG
) ||
615 (arcn
->type
== PAX_CTG
)) {
617 * we will have to read this file. by opening it now we
618 * can avoid writing a header to the archive for a file
619 * we were later unable to read (we also purge it from
622 if ((fd
= open(arcn
->org_name
, O_RDONLY
, 0)) < 0) {
623 syswarn(1, errno
, "Unable to open %s to read",
631 * Now modify the name as requested by the user
633 if ((res
= mod_name(arcn
, RENM
)) < 0) {
635 * name modification says to skip this file, close the
636 * file and purge link table entry
638 rdfile_close(arcn
, &fd
);
643 if (arcn
->name
[0] == '/' && !check_Aflag()) {
644 memmove(arcn
->name
, arcn
->name
+ 1, strlen(arcn
->name
));
647 if ((res
> 0) || (docrc
&& (set_crc(arcn
, fd
) < 0))) {
649 * unable to obtain the crc we need, close the file,
650 * purge link table entry
652 rdfile_close(arcn
, &fd
);
659 ls_list(arcn
, now
, listf
);
661 (void)safe_print(arcn
->name
, listf
);
668 * looks safe to store the file, have the format specific
669 * routine write routine store the file header on the archive
671 if ((res
= (*wrf
)(arcn
)) < 0) {
672 rdfile_close(arcn
, &fd
);
678 * format write says no file data needs to be stored
679 * so we are done messing with this file
681 if (vflag
&& vfpart
) {
682 (void)putc('\n', listf
);
685 rdfile_close(arcn
, &fd
);
690 * Add file data to the archive, quit on write error. if we
691 * cannot write the entire file contents to the archive we
692 * must pad the archive to replace the missing file data
693 * (otherwise during an extract the file header for the file
694 * which FOLLOWS this one will not be where we expect it to
697 res
= (*frmt
->wr_data
)(arcn
, fd
, &cnt
);
698 rdfile_close(arcn
, &fd
);
699 if (vflag
&& vfpart
) {
700 (void)putc('\n', listf
);
707 * pad as required, cnt is number of bytes not written
709 if (((cnt
> 0) && (wr_skip(cnt
) < 0)) ||
710 ((arcn
->pad
> 0) && (wr_skip(arcn
->pad
) < 0)))
715 * tell format to write trailer; pad to block boundary; reset directory
716 * mode/access times, and check if all patterns supplied by the user
717 * were matched. block off signals to avoid chance for multiple entry
718 * into the cleanup code
724 (void)sigprocmask(SIG_BLOCK
, &s_mask
, (sigset_t
*)NULL
);
735 * Add file to previously written archive. Archive format specified by the
736 * user must agree with archive. The archive is read first to collect
737 * modification times (if -u) and locate the archive trailer. The archive
738 * is positioned in front of the record with the trailer and wr_archive()
739 * is called to add the new members.
740 * PAX IMPLEMENTATION DETAIL NOTE:
741 * -u is implemented by adding the new members to the end of the archive.
742 * Care is taken so that these do not end up as links to the older
743 * version of the same file already stored in the archive. It is expected
744 * when extraction occurs these newer versions will over-write the older
745 * ones stored "earlier" in the archive (this may be a bad assumption as
746 * it depends on the implementation of the program doing the extraction).
747 * It is really difficult to splice in members without either re-writing
748 * the entire archive (from the point were the old version was), or having
749 * assistance of the format specification in terms of a special update
750 * header that invalidates a previous archive record. The posix spec left
751 * the method used to implement -u unspecified. This pax is able to
752 * over write existing files that it creates.
768 * Do not allow an append operation if the actual archive is of a
769 * different format than the user specified format.
773 if ((orgfrmt
!= NULL
) && (orgfrmt
!= frmt
)) {
774 tty_warn(1, "Cannot mix current archive format %s with %s",
775 frmt
->name
, orgfrmt
->name
);
780 * pass the format any options and start up format
782 if (((*frmt
->options
)() < 0) || ((*frmt
->st_rd
)() < 0))
786 * if we only are adding members that are newer, we need to save the
787 * mod times for all files we see.
789 if (uflag
&& (ftime_start() < 0))
793 * some archive formats encode hard links by recording the device and
794 * file serial number (inode) but copy the file anyway (multiple times)
795 * to the archive. When we append, we run the risk that newly added
796 * files may have the same device and inode numbers as those recorded
797 * on the archive but during a previous run. If this happens, when the
798 * archive is extracted we get INCORRECT hard links. We avoid this by
799 * remapping the device numbers so that newly added files will never
800 * use the same device number as one found on the archive. remapping
801 * allows new members to safely have links among themselves. remapping
802 * also avoids problems with file inode (serial number) truncations
803 * when the inode number is larger than storage space in the archive
804 * header. See the remap routines for more details.
806 if ((udev
= frmt
->udev
) && (dev_start() < 0))
810 * reading the archive may take a long time. If verbose tell the user
812 if (vflag
|| Vflag
) {
814 "%s: Reading archive to position at the end...", argv0
);
819 * step through the archive until the format says it is done
821 while (next_head(arcn
) == 0) {
823 * check if this file meets user specified options.
825 if (sel_chk(arcn
) != 0) {
826 if (rd_skip(arcn
->skip
+ arcn
->pad
) == 1)
833 * see if this is the newest version of this file has
834 * already been seen, if so skip.
836 if ((res
= chk_ftime(arcn
)) < 0)
839 if (rd_skip(arcn
->skip
+ arcn
->pad
) == 1)
846 * Store this device number. Device numbers seen during the
847 * read phase of append will cause newly appended files with a
848 * device number seen in the old part of the archive to be
849 * remapped to an unused device number.
851 if ((udev
&& (add_dev(arcn
) < 0)) ||
852 (rd_skip(arcn
->skip
+ arcn
->pad
) == 1))
857 * done, finish up read and get the number of bytes to back up so we
858 * can add new members. The format might have used the hard link table,
861 tlen
= (*frmt
->end_rd
)();
865 * try to position for write, if this fails quit. if any error occurs,
866 * we will refuse to write
868 if (appnd_start(tlen
) < 0)
872 * tell the user we are done reading.
874 if ((vflag
|| Vflag
) && vfpart
) {
875 (void)safe_print("done.\n", listf
);
880 * go to the writing phase to add the new members
882 res
= wr_archive(arcn
, 1);
885 * wr_archive failed in some way, but before any files were
886 * added. These are the only steps needed to cleanup (and
887 * not truncate the archive).
890 (void)sigprocmask(SIG_BLOCK
, &s_mask
, (sigset_t
*)NULL
);
898 * write a new archive
906 * if we only are adding members that are newer, we need to save the
907 * mod times for all files; set up for writing; pass the format any
908 * options write the archive
910 if ((uflag
&& (ftime_start() < 0)) || (wr_start() < 0))
912 if ((*frmt
->options
)() < 0)
915 return wr_archive(&archd
, 0);
920 * copy files from one part of the file system to another. this does not
921 * use any archive storage. The EFFECT OF THE COPY IS THE SAME as if an
922 * archive was written and then extracted in the destination directory
923 * (except the files are forced to be under the destination directory).
937 char dirbuf
[PAXPATHLEN
+1];
941 * set up the destination dir path and make sure it is a directory. We
942 * make sure we have a trailing / on the destination
944 dlen
= strlcpy(dirbuf
, dirptr
, sizeof(dirbuf
));
945 if (dlen
>= sizeof(dirbuf
) ||
946 (dlen
== sizeof(dirbuf
) - 1 && dirbuf
[dlen
- 1] != '/')) {
947 tty_warn(1, "directory name is too long %s", dirptr
);
950 dest_pt
= dirbuf
+ dlen
;
951 if (*(dest_pt
-1) != '/') {
956 drem
= PAXPATHLEN
- dlen
;
958 if (stat(dirptr
, &sb
) < 0) {
959 syswarn(1, errno
, "Cannot access destination directory %s",
963 if (!S_ISDIR(sb
.st_mode
)) {
964 tty_warn(1, "Destination is not a directory %s", dirptr
);
969 * start up the hard link table; file traversal routines and the
970 * modification time and access mode database
972 if ((lnk_start() < 0) || (ftree_start() < 0) || (dir_start() < 0))
976 * When we are doing interactive rename, we store the mapping of names
977 * so we can fix up hard links files later in the archive.
979 if (iflag
&& (name_start() < 0))
983 * set up to cp file trees
988 * while there are files to archive, process them
990 while (next_file(arcn
) == 0) {
994 * check if this file meets user specified options
996 if (sel_chk(arcn
) != 0)
1000 * if there is already a file in the destination directory with
1001 * the same name and it is newer, skip the one stored on the
1003 * NOTE: this test is done BEFORE name modifications as
1004 * specified by pax. this can be confusing to the user who
1005 * might expect the test to be done on an existing file AFTER
1006 * the name mod. In honesty the pax spec is probably flawed in
1009 if (uflag
|| Dflag
) {
1011 * create the destination name
1013 if (strlcpy(dest_pt
, arcn
->name
+ (*arcn
->name
== '/'),
1015 tty_warn(1, "Destination pathname too long %s",
1021 * if existing file is same age or newer skip
1023 res
= lstat(dirbuf
, &sb
);
1027 if (uflag
&& Dflag
) {
1028 if ((arcn
->sb
.st_mtime
<=sb
.st_mtime
) &&
1029 (arcn
->sb
.st_ctime
<=sb
.st_ctime
))
1032 if (arcn
->sb
.st_ctime
<= sb
.st_ctime
)
1034 } else if (arcn
->sb
.st_mtime
<= sb
.st_mtime
)
1040 * this file is considered selected. See if this is a hard link
1041 * to a previous file; modify the name as requested by the
1042 * user; set the final destination.
1045 if ((chk_lnk(arcn
) < 0) || ((res
= mod_name(arcn
, RENM
)) < 0))
1047 if ((res
> 0) || (set_dest(arcn
, dirbuf
, dlen
) < 0)) {
1049 * skip file, purge from link table
1056 * Non standard -Y and -Z flag. When the exisiting file is
1057 * same age or newer skip
1059 if ((Yflag
|| Zflag
) && ((lstat(arcn
->name
, &sb
) == 0))) {
1060 if (Yflag
&& Zflag
) {
1061 if ((arcn
->sb
.st_mtime
<= sb
.st_mtime
) &&
1062 (arcn
->sb
.st_ctime
<= sb
.st_ctime
))
1065 if (arcn
->sb
.st_ctime
<= sb
.st_ctime
)
1067 } else if (arcn
->sb
.st_mtime
<= sb
.st_mtime
)
1072 (void)safe_print(arcn
->name
, listf
);
1078 * try to create a hard link to the src file if requested
1079 * but make sure we are not trying to overwrite ourselves.
1082 res
= cross_lnk(arcn
);
1084 res
= chk_same(arcn
);
1086 if (vflag
&& vfpart
) {
1087 (void)putc('\n', listf
);
1094 * have to create a new file
1096 if ((arcn
->type
!= PAX_REG
) && (arcn
->type
!= PAX_CTG
)) {
1098 * create a link or special file
1100 if ((arcn
->type
== PAX_HLK
) ||
1101 (arcn
->type
== PAX_HRG
)) {
1104 res
= lnk_creat(arcn
, &payload
);
1106 res
= node_creat(arcn
);
1110 if (vflag
&& vfpart
) {
1111 (void)putc('\n', listf
);
1118 * have to copy a regular file to the destination directory.
1119 * first open source file and then create the destination file
1121 if ((fdsrc
= open(arcn
->org_name
, O_RDONLY
, 0)) < 0) {
1122 syswarn(1, errno
, "Unable to open %s to read",
1127 if ((fddest
= file_creat(arcn
, 0)) < 0) {
1128 rdfile_close(arcn
, &fdsrc
);
1134 * copy source file data to the destination file
1136 cp_file(arcn
, fdsrc
, fddest
);
1137 file_close(arcn
, fddest
);
1138 rdfile_close(arcn
, &fdsrc
);
1140 if (vflag
&& vfpart
) {
1141 (void)putc('\n', listf
);
1147 * restore directory modes and times as required; make sure all
1148 * patterns were selected block off signals to avoid chance for
1149 * multiple entry into the cleanup code.
1151 (void)sigprocmask(SIG_BLOCK
, &s_mask
, (sigset_t
*)NULL
);
1161 * try to find a valid header in the archive. Uses format specific
1162 * routines to extract the header and id the trailer. Trailers may be
1163 * located within a valid header or in an invalid header (the location
1164 * is format specific. The inhead field from the option table tells us
1165 * where to look for the trailer).
1166 * We keep reading (and resyncing) until we get enough contiguous data
1167 * to check for a header. If we cannot find one, we shift by a byte
1168 * add a new byte from the archive to the end of the buffer and try again.
1169 * If we get a read error, we throw out what we have (as we must have
1170 * contiguous data) and start over again.
1171 * ASSUMED: headers fit within a BLKMULT header.
1173 * 0 if we got a header, -1 if we are unable to ever find another one
1174 * (we reached the end of input, or we reached the limit on retries. see
1175 * the specs for rd_wrbuf() for more details)
1179 next_head(ARCHD
*arcn
)
1186 int in_resync
= 0; /* set when we are in resync mode */
1187 int cnt
= 0; /* counter for trailer function */
1188 int first
= 1; /* on 1st read, EOF isn't premature. */
1191 * set up initial conditions, we want a whole frmt->hsz block as we
1194 res
= hsz
= frmt
->hsz
;
1199 * keep looping until we get a contiguous FULL buffer
1200 * (frmt->hsz is the proper size)
1203 if ((ret
= rd_wrbuf(hdend
, res
)) == res
)
1207 * If we read 0 bytes (EOF) from an archive when we
1208 * expect to find a header, we have stepped upon
1209 * an archive without the customary block of zeroes
1210 * end marker. It's just stupid to error out on
1211 * them, so exit gracefully.
1213 if (first
&& ret
== 0)
1218 * some kind of archive read problem, try to resync the
1219 * storage device, better give the user the bad news.
1221 if ((ret
== 0) || (rd_sync() < 0)) {
1223 "Premature end of file on archive read");
1229 "Archive I/O error, cannot continue");
1233 "Archive I/O error. Trying to recover.");
1238 * oh well, throw it all out and start over
1245 * ok we have a contiguous buffer of the right size. Call the
1246 * format read routine. If this was not a valid header and this
1247 * format stores trailers outside of the header, call the
1248 * format specific trailer routine to check for a trailer. We
1249 * have to watch out that we do not mis-identify file data or
1250 * block padding as a header or trailer. Format specific
1251 * trailer functions must NOT check for the trailer while we
1252 * are running in resync mode. Some trailer functions may tell
1253 * us that this block cannot contain a valid header either, so
1254 * we then throw out the entire block and start over.
1256 if ((*frmt
->rd
)(arcn
, hdbuf
) == 0)
1259 if (!frmt
->inhead
) {
1261 * this format has trailers outside of valid headers
1263 if ((ret
= (*frmt
->trail
)(hdbuf
,in_resync
,&cnt
)) == 0){
1265 * valid trailer found, drain input as required
1273 * we are in resync and we were told to throw
1274 * the whole block out because none of the
1275 * bytes in this block can be used to form a
1285 * Brute force section.
1286 * not a valid header. We may be able to find a header yet. So
1287 * we shift over by one byte, and set up to read one byte at a
1288 * time from the archive and place it at the end of the buffer.
1289 * We will keep moving byte at a time until we find a header or
1290 * get a read error and have to start over.
1295 "Unable to append, archive header flaw");
1299 "Invalid header, starting valid header search.");
1302 memmove(hdbuf
, hdbuf
+1, shftsz
);
1304 hdend
= hdbuf
+ shftsz
;
1308 * ok got a valid header, check for trailer if format encodes it in the
1309 * the header. NOTE: the parameters are different than trailer routines
1310 * which encode trailers outside of the header!
1312 if (frmt
->inhead
&& ((*frmt
->subtrail
)(arcn
) == 0)) {
1314 * valid trailer found, drain input as required
1326 * Figure out what format an archive is. Handles archive with flaws by
1327 * brute force searches for a legal header in any supported format. The
1328 * format id routines have to be careful to NOT mis-identify a format.
1329 * ASSUMED: headers fit within a BLKMULT header.
1331 * 0 if archive found -1 otherwise
1340 int minhd
= BLKMULT
;
1345 * find the smallest header size in all archive formats and then set up
1346 * to read the archive.
1348 for (i
= 0; ford
[i
] >= 0; ++i
) {
1349 if (fsub
[ford
[i
]].hsz
< minhd
)
1350 minhd
= fsub
[ford
[i
]].hsz
;
1360 * fill the buffer with at least the smallest header
1362 i
= rd_wrbuf(hdend
, res
);
1369 * if we cannot recover from a read error quit
1371 if ((i
== 0) || (rd_sync() < 0))
1375 * when we get an error none of the data we already
1376 * have can be used to create a legal header (we just
1377 * got an error in the middle), so we throw it all out
1378 * and refill the buffer with fresh data.
1387 "Cannot identify format. Searching...");
1393 * we have at least the size of the smallest header in any
1394 * archive format. Look to see if we have a match. The array
1395 * ford[] is used to specify the header id order to reduce the
1396 * chance of incorrectly id'ing a valid header (some formats
1397 * may be subsets of each other and the order would then be
1400 for (i
= 0; ford
[i
] >= 0; ++i
) {
1401 if ((*fsub
[ford
[i
]].id
)(hdbuf
, hdsz
) < 0)
1403 frmt
= &(fsub
[ford
[i
]]);
1405 * yuck, to avoid slow special case code in the extract
1406 * routines, just push this header back as if it was
1407 * not seen. We have left extra space at start of the
1408 * buffer for this purpose. This is a bit ugly, but
1409 * adding all the special case code is far worse.
1416 * We have a flawed archive, no match. we start searching, but
1417 * we never allow additions to flawed archives
1422 tty_warn(1, "Cannot identify format. Searching...");
1427 * brute force search for a header that we can id.
1428 * we shift through byte at a time. this is slow, but we cannot
1429 * determine the nature of the flaw in the archive in a
1433 memmove(hdbuf
, hdbuf
+1, hdsz
);
1434 res
= BLKMULT
- hdsz
;
1435 hdend
= hdbuf
+ hdsz
;
1445 * we cannot find a header, bow, apologize and quit
1447 tty_warn(1, "Sorry, unable to determine archive format.");