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