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 * 4. 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
36 static char sccsid[] = "@(#)ar_subs.c 8.2 (Berkeley) 4/18/94";
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
42 #include <sys/types.h>
55 static void wr_archive(ARCHD *, int is_app);
56 static int get_arc(void);
57 static int next_head(ARCHD *);
58 extern sigset_t s_mask;
61 * Routines which control the overall operation modes of pax as specified by
62 * the user: list, append, read ...
65 static char hdbuf[BLKMULT]; /* space for archive header on read */
66 u_long flcnt; /* number of files processed */
70 * list the contents of an archive which match user supplied pattern(s)
71 * (no pattern matches all).
84 * figure out archive type; pass any format specific options to the
85 * archive option processing routine; call the format init routine. We
86 * also save current time for ls_list() so we do not make a system
87 * call for each file we need to print. If verbose (vflag) start up
88 * the name and group caches.
90 if ((get_arc() < 0) || ((*frmt->options)() < 0) ||
91 ((*frmt->st_rd)() < 0))
94 if (vflag && ((uidtb_start() < 0) || (gidtb_start() < 0)))
100 * step through the archive until the format says it is done
102 while (next_head(arcn) == 0) {
104 * check for pattern, and user specified options match.
105 * When all patterns are matched we are done.
107 if ((res = pat_match(arcn)) < 0)
110 if ((res == 0) && (sel_chk(arcn) == 0)) {
112 * pattern resulted in a selected file
114 if (pat_sel(arcn) < 0)
118 * modify the name as requested by the user if name
119 * survives modification, do a listing of the file
121 if ((res = mod_name(arcn)) < 0)
124 ls_list(arcn, now, stdout);
128 * skip to next archive format header using values calculated
129 * by the format header read routine
131 if (rd_skip(arcn->skip + arcn->pad) == 1)
136 * all done, let format have a chance to cleanup, and make sure that
137 * the patterns supplied by the user were all matched
139 (void)(*frmt->end_rd)();
140 (void)sigprocmask(SIG_BLOCK, &s_mask, NULL);
147 * extract the member(s) of an archive as specified by user supplied
148 * pattern(s) (no patterns extracts all members)
164 * figure out archive type; pass any format specific options to the
165 * archive option processing routine; call the format init routine;
166 * start up the directory modification time and access mode database
168 if ((get_arc() < 0) || ((*frmt->options)() < 0) ||
169 ((*frmt->st_rd)() < 0) || (dir_start() < 0))
173 * When we are doing interactive rename, we store the mapping of names
174 * so we can fix up hard links files later in the archive.
176 if (iflag && (name_start() < 0))
182 * step through each entry on the archive until the format read routine
185 while (next_head(arcn) == 0) {
188 * check for pattern, and user specified options match. When
189 * all the patterns are matched we are done
191 if ((res = pat_match(arcn)) < 0)
194 if ((res > 0) || (sel_chk(arcn) != 0)) {
196 * file is not selected. skip past any file data and
197 * padding and go back for the next archive member
199 (void)rd_skip(arcn->skip + arcn->pad);
204 * with -u or -D only extract when the archive member is newer
205 * than the file with the same name in the file system (nos
206 * test of being the same type is required).
207 * NOTE: this test is done BEFORE name modifications as
208 * specified by pax. this operation can be confusing to the
209 * user who might expect the test to be done on an existing
210 * file AFTER the name mod. In honesty the pax spec is probably
211 * flawed in this respect.
213 if ((uflag || Dflag) && ((lstat(arcn->name, &sb) == 0))) {
214 if (uflag && Dflag) {
215 if ((arcn->sb.st_mtime <= sb.st_mtime) &&
216 (arcn->sb.st_ctime <= sb.st_ctime)) {
217 (void)rd_skip(arcn->skip + arcn->pad);
221 if (arcn->sb.st_ctime <= sb.st_ctime) {
222 (void)rd_skip(arcn->skip + arcn->pad);
225 } else if (arcn->sb.st_mtime <= sb.st_mtime) {
226 (void)rd_skip(arcn->skip + arcn->pad);
232 * this archive member is now been selected. modify the name.
234 if ((pat_sel(arcn) < 0) || ((res = mod_name(arcn)) < 0))
238 * a bad name mod, skip and purge name from link table
241 (void)rd_skip(arcn->skip + arcn->pad);
246 * Non standard -Y and -Z flag. When the existing file is
247 * same age or newer skip
249 if ((Yflag || Zflag) && ((lstat(arcn->name, &sb) == 0))) {
250 if (Yflag && Zflag) {
251 if ((arcn->sb.st_mtime <= sb.st_mtime) &&
252 (arcn->sb.st_ctime <= sb.st_ctime)) {
253 (void)rd_skip(arcn->skip + arcn->pad);
257 if (arcn->sb.st_ctime <= sb.st_ctime) {
258 (void)rd_skip(arcn->skip + arcn->pad);
261 } else if (arcn->sb.st_mtime <= sb.st_mtime) {
262 (void)rd_skip(arcn->skip + arcn->pad);
269 ls_list(arcn, now, listf);
271 (void)fputs(arcn->name, listf);
277 * if required, chdir around.
279 if ((arcn->pat != NULL) && (arcn->pat->chdname != NULL))
280 if (chdir(arcn->pat->chdname) != 0)
281 syswarn(1, errno, "Cannot chdir to %s",
285 * all ok, extract this member based on type
287 if ((arcn->type != PAX_REG) && (arcn->type != PAX_CTG)) {
289 * process archive members that are not regular files.
290 * throw out padding and any data that might follow the
291 * header (as determined by the format).
293 if ((arcn->type == PAX_HLK) || (arcn->type == PAX_HRG))
294 res = lnk_creat(arcn);
296 res = node_creat(arcn);
298 (void)rd_skip(arcn->skip + arcn->pad);
302 if (vflag && vfpart) {
303 (void)putc('\n', listf);
309 * we have a file with data here. If we can not create it, skip
310 * over the data and purge the name from hard link table
312 if ((fd = file_creat(arcn)) < 0) {
313 (void)rd_skip(arcn->skip + arcn->pad);
318 * extract the file from the archive and skip over padding and
319 * any unprocessed data
321 res = (*frmt->rd_data)(arcn, fd, &cnt);
322 file_close(arcn, fd);
323 if (vflag && vfpart) {
324 (void)putc('\n', listf);
328 (void)rd_skip(cnt + arcn->pad);
331 * if required, chdir around.
333 if ((arcn->pat != NULL) && (arcn->pat->chdname != NULL))
334 if (fchdir(cwdfd) != 0)
336 "Can't fchdir to starting directory");
340 * all done, restore directory modes and times as required; make sure
341 * all patterns supplied by the user were matched; block off signals
342 * to avoid chance for multiple entry into the cleanup code.
344 (void)(*frmt->end_rd)();
345 (void)sigprocmask(SIG_BLOCK, &s_mask, NULL);
353 * Write an archive. used in both creating a new archive and appends on
354 * previously written archive.
358 wr_archive(ARCHD *arcn, int is_app)
369 * if this format supports hard link storage, start up the database
372 if (((hlk = frmt->hlk) == 1) && (lnk_start() < 0))
376 * start up the file traversal code and format specific write
378 if ((ftree_start() < 0) || ((*frmt->st_wr)() < 0))
383 * When we are doing interactive rename, we store the mapping of names
384 * so we can fix up hard links files later in the archive.
386 if (iflag && (name_start() < 0))
390 * if this not append, and there are no files, we do no write a trailer
397 * while there are files to archive, process them one at at time
399 while (next_file(arcn) == 0) {
401 * check if this file meets user specified options match.
403 if (sel_chk(arcn) != 0) {
410 * only archive if this file is newer than a file with
411 * the same name that is already stored on the archive
413 if ((res = chk_ftime(arcn)) < 0)
420 * this file is considered selected now. see if this is a hard
421 * link to a file already stored
424 if (hlk && (chk_lnk(arcn) < 0))
427 if ((arcn->type == PAX_REG) || (arcn->type == PAX_HRG) ||
428 (arcn->type == PAX_CTG)) {
430 * we will have to read this file. by opening it now we
431 * can avoid writing a header to the archive for a file
432 * we were later unable to read (we also purge it from
435 if ((fd = open(arcn->org_name, O_RDONLY, 0)) < 0) {
436 syswarn(1,errno, "Unable to open %s to read",
444 * Now modify the name as requested by the user
446 if ((res = mod_name(arcn)) < 0) {
448 * name modification says to skip this file, close the
449 * file and purge link table entry
451 rdfile_close(arcn, &fd);
456 if ((res > 0) || (docrc && (set_crc(arcn, fd) < 0))) {
458 * unable to obtain the crc we need, close the file,
459 * purge link table entry
461 rdfile_close(arcn, &fd);
468 ls_list(arcn, now, listf);
470 (void)fputs(arcn->name, listf);
477 * looks safe to store the file, have the format specific
478 * routine write routine store the file header on the archive
480 if ((res = (*wrf)(arcn)) < 0) {
481 rdfile_close(arcn, &fd);
487 * format write says no file data needs to be stored
488 * so we are done messing with this file
490 if (vflag && vfpart) {
491 (void)putc('\n', listf);
494 rdfile_close(arcn, &fd);
499 * Add file data to the archive, quit on write error. if we
500 * cannot write the entire file contents to the archive we
501 * must pad the archive to replace the missing file data
502 * (otherwise during an extract the file header for the file
503 * which FOLLOWS this one will not be where we expect it to
506 res = (*frmt->wr_data)(arcn, fd, &cnt);
507 rdfile_close(arcn, &fd);
508 if (vflag && vfpart) {
509 (void)putc('\n', listf);
516 * pad as required, cnt is number of bytes not written
518 if (((cnt > 0) && (wr_skip(cnt) < 0)) ||
519 ((arcn->pad > 0) && (wr_skip(arcn->pad) < 0)))
524 * tell format to write trailer; pad to block boundary; reset directory
525 * mode/access times, and check if all patterns supplied by the user
526 * were matched. block off signals to avoid chance for multiple entry
527 * into the cleanup code
533 (void)sigprocmask(SIG_BLOCK, &s_mask, NULL);
542 * Add file to previously written archive. Archive format specified by the
543 * user must agree with archive. The archive is read first to collect
544 * modification times (if -u) and locate the archive trailer. The archive
545 * is positioned in front of the record with the trailer and wr_archive()
546 * is called to add the new members.
547 * PAX IMPLEMENTATION DETAIL NOTE:
548 * -u is implemented by adding the new members to the end of the archive.
549 * Care is taken so that these do not end up as links to the older
550 * version of the same file already stored in the archive. It is expected
551 * when extraction occurs these newer versions will over-write the older
552 * ones stored "earlier" in the archive (this may be a bad assumption as
553 * it depends on the implementation of the program doing the extraction).
554 * It is really difficult to splice in members without either re-writing
555 * the entire archive (from the point were the old version was), or having
556 * assistance of the format specification in terms of a special update
557 * header that invalidates a previous archive record. The POSIX spec left
558 * the method used to implement -u unspecified. This pax is able to
559 * over write existing files that it creates.
576 * Do not allow an append operation if the actual archive is of a
577 * different format than the user specified format.
581 if ((orgfrmt != NULL) && (orgfrmt != frmt)) {
582 paxwarn(1, "Cannot mix current archive format %s with %s",
583 frmt->name, orgfrmt->name);
588 * pass the format any options and start up format
590 if (((*frmt->options)() < 0) || ((*frmt->st_rd)() < 0))
594 * if we only are adding members that are newer, we need to save the
595 * mod times for all files we see.
597 if (uflag && (ftime_start() < 0))
601 * some archive formats encode hard links by recording the device and
602 * file serial number (inode) but copy the file anyway (multiple times)
603 * to the archive. When we append, we run the risk that newly added
604 * files may have the same device and inode numbers as those recorded
605 * on the archive but during a previous run. If this happens, when the
606 * archive is extracted we get INCORRECT hard links. We avoid this by
607 * remapping the device numbers so that newly added files will never
608 * use the same device number as one found on the archive. remapping
609 * allows new members to safely have links among themselves. remapping
610 * also avoids problems with file inode (serial number) truncations
611 * when the inode number is larger than storage space in the archive
612 * header. See the remap routines for more details.
614 if ((udev = frmt->udev) && (dev_start() < 0))
618 * reading the archive may take a long time. If verbose tell the user
622 "%s: Reading archive to position at the end...", argv0);
627 * step through the archive until the format says it is done
629 while (next_head(arcn) == 0) {
631 * check if this file meets user specified options.
633 if (sel_chk(arcn) != 0) {
634 if (rd_skip(arcn->skip + arcn->pad) == 1)
641 * see if this is the newest version of this file has
642 * already been seen, if so skip.
644 if ((res = chk_ftime(arcn)) < 0)
647 if (rd_skip(arcn->skip + arcn->pad) == 1)
654 * Store this device number. Device numbers seen during the
655 * read phase of append will cause newly appended files with a
656 * device number seen in the old part of the archive to be
657 * remapped to an unused device number.
659 if ((udev && (add_dev(arcn) < 0)) ||
660 (rd_skip(arcn->skip + arcn->pad) == 1))
665 * done, finish up read and get the number of bytes to back up so we
666 * can add new members. The format might have used the hard link table,
669 tlen = (*frmt->end_rd)();
673 * try to position for write, if this fails quit. if any error occurs,
674 * we will refuse to write
676 if (appnd_start(tlen) < 0)
680 * tell the user we are done reading.
682 if (vflag && vfpart) {
683 (void)fputs("done.\n", listf);
688 * go to the writing phase to add the new members
695 * write a new archive
704 * if we only are adding members that are newer, we need to save the
705 * mod times for all files; set up for writing; pass the format any
706 * options write the archive
708 if ((uflag && (ftime_start() < 0)) || (wr_start() < 0))
710 if ((*frmt->options)() < 0)
713 wr_archive(&archd, 0);
718 * copy files from one part of the file system to another. this does not
719 * use any archive storage. The EFFECT OF THE COPY IS THE SAME as if an
720 * archive was written and then extracted in the destination directory
721 * (except the files are forced to be under the destination directory).
736 char dirbuf[PAXPATHLEN+1];
740 * set up the destination dir path and make sure it is a directory. We
741 * make sure we have a trailing / on the destination
743 dlen = l_strncpy(dirbuf, dirptr, sizeof(dirbuf) - 1);
744 dest_pt = dirbuf + dlen;
745 if (*(dest_pt-1) != '/') {
750 drem = PAXPATHLEN - dlen;
752 if (stat(dirptr, &sb) < 0) {
753 syswarn(1, errno, "Cannot access destination directory %s",
757 if (!S_ISDIR(sb.st_mode)) {
758 paxwarn(1, "Destination is not a directory %s", dirptr);
763 * start up the hard link table; file traversal routines and the
764 * modification time and access mode database
766 if ((lnk_start() < 0) || (ftree_start() < 0) || (dir_start() < 0))
770 * When we are doing interactive rename, we store the mapping of names
771 * so we can fix up hard links files later in the archive.
773 if (iflag && (name_start() < 0))
777 * set up to cp file trees
782 * while there are files to archive, process them
784 while (next_file(arcn) == 0) {
788 * check if this file meets user specified options
790 if (sel_chk(arcn) != 0) {
796 * if there is already a file in the destination directory with
797 * the same name and it is newer, skip the one stored on the
799 * NOTE: this test is done BEFORE name modifications as
800 * specified by pax. this can be confusing to the user who
801 * might expect the test to be done on an existing file AFTER
802 * the name mod. In honesty the pax spec is probably flawed in
805 if (uflag || Dflag) {
807 * create the destination name
809 if (*(arcn->name) == '/')
813 if ((arcn->nlen - res) > drem) {
814 paxwarn(1, "Destination pathname too long %s",
818 (void)strncpy(dest_pt, arcn->name + res, drem);
819 dirbuf[PAXPATHLEN] = '\0';
822 * if existing file is same age or newer skip
824 res = lstat(dirbuf, &sb);
828 if (uflag && Dflag) {
829 if ((arcn->sb.st_mtime<=sb.st_mtime) &&
830 (arcn->sb.st_ctime<=sb.st_ctime))
833 if (arcn->sb.st_ctime <= sb.st_ctime)
835 } else if (arcn->sb.st_mtime <= sb.st_mtime)
841 * this file is considered selected. See if this is a hard link
842 * to a previous file; modify the name as requested by the
843 * user; set the final destination.
846 if ((chk_lnk(arcn) < 0) || ((res = mod_name(arcn)) < 0))
848 if ((res > 0) || (set_dest(arcn, dirbuf, dlen) < 0)) {
850 * skip file, purge from link table
857 * Non standard -Y and -Z flag. When the existing file is
858 * same age or newer skip
860 if ((Yflag || Zflag) && ((lstat(arcn->name, &sb) == 0))) {
861 if (Yflag && Zflag) {
862 if ((arcn->sb.st_mtime <= sb.st_mtime) &&
863 (arcn->sb.st_ctime <= sb.st_ctime))
866 if (arcn->sb.st_ctime <= sb.st_ctime)
868 } else if (arcn->sb.st_mtime <= sb.st_mtime)
873 (void)fputs(arcn->name, listf);
879 * try to create a hard link to the src file if requested
880 * but make sure we are not trying to overwrite ourselves.
883 res = cross_lnk(arcn);
885 res = chk_same(arcn);
887 if (vflag && vfpart) {
888 (void)putc('\n', listf);
895 * have to create a new file
897 if ((arcn->type != PAX_REG) && (arcn->type != PAX_CTG)) {
899 * create a link or special file
901 if ((arcn->type == PAX_HLK) || (arcn->type == PAX_HRG))
902 res = lnk_creat(arcn);
904 res = node_creat(arcn);
907 if (vflag && vfpart) {
908 (void)putc('\n', listf);
915 * have to copy a regular file to the destination directory.
916 * first open source file and then create the destination file
918 if ((fdsrc = open(arcn->org_name, O_RDONLY, 0)) < 0) {
919 syswarn(1, errno, "Unable to open %s to read",
924 if ((fddest = file_creat(arcn)) < 0) {
925 rdfile_close(arcn, &fdsrc);
931 * copy source file data to the destination file
933 cp_file(arcn, fdsrc, fddest);
934 file_close(arcn, fddest);
935 rdfile_close(arcn, &fdsrc);
937 if (vflag && vfpart) {
938 (void)putc('\n', listf);
944 * restore directory modes and times as required; make sure all
945 * patterns were selected block off signals to avoid chance for
946 * multiple entry into the cleanup code.
948 (void)sigprocmask(SIG_BLOCK, &s_mask, NULL);
956 * try to find a valid header in the archive. Uses format specific
957 * routines to extract the header and id the trailer. Trailers may be
958 * located within a valid header or in an invalid header (the location
959 * is format specific. The inhead field from the option table tells us
960 * where to look for the trailer).
961 * We keep reading (and resyncing) until we get enough contiguous data
962 * to check for a header. If we cannot find one, we shift by a byte
963 * add a new byte from the archive to the end of the buffer and try again.
964 * If we get a read error, we throw out what we have (as we must have
965 * contiguous data) and start over again.
966 * ASSUMED: headers fit within a BLKMULT header.
968 * 0 if we got a header, -1 if we are unable to ever find another one
969 * (we reached the end of input, or we reached the limit on retries. see
970 * the specs for rd_wrbuf() for more details)
974 next_head(ARCHD *arcn)
981 int in_resync = 0; /* set when we are in resync mode */
982 int cnt = 0; /* counter for trailer function */
983 int first = 1; /* on 1st read, EOF isn't premature. */
986 * set up initial conditions, we want a whole frmt->hsz block as we
989 res = hsz = frmt->hsz;
994 * keep looping until we get a contiguous FULL buffer
995 * (frmt->hsz is the proper size)
998 if ((ret = rd_wrbuf(hdend, res)) == res)
1002 * If we read 0 bytes (EOF) from an archive when we
1003 * expect to find a header, we have stepped upon
1004 * an archive without the customary block of zeroes
1005 * end marker. It's just stupid to error out on
1006 * them, so exit gracefully.
1008 if (first && ret == 0)
1013 * some kind of archive read problem, try to resync the
1014 * storage device, better give the user the bad news.
1016 if ((ret == 0) || (rd_sync() < 0)) {
1017 paxwarn(1,"Premature end of file on archive read");
1023 "Archive I/O error, cannot continue");
1026 paxwarn(1,"Archive I/O error. Trying to recover.");
1031 * oh well, throw it all out and start over
1038 * ok we have a contiguous buffer of the right size. Call the
1039 * format read routine. If this was not a valid header and this
1040 * format stores trailers outside of the header, call the
1041 * format specific trailer routine to check for a trailer. We
1042 * have to watch out that we do not mis-identify file data or
1043 * block padding as a header or trailer. Format specific
1044 * trailer functions must NOT check for the trailer while we
1045 * are running in resync mode. Some trailer functions may tell
1046 * us that this block cannot contain a valid header either, so
1047 * we then throw out the entire block and start over.
1049 if ((*frmt->rd)(arcn, hdbuf) == 0)
1052 if (!frmt->inhead) {
1054 * this format has trailers outside of valid headers
1056 if ((ret = (*frmt->trail_tar)(hdbuf,in_resync,&cnt)) == 0){
1058 * valid trailer found, drain input as required
1066 * we are in resync and we were told to throw
1067 * the whole block out because none of the
1068 * bytes in this block can be used to form a
1078 * Brute force section.
1079 * not a valid header. We may be able to find a header yet. So
1080 * we shift over by one byte, and set up to read one byte at a
1081 * time from the archive and place it at the end of the buffer.
1082 * We will keep moving byte at a time until we find a header or
1083 * get a read error and have to start over.
1087 paxwarn(1,"Unable to append, archive header flaw");
1090 paxwarn(1,"Invalid header, starting valid header search.");
1093 memmove(hdbuf, hdbuf+1, shftsz);
1095 hdend = hdbuf + shftsz;
1099 * ok got a valid header, check for trailer if format encodes it in
1102 if (frmt->inhead && ((*frmt->trail_cpio)(arcn) == 0)) {
1104 * valid trailer found, drain input as required
1116 * Figure out what format an archive is. Handles archive with flaws by
1117 * brute force searches for a legal header in any supported format. The
1118 * format id routines have to be careful to NOT mis-identify a format.
1119 * ASSUMED: headers fit within a BLKMULT header.
1121 * 0 if archive found -1 otherwise
1130 int minhd = BLKMULT;
1135 * find the smallest header size in all archive formats and then set up
1136 * to read the archive.
1138 for (i = 0; ford[i] >= 0; ++i) {
1139 if (fsub[ford[i]].hsz < minhd)
1140 minhd = fsub[ford[i]].hsz;
1150 * fill the buffer with at least the smallest header
1152 i = rd_wrbuf(hdend, res);
1159 * if we cannot recover from a read error quit
1161 if ((i == 0) || (rd_sync() < 0))
1165 * when we get an error none of the data we already
1166 * have can be used to create a legal header (we just
1167 * got an error in the middle), so we throw it all out
1168 * and refill the buffer with fresh data.
1176 paxwarn(1,"Cannot identify format. Searching...");
1182 * we have at least the size of the smallest header in any
1183 * archive format. Look to see if we have a match. The array
1184 * ford[] is used to specify the header id order to reduce the
1185 * chance of incorrectly id'ing a valid header (some formats
1186 * may be subsets of each other and the order would then be
1189 for (i = 0; ford[i] >= 0; ++i) {
1190 if ((*fsub[ford[i]].id)(hdbuf, hdsz) < 0)
1192 frmt = &(fsub[ford[i]]);
1194 * yuck, to avoid slow special case code in the extract
1195 * routines, just push this header back as if it was
1196 * not seen. We have left extra space at start of the
1197 * buffer for this purpose. This is a bit ugly, but
1198 * adding all the special case code is far worse.
1205 * We have a flawed archive, no match. we start searching, but
1206 * we never allow additions to flawed archives
1211 paxwarn(1, "Cannot identify format. Searching...");
1216 * brute force search for a header that we can id.
1217 * we shift through byte at a time. this is slow, but we cannot
1218 * determine the nature of the flaw in the archive in a
1222 memmove(hdbuf, hdbuf+1, hdsz);
1223 res = BLKMULT - hdsz;
1224 hdend = hdbuf + hdsz;
1234 * we cannot find a header, bow, apologize and quit
1236 paxwarn(1, "Sorry, unable to determine archive format.");