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