blob: 69362f1cc6ccadcc6507d37c7d17ed65371aa069 [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 Vlasenko23ffb6a2008-02-13 17:52:42 +000095 i = full_read(archive_handle->src_fd, &tar, 512);
Denis Vlasenkoa60936d2008-06-28 05:04:09 +000096 /* If GNU tar sees EOF in above read, it says:
Denis Vlasenko23ffb6a2008-02-13 17:52:42 +000097 * "tar: A lone zero block at N", where N = kilobyte
98 * where EOF was met (not EOF block, actual EOF!),
Denis Vlasenko12c06222008-02-14 08:52:30 +000099 * and tar will exit with error code 0.
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000100 * We will mimic exit(EXIT_SUCCESS), although we will not mimic
Denis Vlasenko12c06222008-02-14 08:52:30 +0000101 * the message and we don't check whether we indeed
102 * saw zero block directly before this. */
Denis Vlasenko23ffb6a2008-02-13 17:52:42 +0000103 if (i == 0)
104 xfunc_error_retval = 0;
105 if (i != 512)
106 bb_error_msg_and_die("short read");
Denis Vlasenko12c06222008-02-14 08:52:30 +0000107#else
108 xread(archive_handle->src_fd, &tar, 512);
109#endif
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000110 archive_handle->offset += 512;
111
112 /* If there is no filename its an empty header */
Denis Vlasenko05efca92008-04-29 04:12:58 +0000113 if (tar.name[0] == 0 && tar.prefix[0] == 0) {
Denis Vlasenkoa60936d2008-06-28 05:04:09 +0000114 if (p_end) {
115 /* Second consecutive empty header - end of archive.
Paul Fox94ff9f12005-07-20 19:24:13 +0000116 * Read until the end to empty the pipe from gz or bz2
117 */
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000118 while (full_read(archive_handle->src_fd, &tar, 512) == 512)
Denis Vlasenko431a7c92008-02-19 11:26:28 +0000119 continue;
Denis Vlasenko13858992006-10-08 12:49:22 +0000120 return EXIT_FAILURE;
Paul Fox94ff9f12005-07-20 19:24:13 +0000121 }
Denis Vlasenkoa60936d2008-06-28 05:04:09 +0000122 p_end = 1;
Denis Vlasenko13858992006-10-08 12:49:22 +0000123 return EXIT_SUCCESS;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000124 }
Denis Vlasenkoa60936d2008-06-28 05:04:09 +0000125 p_end = 0;
Glenn L McGrath95ebf612001-10-25 14:18:08 +0000126
Denis Vlasenko431a7c92008-02-19 11:26:28 +0000127 /* Check header has valid magic, "ustar" is for the proper tar,
128 * five NULs are for the old tar format */
129 if (strncmp(tar.magic, "ustar", 5) != 0
130 && (!ENABLE_FEATURE_TAR_OLDGNU_COMPATIBILITY
131 || memcmp(tar.magic, "\0\0\0\0", 5) != 0)
132 ) {
133#if ENABLE_FEATURE_TAR_AUTODETECT
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000134 char FAST_FUNC (*get_header_ptr)(archive_handle_t *);
Denis Vlasenko431a7c92008-02-19 11:26:28 +0000135
136 /* tar gz/bz autodetect: check for gz/bz2 magic.
Denis Vlasenkoa60936d2008-06-28 05:04:09 +0000137 * If we see the magic, and it is the very first block,
Denis Vlasenko431a7c92008-02-19 11:26:28 +0000138 * we can switch to get_header_tar_gz/bz2/lzma().
Denis Vlasenko77ad97f2008-05-13 02:27:31 +0000139 * Needs seekable fd. I wish recv(MSG_PEEK) works
Denis Vlasenko431a7c92008-02-19 11:26:28 +0000140 * on any fd... */
Denis Vlasenko431a7c92008-02-19 11:26:28 +0000141#if ENABLE_FEATURE_TAR_GZIP
Denis Vlasenko77ad97f2008-05-13 02:27:31 +0000142 if (tar.name[0] == 0x1f && tar.name[1] == (char)0x8b) { /* gzip */
Denis Vlasenko431a7c92008-02-19 11:26:28 +0000143 get_header_ptr = get_header_tar_gz;
144 } else
Glenn L McGrath1d23f3a2002-08-13 05:06:43 +0000145#endif
Denis Vlasenko431a7c92008-02-19 11:26:28 +0000146#if ENABLE_FEATURE_TAR_BZIP2
147 if (tar.name[0] == 'B' && tar.name[1] == 'Z'
148 && tar.name[2] == 'h' && isdigit(tar.name[3])
149 ) { /* bzip2 */
150 get_header_ptr = get_header_tar_bz2;
151 } else
152#endif
153 goto err;
Denis Vlasenkoa60936d2008-06-28 05:04:09 +0000154 /* Two different causes for lseek() != 0:
155 * unseekable fd (would like to support that too, but...),
156 * or not first block (false positive, it's not .gz/.bz2!) */
Denis Vlasenko431a7c92008-02-19 11:26:28 +0000157 if (lseek(archive_handle->src_fd, -512, SEEK_CUR) != 0)
158 goto err;
159 while (get_header_ptr(archive_handle) == EXIT_SUCCESS)
160 continue;
161 return EXIT_FAILURE;
162 err:
163#endif /* FEATURE_TAR_AUTODETECT */
164 bb_error_msg_and_die("invalid tar magic");
Glenn L McGrath95ebf612001-10-25 14:18:08 +0000165 }
Denis Vlasenkodcbd51d2007-03-03 20:06:59 +0000166
167 /* Do checksum on headers.
168 * POSIX says that checksum is done on unsigned bytes, but
Denis Vlasenko940494f2007-03-04 18:09:50 +0000169 * Sun and HP-UX gets it wrong... more details in
Denis Vlasenkodcbd51d2007-03-03 20:06:59 +0000170 * GNU tar source. */
Denis Vlasenkoa80b4a02007-06-21 12:41:59 +0000171#if ENABLE_FEATURE_TAR_OLDSUN_COMPATIBILITY
172 sum_s = ' ' * sizeof(tar.chksum);
173#endif
174 sum_u = ' ' * sizeof(tar.chksum);
Denis Vlasenkob71c6682007-07-21 15:08:09 +0000175 for (i = 0; i < 148; i++) {
Denis Vlasenkodcbd51d2007-03-03 20:06:59 +0000176 sum_u += ((unsigned char*)&tar)[i];
Denis Vlasenkoa80b4a02007-06-21 12:41:59 +0000177#if ENABLE_FEATURE_TAR_OLDSUN_COMPATIBILITY
Denis Vlasenkodcbd51d2007-03-03 20:06:59 +0000178 sum_s += ((signed char*)&tar)[i];
Denis Vlasenkoa80b4a02007-06-21 12:41:59 +0000179#endif
Glenn L McGrath95ebf612001-10-25 14:18:08 +0000180 }
Denis Vlasenkob71c6682007-07-21 15:08:09 +0000181 for (i = 156; i < 512; i++) {
Denis Vlasenkodcbd51d2007-03-03 20:06:59 +0000182 sum_u += ((unsigned char*)&tar)[i];
Denis Vlasenkoa80b4a02007-06-21 12:41:59 +0000183#if ENABLE_FEATURE_TAR_OLDSUN_COMPATIBILITY
Denis Vlasenkodcbd51d2007-03-03 20:06:59 +0000184 sum_s += ((signed char*)&tar)[i];
Denis Vlasenkoa80b4a02007-06-21 12:41:59 +0000185#endif
Glenn L McGrath95ebf612001-10-25 14:18:08 +0000186 }
Denis Vlasenkoa80b4a02007-06-21 12:41:59 +0000187#if ENABLE_FEATURE_TAR_OLDGNU_COMPATIBILITY
188 sum = strtoul(tar.chksum, &cp, 8);
189 if ((*cp && *cp != ' ')
190 || (sum_u != sum USE_FEATURE_TAR_OLDSUN_COMPATIBILITY(&& sum_s != sum))
191 ) {
Denis Vlasenko13858992006-10-08 12:49:22 +0000192 bb_error_msg_and_die("invalid tar header checksum");
Glenn L McGrath95ebf612001-10-25 14:18:08 +0000193 }
Denis Vlasenkoa80b4a02007-06-21 12:41:59 +0000194#else
195 /* This field does not need special treatment (getOctal) */
196 sum = xstrtoul(tar.chksum, 8);
197 if (sum_u != sum USE_FEATURE_TAR_OLDSUN_COMPATIBILITY(&& sum_s != sum)) {
198 bb_error_msg_and_die("invalid tar header checksum");
199 }
200#endif
Glenn L McGrath95ebf612001-10-25 14:18:08 +0000201
Denis Vlasenkob5963352006-11-26 01:46:59 +0000202 /* 0 is reserved for high perf file, treat as normal file */
203 if (!tar.typeflag) tar.typeflag = '0';
204 parse_names = (tar.typeflag >= '0' && tar.typeflag <= '7');
Denis Vlasenkod6772502006-11-24 17:21:44 +0000205
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000206 /* getOctal trashes subsequent field, therefore we call it
207 * on fields in reverse order */
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000208 if (tar.devmajor[0]) {
Denis Vlasenkod93400b2008-04-29 03:54:16 +0000209 char t = tar.prefix[0];
210 /* we trash prefix[0] here, but we DO need it later! */
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000211 unsigned minor = GET_OCTAL(tar.devminor);
212 unsigned major = GET_OCTAL(tar.devmajor);
213 file_header->device = makedev(major, minor);
Denis Vlasenkod93400b2008-04-29 03:54:16 +0000214 tar.prefix[0] = t;
Denis Vlasenkocba9ef52006-10-10 21:00:47 +0000215 }
Denis Vlasenko75103842007-06-20 14:49:47 +0000216 file_header->link_target = NULL;
Denis Vlasenkoa60936d2008-06-28 05:04:09 +0000217 if (!p_linkname && parse_names && tar.linkname[0]) {
Denis Vlasenkobc1918a2008-04-15 01:17:50 +0000218 file_header->link_target = xstrndup(tar.linkname, sizeof(tar.linkname));
Denis Vlasenko75103842007-06-20 14:49:47 +0000219 /* FIXME: what if we have non-link object with link_target? */
220 /* Will link_target be free()ed? */
Denis Vlasenkod6772502006-11-24 17:21:44 +0000221 }
Denis Vlasenkoe00e5022008-02-14 20:37:54 +0000222#if ENABLE_FEATURE_TAR_UNAME_GNAME
223 file_header->uname = tar.uname[0] ? xstrndup(tar.uname, sizeof(tar.uname)) : NULL;
224 file_header->gname = tar.gname[0] ? xstrndup(tar.gname, sizeof(tar.gname)) : NULL;
225#endif
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000226 file_header->mtime = GET_OCTAL(tar.mtime);
227 file_header->size = GET_OCTAL(tar.size);
228 file_header->gid = GET_OCTAL(tar.gid);
229 file_header->uid = GET_OCTAL(tar.uid);
Glenn L McGrath916ba532004-02-20 02:34:42 +0000230 /* Set bits 0-11 of the files mode */
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000231 file_header->mode = 07777 & GET_OCTAL(tar.mode);
Glenn L McGrath916ba532004-02-20 02:34:42 +0000232
Denis Vlasenkob5963352006-11-26 01:46:59 +0000233 file_header->name = NULL;
Denis Vlasenkoa60936d2008-06-28 05:04:09 +0000234 if (!p_longname && parse_names) {
Denis Vlasenkod6772502006-11-24 17:21:44 +0000235 /* we trash mode[0] here, it's ok */
Denis Vlasenkobc1918a2008-04-15 01:17:50 +0000236 //tar.name[sizeof(tar.name)] = '\0'; - gcc 4.3.0 would complain
237 tar.mode[0] = '\0';
Denis Vlasenko87cd4a82006-11-25 23:47:32 +0000238 if (tar.prefix[0]) {
239 /* and padding[0] */
Denis Vlasenkobc1918a2008-04-15 01:17:50 +0000240 //tar.prefix[sizeof(tar.prefix)] = '\0'; - gcc 4.3.0 would complain
241 tar.padding[0] = '\0';
Denis Vlasenkocf30cc82006-11-24 14:53:18 +0000242 file_header->name = concat_path_file(tar.prefix, tar.name);
Denis Vlasenko87cd4a82006-11-25 23:47:32 +0000243 } else
Denis Vlasenkocf30cc82006-11-24 14:53:18 +0000244 file_header->name = xstrdup(tar.name);
245 }
246
Glenn L McGrath916ba532004-02-20 02:34:42 +0000247 /* Set bits 12-15 of the files mode */
Denis Vlasenkod6772502006-11-24 17:21:44 +0000248 /* (typeflag was not trashed because chksum does not use getOctal) */
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000249 switch (tar.typeflag) {
Glenn L McGrathb0e163a2004-02-19 08:48:30 +0000250 /* busybox identifies hard links as being regular files with 0 size and a link name */
251 case '1':
Glenn L McGrath916ba532004-02-20 02:34:42 +0000252 file_header->mode |= S_IFREG;
Glenn L McGrathb0e163a2004-02-19 08:48:30 +0000253 break;
Glenn L McGrathc9f1fce2004-02-20 02:25:18 +0000254 case '7':
Denis Vlasenkob5963352006-11-26 01:46:59 +0000255 /* case 0: */
Glenn L McGrath21110a02003-01-28 01:45:48 +0000256 case '0':
Denis Vlasenkod6772502006-11-24 17:21:44 +0000257#if ENABLE_FEATURE_TAR_OLDGNU_COMPATIBILITY
Glenn L McGrath87af49f2003-09-09 17:41:03 +0000258 if (last_char_is(file_header->name, '/')) {
259 file_header->mode |= S_IFDIR;
Glenn L McGrathc9f1fce2004-02-20 02:25:18 +0000260 } else
261#endif
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000262 file_header->mode |= S_IFREG;
Glenn L McGrath99b12542002-08-22 17:47:09 +0000263 break;
Glenn L McGrath21110a02003-01-28 01:45:48 +0000264 case '2':
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000265 file_header->mode |= S_IFLNK;
Glenn L McGrath99b12542002-08-22 17:47:09 +0000266 break;
Glenn L McGrath21110a02003-01-28 01:45:48 +0000267 case '3':
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000268 file_header->mode |= S_IFCHR;
Glenn L McGrath99b12542002-08-22 17:47:09 +0000269 break;
Glenn L McGrath21110a02003-01-28 01:45:48 +0000270 case '4':
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000271 file_header->mode |= S_IFBLK;
Glenn L McGrath99b12542002-08-22 17:47:09 +0000272 break;
Glenn L McGrath21110a02003-01-28 01:45:48 +0000273 case '5':
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000274 file_header->mode |= S_IFDIR;
Glenn L McGrath99b12542002-08-22 17:47:09 +0000275 break;
Glenn L McGrath21110a02003-01-28 01:45:48 +0000276 case '6':
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000277 file_header->mode |= S_IFIFO;
Glenn L McGrath99b12542002-08-22 17:47:09 +0000278 break;
Denis Vlasenkod6772502006-11-24 17:21:44 +0000279#if ENABLE_FEATURE_TAR_GNU_EXTENSIONS
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000280 case 'L':
Denis Vlasenkob5963352006-11-26 01:46:59 +0000281 /* free: paranoia: tar with several consecutive longnames */
Denis Vlasenkoa60936d2008-06-28 05:04:09 +0000282 free(p_longname);
Denis Vlasenkob5963352006-11-26 01:46:59 +0000283 /* For paranoia reasons we allocate extra NUL char */
Denis Vlasenkoa60936d2008-06-28 05:04:09 +0000284 p_longname = xzalloc(file_header->size + 1);
Denis Vlasenkob5963352006-11-26 01:46:59 +0000285 /* We read ASCIZ string, including NUL */
Denis Vlasenkoa60936d2008-06-28 05:04:09 +0000286 xread(archive_handle->src_fd, p_longname, file_header->size);
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000287 archive_handle->offset += file_header->size;
Denis Vlasenkob5963352006-11-26 01:46:59 +0000288 /* return get_header_tar(archive_handle); */
289 /* gcc 4.1.1 didn't optimize it into jump */
290 /* so we will do it ourself, this also saves stack */
291 goto again;
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000292 case 'K':
Denis Vlasenkoa60936d2008-06-28 05:04:09 +0000293 free(p_linkname);
294 p_linkname = xzalloc(file_header->size + 1);
295 xread(archive_handle->src_fd, p_linkname, file_header->size);
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000296 archive_handle->offset += file_header->size;
Denis Vlasenkob5963352006-11-26 01:46:59 +0000297 /* return get_header_tar(archive_handle); */
298 goto again;
Glenn L McGrathb0e163a2004-02-19 08:48:30 +0000299 case 'D': /* GNU dump dir */
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000300 case 'M': /* Continuation of multi volume archive */
Glenn L McGrathb0e163a2004-02-19 08:48:30 +0000301 case 'N': /* Old GNU for names > 100 characters */
302 case 'S': /* Sparse file */
303 case 'V': /* Volume header */
Glenn L McGrath6aa52232004-02-17 11:55:06 +0000304#endif
Rob Landleyff6e21c2006-07-06 20:30:19 +0000305 case 'g': /* pax global header */
Denis Vlasenkob5963352006-11-26 01:46:59 +0000306 case 'x': { /* pax extended header */
307 off_t sz;
308 bb_error_msg("warning: skipping header '%c'", tar.typeflag);
309 sz = (file_header->size + 511) & ~(off_t)511;
310 archive_handle->offset += sz;
311 sz >>= 9; /* sz /= 512 but w/o contortions for signed div */
312 while (sz--)
313 xread(archive_handle->src_fd, &tar, 512);
314 /* return get_header_tar(archive_handle); */
315 goto again_after_align;
316 }
Glenn L McGrathb0e163a2004-02-19 08:48:30 +0000317 default:
Denis Vlasenkob5963352006-11-26 01:46:59 +0000318 bb_error_msg_and_die("unknown typeflag: 0x%x", tar.typeflag);
Glenn L McGrath6aa52232004-02-17 11:55:06 +0000319 }
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000320
Denis Vlasenkod6772502006-11-24 17:21:44 +0000321#if ENABLE_FEATURE_TAR_GNU_EXTENSIONS
Denis Vlasenkoa60936d2008-06-28 05:04:09 +0000322 if (p_longname) {
323 file_header->name = p_longname;
324 p_longname = NULL;
Denis Vlasenkod6772502006-11-24 17:21:44 +0000325 }
Denis Vlasenkoa60936d2008-06-28 05:04:09 +0000326 if (p_linkname) {
327 file_header->link_target = p_linkname;
328 p_linkname = NULL;
Denis Vlasenkod6772502006-11-24 17:21:44 +0000329 }
330#endif
Denis Vlasenkoc1660fe2006-11-26 15:42:03 +0000331 if (!strncmp(file_header->name, "/../"+1, 3)
332 || strstr(file_header->name, "/../")
333 ) {
334 bb_error_msg_and_die("name with '..' encountered: '%s'",
335 file_header->name);
336 }
Denis Vlasenkod6772502006-11-24 17:21:44 +0000337
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000338 /* Strip trailing '/' in directories */
Denis Vlasenko714701c2006-12-22 00:21:07 +0000339 /* Must be done after mode is set as '/' is used to check if it's a directory */
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000340 cp = last_char_is(file_header->name, '/');
Glenn L McGrath3d5828f2003-08-14 02:55:15 +0000341
Glenn L McGrath8e940982002-11-04 23:47:31 +0000342 if (archive_handle->filter(archive_handle) == EXIT_SUCCESS) {
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000343 archive_handle->action_header(archive_handle->file_header);
Denis Vlasenkocf30cc82006-11-24 14:53:18 +0000344 /* Note that we kill the '/' only after action_header() */
345 /* (like GNU tar 1.15.1: verbose mode outputs "dir/dir/") */
346 if (cp) *cp = '\0';
Denis Vlasenkoa60936d2008-06-28 05:04:09 +0000347 archive_handle->ah_flags |= ARCHIVE_EXTRACT_QUIET;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000348 archive_handle->action_data(archive_handle);
Rob Landley8bb50782006-05-26 23:44:51 +0000349 llist_add_to(&(archive_handle->passed), file_header->name);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000350 } else {
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000351 data_skip(archive_handle);
Denis Vlasenkob5963352006-11-26 01:46:59 +0000352 free(file_header->name);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000353 }
354 archive_handle->offset += file_header->size;
355
Denis Vlasenko75103842007-06-20 14:49:47 +0000356 free(file_header->link_target);
Denis Vlasenkob5963352006-11-26 01:46:59 +0000357 /* Do not free(file_header->name)! */
Denis Vlasenkoe00e5022008-02-14 20:37:54 +0000358#if ENABLE_FEATURE_TAR_UNAME_GNAME
359 free(file_header->uname);
360 free(file_header->gname);
361#endif
Denis Vlasenko13858992006-10-08 12:49:22 +0000362 return EXIT_SUCCESS;
Eric Andersen2276d832002-07-11 11:11:56 +0000363}