"Robert P. J. Day" | 63fc1a9 | 2006-07-02 19:47:05 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Rob Landley | 06249fe | 2006-02-20 19:28:53 +0000 | [diff] [blame] | 2 | /* Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
Glenn L McGrath | 6aa5223 | 2004-02-17 11:55:06 +0000 | [diff] [blame] | 3 | * |
Glenn L McGrath | c9f1fce | 2004-02-20 02:25:18 +0000 | [diff] [blame] | 4 | * FIXME: |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 5 | * 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] | 6 | * mapped value instead of the uid/gid values in tar header |
Glenn L McGrath | b0e163a | 2004-02-19 08:48:30 +0000 | [diff] [blame] | 7 | * |
| 8 | * References: |
| 9 | * GNU tar and star man pages, |
| 10 | * Opengroup's ustar interchange format, |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 11 | * http://www.opengroup.org/onlinepubs/007904975/utilities/pax.html |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 12 | */ |
| 13 | |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 14 | #include "libbb.h" |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 15 | #include "unarchive.h" |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 16 | |
Denis Vlasenko | d677250 | 2006-11-24 17:21:44 +0000 | [diff] [blame] | 17 | #if ENABLE_FEATURE_TAR_GNU_EXTENSIONS |
| 18 | static char *longname; |
| 19 | static char *linkname; |
| 20 | #else |
| 21 | enum { |
| 22 | longname = 0, |
| 23 | linkname = 0, |
| 24 | }; |
Glenn L McGrath | b7a76df | 2002-11-23 10:44:47 +0000 | [diff] [blame] | 25 | #endif |
| 26 | |
Denis Vlasenko | 376ce1e | 2006-11-24 14:51:01 +0000 | [diff] [blame] | 27 | /* NB: _DESTROYS_ str[len] character! */ |
| 28 | static unsigned long long getOctal(char *str, int len) |
| 29 | { |
| 30 | unsigned long long v; |
| 31 | /* Actually, tar header allows leading spaces also. |
| 32 | * Oh well, we will be liberal and skip this... |
| 33 | * The only downside probably is that we allow "-123" too :) |
| 34 | if (*str < '0' || *str > '7') |
| 35 | bb_error_msg_and_die("corrupted octal value in tar header"); |
| 36 | */ |
| 37 | str[len] = '\0'; |
| 38 | v = strtoull(str, &str, 8); |
Denis Vlasenko | a80b4a0 | 2007-06-21 12:41:59 +0000 | [diff] [blame] | 39 | if (*str && (!ENABLE_FEATURE_TAR_OLDGNU_COMPATIBILITY || *str != ' ')) |
Denis Vlasenko | 376ce1e | 2006-11-24 14:51:01 +0000 | [diff] [blame] | 40 | bb_error_msg_and_die("corrupted octal value in tar header"); |
| 41 | return v; |
| 42 | } |
Denis Vlasenko | b596335 | 2006-11-26 01:46:59 +0000 | [diff] [blame] | 43 | #define GET_OCTAL(a) getOctal((a), sizeof(a)) |
Denis Vlasenko | 376ce1e | 2006-11-24 14:51:01 +0000 | [diff] [blame] | 44 | |
| 45 | void BUG_tar_header_size(void); |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 46 | char get_header_tar(archive_handle_t *archive_handle) |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 47 | { |
Denis Vlasenko | a80b4a0 | 2007-06-21 12:41:59 +0000 | [diff] [blame] | 48 | static smallint end; |
Denis Vlasenko | 431a7c9 | 2008-02-19 11:26:28 +0000 | [diff] [blame] | 49 | #if ENABLE_FEATURE_TAR_AUTODETECT |
| 50 | static smallint not_first; |
| 51 | #endif |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 52 | |
Denis Vlasenko | 376ce1e | 2006-11-24 14:51:01 +0000 | [diff] [blame] | 53 | file_header_t *file_header = archive_handle->file_header; |
| 54 | struct { |
| 55 | /* ustar header, Posix 1003.1 */ |
| 56 | char name[100]; /* 0-99 */ |
| 57 | char mode[8]; /* 100-107 */ |
| 58 | char uid[8]; /* 108-115 */ |
| 59 | char gid[8]; /* 116-123 */ |
| 60 | char size[12]; /* 124-135 */ |
| 61 | char mtime[12]; /* 136-147 */ |
| 62 | char chksum[8]; /* 148-155 */ |
| 63 | char typeflag; /* 156-156 */ |
| 64 | char linkname[100]; /* 157-256 */ |
Denis Vlasenko | 8512862 | 2007-11-16 20:35:30 +0000 | [diff] [blame] | 65 | /* POSIX: "ustar" NUL "00" */ |
| 66 | /* GNU tar: "ustar " NUL */ |
Paul Fox | 16aec39 | 2007-11-17 19:11:05 +0000 | [diff] [blame] | 67 | /* Normally it's defined as magic[6] followed by |
| 68 | * version[2], but we put them together to save code. |
| 69 | */ |
Denis Vlasenko | 8512862 | 2007-11-16 20:35:30 +0000 | [diff] [blame] | 70 | char magic[8]; /* 257-264 */ |
Denis Vlasenko | 376ce1e | 2006-11-24 14:51:01 +0000 | [diff] [blame] | 71 | char uname[32]; /* 265-296 */ |
| 72 | char gname[32]; /* 297-328 */ |
| 73 | char devmajor[8]; /* 329-336 */ |
| 74 | char devminor[8]; /* 337-344 */ |
| 75 | char prefix[155]; /* 345-499 */ |
| 76 | char padding[12]; /* 500-512 */ |
| 77 | } tar; |
| 78 | char *cp; |
Denis Vlasenko | a80b4a0 | 2007-06-21 12:41:59 +0000 | [diff] [blame] | 79 | int i, sum_u, sum; |
| 80 | #if ENABLE_FEATURE_TAR_OLDSUN_COMPATIBILITY |
| 81 | int sum_s; |
| 82 | #endif |
Denis Vlasenko | d677250 | 2006-11-24 17:21:44 +0000 | [diff] [blame] | 83 | int parse_names; |
Denis Vlasenko | 376ce1e | 2006-11-24 14:51:01 +0000 | [diff] [blame] | 84 | |
| 85 | if (sizeof(tar) != 512) |
| 86 | BUG_tar_header_size(); |
| 87 | |
Denis Vlasenko | 666da5e | 2006-12-26 18:17:42 +0000 | [diff] [blame] | 88 | #if ENABLE_FEATURE_TAR_GNU_EXTENSIONS |
| 89 | again: |
| 90 | #endif |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 91 | /* Align header */ |
Glenn L McGrath | 237ae42 | 2002-11-03 14:05:15 +0000 | [diff] [blame] | 92 | data_align(archive_handle, 512); |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 93 | |
Denis Vlasenko | b596335 | 2006-11-26 01:46:59 +0000 | [diff] [blame] | 94 | again_after_align: |
| 95 | |
Denis Vlasenko | 12c0622 | 2008-02-14 08:52:30 +0000 | [diff] [blame] | 96 | #if ENABLE_DESKTOP |
Denis Vlasenko | 23ffb6a | 2008-02-13 17:52:42 +0000 | [diff] [blame] | 97 | i = full_read(archive_handle->src_fd, &tar, 512); |
| 98 | /* if GNU tar sees EOF in above read, it says: |
| 99 | * "tar: A lone zero block at N", where N = kilobyte |
| 100 | * where EOF was met (not EOF block, actual EOF!), |
Denis Vlasenko | 12c0622 | 2008-02-14 08:52:30 +0000 | [diff] [blame] | 101 | * and tar will exit with error code 0. |
| 102 | * We will mimic exit(0), although we will not mimic |
| 103 | * the message and we don't check whether we indeed |
| 104 | * saw zero block directly before this. */ |
Denis Vlasenko | 23ffb6a | 2008-02-13 17:52:42 +0000 | [diff] [blame] | 105 | if (i == 0) |
| 106 | xfunc_error_retval = 0; |
| 107 | if (i != 512) |
| 108 | bb_error_msg_and_die("short read"); |
Denis Vlasenko | 12c0622 | 2008-02-14 08:52:30 +0000 | [diff] [blame] | 109 | #else |
| 110 | xread(archive_handle->src_fd, &tar, 512); |
| 111 | #endif |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 112 | archive_handle->offset += 512; |
| 113 | |
| 114 | /* If there is no filename its an empty header */ |
Denis Vlasenko | 376ce1e | 2006-11-24 14:51:01 +0000 | [diff] [blame] | 115 | if (tar.name[0] == 0) { |
Paul Fox | 94ff9f1 | 2005-07-20 19:24:13 +0000 | [diff] [blame] | 116 | if (end) { |
| 117 | /* This is the second consecutive empty header! End of archive! |
| 118 | * Read until the end to empty the pipe from gz or bz2 |
| 119 | */ |
Denis Vlasenko | 376ce1e | 2006-11-24 14:51:01 +0000 | [diff] [blame] | 120 | while (full_read(archive_handle->src_fd, &tar, 512) == 512) |
Denis Vlasenko | 431a7c9 | 2008-02-19 11:26:28 +0000 | [diff] [blame] | 121 | continue; |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 122 | return EXIT_FAILURE; |
Paul Fox | 94ff9f1 | 2005-07-20 19:24:13 +0000 | [diff] [blame] | 123 | } |
| 124 | end = 1; |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 125 | return EXIT_SUCCESS; |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 126 | } |
Paul Fox | 94ff9f1 | 2005-07-20 19:24:13 +0000 | [diff] [blame] | 127 | end = 0; |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 128 | |
Denis Vlasenko | 431a7c9 | 2008-02-19 11:26:28 +0000 | [diff] [blame] | 129 | /* Check header has valid magic, "ustar" is for the proper tar, |
| 130 | * five NULs are for the old tar format */ |
| 131 | if (strncmp(tar.magic, "ustar", 5) != 0 |
| 132 | && (!ENABLE_FEATURE_TAR_OLDGNU_COMPATIBILITY |
| 133 | || memcmp(tar.magic, "\0\0\0\0", 5) != 0) |
| 134 | ) { |
| 135 | #if ENABLE_FEATURE_TAR_AUTODETECT |
| 136 | char (*get_header_ptr)(archive_handle_t *); |
| 137 | |
| 138 | /* tar gz/bz autodetect: check for gz/bz2 magic. |
| 139 | * If it is the very first block, and we see the magic, |
| 140 | * we can switch to get_header_tar_gz/bz2/lzma(). |
| 141 | * Needs seekable fd. I wish recv(MSG_PEEK) would work |
| 142 | * on any fd... */ |
| 143 | if (not_first) |
| 144 | goto err; |
| 145 | #if ENABLE_FEATURE_TAR_GZIP |
| 146 | if (tar.name[0] == 0x1f && tar.name[1] == 0x8b) { /* gzip */ |
| 147 | get_header_ptr = get_header_tar_gz; |
| 148 | } else |
Glenn L McGrath | 1d23f3a | 2002-08-13 05:06:43 +0000 | [diff] [blame] | 149 | #endif |
Denis Vlasenko | 431a7c9 | 2008-02-19 11:26:28 +0000 | [diff] [blame] | 150 | #if ENABLE_FEATURE_TAR_BZIP2 |
| 151 | if (tar.name[0] == 'B' && tar.name[1] == 'Z' |
| 152 | && tar.name[2] == 'h' && isdigit(tar.name[3]) |
| 153 | ) { /* bzip2 */ |
| 154 | get_header_ptr = get_header_tar_bz2; |
| 155 | } else |
| 156 | #endif |
| 157 | goto err; |
| 158 | if (lseek(archive_handle->src_fd, -512, SEEK_CUR) != 0) |
| 159 | goto err; |
| 160 | while (get_header_ptr(archive_handle) == EXIT_SUCCESS) |
| 161 | continue; |
| 162 | return EXIT_FAILURE; |
| 163 | err: |
| 164 | #endif /* FEATURE_TAR_AUTODETECT */ |
| 165 | bb_error_msg_and_die("invalid tar magic"); |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 166 | } |
Denis Vlasenko | dcbd51d | 2007-03-03 20:06:59 +0000 | [diff] [blame] | 167 | |
Denis Vlasenko | 431a7c9 | 2008-02-19 11:26:28 +0000 | [diff] [blame] | 168 | #if ENABLE_FEATURE_TAR_AUTODETECT |
| 169 | not_first = 1; |
| 170 | #endif |
| 171 | |
Denis Vlasenko | dcbd51d | 2007-03-03 20:06:59 +0000 | [diff] [blame] | 172 | /* Do checksum on headers. |
| 173 | * POSIX says that checksum is done on unsigned bytes, but |
Denis Vlasenko | 940494f | 2007-03-04 18:09:50 +0000 | [diff] [blame] | 174 | * Sun and HP-UX gets it wrong... more details in |
Denis Vlasenko | dcbd51d | 2007-03-03 20:06:59 +0000 | [diff] [blame] | 175 | * GNU tar source. */ |
Denis Vlasenko | a80b4a0 | 2007-06-21 12:41:59 +0000 | [diff] [blame] | 176 | #if ENABLE_FEATURE_TAR_OLDSUN_COMPATIBILITY |
| 177 | sum_s = ' ' * sizeof(tar.chksum); |
| 178 | #endif |
| 179 | sum_u = ' ' * sizeof(tar.chksum); |
Denis Vlasenko | b71c668 | 2007-07-21 15:08:09 +0000 | [diff] [blame] | 180 | for (i = 0; i < 148; i++) { |
Denis Vlasenko | dcbd51d | 2007-03-03 20:06:59 +0000 | [diff] [blame] | 181 | sum_u += ((unsigned char*)&tar)[i]; |
Denis Vlasenko | a80b4a0 | 2007-06-21 12:41:59 +0000 | [diff] [blame] | 182 | #if ENABLE_FEATURE_TAR_OLDSUN_COMPATIBILITY |
Denis Vlasenko | dcbd51d | 2007-03-03 20:06:59 +0000 | [diff] [blame] | 183 | sum_s += ((signed char*)&tar)[i]; |
Denis Vlasenko | a80b4a0 | 2007-06-21 12:41:59 +0000 | [diff] [blame] | 184 | #endif |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 185 | } |
Denis Vlasenko | b71c668 | 2007-07-21 15:08:09 +0000 | [diff] [blame] | 186 | for (i = 156; i < 512; i++) { |
Denis Vlasenko | dcbd51d | 2007-03-03 20:06:59 +0000 | [diff] [blame] | 187 | sum_u += ((unsigned char*)&tar)[i]; |
Denis Vlasenko | a80b4a0 | 2007-06-21 12:41:59 +0000 | [diff] [blame] | 188 | #if ENABLE_FEATURE_TAR_OLDSUN_COMPATIBILITY |
Denis Vlasenko | dcbd51d | 2007-03-03 20:06:59 +0000 | [diff] [blame] | 189 | sum_s += ((signed char*)&tar)[i]; |
Denis Vlasenko | a80b4a0 | 2007-06-21 12:41:59 +0000 | [diff] [blame] | 190 | #endif |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 191 | } |
Denis Vlasenko | a80b4a0 | 2007-06-21 12:41:59 +0000 | [diff] [blame] | 192 | #if ENABLE_FEATURE_TAR_OLDGNU_COMPATIBILITY |
| 193 | sum = strtoul(tar.chksum, &cp, 8); |
| 194 | if ((*cp && *cp != ' ') |
| 195 | || (sum_u != sum USE_FEATURE_TAR_OLDSUN_COMPATIBILITY(&& sum_s != sum)) |
| 196 | ) { |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 197 | bb_error_msg_and_die("invalid tar header checksum"); |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 198 | } |
Denis Vlasenko | a80b4a0 | 2007-06-21 12:41:59 +0000 | [diff] [blame] | 199 | #else |
| 200 | /* This field does not need special treatment (getOctal) */ |
| 201 | sum = xstrtoul(tar.chksum, 8); |
| 202 | if (sum_u != sum USE_FEATURE_TAR_OLDSUN_COMPATIBILITY(&& sum_s != sum)) { |
| 203 | bb_error_msg_and_die("invalid tar header checksum"); |
| 204 | } |
| 205 | #endif |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 206 | |
Denis Vlasenko | b596335 | 2006-11-26 01:46:59 +0000 | [diff] [blame] | 207 | /* 0 is reserved for high perf file, treat as normal file */ |
| 208 | if (!tar.typeflag) tar.typeflag = '0'; |
| 209 | parse_names = (tar.typeflag >= '0' && tar.typeflag <= '7'); |
Denis Vlasenko | d677250 | 2006-11-24 17:21:44 +0000 | [diff] [blame] | 210 | |
Denis Vlasenko | 376ce1e | 2006-11-24 14:51:01 +0000 | [diff] [blame] | 211 | /* getOctal trashes subsequent field, therefore we call it |
| 212 | * on fields in reverse order */ |
Denis Vlasenko | 376ce1e | 2006-11-24 14:51:01 +0000 | [diff] [blame] | 213 | if (tar.devmajor[0]) { |
| 214 | unsigned minor = GET_OCTAL(tar.devminor); |
| 215 | unsigned major = GET_OCTAL(tar.devmajor); |
| 216 | file_header->device = makedev(major, minor); |
Denis Vlasenko | cba9ef5 | 2006-10-10 21:00:47 +0000 | [diff] [blame] | 217 | } |
Denis Vlasenko | 7510384 | 2007-06-20 14:49:47 +0000 | [diff] [blame] | 218 | file_header->link_target = NULL; |
Denis Vlasenko | d677250 | 2006-11-24 17:21:44 +0000 | [diff] [blame] | 219 | if (!linkname && parse_names && tar.linkname[0]) { |
| 220 | /* we trash magic[0] here, it's ok */ |
| 221 | tar.linkname[sizeof(tar.linkname)] = '\0'; |
Denis Vlasenko | 7510384 | 2007-06-20 14:49:47 +0000 | [diff] [blame] | 222 | file_header->link_target = xstrdup(tar.linkname); |
| 223 | /* FIXME: what if we have non-link object with link_target? */ |
| 224 | /* Will link_target be free()ed? */ |
Denis Vlasenko | d677250 | 2006-11-24 17:21:44 +0000 | [diff] [blame] | 225 | } |
Denis Vlasenko | e00e502 | 2008-02-14 20:37:54 +0000 | [diff] [blame] | 226 | #if ENABLE_FEATURE_TAR_UNAME_GNAME |
| 227 | file_header->uname = tar.uname[0] ? xstrndup(tar.uname, sizeof(tar.uname)) : NULL; |
| 228 | file_header->gname = tar.gname[0] ? xstrndup(tar.gname, sizeof(tar.gname)) : NULL; |
| 229 | #endif |
Denis Vlasenko | 376ce1e | 2006-11-24 14:51:01 +0000 | [diff] [blame] | 230 | file_header->mtime = GET_OCTAL(tar.mtime); |
| 231 | file_header->size = GET_OCTAL(tar.size); |
| 232 | file_header->gid = GET_OCTAL(tar.gid); |
| 233 | file_header->uid = GET_OCTAL(tar.uid); |
Glenn L McGrath | 916ba53 | 2004-02-20 02:34:42 +0000 | [diff] [blame] | 234 | /* Set bits 0-11 of the files mode */ |
Denis Vlasenko | 376ce1e | 2006-11-24 14:51:01 +0000 | [diff] [blame] | 235 | file_header->mode = 07777 & GET_OCTAL(tar.mode); |
Glenn L McGrath | 916ba53 | 2004-02-20 02:34:42 +0000 | [diff] [blame] | 236 | |
Denis Vlasenko | b596335 | 2006-11-26 01:46:59 +0000 | [diff] [blame] | 237 | file_header->name = NULL; |
Denis Vlasenko | d677250 | 2006-11-24 17:21:44 +0000 | [diff] [blame] | 238 | if (!longname && parse_names) { |
| 239 | /* we trash mode[0] here, it's ok */ |
Denis Vlasenko | cf30cc8 | 2006-11-24 14:53:18 +0000 | [diff] [blame] | 240 | tar.name[sizeof(tar.name)] = '\0'; |
Denis Vlasenko | 87cd4a8 | 2006-11-25 23:47:32 +0000 | [diff] [blame] | 241 | if (tar.prefix[0]) { |
| 242 | /* and padding[0] */ |
| 243 | tar.prefix[sizeof(tar.prefix)] = '\0'; |
Denis Vlasenko | cf30cc8 | 2006-11-24 14:53:18 +0000 | [diff] [blame] | 244 | file_header->name = concat_path_file(tar.prefix, tar.name); |
Denis Vlasenko | 87cd4a8 | 2006-11-25 23:47:32 +0000 | [diff] [blame] | 245 | } else |
Denis Vlasenko | cf30cc8 | 2006-11-24 14:53:18 +0000 | [diff] [blame] | 246 | file_header->name = xstrdup(tar.name); |
| 247 | } |
| 248 | |
Glenn L McGrath | 916ba53 | 2004-02-20 02:34:42 +0000 | [diff] [blame] | 249 | /* Set bits 12-15 of the files mode */ |
Denis Vlasenko | d677250 | 2006-11-24 17:21:44 +0000 | [diff] [blame] | 250 | /* (typeflag was not trashed because chksum does not use getOctal) */ |
Denis Vlasenko | 376ce1e | 2006-11-24 14:51:01 +0000 | [diff] [blame] | 251 | switch (tar.typeflag) { |
Glenn L McGrath | b0e163a | 2004-02-19 08:48:30 +0000 | [diff] [blame] | 252 | /* busybox identifies hard links as being regular files with 0 size and a link name */ |
| 253 | case '1': |
Glenn L McGrath | 916ba53 | 2004-02-20 02:34:42 +0000 | [diff] [blame] | 254 | file_header->mode |= S_IFREG; |
Glenn L McGrath | b0e163a | 2004-02-19 08:48:30 +0000 | [diff] [blame] | 255 | break; |
Glenn L McGrath | c9f1fce | 2004-02-20 02:25:18 +0000 | [diff] [blame] | 256 | case '7': |
Denis Vlasenko | b596335 | 2006-11-26 01:46:59 +0000 | [diff] [blame] | 257 | /* case 0: */ |
Glenn L McGrath | 21110a0 | 2003-01-28 01:45:48 +0000 | [diff] [blame] | 258 | case '0': |
Denis Vlasenko | d677250 | 2006-11-24 17:21:44 +0000 | [diff] [blame] | 259 | #if ENABLE_FEATURE_TAR_OLDGNU_COMPATIBILITY |
Glenn L McGrath | 87af49f | 2003-09-09 17:41:03 +0000 | [diff] [blame] | 260 | if (last_char_is(file_header->name, '/')) { |
| 261 | file_header->mode |= S_IFDIR; |
Glenn L McGrath | c9f1fce | 2004-02-20 02:25:18 +0000 | [diff] [blame] | 262 | } else |
| 263 | #endif |
Denis Vlasenko | 376ce1e | 2006-11-24 14:51:01 +0000 | [diff] [blame] | 264 | file_header->mode |= S_IFREG; |
Glenn L McGrath | 99b1254 | 2002-08-22 17:47:09 +0000 | [diff] [blame] | 265 | break; |
Glenn L McGrath | 21110a0 | 2003-01-28 01:45:48 +0000 | [diff] [blame] | 266 | case '2': |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 267 | file_header->mode |= S_IFLNK; |
Glenn L McGrath | 99b1254 | 2002-08-22 17:47:09 +0000 | [diff] [blame] | 268 | break; |
Glenn L McGrath | 21110a0 | 2003-01-28 01:45:48 +0000 | [diff] [blame] | 269 | case '3': |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 270 | file_header->mode |= S_IFCHR; |
Glenn L McGrath | 99b1254 | 2002-08-22 17:47:09 +0000 | [diff] [blame] | 271 | break; |
Glenn L McGrath | 21110a0 | 2003-01-28 01:45:48 +0000 | [diff] [blame] | 272 | case '4': |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 273 | file_header->mode |= S_IFBLK; |
Glenn L McGrath | 99b1254 | 2002-08-22 17:47:09 +0000 | [diff] [blame] | 274 | break; |
Glenn L McGrath | 21110a0 | 2003-01-28 01:45:48 +0000 | [diff] [blame] | 275 | case '5': |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 276 | file_header->mode |= S_IFDIR; |
Glenn L McGrath | 99b1254 | 2002-08-22 17:47:09 +0000 | [diff] [blame] | 277 | break; |
Glenn L McGrath | 21110a0 | 2003-01-28 01:45:48 +0000 | [diff] [blame] | 278 | case '6': |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 279 | file_header->mode |= S_IFIFO; |
Glenn L McGrath | 99b1254 | 2002-08-22 17:47:09 +0000 | [diff] [blame] | 280 | break; |
Denis Vlasenko | d677250 | 2006-11-24 17:21:44 +0000 | [diff] [blame] | 281 | #if ENABLE_FEATURE_TAR_GNU_EXTENSIONS |
Denis Vlasenko | 376ce1e | 2006-11-24 14:51:01 +0000 | [diff] [blame] | 282 | case 'L': |
Denis Vlasenko | b596335 | 2006-11-26 01:46:59 +0000 | [diff] [blame] | 283 | /* free: paranoia: tar with several consecutive longnames */ |
Denis Vlasenko | d677250 | 2006-11-24 17:21:44 +0000 | [diff] [blame] | 284 | free(longname); |
Denis Vlasenko | b596335 | 2006-11-26 01:46:59 +0000 | [diff] [blame] | 285 | /* For paranoia reasons we allocate extra NUL char */ |
Denis Vlasenko | 376ce1e | 2006-11-24 14:51:01 +0000 | [diff] [blame] | 286 | longname = xzalloc(file_header->size + 1); |
Denis Vlasenko | b596335 | 2006-11-26 01:46:59 +0000 | [diff] [blame] | 287 | /* We read ASCIZ string, including NUL */ |
Denis Vlasenko | 376ce1e | 2006-11-24 14:51:01 +0000 | [diff] [blame] | 288 | xread(archive_handle->src_fd, longname, file_header->size); |
| 289 | archive_handle->offset += file_header->size; |
Denis Vlasenko | b596335 | 2006-11-26 01:46:59 +0000 | [diff] [blame] | 290 | /* return get_header_tar(archive_handle); */ |
| 291 | /* gcc 4.1.1 didn't optimize it into jump */ |
| 292 | /* so we will do it ourself, this also saves stack */ |
| 293 | goto again; |
Denis Vlasenko | 376ce1e | 2006-11-24 14:51:01 +0000 | [diff] [blame] | 294 | case 'K': |
Denis Vlasenko | d677250 | 2006-11-24 17:21:44 +0000 | [diff] [blame] | 295 | free(linkname); |
Denis Vlasenko | 376ce1e | 2006-11-24 14:51:01 +0000 | [diff] [blame] | 296 | linkname = xzalloc(file_header->size + 1); |
| 297 | xread(archive_handle->src_fd, linkname, file_header->size); |
| 298 | archive_handle->offset += file_header->size; |
Denis Vlasenko | b596335 | 2006-11-26 01:46:59 +0000 | [diff] [blame] | 299 | /* return get_header_tar(archive_handle); */ |
| 300 | goto again; |
Glenn L McGrath | b0e163a | 2004-02-19 08:48:30 +0000 | [diff] [blame] | 301 | case 'D': /* GNU dump dir */ |
Denis Vlasenko | 376ce1e | 2006-11-24 14:51:01 +0000 | [diff] [blame] | 302 | case 'M': /* Continuation of multi volume archive */ |
Glenn L McGrath | b0e163a | 2004-02-19 08:48:30 +0000 | [diff] [blame] | 303 | case 'N': /* Old GNU for names > 100 characters */ |
| 304 | case 'S': /* Sparse file */ |
| 305 | case 'V': /* Volume header */ |
Glenn L McGrath | 6aa5223 | 2004-02-17 11:55:06 +0000 | [diff] [blame] | 306 | #endif |
Rob Landley | ff6e21c | 2006-07-06 20:30:19 +0000 | [diff] [blame] | 307 | case 'g': /* pax global header */ |
Denis Vlasenko | b596335 | 2006-11-26 01:46:59 +0000 | [diff] [blame] | 308 | case 'x': { /* pax extended header */ |
| 309 | off_t sz; |
| 310 | bb_error_msg("warning: skipping header '%c'", tar.typeflag); |
| 311 | sz = (file_header->size + 511) & ~(off_t)511; |
| 312 | archive_handle->offset += sz; |
| 313 | sz >>= 9; /* sz /= 512 but w/o contortions for signed div */ |
| 314 | while (sz--) |
| 315 | xread(archive_handle->src_fd, &tar, 512); |
| 316 | /* return get_header_tar(archive_handle); */ |
| 317 | goto again_after_align; |
| 318 | } |
Glenn L McGrath | b0e163a | 2004-02-19 08:48:30 +0000 | [diff] [blame] | 319 | default: |
Denis Vlasenko | b596335 | 2006-11-26 01:46:59 +0000 | [diff] [blame] | 320 | bb_error_msg_and_die("unknown typeflag: 0x%x", tar.typeflag); |
Glenn L McGrath | 6aa5223 | 2004-02-17 11:55:06 +0000 | [diff] [blame] | 321 | } |
Denis Vlasenko | 376ce1e | 2006-11-24 14:51:01 +0000 | [diff] [blame] | 322 | |
Denis Vlasenko | d677250 | 2006-11-24 17:21:44 +0000 | [diff] [blame] | 323 | #if ENABLE_FEATURE_TAR_GNU_EXTENSIONS |
| 324 | if (longname) { |
| 325 | file_header->name = longname; |
| 326 | longname = NULL; |
| 327 | } |
| 328 | if (linkname) { |
Denis Vlasenko | 7510384 | 2007-06-20 14:49:47 +0000 | [diff] [blame] | 329 | file_header->link_target = linkname; |
Denis Vlasenko | d677250 | 2006-11-24 17:21:44 +0000 | [diff] [blame] | 330 | linkname = NULL; |
| 331 | } |
| 332 | #endif |
Denis Vlasenko | c1660fe | 2006-11-26 15:42:03 +0000 | [diff] [blame] | 333 | if (!strncmp(file_header->name, "/../"+1, 3) |
| 334 | || strstr(file_header->name, "/../") |
| 335 | ) { |
| 336 | bb_error_msg_and_die("name with '..' encountered: '%s'", |
| 337 | file_header->name); |
| 338 | } |
Denis Vlasenko | d677250 | 2006-11-24 17:21:44 +0000 | [diff] [blame] | 339 | |
Denis Vlasenko | 376ce1e | 2006-11-24 14:51:01 +0000 | [diff] [blame] | 340 | /* Strip trailing '/' in directories */ |
Denis Vlasenko | 714701c | 2006-12-22 00:21:07 +0000 | [diff] [blame] | 341 | /* 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] | 342 | cp = last_char_is(file_header->name, '/'); |
Glenn L McGrath | 3d5828f | 2003-08-14 02:55:15 +0000 | [diff] [blame] | 343 | |
Glenn L McGrath | 8e94098 | 2002-11-04 23:47:31 +0000 | [diff] [blame] | 344 | if (archive_handle->filter(archive_handle) == EXIT_SUCCESS) { |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 345 | archive_handle->action_header(archive_handle->file_header); |
Denis Vlasenko | cf30cc8 | 2006-11-24 14:53:18 +0000 | [diff] [blame] | 346 | /* Note that we kill the '/' only after action_header() */ |
| 347 | /* (like GNU tar 1.15.1: verbose mode outputs "dir/dir/") */ |
| 348 | if (cp) *cp = '\0'; |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 349 | archive_handle->flags |= ARCHIVE_EXTRACT_QUIET; |
| 350 | archive_handle->action_data(archive_handle); |
Rob Landley | 8bb5078 | 2006-05-26 23:44:51 +0000 | [diff] [blame] | 351 | llist_add_to(&(archive_handle->passed), file_header->name); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 352 | } else { |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 353 | data_skip(archive_handle); |
Denis Vlasenko | b596335 | 2006-11-26 01:46:59 +0000 | [diff] [blame] | 354 | free(file_header->name); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 355 | } |
| 356 | archive_handle->offset += file_header->size; |
| 357 | |
Denis Vlasenko | 7510384 | 2007-06-20 14:49:47 +0000 | [diff] [blame] | 358 | free(file_header->link_target); |
Denis Vlasenko | b596335 | 2006-11-26 01:46:59 +0000 | [diff] [blame] | 359 | /* Do not free(file_header->name)! */ |
Denis Vlasenko | e00e502 | 2008-02-14 20:37:54 +0000 | [diff] [blame] | 360 | #if ENABLE_FEATURE_TAR_UNAME_GNAME |
| 361 | free(file_header->uname); |
| 362 | free(file_header->gname); |
| 363 | #endif |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 364 | return EXIT_SUCCESS; |
Eric Andersen | 2276d83 | 2002-07-11 11:11:56 +0000 | [diff] [blame] | 365 | } |