source: trunk/minix/commands/pax/pax.c@ 15

Last change on this file since 15 was 9, checked in by Mattia Monga, 14 years ago

Minix 3.1.2a

File size: 14.8 KB
Line 
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 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#if 0
35#ifndef lint
36static char const copyright[] =
37"@(#) Copyright (c) 1992, 1993\n\
38 The Regents of the University of California. All rights reserved.\n";
39#endif /* not lint */
40
41#ifndef lint
42static char sccsid[] = "@(#)pax.c 8.2 (Berkeley) 4/18/94";
43#endif /* not lint */
44#endif
45
46#include <sys/types.h>
47#include <sys/stat.h>
48#include <sys/time.h>
49#include <sys/resource.h>
50#include <errno.h>
51#include <fcntl.h>
52#include <locale.h>
53#include <minix/paths.h>
54#include <signal.h>
55#include <stdio.h>
56#include <stdlib.h>
57#include <string.h>
58#include <unistd.h>
59#include "pax.h"
60#include "extern.h"
61static int gen_init(void);
62
63/*
64 * PAX main routines, general globals and some simple start up routines
65 */
66
67/*
68 * Variables that can be accessed by any routine within pax
69 */
70int act = DEFOP; /* read/write/append/copy */
71FSUB *frmt = NULL; /* archive format type */
72int cflag; /* match all EXCEPT pattern/file */
73int cwdfd; /* starting cwd */
74int dflag; /* directory member match only */
75int iflag; /* interactive file/archive rename */
76int kflag; /* do not overwrite existing files */
77int lflag; /* use hard links when possible */
78int nflag; /* select first archive member match */
79int tflag; /* restore access time after read */
80int uflag; /* ignore older modification time files */
81int vflag; /* produce verbose output */
82int Dflag; /* same as uflag except inode change time */
83int Hflag; /* follow command line symlinks (write only) */
84int Lflag; /* follow symlinks when writing */
85int Xflag; /* archive files with same device id only */
86int Yflag; /* same as Dflg except after name mode */
87int Zflag; /* same as uflg except after name mode */
88int vfpart; /* is partial verbose output in progress */
89int patime = 1; /* preserve file access time */
90int pmtime = 1; /* preserve file modification times */
91int nodirs; /* do not create directories as needed */
92int pmode; /* preserve file mode bits */
93int pids; /* preserve file uid/gid */
94int rmleadslash = 0; /* remove leading '/' from pathnames */
95int exit_val; /* exit value */
96int docrc; /* check/create file crc */
97char *dirptr; /* destination dir in a copy */
98const char *argv0; /* root of argv[0] */
99sigset_t s_mask; /* signal mask for cleanup critical sect */
100FILE *listf; /* file pointer to print file list to */
101char *tempfile; /* tempfile to use for mkstemp(3) */
102char *tempbase; /* basename of tempfile to use for mkstemp(3) */
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 * file system
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 file system
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
224int
225main(int argc, char *argv[])
226{
227 const char *tmpdir;
228 size_t tdlen;
229
230 (void) setlocale(LC_ALL, "");
231 listf = stderr;
232 /*
233 * Keep a reference to cwd, so we can always come back home.
234 */
235 cwdfd = open(".", O_RDONLY);
236 if (cwdfd < 0) {
237 syswarn(0, errno, "Can't open current working directory.");
238 return(exit_val);
239 }
240
241 /*
242 * Where should we put temporary files?
243 */
244 if ((tmpdir = getenv("TMPDIR")) == NULL || *tmpdir == '\0')
245 tmpdir = _PATH_TMP;
246 tdlen = strlen(tmpdir);
247 while(tdlen > 0 && tmpdir[tdlen - 1] == '/')
248 tdlen--;
249 tempfile = malloc(tdlen + 1 + sizeof(_TFILE_BASE));
250 if (tempfile == NULL) {
251 paxwarn(1, "Cannot allocate memory for temp file name.");
252 return(exit_val);
253 }
254 if (tdlen)
255 memcpy(tempfile, tmpdir, tdlen);
256 tempbase = tempfile + tdlen;
257 *tempbase++ = '/';
258
259 /*
260 * parse options, determine operational mode, general init
261 */
262 options(argc, argv);
263 if ((gen_init() < 0) || (tty_init() < 0))
264 return(exit_val);
265
266 /*
267 * select a primary operation mode
268 */
269 switch(act) {
270 case EXTRACT:
271 extract();
272 break;
273 case ARCHIVE:
274 archive();
275 break;
276 case APPND:
277 if (gzip_program != NULL)
278 err(1, "can not gzip while appending");
279 append();
280 break;
281 case COPY:
282 copy();
283 break;
284 default:
285 case LIST:
286 list();
287 break;
288 }
289 return(exit_val);
290}
291
292/*
293 * sig_cleanup()
294 * when interrupted we try to do whatever delayed processing we can.
295 * This is not critical, but we really ought to limit our damage when we
296 * are aborted by the user.
297 * Return:
298 * never....
299 */
300
301void
302sig_cleanup(int which_sig)
303{
304 /*
305 * restore modes and times for any dirs we may have created
306 * or any dirs we may have read. Set vflag and vfpart so the user
307 * will clearly see the message on a line by itself.
308 */
309 vflag = vfpart = 1;
310#if 0
311 /* ignore this under minix */
312 if (which_sig == SIGXCPU)
313 paxwarn(0, "Cpu time limit reached, cleaning up.");
314 else
315#endif
316 paxwarn(0, "Signal caught, cleaning up.");
317
318 ar_close();
319 proc_dir();
320 if (tflag)
321 atdir_end();
322 exit(1);
323}
324
325/*
326 * gen_init()
327 * general setup routines. Not all are required, but they really help
328 * when dealing with a medium to large sized archives.
329 */
330
331static int
332gen_init(void)
333{
334#if 0
335 struct rlimit reslimit;
336#endif
337 struct sigaction n_hand;
338 struct sigaction o_hand;
339
340#if 0
341 /*
342 * Really needed to handle large archives. We can run out of memory for
343 * internal tables really fast when we have a whole lot of files...
344 */
345 if (getrlimit(RLIMIT_DATA , &reslimit) == 0){
346 reslimit.rlim_cur = reslimit.rlim_max;
347 (void)setrlimit(RLIMIT_DATA , &reslimit);
348 }
349
350 /*
351 * should file size limits be waived? if the os limits us, this is
352 * needed if we want to write a large archive
353 */
354 if (getrlimit(RLIMIT_FSIZE , &reslimit) == 0){
355 reslimit.rlim_cur = reslimit.rlim_max;
356 (void)setrlimit(RLIMIT_FSIZE , &reslimit);
357 }
358
359 /*
360 * increase the size the stack can grow to
361 */
362 if (getrlimit(RLIMIT_STACK , &reslimit) == 0){
363 reslimit.rlim_cur = reslimit.rlim_max;
364 (void)setrlimit(RLIMIT_STACK , &reslimit);
365 }
366
367 /*
368 * not really needed, but doesn't hurt
369 */
370 if (getrlimit(RLIMIT_RSS , &reslimit) == 0){
371 reslimit.rlim_cur = reslimit.rlim_max;
372 (void)setrlimit(RLIMIT_RSS , &reslimit);
373 }
374#endif
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
383 if ((sigemptyset(&s_mask) < 0) || (sigaddset(&s_mask, SIGTERM) < 0) ||
384 (sigaddset(&s_mask,SIGINT) < 0)||(sigaddset(&s_mask,SIGHUP) < 0) ||
385 (sigaddset(&s_mask,SIGPIPE) < 0)||(sigaddset(&s_mask,SIGQUIT)<0)
386#if 0
387 || (sigaddset(&s_mask,SIGXCPU) < 0)||(sigaddset(&s_mask,SIGXFSZ)<0)
388#endif
389 ) {
390 paxwarn(1, "Unable to set up signal mask");
391 return(-1);
392 }
393 memset(&n_hand, 0, sizeof n_hand);
394 n_hand.sa_mask = s_mask;
395 n_hand.sa_flags = 0;
396 n_hand.sa_handler = sig_cleanup;
397
398 if ((sigaction(SIGHUP, &n_hand, &o_hand) < 0) &&
399 (o_hand.sa_handler == SIG_IGN) &&
400 (sigaction(SIGHUP, &o_hand, &o_hand) < 0))
401 goto out;
402
403 if ((sigaction(SIGTERM, &n_hand, &o_hand) < 0) &&
404 (o_hand.sa_handler == SIG_IGN) &&
405 (sigaction(SIGTERM, &o_hand, &o_hand) < 0))
406 goto out;
407
408 if ((sigaction(SIGINT, &n_hand, &o_hand) < 0) &&
409 (o_hand.sa_handler == SIG_IGN) &&
410 (sigaction(SIGINT, &o_hand, &o_hand) < 0))
411 goto out;
412
413 if ((sigaction(SIGQUIT, &n_hand, &o_hand) < 0) &&
414 (o_hand.sa_handler == SIG_IGN) &&
415 (sigaction(SIGQUIT, &o_hand, &o_hand) < 0))
416 goto out;
417
418#if 0
419 if ((sigaction(SIGXCPU, &n_hand, &o_hand) < 0) &&
420 (o_hand.sa_handler == SIG_IGN) &&
421 (sigaction(SIGXCPU, &o_hand, &o_hand) < 0))
422 goto out;
423#endif
424
425 n_hand.sa_handler = SIG_IGN;
426 if ((sigaction(SIGPIPE, &n_hand, &o_hand) < 0)
427#if 0
428 || (sigaction(SIGXFSZ, &n_hand, &o_hand) < 0)
429#endif
430 )
431
432
433 goto out;
434 return(0);
435
436 out:
437 syswarn(1, errno, "Unable to set up signal handler");
438 return(-1);
439}
Note: See TracBrowser for help on using the repository browser.