1/*-
2 * Copyright (c) 1992 Keith Muller.
3 * Copyright (c) 1992, 1993
4 *	The Regents of the University of California.  All rights reserved.
5 *
6 * This code is derived from software contributed to Berkeley by
7 * Keith Muller of the University of California, San Diego.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 *    notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 *    notice, this list of conditions and the following disclaimer in the
16 *    documentation and/or other materials provided with the distribution.
17 * 4. Neither the name of the University nor the names of its contributors
18 *    may be used to endorse or promote products derived from this software
19 *    without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#ifndef lint
35#if 0
36static char sccsid[] = "@(#)ar_subs.c	8.2 (Berkeley) 4/18/94";
37#endif
38#endif /* not lint */
39#include <sys/cdefs.h>
40__FBSDID("$FreeBSD$");
41
42#include <sys/types.h>
43#include <sys/time.h>
44#include <sys/stat.h>
45#include <signal.h>
46#include <string.h>
47#include <stdio.h>
48#include <fcntl.h>
49#include <errno.h>
50#include <unistd.h>
51#include <stdlib.h>
52#include "pax.h"
53#include "extern.h"
54
55static void wr_archive(ARCHD *, int is_app);
56static int get_arc(void);
57static int next_head(ARCHD *);
58
59/*
60 * Routines which control the overall operation modes of pax as specified by
61 * the user: list, append, read ...
62 */
63
64static char hdbuf[BLKMULT];		/* space for archive header on read */
65u_long flcnt;				/* number of files processed */
66
67/*
68 * list()
69 *	list the contents of an archive which match user supplied pattern(s)
70 *	(no pattern matches all).
71 */
72
73void
74list(void)
75{
76	ARCHD *arcn;
77	int res;
78	ARCHD archd;
79	time_t now;
80
81	arcn = &archd;
82	/*
83	 * figure out archive type; pass any format specific options to the
84	 * archive option processing routine; call the format init routine. We
85	 * also save current time for ls_list() so we do not make a system
86	 * call for each file we need to print. If verbose (vflag) start up
87	 * the name and group caches.
88	 */
89	if ((get_arc() < 0) || ((*frmt->options)() < 0) ||
90	    ((*frmt->st_rd)() < 0))
91		return;
92
93	if (vflag && ((uidtb_start() < 0) || (gidtb_start() < 0)))
94		return;
95
96	now = time(NULL);
97
98	/*
99	 * step through the archive until the format says it is done
100	 */
101	while (next_head(arcn) == 0) {
102		/*
103		 * check for pattern, and user specified options match.
104		 * When all patterns are matched we are done.
105		 */
106		if ((res = pat_match(arcn)) < 0)
107			break;
108
109		if ((res == 0) && (sel_chk(arcn) == 0)) {
110			/*
111			 * pattern resulted in a selected file
112			 */
113			if (pat_sel(arcn) < 0)
114				break;
115
116			/*
117			 * modify the name as requested by the user if name
118			 * survives modification, do a listing of the file
119			 */
120			if ((res = mod_name(arcn)) < 0)
121				break;
122			if (res == 0)
123				ls_list(arcn, now, stdout);
124		}
125
126		/*
127		 * skip to next archive format header using values calculated
128		 * by the format header read routine
129		 */
130		if (rd_skip(arcn->skip + arcn->pad) == 1)
131			break;
132	}
133
134	/*
135	 * all done, let format have a chance to cleanup, and make sure that
136	 * the patterns supplied by the user were all matched
137	 */
138	(void)(*frmt->end_rd)();
139	(void)sigprocmask(SIG_BLOCK, &s_mask, NULL);
140	ar_close();
141	pat_chk();
142}
143
144/*
145 * extract()
146 *	extract the member(s) of an archive as specified by user supplied
147 *	pattern(s) (no patterns extracts all members)
148 */
149
150void
151extract(void)
152{
153	ARCHD *arcn;
154	int res;
155	off_t cnt;
156	ARCHD archd;
157	struct stat sb;
158	int fd;
159	time_t now;
160
161	arcn = &archd;
162	/*
163	 * figure out archive type; pass any format specific options to the
164	 * archive option processing routine; call the format init routine;
165	 * start up the directory modification time and access mode database
166	 */
167	if ((get_arc() < 0) || ((*frmt->options)() < 0) ||
168	    ((*frmt->st_rd)() < 0) || (dir_start() < 0))
169		return;
170
171	/*
172	 * When we are doing interactive rename, we store the mapping of names
173	 * so we can fix up hard links files later in the archive.
174	 */
175	if (iflag && (name_start() < 0))
176		return;
177
178	now = time(NULL);
179
180	/*
181	 * step through each entry on the archive until the format read routine
182	 * says it is done
183	 */
184	while (next_head(arcn) == 0) {
185
186		/*
187		 * check for pattern, and user specified options match. When
188		 * all the patterns are matched we are done
189		 */
190		if ((res = pat_match(arcn)) < 0)
191			break;
192
193		if ((res > 0) || (sel_chk(arcn) != 0)) {
194			/*
195			 * file is not selected. skip past any file data and
196			 * padding and go back for the next archive member
197			 */
198			(void)rd_skip(arcn->skip + arcn->pad);
199			continue;
200		}
201
202		/*
203		 * with -u or -D only extract when the archive member is newer
204		 * than the file with the same name in the file system (nos
205		 * test of being the same type is required).
206		 * NOTE: this test is done BEFORE name modifications as
207		 * specified by pax. this operation can be confusing to the
208		 * user who might expect the test to be done on an existing
209		 * file AFTER the name mod. In honesty the pax spec is probably
210		 * flawed in this respect.
211		 */
212		if ((uflag || Dflag) && ((lstat(arcn->name, &sb) == 0))) {
213			if (uflag && Dflag) {
214				if ((arcn->sb.st_mtime <= sb.st_mtime) &&
215				    (arcn->sb.st_ctime <= sb.st_ctime)) {
216					(void)rd_skip(arcn->skip + arcn->pad);
217					continue;
218				}
219			} else if (Dflag) {
220				if (arcn->sb.st_ctime <= sb.st_ctime) {
221					(void)rd_skip(arcn->skip + arcn->pad);
222					continue;
223				}
224			} else if (arcn->sb.st_mtime <= sb.st_mtime) {
225				(void)rd_skip(arcn->skip + arcn->pad);
226				continue;
227			}
228		}
229
230		/*
231		 * this archive member is now been selected. modify the name.
232		 */
233		if ((pat_sel(arcn) < 0) || ((res = mod_name(arcn)) < 0))
234			break;
235		if (res > 0) {
236			/*
237			 * a bad name mod, skip and purge name from link table
238			 */
239			purg_lnk(arcn);
240			(void)rd_skip(arcn->skip + arcn->pad);
241			continue;
242		}
243
244		/*
245		 * Non standard -Y and -Z flag. When the existing file is
246		 * same age or newer skip
247		 */
248		if ((Yflag || Zflag) && ((lstat(arcn->name, &sb) == 0))) {
249			if (Yflag && Zflag) {
250				if ((arcn->sb.st_mtime <= sb.st_mtime) &&
251				    (arcn->sb.st_ctime <= sb.st_ctime)) {
252					(void)rd_skip(arcn->skip + arcn->pad);
253					continue;
254				}
255			} else if (Yflag) {
256				if (arcn->sb.st_ctime <= sb.st_ctime) {
257					(void)rd_skip(arcn->skip + arcn->pad);
258					continue;
259				}
260			} else if (arcn->sb.st_mtime <= sb.st_mtime) {
261				(void)rd_skip(arcn->skip + arcn->pad);
262				continue;
263			}
264		}
265
266		if (vflag) {
267			if (vflag > 1)
268				ls_list(arcn, now, listf);
269			else {
270				(void)fputs(arcn->name, listf);
271				vfpart = 1;
272			}
273		}
274
275		/*
276		 * if required, chdir around.
277		 */
278		if ((arcn->pat != NULL) && (arcn->pat->chdname != NULL))
279			if (chdir(arcn->pat->chdname) != 0)
280				syswarn(1, errno, "Cannot chdir to %s",
281				    arcn->pat->chdname);
282
283		/*
284		 * all ok, extract this member based on type
285		 */
286		if ((arcn->type != PAX_REG) && (arcn->type != PAX_CTG)) {
287			/*
288			 * process archive members that are not regular files.
289			 * throw out padding and any data that might follow the
290			 * header (as determined by the format).
291			 */
292			if ((arcn->type == PAX_HLK) || (arcn->type == PAX_HRG))
293				res = lnk_creat(arcn);
294			else
295				res = node_creat(arcn);
296
297			(void)rd_skip(arcn->skip + arcn->pad);
298			if (res < 0)
299				purg_lnk(arcn);
300
301			if (vflag && vfpart) {
302				(void)putc('\n', listf);
303				vfpart = 0;
304			}
305			continue;
306		}
307		/*
308		 * we have a file with data here. If we can not create it, skip
309		 * over the data and purge the name from hard link table
310		 */
311		if ((fd = file_creat(arcn)) < 0) {
312			(void)rd_skip(arcn->skip + arcn->pad);
313			purg_lnk(arcn);
314			continue;
315		}
316		/*
317		 * extract the file from the archive and skip over padding and
318		 * any unprocessed data
319		 */
320		res = (*frmt->rd_data)(arcn, fd, &cnt);
321		file_close(arcn, fd);
322		if (vflag && vfpart) {
323			(void)putc('\n', listf);
324			vfpart = 0;
325		}
326		if (!res)
327			(void)rd_skip(cnt + arcn->pad);
328
329		/*
330		 * if required, chdir around.
331		 */
332		if ((arcn->pat != NULL) && (arcn->pat->chdname != NULL))
333			if (fchdir(cwdfd) != 0)
334				syswarn(1, errno,
335				    "Can't fchdir to starting directory");
336	}
337
338	/*
339	 * all done, restore directory modes and times as required; make sure
340	 * all patterns supplied by the user were matched; block off signals
341	 * to avoid chance for multiple entry into the cleanup code.
342	 */
343	(void)(*frmt->end_rd)();
344	(void)sigprocmask(SIG_BLOCK, &s_mask, NULL);
345	ar_close();
346	proc_dir();
347	pat_chk();
348}
349
350/*
351 * wr_archive()
352 *	Write an archive. used in both creating a new archive and appends on
353 *	previously written archive.
354 */
355
356static void
357wr_archive(ARCHD *arcn, int is_app)
358{
359	int res;
360	int hlk;
361	int wr_one;
362	off_t cnt;
363	int (*wrf)(ARCHD *);
364	int fd = -1;
365	time_t now;
366
367	/*
368	 * if this format supports hard link storage, start up the database
369	 * that detects them.
370	 */
371	if (((hlk = frmt->hlk) == 1) && (lnk_start() < 0))
372		return;
373
374	/*
375	 * start up the file traversal code and format specific write
376	 */
377	if ((ftree_start() < 0) || ((*frmt->st_wr)() < 0))
378		return;
379	wrf = frmt->wr;
380
381	/*
382	 * When we are doing interactive rename, we store the mapping of names
383	 * so we can fix up hard links files later in the archive.
384	 */
385	if (iflag && (name_start() < 0))
386		return;
387
388	/*
389	 * if this not append, and there are no files, we do no write a trailer
390	 */
391	wr_one = is_app;
392
393	now = time(NULL);
394
395	/*
396	 * while there are files to archive, process them one at at time
397	 */
398	while (next_file(arcn) == 0) {
399		/*
400		 * check if this file meets user specified options match.
401		 */
402		if (sel_chk(arcn) != 0) {
403			ftree_notsel();
404			continue;
405		}
406		fd = -1;
407		if (uflag) {
408			/*
409			 * only archive if this file is newer than a file with
410			 * the same name that is already stored on the archive
411			 */
412			if ((res = chk_ftime(arcn)) < 0)
413				break;
414			if (res > 0)
415				continue;
416		}
417
418		/*
419		 * this file is considered selected now. see if this is a hard
420		 * link to a file already stored
421		 */
422		ftree_sel(arcn);
423		if (hlk && (chk_lnk(arcn) < 0))
424			break;
425
426		if ((arcn->type == PAX_REG) || (arcn->type == PAX_HRG) ||
427		    (arcn->type == PAX_CTG)) {
428			/*
429			 * we will have to read this file. by opening it now we
430			 * can avoid writing a header to the archive for a file
431			 * we were later unable to read (we also purge it from
432			 * the link table).
433			 */
434			if ((fd = open(arcn->org_name, O_RDONLY, 0)) < 0) {
435				syswarn(1,errno, "Unable to open %s to read",
436					arcn->org_name);
437				purg_lnk(arcn);
438				continue;
439			}
440		}
441
442		/*
443		 * Now modify the name as requested by the user
444		 */
445		if ((res = mod_name(arcn)) < 0) {
446			/*
447			 * name modification says to skip this file, close the
448			 * file and purge link table entry
449			 */
450			rdfile_close(arcn, &fd);
451			purg_lnk(arcn);
452			break;
453		}
454
455		if ((res > 0) || (docrc && (set_crc(arcn, fd) < 0))) {
456			/*
457			 * unable to obtain the crc we need, close the file,
458			 * purge link table entry
459			 */
460			rdfile_close(arcn, &fd);
461			purg_lnk(arcn);
462			continue;
463		}
464
465		if (vflag) {
466			if (vflag > 1)
467				ls_list(arcn, now, listf);
468			else {
469				(void)fputs(arcn->name, listf);
470				vfpart = 1;
471			}
472		}
473		++flcnt;
474
475		/*
476		 * looks safe to store the file, have the format specific
477		 * routine write routine store the file header on the archive
478		 */
479		if ((res = (*wrf)(arcn)) < 0) {
480			rdfile_close(arcn, &fd);
481			break;
482		}
483		wr_one = 1;
484		if (res > 0) {
485			/*
486			 * format write says no file data needs to be stored
487			 * so we are done messing with this file
488			 */
489			if (vflag && vfpart) {
490				(void)putc('\n', listf);
491				vfpart = 0;
492			}
493			rdfile_close(arcn, &fd);
494			continue;
495		}
496
497		/*
498		 * Add file data to the archive, quit on write error. if we
499		 * cannot write the entire file contents to the archive we
500		 * must pad the archive to replace the missing file data
501		 * (otherwise during an extract the file header for the file
502		 * which FOLLOWS this one will not be where we expect it to
503		 * be).
504		 */
505		res = (*frmt->wr_data)(arcn, fd, &cnt);
506		rdfile_close(arcn, &fd);
507		if (vflag && vfpart) {
508			(void)putc('\n', listf);
509			vfpart = 0;
510		}
511		if (res < 0)
512			break;
513
514		/*
515		 * pad as required, cnt is number of bytes not written
516		 */
517		if (((cnt > 0) && (wr_skip(cnt) < 0)) ||
518		    ((arcn->pad > 0) && (wr_skip(arcn->pad) < 0)))
519			break;
520	}
521
522	/*
523	 * tell format to write trailer; pad to block boundary; reset directory
524	 * mode/access times, and check if all patterns supplied by the user
525	 * were matched. block off signals to avoid chance for multiple entry
526	 * into the cleanup code
527	 */
528	if (wr_one) {
529		(*frmt->end_wr)();
530		wr_fin();
531	}
532	(void)sigprocmask(SIG_BLOCK, &s_mask, NULL);
533	ar_close();
534	if (tflag)
535		proc_dir();
536	ftree_chk();
537}
538
539/*
540 * append()
541 *	Add file to previously written archive. Archive format specified by the
542 *	user must agree with archive. The archive is read first to collect
543 *	modification times (if -u) and locate the archive trailer. The archive
544 *	is positioned in front of the record with the trailer and wr_archive()
545 *	is called to add the new members.
546 *	PAX IMPLEMENTATION DETAIL NOTE:
547 *	-u is implemented by adding the new members to the end of the archive.
548 *	Care is taken so that these do not end up as links to the older
549 *	version of the same file already stored in the archive. It is expected
550 *	when extraction occurs these newer versions will over-write the older
551 *	ones stored "earlier" in the archive (this may be a bad assumption as
552 *	it depends on the implementation of the program doing the extraction).
553 *	It is really difficult to splice in members without either re-writing
554 *	the entire archive (from the point were the old version was), or having
555 *	assistance of the format specification in terms of a special update
556 *	header that invalidates a previous archive record. The POSIX spec left
557 *	the method used to implement -u unspecified. This pax is able to
558 *	over write existing files that it creates.
559 */
560
561void
562append(void)
563{
564	ARCHD *arcn;
565	int res;
566	ARCHD archd;
567	FSUB *orgfrmt;
568	int udev;
569	off_t tlen;
570
571	arcn = &archd;
572	orgfrmt = frmt;
573
574	/*
575	 * Do not allow an append operation if the actual archive is of a
576	 * different format than the user specified format.
577	 */
578	if (get_arc() < 0)
579		return;
580	if ((orgfrmt != NULL) && (orgfrmt != frmt)) {
581		paxwarn(1, "Cannot mix current archive format %s with %s",
582		    frmt->name, orgfrmt->name);
583		return;
584	}
585
586	/*
587	 * pass the format any options and start up format
588	 */
589	if (((*frmt->options)() < 0) || ((*frmt->st_rd)() < 0))
590		return;
591
592	/*
593	 * if we only are adding members that are newer, we need to save the
594	 * mod times for all files we see.
595	 */
596	if (uflag && (ftime_start() < 0))
597		return;
598
599	/*
600	 * some archive formats encode hard links by recording the device and
601	 * file serial number (inode) but copy the file anyway (multiple times)
602	 * to the archive. When we append, we run the risk that newly added
603	 * files may have the same device and inode numbers as those recorded
604	 * on the archive but during a previous run. If this happens, when the
605	 * archive is extracted we get INCORRECT hard links. We avoid this by
606	 * remapping the device numbers so that newly added files will never
607	 * use the same device number as one found on the archive. remapping
608	 * allows new members to safely have links among themselves. remapping
609	 * also avoids problems with file inode (serial number) truncations
610	 * when the inode number is larger than storage space in the archive
611	 * header. See the remap routines for more details.
612	 */
613	if ((udev = frmt->udev) && (dev_start() < 0))
614		return;
615
616	/*
617	 * reading the archive may take a long time. If verbose tell the user
618	 */
619	if (vflag) {
620		(void)fprintf(listf,
621			"%s: Reading archive to position at the end...", argv0);
622		vfpart = 1;
623	}
624
625	/*
626	 * step through the archive until the format says it is done
627	 */
628	while (next_head(arcn) == 0) {
629		/*
630		 * check if this file meets user specified options.
631		 */
632		if (sel_chk(arcn) != 0) {
633			if (rd_skip(arcn->skip + arcn->pad) == 1)
634				break;
635			continue;
636		}
637
638		if (uflag) {
639			/*
640			 * see if this is the newest version of this file has
641			 * already been seen, if so skip.
642			 */
643			if ((res = chk_ftime(arcn)) < 0)
644				break;
645			if (res > 0) {
646				if (rd_skip(arcn->skip + arcn->pad) == 1)
647					break;
648				continue;
649			}
650		}
651
652		/*
653		 * Store this device number. Device numbers seen during the
654		 * read phase of append will cause newly appended files with a
655		 * device number seen in the old part of the archive to be
656		 * remapped to an unused device number.
657		 */
658		if ((udev && (add_dev(arcn) < 0)) ||
659		    (rd_skip(arcn->skip + arcn->pad) == 1))
660			break;
661	}
662
663	/*
664	 * done, finish up read and get the number of bytes to back up so we
665	 * can add new members. The format might have used the hard link table,
666	 * purge it.
667	 */
668	tlen = (*frmt->end_rd)();
669	lnk_end();
670
671	/*
672	 * try to position for write, if this fails quit. if any error occurs,
673	 * we will refuse to write
674	 */
675	if (appnd_start(tlen) < 0)
676		return;
677
678	/*
679	 * tell the user we are done reading.
680	 */
681	if (vflag && vfpart) {
682		(void)fputs("done.\n", listf);
683		vfpart = 0;
684	}
685
686	/*
687	 * go to the writing phase to add the new members
688	 */
689	wr_archive(arcn, 1);
690}
691
692/*
693 * archive()
694 *	write a new archive
695 */
696
697void
698archive(void)
699{
700	ARCHD archd;
701
702	/*
703	 * if we only are adding members that are newer, we need to save the
704	 * mod times for all files; set up for writing; pass the format any
705	 * options write the archive
706	 */
707	if ((uflag && (ftime_start() < 0)) || (wr_start() < 0))
708		return;
709	if ((*frmt->options)() < 0)
710		return;
711
712	wr_archive(&archd, 0);
713}
714
715/*
716 * copy()
717 *	copy files from one part of the file system to another. this does not
718 *	use any archive storage. The EFFECT OF THE COPY IS THE SAME as if an
719 *	archive was written and then extracted in the destination directory
720 *	(except the files are forced to be under the destination directory).
721 */
722
723void
724copy(void)
725{
726	ARCHD *arcn;
727	int res;
728	int fddest;
729	char *dest_pt;
730	int dlen;
731	int drem;
732	int fdsrc = -1;
733	struct stat sb;
734	ARCHD archd;
735	char dirbuf[PAXPATHLEN+1];
736
737	arcn = &archd;
738	/*
739	 * set up the destination dir path and make sure it is a directory. We
740	 * make sure we have a trailing / on the destination
741	 */
742	dlen = l_strncpy(dirbuf, dirptr, sizeof(dirbuf) - 1);
743	dest_pt = dirbuf + dlen;
744	if (*(dest_pt-1) != '/') {
745		*dest_pt++ = '/';
746		++dlen;
747	}
748	*dest_pt = '\0';
749	drem = PAXPATHLEN - dlen;
750
751	if (stat(dirptr, &sb) < 0) {
752		syswarn(1, errno, "Cannot access destination directory %s",
753			dirptr);
754		return;
755	}
756	if (!S_ISDIR(sb.st_mode)) {
757		paxwarn(1, "Destination is not a directory %s", dirptr);
758		return;
759	}
760
761	/*
762	 * start up the hard link table; file traversal routines and the
763	 * modification time and access mode database
764	 */
765	if ((lnk_start() < 0) || (ftree_start() < 0) || (dir_start() < 0))
766		return;
767
768	/*
769	 * When we are doing interactive rename, we store the mapping of names
770	 * so we can fix up hard links files later in the archive.
771	 */
772	if (iflag && (name_start() < 0))
773		return;
774
775	/*
776	 * set up to cp file trees
777	 */
778	cp_start();
779
780	/*
781	 * while there are files to archive, process them
782	 */
783	while (next_file(arcn) == 0) {
784		fdsrc = -1;
785
786		/*
787		 * check if this file meets user specified options
788		 */
789		if (sel_chk(arcn) != 0) {
790			ftree_notsel();
791			continue;
792		}
793
794		/*
795		 * if there is already a file in the destination directory with
796		 * the same name and it is newer, skip the one stored on the
797		 * archive.
798		 * NOTE: this test is done BEFORE name modifications as
799		 * specified by pax. this can be confusing to the user who
800		 * might expect the test to be done on an existing file AFTER
801		 * the name mod. In honesty the pax spec is probably flawed in
802		 * this respect
803		 */
804		if (uflag || Dflag) {
805			/*
806			 * create the destination name
807			 */
808			if (*(arcn->name) == '/')
809				res = 1;
810			else
811				res = 0;
812			if ((arcn->nlen - res) > drem) {
813				paxwarn(1, "Destination pathname too long %s",
814					arcn->name);
815				continue;
816			}
817			(void)strncpy(dest_pt, arcn->name + res, drem);
818			dirbuf[PAXPATHLEN] = '\0';
819
820			/*
821			 * if existing file is same age or newer skip
822			 */
823			res = lstat(dirbuf, &sb);
824			*dest_pt = '\0';
825
826		    	if (res == 0) {
827				if (uflag && Dflag) {
828					if ((arcn->sb.st_mtime<=sb.st_mtime) &&
829			    		    (arcn->sb.st_ctime<=sb.st_ctime))
830						continue;
831				} else if (Dflag) {
832					if (arcn->sb.st_ctime <= sb.st_ctime)
833						continue;
834				} else if (arcn->sb.st_mtime <= sb.st_mtime)
835					continue;
836			}
837		}
838
839		/*
840		 * this file is considered selected. See if this is a hard link
841		 * to a previous file; modify the name as requested by the
842		 * user; set the final destination.
843		 */
844		ftree_sel(arcn);
845		if ((chk_lnk(arcn) < 0) || ((res = mod_name(arcn)) < 0))
846			break;
847		if ((res > 0) || (set_dest(arcn, dirbuf, dlen) < 0)) {
848			/*
849			 * skip file, purge from link table
850			 */
851			purg_lnk(arcn);
852			continue;
853		}
854
855		/*
856		 * Non standard -Y and -Z flag. When the existing file is
857		 * same age or newer skip
858		 */
859		if ((Yflag || Zflag) && ((lstat(arcn->name, &sb) == 0))) {
860			if (Yflag && Zflag) {
861				if ((arcn->sb.st_mtime <= sb.st_mtime) &&
862				    (arcn->sb.st_ctime <= sb.st_ctime))
863					continue;
864			} else if (Yflag) {
865				if (arcn->sb.st_ctime <= sb.st_ctime)
866					continue;
867			} else if (arcn->sb.st_mtime <= sb.st_mtime)
868				continue;
869		}
870
871		if (vflag) {
872			(void)fputs(arcn->name, listf);
873			vfpart = 1;
874		}
875		++flcnt;
876
877		/*
878		 * try to create a hard link to the src file if requested
879		 * but make sure we are not trying to overwrite ourselves.
880		 */
881		if (lflag)
882			res = cross_lnk(arcn);
883		else
884			res = chk_same(arcn);
885		if (res <= 0) {
886			if (vflag && vfpart) {
887				(void)putc('\n', listf);
888				vfpart = 0;
889			}
890			continue;
891		}
892
893		/*
894		 * have to create a new file
895		 */
896		if ((arcn->type != PAX_REG) && (arcn->type != PAX_CTG)) {
897			/*
898			 * create a link or special file
899			 */
900			if ((arcn->type == PAX_HLK) || (arcn->type == PAX_HRG))
901				res = lnk_creat(arcn);
902			else
903				res = node_creat(arcn);
904			if (res < 0)
905				purg_lnk(arcn);
906			if (vflag && vfpart) {
907				(void)putc('\n', listf);
908				vfpart = 0;
909			}
910			continue;
911		}
912
913		/*
914		 * have to copy a regular file to the destination directory.
915		 * first open source file and then create the destination file
916		 */
917		if ((fdsrc = open(arcn->org_name, O_RDONLY, 0)) < 0) {
918			syswarn(1, errno, "Unable to open %s to read",
919			    arcn->org_name);
920			purg_lnk(arcn);
921			continue;
922		}
923		if ((fddest = file_creat(arcn)) < 0) {
924			rdfile_close(arcn, &fdsrc);
925			purg_lnk(arcn);
926			continue;
927		}
928
929		/*
930		 * copy source file data to the destination file
931		 */
932		cp_file(arcn, fdsrc, fddest);
933		file_close(arcn, fddest);
934		rdfile_close(arcn, &fdsrc);
935
936		if (vflag && vfpart) {
937			(void)putc('\n', listf);
938			vfpart = 0;
939		}
940	}
941
942	/*
943	 * restore directory modes and times as required; make sure all
944	 * patterns were selected block off signals to avoid chance for
945	 * multiple entry into the cleanup code.
946	 */
947	(void)sigprocmask(SIG_BLOCK, &s_mask, NULL);
948	ar_close();
949	proc_dir();
950	ftree_chk();
951}
952
953/*
954 * next_head()
955 *	try to find a valid header in the archive. Uses format specific
956 *	routines to extract the header and id the trailer. Trailers may be
957 *	located within a valid header or in an invalid header (the location
958 *	is format specific. The inhead field from the option table tells us
959 *	where to look for the trailer).
960 *	We keep reading (and resyncing) until we get enough contiguous data
961 *	to check for a header. If we cannot find one, we shift by a byte
962 *	add a new byte from the archive to the end of the buffer and try again.
963 *	If we get a read error, we throw out what we have (as we must have
964 *	contiguous data) and start over again.
965 *	ASSUMED: headers fit within a BLKMULT header.
966 * Return:
967 *	0 if we got a header, -1 if we are unable to ever find another one
968 *	(we reached the end of input, or we reached the limit on retries. see
969 *	the specs for rd_wrbuf() for more details)
970 */
971
972static int
973next_head(ARCHD *arcn)
974{
975	int ret;
976	char *hdend;
977	int res;
978	int shftsz;
979	int hsz;
980	int in_resync = 0; 	/* set when we are in resync mode */
981	int cnt = 0;			/* counter for trailer function */
982	int first = 1;			/* on 1st read, EOF isn't premature. */
983
984	/*
985	 * set up initial conditions, we want a whole frmt->hsz block as we
986	 * have no data yet.
987	 */
988	res = hsz = frmt->hsz;
989	hdend = hdbuf;
990	shftsz = hsz - 1;
991	for(;;) {
992		/*
993		 * keep looping until we get a contiguous FULL buffer
994		 * (frmt->hsz is the proper size)
995		 */
996		for (;;) {
997			if ((ret = rd_wrbuf(hdend, res)) == res)
998				break;
999
1000			/*
1001			 * If we read 0 bytes (EOF) from an archive when we
1002			 * expect to find a header, we have stepped upon
1003			 * an archive without the customary block of zeroes
1004			 * end marker.  It's just stupid to error out on
1005			 * them, so exit gracefully.
1006			 */
1007			if (first && ret == 0)
1008				return(-1);
1009			first = 0;
1010
1011			/*
1012			 * some kind of archive read problem, try to resync the
1013			 * storage device, better give the user the bad news.
1014			 */
1015			if ((ret == 0) || (rd_sync() < 0)) {
1016				paxwarn(1,"Premature end of file on archive read");
1017				return(-1);
1018			}
1019			if (!in_resync) {
1020				if (act == APPND) {
1021					paxwarn(1,
1022					  "Archive I/O error, cannot continue");
1023					return(-1);
1024				}
1025				paxwarn(1,"Archive I/O error. Trying to recover.");
1026				++in_resync;
1027			}
1028
1029			/*
1030			 * oh well, throw it all out and start over
1031			 */
1032			res = hsz;
1033			hdend = hdbuf;
1034		}
1035
1036		/*
1037		 * ok we have a contiguous buffer of the right size. Call the
1038		 * format read routine. If this was not a valid header and this
1039		 * format stores trailers outside of the header, call the
1040		 * format specific trailer routine to check for a trailer. We
1041		 * have to watch out that we do not mis-identify file data or
1042		 * block padding as a header or trailer. Format specific
1043		 * trailer functions must NOT check for the trailer while we
1044		 * are running in resync mode. Some trailer functions may tell
1045		 * us that this block cannot contain a valid header either, so
1046		 * we then throw out the entire block and start over.
1047		 */
1048		if ((*frmt->rd)(arcn, hdbuf) == 0)
1049			break;
1050
1051		if (!frmt->inhead) {
1052			/*
1053			 * this format has trailers outside of valid headers
1054			 */
1055			if ((ret = (*frmt->trail_tar)(hdbuf,in_resync,&cnt)) == 0){
1056				/*
1057				 * valid trailer found, drain input as required
1058				 */
1059				ar_drain();
1060				return(-1);
1061			}
1062
1063			if (ret == 1) {
1064				/*
1065				 * we are in resync and we were told to throw
1066				 * the whole block out because none of the
1067				 * bytes in this block can be used to form a
1068				 * valid header
1069				 */
1070				res = hsz;
1071				hdend = hdbuf;
1072				continue;
1073			}
1074		}
1075
1076		/*
1077		 * Brute force section.
1078		 * not a valid header. We may be able to find a header yet. So
1079		 * we shift over by one byte, and set up to read one byte at a
1080		 * time from the archive and place it at the end of the buffer.
1081		 * We will keep moving byte at a time until we find a header or
1082		 * get a read error and have to start over.
1083		 */
1084		if (!in_resync) {
1085			if (act == APPND) {
1086				paxwarn(1,"Unable to append, archive header flaw");
1087				return(-1);
1088			}
1089			paxwarn(1,"Invalid header, starting valid header search.");
1090			++in_resync;
1091		}
1092		memmove(hdbuf, hdbuf+1, shftsz);
1093		res = 1;
1094		hdend = hdbuf + shftsz;
1095	}
1096
1097	/*
1098	 * ok got a valid header, check for trailer if format encodes it in
1099	 * the header.
1100	 */
1101	if (frmt->inhead && ((*frmt->trail_cpio)(arcn) == 0)) {
1102		/*
1103		 * valid trailer found, drain input as required
1104		 */
1105		ar_drain();
1106		return(-1);
1107	}
1108
1109	++flcnt;
1110	return(0);
1111}
1112
1113/*
1114 * get_arc()
1115 *	Figure out what format an archive is. Handles archive with flaws by
1116 *	brute force searches for a legal header in any supported format. The
1117 *	format id routines have to be careful to NOT mis-identify a format.
1118 *	ASSUMED: headers fit within a BLKMULT header.
1119 * Return:
1120 *	0 if archive found -1 otherwise
1121 */
1122
1123static int
1124get_arc(void)
1125{
1126	int i;
1127	int hdsz = 0;
1128	int res;
1129	int minhd = BLKMULT;
1130	char *hdend;
1131	int notice = 0;
1132
1133	/*
1134	 * find the smallest header size in all archive formats and then set up
1135	 * to read the archive.
1136	 */
1137	for (i = 0; ford[i] >= 0; ++i) {
1138		if (fsub[ford[i]].hsz < minhd)
1139			minhd = fsub[ford[i]].hsz;
1140	}
1141	if (rd_start() < 0)
1142		return(-1);
1143	res = BLKMULT;
1144	hdsz = 0;
1145	hdend = hdbuf;
1146	for(;;) {
1147		for (;;) {
1148			/*
1149			 * fill the buffer with at least the smallest header
1150			 */
1151			i = rd_wrbuf(hdend, res);
1152			if (i > 0)
1153				hdsz += i;
1154			if (hdsz >= minhd)
1155				break;
1156
1157			/*
1158			 * if we cannot recover from a read error quit
1159			 */
1160			if ((i == 0) || (rd_sync() < 0))
1161				goto out;
1162
1163			/*
1164			 * when we get an error none of the data we already
1165			 * have can be used to create a legal header (we just
1166			 * got an error in the middle), so we throw it all out
1167			 * and refill the buffer with fresh data.
1168			 */
1169			res = BLKMULT;
1170			hdsz = 0;
1171			hdend = hdbuf;
1172			if (!notice) {
1173				if (act == APPND)
1174					return(-1);
1175				paxwarn(1,"Cannot identify format. Searching...");
1176				++notice;
1177			}
1178		}
1179
1180		/*
1181		 * we have at least the size of the smallest header in any
1182		 * archive format. Look to see if we have a match. The array
1183		 * ford[] is used to specify the header id order to reduce the
1184		 * chance of incorrectly id'ing a valid header (some formats
1185		 * may be subsets of each other and the order would then be
1186		 * important).
1187		 */
1188		for (i = 0; ford[i] >= 0; ++i) {
1189			if ((*fsub[ford[i]].id)(hdbuf, hdsz) < 0)
1190				continue;
1191			frmt = &(fsub[ford[i]]);
1192			/*
1193			 * yuck, to avoid slow special case code in the extract
1194			 * routines, just push this header back as if it was
1195			 * not seen. We have left extra space at start of the
1196			 * buffer for this purpose. This is a bit ugly, but
1197			 * adding all the special case code is far worse.
1198			 */
1199			pback(hdbuf, hdsz);
1200			return(0);
1201		}
1202
1203		/*
1204		 * We have a flawed archive, no match. we start searching, but
1205		 * we never allow additions to flawed archives
1206		 */
1207		if (!notice) {
1208			if (act == APPND)
1209				return(-1);
1210			paxwarn(1, "Cannot identify format. Searching...");
1211			++notice;
1212		}
1213
1214		/*
1215		 * brute force search for a header that we can id.
1216		 * we shift through byte at a time. this is slow, but we cannot
1217		 * determine the nature of the flaw in the archive in a
1218		 * portable manner
1219		 */
1220		if (--hdsz > 0) {
1221			memmove(hdbuf, hdbuf+1, hdsz);
1222			res = BLKMULT - hdsz;
1223			hdend = hdbuf + hdsz;
1224		} else {
1225			res = BLKMULT;
1226			hdend = hdbuf;
1227			hdsz = 0;
1228		}
1229	}
1230
1231    out:
1232	/*
1233	 * we cannot find a header, bow, apologize and quit
1234	 */
1235	paxwarn(1, "Sorry, unable to determine archive format.");
1236	return(-1);
1237}
1238