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