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