1 /* $OpenBSD: ar_subs.c,v 1.48 2016/08/26 05:06:14 guenther Exp $ */
2 /* $NetBSD: ar_subs.c,v 1.5 1995/03/21 09:07:06 cgd Exp $ */
5 * Copyright (c) 1992 Keith Muller.
6 * Copyright (c) 1992, 1993
7 * The Regents of the University of California. All rights reserved.
9 * This code is derived from software contributed to Berkeley by
10 * Keith Muller of the University of California, San Diego.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
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) {
95 if (arcn
->type
== PAX_GLL
|| arcn
->type
== PAX_GLF
) {
97 * we need to read, to get the real filename
100 if (!rd_wrfile(arcn
, arcn
->type
== PAX_GLF
102 (void)rd_skip(cnt
+ arcn
->pad
);
107 * check for pattern, and user specified options match.
108 * When all patterns are matched we are done.
110 if ((res
= pat_match(arcn
)) < 0)
113 if ((res
== 0) && (sel_chk(arcn
) == 0)) {
115 * pattern resulted in a selected file
117 if (pat_sel(arcn
) < 0)
121 * modify the name as requested by the user if name
122 * survives modification, do a listing of the file
124 if ((res
= mod_name(arcn
)) < 0)
127 ls_list(arcn
, now
, stdout
);
131 * skip to next archive format header using values calculated
132 * by the format header read routine
134 if (rd_skip(arcn
->skip
+ arcn
->pad
) == 1)
139 * all done, let format have a chance to cleanup, and make sure that
140 * the patterns supplied by the user were all matched
142 (void)(*frmt
->end_rd
)();
143 (void)sigprocmask(SIG_BLOCK
, &s_mask
, NULL
);
149 cmp_file_times(int mtime_flag
, int ctime_flag
, ARCHD
*arcn
, struct stat
*sbp
)
154 if (lstat(arcn
->name
, &sb
) != 0)
159 if (ctime_flag
&& mtime_flag
)
160 return (timespeccmp(&arcn
->sb
.st_mtim
, &sbp
->st_mtim
, <=) &&
161 timespeccmp(&arcn
->sb
.st_ctim
, &sbp
->st_ctim
, <=));
163 return (timespeccmp(&arcn
->sb
.st_ctim
, &sbp
->st_ctim
, <=));
165 return (timespeccmp(&arcn
->sb
.st_mtim
, &sbp
->st_mtim
, <=));
170 * extract the member(s) of an archive as specified by user supplied
171 * pattern(s) (no patterns extracts all members)
188 * figure out archive type; pass any format specific options to the
189 * archive option processing routine; call the format init routine;
190 * start up the directory modification time and access mode database
192 if ((get_arc() < 0) || ((*frmt
->options
)() < 0) ||
193 ((*frmt
->st_rd
)() < 0) || (dir_start() < 0))
197 * When we are doing interactive rename, we store the mapping of names
198 * so we can fix up hard links files later in the archive.
200 if (iflag
&& (name_start() < 0))
206 * step through each entry on the archive until the format read routine
209 while (next_head(arcn
) == 0) {
210 if (arcn
->type
== PAX_GLL
|| arcn
->type
== PAX_GLF
) {
212 * we need to read, to get the real filename
214 if (!rd_wrfile(arcn
, arcn
->type
== PAX_GLF
216 (void)rd_skip(cnt
+ arcn
->pad
);
221 * check for pattern, and user specified options match. When
222 * all the patterns are matched we are done
224 if ((res
= pat_match(arcn
)) < 0)
227 if ((res
> 0) || (sel_chk(arcn
) != 0)) {
229 * file is not selected. skip past any file data and
230 * padding and go back for the next archive member
232 (void)rd_skip(arcn
->skip
+ arcn
->pad
);
237 * with -u or -D only extract when the archive member is newer
238 * than the file with the same name in the file system (no
239 * test of being the same type is required).
240 * NOTE: this test is done BEFORE name modifications as
241 * specified by pax. this operation can be confusing to the
242 * user who might expect the test to be done on an existing
243 * file AFTER the name mod. In honesty the pax spec is probably
244 * flawed in this respect.
246 if ((uflag
|| Dflag
) &&
247 cmp_file_times(uflag
, Dflag
, arcn
, NULL
)) {
248 (void)rd_skip(arcn
->skip
+ arcn
->pad
);
253 * this archive member is now been selected. modify the name.
255 if ((pat_sel(arcn
) < 0) || ((res
= mod_name(arcn
)) < 0))
259 * a bad name mod, skip and purge name from link table
262 (void)rd_skip(arcn
->skip
+ arcn
->pad
);
267 * Non standard -Y and -Z flag. When the existing file is
268 * same age or newer skip
270 if ((Yflag
|| Zflag
) &&
271 cmp_file_times(Yflag
, Zflag
, arcn
, NULL
)) {
272 (void)rd_skip(arcn
->skip
+ arcn
->pad
);
278 ls_list(arcn
, now
, listf
);
280 (void)safe_print(arcn
->name
, listf
);
286 * if required, chdir around.
288 if ((arcn
->pat
!= NULL
) && (arcn
->pat
->chdname
!= NULL
))
289 if (chdir(arcn
->pat
->chdname
) != 0)
290 syswarn(1, errno
, "Cannot chdir to %s",
294 * all ok, extract this member based on type
296 if (!PAX_IS_REG(arcn
->type
)) {
298 * process archive members that are not regular files.
299 * throw out padding and any data that might follow the
300 * header (as determined by the format).
302 if (PAX_IS_HARDLINK(arcn
->type
))
303 res
= lnk_creat(arcn
);
305 res
= node_creat(arcn
);
307 (void)rd_skip(arcn
->skip
+ arcn
->pad
);
311 if (vflag
&& vfpart
) {
312 (void)putc('\n', listf
);
318 * we have a file with data here. If we can not create it, skip
319 * over the data and purge the name from hard link table
321 if ((fd
= file_creat(arcn
)) < 0) {
322 (void)rd_skip(arcn
->skip
+ arcn
->pad
);
327 * extract the file from the archive and skip over padding and
328 * any unprocessed data
330 res
= rd_wrfile(arcn
, fd
, &cnt
);
331 file_close(arcn
, fd
);
332 if (vflag
&& vfpart
) {
333 (void)putc('\n', listf
);
337 (void)rd_skip(cnt
+ arcn
->pad
);
341 * if required, chdir around.
343 if ((arcn
->pat
!= NULL
) && (arcn
->pat
->chdname
!= NULL
))
344 if (fchdir(cwdfd
) != 0)
346 "Can't fchdir to starting directory");
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.
354 (void)(*frmt
->end_rd
)();
355 (void)sigprocmask(SIG_BLOCK
, &s_mask
, NULL
);
364 * Write an archive. used in both creating a new archive and appends on
365 * previously written archive.
369 wr_archive(ARCHD
*arcn
, int is_app
)
380 * if this format supports hard link storage, start up the database
383 if (((hlk
= frmt
->hlk
) == 1) && (lnk_start() < 0))
387 * if this is not append, and there are no files, we do not write a
393 * start up the file traversal code and format specific write
395 if (ftree_start() < 0) {
399 } else if (((*frmt
->st_wr
)() < 0))
405 * When we are doing interactive rename, we store the mapping of names
406 * so we can fix up hard links files later in the archive.
408 if (iflag
&& (name_start() < 0))
414 * while there are files to archive, process them one at at time
416 while (next_file(arcn
) == 0) {
418 * check if this file meets user specified options match.
420 if (sel_chk(arcn
) != 0)
425 * only archive if this file is newer than a file with
426 * the same name that is already stored on the archive
428 if ((res
= chk_ftime(arcn
)) < 0)
431 ftree_skipped_newer(arcn
);
437 * this file is considered selected now. see if this is a hard
438 * link to a file already stored
441 if (hlk
&& (chk_lnk(arcn
) < 0))
444 if (PAX_IS_REG(arcn
->type
) || (arcn
->type
== PAX_HRG
)) {
446 * we will have to read this file. by opening it now we
447 * can avoid writing a header to the archive for a file
448 * we were later unable to read (we also purge it from
451 if ((fd
= open(arcn
->org_name
, O_RDONLY
, 0)) < 0) {
452 syswarn(1,errno
, "Unable to open %s to read",
460 * Now modify the name as requested by the user
462 if ((res
= mod_name(arcn
)) < 0) {
464 * name modification says to skip this file, close the
465 * file and purge link table entry
467 rdfile_close(arcn
, &fd
);
472 if ((res
> 0) || (docrc
&& (set_crc(arcn
, fd
) < 0))) {
474 * unable to obtain the crc we need, close the file,
475 * purge link table entry
477 rdfile_close(arcn
, &fd
);
484 ls_list(arcn
, now
, listf
);
486 (void)safe_print(arcn
->name
, listf
);
493 * looks safe to store the file, have the format specific
494 * routine write routine store the file header on the archive
496 if ((res
= (*wrf
)(arcn
)) < 0) {
497 rdfile_close(arcn
, &fd
);
503 * format write says no file data needs to be stored
504 * so we are done messing with this file
506 if (vflag
&& vfpart
) {
507 (void)putc('\n', listf
);
510 rdfile_close(arcn
, &fd
);
515 * Add file data to the archive, quit on write error. if we
516 * cannot write the entire file contents to the archive we
517 * must pad the archive to replace the missing file data
518 * (otherwise during an extract the file header for the file
519 * which FOLLOWS this one will not be where we expect it to
522 res
= wr_rdfile(arcn
, fd
, &cnt
);
523 rdfile_close(arcn
, &fd
);
524 if (vflag
&& vfpart
) {
525 (void)putc('\n', listf
);
532 * pad as required, cnt is number of bytes not written
534 if (((cnt
> 0) && (wr_skip(cnt
) < 0)) ||
535 ((arcn
->pad
> 0) && (wr_skip(arcn
->pad
) < 0)))
541 * tell format to write trailer; pad to block boundary; reset directory
542 * mode/access times, and check if all patterns supplied by the user
543 * were matched. block off signals to avoid chance for multiple entry
544 * into the cleanup code
550 (void)sigprocmask(SIG_BLOCK
, &s_mask
, NULL
);
559 * Add file to previously written archive. Archive format specified by the
560 * user must agree with archive. The archive is read first to collect
561 * modification times (if -u) and locate the archive trailer. The archive
562 * is positioned in front of the record with the trailer and wr_archive()
563 * is called to add the new members.
564 * PAX IMPLEMENTATION DETAIL NOTE:
565 * -u is implemented by adding the new members to the end of the archive.
566 * Care is taken so that these do not end up as links to the older
567 * version of the same file already stored in the archive. It is expected
568 * when extraction occurs these newer versions will over-write the older
569 * ones stored "earlier" in the archive (this may be a bad assumption as
570 * it depends on the implementation of the program doing the extraction).
571 * It is really difficult to splice in members without either re-writing
572 * the entire archive (from the point were the old version was), or having
573 * assistance of the format specification in terms of a special update
574 * header that invalidates a previous archive record. The posix spec left
575 * the method used to implement -u unspecified. This pax is able to
576 * over write existing files that it creates.
593 * Do not allow an append operation if the actual archive is of a
594 * different format than the user specified format.
598 if ((orgfrmt
!= NULL
) && (orgfrmt
!= frmt
)) {
599 paxwarn(1, "Cannot mix current archive format %s with %s",
600 frmt
->name
, orgfrmt
->name
);
605 * pass the format any options and start up format
607 if (((*frmt
->options
)() < 0) || ((*frmt
->st_rd
)() < 0))
611 * if we only are adding members that are newer, we need to save the
612 * mod times for all files we see.
614 if (uflag
&& (ftime_start() < 0))
618 * some archive formats encode hard links by recording the device and
619 * file serial number (inode) but copy the file anyway (multiple times)
620 * to the archive. When we append, we run the risk that newly added
621 * files may have the same device and inode numbers as those recorded
622 * on the archive but during a previous run. If this happens, when the
623 * archive is extracted we get INCORRECT hard links. We avoid this by
624 * remapping the device numbers so that newly added files will never
625 * use the same device number as one found on the archive. remapping
626 * allows new members to safely have links among themselves. remapping
627 * also avoids problems with file inode (serial number) truncations
628 * when the inode number is larger than storage space in the archive
629 * header. See the remap routines for more details.
631 if ((udev
= frmt
->udev
) && (dev_start() < 0))
635 * reading the archive may take a long time. If verbose tell the user
639 "%s: Reading archive to position at the end...", argv0
);
644 * step through the archive until the format says it is done
646 while (next_head(arcn
) == 0) {
648 * check if this file meets user specified options.
650 if (sel_chk(arcn
) != 0) {
651 if (rd_skip(arcn
->skip
+ arcn
->pad
) == 1)
658 * see if this is the newest version of this file has
659 * already been seen, if so skip.
661 if ((res
= chk_ftime(arcn
)) < 0)
664 if (rd_skip(arcn
->skip
+ arcn
->pad
) == 1)
671 * Store this device number. Device numbers seen during the
672 * read phase of append will cause newly appended files with a
673 * device number seen in the old part of the archive to be
674 * remapped to an unused device number.
676 if ((udev
&& (add_dev(arcn
) < 0)) ||
677 (rd_skip(arcn
->skip
+ arcn
->pad
) == 1))
682 * done, finish up read and get the number of bytes to back up so we
683 * can add new members. The format might have used the hard link table,
686 tlen
= (*frmt
->end_rd
)();
690 * try to position for write, if this fails quit. if any error occurs,
691 * we will refuse to write
693 if (appnd_start(tlen
) < 0)
697 * tell the user we are done reading.
699 if (vflag
&& vfpart
) {
700 (void)fputs("done.\n", listf
);
705 * go to the writing phase to add the new members
712 * write a new archive
721 * if we only are adding members that are newer, we need to save the
722 * mod times for all files; set up for writing; pass the format any
723 * options write the archive
725 if ((uflag
&& (ftime_start() < 0)) || (wr_start() < 0))
727 if ((*frmt
->options
)() < 0)
730 wr_archive(&archd
, 0);
735 * copy files from one part of the file system to another. this does not
736 * use any archive storage. The EFFECT OF THE COPY IS THE SAME as if an
737 * archive was written and then extracted in the destination directory
738 * (except the files are forced to be under the destination directory).
753 char dirbuf
[PAXPATHLEN
+1];
759 * set up the destination dir path and make sure it is a directory. We
760 * make sure we have a trailing / on the destination
762 dlen
= strlcpy(dirbuf
, dirptr
, sizeof(dirbuf
));
763 if (dlen
>= sizeof(dirbuf
) ||
764 (dlen
== sizeof(dirbuf
) - 1 && dirbuf
[dlen
- 1] != '/')) {
765 paxwarn(1, "directory name is too long %s", dirptr
);
768 dest_pt
= dirbuf
+ dlen
;
769 if (*(dest_pt
-1) != '/') {
774 drem
= PAXPATHLEN
- dlen
;
776 if (stat(dirptr
, &sb
) < 0) {
777 syswarn(1, errno
, "Cannot access destination directory %s",
781 if (!S_ISDIR(sb
.st_mode
)) {
782 paxwarn(1, "Destination is not a directory %s", dirptr
);
787 * start up the hard link table; file traversal routines and the
788 * modification time and access mode database
790 if ((lnk_start() < 0) || (ftree_start() < 0) || (dir_start() < 0))
794 * When we are doing interactive rename, we store the mapping of names
795 * so we can fix up hard links files later in the archive.
797 if (iflag
&& (name_start() < 0))
801 * set up to cp file trees
806 * while there are files to archive, process them
808 while (next_file(arcn
) == 0) {
812 * check if this file meets user specified options
814 if (sel_chk(arcn
) != 0)
818 * if there is already a file in the destination directory with
819 * the same name and it is newer, skip the one stored on the
821 * NOTE: this test is done BEFORE name modifications as
822 * specified by pax. this can be confusing to the user who
823 * might expect the test to be done on an existing file AFTER
824 * the name mod. In honesty the pax spec is probably flawed in
827 if (uflag
|| Dflag
) {
829 * create the destination name
831 if (strlcpy(dest_pt
, arcn
->name
+ (*arcn
->name
== '/'),
833 paxwarn(1, "Destination pathname too long %s",
839 * if existing file is same age or newer skip
841 res
= lstat(dirbuf
, &sb
);
845 ftree_skipped_newer(arcn
);
846 if (cmp_file_times(uflag
, Dflag
, arcn
, &sb
))
852 * this file is considered selected. See if this is a hard link
853 * to a previous file; modify the name as requested by the
854 * user; set the final destination.
857 if ((chk_lnk(arcn
) < 0) || ((res
= mod_name(arcn
)) < 0))
859 if ((res
> 0) || (set_dest(arcn
, dirbuf
, dlen
) < 0)) {
861 * skip file, purge from link table
868 * Non standard -Y and -Z flag. When the existing file is
869 * same age or newer skip
871 if ((Yflag
|| Zflag
) &&
872 cmp_file_times(Yflag
, Zflag
, arcn
, NULL
))
876 (void)safe_print(arcn
->name
, listf
);
882 * try to create a hard link to the src file if requested
883 * but make sure we are not trying to overwrite ourselves.
886 res
= cross_lnk(arcn
);
888 res
= chk_same(arcn
);
890 if (vflag
&& vfpart
) {
891 (void)putc('\n', listf
);
898 * have to create a new file
900 if (!PAX_IS_REG(arcn
->type
)) {
902 * create a link or special file
904 if (PAX_IS_HARDLINK(arcn
->type
))
905 res
= lnk_creat(arcn
);
907 res
= node_creat(arcn
);
910 if (vflag
&& vfpart
) {
911 (void)putc('\n', listf
);
918 * have to copy a regular file to the destination directory.
919 * first open source file and then create the destination file
921 if ((fdsrc
= open(arcn
->org_name
, O_RDONLY
, 0)) < 0) {
922 syswarn(1, errno
, "Unable to open %s to read",
927 if ((fddest
= file_creat(arcn
)) < 0) {
928 rdfile_close(arcn
, &fdsrc
);
934 * copy source file data to the destination file
936 cp_file(arcn
, fdsrc
, fddest
);
937 file_close(arcn
, fddest
);
938 rdfile_close(arcn
, &fdsrc
);
940 if (vflag
&& vfpart
) {
941 (void)putc('\n', listf
);
947 * restore directory modes and times as required; make sure all
948 * patterns were selected block off signals to avoid chance for
949 * multiple entry into the cleanup code.
951 (void)sigprocmask(SIG_BLOCK
, &s_mask
, NULL
);
960 * try to find a valid header in the archive. Uses format specific
961 * routines to extract the header and id the trailer. Trailers may be
962 * located within a valid header or in an invalid header (the location
963 * is format specific. The inhead field from the option table tells us
964 * where to look for the trailer).
965 * We keep reading (and resyncing) until we get enough contiguous data
966 * to check for a header. If we cannot find one, we shift by a byte
967 * add a new byte from the archive to the end of the buffer and try again.
968 * If we get a read error, we throw out what we have (as we must have
969 * contiguous data) and start over again.
970 * ASSUMED: headers fit within a BLKMULT header.
972 * 0 if we got a header, -1 if we are unable to ever find another one
973 * (we reached the end of input, or we reached the limit on retries. see
974 * the specs for rd_wrbuf() for more details)
978 next_head(ARCHD
*arcn
)
985 int in_resync
= 0; /* set when we are in resync mode */
986 int cnt
= 0; /* counter for trailer function */
987 int first
= 1; /* on 1st read, EOF isn't premature. */
990 * set up initial conditions, we want a whole frmt->hsz block as we
993 res
= hsz
= frmt
->hsz
;
998 * keep looping until we get a contiguous FULL buffer
999 * (frmt->hsz is the proper size)
1002 if ((ret
= rd_wrbuf(hdend
, res
)) == res
)
1006 * If we read 0 bytes (EOF) from an archive when we
1007 * expect to find a header, we have stepped upon
1008 * an archive without the customary block of zeroes
1009 * end marker. It's just stupid to error out on
1010 * them, so exit gracefully.
1012 if (first
&& ret
== 0)
1017 * some kind of archive read problem, try to resync the
1018 * storage device, better give the user the bad news.
1020 if ((ret
== 0) || (rd_sync() < 0)) {
1021 paxwarn(1,"Premature end of file on archive read");
1027 "Archive I/O error, cannot continue");
1030 paxwarn(1,"Archive I/O error. Trying to recover.");
1035 * oh well, throw it all out and start over
1042 * ok we have a contiguous buffer of the right size. Call the
1043 * format read routine. If this was not a valid header and this
1044 * format stores trailers outside of the header, call the
1045 * format specific trailer routine to check for a trailer. We
1046 * have to watch out that we do not mis-identify file data or
1047 * block padding as a header or trailer. Format specific
1048 * trailer functions must NOT check for the trailer while we
1049 * are running in resync mode. Some trailer functions may tell
1050 * us that this block cannot contain a valid header either, so
1051 * we then throw out the entire block and start over.
1053 if ((*frmt
->rd
)(arcn
, hdbuf
) == 0)
1056 if (!frmt
->inhead
) {
1058 * this format has trailers outside of valid headers
1060 if ((ret
= (*frmt
->trail
)(arcn
,hdbuf
,in_resync
,&cnt
)) == 0){
1062 * valid trailer found, drain input as required
1070 * we are in resync and we were told to throw
1071 * the whole block out because none of the
1072 * bytes in this block can be used to form a
1082 * Brute force section.
1083 * not a valid header. We may be able to find a header yet. So
1084 * we shift over by one byte, and set up to read one byte at a
1085 * time from the archive and place it at the end of the buffer.
1086 * We will keep moving byte at a time until we find a header or
1087 * get a read error and have to start over.
1091 paxwarn(1,"Unable to append, archive header flaw");
1094 paxwarn(1,"Invalid header, starting valid header search.");
1097 memmove(hdbuf
, hdbuf
+1, shftsz
);
1099 hdend
= hdbuf
+ shftsz
;
1103 * ok got a valid header, check for trailer if format encodes it in the
1104 * the header. NOTE: the parameters are different than trailer routines
1105 * which encode trailers outside of the header!
1107 if (frmt
->inhead
&& ((*frmt
->trail
)(arcn
,NULL
,0,NULL
) == 0)) {
1109 * valid trailer found, drain input as required
1121 * Figure out what format an archive is. Handles archive with flaws by
1122 * brute force searches for a legal header in any supported format. The
1123 * format id routines have to be careful to NOT mis-identify a format.
1124 * ASSUMED: headers fit within a BLKMULT header.
1126 * 0 if archive found -1 otherwise
1135 int minhd
= BLKMULT
;
1140 * find the smallest header size in all archive formats and then set up
1141 * to read the archive.
1143 for (i
= 0; ford
[i
] >= 0; ++i
) {
1144 if (fsub
[ford
[i
]].name
!= NULL
&& fsub
[ford
[i
]].hsz
< minhd
)
1145 minhd
= fsub
[ford
[i
]].hsz
;
1155 * fill the buffer with at least the smallest header
1157 i
= rd_wrbuf(hdend
, res
);
1164 * if we cannot recover from a read error quit
1166 if ((i
== 0) || (rd_sync() < 0))
1170 * when we get an error none of the data we already
1171 * have can be used to create a legal header (we just
1172 * got an error in the middle), so we throw it all out
1173 * and refill the buffer with fresh data.
1181 paxwarn(1,"Cannot identify format. Searching...");
1187 * we have at least the size of the smallest header in any
1188 * archive format. Look to see if we have a match. The array
1189 * ford[] is used to specify the header id order to reduce the
1190 * chance of incorrectly id'ing a valid header (some formats
1191 * may be subsets of each other and the order would then be
1194 for (i
= 0; ford
[i
] >= 0; ++i
) {
1195 if (fsub
[ford
[i
]].id
== NULL
||
1196 (*fsub
[ford
[i
]].id
)(hdbuf
, hdsz
) < 0)
1198 frmt
= &(fsub
[ford
[i
]]);
1200 * yuck, to avoid slow special case code in the extract
1201 * routines, just push this header back as if it was
1202 * not seen. We have left extra space at start of the
1203 * buffer for this purpose. This is a bit ugly, but
1204 * adding all the special case code is far worse.
1211 * We have a flawed archive, no match. we start searching, but
1212 * we never allow additions to flawed archives
1217 paxwarn(1, "Cannot identify format. Searching...");
1222 * brute force search for a header that we can id.
1223 * we shift through byte at a time. this is slow, but we cannot
1224 * determine the nature of the flaw in the archive in a
1228 memmove(hdbuf
, hdbuf
+1, hdsz
);
1229 res
= BLKMULT
- hdsz
;
1230 hdend
= hdbuf
+ hdsz
;
1240 * we cannot find a header, bow, apologize and quit
1242 paxwarn(1, "Sorry, unable to determine archive format.");