xref: /freebsd/bin/pax/ar_subs.c (revision e043f37205ffbde5627ff299ad25cd532f2956f0)
14b88c807SRodney W. Grimes /*-
28a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
38a16b7a1SPedro F. Giffuni  *
44b88c807SRodney W. Grimes  * Copyright (c) 1992 Keith Muller.
54b88c807SRodney W. Grimes  * Copyright (c) 1992, 1993
64b88c807SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
74b88c807SRodney W. Grimes  *
84b88c807SRodney W. Grimes  * This code is derived from software contributed to Berkeley by
94b88c807SRodney W. Grimes  * Keith Muller of the University of California, San Diego.
104b88c807SRodney W. Grimes  *
114b88c807SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
124b88c807SRodney W. Grimes  * modification, are permitted provided that the following conditions
134b88c807SRodney W. Grimes  * are met:
144b88c807SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
154b88c807SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
164b88c807SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
174b88c807SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
184b88c807SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
19fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
204b88c807SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
214b88c807SRodney W. Grimes  *    without specific prior written permission.
224b88c807SRodney W. Grimes  *
234b88c807SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
244b88c807SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
254b88c807SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
264b88c807SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
274b88c807SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
284b88c807SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
294b88c807SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
304b88c807SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
314b88c807SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
324b88c807SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
334b88c807SRodney W. Grimes  * SUCH DAMAGE.
344b88c807SRodney W. Grimes  */
354b88c807SRodney W. Grimes 
364b88c807SRodney W. Grimes #include <sys/types.h>
374b88c807SRodney W. Grimes #include <sys/time.h>
384b88c807SRodney W. Grimes #include <sys/stat.h>
394b88c807SRodney W. Grimes #include <signal.h>
404b88c807SRodney W. Grimes #include <string.h>
414b88c807SRodney W. Grimes #include <stdio.h>
424b88c807SRodney W. Grimes #include <fcntl.h>
434b88c807SRodney W. Grimes #include <errno.h>
444b88c807SRodney W. Grimes #include <unistd.h>
454b88c807SRodney W. Grimes #include "pax.h"
464b88c807SRodney W. Grimes #include "extern.h"
474b88c807SRodney W. Grimes 
48f789b261SWarner Losh static void wr_archive(ARCHD *, int is_app);
4946251ddeSWarner Losh static int get_arc(void);
50f789b261SWarner Losh static int next_head(ARCHD *);
514b88c807SRodney W. Grimes 
524b88c807SRodney W. Grimes /*
534b88c807SRodney W. Grimes  * Routines which control the overall operation modes of pax as specified by
544b88c807SRodney W. Grimes  * the user: list, append, read ...
554b88c807SRodney W. Grimes  */
564b88c807SRodney W. Grimes 
574b88c807SRodney W. Grimes static char hdbuf[BLKMULT];		/* space for archive header on read */
584b88c807SRodney W. Grimes u_long flcnt;				/* number of files processed */
594b88c807SRodney W. Grimes 
604b88c807SRodney W. Grimes /*
614b88c807SRodney W. Grimes  * list()
624b88c807SRodney W. Grimes  *	list the contents of an archive which match user supplied pattern(s)
634b88c807SRodney W. Grimes  *	(no pattern matches all).
644b88c807SRodney W. Grimes  */
654b88c807SRodney W. Grimes 
664b88c807SRodney W. Grimes void
list(void)674b88c807SRodney W. Grimes list(void)
684b88c807SRodney W. Grimes {
69f789b261SWarner Losh 	ARCHD *arcn;
70f789b261SWarner Losh 	int res;
714b88c807SRodney W. Grimes 	ARCHD archd;
724b88c807SRodney W. Grimes 	time_t now;
734b88c807SRodney W. Grimes 
744b88c807SRodney W. Grimes 	arcn = &archd;
754b88c807SRodney W. Grimes 	/*
764b88c807SRodney W. Grimes 	 * figure out archive type; pass any format specific options to the
774b88c807SRodney W. Grimes 	 * archive option processing routine; call the format init routine. We
784b88c807SRodney W. Grimes 	 * also save current time for ls_list() so we do not make a system
794b88c807SRodney W. Grimes 	 * call for each file we need to print. If verbose (vflag) start up
804b88c807SRodney W. Grimes 	 * the name and group caches.
814b88c807SRodney W. Grimes 	 */
824b88c807SRodney W. Grimes 	if ((get_arc() < 0) || ((*frmt->options)() < 0) ||
834b88c807SRodney W. Grimes 	    ((*frmt->st_rd)() < 0))
844b88c807SRodney W. Grimes 		return;
854b88c807SRodney W. Grimes 
864b88c807SRodney W. Grimes 	if (vflag && ((uidtb_start() < 0) || (gidtb_start() < 0)))
874b88c807SRodney W. Grimes 		return;
884b88c807SRodney W. Grimes 
89778766feSKris Kennaway 	now = time(NULL);
904b88c807SRodney W. Grimes 
914b88c807SRodney W. Grimes 	/*
924b88c807SRodney W. Grimes 	 * step through the archive until the format says it is done
934b88c807SRodney W. Grimes 	 */
944b88c807SRodney W. Grimes 	while (next_head(arcn) == 0) {
954b88c807SRodney W. Grimes 		/*
964b88c807SRodney W. Grimes 		 * check for pattern, and user specified options match.
974b88c807SRodney W. Grimes 		 * When all patterns are matched we are done.
984b88c807SRodney W. Grimes 		 */
994b88c807SRodney W. Grimes 		if ((res = pat_match(arcn)) < 0)
1004b88c807SRodney W. Grimes 			break;
1014b88c807SRodney W. Grimes 
1024b88c807SRodney W. Grimes 		if ((res == 0) && (sel_chk(arcn) == 0)) {
1034b88c807SRodney W. Grimes 			/*
1044b88c807SRodney W. Grimes 			 * pattern resulted in a selected file
1054b88c807SRodney W. Grimes 			 */
1064b88c807SRodney W. Grimes 			if (pat_sel(arcn) < 0)
1074b88c807SRodney W. Grimes 				break;
1084b88c807SRodney W. Grimes 
1094b88c807SRodney W. Grimes 			/*
1104b88c807SRodney W. Grimes 			 * modify the name as requested by the user if name
1114b88c807SRodney W. Grimes 			 * survives modification, do a listing of the file
1124b88c807SRodney W. Grimes 			 */
1134b88c807SRodney W. Grimes 			if ((res = mod_name(arcn)) < 0)
1144b88c807SRodney W. Grimes 				break;
1154b88c807SRodney W. Grimes 			if (res == 0)
116b1787decSKris Kennaway 				ls_list(arcn, now, stdout);
1174b88c807SRodney W. Grimes 		}
1184b88c807SRodney W. Grimes 
1194b88c807SRodney W. Grimes 		/*
1204b88c807SRodney W. Grimes 		 * skip to next archive format header using values calculated
1214b88c807SRodney W. Grimes 		 * by the format header read routine
1224b88c807SRodney W. Grimes 		 */
1234b88c807SRodney W. Grimes 		if (rd_skip(arcn->skip + arcn->pad) == 1)
1244b88c807SRodney W. Grimes 			break;
1254b88c807SRodney W. Grimes 	}
1264b88c807SRodney W. Grimes 
1274b88c807SRodney W. Grimes 	/*
1284b88c807SRodney W. Grimes 	 * all done, let format have a chance to cleanup, and make sure that
1294b88c807SRodney W. Grimes 	 * the patterns supplied by the user were all matched
1304b88c807SRodney W. Grimes 	 */
1314b88c807SRodney W. Grimes 	(void)(*frmt->end_rd)();
132778766feSKris Kennaway 	(void)sigprocmask(SIG_BLOCK, &s_mask, NULL);
1334b88c807SRodney W. Grimes 	ar_close();
1344b88c807SRodney W. Grimes 	pat_chk();
1354b88c807SRodney W. Grimes }
1364b88c807SRodney W. Grimes 
1374b88c807SRodney W. Grimes /*
1384b88c807SRodney W. Grimes  * extract()
1394b88c807SRodney W. Grimes  *	extract the member(s) of an archive as specified by user supplied
1404b88c807SRodney W. Grimes  *	pattern(s) (no patterns extracts all members)
1414b88c807SRodney W. Grimes  */
1424b88c807SRodney W. Grimes 
1434b88c807SRodney W. Grimes void
extract(void)1444b88c807SRodney W. Grimes extract(void)
1454b88c807SRodney W. Grimes {
146f789b261SWarner Losh 	ARCHD *arcn;
147f789b261SWarner Losh 	int res;
1484b88c807SRodney W. Grimes 	off_t cnt;
1494b88c807SRodney W. Grimes 	ARCHD archd;
1504b88c807SRodney W. Grimes 	struct stat sb;
1514b88c807SRodney W. Grimes 	int fd;
152b1787decSKris Kennaway 	time_t now;
1534b88c807SRodney W. Grimes 
1544b88c807SRodney W. Grimes 	arcn = &archd;
1554b88c807SRodney W. Grimes 	/*
1564b88c807SRodney W. Grimes 	 * figure out archive type; pass any format specific options to the
1574b88c807SRodney W. Grimes 	 * archive option processing routine; call the format init routine;
1584b88c807SRodney W. Grimes 	 * start up the directory modification time and access mode database
1594b88c807SRodney W. Grimes 	 */
1604b88c807SRodney W. Grimes 	if ((get_arc() < 0) || ((*frmt->options)() < 0) ||
1614b88c807SRodney W. Grimes 	    ((*frmt->st_rd)() < 0) || (dir_start() < 0))
1624b88c807SRodney W. Grimes 		return;
1634b88c807SRodney W. Grimes 
1644b88c807SRodney W. Grimes 	/*
1654b88c807SRodney W. Grimes 	 * When we are doing interactive rename, we store the mapping of names
1664b88c807SRodney W. Grimes 	 * so we can fix up hard links files later in the archive.
1674b88c807SRodney W. Grimes 	 */
1684b88c807SRodney W. Grimes 	if (iflag && (name_start() < 0))
1694b88c807SRodney W. Grimes 		return;
1704b88c807SRodney W. Grimes 
171b1787decSKris Kennaway 	now = time(NULL);
172b1787decSKris Kennaway 
1734b88c807SRodney W. Grimes 	/*
1744b88c807SRodney W. Grimes 	 * step through each entry on the archive until the format read routine
1754b88c807SRodney W. Grimes 	 * says it is done
1764b88c807SRodney W. Grimes 	 */
1774b88c807SRodney W. Grimes 	while (next_head(arcn) == 0) {
1784b88c807SRodney W. Grimes 
1794b88c807SRodney W. Grimes 		/*
1804b88c807SRodney W. Grimes 		 * check for pattern, and user specified options match. When
1814b88c807SRodney W. Grimes 		 * all the patterns are matched we are done
1824b88c807SRodney W. Grimes 		 */
1834b88c807SRodney W. Grimes 		if ((res = pat_match(arcn)) < 0)
1844b88c807SRodney W. Grimes 			break;
1854b88c807SRodney W. Grimes 
1864b88c807SRodney W. Grimes 		if ((res > 0) || (sel_chk(arcn) != 0)) {
1874b88c807SRodney W. Grimes 			/*
1884b88c807SRodney W. Grimes 			 * file is not selected. skip past any file data and
1894b88c807SRodney W. Grimes 			 * padding and go back for the next archive member
1904b88c807SRodney W. Grimes 			 */
1914b88c807SRodney W. Grimes 			(void)rd_skip(arcn->skip + arcn->pad);
1924b88c807SRodney W. Grimes 			continue;
1934b88c807SRodney W. Grimes 		}
1944b88c807SRodney W. Grimes 
1954b88c807SRodney W. Grimes 		/*
1964b88c807SRodney W. Grimes 		 * with -u or -D only extract when the archive member is newer
197*0266a5d6SDag-Erling Smørgrav 		 * than the file with the same name in the file system (no
1984b88c807SRodney W. Grimes 		 * test of being the same type is required).
1994b88c807SRodney W. Grimes 		 * NOTE: this test is done BEFORE name modifications as
2004b88c807SRodney W. Grimes 		 * specified by pax. this operation can be confusing to the
2014b88c807SRodney W. Grimes 		 * user who might expect the test to be done on an existing
2024b88c807SRodney W. Grimes 		 * file AFTER the name mod. In honesty the pax spec is probably
2034b88c807SRodney W. Grimes 		 * flawed in this respect.
2044b88c807SRodney W. Grimes 		 */
2054b88c807SRodney W. Grimes 		if ((uflag || Dflag) && ((lstat(arcn->name, &sb) == 0))) {
2064b88c807SRodney W. Grimes 			if (uflag && Dflag) {
2074b88c807SRodney W. Grimes 				if ((arcn->sb.st_mtime <= sb.st_mtime) &&
2084b88c807SRodney W. Grimes 				    (arcn->sb.st_ctime <= sb.st_ctime)) {
2094b88c807SRodney W. Grimes 					(void)rd_skip(arcn->skip + arcn->pad);
2104b88c807SRodney W. Grimes 					continue;
2114b88c807SRodney W. Grimes 				}
2124b88c807SRodney W. Grimes 			} else if (Dflag) {
2134b88c807SRodney W. Grimes 				if (arcn->sb.st_ctime <= sb.st_ctime) {
2144b88c807SRodney W. Grimes 					(void)rd_skip(arcn->skip + arcn->pad);
2154b88c807SRodney W. Grimes 					continue;
2164b88c807SRodney W. Grimes 				}
2174b88c807SRodney W. Grimes 			} else if (arcn->sb.st_mtime <= sb.st_mtime) {
2184b88c807SRodney W. Grimes 				(void)rd_skip(arcn->skip + arcn->pad);
2194b88c807SRodney W. Grimes 				continue;
2204b88c807SRodney W. Grimes 			}
2214b88c807SRodney W. Grimes 		}
2224b88c807SRodney W. Grimes 
2234b88c807SRodney W. Grimes 		/*
2244b88c807SRodney W. Grimes 		 * this archive member is now been selected. modify the name.
2254b88c807SRodney W. Grimes 		 */
2264b88c807SRodney W. Grimes 		if ((pat_sel(arcn) < 0) || ((res = mod_name(arcn)) < 0))
2274b88c807SRodney W. Grimes 			break;
2284b88c807SRodney W. Grimes 		if (res > 0) {
2294b88c807SRodney W. Grimes 			/*
2304b88c807SRodney W. Grimes 			 * a bad name mod, skip and purge name from link table
2314b88c807SRodney W. Grimes 			 */
2324b88c807SRodney W. Grimes 			purg_lnk(arcn);
2334b88c807SRodney W. Grimes 			(void)rd_skip(arcn->skip + arcn->pad);
2344b88c807SRodney W. Grimes 			continue;
2354b88c807SRodney W. Grimes 		}
2364b88c807SRodney W. Grimes 
2374b88c807SRodney W. Grimes 		/*
23846be34b9SKris Kennaway 		 * Non standard -Y and -Z flag. When the existing file is
2394b88c807SRodney W. Grimes 		 * same age or newer skip
2404b88c807SRodney W. Grimes 		 */
2414b88c807SRodney W. Grimes 		if ((Yflag || Zflag) && ((lstat(arcn->name, &sb) == 0))) {
2424b88c807SRodney W. Grimes 			if (Yflag && Zflag) {
2434b88c807SRodney W. Grimes 				if ((arcn->sb.st_mtime <= sb.st_mtime) &&
2444b88c807SRodney W. Grimes 				    (arcn->sb.st_ctime <= sb.st_ctime)) {
2454b88c807SRodney W. Grimes 					(void)rd_skip(arcn->skip + arcn->pad);
2464b88c807SRodney W. Grimes 					continue;
2474b88c807SRodney W. Grimes 				}
2484b88c807SRodney W. Grimes 			} else if (Yflag) {
2494b88c807SRodney W. Grimes 				if (arcn->sb.st_ctime <= sb.st_ctime) {
2504b88c807SRodney W. Grimes 					(void)rd_skip(arcn->skip + arcn->pad);
2514b88c807SRodney W. Grimes 					continue;
2524b88c807SRodney W. Grimes 				}
2534b88c807SRodney W. Grimes 			} else if (arcn->sb.st_mtime <= sb.st_mtime) {
2544b88c807SRodney W. Grimes 				(void)rd_skip(arcn->skip + arcn->pad);
2554b88c807SRodney W. Grimes 				continue;
2564b88c807SRodney W. Grimes 			}
2574b88c807SRodney W. Grimes 		}
2584b88c807SRodney W. Grimes 
2594b88c807SRodney W. Grimes 		if (vflag) {
260b1787decSKris Kennaway 			if (vflag > 1)
261b1787decSKris Kennaway 				ls_list(arcn, now, listf);
262b1787decSKris Kennaway 			else {
263b1787decSKris Kennaway 				(void)fputs(arcn->name, listf);
2644b88c807SRodney W. Grimes 				vfpart = 1;
2654b88c807SRodney W. Grimes 			}
266b1787decSKris Kennaway 		}
267b1787decSKris Kennaway 
268b1787decSKris Kennaway 		/*
269b1787decSKris Kennaway 		 * if required, chdir around.
270b1787decSKris Kennaway 		 */
271b1787decSKris Kennaway 		if ((arcn->pat != NULL) && (arcn->pat->chdname != NULL))
272b1787decSKris Kennaway 			if (chdir(arcn->pat->chdname) != 0)
273b1787decSKris Kennaway 				syswarn(1, errno, "Cannot chdir to %s",
274b1787decSKris Kennaway 				    arcn->pat->chdname);
2754b88c807SRodney W. Grimes 
2764b88c807SRodney W. Grimes 		/*
2774b88c807SRodney W. Grimes 		 * all ok, extract this member based on type
2784b88c807SRodney W. Grimes 		 */
2794b88c807SRodney W. Grimes 		if ((arcn->type != PAX_REG) && (arcn->type != PAX_CTG)) {
2804b88c807SRodney W. Grimes 			/*
2814b88c807SRodney W. Grimes 			 * process archive members that are not regular files.
2824b88c807SRodney W. Grimes 			 * throw out padding and any data that might follow the
2834b88c807SRodney W. Grimes 			 * header (as determined by the format).
2844b88c807SRodney W. Grimes 			 */
2854b88c807SRodney W. Grimes 			if ((arcn->type == PAX_HLK) || (arcn->type == PAX_HRG))
2864b88c807SRodney W. Grimes 				res = lnk_creat(arcn);
2874b88c807SRodney W. Grimes 			else
2884b88c807SRodney W. Grimes 				res = node_creat(arcn);
2894b88c807SRodney W. Grimes 
2904b88c807SRodney W. Grimes 			(void)rd_skip(arcn->skip + arcn->pad);
2914b88c807SRodney W. Grimes 			if (res < 0)
2924b88c807SRodney W. Grimes 				purg_lnk(arcn);
2934b88c807SRodney W. Grimes 
2944b88c807SRodney W. Grimes 			if (vflag && vfpart) {
295b1787decSKris Kennaway 				(void)putc('\n', listf);
2964b88c807SRodney W. Grimes 				vfpart = 0;
2974b88c807SRodney W. Grimes 			}
2984b88c807SRodney W. Grimes 			continue;
2994b88c807SRodney W. Grimes 		}
3004b88c807SRodney W. Grimes 		/*
3014b88c807SRodney W. Grimes 		 * we have a file with data here. If we can not create it, skip
3024b88c807SRodney W. Grimes 		 * over the data and purge the name from hard link table
3034b88c807SRodney W. Grimes 		 */
3044b88c807SRodney W. Grimes 		if ((fd = file_creat(arcn)) < 0) {
3054b88c807SRodney W. Grimes 			(void)rd_skip(arcn->skip + arcn->pad);
3064b88c807SRodney W. Grimes 			purg_lnk(arcn);
3074b88c807SRodney W. Grimes 			continue;
3084b88c807SRodney W. Grimes 		}
3094b88c807SRodney W. Grimes 		/*
3104b88c807SRodney W. Grimes 		 * extract the file from the archive and skip over padding and
3114b88c807SRodney W. Grimes 		 * any unprocessed data
3124b88c807SRodney W. Grimes 		 */
3134b88c807SRodney W. Grimes 		res = (*frmt->rd_data)(arcn, fd, &cnt);
3144b88c807SRodney W. Grimes 		file_close(arcn, fd);
3154b88c807SRodney W. Grimes 		if (vflag && vfpart) {
316b1787decSKris Kennaway 			(void)putc('\n', listf);
3174b88c807SRodney W. Grimes 			vfpart = 0;
3184b88c807SRodney W. Grimes 		}
3194b88c807SRodney W. Grimes 		if (!res)
3204b88c807SRodney W. Grimes 			(void)rd_skip(cnt + arcn->pad);
321b1787decSKris Kennaway 
322b1787decSKris Kennaway 		/*
323b1787decSKris Kennaway 		 * if required, chdir around.
324b1787decSKris Kennaway 		 */
325b1787decSKris Kennaway 		if ((arcn->pat != NULL) && (arcn->pat->chdname != NULL))
326b1787decSKris Kennaway 			if (fchdir(cwdfd) != 0)
327b1787decSKris Kennaway 				syswarn(1, errno,
328b1787decSKris Kennaway 				    "Can't fchdir to starting directory");
3294b88c807SRodney W. Grimes 	}
3304b88c807SRodney W. Grimes 
3314b88c807SRodney W. Grimes 	/*
3324b88c807SRodney W. Grimes 	 * all done, restore directory modes and times as required; make sure
3334b88c807SRodney W. Grimes 	 * all patterns supplied by the user were matched; block off signals
3344b88c807SRodney W. Grimes 	 * to avoid chance for multiple entry into the cleanup code.
3354b88c807SRodney W. Grimes 	 */
3364b88c807SRodney W. Grimes 	(void)(*frmt->end_rd)();
337778766feSKris Kennaway 	(void)sigprocmask(SIG_BLOCK, &s_mask, NULL);
3384b88c807SRodney W. Grimes 	ar_close();
3394b88c807SRodney W. Grimes 	proc_dir();
3404b88c807SRodney W. Grimes 	pat_chk();
3414b88c807SRodney W. Grimes }
3424b88c807SRodney W. Grimes 
3434b88c807SRodney W. Grimes /*
3444b88c807SRodney W. Grimes  * wr_archive()
3454b88c807SRodney W. Grimes  *	Write an archive. used in both creating a new archive and appends on
3464b88c807SRodney W. Grimes  *	previously written archive.
3474b88c807SRodney W. Grimes  */
3484b88c807SRodney W. Grimes 
3494b88c807SRodney W. Grimes static void
wr_archive(ARCHD * arcn,int is_app)350f789b261SWarner Losh wr_archive(ARCHD *arcn, int is_app)
3514b88c807SRodney W. Grimes {
352f789b261SWarner Losh 	int res;
353f789b261SWarner Losh 	int hlk;
354f789b261SWarner Losh 	int wr_one;
3554b88c807SRodney W. Grimes 	off_t cnt;
35640feca3aSMark Murray 	int (*wrf)(ARCHD *);
3574b88c807SRodney W. Grimes 	int fd = -1;
358b1787decSKris Kennaway 	time_t now;
3594b88c807SRodney W. Grimes 
3604b88c807SRodney W. Grimes 	/*
3614b88c807SRodney W. Grimes 	 * if this format supports hard link storage, start up the database
3624b88c807SRodney W. Grimes 	 * that detects them.
3634b88c807SRodney W. Grimes 	 */
3644b88c807SRodney W. Grimes 	if (((hlk = frmt->hlk) == 1) && (lnk_start() < 0))
3654b88c807SRodney W. Grimes 		return;
3664b88c807SRodney W. Grimes 
3674b88c807SRodney W. Grimes 	/*
3684b88c807SRodney W. Grimes 	 * start up the file traversal code and format specific write
3694b88c807SRodney W. Grimes 	 */
3704b88c807SRodney W. Grimes 	if ((ftree_start() < 0) || ((*frmt->st_wr)() < 0))
3714b88c807SRodney W. Grimes 		return;
3724b88c807SRodney W. Grimes 	wrf = frmt->wr;
3734b88c807SRodney W. Grimes 
3744b88c807SRodney W. Grimes 	/*
3754b88c807SRodney W. Grimes 	 * When we are doing interactive rename, we store the mapping of names
3764b88c807SRodney W. Grimes 	 * so we can fix up hard links files later in the archive.
3774b88c807SRodney W. Grimes 	 */
3784b88c807SRodney W. Grimes 	if (iflag && (name_start() < 0))
3794b88c807SRodney W. Grimes 		return;
3804b88c807SRodney W. Grimes 
3814b88c807SRodney W. Grimes 	/*
382*0266a5d6SDag-Erling Smørgrav 	 * if this is not append, and there are no files, we do not write a
383*0266a5d6SDag-Erling Smørgrav 	 * trailer
3844b88c807SRodney W. Grimes 	 */
3854b88c807SRodney W. Grimes 	wr_one = is_app;
3864b88c807SRodney W. Grimes 
387b1787decSKris Kennaway 	now = time(NULL);
388b1787decSKris Kennaway 
3894b88c807SRodney W. Grimes 	/*
3904b88c807SRodney W. Grimes 	 * while there are files to archive, process them one at at time
3914b88c807SRodney W. Grimes 	 */
3924b88c807SRodney W. Grimes 	while (next_file(arcn) == 0) {
3934b88c807SRodney W. Grimes 		/*
3944b88c807SRodney W. Grimes 		 * check if this file meets user specified options match.
3954b88c807SRodney W. Grimes 		 */
3960e32d64eSBrian Somers 		if (sel_chk(arcn) != 0) {
3970e32d64eSBrian Somers 			ftree_notsel();
3984b88c807SRodney W. Grimes 			continue;
3990e32d64eSBrian Somers 		}
4004b88c807SRodney W. Grimes 		fd = -1;
4014b88c807SRodney W. Grimes 		if (uflag) {
4024b88c807SRodney W. Grimes 			/*
4034b88c807SRodney W. Grimes 			 * only archive if this file is newer than a file with
4044b88c807SRodney W. Grimes 			 * the same name that is already stored on the archive
4054b88c807SRodney W. Grimes 			 */
4064b88c807SRodney W. Grimes 			if ((res = chk_ftime(arcn)) < 0)
4074b88c807SRodney W. Grimes 				break;
4084b88c807SRodney W. Grimes 			if (res > 0)
4094b88c807SRodney W. Grimes 				continue;
4104b88c807SRodney W. Grimes 		}
4114b88c807SRodney W. Grimes 
4124b88c807SRodney W. Grimes 		/*
4134b88c807SRodney W. Grimes 		 * this file is considered selected now. see if this is a hard
4144b88c807SRodney W. Grimes 		 * link to a file already stored
4154b88c807SRodney W. Grimes 		 */
4164b88c807SRodney W. Grimes 		ftree_sel(arcn);
4174b88c807SRodney W. Grimes 		if (hlk && (chk_lnk(arcn) < 0))
4184b88c807SRodney W. Grimes 			break;
4194b88c807SRodney W. Grimes 
4204b88c807SRodney W. Grimes 		if ((arcn->type == PAX_REG) || (arcn->type == PAX_HRG) ||
4214b88c807SRodney W. Grimes 		    (arcn->type == PAX_CTG)) {
4224b88c807SRodney W. Grimes 			/*
4234b88c807SRodney W. Grimes 			 * we will have to read this file. by opening it now we
4244b88c807SRodney W. Grimes 			 * can avoid writing a header to the archive for a file
4254b88c807SRodney W. Grimes 			 * we were later unable to read (we also purge it from
4264b88c807SRodney W. Grimes 			 * the link table).
4274b88c807SRodney W. Grimes 			 */
4284b88c807SRodney W. Grimes 			if ((fd = open(arcn->org_name, O_RDONLY, 0)) < 0) {
429778766feSKris Kennaway 				syswarn(1,errno, "Unable to open %s to read",
4304b88c807SRodney W. Grimes 					arcn->org_name);
4314b88c807SRodney W. Grimes 				purg_lnk(arcn);
4324b88c807SRodney W. Grimes 				continue;
4334b88c807SRodney W. Grimes 			}
4344b88c807SRodney W. Grimes 		}
4354b88c807SRodney W. Grimes 
4364b88c807SRodney W. Grimes 		/*
4374b88c807SRodney W. Grimes 		 * Now modify the name as requested by the user
4384b88c807SRodney W. Grimes 		 */
4394b88c807SRodney W. Grimes 		if ((res = mod_name(arcn)) < 0) {
4404b88c807SRodney W. Grimes 			/*
4414b88c807SRodney W. Grimes 			 * name modification says to skip this file, close the
4424b88c807SRodney W. Grimes 			 * file and purge link table entry
4434b88c807SRodney W. Grimes 			 */
4444b88c807SRodney W. Grimes 			rdfile_close(arcn, &fd);
4454b88c807SRodney W. Grimes 			purg_lnk(arcn);
4464b88c807SRodney W. Grimes 			break;
4474b88c807SRodney W. Grimes 		}
4484b88c807SRodney W. Grimes 
4494b88c807SRodney W. Grimes 		if ((res > 0) || (docrc && (set_crc(arcn, fd) < 0))) {
4504b88c807SRodney W. Grimes 			/*
4514b88c807SRodney W. Grimes 			 * unable to obtain the crc we need, close the file,
4524b88c807SRodney W. Grimes 			 * purge link table entry
4534b88c807SRodney W. Grimes 			 */
4544b88c807SRodney W. Grimes 			rdfile_close(arcn, &fd);
4554b88c807SRodney W. Grimes 			purg_lnk(arcn);
4564b88c807SRodney W. Grimes 			continue;
4574b88c807SRodney W. Grimes 		}
4584b88c807SRodney W. Grimes 
4594b88c807SRodney W. Grimes 		if (vflag) {
460b1787decSKris Kennaway 			if (vflag > 1)
461b1787decSKris Kennaway 				ls_list(arcn, now, listf);
462b1787decSKris Kennaway 			else {
463b1787decSKris Kennaway 				(void)fputs(arcn->name, listf);
4644b88c807SRodney W. Grimes 				vfpart = 1;
4654b88c807SRodney W. Grimes 			}
466b1787decSKris Kennaway 		}
4674b88c807SRodney W. Grimes 		++flcnt;
4684b88c807SRodney W. Grimes 
4694b88c807SRodney W. Grimes 		/*
4704b88c807SRodney W. Grimes 		 * looks safe to store the file, have the format specific
4714b88c807SRodney W. Grimes 		 * routine write routine store the file header on the archive
4724b88c807SRodney W. Grimes 		 */
4734b88c807SRodney W. Grimes 		if ((res = (*wrf)(arcn)) < 0) {
4744b88c807SRodney W. Grimes 			rdfile_close(arcn, &fd);
4754b88c807SRodney W. Grimes 			break;
4764b88c807SRodney W. Grimes 		}
4774b88c807SRodney W. Grimes 		wr_one = 1;
4784b88c807SRodney W. Grimes 		if (res > 0) {
4794b88c807SRodney W. Grimes 			/*
4804b88c807SRodney W. Grimes 			 * format write says no file data needs to be stored
4814b88c807SRodney W. Grimes 			 * so we are done messing with this file
4824b88c807SRodney W. Grimes 			 */
4834b88c807SRodney W. Grimes 			if (vflag && vfpart) {
484b1787decSKris Kennaway 				(void)putc('\n', listf);
4854b88c807SRodney W. Grimes 				vfpart = 0;
4864b88c807SRodney W. Grimes 			}
4874b88c807SRodney W. Grimes 			rdfile_close(arcn, &fd);
4884b88c807SRodney W. Grimes 			continue;
4894b88c807SRodney W. Grimes 		}
4904b88c807SRodney W. Grimes 
4914b88c807SRodney W. Grimes 		/*
4924b88c807SRodney W. Grimes 		 * Add file data to the archive, quit on write error. if we
4934b88c807SRodney W. Grimes 		 * cannot write the entire file contents to the archive we
4944b88c807SRodney W. Grimes 		 * must pad the archive to replace the missing file data
4954b88c807SRodney W. Grimes 		 * (otherwise during an extract the file header for the file
4964b88c807SRodney W. Grimes 		 * which FOLLOWS this one will not be where we expect it to
4974b88c807SRodney W. Grimes 		 * be).
4984b88c807SRodney W. Grimes 		 */
4994b88c807SRodney W. Grimes 		res = (*frmt->wr_data)(arcn, fd, &cnt);
5004b88c807SRodney W. Grimes 		rdfile_close(arcn, &fd);
5014b88c807SRodney W. Grimes 		if (vflag && vfpart) {
502b1787decSKris Kennaway 			(void)putc('\n', listf);
5034b88c807SRodney W. Grimes 			vfpart = 0;
5044b88c807SRodney W. Grimes 		}
5054b88c807SRodney W. Grimes 		if (res < 0)
5064b88c807SRodney W. Grimes 			break;
5074b88c807SRodney W. Grimes 
5084b88c807SRodney W. Grimes 		/*
5094b88c807SRodney W. Grimes 		 * pad as required, cnt is number of bytes not written
5104b88c807SRodney W. Grimes 		 */
5114b88c807SRodney W. Grimes 		if (((cnt > 0) && (wr_skip(cnt) < 0)) ||
5124b88c807SRodney W. Grimes 		    ((arcn->pad > 0) && (wr_skip(arcn->pad) < 0)))
5134b88c807SRodney W. Grimes 			break;
5144b88c807SRodney W. Grimes 	}
5154b88c807SRodney W. Grimes 
5164b88c807SRodney W. Grimes 	/*
51746be34b9SKris Kennaway 	 * tell format to write trailer; pad to block boundary; reset directory
5184b88c807SRodney W. Grimes 	 * mode/access times, and check if all patterns supplied by the user
5194b88c807SRodney W. Grimes 	 * were matched. block off signals to avoid chance for multiple entry
5204b88c807SRodney W. Grimes 	 * into the cleanup code
5214b88c807SRodney W. Grimes 	 */
5224b88c807SRodney W. Grimes 	if (wr_one) {
5234b88c807SRodney W. Grimes 		(*frmt->end_wr)();
5244b88c807SRodney W. Grimes 		wr_fin();
5254b88c807SRodney W. Grimes 	}
526778766feSKris Kennaway 	(void)sigprocmask(SIG_BLOCK, &s_mask, NULL);
5274b88c807SRodney W. Grimes 	ar_close();
5284b88c807SRodney W. Grimes 	if (tflag)
5294b88c807SRodney W. Grimes 		proc_dir();
5304b88c807SRodney W. Grimes 	ftree_chk();
5314b88c807SRodney W. Grimes }
5324b88c807SRodney W. Grimes 
5334b88c807SRodney W. Grimes /*
5344b88c807SRodney W. Grimes  * append()
5354b88c807SRodney W. Grimes  *	Add file to previously written archive. Archive format specified by the
5364b88c807SRodney W. Grimes  *	user must agree with archive. The archive is read first to collect
5374b88c807SRodney W. Grimes  *	modification times (if -u) and locate the archive trailer. The archive
5384b88c807SRodney W. Grimes  *	is positioned in front of the record with the trailer and wr_archive()
5394b88c807SRodney W. Grimes  *	is called to add the new members.
5404b88c807SRodney W. Grimes  *	PAX IMPLEMENTATION DETAIL NOTE:
5414b88c807SRodney W. Grimes  *	-u is implemented by adding the new members to the end of the archive.
5424b88c807SRodney W. Grimes  *	Care is taken so that these do not end up as links to the older
5434b88c807SRodney W. Grimes  *	version of the same file already stored in the archive. It is expected
5444b88c807SRodney W. Grimes  *	when extraction occurs these newer versions will over-write the older
5454b88c807SRodney W. Grimes  *	ones stored "earlier" in the archive (this may be a bad assumption as
5464b88c807SRodney W. Grimes  *	it depends on the implementation of the program doing the extraction).
5474b88c807SRodney W. Grimes  *	It is really difficult to splice in members without either re-writing
5484b88c807SRodney W. Grimes  *	the entire archive (from the point were the old version was), or having
5494b88c807SRodney W. Grimes  *	assistance of the format specification in terms of a special update
55046be34b9SKris Kennaway  *	header that invalidates a previous archive record. The POSIX spec left
5514b88c807SRodney W. Grimes  *	the method used to implement -u unspecified. This pax is able to
5524b88c807SRodney W. Grimes  *	over write existing files that it creates.
5534b88c807SRodney W. Grimes  */
5544b88c807SRodney W. Grimes 
5554b88c807SRodney W. Grimes void
append(void)5564b88c807SRodney W. Grimes append(void)
5574b88c807SRodney W. Grimes {
558f789b261SWarner Losh 	ARCHD *arcn;
559f789b261SWarner Losh 	int res;
5604b88c807SRodney W. Grimes 	ARCHD archd;
5614b88c807SRodney W. Grimes 	FSUB *orgfrmt;
5624b88c807SRodney W. Grimes 	int udev;
5634b88c807SRodney W. Grimes 	off_t tlen;
5644b88c807SRodney W. Grimes 
5654b88c807SRodney W. Grimes 	arcn = &archd;
5664b88c807SRodney W. Grimes 	orgfrmt = frmt;
5674b88c807SRodney W. Grimes 
5684b88c807SRodney W. Grimes 	/*
5694b88c807SRodney W. Grimes 	 * Do not allow an append operation if the actual archive is of a
57046be34b9SKris Kennaway 	 * different format than the user specified format.
5714b88c807SRodney W. Grimes 	 */
5724b88c807SRodney W. Grimes 	if (get_arc() < 0)
5734b88c807SRodney W. Grimes 		return;
5744b88c807SRodney W. Grimes 	if ((orgfrmt != NULL) && (orgfrmt != frmt)) {
575778766feSKris Kennaway 		paxwarn(1, "Cannot mix current archive format %s with %s",
5764b88c807SRodney W. Grimes 		    frmt->name, orgfrmt->name);
5774b88c807SRodney W. Grimes 		return;
5784b88c807SRodney W. Grimes 	}
5794b88c807SRodney W. Grimes 
5804b88c807SRodney W. Grimes 	/*
5814b88c807SRodney W. Grimes 	 * pass the format any options and start up format
5824b88c807SRodney W. Grimes 	 */
5834b88c807SRodney W. Grimes 	if (((*frmt->options)() < 0) || ((*frmt->st_rd)() < 0))
5844b88c807SRodney W. Grimes 		return;
5854b88c807SRodney W. Grimes 
5864b88c807SRodney W. Grimes 	/*
5874b88c807SRodney W. Grimes 	 * if we only are adding members that are newer, we need to save the
5884b88c807SRodney W. Grimes 	 * mod times for all files we see.
5894b88c807SRodney W. Grimes 	 */
5904b88c807SRodney W. Grimes 	if (uflag && (ftime_start() < 0))
5914b88c807SRodney W. Grimes 		return;
5924b88c807SRodney W. Grimes 
5934b88c807SRodney W. Grimes 	/*
5944b88c807SRodney W. Grimes 	 * some archive formats encode hard links by recording the device and
5954b88c807SRodney W. Grimes 	 * file serial number (inode) but copy the file anyway (multiple times)
5964b88c807SRodney W. Grimes 	 * to the archive. When we append, we run the risk that newly added
5974b88c807SRodney W. Grimes 	 * files may have the same device and inode numbers as those recorded
5984b88c807SRodney W. Grimes 	 * on the archive but during a previous run. If this happens, when the
5994b88c807SRodney W. Grimes 	 * archive is extracted we get INCORRECT hard links. We avoid this by
6004b88c807SRodney W. Grimes 	 * remapping the device numbers so that newly added files will never
6014b88c807SRodney W. Grimes 	 * use the same device number as one found on the archive. remapping
6024b88c807SRodney W. Grimes 	 * allows new members to safely have links among themselves. remapping
6034b88c807SRodney W. Grimes 	 * also avoids problems with file inode (serial number) truncations
6044b88c807SRodney W. Grimes 	 * when the inode number is larger than storage space in the archive
6054b88c807SRodney W. Grimes 	 * header. See the remap routines for more details.
6064b88c807SRodney W. Grimes 	 */
6074b88c807SRodney W. Grimes 	if ((udev = frmt->udev) && (dev_start() < 0))
6084b88c807SRodney W. Grimes 		return;
6094b88c807SRodney W. Grimes 
6104b88c807SRodney W. Grimes 	/*
6114b88c807SRodney W. Grimes 	 * reading the archive may take a long time. If verbose tell the user
6124b88c807SRodney W. Grimes 	 */
6134b88c807SRodney W. Grimes 	if (vflag) {
614b1787decSKris Kennaway 		(void)fprintf(listf,
6154b88c807SRodney W. Grimes 			"%s: Reading archive to position at the end...", argv0);
6164b88c807SRodney W. Grimes 		vfpart = 1;
6174b88c807SRodney W. Grimes 	}
6184b88c807SRodney W. Grimes 
6194b88c807SRodney W. Grimes 	/*
6204b88c807SRodney W. Grimes 	 * step through the archive until the format says it is done
6214b88c807SRodney W. Grimes 	 */
6224b88c807SRodney W. Grimes 	while (next_head(arcn) == 0) {
6234b88c807SRodney W. Grimes 		/*
6244b88c807SRodney W. Grimes 		 * check if this file meets user specified options.
6254b88c807SRodney W. Grimes 		 */
6264b88c807SRodney W. Grimes 		if (sel_chk(arcn) != 0) {
6274b88c807SRodney W. Grimes 			if (rd_skip(arcn->skip + arcn->pad) == 1)
6284b88c807SRodney W. Grimes 				break;
6294b88c807SRodney W. Grimes 			continue;
6304b88c807SRodney W. Grimes 		}
6314b88c807SRodney W. Grimes 
6324b88c807SRodney W. Grimes 		if (uflag) {
6334b88c807SRodney W. Grimes 			/*
6344b88c807SRodney W. Grimes 			 * see if this is the newest version of this file has
6354b88c807SRodney W. Grimes 			 * already been seen, if so skip.
6364b88c807SRodney W. Grimes 			 */
6374b88c807SRodney W. Grimes 			if ((res = chk_ftime(arcn)) < 0)
6384b88c807SRodney W. Grimes 				break;
6394b88c807SRodney W. Grimes 			if (res > 0) {
6404b88c807SRodney W. Grimes 				if (rd_skip(arcn->skip + arcn->pad) == 1)
6414b88c807SRodney W. Grimes 					break;
6424b88c807SRodney W. Grimes 				continue;
6434b88c807SRodney W. Grimes 			}
6444b88c807SRodney W. Grimes 		}
6454b88c807SRodney W. Grimes 
6464b88c807SRodney W. Grimes 		/*
6474b88c807SRodney W. Grimes 		 * Store this device number. Device numbers seen during the
6484b88c807SRodney W. Grimes 		 * read phase of append will cause newly appended files with a
6494b88c807SRodney W. Grimes 		 * device number seen in the old part of the archive to be
6504b88c807SRodney W. Grimes 		 * remapped to an unused device number.
6514b88c807SRodney W. Grimes 		 */
6524b88c807SRodney W. Grimes 		if ((udev && (add_dev(arcn) < 0)) ||
6534b88c807SRodney W. Grimes 		    (rd_skip(arcn->skip + arcn->pad) == 1))
6544b88c807SRodney W. Grimes 			break;
6554b88c807SRodney W. Grimes 	}
6564b88c807SRodney W. Grimes 
6574b88c807SRodney W. Grimes 	/*
6584b88c807SRodney W. Grimes 	 * done, finish up read and get the number of bytes to back up so we
6594b88c807SRodney W. Grimes 	 * can add new members. The format might have used the hard link table,
6604b88c807SRodney W. Grimes 	 * purge it.
6614b88c807SRodney W. Grimes 	 */
6624b88c807SRodney W. Grimes 	tlen = (*frmt->end_rd)();
6634b88c807SRodney W. Grimes 	lnk_end();
6644b88c807SRodney W. Grimes 
6654b88c807SRodney W. Grimes 	/*
66646be34b9SKris Kennaway 	 * try to position for write, if this fails quit. if any error occurs,
6674b88c807SRodney W. Grimes 	 * we will refuse to write
6684b88c807SRodney W. Grimes 	 */
6694b88c807SRodney W. Grimes 	if (appnd_start(tlen) < 0)
6704b88c807SRodney W. Grimes 		return;
6714b88c807SRodney W. Grimes 
6724b88c807SRodney W. Grimes 	/*
6734b88c807SRodney W. Grimes 	 * tell the user we are done reading.
6744b88c807SRodney W. Grimes 	 */
6754b88c807SRodney W. Grimes 	if (vflag && vfpart) {
676b1787decSKris Kennaway 		(void)fputs("done.\n", listf);
6774b88c807SRodney W. Grimes 		vfpart = 0;
6784b88c807SRodney W. Grimes 	}
6794b88c807SRodney W. Grimes 
6804b88c807SRodney W. Grimes 	/*
6814b88c807SRodney W. Grimes 	 * go to the writing phase to add the new members
6824b88c807SRodney W. Grimes 	 */
6834b88c807SRodney W. Grimes 	wr_archive(arcn, 1);
6844b88c807SRodney W. Grimes }
6854b88c807SRodney W. Grimes 
6864b88c807SRodney W. Grimes /*
6874b88c807SRodney W. Grimes  * archive()
6884b88c807SRodney W. Grimes  *	write a new archive
6894b88c807SRodney W. Grimes  */
6904b88c807SRodney W. Grimes 
6914b88c807SRodney W. Grimes void
archive(void)6924b88c807SRodney W. Grimes archive(void)
6934b88c807SRodney W. Grimes {
6944b88c807SRodney W. Grimes 	ARCHD archd;
6954b88c807SRodney W. Grimes 
6964b88c807SRodney W. Grimes 	/*
6974b88c807SRodney W. Grimes 	 * if we only are adding members that are newer, we need to save the
6984b88c807SRodney W. Grimes 	 * mod times for all files; set up for writing; pass the format any
6994b88c807SRodney W. Grimes 	 * options write the archive
7004b88c807SRodney W. Grimes 	 */
7014b88c807SRodney W. Grimes 	if ((uflag && (ftime_start() < 0)) || (wr_start() < 0))
7024b88c807SRodney W. Grimes 		return;
7034b88c807SRodney W. Grimes 	if ((*frmt->options)() < 0)
7044b88c807SRodney W. Grimes 		return;
7054b88c807SRodney W. Grimes 
7064b88c807SRodney W. Grimes 	wr_archive(&archd, 0);
7074b88c807SRodney W. Grimes }
7084b88c807SRodney W. Grimes 
7094b88c807SRodney W. Grimes /*
7104b88c807SRodney W. Grimes  * copy()
7114b88c807SRodney W. Grimes  *	copy files from one part of the file system to another. this does not
7124b88c807SRodney W. Grimes  *	use any archive storage. The EFFECT OF THE COPY IS THE SAME as if an
7134b88c807SRodney W. Grimes  *	archive was written and then extracted in the destination directory
7144b88c807SRodney W. Grimes  *	(except the files are forced to be under the destination directory).
7154b88c807SRodney W. Grimes  */
7164b88c807SRodney W. Grimes 
7174b88c807SRodney W. Grimes void
copy(void)7184b88c807SRodney W. Grimes copy(void)
7194b88c807SRodney W. Grimes {
720f789b261SWarner Losh 	ARCHD *arcn;
721f789b261SWarner Losh 	int res;
722f789b261SWarner Losh 	int fddest;
723f789b261SWarner Losh 	char *dest_pt;
724f789b261SWarner Losh 	int dlen;
725f789b261SWarner Losh 	int drem;
7264b88c807SRodney W. Grimes 	int fdsrc = -1;
7274b88c807SRodney W. Grimes 	struct stat sb;
7284b88c807SRodney W. Grimes 	ARCHD archd;
7294b88c807SRodney W. Grimes 	char dirbuf[PAXPATHLEN+1];
7304b88c807SRodney W. Grimes 
7314b88c807SRodney W. Grimes 	arcn = &archd;
7324b88c807SRodney W. Grimes 	/*
7334b88c807SRodney W. Grimes 	 * set up the destination dir path and make sure it is a directory. We
7344b88c807SRodney W. Grimes 	 * make sure we have a trailing / on the destination
7354b88c807SRodney W. Grimes 	 */
7365b421cacSEivind Eklund 	dlen = l_strncpy(dirbuf, dirptr, sizeof(dirbuf) - 1);
7374b88c807SRodney W. Grimes 	dest_pt = dirbuf + dlen;
7384b88c807SRodney W. Grimes 	if (*(dest_pt-1) != '/') {
7394b88c807SRodney W. Grimes 		*dest_pt++ = '/';
7404b88c807SRodney W. Grimes 		++dlen;
7414b88c807SRodney W. Grimes 	}
7424b88c807SRodney W. Grimes 	*dest_pt = '\0';
7434b88c807SRodney W. Grimes 	drem = PAXPATHLEN - dlen;
7444b88c807SRodney W. Grimes 
7454b88c807SRodney W. Grimes 	if (stat(dirptr, &sb) < 0) {
746778766feSKris Kennaway 		syswarn(1, errno, "Cannot access destination directory %s",
7474b88c807SRodney W. Grimes 			dirptr);
7484b88c807SRodney W. Grimes 		return;
7494b88c807SRodney W. Grimes 	}
7504b88c807SRodney W. Grimes 	if (!S_ISDIR(sb.st_mode)) {
751778766feSKris Kennaway 		paxwarn(1, "Destination is not a directory %s", dirptr);
7524b88c807SRodney W. Grimes 		return;
7534b88c807SRodney W. Grimes 	}
7544b88c807SRodney W. Grimes 
7554b88c807SRodney W. Grimes 	/*
7564b88c807SRodney W. Grimes 	 * start up the hard link table; file traversal routines and the
7574b88c807SRodney W. Grimes 	 * modification time and access mode database
7584b88c807SRodney W. Grimes 	 */
7594b88c807SRodney W. Grimes 	if ((lnk_start() < 0) || (ftree_start() < 0) || (dir_start() < 0))
7604b88c807SRodney W. Grimes 		return;
7614b88c807SRodney W. Grimes 
7624b88c807SRodney W. Grimes 	/*
7634b88c807SRodney W. Grimes 	 * When we are doing interactive rename, we store the mapping of names
7644b88c807SRodney W. Grimes 	 * so we can fix up hard links files later in the archive.
7654b88c807SRodney W. Grimes 	 */
7664b88c807SRodney W. Grimes 	if (iflag && (name_start() < 0))
7674b88c807SRodney W. Grimes 		return;
7684b88c807SRodney W. Grimes 
7694b88c807SRodney W. Grimes 	/*
7704b88c807SRodney W. Grimes 	 * set up to cp file trees
7714b88c807SRodney W. Grimes 	 */
7724b88c807SRodney W. Grimes 	cp_start();
7734b88c807SRodney W. Grimes 
7744b88c807SRodney W. Grimes 	/*
7754b88c807SRodney W. Grimes 	 * while there are files to archive, process them
7764b88c807SRodney W. Grimes 	 */
7774b88c807SRodney W. Grimes 	while (next_file(arcn) == 0) {
7784b88c807SRodney W. Grimes 		fdsrc = -1;
7794b88c807SRodney W. Grimes 
7804b88c807SRodney W. Grimes 		/*
7814b88c807SRodney W. Grimes 		 * check if this file meets user specified options
7824b88c807SRodney W. Grimes 		 */
7830e32d64eSBrian Somers 		if (sel_chk(arcn) != 0) {
7840e32d64eSBrian Somers 			ftree_notsel();
7854b88c807SRodney W. Grimes 			continue;
7860e32d64eSBrian Somers 		}
7874b88c807SRodney W. Grimes 
7884b88c807SRodney W. Grimes 		/*
7894b88c807SRodney W. Grimes 		 * if there is already a file in the destination directory with
7904b88c807SRodney W. Grimes 		 * the same name and it is newer, skip the one stored on the
7914b88c807SRodney W. Grimes 		 * archive.
7924b88c807SRodney W. Grimes 		 * NOTE: this test is done BEFORE name modifications as
7934b88c807SRodney W. Grimes 		 * specified by pax. this can be confusing to the user who
7944b88c807SRodney W. Grimes 		 * might expect the test to be done on an existing file AFTER
7954b88c807SRodney W. Grimes 		 * the name mod. In honesty the pax spec is probably flawed in
7964b88c807SRodney W. Grimes 		 * this respect
7974b88c807SRodney W. Grimes 		 */
7984b88c807SRodney W. Grimes 		if (uflag || Dflag) {
7994b88c807SRodney W. Grimes 			/*
8004b88c807SRodney W. Grimes 			 * create the destination name
8014b88c807SRodney W. Grimes 			 */
8024b88c807SRodney W. Grimes 			if (*(arcn->name) == '/')
8034b88c807SRodney W. Grimes 				res = 1;
8044b88c807SRodney W. Grimes 			else
8054b88c807SRodney W. Grimes 				res = 0;
8064b88c807SRodney W. Grimes 			if ((arcn->nlen - res) > drem) {
807778766feSKris Kennaway 				paxwarn(1, "Destination pathname too long %s",
8084b88c807SRodney W. Grimes 					arcn->name);
8094b88c807SRodney W. Grimes 				continue;
8104b88c807SRodney W. Grimes 			}
8114b88c807SRodney W. Grimes 			(void)strncpy(dest_pt, arcn->name + res, drem);
8124b88c807SRodney W. Grimes 			dirbuf[PAXPATHLEN] = '\0';
8134b88c807SRodney W. Grimes 
8144b88c807SRodney W. Grimes 			/*
8154b88c807SRodney W. Grimes 			 * if existing file is same age or newer skip
8164b88c807SRodney W. Grimes 			 */
8174b88c807SRodney W. Grimes 			res = lstat(dirbuf, &sb);
8184b88c807SRodney W. Grimes 			*dest_pt = '\0';
8194b88c807SRodney W. Grimes 
8204b88c807SRodney W. Grimes 		    	if (res == 0) {
8214b88c807SRodney W. Grimes 				if (uflag && Dflag) {
8224b88c807SRodney W. Grimes 					if ((arcn->sb.st_mtime<=sb.st_mtime) &&
8234b88c807SRodney W. Grimes 			    		    (arcn->sb.st_ctime<=sb.st_ctime))
8244b88c807SRodney W. Grimes 						continue;
8254b88c807SRodney W. Grimes 				} else if (Dflag) {
8264b88c807SRodney W. Grimes 					if (arcn->sb.st_ctime <= sb.st_ctime)
8274b88c807SRodney W. Grimes 						continue;
8284b88c807SRodney W. Grimes 				} else if (arcn->sb.st_mtime <= sb.st_mtime)
8294b88c807SRodney W. Grimes 					continue;
8304b88c807SRodney W. Grimes 			}
8314b88c807SRodney W. Grimes 		}
8324b88c807SRodney W. Grimes 
8334b88c807SRodney W. Grimes 		/*
8344b88c807SRodney W. Grimes 		 * this file is considered selected. See if this is a hard link
8354b88c807SRodney W. Grimes 		 * to a previous file; modify the name as requested by the
8364b88c807SRodney W. Grimes 		 * user; set the final destination.
8374b88c807SRodney W. Grimes 		 */
8384b88c807SRodney W. Grimes 		ftree_sel(arcn);
8394b88c807SRodney W. Grimes 		if ((chk_lnk(arcn) < 0) || ((res = mod_name(arcn)) < 0))
8404b88c807SRodney W. Grimes 			break;
8414b88c807SRodney W. Grimes 		if ((res > 0) || (set_dest(arcn, dirbuf, dlen) < 0)) {
8424b88c807SRodney W. Grimes 			/*
8434b88c807SRodney W. Grimes 			 * skip file, purge from link table
8444b88c807SRodney W. Grimes 			 */
8454b88c807SRodney W. Grimes 			purg_lnk(arcn);
8464b88c807SRodney W. Grimes 			continue;
8474b88c807SRodney W. Grimes 		}
8484b88c807SRodney W. Grimes 
8494b88c807SRodney W. Grimes 		/*
85001c99176SUlrich Spörlein 		 * Non standard -Y and -Z flag. When the existing file is
8514b88c807SRodney W. Grimes 		 * same age or newer skip
8524b88c807SRodney W. Grimes 		 */
8534b88c807SRodney W. Grimes 		if ((Yflag || Zflag) && ((lstat(arcn->name, &sb) == 0))) {
8544b88c807SRodney W. Grimes 			if (Yflag && Zflag) {
8554b88c807SRodney W. Grimes 				if ((arcn->sb.st_mtime <= sb.st_mtime) &&
8564b88c807SRodney W. Grimes 				    (arcn->sb.st_ctime <= sb.st_ctime))
8574b88c807SRodney W. Grimes 					continue;
8584b88c807SRodney W. Grimes 			} else if (Yflag) {
8594b88c807SRodney W. Grimes 				if (arcn->sb.st_ctime <= sb.st_ctime)
8604b88c807SRodney W. Grimes 					continue;
8614b88c807SRodney W. Grimes 			} else if (arcn->sb.st_mtime <= sb.st_mtime)
8624b88c807SRodney W. Grimes 				continue;
8634b88c807SRodney W. Grimes 		}
8644b88c807SRodney W. Grimes 
8654b88c807SRodney W. Grimes 		if (vflag) {
866b1787decSKris Kennaway 			(void)fputs(arcn->name, listf);
8674b88c807SRodney W. Grimes 			vfpart = 1;
8684b88c807SRodney W. Grimes 		}
8694b88c807SRodney W. Grimes 		++flcnt;
8704b88c807SRodney W. Grimes 
8714b88c807SRodney W. Grimes 		/*
8724b88c807SRodney W. Grimes 		 * try to create a hard link to the src file if requested
8734b88c807SRodney W. Grimes 		 * but make sure we are not trying to overwrite ourselves.
8744b88c807SRodney W. Grimes 		 */
8754b88c807SRodney W. Grimes 		if (lflag)
8764b88c807SRodney W. Grimes 			res = cross_lnk(arcn);
8774b88c807SRodney W. Grimes 		else
8784b88c807SRodney W. Grimes 			res = chk_same(arcn);
8794b88c807SRodney W. Grimes 		if (res <= 0) {
8804b88c807SRodney W. Grimes 			if (vflag && vfpart) {
881b1787decSKris Kennaway 				(void)putc('\n', listf);
8824b88c807SRodney W. Grimes 				vfpart = 0;
8834b88c807SRodney W. Grimes 			}
8844b88c807SRodney W. Grimes 			continue;
8854b88c807SRodney W. Grimes 		}
8864b88c807SRodney W. Grimes 
8874b88c807SRodney W. Grimes 		/*
8884b88c807SRodney W. Grimes 		 * have to create a new file
8894b88c807SRodney W. Grimes 		 */
8904b88c807SRodney W. Grimes 		if ((arcn->type != PAX_REG) && (arcn->type != PAX_CTG)) {
8914b88c807SRodney W. Grimes 			/*
8924b88c807SRodney W. Grimes 			 * create a link or special file
8934b88c807SRodney W. Grimes 			 */
8944b88c807SRodney W. Grimes 			if ((arcn->type == PAX_HLK) || (arcn->type == PAX_HRG))
8954b88c807SRodney W. Grimes 				res = lnk_creat(arcn);
8964b88c807SRodney W. Grimes 			else
8974b88c807SRodney W. Grimes 				res = node_creat(arcn);
8984b88c807SRodney W. Grimes 			if (res < 0)
8994b88c807SRodney W. Grimes 				purg_lnk(arcn);
9004b88c807SRodney W. Grimes 			if (vflag && vfpart) {
901b1787decSKris Kennaway 				(void)putc('\n', listf);
9024b88c807SRodney W. Grimes 				vfpart = 0;
9034b88c807SRodney W. Grimes 			}
9044b88c807SRodney W. Grimes 			continue;
9054b88c807SRodney W. Grimes 		}
9064b88c807SRodney W. Grimes 
9074b88c807SRodney W. Grimes 		/*
9084b88c807SRodney W. Grimes 		 * have to copy a regular file to the destination directory.
9094b88c807SRodney W. Grimes 		 * first open source file and then create the destination file
9104b88c807SRodney W. Grimes 		 */
9114b88c807SRodney W. Grimes 		if ((fdsrc = open(arcn->org_name, O_RDONLY, 0)) < 0) {
912778766feSKris Kennaway 			syswarn(1, errno, "Unable to open %s to read",
9134b88c807SRodney W. Grimes 			    arcn->org_name);
9144b88c807SRodney W. Grimes 			purg_lnk(arcn);
9154b88c807SRodney W. Grimes 			continue;
9164b88c807SRodney W. Grimes 		}
9174b88c807SRodney W. Grimes 		if ((fddest = file_creat(arcn)) < 0) {
9184b88c807SRodney W. Grimes 			rdfile_close(arcn, &fdsrc);
9194b88c807SRodney W. Grimes 			purg_lnk(arcn);
9204b88c807SRodney W. Grimes 			continue;
9214b88c807SRodney W. Grimes 		}
9224b88c807SRodney W. Grimes 
9234b88c807SRodney W. Grimes 		/*
9244b88c807SRodney W. Grimes 		 * copy source file data to the destination file
9254b88c807SRodney W. Grimes 		 */
9264b88c807SRodney W. Grimes 		cp_file(arcn, fdsrc, fddest);
9274b88c807SRodney W. Grimes 		file_close(arcn, fddest);
9284b88c807SRodney W. Grimes 		rdfile_close(arcn, &fdsrc);
9294b88c807SRodney W. Grimes 
9304b88c807SRodney W. Grimes 		if (vflag && vfpart) {
931b1787decSKris Kennaway 			(void)putc('\n', listf);
9324b88c807SRodney W. Grimes 			vfpart = 0;
9334b88c807SRodney W. Grimes 		}
9344b88c807SRodney W. Grimes 	}
9354b88c807SRodney W. Grimes 
9364b88c807SRodney W. Grimes 	/*
9374b88c807SRodney W. Grimes 	 * restore directory modes and times as required; make sure all
9384b88c807SRodney W. Grimes 	 * patterns were selected block off signals to avoid chance for
9394b88c807SRodney W. Grimes 	 * multiple entry into the cleanup code.
9404b88c807SRodney W. Grimes 	 */
941778766feSKris Kennaway 	(void)sigprocmask(SIG_BLOCK, &s_mask, NULL);
9424b88c807SRodney W. Grimes 	ar_close();
9434b88c807SRodney W. Grimes 	proc_dir();
9444b88c807SRodney W. Grimes 	ftree_chk();
9454b88c807SRodney W. Grimes }
9464b88c807SRodney W. Grimes 
9474b88c807SRodney W. Grimes /*
9484b88c807SRodney W. Grimes  * next_head()
9494b88c807SRodney W. Grimes  *	try to find a valid header in the archive. Uses format specific
9504b88c807SRodney W. Grimes  *	routines to extract the header and id the trailer. Trailers may be
9514b88c807SRodney W. Grimes  *	located within a valid header or in an invalid header (the location
9524b88c807SRodney W. Grimes  *	is format specific. The inhead field from the option table tells us
9534b88c807SRodney W. Grimes  *	where to look for the trailer).
9544b88c807SRodney W. Grimes  *	We keep reading (and resyncing) until we get enough contiguous data
9554b88c807SRodney W. Grimes  *	to check for a header. If we cannot find one, we shift by a byte
9564b88c807SRodney W. Grimes  *	add a new byte from the archive to the end of the buffer and try again.
9574b88c807SRodney W. Grimes  *	If we get a read error, we throw out what we have (as we must have
9584b88c807SRodney W. Grimes  *	contiguous data) and start over again.
9594b88c807SRodney W. Grimes  *	ASSUMED: headers fit within a BLKMULT header.
9604b88c807SRodney W. Grimes  * Return:
9614b88c807SRodney W. Grimes  *	0 if we got a header, -1 if we are unable to ever find another one
9624b88c807SRodney W. Grimes  *	(we reached the end of input, or we reached the limit on retries. see
9634b88c807SRodney W. Grimes  *	the specs for rd_wrbuf() for more details)
9644b88c807SRodney W. Grimes  */
9654b88c807SRodney W. Grimes 
9664b88c807SRodney W. Grimes static int
next_head(ARCHD * arcn)967f789b261SWarner Losh next_head(ARCHD *arcn)
9684b88c807SRodney W. Grimes {
969f789b261SWarner Losh 	int ret;
970f789b261SWarner Losh 	char *hdend;
971f789b261SWarner Losh 	int res;
972f789b261SWarner Losh 	int shftsz;
973f789b261SWarner Losh 	int hsz;
974f789b261SWarner Losh 	int in_resync = 0; 	/* set when we are in resync mode */
9754b88c807SRodney W. Grimes 	int cnt = 0;			/* counter for trailer function */
976b1787decSKris Kennaway 	int first = 1;			/* on 1st read, EOF isn't premature. */
9774b88c807SRodney W. Grimes 
9784b88c807SRodney W. Grimes 	/*
9794b88c807SRodney W. Grimes 	 * set up initial conditions, we want a whole frmt->hsz block as we
9804b88c807SRodney W. Grimes 	 * have no data yet.
9814b88c807SRodney W. Grimes 	 */
9824b88c807SRodney W. Grimes 	res = hsz = frmt->hsz;
9834b88c807SRodney W. Grimes 	hdend = hdbuf;
9844b88c807SRodney W. Grimes 	shftsz = hsz - 1;
9854b88c807SRodney W. Grimes 	for(;;) {
9864b88c807SRodney W. Grimes 		/*
9874b88c807SRodney W. Grimes 		 * keep looping until we get a contiguous FULL buffer
9884b88c807SRodney W. Grimes 		 * (frmt->hsz is the proper size)
9894b88c807SRodney W. Grimes 		 */
9904b88c807SRodney W. Grimes 		for (;;) {
9914b88c807SRodney W. Grimes 			if ((ret = rd_wrbuf(hdend, res)) == res)
9924b88c807SRodney W. Grimes 				break;
9934b88c807SRodney W. Grimes 
9944b88c807SRodney W. Grimes 			/*
995b1787decSKris Kennaway 			 * If we read 0 bytes (EOF) from an archive when we
996b1787decSKris Kennaway 			 * expect to find a header, we have stepped upon
997b1787decSKris Kennaway 			 * an archive without the customary block of zeroes
998b1787decSKris Kennaway 			 * end marker.  It's just stupid to error out on
999b1787decSKris Kennaway 			 * them, so exit gracefully.
1000b1787decSKris Kennaway 			 */
1001b1787decSKris Kennaway 			if (first && ret == 0)
1002b1787decSKris Kennaway 				return(-1);
1003b1787decSKris Kennaway 			first = 0;
1004b1787decSKris Kennaway 
1005b1787decSKris Kennaway 			/*
10064b88c807SRodney W. Grimes 			 * some kind of archive read problem, try to resync the
10074b88c807SRodney W. Grimes 			 * storage device, better give the user the bad news.
10084b88c807SRodney W. Grimes 			 */
10094b88c807SRodney W. Grimes 			if ((ret == 0) || (rd_sync() < 0)) {
1010778766feSKris Kennaway 				paxwarn(1,"Premature end of file on archive read");
10114b88c807SRodney W. Grimes 				return(-1);
10124b88c807SRodney W. Grimes 			}
10134b88c807SRodney W. Grimes 			if (!in_resync) {
10144b88c807SRodney W. Grimes 				if (act == APPND) {
1015778766feSKris Kennaway 					paxwarn(1,
10164b88c807SRodney W. Grimes 					  "Archive I/O error, cannot continue");
10174b88c807SRodney W. Grimes 					return(-1);
10184b88c807SRodney W. Grimes 				}
1019778766feSKris Kennaway 				paxwarn(1,"Archive I/O error. Trying to recover.");
10204b88c807SRodney W. Grimes 				++in_resync;
10214b88c807SRodney W. Grimes 			}
10224b88c807SRodney W. Grimes 
10234b88c807SRodney W. Grimes 			/*
10244b88c807SRodney W. Grimes 			 * oh well, throw it all out and start over
10254b88c807SRodney W. Grimes 			 */
10264b88c807SRodney W. Grimes 			res = hsz;
10274b88c807SRodney W. Grimes 			hdend = hdbuf;
10284b88c807SRodney W. Grimes 		}
10294b88c807SRodney W. Grimes 
10304b88c807SRodney W. Grimes 		/*
10314b88c807SRodney W. Grimes 		 * ok we have a contiguous buffer of the right size. Call the
10324b88c807SRodney W. Grimes 		 * format read routine. If this was not a valid header and this
10334b88c807SRodney W. Grimes 		 * format stores trailers outside of the header, call the
10344b88c807SRodney W. Grimes 		 * format specific trailer routine to check for a trailer. We
10354b88c807SRodney W. Grimes 		 * have to watch out that we do not mis-identify file data or
10364b88c807SRodney W. Grimes 		 * block padding as a header or trailer. Format specific
10374b88c807SRodney W. Grimes 		 * trailer functions must NOT check for the trailer while we
10384b88c807SRodney W. Grimes 		 * are running in resync mode. Some trailer functions may tell
10394b88c807SRodney W. Grimes 		 * us that this block cannot contain a valid header either, so
10404b88c807SRodney W. Grimes 		 * we then throw out the entire block and start over.
10414b88c807SRodney W. Grimes 		 */
10424b88c807SRodney W. Grimes 		if ((*frmt->rd)(arcn, hdbuf) == 0)
10434b88c807SRodney W. Grimes 			break;
10444b88c807SRodney W. Grimes 
10454b88c807SRodney W. Grimes 		if (!frmt->inhead) {
10464b88c807SRodney W. Grimes 			/*
10474b88c807SRodney W. Grimes 			 * this format has trailers outside of valid headers
10484b88c807SRodney W. Grimes 			 */
104940feca3aSMark Murray 			if ((ret = (*frmt->trail_tar)(hdbuf,in_resync,&cnt)) == 0){
10504b88c807SRodney W. Grimes 				/*
10514b88c807SRodney W. Grimes 				 * valid trailer found, drain input as required
10524b88c807SRodney W. Grimes 				 */
10534b88c807SRodney W. Grimes 				ar_drain();
10544b88c807SRodney W. Grimes 				return(-1);
10554b88c807SRodney W. Grimes 			}
10564b88c807SRodney W. Grimes 
10574b88c807SRodney W. Grimes 			if (ret == 1) {
10584b88c807SRodney W. Grimes 				/*
10594b88c807SRodney W. Grimes 				 * we are in resync and we were told to throw
10604b88c807SRodney W. Grimes 				 * the whole block out because none of the
10614b88c807SRodney W. Grimes 				 * bytes in this block can be used to form a
10624b88c807SRodney W. Grimes 				 * valid header
10634b88c807SRodney W. Grimes 				 */
10644b88c807SRodney W. Grimes 				res = hsz;
10654b88c807SRodney W. Grimes 				hdend = hdbuf;
10664b88c807SRodney W. Grimes 				continue;
10674b88c807SRodney W. Grimes 			}
10684b88c807SRodney W. Grimes 		}
10694b88c807SRodney W. Grimes 
10704b88c807SRodney W. Grimes 		/*
10714b88c807SRodney W. Grimes 		 * Brute force section.
10724b88c807SRodney W. Grimes 		 * not a valid header. We may be able to find a header yet. So
10734b88c807SRodney W. Grimes 		 * we shift over by one byte, and set up to read one byte at a
10744b88c807SRodney W. Grimes 		 * time from the archive and place it at the end of the buffer.
10754b88c807SRodney W. Grimes 		 * We will keep moving byte at a time until we find a header or
10764b88c807SRodney W. Grimes 		 * get a read error and have to start over.
10774b88c807SRodney W. Grimes 		 */
10784b88c807SRodney W. Grimes 		if (!in_resync) {
10794b88c807SRodney W. Grimes 			if (act == APPND) {
1080778766feSKris Kennaway 				paxwarn(1,"Unable to append, archive header flaw");
10814b88c807SRodney W. Grimes 				return(-1);
10824b88c807SRodney W. Grimes 			}
1083778766feSKris Kennaway 			paxwarn(1,"Invalid header, starting valid header search.");
10844b88c807SRodney W. Grimes 			++in_resync;
10854b88c807SRodney W. Grimes 		}
1086778766feSKris Kennaway 		memmove(hdbuf, hdbuf+1, shftsz);
10874b88c807SRodney W. Grimes 		res = 1;
10884b88c807SRodney W. Grimes 		hdend = hdbuf + shftsz;
10894b88c807SRodney W. Grimes 	}
10904b88c807SRodney W. Grimes 
10914b88c807SRodney W. Grimes 	/*
109201c99176SUlrich Spörlein 	 * ok got a valid header, check for trailer if format encodes it in
109340feca3aSMark Murray 	 * the header.
10944b88c807SRodney W. Grimes 	 */
109540feca3aSMark Murray 	if (frmt->inhead && ((*frmt->trail_cpio)(arcn) == 0)) {
10964b88c807SRodney W. Grimes 		/*
10974b88c807SRodney W. Grimes 		 * valid trailer found, drain input as required
10984b88c807SRodney W. Grimes 		 */
10994b88c807SRodney W. Grimes 		ar_drain();
11004b88c807SRodney W. Grimes 		return(-1);
11014b88c807SRodney W. Grimes 	}
11024b88c807SRodney W. Grimes 
11034b88c807SRodney W. Grimes 	++flcnt;
11044b88c807SRodney W. Grimes 	return(0);
11054b88c807SRodney W. Grimes }
11064b88c807SRodney W. Grimes 
11074b88c807SRodney W. Grimes /*
11084b88c807SRodney W. Grimes  * get_arc()
11094b88c807SRodney W. Grimes  *	Figure out what format an archive is. Handles archive with flaws by
11104b88c807SRodney W. Grimes  *	brute force searches for a legal header in any supported format. The
11114b88c807SRodney W. Grimes  *	format id routines have to be careful to NOT mis-identify a format.
11124b88c807SRodney W. Grimes  *	ASSUMED: headers fit within a BLKMULT header.
11134b88c807SRodney W. Grimes  * Return:
11144b88c807SRodney W. Grimes  *	0 if archive found -1 otherwise
11154b88c807SRodney W. Grimes  */
11164b88c807SRodney W. Grimes 
11174b88c807SRodney W. Grimes static int
get_arc(void)11184b88c807SRodney W. Grimes get_arc(void)
11194b88c807SRodney W. Grimes {
1120f789b261SWarner Losh 	int i;
1121f789b261SWarner Losh 	int hdsz = 0;
1122f789b261SWarner Losh 	int res;
1123f789b261SWarner Losh 	int minhd = BLKMULT;
11244b88c807SRodney W. Grimes 	char *hdend;
11254b88c807SRodney W. Grimes 	int notice = 0;
11264b88c807SRodney W. Grimes 
11274b88c807SRodney W. Grimes 	/*
11284b88c807SRodney W. Grimes 	 * find the smallest header size in all archive formats and then set up
11294b88c807SRodney W. Grimes 	 * to read the archive.
11304b88c807SRodney W. Grimes 	 */
11314b88c807SRodney W. Grimes 	for (i = 0; ford[i] >= 0; ++i) {
11324b88c807SRodney W. Grimes 		if (fsub[ford[i]].hsz < minhd)
11334b88c807SRodney W. Grimes 			minhd = fsub[ford[i]].hsz;
11344b88c807SRodney W. Grimes 	}
11354b88c807SRodney W. Grimes 	if (rd_start() < 0)
11364b88c807SRodney W. Grimes 		return(-1);
11374b88c807SRodney W. Grimes 	res = BLKMULT;
11384b88c807SRodney W. Grimes 	hdsz = 0;
11394b88c807SRodney W. Grimes 	hdend = hdbuf;
11404b88c807SRodney W. Grimes 	for(;;) {
11414b88c807SRodney W. Grimes 		for (;;) {
11424b88c807SRodney W. Grimes 			/*
11434b88c807SRodney W. Grimes 			 * fill the buffer with at least the smallest header
11444b88c807SRodney W. Grimes 			 */
11454b88c807SRodney W. Grimes 			i = rd_wrbuf(hdend, res);
11464b88c807SRodney W. Grimes 			if (i > 0)
11474b88c807SRodney W. Grimes 				hdsz += i;
11484b88c807SRodney W. Grimes 			if (hdsz >= minhd)
11494b88c807SRodney W. Grimes 				break;
11504b88c807SRodney W. Grimes 
11514b88c807SRodney W. Grimes 			/*
11524b88c807SRodney W. Grimes 			 * if we cannot recover from a read error quit
11534b88c807SRodney W. Grimes 			 */
11544b88c807SRodney W. Grimes 			if ((i == 0) || (rd_sync() < 0))
11554b88c807SRodney W. Grimes 				goto out;
11564b88c807SRodney W. Grimes 
11574b88c807SRodney W. Grimes 			/*
11584b88c807SRodney W. Grimes 			 * when we get an error none of the data we already
11594b88c807SRodney W. Grimes 			 * have can be used to create a legal header (we just
11604b88c807SRodney W. Grimes 			 * got an error in the middle), so we throw it all out
11614b88c807SRodney W. Grimes 			 * and refill the buffer with fresh data.
11624b88c807SRodney W. Grimes 			 */
11634b88c807SRodney W. Grimes 			res = BLKMULT;
11644b88c807SRodney W. Grimes 			hdsz = 0;
11654b88c807SRodney W. Grimes 			hdend = hdbuf;
11664b88c807SRodney W. Grimes 			if (!notice) {
11674b88c807SRodney W. Grimes 				if (act == APPND)
11684b88c807SRodney W. Grimes 					return(-1);
1169778766feSKris Kennaway 				paxwarn(1,"Cannot identify format. Searching...");
11704b88c807SRodney W. Grimes 				++notice;
11714b88c807SRodney W. Grimes 			}
11724b88c807SRodney W. Grimes 		}
11734b88c807SRodney W. Grimes 
11744b88c807SRodney W. Grimes 		/*
11754b88c807SRodney W. Grimes 		 * we have at least the size of the smallest header in any
11764b88c807SRodney W. Grimes 		 * archive format. Look to see if we have a match. The array
11774b88c807SRodney W. Grimes 		 * ford[] is used to specify the header id order to reduce the
11784b88c807SRodney W. Grimes 		 * chance of incorrectly id'ing a valid header (some formats
11794b88c807SRodney W. Grimes 		 * may be subsets of each other and the order would then be
11804b88c807SRodney W. Grimes 		 * important).
11814b88c807SRodney W. Grimes 		 */
11824b88c807SRodney W. Grimes 		for (i = 0; ford[i] >= 0; ++i) {
11834b88c807SRodney W. Grimes 			if ((*fsub[ford[i]].id)(hdbuf, hdsz) < 0)
11844b88c807SRodney W. Grimes 				continue;
11854b88c807SRodney W. Grimes 			frmt = &(fsub[ford[i]]);
11864b88c807SRodney W. Grimes 			/*
11874b88c807SRodney W. Grimes 			 * yuck, to avoid slow special case code in the extract
11884b88c807SRodney W. Grimes 			 * routines, just push this header back as if it was
11894b88c807SRodney W. Grimes 			 * not seen. We have left extra space at start of the
11904b88c807SRodney W. Grimes 			 * buffer for this purpose. This is a bit ugly, but
11914b88c807SRodney W. Grimes 			 * adding all the special case code is far worse.
11924b88c807SRodney W. Grimes 			 */
11934b88c807SRodney W. Grimes 			pback(hdbuf, hdsz);
11944b88c807SRodney W. Grimes 			return(0);
11954b88c807SRodney W. Grimes 		}
11964b88c807SRodney W. Grimes 
11974b88c807SRodney W. Grimes 		/*
11984b88c807SRodney W. Grimes 		 * We have a flawed archive, no match. we start searching, but
11994b88c807SRodney W. Grimes 		 * we never allow additions to flawed archives
12004b88c807SRodney W. Grimes 		 */
12014b88c807SRodney W. Grimes 		if (!notice) {
12024b88c807SRodney W. Grimes 			if (act == APPND)
12034b88c807SRodney W. Grimes 				return(-1);
1204778766feSKris Kennaway 			paxwarn(1, "Cannot identify format. Searching...");
12054b88c807SRodney W. Grimes 			++notice;
12064b88c807SRodney W. Grimes 		}
12074b88c807SRodney W. Grimes 
12084b88c807SRodney W. Grimes 		/*
12094b88c807SRodney W. Grimes 		 * brute force search for a header that we can id.
12104b88c807SRodney W. Grimes 		 * we shift through byte at a time. this is slow, but we cannot
12114b88c807SRodney W. Grimes 		 * determine the nature of the flaw in the archive in a
12124b88c807SRodney W. Grimes 		 * portable manner
12134b88c807SRodney W. Grimes 		 */
12144b88c807SRodney W. Grimes 		if (--hdsz > 0) {
1215778766feSKris Kennaway 			memmove(hdbuf, hdbuf+1, hdsz);
12164b88c807SRodney W. Grimes 			res = BLKMULT - hdsz;
12174b88c807SRodney W. Grimes 			hdend = hdbuf + hdsz;
12184b88c807SRodney W. Grimes 		} else {
12194b88c807SRodney W. Grimes 			res = BLKMULT;
12204b88c807SRodney W. Grimes 			hdend = hdbuf;
12214b88c807SRodney W. Grimes 			hdsz = 0;
12224b88c807SRodney W. Grimes 		}
12234b88c807SRodney W. Grimes 	}
12244b88c807SRodney W. Grimes 
12254b88c807SRodney W. Grimes     out:
12264b88c807SRodney W. Grimes 	/*
12274b88c807SRodney W. Grimes 	 * we cannot find a header, bow, apologize and quit
12284b88c807SRodney W. Grimes 	 */
1229778766feSKris Kennaway 	paxwarn(1, "Sorry, unable to determine archive format.");
12304b88c807SRodney W. Grimes 	return(-1);
12314b88c807SRodney W. Grimes }
1232