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