2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1992 Keith Muller.
5 * Copyright (c) 1992, 1993
6 * The Regents of the University of California. All rights reserved.
8 * This code is derived from software contributed to Berkeley by
9 * Keith Muller of the University of California, San Diego.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 static char sccsid[] = "@(#)ar_subs.c 8.2 (Berkeley) 4/18/94";
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
44 #include <sys/types.h>
56 static void wr_archive(ARCHD *, int is_app);
57 static int get_arc(void);
58 static int next_head(ARCHD *);
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 (no
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 is not append, and there are no files, we do not write a
398 * while there are files to archive, process them one at at time
400 while (next_file(arcn) == 0) {
402 * check if this file meets user specified options match.
404 if (sel_chk(arcn) != 0) {
411 * only archive if this file is newer than a file with
412 * the same name that is already stored on the archive
414 if ((res = chk_ftime(arcn)) < 0)
421 * this file is considered selected now. see if this is a hard
422 * link to a file already stored
425 if (hlk && (chk_lnk(arcn) < 0))
428 if ((arcn->type == PAX_REG) || (arcn->type == PAX_HRG) ||
429 (arcn->type == PAX_CTG)) {
431 * we will have to read this file. by opening it now we
432 * can avoid writing a header to the archive for a file
433 * we were later unable to read (we also purge it from
436 if ((fd = open(arcn->org_name, O_RDONLY, 0)) < 0) {
437 syswarn(1,errno, "Unable to open %s to read",
445 * Now modify the name as requested by the user
447 if ((res = mod_name(arcn)) < 0) {
449 * name modification says to skip this file, close the
450 * file and purge link table entry
452 rdfile_close(arcn, &fd);
457 if ((res > 0) || (docrc && (set_crc(arcn, fd) < 0))) {
459 * unable to obtain the crc we need, close the file,
460 * purge link table entry
462 rdfile_close(arcn, &fd);
469 ls_list(arcn, now, listf);
471 (void)fputs(arcn->name, listf);
478 * looks safe to store the file, have the format specific
479 * routine write routine store the file header on the archive
481 if ((res = (*wrf)(arcn)) < 0) {
482 rdfile_close(arcn, &fd);
488 * format write says no file data needs to be stored
489 * so we are done messing with this file
491 if (vflag && vfpart) {
492 (void)putc('\n', listf);
495 rdfile_close(arcn, &fd);
500 * Add file data to the archive, quit on write error. if we
501 * cannot write the entire file contents to the archive we
502 * must pad the archive to replace the missing file data
503 * (otherwise during an extract the file header for the file
504 * which FOLLOWS this one will not be where we expect it to
507 res = (*frmt->wr_data)(arcn, fd, &cnt);
508 rdfile_close(arcn, &fd);
509 if (vflag && vfpart) {
510 (void)putc('\n', listf);
517 * pad as required, cnt is number of bytes not written
519 if (((cnt > 0) && (wr_skip(cnt) < 0)) ||
520 ((arcn->pad > 0) && (wr_skip(arcn->pad) < 0)))
525 * tell format to write trailer; pad to block boundary; reset directory
526 * mode/access times, and check if all patterns supplied by the user
527 * were matched. block off signals to avoid chance for multiple entry
528 * into the cleanup code
534 (void)sigprocmask(SIG_BLOCK, &s_mask, NULL);
543 * Add file to previously written archive. Archive format specified by the
544 * user must agree with archive. The archive is read first to collect
545 * modification times (if -u) and locate the archive trailer. The archive
546 * is positioned in front of the record with the trailer and wr_archive()
547 * is called to add the new members.
548 * PAX IMPLEMENTATION DETAIL NOTE:
549 * -u is implemented by adding the new members to the end of the archive.
550 * Care is taken so that these do not end up as links to the older
551 * version of the same file already stored in the archive. It is expected
552 * when extraction occurs these newer versions will over-write the older
553 * ones stored "earlier" in the archive (this may be a bad assumption as
554 * it depends on the implementation of the program doing the extraction).
555 * It is really difficult to splice in members without either re-writing
556 * the entire archive (from the point were the old version was), or having
557 * assistance of the format specification in terms of a special update
558 * header that invalidates a previous archive record. The POSIX spec left
559 * the method used to implement -u unspecified. This pax is able to
560 * over write existing files that it creates.
577 * Do not allow an append operation if the actual archive is of a
578 * different format than the user specified format.
582 if ((orgfrmt != NULL) && (orgfrmt != frmt)) {
583 paxwarn(1, "Cannot mix current archive format %s with %s",
584 frmt->name, orgfrmt->name);
589 * pass the format any options and start up format
591 if (((*frmt->options)() < 0) || ((*frmt->st_rd)() < 0))
595 * if we only are adding members that are newer, we need to save the
596 * mod times for all files we see.
598 if (uflag && (ftime_start() < 0))
602 * some archive formats encode hard links by recording the device and
603 * file serial number (inode) but copy the file anyway (multiple times)
604 * to the archive. When we append, we run the risk that newly added
605 * files may have the same device and inode numbers as those recorded
606 * on the archive but during a previous run. If this happens, when the
607 * archive is extracted we get INCORRECT hard links. We avoid this by
608 * remapping the device numbers so that newly added files will never
609 * use the same device number as one found on the archive. remapping
610 * allows new members to safely have links among themselves. remapping
611 * also avoids problems with file inode (serial number) truncations
612 * when the inode number is larger than storage space in the archive
613 * header. See the remap routines for more details.
615 if ((udev = frmt->udev) && (dev_start() < 0))
619 * reading the archive may take a long time. If verbose tell the user
623 "%s: Reading archive to position at the end...", argv0);
628 * step through the archive until the format says it is done
630 while (next_head(arcn) == 0) {
632 * check if this file meets user specified options.
634 if (sel_chk(arcn) != 0) {
635 if (rd_skip(arcn->skip + arcn->pad) == 1)
642 * see if this is the newest version of this file has
643 * already been seen, if so skip.
645 if ((res = chk_ftime(arcn)) < 0)
648 if (rd_skip(arcn->skip + arcn->pad) == 1)
655 * Store this device number. Device numbers seen during the
656 * read phase of append will cause newly appended files with a
657 * device number seen in the old part of the archive to be
658 * remapped to an unused device number.
660 if ((udev && (add_dev(arcn) < 0)) ||
661 (rd_skip(arcn->skip + arcn->pad) == 1))
666 * done, finish up read and get the number of bytes to back up so we
667 * can add new members. The format might have used the hard link table,
670 tlen = (*frmt->end_rd)();
674 * try to position for write, if this fails quit. if any error occurs,
675 * we will refuse to write
677 if (appnd_start(tlen) < 0)
681 * tell the user we are done reading.
683 if (vflag && vfpart) {
684 (void)fputs("done.\n", listf);
689 * go to the writing phase to add the new members
696 * write a new archive
705 * if we only are adding members that are newer, we need to save the
706 * mod times for all files; set up for writing; pass the format any
707 * options write the archive
709 if ((uflag && (ftime_start() < 0)) || (wr_start() < 0))
711 if ((*frmt->options)() < 0)
714 wr_archive(&archd, 0);
719 * copy files from one part of the file system to another. this does not
720 * use any archive storage. The EFFECT OF THE COPY IS THE SAME as if an
721 * archive was written and then extracted in the destination directory
722 * (except the files are forced to be under the destination directory).
737 char dirbuf[PAXPATHLEN+1];
741 * set up the destination dir path and make sure it is a directory. We
742 * make sure we have a trailing / on the destination
744 dlen = l_strncpy(dirbuf, dirptr, sizeof(dirbuf) - 1);
745 dest_pt = dirbuf + dlen;
746 if (*(dest_pt-1) != '/') {
751 drem = PAXPATHLEN - dlen;
753 if (stat(dirptr, &sb) < 0) {
754 syswarn(1, errno, "Cannot access destination directory %s",
758 if (!S_ISDIR(sb.st_mode)) {
759 paxwarn(1, "Destination is not a directory %s", dirptr);
764 * start up the hard link table; file traversal routines and the
765 * modification time and access mode database
767 if ((lnk_start() < 0) || (ftree_start() < 0) || (dir_start() < 0))
771 * When we are doing interactive rename, we store the mapping of names
772 * so we can fix up hard links files later in the archive.
774 if (iflag && (name_start() < 0))
778 * set up to cp file trees
783 * while there are files to archive, process them
785 while (next_file(arcn) == 0) {
789 * check if this file meets user specified options
791 if (sel_chk(arcn) != 0) {
797 * if there is already a file in the destination directory with
798 * the same name and it is newer, skip the one stored on the
800 * NOTE: this test is done BEFORE name modifications as
801 * specified by pax. this can be confusing to the user who
802 * might expect the test to be done on an existing file AFTER
803 * the name mod. In honesty the pax spec is probably flawed in
806 if (uflag || Dflag) {
808 * create the destination name
810 if (*(arcn->name) == '/')
814 if ((arcn->nlen - res) > drem) {
815 paxwarn(1, "Destination pathname too long %s",
819 (void)strncpy(dest_pt, arcn->name + res, drem);
820 dirbuf[PAXPATHLEN] = '\0';
823 * if existing file is same age or newer skip
825 res = lstat(dirbuf, &sb);
829 if (uflag && Dflag) {
830 if ((arcn->sb.st_mtime<=sb.st_mtime) &&
831 (arcn->sb.st_ctime<=sb.st_ctime))
834 if (arcn->sb.st_ctime <= sb.st_ctime)
836 } else if (arcn->sb.st_mtime <= sb.st_mtime)
842 * this file is considered selected. See if this is a hard link
843 * to a previous file; modify the name as requested by the
844 * user; set the final destination.
847 if ((chk_lnk(arcn) < 0) || ((res = mod_name(arcn)) < 0))
849 if ((res > 0) || (set_dest(arcn, dirbuf, dlen) < 0)) {
851 * skip file, purge from link table
858 * Non standard -Y and -Z flag. When the existing file is
859 * same age or newer skip
861 if ((Yflag || Zflag) && ((lstat(arcn->name, &sb) == 0))) {
862 if (Yflag && Zflag) {
863 if ((arcn->sb.st_mtime <= sb.st_mtime) &&
864 (arcn->sb.st_ctime <= sb.st_ctime))
867 if (arcn->sb.st_ctime <= sb.st_ctime)
869 } else if (arcn->sb.st_mtime <= sb.st_mtime)
874 (void)fputs(arcn->name, listf);
880 * try to create a hard link to the src file if requested
881 * but make sure we are not trying to overwrite ourselves.
884 res = cross_lnk(arcn);
886 res = chk_same(arcn);
888 if (vflag && vfpart) {
889 (void)putc('\n', listf);
896 * have to create a new file
898 if ((arcn->type != PAX_REG) && (arcn->type != PAX_CTG)) {
900 * create a link or special file
902 if ((arcn->type == PAX_HLK) || (arcn->type == PAX_HRG))
903 res = lnk_creat(arcn);
905 res = node_creat(arcn);
908 if (vflag && vfpart) {
909 (void)putc('\n', listf);
916 * have to copy a regular file to the destination directory.
917 * first open source file and then create the destination file
919 if ((fdsrc = open(arcn->org_name, O_RDONLY, 0)) < 0) {
920 syswarn(1, errno, "Unable to open %s to read",
925 if ((fddest = file_creat(arcn)) < 0) {
926 rdfile_close(arcn, &fdsrc);
932 * copy source file data to the destination file
934 cp_file(arcn, fdsrc, fddest);
935 file_close(arcn, fddest);
936 rdfile_close(arcn, &fdsrc);
938 if (vflag && vfpart) {
939 (void)putc('\n', listf);
945 * restore directory modes and times as required; make sure all
946 * patterns were selected block off signals to avoid chance for
947 * multiple entry into the cleanup code.
949 (void)sigprocmask(SIG_BLOCK, &s_mask, NULL);
957 * try to find a valid header in the archive. Uses format specific
958 * routines to extract the header and id the trailer. Trailers may be
959 * located within a valid header or in an invalid header (the location
960 * is format specific. The inhead field from the option table tells us
961 * where to look for the trailer).
962 * We keep reading (and resyncing) until we get enough contiguous data
963 * to check for a header. If we cannot find one, we shift by a byte
964 * add a new byte from the archive to the end of the buffer and try again.
965 * If we get a read error, we throw out what we have (as we must have
966 * contiguous data) and start over again.
967 * ASSUMED: headers fit within a BLKMULT header.
969 * 0 if we got a header, -1 if we are unable to ever find another one
970 * (we reached the end of input, or we reached the limit on retries. see
971 * the specs for rd_wrbuf() for more details)
975 next_head(ARCHD *arcn)
982 int in_resync = 0; /* set when we are in resync mode */
983 int cnt = 0; /* counter for trailer function */
984 int first = 1; /* on 1st read, EOF isn't premature. */
987 * set up initial conditions, we want a whole frmt->hsz block as we
990 res = hsz = frmt->hsz;
995 * keep looping until we get a contiguous FULL buffer
996 * (frmt->hsz is the proper size)
999 if ((ret = rd_wrbuf(hdend, res)) == res)
1003 * If we read 0 bytes (EOF) from an archive when we
1004 * expect to find a header, we have stepped upon
1005 * an archive without the customary block of zeroes
1006 * end marker. It's just stupid to error out on
1007 * them, so exit gracefully.
1009 if (first && ret == 0)
1014 * some kind of archive read problem, try to resync the
1015 * storage device, better give the user the bad news.
1017 if ((ret == 0) || (rd_sync() < 0)) {
1018 paxwarn(1,"Premature end of file on archive read");
1024 "Archive I/O error, cannot continue");
1027 paxwarn(1,"Archive I/O error. Trying to recover.");
1032 * oh well, throw it all out and start over
1039 * ok we have a contiguous buffer of the right size. Call the
1040 * format read routine. If this was not a valid header and this
1041 * format stores trailers outside of the header, call the
1042 * format specific trailer routine to check for a trailer. We
1043 * have to watch out that we do not mis-identify file data or
1044 * block padding as a header or trailer. Format specific
1045 * trailer functions must NOT check for the trailer while we
1046 * are running in resync mode. Some trailer functions may tell
1047 * us that this block cannot contain a valid header either, so
1048 * we then throw out the entire block and start over.
1050 if ((*frmt->rd)(arcn, hdbuf) == 0)
1053 if (!frmt->inhead) {
1055 * this format has trailers outside of valid headers
1057 if ((ret = (*frmt->trail_tar)(hdbuf,in_resync,&cnt)) == 0){
1059 * valid trailer found, drain input as required
1067 * we are in resync and we were told to throw
1068 * the whole block out because none of the
1069 * bytes in this block can be used to form a
1079 * Brute force section.
1080 * not a valid header. We may be able to find a header yet. So
1081 * we shift over by one byte, and set up to read one byte at a
1082 * time from the archive and place it at the end of the buffer.
1083 * We will keep moving byte at a time until we find a header or
1084 * get a read error and have to start over.
1088 paxwarn(1,"Unable to append, archive header flaw");
1091 paxwarn(1,"Invalid header, starting valid header search.");
1094 memmove(hdbuf, hdbuf+1, shftsz);
1096 hdend = hdbuf + shftsz;
1100 * ok got a valid header, check for trailer if format encodes it in
1103 if (frmt->inhead && ((*frmt->trail_cpio)(arcn) == 0)) {
1105 * valid trailer found, drain input as required
1117 * Figure out what format an archive is. Handles archive with flaws by
1118 * brute force searches for a legal header in any supported format. The
1119 * format id routines have to be careful to NOT mis-identify a format.
1120 * ASSUMED: headers fit within a BLKMULT header.
1122 * 0 if archive found -1 otherwise
1131 int minhd = BLKMULT;
1136 * find the smallest header size in all archive formats and then set up
1137 * to read the archive.
1139 for (i = 0; ford[i] >= 0; ++i) {
1140 if (fsub[ford[i]].hsz < minhd)
1141 minhd = fsub[ford[i]].hsz;
1151 * fill the buffer with at least the smallest header
1153 i = rd_wrbuf(hdend, res);
1160 * if we cannot recover from a read error quit
1162 if ((i == 0) || (rd_sync() < 0))
1166 * when we get an error none of the data we already
1167 * have can be used to create a legal header (we just
1168 * got an error in the middle), so we throw it all out
1169 * and refill the buffer with fresh data.
1177 paxwarn(1,"Cannot identify format. Searching...");
1183 * we have at least the size of the smallest header in any
1184 * archive format. Look to see if we have a match. The array
1185 * ford[] is used to specify the header id order to reduce the
1186 * chance of incorrectly id'ing a valid header (some formats
1187 * may be subsets of each other and the order would then be
1190 for (i = 0; ford[i] >= 0; ++i) {
1191 if ((*fsub[ford[i]].id)(hdbuf, hdsz) < 0)
1193 frmt = &(fsub[ford[i]]);
1195 * yuck, to avoid slow special case code in the extract
1196 * routines, just push this header back as if it was
1197 * not seen. We have left extra space at start of the
1198 * buffer for this purpose. This is a bit ugly, but
1199 * adding all the special case code is far worse.
1206 * We have a flawed archive, no match. we start searching, but
1207 * we never allow additions to flawed archives
1212 paxwarn(1, "Cannot identify format. Searching...");
1217 * brute force search for a header that we can id.
1218 * we shift through byte at a time. this is slow, but we cannot
1219 * determine the nature of the flaw in the archive in a
1223 memmove(hdbuf, hdbuf+1, hdsz);
1224 res = BLKMULT - hdsz;
1225 hdend = hdbuf + hdsz;
1235 * we cannot find a header, bow, apologize and quit
1237 paxwarn(1, "Sorry, unable to determine archive format.");