2001-10-25 19:48:08 +05:30
|
|
|
/*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Library General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "unarchive.h"
|
|
|
|
#include "libbb.h"
|
|
|
|
|
2003-01-28 07:15:48 +05:30
|
|
|
#ifdef CONFIG_FEATURE_TAR_GNU_EXTENSIONS
|
2002-11-23 16:14:47 +05:30
|
|
|
static char *longname = NULL;
|
|
|
|
static char *linkname = NULL;
|
|
|
|
#endif
|
|
|
|
|
2002-09-25 08:17:48 +05:30
|
|
|
extern char 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;
|
2001-10-25 19:48:08 +05:30
|
|
|
union {
|
2003-09-12 12:01:28 +05:30
|
|
|
/* ustar header, Posix 1003.1 */
|
2001-10-25 19:48:08 +05:30
|
|
|
unsigned char raw[512];
|
|
|
|
struct {
|
2002-08-22 23:17:09 +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 */
|
2001-10-25 19:48:08 +05:30
|
|
|
char linkname[100]; /* 157-256 */
|
2002-08-22 23:17:09 +05:30
|
|
|
char magic[6]; /* 257-262 */
|
2001-10-25 19:48:08 +05:30
|
|
|
char version[2]; /* 263-264 */
|
2002-08-22 23:17:09 +05:30
|
|
|
char uname[32]; /* 265-296 */
|
|
|
|
char gname[32]; /* 297-328 */
|
2001-10-25 19:48:08 +05:30
|
|
|
char devmajor[8]; /* 329-336 */
|
|
|
|
char devminor[8]; /* 337-344 */
|
|
|
|
char prefix[155]; /* 345-499 */
|
|
|
|
char padding[12]; /* 500-512 */
|
|
|
|
} formated;
|
|
|
|
} tar;
|
|
|
|
long sum = 0;
|
|
|
|
long i;
|
|
|
|
|
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
|
|
|
|
2003-11-22 03:54:57 +05:30
|
|
|
if (bb_full_read(archive_handle->src_fd, tar.raw, 512) != 512) {
|
2002-11-05 08:26:57 +05:30
|
|
|
/* Assume end of file */
|
2002-09-25 08:17:48 +05:30
|
|
|
return(EXIT_FAILURE);
|
2001-10-25 19:48:08 +05:30
|
|
|
}
|
2002-09-25 08:17:48 +05:30
|
|
|
archive_handle->offset += 512;
|
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 */
|
|
|
|
if (tar.formated.name[0] == 0) {
|
2003-11-14 18:27:14 +05:30
|
|
|
return(EXIT_SUCCESS);
|
2001-10-25 19:48:08 +05:30
|
|
|
}
|
|
|
|
|
2002-08-13 10:36:43 +05:30
|
|
|
/* Check header has valid magic, "ustar" is for the proper tar
|
|
|
|
* 0's are for the old tar format
|
2001-10-25 19:48:08 +05:30
|
|
|
*/
|
|
|
|
if (strncmp(tar.formated.magic, "ustar", 5) != 0) {
|
2003-01-28 07:15:48 +05:30
|
|
|
#ifdef CONFIG_FEATURE_TAR_OLDGNU_COMPATABILITY
|
2002-08-13 10:36:43 +05:30
|
|
|
if (strncmp(tar.formated.magic, "\0\0\0\0\0", 5) != 0)
|
|
|
|
#endif
|
2003-03-19 14:43:01 +05:30
|
|
|
bb_error_msg_and_die("Invalid tar magic");
|
2001-10-25 19:48:08 +05:30
|
|
|
}
|
|
|
|
/* Do checksum on headers */
|
2002-09-25 08:17:48 +05:30
|
|
|
for (i = 0; i < 148 ; i++) {
|
2001-10-25 19:48:08 +05:30
|
|
|
sum += tar.raw[i];
|
|
|
|
}
|
|
|
|
sum += ' ' * 8;
|
2002-09-25 08:17:48 +05:30
|
|
|
for (i = 156; i < 512 ; i++) {
|
2001-10-25 19:48:08 +05:30
|
|
|
sum += tar.raw[i];
|
|
|
|
}
|
|
|
|
if (sum != strtol(tar.formated.chksum, NULL, 8)) {
|
2003-03-19 14:43:01 +05:30
|
|
|
bb_error_msg("Invalid tar header checksum");
|
2002-09-25 08:17:48 +05:30
|
|
|
return(EXIT_FAILURE);
|
2001-10-25 19:48:08 +05:30
|
|
|
}
|
|
|
|
|
2003-01-28 07:15:48 +05:30
|
|
|
#ifdef CONFIG_FEATURE_TAR_GNU_EXTENSIONS
|
2002-11-23 16:14:47 +05:30
|
|
|
if (longname) {
|
|
|
|
file_header->name = longname;
|
|
|
|
longname = NULL;
|
|
|
|
}
|
|
|
|
else if (linkname) {
|
|
|
|
file_header->name = linkname;
|
|
|
|
linkname = NULL;
|
|
|
|
} else
|
|
|
|
#endif
|
2002-08-22 17:20:31 +05:30
|
|
|
if (tar.formated.prefix[0] == 0) {
|
2002-09-25 08:17:48 +05:30
|
|
|
file_header->name = strdup(tar.formated.name);
|
2002-08-22 17:20:31 +05:30
|
|
|
} else {
|
2002-09-25 08:17:48 +05:30
|
|
|
file_header->name = concat_path_file(tar.formated.prefix, tar.formated.name);
|
2002-08-22 17:20:31 +05:30
|
|
|
}
|
2002-10-19 11:49:22 +05:30
|
|
|
|
2003-11-20 16:17:06 +05:30
|
|
|
{ /* Strip trailing '/' in directories */
|
|
|
|
char *tmp = last_char_is(file_header->name, '/');
|
|
|
|
if (tmp) {
|
|
|
|
*tmp = '\0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-09-25 08:17:48 +05:30
|
|
|
file_header->mode = strtol(tar.formated.mode, NULL, 8);
|
|
|
|
file_header->uid = strtol(tar.formated.uid, NULL, 8);
|
|
|
|
file_header->gid = strtol(tar.formated.gid, NULL, 8);
|
|
|
|
file_header->size = strtol(tar.formated.size, NULL, 8);
|
|
|
|
file_header->mtime = strtol(tar.formated.mtime, NULL, 8);
|
|
|
|
file_header->link_name = (tar.formated.linkname[0] != '\0') ?
|
2003-03-19 14:43:01 +05:30
|
|
|
bb_xstrdup(tar.formated.linkname) : NULL;
|
2002-09-25 08:17:48 +05:30
|
|
|
file_header->device = (dev_t) ((strtol(tar.formated.devmajor, NULL, 8) << 8) +
|
2002-09-15 22:24:49 +05:30
|
|
|
strtol(tar.formated.devminor, NULL, 8));
|
|
|
|
|
2002-09-25 08:17:48 +05:30
|
|
|
/* Fix mode, used by the old format */
|
2002-08-13 10:36:43 +05:30
|
|
|
switch (tar.formated.typeflag) {
|
2003-01-28 07:15:48 +05:30
|
|
|
# ifdef CONFIG_FEATURE_TAR_OLDGNU_COMPATABILITY
|
2002-08-22 23:17:09 +05:30
|
|
|
case 0:
|
2003-01-28 07:15:48 +05:30
|
|
|
case '0':
|
2003-09-09 23:11:03 +05:30
|
|
|
if (last_char_is(file_header->name, '/')) {
|
|
|
|
file_header->mode |= S_IFDIR;
|
|
|
|
} else {
|
|
|
|
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;
|
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;
|
2002-08-22 23:17:09 +05:30
|
|
|
break;
|
2003-01-28 07:15:48 +05:30
|
|
|
case '4':
|
2002-09-25 08:17:48 +05:30
|
|
|
file_header->mode |= S_IFBLK;
|
2002-08-22 23:17:09 +05:30
|
|
|
break;
|
2003-01-28 07:15:48 +05:30
|
|
|
case '5':
|
2002-09-25 08:17:48 +05:30
|
|
|
file_header->mode |= S_IFDIR;
|
2002-08-22 23:17:09 +05:30
|
|
|
break;
|
2003-01-28 07:15:48 +05:30
|
|
|
case '6':
|
2002-09-25 08:17:48 +05:30
|
|
|
file_header->mode |= S_IFIFO;
|
2002-08-22 23:17:09 +05:30
|
|
|
break;
|
2002-09-15 22:24:49 +05:30
|
|
|
# endif
|
2003-09-12 12:01:28 +05:30
|
|
|
/* hard links are detected as entries with 0 size, a link name,
|
|
|
|
* and not being a symlink, hence we have nothing to do here */
|
|
|
|
case '1':
|
|
|
|
file_header->mode |= ~S_IFLNK;
|
|
|
|
break;
|
2003-01-28 07:15:48 +05:30
|
|
|
# ifdef CONFIG_FEATURE_TAR_GNU_EXTENSIONS
|
2002-09-15 22:24:49 +05:30
|
|
|
case 'L': {
|
2002-09-25 08:17:48 +05:30
|
|
|
longname = xmalloc(file_header->size + 1);
|
2002-11-03 19:35:15 +05:30
|
|
|
archive_xread_all(archive_handle, longname, file_header->size);
|
2002-09-25 08:17:48 +05:30
|
|
|
longname[file_header->size] = '\0';
|
|
|
|
archive_handle->offset += file_header->size;
|
2002-09-15 22:24:49 +05:30
|
|
|
|
2002-11-23 16:14:47 +05:30
|
|
|
return(get_header_tar(archive_handle));
|
2002-09-15 22:24:49 +05:30
|
|
|
}
|
|
|
|
case 'K': {
|
2002-09-25 08:17:48 +05:30
|
|
|
linkname = xmalloc(file_header->size + 1);
|
2002-11-03 19:35:15 +05:30
|
|
|
archive_xread_all(archive_handle, linkname, file_header->size);
|
2002-09-25 08:17:48 +05:30
|
|
|
linkname[file_header->size] = '\0';
|
|
|
|
archive_handle->offset += file_header->size;
|
2002-09-15 22:24:49 +05:30
|
|
|
|
2002-09-25 08:17:48 +05:30
|
|
|
file_header->name = linkname;
|
2002-11-23 16:14:47 +05:30
|
|
|
return(get_header_tar(archive_handle));
|
2002-09-15 22:24:49 +05:30
|
|
|
}
|
2003-01-28 07:15:48 +05:30
|
|
|
case 'D':
|
|
|
|
case 'M':
|
|
|
|
case 'N':
|
|
|
|
case 'S':
|
|
|
|
case 'V':
|
2003-03-19 14:43:01 +05:30
|
|
|
bb_error_msg("Ignoring GNU extension type %c", tar.formated.typeflag);
|
2002-09-15 22:24:49 +05:30
|
|
|
# endif
|
2002-08-13 10:36:43 +05:30
|
|
|
}
|
2003-08-14 08:25:15 +05:30
|
|
|
|
2002-11-05 05:17:31 +05:30
|
|
|
if (archive_handle->filter(archive_handle) == EXIT_SUCCESS) {
|
2002-09-25 08:17:48 +05:30
|
|
|
archive_handle->action_header(archive_handle->file_header);
|
|
|
|
archive_handle->flags |= ARCHIVE_EXTRACT_QUIET;
|
|
|
|
archive_handle->action_data(archive_handle);
|
2003-11-18 03:28:00 +05:30
|
|
|
archive_handle->passed = llist_add_to(archive_handle->passed, file_header->name);
|
2002-09-25 08:17:48 +05:30
|
|
|
} else {
|
|
|
|
data_skip(archive_handle);
|
|
|
|
}
|
|
|
|
archive_handle->offset += file_header->size;
|
|
|
|
|
2003-11-15 05:54:43 +05:30
|
|
|
free(file_header->link_name);
|
|
|
|
|
2002-09-25 08:17:48 +05:30
|
|
|
return(EXIT_SUCCESS);
|
2002-07-11 16:41:56 +05:30
|
|
|
}
|