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 int untar(FILE *src_tar_file, int untar_function, char *base_path)
27 typedef struct raw_tar_header {
28 char name[100]; /* 0-99 */
29 char mode[8]; /* 100-107 */
30 char uid[8]; /* 108-115 */
31 char gid[8]; /* 116-123 */
32 char size[12]; /* 124-135 */
33 char mtime[12]; /* 136-147 */
34 char chksum[8]; /* 148-155 */
35 char typeflag; /* 156-156 */
36 char linkname[100]; /* 157-256 */
37 char magic[6]; /* 257-262 */
38 char version[2]; /* 263-264 */
39 char uname[32]; /* 265-296 */
40 char gname[32]; /* 297-328 */
41 char devmajor[8]; /* 329-336 */
42 char devminor[8]; /* 337-344 */
43 char prefix[155]; /* 345-499 */
44 char padding[12]; /* 500-512 */
47 raw_tar_header_t raw_tar_header;
48 unsigned char *temp = (unsigned char *) &raw_tar_header;
50 long next_header_offset = 0;
51 long uncompressed_count = 0;
55 while (fread((char *) &raw_tar_header, 1, 512, src_tar_file) == 512) {
59 if (ferror(src_tar_file) || feof(src_tar_file)) {
63 uncompressed_count += 512;
65 /* Check header has valid magic */
66 if (strncmp(raw_tar_header.magic, "ustar", 5) != 0) {
68 * FIXME, Need HELP with this
70 * This has to fail silently or it incorrectly reports errors when called from
72 * The problem is deb_extract decompresses the .gz file in a child process and
73 * untar reads from the child proccess. The child process finishes and exits,
74 * but fread reads 0's from the src_tar_file even though the child
77 // error_msg("Invalid tar magic");
81 /* Do checksum on headers */
82 for (i = 0; i < 148 ; i++) {
86 for (i = 156; i < 512 ; i++) {
89 if (sum != strtol(raw_tar_header.chksum, NULL, 8)) {
90 error_msg("Invalid tar header checksum");
94 /* convert to type'ed variables */
95 size = strtol(raw_tar_header.size, NULL, 8);
96 parse_mode(raw_tar_header.mode, &mode);
98 /* start of next header is at */
99 next_header_offset = uncompressed_count + size;
100 if (size % 512 != 0) {
101 next_header_offset += (512 - size % 512);
105 * seek to start of control file, return length
107 if (dpkg_untar_function & dpkg_untar_seek_control) {
108 if ((raw_tar_header.typeflag == '0') || (raw_tar_header.typeflag == '\0')) {
111 tar_filename = strrchr(raw_tar_header.name, '/');
112 if (tar_filename == NULL) {
113 tar_filename = strdup(raw_tar_header.name);
118 if (strcmp(tar_filename, "control") == 0) {
125 if (untar_function & (extract_contents | extract_verbose_extract)) {
126 printf("%s\n", raw_tar_header.name);
130 if (base_path != NULL) {
131 dir = xmalloc(strlen(raw_tar_header.name) + strlen(base_path) + 2);
132 sprintf(dir, "%s/%s", base_path, raw_tar_header.name);
133 create_path(dir, 0777);
135 switch (raw_tar_header.typeflag ) {
138 /* If the name ends in a '/' then assume it is
139 * supposed to be a directory, and fall through
141 if (raw_tar_header.name[strlen(raw_tar_header.name)-1] != '/') {
142 switch (untar_function) {
143 case (extract_extract): {
144 FILE *dst_file = wfopen(dir, "w");
145 copy_file_chunk(src_tar_file, dst_file, size);
150 int remaining = size;
151 while (remaining-- > 0) {
156 uncompressed_count += size;
160 if (untar_function & extract_extract) {
161 if (create_path(dir, mode) != TRUE) {
163 perror_msg("%s: Cannot mkdir", raw_tar_header.name);
164 return(EXIT_FAILURE);
169 if (untar_function & extract_extract) {
170 if (link(raw_tar_header.linkname, raw_tar_header.name) < 0) {
172 perror_msg("%s: Cannot create hard link to '%s'", raw_tar_header.name, raw_tar_header.linkname);
173 return(EXIT_FAILURE);
178 if (untar_function & extract_extract) {
179 if (symlink(raw_tar_header.linkname, raw_tar_header.name) < 0) {
181 perror_msg("%s: Cannot create symlink to '%s'", raw_tar_header.name, raw_tar_header.linkname);
182 return(EXIT_FAILURE);
189 // if (tarExtractSpecial( &header, extractFlag, tostdoutFlag)==FALSE)
193 error_msg("Unknown file type '%c' in tar file", raw_tar_header.typeflag);
195 return(EXIT_FAILURE);
199 return(EXIT_SUCCESS);