Upgrade to libarchive 2.1.9. Including bug fixes, this improves ar support
[dragonfly.git] / contrib / libarchive-2.0 / 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: src/usr.bin/tar/read.c,v 1.29 2007/04/07 05:56:40 kientzle Exp $");
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 #endif
35 #ifdef HAVE_SYS_PARAM_H
36 #include <sys/param.h>
37 #endif
38 #ifdef HAVE_SYS_STAT_H
39 #include <sys/stat.h>
40 #endif
41
42 #ifdef HAVE_ERRNO_H
43 #include <errno.h>
44 #endif
45 #ifdef HAVE_GRP_H
46 #include <grp.h>
47 #endif
48 #ifdef HAVE_LIMITS_H
49 #include <limits.h>
50 #endif
51 #ifdef HAVE_PWD_H
52 #include <pwd.h>
53 #endif
54 #include <stdio.h>
55 #ifdef HAVE_STDLIB_H
56 #include <stdlib.h>
57 #endif
58 #ifdef HAVE_STRING_H
59 #include <string.h>
60 #endif
61 #ifdef HAVE_TIME_H
62 #include <time.h>
63 #endif
64 #ifdef HAVE_UNISTD_H
65 #include <unistd.h>
66 #endif
67
68 #include "bsdtar.h"
69
70 static void     list_item_verbose(struct bsdtar *, FILE *,
71                     struct archive_entry *);
72 static void     read_archive(struct bsdtar *bsdtar, char mode);
73
74 void
75 tar_mode_t(struct bsdtar *bsdtar)
76 {
77         read_archive(bsdtar, 't');
78 }
79
80 void
81 tar_mode_x(struct bsdtar *bsdtar)
82 {
83         read_archive(bsdtar, 'x');
84 }
85
86 /*
87  * Handle 'x' and 't' modes.
88  */
89 static void
90 read_archive(struct bsdtar *bsdtar, char mode)
91 {
92         FILE                     *out;
93         struct archive           *a;
94         struct archive_entry     *entry;
95         const struct stat        *st;
96         int                       r;
97
98         while (*bsdtar->argv) {
99                 include(bsdtar, *bsdtar->argv);
100                 bsdtar->argv++;
101         }
102
103         if (bsdtar->names_from_file != NULL)
104                 include_from_file(bsdtar, bsdtar->names_from_file);
105
106         a = archive_read_new();
107         archive_read_support_compression_all(a);
108         archive_read_support_format_all(a);
109         if (archive_read_open_file(a, bsdtar->filename,
110             bsdtar->bytes_per_block != 0 ? bsdtar->bytes_per_block :
111             DEFAULT_BYTES_PER_BLOCK))
112                 bsdtar_errc(bsdtar, 1, 0, "Error opening archive: %s",
113                     archive_error_string(a));
114
115         do_chdir(bsdtar);
116         for (;;) {
117                 /* Support --fast-read option */
118                 if (bsdtar->option_fast_read &&
119                     unmatched_inclusions(bsdtar) == 0)
120                         break;
121
122                 r = archive_read_next_header(a, &entry);
123                 if (r == ARCHIVE_EOF)
124                         break;
125                 if (r < ARCHIVE_OK)
126                         bsdtar_warnc(bsdtar, 0, "%s", archive_error_string(a));
127                 if (r == ARCHIVE_RETRY) {
128                         /* Retryable error: try again */
129                         bsdtar_warnc(bsdtar, 0, "Retrying...");
130                         continue;
131                 }
132                 if (r != ARCHIVE_OK) {
133                         bsdtar->return_value = 1;
134                         break;
135                 }
136
137                 /*
138                  * Exclude entries that are too old.
139                  */
140                 st = archive_entry_stat(entry);
141                 if (bsdtar->newer_ctime_sec > 0) {
142                         if (st->st_ctime < bsdtar->newer_ctime_sec)
143                                 continue; /* Too old, skip it. */
144                         if (st->st_ctime == bsdtar->newer_ctime_sec
145                             && ARCHIVE_STAT_CTIME_NANOS(st)
146                             <= bsdtar->newer_ctime_nsec)
147                                 continue; /* Too old, skip it. */
148                 }
149                 if (bsdtar->newer_mtime_sec > 0) {
150                         if (st->st_mtime < bsdtar->newer_mtime_sec)
151                                 continue; /* Too old, skip it. */
152                         if (st->st_mtime == bsdtar->newer_mtime_sec
153                             && ARCHIVE_STAT_MTIME_NANOS(st)
154                             <= bsdtar->newer_mtime_nsec)
155                                 continue; /* Too old, skip it. */
156                 }
157
158                 /*
159                  * Note that pattern exclusions are checked before
160                  * pathname rewrites are handled.  This gives more
161                  * control over exclusions, since rewrites always lose
162                  * information.  (For example, consider a rewrite
163                  * s/foo[0-9]/foo/.  If we check exclusions after the
164                  * rewrite, there would be no way to exclude foo1/bar
165                  * while allowing foo2/bar.)
166                  */
167                 if (excluded(bsdtar, archive_entry_pathname(entry)))
168                         continue; /* Excluded by a pattern test. */
169
170                 /*
171                  * Modify the pathname as requested by the user.  We
172                  * do this for -t as well to give users a way to
173                  * preview the effects of their rewrites.  We also do
174                  * this before extraction security checks (including
175                  * leading '/' removal).  Note that some rewrite
176                  * failures prevent extraction.
177                  */
178                 if (edit_pathname(bsdtar, entry))
179                         continue; /* Excluded by a rewrite failure. */
180
181                 if (mode == 't') {
182                         /* Perversely, gtar uses -O to mean "send to stderr"
183                          * when used with -t. */
184                         out = bsdtar->option_stdout ? stderr : stdout;
185
186                         if (bsdtar->verbose < 2)
187                                 safe_fprintf(out, "%s",
188                                     archive_entry_pathname(entry));
189                         else
190                                 list_item_verbose(bsdtar, out, entry);
191                         fflush(out);
192                         r = archive_read_data_skip(a);
193                         if (r == ARCHIVE_WARN) {
194                                 fprintf(out, "\n");
195                                 bsdtar_warnc(bsdtar, 0, "%s",
196                                     archive_error_string(a));
197                         }
198                         if (r == ARCHIVE_RETRY) {
199                                 fprintf(out, "\n");
200                                 bsdtar_warnc(bsdtar, 0, "%s",
201                                     archive_error_string(a));
202                         }
203                         if (r == ARCHIVE_FATAL) {
204                                 fprintf(out, "\n");
205                                 bsdtar_warnc(bsdtar, 0, "%s",
206                                     archive_error_string(a));
207                                 break;
208                         }
209                         fprintf(out, "\n");
210                 } else {
211                         if (bsdtar->option_interactive &&
212                             !yes("extract '%s'", archive_entry_pathname(entry)))
213                                 continue;
214
215                         /*
216                          * Format here is from SUSv2, including the
217                          * deferred '\n'.
218                          */
219                         if (bsdtar->verbose) {
220                                 safe_fprintf(stderr, "x %s",
221                                     archive_entry_pathname(entry));
222                                 fflush(stderr);
223                         }
224                         if (bsdtar->option_stdout) {
225                                 /* TODO: Catch/recover any errors here. */
226                                 archive_read_data_into_fd(a, 1);
227                         } else if (archive_read_extract(a, entry,
228                                        bsdtar->extract_flags)) {
229                                 if (!bsdtar->verbose)
230                                         safe_fprintf(stderr, "%s",
231                                             archive_entry_pathname(entry));
232                                 safe_fprintf(stderr, ": %s",
233                                     archive_error_string(a));
234                                 if (!bsdtar->verbose)
235                                         fprintf(stderr, "\n");
236                                 /*
237                                  * TODO: Decide how to handle
238                                  * extraction error... <sigh>
239                                  */
240                                 bsdtar->return_value = 1;
241                         }
242                         if (bsdtar->verbose)
243                                 fprintf(stderr, "\n");
244                 }
245         }
246
247         if (bsdtar->verbose > 2)
248                 fprintf(stdout, "Archive Format: %s,  Compression: %s\n",
249                     archive_format_name(a), archive_compression_name(a));
250
251         archive_read_finish(a);
252 }
253
254
255 /*
256  * Display information about the current file.
257  *
258  * The format here roughly duplicates the output of 'ls -l'.
259  * This is based on SUSv2, where 'tar tv' is documented as
260  * listing additional information in an "unspecified format,"
261  * and 'pax -l' is documented as using the same format as 'ls -l'.
262  */
263 static void
264 list_item_verbose(struct bsdtar *bsdtar, FILE *out, struct archive_entry *entry)
265 {
266         const struct stat       *st;
267         char                     tmp[100];
268         size_t                   w;
269         const char              *p;
270         const char              *fmt;
271         time_t                   tim;
272         static time_t            now;
273
274         st = archive_entry_stat(entry);
275
276         /*
277          * We avoid collecting the entire list in memory at once by
278          * listing things as we see them.  However, that also means we can't
279          * just pre-compute the field widths.  Instead, we start with guesses
280          * and just widen them as necessary.  These numbers are completely
281          * arbitrary.
282          */
283         if (!bsdtar->u_width) {
284                 bsdtar->u_width = 6;
285                 bsdtar->gs_width = 13;
286         }
287         if (!now)
288                 time(&now);
289         bsdtar_strmode(entry, tmp);
290         fprintf(out, "%s %d ", tmp, (int)(st->st_nlink));
291
292         /* Use uname if it's present, else uid. */
293         p = archive_entry_uname(entry);
294         if ((p == NULL) || (*p == '\0')) {
295                 sprintf(tmp, "%d ", st->st_uid);
296                 p = tmp;
297         }
298         w = strlen(p);
299         if (w > bsdtar->u_width)
300                 bsdtar->u_width = w;
301         fprintf(out, "%-*s ", (int)bsdtar->u_width, p);
302
303         /* Use gname if it's present, else gid. */
304         p = archive_entry_gname(entry);
305         if (p != NULL && p[0] != '\0') {
306                 fprintf(out, "%s", p);
307                 w = strlen(p);
308         } else {
309                 sprintf(tmp, "%d", st->st_gid);
310                 w = strlen(tmp);
311                 fprintf(out, "%s", tmp);
312         }
313
314         /*
315          * Print device number or file size, right-aligned so as to make
316          * total width of group and devnum/filesize fields be gs_width.
317          * If gs_width is too small, grow it.
318          */
319         if (S_ISCHR(st->st_mode) || S_ISBLK(st->st_mode)) {
320                 sprintf(tmp, "%d,%u",
321                     major(st->st_rdev),
322                     (unsigned)minor(st->st_rdev)); /* ls(1) also casts here. */
323         } else {
324                 /*
325                  * Note the use of platform-dependent macros to format
326                  * the filesize here.  We need the format string and the
327                  * corresponding type for the cast.
328                  */
329                 sprintf(tmp, BSDTAR_FILESIZE_PRINTF,
330                     (BSDTAR_FILESIZE_TYPE)st->st_size);
331         }
332         if (w + strlen(tmp) >= bsdtar->gs_width)
333                 bsdtar->gs_width = w+strlen(tmp)+1;
334         fprintf(out, "%*s", (int)(bsdtar->gs_width - w), tmp);
335
336         /* Format the time using 'ls -l' conventions. */
337         tim = (time_t)st->st_mtime;
338         if (abs(tim - now) > (365/2)*86400)
339                 fmt = bsdtar->day_first ? "%e %b  %Y" : "%b %e  %Y";
340         else
341                 fmt = bsdtar->day_first ? "%e %b %R" : "%b %e %R";
342         strftime(tmp, sizeof(tmp), fmt, localtime(&tim));
343         fprintf(out, " %s ", tmp);
344         safe_fprintf(out, "%s", archive_entry_pathname(entry));
345
346         /* Extra information for links. */
347         if (archive_entry_hardlink(entry)) /* Hard link */
348                 safe_fprintf(out, " link to %s",
349                     archive_entry_hardlink(entry));
350         else if (S_ISLNK(st->st_mode)) /* Symbolic link */
351                 safe_fprintf(out, " -> %s", archive_entry_symlink(entry));
352 }