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