ar_subs.c revision 76351
11541Srgrimes/*-
21541Srgrimes * Copyright (c) 1992 Keith Muller.
31541Srgrimes * Copyright (c) 1992, 1993
41541Srgrimes *	The Regents of the University of California.  All rights reserved.
51541Srgrimes *
61541Srgrimes * This code is derived from software contributed to Berkeley by
71541Srgrimes * Keith Muller of the University of California, San Diego.
81541Srgrimes *
91541Srgrimes * Redistribution and use in source and binary forms, with or without
101541Srgrimes * modification, are permitted provided that the following conditions
111541Srgrimes * are met:
121541Srgrimes * 1. Redistributions of source code must retain the above copyright
131541Srgrimes *    notice, this list of conditions and the following disclaimer.
141541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
151541Srgrimes *    notice, this list of conditions and the following disclaimer in the
161541Srgrimes *    documentation and/or other materials provided with the distribution.
171541Srgrimes * 3. All advertising materials mentioning features or use of this software
181541Srgrimes *    must display the following acknowledgement:
191541Srgrimes *	This product includes software developed by the University of
201541Srgrimes *	California, Berkeley and its contributors.
211541Srgrimes * 4. Neither the name of the University nor the names of its contributors
221541Srgrimes *    may be used to endorse or promote products derived from this software
231541Srgrimes *    without specific prior written permission.
241541Srgrimes *
251541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
261541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
271541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
281541Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
291541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
301541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
311541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
321541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
331541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
341541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
351541Srgrimes * SUCH DAMAGE.
361541Srgrimes */
371541Srgrimes
381541Srgrimes#ifndef lint
398876Srgrimes#if 0
401541Srgrimesstatic char sccsid[] = "@(#)ar_subs.c	8.2 (Berkeley) 4/18/94";
411541Srgrimes#endif
421541Srgrimesstatic const char rcsid[] =
432806Sbde  "$FreeBSD: head/bin/pax/ar_subs.c 76351 2001-05-08 06:19:06Z kris $";
441541Srgrimes#endif /* not lint */
451541Srgrimes
461541Srgrimes#include <sys/types.h>
471541Srgrimes#include <sys/time.h>
481541Srgrimes#include <sys/stat.h>
491541Srgrimes#include <signal.h>
501541Srgrimes#include <string.h>
511541Srgrimes#include <stdio.h>
521541Srgrimes#include <fcntl.h>
531541Srgrimes#include <errno.h>
541541Srgrimes#include <unistd.h>
551541Srgrimes#include <stdlib.h>
561541Srgrimes#include "pax.h"
571541Srgrimes#include "extern.h"
581541Srgrimes
591541Srgrimesstatic void wr_archive __P((register ARCHD *, int is_app));
601541Srgrimesstatic int get_arc __P((void));
611541Srgrimesstatic int next_head __P((register ARCHD *));
621541Srgrimesextern sigset_t s_mask;
631541Srgrimes
641541Srgrimes/*
651541Srgrimes * Routines which control the overall operation modes of pax as specified by
661541Srgrimes * the user: list, append, read ...
671541Srgrimes */
681541Srgrimes
691541Srgrimesstatic char hdbuf[BLKMULT];		/* space for archive header on read */
701541Srgrimesu_long flcnt;				/* number of files processed */
711541Srgrimes
721541Srgrimes/*
731541Srgrimes * list()
741541Srgrimes *	list the contents of an archive which match user supplied pattern(s)
751541Srgrimes *	(no pattern matches all).
761541Srgrimes */
771541Srgrimes
781541Srgrimes#ifdef __STDC__
791541Srgrimesvoid
801541Srgrimeslist(void)
811541Srgrimes#else
821541Srgrimesvoid
835651Sjoerglist()
841541Srgrimes#endif
851541Srgrimes{
861541Srgrimes	register ARCHD *arcn;
871541Srgrimes	register int res;
881541Srgrimes	ARCHD archd;
891541Srgrimes	time_t now;
901541Srgrimes
915651Sjoerg	arcn = &archd;
921541Srgrimes	/*
931541Srgrimes	 * figure out archive type; pass any format specific options to the
941541Srgrimes	 * archive option processing routine; call the format init routine. We
951541Srgrimes	 * also save current time for ls_list() so we do not make a system
961541Srgrimes	 * call for each file we need to print. If verbose (vflag) start up
971541Srgrimes	 * the name and group caches.
988876Srgrimes	 */
991541Srgrimes	if ((get_arc() < 0) || ((*frmt->options)() < 0) ||
1001541Srgrimes	    ((*frmt->st_rd)() < 0))
1011541Srgrimes		return;
1021541Srgrimes
1031541Srgrimes	if (vflag && ((uidtb_start() < 0) || (gidtb_start() < 0)))
1048876Srgrimes		return;
1051541Srgrimes
1061541Srgrimes	now = time(NULL);
1071541Srgrimes
1081541Srgrimes	/*
1091541Srgrimes	 * step through the archive until the format says it is done
1101541Srgrimes	 */
1111541Srgrimes	while (next_head(arcn) == 0) {
1128876Srgrimes		/*
1131541Srgrimes		 * check for pattern, and user specified options match.
1141541Srgrimes		 * When all patterns are matched we are done.
1151541Srgrimes		 */
1161541Srgrimes		if ((res = pat_match(arcn)) < 0)
1171541Srgrimes			break;
1181541Srgrimes
1191541Srgrimes		if ((res == 0) && (sel_chk(arcn) == 0)) {
1208876Srgrimes			/*
1211541Srgrimes			 * pattern resulted in a selected file
1221541Srgrimes			 */
1238876Srgrimes			if (pat_sel(arcn) < 0)
1241541Srgrimes				break;
1258876Srgrimes
1261541Srgrimes			/*
1271541Srgrimes			 * modify the name as requested by the user if name
1281541Srgrimes			 * survives modification, do a listing of the file
1291541Srgrimes			 */
1308876Srgrimes			if ((res = mod_name(arcn)) < 0)
1311541Srgrimes				break;
1321541Srgrimes			if (res == 0)
1331541Srgrimes				ls_list(arcn, now, stdout);
1341541Srgrimes		}
1358876Srgrimes
1361541Srgrimes		/*
1371541Srgrimes		 * skip to next archive format header using values calculated
1381541Srgrimes		 * by the format header read routine
1391541Srgrimes		 */
1401541Srgrimes		if (rd_skip(arcn->skip + arcn->pad) == 1)
1411541Srgrimes			break;
1428876Srgrimes	}
1431541Srgrimes
1441541Srgrimes	/*
1451541Srgrimes	 * all done, let format have a chance to cleanup, and make sure that
1461541Srgrimes	 * the patterns supplied by the user were all matched
1471541Srgrimes	 */
1481541Srgrimes	(void)(*frmt->end_rd)();
1491541Srgrimes	(void)sigprocmask(SIG_BLOCK, &s_mask, NULL);
1501541Srgrimes	ar_close();
1518876Srgrimes	pat_chk();
1521541Srgrimes}
1531541Srgrimes
1541541Srgrimes/*
1551541Srgrimes * extract()
1561541Srgrimes *	extract the member(s) of an archive as specified by user supplied
1578876Srgrimes *	pattern(s) (no patterns extracts all members)
1581541Srgrimes */
1591541Srgrimes
1601541Srgrimes#ifdef __STDC__
1611541Srgrimesvoid
1621541Srgrimesextract(void)
1631541Srgrimes#else
1641541Srgrimesvoid
1651541Srgrimesextract()
1661541Srgrimes#endif
1671541Srgrimes{
1681541Srgrimes	register ARCHD *arcn;
1691541Srgrimes	register int res;
1701541Srgrimes	off_t cnt;
1718876Srgrimes	ARCHD archd;
1721541Srgrimes	struct stat sb;
1731541Srgrimes	int fd;
1741541Srgrimes	time_t now;
1751541Srgrimes
1761541Srgrimes	arcn = &archd;
1771541Srgrimes	/*
1781541Srgrimes	 * figure out archive type; pass any format specific options to the
1791541Srgrimes	 * archive option processing routine; call the format init routine;
1808876Srgrimes	 * start up the directory modification time and access mode database
1811541Srgrimes	 */
1821541Srgrimes	if ((get_arc() < 0) || ((*frmt->options)() < 0) ||
1831541Srgrimes	    ((*frmt->st_rd)() < 0) || (dir_start() < 0))
1848876Srgrimes		return;
1851541Srgrimes
1861541Srgrimes	/*
1871541Srgrimes	 * When we are doing interactive rename, we store the mapping of names
1881541Srgrimes	 * so we can fix up hard links files later in the archive.
1898876Srgrimes	 */
1901541Srgrimes	if (iflag && (name_start() < 0))
1911541Srgrimes		return;
1921541Srgrimes
1931541Srgrimes	now = time(NULL);
1941541Srgrimes
1951541Srgrimes	/*
1961541Srgrimes	 * step through each entry on the archive until the format read routine
1971541Srgrimes	 * says it is done
1981541Srgrimes	 */
1991541Srgrimes	while (next_head(arcn) == 0) {
2001541Srgrimes
2011541Srgrimes		/*
2021541Srgrimes		 * check for pattern, and user specified options match. When
2031541Srgrimes		 * all the patterns are matched we are done
2041541Srgrimes		 */
2051541Srgrimes		if ((res = pat_match(arcn)) < 0)
2061541Srgrimes			break;
2071541Srgrimes
2088876Srgrimes		if ((res > 0) || (sel_chk(arcn) != 0)) {
2091541Srgrimes			/*
2101541Srgrimes			 * file is not selected. skip past any file data and
2111541Srgrimes			 * padding and go back for the next archive member
2128876Srgrimes			 */
2131541Srgrimes			(void)rd_skip(arcn->skip + arcn->pad);
2141541Srgrimes			continue;
2151541Srgrimes		}
2161541Srgrimes
2171541Srgrimes		/*
2188876Srgrimes		 * with -u or -D only extract when the archive member is newer
2191541Srgrimes		 * than the file with the same name in the file system (nos
2201541Srgrimes		 * test of being the same type is required).
2211541Srgrimes		 * NOTE: this test is done BEFORE name modifications as
2221541Srgrimes		 * specified by pax. this operation can be confusing to the
2238876Srgrimes		 * user who might expect the test to be done on an existing
2241541Srgrimes		 * file AFTER the name mod. In honesty the pax spec is probably
2251541Srgrimes		 * flawed in this respect.
2261541Srgrimes		 */
2271541Srgrimes		if ((uflag || Dflag) && ((lstat(arcn->name, &sb) == 0))) {
2281541Srgrimes			if (uflag && Dflag) {
2298876Srgrimes				if ((arcn->sb.st_mtime <= sb.st_mtime) &&
2301541Srgrimes				    (arcn->sb.st_ctime <= sb.st_ctime)) {
2311541Srgrimes					(void)rd_skip(arcn->skip + arcn->pad);
2321541Srgrimes					continue;
2331541Srgrimes				}
2341541Srgrimes			} else if (Dflag) {
2351541Srgrimes				if (arcn->sb.st_ctime <= sb.st_ctime) {
2361541Srgrimes					(void)rd_skip(arcn->skip + arcn->pad);
2371541Srgrimes					continue;
2388876Srgrimes				}
2391541Srgrimes			} else if (arcn->sb.st_mtime <= sb.st_mtime) {
2401541Srgrimes				(void)rd_skip(arcn->skip + arcn->pad);
2411541Srgrimes				continue;
2421541Srgrimes			}
2431541Srgrimes		}
2448876Srgrimes
2451541Srgrimes		/*
2461541Srgrimes		 * this archive member is now been selected. modify the name.
2471541Srgrimes		 */
2481541Srgrimes		if ((pat_sel(arcn) < 0) || ((res = mod_name(arcn)) < 0))
2491541Srgrimes			break;
2501541Srgrimes		if (res > 0) {
2511541Srgrimes			/*
2528876Srgrimes			 * a bad name mod, skip and purge name from link table
2531541Srgrimes			 */
2541541Srgrimes			purg_lnk(arcn);
2558876Srgrimes			(void)rd_skip(arcn->skip + arcn->pad);
2561541Srgrimes			continue;
2571541Srgrimes		}
2581541Srgrimes
2591541Srgrimes		/*
2601541Srgrimes		 * Non standard -Y and -Z flag. When the existing file is
2611541Srgrimes		 * same age or newer skip
2621541Srgrimes		 */
2631541Srgrimes		if ((Yflag || Zflag) && ((lstat(arcn->name, &sb) == 0))) {
2641541Srgrimes			if (Yflag && Zflag) {
2651541Srgrimes				if ((arcn->sb.st_mtime <= sb.st_mtime) &&
2661541Srgrimes				    (arcn->sb.st_ctime <= sb.st_ctime)) {
2671541Srgrimes					(void)rd_skip(arcn->skip + arcn->pad);
2681541Srgrimes					continue;
2691541Srgrimes				}
2701541Srgrimes			} else if (Yflag) {
2711541Srgrimes				if (arcn->sb.st_ctime <= sb.st_ctime) {
2721541Srgrimes					(void)rd_skip(arcn->skip + arcn->pad);
2731541Srgrimes					continue;
2741541Srgrimes				}
2751541Srgrimes			} else if (arcn->sb.st_mtime <= sb.st_mtime) {
2761541Srgrimes				(void)rd_skip(arcn->skip + arcn->pad);
2771541Srgrimes				continue;
2781541Srgrimes			}
2791541Srgrimes		}
2801541Srgrimes
2811541Srgrimes		if (vflag) {
2821541Srgrimes			if (vflag > 1)
2831541Srgrimes				ls_list(arcn, now, listf);
2841541Srgrimes			else {
2851541Srgrimes				(void)fputs(arcn->name, listf);
2861541Srgrimes				vfpart = 1;
2871541Srgrimes			}
2881541Srgrimes		}
2895651Sjoerg
2901541Srgrimes		/*
2911541Srgrimes		 * if required, chdir around.
2921541Srgrimes		 */
2931541Srgrimes		if ((arcn->pat != NULL) && (arcn->pat->chdname != NULL))
2941541Srgrimes			if (chdir(arcn->pat->chdname) != 0)
2951541Srgrimes				syswarn(1, errno, "Cannot chdir to %s",
2961541Srgrimes				    arcn->pat->chdname);
2971541Srgrimes
2981541Srgrimes		/*
2991541Srgrimes		 * all ok, extract this member based on type
3001541Srgrimes		 */
3011541Srgrimes		if ((arcn->type != PAX_REG) && (arcn->type != PAX_CTG)) {
3025651Sjoerg			/*
3031541Srgrimes			 * process archive members that are not regular files.
3041541Srgrimes			 * throw out padding and any data that might follow the
3051541Srgrimes			 * header (as determined by the format).
3061541Srgrimes			 */
3071541Srgrimes			if ((arcn->type == PAX_HLK) || (arcn->type == PAX_HRG))
3081541Srgrimes				res = lnk_creat(arcn);
3091541Srgrimes			else
3101541Srgrimes				res = node_creat(arcn);
3111541Srgrimes
3121541Srgrimes			(void)rd_skip(arcn->skip + arcn->pad);
3131541Srgrimes			if (res < 0)
3141541Srgrimes				purg_lnk(arcn);
3151541Srgrimes
3161541Srgrimes			if (vflag && vfpart) {
3178876Srgrimes				(void)putc('\n', listf);
3181541Srgrimes				vfpart = 0;
3198876Srgrimes			}
3201541Srgrimes			continue;
3211541Srgrimes		}
3221541Srgrimes		/*
3231541Srgrimes		 * we have a file with data here. If we can not create it, skip
3248876Srgrimes		 * over the data and purge the name from hard link table
3251541Srgrimes		 */
3265651Sjoerg		if ((fd = file_creat(arcn)) < 0) {
3275651Sjoerg			(void)rd_skip(arcn->skip + arcn->pad);
3281541Srgrimes			purg_lnk(arcn);
3291541Srgrimes			continue;
3302806Sbde		}
3318876Srgrimes		/*
3321541Srgrimes		 * extract the file from the archive and skip over padding and
3335651Sjoerg		 * any unprocessed data
3345651Sjoerg		 */
3351541Srgrimes		res = (*frmt->rd_data)(arcn, fd, &cnt);
3361541Srgrimes		file_close(arcn, fd);
3371541Srgrimes		if (vflag && vfpart) {
3388876Srgrimes			(void)putc('\n', listf);
3391541Srgrimes			vfpart = 0;
3405651Sjoerg		}
3415651Sjoerg		if (!res)
3421541Srgrimes			(void)rd_skip(cnt + arcn->pad);
3431541Srgrimes
3448876Srgrimes		/*
3451541Srgrimes		 * if required, chdir around.
3461541Srgrimes		 */
3471541Srgrimes		if ((arcn->pat != NULL) && (arcn->pat->chdname != NULL))
3488876Srgrimes			if (fchdir(cwdfd) != 0)
3491541Srgrimes				syswarn(1, errno,
3501541Srgrimes				    "Can't fchdir to starting directory");
3511541Srgrimes	}
3521541Srgrimes
3532806Sbde	/*
3548876Srgrimes	 * all done, restore directory modes and times as required; make sure
3551541Srgrimes	 * all patterns supplied by the user were matched; block off signals
3561541Srgrimes	 * to avoid chance for multiple entry into the cleanup code.
3571541Srgrimes	 */
3581541Srgrimes	(void)(*frmt->end_rd)();
3591541Srgrimes	(void)sigprocmask(SIG_BLOCK, &s_mask, NULL);
3608876Srgrimes	ar_close();
3611541Srgrimes	proc_dir();
3621541Srgrimes	pat_chk();
3631541Srgrimes}
3641541Srgrimes
3658876Srgrimes/*
3661541Srgrimes * wr_archive()
3671541Srgrimes *	Write an archive. used in both creating a new archive and appends on
3681541Srgrimes *	previously written archive.
3691541Srgrimes */
3701541Srgrimes
3711541Srgrimes#ifdef __STDC__
3721541Srgrimesstatic void
3731541Srgrimeswr_archive(register ARCHD *arcn, int is_app)
3741541Srgrimes#else
3751541Srgrimesstatic void
3765651Sjoergwr_archive(arcn, is_app)
3771541Srgrimes	register ARCHD *arcn;
3781541Srgrimes	int is_app;
3791541Srgrimes#endif
3801541Srgrimes{
3811541Srgrimes	register int res;
3821541Srgrimes	register int hlk;
3831541Srgrimes	register int wr_one;
3841541Srgrimes	off_t cnt;
3851541Srgrimes	int (*wrf)();
3861541Srgrimes	int fd = -1;
3871541Srgrimes	time_t now;
3888876Srgrimes
3891541Srgrimes	/*
3901541Srgrimes	 * if this format supports hard link storage, start up the database
3918876Srgrimes	 * that detects them.
3921541Srgrimes	 */
3931541Srgrimes	if (((hlk = frmt->hlk) == 1) && (lnk_start() < 0))
3941541Srgrimes		return;
3951541Srgrimes
3961541Srgrimes	/*
3971541Srgrimes	 * start up the file traversal code and format specific write
3981541Srgrimes	 */
3991541Srgrimes	if ((ftree_start() < 0) || ((*frmt->st_wr)() < 0))
4001541Srgrimes		return;
4011541Srgrimes	wrf = frmt->wr;
4021541Srgrimes
4031541Srgrimes	/*
4041541Srgrimes	 * When we are doing interactive rename, we store the mapping of names
4051541Srgrimes	 * so we can fix up hard links files later in the archive.
4061541Srgrimes	 */
4071541Srgrimes	if (iflag && (name_start() < 0))
4081541Srgrimes		return;
4091541Srgrimes
4101541Srgrimes	/*
4111541Srgrimes	 * if this not append, and there are no files, we do no write a trailer
4121541Srgrimes	 */
4138876Srgrimes	wr_one = is_app;
4141541Srgrimes
4151541Srgrimes	now = time(NULL);
4161541Srgrimes
4171541Srgrimes	/*
4181541Srgrimes	 * while there are files to archive, process them one at at time
4191541Srgrimes	 */
4201541Srgrimes	while (next_file(arcn) == 0) {
4211541Srgrimes		/*
4221541Srgrimes		 * check if this file meets user specified options match.
4231541Srgrimes		 */
4241541Srgrimes		if (sel_chk(arcn) != 0)
4251541Srgrimes			continue;
4261541Srgrimes		fd = -1;
4271541Srgrimes		if (uflag) {
4281541Srgrimes			/*
4291541Srgrimes			 * only archive if this file is newer than a file with
4301541Srgrimes			 * the same name that is already stored on the archive
4311541Srgrimes			 */
4321541Srgrimes			if ((res = chk_ftime(arcn)) < 0)
4331541Srgrimes				break;
4341541Srgrimes			if (res > 0)
4351541Srgrimes				continue;
4361541Srgrimes		}
4371541Srgrimes
4381541Srgrimes		/*
4391541Srgrimes		 * this file is considered selected now. see if this is a hard
4401541Srgrimes		 * link to a file already stored
4411541Srgrimes		 */
4421541Srgrimes		ftree_sel(arcn);
4431541Srgrimes		if (hlk && (chk_lnk(arcn) < 0))
4441541Srgrimes			break;
4451541Srgrimes
4461541Srgrimes		if ((arcn->type == PAX_REG) || (arcn->type == PAX_HRG) ||
4471541Srgrimes		    (arcn->type == PAX_CTG)) {
4481541Srgrimes			/*
4491541Srgrimes			 * we will have to read this file. by opening it now we
4501541Srgrimes			 * can avoid writing a header to the archive for a file
4511541Srgrimes			 * we were later unable to read (we also purge it from
4521541Srgrimes			 * the link table).
4531541Srgrimes			 */
4541541Srgrimes			if ((fd = open(arcn->org_name, O_RDONLY, 0)) < 0) {
4551541Srgrimes				syswarn(1,errno, "Unable to open %s to read",
4561541Srgrimes					arcn->org_name);
4571541Srgrimes				purg_lnk(arcn);
4581541Srgrimes				continue;
4591541Srgrimes			}
4601541Srgrimes		}
4611541Srgrimes
4621541Srgrimes		/*
4631541Srgrimes		 * Now modify the name as requested by the user
4641541Srgrimes		 */
4651541Srgrimes		if ((res = mod_name(arcn)) < 0) {
4661541Srgrimes			/*
4671541Srgrimes			 * name modification says to skip this file, close the
4681541Srgrimes			 * file and purge link table entry
4691541Srgrimes			 */
4701541Srgrimes			rdfile_close(arcn, &fd);
4711541Srgrimes			purg_lnk(arcn);
4721541Srgrimes			break;
4731541Srgrimes		}
4741541Srgrimes
4751541Srgrimes		if ((res > 0) || (docrc && (set_crc(arcn, fd) < 0))) {
4761541Srgrimes			/*
4771541Srgrimes			 * unable to obtain the crc we need, close the file,
4781541Srgrimes			 * purge link table entry
4798876Srgrimes			 */
4801541Srgrimes			rdfile_close(arcn, &fd);
4811541Srgrimes			purg_lnk(arcn);
4825651Sjoerg			continue;
4831541Srgrimes		}
4841541Srgrimes
4851541Srgrimes		if (vflag) {
4861541Srgrimes			if (vflag > 1)
4878876Srgrimes				ls_list(arcn, now, listf);
4881541Srgrimes			else {
4891541Srgrimes				(void)fputs(arcn->name, listf);
4901541Srgrimes				vfpart = 1;
4911541Srgrimes			}
4921541Srgrimes		}
4931541Srgrimes		++flcnt;
4948876Srgrimes
4951541Srgrimes		/*
4961541Srgrimes		 * looks safe to store the file, have the format specific
4978876Srgrimes		 * routine write routine store the file header on the archive
4981541Srgrimes		 */
4991541Srgrimes		if ((res = (*wrf)(arcn)) < 0) {
5001541Srgrimes			rdfile_close(arcn, &fd);
5011541Srgrimes			break;
5021541Srgrimes		}
5038876Srgrimes		wr_one = 1;
5041541Srgrimes		if (res > 0) {
5051541Srgrimes			/*
5061541Srgrimes			 * format write says no file data needs to be stored
5071541Srgrimes			 * so we are done messing with this file
5081541Srgrimes			 */
5091541Srgrimes			if (vflag && vfpart) {
5101541Srgrimes				(void)putc('\n', listf);
5111541Srgrimes				vfpart = 0;
5121541Srgrimes			}
5131541Srgrimes			rdfile_close(arcn, &fd);
5141541Srgrimes			continue;
5151541Srgrimes		}
5161541Srgrimes
5171541Srgrimes		/*
5181541Srgrimes		 * Add file data to the archive, quit on write error. if we
5191541Srgrimes		 * cannot write the entire file contents to the archive we
5201541Srgrimes		 * must pad the archive to replace the missing file data
5211541Srgrimes		 * (otherwise during an extract the file header for the file
5228876Srgrimes		 * which FOLLOWS this one will not be where we expect it to
5231541Srgrimes		 * be).
5241541Srgrimes		 */
5251541Srgrimes		res = (*frmt->wr_data)(arcn, fd, &cnt);
5261541Srgrimes		rdfile_close(arcn, &fd);
5272604Sdfr		if (vflag && vfpart) {
5281541Srgrimes			(void)putc('\n', listf);
5291541Srgrimes			vfpart = 0;
5301541Srgrimes		}
5311541Srgrimes		if (res < 0)
5321541Srgrimes			break;
5331541Srgrimes
5341541Srgrimes		/*
5351541Srgrimes		 * pad as required, cnt is number of bytes not written
5361541Srgrimes		 */
5371541Srgrimes		if (((cnt > 0) && (wr_skip(cnt) < 0)) ||
5381541Srgrimes		    ((arcn->pad > 0) && (wr_skip(arcn->pad) < 0)))
5391541Srgrimes			break;
5401541Srgrimes	}
5411541Srgrimes
5421541Srgrimes	/*
5431541Srgrimes	 * tell format to write trailer; pad to block boundary; reset directory
5441541Srgrimes	 * mode/access times, and check if all patterns supplied by the user
5458876Srgrimes	 * were matched. block off signals to avoid chance for multiple entry
5461541Srgrimes	 * into the cleanup code
5471541Srgrimes	 */
5481541Srgrimes	if (wr_one) {
5491541Srgrimes		(*frmt->end_wr)();
5501541Srgrimes		wr_fin();
5511541Srgrimes	}
5521541Srgrimes	(void)sigprocmask(SIG_BLOCK, &s_mask, NULL);
5531541Srgrimes	ar_close();
5541541Srgrimes	if (tflag)
5551541Srgrimes		proc_dir();
5561541Srgrimes	ftree_chk();
5571541Srgrimes}
5581541Srgrimes
5591541Srgrimes/*
5601541Srgrimes * append()
5611541Srgrimes *	Add file to previously written archive. Archive format specified by the
5621541Srgrimes *	user must agree with archive. The archive is read first to collect
5631541Srgrimes *	modification times (if -u) and locate the archive trailer. The archive
5641541Srgrimes *	is positioned in front of the record with the trailer and wr_archive()
5651541Srgrimes *	is called to add the new members.
5668876Srgrimes *	PAX IMPLEMENTATION DETAIL NOTE:
5671541Srgrimes *	-u is implemented by adding the new members to the end of the archive.
5681541Srgrimes *	Care is taken so that these do not end up as links to the older
5691541Srgrimes *	version of the same file already stored in the archive. It is expected
5708876Srgrimes *	when extraction occurs these newer versions will over-write the older
5711541Srgrimes *	ones stored "earlier" in the archive (this may be a bad assumption as
5721541Srgrimes *	it depends on the implementation of the program doing the extraction).
5731541Srgrimes *	It is really difficult to splice in members without either re-writing
5748876Srgrimes *	the entire archive (from the point were the old version was), or having
5758876Srgrimes *	assistance of the format specification in terms of a special update
5768876Srgrimes *	header that invalidates a previous archive record. The POSIX spec left
5771541Srgrimes *	the method used to implement -u unspecified. This pax is able to
5781541Srgrimes *	over write existing files that it creates.
5791541Srgrimes */
5801541Srgrimes
5811541Srgrimes#ifdef __STDC__
5821541Srgrimesvoid
5831541Srgrimesappend(void)
5841541Srgrimes#else
5851541Srgrimesvoid
5861541Srgrimesappend()
5871541Srgrimes#endif
5881541Srgrimes{
5891541Srgrimes	register ARCHD *arcn;
5901541Srgrimes	register int res;
5911541Srgrimes	ARCHD archd;
5921541Srgrimes	FSUB *orgfrmt;
5931541Srgrimes	int udev;
5941541Srgrimes	off_t tlen;
5951541Srgrimes
5961541Srgrimes	arcn = &archd;
5971541Srgrimes	orgfrmt = frmt;
5981541Srgrimes
5991541Srgrimes	/*
6008876Srgrimes	 * Do not allow an append operation if the actual archive is of a
6011541Srgrimes	 * different format than the user specified format.
6021541Srgrimes	 */
6031541Srgrimes	if (get_arc() < 0)
6041541Srgrimes		return;
6051541Srgrimes	if ((orgfrmt != NULL) && (orgfrmt != frmt)) {
6061541Srgrimes		paxwarn(1, "Cannot mix current archive format %s with %s",
6071541Srgrimes		    frmt->name, orgfrmt->name);
6088876Srgrimes		return;
6091541Srgrimes	}
6101541Srgrimes
6111541Srgrimes	/*
6121541Srgrimes	 * pass the format any options and start up format
6138876Srgrimes	 */
6141541Srgrimes	if (((*frmt->options)() < 0) || ((*frmt->st_rd)() < 0))
6151541Srgrimes		return;
6161541Srgrimes
6178876Srgrimes	/*
6181541Srgrimes	 * if we only are adding members that are newer, we need to save the
6191541Srgrimes	 * mod times for all files we see.
6201541Srgrimes	 */
6218876Srgrimes	if (uflag && (ftime_start() < 0))
6228876Srgrimes		return;
6231541Srgrimes
6241541Srgrimes	/*
6251541Srgrimes	 * some archive formats encode hard links by recording the device and
6261541Srgrimes	 * file serial number (inode) but copy the file anyway (multiple times)
6271541Srgrimes	 * to the archive. When we append, we run the risk that newly added
6281541Srgrimes	 * files may have the same device and inode numbers as those recorded
6291541Srgrimes	 * on the archive but during a previous run. If this happens, when the
6301541Srgrimes	 * archive is extracted we get INCORRECT hard links. We avoid this by
6311541Srgrimes	 * remapping the device numbers so that newly added files will never
6321541Srgrimes	 * use the same device number as one found on the archive. remapping
6331541Srgrimes	 * allows new members to safely have links among themselves. remapping
6341541Srgrimes	 * also avoids problems with file inode (serial number) truncations
6351541Srgrimes	 * when the inode number is larger than storage space in the archive
6361541Srgrimes	 * header. See the remap routines for more details.
6371541Srgrimes	 */
6381541Srgrimes	if ((udev = frmt->udev) && (dev_start() < 0))
6391541Srgrimes		return;
6401541Srgrimes
6411541Srgrimes	/*
6428876Srgrimes	 * reading the archive may take a long time. If verbose tell the user
6431541Srgrimes	 */
6441541Srgrimes	if (vflag) {
6451541Srgrimes		(void)fprintf(listf,
6461541Srgrimes			"%s: Reading archive to position at the end...", argv0);
6471541Srgrimes		vfpart = 1;
6481541Srgrimes	}
6491541Srgrimes
6508876Srgrimes	/*
6511541Srgrimes	 * step through the archive until the format says it is done
6521541Srgrimes	 */
6531541Srgrimes	while (next_head(arcn) == 0) {
6541541Srgrimes		/*
6551541Srgrimes		 * check if this file meets user specified options.
6561541Srgrimes		 */
6571541Srgrimes		if (sel_chk(arcn) != 0) {
6581541Srgrimes			if (rd_skip(arcn->skip + arcn->pad) == 1)
6591541Srgrimes				break;
6601541Srgrimes			continue;
6611541Srgrimes		}
6621541Srgrimes
6631541Srgrimes		if (uflag) {
6641541Srgrimes			/*
6651541Srgrimes			 * see if this is the newest version of this file has
6661541Srgrimes			 * already been seen, if so skip.
6671541Srgrimes			 */
6681541Srgrimes			if ((res = chk_ftime(arcn)) < 0)
6691541Srgrimes				break;
6701541Srgrimes			if (res > 0) {
6711541Srgrimes				if (rd_skip(arcn->skip + arcn->pad) == 1)
6728876Srgrimes					break;
6731541Srgrimes				continue;
6741541Srgrimes			}
6751541Srgrimes		}
6761541Srgrimes
6771541Srgrimes		/*
6781541Srgrimes		 * Store this device number. Device numbers seen during the
6791541Srgrimes		 * read phase of append will cause newly appended files with a
6801541Srgrimes		 * device number seen in the old part of the archive to be
6811541Srgrimes		 * remapped to an unused device number.
6828876Srgrimes		 */
6831541Srgrimes		if ((udev && (add_dev(arcn) < 0)) ||
6841541Srgrimes		    (rd_skip(arcn->skip + arcn->pad) == 1))
6851541Srgrimes			break;
6861541Srgrimes	}
6878876Srgrimes
6881541Srgrimes	/*
6891541Srgrimes	 * done, finish up read and get the number of bytes to back up so we
690	 * can add new members. The format might have used the hard link table,
691	 * purge it.
692	 */
693	tlen = (*frmt->end_rd)();
694	lnk_end();
695
696	/*
697	 * try to position for write, if this fails quit. if any error occurs,
698	 * we will refuse to write
699	 */
700	if (appnd_start(tlen) < 0)
701		return;
702
703	/*
704	 * tell the user we are done reading.
705	 */
706	if (vflag && vfpart) {
707		(void)fputs("done.\n", listf);
708		vfpart = 0;
709	}
710
711	/*
712	 * go to the writing phase to add the new members
713	 */
714	wr_archive(arcn, 1);
715}
716
717/*
718 * archive()
719 *	write a new archive
720 */
721
722#ifdef __STDC__
723void
724archive(void)
725#else
726void
727archive()
728#endif
729{
730	ARCHD archd;
731
732	/*
733	 * if we only are adding members that are newer, we need to save the
734	 * mod times for all files; set up for writing; pass the format any
735	 * options write the archive
736	 */
737	if ((uflag && (ftime_start() < 0)) || (wr_start() < 0))
738		return;
739	if ((*frmt->options)() < 0)
740		return;
741
742	wr_archive(&archd, 0);
743}
744
745/*
746 * copy()
747 *	copy files from one part of the file system to another. this does not
748 *	use any archive storage. The EFFECT OF THE COPY IS THE SAME as if an
749 *	archive was written and then extracted in the destination directory
750 *	(except the files are forced to be under the destination directory).
751 */
752
753#ifdef __STDC__
754void
755copy(void)
756#else
757void
758copy()
759#endif
760{
761	register ARCHD *arcn;
762	register int res;
763	register int fddest;
764	register char *dest_pt;
765	register int dlen;
766	register int drem;
767	int fdsrc = -1;
768	struct stat sb;
769	ARCHD archd;
770	char dirbuf[PAXPATHLEN+1];
771
772	arcn = &archd;
773	/*
774	 * set up the destination dir path and make sure it is a directory. We
775	 * make sure we have a trailing / on the destination
776	 */
777	dlen = l_strncpy(dirbuf, dirptr, sizeof(dirbuf) - 1);
778	dest_pt = dirbuf + dlen;
779	if (*(dest_pt-1) != '/') {
780		*dest_pt++ = '/';
781		++dlen;
782	}
783	*dest_pt = '\0';
784	drem = PAXPATHLEN - dlen;
785
786	if (stat(dirptr, &sb) < 0) {
787		syswarn(1, errno, "Cannot access destination directory %s",
788			dirptr);
789		return;
790	}
791	if (!S_ISDIR(sb.st_mode)) {
792		paxwarn(1, "Destination is not a directory %s", dirptr);
793		return;
794	}
795
796	/*
797	 * start up the hard link table; file traversal routines and the
798	 * modification time and access mode database
799	 */
800	if ((lnk_start() < 0) || (ftree_start() < 0) || (dir_start() < 0))
801		return;
802
803	/*
804	 * When we are doing interactive rename, we store the mapping of names
805	 * so we can fix up hard links files later in the archive.
806	 */
807	if (iflag && (name_start() < 0))
808		return;
809
810	/*
811	 * set up to cp file trees
812	 */
813	cp_start();
814
815	/*
816	 * while there are files to archive, process them
817	 */
818	while (next_file(arcn) == 0) {
819		fdsrc = -1;
820
821		/*
822		 * check if this file meets user specified options
823		 */
824		if (sel_chk(arcn) != 0)
825			continue;
826
827		/*
828		 * if there is already a file in the destination directory with
829		 * the same name and it is newer, skip the one stored on the
830		 * archive.
831		 * NOTE: this test is done BEFORE name modifications as
832		 * specified by pax. this can be confusing to the user who
833		 * might expect the test to be done on an existing file AFTER
834		 * the name mod. In honesty the pax spec is probably flawed in
835		 * this respect
836		 */
837		if (uflag || Dflag) {
838			/*
839			 * create the destination name
840			 */
841			if (*(arcn->name) == '/')
842				res = 1;
843			else
844				res = 0;
845			if ((arcn->nlen - res) > drem) {
846				paxwarn(1, "Destination pathname too long %s",
847					arcn->name);
848				continue;
849			}
850			(void)strncpy(dest_pt, arcn->name + res, drem);
851			dirbuf[PAXPATHLEN] = '\0';
852
853			/*
854			 * if existing file is same age or newer skip
855			 */
856			res = lstat(dirbuf, &sb);
857			*dest_pt = '\0';
858
859		    	if (res == 0) {
860				if (uflag && Dflag) {
861					if ((arcn->sb.st_mtime<=sb.st_mtime) &&
862			    		    (arcn->sb.st_ctime<=sb.st_ctime))
863						continue;
864				} else if (Dflag) {
865					if (arcn->sb.st_ctime <= sb.st_ctime)
866						continue;
867				} else if (arcn->sb.st_mtime <= sb.st_mtime)
868					continue;
869			}
870		}
871
872		/*
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.
876		 */
877		ftree_sel(arcn);
878		if ((chk_lnk(arcn) < 0) || ((res = mod_name(arcn)) < 0))
879			break;
880		if ((res > 0) || (set_dest(arcn, dirbuf, dlen) < 0)) {
881			/*
882			 * skip file, purge from link table
883			 */
884			purg_lnk(arcn);
885			continue;
886		}
887
888		/*
889		 * Non standard -Y and -Z flag. When the exisiting file is
890		 * same age or newer skip
891		 */
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))
896					continue;
897			} else if (Yflag) {
898				if (arcn->sb.st_ctime <= sb.st_ctime)
899					continue;
900			} else if (arcn->sb.st_mtime <= sb.st_mtime)
901				continue;
902		}
903
904		if (vflag) {
905			(void)fputs(arcn->name, listf);
906			vfpart = 1;
907		}
908		++flcnt;
909
910		/*
911		 * try to create a hard link to the src file if requested
912		 * but make sure we are not trying to overwrite ourselves.
913		 */
914		if (lflag)
915			res = cross_lnk(arcn);
916		else
917			res = chk_same(arcn);
918		if (res <= 0) {
919			if (vflag && vfpart) {
920				(void)putc('\n', listf);
921				vfpart = 0;
922			}
923			continue;
924		}
925
926		/*
927		 * have to create a new file
928		 */
929		if ((arcn->type != PAX_REG) && (arcn->type != PAX_CTG)) {
930			/*
931			 * create a link or special file
932			 */
933			if ((arcn->type == PAX_HLK) || (arcn->type == PAX_HRG))
934				res = lnk_creat(arcn);
935			else
936				res = node_creat(arcn);
937			if (res < 0)
938				purg_lnk(arcn);
939			if (vflag && vfpart) {
940				(void)putc('\n', listf);
941				vfpart = 0;
942			}
943			continue;
944		}
945
946		/*
947		 * have to copy a regular file to the destination directory.
948		 * first open source file and then create the destination file
949		 */
950		if ((fdsrc = open(arcn->org_name, O_RDONLY, 0)) < 0) {
951			syswarn(1, errno, "Unable to open %s to read",
952			    arcn->org_name);
953			purg_lnk(arcn);
954			continue;
955		}
956		if ((fddest = file_creat(arcn)) < 0) {
957			rdfile_close(arcn, &fdsrc);
958			purg_lnk(arcn);
959			continue;
960		}
961
962		/*
963		 * copy source file data to the destination file
964		 */
965		cp_file(arcn, fdsrc, fddest);
966		file_close(arcn, fddest);
967		rdfile_close(arcn, &fdsrc);
968
969		if (vflag && vfpart) {
970			(void)putc('\n', listf);
971			vfpart = 0;
972		}
973	}
974
975	/*
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.
979	 */
980	(void)sigprocmask(SIG_BLOCK, &s_mask, NULL);
981	ar_close();
982	proc_dir();
983	ftree_chk();
984}
985
986/*
987 * next_head()
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.
999 * Return:
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)
1003 */
1004
1005#ifdef __STDC__
1006static int
1007next_head(register ARCHD *arcn)
1008#else
1009static int
1010next_head(arcn)
1011	register ARCHD *arcn;
1012#endif
1013{
1014	register int ret;
1015	register char *hdend;
1016	register int res;
1017	register int shftsz;
1018	register int hsz;
1019	register int in_resync = 0; 	/* set when we are in resync mode */
1020	int cnt = 0;			/* counter for trailer function */
1021	int first = 1;			/* on 1st read, EOF isn't premature. */
1022
1023	/*
1024	 * set up initial conditions, we want a whole frmt->hsz block as we
1025	 * have no data yet.
1026	 */
1027	res = hsz = frmt->hsz;
1028	hdend = hdbuf;
1029	shftsz = hsz - 1;
1030	for(;;) {
1031		/*
1032		 * keep looping until we get a contiguous FULL buffer
1033		 * (frmt->hsz is the proper size)
1034		 */
1035		for (;;) {
1036			if ((ret = rd_wrbuf(hdend, res)) == res)
1037				break;
1038
1039			/*
1040			 * If we read 0 bytes (EOF) from an archive when we
1041			 * expect to find a header, we have stepped upon
1042			 * an archive without the customary block of zeroes
1043			 * end marker.  It's just stupid to error out on
1044			 * them, so exit gracefully.
1045			 */
1046			if (first && ret == 0)
1047				return(-1);
1048			first = 0;
1049
1050			/*
1051			 * some kind of archive read problem, try to resync the
1052			 * storage device, better give the user the bad news.
1053			 */
1054			if ((ret == 0) || (rd_sync() < 0)) {
1055				paxwarn(1,"Premature end of file on archive read");
1056				return(-1);
1057			}
1058			if (!in_resync) {
1059				if (act == APPND) {
1060					paxwarn(1,
1061					  "Archive I/O error, cannot continue");
1062					return(-1);
1063				}
1064				paxwarn(1,"Archive I/O error. Trying to recover.");
1065				++in_resync;
1066			}
1067
1068			/*
1069			 * oh well, throw it all out and start over
1070			 */
1071			res = hsz;
1072			hdend = hdbuf;
1073		}
1074
1075		/*
1076		 * ok we have a contiguous buffer of the right size. Call the
1077		 * format read routine. If this was not a valid header and this
1078		 * format stores trailers outside of the header, call the
1079		 * format specific trailer routine to check for a trailer. We
1080		 * have to watch out that we do not mis-identify file data or
1081		 * block padding as a header or trailer. Format specific
1082		 * trailer functions must NOT check for the trailer while we
1083		 * are running in resync mode. Some trailer functions may tell
1084		 * us that this block cannot contain a valid header either, so
1085		 * we then throw out the entire block and start over.
1086		 */
1087		if ((*frmt->rd)(arcn, hdbuf) == 0)
1088			break;
1089
1090		if (!frmt->inhead) {
1091			/*
1092			 * this format has trailers outside of valid headers
1093			 */
1094			if ((ret = (*frmt->trail)(hdbuf,in_resync,&cnt)) == 0){
1095				/*
1096				 * valid trailer found, drain input as required
1097				 */
1098				ar_drain();
1099				return(-1);
1100			}
1101
1102			if (ret == 1) {
1103				/*
1104				 * we are in resync and we were told to throw
1105				 * the whole block out because none of the
1106				 * bytes in this block can be used to form a
1107				 * valid header
1108				 */
1109				res = hsz;
1110				hdend = hdbuf;
1111				continue;
1112			}
1113		}
1114
1115		/*
1116		 * Brute force section.
1117		 * not a valid header. We may be able to find a header yet. So
1118		 * we shift over by one byte, and set up to read one byte at a
1119		 * time from the archive and place it at the end of the buffer.
1120		 * We will keep moving byte at a time until we find a header or
1121		 * get a read error and have to start over.
1122		 */
1123		if (!in_resync) {
1124			if (act == APPND) {
1125				paxwarn(1,"Unable to append, archive header flaw");
1126				return(-1);
1127			}
1128			paxwarn(1,"Invalid header, starting valid header search.");
1129			++in_resync;
1130		}
1131		memmove(hdbuf, hdbuf+1, shftsz);
1132		res = 1;
1133		hdend = hdbuf + shftsz;
1134	}
1135
1136	/*
1137	 * ok got a valid header, check for trailer if format encodes it in the
1138	 * the header. NOTE: the parameters are different than trailer routines
1139	 * which encode trailers outside of the header!
1140	 */
1141	if (frmt->inhead && ((*frmt->trail)(arcn) == 0)) {
1142		/*
1143		 * valid trailer found, drain input as required
1144		 */
1145		ar_drain();
1146		return(-1);
1147	}
1148
1149	++flcnt;
1150	return(0);
1151}
1152
1153/*
1154 * get_arc()
1155 *	Figure out what format an archive is. Handles archive with flaws by
1156 *	brute force searches for a legal header in any supported format. The
1157 *	format id routines have to be careful to NOT mis-identify a format.
1158 *	ASSUMED: headers fit within a BLKMULT header.
1159 * Return:
1160 *	0 if archive found -1 otherwise
1161 */
1162
1163#ifdef __STDC__
1164static int
1165get_arc(void)
1166#else
1167static int
1168get_arc()
1169#endif
1170{
1171	register int i;
1172	register int hdsz = 0;
1173	register int res;
1174	register int minhd = BLKMULT;
1175	char *hdend;
1176	int notice = 0;
1177
1178	/*
1179	 * find the smallest header size in all archive formats and then set up
1180	 * to read the archive.
1181	 */
1182	for (i = 0; ford[i] >= 0; ++i) {
1183		if (fsub[ford[i]].hsz < minhd)
1184			minhd = fsub[ford[i]].hsz;
1185	}
1186	if (rd_start() < 0)
1187		return(-1);
1188	res = BLKMULT;
1189	hdsz = 0;
1190	hdend = hdbuf;
1191	for(;;) {
1192		for (;;) {
1193			/*
1194			 * fill the buffer with at least the smallest header
1195			 */
1196			i = rd_wrbuf(hdend, res);
1197			if (i > 0)
1198				hdsz += i;
1199			if (hdsz >= minhd)
1200				break;
1201
1202			/*
1203			 * if we cannot recover from a read error quit
1204			 */
1205			if ((i == 0) || (rd_sync() < 0))
1206				goto out;
1207
1208			/*
1209			 * when we get an error none of the data we already
1210			 * have can be used to create a legal header (we just
1211			 * got an error in the middle), so we throw it all out
1212			 * and refill the buffer with fresh data.
1213			 */
1214			res = BLKMULT;
1215			hdsz = 0;
1216			hdend = hdbuf;
1217			if (!notice) {
1218				if (act == APPND)
1219					return(-1);
1220				paxwarn(1,"Cannot identify format. Searching...");
1221				++notice;
1222			}
1223		}
1224
1225		/*
1226		 * we have at least the size of the smallest header in any
1227		 * archive format. Look to see if we have a match. The array
1228		 * ford[] is used to specify the header id order to reduce the
1229		 * chance of incorrectly id'ing a valid header (some formats
1230		 * may be subsets of each other and the order would then be
1231		 * important).
1232		 */
1233		for (i = 0; ford[i] >= 0; ++i) {
1234			if ((*fsub[ford[i]].id)(hdbuf, hdsz) < 0)
1235				continue;
1236			frmt = &(fsub[ford[i]]);
1237			/*
1238			 * yuck, to avoid slow special case code in the extract
1239			 * routines, just push this header back as if it was
1240			 * not seen. We have left extra space at start of the
1241			 * buffer for this purpose. This is a bit ugly, but
1242			 * adding all the special case code is far worse.
1243			 */
1244			pback(hdbuf, hdsz);
1245			return(0);
1246		}
1247
1248		/*
1249		 * We have a flawed archive, no match. we start searching, but
1250		 * we never allow additions to flawed archives
1251		 */
1252		if (!notice) {
1253			if (act == APPND)
1254				return(-1);
1255			paxwarn(1, "Cannot identify format. Searching...");
1256			++notice;
1257		}
1258
1259		/*
1260		 * brute force search for a header that we can id.
1261		 * we shift through byte at a time. this is slow, but we cannot
1262		 * determine the nature of the flaw in the archive in a
1263		 * portable manner
1264		 */
1265		if (--hdsz > 0) {
1266			memmove(hdbuf, hdbuf+1, hdsz);
1267			res = BLKMULT - hdsz;
1268			hdend = hdbuf + hdsz;
1269		} else {
1270			res = BLKMULT;
1271			hdend = hdbuf;
1272			hdsz = 0;
1273		}
1274	}
1275
1276    out:
1277	/*
1278	 * we cannot find a header, bow, apologize and quit
1279	 */
1280	paxwarn(1, "Sorry, unable to determine archive format.");
1281	return(-1);
1282}
1283