]> CyberLeo.Net >> Repos - FreeBSD/releng/9.0.git/blob - usr.bin/tar/read.c
Copy stable/9 to releng/9.0 as part of the FreeBSD 9.0-RELEASE release
[FreeBSD/releng/9.0.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 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->option_numeric_owner) {
213                         archive_entry_set_uname(entry, NULL);
214                         archive_entry_set_gname(entry, NULL);
215                 }
216
217                 /*
218                  * Exclude entries that are too old.
219                  */
220                 st = archive_entry_stat(entry);
221                 if (bsdtar->newer_ctime_sec > 0) {
222                         if (st->st_ctime < bsdtar->newer_ctime_sec)
223                                 continue; /* Too old, skip it. */
224                         if (st->st_ctime == bsdtar->newer_ctime_sec
225                             && ARCHIVE_STAT_CTIME_NANOS(st)
226                             <= bsdtar->newer_ctime_nsec)
227                                 continue; /* Too old, skip it. */
228                 }
229                 if (bsdtar->newer_mtime_sec > 0) {
230                         if (st->st_mtime < bsdtar->newer_mtime_sec)
231                                 continue; /* Too old, skip it. */
232                         if (st->st_mtime == bsdtar->newer_mtime_sec
233                             && ARCHIVE_STAT_MTIME_NANOS(st)
234                             <= bsdtar->newer_mtime_nsec)
235                                 continue; /* Too old, skip it. */
236                 }
237
238                 /*
239                  * Note that pattern exclusions are checked before
240                  * pathname rewrites are handled.  This gives more
241                  * control over exclusions, since rewrites always lose
242                  * information.  (For example, consider a rewrite
243                  * s/foo[0-9]/foo/.  If we check exclusions after the
244                  * rewrite, there would be no way to exclude foo1/bar
245                  * while allowing foo2/bar.)
246                  */
247                 if (lafe_excluded(bsdtar->matching, archive_entry_pathname(entry)))
248                         continue; /* Excluded by a pattern test. */
249
250                 if (mode == 't') {
251                         /* Perversely, gtar uses -O to mean "send to stderr"
252                          * when used with -t. */
253                         out = bsdtar->option_stdout ? stderr : stdout;
254
255                         /*
256                          * TODO: Provide some reasonable way to
257                          * preview rewrites.  gtar always displays
258                          * the unedited path in -t output, which means
259                          * you cannot easily preview rewrites.
260                          */
261                         if (bsdtar->verbose < 2)
262                                 safe_fprintf(out, "%s",
263                                     archive_entry_pathname(entry));
264                         else
265                                 list_item_verbose(bsdtar, out, entry);
266                         fflush(out);
267                         r = archive_read_data_skip(a);
268                         if (r == ARCHIVE_WARN) {
269                                 fprintf(out, "\n");
270                                 lafe_warnc(0, "%s",
271                                     archive_error_string(a));
272                         }
273                         if (r == ARCHIVE_RETRY) {
274                                 fprintf(out, "\n");
275                                 lafe_warnc(0, "%s",
276                                     archive_error_string(a));
277                         }
278                         if (r == ARCHIVE_FATAL) {
279                                 fprintf(out, "\n");
280                                 lafe_warnc(0, "%s",
281                                     archive_error_string(a));
282                                 bsdtar->return_value = 1;
283                                 break;
284                         }
285                         fprintf(out, "\n");
286                 } else {
287                         /* Note: some rewrite failures prevent extraction. */
288                         if (edit_pathname(bsdtar, entry))
289                                 continue; /* Excluded by a rewrite failure. */
290
291                         if (bsdtar->option_interactive &&
292                             !yes("extract '%s'", archive_entry_pathname(entry)))
293                                 continue;
294
295                         /*
296                          * Format here is from SUSv2, including the
297                          * deferred '\n'.
298                          */
299                         if (bsdtar->verbose) {
300                                 safe_fprintf(stderr, "x %s",
301                                     archive_entry_pathname(entry));
302                                 fflush(stderr);
303                         }
304
305                         // TODO siginfo_printinfo(bsdtar, 0);
306
307                         if (bsdtar->option_stdout)
308                                 r = archive_read_data_into_fd(a, 1);
309                         else
310                                 r = archive_read_extract(a, entry,
311                                     bsdtar->extract_flags);
312                         if (r != ARCHIVE_OK) {
313                                 if (!bsdtar->verbose)
314                                         safe_fprintf(stderr, "%s",
315                                             archive_entry_pathname(entry));
316                                 safe_fprintf(stderr, ": %s",
317                                     archive_error_string(a));
318                                 if (!bsdtar->verbose)
319                                         fprintf(stderr, "\n");
320                                 bsdtar->return_value = 1;
321                         }
322                         if (bsdtar->verbose)
323                                 fprintf(stderr, "\n");
324                         if (r == ARCHIVE_FATAL)
325                                 break;
326                 }
327         }
328
329
330         r = archive_read_close(a);
331         if (r != ARCHIVE_OK)
332                 lafe_warnc(0, "%s", archive_error_string(a));
333         if (r <= ARCHIVE_WARN)
334                 bsdtar->return_value = 1;
335
336         if (bsdtar->verbose > 2)
337                 fprintf(stdout, "Archive Format: %s,  Compression: %s\n",
338                     archive_format_name(a), archive_compression_name(a));
339
340         archive_read_finish(a);
341 }
342
343
344 /*
345  * Display information about the current file.
346  *
347  * The format here roughly duplicates the output of 'ls -l'.
348  * This is based on SUSv2, where 'tar tv' is documented as
349  * listing additional information in an "unspecified format,"
350  * and 'pax -l' is documented as using the same format as 'ls -l'.
351  */
352 static void
353 list_item_verbose(struct bsdtar *bsdtar, FILE *out, struct archive_entry *entry)
354 {
355         char                     tmp[100];
356         size_t                   w;
357         const char              *p;
358         const char              *fmt;
359         time_t                   tim;
360         static time_t            now;
361
362         /*
363          * We avoid collecting the entire list in memory at once by
364          * listing things as we see them.  However, that also means we can't
365          * just pre-compute the field widths.  Instead, we start with guesses
366          * and just widen them as necessary.  These numbers are completely
367          * arbitrary.
368          */
369         if (!bsdtar->u_width) {
370                 bsdtar->u_width = 6;
371                 bsdtar->gs_width = 13;
372         }
373         if (!now)
374                 time(&now);
375         fprintf(out, "%s %d ",
376             archive_entry_strmode(entry),
377             archive_entry_nlink(entry));
378
379         /* Use uname if it's present, else uid. */
380         p = archive_entry_uname(entry);
381         if ((p == NULL) || (*p == '\0')) {
382                 sprintf(tmp, "%lu ",
383                     (unsigned long)archive_entry_uid(entry));
384                 p = tmp;
385         }
386         w = strlen(p);
387         if (w > bsdtar->u_width)
388                 bsdtar->u_width = w;
389         fprintf(out, "%-*s ", (int)bsdtar->u_width, p);
390
391         /* Use gname if it's present, else gid. */
392         p = archive_entry_gname(entry);
393         if (p != NULL && p[0] != '\0') {
394                 fprintf(out, "%s", p);
395                 w = strlen(p);
396         } else {
397                 sprintf(tmp, "%lu",
398                     (unsigned long)archive_entry_gid(entry));
399                 w = strlen(tmp);
400                 fprintf(out, "%s", tmp);
401         }
402
403         /*
404          * Print device number or file size, right-aligned so as to make
405          * total width of group and devnum/filesize fields be gs_width.
406          * If gs_width is too small, grow it.
407          */
408         if (archive_entry_filetype(entry) == AE_IFCHR
409             || archive_entry_filetype(entry) == AE_IFBLK) {
410                 sprintf(tmp, "%lu,%lu",
411                     (unsigned long)archive_entry_rdevmajor(entry),
412                     (unsigned long)archive_entry_rdevminor(entry));
413         } else {
414                 strcpy(tmp, tar_i64toa(archive_entry_size(entry)));
415         }
416         if (w + strlen(tmp) >= bsdtar->gs_width)
417                 bsdtar->gs_width = w+strlen(tmp)+1;
418         fprintf(out, "%*s", (int)(bsdtar->gs_width - w), tmp);
419
420         /* Format the time using 'ls -l' conventions. */
421         tim = archive_entry_mtime(entry);
422 #define HALF_YEAR (time_t)365 * 86400 / 2
423 #if defined(_WIN32) && !defined(__CYGWIN__)
424 #define DAY_FMT  "%d"  /* Windows' strftime function does not support %e format. */
425 #else
426 #define DAY_FMT  "%e"  /* Day number without leading zeros */
427 #endif
428         if (tim < now - HALF_YEAR || tim > now + HALF_YEAR)
429                 fmt = bsdtar->day_first ? DAY_FMT " %b  %Y" : "%b " DAY_FMT "  %Y";
430         else
431                 fmt = bsdtar->day_first ? DAY_FMT " %b %H:%M" : "%b " DAY_FMT " %H:%M";
432         strftime(tmp, sizeof(tmp), fmt, localtime(&tim));
433         fprintf(out, " %s ", tmp);
434         safe_fprintf(out, "%s", archive_entry_pathname(entry));
435
436         /* Extra information for links. */
437         if (archive_entry_hardlink(entry)) /* Hard link */
438                 safe_fprintf(out, " link to %s",
439                     archive_entry_hardlink(entry));
440         else if (archive_entry_symlink(entry)) /* Symbolic link */
441                 safe_fprintf(out, " -> %s", archive_entry_symlink(entry));
442 }