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