xref: /freebsd/bin/pax/pax.c (revision 952d112864d8008aa87278a30a539d888a8493cd)
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  *	$Id$
38  */
39 
40 #ifndef lint
41 static char const copyright[] =
42 "@(#) Copyright (c) 1992, 1993\n\
43 	The Regents of the University of California.  All rights reserved.\n";
44 #endif /* not lint */
45 
46 #ifndef lint
47 static char const sccsid[] = "@(#)pax.c	8.2 (Berkeley) 4/18/94";
48 #endif /* not lint */
49 
50 #include <stdio.h>
51 #include <sys/types.h>
52 #include <sys/param.h>
53 #include <sys/stat.h>
54 #include <sys/time.h>
55 #include <sys/resource.h>
56 #include <signal.h>
57 #include <unistd.h>
58 #include <stdlib.h>
59 #include <errno.h>
60 #include <locale.h>
61 #include "pax.h"
62 #include "extern.h"
63 static int gen_init __P((void));
64 
65 /*
66  * PAX main routines, general globals and some simple start up routines
67  */
68 
69 /*
70  * Variables that can be accessed by any routine within pax
71  */
72 int	act = DEFOP;		/* read/write/append/copy */
73 FSUB	*frmt = NULL;		/* archive format type */
74 int	cflag;			/* match all EXCEPT pattern/file */
75 int	dflag;			/* directory member match only  */
76 int	iflag;			/* interactive file/archive rename */
77 int	kflag;			/* do not overwrite existing files */
78 int	lflag;			/* use hard links when possible */
79 int	nflag;			/* select first archive member match */
80 int	tflag;			/* restore access time after read */
81 int	uflag;			/* ignore older modification time files */
82 int	vflag;			/* produce verbose output */
83 int	Dflag;			/* same as uflag except inode change time */
84 int	Hflag;			/* follow command line symlinks (write only) */
85 int	Lflag;			/* follow symlinks when writing */
86 int	Xflag;			/* archive files with same device id only */
87 int	Yflag;			/* same as Dflg except after name mode */
88 int	Zflag;			/* same as uflg except after name mode */
89 int	vfpart;			/* is partial verbose output in progress */
90 int	patime = 1;		/* preserve file access time */
91 int	pmtime = 1;		/* preserve file modification times */
92 int	pmode;			/* preserve file mode bits */
93 int	pids;			/* preserve file uid/gid */
94 int	exit_val;		/* exit value */
95 int	docrc;			/* check/create file crc */
96 char	*dirptr;		/* destination dir in a copy */
97 char	*ltmfrmt;		/* -v locale time format (if any) */
98 char	*argv0;			/* root of argv[0] */
99 sigset_t s_mask;		/* signal mask for cleanup critical sect */
100 
101 /*
102  *	PAX - Portable Archive Interchange
103  *
104  * 	A utility to read, write, and write lists of the members of archive
105  *	files and copy directory hierarchies. A variety of archive formats
106  *	are supported (some are described in POSIX 1003.1 10.1):
107  *
108  *		ustar - 10.1.1 extended tar interchange format
109  *		cpio  - 10.1.2 extended cpio interchange format
110  *		tar - old BSD 4.3 tar format
111  *		binary cpio - old cpio with binary header format
112  *		sysVR4 cpio -  with and without CRC
113  *
114  * This version is a superset of IEEE Std 1003.2b-d3
115  *
116  * Summary of Extensions to the IEEE Standard:
117  *
118  * 1	READ ENHANCEMENTS
119  * 1.1	Operations which read archives will continue to operate even when
120  *	processing archives which may be damaged, truncated, or fail to meet
121  *	format specs in several different ways. Damaged sections of archives
122  *	are detected and avoided if possible. Attempts will be made to resync
123  *	archive read operations even with badly damaged media.
124  * 1.2	Blocksize requirements are not strictly enforced on archive read.
125  *	Tapes which have variable sized records can be read without errors.
126  * 1.3	The user can specify via the non-standard option flag -E if error
127  *	resync operation should stop on a media error, try a specified number
128  *	of times to correct, or try to correct forever.
129  * 1.4	Sparse files (lseek holes) stored on the archive (but stored with blocks
130  *	of all zeros will be restored with holes appropriate for the target
131  *	filesystem
132  * 1.5	The user is notified whenever something is found during archive
133  *	read operations which violates spec (but the read will continue).
134  * 1.6	Multiple archive volumes can be read and may span over different
135  *	archive devices
136  * 1.7	Rigidly restores all file attributes exactly as they are stored on the
137  *	archive.
138  * 1.8	Modification change time ranges can be specified via multiple -T
139  *	options. These allow a user to select files whose modification time
140  *	lies within a specific time range.
141  * 1.9	Files can be selected based on owner (user name or uid) via one or more
142  *	-U options.
143  * 1.10	Files can be selected based on group (group name or gid) via one o
144  *	more -G options.
145  * 1.11	File modification time can be checked against exisiting file after
146  *	name modification (-Z)
147  *
148  * 2	WRITE ENHANCEMENTS
149  * 2.1	Write operation will stop instead of allowing a user to create a flawed
150  *	flawed archive (due to any problem).
151  * 2.2	Archives writtens by pax are forced to strictly conform to both the
152  *	archive and pax the spceific format specifications.
153  * 2.3	Blocking size and format is rigidly enforced on writes.
154  * 2.4	Formats which may exhibit header overflow problems (they have fields
155  *	too small for large file systems, such as inode number storage), use
156  *	routines designed to repair this problem. These techniques still
157  *	conform to both pax and format specifications, but no longer truncate
158  *	these fields. This removes any restrictions on using these archive
159  *	formats on large file systems.
160  * 2.5	Multiple archive volumes can be written and may span over different
161  *	archive devices
162  * 2.6	A archive volume record limit allows the user to specify the number
163  *	of bytes stored on an archive volume. When reached the user is
164  *	prompted for the next archive volume. This is specified with the
165  *	non-standard -B flag. THe limit is rounded up to the next blocksize.
166  * 2.7	All archive padding during write use zero filled sections. This makes
167  *	it much easier to pull data out of flawed archive during read
168  *	operations.
169  * 2.8	Access time reset with the -t applies to all file nodes (including
170  *	directories).
171  * 2.9	Symbolic links can be followed with -L (optional in the spec).
172  * 2.10	Modification or inode change time ranges can be specified via
173  *	multiple -T options. These allow a user to select files whose
174  *	modification or inode change time lies within a specific time range.
175  * 2.11	Files can be selected based on owner (user name or uid) via one or more
176  *	-U options.
177  * 2.12	Files can be selected based on group (group name or gid) via one o
178  *	more -G options.
179  * 2.13	Symlinks which appear on the command line can be followed (without
180  *	following other symlinks; -H flag)
181  *
182  * 3	COPY ENHANCEMENTS
183  * 3.1	Sparse files (lseek holes) can be copied without expanding the holes
184  *	into zero filled blocks. The file copy is created with holes which are
185  *	appropriate for the target filesystem
186  * 3.2	Access time as well as modification time on copied file trees can be
187  *	preserved with the appropriate -p options.
188  * 3.3	Access time reset with the -t applies to all file nodes (including
189  *	directories).
190  * 3.4	Symbolic links can be followed with -L (optional in the spec).
191  * 3.5	Modification or inode change time ranges can be specified via
192  *	multiple -T options. These allow a user to select files whose
193  *	modification or inode change time lies within a specific time range.
194  * 3.6	Files can be selected based on owner (user name or uid) via one or more
195  *	-U options.
196  * 3.7	Files can be selected based on group (group name or gid) via one o
197  *	more -G options.
198  * 3.8	Symlinks which appear on the command line can be followed (without
199  *	following other symlinks; -H flag)
200  * 3.9  File inode change time can be checked against exisiting file before
201  *	name modification (-D)
202  * 3.10 File inode change time can be checked against exisiting file after
203  *	name modification (-Y)
204  * 3.11	File modification time can be checked against exisiting file after
205  *	name modification (-Z)
206  *
207  * 4	GENERAL ENHANCEMENTS
208  * 4.1	Internal structure is designed to isolate format dependent and
209  *	independent functions. Formats are selected via a format driver table.
210  *	This encourages the addition of new archive formats by only having to
211  *	write those routines which id, read and write the archive header.
212  */
213 
214 /*
215  * main()
216  *	parse options, set up and operate as specified by the user.
217  *	any operational flaw will set exit_val to non-zero
218  * Return: 0 if ok, 1 otherwise
219  */
220 
221 #if __STDC__
222 int
223 main(int argc, char **argv)
224 #else
225 int
226 main(argc, argv)
227 	int argc;
228 	char **argv;
229 #endif
230 {
231 	(void) setlocale(LC_ALL, "");
232 	/*
233 	 * parse options, determine operational mode, general init
234 	 */
235 	options(argc, argv);
236         if ((gen_init() < 0) || (tty_init() < 0))
237 		return(exit_val);
238 
239 	/*
240 	 * select a primary operation mode
241 	 */
242 	switch(act) {
243 	case EXTRACT:
244 		extract();
245 		break;
246 	case ARCHIVE:
247 		archive();
248 		break;
249 	case APPND:
250 		append();
251 		break;
252 	case COPY:
253 		copy();
254 		break;
255 	default:
256 	case LIST:
257 		list();
258 		break;
259 	}
260 	return(exit_val);
261 }
262 
263 /*
264  * sig_cleanup()
265  *	when interrupted we try to do whatever delayed processing we can.
266  *	This is not critical, but we really ought to limit our damage when we
267  *	are aborted by the user.
268  * Return:
269  *	never....
270  */
271 
272 #if __STDC__
273 void
274 sig_cleanup(int which_sig)
275 #else
276 void
277 sig_cleanup(which_sig)
278 	int which_sig;
279 #endif
280 {
281 	/*
282 	 * restore modes and times for any dirs we may have created
283 	 * or any dirs we may have read. Set vflag and vfpart so the user
284 	 * will clearly see the message on a line by itself.
285 	 */
286 	vflag = vfpart = 1;
287 	if (which_sig == SIGXCPU)
288 		warn(0, "Cpu time limit reached, cleaning up.");
289 	else
290 		warn(0, "Signal caught, cleaning up.");
291 
292 	ar_close();
293 	proc_dir();
294 	if (tflag)
295 		atdir_end();
296 	exit(1);
297 }
298 
299 /*
300  * gen_init()
301  *	general setup routines. Not all are required, but they really help
302  *	when dealing with a medium to large sized archives.
303  */
304 
305 #if __STDC__
306 static int
307 gen_init(void)
308 #else
309 static int
310 gen_init()
311 #endif
312 {
313 	struct rlimit reslimit;
314 	struct sigaction n_hand;
315 	struct sigaction o_hand;
316 
317 	/*
318 	 * Really needed to handle large archives. We can run out of memory for
319 	 * internal tables really fast when we have a whole lot of files...
320 	 */
321 	if (getrlimit(RLIMIT_DATA , &reslimit) == 0){
322 		reslimit.rlim_cur = reslimit.rlim_max;
323 		(void)setrlimit(RLIMIT_DATA , &reslimit);
324 	}
325 
326 	/*
327 	 * should file size limits be waived? if the os limits us, this is
328 	 * needed if we want to write a large archive
329 	 */
330 	if (getrlimit(RLIMIT_FSIZE , &reslimit) == 0){
331 		reslimit.rlim_cur = reslimit.rlim_max;
332 		(void)setrlimit(RLIMIT_FSIZE , &reslimit);
333 	}
334 
335 	/*
336 	 * increase the size the stack can grow to
337 	 */
338 	if (getrlimit(RLIMIT_STACK , &reslimit) == 0){
339 		reslimit.rlim_cur = reslimit.rlim_max;
340 		(void)setrlimit(RLIMIT_STACK , &reslimit);
341 	}
342 
343 	/*
344 	 * not really needed, but doesn't hurt
345 	 */
346 	if (getrlimit(RLIMIT_RSS , &reslimit) == 0){
347 		reslimit.rlim_cur = reslimit.rlim_max;
348 		(void)setrlimit(RLIMIT_RSS , &reslimit);
349 	}
350 
351 	/*
352 	 * Handle posix locale
353 	 *
354 	 * set user defines time printing format for -v option
355 	 */
356 	ltmfrmt = getenv("LC_TIME");
357 
358 	/*
359 	 * signal handling to reset stored directory times and modes. Since
360 	 * we deal with broken pipes via failed writes we ignore it. We also
361 	 * deal with any file size limit thorugh failed writes. Cpu time
362 	 * limits are caught and a cleanup is forced.
363 	 */
364 	if ((sigemptyset(&s_mask) < 0) || (sigaddset(&s_mask, SIGTERM) < 0) ||
365 	    (sigaddset(&s_mask,SIGINT) < 0)||(sigaddset(&s_mask,SIGHUP) < 0) ||
366 	    (sigaddset(&s_mask,SIGPIPE) < 0)||(sigaddset(&s_mask,SIGQUIT)<0) ||
367 	    (sigaddset(&s_mask,SIGXCPU) < 0)||(sigaddset(&s_mask,SIGXFSZ)<0)) {
368 		warn(1, "Unable to set up signal mask");
369 		return(-1);
370 	}
371 	n_hand.sa_mask = s_mask;
372 	n_hand.sa_flags = 0;
373 	n_hand.sa_handler = sig_cleanup;
374 
375 	if ((sigaction(SIGHUP, &n_hand, &o_hand) < 0) &&
376 	    (o_hand.sa_handler == SIG_IGN) &&
377 	    (sigaction(SIGHUP, &o_hand, &o_hand) < 0))
378 		goto out;
379 
380 	if ((sigaction(SIGTERM, &n_hand, &o_hand) < 0) &&
381 	    (o_hand.sa_handler == SIG_IGN) &&
382 	    (sigaction(SIGTERM, &o_hand, &o_hand) < 0))
383 		goto out;
384 
385 	if ((sigaction(SIGINT, &n_hand, &o_hand) < 0) &&
386 	    (o_hand.sa_handler == SIG_IGN) &&
387 	    (sigaction(SIGINT, &o_hand, &o_hand) < 0))
388 		goto out;
389 
390 	if ((sigaction(SIGQUIT, &n_hand, &o_hand) < 0) &&
391 	    (o_hand.sa_handler == SIG_IGN) &&
392 	    (sigaction(SIGQUIT, &o_hand, &o_hand) < 0))
393 		goto out;
394 
395 	if ((sigaction(SIGXCPU, &n_hand, &o_hand) < 0) &&
396 	    (o_hand.sa_handler == SIG_IGN) &&
397 	    (sigaction(SIGXCPU, &o_hand, &o_hand) < 0))
398 		goto out;
399 
400 	n_hand.sa_handler = SIG_IGN;
401 	if ((sigaction(SIGPIPE, &n_hand, &o_hand) < 0) ||
402 	    (sigaction(SIGXFSZ, &n_hand, &o_hand) < 0))
403 		goto out;
404 	return(0);
405 
406     out:
407 	syswarn(1, errno, "Unable to set up signal handler");
408 	return(-1);
409 }
410