1 /* $OpenBSD: ar_subs.c,v 1.32 2008/05/06 06:54:28 henning 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
39 static const char sccsid[] = "@(#)ar_subs.c 8.2 (Berkeley) 4/18/94";
41 static const char rcsid[] = "$OpenBSD: ar_subs.c,v 1.32 2008/05/06 06:54:28 henning Exp $";
45 #include <sys/types.h>
49 #include <sys/param.h>
60 static void wr_archive(ARCHD *, int is_app);
61 static int get_arc(void);
62 static int next_head(ARCHD *);
63 extern sigset_t s_mask;
66 * Routines which control the overall operation modes of pax as specified by
67 * the user: list, append, read ...
70 static char hdbuf[BLKMULT]; /* space for archive header on read */
71 u_long flcnt; /* number of files processed */
75 * list the contents of an archive which match user supplied pattern(s)
76 * (no pattern matches all).
89 * figure out archive type; pass any format specific options to the
90 * archive option processing routine; call the format init routine. We
91 * also save current time for ls_list() so we do not make a system
92 * call for each file we need to print. If verbose (vflag) start up
93 * the name and group caches.
95 if ((get_arc() < 0) || ((*frmt->options)() < 0) ||
96 ((*frmt->st_rd)() < 0))
99 if (vflag && ((uidtb_start() < 0) || (gidtb_start() < 0)))
105 * step through the archive until the format says it is done
107 while (next_head(arcn) == 0) {
108 if (arcn->type == PAX_GLL || arcn->type == PAX_GLF) {
110 * we need to read, to get the real filename
113 if (!(*frmt->rd_data)(arcn, arcn->type == PAX_GLF
115 (void)rd_skip(cnt + arcn->pad);
120 * check for pattern, and user specified options match.
121 * When all patterns are matched we are done.
123 if ((res = pat_match(arcn)) < 0)
126 if ((res == 0) && (sel_chk(arcn) == 0)) {
128 * pattern resulted in a selected file
130 if (pat_sel(arcn) < 0)
134 * modify the name as requested by the user if name
135 * survives modification, do a listing of the file
137 if ((res = mod_name(arcn)) < 0)
140 ls_list(arcn, now, stdout);
144 * skip to next archive format header using values calculated
145 * by the format header read routine
147 if (rd_skip(arcn->skip + arcn->pad) == 1)
152 * all done, let format have a chance to cleanup, and make sure that
153 * the patterns supplied by the user were all matched
155 (void)(*frmt->end_rd)();
156 (void)sigprocmask(SIG_BLOCK, &s_mask, NULL);
163 * extract the member(s) of an archive as specified by user supplied
164 * pattern(s) (no patterns extracts all members)
180 * figure out archive type; pass any format specific options to the
181 * archive option processing routine; call the format init routine;
182 * start up the directory modification time and access mode database
184 if ((get_arc() < 0) || ((*frmt->options)() < 0) ||
185 ((*frmt->st_rd)() < 0) || (dir_start() < 0))
189 * When we are doing interactive rename, we store the mapping of names
190 * so we can fix up hard links files later in the archive.
192 if (iflag && (name_start() < 0))
198 * step through each entry on the archive until the format read routine
201 while (next_head(arcn) == 0) {
202 if (arcn->type == PAX_GLL || arcn->type == PAX_GLF) {
204 * we need to read, to get the real filename
206 if (!(*frmt->rd_data)(arcn, arcn->type == PAX_GLF
208 (void)rd_skip(cnt + arcn->pad);
213 * check for pattern, and user specified options match. When
214 * all the patterns are matched we are done
216 if ((res = pat_match(arcn)) < 0)
219 if ((res > 0) || (sel_chk(arcn) != 0)) {
221 * file is not selected. skip past any file data and
222 * padding and go back for the next archive member
224 (void)rd_skip(arcn->skip + arcn->pad);
229 * with -u or -D only extract when the archive member is newer
230 * than the file with the same name in the file system (no
231 * test of being the same type is required).
232 * NOTE: this test is done BEFORE name modifications as
233 * specified by pax. this operation can be confusing to the
234 * user who might expect the test to be done on an existing
235 * file AFTER the name mod. In honesty the pax spec is probably
236 * flawed in this respect.
238 if ((uflag || Dflag) && ((lstat(arcn->name, &sb) == 0))) {
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);
246 if (arcn->sb.st_ctime <= sb.st_ctime) {
247 (void)rd_skip(arcn->skip + arcn->pad);
250 } else if (arcn->sb.st_mtime <= sb.st_mtime) {
251 (void)rd_skip(arcn->skip + arcn->pad);
257 * this archive member is now been selected. modify the name.
259 if ((pat_sel(arcn) < 0) || ((res = mod_name(arcn)) < 0))
263 * a bad name mod, skip and purge name from link table
266 (void)rd_skip(arcn->skip + arcn->pad);
271 * Non standard -Y and -Z flag. When the existing file is
272 * same age or newer skip
274 if ((Yflag || Zflag) && ((lstat(arcn->name, &sb) == 0))) {
275 if (Yflag && Zflag) {
276 if ((arcn->sb.st_mtime <= sb.st_mtime) &&
277 (arcn->sb.st_ctime <= sb.st_ctime)) {
278 (void)rd_skip(arcn->skip + arcn->pad);
282 if (arcn->sb.st_ctime <= sb.st_ctime) {
283 (void)rd_skip(arcn->skip + arcn->pad);
286 } else if (arcn->sb.st_mtime <= sb.st_mtime) {
287 (void)rd_skip(arcn->skip + arcn->pad);
294 ls_list(arcn, now, listf);
296 (void)safe_print(arcn->name, listf);
302 * if required, chdir around.
304 if ((arcn->pat != NULL) && (arcn->pat->chdname != NULL))
305 if (chdir(arcn->pat->chdname) != 0)
306 syswarn(1, errno, "Cannot chdir to %s",
310 * all ok, extract this member based on type
312 if ((arcn->type != PAX_REG) && (arcn->type != PAX_CTG)) {
314 * process archive members that are not regular files.
315 * throw out padding and any data that might follow the
316 * header (as determined by the format).
318 if ((arcn->type == PAX_HLK) || (arcn->type == PAX_HRG))
319 res = lnk_creat(arcn);
321 res = node_creat(arcn);
323 (void)rd_skip(arcn->skip + arcn->pad);
327 if (vflag && vfpart) {
328 (void)putc('\n', listf);
334 * we have a file with data here. If we can not create it, skip
335 * over the data and purge the name from hard link table
337 if ((fd = file_creat(arcn)) < 0) {
338 (void)rd_skip(arcn->skip + arcn->pad);
343 * extract the file from the archive and skip over padding and
344 * any unprocessed data
346 res = (*frmt->rd_data)(arcn, fd, &cnt);
347 file_close(arcn, fd);
348 if (vflag && vfpart) {
349 (void)putc('\n', listf);
353 (void)rd_skip(cnt + arcn->pad);
357 * if required, chdir around.
359 if ((arcn->pat != NULL) && (arcn->pat->chdname != NULL))
360 if (fchdir(cwdfd) != 0)
362 "Can't fchdir to starting directory");
366 * all done, restore directory modes and times as required; make sure
367 * all patterns supplied by the user were matched; block off signals
368 * to avoid chance for multiple entry into the cleanup code.
370 (void)(*frmt->end_rd)();
371 (void)sigprocmask(SIG_BLOCK, &s_mask, NULL);
379 * Write an archive. used in both creating a new archive and appends on
380 * previously written archive.
384 wr_archive(ARCHD *arcn, int is_app)
395 * if this format supports hard link storage, start up the database
398 if (((hlk = frmt->hlk) == 1) && (lnk_start() < 0))
402 * if this is not append, and there are no files, we do not write a
408 * start up the file traversal code and format specific write
410 if (ftree_start() < 0) {
414 } else if (((*frmt->st_wr)() < 0))
420 * When we are doing interactive rename, we store the mapping of names
421 * so we can fix up hard links files later in the archive.
423 if (iflag && (name_start() < 0))
429 * while there are files to archive, process them one at at time
431 while (next_file(arcn) == 0) {
433 * check if this file meets user specified options match.
435 if (sel_chk(arcn) != 0)
440 * only archive if this file is newer than a file with
441 * the same name that is already stored on the archive
443 if ((res = chk_ftime(arcn)) < 0)
446 ftree_skipped_newer(arcn);
452 * this file is considered selected now. see if this is a hard
453 * link to a file already stored
456 if (hlk && (chk_lnk(arcn) < 0))
459 if ((arcn->type == PAX_REG) || (arcn->type == PAX_HRG) ||
460 (arcn->type == PAX_CTG)) {
462 * we will have to read this file. by opening it now we
463 * can avoid writing a header to the archive for a file
464 * we were later unable to read (we also purge it from
467 if ((fd = open(arcn->org_name, O_RDONLY, 0)) < 0) {
468 syswarn(1,errno, "Unable to open %s to read",
476 * Now modify the name as requested by the user
478 if ((res = mod_name(arcn)) < 0) {
480 * name modification says to skip this file, close the
481 * file and purge link table entry
483 rdfile_close(arcn, &fd);
488 if ((res > 0) || (docrc && (set_crc(arcn, fd) < 0))) {
490 * unable to obtain the crc we need, close the file,
491 * purge link table entry
493 rdfile_close(arcn, &fd);
500 ls_list(arcn, now, listf);
502 (void)safe_print(arcn->name, listf);
509 * looks safe to store the file, have the format specific
510 * routine write routine store the file header on the archive
512 if ((res = (*wrf)(arcn)) < 0) {
513 rdfile_close(arcn, &fd);
519 * format write says no file data needs to be stored
520 * so we are done messing with this file
522 if (vflag && vfpart) {
523 (void)putc('\n', listf);
526 rdfile_close(arcn, &fd);
531 * Add file data to the archive, quit on write error. if we
532 * cannot write the entire file contents to the archive we
533 * must pad the archive to replace the missing file data
534 * (otherwise during an extract the file header for the file
535 * which FOLLOWS this one will not be where we expect it to
538 res = (*frmt->wr_data)(arcn, fd, &cnt);
539 rdfile_close(arcn, &fd);
540 if (vflag && vfpart) {
541 (void)putc('\n', listf);
548 * pad as required, cnt is number of bytes not written
550 if (((cnt > 0) && (wr_skip(cnt) < 0)) ||
551 ((arcn->pad > 0) && (wr_skip(arcn->pad) < 0)))
557 * tell format to write trailer; pad to block boundary; reset directory
558 * mode/access times, and check if all patterns supplied by the user
559 * were matched. block off signals to avoid chance for multiple entry
560 * into the cleanup code
566 (void)sigprocmask(SIG_BLOCK, &s_mask, NULL);
575 * Add file to previously written archive. Archive format specified by the
576 * user must agree with archive. The archive is read first to collect
577 * modification times (if -u) and locate the archive trailer. The archive
578 * is positioned in front of the record with the trailer and wr_archive()
579 * is called to add the new members.
580 * PAX IMPLEMENTATION DETAIL NOTE:
581 * -u is implemented by adding the new members to the end of the archive.
582 * Care is taken so that these do not end up as links to the older
583 * version of the same file already stored in the archive. It is expected
584 * when extraction occurs these newer versions will over-write the older
585 * ones stored "earlier" in the archive (this may be a bad assumption as
586 * it depends on the implementation of the program doing the extraction).
587 * It is really difficult to splice in members without either re-writing
588 * the entire archive (from the point were the old version was), or having
589 * assistance of the format specification in terms of a special update
590 * header that invalidates a previous archive record. The posix spec left
591 * the method used to implement -u unspecified. This pax is able to
592 * over write existing files that it creates.
609 * Do not allow an append operation if the actual archive is of a
610 * different format than the user specified format.
614 if ((orgfrmt != NULL) && (orgfrmt != frmt)) {
615 paxwarn(1, "Cannot mix current archive format %s with %s",
616 frmt->name, orgfrmt->name);
621 * pass the format any options and start up format
623 if (((*frmt->options)() < 0) || ((*frmt->st_rd)() < 0))
627 * if we only are adding members that are newer, we need to save the
628 * mod times for all files we see.
630 if (uflag && (ftime_start() < 0))
634 * some archive formats encode hard links by recording the device and
635 * file serial number (inode) but copy the file anyway (multiple times)
636 * to the archive. When we append, we run the risk that newly added
637 * files may have the same device and inode numbers as those recorded
638 * on the archive but during a previous run. If this happens, when the
639 * archive is extracted we get INCORRECT hard links. We avoid this by
640 * remapping the device numbers so that newly added files will never
641 * use the same device number as one found on the archive. remapping
642 * allows new members to safely have links among themselves. remapping
643 * also avoids problems with file inode (serial number) truncations
644 * when the inode number is larger than storage space in the archive
645 * header. See the remap routines for more details.
647 if ((udev = frmt->udev) && (dev_start() < 0))
651 * reading the archive may take a long time. If verbose tell the user
655 "%s: Reading archive to position at the end...", argv0);
660 * step through the archive until the format says it is done
662 while (next_head(arcn) == 0) {
664 * check if this file meets user specified options.
666 if (sel_chk(arcn) != 0) {
667 if (rd_skip(arcn->skip + arcn->pad) == 1)
674 * see if this is the newest version of this file has
675 * already been seen, if so skip.
677 if ((res = chk_ftime(arcn)) < 0)
680 if (rd_skip(arcn->skip + arcn->pad) == 1)
687 * Store this device number. Device numbers seen during the
688 * read phase of append will cause newly appended files with a
689 * device number seen in the old part of the archive to be
690 * remapped to an unused device number.
692 if ((udev && (add_dev(arcn) < 0)) ||
693 (rd_skip(arcn->skip + arcn->pad) == 1))
698 * done, finish up read and get the number of bytes to back up so we
699 * can add new members. The format might have used the hard link table,
702 tlen = (*frmt->end_rd)();
706 * try to position for write, if this fails quit. if any error occurs,
707 * we will refuse to write
709 if (appnd_start(tlen) < 0)
713 * tell the user we are done reading.
715 if (vflag && vfpart) {
716 (void)fputs("done.\n", listf);
721 * go to the writing phase to add the new members
728 * write a new archive
737 * if we only are adding members that are newer, we need to save the
738 * mod times for all files; set up for writing; pass the format any
739 * options write the archive
741 if ((uflag && (ftime_start() < 0)) || (wr_start() < 0))
743 if ((*frmt->options)() < 0)
746 wr_archive(&archd, 0);
751 * copy files from one part of the file system to another. this does not
752 * use any archive storage. The EFFECT OF THE COPY IS THE SAME as if an
753 * archive was written and then extracted in the destination directory
754 * (except the files are forced to be under the destination directory).
769 char dirbuf[PAXPATHLEN+1];
773 * set up the destination dir path and make sure it is a directory. We
774 * make sure we have a trailing / on the destination
776 dlen = strlcpy(dirbuf, dirptr, sizeof(dirbuf));
777 if (dlen >= sizeof(dirbuf) ||
778 (dlen == sizeof(dirbuf) - 1 && dirbuf[dlen - 1] != '/')) {
779 paxwarn(1, "directory name is too long %s", dirptr);
782 dest_pt = dirbuf + dlen;
783 if (*(dest_pt-1) != '/') {
788 drem = PAXPATHLEN - dlen;
790 if (stat(dirptr, &sb) < 0) {
791 syswarn(1, errno, "Cannot access destination directory %s",
795 if (!S_ISDIR(sb.st_mode)) {
796 paxwarn(1, "Destination is not a directory %s", dirptr);
801 * start up the hard link table; file traversal routines and the
802 * modification time and access mode database
804 if ((lnk_start() < 0) || (ftree_start() < 0) || (dir_start() < 0))
808 * When we are doing interactive rename, we store the mapping of names
809 * so we can fix up hard links files later in the archive.
811 if (iflag && (name_start() < 0))
815 * set up to cp file trees
820 * while there are files to archive, process them
822 while (next_file(arcn) == 0) {
826 * check if this file meets user specified options
828 if (sel_chk(arcn) != 0)
832 * if there is already a file in the destination directory with
833 * the same name and it is newer, skip the one stored on the
835 * NOTE: this test is done BEFORE name modifications as
836 * specified by pax. this can be confusing to the user who
837 * might expect the test to be done on an existing file AFTER
838 * the name mod. In honesty the pax spec is probably flawed in
841 if (uflag || Dflag) {
843 * create the destination name
845 if (strlcpy(dest_pt, arcn->name + (*arcn->name == '/'),
847 paxwarn(1, "Destination pathname too long %s",
853 * if existing file is same age or newer skip
855 res = lstat(dirbuf, &sb);
859 ftree_skipped_newer(arcn);
860 if (uflag && Dflag) {
861 if ((arcn->sb.st_mtime<=sb.st_mtime) &&
862 (arcn->sb.st_ctime<=sb.st_ctime))
865 if (arcn->sb.st_ctime <= sb.st_ctime)
867 } else if (arcn->sb.st_mtime <= sb.st_mtime)
873 * this file is considered selected. See if this is a hard link
874 * to a previous file; modify the name as requested by the
875 * user; set the final destination.
878 if ((chk_lnk(arcn) < 0) || ((res = mod_name(arcn)) < 0))
880 if ((res > 0) || (set_dest(arcn, dirbuf, dlen) < 0)) {
882 * skip file, purge from link table
889 * Non standard -Y and -Z flag. When the existing file is
890 * same age or newer skip
892 if ((Yflag || Zflag) && ((lstat(arcn->name, &sb) == 0))) {
893 if (Yflag && Zflag) {
894 if ((arcn->sb.st_mtime <= sb.st_mtime) &&
895 (arcn->sb.st_ctime <= sb.st_ctime))
898 if (arcn->sb.st_ctime <= sb.st_ctime)
900 } else if (arcn->sb.st_mtime <= sb.st_mtime)
905 (void)safe_print(arcn->name, listf);
911 * try to create a hard link to the src file if requested
912 * but make sure we are not trying to overwrite ourselves.
915 res = cross_lnk(arcn);
917 res = chk_same(arcn);
919 if (vflag && vfpart) {
920 (void)putc('\n', listf);
927 * have to create a new file
929 if ((arcn->type != PAX_REG) && (arcn->type != PAX_CTG)) {
931 * create a link or special file
933 if ((arcn->type == PAX_HLK) || (arcn->type == PAX_HRG))
934 res = lnk_creat(arcn);
936 res = node_creat(arcn);
939 if (vflag && vfpart) {
940 (void)putc('\n', listf);
947 * have to copy a regular file to the destination directory.
948 * first open source file and then create the destination file
950 if ((fdsrc = open(arcn->org_name, O_RDONLY, 0)) < 0) {
951 syswarn(1, errno, "Unable to open %s to read",
956 if ((fddest = file_creat(arcn)) < 0) {
957 rdfile_close(arcn, &fdsrc);
963 * copy source file data to the destination file
965 cp_file(arcn, fdsrc, fddest);
966 file_close(arcn, fddest);
967 rdfile_close(arcn, &fdsrc);
969 if (vflag && vfpart) {
970 (void)putc('\n', listf);
976 * restore directory modes and times as required; make sure all
977 * patterns were selected block off signals to avoid chance for
978 * multiple entry into the cleanup code.
980 (void)sigprocmask(SIG_BLOCK, &s_mask, NULL);
988 * try to find a valid header in the archive. Uses format specific
989 * routines to extract the header and id the trailer. Trailers may be
990 * located within a valid header or in an invalid header (the location
991 * is format specific. The inhead field from the option table tells us
992 * where to look for the trailer).
993 * We keep reading (and resyncing) until we get enough contiguous data
994 * to check for a header. If we cannot find one, we shift by a byte
995 * add a new byte from the archive to the end of the buffer and try again.
996 * If we get a read error, we throw out what we have (as we must have
997 * contiguous data) and start over again.
998 * ASSUMED: headers fit within a BLKMULT header.
1000 * 0 if we got a header, -1 if we are unable to ever find another one
1001 * (we reached the end of input, or we reached the limit on retries. see
1002 * the specs for rd_wrbuf() for more details)
1006 next_head(ARCHD *arcn)
1013 int in_resync = 0; /* set when we are in resync mode */
1014 int cnt = 0; /* counter for trailer function */
1015 int first = 1; /* on 1st read, EOF isn't premature. */
1018 * set up initial conditions, we want a whole frmt->hsz block as we
1021 res = hsz = frmt->hsz;
1026 * keep looping until we get a contiguous FULL buffer
1027 * (frmt->hsz is the proper size)
1030 if ((ret = rd_wrbuf(hdend, res)) == res)
1034 * If we read 0 bytes (EOF) from an archive when we
1035 * expect to find a header, we have stepped upon
1036 * an archive without the customary block of zeroes
1037 * end marker. It's just stupid to error out on
1038 * them, so exit gracefully.
1040 if (first && ret == 0)
1045 * some kind of archive read problem, try to resync the
1046 * storage device, better give the user the bad news.
1048 if ((ret == 0) || (rd_sync() < 0)) {
1049 paxwarn(1,"Premature end of file on archive read");
1055 "Archive I/O error, cannot continue");
1058 paxwarn(1,"Archive I/O error. Trying to recover.");
1063 * oh well, throw it all out and start over
1070 * ok we have a contiguous buffer of the right size. Call the
1071 * format read routine. If this was not a valid header and this
1072 * format stores trailers outside of the header, call the
1073 * format specific trailer routine to check for a trailer. We
1074 * have to watch out that we do not mis-identify file data or
1075 * block padding as a header or trailer. Format specific
1076 * trailer functions must NOT check for the trailer while we
1077 * are running in resync mode. Some trailer functions may tell
1078 * us that this block cannot contain a valid header either, so
1079 * we then throw out the entire block and start over.
1081 if ((*frmt->rd)(arcn, hdbuf) == 0)
1084 if (!frmt->inhead) {
1086 * this format has trailers outside of valid headers
1088 if ((ret = (*frmt->trail)(arcn,hdbuf,in_resync,&cnt)) == 0){
1090 * valid trailer found, drain input as required
1098 * we are in resync and we were told to throw
1099 * the whole block out because none of the
1100 * bytes in this block can be used to form a
1110 * Brute force section.
1111 * not a valid header. We may be able to find a header yet. So
1112 * we shift over by one byte, and set up to read one byte at a
1113 * time from the archive and place it at the end of the buffer.
1114 * We will keep moving byte at a time until we find a header or
1115 * get a read error and have to start over.
1119 paxwarn(1,"Unable to append, archive header flaw");
1122 paxwarn(1,"Invalid header, starting valid header search.");
1125 memmove(hdbuf, hdbuf+1, shftsz);
1127 hdend = hdbuf + shftsz;
1131 * ok got a valid header, check for trailer if format encodes it in the
1132 * the header. NOTE: the parameters are different than trailer routines
1133 * which encode trailers outside of the header!
1135 if (frmt->inhead && ((*frmt->trail)(arcn,NULL,0,NULL) == 0)) {
1137 * valid trailer found, drain input as required
1149 * Figure out what format an archive is. Handles archive with flaws by
1150 * brute force searches for a legal header in any supported format. The
1151 * format id routines have to be careful to NOT mis-identify a format.
1152 * ASSUMED: headers fit within a BLKMULT header.
1154 * 0 if archive found -1 otherwise
1163 int minhd = BLKMULT;
1168 * find the smallest header size in all archive formats and then set up
1169 * to read the archive.
1171 for (i = 0; ford[i] >= 0; ++i) {
1172 if (fsub[ford[i]].hsz < minhd)
1173 minhd = fsub[ford[i]].hsz;
1183 * fill the buffer with at least the smallest header
1185 i = rd_wrbuf(hdend, res);
1192 * if we cannot recover from a read error quit
1194 if ((i == 0) || (rd_sync() < 0))
1198 * when we get an error none of the data we already
1199 * have can be used to create a legal header (we just
1200 * got an error in the middle), so we throw it all out
1201 * and refill the buffer with fresh data.
1209 paxwarn(1,"Cannot identify format. Searching...");
1215 * we have at least the size of the smallest header in any
1216 * archive format. Look to see if we have a match. The array
1217 * ford[] is used to specify the header id order to reduce the
1218 * chance of incorrectly id'ing a valid header (some formats
1219 * may be subsets of each other and the order would then be
1222 for (i = 0; ford[i] >= 0; ++i) {
1223 if ((*fsub[ford[i]].id)(hdbuf, hdsz) < 0)
1225 frmt = &(fsub[ford[i]]);
1227 * yuck, to avoid slow special case code in the extract
1228 * routines, just push this header back as if it was
1229 * not seen. We have left extra space at start of the
1230 * buffer for this purpose. This is a bit ugly, but
1231 * adding all the special case code is far worse.
1238 * We have a flawed archive, no match. we start searching, but
1239 * we never allow additions to flawed archives
1244 paxwarn(1, "Cannot identify format. Searching...");
1249 * brute force search for a header that we can id.
1250 * we shift through byte at a time. this is slow, but we cannot
1251 * determine the nature of the flaw in the archive in a
1255 memmove(hdbuf, hdbuf+1, hdsz);
1256 res = BLKMULT - hdsz;
1257 hdend = hdbuf + hdsz;
1267 * we cannot find a header, bow, apologize and quit
1269 paxwarn(1, "Sorry, unable to determine archive format.");