xref: /openbsd-src/bin/pax/ar_subs.c (revision c7d9714183d5a343cc1aeb9df7808b42296b4c5e)
1*c7d97141Sjca /*	$OpenBSD: ar_subs.c,v 1.53 2024/07/14 14:32:02 jca Exp $	*/
2df930be7Sderaadt /*	$NetBSD: ar_subs.c,v 1.5 1995/03/21 09:07:06 cgd Exp $	*/
3df930be7Sderaadt 
4df930be7Sderaadt /*-
5df930be7Sderaadt  * Copyright (c) 1992 Keith Muller.
6df930be7Sderaadt  * Copyright (c) 1992, 1993
7df930be7Sderaadt  *	The Regents of the University of California.  All rights reserved.
8df930be7Sderaadt  *
9df930be7Sderaadt  * This code is derived from software contributed to Berkeley by
10df930be7Sderaadt  * Keith Muller of the University of California, San Diego.
11df930be7Sderaadt  *
12df930be7Sderaadt  * Redistribution and use in source and binary forms, with or without
13df930be7Sderaadt  * modification, are permitted provided that the following conditions
14df930be7Sderaadt  * are met:
15df930be7Sderaadt  * 1. Redistributions of source code must retain the above copyright
16df930be7Sderaadt  *    notice, this list of conditions and the following disclaimer.
17df930be7Sderaadt  * 2. Redistributions in binary form must reproduce the above copyright
18df930be7Sderaadt  *    notice, this list of conditions and the following disclaimer in the
19df930be7Sderaadt  *    documentation and/or other materials provided with the distribution.
2029295d1cSmillert  * 3. Neither the name of the University nor the names of its contributors
21df930be7Sderaadt  *    may be used to endorse or promote products derived from this software
22df930be7Sderaadt  *    without specific prior written permission.
23df930be7Sderaadt  *
24df930be7Sderaadt  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25df930be7Sderaadt  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26df930be7Sderaadt  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27df930be7Sderaadt  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28df930be7Sderaadt  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29df930be7Sderaadt  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30df930be7Sderaadt  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31df930be7Sderaadt  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32df930be7Sderaadt  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33df930be7Sderaadt  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34df930be7Sderaadt  * SUCH DAMAGE.
35df930be7Sderaadt  */
36df930be7Sderaadt 
37df930be7Sderaadt #include <sys/types.h>
38df930be7Sderaadt #include <sys/stat.h>
39df930be7Sderaadt #include <errno.h>
405645e6d3Sguenther #include <fcntl.h>
415645e6d3Sguenther #include <signal.h>
425645e6d3Sguenther #include <stdio.h>
435645e6d3Sguenther #include <string.h>
443055081cSguenther #include <time.h>
45df930be7Sderaadt #include <unistd.h>
465645e6d3Sguenther 
47df930be7Sderaadt #include "pax.h"
48df930be7Sderaadt #include "extern.h"
49df930be7Sderaadt 
50be87792eSmillert static void wr_archive(ARCHD *, int is_app);
51c72b5b24Smillert static int get_arc(void);
52be87792eSmillert static int next_head(ARCHD *);
53df930be7Sderaadt extern sigset_t s_mask;
54df930be7Sderaadt 
55df930be7Sderaadt /*
56df930be7Sderaadt  * Routines which control the overall operation modes of pax as specified by
57df930be7Sderaadt  * the user: list, append, read ...
58df930be7Sderaadt  */
59df930be7Sderaadt 
60df930be7Sderaadt static char hdbuf[BLKMULT];		/* space for archive header on read */
61df930be7Sderaadt u_long flcnt;				/* number of files processed */
62df930be7Sderaadt 
63df930be7Sderaadt /*
64df930be7Sderaadt  * list()
65df930be7Sderaadt  *	list the contents of an archive which match user supplied pattern(s)
66df930be7Sderaadt  *	(no pattern matches all).
67df930be7Sderaadt  */
68df930be7Sderaadt 
69df930be7Sderaadt void
list(void)70df930be7Sderaadt list(void)
71df930be7Sderaadt {
72be87792eSmillert 	ARCHD *arcn;
73be87792eSmillert 	int res;
74df930be7Sderaadt 	ARCHD archd;
75df930be7Sderaadt 	time_t now;
76df930be7Sderaadt 
77df930be7Sderaadt 	arcn = &archd;
78df930be7Sderaadt 	/*
79df930be7Sderaadt 	 * figure out archive type; pass any format specific options to the
80df930be7Sderaadt 	 * archive option processing routine; call the format init routine. We
81df930be7Sderaadt 	 * also save current time for ls_list() so we do not make a system
82df930be7Sderaadt 	 * call for each file we need to print. If verbose (vflag) start up
83df930be7Sderaadt 	 * the name and group caches.
84df930be7Sderaadt 	 */
85df930be7Sderaadt 	if ((get_arc() < 0) || ((*frmt->options)() < 0) ||
86df930be7Sderaadt 	    ((*frmt->st_rd)() < 0))
87df930be7Sderaadt 		return;
88df930be7Sderaadt 
894bbb0cd1Skstailey 	now = time(NULL);
90df930be7Sderaadt 
91df930be7Sderaadt 	/*
92df930be7Sderaadt 	 * step through the archive until the format says it is done
93df930be7Sderaadt 	 */
94df930be7Sderaadt 	while (next_head(arcn) == 0) {
95da60b202Smillert 		if (arcn->type == PAX_GLL || arcn->type == PAX_GLF) {
96da60b202Smillert 			/*
97da60b202Smillert 			 * we need to read, to get the real filename
98da60b202Smillert 			 */
99da60b202Smillert 			off_t cnt;
1005647df80Sespie 			if (!rd_wrfile(arcn, arcn->type == PAX_GLF
101a79a0570Smillert 			    ? -1 : -2, &cnt))
102da60b202Smillert 				(void)rd_skip(cnt + arcn->pad);
103da60b202Smillert 			continue;
104da60b202Smillert 		}
105da60b202Smillert 
106df930be7Sderaadt 		/*
107df930be7Sderaadt 		 * check for pattern, and user specified options match.
108df930be7Sderaadt 		 * When all patterns are matched we are done.
109df930be7Sderaadt 		 */
110df930be7Sderaadt 		if ((res = pat_match(arcn)) < 0)
111df930be7Sderaadt 			break;
112df930be7Sderaadt 
113df930be7Sderaadt 		if ((res == 0) && (sel_chk(arcn) == 0)) {
114df930be7Sderaadt 			/*
115df930be7Sderaadt 			 * pattern resulted in a selected file
116df930be7Sderaadt 			 */
117df930be7Sderaadt 			if (pat_sel(arcn) < 0)
118df930be7Sderaadt 				break;
119df930be7Sderaadt 
120df930be7Sderaadt 			/*
121df930be7Sderaadt 			 * modify the name as requested by the user if name
122df930be7Sderaadt 			 * survives modification, do a listing of the file
123df930be7Sderaadt 			 */
124df930be7Sderaadt 			if ((res = mod_name(arcn)) < 0)
125df930be7Sderaadt 				break;
126df930be7Sderaadt 			if (res == 0)
127f2d26cdcSmillert 				ls_list(arcn, now, stdout);
128df930be7Sderaadt 		}
129df930be7Sderaadt 
130df930be7Sderaadt 		/*
131df930be7Sderaadt 		 * skip to next archive format header using values calculated
132df930be7Sderaadt 		 * by the format header read routine
133df930be7Sderaadt 		 */
134df930be7Sderaadt 		if (rd_skip(arcn->skip + arcn->pad) == 1)
135df930be7Sderaadt 			break;
136df930be7Sderaadt 	}
137df930be7Sderaadt 
138df930be7Sderaadt 	/*
139df930be7Sderaadt 	 * all done, let format have a chance to cleanup, and make sure that
140df930be7Sderaadt 	 * the patterns supplied by the user were all matched
141df930be7Sderaadt 	 */
142df930be7Sderaadt 	(void)(*frmt->end_rd)();
1434bbb0cd1Skstailey 	(void)sigprocmask(SIG_BLOCK, &s_mask, NULL);
1440a8bc8ffSguenther 	ar_close(0);
145df930be7Sderaadt 	pat_chk();
146df930be7Sderaadt }
147df930be7Sderaadt 
1488b72bc25Sguenther static int
cmp_file_times(int mtime_flag,int ctime_flag,ARCHD * arcn,const char * path)149bb7daa6dSguenther cmp_file_times(int mtime_flag, int ctime_flag, ARCHD *arcn, const char *path)
1508b72bc25Sguenther {
1518b72bc25Sguenther 	struct stat sb;
152bb7daa6dSguenther 	long res;
1538b72bc25Sguenther 
154bb7daa6dSguenther 	if (path == NULL)
155bb7daa6dSguenther 		path = arcn->name;
156bb7daa6dSguenther 	if (lstat(path, &sb) != 0)
1578b72bc25Sguenther 		return (0);
1588b72bc25Sguenther 
159bb7daa6dSguenther 	/*
160bb7daa6dSguenther 	 * The target (sb) mtime might be rounded down due to the limitations
161bb7daa6dSguenther 	 * of the FS it's on.  If it's strictly greater or we don't care about
162bb7daa6dSguenther 	 * mtime, then precision doesn't matter, so check those cases first.
163bb7daa6dSguenther 	 */
164bb7daa6dSguenther 	if (ctime_flag && mtime_flag) {
165bb7daa6dSguenther 		if (timespeccmp(&arcn->sb.st_mtim, &sb.st_mtim, <=))
166bb7daa6dSguenther 			return timespeccmp(&arcn->sb.st_ctim, &sb.st_ctim, <=);
167bb7daa6dSguenther 		if (!timespeccmp(&arcn->sb.st_ctim, &sb.st_ctim, <=))
168bb7daa6dSguenther 			return 0;
169bb7daa6dSguenther 		/* <= ctim, but >= mtim */
170bb7daa6dSguenther 	} else if (ctime_flag)
171bb7daa6dSguenther 		return timespeccmp(&arcn->sb.st_ctim, &sb.st_ctim, <=);
172bb7daa6dSguenther 	else if (timespeccmp(&arcn->sb.st_mtim, &sb.st_mtim, <=))
173bb7daa6dSguenther 		return 1;
174bb7daa6dSguenther 
175bb7daa6dSguenther 	/*
176bb7daa6dSguenther 	 * If we got here then the target arcn > sb for mtime *and* that's
177bb7daa6dSguenther 	 * the deciding factor.  Check whether they're equal after rounding
178bb7daa6dSguenther 	 * down the arcn mtime to the precision of the target path.
179bb7daa6dSguenther 	 */
180bb7daa6dSguenther 	res = pathconfat(AT_FDCWD, path, _PC_TIMESTAMP_RESOLUTION,
181bb7daa6dSguenther 	    AT_SYMLINK_NOFOLLOW);
182bb7daa6dSguenther 	if (res == -1)
183bb7daa6dSguenther 		return 0;
184bb7daa6dSguenther 
185bb7daa6dSguenther 	/* nanosecond resolution?  previous comparisons were accurate */
186bb7daa6dSguenther 	if (res == 1)
187bb7daa6dSguenther 		return 0;
188bb7daa6dSguenther 
189bb7daa6dSguenther 	/* common case: second accuracy */
190bb7daa6dSguenther 	if (res == 1000000000)
191bb7daa6dSguenther 		return arcn->sb.st_mtime <= sb.st_mtime;
192bb7daa6dSguenther 
193bb7daa6dSguenther 	if (res < 1000000000) {
194bb7daa6dSguenther 		struct timespec ts = arcn->sb.st_mtim;
195bb7daa6dSguenther 		ts.tv_nsec = (ts.tv_nsec / res) * res;
196bb7daa6dSguenther 		return timespeccmp(&ts, &sb.st_mtim, <=);
197bb7daa6dSguenther 	} else {
198bb7daa6dSguenther 		/* not a POSIX compliant FS */
199bb7daa6dSguenther 		res /= 1000000000;
200bb7daa6dSguenther 		return ((arcn->sb.st_mtime / res) * res) <= sb.st_mtime;
201bb7daa6dSguenther 		return arcn->sb.st_mtime <= ((sb.st_mtime / res) * res);
202bb7daa6dSguenther 	}
2038b72bc25Sguenther }
2048b72bc25Sguenther 
205df930be7Sderaadt /*
206df930be7Sderaadt  * extract()
207df930be7Sderaadt  *	extract the member(s) of an archive as specified by user supplied
208df930be7Sderaadt  *	pattern(s) (no patterns extracts all members)
209df930be7Sderaadt  */
210df930be7Sderaadt 
211df930be7Sderaadt void
extract(void)212df930be7Sderaadt extract(void)
213df930be7Sderaadt {
214be87792eSmillert 	ARCHD *arcn;
215be87792eSmillert 	int res;
216df930be7Sderaadt 	off_t cnt;
217df930be7Sderaadt 	ARCHD archd;
218df930be7Sderaadt 	int fd;
219f2d26cdcSmillert 	time_t now;
220df930be7Sderaadt 
2212dbd6dc5Sguenther 	sltab_start();
2222dbd6dc5Sguenther 
223df930be7Sderaadt 	arcn = &archd;
224df930be7Sderaadt 	/*
225df930be7Sderaadt 	 * figure out archive type; pass any format specific options to the
226df930be7Sderaadt 	 * archive option processing routine; call the format init routine;
227df930be7Sderaadt 	 * start up the directory modification time and access mode database
228df930be7Sderaadt 	 */
229df930be7Sderaadt 	if ((get_arc() < 0) || ((*frmt->options)() < 0) ||
230df930be7Sderaadt 	    ((*frmt->st_rd)() < 0) || (dir_start() < 0))
231df930be7Sderaadt 		return;
232df930be7Sderaadt 
233df930be7Sderaadt 	/*
234df930be7Sderaadt 	 * When we are doing interactive rename, we store the mapping of names
235df930be7Sderaadt 	 * so we can fix up hard links files later in the archive.
236df930be7Sderaadt 	 */
237df930be7Sderaadt 	if (iflag && (name_start() < 0))
238df930be7Sderaadt 		return;
239df930be7Sderaadt 
2404bbb0cd1Skstailey 	now = time(NULL);
241f2d26cdcSmillert 
242df930be7Sderaadt 	/*
243df930be7Sderaadt 	 * step through each entry on the archive until the format read routine
244df930be7Sderaadt 	 * says it is done
245df930be7Sderaadt 	 */
246df930be7Sderaadt 	while (next_head(arcn) == 0) {
247da60b202Smillert 		if (arcn->type == PAX_GLL || arcn->type == PAX_GLF) {
248da60b202Smillert 			/*
249da60b202Smillert 			 * we need to read, to get the real filename
250da60b202Smillert 			 */
2515647df80Sespie 			if (!rd_wrfile(arcn, arcn->type == PAX_GLF
252a79a0570Smillert 			    ? -1 : -2, &cnt))
253da60b202Smillert 				(void)rd_skip(cnt + arcn->pad);
254da60b202Smillert 			continue;
255da60b202Smillert 		}
256df930be7Sderaadt 
257df930be7Sderaadt 		/*
258df930be7Sderaadt 		 * check for pattern, and user specified options match. When
259df930be7Sderaadt 		 * all the patterns are matched we are done
260df930be7Sderaadt 		 */
261df930be7Sderaadt 		if ((res = pat_match(arcn)) < 0)
262df930be7Sderaadt 			break;
263df930be7Sderaadt 
264df930be7Sderaadt 		if ((res > 0) || (sel_chk(arcn) != 0)) {
265df930be7Sderaadt 			/*
266df930be7Sderaadt 			 * file is not selected. skip past any file data and
267df930be7Sderaadt 			 * padding and go back for the next archive member
268df930be7Sderaadt 			 */
269df930be7Sderaadt 			(void)rd_skip(arcn->skip + arcn->pad);
270df930be7Sderaadt 			continue;
271df930be7Sderaadt 		}
272df930be7Sderaadt 
273df930be7Sderaadt 		/*
274df930be7Sderaadt 		 * with -u or -D only extract when the archive member is newer
275f0f82a05Sjmc 		 * than the file with the same name in the file system (no
276df930be7Sderaadt 		 * test of being the same type is required).
277df930be7Sderaadt 		 * NOTE: this test is done BEFORE name modifications as
278df930be7Sderaadt 		 * specified by pax. this operation can be confusing to the
279df930be7Sderaadt 		 * user who might expect the test to be done on an existing
280df930be7Sderaadt 		 * file AFTER the name mod. In honesty the pax spec is probably
281df930be7Sderaadt 		 * flawed in this respect.
282df930be7Sderaadt 		 */
2838b72bc25Sguenther 		if ((uflag || Dflag) &&
2848b72bc25Sguenther 		    cmp_file_times(uflag, Dflag, arcn, NULL)) {
285df930be7Sderaadt 			(void)rd_skip(arcn->skip + arcn->pad);
286df930be7Sderaadt 			continue;
287df930be7Sderaadt 		}
288df930be7Sderaadt 
289df930be7Sderaadt 		/*
290df930be7Sderaadt 		 * this archive member is now been selected. modify the name.
291df930be7Sderaadt 		 */
292df930be7Sderaadt 		if ((pat_sel(arcn) < 0) || ((res = mod_name(arcn)) < 0))
293df930be7Sderaadt 			break;
294df930be7Sderaadt 		if (res > 0) {
295df930be7Sderaadt 			/*
296df930be7Sderaadt 			 * a bad name mod, skip and purge name from link table
297df930be7Sderaadt 			 */
298df930be7Sderaadt 			purg_lnk(arcn);
299df930be7Sderaadt 			(void)rd_skip(arcn->skip + arcn->pad);
300df930be7Sderaadt 			continue;
301df930be7Sderaadt 		}
302df930be7Sderaadt 
303df930be7Sderaadt 		/*
3044eb0b000Smillert 		 * Non standard -Y and -Z flag. When the existing file is
305df930be7Sderaadt 		 * same age or newer skip
306df930be7Sderaadt 		 */
3078b72bc25Sguenther 		if ((Yflag || Zflag) &&
3088b72bc25Sguenther 		    cmp_file_times(Yflag, Zflag, arcn, NULL)) {
309df930be7Sderaadt 			(void)rd_skip(arcn->skip + arcn->pad);
310df930be7Sderaadt 			continue;
311df930be7Sderaadt 		}
312df930be7Sderaadt 
313df930be7Sderaadt 		if (vflag) {
314f2d26cdcSmillert 			if (vflag > 1)
31510854502Smillert 				ls_list(arcn, now, listf);
316f2d26cdcSmillert 			else {
317c5738c9aSmillert 				(void)safe_print(arcn->name, listf);
318df930be7Sderaadt 				vfpart = 1;
319df930be7Sderaadt 			}
320f2d26cdcSmillert 		}
321df930be7Sderaadt 
322df930be7Sderaadt 		/*
3233c2af966Sdownsj 		 * if required, chdir around.
3243c2af966Sdownsj 		 */
325fd899314Smichaels 		if ((arcn->pat != NULL) && (arcn->pat->chdname != NULL))
326fd899314Smichaels 			if (chdir(arcn->pat->chdname) != 0)
3273c2af966Sdownsj 				syswarn(1, errno, "Cannot chdir to %s",
328fd899314Smichaels 				    arcn->pat->chdname);
3293c2af966Sdownsj 
3303c2af966Sdownsj 		/*
331df930be7Sderaadt 		 * all ok, extract this member based on type
332df930be7Sderaadt 		 */
3335316f7a4Sguenther 		if (!PAX_IS_REG(arcn->type)) {
334df930be7Sderaadt 			/*
335df930be7Sderaadt 			 * process archive members that are not regular files.
336df930be7Sderaadt 			 * throw out padding and any data that might follow the
337df930be7Sderaadt 			 * header (as determined by the format).
338df930be7Sderaadt 			 */
3395316f7a4Sguenther 			if (PAX_IS_HARDLINK(arcn->type))
340df930be7Sderaadt 				res = lnk_creat(arcn);
341df930be7Sderaadt 			else
342df930be7Sderaadt 				res = node_creat(arcn);
343df930be7Sderaadt 
344df930be7Sderaadt 			(void)rd_skip(arcn->skip + arcn->pad);
345df930be7Sderaadt 			if (res < 0)
346df930be7Sderaadt 				purg_lnk(arcn);
347df930be7Sderaadt 
348df930be7Sderaadt 			if (vflag && vfpart) {
34910854502Smillert 				(void)putc('\n', listf);
350df930be7Sderaadt 				vfpart = 0;
351df930be7Sderaadt 			}
352a7a4d07aSotto 			goto popd;
353df930be7Sderaadt 		}
354df930be7Sderaadt 		/*
355df930be7Sderaadt 		 * we have a file with data here. If we can not create it, skip
356df930be7Sderaadt 		 * over the data and purge the name from hard link table
357df930be7Sderaadt 		 */
358df930be7Sderaadt 		if ((fd = file_creat(arcn)) < 0) {
359df930be7Sderaadt 			(void)rd_skip(arcn->skip + arcn->pad);
360df930be7Sderaadt 			purg_lnk(arcn);
361a7a4d07aSotto 			goto popd;
362df930be7Sderaadt 		}
363df930be7Sderaadt 		/*
364df930be7Sderaadt 		 * extract the file from the archive and skip over padding and
365df930be7Sderaadt 		 * any unprocessed data
366df930be7Sderaadt 		 */
3675647df80Sespie 		res = rd_wrfile(arcn, fd, &cnt);
368df930be7Sderaadt 		file_close(arcn, fd);
369df930be7Sderaadt 		if (vflag && vfpart) {
37010854502Smillert 			(void)putc('\n', listf);
371df930be7Sderaadt 			vfpart = 0;
372df930be7Sderaadt 		}
373df930be7Sderaadt 		if (!res)
374df930be7Sderaadt 			(void)rd_skip(cnt + arcn->pad);
3753c2af966Sdownsj 
376a7a4d07aSotto popd:
3773c2af966Sdownsj 		/*
3783c2af966Sdownsj 		 * if required, chdir around.
3793c2af966Sdownsj 		 */
380fd899314Smichaels 		if ((arcn->pat != NULL) && (arcn->pat->chdname != NULL))
38170cbca63Smillert 			if (fchdir(cwdfd) != 0)
38270cbca63Smillert 				syswarn(1, errno,
38370cbca63Smillert 				    "Can't fchdir to starting directory");
384df930be7Sderaadt 	}
385df930be7Sderaadt 
386df930be7Sderaadt 	/*
387df930be7Sderaadt 	 * all done, restore directory modes and times as required; make sure
388df930be7Sderaadt 	 * all patterns supplied by the user were matched; block off signals
389df930be7Sderaadt 	 * to avoid chance for multiple entry into the cleanup code.
390df930be7Sderaadt 	 */
391df930be7Sderaadt 	(void)(*frmt->end_rd)();
3924bbb0cd1Skstailey 	(void)sigprocmask(SIG_BLOCK, &s_mask, NULL);
3930a8bc8ffSguenther 	ar_close(0);
3942dbd6dc5Sguenther 	sltab_process(0);
3950a8bc8ffSguenther 	proc_dir(0);
396df930be7Sderaadt 	pat_chk();
397df930be7Sderaadt }
398df930be7Sderaadt 
399df930be7Sderaadt /*
400df930be7Sderaadt  * wr_archive()
401df930be7Sderaadt  *	Write an archive. used in both creating a new archive and appends on
402df930be7Sderaadt  *	previously written archive.
403df930be7Sderaadt  */
404df930be7Sderaadt 
405df930be7Sderaadt static void
wr_archive(ARCHD * arcn,int is_app)406be87792eSmillert wr_archive(ARCHD *arcn, int is_app)
407df930be7Sderaadt {
408be87792eSmillert 	int res;
409be87792eSmillert 	int hlk;
410be87792eSmillert 	int wr_one;
411df930be7Sderaadt 	off_t cnt;
412cd90c754Sderaadt 	int (*wrf)(ARCHD *);
413df930be7Sderaadt 	int fd = -1;
414f2d26cdcSmillert 	time_t now;
415df930be7Sderaadt 
416df930be7Sderaadt 	/*
417df930be7Sderaadt 	 * if this format supports hard link storage, start up the database
418df930be7Sderaadt 	 * that detects them.
419df930be7Sderaadt 	 */
420df930be7Sderaadt 	if (((hlk = frmt->hlk) == 1) && (lnk_start() < 0))
421df930be7Sderaadt 		return;
422df930be7Sderaadt 
423df930be7Sderaadt 	/*
42419a32792Smarkus 	 * if this is not append, and there are no files, we do not write a
42519a32792Smarkus 	 * trailer
42619a32792Smarkus 	 */
42719a32792Smarkus 	wr_one = is_app;
42819a32792Smarkus 
42919a32792Smarkus 	/*
430df930be7Sderaadt 	 * start up the file traversal code and format specific write
431df930be7Sderaadt 	 */
43219a32792Smarkus 	if (ftree_start() < 0) {
43319a32792Smarkus 		if (is_app)
43419a32792Smarkus 			goto trailer;
435df930be7Sderaadt 		return;
43696eb9a8bSray 	} else if (((*frmt->st_wr)() < 0))
43796eb9a8bSray 		return;
43896eb9a8bSray 
439df930be7Sderaadt 	wrf = frmt->wr;
440df930be7Sderaadt 
441df930be7Sderaadt 	/*
442df930be7Sderaadt 	 * When we are doing interactive rename, we store the mapping of names
443df930be7Sderaadt 	 * so we can fix up hard links files later in the archive.
444df930be7Sderaadt 	 */
445df930be7Sderaadt 	if (iflag && (name_start() < 0))
446df930be7Sderaadt 		return;
447df930be7Sderaadt 
4484bbb0cd1Skstailey 	now = time(NULL);
449f2d26cdcSmillert 
450df930be7Sderaadt 	/*
451df930be7Sderaadt 	 * while there are files to archive, process them one at at time
452df930be7Sderaadt 	 */
453df930be7Sderaadt 	while (next_file(arcn) == 0) {
454df930be7Sderaadt 		/*
455df930be7Sderaadt 		 * check if this file meets user specified options match.
456df930be7Sderaadt 		 */
457df930be7Sderaadt 		if (sel_chk(arcn) != 0)
458df930be7Sderaadt 			continue;
459df930be7Sderaadt 		fd = -1;
460df930be7Sderaadt 		if (uflag) {
461df930be7Sderaadt 			/*
462df930be7Sderaadt 			 * only archive if this file is newer than a file with
463df930be7Sderaadt 			 * the same name that is already stored on the archive
464df930be7Sderaadt 			 */
465df930be7Sderaadt 			if ((res = chk_ftime(arcn)) < 0)
466df930be7Sderaadt 				break;
46785c9729bShenning 			if (res > 0) {
46885c9729bShenning 				ftree_skipped_newer(arcn);
469df930be7Sderaadt 				continue;
470df930be7Sderaadt 			}
47185c9729bShenning 		}
472df930be7Sderaadt 
473df930be7Sderaadt 		/*
474df930be7Sderaadt 		 * this file is considered selected now. see if this is a hard
475df930be7Sderaadt 		 * link to a file already stored
476df930be7Sderaadt 		 */
477df930be7Sderaadt 		ftree_sel(arcn);
478df930be7Sderaadt 		if (hlk && (chk_lnk(arcn) < 0))
479df930be7Sderaadt 			break;
480df930be7Sderaadt 
48107ca5729Sjeremy 		/*
48207ca5729Sjeremy 		 * Modify the name as requested by the user
48307ca5729Sjeremy 		 */
48407ca5729Sjeremy 		if ((res = mod_name(arcn)) < 0) {
48507ca5729Sjeremy 			/*
48607ca5729Sjeremy 			 * pax finished, purge link table entry and stop
48707ca5729Sjeremy 			 */
48807ca5729Sjeremy 			purg_lnk(arcn);
48907ca5729Sjeremy 			break;
49007ca5729Sjeremy 		} else if (res > 0) {
49107ca5729Sjeremy 			/*
49207ca5729Sjeremy 			 * skipping file, purge link table entry
49307ca5729Sjeremy 			 */
49407ca5729Sjeremy 			purg_lnk(arcn);
49507ca5729Sjeremy 			continue;
49607ca5729Sjeremy 		}
49707ca5729Sjeremy 
4985316f7a4Sguenther 		if (PAX_IS_REG(arcn->type) || (arcn->type == PAX_HRG)) {
499df930be7Sderaadt 			/*
500df930be7Sderaadt 			 * we will have to read this file. by opening it now we
501df930be7Sderaadt 			 * can avoid writing a header to the archive for a file
502df930be7Sderaadt 			 * we were later unable to read (we also purge it from
503df930be7Sderaadt 			 * the link table).
504df930be7Sderaadt 			 */
505b7041c07Sderaadt 			if ((fd = open(arcn->org_name, O_RDONLY)) < 0) {
506df930be7Sderaadt 				syswarn(1,errno, "Unable to open %s to read",
507df930be7Sderaadt 					arcn->org_name);
508df930be7Sderaadt 				purg_lnk(arcn);
509df930be7Sderaadt 				continue;
510df930be7Sderaadt 			}
511df930be7Sderaadt 		}
512df930be7Sderaadt 
51307ca5729Sjeremy 		if (docrc && (set_crc(arcn, fd) < 0)) {
514df930be7Sderaadt 			/*
515df930be7Sderaadt 			 * unable to obtain the crc we need, close the file,
516df930be7Sderaadt 			 * purge link table entry
517df930be7Sderaadt 			 */
518df930be7Sderaadt 			rdfile_close(arcn, &fd);
519df930be7Sderaadt 			purg_lnk(arcn);
520df930be7Sderaadt 			continue;
521df930be7Sderaadt 		}
522df930be7Sderaadt 
523df930be7Sderaadt 		if (vflag) {
524f2d26cdcSmillert 			if (vflag > 1)
52510854502Smillert 				ls_list(arcn, now, listf);
526f2d26cdcSmillert 			else {
527c5738c9aSmillert 				(void)safe_print(arcn->name, listf);
528df930be7Sderaadt 				vfpart = 1;
529df930be7Sderaadt 			}
530f2d26cdcSmillert 		}
531df930be7Sderaadt 		++flcnt;
532df930be7Sderaadt 
533df930be7Sderaadt 		/*
534df930be7Sderaadt 		 * looks safe to store the file, have the format specific
535df930be7Sderaadt 		 * routine write routine store the file header on the archive
536df930be7Sderaadt 		 */
537df930be7Sderaadt 		if ((res = (*wrf)(arcn)) < 0) {
538df930be7Sderaadt 			rdfile_close(arcn, &fd);
539df930be7Sderaadt 			break;
540df930be7Sderaadt 		}
541df930be7Sderaadt 		wr_one = 1;
542df930be7Sderaadt 		if (res > 0) {
543df930be7Sderaadt 			/*
544df930be7Sderaadt 			 * format write says no file data needs to be stored
545df930be7Sderaadt 			 * so we are done messing with this file
546df930be7Sderaadt 			 */
547df930be7Sderaadt 			if (vflag && vfpart) {
54810854502Smillert 				(void)putc('\n', listf);
549df930be7Sderaadt 				vfpart = 0;
550df930be7Sderaadt 			}
551df930be7Sderaadt 			rdfile_close(arcn, &fd);
552df930be7Sderaadt 			continue;
553df930be7Sderaadt 		}
554df930be7Sderaadt 
555df930be7Sderaadt 		/*
556df930be7Sderaadt 		 * Add file data to the archive, quit on write error. if we
557df930be7Sderaadt 		 * cannot write the entire file contents to the archive we
558df930be7Sderaadt 		 * must pad the archive to replace the missing file data
559df930be7Sderaadt 		 * (otherwise during an extract the file header for the file
560df930be7Sderaadt 		 * which FOLLOWS this one will not be where we expect it to
561df930be7Sderaadt 		 * be).
562df930be7Sderaadt 		 */
5635647df80Sespie 		res = wr_rdfile(arcn, fd, &cnt);
564df930be7Sderaadt 		rdfile_close(arcn, &fd);
565df930be7Sderaadt 		if (vflag && vfpart) {
56610854502Smillert 			(void)putc('\n', listf);
567df930be7Sderaadt 			vfpart = 0;
568df930be7Sderaadt 		}
569df930be7Sderaadt 		if (res < 0)
570df930be7Sderaadt 			break;
571df930be7Sderaadt 
572df930be7Sderaadt 		/*
573df930be7Sderaadt 		 * pad as required, cnt is number of bytes not written
574df930be7Sderaadt 		 */
575df930be7Sderaadt 		if (((cnt > 0) && (wr_skip(cnt) < 0)) ||
576df930be7Sderaadt 		    ((arcn->pad > 0) && (wr_skip(arcn->pad) < 0)))
577df930be7Sderaadt 			break;
578df930be7Sderaadt 	}
579df930be7Sderaadt 
58019a32792Smarkus trailer:
581df930be7Sderaadt 	/*
5824eb0b000Smillert 	 * tell format to write trailer; pad to block boundary; reset directory
583df930be7Sderaadt 	 * mode/access times, and check if all patterns supplied by the user
584df930be7Sderaadt 	 * were matched. block off signals to avoid chance for multiple entry
585df930be7Sderaadt 	 * into the cleanup code
586df930be7Sderaadt 	 */
587df930be7Sderaadt 	if (wr_one) {
588df930be7Sderaadt 		(*frmt->end_wr)();
589df930be7Sderaadt 		wr_fin();
590df930be7Sderaadt 	}
5914bbb0cd1Skstailey 	(void)sigprocmask(SIG_BLOCK, &s_mask, NULL);
5920a8bc8ffSguenther 	ar_close(0);
593df930be7Sderaadt 	if (tflag)
5940a8bc8ffSguenther 		proc_dir(0);
595df930be7Sderaadt 	ftree_chk();
596df930be7Sderaadt }
597df930be7Sderaadt 
598df930be7Sderaadt /*
599df930be7Sderaadt  * append()
600df930be7Sderaadt  *	Add file to previously written archive. Archive format specified by the
601df930be7Sderaadt  *	user must agree with archive. The archive is read first to collect
602df930be7Sderaadt  *	modification times (if -u) and locate the archive trailer. The archive
603df930be7Sderaadt  *	is positioned in front of the record with the trailer and wr_archive()
604df930be7Sderaadt  *	is called to add the new members.
605df930be7Sderaadt  *	PAX IMPLEMENTATION DETAIL NOTE:
606df930be7Sderaadt  *	-u is implemented by adding the new members to the end of the archive.
607df930be7Sderaadt  *	Care is taken so that these do not end up as links to the older
608df930be7Sderaadt  *	version of the same file already stored in the archive. It is expected
609df930be7Sderaadt  *	when extraction occurs these newer versions will over-write the older
610df930be7Sderaadt  *	ones stored "earlier" in the archive (this may be a bad assumption as
611df930be7Sderaadt  *	it depends on the implementation of the program doing the extraction).
612df930be7Sderaadt  *	It is really difficult to splice in members without either re-writing
613df930be7Sderaadt  *	the entire archive (from the point were the old version was), or having
614df930be7Sderaadt  *	assistance of the format specification in terms of a special update
615df930be7Sderaadt  *	header that invalidates a previous archive record. The posix spec left
616df930be7Sderaadt  *	the method used to implement -u unspecified. This pax is able to
617df930be7Sderaadt  *	over write existing files that it creates.
618df930be7Sderaadt  */
619df930be7Sderaadt 
620df930be7Sderaadt void
append(void)621df930be7Sderaadt append(void)
622df930be7Sderaadt {
623be87792eSmillert 	ARCHD *arcn;
624be87792eSmillert 	int res;
625df930be7Sderaadt 	ARCHD archd;
626df930be7Sderaadt 	FSUB *orgfrmt;
627df930be7Sderaadt 	int udev;
628df930be7Sderaadt 	off_t tlen;
629df930be7Sderaadt 
630df930be7Sderaadt 	arcn = &archd;
631df930be7Sderaadt 	orgfrmt = frmt;
632df930be7Sderaadt 
633df930be7Sderaadt 	/*
634df930be7Sderaadt 	 * Do not allow an append operation if the actual archive is of a
6354eb0b000Smillert 	 * different format than the user specified format.
636df930be7Sderaadt 	 */
637df930be7Sderaadt 	if (get_arc() < 0)
638df930be7Sderaadt 		return;
639df930be7Sderaadt 	if ((orgfrmt != NULL) && (orgfrmt != frmt)) {
64042cf9836Stholo 		paxwarn(1, "Cannot mix current archive format %s with %s",
641df930be7Sderaadt 		    frmt->name, orgfrmt->name);
642df930be7Sderaadt 		return;
643df930be7Sderaadt 	}
644df930be7Sderaadt 
645df930be7Sderaadt 	/*
646df930be7Sderaadt 	 * pass the format any options and start up format
647df930be7Sderaadt 	 */
648df930be7Sderaadt 	if (((*frmt->options)() < 0) || ((*frmt->st_rd)() < 0))
649df930be7Sderaadt 		return;
650df930be7Sderaadt 
651df930be7Sderaadt 	/*
652df930be7Sderaadt 	 * if we only are adding members that are newer, we need to save the
653df930be7Sderaadt 	 * mod times for all files we see.
654df930be7Sderaadt 	 */
655df930be7Sderaadt 	if (uflag && (ftime_start() < 0))
656df930be7Sderaadt 		return;
657df930be7Sderaadt 
658df930be7Sderaadt 	/*
659df930be7Sderaadt 	 * some archive formats encode hard links by recording the device and
660df930be7Sderaadt 	 * file serial number (inode) but copy the file anyway (multiple times)
661df930be7Sderaadt 	 * to the archive. When we append, we run the risk that newly added
662df930be7Sderaadt 	 * files may have the same device and inode numbers as those recorded
663df930be7Sderaadt 	 * on the archive but during a previous run. If this happens, when the
664df930be7Sderaadt 	 * archive is extracted we get INCORRECT hard links. We avoid this by
665df930be7Sderaadt 	 * remapping the device numbers so that newly added files will never
666df930be7Sderaadt 	 * use the same device number as one found on the archive. remapping
667df930be7Sderaadt 	 * allows new members to safely have links among themselves. remapping
668df930be7Sderaadt 	 * also avoids problems with file inode (serial number) truncations
669df930be7Sderaadt 	 * when the inode number is larger than storage space in the archive
670df930be7Sderaadt 	 * header. See the remap routines for more details.
671df930be7Sderaadt 	 */
672df930be7Sderaadt 	if ((udev = frmt->udev) && (dev_start() < 0))
673df930be7Sderaadt 		return;
674df930be7Sderaadt 
675df930be7Sderaadt 	/*
676df930be7Sderaadt 	 * reading the archive may take a long time. If verbose tell the user
677df930be7Sderaadt 	 */
678df930be7Sderaadt 	if (vflag) {
67910854502Smillert 		(void)fprintf(listf,
680df930be7Sderaadt 			"%s: Reading archive to position at the end...", argv0);
681df930be7Sderaadt 		vfpart = 1;
682df930be7Sderaadt 	}
683df930be7Sderaadt 
684df930be7Sderaadt 	/*
685df930be7Sderaadt 	 * step through the archive until the format says it is done
686df930be7Sderaadt 	 */
687df930be7Sderaadt 	while (next_head(arcn) == 0) {
688df930be7Sderaadt 		/*
689df930be7Sderaadt 		 * check if this file meets user specified options.
690df930be7Sderaadt 		 */
691df930be7Sderaadt 		if (sel_chk(arcn) != 0) {
692df930be7Sderaadt 			if (rd_skip(arcn->skip + arcn->pad) == 1)
693df930be7Sderaadt 				break;
694df930be7Sderaadt 			continue;
695df930be7Sderaadt 		}
696df930be7Sderaadt 
697df930be7Sderaadt 		if (uflag) {
698df930be7Sderaadt 			/*
699df930be7Sderaadt 			 * see if this is the newest version of this file has
700df930be7Sderaadt 			 * already been seen, if so skip.
701df930be7Sderaadt 			 */
702df930be7Sderaadt 			if ((res = chk_ftime(arcn)) < 0)
703df930be7Sderaadt 				break;
704df930be7Sderaadt 			if (res > 0) {
705df930be7Sderaadt 				if (rd_skip(arcn->skip + arcn->pad) == 1)
706df930be7Sderaadt 					break;
707df930be7Sderaadt 				continue;
708df930be7Sderaadt 			}
709df930be7Sderaadt 		}
710df930be7Sderaadt 
711df930be7Sderaadt 		/*
712df930be7Sderaadt 		 * Store this device number. Device numbers seen during the
713df930be7Sderaadt 		 * read phase of append will cause newly appended files with a
714df930be7Sderaadt 		 * device number seen in the old part of the archive to be
715df930be7Sderaadt 		 * remapped to an unused device number.
716df930be7Sderaadt 		 */
717df930be7Sderaadt 		if ((udev && (add_dev(arcn) < 0)) ||
718df930be7Sderaadt 		    (rd_skip(arcn->skip + arcn->pad) == 1))
719df930be7Sderaadt 			break;
720df930be7Sderaadt 	}
721df930be7Sderaadt 
722df930be7Sderaadt 	/*
723df930be7Sderaadt 	 * done, finish up read and get the number of bytes to back up so we
724df930be7Sderaadt 	 * can add new members. The format might have used the hard link table,
725df930be7Sderaadt 	 * purge it.
726df930be7Sderaadt 	 */
727df930be7Sderaadt 	tlen = (*frmt->end_rd)();
728df930be7Sderaadt 	lnk_end();
729df930be7Sderaadt 
730df930be7Sderaadt 	/*
7314eb0b000Smillert 	 * try to position for write, if this fails quit. if any error occurs,
732df930be7Sderaadt 	 * we will refuse to write
733df930be7Sderaadt 	 */
734df930be7Sderaadt 	if (appnd_start(tlen) < 0)
735df930be7Sderaadt 		return;
736df930be7Sderaadt 
737df930be7Sderaadt 	/*
738df930be7Sderaadt 	 * tell the user we are done reading.
739df930be7Sderaadt 	 */
740df930be7Sderaadt 	if (vflag && vfpart) {
74110854502Smillert 		(void)fputs("done.\n", listf);
742df930be7Sderaadt 		vfpart = 0;
743df930be7Sderaadt 	}
744df930be7Sderaadt 
745df930be7Sderaadt 	/*
746df930be7Sderaadt 	 * go to the writing phase to add the new members
747df930be7Sderaadt 	 */
748df930be7Sderaadt 	wr_archive(arcn, 1);
749df930be7Sderaadt }
750df930be7Sderaadt 
751df930be7Sderaadt /*
752df930be7Sderaadt  * archive()
753df930be7Sderaadt  *	write a new archive
754df930be7Sderaadt  */
755df930be7Sderaadt 
756df930be7Sderaadt void
archive(void)757df930be7Sderaadt archive(void)
758df930be7Sderaadt {
759df930be7Sderaadt 	ARCHD archd;
760df930be7Sderaadt 
761df930be7Sderaadt 	/*
762df930be7Sderaadt 	 * if we only are adding members that are newer, we need to save the
763df930be7Sderaadt 	 * mod times for all files; set up for writing; pass the format any
764df930be7Sderaadt 	 * options write the archive
765df930be7Sderaadt 	 */
766df930be7Sderaadt 	if ((uflag && (ftime_start() < 0)) || (wr_start() < 0))
767df930be7Sderaadt 		return;
768df930be7Sderaadt 	if ((*frmt->options)() < 0)
769df930be7Sderaadt 		return;
770df930be7Sderaadt 
771df930be7Sderaadt 	wr_archive(&archd, 0);
772df930be7Sderaadt }
773df930be7Sderaadt 
774df930be7Sderaadt /*
775df930be7Sderaadt  * copy()
776df930be7Sderaadt  *	copy files from one part of the file system to another. this does not
777df930be7Sderaadt  *	use any archive storage. The EFFECT OF THE COPY IS THE SAME as if an
778df930be7Sderaadt  *	archive was written and then extracted in the destination directory
779df930be7Sderaadt  *	(except the files are forced to be under the destination directory).
780df930be7Sderaadt  */
781df930be7Sderaadt 
782df930be7Sderaadt void
copy(void)783df930be7Sderaadt copy(void)
784df930be7Sderaadt {
785be87792eSmillert 	ARCHD *arcn;
786be87792eSmillert 	int res;
787be87792eSmillert 	int fddest;
788be87792eSmillert 	char *dest_pt;
78924c549d1Sguenther 	size_t dlen;
79024c549d1Sguenther 	size_t drem;
791df930be7Sderaadt 	int fdsrc = -1;
792df930be7Sderaadt 	struct stat sb;
793df930be7Sderaadt 	ARCHD archd;
794df930be7Sderaadt 	char dirbuf[PAXPATHLEN+1];
795df930be7Sderaadt 
7962dbd6dc5Sguenther 	sltab_start();
7972dbd6dc5Sguenther 
798df930be7Sderaadt 	arcn = &archd;
799df930be7Sderaadt 	/*
800df930be7Sderaadt 	 * set up the destination dir path and make sure it is a directory. We
801df930be7Sderaadt 	 * make sure we have a trailing / on the destination
802df930be7Sderaadt 	 */
8038f3d3452Smickey 	dlen = strlcpy(dirbuf, dirptr, sizeof(dirbuf));
8048f3d3452Smickey 	if (dlen >= sizeof(dirbuf) ||
8058f3d3452Smickey 	    (dlen == sizeof(dirbuf) - 1 && dirbuf[dlen - 1] != '/')) {
8068f3d3452Smickey 		paxwarn(1, "directory name is too long %s", dirptr);
8078f3d3452Smickey 		return;
8088f3d3452Smickey 	}
809df930be7Sderaadt 	dest_pt = dirbuf + dlen;
810df930be7Sderaadt 	if (*(dest_pt-1) != '/') {
811df930be7Sderaadt 		*dest_pt++ = '/';
8128f3d3452Smickey 		*dest_pt = '\0';
813df930be7Sderaadt 		++dlen;
814df930be7Sderaadt 	}
815df930be7Sderaadt 	drem = PAXPATHLEN - dlen;
816df930be7Sderaadt 
8173aaa63ebSderaadt 	if (stat(dirptr, &sb) == -1) {
818df930be7Sderaadt 		syswarn(1, errno, "Cannot access destination directory %s",
819df930be7Sderaadt 			dirptr);
820df930be7Sderaadt 		return;
821df930be7Sderaadt 	}
822df930be7Sderaadt 	if (!S_ISDIR(sb.st_mode)) {
82342cf9836Stholo 		paxwarn(1, "Destination is not a directory %s", dirptr);
824df930be7Sderaadt 		return;
825df930be7Sderaadt 	}
826df930be7Sderaadt 
827df930be7Sderaadt 	/*
828df930be7Sderaadt 	 * start up the hard link table; file traversal routines and the
829df930be7Sderaadt 	 * modification time and access mode database
830df930be7Sderaadt 	 */
831df930be7Sderaadt 	if ((lnk_start() < 0) || (ftree_start() < 0) || (dir_start() < 0))
832df930be7Sderaadt 		return;
833df930be7Sderaadt 
834df930be7Sderaadt 	/*
835df930be7Sderaadt 	 * When we are doing interactive rename, we store the mapping of names
836df930be7Sderaadt 	 * so we can fix up hard links files later in the archive.
837df930be7Sderaadt 	 */
838df930be7Sderaadt 	if (iflag && (name_start() < 0))
839df930be7Sderaadt 		return;
840df930be7Sderaadt 
841df930be7Sderaadt 	/*
842df930be7Sderaadt 	 * set up to cp file trees
843df930be7Sderaadt 	 */
844df930be7Sderaadt 	cp_start();
845df930be7Sderaadt 
846df930be7Sderaadt 	/*
847df930be7Sderaadt 	 * while there are files to archive, process them
848df930be7Sderaadt 	 */
849df930be7Sderaadt 	while (next_file(arcn) == 0) {
850df930be7Sderaadt 		fdsrc = -1;
851df930be7Sderaadt 
852df930be7Sderaadt 		/*
853df930be7Sderaadt 		 * check if this file meets user specified options
854df930be7Sderaadt 		 */
855df930be7Sderaadt 		if (sel_chk(arcn) != 0)
856df930be7Sderaadt 			continue;
857df930be7Sderaadt 
858df930be7Sderaadt 		/*
859df930be7Sderaadt 		 * if there is already a file in the destination directory with
860df930be7Sderaadt 		 * the same name and it is newer, skip the one stored on the
861df930be7Sderaadt 		 * archive.
862df930be7Sderaadt 		 * NOTE: this test is done BEFORE name modifications as
863df930be7Sderaadt 		 * specified by pax. this can be confusing to the user who
864df930be7Sderaadt 		 * might expect the test to be done on an existing file AFTER
865df930be7Sderaadt 		 * the name mod. In honesty the pax spec is probably flawed in
866df930be7Sderaadt 		 * this respect
867df930be7Sderaadt 		 */
868df930be7Sderaadt 		if (uflag || Dflag) {
869df930be7Sderaadt 			/*
870df930be7Sderaadt 			 * create the destination name
871df930be7Sderaadt 			 */
8728f3d3452Smickey 			if (strlcpy(dest_pt, arcn->name + (*arcn->name == '/'),
8738f3d3452Smickey 			    drem + 1) > drem) {
87442cf9836Stholo 				paxwarn(1, "Destination pathname too long %s",
875df930be7Sderaadt 					arcn->name);
876df930be7Sderaadt 				continue;
877df930be7Sderaadt 			}
878df930be7Sderaadt 
879df930be7Sderaadt 			/*
880df930be7Sderaadt 			 * if existing file is same age or newer skip
881df930be7Sderaadt 			 */
882bb7daa6dSguenther 			if (cmp_file_times(uflag, Dflag, arcn, dirbuf)) {
883df930be7Sderaadt 				*dest_pt = '\0';
88485c9729bShenning 				ftree_skipped_newer(arcn);
885df930be7Sderaadt 				continue;
886df930be7Sderaadt 			}
887bb7daa6dSguenther 			*dest_pt = '\0';
888df930be7Sderaadt 		}
889df930be7Sderaadt 
890df930be7Sderaadt 		/*
891df930be7Sderaadt 		 * this file is considered selected. See if this is a hard link
892df930be7Sderaadt 		 * to a previous file; modify the name as requested by the
893df930be7Sderaadt 		 * user; set the final destination.
894df930be7Sderaadt 		 */
895df930be7Sderaadt 		ftree_sel(arcn);
896df930be7Sderaadt 		if ((chk_lnk(arcn) < 0) || ((res = mod_name(arcn)) < 0))
897df930be7Sderaadt 			break;
898df930be7Sderaadt 		if ((res > 0) || (set_dest(arcn, dirbuf, dlen) < 0)) {
899df930be7Sderaadt 			/*
900df930be7Sderaadt 			 * skip file, purge from link table
901df930be7Sderaadt 			 */
902df930be7Sderaadt 			purg_lnk(arcn);
903df930be7Sderaadt 			continue;
904df930be7Sderaadt 		}
905df930be7Sderaadt 
906df930be7Sderaadt 		/*
9074eb0b000Smillert 		 * Non standard -Y and -Z flag. When the existing file is
908df930be7Sderaadt 		 * same age or newer skip
909df930be7Sderaadt 		 */
9108b72bc25Sguenther 		if ((Yflag || Zflag) &&
9118b72bc25Sguenther 		    cmp_file_times(Yflag, Zflag, arcn, NULL))
912df930be7Sderaadt 			continue;
913df930be7Sderaadt 
914df930be7Sderaadt 		if (vflag) {
915c5738c9aSmillert 			(void)safe_print(arcn->name, listf);
916df930be7Sderaadt 			vfpart = 1;
917df930be7Sderaadt 		}
918df930be7Sderaadt 		++flcnt;
919df930be7Sderaadt 
920df930be7Sderaadt 		/*
921df930be7Sderaadt 		 * try to create a hard link to the src file if requested
922df930be7Sderaadt 		 * but make sure we are not trying to overwrite ourselves.
923df930be7Sderaadt 		 */
924df930be7Sderaadt 		if (lflag)
925df930be7Sderaadt 			res = cross_lnk(arcn);
926df930be7Sderaadt 		else
927df930be7Sderaadt 			res = chk_same(arcn);
928df930be7Sderaadt 		if (res <= 0) {
929df930be7Sderaadt 			if (vflag && vfpart) {
93010854502Smillert 				(void)putc('\n', listf);
931df930be7Sderaadt 				vfpart = 0;
932df930be7Sderaadt 			}
933df930be7Sderaadt 			continue;
934df930be7Sderaadt 		}
935df930be7Sderaadt 
936df930be7Sderaadt 		/*
937df930be7Sderaadt 		 * have to create a new file
938df930be7Sderaadt 		 */
9395316f7a4Sguenther 		if (!PAX_IS_REG(arcn->type)) {
940df930be7Sderaadt 			/*
941df930be7Sderaadt 			 * create a link or special file
942df930be7Sderaadt 			 */
9435316f7a4Sguenther 			if (PAX_IS_HARDLINK(arcn->type))
944df930be7Sderaadt 				res = lnk_creat(arcn);
945df930be7Sderaadt 			else
946df930be7Sderaadt 				res = node_creat(arcn);
947df930be7Sderaadt 			if (res < 0)
948df930be7Sderaadt 				purg_lnk(arcn);
949df930be7Sderaadt 			if (vflag && vfpart) {
95010854502Smillert 				(void)putc('\n', listf);
951df930be7Sderaadt 				vfpart = 0;
952df930be7Sderaadt 			}
953df930be7Sderaadt 			continue;
954df930be7Sderaadt 		}
955df930be7Sderaadt 
956df930be7Sderaadt 		/*
957df930be7Sderaadt 		 * have to copy a regular file to the destination directory.
958df930be7Sderaadt 		 * first open source file and then create the destination file
959df930be7Sderaadt 		 */
960b7041c07Sderaadt 		if ((fdsrc = open(arcn->org_name, O_RDONLY)) < 0) {
961df930be7Sderaadt 			syswarn(1, errno, "Unable to open %s to read",
962df930be7Sderaadt 			    arcn->org_name);
963df930be7Sderaadt 			purg_lnk(arcn);
964df930be7Sderaadt 			continue;
965df930be7Sderaadt 		}
966df930be7Sderaadt 		if ((fddest = file_creat(arcn)) < 0) {
967df930be7Sderaadt 			rdfile_close(arcn, &fdsrc);
968df930be7Sderaadt 			purg_lnk(arcn);
969df930be7Sderaadt 			continue;
970df930be7Sderaadt 		}
971df930be7Sderaadt 
972df930be7Sderaadt 		/*
973df930be7Sderaadt 		 * copy source file data to the destination file
974df930be7Sderaadt 		 */
975df930be7Sderaadt 		cp_file(arcn, fdsrc, fddest);
976df930be7Sderaadt 		file_close(arcn, fddest);
977df930be7Sderaadt 		rdfile_close(arcn, &fdsrc);
978df930be7Sderaadt 
979df930be7Sderaadt 		if (vflag && vfpart) {
98010854502Smillert 			(void)putc('\n', listf);
981df930be7Sderaadt 			vfpart = 0;
982df930be7Sderaadt 		}
983df930be7Sderaadt 	}
984df930be7Sderaadt 
985df930be7Sderaadt 	/*
986df930be7Sderaadt 	 * restore directory modes and times as required; make sure all
987df930be7Sderaadt 	 * patterns were selected block off signals to avoid chance for
988df930be7Sderaadt 	 * multiple entry into the cleanup code.
989df930be7Sderaadt 	 */
9904bbb0cd1Skstailey 	(void)sigprocmask(SIG_BLOCK, &s_mask, NULL);
9910a8bc8ffSguenther 	ar_close(0);
9922dbd6dc5Sguenther 	sltab_process(0);
9930a8bc8ffSguenther 	proc_dir(0);
994df930be7Sderaadt 	ftree_chk();
995df930be7Sderaadt }
996df930be7Sderaadt 
997df930be7Sderaadt /*
998df930be7Sderaadt  * next_head()
999df930be7Sderaadt  *	try to find a valid header in the archive. Uses format specific
1000df930be7Sderaadt  *	routines to extract the header and id the trailer. Trailers may be
1001df930be7Sderaadt  *	located within a valid header or in an invalid header (the location
1002df930be7Sderaadt  *	is format specific. The inhead field from the option table tells us
1003df930be7Sderaadt  *	where to look for the trailer).
1004df930be7Sderaadt  *	We keep reading (and resyncing) until we get enough contiguous data
1005df930be7Sderaadt  *	to check for a header. If we cannot find one, we shift by a byte
1006df930be7Sderaadt  *	add a new byte from the archive to the end of the buffer and try again.
1007df930be7Sderaadt  *	If we get a read error, we throw out what we have (as we must have
1008df930be7Sderaadt  *	contiguous data) and start over again.
1009df930be7Sderaadt  *	ASSUMED: headers fit within a BLKMULT header.
1010df930be7Sderaadt  * Return:
1011df930be7Sderaadt  *	0 if we got a header, -1 if we are unable to ever find another one
1012df930be7Sderaadt  *	(we reached the end of input, or we reached the limit on retries. see
1013df930be7Sderaadt  *	the specs for rd_wrbuf() for more details)
1014df930be7Sderaadt  */
1015df930be7Sderaadt 
1016df930be7Sderaadt static int
next_head(ARCHD * arcn)1017be87792eSmillert next_head(ARCHD *arcn)
1018df930be7Sderaadt {
1019be87792eSmillert 	int ret;
1020be87792eSmillert 	char *hdend;
1021be87792eSmillert 	int res;
1022be87792eSmillert 	int shftsz;
1023be87792eSmillert 	int hsz;
1024be87792eSmillert 	int in_resync = 0;		/* set when we are in resync mode */
1025df930be7Sderaadt 	int cnt = 0;			/* counter for trailer function */
1026519defe3Sniklas 	int first = 1;			/* on 1st read, EOF isn't premature. */
1027df930be7Sderaadt 
1028df930be7Sderaadt 	/*
1029df930be7Sderaadt 	 * set up initial conditions, we want a whole frmt->hsz block as we
1030df930be7Sderaadt 	 * have no data yet.
1031df930be7Sderaadt 	 */
1032df930be7Sderaadt 	res = hsz = frmt->hsz;
1033df930be7Sderaadt 	hdend = hdbuf;
1034df930be7Sderaadt 	shftsz = hsz - 1;
1035df930be7Sderaadt 	for (;;) {
1036df930be7Sderaadt 		/*
1037df930be7Sderaadt 		 * keep looping until we get a contiguous FULL buffer
1038df930be7Sderaadt 		 * (frmt->hsz is the proper size)
1039df930be7Sderaadt 		 */
1040df930be7Sderaadt 		for (;;) {
1041df930be7Sderaadt 			if ((ret = rd_wrbuf(hdend, res)) == res)
1042df930be7Sderaadt 				break;
1043df930be7Sderaadt 
1044df930be7Sderaadt 			/*
1045519defe3Sniklas 			 * If we read 0 bytes (EOF) from an archive when we
1046519defe3Sniklas 			 * expect to find a header, we have stepped upon
1047519defe3Sniklas 			 * an archive without the customary block of zeroes
1048519defe3Sniklas 			 * end marker.  It's just stupid to error out on
1049519defe3Sniklas 			 * them, so exit gracefully.
1050519defe3Sniklas 			 */
1051519defe3Sniklas 			if (first && ret == 0)
1052519defe3Sniklas 				return(-1);
1053519defe3Sniklas 			first = 0;
1054519defe3Sniklas 
1055519defe3Sniklas 			/*
1056df930be7Sderaadt 			 * some kind of archive read problem, try to resync the
1057df930be7Sderaadt 			 * storage device, better give the user the bad news.
1058df930be7Sderaadt 			 */
1059df930be7Sderaadt 			if ((ret == 0) || (rd_sync() < 0)) {
106042cf9836Stholo 				paxwarn(1,"Premature end of file on archive read");
1061df930be7Sderaadt 				return(-1);
1062df930be7Sderaadt 			}
1063df930be7Sderaadt 			if (!in_resync) {
1064df930be7Sderaadt 				if (act == APPND) {
106542cf9836Stholo 					paxwarn(1,
1066df930be7Sderaadt 					  "Archive I/O error, cannot continue");
1067df930be7Sderaadt 					return(-1);
1068df930be7Sderaadt 				}
106942cf9836Stholo 				paxwarn(1,"Archive I/O error. Trying to recover.");
1070df930be7Sderaadt 				++in_resync;
1071df930be7Sderaadt 			}
1072df930be7Sderaadt 
1073df930be7Sderaadt 			/*
1074df930be7Sderaadt 			 * oh well, throw it all out and start over
1075df930be7Sderaadt 			 */
1076df930be7Sderaadt 			res = hsz;
1077df930be7Sderaadt 			hdend = hdbuf;
1078df930be7Sderaadt 		}
1079df930be7Sderaadt 
1080df930be7Sderaadt 		/*
1081df930be7Sderaadt 		 * ok we have a contiguous buffer of the right size. Call the
1082df930be7Sderaadt 		 * format read routine. If this was not a valid header and this
1083df930be7Sderaadt 		 * format stores trailers outside of the header, call the
1084df930be7Sderaadt 		 * format specific trailer routine to check for a trailer. We
1085df930be7Sderaadt 		 * have to watch out that we do not mis-identify file data or
1086df930be7Sderaadt 		 * block padding as a header or trailer. Format specific
1087df930be7Sderaadt 		 * trailer functions must NOT check for the trailer while we
1088df930be7Sderaadt 		 * are running in resync mode. Some trailer functions may tell
1089df930be7Sderaadt 		 * us that this block cannot contain a valid header either, so
1090df930be7Sderaadt 		 * we then throw out the entire block and start over.
1091df930be7Sderaadt 		 */
1092df930be7Sderaadt 		if ((*frmt->rd)(arcn, hdbuf) == 0)
1093df930be7Sderaadt 			break;
1094df930be7Sderaadt 
1095df930be7Sderaadt 		if (!frmt->inhead) {
1096df930be7Sderaadt 			/*
1097df930be7Sderaadt 			 * this format has trailers outside of valid headers
1098df930be7Sderaadt 			 */
1099cd90c754Sderaadt 			if ((ret = (*frmt->trail)(arcn,hdbuf,in_resync,&cnt)) == 0){
1100df930be7Sderaadt 				/*
1101df930be7Sderaadt 				 * valid trailer found, drain input as required
1102df930be7Sderaadt 				 */
1103df930be7Sderaadt 				ar_drain();
1104df930be7Sderaadt 				return(-1);
1105df930be7Sderaadt 			}
1106df930be7Sderaadt 
1107df930be7Sderaadt 			if (ret == 1) {
1108df930be7Sderaadt 				/*
1109df930be7Sderaadt 				 * we are in resync and we were told to throw
1110df930be7Sderaadt 				 * the whole block out because none of the
1111df930be7Sderaadt 				 * bytes in this block can be used to form a
1112df930be7Sderaadt 				 * valid header
1113df930be7Sderaadt 				 */
1114df930be7Sderaadt 				res = hsz;
1115df930be7Sderaadt 				hdend = hdbuf;
1116df930be7Sderaadt 				continue;
1117df930be7Sderaadt 			}
1118df930be7Sderaadt 		}
1119df930be7Sderaadt 
1120df930be7Sderaadt 		/*
1121df930be7Sderaadt 		 * Brute force section.
1122df930be7Sderaadt 		 * not a valid header. We may be able to find a header yet. So
1123df930be7Sderaadt 		 * we shift over by one byte, and set up to read one byte at a
1124df930be7Sderaadt 		 * time from the archive and place it at the end of the buffer.
1125df930be7Sderaadt 		 * We will keep moving byte at a time until we find a header or
1126df930be7Sderaadt 		 * get a read error and have to start over.
1127df930be7Sderaadt 		 */
1128df930be7Sderaadt 		if (!in_resync) {
1129df930be7Sderaadt 			if (act == APPND) {
113042cf9836Stholo 				paxwarn(1,"Unable to append, archive header flaw");
1131df930be7Sderaadt 				return(-1);
1132df930be7Sderaadt 			}
113342cf9836Stholo 			paxwarn(1,"Invalid header, starting valid header search.");
1134df930be7Sderaadt 			++in_resync;
1135df930be7Sderaadt 		}
1136df930be7Sderaadt 		memmove(hdbuf, hdbuf+1, shftsz);
1137df930be7Sderaadt 		res = 1;
1138df930be7Sderaadt 		hdend = hdbuf + shftsz;
1139df930be7Sderaadt 	}
1140df930be7Sderaadt 
1141df930be7Sderaadt 	/*
1142df930be7Sderaadt 	 * ok got a valid header, check for trailer if format encodes it in the
1143df930be7Sderaadt 	 * the header. NOTE: the parameters are different than trailer routines
1144df930be7Sderaadt 	 * which encode trailers outside of the header!
1145df930be7Sderaadt 	 */
1146cd90c754Sderaadt 	if (frmt->inhead && ((*frmt->trail)(arcn,NULL,0,NULL) == 0)) {
1147df930be7Sderaadt 		/*
1148df930be7Sderaadt 		 * valid trailer found, drain input as required
1149df930be7Sderaadt 		 */
1150df930be7Sderaadt 		ar_drain();
1151df930be7Sderaadt 		return(-1);
1152df930be7Sderaadt 	}
1153df930be7Sderaadt 
1154df930be7Sderaadt 	++flcnt;
1155df930be7Sderaadt 	return(0);
1156df930be7Sderaadt }
1157df930be7Sderaadt 
1158df930be7Sderaadt /*
1159df930be7Sderaadt  * get_arc()
1160df930be7Sderaadt  *	Figure out what format an archive is. Handles archive with flaws by
1161df930be7Sderaadt  *	brute force searches for a legal header in any supported format. The
1162df930be7Sderaadt  *	format id routines have to be careful to NOT mis-identify a format.
1163df930be7Sderaadt  *	ASSUMED: headers fit within a BLKMULT header.
1164df930be7Sderaadt  * Return:
1165df930be7Sderaadt  *	0 if archive found -1 otherwise
1166df930be7Sderaadt  */
1167df930be7Sderaadt 
1168df930be7Sderaadt static int
get_arc(void)1169df930be7Sderaadt get_arc(void)
1170df930be7Sderaadt {
1171be87792eSmillert 	int i;
1172be87792eSmillert 	int hdsz = 0;
1173be87792eSmillert 	int res;
1174be87792eSmillert 	int minhd = BLKMULT;
1175df930be7Sderaadt 	char *hdend;
1176df930be7Sderaadt 	int notice = 0;
1177df930be7Sderaadt 
1178df930be7Sderaadt 	/*
1179df930be7Sderaadt 	 * find the smallest header size in all archive formats and then set up
1180df930be7Sderaadt 	 * to read the archive.
1181df930be7Sderaadt 	 */
1182df930be7Sderaadt 	for (i = 0; ford[i] >= 0; ++i) {
1183e1fb501dShalex 		if (fsub[ford[i]].name != NULL && fsub[ford[i]].hsz < minhd)
1184df930be7Sderaadt 			minhd = fsub[ford[i]].hsz;
1185df930be7Sderaadt 	}
1186df930be7Sderaadt 	if (rd_start() < 0)
1187df930be7Sderaadt 		return(-1);
1188df930be7Sderaadt 	res = BLKMULT;
1189df930be7Sderaadt 	hdsz = 0;
1190df930be7Sderaadt 	hdend = hdbuf;
1191df930be7Sderaadt 	for (;;) {
1192df930be7Sderaadt 		for (;;) {
1193df930be7Sderaadt 			/*
1194df930be7Sderaadt 			 * fill the buffer with at least the smallest header
1195df930be7Sderaadt 			 */
1196df930be7Sderaadt 			i = rd_wrbuf(hdend, res);
1197df930be7Sderaadt 			if (i > 0)
1198df930be7Sderaadt 				hdsz += i;
1199df930be7Sderaadt 			if (hdsz >= minhd)
1200df930be7Sderaadt 				break;
1201df930be7Sderaadt 
1202df930be7Sderaadt 			/*
1203df930be7Sderaadt 			 * if we cannot recover from a read error quit
1204df930be7Sderaadt 			 */
1205df930be7Sderaadt 			if ((i == 0) || (rd_sync() < 0))
1206df930be7Sderaadt 				goto out;
1207df930be7Sderaadt 
1208df930be7Sderaadt 			/*
1209df930be7Sderaadt 			 * when we get an error none of the data we already
1210df930be7Sderaadt 			 * have can be used to create a legal header (we just
1211df930be7Sderaadt 			 * got an error in the middle), so we throw it all out
1212df930be7Sderaadt 			 * and refill the buffer with fresh data.
1213df930be7Sderaadt 			 */
1214df930be7Sderaadt 			res = BLKMULT;
1215df930be7Sderaadt 			hdsz = 0;
1216df930be7Sderaadt 			hdend = hdbuf;
1217df930be7Sderaadt 			if (!notice) {
1218df930be7Sderaadt 				if (act == APPND)
1219df930be7Sderaadt 					return(-1);
122042cf9836Stholo 				paxwarn(1,"Cannot identify format. Searching...");
1221df930be7Sderaadt 				++notice;
1222df930be7Sderaadt 			}
1223df930be7Sderaadt 		}
1224df930be7Sderaadt 
1225df930be7Sderaadt 		/*
1226df930be7Sderaadt 		 * we have at least the size of the smallest header in any
1227df930be7Sderaadt 		 * archive format. Look to see if we have a match. The array
1228df930be7Sderaadt 		 * ford[] is used to specify the header id order to reduce the
1229df930be7Sderaadt 		 * chance of incorrectly id'ing a valid header (some formats
1230df930be7Sderaadt 		 * may be subsets of each other and the order would then be
1231df930be7Sderaadt 		 * important).
1232df930be7Sderaadt 		 */
1233df930be7Sderaadt 		for (i = 0; ford[i] >= 0; ++i) {
1234a48f739dSguenther 			if (fsub[ford[i]].id == NULL ||
1235e1fb501dShalex 			    (*fsub[ford[i]].id)(hdbuf, hdsz) < 0)
1236df930be7Sderaadt 				continue;
1237df930be7Sderaadt 			frmt = &(fsub[ford[i]]);
1238df930be7Sderaadt 			/*
1239df930be7Sderaadt 			 * yuck, to avoid slow special case code in the extract
1240df930be7Sderaadt 			 * routines, just push this header back as if it was
1241df930be7Sderaadt 			 * not seen. We have left extra space at start of the
1242df930be7Sderaadt 			 * buffer for this purpose. This is a bit ugly, but
1243df930be7Sderaadt 			 * adding all the special case code is far worse.
1244df930be7Sderaadt 			 */
1245df930be7Sderaadt 			pback(hdbuf, hdsz);
1246df930be7Sderaadt 			return(0);
1247df930be7Sderaadt 		}
1248df930be7Sderaadt 
1249df930be7Sderaadt 		/*
1250df930be7Sderaadt 		 * We have a flawed archive, no match. we start searching, but
1251df930be7Sderaadt 		 * we never allow additions to flawed archives
1252df930be7Sderaadt 		 */
1253df930be7Sderaadt 		if (!notice) {
1254df930be7Sderaadt 			if (act == APPND)
1255df930be7Sderaadt 				return(-1);
125642cf9836Stholo 			paxwarn(1, "Cannot identify format. Searching...");
1257df930be7Sderaadt 			++notice;
1258df930be7Sderaadt 		}
1259df930be7Sderaadt 
1260df930be7Sderaadt 		/*
1261df930be7Sderaadt 		 * brute force search for a header that we can id.
1262df930be7Sderaadt 		 * we shift through byte at a time. this is slow, but we cannot
1263df930be7Sderaadt 		 * determine the nature of the flaw in the archive in a
1264df930be7Sderaadt 		 * portable manner
1265df930be7Sderaadt 		 */
1266df930be7Sderaadt 		if (--hdsz > 0) {
1267df930be7Sderaadt 			memmove(hdbuf, hdbuf+1, hdsz);
1268df930be7Sderaadt 			res = BLKMULT - hdsz;
1269df930be7Sderaadt 			hdend = hdbuf + hdsz;
1270df930be7Sderaadt 		} else {
1271df930be7Sderaadt 			res = BLKMULT;
1272df930be7Sderaadt 			hdend = hdbuf;
1273df930be7Sderaadt 			hdsz = 0;
1274df930be7Sderaadt 		}
1275df930be7Sderaadt 	}
1276df930be7Sderaadt 
1277df930be7Sderaadt     out:
1278df930be7Sderaadt 	/*
1279df930be7Sderaadt 	 * we cannot find a header, bow, apologize and quit
1280df930be7Sderaadt 	 */
128142cf9836Stholo 	paxwarn(1, "Sorry, unable to determine archive format.");
1282df930be7Sderaadt 	return(-1);
1283df930be7Sderaadt }
1284