Home | History | Annotate | Line # | Download | only in pax
pax.c revision 1.29
      1 /*	$NetBSD: pax.c,v 1.29 2003/08/07 09:05:22 agc Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1992, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * This code is derived from software contributed to Berkeley by
      8  * Keith Muller of the University of California, San Diego.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. Neither the name of the University nor the names of its contributors
     19  *    may be used to endorse or promote products derived from this software
     20  *    without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  * SUCH DAMAGE.
     33  */
     34 
     35 /*-
     36  * Copyright (c) 1992 Keith Muller.
     37  *
     38  * This code is derived from software contributed to Berkeley by
     39  * Keith Muller of the University of California, San Diego.
     40  *
     41  * Redistribution and use in source and binary forms, with or without
     42  * modification, are permitted provided that the following conditions
     43  * are met:
     44  * 1. Redistributions of source code must retain the above copyright
     45  *    notice, this list of conditions and the following disclaimer.
     46  * 2. Redistributions in binary form must reproduce the above copyright
     47  *    notice, this list of conditions and the following disclaimer in the
     48  *    documentation and/or other materials provided with the distribution.
     49  * 3. All advertising materials mentioning features or use of this software
     50  *    must display the following acknowledgement:
     51  *	This product includes software developed by the University of
     52  *	California, Berkeley and its contributors.
     53  * 4. Neither the name of the University nor the names of its contributors
     54  *    may be used to endorse or promote products derived from this software
     55  *    without specific prior written permission.
     56  *
     57  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     58  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     59  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     60  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     61  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     62  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     63  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     64  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     65  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     66  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     67  * SUCH DAMAGE.
     68  */
     69 
     70 #include <sys/cdefs.h>
     71 #if defined(__COPYRIGHT) && !defined(lint)
     72 __COPYRIGHT("@(#) Copyright (c) 1992, 1993\n\
     73 	The Regents of the University of California.  All rights reserved.\n");
     74 #endif /* not lint */
     75 
     76 #if defined(__RCSID) && !defined(lint)
     77 #if 0
     78 static char sccsid[] = "@(#)pax.c	8.2 (Berkeley) 4/18/94";
     79 #else
     80 __RCSID("$NetBSD: pax.c,v 1.29 2003/08/07 09:05:22 agc Exp $");
     81 #endif
     82 #endif /* not lint */
     83 
     84 #include <sys/types.h>
     85 #include <sys/param.h>
     86 #include <sys/stat.h>
     87 #include <sys/time.h>
     88 #include <sys/resource.h>
     89 #include <stdio.h>
     90 #include <fcntl.h>
     91 #include <signal.h>
     92 #include <unistd.h>
     93 #include <stdlib.h>
     94 #include <string.h>
     95 #include <errno.h>
     96 #include <paths.h>
     97 #include "pax.h"
     98 #include "extern.h"
     99 static int gen_init(void);
    100 
    101 /*
    102  * PAX main routines, general globals and some simple start up routines
    103  */
    104 
    105 /*
    106  * Variables that can be accessed by any routine within pax
    107  */
    108 int	act = ERROR;		/* read/write/append/copy */
    109 FSUB	*frmt = NULL;		/* archive format type */
    110 int	cflag;			/* match all EXCEPT pattern/file */
    111 int	cwdfd;			/* starting cwd */
    112 int	dflag;			/* directory member match only  */
    113 int	iflag;			/* interactive file/archive rename */
    114 int	jflag;			/* pass through bzip2 */
    115 int	kflag;			/* do not overwrite existing files */
    116 int	lflag;			/* use hard links when possible */
    117 int	nflag;			/* select first archive member match */
    118 int	tflag;			/* restore access time after read */
    119 int	uflag;			/* ignore older modification time files */
    120 int	vflag;			/* produce verbose output */
    121 int	zflag;			/* use gzip */
    122 int	Aflag;			/* honor absolute path */
    123 int	Dflag;			/* same as uflag except inode change time */
    124 int	Hflag;			/* follow command line symlinks (write only) */
    125 int	Lflag;			/* follow symlinks when writing */
    126 int	Mflag;			/* treat stdin as an mtree(8) specfile */
    127 int	Xflag;			/* archive files with same device id only */
    128 int	Yflag;			/* same as Dflg except after name mode */
    129 int	Zflag;			/* same as uflg except after name mode */
    130 int	vfpart;			/* is partial verbose output in progress */
    131 int	patime = 1;		/* preserve file access time */
    132 int	pmtime = 1;		/* preserve file modification times */
    133 int	nodirs;			/* do not create directories as needed */
    134 int	pfflags = 1;		/* preserve file flags */
    135 int	pmode;			/* preserve file mode bits */
    136 int	pids;			/* preserve file uid/gid */
    137 int	rmleadslash = 0;	/* remove leading '/' from pathnames */
    138 int	exit_val;		/* exit value */
    139 int	docrc;			/* check/create file crc */
    140 int	to_stdout;		/* extract to stdout */
    141 char	*dirptr;		/* destination dir in a copy */
    142 char	*ltmfrmt;		/* -v locale time format (if any) */
    143 char	*argv0;			/* root of argv[0] */
    144 sigset_t s_mask;		/* signal mask for cleanup critical sect */
    145 FILE	*listf;			/* file pointer to print file list to */
    146 char	*tempfile;		/* tempfile to use for mkstemp(3) */
    147 char	*tempbase;		/* basename of tempfile to use for mkstemp(3) */
    148 int	forcelocal;		/* force local operation even if the name
    149 				 * contains a :
    150 				 */
    151 int	secure = 1;		/* don't extract names that contain .. */
    152 
    153 /*
    154  *	PAX - Portable Archive Interchange
    155  *
    156  *	A utility to read, write, and write lists of the members of archive
    157  *	files and copy directory hierarchies. A variety of archive formats
    158  *	are supported (some are described in POSIX 1003.1 10.1):
    159  *
    160  *		ustar - 10.1.1 extended tar interchange format
    161  *		cpio  - 10.1.2 extended cpio interchange format
    162  *		tar - old BSD 4.3 tar format
    163  *		binary cpio - old cpio with binary header format
    164  *		sysVR4 cpio -  with and without CRC
    165  *
    166  * This version is a superset of IEEE Std 1003.2b-d3
    167  *
    168  * Summary of Extensions to the IEEE Standard:
    169  *
    170  * 1	READ ENHANCEMENTS
    171  * 1.1	Operations which read archives will continue to operate even when
    172  *	processing archives which may be damaged, truncated, or fail to meet
    173  *	format specs in several different ways. Damaged sections of archives
    174  *	are detected and avoided if possible. Attempts will be made to resync
    175  *	archive read operations even with badly damaged media.
    176  * 1.2	Blocksize requirements are not strictly enforced on archive read.
    177  *	Tapes which have variable sized records can be read without errors.
    178  * 1.3	The user can specify via the non-standard option flag -E if error
    179  *	resync operation should stop on a media error, try a specified number
    180  *	of times to correct, or try to correct forever.
    181  * 1.4	Sparse files (lseek holes) stored on the archive (but stored with blocks
    182  *	of all zeros will be restored with holes appropriate for the target
    183  *	filesystem
    184  * 1.5	The user is notified whenever something is found during archive
    185  *	read operations which violates spec (but the read will continue).
    186  * 1.6	Multiple archive volumes can be read and may span over different
    187  *	archive devices
    188  * 1.7	Rigidly restores all file attributes exactly as they are stored on the
    189  *	archive.
    190  * 1.8	Modification change time ranges can be specified via multiple -T
    191  *	options. These allow a user to select files whose modification time
    192  *	lies within a specific time range.
    193  * 1.9	Files can be selected based on owner (user name or uid) via one or more
    194  *	-U options.
    195  * 1.10	Files can be selected based on group (group name or gid) via one o
    196  *	more -G options.
    197  * 1.11	File modification time can be checked against existing file after
    198  *	name modification (-Z)
    199  *
    200  * 2	WRITE ENHANCEMENTS
    201  * 2.1	Write operation will stop instead of allowing a user to create a flawed
    202  *	flawed archive (due to any problem).
    203  * 2.2	Archives written by pax are forced to strictly conform to both the
    204  *	archive and pax the spceific format specifications.
    205  * 2.3	Blocking size and format is rigidly enforced on writes.
    206  * 2.4	Formats which may exhibit header overflow problems (they have fields
    207  *	too small for large file systems, such as inode number storage), use
    208  *	routines designed to repair this problem. These techniques still
    209  *	conform to both pax and format specifications, but no longer truncate
    210  *	these fields. This removes any restrictions on using these archive
    211  *	formats on large file systems.
    212  * 2.5	Multiple archive volumes can be written and may span over different
    213  *	archive devices
    214  * 2.6	A archive volume record limit allows the user to specify the number
    215  *	of bytes stored on an archive volume. When reached the user is
    216  *	prompted for the next archive volume. This is specified with the
    217  *	non-standard -B flag. The limit is rounded up to the next blocksize.
    218  * 2.7	All archive padding during write use zero filled sections. This makes
    219  *	it much easier to pull data out of flawed archive during read
    220  *	operations.
    221  * 2.8	Access time reset with the -t applies to all file nodes (including
    222  *	directories).
    223  * 2.9	Symbolic links can be followed with -L (optional in the spec).
    224  * 2.10	Modification or inode change time ranges can be specified via
    225  *	multiple -T options. These allow a user to select files whose
    226  *	modification or inode change time lies within a specific time range.
    227  * 2.11	Files can be selected based on owner (user name or uid) via one or more
    228  *	-U options.
    229  * 2.12	Files can be selected based on group (group name or gid) via one o
    230  *	more -G options.
    231  * 2.13	Symlinks which appear on the command line can be followed (without
    232  *	following other symlinks; -H flag)
    233  *
    234  * 3	COPY ENHANCEMENTS
    235  * 3.1	Sparse files (lseek holes) can be copied without expanding the holes
    236  *	into zero filled blocks. The file copy is created with holes which are
    237  *	appropriate for the target filesystem
    238  * 3.2	Access time as well as modification time on copied file trees can be
    239  *	preserved with the appropriate -p options.
    240  * 3.3	Access time reset with the -t applies to all file nodes (including
    241  *	directories).
    242  * 3.4	Symbolic links can be followed with -L (optional in the spec).
    243  * 3.5	Modification or inode change time ranges can be specified via
    244  *	multiple -T options. These allow a user to select files whose
    245  *	modification or inode change time lies within a specific time range.
    246  * 3.6	Files can be selected based on owner (user name or uid) via one or more
    247  *	-U options.
    248  * 3.7	Files can be selected based on group (group name or gid) via one o
    249  *	more -G options.
    250  * 3.8	Symlinks which appear on the command line can be followed (without
    251  *	following other symlinks; -H flag)
    252  * 3.9	File inode change time can be checked against existing file before
    253  *	name modification (-D)
    254  * 3.10	File inode change time can be checked against existing file after
    255  *	name modification (-Y)
    256  * 3.11	File modification time can be checked against existing file after
    257  *	name modification (-Z)
    258  *
    259  * 4	GENERAL ENHANCEMENTS
    260  * 4.1	Internal structure is designed to isolate format dependent and
    261  *	independent functions. Formats are selected via a format driver table.
    262  *	This encourages the addition of new archive formats by only having to
    263  *	write those routines which id, read and write the archive header.
    264  */
    265 
    266 /*
    267  * main()
    268  *	parse options, set up and operate as specified by the user.
    269  *	any operational flaw will set exit_val to non-zero
    270  * Return: 0 if ok, 1 otherwise
    271  */
    272 
    273 int
    274 main(int argc, char **argv)
    275 {
    276 	char *tmpdir;
    277 	size_t tdlen;
    278 
    279 	setprogname(argv[0]);
    280 
    281 	listf = stderr;
    282 
    283 	/*
    284 	 * Keep a reference to cwd, so we can always come back home.
    285 	 */
    286 	cwdfd = open(".", O_RDONLY);
    287 	if (cwdfd < 0) {
    288 		syswarn(0, errno, "Can't open current working directory.");
    289 		return(exit_val);
    290 	}
    291 
    292 	/*
    293 	 * Where should we put temporary files?
    294 	 */
    295 	if ((tmpdir = getenv("TMPDIR")) == NULL || *tmpdir == '\0')
    296 		tmpdir = _PATH_TMP;
    297 	tdlen = strlen(tmpdir);
    298 	while(tdlen > 0 && tmpdir[tdlen - 1] == '/')
    299 		tdlen--;
    300 	tempfile = malloc(tdlen + 1 + sizeof(_TFILE_BASE));
    301 	if (tempfile == NULL) {
    302 		tty_warn(1, "Cannot allocate memory for temp file name.");
    303 		return(exit_val);
    304 	}
    305 	if (tdlen)
    306 		memcpy(tempfile, tmpdir, tdlen);
    307 	tempbase = tempfile + tdlen;
    308 	*tempbase++ = '/';
    309 
    310 	/*
    311 	 * parse options, determine operational mode, general init
    312 	 */
    313 	options(argc, argv);
    314 	if ((gen_init() < 0) || (tty_init() < 0))
    315 		return(exit_val);
    316 
    317 	(void)time(&starttime);
    318 #ifdef SIGINFO
    319 	(void)signal(SIGINFO, ar_summary);
    320 #endif
    321 	/*
    322 	 * select a primary operation mode
    323 	 */
    324 	switch(act) {
    325 	case EXTRACT:
    326 		extract();
    327 		break;
    328 	case ARCHIVE:
    329 		archive();
    330 		break;
    331 	case APPND:
    332 		if (gzip_program != NULL)
    333 			err(1, "cannot gzip while appending");
    334 		append();
    335 		/*
    336 		 * Check if we tried to append on an empty file and
    337 		 * turned into ARCHIVE mode.
    338 		 */
    339 		if (act == -ARCHIVE) {
    340 			act = ARCHIVE;
    341 			archive();
    342 		}
    343 		break;
    344 	case COPY:
    345 		copy();
    346 		break;
    347 	default:
    348 	case LIST:
    349 		list();
    350 		break;
    351 	}
    352 	return(exit_val);
    353 }
    354 
    355 /*
    356  * sig_cleanup()
    357  *	when interrupted we try to do whatever delayed processing we can.
    358  *	This is not critical, but we really ought to limit our damage when we
    359  *	are aborted by the user.
    360  * Return:
    361  *	never....
    362  */
    363 
    364 void
    365 sig_cleanup(int which_sig)
    366 {
    367 	/*
    368 	 * restore modes and times for any dirs we may have created
    369 	 * or any dirs we may have read. Set vflag and vfpart so the user
    370 	 * will clearly see the message on a line by itself.
    371 	 */
    372 	vflag = vfpart = 1;
    373 	if (which_sig == SIGXCPU)
    374 		tty_warn(0, "Cpu time limit reached, cleaning up.");
    375 	else
    376 		tty_warn(0, "Signal caught, cleaning up.");
    377 
    378 	ar_close();
    379 	proc_dir();
    380 	if (tflag)
    381 		atdir_end();
    382 	exit(1);
    383 }
    384 
    385 /*
    386  * gen_init()
    387  *	general setup routines. Not all are required, but they really help
    388  *	when dealing with a medium to large sized archives.
    389  */
    390 
    391 static int
    392 gen_init(void)
    393 {
    394 	struct rlimit reslimit;
    395 	struct sigaction n_hand;
    396 	struct sigaction o_hand;
    397 
    398 	/*
    399 	 * Really needed to handle large archives. We can run out of memory for
    400 	 * internal tables really fast when we have a whole lot of files...
    401 	 */
    402 	if (getrlimit(RLIMIT_DATA , &reslimit) == 0){
    403 		reslimit.rlim_cur = reslimit.rlim_max;
    404 		(void)setrlimit(RLIMIT_DATA , &reslimit);
    405 	}
    406 
    407 	/*
    408 	 * should file size limits be waived? if the os limits us, this is
    409 	 * needed if we want to write a large archive
    410 	 */
    411 	if (getrlimit(RLIMIT_FSIZE , &reslimit) == 0){
    412 		reslimit.rlim_cur = reslimit.rlim_max;
    413 		(void)setrlimit(RLIMIT_FSIZE , &reslimit);
    414 	}
    415 
    416 	/*
    417 	 * increase the size the stack can grow to
    418 	 */
    419 	if (getrlimit(RLIMIT_STACK , &reslimit) == 0){
    420 		reslimit.rlim_cur = reslimit.rlim_max;
    421 		(void)setrlimit(RLIMIT_STACK , &reslimit);
    422 	}
    423 
    424 #ifdef RLIMIT_RSS
    425 	/*
    426 	 * not really needed, but doesn't hurt
    427 	 */
    428 	if (getrlimit(RLIMIT_RSS , &reslimit) == 0){
    429 		reslimit.rlim_cur = reslimit.rlim_max;
    430 		(void)setrlimit(RLIMIT_RSS , &reslimit);
    431 	}
    432 #endif
    433 
    434 	/*
    435 	 * Handle posix locale
    436 	 *
    437 	 * set user defines time printing format for -v option
    438 	 */
    439 	ltmfrmt = getenv("LC_TIME");
    440 
    441 	/*
    442 	 * signal handling to reset stored directory times and modes. Since
    443 	 * we deal with broken pipes via failed writes we ignore it. We also
    444 	 * deal with any file size limit thorugh failed writes. Cpu time
    445 	 * limits are caught and a cleanup is forced.
    446 	 */
    447 	if ((sigemptyset(&s_mask) < 0) || (sigaddset(&s_mask, SIGTERM) < 0) ||
    448 	    (sigaddset(&s_mask,SIGINT) < 0)||(sigaddset(&s_mask,SIGHUP) < 0) ||
    449 	    (sigaddset(&s_mask,SIGPIPE) < 0)||(sigaddset(&s_mask,SIGQUIT)<0) ||
    450 	    (sigaddset(&s_mask,SIGXCPU) < 0)||(sigaddset(&s_mask,SIGXFSZ)<0)) {
    451 		tty_warn(1, "Unable to set up signal mask");
    452 		return(-1);
    453 	}
    454 	memset(&n_hand, 0, sizeof n_hand);
    455 	n_hand.sa_mask = s_mask;
    456 	n_hand.sa_flags = 0;
    457 	n_hand.sa_handler = sig_cleanup;
    458 
    459 	if ((sigaction(SIGHUP, &n_hand, &o_hand) < 0) &&
    460 	    (o_hand.sa_handler == SIG_IGN) &&
    461 	    (sigaction(SIGHUP, &o_hand, &o_hand) < 0))
    462 		goto out;
    463 
    464 	if ((sigaction(SIGTERM, &n_hand, &o_hand) < 0) &&
    465 	    (o_hand.sa_handler == SIG_IGN) &&
    466 	    (sigaction(SIGTERM, &o_hand, &o_hand) < 0))
    467 		goto out;
    468 
    469 	if ((sigaction(SIGINT, &n_hand, &o_hand) < 0) &&
    470 	    (o_hand.sa_handler == SIG_IGN) &&
    471 	    (sigaction(SIGINT, &o_hand, &o_hand) < 0))
    472 		goto out;
    473 
    474 	if ((sigaction(SIGQUIT, &n_hand, &o_hand) < 0) &&
    475 	    (o_hand.sa_handler == SIG_IGN) &&
    476 	    (sigaction(SIGQUIT, &o_hand, &o_hand) < 0))
    477 		goto out;
    478 
    479 	if ((sigaction(SIGXCPU, &n_hand, &o_hand) < 0) &&
    480 	    (o_hand.sa_handler == SIG_IGN) &&
    481 	    (sigaction(SIGXCPU, &o_hand, &o_hand) < 0))
    482 		goto out;
    483 
    484 	n_hand.sa_handler = SIG_IGN;
    485 	if ((sigaction(SIGPIPE, &n_hand, &o_hand) < 0) ||
    486 	    (sigaction(SIGXFSZ, &n_hand, &o_hand) < 0))
    487 		goto out;
    488 	return(0);
    489 
    490     out:
    491 	syswarn(1, errno, "Unable to set up signal handler");
    492 	return(-1);
    493 }
    494