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