blob: bf0f92b258d1c547ada79a23e0798ee515e94fef [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
Denis Vlasenko376ce1e2006-11-24 14:51:01 +000017/* NB: _DESTROYS_ str[len] character! */
18static unsigned long long getOctal(char *str, int len)
19{
20 unsigned long long v;
Denis Vlasenkoa60936d2008-06-28 05:04:09 +000021 /* NB: leading spaces are allowed. Using strtoull to handle that.
22 * The downside is that we accept e.g. "-123" too :)
23 */
Denis Vlasenko376ce1e2006-11-24 14:51:01 +000024 str[len] = '\0';
25 v = strtoull(str, &str, 8);
Denis Vlasenkoa80b4a02007-06-21 12:41:59 +000026 if (*str && (!ENABLE_FEATURE_TAR_OLDGNU_COMPATIBILITY || *str != ' '))
Denis Vlasenko376ce1e2006-11-24 14:51:01 +000027 bb_error_msg_and_die("corrupted octal value in tar header");
28 return v;
29}
Denis Vlasenkob5963352006-11-26 01:46:59 +000030#define GET_OCTAL(a) getOctal((a), sizeof(a))
Denis Vlasenko376ce1e2006-11-24 14:51:01 +000031
32void BUG_tar_header_size(void);
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000033char FAST_FUNC get_header_tar(archive_handle_t *archive_handle)
Glenn L McGrath95ebf612001-10-25 14:18:08 +000034{
Denis Vlasenko376ce1e2006-11-24 14:51:01 +000035 file_header_t *file_header = archive_handle->file_header;
36 struct {
37 /* ustar header, Posix 1003.1 */
38 char name[100]; /* 0-99 */
39 char mode[8]; /* 100-107 */
40 char uid[8]; /* 108-115 */
41 char gid[8]; /* 116-123 */
42 char size[12]; /* 124-135 */
43 char mtime[12]; /* 136-147 */
44 char chksum[8]; /* 148-155 */
45 char typeflag; /* 156-156 */
46 char linkname[100]; /* 157-256 */
Denis Vlasenko85128622007-11-16 20:35:30 +000047 /* POSIX: "ustar" NUL "00" */
48 /* GNU tar: "ustar " NUL */
Paul Fox16aec392007-11-17 19:11:05 +000049 /* Normally it's defined as magic[6] followed by
Denis Vlasenkoa60936d2008-06-28 05:04:09 +000050 * version[2], but we put them together to simplify code
Paul Fox16aec392007-11-17 19:11:05 +000051 */
Denis Vlasenko85128622007-11-16 20:35:30 +000052 char magic[8]; /* 257-264 */
Denis Vlasenko376ce1e2006-11-24 14:51:01 +000053 char uname[32]; /* 265-296 */
54 char gname[32]; /* 297-328 */
55 char devmajor[8]; /* 329-336 */
56 char devminor[8]; /* 337-344 */
57 char prefix[155]; /* 345-499 */
58 char padding[12]; /* 500-512 */
59 } tar;
60 char *cp;
Denis Vlasenkoa80b4a02007-06-21 12:41:59 +000061 int i, sum_u, sum;
62#if ENABLE_FEATURE_TAR_OLDSUN_COMPATIBILITY
63 int sum_s;
64#endif
Denis Vlasenkod6772502006-11-24 17:21:44 +000065 int parse_names;
Denis Vlasenko376ce1e2006-11-24 14:51:01 +000066
Denis Vlasenkoa60936d2008-06-28 05:04:09 +000067 /* Our "private data" */
68#define p_end (*(smallint *)(&archive_handle->ah_priv[0]))
69#if ENABLE_FEATURE_TAR_GNU_EXTENSIONS
70#define p_longname (*(char* *)(&archive_handle->ah_priv[1]))
71#define p_linkname (*(char* *)(&archive_handle->ah_priv[2]))
72#else
73#define p_longname 0
74#define p_linkname 0
75#endif
76// if (!archive_handle->ah_priv_inited) {
77// archive_handle->ah_priv_inited = 1;
78// p_end = 0;
79// USE_FEATURE_TAR_GNU_EXTENSIONS(p_longname = NULL;)
80// USE_FEATURE_TAR_GNU_EXTENSIONS(p_linkname = NULL;)
81// }
82
Denis Vlasenko376ce1e2006-11-24 14:51:01 +000083 if (sizeof(tar) != 512)
84 BUG_tar_header_size();
85
Denis Vlasenko666da5e2006-12-26 18:17:42 +000086#if ENABLE_FEATURE_TAR_GNU_EXTENSIONS
87 again:
88#endif
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000089 /* Align header */
Glenn L McGrath237ae422002-11-03 14:05:15 +000090 data_align(archive_handle, 512);
Glenn L McGrath95ebf612001-10-25 14:18:08 +000091
Denis Vlasenkob5963352006-11-26 01:46:59 +000092 again_after_align:
93
Denis Vlasenko12c06222008-02-14 08:52:30 +000094#if ENABLE_DESKTOP
Denis Vlasenko0381d422008-07-10 23:06:00 +000095 /* to prevent misdetection of bz2 sig */
96 *(uint32_t*)(&tar) = 0;
Denis Vlasenko23ffb6a2008-02-13 17:52:42 +000097 i = full_read(archive_handle->src_fd, &tar, 512);
Denis Vlasenkoa60936d2008-06-28 05:04:09 +000098 /* If GNU tar sees EOF in above read, it says:
Denis Vlasenko23ffb6a2008-02-13 17:52:42 +000099 * "tar: A lone zero block at N", where N = kilobyte
100 * where EOF was met (not EOF block, actual EOF!),
Denis Vlasenko0381d422008-07-10 23:06:00 +0000101 * and exits with EXIT_SUCCESS.
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000102 * We will mimic exit(EXIT_SUCCESS), although we will not mimic
Denis Vlasenko12c06222008-02-14 08:52:30 +0000103 * the message and we don't check whether we indeed
104 * saw zero block directly before this. */
Denis Vlasenko0381d422008-07-10 23:06:00 +0000105 if (i == 0) {
Denis Vlasenko23ffb6a2008-02-13 17:52:42 +0000106 xfunc_error_retval = 0;
Denis Vlasenko0381d422008-07-10 23:06:00 +0000107 short_read:
Denis Vlasenko23ffb6a2008-02-13 17:52:42 +0000108 bb_error_msg_and_die("short read");
Denis Vlasenko0381d422008-07-10 23:06:00 +0000109 }
110 if (i != 512) {
Denis Vlasenko86090e22008-07-11 08:23:52 +0000111 USE_FEATURE_TAR_AUTODETECT(goto autodetect;)
Denis Vlasenko0381d422008-07-10 23:06:00 +0000112 goto short_read;
113 }
114
Denis Vlasenko12c06222008-02-14 08:52:30 +0000115#else
Denis Vlasenko0381d422008-07-10 23:06:00 +0000116 i = 512;
117 xread(archive_handle->src_fd, &tar, i);
Denis Vlasenko12c06222008-02-14 08:52:30 +0000118#endif
Denis Vlasenko0381d422008-07-10 23:06:00 +0000119 archive_handle->offset += i;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000120
121 /* If there is no filename its an empty header */
Denis Vlasenko05efca92008-04-29 04:12:58 +0000122 if (tar.name[0] == 0 && tar.prefix[0] == 0) {
Denis Vlasenkoa60936d2008-06-28 05:04:09 +0000123 if (p_end) {
124 /* Second consecutive empty header - end of archive.
Paul Fox94ff9f12005-07-20 19:24:13 +0000125 * Read until the end to empty the pipe from gz or bz2
126 */
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000127 while (full_read(archive_handle->src_fd, &tar, 512) == 512)
Denis Vlasenko431a7c92008-02-19 11:26:28 +0000128 continue;
Denis Vlasenko13858992006-10-08 12:49:22 +0000129 return EXIT_FAILURE;
Paul Fox94ff9f12005-07-20 19:24:13 +0000130 }
Denis Vlasenkoa60936d2008-06-28 05:04:09 +0000131 p_end = 1;
Denis Vlasenko13858992006-10-08 12:49:22 +0000132 return EXIT_SUCCESS;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000133 }
Denis Vlasenkoa60936d2008-06-28 05:04:09 +0000134 p_end = 0;
Glenn L McGrath95ebf612001-10-25 14:18:08 +0000135
Denis Vlasenko431a7c92008-02-19 11:26:28 +0000136 /* Check header has valid magic, "ustar" is for the proper tar,
137 * five NULs are for the old tar format */
138 if (strncmp(tar.magic, "ustar", 5) != 0
139 && (!ENABLE_FEATURE_TAR_OLDGNU_COMPATIBILITY
140 || memcmp(tar.magic, "\0\0\0\0", 5) != 0)
141 ) {
142#if ENABLE_FEATURE_TAR_AUTODETECT
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000143 char FAST_FUNC (*get_header_ptr)(archive_handle_t *);
Denis Vlasenko431a7c92008-02-19 11:26:28 +0000144
Denis Vlasenko0381d422008-07-10 23:06:00 +0000145 USE_DESKTOP(autodetect:)
Denis Vlasenko431a7c92008-02-19 11:26:28 +0000146 /* tar gz/bz autodetect: check for gz/bz2 magic.
Denis Vlasenkoa60936d2008-06-28 05:04:09 +0000147 * If we see the magic, and it is the very first block,
Denis Vlasenko431a7c92008-02-19 11:26:28 +0000148 * we can switch to get_header_tar_gz/bz2/lzma().
Denis Vlasenko77ad97f2008-05-13 02:27:31 +0000149 * Needs seekable fd. I wish recv(MSG_PEEK) works
Denis Vlasenko431a7c92008-02-19 11:26:28 +0000150 * on any fd... */
Denis Vlasenkoe9ad84d2008-08-05 13:10:34 +0000151#if ENABLE_FEATURE_SEAMLESS_GZ
Denis Vlasenko77ad97f2008-05-13 02:27:31 +0000152 if (tar.name[0] == 0x1f && tar.name[1] == (char)0x8b) { /* gzip */
Denis Vlasenko431a7c92008-02-19 11:26:28 +0000153 get_header_ptr = get_header_tar_gz;
154 } else
Glenn L McGrath1d23f3a2002-08-13 05:06:43 +0000155#endif
Denis Vlasenkoe9ad84d2008-08-05 13:10:34 +0000156#if ENABLE_FEATURE_SEAMLESS_BZ2
Denis Vlasenko431a7c92008-02-19 11:26:28 +0000157 if (tar.name[0] == 'B' && tar.name[1] == 'Z'
158 && tar.name[2] == 'h' && isdigit(tar.name[3])
159 ) { /* bzip2 */
160 get_header_ptr = get_header_tar_bz2;
161 } else
162#endif
163 goto err;
Denis Vlasenkoa60936d2008-06-28 05:04:09 +0000164 /* Two different causes for lseek() != 0:
165 * unseekable fd (would like to support that too, but...),
166 * or not first block (false positive, it's not .gz/.bz2!) */
Denis Vlasenko0381d422008-07-10 23:06:00 +0000167 if (lseek(archive_handle->src_fd, -i, SEEK_CUR) != 0)
Denis Vlasenko431a7c92008-02-19 11:26:28 +0000168 goto err;
169 while (get_header_ptr(archive_handle) == EXIT_SUCCESS)
170 continue;
171 return EXIT_FAILURE;
172 err:
173#endif /* FEATURE_TAR_AUTODETECT */
174 bb_error_msg_and_die("invalid tar magic");
Glenn L McGrath95ebf612001-10-25 14:18:08 +0000175 }
Denis Vlasenkodcbd51d2007-03-03 20:06:59 +0000176
177 /* Do checksum on headers.
178 * POSIX says that checksum is done on unsigned bytes, but
Denis Vlasenko940494f2007-03-04 18:09:50 +0000179 * Sun and HP-UX gets it wrong... more details in
Denis Vlasenkodcbd51d2007-03-03 20:06:59 +0000180 * GNU tar source. */
Denis Vlasenkoa80b4a02007-06-21 12:41:59 +0000181#if ENABLE_FEATURE_TAR_OLDSUN_COMPATIBILITY
182 sum_s = ' ' * sizeof(tar.chksum);
183#endif
184 sum_u = ' ' * sizeof(tar.chksum);
Denis Vlasenkob71c6682007-07-21 15:08:09 +0000185 for (i = 0; i < 148; i++) {
Denis Vlasenkodcbd51d2007-03-03 20:06:59 +0000186 sum_u += ((unsigned char*)&tar)[i];
Denis Vlasenkoa80b4a02007-06-21 12:41:59 +0000187#if ENABLE_FEATURE_TAR_OLDSUN_COMPATIBILITY
Denis Vlasenkodcbd51d2007-03-03 20:06:59 +0000188 sum_s += ((signed char*)&tar)[i];
Denis Vlasenkoa80b4a02007-06-21 12:41:59 +0000189#endif
Glenn L McGrath95ebf612001-10-25 14:18:08 +0000190 }
Denis Vlasenkob71c6682007-07-21 15:08:09 +0000191 for (i = 156; i < 512; i++) {
Denis Vlasenkodcbd51d2007-03-03 20:06:59 +0000192 sum_u += ((unsigned char*)&tar)[i];
Denis Vlasenkoa80b4a02007-06-21 12:41:59 +0000193#if ENABLE_FEATURE_TAR_OLDSUN_COMPATIBILITY
Denis Vlasenkodcbd51d2007-03-03 20:06:59 +0000194 sum_s += ((signed char*)&tar)[i];
Denis Vlasenkoa80b4a02007-06-21 12:41:59 +0000195#endif
Glenn L McGrath95ebf612001-10-25 14:18:08 +0000196 }
Denis Vlasenkoa80b4a02007-06-21 12:41:59 +0000197#if ENABLE_FEATURE_TAR_OLDGNU_COMPATIBILITY
198 sum = strtoul(tar.chksum, &cp, 8);
199 if ((*cp && *cp != ' ')
200 || (sum_u != sum USE_FEATURE_TAR_OLDSUN_COMPATIBILITY(&& sum_s != sum))
201 ) {
Denis Vlasenko13858992006-10-08 12:49:22 +0000202 bb_error_msg_and_die("invalid tar header checksum");
Glenn L McGrath95ebf612001-10-25 14:18:08 +0000203 }
Denis Vlasenkoa80b4a02007-06-21 12:41:59 +0000204#else
205 /* This field does not need special treatment (getOctal) */
206 sum = xstrtoul(tar.chksum, 8);
207 if (sum_u != sum USE_FEATURE_TAR_OLDSUN_COMPATIBILITY(&& sum_s != sum)) {
208 bb_error_msg_and_die("invalid tar header checksum");
209 }
210#endif
Glenn L McGrath95ebf612001-10-25 14:18:08 +0000211
Denis Vlasenkob5963352006-11-26 01:46:59 +0000212 /* 0 is reserved for high perf file, treat as normal file */
213 if (!tar.typeflag) tar.typeflag = '0';
214 parse_names = (tar.typeflag >= '0' && tar.typeflag <= '7');
Denis Vlasenkod6772502006-11-24 17:21:44 +0000215
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000216 /* getOctal trashes subsequent field, therefore we call it
217 * on fields in reverse order */
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000218 if (tar.devmajor[0]) {
Denis Vlasenkod93400b2008-04-29 03:54:16 +0000219 char t = tar.prefix[0];
220 /* we trash prefix[0] here, but we DO need it later! */
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000221 unsigned minor = GET_OCTAL(tar.devminor);
222 unsigned major = GET_OCTAL(tar.devmajor);
223 file_header->device = makedev(major, minor);
Denis Vlasenkod93400b2008-04-29 03:54:16 +0000224 tar.prefix[0] = t;
Denis Vlasenkocba9ef52006-10-10 21:00:47 +0000225 }
Denis Vlasenko75103842007-06-20 14:49:47 +0000226 file_header->link_target = NULL;
Denis Vlasenkoa60936d2008-06-28 05:04:09 +0000227 if (!p_linkname && parse_names && tar.linkname[0]) {
Denis Vlasenkobc1918a2008-04-15 01:17:50 +0000228 file_header->link_target = xstrndup(tar.linkname, sizeof(tar.linkname));
Denis Vlasenko75103842007-06-20 14:49:47 +0000229 /* FIXME: what if we have non-link object with link_target? */
230 /* Will link_target be free()ed? */
Denis Vlasenkod6772502006-11-24 17:21:44 +0000231 }
Denis Vlasenkoe00e5022008-02-14 20:37:54 +0000232#if ENABLE_FEATURE_TAR_UNAME_GNAME
233 file_header->uname = tar.uname[0] ? xstrndup(tar.uname, sizeof(tar.uname)) : NULL;
234 file_header->gname = tar.gname[0] ? xstrndup(tar.gname, sizeof(tar.gname)) : NULL;
235#endif
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000236 file_header->mtime = GET_OCTAL(tar.mtime);
237 file_header->size = GET_OCTAL(tar.size);
238 file_header->gid = GET_OCTAL(tar.gid);
239 file_header->uid = GET_OCTAL(tar.uid);
Glenn L McGrath916ba532004-02-20 02:34:42 +0000240 /* Set bits 0-11 of the files mode */
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000241 file_header->mode = 07777 & GET_OCTAL(tar.mode);
Glenn L McGrath916ba532004-02-20 02:34:42 +0000242
Denis Vlasenkob5963352006-11-26 01:46:59 +0000243 file_header->name = NULL;
Denis Vlasenkoa60936d2008-06-28 05:04:09 +0000244 if (!p_longname && parse_names) {
Denis Vlasenkod6772502006-11-24 17:21:44 +0000245 /* we trash mode[0] here, it's ok */
Denis Vlasenkobc1918a2008-04-15 01:17:50 +0000246 //tar.name[sizeof(tar.name)] = '\0'; - gcc 4.3.0 would complain
247 tar.mode[0] = '\0';
Denis Vlasenko87cd4a82006-11-25 23:47:32 +0000248 if (tar.prefix[0]) {
249 /* and padding[0] */
Denis Vlasenkobc1918a2008-04-15 01:17:50 +0000250 //tar.prefix[sizeof(tar.prefix)] = '\0'; - gcc 4.3.0 would complain
251 tar.padding[0] = '\0';
Denis Vlasenkocf30cc82006-11-24 14:53:18 +0000252 file_header->name = concat_path_file(tar.prefix, tar.name);
Denis Vlasenko87cd4a82006-11-25 23:47:32 +0000253 } else
Denis Vlasenkocf30cc82006-11-24 14:53:18 +0000254 file_header->name = xstrdup(tar.name);
255 }
256
Glenn L McGrath916ba532004-02-20 02:34:42 +0000257 /* Set bits 12-15 of the files mode */
Denis Vlasenkod6772502006-11-24 17:21:44 +0000258 /* (typeflag was not trashed because chksum does not use getOctal) */
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000259 switch (tar.typeflag) {
Glenn L McGrathb0e163a2004-02-19 08:48:30 +0000260 /* busybox identifies hard links as being regular files with 0 size and a link name */
261 case '1':
Glenn L McGrath916ba532004-02-20 02:34:42 +0000262 file_header->mode |= S_IFREG;
Glenn L McGrathb0e163a2004-02-19 08:48:30 +0000263 break;
Glenn L McGrathc9f1fce2004-02-20 02:25:18 +0000264 case '7':
Denis Vlasenkob5963352006-11-26 01:46:59 +0000265 /* case 0: */
Glenn L McGrath21110a02003-01-28 01:45:48 +0000266 case '0':
Denis Vlasenkod6772502006-11-24 17:21:44 +0000267#if ENABLE_FEATURE_TAR_OLDGNU_COMPATIBILITY
Glenn L McGrath87af49f2003-09-09 17:41:03 +0000268 if (last_char_is(file_header->name, '/')) {
Denis Vlasenkoadc772a2008-07-20 17:10:43 +0000269 goto set_dir;
270 }
Glenn L McGrathc9f1fce2004-02-20 02:25:18 +0000271#endif
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000272 file_header->mode |= S_IFREG;
Glenn L McGrath99b12542002-08-22 17:47:09 +0000273 break;
Glenn L McGrath21110a02003-01-28 01:45:48 +0000274 case '2':
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000275 file_header->mode |= S_IFLNK;
Denis Vlasenkoadc772a2008-07-20 17:10:43 +0000276 /* have seen tarballs with size field containing
277 * the size of the link target's name */
278 size0:
279 file_header->size = 0;
Glenn L McGrath99b12542002-08-22 17:47:09 +0000280 break;
Glenn L McGrath21110a02003-01-28 01:45:48 +0000281 case '3':
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000282 file_header->mode |= S_IFCHR;
Denis Vlasenkoadc772a2008-07-20 17:10:43 +0000283 goto size0; /* paranoia */
Glenn L McGrath21110a02003-01-28 01:45:48 +0000284 case '4':
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000285 file_header->mode |= S_IFBLK;
Denis Vlasenkoadc772a2008-07-20 17:10:43 +0000286 goto size0;
Glenn L McGrath21110a02003-01-28 01:45:48 +0000287 case '5':
Denis Vlasenko68a192c2008-07-22 19:38:57 +0000288 USE_FEATURE_TAR_OLDGNU_COMPATIBILITY(set_dir:)
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000289 file_header->mode |= S_IFDIR;
Denis Vlasenkoadc772a2008-07-20 17:10:43 +0000290 goto size0;
Glenn L McGrath21110a02003-01-28 01:45:48 +0000291 case '6':
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000292 file_header->mode |= S_IFIFO;
Denis Vlasenkoadc772a2008-07-20 17:10:43 +0000293 goto size0;
Denis Vlasenkod6772502006-11-24 17:21:44 +0000294#if ENABLE_FEATURE_TAR_GNU_EXTENSIONS
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000295 case 'L':
Denis Vlasenkob5963352006-11-26 01:46:59 +0000296 /* free: paranoia: tar with several consecutive longnames */
Denis Vlasenkoa60936d2008-06-28 05:04:09 +0000297 free(p_longname);
Denis Vlasenkob5963352006-11-26 01:46:59 +0000298 /* For paranoia reasons we allocate extra NUL char */
Denis Vlasenkoa60936d2008-06-28 05:04:09 +0000299 p_longname = xzalloc(file_header->size + 1);
Denis Vlasenkob5963352006-11-26 01:46:59 +0000300 /* We read ASCIZ string, including NUL */
Denis Vlasenkoa60936d2008-06-28 05:04:09 +0000301 xread(archive_handle->src_fd, p_longname, file_header->size);
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000302 archive_handle->offset += file_header->size;
Denis Vlasenkob5963352006-11-26 01:46:59 +0000303 /* return get_header_tar(archive_handle); */
304 /* gcc 4.1.1 didn't optimize it into jump */
305 /* so we will do it ourself, this also saves stack */
306 goto again;
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000307 case 'K':
Denis Vlasenkoa60936d2008-06-28 05:04:09 +0000308 free(p_linkname);
309 p_linkname = xzalloc(file_header->size + 1);
310 xread(archive_handle->src_fd, p_linkname, file_header->size);
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000311 archive_handle->offset += file_header->size;
Denis Vlasenkob5963352006-11-26 01:46:59 +0000312 /* return get_header_tar(archive_handle); */
313 goto again;
Glenn L McGrathb0e163a2004-02-19 08:48:30 +0000314 case 'D': /* GNU dump dir */
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000315 case 'M': /* Continuation of multi volume archive */
Glenn L McGrathb0e163a2004-02-19 08:48:30 +0000316 case 'N': /* Old GNU for names > 100 characters */
317 case 'S': /* Sparse file */
318 case 'V': /* Volume header */
Glenn L McGrath6aa52232004-02-17 11:55:06 +0000319#endif
Rob Landleyff6e21c2006-07-06 20:30:19 +0000320 case 'g': /* pax global header */
Denis Vlasenkob5963352006-11-26 01:46:59 +0000321 case 'x': { /* pax extended header */
322 off_t sz;
323 bb_error_msg("warning: skipping header '%c'", tar.typeflag);
324 sz = (file_header->size + 511) & ~(off_t)511;
325 archive_handle->offset += sz;
326 sz >>= 9; /* sz /= 512 but w/o contortions for signed div */
327 while (sz--)
328 xread(archive_handle->src_fd, &tar, 512);
329 /* return get_header_tar(archive_handle); */
330 goto again_after_align;
331 }
Glenn L McGrathb0e163a2004-02-19 08:48:30 +0000332 default:
Denis Vlasenkob5963352006-11-26 01:46:59 +0000333 bb_error_msg_and_die("unknown typeflag: 0x%x", tar.typeflag);
Glenn L McGrath6aa52232004-02-17 11:55:06 +0000334 }
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000335
Denis Vlasenkod6772502006-11-24 17:21:44 +0000336#if ENABLE_FEATURE_TAR_GNU_EXTENSIONS
Denis Vlasenkoa60936d2008-06-28 05:04:09 +0000337 if (p_longname) {
338 file_header->name = p_longname;
339 p_longname = NULL;
Denis Vlasenkod6772502006-11-24 17:21:44 +0000340 }
Denis Vlasenkoa60936d2008-06-28 05:04:09 +0000341 if (p_linkname) {
342 file_header->link_target = p_linkname;
343 p_linkname = NULL;
Denis Vlasenkod6772502006-11-24 17:21:44 +0000344 }
345#endif
Denis Vlasenko0381d422008-07-10 23:06:00 +0000346 if (strncmp(file_header->name, "/../"+1, 3) == 0
Denis Vlasenkoc1660fe2006-11-26 15:42:03 +0000347 || strstr(file_header->name, "/../")
348 ) {
349 bb_error_msg_and_die("name with '..' encountered: '%s'",
350 file_header->name);
351 }
Denis Vlasenkod6772502006-11-24 17:21:44 +0000352
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000353 /* Strip trailing '/' in directories */
Denis Vlasenko714701c2006-12-22 00:21:07 +0000354 /* Must be done after mode is set as '/' is used to check if it's a directory */
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000355 cp = last_char_is(file_header->name, '/');
Glenn L McGrath3d5828f2003-08-14 02:55:15 +0000356
Glenn L McGrath8e940982002-11-04 23:47:31 +0000357 if (archive_handle->filter(archive_handle) == EXIT_SUCCESS) {
Denis Vlasenko0381d422008-07-10 23:06:00 +0000358 archive_handle->action_header(/*archive_handle->*/ file_header);
Denis Vlasenkocf30cc82006-11-24 14:53:18 +0000359 /* Note that we kill the '/' only after action_header() */
360 /* (like GNU tar 1.15.1: verbose mode outputs "dir/dir/") */
361 if (cp) *cp = '\0';
Denis Vlasenkoa60936d2008-06-28 05:04:09 +0000362 archive_handle->ah_flags |= ARCHIVE_EXTRACT_QUIET;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000363 archive_handle->action_data(archive_handle);
Rob Landley8bb50782006-05-26 23:44:51 +0000364 llist_add_to(&(archive_handle->passed), file_header->name);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000365 } else {
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000366 data_skip(archive_handle);
Denis Vlasenkob5963352006-11-26 01:46:59 +0000367 free(file_header->name);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000368 }
369 archive_handle->offset += file_header->size;
370
Denis Vlasenko75103842007-06-20 14:49:47 +0000371 free(file_header->link_target);
Denis Vlasenkoa46dd892008-07-12 09:20:44 +0000372 /* Do not free(file_header->name)! (why?) */
Denis Vlasenkoe00e5022008-02-14 20:37:54 +0000373#if ENABLE_FEATURE_TAR_UNAME_GNAME
374 free(file_header->uname);
375 free(file_header->gname);
376#endif
Denis Vlasenko13858992006-10-08 12:49:22 +0000377 return EXIT_SUCCESS;
Eric Andersen2276d832002-07-11 11:11:56 +0000378}