2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU Library General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
16 * NOTE: This is used by deb_extract to avoid calling the whole tar applet.
17 * Its functionality should be merged with tar to avoid duplication
25 extern char *untar(FILE *src_tar_file, FILE *output, const int untar_function,
26 const char *argument, const char *file_prefix)
28 typedef struct raw_tar_header {
29 char name[100]; /* 0-99 */
30 char mode[8]; /* 100-107 */
31 char uid[8]; /* 108-115 */
32 char gid[8]; /* 116-123 */
33 char size[12]; /* 124-135 */
34 char mtime[12]; /* 136-147 */
35 char chksum[8]; /* 148-155 */
36 char typeflag; /* 156-156 */
37 char linkname[100]; /* 157-256 */
38 char magic[6]; /* 257-262 */
39 char version[2]; /* 263-264 */
40 char uname[32]; /* 265-296 */
41 char gname[32]; /* 297-328 */
42 char devmajor[8]; /* 329-336 */
43 char devminor[8]; /* 337-344 */
44 char prefix[155]; /* 345-499 */
45 char padding[12]; /* 500-512 */
48 raw_tar_header_t raw_tar_header;
49 unsigned char *temp = (unsigned char *) &raw_tar_header;
51 long next_header_offset = 0;
52 long uncompressed_count = 0;
56 while (fread((char *) &raw_tar_header, 1, 512, src_tar_file) == 512) {
60 if (ferror(src_tar_file) || feof(src_tar_file)) {
61 perror_msg("untar: ");
65 uncompressed_count += 512;
67 /* Check header has valid magic */
68 if (strncmp(raw_tar_header.magic, "ustar", 5) != 0) {
70 * FIXME, Need HELP with this
72 * This has to fail silently or it incorrectly reports errors when called from
74 * The problem is deb_extract decompresses the .gz file in a child process and
75 * untar reads from the child proccess. The child process finishes and exits,
76 * but fread reads 0's from the src_tar_file even though the child
79 // error_msg("Invalid tar magic");
83 /* Do checksum on headers */
84 for (i = 0; i < 148 ; i++) {
88 for (i = 156; i < 512 ; i++) {
91 if (sum != strtol(raw_tar_header.chksum, NULL, 8)) {
92 error_msg("Invalid tar header checksum");
96 /* convert to type'ed variables */
97 size = strtol(raw_tar_header.size, NULL, 8);
98 parse_mode(raw_tar_header.mode, &mode);
100 /* start of next header is at */
101 next_header_offset = uncompressed_count + size;
102 if (size % 512 != 0) {
103 next_header_offset += (512 - size % 512);
106 /* If an exclude list is specified check current file against list
107 if (*exclude_list != NULL) {
109 while (exclude_list[i] != 0) {
110 if (strncmp(exclude_list[i], raw_tar_header.name, strlen(raw_tar_header.name)) == 0) {
115 if (exclude_list[i] != 0) {
120 if (untar_function & (extract_contents | extract_verbose_extract | extract_contents_to_file)) {
121 fprintf(output, "%s\n", raw_tar_header.name);
125 if (untar_function & (extract_extract | extract_verbose_extract | extract_control)) {
126 dir = concat_path_file(argument, raw_tar_header.name);
127 create_path(dir, 0777);
130 switch (raw_tar_header.typeflag ) {
133 /* If the name ends in a '/' then assume it is
134 * supposed to be a directory, and fall through
136 if (raw_tar_header.name[strlen(raw_tar_header.name)-1] != '/') {
137 switch (untar_function) {
138 case (extract_extract):
139 case (extract_verbose_extract):
140 case (extract_control): {
141 FILE *dst_file = NULL;
144 if (file_prefix != NULL) {
145 char *file_name = NULL, *file_extension = NULL;
147 file_extension = xmalloc(strlen(raw_tar_header.name) + 1);
148 file_extension = strrchr(raw_tar_header.name, '/');
150 file_name = xmalloc(strlen(file_prefix) + strlen(file_extension) + 2);
151 strcpy(file_name, file_prefix);
152 strcat(file_name, ".");
153 strcat(file_name, file_extension);
155 full_name = concat_path_file(strndup(dir, strlen(dir) - strlen(strrchr(dir, '/'))), file_name);
157 full_name = xstrdup(dir);
159 dst_file = wfopen(full_name, "w");
160 copy_file_chunk(src_tar_file, dst_file, (unsigned long long) size);
161 uncompressed_count += size;
163 chmod(full_name, mode);
167 if (strstr(raw_tar_header.name, argument) != NULL) {
168 copy_file_chunk(src_tar_file, stdout, (unsigned long long) size);
169 uncompressed_count += size;
172 case (extract_field):
173 if (strstr(raw_tar_header.name, "./control") != NULL) {
174 return(read_text_file_to_buffer(src_tar_file));
181 if (untar_function & (extract_extract | extract_verbose_extract | extract_control)) {
182 if (create_path(dir, mode) != TRUE) {
184 perror_msg("%s: Cannot mkdir", raw_tar_header.name);
190 if (untar_function & (extract_extract | extract_verbose_extract | extract_control)) {
191 if (link(raw_tar_header.linkname, raw_tar_header.name) < 0) {
193 perror_msg("%s: Cannot create hard link to '%s'", raw_tar_header.name, raw_tar_header.linkname);
199 if (untar_function & (extract_extract | extract_verbose_extract | extract_control)) {
200 if (symlink(raw_tar_header.linkname, raw_tar_header.name) < 0) {
202 perror_msg("%s: Cannot create symlink to '%s'", raw_tar_header.name, raw_tar_header.linkname);
210 // if (tarExtractSpecial( &header, extractFlag, tostdoutFlag)==FALSE)
214 error_msg("Unknown file type '%c' in tar file", raw_tar_header.typeflag);
220 * Seek to start of next block, cant use fseek as unzip() does support it
222 while (uncompressed_count < next_header_offset) {
223 if (fgetc(src_tar_file) == EOF) {
226 uncompressed_count++;