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