]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.bin/tar/read.c
This commit was generated by cvs2svn to compensate for changes in r178525,
[FreeBSD/FreeBSD.git] / usr.bin / tar / read.c
1 /*-
2  * Copyright (c) 2003-2007 Tim Kientzle
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #include "bsdtar_platform.h"
27 __FBSDID("$FreeBSD$");
28
29 #ifdef HAVE_SYS_TYPES_H
30 #include <sys/types.h>
31 #endif
32 #ifdef MAJOR_IN_MKDEV
33 #include <sys/mkdev.h>
34 #elif defined(MAJOR_IN_SYSMACROS)
35 #include <sys/sysmacros.h>
36 #endif
37 #ifdef HAVE_SYS_PARAM_H
38 #include <sys/param.h>
39 #endif
40 #ifdef HAVE_SYS_STAT_H
41 #include <sys/stat.h>
42 #endif
43
44 #ifdef HAVE_ERRNO_H
45 #include <errno.h>
46 #endif
47 #ifdef HAVE_GRP_H
48 #include <grp.h>
49 #endif
50 #ifdef HAVE_LIMITS_H
51 #include <limits.h>
52 #endif
53 #ifdef HAVE_PWD_H
54 #include <pwd.h>
55 #endif
56 #include <stdio.h>
57 #ifdef HAVE_STDLIB_H
58 #include <stdlib.h>
59 #endif
60 #ifdef HAVE_STRING_H
61 #include <string.h>
62 #endif
63 #ifdef HAVE_TIME_H
64 #include <time.h>
65 #endif
66 #ifdef HAVE_UNISTD_H
67 #include <unistd.h>
68 #endif
69
70 #include "bsdtar.h"
71
72 static void     list_item_verbose(struct bsdtar *, FILE *,
73                     struct archive_entry *);
74 static void     read_archive(struct bsdtar *bsdtar, char mode);
75
76 void
77 tar_mode_t(struct bsdtar *bsdtar)
78 {
79         read_archive(bsdtar, 't');
80 }
81
82 void
83 tar_mode_x(struct bsdtar *bsdtar)
84 {
85         read_archive(bsdtar, 'x');
86 }
87
88 /*
89  * Handle 'x' and 't' modes.
90  */
91 static void
92 read_archive(struct bsdtar *bsdtar, char mode)
93 {
94         FILE                     *out;
95         struct archive           *a;
96         struct archive_entry     *entry;
97         const struct stat        *st;
98         int                       r;
99
100         while (*bsdtar->argv) {
101                 include(bsdtar, *bsdtar->argv);
102                 bsdtar->argv++;
103         }
104
105         if (bsdtar->names_from_file != NULL)
106                 include_from_file(bsdtar, bsdtar->names_from_file);
107
108         a = archive_read_new();
109         if (bsdtar->compress_program != NULL)
110                 archive_read_support_compression_program(a, bsdtar->compress_program);
111         else
112                 archive_read_support_compression_all(a);
113         archive_read_support_format_all(a);
114         if (archive_read_open_file(a, bsdtar->filename,
115             bsdtar->bytes_per_block != 0 ? bsdtar->bytes_per_block :
116             DEFAULT_BYTES_PER_BLOCK))
117                 bsdtar_errc(bsdtar, 1, 0, "Error opening archive: %s",
118                     archive_error_string(a));
119
120         do_chdir(bsdtar);
121
122         if (mode == 'x' && bsdtar->option_chroot) {
123 #if HAVE_CHROOT
124                 if (chroot(".") != 0)
125                         bsdtar_errc(bsdtar, 1, errno, "Can't chroot to \".\"");
126 #else
127                 bsdtar_errc(bsdtar, 1, 0,
128                     "chroot isn't supported on this platform");
129 #endif
130         }
131
132         for (;;) {
133                 /* Support --fast-read option */
134                 if (bsdtar->option_fast_read &&
135                     unmatched_inclusions(bsdtar) == 0)
136                         break;
137
138                 r = archive_read_next_header(a, &entry);
139                 if (r == ARCHIVE_EOF)
140                         break;
141                 if (r < ARCHIVE_OK)
142                         bsdtar_warnc(bsdtar, 0, "%s", archive_error_string(a));
143                 if (r <= ARCHIVE_WARN)
144                         bsdtar->return_value = 1;
145                 if (r == ARCHIVE_RETRY) {
146                         /* Retryable error: try again */
147                         bsdtar_warnc(bsdtar, 0, "Retrying...");
148                         continue;
149                 }
150                 if (r == ARCHIVE_FATAL)
151                         break;
152
153                 /*
154                  * Exclude entries that are too old.
155                  */
156                 st = archive_entry_stat(entry);
157                 if (bsdtar->newer_ctime_sec > 0) {
158                         if (st->st_ctime < bsdtar->newer_ctime_sec)
159                                 continue; /* Too old, skip it. */
160                         if (st->st_ctime == bsdtar->newer_ctime_sec
161                             && ARCHIVE_STAT_CTIME_NANOS(st)
162                             <= bsdtar->newer_ctime_nsec)
163                                 continue; /* Too old, skip it. */
164                 }
165                 if (bsdtar->newer_mtime_sec > 0) {
166                         if (st->st_mtime < bsdtar->newer_mtime_sec)
167                                 continue; /* Too old, skip it. */
168                         if (st->st_mtime == bsdtar->newer_mtime_sec
169                             && ARCHIVE_STAT_MTIME_NANOS(st)
170                             <= bsdtar->newer_mtime_nsec)
171                                 continue; /* Too old, skip it. */
172                 }
173
174                 /*
175                  * Note that pattern exclusions are checked before
176                  * pathname rewrites are handled.  This gives more
177                  * control over exclusions, since rewrites always lose
178                  * information.  (For example, consider a rewrite
179                  * s/foo[0-9]/foo/.  If we check exclusions after the
180                  * rewrite, there would be no way to exclude foo1/bar
181                  * while allowing foo2/bar.)
182                  */
183                 if (excluded(bsdtar, archive_entry_pathname(entry)))
184                         continue; /* Excluded by a pattern test. */
185
186                 /*
187                  * Modify the pathname as requested by the user.  We
188                  * do this for -t as well to give users a way to
189                  * preview the effects of their rewrites.  We also do
190                  * this before extraction security checks (including
191                  * leading '/' removal).  Note that some rewrite
192                  * failures prevent extraction.
193                  */
194                 if (edit_pathname(bsdtar, entry))
195                         continue; /* Excluded by a rewrite failure. */
196
197                 if (mode == 't') {
198                         /* Perversely, gtar uses -O to mean "send to stderr"
199                          * when used with -t. */
200                         out = bsdtar->option_stdout ? stderr : stdout;
201
202                         if (bsdtar->verbose < 2)
203                                 safe_fprintf(out, "%s",
204                                     archive_entry_pathname(entry));
205                         else
206                                 list_item_verbose(bsdtar, out, entry);
207                         fflush(out);
208                         r = archive_read_data_skip(a);
209                         if (r == ARCHIVE_WARN) {
210                                 fprintf(out, "\n");
211                                 bsdtar_warnc(bsdtar, 0, "%s",
212                                     archive_error_string(a));
213                         }
214                         if (r == ARCHIVE_RETRY) {
215                                 fprintf(out, "\n");
216                                 bsdtar_warnc(bsdtar, 0, "%s",
217                                     archive_error_string(a));
218                         }
219                         if (r == ARCHIVE_FATAL) {
220                                 fprintf(out, "\n");
221                                 bsdtar_warnc(bsdtar, 0, "%s",
222                                     archive_error_string(a));
223                                 bsdtar->return_value = 1;
224                                 break;
225                         }
226                         fprintf(out, "\n");
227                 } else {
228                         if (bsdtar->option_interactive &&
229                             !yes("extract '%s'", archive_entry_pathname(entry)))
230                                 continue;
231
232                         /*
233                          * Format here is from SUSv2, including the
234                          * deferred '\n'.
235                          */
236                         if (bsdtar->verbose) {
237                                 safe_fprintf(stderr, "x %s",
238                                     archive_entry_pathname(entry));
239                                 fflush(stderr);
240                         }
241                         if (bsdtar->option_stdout)
242                                 r = archive_read_data_into_fd(a, 1);
243                         else
244                                 r = archive_read_extract(a, entry,
245                                     bsdtar->extract_flags);
246                         if (r != ARCHIVE_OK) {
247                                 if (!bsdtar->verbose)
248                                         safe_fprintf(stderr, "%s",
249                                             archive_entry_pathname(entry));
250                                 safe_fprintf(stderr, ": %s",
251                                     archive_error_string(a));
252                                 if (!bsdtar->verbose)
253                                         fprintf(stderr, "\n");
254                                 bsdtar->return_value = 1;
255                         }
256                         if (bsdtar->verbose)
257                                 fprintf(stderr, "\n");
258                         if (r == ARCHIVE_FATAL)
259                                 break;
260                 }
261         }
262
263         if (bsdtar->verbose > 2)
264                 fprintf(stdout, "Archive Format: %s,  Compression: %s\n",
265                     archive_format_name(a), archive_compression_name(a));
266
267         archive_read_finish(a);
268 }
269
270
271 /*
272  * Display information about the current file.
273  *
274  * The format here roughly duplicates the output of 'ls -l'.
275  * This is based on SUSv2, where 'tar tv' is documented as
276  * listing additional information in an "unspecified format,"
277  * and 'pax -l' is documented as using the same format as 'ls -l'.
278  */
279 static void
280 list_item_verbose(struct bsdtar *bsdtar, FILE *out, struct archive_entry *entry)
281 {
282         const struct stat       *st;
283         char                     tmp[100];
284         size_t                   w;
285         const char              *p;
286         const char              *fmt;
287         time_t                   tim;
288         static time_t            now;
289
290         st = archive_entry_stat(entry);
291
292         /*
293          * We avoid collecting the entire list in memory at once by
294          * listing things as we see them.  However, that also means we can't
295          * just pre-compute the field widths.  Instead, we start with guesses
296          * and just widen them as necessary.  These numbers are completely
297          * arbitrary.
298          */
299         if (!bsdtar->u_width) {
300                 bsdtar->u_width = 6;
301                 bsdtar->gs_width = 13;
302         }
303         if (!now)
304                 time(&now);
305         fprintf(out, "%s %d ",
306             archive_entry_strmode(entry),
307             (int)(st->st_nlink));
308
309         /* Use uname if it's present, else uid. */
310         p = archive_entry_uname(entry);
311         if ((p == NULL) || (*p == '\0')) {
312                 sprintf(tmp, "%lu ", (unsigned long)st->st_uid);
313                 p = tmp;
314         }
315         w = strlen(p);
316         if (w > bsdtar->u_width)
317                 bsdtar->u_width = w;
318         fprintf(out, "%-*s ", (int)bsdtar->u_width, p);
319
320         /* Use gname if it's present, else gid. */
321         p = archive_entry_gname(entry);
322         if (p != NULL && p[0] != '\0') {
323                 fprintf(out, "%s", p);
324                 w = strlen(p);
325         } else {
326                 sprintf(tmp, "%lu", (unsigned long)st->st_gid);
327                 w = strlen(tmp);
328                 fprintf(out, "%s", tmp);
329         }
330
331         /*
332          * Print device number or file size, right-aligned so as to make
333          * total width of group and devnum/filesize fields be gs_width.
334          * If gs_width is too small, grow it.
335          */
336         if (S_ISCHR(st->st_mode) || S_ISBLK(st->st_mode)) {
337                 sprintf(tmp, "%lu,%lu",
338                     (unsigned long)major(st->st_rdev),
339                     (unsigned long)minor(st->st_rdev)); /* ls(1) also casts here. */
340         } else {
341                 /*
342                  * Note the use of platform-dependent macros to format
343                  * the filesize here.  We need the format string and the
344                  * corresponding type for the cast.
345                  */
346                 sprintf(tmp, BSDTAR_FILESIZE_PRINTF,
347                     (BSDTAR_FILESIZE_TYPE)st->st_size);
348         }
349         if (w + strlen(tmp) >= bsdtar->gs_width)
350                 bsdtar->gs_width = w+strlen(tmp)+1;
351         fprintf(out, "%*s", (int)(bsdtar->gs_width - w), tmp);
352
353         /* Format the time using 'ls -l' conventions. */
354         tim = (time_t)st->st_mtime;
355         if (abs(tim - now) > (365/2)*86400)
356                 fmt = bsdtar->day_first ? "%e %b  %Y" : "%b %e  %Y";
357         else
358                 fmt = bsdtar->day_first ? "%e %b %R" : "%b %e %R";
359         strftime(tmp, sizeof(tmp), fmt, localtime(&tim));
360         fprintf(out, " %s ", tmp);
361         safe_fprintf(out, "%s", archive_entry_pathname(entry));
362
363         /* Extra information for links. */
364         if (archive_entry_hardlink(entry)) /* Hard link */
365                 safe_fprintf(out, " link to %s",
366                     archive_entry_hardlink(entry));
367         else if (S_ISLNK(st->st_mode)) /* Symbolic link */
368                 safe_fprintf(out, " -> %s", archive_entry_symlink(entry));
369 }