2006-07-03 01:17:05 +05:30
|
|
|
/* vi: set sw=4 ts=4: */
|
2006-02-21 00:58:53 +05:30
|
|
|
/* Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
|
2004-02-17 17:25:06 +05:30
|
|
|
*
|
2004-02-20 07:55:18 +05:30
|
|
|
* FIXME:
|
2004-04-14 23:21:38 +05:30
|
|
|
* In privileged mode if uname and gname map to a uid and gid then use the
|
2004-02-20 07:55:18 +05:30
|
|
|
* mapped value instead of the uid/gid values in tar header
|
2004-02-19 14:18:30 +05:30
|
|
|
*
|
|
|
|
* References:
|
|
|
|
* GNU tar and star man pages,
|
|
|
|
* Opengroup's ustar interchange format,
|
2006-01-25 05:38:53 +05:30
|
|
|
* http://www.opengroup.org/onlinepubs/007904975/utilities/pax.html
|
2001-10-25 19:48:08 +05:30
|
|
|
*/
|
|
|
|
|
|
|
|
#include "libbb.h"
|
2006-08-03 21:11:12 +05:30
|
|
|
#include "unarchive.h"
|
2001-10-25 19:48:08 +05:30
|
|
|
|
2010-02-04 19:30:15 +05:30
|
|
|
typedef uint32_t aliased_uint32_t FIX_ALIASING;
|
|
|
|
typedef off_t aliased_off_t FIX_ALIASING;
|
|
|
|
|
|
|
|
|
2008-12-12 07:44:35 +05:30
|
|
|
/*
|
|
|
|
* GNU tar uses "base-256 encoding" for very large numbers (>8 billion).
|
|
|
|
* Encoding is binary, with highest bit always set as a marker
|
|
|
|
* and sign in next-highest bit:
|
|
|
|
* 80 00 .. 00 - zero
|
|
|
|
* bf ff .. ff - largest positive number
|
|
|
|
* ff ff .. ff - minus 1
|
|
|
|
* c0 00 .. 00 - smallest negative number
|
|
|
|
*
|
|
|
|
* We expect it only in size field, where negative numbers don't make sense.
|
|
|
|
*/
|
|
|
|
static off_t getBase256_len12(const char *str)
|
|
|
|
{
|
|
|
|
off_t value;
|
|
|
|
int len;
|
|
|
|
|
|
|
|
/* if (*str & 0x40) error; - caller prevents this */
|
|
|
|
|
|
|
|
if (sizeof(off_t) >= 12) {
|
|
|
|
/* Probably 128-bit (16 byte) off_t. Can be optimized. */
|
|
|
|
len = 12;
|
|
|
|
value = *str++ & 0x3f;
|
|
|
|
while (--len)
|
|
|
|
value = (value << 8) + (unsigned char) *str++;
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef CHECK_FOR_OVERFLOW
|
|
|
|
/* Can be optimized to eat 32-bit chunks */
|
|
|
|
char c = *str++ & 0x3f;
|
|
|
|
len = 12;
|
|
|
|
while (1) {
|
|
|
|
if (c)
|
|
|
|
bb_error_msg_and_die("overflow in base-256 encoded file size");
|
2009-04-01 16:54:04 +05:30
|
|
|
if (--len == sizeof(off_t))
|
2008-12-12 07:44:35 +05:30
|
|
|
break;
|
|
|
|
c = *str++;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
str += (12 - sizeof(off_t));
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Now str points to sizeof(off_t) least significant bytes.
|
|
|
|
*
|
|
|
|
* Example of tar file with 8914993153 (0x213600001) byte file.
|
|
|
|
* Field starts at offset 7c:
|
|
|
|
* 00070 30 30 30 00 30 30 30 30 30 30 30 00 80 00 00 00 |000.0000000.....|
|
|
|
|
* 00080 00 00 00 02 13 60 00 01 31 31 31 32 30 33 33 36 |.....`..11120336|
|
|
|
|
*
|
|
|
|
* str is at offset 80 or 84 now (64-bit or 32-bit off_t).
|
|
|
|
* We (ab)use the fact that value happens to be aligned,
|
|
|
|
* and fetch it in one go:
|
|
|
|
*/
|
|
|
|
if (sizeof(off_t) == 8) {
|
2010-02-04 19:30:15 +05:30
|
|
|
value = *(aliased_off_t*)str;
|
2008-12-12 07:44:35 +05:30
|
|
|
value = SWAP_BE64(value);
|
|
|
|
} else if (sizeof(off_t) == 4) {
|
2010-02-04 19:30:15 +05:30
|
|
|
value = *(aliased_off_t*)str;
|
2008-12-12 07:44:35 +05:30
|
|
|
value = SWAP_BE32(value);
|
|
|
|
} else {
|
|
|
|
value = 0;
|
|
|
|
len = sizeof(off_t);
|
|
|
|
while (--len)
|
|
|
|
value = (value << 8) + (unsigned char) *str++;
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2006-11-24 20:21:01 +05:30
|
|
|
/* NB: _DESTROYS_ str[len] character! */
|
|
|
|
static unsigned long long getOctal(char *str, int len)
|
|
|
|
{
|
|
|
|
unsigned long long v;
|
2008-06-28 10:34:09 +05:30
|
|
|
/* NB: leading spaces are allowed. Using strtoull to handle that.
|
2009-09-23 20:46:37 +05:30
|
|
|
* The downside is that we accept e.g. "-123" too :(
|
2008-06-28 10:34:09 +05:30
|
|
|
*/
|
2006-11-24 20:21:01 +05:30
|
|
|
str[len] = '\0';
|
|
|
|
v = strtoull(str, &str, 8);
|
2009-09-23 20:46:37 +05:30
|
|
|
/* std: "Each numeric field is terminated by one or more
|
|
|
|
* <space> or NUL characters". We must support ' '! */
|
|
|
|
if (*str != '\0' && *str != ' ')
|
2006-11-24 20:21:01 +05:30
|
|
|
bb_error_msg_and_die("corrupted octal value in tar header");
|
|
|
|
return v;
|
|
|
|
}
|
2006-11-26 07:16:59 +05:30
|
|
|
#define GET_OCTAL(a) getOctal((a), sizeof(a))
|
2006-11-24 20:21:01 +05:30
|
|
|
|
|
|
|
void BUG_tar_header_size(void);
|
2008-06-27 08:22:20 +05:30
|
|
|
char FAST_FUNC get_header_tar(archive_handle_t *archive_handle)
|
2001-10-25 19:48:08 +05:30
|
|
|
{
|
2002-09-25 08:17:48 +05:30
|
|
|
file_header_t *file_header = archive_handle->file_header;
|
2006-11-24 20:21:01 +05:30
|
|
|
struct {
|
2003-09-12 12:01:28 +05:30
|
|
|
/* ustar header, Posix 1003.1 */
|
2006-11-24 20:21:01 +05:30
|
|
|
char name[100]; /* 0-99 */
|
|
|
|
char mode[8]; /* 100-107 */
|
|
|
|
char uid[8]; /* 108-115 */
|
|
|
|
char gid[8]; /* 116-123 */
|
|
|
|
char size[12]; /* 124-135 */
|
|
|
|
char mtime[12]; /* 136-147 */
|
|
|
|
char chksum[8]; /* 148-155 */
|
|
|
|
char typeflag; /* 156-156 */
|
|
|
|
char linkname[100]; /* 157-256 */
|
2007-11-17 02:05:30 +05:30
|
|
|
/* POSIX: "ustar" NUL "00" */
|
|
|
|
/* GNU tar: "ustar " NUL */
|
2007-11-18 00:41:05 +05:30
|
|
|
/* Normally it's defined as magic[6] followed by
|
2008-06-28 10:34:09 +05:30
|
|
|
* version[2], but we put them together to simplify code
|
2007-11-18 00:41:05 +05:30
|
|
|
*/
|
2007-11-17 02:05:30 +05:30
|
|
|
char magic[8]; /* 257-264 */
|
2006-11-24 20:21:01 +05:30
|
|
|
char uname[32]; /* 265-296 */
|
|
|
|
char gname[32]; /* 297-328 */
|
|
|
|
char devmajor[8]; /* 329-336 */
|
|
|
|
char devminor[8]; /* 337-344 */
|
|
|
|
char prefix[155]; /* 345-499 */
|
|
|
|
char padding[12]; /* 500-512 */
|
2001-10-25 19:48:08 +05:30
|
|
|
} tar;
|
2006-11-24 20:21:01 +05:30
|
|
|
char *cp;
|
2007-06-21 18:11:59 +05:30
|
|
|
int i, sum_u, sum;
|
|
|
|
#if ENABLE_FEATURE_TAR_OLDSUN_COMPATIBILITY
|
|
|
|
int sum_s;
|
|
|
|
#endif
|
2006-11-24 22:51:44 +05:30
|
|
|
int parse_names;
|
2006-11-24 20:21:01 +05:30
|
|
|
|
2008-06-28 10:34:09 +05:30
|
|
|
/* Our "private data" */
|
|
|
|
#if ENABLE_FEATURE_TAR_GNU_EXTENSIONS
|
2010-01-06 15:23:17 +05:30
|
|
|
# define p_longname (archive_handle->tar__longname)
|
|
|
|
# define p_linkname (archive_handle->tar__linkname)
|
2008-06-28 10:34:09 +05:30
|
|
|
#else
|
2010-01-06 15:23:17 +05:30
|
|
|
# define p_longname 0
|
|
|
|
# define p_linkname 0
|
2008-06-28 10:34:09 +05:30
|
|
|
#endif
|
|
|
|
|
2006-11-24 20:21:01 +05:30
|
|
|
if (sizeof(tar) != 512)
|
|
|
|
BUG_tar_header_size();
|
2001-10-25 19:48:08 +05:30
|
|
|
|
2006-12-26 23:47:42 +05:30
|
|
|
#if ENABLE_FEATURE_TAR_GNU_EXTENSIONS
|
|
|
|
again:
|
|
|
|
#endif
|
2002-09-25 08:17:48 +05:30
|
|
|
/* Align header */
|
2002-11-03 19:35:15 +05:30
|
|
|
data_align(archive_handle, 512);
|
2002-09-25 08:17:48 +05:30
|
|
|
|
2006-11-26 07:16:59 +05:30
|
|
|
again_after_align:
|
|
|
|
|
2009-03-13 19:56:44 +05:30
|
|
|
#if ENABLE_DESKTOP || ENABLE_FEATURE_TAR_AUTODETECT
|
2008-07-11 04:36:00 +05:30
|
|
|
/* to prevent misdetection of bz2 sig */
|
2010-02-04 19:30:15 +05:30
|
|
|
*(aliased_uint32_t*)&tar = 0;
|
2008-02-13 23:22:42 +05:30
|
|
|
i = full_read(archive_handle->src_fd, &tar, 512);
|
2008-06-28 10:34:09 +05:30
|
|
|
/* If GNU tar sees EOF in above read, it says:
|
2008-02-13 23:22:42 +05:30
|
|
|
* "tar: A lone zero block at N", where N = kilobyte
|
|
|
|
* where EOF was met (not EOF block, actual EOF!),
|
2008-07-11 04:36:00 +05:30
|
|
|
* and exits with EXIT_SUCCESS.
|
2008-05-19 14:59:47 +05:30
|
|
|
* We will mimic exit(EXIT_SUCCESS), although we will not mimic
|
2008-02-14 14:22:30 +05:30
|
|
|
* the message and we don't check whether we indeed
|
|
|
|
* saw zero block directly before this. */
|
2008-07-11 04:36:00 +05:30
|
|
|
if (i == 0) {
|
2008-02-13 23:22:42 +05:30
|
|
|
xfunc_error_retval = 0;
|
2008-07-11 04:36:00 +05:30
|
|
|
short_read:
|
2008-02-13 23:22:42 +05:30
|
|
|
bb_error_msg_and_die("short read");
|
2008-07-11 04:36:00 +05:30
|
|
|
}
|
|
|
|
if (i != 512) {
|
2009-04-21 16:39:40 +05:30
|
|
|
IF_FEATURE_TAR_AUTODETECT(goto autodetect;)
|
2008-07-11 04:36:00 +05:30
|
|
|
goto short_read;
|
|
|
|
}
|
|
|
|
|
2008-02-14 14:22:30 +05:30
|
|
|
#else
|
2008-07-11 04:36:00 +05:30
|
|
|
i = 512;
|
|
|
|
xread(archive_handle->src_fd, &tar, i);
|
2008-02-14 14:22:30 +05:30
|
|
|
#endif
|
2008-07-11 04:36:00 +05:30
|
|
|
archive_handle->offset += i;
|
2001-10-25 19:48:08 +05:30
|
|
|
|
2002-09-25 08:17:48 +05:30
|
|
|
/* If there is no filename its an empty header */
|
2008-04-29 09:42:58 +05:30
|
|
|
if (tar.name[0] == 0 && tar.prefix[0] == 0) {
|
2010-01-06 15:23:17 +05:30
|
|
|
if (archive_handle->tar__end) {
|
2008-06-28 10:34:09 +05:30
|
|
|
/* Second consecutive empty header - end of archive.
|
2005-07-21 00:54:13 +05:30
|
|
|
* Read until the end to empty the pipe from gz or bz2
|
|
|
|
*/
|
2006-11-24 20:21:01 +05:30
|
|
|
while (full_read(archive_handle->src_fd, &tar, 512) == 512)
|
2008-02-19 16:56:28 +05:30
|
|
|
continue;
|
2006-10-08 18:19:22 +05:30
|
|
|
return EXIT_FAILURE;
|
2005-07-21 00:54:13 +05:30
|
|
|
}
|
2010-01-06 15:23:17 +05:30
|
|
|
archive_handle->tar__end = 1;
|
2006-10-08 18:19:22 +05:30
|
|
|
return EXIT_SUCCESS;
|
2001-10-25 19:48:08 +05:30
|
|
|
}
|
2010-01-06 15:23:17 +05:30
|
|
|
archive_handle->tar__end = 0;
|
2001-10-25 19:48:08 +05:30
|
|
|
|
2008-02-19 16:56:28 +05:30
|
|
|
/* Check header has valid magic, "ustar" is for the proper tar,
|
|
|
|
* five NULs are for the old tar format */
|
|
|
|
if (strncmp(tar.magic, "ustar", 5) != 0
|
|
|
|
&& (!ENABLE_FEATURE_TAR_OLDGNU_COMPATIBILITY
|
|
|
|
|| memcmp(tar.magic, "\0\0\0\0", 5) != 0)
|
|
|
|
) {
|
|
|
|
#if ENABLE_FEATURE_TAR_AUTODETECT
|
2008-06-27 08:22:20 +05:30
|
|
|
char FAST_FUNC (*get_header_ptr)(archive_handle_t *);
|
2008-02-19 16:56:28 +05:30
|
|
|
|
2009-03-13 19:56:44 +05:30
|
|
|
autodetect:
|
2008-02-19 16:56:28 +05:30
|
|
|
/* tar gz/bz autodetect: check for gz/bz2 magic.
|
2008-06-28 10:34:09 +05:30
|
|
|
* If we see the magic, and it is the very first block,
|
2008-02-19 16:56:28 +05:30
|
|
|
* we can switch to get_header_tar_gz/bz2/lzma().
|
2008-05-13 07:57:31 +05:30
|
|
|
* Needs seekable fd. I wish recv(MSG_PEEK) works
|
2008-02-19 16:56:28 +05:30
|
|
|
* on any fd... */
|
2008-08-05 18:40:34 +05:30
|
|
|
#if ENABLE_FEATURE_SEAMLESS_GZ
|
2008-05-13 07:57:31 +05:30
|
|
|
if (tar.name[0] == 0x1f && tar.name[1] == (char)0x8b) { /* gzip */
|
2008-02-19 16:56:28 +05:30
|
|
|
get_header_ptr = get_header_tar_gz;
|
|
|
|
} else
|
|
|
|
#endif
|
2008-08-05 18:40:34 +05:30
|
|
|
#if ENABLE_FEATURE_SEAMLESS_BZ2
|
2008-02-19 16:56:28 +05:30
|
|
|
if (tar.name[0] == 'B' && tar.name[1] == 'Z'
|
|
|
|
&& tar.name[2] == 'h' && isdigit(tar.name[3])
|
|
|
|
) { /* bzip2 */
|
|
|
|
get_header_ptr = get_header_tar_bz2;
|
|
|
|
} else
|
2002-08-13 10:36:43 +05:30
|
|
|
#endif
|
2008-02-19 16:56:28 +05:30
|
|
|
goto err;
|
2008-06-28 10:34:09 +05:30
|
|
|
/* Two different causes for lseek() != 0:
|
|
|
|
* unseekable fd (would like to support that too, but...),
|
|
|
|
* or not first block (false positive, it's not .gz/.bz2!) */
|
2008-07-11 04:36:00 +05:30
|
|
|
if (lseek(archive_handle->src_fd, -i, SEEK_CUR) != 0)
|
2008-02-19 16:56:28 +05:30
|
|
|
goto err;
|
|
|
|
while (get_header_ptr(archive_handle) == EXIT_SUCCESS)
|
|
|
|
continue;
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
err:
|
|
|
|
#endif /* FEATURE_TAR_AUTODETECT */
|
|
|
|
bb_error_msg_and_die("invalid tar magic");
|
2001-10-25 19:48:08 +05:30
|
|
|
}
|
2007-03-04 01:36:59 +05:30
|
|
|
|
|
|
|
/* Do checksum on headers.
|
|
|
|
* POSIX says that checksum is done on unsigned bytes, but
|
2007-03-04 23:39:50 +05:30
|
|
|
* Sun and HP-UX gets it wrong... more details in
|
2007-03-04 01:36:59 +05:30
|
|
|
* GNU tar source. */
|
2007-06-21 18:11:59 +05:30
|
|
|
#if ENABLE_FEATURE_TAR_OLDSUN_COMPATIBILITY
|
|
|
|
sum_s = ' ' * sizeof(tar.chksum);
|
|
|
|
#endif
|
|
|
|
sum_u = ' ' * sizeof(tar.chksum);
|
2007-07-21 20:38:09 +05:30
|
|
|
for (i = 0; i < 148; i++) {
|
2007-03-04 01:36:59 +05:30
|
|
|
sum_u += ((unsigned char*)&tar)[i];
|
2007-06-21 18:11:59 +05:30
|
|
|
#if ENABLE_FEATURE_TAR_OLDSUN_COMPATIBILITY
|
2007-03-04 01:36:59 +05:30
|
|
|
sum_s += ((signed char*)&tar)[i];
|
2007-06-21 18:11:59 +05:30
|
|
|
#endif
|
2001-10-25 19:48:08 +05:30
|
|
|
}
|
2007-07-21 20:38:09 +05:30
|
|
|
for (i = 156; i < 512; i++) {
|
2007-03-04 01:36:59 +05:30
|
|
|
sum_u += ((unsigned char*)&tar)[i];
|
2007-06-21 18:11:59 +05:30
|
|
|
#if ENABLE_FEATURE_TAR_OLDSUN_COMPATIBILITY
|
2007-03-04 01:36:59 +05:30
|
|
|
sum_s += ((signed char*)&tar)[i];
|
2007-06-21 18:11:59 +05:30
|
|
|
#endif
|
2001-10-25 19:48:08 +05:30
|
|
|
}
|
2006-11-24 20:21:01 +05:30
|
|
|
/* This field does not need special treatment (getOctal) */
|
2009-09-23 20:46:37 +05:30
|
|
|
{
|
|
|
|
char *endp; /* gcc likes temp var for &endp */
|
|
|
|
sum = strtoul(tar.chksum, &endp, 8);
|
|
|
|
if ((*endp != '\0' && *endp != ' ')
|
|
|
|
|| (sum_u != sum IF_FEATURE_TAR_OLDSUN_COMPATIBILITY(&& sum_s != sum))
|
|
|
|
) {
|
|
|
|
bb_error_msg_and_die("invalid tar header checksum");
|
|
|
|
}
|
|
|
|
}
|
2010-01-04 04:11:11 +05:30
|
|
|
/* don't use xstrtoul, tar.chksum may have leading spaces */
|
|
|
|
sum = strtoul(tar.chksum, NULL, 8);
|
2009-04-21 16:39:40 +05:30
|
|
|
if (sum_u != sum IF_FEATURE_TAR_OLDSUN_COMPATIBILITY(&& sum_s != sum)) {
|
2006-10-08 18:19:22 +05:30
|
|
|
bb_error_msg_and_die("invalid tar header checksum");
|
2001-10-25 19:48:08 +05:30
|
|
|
}
|
|
|
|
|
2006-11-26 07:16:59 +05:30
|
|
|
/* 0 is reserved for high perf file, treat as normal file */
|
|
|
|
if (!tar.typeflag) tar.typeflag = '0';
|
|
|
|
parse_names = (tar.typeflag >= '0' && tar.typeflag <= '7');
|
2006-11-24 22:51:44 +05:30
|
|
|
|
2006-11-24 20:21:01 +05:30
|
|
|
/* getOctal trashes subsequent field, therefore we call it
|
|
|
|
* on fields in reverse order */
|
|
|
|
if (tar.devmajor[0]) {
|
2008-04-29 09:24:16 +05:30
|
|
|
char t = tar.prefix[0];
|
|
|
|
/* we trash prefix[0] here, but we DO need it later! */
|
2006-11-24 20:21:01 +05:30
|
|
|
unsigned minor = GET_OCTAL(tar.devminor);
|
|
|
|
unsigned major = GET_OCTAL(tar.devmajor);
|
|
|
|
file_header->device = makedev(major, minor);
|
2008-04-29 09:24:16 +05:30
|
|
|
tar.prefix[0] = t;
|
2006-10-11 02:30:47 +05:30
|
|
|
}
|
2007-06-20 20:19:47 +05:30
|
|
|
file_header->link_target = NULL;
|
2008-06-28 10:34:09 +05:30
|
|
|
if (!p_linkname && parse_names && tar.linkname[0]) {
|
2008-04-15 06:47:50 +05:30
|
|
|
file_header->link_target = xstrndup(tar.linkname, sizeof(tar.linkname));
|
2007-06-20 20:19:47 +05:30
|
|
|
/* FIXME: what if we have non-link object with link_target? */
|
|
|
|
/* Will link_target be free()ed? */
|
2006-11-24 22:51:44 +05:30
|
|
|
}
|
2008-02-15 02:07:54 +05:30
|
|
|
#if ENABLE_FEATURE_TAR_UNAME_GNAME
|
2010-01-06 15:23:17 +05:30
|
|
|
file_header->tar__uname = tar.uname[0] ? xstrndup(tar.uname, sizeof(tar.uname)) : NULL;
|
|
|
|
file_header->tar__gname = tar.gname[0] ? xstrndup(tar.gname, sizeof(tar.gname)) : NULL;
|
2008-02-15 02:07:54 +05:30
|
|
|
#endif
|
2009-07-08 22:28:09 +05:30
|
|
|
/* mtime: rudimentally handle GNU tar's "base256 encoding"
|
|
|
|
* People report tarballs with NEGATIVE unix times encoded that way */
|
|
|
|
file_header->mtime = (tar.mtime[0] & 0x80) /* base256? */
|
|
|
|
? 0 /* bogus */
|
|
|
|
: GET_OCTAL(tar.mtime);
|
|
|
|
/* size: handle GNU tar's "base256 encoding" */
|
|
|
|
file_header->size = (tar.size[0] & 0xc0) == 0x80 /* positive base256? */
|
2008-12-12 07:44:35 +05:30
|
|
|
? getBase256_len12(tar.size)
|
|
|
|
: GET_OCTAL(tar.size);
|
2006-11-24 20:21:01 +05:30
|
|
|
file_header->gid = GET_OCTAL(tar.gid);
|
|
|
|
file_header->uid = GET_OCTAL(tar.uid);
|
2004-02-20 08:04:42 +05:30
|
|
|
/* Set bits 0-11 of the files mode */
|
2006-11-24 20:21:01 +05:30
|
|
|
file_header->mode = 07777 & GET_OCTAL(tar.mode);
|
2004-02-20 08:04:42 +05:30
|
|
|
|
2006-11-26 07:16:59 +05:30
|
|
|
file_header->name = NULL;
|
2008-06-28 10:34:09 +05:30
|
|
|
if (!p_longname && parse_names) {
|
2006-11-24 22:51:44 +05:30
|
|
|
/* we trash mode[0] here, it's ok */
|
2008-04-15 06:47:50 +05:30
|
|
|
//tar.name[sizeof(tar.name)] = '\0'; - gcc 4.3.0 would complain
|
|
|
|
tar.mode[0] = '\0';
|
2006-11-26 05:17:32 +05:30
|
|
|
if (tar.prefix[0]) {
|
|
|
|
/* and padding[0] */
|
2008-04-15 06:47:50 +05:30
|
|
|
//tar.prefix[sizeof(tar.prefix)] = '\0'; - gcc 4.3.0 would complain
|
|
|
|
tar.padding[0] = '\0';
|
2006-11-24 20:23:18 +05:30
|
|
|
file_header->name = concat_path_file(tar.prefix, tar.name);
|
2006-11-26 05:17:32 +05:30
|
|
|
} else
|
2006-11-24 20:23:18 +05:30
|
|
|
file_header->name = xstrdup(tar.name);
|
|
|
|
}
|
|
|
|
|
2004-02-20 08:04:42 +05:30
|
|
|
/* Set bits 12-15 of the files mode */
|
2006-11-24 22:51:44 +05:30
|
|
|
/* (typeflag was not trashed because chksum does not use getOctal) */
|
2006-11-24 20:21:01 +05:30
|
|
|
switch (tar.typeflag) {
|
2004-02-19 14:18:30 +05:30
|
|
|
/* busybox identifies hard links as being regular files with 0 size and a link name */
|
|
|
|
case '1':
|
2004-02-20 08:04:42 +05:30
|
|
|
file_header->mode |= S_IFREG;
|
2004-02-19 14:18:30 +05:30
|
|
|
break;
|
2004-02-20 07:55:18 +05:30
|
|
|
case '7':
|
2006-11-26 07:16:59 +05:30
|
|
|
/* case 0: */
|
2003-01-28 07:15:48 +05:30
|
|
|
case '0':
|
2006-11-24 22:51:44 +05:30
|
|
|
#if ENABLE_FEATURE_TAR_OLDGNU_COMPATIBILITY
|
2003-09-09 23:11:03 +05:30
|
|
|
if (last_char_is(file_header->name, '/')) {
|
2008-07-20 22:40:43 +05:30
|
|
|
goto set_dir;
|
|
|
|
}
|
2004-02-20 07:55:18 +05:30
|
|
|
#endif
|
2006-11-24 20:21:01 +05:30
|
|
|
file_header->mode |= S_IFREG;
|
2002-08-22 23:17:09 +05:30
|
|
|
break;
|
2003-01-28 07:15:48 +05:30
|
|
|
case '2':
|
2002-09-25 08:17:48 +05:30
|
|
|
file_header->mode |= S_IFLNK;
|
2008-07-20 22:40:43 +05:30
|
|
|
/* have seen tarballs with size field containing
|
|
|
|
* the size of the link target's name */
|
|
|
|
size0:
|
|
|
|
file_header->size = 0;
|
2002-08-22 23:17:09 +05:30
|
|
|
break;
|
2003-01-28 07:15:48 +05:30
|
|
|
case '3':
|
2002-09-25 08:17:48 +05:30
|
|
|
file_header->mode |= S_IFCHR;
|
2008-07-20 22:40:43 +05:30
|
|
|
goto size0; /* paranoia */
|
2003-01-28 07:15:48 +05:30
|
|
|
case '4':
|
2002-09-25 08:17:48 +05:30
|
|
|
file_header->mode |= S_IFBLK;
|
2008-07-20 22:40:43 +05:30
|
|
|
goto size0;
|
2003-01-28 07:15:48 +05:30
|
|
|
case '5':
|
2009-04-21 16:39:40 +05:30
|
|
|
IF_FEATURE_TAR_OLDGNU_COMPATIBILITY(set_dir:)
|
2002-09-25 08:17:48 +05:30
|
|
|
file_header->mode |= S_IFDIR;
|
2008-07-20 22:40:43 +05:30
|
|
|
goto size0;
|
2003-01-28 07:15:48 +05:30
|
|
|
case '6':
|
2002-09-25 08:17:48 +05:30
|
|
|
file_header->mode |= S_IFIFO;
|
2008-07-20 22:40:43 +05:30
|
|
|
goto size0;
|
2006-11-24 22:51:44 +05:30
|
|
|
#if ENABLE_FEATURE_TAR_GNU_EXTENSIONS
|
2006-11-24 20:21:01 +05:30
|
|
|
case 'L':
|
2006-11-26 07:16:59 +05:30
|
|
|
/* free: paranoia: tar with several consecutive longnames */
|
2008-06-28 10:34:09 +05:30
|
|
|
free(p_longname);
|
2006-11-26 07:16:59 +05:30
|
|
|
/* For paranoia reasons we allocate extra NUL char */
|
2008-06-28 10:34:09 +05:30
|
|
|
p_longname = xzalloc(file_header->size + 1);
|
2006-11-26 07:16:59 +05:30
|
|
|
/* We read ASCIZ string, including NUL */
|
2008-06-28 10:34:09 +05:30
|
|
|
xread(archive_handle->src_fd, p_longname, file_header->size);
|
2006-11-24 20:21:01 +05:30
|
|
|
archive_handle->offset += file_header->size;
|
2006-11-26 07:16:59 +05:30
|
|
|
/* return get_header_tar(archive_handle); */
|
|
|
|
/* gcc 4.1.1 didn't optimize it into jump */
|
|
|
|
/* so we will do it ourself, this also saves stack */
|
|
|
|
goto again;
|
2006-11-24 20:21:01 +05:30
|
|
|
case 'K':
|
2008-06-28 10:34:09 +05:30
|
|
|
free(p_linkname);
|
|
|
|
p_linkname = xzalloc(file_header->size + 1);
|
|
|
|
xread(archive_handle->src_fd, p_linkname, file_header->size);
|
2006-11-24 20:21:01 +05:30
|
|
|
archive_handle->offset += file_header->size;
|
2006-11-26 07:16:59 +05:30
|
|
|
/* return get_header_tar(archive_handle); */
|
|
|
|
goto again;
|
2004-02-19 14:18:30 +05:30
|
|
|
case 'D': /* GNU dump dir */
|
2006-11-24 20:21:01 +05:30
|
|
|
case 'M': /* Continuation of multi volume archive */
|
2004-02-19 14:18:30 +05:30
|
|
|
case 'N': /* Old GNU for names > 100 characters */
|
|
|
|
case 'S': /* Sparse file */
|
|
|
|
case 'V': /* Volume header */
|
2004-02-17 17:25:06 +05:30
|
|
|
#endif
|
2006-07-07 02:00:19 +05:30
|
|
|
case 'g': /* pax global header */
|
2006-11-26 07:16:59 +05:30
|
|
|
case 'x': { /* pax extended header */
|
|
|
|
off_t sz;
|
|
|
|
bb_error_msg("warning: skipping header '%c'", tar.typeflag);
|
|
|
|
sz = (file_header->size + 511) & ~(off_t)511;
|
|
|
|
archive_handle->offset += sz;
|
|
|
|
sz >>= 9; /* sz /= 512 but w/o contortions for signed div */
|
|
|
|
while (sz--)
|
|
|
|
xread(archive_handle->src_fd, &tar, 512);
|
|
|
|
/* return get_header_tar(archive_handle); */
|
|
|
|
goto again_after_align;
|
|
|
|
}
|
2004-02-19 14:18:30 +05:30
|
|
|
default:
|
2006-11-26 07:16:59 +05:30
|
|
|
bb_error_msg_and_die("unknown typeflag: 0x%x", tar.typeflag);
|
2002-08-13 10:36:43 +05:30
|
|
|
}
|
2003-08-14 08:25:15 +05:30
|
|
|
|
2006-11-24 22:51:44 +05:30
|
|
|
#if ENABLE_FEATURE_TAR_GNU_EXTENSIONS
|
2008-06-28 10:34:09 +05:30
|
|
|
if (p_longname) {
|
|
|
|
file_header->name = p_longname;
|
|
|
|
p_longname = NULL;
|
2006-11-24 22:51:44 +05:30
|
|
|
}
|
2008-06-28 10:34:09 +05:30
|
|
|
if (p_linkname) {
|
|
|
|
file_header->link_target = p_linkname;
|
|
|
|
p_linkname = NULL;
|
2006-11-24 22:51:44 +05:30
|
|
|
}
|
|
|
|
#endif
|
2008-07-11 04:36:00 +05:30
|
|
|
if (strncmp(file_header->name, "/../"+1, 3) == 0
|
2006-11-26 21:12:03 +05:30
|
|
|
|| strstr(file_header->name, "/../")
|
|
|
|
) {
|
|
|
|
bb_error_msg_and_die("name with '..' encountered: '%s'",
|
|
|
|
file_header->name);
|
|
|
|
}
|
2006-11-24 22:51:44 +05:30
|
|
|
|
2006-11-24 20:21:01 +05:30
|
|
|
/* Strip trailing '/' in directories */
|
2006-12-22 05:51:07 +05:30
|
|
|
/* Must be done after mode is set as '/' is used to check if it's a directory */
|
2006-11-24 20:21:01 +05:30
|
|
|
cp = last_char_is(file_header->name, '/');
|
|
|
|
|
2002-11-05 05:17:31 +05:30
|
|
|
if (archive_handle->filter(archive_handle) == EXIT_SUCCESS) {
|
2008-07-11 04:36:00 +05:30
|
|
|
archive_handle->action_header(/*archive_handle->*/ file_header);
|
2006-11-24 20:23:18 +05:30
|
|
|
/* Note that we kill the '/' only after action_header() */
|
|
|
|
/* (like GNU tar 1.15.1: verbose mode outputs "dir/dir/") */
|
|
|
|
if (cp) *cp = '\0';
|
2008-06-28 10:34:09 +05:30
|
|
|
archive_handle->ah_flags |= ARCHIVE_EXTRACT_QUIET;
|
2002-09-25 08:17:48 +05:30
|
|
|
archive_handle->action_data(archive_handle);
|
2006-05-27 05:14:51 +05:30
|
|
|
llist_add_to(&(archive_handle->passed), file_header->name);
|
2002-09-25 08:17:48 +05:30
|
|
|
} else {
|
2004-03-15 13:59:22 +05:30
|
|
|
data_skip(archive_handle);
|
2006-11-26 07:16:59 +05:30
|
|
|
free(file_header->name);
|
2002-09-25 08:17:48 +05:30
|
|
|
}
|
|
|
|
archive_handle->offset += file_header->size;
|
|
|
|
|
2007-06-20 20:19:47 +05:30
|
|
|
free(file_header->link_target);
|
2008-07-12 14:50:44 +05:30
|
|
|
/* Do not free(file_header->name)! (why?) */
|
2008-02-15 02:07:54 +05:30
|
|
|
#if ENABLE_FEATURE_TAR_UNAME_GNAME
|
2010-01-06 15:23:17 +05:30
|
|
|
free(file_header->tar__uname);
|
|
|
|
free(file_header->tar__gname);
|
2008-02-15 02:07:54 +05:30
|
|
|
#endif
|
2006-10-08 18:19:22 +05:30
|
|
|
return EXIT_SUCCESS;
|
2002-07-11 16:41:56 +05:30
|
|
|
}
|