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