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