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