2001-04-11 16:14:24 +00:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* NOTE: This is used by deb_extract to avoid calling the whole tar applet.
|
|
|
|
* Its functionality should be merged with tar to avoid duplication
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include "libbb.h"
|
|
|
|
|
2001-04-23 13:52:02 +00:00
|
|
|
/*
|
|
|
|
* stdout can be redircted to FILE *output
|
|
|
|
* If const char *file_prefix isnt NULL is prepended to all the extracted filenames.
|
|
|
|
* The purpose of const char *argument depends on the value of const int untar_function
|
|
|
|
*/
|
|
|
|
|
2001-04-16 04:52:19 +00:00
|
|
|
extern char *untar(FILE *src_tar_file, FILE *output, const int untar_function,
|
|
|
|
const char *argument, const char *file_prefix)
|
2001-04-11 16:14:24 +00:00
|
|
|
{
|
|
|
|
typedef struct raw_tar_header {
|
|
|
|
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 */
|
|
|
|
char magic[6]; /* 257-262 */
|
|
|
|
char version[2]; /* 263-264 */
|
|
|
|
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 */
|
|
|
|
} raw_tar_header_t;
|
|
|
|
|
|
|
|
raw_tar_header_t raw_tar_header;
|
|
|
|
unsigned char *temp = (unsigned char *) &raw_tar_header;
|
|
|
|
long i;
|
|
|
|
long next_header_offset = 0;
|
|
|
|
long uncompressed_count = 0;
|
|
|
|
size_t size;
|
|
|
|
mode_t mode;
|
|
|
|
|
2001-04-12 10:19:08 +00:00
|
|
|
while (fread((char *) &raw_tar_header, 1, 512, src_tar_file) == 512) {
|
2001-04-11 16:14:24 +00:00
|
|
|
long sum = 0;
|
2001-04-12 13:49:09 +00:00
|
|
|
|
|
|
|
if (ferror(src_tar_file) || feof(src_tar_file)) {
|
2001-04-12 16:40:21 +00:00
|
|
|
perror_msg("untar: ");
|
2001-04-12 13:49:09 +00:00
|
|
|
break;
|
|
|
|
}
|
2001-04-11 16:14:24 +00:00
|
|
|
|
|
|
|
uncompressed_count += 512;
|
|
|
|
|
|
|
|
/* Check header has valid magic */
|
|
|
|
if (strncmp(raw_tar_header.magic, "ustar", 5) != 0) {
|
2001-04-12 13:49:09 +00:00
|
|
|
/*
|
|
|
|
* FIXME, Need HELP with this
|
|
|
|
*
|
|
|
|
* This has to fail silently or it incorrectly reports errors when called from
|
|
|
|
* deb_extract.
|
|
|
|
* The problem is deb_extract decompresses the .gz file in a child process and
|
|
|
|
* untar reads from the child proccess. The child process finishes and exits,
|
|
|
|
* but fread reads 0's from the src_tar_file even though the child
|
|
|
|
* closed its handle.
|
|
|
|
*/
|
|
|
|
// error_msg("Invalid tar magic");
|
2001-04-11 16:14:24 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Do checksum on headers */
|
|
|
|
for (i = 0; i < 148 ; i++) {
|
|
|
|
sum += temp[i];
|
|
|
|
}
|
|
|
|
sum += ' ' * 8;
|
|
|
|
for (i = 156; i < 512 ; i++) {
|
|
|
|
sum += temp[i];
|
|
|
|
}
|
|
|
|
if (sum != strtol(raw_tar_header.chksum, NULL, 8)) {
|
|
|
|
error_msg("Invalid tar header checksum");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* convert to type'ed variables */
|
|
|
|
size = strtol(raw_tar_header.size, NULL, 8);
|
|
|
|
parse_mode(raw_tar_header.mode, &mode);
|
|
|
|
|
|
|
|
/* start of next header is at */
|
|
|
|
next_header_offset = uncompressed_count + size;
|
|
|
|
if (size % 512 != 0) {
|
|
|
|
next_header_offset += (512 - size % 512);
|
|
|
|
}
|
|
|
|
|
2001-04-21 02:12:13 +00:00
|
|
|
/* If an exclude list is specified check current file against list */
|
|
|
|
#if 0
|
2001-04-16 04:52:19 +00:00
|
|
|
if (*exclude_list != NULL) {
|
|
|
|
i = 0;
|
|
|
|
while (exclude_list[i] != 0) {
|
|
|
|
if (strncmp(exclude_list[i], raw_tar_header.name, strlen(raw_tar_header.name)) == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
if (exclude_list[i] != 0) {
|
|
|
|
continue;
|
|
|
|
}
|
2001-04-21 02:12:13 +00:00
|
|
|
}
|
|
|
|
#endif
|
2001-04-16 04:52:19 +00:00
|
|
|
if (untar_function & (extract_contents | extract_verbose_extract | extract_contents_to_file)) {
|
2001-04-15 12:51:59 +00:00
|
|
|
fprintf(output, "%s\n", raw_tar_header.name);
|
2001-04-12 11:48:02 +00:00
|
|
|
}
|
2001-04-11 16:14:24 +00:00
|
|
|
|
|
|
|
switch (raw_tar_header.typeflag ) {
|
|
|
|
case '0':
|
2001-04-23 13:52:02 +00:00
|
|
|
case '\0': {
|
2001-04-11 16:14:24 +00:00
|
|
|
/* If the name ends in a '/' then assume it is
|
|
|
|
* supposed to be a directory, and fall through
|
|
|
|
*/
|
2001-04-23 13:52:02 +00:00
|
|
|
int name_length = strlen(raw_tar_header.name);
|
|
|
|
if (raw_tar_header.name[name_length - 1] != '/') {
|
2001-04-15 12:51:59 +00:00
|
|
|
switch (untar_function) {
|
|
|
|
case (extract_extract):
|
|
|
|
case (extract_verbose_extract):
|
|
|
|
case (extract_control): {
|
2001-04-16 04:52:19 +00:00
|
|
|
FILE *dst_file = NULL;
|
|
|
|
char *full_name;
|
|
|
|
|
|
|
|
if (file_prefix != NULL) {
|
2001-04-23 13:52:02 +00:00
|
|
|
full_name = xmalloc(strlen(argument) + strlen(file_prefix) + name_length + 3);
|
|
|
|
sprintf(full_name, "%s/%s.%s", argument, file_prefix, strrchr(raw_tar_header.name, '/') + 1);
|
2001-04-16 04:52:19 +00:00
|
|
|
} else {
|
2001-04-23 13:52:02 +00:00
|
|
|
full_name = concat_path_file(argument, raw_tar_header.name);
|
2001-04-16 04:52:19 +00:00
|
|
|
}
|
|
|
|
dst_file = wfopen(full_name, "w");
|
2001-04-15 12:51:59 +00:00
|
|
|
copy_file_chunk(src_tar_file, dst_file, (unsigned long long) size);
|
|
|
|
uncompressed_count += size;
|
|
|
|
fclose(dst_file);
|
2001-04-16 04:52:19 +00:00
|
|
|
chmod(full_name, mode);
|
2001-04-23 13:52:02 +00:00
|
|
|
free(full_name);
|
2001-04-15 12:51:59 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case (extract_info):
|
|
|
|
if (strstr(raw_tar_header.name, argument) != NULL) {
|
|
|
|
copy_file_chunk(src_tar_file, stdout, (unsigned long long) size);
|
|
|
|
uncompressed_count += size;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case (extract_field):
|
2001-04-16 04:52:19 +00:00
|
|
|
if (strstr(raw_tar_header.name, "./control") != NULL) {
|
2001-04-15 12:51:59 +00:00
|
|
|
return(read_text_file_to_buffer(src_tar_file));
|
|
|
|
}
|
|
|
|
break;
|
2001-04-11 16:14:24 +00:00
|
|
|
}
|
2001-04-12 10:19:08 +00:00
|
|
|
break;
|
2001-04-11 16:14:24 +00:00
|
|
|
}
|
2001-04-23 13:52:02 +00:00
|
|
|
}
|
2001-04-11 16:14:24 +00:00
|
|
|
case '5':
|
2001-04-12 16:40:21 +00:00
|
|
|
if (untar_function & (extract_extract | extract_verbose_extract | extract_control)) {
|
2001-04-23 13:52:02 +00:00
|
|
|
int ret;
|
|
|
|
char *dir;
|
|
|
|
dir = concat_path_file(argument, raw_tar_header.name);
|
|
|
|
ret = create_path(dir, mode);
|
|
|
|
free(dir);
|
|
|
|
if (ret == FALSE) {
|
2001-04-12 11:48:02 +00:00
|
|
|
perror_msg("%s: Cannot mkdir", raw_tar_header.name);
|
2001-04-15 12:51:59 +00:00
|
|
|
return NULL;
|
2001-04-12 11:48:02 +00:00
|
|
|
}
|
2001-04-23 13:52:02 +00:00
|
|
|
chmod(dir, mode);
|
2001-04-11 16:14:24 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '1':
|
2001-04-12 16:40:21 +00:00
|
|
|
if (untar_function & (extract_extract | extract_verbose_extract | extract_control)) {
|
2001-04-11 16:14:24 +00:00
|
|
|
if (link(raw_tar_header.linkname, raw_tar_header.name) < 0) {
|
|
|
|
perror_msg("%s: Cannot create hard link to '%s'", raw_tar_header.name, raw_tar_header.linkname);
|
2001-04-15 12:51:59 +00:00
|
|
|
return NULL;
|
2001-04-11 16:14:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '2':
|
2001-04-12 16:40:21 +00:00
|
|
|
if (untar_function & (extract_extract | extract_verbose_extract | extract_control)) {
|
2001-04-11 16:14:24 +00:00
|
|
|
if (symlink(raw_tar_header.linkname, raw_tar_header.name) < 0) {
|
|
|
|
perror_msg("%s: Cannot create symlink to '%s'", raw_tar_header.name, raw_tar_header.linkname);
|
2001-04-15 12:51:59 +00:00
|
|
|
return NULL;
|
2001-04-11 16:14:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '3':
|
|
|
|
case '4':
|
|
|
|
case '6':
|
|
|
|
// if (tarExtractSpecial( &header, extractFlag, tostdoutFlag)==FALSE)
|
|
|
|
// errorFlag=TRUE;
|
|
|
|
// break;
|
|
|
|
default:
|
|
|
|
error_msg("Unknown file type '%c' in tar file", raw_tar_header.typeflag);
|
2001-04-15 12:51:59 +00:00
|
|
|
return NULL;
|
2001-04-11 16:14:24 +00:00
|
|
|
}
|
2001-04-13 04:02:57 +00:00
|
|
|
/*
|
|
|
|
* Seek to start of next block, cant use fseek as unzip() does support it
|
|
|
|
*/
|
|
|
|
while (uncompressed_count < next_header_offset) {
|
|
|
|
if (fgetc(src_tar_file) == EOF) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
uncompressed_count++;
|
|
|
|
}
|
2001-04-11 16:14:24 +00:00
|
|
|
}
|
2001-04-15 12:51:59 +00:00
|
|
|
return NULL;
|
2001-04-11 16:14:24 +00:00
|
|
|
}
|