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