blob: d5b86ff5c4704720d4217ed98bb668668eba2b5a [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Rob Landley06249fe2006-02-20 19:28:53 +00002/* Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Glenn L McGrath6aa52232004-02-17 11:55:06 +00003 *
Glenn L McGrathc9f1fce2004-02-20 02:25:18 +00004 * FIXME:
Eric Andersenaff114c2004-04-14 17:51:38 +00005 * In privileged mode if uname and gname map to a uid and gid then use the
Glenn L McGrathc9f1fce2004-02-20 02:25:18 +00006 * mapped value instead of the uid/gid values in tar header
Glenn L McGrathb0e163a2004-02-19 08:48:30 +00007 *
8 * References:
9 * GNU tar and star man pages,
10 * Opengroup's ustar interchange format,
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000011 * http://www.opengroup.org/onlinepubs/007904975/utilities/pax.html
Glenn L McGrath95ebf612001-10-25 14:18:08 +000012 */
13
Glenn L McGrath95ebf612001-10-25 14:18:08 +000014#include "libbb.h"
Rob Landleyd921b2e2006-08-03 15:41:12 +000015#include "unarchive.h"
Glenn L McGrath95ebf612001-10-25 14:18:08 +000016
Denys Vlasenko98a4c7c2010-02-04 15:00:15 +010017typedef uint32_t aliased_uint32_t FIX_ALIASING;
18typedef off_t aliased_off_t FIX_ALIASING;
19
20
Denis Vlasenkoab6b4462008-12-12 02:14:35 +000021/*
22 * GNU tar uses "base-256 encoding" for very large numbers (>8 billion).
23 * Encoding is binary, with highest bit always set as a marker
24 * and sign in next-highest bit:
25 * 80 00 .. 00 - zero
26 * bf ff .. ff - largest positive number
27 * ff ff .. ff - minus 1
28 * c0 00 .. 00 - smallest negative number
29 *
30 * We expect it only in size field, where negative numbers don't make sense.
31 */
32static off_t getBase256_len12(const char *str)
33{
34 off_t value;
35 int len;
36
37 /* if (*str & 0x40) error; - caller prevents this */
38
39 if (sizeof(off_t) >= 12) {
40 /* Probably 128-bit (16 byte) off_t. Can be optimized. */
41 len = 12;
42 value = *str++ & 0x3f;
43 while (--len)
44 value = (value << 8) + (unsigned char) *str++;
45 return value;
46 }
47
48#ifdef CHECK_FOR_OVERFLOW
49 /* Can be optimized to eat 32-bit chunks */
50 char c = *str++ & 0x3f;
51 len = 12;
52 while (1) {
53 if (c)
54 bb_error_msg_and_die("overflow in base-256 encoded file size");
Denis Vlasenko3266aa92009-04-01 11:24:04 +000055 if (--len == sizeof(off_t))
Denis Vlasenkoab6b4462008-12-12 02:14:35 +000056 break;
57 c = *str++;
58 }
59#else
60 str += (12 - sizeof(off_t));
61#endif
62
63/* Now str points to sizeof(off_t) least significant bytes.
64 *
65 * Example of tar file with 8914993153 (0x213600001) byte file.
66 * Field starts at offset 7c:
67 * 00070 30 30 30 00 30 30 30 30 30 30 30 00 80 00 00 00 |000.0000000.....|
68 * 00080 00 00 00 02 13 60 00 01 31 31 31 32 30 33 33 36 |.....`..11120336|
69 *
70 * str is at offset 80 or 84 now (64-bit or 32-bit off_t).
71 * We (ab)use the fact that value happens to be aligned,
72 * and fetch it in one go:
73 */
74 if (sizeof(off_t) == 8) {
Denys Vlasenko98a4c7c2010-02-04 15:00:15 +010075 value = *(aliased_off_t*)str;
Denis Vlasenkoab6b4462008-12-12 02:14:35 +000076 value = SWAP_BE64(value);
77 } else if (sizeof(off_t) == 4) {
Denys Vlasenko98a4c7c2010-02-04 15:00:15 +010078 value = *(aliased_off_t*)str;
Denis Vlasenkoab6b4462008-12-12 02:14:35 +000079 value = SWAP_BE32(value);
80 } else {
81 value = 0;
82 len = sizeof(off_t);
83 while (--len)
84 value = (value << 8) + (unsigned char) *str++;
85 }
86 return value;
87}
88
Denis Vlasenko376ce1e2006-11-24 14:51:01 +000089/* NB: _DESTROYS_ str[len] character! */
90static unsigned long long getOctal(char *str, int len)
91{
92 unsigned long long v;
Denis Vlasenkoa60936d2008-06-28 05:04:09 +000093 /* NB: leading spaces are allowed. Using strtoull to handle that.
Denys Vlasenko8d338172009-09-23 17:16:37 +020094 * The downside is that we accept e.g. "-123" too :(
Denis Vlasenkoa60936d2008-06-28 05:04:09 +000095 */
Denis Vlasenko376ce1e2006-11-24 14:51:01 +000096 str[len] = '\0';
97 v = strtoull(str, &str, 8);
Denys Vlasenko8d338172009-09-23 17:16:37 +020098 /* std: "Each numeric field is terminated by one or more
99 * <space> or NUL characters". We must support ' '! */
100 if (*str != '\0' && *str != ' ')
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000101 bb_error_msg_and_die("corrupted octal value in tar header");
102 return v;
103}
Denis Vlasenkob5963352006-11-26 01:46:59 +0000104#define GET_OCTAL(a) getOctal((a), sizeof(a))
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000105
106void BUG_tar_header_size(void);
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000107char FAST_FUNC get_header_tar(archive_handle_t *archive_handle)
Glenn L McGrath95ebf612001-10-25 14:18:08 +0000108{
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000109 file_header_t *file_header = archive_handle->file_header;
110 struct {
111 /* ustar header, Posix 1003.1 */
112 char name[100]; /* 0-99 */
113 char mode[8]; /* 100-107 */
114 char uid[8]; /* 108-115 */
115 char gid[8]; /* 116-123 */
116 char size[12]; /* 124-135 */
117 char mtime[12]; /* 136-147 */
118 char chksum[8]; /* 148-155 */
119 char typeflag; /* 156-156 */
120 char linkname[100]; /* 157-256 */
Denis Vlasenko85128622007-11-16 20:35:30 +0000121 /* POSIX: "ustar" NUL "00" */
122 /* GNU tar: "ustar " NUL */
Paul Fox16aec392007-11-17 19:11:05 +0000123 /* Normally it's defined as magic[6] followed by
Denis Vlasenkoa60936d2008-06-28 05:04:09 +0000124 * version[2], but we put them together to simplify code
Paul Fox16aec392007-11-17 19:11:05 +0000125 */
Denis Vlasenko85128622007-11-16 20:35:30 +0000126 char magic[8]; /* 257-264 */
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000127 char uname[32]; /* 265-296 */
128 char gname[32]; /* 297-328 */
129 char devmajor[8]; /* 329-336 */
130 char devminor[8]; /* 337-344 */
131 char prefix[155]; /* 345-499 */
132 char padding[12]; /* 500-512 */
133 } tar;
134 char *cp;
Denis Vlasenkoa80b4a02007-06-21 12:41:59 +0000135 int i, sum_u, sum;
136#if ENABLE_FEATURE_TAR_OLDSUN_COMPATIBILITY
137 int sum_s;
138#endif
Denis Vlasenkod6772502006-11-24 17:21:44 +0000139 int parse_names;
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000140
Denis Vlasenkoa60936d2008-06-28 05:04:09 +0000141 /* Our "private data" */
Denis Vlasenkoa60936d2008-06-28 05:04:09 +0000142#if ENABLE_FEATURE_TAR_GNU_EXTENSIONS
Denys Vlasenkoaa4977d2010-01-06 10:53:17 +0100143# define p_longname (archive_handle->tar__longname)
144# define p_linkname (archive_handle->tar__linkname)
Denis Vlasenkoa60936d2008-06-28 05:04:09 +0000145#else
Denys Vlasenkoaa4977d2010-01-06 10:53:17 +0100146# define p_longname 0
147# define p_linkname 0
Denis Vlasenkoa60936d2008-06-28 05:04:09 +0000148#endif
Denis Vlasenkoa60936d2008-06-28 05:04:09 +0000149
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000150 if (sizeof(tar) != 512)
151 BUG_tar_header_size();
152
Denis Vlasenko666da5e2006-12-26 18:17:42 +0000153#if ENABLE_FEATURE_TAR_GNU_EXTENSIONS
154 again:
155#endif
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000156 /* Align header */
Glenn L McGrath237ae422002-11-03 14:05:15 +0000157 data_align(archive_handle, 512);
Glenn L McGrath95ebf612001-10-25 14:18:08 +0000158
Denis Vlasenkob5963352006-11-26 01:46:59 +0000159 again_after_align:
160
Denis Vlasenko1f0b95f2009-03-13 14:26:44 +0000161#if ENABLE_DESKTOP || ENABLE_FEATURE_TAR_AUTODETECT
Denis Vlasenko0381d422008-07-10 23:06:00 +0000162 /* to prevent misdetection of bz2 sig */
Denys Vlasenko98a4c7c2010-02-04 15:00:15 +0100163 *(aliased_uint32_t*)&tar = 0;
Denis Vlasenko23ffb6a2008-02-13 17:52:42 +0000164 i = full_read(archive_handle->src_fd, &tar, 512);
Denis Vlasenkoa60936d2008-06-28 05:04:09 +0000165 /* If GNU tar sees EOF in above read, it says:
Denis Vlasenko23ffb6a2008-02-13 17:52:42 +0000166 * "tar: A lone zero block at N", where N = kilobyte
167 * where EOF was met (not EOF block, actual EOF!),
Denis Vlasenko0381d422008-07-10 23:06:00 +0000168 * and exits with EXIT_SUCCESS.
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000169 * We will mimic exit(EXIT_SUCCESS), although we will not mimic
Denis Vlasenko12c06222008-02-14 08:52:30 +0000170 * the message and we don't check whether we indeed
171 * saw zero block directly before this. */
Denis Vlasenko0381d422008-07-10 23:06:00 +0000172 if (i == 0) {
Denis Vlasenko23ffb6a2008-02-13 17:52:42 +0000173 xfunc_error_retval = 0;
Denis Vlasenko0381d422008-07-10 23:06:00 +0000174 short_read:
Denis Vlasenko23ffb6a2008-02-13 17:52:42 +0000175 bb_error_msg_and_die("short read");
Denis Vlasenko0381d422008-07-10 23:06:00 +0000176 }
177 if (i != 512) {
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000178 IF_FEATURE_TAR_AUTODETECT(goto autodetect;)
Denis Vlasenko0381d422008-07-10 23:06:00 +0000179 goto short_read;
180 }
181
Denis Vlasenko12c06222008-02-14 08:52:30 +0000182#else
Denis Vlasenko0381d422008-07-10 23:06:00 +0000183 i = 512;
184 xread(archive_handle->src_fd, &tar, i);
Denis Vlasenko12c06222008-02-14 08:52:30 +0000185#endif
Denis Vlasenko0381d422008-07-10 23:06:00 +0000186 archive_handle->offset += i;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000187
188 /* If there is no filename its an empty header */
Denis Vlasenko05efca92008-04-29 04:12:58 +0000189 if (tar.name[0] == 0 && tar.prefix[0] == 0) {
Denys Vlasenkoaa4977d2010-01-06 10:53:17 +0100190 if (archive_handle->tar__end) {
Denis Vlasenkoa60936d2008-06-28 05:04:09 +0000191 /* Second consecutive empty header - end of archive.
Paul Fox94ff9f12005-07-20 19:24:13 +0000192 * Read until the end to empty the pipe from gz or bz2
193 */
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000194 while (full_read(archive_handle->src_fd, &tar, 512) == 512)
Denis Vlasenko431a7c92008-02-19 11:26:28 +0000195 continue;
Denis Vlasenko13858992006-10-08 12:49:22 +0000196 return EXIT_FAILURE;
Paul Fox94ff9f12005-07-20 19:24:13 +0000197 }
Denys Vlasenkoaa4977d2010-01-06 10:53:17 +0100198 archive_handle->tar__end = 1;
Denis Vlasenko13858992006-10-08 12:49:22 +0000199 return EXIT_SUCCESS;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000200 }
Denys Vlasenkoaa4977d2010-01-06 10:53:17 +0100201 archive_handle->tar__end = 0;
Glenn L McGrath95ebf612001-10-25 14:18:08 +0000202
Denis Vlasenko431a7c92008-02-19 11:26:28 +0000203 /* Check header has valid magic, "ustar" is for the proper tar,
204 * five NULs are for the old tar format */
205 if (strncmp(tar.magic, "ustar", 5) != 0
206 && (!ENABLE_FEATURE_TAR_OLDGNU_COMPATIBILITY
207 || memcmp(tar.magic, "\0\0\0\0", 5) != 0)
208 ) {
209#if ENABLE_FEATURE_TAR_AUTODETECT
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000210 char FAST_FUNC (*get_header_ptr)(archive_handle_t *);
Denis Vlasenko431a7c92008-02-19 11:26:28 +0000211
Denis Vlasenko1f0b95f2009-03-13 14:26:44 +0000212 autodetect:
Denis Vlasenko431a7c92008-02-19 11:26:28 +0000213 /* tar gz/bz autodetect: check for gz/bz2 magic.
Denis Vlasenkoa60936d2008-06-28 05:04:09 +0000214 * If we see the magic, and it is the very first block,
Denis Vlasenko431a7c92008-02-19 11:26:28 +0000215 * we can switch to get_header_tar_gz/bz2/lzma().
Denis Vlasenko77ad97f2008-05-13 02:27:31 +0000216 * Needs seekable fd. I wish recv(MSG_PEEK) works
Denis Vlasenko431a7c92008-02-19 11:26:28 +0000217 * on any fd... */
Denis Vlasenkoe9ad84d2008-08-05 13:10:34 +0000218#if ENABLE_FEATURE_SEAMLESS_GZ
Denis Vlasenko77ad97f2008-05-13 02:27:31 +0000219 if (tar.name[0] == 0x1f && tar.name[1] == (char)0x8b) { /* gzip */
Denis Vlasenko431a7c92008-02-19 11:26:28 +0000220 get_header_ptr = get_header_tar_gz;
221 } else
Glenn L McGrath1d23f3a2002-08-13 05:06:43 +0000222#endif
Denis Vlasenkoe9ad84d2008-08-05 13:10:34 +0000223#if ENABLE_FEATURE_SEAMLESS_BZ2
Denis Vlasenko431a7c92008-02-19 11:26:28 +0000224 if (tar.name[0] == 'B' && tar.name[1] == 'Z'
225 && tar.name[2] == 'h' && isdigit(tar.name[3])
226 ) { /* bzip2 */
227 get_header_ptr = get_header_tar_bz2;
228 } else
229#endif
230 goto err;
Denis Vlasenkoa60936d2008-06-28 05:04:09 +0000231 /* Two different causes for lseek() != 0:
232 * unseekable fd (would like to support that too, but...),
233 * or not first block (false positive, it's not .gz/.bz2!) */
Denis Vlasenko0381d422008-07-10 23:06:00 +0000234 if (lseek(archive_handle->src_fd, -i, SEEK_CUR) != 0)
Denis Vlasenko431a7c92008-02-19 11:26:28 +0000235 goto err;
236 while (get_header_ptr(archive_handle) == EXIT_SUCCESS)
237 continue;
238 return EXIT_FAILURE;
239 err:
240#endif /* FEATURE_TAR_AUTODETECT */
241 bb_error_msg_and_die("invalid tar magic");
Glenn L McGrath95ebf612001-10-25 14:18:08 +0000242 }
Denis Vlasenkodcbd51d2007-03-03 20:06:59 +0000243
244 /* Do checksum on headers.
245 * POSIX says that checksum is done on unsigned bytes, but
Denis Vlasenko940494f2007-03-04 18:09:50 +0000246 * Sun and HP-UX gets it wrong... more details in
Denis Vlasenkodcbd51d2007-03-03 20:06:59 +0000247 * GNU tar source. */
Denis Vlasenkoa80b4a02007-06-21 12:41:59 +0000248#if ENABLE_FEATURE_TAR_OLDSUN_COMPATIBILITY
249 sum_s = ' ' * sizeof(tar.chksum);
250#endif
251 sum_u = ' ' * sizeof(tar.chksum);
Denis Vlasenkob71c6682007-07-21 15:08:09 +0000252 for (i = 0; i < 148; i++) {
Denis Vlasenkodcbd51d2007-03-03 20:06:59 +0000253 sum_u += ((unsigned char*)&tar)[i];
Denis Vlasenkoa80b4a02007-06-21 12:41:59 +0000254#if ENABLE_FEATURE_TAR_OLDSUN_COMPATIBILITY
Denis Vlasenkodcbd51d2007-03-03 20:06:59 +0000255 sum_s += ((signed char*)&tar)[i];
Denis Vlasenkoa80b4a02007-06-21 12:41:59 +0000256#endif
Glenn L McGrath95ebf612001-10-25 14:18:08 +0000257 }
Denis Vlasenkob71c6682007-07-21 15:08:09 +0000258 for (i = 156; i < 512; i++) {
Denis Vlasenkodcbd51d2007-03-03 20:06:59 +0000259 sum_u += ((unsigned char*)&tar)[i];
Denis Vlasenkoa80b4a02007-06-21 12:41:59 +0000260#if ENABLE_FEATURE_TAR_OLDSUN_COMPATIBILITY
Denis Vlasenkodcbd51d2007-03-03 20:06:59 +0000261 sum_s += ((signed char*)&tar)[i];
Denis Vlasenkoa80b4a02007-06-21 12:41:59 +0000262#endif
Glenn L McGrath95ebf612001-10-25 14:18:08 +0000263 }
Denis Vlasenkoa80b4a02007-06-21 12:41:59 +0000264 /* This field does not need special treatment (getOctal) */
Denys Vlasenko8d338172009-09-23 17:16:37 +0200265 {
266 char *endp; /* gcc likes temp var for &endp */
267 sum = strtoul(tar.chksum, &endp, 8);
268 if ((*endp != '\0' && *endp != ' ')
269 || (sum_u != sum IF_FEATURE_TAR_OLDSUN_COMPATIBILITY(&& sum_s != sum))
270 ) {
271 bb_error_msg_and_die("invalid tar header checksum");
272 }
273 }
Denys Vlasenkod6459682010-01-03 23:41:11 +0100274 /* don't use xstrtoul, tar.chksum may have leading spaces */
275 sum = strtoul(tar.chksum, NULL, 8);
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000276 if (sum_u != sum IF_FEATURE_TAR_OLDSUN_COMPATIBILITY(&& sum_s != sum)) {
Denis Vlasenkoa80b4a02007-06-21 12:41:59 +0000277 bb_error_msg_and_die("invalid tar header checksum");
278 }
Glenn L McGrath95ebf612001-10-25 14:18:08 +0000279
Denis Vlasenkob5963352006-11-26 01:46:59 +0000280 /* 0 is reserved for high perf file, treat as normal file */
281 if (!tar.typeflag) tar.typeflag = '0';
282 parse_names = (tar.typeflag >= '0' && tar.typeflag <= '7');
Denis Vlasenkod6772502006-11-24 17:21:44 +0000283
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000284 /* getOctal trashes subsequent field, therefore we call it
285 * on fields in reverse order */
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000286 if (tar.devmajor[0]) {
Denis Vlasenkod93400b2008-04-29 03:54:16 +0000287 char t = tar.prefix[0];
288 /* we trash prefix[0] here, but we DO need it later! */
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000289 unsigned minor = GET_OCTAL(tar.devminor);
290 unsigned major = GET_OCTAL(tar.devmajor);
291 file_header->device = makedev(major, minor);
Denis Vlasenkod93400b2008-04-29 03:54:16 +0000292 tar.prefix[0] = t;
Denis Vlasenkocba9ef52006-10-10 21:00:47 +0000293 }
Denis Vlasenko75103842007-06-20 14:49:47 +0000294 file_header->link_target = NULL;
Denis Vlasenkoa60936d2008-06-28 05:04:09 +0000295 if (!p_linkname && parse_names && tar.linkname[0]) {
Denis Vlasenkobc1918a2008-04-15 01:17:50 +0000296 file_header->link_target = xstrndup(tar.linkname, sizeof(tar.linkname));
Denis Vlasenko75103842007-06-20 14:49:47 +0000297 /* FIXME: what if we have non-link object with link_target? */
298 /* Will link_target be free()ed? */
Denis Vlasenkod6772502006-11-24 17:21:44 +0000299 }
Denis Vlasenkoe00e5022008-02-14 20:37:54 +0000300#if ENABLE_FEATURE_TAR_UNAME_GNAME
Denys Vlasenkoaa4977d2010-01-06 10:53:17 +0100301 file_header->tar__uname = tar.uname[0] ? xstrndup(tar.uname, sizeof(tar.uname)) : NULL;
302 file_header->tar__gname = tar.gname[0] ? xstrndup(tar.gname, sizeof(tar.gname)) : NULL;
Denis Vlasenkoe00e5022008-02-14 20:37:54 +0000303#endif
Denys Vlasenkob773f712009-07-08 18:58:09 +0200304 /* mtime: rudimentally handle GNU tar's "base256 encoding"
305 * People report tarballs with NEGATIVE unix times encoded that way */
306 file_header->mtime = (tar.mtime[0] & 0x80) /* base256? */
307 ? 0 /* bogus */
308 : GET_OCTAL(tar.mtime);
309 /* size: handle GNU tar's "base256 encoding" */
310 file_header->size = (tar.size[0] & 0xc0) == 0x80 /* positive base256? */
Denis Vlasenkoab6b4462008-12-12 02:14:35 +0000311 ? getBase256_len12(tar.size)
312 : GET_OCTAL(tar.size);
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000313 file_header->gid = GET_OCTAL(tar.gid);
314 file_header->uid = GET_OCTAL(tar.uid);
Glenn L McGrath916ba532004-02-20 02:34:42 +0000315 /* Set bits 0-11 of the files mode */
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000316 file_header->mode = 07777 & GET_OCTAL(tar.mode);
Glenn L McGrath916ba532004-02-20 02:34:42 +0000317
Denis Vlasenkob5963352006-11-26 01:46:59 +0000318 file_header->name = NULL;
Denis Vlasenkoa60936d2008-06-28 05:04:09 +0000319 if (!p_longname && parse_names) {
Denis Vlasenkod6772502006-11-24 17:21:44 +0000320 /* we trash mode[0] here, it's ok */
Denis Vlasenkobc1918a2008-04-15 01:17:50 +0000321 //tar.name[sizeof(tar.name)] = '\0'; - gcc 4.3.0 would complain
322 tar.mode[0] = '\0';
Denis Vlasenko87cd4a82006-11-25 23:47:32 +0000323 if (tar.prefix[0]) {
324 /* and padding[0] */
Denis Vlasenkobc1918a2008-04-15 01:17:50 +0000325 //tar.prefix[sizeof(tar.prefix)] = '\0'; - gcc 4.3.0 would complain
326 tar.padding[0] = '\0';
Denis Vlasenkocf30cc82006-11-24 14:53:18 +0000327 file_header->name = concat_path_file(tar.prefix, tar.name);
Denis Vlasenko87cd4a82006-11-25 23:47:32 +0000328 } else
Denis Vlasenkocf30cc82006-11-24 14:53:18 +0000329 file_header->name = xstrdup(tar.name);
330 }
331
Glenn L McGrath916ba532004-02-20 02:34:42 +0000332 /* Set bits 12-15 of the files mode */
Denis Vlasenkod6772502006-11-24 17:21:44 +0000333 /* (typeflag was not trashed because chksum does not use getOctal) */
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000334 switch (tar.typeflag) {
Glenn L McGrathb0e163a2004-02-19 08:48:30 +0000335 /* busybox identifies hard links as being regular files with 0 size and a link name */
336 case '1':
Glenn L McGrath916ba532004-02-20 02:34:42 +0000337 file_header->mode |= S_IFREG;
Glenn L McGrathb0e163a2004-02-19 08:48:30 +0000338 break;
Glenn L McGrathc9f1fce2004-02-20 02:25:18 +0000339 case '7':
Denis Vlasenkob5963352006-11-26 01:46:59 +0000340 /* case 0: */
Glenn L McGrath21110a02003-01-28 01:45:48 +0000341 case '0':
Denis Vlasenkod6772502006-11-24 17:21:44 +0000342#if ENABLE_FEATURE_TAR_OLDGNU_COMPATIBILITY
Glenn L McGrath87af49f2003-09-09 17:41:03 +0000343 if (last_char_is(file_header->name, '/')) {
Denis Vlasenkoadc772a2008-07-20 17:10:43 +0000344 goto set_dir;
345 }
Glenn L McGrathc9f1fce2004-02-20 02:25:18 +0000346#endif
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000347 file_header->mode |= S_IFREG;
Glenn L McGrath99b12542002-08-22 17:47:09 +0000348 break;
Glenn L McGrath21110a02003-01-28 01:45:48 +0000349 case '2':
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000350 file_header->mode |= S_IFLNK;
Denis Vlasenkoadc772a2008-07-20 17:10:43 +0000351 /* have seen tarballs with size field containing
352 * the size of the link target's name */
353 size0:
354 file_header->size = 0;
Glenn L McGrath99b12542002-08-22 17:47:09 +0000355 break;
Glenn L McGrath21110a02003-01-28 01:45:48 +0000356 case '3':
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000357 file_header->mode |= S_IFCHR;
Denis Vlasenkoadc772a2008-07-20 17:10:43 +0000358 goto size0; /* paranoia */
Glenn L McGrath21110a02003-01-28 01:45:48 +0000359 case '4':
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000360 file_header->mode |= S_IFBLK;
Denis Vlasenkoadc772a2008-07-20 17:10:43 +0000361 goto size0;
Glenn L McGrath21110a02003-01-28 01:45:48 +0000362 case '5':
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000363 IF_FEATURE_TAR_OLDGNU_COMPATIBILITY(set_dir:)
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000364 file_header->mode |= S_IFDIR;
Denis Vlasenkoadc772a2008-07-20 17:10:43 +0000365 goto size0;
Glenn L McGrath21110a02003-01-28 01:45:48 +0000366 case '6':
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000367 file_header->mode |= S_IFIFO;
Denis Vlasenkoadc772a2008-07-20 17:10:43 +0000368 goto size0;
Denis Vlasenkod6772502006-11-24 17:21:44 +0000369#if ENABLE_FEATURE_TAR_GNU_EXTENSIONS
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000370 case 'L':
Denis Vlasenkob5963352006-11-26 01:46:59 +0000371 /* free: paranoia: tar with several consecutive longnames */
Denis Vlasenkoa60936d2008-06-28 05:04:09 +0000372 free(p_longname);
Denis Vlasenkob5963352006-11-26 01:46:59 +0000373 /* For paranoia reasons we allocate extra NUL char */
Denis Vlasenkoa60936d2008-06-28 05:04:09 +0000374 p_longname = xzalloc(file_header->size + 1);
Denis Vlasenkob5963352006-11-26 01:46:59 +0000375 /* We read ASCIZ string, including NUL */
Denis Vlasenkoa60936d2008-06-28 05:04:09 +0000376 xread(archive_handle->src_fd, p_longname, file_header->size);
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000377 archive_handle->offset += file_header->size;
Denis Vlasenkob5963352006-11-26 01:46:59 +0000378 /* return get_header_tar(archive_handle); */
379 /* gcc 4.1.1 didn't optimize it into jump */
380 /* so we will do it ourself, this also saves stack */
381 goto again;
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000382 case 'K':
Denis Vlasenkoa60936d2008-06-28 05:04:09 +0000383 free(p_linkname);
384 p_linkname = xzalloc(file_header->size + 1);
385 xread(archive_handle->src_fd, p_linkname, file_header->size);
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000386 archive_handle->offset += file_header->size;
Denis Vlasenkob5963352006-11-26 01:46:59 +0000387 /* return get_header_tar(archive_handle); */
388 goto again;
Glenn L McGrathb0e163a2004-02-19 08:48:30 +0000389 case 'D': /* GNU dump dir */
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000390 case 'M': /* Continuation of multi volume archive */
Glenn L McGrathb0e163a2004-02-19 08:48:30 +0000391 case 'N': /* Old GNU for names > 100 characters */
392 case 'S': /* Sparse file */
393 case 'V': /* Volume header */
Glenn L McGrath6aa52232004-02-17 11:55:06 +0000394#endif
Rob Landleyff6e21c2006-07-06 20:30:19 +0000395 case 'g': /* pax global header */
Denis Vlasenkob5963352006-11-26 01:46:59 +0000396 case 'x': { /* pax extended header */
397 off_t sz;
398 bb_error_msg("warning: skipping header '%c'", tar.typeflag);
399 sz = (file_header->size + 511) & ~(off_t)511;
400 archive_handle->offset += sz;
401 sz >>= 9; /* sz /= 512 but w/o contortions for signed div */
402 while (sz--)
403 xread(archive_handle->src_fd, &tar, 512);
404 /* return get_header_tar(archive_handle); */
405 goto again_after_align;
406 }
Glenn L McGrathb0e163a2004-02-19 08:48:30 +0000407 default:
Denis Vlasenkob5963352006-11-26 01:46:59 +0000408 bb_error_msg_and_die("unknown typeflag: 0x%x", tar.typeflag);
Glenn L McGrath6aa52232004-02-17 11:55:06 +0000409 }
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000410
Denis Vlasenkod6772502006-11-24 17:21:44 +0000411#if ENABLE_FEATURE_TAR_GNU_EXTENSIONS
Denis Vlasenkoa60936d2008-06-28 05:04:09 +0000412 if (p_longname) {
413 file_header->name = p_longname;
414 p_longname = NULL;
Denis Vlasenkod6772502006-11-24 17:21:44 +0000415 }
Denis Vlasenkoa60936d2008-06-28 05:04:09 +0000416 if (p_linkname) {
417 file_header->link_target = p_linkname;
418 p_linkname = NULL;
Denis Vlasenkod6772502006-11-24 17:21:44 +0000419 }
420#endif
Denis Vlasenko0381d422008-07-10 23:06:00 +0000421 if (strncmp(file_header->name, "/../"+1, 3) == 0
Denis Vlasenkoc1660fe2006-11-26 15:42:03 +0000422 || strstr(file_header->name, "/../")
423 ) {
424 bb_error_msg_and_die("name with '..' encountered: '%s'",
425 file_header->name);
426 }
Denis Vlasenkod6772502006-11-24 17:21:44 +0000427
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000428 /* Strip trailing '/' in directories */
Denis Vlasenko714701c2006-12-22 00:21:07 +0000429 /* Must be done after mode is set as '/' is used to check if it's a directory */
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000430 cp = last_char_is(file_header->name, '/');
Glenn L McGrath3d5828f2003-08-14 02:55:15 +0000431
Glenn L McGrath8e940982002-11-04 23:47:31 +0000432 if (archive_handle->filter(archive_handle) == EXIT_SUCCESS) {
Denis Vlasenko0381d422008-07-10 23:06:00 +0000433 archive_handle->action_header(/*archive_handle->*/ file_header);
Denis Vlasenkocf30cc82006-11-24 14:53:18 +0000434 /* Note that we kill the '/' only after action_header() */
435 /* (like GNU tar 1.15.1: verbose mode outputs "dir/dir/") */
436 if (cp) *cp = '\0';
Denis Vlasenkoa60936d2008-06-28 05:04:09 +0000437 archive_handle->ah_flags |= ARCHIVE_EXTRACT_QUIET;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000438 archive_handle->action_data(archive_handle);
Rob Landley8bb50782006-05-26 23:44:51 +0000439 llist_add_to(&(archive_handle->passed), file_header->name);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000440 } else {
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000441 data_skip(archive_handle);
Denis Vlasenkob5963352006-11-26 01:46:59 +0000442 free(file_header->name);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000443 }
444 archive_handle->offset += file_header->size;
445
Denis Vlasenko75103842007-06-20 14:49:47 +0000446 free(file_header->link_target);
Denis Vlasenkoa46dd892008-07-12 09:20:44 +0000447 /* Do not free(file_header->name)! (why?) */
Denis Vlasenkoe00e5022008-02-14 20:37:54 +0000448#if ENABLE_FEATURE_TAR_UNAME_GNAME
Denys Vlasenkoaa4977d2010-01-06 10:53:17 +0100449 free(file_header->tar__uname);
450 free(file_header->tar__gname);
Denis Vlasenkoe00e5022008-02-14 20:37:54 +0000451#endif
Denis Vlasenko13858992006-10-08 12:49:22 +0000452 return EXIT_SUCCESS;
Eric Andersen2276d832002-07-11 11:11:56 +0000453}