]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.bin/tar/read.c
This commit was generated by cvs2svn to compensate for changes in r169765,
[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         archive_read_support_compression_all(a);
110         archive_read_support_format_all(a);
111         if (archive_read_open_file(a, bsdtar->filename,
112             bsdtar->bytes_per_block != 0 ? bsdtar->bytes_per_block :
113             DEFAULT_BYTES_PER_BLOCK))
114                 bsdtar_errc(bsdtar, 1, 0, "Error opening archive: %s",
115                     archive_error_string(a));
116
117         do_chdir(bsdtar);
118         for (;;) {
119                 /* Support --fast-read option */
120                 if (bsdtar->option_fast_read &&
121                     unmatched_inclusions(bsdtar) == 0)
122                         break;
123
124                 r = archive_read_next_header(a, &entry);
125                 if (r == ARCHIVE_EOF)
126                         break;
127                 if (r < ARCHIVE_OK)
128                         bsdtar_warnc(bsdtar, 0, "%s", archive_error_string(a));
129                 if (r == ARCHIVE_RETRY) {
130                         /* Retryable error: try again */
131                         bsdtar_warnc(bsdtar, 0, "Retrying...");
132                         continue;
133                 }
134                 if (r != ARCHIVE_OK) {
135                         bsdtar->return_value = 1;
136                         break;
137                 }
138
139                 /*
140                  * Exclude entries that are too old.
141                  */
142                 st = archive_entry_stat(entry);
143                 if (bsdtar->newer_ctime_sec > 0) {
144                         if (st->st_ctime < bsdtar->newer_ctime_sec)
145                                 continue; /* Too old, skip it. */
146                         if (st->st_ctime == bsdtar->newer_ctime_sec
147                             && ARCHIVE_STAT_CTIME_NANOS(st)
148                             <= bsdtar->newer_ctime_nsec)
149                                 continue; /* Too old, skip it. */
150                 }
151                 if (bsdtar->newer_mtime_sec > 0) {
152                         if (st->st_mtime < bsdtar->newer_mtime_sec)
153                                 continue; /* Too old, skip it. */
154                         if (st->st_mtime == bsdtar->newer_mtime_sec
155                             && ARCHIVE_STAT_MTIME_NANOS(st)
156                             <= bsdtar->newer_mtime_nsec)
157                                 continue; /* Too old, skip it. */
158                 }
159
160                 /*
161                  * Note that pattern exclusions are checked before
162                  * pathname rewrites are handled.  This gives more
163                  * control over exclusions, since rewrites always lose
164                  * information.  (For example, consider a rewrite
165                  * s/foo[0-9]/foo/.  If we check exclusions after the
166                  * rewrite, there would be no way to exclude foo1/bar
167                  * while allowing foo2/bar.)
168                  */
169                 if (excluded(bsdtar, archive_entry_pathname(entry)))
170                         continue; /* Excluded by a pattern test. */
171
172                 /*
173                  * Modify the pathname as requested by the user.  We
174                  * do this for -t as well to give users a way to
175                  * preview the effects of their rewrites.  We also do
176                  * this before extraction security checks (including
177                  * leading '/' removal).  Note that some rewrite
178                  * failures prevent extraction.
179                  */
180                 if (edit_pathname(bsdtar, entry))
181                         continue; /* Excluded by a rewrite failure. */
182
183                 if (mode == 't') {
184                         /* Perversely, gtar uses -O to mean "send to stderr"
185                          * when used with -t. */
186                         out = bsdtar->option_stdout ? stderr : stdout;
187
188                         if (bsdtar->verbose < 2)
189                                 safe_fprintf(out, "%s",
190                                     archive_entry_pathname(entry));
191                         else
192                                 list_item_verbose(bsdtar, out, entry);
193                         fflush(out);
194                         r = archive_read_data_skip(a);
195                         if (r == ARCHIVE_WARN) {
196                                 fprintf(out, "\n");
197                                 bsdtar_warnc(bsdtar, 0, "%s",
198                                     archive_error_string(a));
199                         }
200                         if (r == ARCHIVE_RETRY) {
201                                 fprintf(out, "\n");
202                                 bsdtar_warnc(bsdtar, 0, "%s",
203                                     archive_error_string(a));
204                         }
205                         if (r == ARCHIVE_FATAL) {
206                                 fprintf(out, "\n");
207                                 bsdtar_warnc(bsdtar, 0, "%s",
208                                     archive_error_string(a));
209                                 bsdtar->return_value = 1;
210                                 break;
211                         }
212                         fprintf(out, "\n");
213                 } else {
214                         if (bsdtar->option_interactive &&
215                             !yes("extract '%s'", archive_entry_pathname(entry)))
216                                 continue;
217
218                         /*
219                          * Format here is from SUSv2, including the
220                          * deferred '\n'.
221                          */
222                         if (bsdtar->verbose) {
223                                 safe_fprintf(stderr, "x %s",
224                                     archive_entry_pathname(entry));
225                                 fflush(stderr);
226                         }
227                         if (bsdtar->option_stdout)
228                                 r = archive_read_data_into_fd(a, 1);
229                         else
230                                 r = archive_read_extract(a, entry,
231                                     bsdtar->extract_flags);
232                         if (r != ARCHIVE_OK) {
233                                 if (!bsdtar->verbose)
234                                         safe_fprintf(stderr, "%s",
235                                             archive_entry_pathname(entry));
236                                 safe_fprintf(stderr, ": %s",
237                                     archive_error_string(a));
238                                 if (!bsdtar->verbose)
239                                         fprintf(stderr, "\n");
240                                 bsdtar->return_value = 1;
241                         }
242                         if (bsdtar->verbose)
243                                 fprintf(stderr, "\n");
244                         if (r == ARCHIVE_FATAL)
245                                 break;
246                 }
247         }
248
249         if (bsdtar->verbose > 2)
250                 fprintf(stdout, "Archive Format: %s,  Compression: %s\n",
251                     archive_format_name(a), archive_compression_name(a));
252
253         archive_read_finish(a);
254 }
255
256
257 /*
258  * Display information about the current file.
259  *
260  * The format here roughly duplicates the output of 'ls -l'.
261  * This is based on SUSv2, where 'tar tv' is documented as
262  * listing additional information in an "unspecified format,"
263  * and 'pax -l' is documented as using the same format as 'ls -l'.
264  */
265 static void
266 list_item_verbose(struct bsdtar *bsdtar, FILE *out, struct archive_entry *entry)
267 {
268         const struct stat       *st;
269         char                     tmp[100];
270         size_t                   w;
271         const char              *p;
272         const char              *fmt;
273         time_t                   tim;
274         static time_t            now;
275
276         st = archive_entry_stat(entry);
277
278         /*
279          * We avoid collecting the entire list in memory at once by
280          * listing things as we see them.  However, that also means we can't
281          * just pre-compute the field widths.  Instead, we start with guesses
282          * and just widen them as necessary.  These numbers are completely
283          * arbitrary.
284          */
285         if (!bsdtar->u_width) {
286                 bsdtar->u_width = 6;
287                 bsdtar->gs_width = 13;
288         }
289         if (!now)
290                 time(&now);
291         bsdtar_strmode(entry, tmp);
292         fprintf(out, "%s %d ", tmp, (int)(st->st_nlink));
293
294         /* Use uname if it's present, else uid. */
295         p = archive_entry_uname(entry);
296         if ((p == NULL) || (*p == '\0')) {
297                 sprintf(tmp, "%d ", st->st_uid);
298                 p = tmp;
299         }
300         w = strlen(p);
301         if (w > bsdtar->u_width)
302                 bsdtar->u_width = w;
303         fprintf(out, "%-*s ", (int)bsdtar->u_width, p);
304
305         /* Use gname if it's present, else gid. */
306         p = archive_entry_gname(entry);
307         if (p != NULL && p[0] != '\0') {
308                 fprintf(out, "%s", p);
309                 w = strlen(p);
310         } else {
311                 sprintf(tmp, "%d", st->st_gid);
312                 w = strlen(tmp);
313                 fprintf(out, "%s", tmp);
314         }
315
316         /*
317          * Print device number or file size, right-aligned so as to make
318          * total width of group and devnum/filesize fields be gs_width.
319          * If gs_width is too small, grow it.
320          */
321         if (S_ISCHR(st->st_mode) || S_ISBLK(st->st_mode)) {
322                 sprintf(tmp, "%d,%u",
323                     major(st->st_rdev),
324                     (unsigned)minor(st->st_rdev)); /* ls(1) also casts here. */
325         } else {
326                 /*
327                  * Note the use of platform-dependent macros to format
328                  * the filesize here.  We need the format string and the
329                  * corresponding type for the cast.
330                  */
331                 sprintf(tmp, BSDTAR_FILESIZE_PRINTF,
332                     (BSDTAR_FILESIZE_TYPE)st->st_size);
333         }
334         if (w + strlen(tmp) >= bsdtar->gs_width)
335                 bsdtar->gs_width = w+strlen(tmp)+1;
336         fprintf(out, "%*s", (int)(bsdtar->gs_width - w), tmp);
337
338         /* Format the time using 'ls -l' conventions. */
339         tim = (time_t)st->st_mtime;
340         if (abs(tim - now) > (365/2)*86400)
341                 fmt = bsdtar->day_first ? "%e %b  %Y" : "%b %e  %Y";
342         else
343                 fmt = bsdtar->day_first ? "%e %b %R" : "%b %e %R";
344         strftime(tmp, sizeof(tmp), fmt, localtime(&tim));
345         fprintf(out, " %s ", tmp);
346         safe_fprintf(out, "%s", archive_entry_pathname(entry));
347
348         /* Extra information for links. */
349         if (archive_entry_hardlink(entry)) /* Hard link */
350                 safe_fprintf(out, " link to %s",
351                     archive_entry_hardlink(entry));
352         else if (S_ISLNK(st->st_mode)) /* Symbolic link */
353                 safe_fprintf(out, " -> %s", archive_entry_symlink(entry));
354 }