]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - contrib/libarchive/tar/read.c
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / contrib / libarchive / 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 HAVE_SYS_PARAM_H
33 #include <sys/param.h>
34 #endif
35 #ifdef HAVE_SYS_STAT_H
36 #include <sys/stat.h>
37 #endif
38
39 #ifdef HAVE_ERRNO_H
40 #include <errno.h>
41 #endif
42 #ifdef HAVE_GRP_H
43 #include <grp.h>
44 #endif
45 #ifdef HAVE_LIMITS_H
46 #include <limits.h>
47 #endif
48 #ifdef HAVE_PWD_H
49 #include <pwd.h>
50 #endif
51 #ifdef HAVE_STDINT_H
52 #include <stdint.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 #include "err.h"
70
71 struct progress_data {
72         struct bsdtar *bsdtar;
73         struct archive *archive;
74         struct archive_entry *entry;
75 };
76
77 static void     list_item_verbose(struct bsdtar *, FILE *,
78                     struct archive_entry *);
79 static void     read_archive(struct bsdtar *bsdtar, char mode);
80
81 void
82 tar_mode_t(struct bsdtar *bsdtar)
83 {
84         read_archive(bsdtar, 't');
85         if (lafe_unmatched_inclusions_warn(bsdtar->matching, "Not found in archive") != 0)
86                 bsdtar->return_value = 1;
87 }
88
89 void
90 tar_mode_x(struct bsdtar *bsdtar)
91 {
92         read_archive(bsdtar, 'x');
93
94         if (lafe_unmatched_inclusions_warn(bsdtar->matching, "Not found in archive") != 0)
95                 bsdtar->return_value = 1;
96 }
97
98 static void
99 progress_func(void *cookie)
100 {
101         struct progress_data *progress_data = cookie;
102         struct bsdtar *bsdtar = progress_data->bsdtar;
103         struct archive *a = progress_data->archive;
104         struct archive_entry *entry = progress_data->entry;
105         uint64_t comp, uncomp;
106         int compression;
107
108         if (!need_report())
109                 return;
110
111         if (bsdtar->verbose)
112                 fprintf(stderr, "\n");
113         if (a != NULL) {
114                 comp = archive_position_compressed(a);
115                 uncomp = archive_position_uncompressed(a);
116                 if (comp > uncomp)
117                         compression = 0;
118                 else
119                         compression = (int)((uncomp - comp) * 100 / uncomp);
120                 fprintf(stderr,
121                     "In: %s bytes, compression %d%%;",
122                     tar_i64toa(comp), compression);
123                 fprintf(stderr, "  Out: %d files, %s bytes\n",
124                     archive_file_count(a), tar_i64toa(uncomp));
125         }
126         if (entry != NULL) {
127                 safe_fprintf(stderr, "Current: %s",
128                     archive_entry_pathname(entry));
129                 fprintf(stderr, " (%s bytes)\n",
130                     tar_i64toa(archive_entry_size(entry)));
131         }
132 }
133
134 /*
135  * Handle 'x' and 't' modes.
136  */
137 static void
138 read_archive(struct bsdtar *bsdtar, char mode)
139 {
140         struct progress_data    progress_data;
141         FILE                     *out;
142         struct archive           *a;
143         struct archive_entry     *entry;
144         const struct stat        *st;
145         int                       r;
146
147         while (*bsdtar->argv) {
148                 lafe_include(&bsdtar->matching, *bsdtar->argv);
149                 bsdtar->argv++;
150         }
151
152         if (bsdtar->names_from_file != NULL)
153                 lafe_include_from_file(&bsdtar->matching,
154                     bsdtar->names_from_file, bsdtar->option_null);
155
156         a = archive_read_new();
157         if (bsdtar->compress_program != NULL)
158                 archive_read_support_compression_program(a, bsdtar->compress_program);
159         else
160                 archive_read_support_compression_all(a);
161         archive_read_support_format_all(a);
162         if (ARCHIVE_OK != archive_read_set_options(a, bsdtar->option_options))
163                 lafe_errc(1, 0, "%s", archive_error_string(a));
164         if (archive_read_open_file(a, bsdtar->filename,
165             bsdtar->bytes_per_block != 0 ? bsdtar->bytes_per_block :
166             DEFAULT_BYTES_PER_BLOCK))
167                 lafe_errc(1, 0, "Error opening archive: %s",
168                     archive_error_string(a));
169
170         do_chdir(bsdtar);
171
172         if (mode == 'x') {
173                 /* Set an extract callback so that we can handle SIGINFO. */
174                 progress_data.bsdtar = bsdtar;
175                 progress_data.archive = a;
176                 archive_read_extract_set_progress_callback(a, progress_func,
177                     &progress_data);
178         }
179
180         if (mode == 'x' && bsdtar->option_chroot) {
181 #if HAVE_CHROOT
182                 if (chroot(".") != 0)
183                         lafe_errc(1, errno, "Can't chroot to \".\"");
184 #else
185                 lafe_errc(1, 0,
186                     "chroot isn't supported on this platform");
187 #endif
188         }
189
190         for (;;) {
191                 /* Support --fast-read option */
192                 if (bsdtar->option_fast_read &&
193                     lafe_unmatched_inclusions(bsdtar->matching) == 0)
194                         break;
195
196                 r = archive_read_next_header(a, &entry);
197                 progress_data.entry = entry;
198                 if (r == ARCHIVE_EOF)
199                         break;
200                 if (r < ARCHIVE_OK)
201                         lafe_warnc(0, "%s", archive_error_string(a));
202                 if (r <= ARCHIVE_WARN)
203                         bsdtar->return_value = 1;
204                 if (r == ARCHIVE_RETRY) {
205                         /* Retryable error: try again */
206                         lafe_warnc(0, "Retrying...");
207                         continue;
208                 }
209                 if (r == ARCHIVE_FATAL)
210                         break;
211
212                 if (bsdtar->uid >= 0) {
213                         archive_entry_set_uid(entry, bsdtar->uid);
214                         archive_entry_set_uname(entry, NULL);
215                 }
216                 if (bsdtar->gid >= 0) {
217                         archive_entry_set_gid(entry, bsdtar->gid);
218                         archive_entry_set_gname(entry, NULL);
219                 }
220                 if (bsdtar->uname)
221                         archive_entry_set_uname(entry, bsdtar->uname);
222                 if (bsdtar->gname)
223                         archive_entry_set_gname(entry, bsdtar->gname);
224
225                 /*
226                  * Exclude entries that are too old.
227                  */
228                 st = archive_entry_stat(entry);
229                 if (bsdtar->newer_ctime_sec > 0) {
230                         if (st->st_ctime < bsdtar->newer_ctime_sec)
231                                 continue; /* Too old, skip it. */
232                         if (st->st_ctime == bsdtar->newer_ctime_sec
233                             && ARCHIVE_STAT_CTIME_NANOS(st)
234                             <= bsdtar->newer_ctime_nsec)
235                                 continue; /* Too old, skip it. */
236                 }
237                 if (bsdtar->newer_mtime_sec > 0) {
238                         if (st->st_mtime < bsdtar->newer_mtime_sec)
239                                 continue; /* Too old, skip it. */
240                         if (st->st_mtime == bsdtar->newer_mtime_sec
241                             && ARCHIVE_STAT_MTIME_NANOS(st)
242                             <= bsdtar->newer_mtime_nsec)
243                                 continue; /* Too old, skip it. */
244                 }
245
246                 /*
247                  * Note that pattern exclusions are checked before
248                  * pathname rewrites are handled.  This gives more
249                  * control over exclusions, since rewrites always lose
250                  * information.  (For example, consider a rewrite
251                  * s/foo[0-9]/foo/.  If we check exclusions after the
252                  * rewrite, there would be no way to exclude foo1/bar
253                  * while allowing foo2/bar.)
254                  */
255                 if (lafe_excluded(bsdtar->matching, archive_entry_pathname(entry)))
256                         continue; /* Excluded by a pattern test. */
257
258                 if (mode == 't') {
259                         /* Perversely, gtar uses -O to mean "send to stderr"
260                          * when used with -t. */
261                         out = bsdtar->option_stdout ? stderr : stdout;
262
263                         /*
264                          * TODO: Provide some reasonable way to
265                          * preview rewrites.  gtar always displays
266                          * the unedited path in -t output, which means
267                          * you cannot easily preview rewrites.
268                          */
269                         if (bsdtar->verbose < 2)
270                                 safe_fprintf(out, "%s",
271                                     archive_entry_pathname(entry));
272                         else
273                                 list_item_verbose(bsdtar, out, entry);
274                         fflush(out);
275                         r = archive_read_data_skip(a);
276                         if (r == ARCHIVE_WARN) {
277                                 fprintf(out, "\n");
278                                 lafe_warnc(0, "%s",
279                                     archive_error_string(a));
280                         }
281                         if (r == ARCHIVE_RETRY) {
282                                 fprintf(out, "\n");
283                                 lafe_warnc(0, "%s",
284                                     archive_error_string(a));
285                         }
286                         if (r == ARCHIVE_FATAL) {
287                                 fprintf(out, "\n");
288                                 lafe_warnc(0, "%s",
289                                     archive_error_string(a));
290                                 bsdtar->return_value = 1;
291                                 break;
292                         }
293                         fprintf(out, "\n");
294                 } else {
295                         /* Note: some rewrite failures prevent extraction. */
296                         if (edit_pathname(bsdtar, entry))
297                                 continue; /* Excluded by a rewrite failure. */
298
299                         if (bsdtar->option_interactive &&
300                             !yes("extract '%s'", archive_entry_pathname(entry)))
301                                 continue;
302
303                         /*
304                          * Format here is from SUSv2, including the
305                          * deferred '\n'.
306                          */
307                         if (bsdtar->verbose) {
308                                 safe_fprintf(stderr, "x %s",
309                                     archive_entry_pathname(entry));
310                                 fflush(stderr);
311                         }
312
313                         // TODO siginfo_printinfo(bsdtar, 0);
314
315                         if (bsdtar->option_stdout)
316                                 r = archive_read_data_into_fd(a, 1);
317                         else
318                                 r = archive_read_extract(a, entry,
319                                     bsdtar->extract_flags);
320                         if (r != ARCHIVE_OK) {
321                                 if (!bsdtar->verbose)
322                                         safe_fprintf(stderr, "%s",
323                                             archive_entry_pathname(entry));
324                                 safe_fprintf(stderr, ": %s",
325                                     archive_error_string(a));
326                                 if (!bsdtar->verbose)
327                                         fprintf(stderr, "\n");
328                                 bsdtar->return_value = 1;
329                         }
330                         if (bsdtar->verbose)
331                                 fprintf(stderr, "\n");
332                         if (r == ARCHIVE_FATAL)
333                                 break;
334                 }
335         }
336
337
338         r = archive_read_close(a);
339         if (r != ARCHIVE_OK)
340                 lafe_warnc(0, "%s", archive_error_string(a));
341         if (r <= ARCHIVE_WARN)
342                 bsdtar->return_value = 1;
343
344         if (bsdtar->verbose > 2)
345                 fprintf(stdout, "Archive Format: %s,  Compression: %s\n",
346                     archive_format_name(a), archive_compression_name(a));
347
348         archive_read_finish(a);
349 }
350
351
352 /*
353  * Display information about the current file.
354  *
355  * The format here roughly duplicates the output of 'ls -l'.
356  * This is based on SUSv2, where 'tar tv' is documented as
357  * listing additional information in an "unspecified format,"
358  * and 'pax -l' is documented as using the same format as 'ls -l'.
359  */
360 static void
361 list_item_verbose(struct bsdtar *bsdtar, FILE *out, struct archive_entry *entry)
362 {
363         char                     tmp[100];
364         size_t                   w;
365         const char              *p;
366         const char              *fmt;
367         time_t                   tim;
368         static time_t            now;
369
370         /*
371          * We avoid collecting the entire list in memory at once by
372          * listing things as we see them.  However, that also means we can't
373          * just pre-compute the field widths.  Instead, we start with guesses
374          * and just widen them as necessary.  These numbers are completely
375          * arbitrary.
376          */
377         if (!bsdtar->u_width) {
378                 bsdtar->u_width = 6;
379                 bsdtar->gs_width = 13;
380         }
381         if (!now)
382                 time(&now);
383         fprintf(out, "%s %d ",
384             archive_entry_strmode(entry),
385             archive_entry_nlink(entry));
386
387         /* Use uname if it's present, else uid. */
388         p = archive_entry_uname(entry);
389         if ((p == NULL) || (*p == '\0')) {
390                 sprintf(tmp, "%lu ",
391                     (unsigned long)archive_entry_uid(entry));
392                 p = tmp;
393         }
394         w = strlen(p);
395         if (w > bsdtar->u_width)
396                 bsdtar->u_width = w;
397         fprintf(out, "%-*s ", (int)bsdtar->u_width, p);
398
399         /* Use gname if it's present, else gid. */
400         p = archive_entry_gname(entry);
401         if (p != NULL && p[0] != '\0') {
402                 fprintf(out, "%s", p);
403                 w = strlen(p);
404         } else {
405                 sprintf(tmp, "%lu",
406                     (unsigned long)archive_entry_gid(entry));
407                 w = strlen(tmp);
408                 fprintf(out, "%s", tmp);
409         }
410
411         /*
412          * Print device number or file size, right-aligned so as to make
413          * total width of group and devnum/filesize fields be gs_width.
414          * If gs_width is too small, grow it.
415          */
416         if (archive_entry_filetype(entry) == AE_IFCHR
417             || archive_entry_filetype(entry) == AE_IFBLK) {
418                 sprintf(tmp, "%lu,%lu",
419                     (unsigned long)archive_entry_rdevmajor(entry),
420                     (unsigned long)archive_entry_rdevminor(entry));
421         } else {
422                 strcpy(tmp, tar_i64toa(archive_entry_size(entry)));
423         }
424         if (w + strlen(tmp) >= bsdtar->gs_width)
425                 bsdtar->gs_width = w+strlen(tmp)+1;
426         fprintf(out, "%*s", (int)(bsdtar->gs_width - w), tmp);
427
428         /* Format the time using 'ls -l' conventions. */
429         tim = archive_entry_mtime(entry);
430 #define HALF_YEAR (time_t)365 * 86400 / 2
431 #if defined(_WIN32) && !defined(__CYGWIN__)
432 #define DAY_FMT  "%d"  /* Windows' strftime function does not support %e format. */
433 #else
434 #define DAY_FMT  "%e"  /* Day number without leading zeros */
435 #endif
436         if (tim < now - HALF_YEAR || tim > now + HALF_YEAR)
437                 fmt = bsdtar->day_first ? DAY_FMT " %b  %Y" : "%b " DAY_FMT "  %Y";
438         else
439                 fmt = bsdtar->day_first ? DAY_FMT " %b %H:%M" : "%b " DAY_FMT " %H:%M";
440         strftime(tmp, sizeof(tmp), fmt, localtime(&tim));
441         fprintf(out, " %s ", tmp);
442         safe_fprintf(out, "%s", archive_entry_pathname(entry));
443
444         /* Extra information for links. */
445         if (archive_entry_hardlink(entry)) /* Hard link */
446                 safe_fprintf(out, " link to %s",
447                     archive_entry_hardlink(entry));
448         else if (archive_entry_symlink(entry)) /* Symbolic link */
449                 safe_fprintf(out, " -> %s", archive_entry_symlink(entry));
450 }