"Robert P. J. Day" | 63fc1a9 | 2006-07-02 19:47:05 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Denys Vlasenko | 2ab9403 | 2017-10-05 15:33:28 +0200 | [diff] [blame] | 2 | /* |
| 3 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Glenn L McGrath | 6aa5223 | 2004-02-17 11:55:06 +0000 | [diff] [blame] | 4 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 5 | * FIXME: |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 6 | * In privileged mode if uname and gname map to a uid and gid then use the |
Glenn L McGrath | c9f1fce | 2004-02-20 02:25:18 +0000 | [diff] [blame] | 7 | * mapped value instead of the uid/gid values in tar header |
Glenn L McGrath | b0e163a | 2004-02-19 08:48:30 +0000 | [diff] [blame] | 8 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 9 | * References: |
Glenn L McGrath | b0e163a | 2004-02-19 08:48:30 +0000 | [diff] [blame] | 10 | * GNU tar and star man pages, |
| 11 | * Opengroup's ustar interchange format, |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 12 | * http://www.opengroup.org/onlinepubs/007904975/utilities/pax.html |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 13 | */ |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 14 | #include "libbb.h" |
Denys Vlasenko | d184a72 | 2011-09-22 12:45:14 +0200 | [diff] [blame] | 15 | #include "bb_archive.h" |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 16 | |
Denys Vlasenko | 98a4c7c | 2010-02-04 15:00:15 +0100 | [diff] [blame] | 17 | typedef uint32_t aliased_uint32_t FIX_ALIASING; |
| 18 | typedef off_t aliased_off_t FIX_ALIASING; |
| 19 | |
Denis Vlasenko | 376ce1e | 2006-11-24 14:51:01 +0000 | [diff] [blame] | 20 | /* NB: _DESTROYS_ str[len] character! */ |
| 21 | static unsigned long long getOctal(char *str, int len) |
| 22 | { |
| 23 | unsigned long long v; |
Denys Vlasenko | 7b48eb4 | 2010-05-06 20:08:14 +0200 | [diff] [blame] | 24 | char *end; |
Denis Vlasenko | a60936d | 2008-06-28 05:04:09 +0000 | [diff] [blame] | 25 | /* NB: leading spaces are allowed. Using strtoull to handle that. |
Denys Vlasenko | 8d33817 | 2009-09-23 17:16:37 +0200 | [diff] [blame] | 26 | * The downside is that we accept e.g. "-123" too :( |
Denis Vlasenko | a60936d | 2008-06-28 05:04:09 +0000 | [diff] [blame] | 27 | */ |
Denis Vlasenko | 376ce1e | 2006-11-24 14:51:01 +0000 | [diff] [blame] | 28 | str[len] = '\0'; |
Denys Vlasenko | 7b48eb4 | 2010-05-06 20:08:14 +0200 | [diff] [blame] | 29 | v = strtoull(str, &end, 8); |
Denys Vlasenko | 8d33817 | 2009-09-23 17:16:37 +0200 | [diff] [blame] | 30 | /* std: "Each numeric field is terminated by one or more |
| 31 | * <space> or NUL characters". We must support ' '! */ |
Denys Vlasenko | 7b48eb4 | 2010-05-06 20:08:14 +0200 | [diff] [blame] | 32 | if (*end != '\0' && *end != ' ') { |
| 33 | int8_t first = str[0]; |
| 34 | if (!(first & 0x80)) |
| 35 | bb_error_msg_and_die("corrupted octal value in tar header"); |
| 36 | /* |
| 37 | * GNU tar uses "base-256 encoding" for very large numbers. |
| 38 | * Encoding is binary, with highest bit always set as a marker |
| 39 | * and sign in next-highest bit: |
| 40 | * 80 00 .. 00 - zero |
| 41 | * bf ff .. ff - largest positive number |
| 42 | * ff ff .. ff - minus 1 |
| 43 | * c0 00 .. 00 - smallest negative number |
| 44 | * |
| 45 | * Example of tar file with 8914993153 (0x213600001) byte file. |
| 46 | * Field starts at offset 7c: |
| 47 | * 00070 30 30 30 00 30 30 30 30 30 30 30 00 80 00 00 00 |000.0000000.....| |
| 48 | * 00080 00 00 00 02 13 60 00 01 31 31 31 32 30 33 33 36 |.....`..11120336| |
| 49 | * |
| 50 | * NB: tarballs with NEGATIVE unix times encoded that way were seen! |
| 51 | */ |
Denys Vlasenko | f74f280 | 2011-10-19 14:51:12 +0200 | [diff] [blame] | 52 | /* Sign-extend 7bit 'first' to 64bit 'v' (that is, using 6th bit as sign): */ |
| 53 | first <<= 1; |
| 54 | first >>= 1; /* now 7th bit = 6th bit */ |
| 55 | v = first; /* sign-extend 8 bits to 64 */ |
Denys Vlasenko | 7b48eb4 | 2010-05-06 20:08:14 +0200 | [diff] [blame] | 56 | while (--len != 0) |
Etienne Le Sueur | cfc212c | 2012-06-09 08:37:05 +0200 | [diff] [blame] | 57 | v = (v << 8) + (uint8_t) *++str; |
Denys Vlasenko | 7b48eb4 | 2010-05-06 20:08:14 +0200 | [diff] [blame] | 58 | } |
Denis Vlasenko | 376ce1e | 2006-11-24 14:51:01 +0000 | [diff] [blame] | 59 | return v; |
| 60 | } |
Denis Vlasenko | b596335 | 2006-11-26 01:46:59 +0000 | [diff] [blame] | 61 | #define GET_OCTAL(a) getOctal((a), sizeof(a)) |
Denis Vlasenko | 376ce1e | 2006-11-24 14:51:01 +0000 | [diff] [blame] | 62 | |
Denys Vlasenko | 015db58 | 2016-06-19 18:15:33 +0200 | [diff] [blame] | 63 | #define TAR_EXTD (ENABLE_FEATURE_TAR_GNU_EXTENSIONS || ENABLE_FEATURE_TAR_SELINUX) |
| 64 | #if !TAR_EXTD |
| 65 | #define process_pax_hdr(archive_handle, sz, global) \ |
| 66 | process_pax_hdr(archive_handle, sz) |
| 67 | #endif |
Denys Vlasenko | 6111f96 | 2012-02-23 13:45:18 +0100 | [diff] [blame] | 68 | /* "global" is 0 or 1 */ |
| 69 | static void process_pax_hdr(archive_handle_t *archive_handle, unsigned sz, int global) |
J. Tang | 77a2c51 | 2010-03-19 14:48:51 +0100 | [diff] [blame] | 70 | { |
Denys Vlasenko | 015db58 | 2016-06-19 18:15:33 +0200 | [diff] [blame] | 71 | #if !TAR_EXTD |
| 72 | unsigned blk_sz = (sz + 511) & (~511); |
| 73 | seek_by_read(archive_handle->src_fd, blk_sz); |
| 74 | #else |
| 75 | unsigned blk_sz = (sz + 511) & (~511); |
J. Tang | 77a2c51 | 2010-03-19 14:48:51 +0100 | [diff] [blame] | 76 | char *buf, *p; |
J. Tang | 77a2c51 | 2010-03-19 14:48:51 +0100 | [diff] [blame] | 77 | |
Denys Vlasenko | 6111f96 | 2012-02-23 13:45:18 +0100 | [diff] [blame] | 78 | p = buf = xmalloc(blk_sz + 1); |
| 79 | xread(archive_handle->src_fd, buf, blk_sz); |
| 80 | archive_handle->offset += blk_sz; |
| 81 | |
J. Tang | 77a2c51 | 2010-03-19 14:48:51 +0100 | [diff] [blame] | 82 | /* prevent bb_strtou from running off the buffer */ |
| 83 | buf[sz] = '\0'; |
J. Tang | 77a2c51 | 2010-03-19 14:48:51 +0100 | [diff] [blame] | 84 | |
J. Tang | 77a2c51 | 2010-03-19 14:48:51 +0100 | [diff] [blame] | 85 | while (sz != 0) { |
| 86 | char *end, *value; |
| 87 | unsigned len; |
| 88 | |
| 89 | /* Every record has this format: "LEN NAME=VALUE\n" */ |
| 90 | len = bb_strtou(p, &end, 10); |
| 91 | /* expect errno to be EINVAL, because the character |
| 92 | * following the digits should be a space |
| 93 | */ |
| 94 | p += len; |
| 95 | sz -= len; |
Denys Vlasenko | 0f592d7 | 2014-01-10 18:02:38 +0100 | [diff] [blame] | 96 | if ( |
| 97 | /** (int)sz < 0 - not good enough for huge malicious VALUE of 2^32-1 */ |
| 98 | (int)(sz|len) < 0 /* this works */ |
J. Tang | 77a2c51 | 2010-03-19 14:48:51 +0100 | [diff] [blame] | 99 | || len == 0 |
| 100 | || errno != EINVAL |
| 101 | || *end != ' ' |
| 102 | ) { |
| 103 | bb_error_msg("malformed extended header, skipped"); |
| 104 | // More verbose version: |
| 105 | //bb_error_msg("malformed extended header at %"OFF_FMT"d, skipped", |
| 106 | // archive_handle->offset - (sz + len)); |
| 107 | break; |
| 108 | } |
| 109 | /* overwrite the terminating newline with NUL |
| 110 | * (we do not bother to check that it *was* a newline) |
| 111 | */ |
| 112 | p[-1] = '\0'; |
J. Tang | 77a2c51 | 2010-03-19 14:48:51 +0100 | [diff] [blame] | 113 | value = end + 1; |
Denys Vlasenko | 6111f96 | 2012-02-23 13:45:18 +0100 | [diff] [blame] | 114 | |
Denys Vlasenko | 015db58 | 2016-06-19 18:15:33 +0200 | [diff] [blame] | 115 | # if ENABLE_FEATURE_TAR_GNU_EXTENSIONS |
Denys Vlasenko | 9655f95 | 2016-11-11 17:56:45 +0100 | [diff] [blame] | 116 | if (!global) { |
| 117 | if (is_prefixed_with(value, "path=")) { |
| 118 | value += sizeof("path=") - 1; |
| 119 | free(archive_handle->tar__longname); |
| 120 | archive_handle->tar__longname = xstrdup(value); |
| 121 | continue; |
| 122 | } |
| 123 | if (is_prefixed_with(value, "linkpath=")) { |
| 124 | value += sizeof("linkpath=") - 1; |
| 125 | free(archive_handle->tar__linkname); |
| 126 | archive_handle->tar__linkname = xstrdup(value); |
| 127 | continue; |
| 128 | } |
Denys Vlasenko | 6111f96 | 2012-02-23 13:45:18 +0100 | [diff] [blame] | 129 | } |
Denys Vlasenko | 015db58 | 2016-06-19 18:15:33 +0200 | [diff] [blame] | 130 | # endif |
Denys Vlasenko | 6111f96 | 2012-02-23 13:45:18 +0100 | [diff] [blame] | 131 | |
Denys Vlasenko | 015db58 | 2016-06-19 18:15:33 +0200 | [diff] [blame] | 132 | # if ENABLE_FEATURE_TAR_SELINUX |
Denys Vlasenko | 6111f96 | 2012-02-23 13:45:18 +0100 | [diff] [blame] | 133 | /* Scan for SELinux contexts, via "RHT.security.selinux" keyword. |
| 134 | * This is what Red Hat's patched version of tar uses. |
| 135 | */ |
Denys Vlasenko | 015db58 | 2016-06-19 18:15:33 +0200 | [diff] [blame] | 136 | # define SELINUX_CONTEXT_KEYWORD "RHT.security.selinux" |
Denys Vlasenko | 8dff01d | 2015-03-12 17:48:34 +0100 | [diff] [blame] | 137 | if (is_prefixed_with(value, SELINUX_CONTEXT_KEYWORD"=")) { |
J. Tang | 77a2c51 | 2010-03-19 14:48:51 +0100 | [diff] [blame] | 138 | value += sizeof(SELINUX_CONTEXT_KEYWORD"=") - 1; |
Denys Vlasenko | 6111f96 | 2012-02-23 13:45:18 +0100 | [diff] [blame] | 139 | free(archive_handle->tar__sctx[global]); |
| 140 | archive_handle->tar__sctx[global] = xstrdup(value); |
| 141 | continue; |
J. Tang | 77a2c51 | 2010-03-19 14:48:51 +0100 | [diff] [blame] | 142 | } |
Denys Vlasenko | 015db58 | 2016-06-19 18:15:33 +0200 | [diff] [blame] | 143 | # endif |
J. Tang | 77a2c51 | 2010-03-19 14:48:51 +0100 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | free(buf); |
Denys Vlasenko | 015db58 | 2016-06-19 18:15:33 +0200 | [diff] [blame] | 147 | #endif |
J. Tang | 77a2c51 | 2010-03-19 14:48:51 +0100 | [diff] [blame] | 148 | } |
J. Tang | 77a2c51 | 2010-03-19 14:48:51 +0100 | [diff] [blame] | 149 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 150 | char FAST_FUNC get_header_tar(archive_handle_t *archive_handle) |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 151 | { |
Denis Vlasenko | 376ce1e | 2006-11-24 14:51:01 +0000 | [diff] [blame] | 152 | file_header_t *file_header = archive_handle->file_header; |
Denys Vlasenko | 52827e3 | 2010-06-26 18:21:36 +0200 | [diff] [blame] | 153 | struct tar_header_t tar; |
Denis Vlasenko | 376ce1e | 2006-11-24 14:51:01 +0000 | [diff] [blame] | 154 | char *cp; |
Denys Vlasenko | bb373db | 2018-01-27 19:04:08 +0100 | [diff] [blame] | 155 | int tar_typeflag; /* can be "char", "int" seems give smaller code */ |
Denis Vlasenko | a80b4a0 | 2007-06-21 12:41:59 +0000 | [diff] [blame] | 156 | int i, sum_u, sum; |
| 157 | #if ENABLE_FEATURE_TAR_OLDSUN_COMPATIBILITY |
| 158 | int sum_s; |
| 159 | #endif |
Denis Vlasenko | d677250 | 2006-11-24 17:21:44 +0000 | [diff] [blame] | 160 | int parse_names; |
Denis Vlasenko | 376ce1e | 2006-11-24 14:51:01 +0000 | [diff] [blame] | 161 | |
Denis Vlasenko | a60936d | 2008-06-28 05:04:09 +0000 | [diff] [blame] | 162 | /* Our "private data" */ |
Denis Vlasenko | a60936d | 2008-06-28 05:04:09 +0000 | [diff] [blame] | 163 | #if ENABLE_FEATURE_TAR_GNU_EXTENSIONS |
Denys Vlasenko | aa4977d | 2010-01-06 10:53:17 +0100 | [diff] [blame] | 164 | # define p_longname (archive_handle->tar__longname) |
| 165 | # define p_linkname (archive_handle->tar__linkname) |
Denis Vlasenko | a60936d | 2008-06-28 05:04:09 +0000 | [diff] [blame] | 166 | #else |
Denys Vlasenko | aa4977d | 2010-01-06 10:53:17 +0100 | [diff] [blame] | 167 | # define p_longname 0 |
| 168 | # define p_linkname 0 |
Denis Vlasenko | a60936d | 2008-06-28 05:04:09 +0000 | [diff] [blame] | 169 | #endif |
Denis Vlasenko | a60936d | 2008-06-28 05:04:09 +0000 | [diff] [blame] | 170 | |
J. Tang | 77a2c51 | 2010-03-19 14:48:51 +0100 | [diff] [blame] | 171 | #if ENABLE_FEATURE_TAR_GNU_EXTENSIONS || ENABLE_FEATURE_TAR_SELINUX |
Denis Vlasenko | 666da5e | 2006-12-26 18:17:42 +0000 | [diff] [blame] | 172 | again: |
| 173 | #endif |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 174 | /* Align header */ |
Glenn L McGrath | 237ae42 | 2002-11-03 14:05:15 +0000 | [diff] [blame] | 175 | data_align(archive_handle, 512); |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 176 | |
Denis Vlasenko | b596335 | 2006-11-26 01:46:59 +0000 | [diff] [blame] | 177 | again_after_align: |
| 178 | |
Denis Vlasenko | 1f0b95f | 2009-03-13 14:26:44 +0000 | [diff] [blame] | 179 | #if ENABLE_DESKTOP || ENABLE_FEATURE_TAR_AUTODETECT |
Denis Vlasenko | 0381d42 | 2008-07-10 23:06:00 +0000 | [diff] [blame] | 180 | /* to prevent misdetection of bz2 sig */ |
Denys Vlasenko | 98a4c7c | 2010-02-04 15:00:15 +0100 | [diff] [blame] | 181 | *(aliased_uint32_t*)&tar = 0; |
Denis Vlasenko | 23ffb6a | 2008-02-13 17:52:42 +0000 | [diff] [blame] | 182 | i = full_read(archive_handle->src_fd, &tar, 512); |
Denis Vlasenko | a60936d | 2008-06-28 05:04:09 +0000 | [diff] [blame] | 183 | /* If GNU tar sees EOF in above read, it says: |
Denis Vlasenko | 23ffb6a | 2008-02-13 17:52:42 +0000 | [diff] [blame] | 184 | * "tar: A lone zero block at N", where N = kilobyte |
| 185 | * where EOF was met (not EOF block, actual EOF!), |
Denis Vlasenko | 0381d42 | 2008-07-10 23:06:00 +0000 | [diff] [blame] | 186 | * and exits with EXIT_SUCCESS. |
Bernhard Reutner-Fischer | 636a1f8 | 2008-05-19 09:29:47 +0000 | [diff] [blame] | 187 | * We will mimic exit(EXIT_SUCCESS), although we will not mimic |
Denis Vlasenko | 12c0622 | 2008-02-14 08:52:30 +0000 | [diff] [blame] | 188 | * the message and we don't check whether we indeed |
| 189 | * saw zero block directly before this. */ |
Denis Vlasenko | 0381d42 | 2008-07-10 23:06:00 +0000 | [diff] [blame] | 190 | if (i == 0) { |
Denys Vlasenko | 9655f95 | 2016-11-11 17:56:45 +0100 | [diff] [blame] | 191 | /* GNU tar 1.29 will be silent if tar archive ends abruptly |
| 192 | * (if there are no zero blocks at all, and last read returns zero, |
| 193 | * not short read 0 < len < 512). Complain only if |
| 194 | * the very first read fails. Grrr. |
| 195 | */ |
| 196 | if (archive_handle->offset == 0) |
| 197 | bb_error_msg("short read"); |
Denys Vlasenko | ebfa9b5 | 2013-11-19 14:44:04 +0100 | [diff] [blame] | 198 | /* this merely signals end of archive, not exit(1): */ |
| 199 | return EXIT_FAILURE; |
Denis Vlasenko | 0381d42 | 2008-07-10 23:06:00 +0000 | [diff] [blame] | 200 | } |
| 201 | if (i != 512) { |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 202 | IF_FEATURE_TAR_AUTODETECT(goto autodetect;) |
Denys Vlasenko | ebfa9b5 | 2013-11-19 14:44:04 +0100 | [diff] [blame] | 203 | bb_error_msg_and_die("short read"); |
Denis Vlasenko | 0381d42 | 2008-07-10 23:06:00 +0000 | [diff] [blame] | 204 | } |
| 205 | |
Denis Vlasenko | 12c0622 | 2008-02-14 08:52:30 +0000 | [diff] [blame] | 206 | #else |
Denis Vlasenko | 0381d42 | 2008-07-10 23:06:00 +0000 | [diff] [blame] | 207 | i = 512; |
| 208 | xread(archive_handle->src_fd, &tar, i); |
Denis Vlasenko | 12c0622 | 2008-02-14 08:52:30 +0000 | [diff] [blame] | 209 | #endif |
Denis Vlasenko | 0381d42 | 2008-07-10 23:06:00 +0000 | [diff] [blame] | 210 | archive_handle->offset += i; |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 211 | |
| 212 | /* If there is no filename its an empty header */ |
Denys Vlasenko | 9655f95 | 2016-11-11 17:56:45 +0100 | [diff] [blame] | 213 | if (tar.name[0] == 0 && tar.prefix[0] == 0 |
| 214 | /* Have seen a tar archive with pax 'x' header supplying UTF8 filename, |
| 215 | * with actual file having all name fields NUL-filled. Check this: */ |
| 216 | && !p_longname |
| 217 | ) { |
Denys Vlasenko | aa4977d | 2010-01-06 10:53:17 +0100 | [diff] [blame] | 218 | if (archive_handle->tar__end) { |
Denis Vlasenko | a60936d | 2008-06-28 05:04:09 +0000 | [diff] [blame] | 219 | /* Second consecutive empty header - end of archive. |
Paul Fox | 94ff9f1 | 2005-07-20 19:24:13 +0000 | [diff] [blame] | 220 | * Read until the end to empty the pipe from gz or bz2 |
| 221 | */ |
Denis Vlasenko | 376ce1e | 2006-11-24 14:51:01 +0000 | [diff] [blame] | 222 | while (full_read(archive_handle->src_fd, &tar, 512) == 512) |
Denis Vlasenko | 431a7c9 | 2008-02-19 11:26:28 +0000 | [diff] [blame] | 223 | continue; |
Denys Vlasenko | ebfa9b5 | 2013-11-19 14:44:04 +0100 | [diff] [blame] | 224 | return EXIT_FAILURE; /* "end of archive" */ |
Paul Fox | 94ff9f1 | 2005-07-20 19:24:13 +0000 | [diff] [blame] | 225 | } |
Denys Vlasenko | aa4977d | 2010-01-06 10:53:17 +0100 | [diff] [blame] | 226 | archive_handle->tar__end = 1; |
Denys Vlasenko | ebfa9b5 | 2013-11-19 14:44:04 +0100 | [diff] [blame] | 227 | return EXIT_SUCCESS; /* "decoded one header" */ |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 228 | } |
Denys Vlasenko | aa4977d | 2010-01-06 10:53:17 +0100 | [diff] [blame] | 229 | archive_handle->tar__end = 0; |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 230 | |
Denis Vlasenko | 431a7c9 | 2008-02-19 11:26:28 +0000 | [diff] [blame] | 231 | /* Check header has valid magic, "ustar" is for the proper tar, |
| 232 | * five NULs are for the old tar format */ |
Denys Vlasenko | 8dff01d | 2015-03-12 17:48:34 +0100 | [diff] [blame] | 233 | if (!is_prefixed_with(tar.magic, "ustar") |
Denis Vlasenko | 431a7c9 | 2008-02-19 11:26:28 +0000 | [diff] [blame] | 234 | && (!ENABLE_FEATURE_TAR_OLDGNU_COMPATIBILITY |
| 235 | || memcmp(tar.magic, "\0\0\0\0", 5) != 0) |
| 236 | ) { |
| 237 | #if ENABLE_FEATURE_TAR_AUTODETECT |
Denis Vlasenko | 1f0b95f | 2009-03-13 14:26:44 +0000 | [diff] [blame] | 238 | autodetect: |
Denis Vlasenko | a60936d | 2008-06-28 05:04:09 +0000 | [diff] [blame] | 239 | /* Two different causes for lseek() != 0: |
| 240 | * unseekable fd (would like to support that too, but...), |
| 241 | * or not first block (false positive, it's not .gz/.bz2!) */ |
Denis Vlasenko | 0381d42 | 2008-07-10 23:06:00 +0000 | [diff] [blame] | 242 | if (lseek(archive_handle->src_fd, -i, SEEK_CUR) != 0) |
Denis Vlasenko | 431a7c9 | 2008-02-19 11:26:28 +0000 | [diff] [blame] | 243 | goto err; |
Denys Vlasenko | 640ce3d | 2014-02-02 02:06:38 +0100 | [diff] [blame] | 244 | if (setup_unzip_on_fd(archive_handle->src_fd, /*fail_if_not_compressed:*/ 0) != 0) |
Denis Vlasenko | 431a7c9 | 2008-02-19 11:26:28 +0000 | [diff] [blame] | 245 | err: |
Denys Vlasenko | 8a6a2f9 | 2012-03-06 16:27:48 +0100 | [diff] [blame] | 246 | bb_error_msg_and_die("invalid tar magic"); |
| 247 | archive_handle->offset = 0; |
| 248 | goto again_after_align; |
| 249 | #endif |
Denis Vlasenko | 431a7c9 | 2008-02-19 11:26:28 +0000 | [diff] [blame] | 250 | bb_error_msg_and_die("invalid tar magic"); |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 251 | } |
Denis Vlasenko | dcbd51d | 2007-03-03 20:06:59 +0000 | [diff] [blame] | 252 | |
| 253 | /* Do checksum on headers. |
| 254 | * POSIX says that checksum is done on unsigned bytes, but |
Denis Vlasenko | 940494f | 2007-03-04 18:09:50 +0000 | [diff] [blame] | 255 | * Sun and HP-UX gets it wrong... more details in |
Denis Vlasenko | dcbd51d | 2007-03-03 20:06:59 +0000 | [diff] [blame] | 256 | * GNU tar source. */ |
Denis Vlasenko | a80b4a0 | 2007-06-21 12:41:59 +0000 | [diff] [blame] | 257 | sum_u = ' ' * sizeof(tar.chksum); |
Denys Vlasenko | bb373db | 2018-01-27 19:04:08 +0100 | [diff] [blame] | 258 | #if ENABLE_FEATURE_TAR_OLDSUN_COMPATIBILITY |
| 259 | sum_s = sum_u; |
| 260 | #endif |
Denis Vlasenko | b71c668 | 2007-07-21 15:08:09 +0000 | [diff] [blame] | 261 | for (i = 0; i < 148; i++) { |
Denis Vlasenko | dcbd51d | 2007-03-03 20:06:59 +0000 | [diff] [blame] | 262 | sum_u += ((unsigned char*)&tar)[i]; |
Denis Vlasenko | a80b4a0 | 2007-06-21 12:41:59 +0000 | [diff] [blame] | 263 | #if ENABLE_FEATURE_TAR_OLDSUN_COMPATIBILITY |
Denis Vlasenko | dcbd51d | 2007-03-03 20:06:59 +0000 | [diff] [blame] | 264 | sum_s += ((signed char*)&tar)[i]; |
Denis Vlasenko | a80b4a0 | 2007-06-21 12:41:59 +0000 | [diff] [blame] | 265 | #endif |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 266 | } |
Denis Vlasenko | b71c668 | 2007-07-21 15:08:09 +0000 | [diff] [blame] | 267 | for (i = 156; i < 512; i++) { |
Denis Vlasenko | dcbd51d | 2007-03-03 20:06:59 +0000 | [diff] [blame] | 268 | sum_u += ((unsigned char*)&tar)[i]; |
Denis Vlasenko | a80b4a0 | 2007-06-21 12:41:59 +0000 | [diff] [blame] | 269 | #if ENABLE_FEATURE_TAR_OLDSUN_COMPATIBILITY |
Denis Vlasenko | dcbd51d | 2007-03-03 20:06:59 +0000 | [diff] [blame] | 270 | sum_s += ((signed char*)&tar)[i]; |
Denis Vlasenko | a80b4a0 | 2007-06-21 12:41:59 +0000 | [diff] [blame] | 271 | #endif |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 272 | } |
Denys Vlasenko | bb373db | 2018-01-27 19:04:08 +0100 | [diff] [blame] | 273 | /* Most tarfiles have tar.chksum NUL or space terminated, but |
| 274 | * github.com decided to be "special" and have unterminated field: |
| 275 | * 0090: 30343300 30303031 33323731 30000000 |043.000132710...| |
| 276 | * ^^^^^^^^| |
| 277 | * Need to use GET_OCTAL. This overwrites tar.typeflag ---+ |
| 278 | * (the '0' char immediately after chksum in example above) with NUL. |
| 279 | */ |
| 280 | tar_typeflag = (uint8_t)tar.typeflag; /* save it */ |
| 281 | sum = GET_OCTAL(tar.chksum); |
| 282 | if (sum_u != sum |
| 283 | IF_FEATURE_TAR_OLDSUN_COMPATIBILITY(&& sum_s != sum) |
| 284 | ) { |
Denis Vlasenko | a80b4a0 | 2007-06-21 12:41:59 +0000 | [diff] [blame] | 285 | bb_error_msg_and_die("invalid tar header checksum"); |
| 286 | } |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 287 | |
Denys Vlasenko | bb373db | 2018-01-27 19:04:08 +0100 | [diff] [blame] | 288 | /* GET_OCTAL trashes subsequent field, therefore we call it |
Denis Vlasenko | 376ce1e | 2006-11-24 14:51:01 +0000 | [diff] [blame] | 289 | * on fields in reverse order */ |
Denis Vlasenko | 376ce1e | 2006-11-24 14:51:01 +0000 | [diff] [blame] | 290 | if (tar.devmajor[0]) { |
Denis Vlasenko | d93400b | 2008-04-29 03:54:16 +0000 | [diff] [blame] | 291 | char t = tar.prefix[0]; |
| 292 | /* we trash prefix[0] here, but we DO need it later! */ |
Denis Vlasenko | 376ce1e | 2006-11-24 14:51:01 +0000 | [diff] [blame] | 293 | unsigned minor = GET_OCTAL(tar.devminor); |
| 294 | unsigned major = GET_OCTAL(tar.devmajor); |
| 295 | file_header->device = makedev(major, minor); |
Denis Vlasenko | d93400b | 2008-04-29 03:54:16 +0000 | [diff] [blame] | 296 | tar.prefix[0] = t; |
Denis Vlasenko | cba9ef5 | 2006-10-10 21:00:47 +0000 | [diff] [blame] | 297 | } |
Denys Vlasenko | bb373db | 2018-01-27 19:04:08 +0100 | [diff] [blame] | 298 | |
| 299 | /* 0 is reserved for high perf file, treat as normal file */ |
| 300 | if (tar_typeflag == '\0') tar_typeflag = '0'; |
| 301 | parse_names = (tar_typeflag >= '0' && tar_typeflag <= '7'); |
| 302 | |
Denis Vlasenko | 7510384 | 2007-06-20 14:49:47 +0000 | [diff] [blame] | 303 | file_header->link_target = NULL; |
Denis Vlasenko | a60936d | 2008-06-28 05:04:09 +0000 | [diff] [blame] | 304 | if (!p_linkname && parse_names && tar.linkname[0]) { |
Denis Vlasenko | bc1918a | 2008-04-15 01:17:50 +0000 | [diff] [blame] | 305 | file_header->link_target = xstrndup(tar.linkname, sizeof(tar.linkname)); |
Denis Vlasenko | 7510384 | 2007-06-20 14:49:47 +0000 | [diff] [blame] | 306 | /* FIXME: what if we have non-link object with link_target? */ |
| 307 | /* Will link_target be free()ed? */ |
Denis Vlasenko | d677250 | 2006-11-24 17:21:44 +0000 | [diff] [blame] | 308 | } |
Denis Vlasenko | e00e502 | 2008-02-14 20:37:54 +0000 | [diff] [blame] | 309 | #if ENABLE_FEATURE_TAR_UNAME_GNAME |
Denys Vlasenko | aa4977d | 2010-01-06 10:53:17 +0100 | [diff] [blame] | 310 | file_header->tar__uname = tar.uname[0] ? xstrndup(tar.uname, sizeof(tar.uname)) : NULL; |
| 311 | file_header->tar__gname = tar.gname[0] ? xstrndup(tar.gname, sizeof(tar.gname)) : NULL; |
Denis Vlasenko | e00e502 | 2008-02-14 20:37:54 +0000 | [diff] [blame] | 312 | #endif |
Denys Vlasenko | 7b48eb4 | 2010-05-06 20:08:14 +0200 | [diff] [blame] | 313 | file_header->mtime = GET_OCTAL(tar.mtime); |
| 314 | file_header->size = GET_OCTAL(tar.size); |
Denis Vlasenko | 376ce1e | 2006-11-24 14:51:01 +0000 | [diff] [blame] | 315 | file_header->gid = GET_OCTAL(tar.gid); |
| 316 | file_header->uid = GET_OCTAL(tar.uid); |
Glenn L McGrath | 916ba53 | 2004-02-20 02:34:42 +0000 | [diff] [blame] | 317 | /* Set bits 0-11 of the files mode */ |
Denis Vlasenko | 376ce1e | 2006-11-24 14:51:01 +0000 | [diff] [blame] | 318 | file_header->mode = 07777 & GET_OCTAL(tar.mode); |
Glenn L McGrath | 916ba53 | 2004-02-20 02:34:42 +0000 | [diff] [blame] | 319 | |
Denis Vlasenko | b596335 | 2006-11-26 01:46:59 +0000 | [diff] [blame] | 320 | file_header->name = NULL; |
Denis Vlasenko | a60936d | 2008-06-28 05:04:09 +0000 | [diff] [blame] | 321 | if (!p_longname && parse_names) { |
Denis Vlasenko | d677250 | 2006-11-24 17:21:44 +0000 | [diff] [blame] | 322 | /* we trash mode[0] here, it's ok */ |
Denis Vlasenko | bc1918a | 2008-04-15 01:17:50 +0000 | [diff] [blame] | 323 | //tar.name[sizeof(tar.name)] = '\0'; - gcc 4.3.0 would complain |
| 324 | tar.mode[0] = '\0'; |
Denis Vlasenko | 87cd4a8 | 2006-11-25 23:47:32 +0000 | [diff] [blame] | 325 | if (tar.prefix[0]) { |
| 326 | /* and padding[0] */ |
Denis Vlasenko | bc1918a | 2008-04-15 01:17:50 +0000 | [diff] [blame] | 327 | //tar.prefix[sizeof(tar.prefix)] = '\0'; - gcc 4.3.0 would complain |
| 328 | tar.padding[0] = '\0'; |
Denis Vlasenko | cf30cc8 | 2006-11-24 14:53:18 +0000 | [diff] [blame] | 329 | file_header->name = concat_path_file(tar.prefix, tar.name); |
Denis Vlasenko | 87cd4a8 | 2006-11-25 23:47:32 +0000 | [diff] [blame] | 330 | } else |
Denis Vlasenko | cf30cc8 | 2006-11-24 14:53:18 +0000 | [diff] [blame] | 331 | file_header->name = xstrdup(tar.name); |
| 332 | } |
| 333 | |
Glenn L McGrath | 916ba53 | 2004-02-20 02:34:42 +0000 | [diff] [blame] | 334 | /* Set bits 12-15 of the files mode */ |
Denis Vlasenko | d677250 | 2006-11-24 17:21:44 +0000 | [diff] [blame] | 335 | /* (typeflag was not trashed because chksum does not use getOctal) */ |
Denys Vlasenko | bb373db | 2018-01-27 19:04:08 +0100 | [diff] [blame] | 336 | switch (tar_typeflag) { |
Ian Wienand | 954dbd3 | 2011-07-29 08:33:47 +0200 | [diff] [blame] | 337 | case '1': /* hardlink */ |
| 338 | /* we mark hardlinks as regular files with zero size and a link name */ |
Glenn L McGrath | 916ba53 | 2004-02-20 02:34:42 +0000 | [diff] [blame] | 339 | file_header->mode |= S_IFREG; |
Ian Wienand | 954dbd3 | 2011-07-29 08:33:47 +0200 | [diff] [blame] | 340 | /* on size of link fields from star(4) |
| 341 | * ... For tar archives written by pre POSIX.1-1988 |
| 342 | * implementations, the size field usually contains the size of |
| 343 | * the file and needs to be ignored as no data may follow this |
| 344 | * header type. For POSIX.1- 1988 compliant archives, the size |
| 345 | * field needs to be 0. For POSIX.1-2001 compliant archives, |
| 346 | * the size field may be non zero, indicating that file data is |
| 347 | * included in the archive. |
| 348 | * i.e; always assume this is zero for safety. |
| 349 | */ |
| 350 | goto size0; |
Glenn L McGrath | c9f1fce | 2004-02-20 02:25:18 +0000 | [diff] [blame] | 351 | case '7': |
Denis Vlasenko | b596335 | 2006-11-26 01:46:59 +0000 | [diff] [blame] | 352 | /* case 0: */ |
Glenn L McGrath | 21110a0 | 2003-01-28 01:45:48 +0000 | [diff] [blame] | 353 | case '0': |
Denis Vlasenko | d677250 | 2006-11-24 17:21:44 +0000 | [diff] [blame] | 354 | #if ENABLE_FEATURE_TAR_OLDGNU_COMPATIBILITY |
Glenn L McGrath | 87af49f | 2003-09-09 17:41:03 +0000 | [diff] [blame] | 355 | if (last_char_is(file_header->name, '/')) { |
Denis Vlasenko | adc772a | 2008-07-20 17:10:43 +0000 | [diff] [blame] | 356 | goto set_dir; |
| 357 | } |
Glenn L McGrath | c9f1fce | 2004-02-20 02:25:18 +0000 | [diff] [blame] | 358 | #endif |
Denis Vlasenko | 376ce1e | 2006-11-24 14:51:01 +0000 | [diff] [blame] | 359 | file_header->mode |= S_IFREG; |
Glenn L McGrath | 99b1254 | 2002-08-22 17:47:09 +0000 | [diff] [blame] | 360 | break; |
Glenn L McGrath | 21110a0 | 2003-01-28 01:45:48 +0000 | [diff] [blame] | 361 | case '2': |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 362 | file_header->mode |= S_IFLNK; |
Denis Vlasenko | adc772a | 2008-07-20 17:10:43 +0000 | [diff] [blame] | 363 | /* have seen tarballs with size field containing |
| 364 | * the size of the link target's name */ |
| 365 | size0: |
| 366 | file_header->size = 0; |
Glenn L McGrath | 99b1254 | 2002-08-22 17:47:09 +0000 | [diff] [blame] | 367 | break; |
Glenn L McGrath | 21110a0 | 2003-01-28 01:45:48 +0000 | [diff] [blame] | 368 | case '3': |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 369 | file_header->mode |= S_IFCHR; |
Denis Vlasenko | adc772a | 2008-07-20 17:10:43 +0000 | [diff] [blame] | 370 | goto size0; /* paranoia */ |
Glenn L McGrath | 21110a0 | 2003-01-28 01:45:48 +0000 | [diff] [blame] | 371 | case '4': |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 372 | file_header->mode |= S_IFBLK; |
Denis Vlasenko | adc772a | 2008-07-20 17:10:43 +0000 | [diff] [blame] | 373 | goto size0; |
Glenn L McGrath | 21110a0 | 2003-01-28 01:45:48 +0000 | [diff] [blame] | 374 | case '5': |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 375 | IF_FEATURE_TAR_OLDGNU_COMPATIBILITY(set_dir:) |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 376 | file_header->mode |= S_IFDIR; |
Denis Vlasenko | adc772a | 2008-07-20 17:10:43 +0000 | [diff] [blame] | 377 | goto size0; |
Glenn L McGrath | 21110a0 | 2003-01-28 01:45:48 +0000 | [diff] [blame] | 378 | case '6': |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 379 | file_header->mode |= S_IFIFO; |
Denis Vlasenko | adc772a | 2008-07-20 17:10:43 +0000 | [diff] [blame] | 380 | goto size0; |
Denys Vlasenko | bdb540e | 2015-05-11 16:55:16 +0200 | [diff] [blame] | 381 | case 'g': /* pax global header */ |
| 382 | case 'x': { /* pax extended header */ |
| 383 | if ((uoff_t)file_header->size > 0xfffff) /* paranoia */ |
| 384 | goto skip_ext_hdr; |
Denys Vlasenko | bb373db | 2018-01-27 19:04:08 +0100 | [diff] [blame] | 385 | process_pax_hdr(archive_handle, file_header->size, (tar_typeflag == 'g')); |
Denys Vlasenko | bdb540e | 2015-05-11 16:55:16 +0200 | [diff] [blame] | 386 | goto again_after_align; |
Denis Vlasenko | d677250 | 2006-11-24 17:21:44 +0000 | [diff] [blame] | 387 | #if ENABLE_FEATURE_TAR_GNU_EXTENSIONS |
Denys Vlasenko | bdb540e | 2015-05-11 16:55:16 +0200 | [diff] [blame] | 388 | /* See http://www.gnu.org/software/tar/manual/html_node/Extensions.html */ |
Denis Vlasenko | 376ce1e | 2006-11-24 14:51:01 +0000 | [diff] [blame] | 389 | case 'L': |
Denis Vlasenko | b596335 | 2006-11-26 01:46:59 +0000 | [diff] [blame] | 390 | /* free: paranoia: tar with several consecutive longnames */ |
Denis Vlasenko | a60936d | 2008-06-28 05:04:09 +0000 | [diff] [blame] | 391 | free(p_longname); |
Denis Vlasenko | b596335 | 2006-11-26 01:46:59 +0000 | [diff] [blame] | 392 | /* For paranoia reasons we allocate extra NUL char */ |
Denis Vlasenko | a60936d | 2008-06-28 05:04:09 +0000 | [diff] [blame] | 393 | p_longname = xzalloc(file_header->size + 1); |
Denis Vlasenko | b596335 | 2006-11-26 01:46:59 +0000 | [diff] [blame] | 394 | /* We read ASCIZ string, including NUL */ |
Denis Vlasenko | a60936d | 2008-06-28 05:04:09 +0000 | [diff] [blame] | 395 | xread(archive_handle->src_fd, p_longname, file_header->size); |
Denis Vlasenko | 376ce1e | 2006-11-24 14:51:01 +0000 | [diff] [blame] | 396 | archive_handle->offset += file_header->size; |
Denis Vlasenko | b596335 | 2006-11-26 01:46:59 +0000 | [diff] [blame] | 397 | /* return get_header_tar(archive_handle); */ |
| 398 | /* gcc 4.1.1 didn't optimize it into jump */ |
| 399 | /* so we will do it ourself, this also saves stack */ |
| 400 | goto again; |
Denis Vlasenko | 376ce1e | 2006-11-24 14:51:01 +0000 | [diff] [blame] | 401 | case 'K': |
Denis Vlasenko | a60936d | 2008-06-28 05:04:09 +0000 | [diff] [blame] | 402 | free(p_linkname); |
| 403 | p_linkname = xzalloc(file_header->size + 1); |
| 404 | xread(archive_handle->src_fd, p_linkname, file_header->size); |
Denis Vlasenko | 376ce1e | 2006-11-24 14:51:01 +0000 | [diff] [blame] | 405 | archive_handle->offset += file_header->size; |
Denis Vlasenko | b596335 | 2006-11-26 01:46:59 +0000 | [diff] [blame] | 406 | /* return get_header_tar(archive_handle); */ |
| 407 | goto again; |
Denys Vlasenko | bdb540e | 2015-05-11 16:55:16 +0200 | [diff] [blame] | 408 | /* |
| 409 | * case 'S': // Sparse file |
| 410 | * Was seen in the wild. Not supported (yet?). |
| 411 | * See https://www.gnu.org/software/tar/manual/html_section/tar_92.html |
| 412 | * for the format. (An "Old GNU Format" was seen, not PAX formats). |
| 413 | */ |
| 414 | // case 'D': /* GNU dump dir */ |
| 415 | // case 'M': /* Continuation of multi volume archive */ |
| 416 | // case 'N': /* Old GNU for names > 100 characters */ |
| 417 | // case 'V': /* Volume header */ |
Glenn L McGrath | 6aa5223 | 2004-02-17 11:55:06 +0000 | [diff] [blame] | 418 | #endif |
Denys Vlasenko | 6111f96 | 2012-02-23 13:45:18 +0100 | [diff] [blame] | 419 | } |
J. Tang | 77a2c51 | 2010-03-19 14:48:51 +0100 | [diff] [blame] | 420 | skip_ext_hdr: |
J. Tang | 77a2c51 | 2010-03-19 14:48:51 +0100 | [diff] [blame] | 421 | { |
Denis Vlasenko | b596335 | 2006-11-26 01:46:59 +0000 | [diff] [blame] | 422 | off_t sz; |
Denys Vlasenko | bb373db | 2018-01-27 19:04:08 +0100 | [diff] [blame] | 423 | bb_error_msg("warning: skipping header '%c'", tar_typeflag); |
Denis Vlasenko | b596335 | 2006-11-26 01:46:59 +0000 | [diff] [blame] | 424 | sz = (file_header->size + 511) & ~(off_t)511; |
| 425 | archive_handle->offset += sz; |
| 426 | sz >>= 9; /* sz /= 512 but w/o contortions for signed div */ |
| 427 | while (sz--) |
| 428 | xread(archive_handle->src_fd, &tar, 512); |
| 429 | /* return get_header_tar(archive_handle); */ |
| 430 | goto again_after_align; |
| 431 | } |
Glenn L McGrath | b0e163a | 2004-02-19 08:48:30 +0000 | [diff] [blame] | 432 | default: |
Denys Vlasenko | bb373db | 2018-01-27 19:04:08 +0100 | [diff] [blame] | 433 | bb_error_msg_and_die("unknown typeflag: 0x%x", tar_typeflag); |
Glenn L McGrath | 6aa5223 | 2004-02-17 11:55:06 +0000 | [diff] [blame] | 434 | } |
Denis Vlasenko | 376ce1e | 2006-11-24 14:51:01 +0000 | [diff] [blame] | 435 | |
Denis Vlasenko | d677250 | 2006-11-24 17:21:44 +0000 | [diff] [blame] | 436 | #if ENABLE_FEATURE_TAR_GNU_EXTENSIONS |
Denis Vlasenko | a60936d | 2008-06-28 05:04:09 +0000 | [diff] [blame] | 437 | if (p_longname) { |
| 438 | file_header->name = p_longname; |
| 439 | p_longname = NULL; |
Denis Vlasenko | d677250 | 2006-11-24 17:21:44 +0000 | [diff] [blame] | 440 | } |
Denis Vlasenko | a60936d | 2008-06-28 05:04:09 +0000 | [diff] [blame] | 441 | if (p_linkname) { |
| 442 | file_header->link_target = p_linkname; |
| 443 | p_linkname = NULL; |
Denis Vlasenko | d677250 | 2006-11-24 17:21:44 +0000 | [diff] [blame] | 444 | } |
| 445 | #endif |
Denys Vlasenko | 5e29e26 | 2011-03-01 17:21:07 +0100 | [diff] [blame] | 446 | |
| 447 | /* Everything up to and including last ".." component is stripped */ |
Denys Vlasenko | b80acf5 | 2011-03-02 01:21:02 +0100 | [diff] [blame] | 448 | overlapping_strcpy(file_header->name, strip_unsafe_prefix(file_header->name)); |
Denys Vlasenko | 6c563e3 | 2015-10-22 01:07:13 +0200 | [diff] [blame] | 449 | //TODO: do the same for file_header->link_target? |
Denis Vlasenko | d677250 | 2006-11-24 17:21:44 +0000 | [diff] [blame] | 450 | |
Denis Vlasenko | 376ce1e | 2006-11-24 14:51:01 +0000 | [diff] [blame] | 451 | /* Strip trailing '/' in directories */ |
Denis Vlasenko | 714701c | 2006-12-22 00:21:07 +0000 | [diff] [blame] | 452 | /* Must be done after mode is set as '/' is used to check if it's a directory */ |
Denis Vlasenko | 376ce1e | 2006-11-24 14:51:01 +0000 | [diff] [blame] | 453 | cp = last_char_is(file_header->name, '/'); |
Glenn L McGrath | 3d5828f | 2003-08-14 02:55:15 +0000 | [diff] [blame] | 454 | |
Glenn L McGrath | 8e94098 | 2002-11-04 23:47:31 +0000 | [diff] [blame] | 455 | if (archive_handle->filter(archive_handle) == EXIT_SUCCESS) { |
Denis Vlasenko | 0381d42 | 2008-07-10 23:06:00 +0000 | [diff] [blame] | 456 | archive_handle->action_header(/*archive_handle->*/ file_header); |
Denis Vlasenko | cf30cc8 | 2006-11-24 14:53:18 +0000 | [diff] [blame] | 457 | /* Note that we kill the '/' only after action_header() */ |
| 458 | /* (like GNU tar 1.15.1: verbose mode outputs "dir/dir/") */ |
Denys Vlasenko | 02365a6 | 2010-04-09 10:52:52 +0200 | [diff] [blame] | 459 | if (cp) |
| 460 | *cp = '\0'; |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 461 | archive_handle->action_data(archive_handle); |
Denys Vlasenko | 440a509 | 2012-06-22 16:27:21 +0200 | [diff] [blame] | 462 | if (archive_handle->accept || archive_handle->reject |
| 463 | || (archive_handle->ah_flags & ARCHIVE_REMEMBER_NAMES) |
| 464 | ) { |
Denys Vlasenko | b768aeb | 2010-06-26 18:22:41 +0200 | [diff] [blame] | 465 | llist_add_to(&archive_handle->passed, file_header->name); |
Denys Vlasenko | 440a509 | 2012-06-22 16:27:21 +0200 | [diff] [blame] | 466 | } else /* Caller isn't interested in list of unpacked files */ |
Denys Vlasenko | b768aeb | 2010-06-26 18:22:41 +0200 | [diff] [blame] | 467 | free(file_header->name); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 468 | } else { |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 469 | data_skip(archive_handle); |
Denis Vlasenko | b596335 | 2006-11-26 01:46:59 +0000 | [diff] [blame] | 470 | free(file_header->name); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 471 | } |
| 472 | archive_handle->offset += file_header->size; |
| 473 | |
Denis Vlasenko | 7510384 | 2007-06-20 14:49:47 +0000 | [diff] [blame] | 474 | free(file_header->link_target); |
Denys Vlasenko | b768aeb | 2010-06-26 18:22:41 +0200 | [diff] [blame] | 475 | /* Do not free(file_header->name)! |
| 476 | * It might be inserted in archive_handle->passed - see above */ |
Denis Vlasenko | e00e502 | 2008-02-14 20:37:54 +0000 | [diff] [blame] | 477 | #if ENABLE_FEATURE_TAR_UNAME_GNAME |
Denys Vlasenko | aa4977d | 2010-01-06 10:53:17 +0100 | [diff] [blame] | 478 | free(file_header->tar__uname); |
| 479 | free(file_header->tar__gname); |
Denis Vlasenko | e00e502 | 2008-02-14 20:37:54 +0000 | [diff] [blame] | 480 | #endif |
Denys Vlasenko | ebfa9b5 | 2013-11-19 14:44:04 +0100 | [diff] [blame] | 481 | return EXIT_SUCCESS; /* "decoded one header" */ |
Eric Andersen | 2276d83 | 2002-07-11 11:11:56 +0000 | [diff] [blame] | 482 | } |