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