Eliminated seeks so that we work correctly on pipes, and removed reliance on
[oweals/busybox.git] / libbb / untar.c
1 /*
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.
6  *
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.
11  *
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.
15  *
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
18  */
19
20 #include <stdlib.h>
21 #include <string.h>
22 #include <unistd.h>
23 #include "libbb.h"
24
25 extern char *untar(FILE *src_tar_file, FILE *output, const int untar_function,
26         const char *argument, const char *file_prefix)
27 {
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 */
46         } raw_tar_header_t;
47
48         raw_tar_header_t raw_tar_header;
49         unsigned char *temp = (unsigned char *) &raw_tar_header;
50         long i;
51         long next_header_offset = 0;
52         long uncompressed_count = 0;
53         size_t size;
54         mode_t mode;
55
56         while (fread((char *) &raw_tar_header, 1, 512, src_tar_file) == 512) {
57                 long sum = 0;
58                 char *dir = NULL;
59                 
60                 if (ferror(src_tar_file) || feof(src_tar_file)) {
61                         perror_msg("untar: ");
62                         break;
63                 }
64
65                 uncompressed_count += 512;
66
67                 /* Check header has valid magic */
68                 if (strncmp(raw_tar_header.magic, "ustar", 5) != 0) {
69 /*
70  * FIXME, Need HELP with this
71  *
72  * This has to fail silently or it incorrectly reports errors when called from
73  * deb_extract.
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
77  * closed its handle.
78  */
79 //                      error_msg("Invalid tar magic");
80                         break;
81                 }
82
83                 /* Do checksum on headers */
84         for (i =  0; i < 148 ; i++) {
85                         sum += temp[i];
86                 }
87         sum += ' ' * 8;
88                 for (i =  156; i < 512 ; i++) {
89                         sum += temp[i];
90                 }
91         if (sum != strtol(raw_tar_header.chksum, NULL, 8)) {
92                         error_msg("Invalid tar header checksum");
93                         break;
94                 }
95
96                 /* convert to type'ed variables */
97         size = strtol(raw_tar_header.size, NULL, 8);
98                 parse_mode(raw_tar_header.mode, &mode);
99
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);
104                 }
105
106                 /* If an exclude list is specified check current file against list 
107                 if (*exclude_list != NULL) {
108                         i = 0;
109                         while (exclude_list[i] != 0) {
110                                 if (strncmp(exclude_list[i], raw_tar_header.name, strlen(raw_tar_header.name)) == 0) {
111                                         break;
112                                 }
113                                 i++;
114                         }
115                 if (exclude_list[i] != 0) {
116                                 continue;
117                         }
118                 } */
119
120                 if (untar_function & (extract_contents | extract_verbose_extract | extract_contents_to_file)) {
121                         fprintf(output, "%s\n", raw_tar_header.name);
122                 }
123
124                 /* extract files */
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);
128                 }
129
130                 switch (raw_tar_header.typeflag ) {
131                         case '0':
132                         case '\0':
133                                 /* If the name ends in a '/' then assume it is
134                                  * supposed to be a directory, and fall through
135                                  */
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;
142                                                                 char *full_name;
143
144                                                                 if (file_prefix != NULL) {
145                                                                         char *file_name = NULL, *file_extension = NULL;
146
147                                                                         file_extension = xmalloc(strlen(raw_tar_header.name) + 1);
148                                                                         file_extension = strrchr(raw_tar_header.name, '/');
149                                                                         file_extension++;
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);
154
155                                                                         full_name = concat_path_file(strndup(dir, strlen(dir) - strlen(strrchr(dir, '/'))), file_name);
156                                                                 } else {
157                                                                         full_name = xstrdup(dir);
158                                                                 }
159                                                                 dst_file = wfopen(full_name, "w");
160                                                                 copy_file_chunk(src_tar_file, dst_file, (unsigned long long) size);
161                                                                 uncompressed_count += size;
162                                                                 fclose(dst_file);
163                                                                 chmod(full_name, mode);
164                                                         }
165                                                         break;
166                                                 case (extract_info):
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;
170                                                         }
171                                                         break;
172                                                 case (extract_field):
173                                                         if (strstr(raw_tar_header.name, "./control") != NULL) {
174                                                                 return(read_text_file_to_buffer(src_tar_file));
175                                                         }
176                                                         break;
177                                         }
178                                         break;
179                                 }
180                         case '5':
181                                 if (untar_function & (extract_extract | extract_verbose_extract | extract_control)) {
182                                         if (create_path(dir, mode) != TRUE) {
183                                                 free(dir);
184                                                 perror_msg("%s: Cannot mkdir", raw_tar_header.name); 
185                                                 return NULL;
186                                         }
187                                 }
188                                 break;
189                         case '1':
190                                 if (untar_function & (extract_extract | extract_verbose_extract | extract_control)) {
191                                         if (link(raw_tar_header.linkname, raw_tar_header.name) < 0) {
192                                                 free(dir);
193                                                 perror_msg("%s: Cannot create hard link to '%s'", raw_tar_header.name, raw_tar_header.linkname); 
194                                                 return NULL;
195                                         }
196                                 }
197                                 break;
198                         case '2':
199                                 if (untar_function & (extract_extract | extract_verbose_extract | extract_control)) {
200                                         if (symlink(raw_tar_header.linkname, raw_tar_header.name) < 0) {
201                                                 free(dir);
202                                                 perror_msg("%s: Cannot create symlink to '%s'", raw_tar_header.name, raw_tar_header.linkname); 
203                                                 return NULL;
204                                         }
205                                 }
206                                 break;
207                         case '3':
208                         case '4':
209                         case '6':
210 //                              if (tarExtractSpecial( &header, extractFlag, tostdoutFlag)==FALSE)
211 //                                      errorFlag=TRUE;
212 //                              break;
213                         default:
214                                 error_msg("Unknown file type '%c' in tar file", raw_tar_header.typeflag);
215                                 free(dir);
216                                 return NULL;
217                 }
218
219                 /*
220                  * Seek to start of next block, cant use fseek as unzip() does support it
221                  */
222                 while (uncompressed_count < next_header_offset) {
223                         if (fgetc(src_tar_file) == EOF) {
224                                 break;
225                         }
226                         uncompressed_count++;
227                 }
228
229 //              free(dir);
230         }
231         return NULL;
232 }