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