2 * Copyright (c) 1992 Keith Muller.
3 * Copyright (c) 1992, 1993
4 * The Regents of the University of California. All rights reserved.
6 * This code is derived from software contributed to Berkeley by
7 * Keith Muller of the University of California, San Diego.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * @(#)ar_subs.c 8.2 (Berkeley) 4/18/94
34 * $FreeBSD: src/bin/pax/ar_subs.c,v 1.13.2.1 2001/08/01 05:03:11 obrien Exp $
37 #include <sys/types.h>
50 static void wr_archive (ARCHD
*, int is_app
);
51 static int get_arc (void);
52 static int next_head (ARCHD
*);
53 extern sigset_t s_mask
;
56 * Routines which control the overall operation modes of pax as specified by
57 * the user: list, append, read ...
60 static char hdbuf
[BLKMULT
]; /* space for archive header on read */
61 u_long flcnt
; /* number of files processed */
65 * list the contents of an archive which match user supplied pattern(s)
66 * (no pattern matches all).
79 * figure out archive type; pass any format specific options to the
80 * archive option processing routine; call the format init routine. We
81 * also save current time for ls_list() so we do not make a system
82 * call for each file we need to print. If verbose (vflag) start up
83 * the name and group caches.
85 if ((get_arc() < 0) || ((*frmt
->options
)() < 0) ||
86 ((*frmt
->st_rd
)() < 0))
92 * step through the archive until the format says it is done
94 while (next_head(arcn
) == 0) {
96 * check for pattern, and user specified options match.
97 * When all patterns are matched we are done.
99 if ((res
= pat_match(arcn
)) < 0)
102 if ((res
== 0) && (sel_chk(arcn
) == 0)) {
104 * pattern resulted in a selected file
106 if (pat_sel(arcn
) < 0)
110 * modify the name as requested by the user if name
111 * survives modification, do a listing of the file
113 if ((res
= mod_name(arcn
)) < 0)
116 ls_list(arcn
, now
, stdout
);
120 * skip to next archive format header using values calculated
121 * by the format header read routine
123 if (rd_skip(arcn
->skip
+ arcn
->pad
) == 1)
128 * all done, let format have a chance to cleanup, and make sure that
129 * the patterns supplied by the user were all matched
132 sigprocmask(SIG_BLOCK
, &s_mask
, NULL
);
139 * extract the member(s) of an archive as specified by user supplied
140 * pattern(s) (no patterns extracts all members)
156 * figure out archive type; pass any format specific options to the
157 * archive option processing routine; call the format init routine;
158 * start up the directory modification time and access mode database
160 if ((get_arc() < 0) || ((*frmt
->options
)() < 0) ||
161 ((*frmt
->st_rd
)() < 0) || (dir_start() < 0))
165 * When we are doing interactive rename, we store the mapping of names
166 * so we can fix up hard links files later in the archive.
168 if (iflag
&& (name_start() < 0))
174 * step through each entry on the archive until the format read routine
177 while (next_head(arcn
) == 0) {
180 * check for pattern, and user specified options match. When
181 * all the patterns are matched we are done
183 if ((res
= pat_match(arcn
)) < 0)
186 if ((res
> 0) || (sel_chk(arcn
) != 0)) {
188 * file is not selected. skip past any file data and
189 * padding and go back for the next archive member
191 rd_skip(arcn
->skip
+ arcn
->pad
);
196 * with -u or -D only extract when the archive member is newer
197 * than the file with the same name in the file system (no
198 * test of being the same type is required).
199 * NOTE: this test is done BEFORE name modifications as
200 * specified by pax. this operation can be confusing to the
201 * user who might expect the test to be done on an existing
202 * file AFTER the name mod. In honesty the pax spec is probably
203 * flawed in this respect.
205 if ((uflag
|| Dflag
) && ((lstat(arcn
->name
, &sb
) == 0))) {
206 if (uflag
&& Dflag
) {
207 if ((arcn
->sb
.st_mtime
<= sb
.st_mtime
) &&
208 (arcn
->sb
.st_ctime
<= sb
.st_ctime
)) {
209 rd_skip(arcn
->skip
+ arcn
->pad
);
213 if (arcn
->sb
.st_ctime
<= sb
.st_ctime
) {
214 rd_skip(arcn
->skip
+ arcn
->pad
);
217 } else if (arcn
->sb
.st_mtime
<= sb
.st_mtime
) {
218 rd_skip(arcn
->skip
+ arcn
->pad
);
224 * this archive member is now been selected. modify the name.
226 if ((pat_sel(arcn
) < 0) || ((res
= mod_name(arcn
)) < 0))
230 * a bad name mod, skip and purge name from link table
233 rd_skip(arcn
->skip
+ arcn
->pad
);
238 * Non standard -Y and -Z flag. When the existing file is
239 * same age or newer skip
241 if ((Yflag
|| Zflag
) && ((lstat(arcn
->name
, &sb
) == 0))) {
242 if (Yflag
&& Zflag
) {
243 if ((arcn
->sb
.st_mtime
<= sb
.st_mtime
) &&
244 (arcn
->sb
.st_ctime
<= sb
.st_ctime
)) {
245 rd_skip(arcn
->skip
+ arcn
->pad
);
249 if (arcn
->sb
.st_ctime
<= sb
.st_ctime
) {
250 rd_skip(arcn
->skip
+ arcn
->pad
);
253 } else if (arcn
->sb
.st_mtime
<= sb
.st_mtime
) {
254 rd_skip(arcn
->skip
+ arcn
->pad
);
261 ls_list(arcn
, now
, listf
);
263 fputs(arcn
->name
, listf
);
269 * if required, chdir around.
271 if ((arcn
->pat
!= NULL
) && (arcn
->pat
->chdname
!= NULL
))
272 if (chdir(arcn
->pat
->chdname
) != 0)
273 syswarn(1, errno
, "Cannot chdir to %s",
277 * all ok, extract this member based on type
279 if ((arcn
->type
!= PAX_REG
) && (arcn
->type
!= PAX_CTG
)) {
281 * process archive members that are not regular files.
282 * throw out padding and any data that might follow the
283 * header (as determined by the format).
285 if ((arcn
->type
== PAX_HLK
) || (arcn
->type
== PAX_HRG
))
286 res
= lnk_creat(arcn
);
288 res
= node_creat(arcn
);
290 rd_skip(arcn
->skip
+ arcn
->pad
);
294 if (vflag
&& vfpart
) {
301 * we have a file with data here. If we can not create it, skip
302 * over the data and purge the name from hard link table
304 if ((fd
= file_creat(arcn
)) < 0) {
305 rd_skip(arcn
->skip
+ arcn
->pad
);
310 * extract the file from the archive and skip over padding and
311 * any unprocessed data
313 res
= (*frmt
->rd_data
)(arcn
, fd
, &cnt
);
314 file_close(arcn
, fd
);
315 if (vflag
&& vfpart
) {
320 rd_skip(cnt
+ arcn
->pad
);
323 * if required, chdir around.
325 if ((arcn
->pat
!= NULL
) && (arcn
->pat
->chdname
!= NULL
))
326 if (fchdir(cwdfd
) != 0)
328 "Can't fchdir to starting directory");
332 * all done, restore directory modes and times as required; make sure
333 * all patterns supplied by the user were matched; block off signals
334 * to avoid chance for multiple entry into the cleanup code.
337 sigprocmask(SIG_BLOCK
, &s_mask
, NULL
);
345 * Write an archive. used in both creating a new archive and appends on
346 * previously written archive.
350 wr_archive(ARCHD
*arcn
, int is_app
)
361 * if this format supports hard link storage, start up the database
364 if (((hlk
= frmt
->hlk
) == 1) && (lnk_start() < 0))
368 * start up the file traversal code and format specific write
370 if ((ftree_start() < 0) || ((*frmt
->st_wr
)() < 0))
375 * When we are doing interactive rename, we store the mapping of names
376 * so we can fix up hard links files later in the archive.
378 if (iflag
&& (name_start() < 0))
382 * if this not append, and there are no files, we do no write a trailer
389 * while there are files to archive, process them one at at time
391 while (next_file(arcn
) == 0) {
393 * check if this file meets user specified options match.
395 if (sel_chk(arcn
) != 0)
400 * only archive if this file is newer than a file with
401 * the same name that is already stored on the archive
403 if ((res
= chk_ftime(arcn
)) < 0)
410 * this file is considered selected now. see if this is a hard
411 * link to a file already stored
414 if (hlk
&& (chk_lnk(arcn
) < 0))
417 if ((arcn
->type
== PAX_REG
) || (arcn
->type
== PAX_HRG
) ||
418 (arcn
->type
== PAX_CTG
)) {
420 * we will have to read this file. by opening it now we
421 * can avoid writing a header to the archive for a file
422 * we were later unable to read (we also purge it from
425 if ((fd
= open(arcn
->org_name
, O_RDONLY
, 0)) < 0) {
426 syswarn(1,errno
, "Unable to open %s to read",
434 * Now modify the name as requested by the user
436 if ((res
= mod_name(arcn
)) < 0) {
438 * name modification says to skip this file, close the
439 * file and purge link table entry
441 rdfile_close(arcn
, &fd
);
446 if ((res
> 0) || (docrc
&& (set_crc(arcn
, fd
) < 0))) {
448 * unable to obtain the crc we need, close the file,
449 * purge link table entry
451 rdfile_close(arcn
, &fd
);
458 ls_list(arcn
, now
, listf
);
460 fputs(arcn
->name
, listf
);
467 * looks safe to store the file, have the format specific
468 * routine write routine store the file header on the archive
470 if ((res
= (*wrf
)(arcn
)) < 0) {
471 rdfile_close(arcn
, &fd
);
477 * format write says no file data needs to be stored
478 * so we are done messing with this file
480 if (vflag
&& vfpart
) {
484 rdfile_close(arcn
, &fd
);
489 * Add file data to the archive, quit on write error. if we
490 * cannot write the entire file contents to the archive we
491 * must pad the archive to replace the missing file data
492 * (otherwise during an extract the file header for the file
493 * which FOLLOWS this one will not be where we expect it to
496 res
= (*frmt
->wr_data
)(arcn
, fd
, &cnt
);
497 rdfile_close(arcn
, &fd
);
498 if (vflag
&& vfpart
) {
506 * pad as required, cnt is number of bytes not written
508 if (((cnt
> 0) && (wr_skip(cnt
) < 0)) ||
509 ((arcn
->pad
> 0) && (wr_skip(arcn
->pad
) < 0)))
514 * tell format to write trailer; pad to block boundary; reset directory
515 * mode/access times, and check if all patterns supplied by the user
516 * were matched. block off signals to avoid chance for multiple entry
517 * into the cleanup code
523 sigprocmask(SIG_BLOCK
, &s_mask
, NULL
);
532 * Add file to previously written archive. Archive format specified by the
533 * user must agree with archive. The archive is read first to collect
534 * modification times (if -u) and locate the archive trailer. The archive
535 * is positioned in front of the record with the trailer and wr_archive()
536 * is called to add the new members.
537 * PAX IMPLEMENTATION DETAIL NOTE:
538 * -u is implemented by adding the new members to the end of the archive.
539 * Care is taken so that these do not end up as links to the older
540 * version of the same file already stored in the archive. It is expected
541 * when extraction occurs these newer versions will over-write the older
542 * ones stored "earlier" in the archive (this may be a bad assumption as
543 * it depends on the implementation of the program doing the extraction).
544 * It is really difficult to splice in members without either re-writing
545 * the entire archive (from the point were the old version was), or having
546 * assistance of the format specification in terms of a special update
547 * header that invalidates a previous archive record. The POSIX spec left
548 * the method used to implement -u unspecified. This pax is able to
549 * over write existing files that it creates.
566 * Do not allow an append operation if the actual archive is of a
567 * different format than the user specified format.
571 if ((orgfrmt
!= NULL
) && (orgfrmt
!= frmt
)) {
572 paxwarn(1, "Cannot mix current archive format %s with %s",
573 frmt
->name
, orgfrmt
->name
);
578 * pass the format any options and start up format
580 if (((*frmt
->options
)() < 0) || ((*frmt
->st_rd
)() < 0))
584 * if we only are adding members that are newer, we need to save the
585 * mod times for all files we see.
587 if (uflag
&& (ftime_start() < 0))
591 * some archive formats encode hard links by recording the device and
592 * file serial number (inode) but copy the file anyway (multiple times)
593 * to the archive. When we append, we run the risk that newly added
594 * files may have the same device and inode numbers as those recorded
595 * on the archive but during a previous run. If this happens, when the
596 * archive is extracted we get INCORRECT hard links. We avoid this by
597 * remapping the device numbers so that newly added files will never
598 * use the same device number as one found on the archive. remapping
599 * allows new members to safely have links among themselves. remapping
600 * also avoids problems with file inode (serial number) truncations
601 * when the inode number is larger than storage space in the archive
602 * header. See the remap routines for more details.
604 if ((udev
= frmt
->udev
) && (dev_start() < 0))
608 * reading the archive may take a long time. If verbose tell the user
612 "%s: Reading archive to position at the end...", argv0
);
617 * step through the archive until the format says it is done
619 while (next_head(arcn
) == 0) {
621 * check if this file meets user specified options.
623 if (sel_chk(arcn
) != 0) {
624 if (rd_skip(arcn
->skip
+ arcn
->pad
) == 1)
631 * see if this is the newest version of this file has
632 * already been seen, if so skip.
634 if ((res
= chk_ftime(arcn
)) < 0)
637 if (rd_skip(arcn
->skip
+ arcn
->pad
) == 1)
644 * Store this device number. Device numbers seen during the
645 * read phase of append will cause newly appended files with a
646 * device number seen in the old part of the archive to be
647 * remapped to an unused device number.
649 if ((udev
&& (add_dev(arcn
) < 0)) ||
650 (rd_skip(arcn
->skip
+ arcn
->pad
) == 1))
655 * done, finish up read and get the number of bytes to back up so we
656 * can add new members. The format might have used the hard link table,
659 tlen
= (*frmt
->end_rd
)();
663 * try to position for write, if this fails quit. if any error occurs,
664 * we will refuse to write
666 if (appnd_start(tlen
) < 0)
670 * tell the user we are done reading.
672 if (vflag
&& vfpart
) {
673 fputs("done.\n", listf
);
678 * go to the writing phase to add the new members
685 * write a new archive
694 * if we only are adding members that are newer, we need to save the
695 * mod times for all files; set up for writing; pass the format any
696 * options write the archive
698 if ((uflag
&& (ftime_start() < 0)) || (wr_start() < 0))
700 if ((*frmt
->options
)() < 0)
703 wr_archive(&archd
, 0);
708 * copy files from one part of the file system to another. this does not
709 * use any archive storage. The EFFECT OF THE COPY IS THE SAME as if an
710 * archive was written and then extracted in the destination directory
711 * (except the files are forced to be under the destination directory).
726 char dirbuf
[PAXPATHLEN
+1];
730 * set up the destination dir path and make sure it is a directory. We
731 * make sure we have a trailing / on the destination
733 dlen
= l_strncpy(dirbuf
, dirptr
, sizeof(dirbuf
) - 1);
734 dest_pt
= dirbuf
+ dlen
;
735 if (*(dest_pt
-1) != '/') {
740 drem
= PAXPATHLEN
- dlen
;
742 if (stat(dirptr
, &sb
) < 0) {
743 syswarn(1, errno
, "Cannot access destination directory %s",
747 if (!S_ISDIR(sb
.st_mode
)) {
748 paxwarn(1, "Destination is not a directory %s", dirptr
);
753 * start up the hard link table; file traversal routines and the
754 * modification time and access mode database
756 if ((lnk_start() < 0) || (ftree_start() < 0) || (dir_start() < 0))
760 * When we are doing interactive rename, we store the mapping of names
761 * so we can fix up hard links files later in the archive.
763 if (iflag
&& (name_start() < 0))
767 * set up to cp file trees
772 * while there are files to archive, process them
774 while (next_file(arcn
) == 0) {
778 * check if this file meets user specified options
780 if (sel_chk(arcn
) != 0)
784 * if there is already a file in the destination directory with
785 * the same name and it is newer, skip the one stored on the
787 * NOTE: this test is done BEFORE name modifications as
788 * specified by pax. this can be confusing to the user who
789 * might expect the test to be done on an existing file AFTER
790 * the name mod. In honesty the pax spec is probably flawed in
793 if (uflag
|| Dflag
) {
795 * create the destination name
797 if (*(arcn
->name
) == '/')
801 if ((arcn
->nlen
- res
) > drem
) {
802 paxwarn(1, "Destination pathname too long %s",
806 strncpy(dest_pt
, arcn
->name
+ res
, drem
);
807 dirbuf
[PAXPATHLEN
] = '\0';
810 * if existing file is same age or newer skip
812 res
= lstat(dirbuf
, &sb
);
816 if (uflag
&& Dflag
) {
817 if ((arcn
->sb
.st_mtime
<=sb
.st_mtime
) &&
818 (arcn
->sb
.st_ctime
<=sb
.st_ctime
))
821 if (arcn
->sb
.st_ctime
<= sb
.st_ctime
)
823 } else if (arcn
->sb
.st_mtime
<= sb
.st_mtime
)
829 * this file is considered selected. See if this is a hard link
830 * to a previous file; modify the name as requested by the
831 * user; set the final destination.
834 if ((chk_lnk(arcn
) < 0) || ((res
= mod_name(arcn
)) < 0))
836 if ((res
> 0) || (set_dest(arcn
, dirbuf
, dlen
) < 0)) {
838 * skip file, purge from link table
845 * Non standard -Y and -Z flag. When the existing file is
846 * same age or newer skip
848 if ((Yflag
|| Zflag
) && ((lstat(arcn
->name
, &sb
) == 0))) {
849 if (Yflag
&& Zflag
) {
850 if ((arcn
->sb
.st_mtime
<= sb
.st_mtime
) &&
851 (arcn
->sb
.st_ctime
<= sb
.st_ctime
))
854 if (arcn
->sb
.st_ctime
<= sb
.st_ctime
)
856 } else if (arcn
->sb
.st_mtime
<= sb
.st_mtime
)
861 fputs(arcn
->name
, listf
);
867 * try to create a hard link to the src file if requested
868 * but make sure we are not trying to overwrite ourselves.
871 res
= cross_lnk(arcn
);
873 res
= chk_same(arcn
);
875 if (vflag
&& vfpart
) {
883 * have to create a new file
885 if ((arcn
->type
!= PAX_REG
) && (arcn
->type
!= PAX_CTG
)) {
887 * create a link or special file
889 if ((arcn
->type
== PAX_HLK
) || (arcn
->type
== PAX_HRG
))
890 res
= lnk_creat(arcn
);
892 res
= node_creat(arcn
);
895 if (vflag
&& vfpart
) {
903 * have to copy a regular file to the destination directory.
904 * first open source file and then create the destination file
906 if ((fdsrc
= open(arcn
->org_name
, O_RDONLY
, 0)) < 0) {
907 syswarn(1, errno
, "Unable to open %s to read",
912 if ((fddest
= file_creat(arcn
)) < 0) {
913 rdfile_close(arcn
, &fdsrc
);
919 * copy source file data to the destination file
921 cp_file(arcn
, fdsrc
, fddest
);
922 file_close(arcn
, fddest
);
923 rdfile_close(arcn
, &fdsrc
);
925 if (vflag
&& vfpart
) {
932 * restore directory modes and times as required; make sure all
933 * patterns were selected block off signals to avoid chance for
934 * multiple entry into the cleanup code.
936 sigprocmask(SIG_BLOCK
, &s_mask
, NULL
);
944 * try to find a valid header in the archive. Uses format specific
945 * routines to extract the header and id the trailer. Trailers may be
946 * located within a valid header or in an invalid header (the location
947 * is format specific. The inhead field from the option table tells us
948 * where to look for the trailer).
949 * We keep reading (and resyncing) until we get enough contiguous data
950 * to check for a header. If we cannot find one, we shift by a byte
951 * add a new byte from the archive to the end of the buffer and try again.
952 * If we get a read error, we throw out what we have (as we must have
953 * contiguous data) and start over again.
954 * ASSUMED: headers fit within a BLKMULT header.
956 * 0 if we got a header, -1 if we are unable to ever find another one
957 * (we reached the end of input, or we reached the limit on retries. see
958 * the specs for rd_wrbuf() for more details)
962 next_head(ARCHD
*arcn
)
969 int in_resync
= 0; /* set when we are in resync mode */
970 int cnt
= 0; /* counter for trailer function */
971 int first
= 1; /* on 1st read, EOF isn't premature. */
974 * set up initial conditions, we want a whole frmt->hsz block as we
977 res
= hsz
= frmt
->hsz
;
982 * keep looping until we get a contiguous FULL buffer
983 * (frmt->hsz is the proper size)
986 if ((ret
= rd_wrbuf(hdend
, res
)) == res
)
990 * If we read 0 bytes (EOF) from an archive when we
991 * expect to find a header, we have stepped upon
992 * an archive without the customary block of zeroes
993 * end marker. It's just stupid to error out on
994 * them, so exit gracefully.
996 if (first
&& ret
== 0)
1001 * some kind of archive read problem, try to resync the
1002 * storage device, better give the user the bad news.
1004 if ((ret
== 0) || (rd_sync() < 0)) {
1005 paxwarn(1,"Premature end of file on archive read");
1011 "Archive I/O error, cannot continue");
1014 paxwarn(1,"Archive I/O error. Trying to recover.");
1019 * oh well, throw it all out and start over
1026 * ok we have a contiguous buffer of the right size. Call the
1027 * format read routine. If this was not a valid header and this
1028 * format stores trailers outside of the header, call the
1029 * format specific trailer routine to check for a trailer. We
1030 * have to watch out that we do not mis-identify file data or
1031 * block padding as a header or trailer. Format specific
1032 * trailer functions must NOT check for the trailer while we
1033 * are running in resync mode. Some trailer functions may tell
1034 * us that this block cannot contain a valid header either, so
1035 * we then throw out the entire block and start over.
1037 if ((*frmt
->rd
)(arcn
, hdbuf
) == 0)
1040 if (!frmt
->inhead
) {
1042 * this format has trailers outside of valid headers
1044 if ((ret
= (*frmt
->trail
)(hdbuf
,in_resync
,&cnt
)) == 0){
1046 * valid trailer found, drain input as required
1054 * we are in resync and we were told to throw
1055 * the whole block out because none of the
1056 * bytes in this block can be used to form a
1066 * Brute force section.
1067 * not a valid header. We may be able to find a header yet. So
1068 * we shift over by one byte, and set up to read one byte at a
1069 * time from the archive and place it at the end of the buffer.
1070 * We will keep moving byte at a time until we find a header or
1071 * get a read error and have to start over.
1075 paxwarn(1,"Unable to append, archive header flaw");
1078 paxwarn(1,"Invalid header, starting valid header search.");
1081 memmove(hdbuf
, hdbuf
+1, shftsz
);
1083 hdend
= hdbuf
+ shftsz
;
1087 * ok got a valid header, check for trailer if format encodes it in the
1088 * the header. NOTE: the parameters are different than trailer routines
1089 * which encode trailers outside of the header!
1091 if (frmt
->inhead
&& ((*frmt
->trail
)(arcn
) == 0)) {
1093 * valid trailer found, drain input as required
1105 * Figure out what format an archive is. Handles archive with flaws by
1106 * brute force searches for a legal header in any supported format. The
1107 * format id routines have to be careful to NOT mis-identify a format.
1108 * ASSUMED: headers fit within a BLKMULT header.
1110 * 0 if archive found -1 otherwise
1119 int minhd
= BLKMULT
;
1124 * find the smallest header size in all archive formats and then set up
1125 * to read the archive.
1127 for (i
= 0; ford
[i
] >= 0; ++i
) {
1128 if (fsub
[ford
[i
]].hsz
< minhd
)
1129 minhd
= fsub
[ford
[i
]].hsz
;
1139 * fill the buffer with at least the smallest header
1141 i
= rd_wrbuf(hdend
, res
);
1148 * if we cannot recover from a read error quit
1150 if ((i
== 0) || (rd_sync() < 0))
1154 * when we get an error none of the data we already
1155 * have can be used to create a legal header (we just
1156 * got an error in the middle), so we throw it all out
1157 * and refill the buffer with fresh data.
1165 paxwarn(1,"Cannot identify format. Searching...");
1171 * we have at least the size of the smallest header in any
1172 * archive format. Look to see if we have a match. The array
1173 * ford[] is used to specify the header id order to reduce the
1174 * chance of incorrectly id'ing a valid header (some formats
1175 * may be subsets of each other and the order would then be
1178 for (i
= 0; ford
[i
] >= 0; ++i
) {
1179 if ((*fsub
[ford
[i
]].id
)(hdbuf
, hdsz
) < 0)
1181 frmt
= &(fsub
[ford
[i
]]);
1183 * yuck, to avoid slow special case code in the extract
1184 * routines, just push this header back as if it was
1185 * not seen. We have left extra space at start of the
1186 * buffer for this purpose. This is a bit ugly, but
1187 * adding all the special case code is far worse.
1194 * We have a flawed archive, no match. we start searching, but
1195 * we never allow additions to flawed archives
1200 paxwarn(1, "Cannot identify format. Searching...");
1205 * brute force search for a header that we can id.
1206 * we shift through byte at a time. this is slow, but we cannot
1207 * determine the nature of the flaw in the archive in a
1211 memmove(hdbuf
, hdbuf
+1, hdsz
);
1212 res
= BLKMULT
- hdsz
;
1213 hdend
= hdbuf
+ hdsz
;
1223 * we cannot find a header, bow, apologize and quit
1225 paxwarn(1, "Sorry, unable to determine archive format.");