source: freewrt/tools/paxmirabilis/src/pax.c@ a569125

freewrt_1_0 freewrt_2_0
Last change on this file since a569125 was a569125, checked in by Thorsten Glaser <tg@…>, 14 years ago

even FreeWRT 1.0-stable deserves paxmirabilis-20120216 compiled with LTO ☺

git-svn-id: svn://www.freewrt.org/branches/freewrt_1_0@3981 afb5a338-a214-0410-bd46-81f09a774fd1

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