4e3a2edc04463b75e88cf86ca3535f17c39f7059
[oweals/opkg-lede.git] / libbb / unarchive.c
1 /*
2  *  Copyright (C) 2000 by Glenn McGrath
3  *  Copyright (C) 2001 by Laurence Anderson
4  *      
5  *  Based on previous work by busybox developers and others.
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation; either version 2 of the License, or
10  *  (at your option) any later version.
11  *
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU Library General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program; if not, write to the Free Software
19  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */
21
22 #include <stdio.h>
23 #include <errno.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
27 #include <utime.h>
28 #include "libbb.h"
29
30 #define CONFIG_FEATURE_TAR_OLDGNU_COMPATABILITY 1
31 #define CONFIG_FEATURE_TAR_GNU_EXTENSIONS
32
33 #ifdef CONFIG_FEATURE_TAR_GNU_EXTENSIONS
34 static char *longname = NULL;
35 static char *linkname = NULL;
36 #endif
37
38 extern void seek_sub_file(FILE *src_stream, const int count);
39 extern char *extract_archive(FILE *src_stream, FILE *out_stream, const file_header_t *file_entry,
40                               const int function, const char *prefix);
41
42
43 #ifdef L_archive_offset
44 off_t archive_offset;
45 #else
46 extern off_t archive_offset;
47 #endif  
48
49 #ifdef L_seek_sub_file
50 void seek_sub_file(FILE *src_stream, const int count)
51 {
52         int i;
53         /* Try to fseek as faster */
54         archive_offset += count;
55         if (fseek(src_stream, count, SEEK_CUR) != 0 && errno == ESPIPE) {
56         for (i = 0; i < count; i++) {
57                 fgetc(src_stream);
58                 }
59         }
60         return;
61 }
62 #endif  
63
64
65
66 #ifdef L_extract_archive
67 /* Extract the data postioned at src_stream to either filesystem, stdout or 
68  * buffer depending on the value of 'function' which is defined in libbb.h 
69  *
70  * prefix doesnt have to be just a directory, it may prefix the filename as well.
71  *
72  * e.g. '/var/lib/dpkg/info/dpkg.' will extract all files to the base bath 
73  * '/var/lib/dpkg/info/' and all files/dirs created in that dir will have 
74  * 'dpkg.' as their prefix
75  *
76  * For this reason if prefix does point to a dir then it must end with a
77  * trailing '/' or else the last dir will be assumed to be the file prefix 
78  */
79 char *extract_archive(FILE *src_stream, FILE *out_stream, const file_header_t *file_entry,
80  const int function, const char *prefix)
81 {
82         FILE *dst_stream = NULL;
83         char *full_name = NULL;
84         char *full_link_name = NULL;
85         char *buffer = NULL;
86         struct utimbuf t;
87         int res;
88
89
90         /* prefix doesnt have to be a proper path it may prepend 
91          * the filename as well */
92         if (prefix != NULL) {
93                 /* strip leading '/' in filename to extract as prefix may not be dir */
94                 /* Cant use concat_path_file here as prefix might not be a directory */
95                 char *path = file_entry->name;
96                 if (strncmp("./", path, 2) == 0) {
97                         path += 2;
98                         if (strlen(path) == 0) {
99                                 return(NULL);
100                         }
101                 }
102                 full_name = xmalloc(strlen(prefix) + strlen(path) + 1);
103                 strcpy(full_name, prefix);
104                 strcat(full_name, path);
105                 if ( file_entry->link_name ){
106                    full_link_name = xmalloc(strlen(prefix) + strlen(file_entry->link_name) + 1);
107                    strcpy(full_link_name, prefix);
108                    strcat(full_link_name, file_entry->link_name);
109                 }
110         } else {
111                 full_name = strdup(file_entry->name);
112                 if ( file_entry->link_name )
113                    full_link_name = strdup(file_entry->link_name);
114         }
115
116
117         if (function & extract_to_stdout) {
118                 if (S_ISREG(file_entry->mode)) {
119                         copy_file_chunk(src_stream, out_stream, file_entry->size);                      
120                         archive_offset += file_entry->size;
121                 }
122         }
123         else if (function & extract_one_to_buffer) { 
124                 if (S_ISREG(file_entry->mode)) {
125                         buffer = (char *) xmalloc(file_entry->size + 1);
126                         fread(buffer, 1, file_entry->size, src_stream);
127                         buffer[file_entry->size] = '\0';
128                         archive_offset += file_entry->size;
129                         return(buffer);
130                 }
131         }
132         else if (function & extract_all_to_fs) {
133                 struct stat oldfile;
134                 int stat_res;
135                 stat_res = lstat (full_name, &oldfile);
136                 if (stat_res == 0) { /* The file already exists */
137                         if ((function & extract_unconditional) || (oldfile.st_mtime < file_entry->mtime)) {
138                                 if (!S_ISDIR(oldfile.st_mode)) {
139                                         unlink(full_name); /* Directories might not be empty etc */
140                                 }
141                         } else {
142                                 if ((function & extract_quiet) != extract_quiet) {
143                                         error_msg("%s not created: newer or same age file exists", file_entry->name);
144                                 }
145                                 seek_sub_file(src_stream, file_entry->size);
146                                 return (NULL);
147                         }
148                 }
149                 if (function & extract_create_leading_dirs) { /* Create leading directories with default umask */
150                         char *buf, *parent;
151                         buf = xstrdup(full_name);
152                         parent = dirname(buf);
153                         if (make_directory (parent, -1, FILEUTILS_RECUR) != 0) {
154                                 if ((function & extract_quiet) != extract_quiet) {
155                                         error_msg("couldn't create leading directories");
156                                 }
157                         }
158                         free (buf);
159                 }
160                 switch(file_entry->mode & S_IFMT) {
161                         case S_IFREG:
162                                 if (file_entry->link_name) { /* Found a cpio hard link */
163                                         if (link(full_link_name, full_name) != 0) {
164                                                 if ((function & extract_quiet) != extract_quiet) {
165                                                         perror_msg("Cannot link from %s to '%s'",
166                                                                 file_entry->name, file_entry->link_name);
167                                                 }
168                                         }
169                                 } else {
170                                         if ((dst_stream = wfopen(full_name, "w")) == NULL) {
171                                                 seek_sub_file(src_stream, file_entry->size);
172                                                 return NULL;
173                                         }
174                                         archive_offset += file_entry->size;
175                                         copy_file_chunk(src_stream, dst_stream, file_entry->size);                      
176                                         fclose(dst_stream);
177                                 }
178                                 break;
179                         case S_IFDIR:
180                                 if (stat_res != 0) {
181                                         if (mkdir(full_name, file_entry->mode) < 0) {
182                                                 if ((function & extract_quiet) != extract_quiet) {
183                                                         perror_msg("extract_archive: %s", full_name);
184                                                 }
185                                         }
186                                 }
187                                 break;
188                         case S_IFLNK:
189                                 if (symlink(file_entry->link_name, full_name) < 0) {
190                                         if ((function & extract_quiet) != extract_quiet) {
191                                                 perror_msg("Cannot create symlink from %s to '%s'", file_entry->name, file_entry->link_name);
192                                         }
193                                         return NULL;
194                                 }
195                                 break;
196                         case S_IFSOCK:
197                         case S_IFBLK:
198                         case S_IFCHR:
199                         case S_IFIFO:
200                                 if (mknod(full_name, file_entry->mode, file_entry->device) == -1) {
201                                         if ((function & extract_quiet) != extract_quiet) {
202                                                 perror_msg("Cannot create node %s", file_entry->name);
203                                         }
204                                         return NULL;
205                                 }
206                                 break;
207                          default:
208                             perror_msg("Don't know how to handle %s", full_name);
209
210                 }
211
212                 /* Changing a symlink's properties normally changes the properties of the 
213                  * file pointed to, so dont try and change the date or mode, lchown does
214                  * does the right thing, but isnt available in older versions of libc */
215                 if (S_ISLNK(file_entry->mode)) {
216 #if (__GLIBC__ > 2) && (__GLIBC_MINOR__ > 1)
217                         lchown(full_name, file_entry->uid, file_entry->gid);
218 #endif
219                 } else {
220                         if (function & extract_preserve_date) {
221                                 t.actime = file_entry->mtime;
222                                 t.modtime = file_entry->mtime;
223                                 utime(full_name, &t);
224                         }
225                         chown(full_name, file_entry->uid, file_entry->gid);
226                         chmod(full_name, file_entry->mode);
227                 }
228         } else {
229                 /* If we arent extracting data we have to skip it, 
230                  * if data size is 0 then then just do it anyway
231                  * (saves testing for it) */
232                 seek_sub_file(src_stream, file_entry->size);
233         }
234
235         /* extract_list and extract_verbose_list can be used in conjunction
236          * with one of the above four extraction functions, so do this seperately */
237         if (function & extract_verbose_list) {
238                 fprintf(out_stream, "%s %d/%d %8d %s ", mode_string(file_entry->mode), 
239                         file_entry->uid, file_entry->gid,
240                         (int) file_entry->size, time_string(file_entry->mtime));
241         }
242         if ((function & extract_list) || (function & extract_verbose_list)){
243                 /* fputs doesnt add a trailing \n, so use fprintf */
244                 fprintf(out_stream, "%s\n", file_entry->name);
245         }
246
247         free(full_name);
248         if ( full_link_name )
249             free(full_link_name);
250
251         return(NULL); /* Maybe we should say if failed */
252 }
253 #endif
254
255 #ifdef L_unarchive
256 char *unarchive(FILE *src_stream, FILE *out_stream,
257                 file_header_t *(*get_headers)(FILE *),
258                 void (*free_headers)(file_header_t *),
259                 const int extract_function, const char *prefix, char **extract_names)
260 {
261         file_header_t *file_entry;
262         int extract_flag;
263         int i;
264         char *buffer = NULL;
265
266         archive_offset = 0;
267         while ((file_entry = get_headers(src_stream)) != NULL) {
268                 extract_flag = TRUE;
269                 /* fprintf(stderr, __FUNCTION__ " getting headers\n"); */
270                 if (extract_names != NULL) {
271                         int found_flag = FALSE;
272                         for(i = 0; extract_names[i] != 0; i++) {
273                                 if (strcmp(extract_names[i], file_entry->name) == 0) {
274                                         found_flag = TRUE;
275                                         break;
276                                 }
277                         }
278                         if (extract_function & extract_exclude_list) {
279                                 if (found_flag == TRUE) {
280                                         extract_flag = FALSE;
281                                 }
282                         } else {
283                                 /* If its not found in the include list dont extract it */
284                                 if (found_flag == FALSE) {
285                                         extract_flag = FALSE;
286                                 }
287                         }
288
289                 }
290
291                 if (extract_flag == TRUE) {
292                         /* fprintf(stderr, __FUNCTION__ " extract?\n"); */
293                         buffer = extract_archive(src_stream, out_stream, file_entry, extract_function, prefix);
294                         /* fprintf(stderr, __FUNCTION__ " extracted\n"); */
295                 } else {
296                         /* seek past the data entry */
297                         seek_sub_file(src_stream, file_entry->size);
298                 }
299                 free_headers(file_entry);
300         }
301         /*fprintf(stderr, __FUNCTION__ " goin home\n");*/
302         
303         return(buffer);
304 }
305 #endif
306
307 #ifdef L_get_header_ar
308 file_header_t *get_header_ar(FILE *src_stream)
309 {
310         file_header_t *typed;
311         union {
312                 char raw[60];
313                 struct {
314                         char name[16];
315                         char date[12];
316                         char uid[6];
317                         char gid[6];
318                         char mode[8];
319                         char size[10];
320                         char magic[2];
321                 } formated;
322         } ar;
323         static char *ar_long_names;
324
325         if (fread(ar.raw, 1, 60, src_stream) != 60) {
326                 return(NULL);
327         }
328         archive_offset += 60;
329         /* align the headers based on the header magic */
330         if ((ar.formated.magic[0] != '`') || (ar.formated.magic[1] != '\n')) {
331                 /* some version of ar, have an extra '\n' after each data entry,
332                  * this puts the next header out by 1 */
333                 if (ar.formated.magic[1] != '`') {
334                         error_msg("Invalid magic");
335                         return(NULL);
336                 }
337                 /* read the next char out of what would be the data section,
338                  * if its a '\n' then it is a valid header offset by 1*/
339                 archive_offset++;
340                 if (fgetc(src_stream) != '\n') {
341                         error_msg("Invalid magic");
342                         return(NULL);
343                 }
344                 /* fix up the header, we started reading 1 byte too early */
345                 /* raw_header[60] wont be '\n' as it should, but it doesnt matter */
346                 memmove(ar.raw, &ar.raw[1], 59);
347         }
348                 
349         typed = (file_header_t *) xcalloc(1, sizeof(file_header_t));
350
351         typed->size = (size_t) atoi(ar.formated.size);
352         /* long filenames have '/' as the first character */
353         if (ar.formated.name[0] == '/') {
354                 if (ar.formated.name[1] == '/') {
355                         /* If the second char is a '/' then this entries data section
356                          * stores long filename for multiple entries, they are stored
357                          * in static variable long_names for use in future entries */
358                         ar_long_names = (char *) xrealloc(ar_long_names, typed->size);
359                         fread(ar_long_names, 1, typed->size, src_stream);
360                         archive_offset += typed->size;
361                         /* This ar entries data section only contained filenames for other records
362                          * they are stored in the static ar_long_names for future reference */
363                         return (get_header_ar(src_stream)); /* Return next header */
364                 } else if (ar.formated.name[1] == ' ') {
365                         /* This is the index of symbols in the file for compilers */
366                         seek_sub_file(src_stream, typed->size);
367                         return (get_header_ar(src_stream)); /* Return next header */
368                 } else {
369                         /* The number after the '/' indicates the offset in the ar data section
370                         (saved in variable long_name) that conatains the real filename */
371                         if (!ar_long_names) {
372                                 error_msg("Cannot resolve long file name");
373                                 return (NULL);
374                         }
375                         typed->name = xstrdup(ar_long_names + atoi(&ar.formated.name[1]));
376                 }
377         } else {
378                 /* short filenames */
379                 typed->name = xcalloc(1, 16);
380                 strncpy(typed->name, ar.formated.name, 16);
381         }
382         typed->name[strcspn(typed->name, " /")]='\0';
383
384         /* convert the rest of the now valid char header to its typed struct */ 
385         parse_mode(ar.formated.mode, &typed->mode);
386         typed->mtime = atoi(ar.formated.date);
387         typed->uid = atoi(ar.formated.uid);
388         typed->gid = atoi(ar.formated.gid);
389
390         return(typed);
391 }
392
393 void free_header_ar(file_header_t *ar_entry)
394 {
395     if (ar_entry == NULL) {
396         return;
397     }
398
399     free(ar_entry->name);
400     free(ar_entry->link_name);
401
402     free(ar_entry);
403 }
404 #endif
405
406 #ifdef L_get_header_cpio
407 struct hardlinks {
408         file_header_t *entry;
409         int inode;
410         struct hardlinks *next;
411 };
412
413 file_header_t *get_header_cpio(FILE *src_stream)
414 {
415         file_header_t *cpio_entry = NULL;
416         char cpio_header[110];
417         int namesize;
418         char dummy[16];
419         int major, minor, nlink, inode;
420         static struct hardlinks *saved_hardlinks = NULL;
421         static int pending_hardlinks = 0;
422
423
424         if (pending_hardlinks) { /* Deal with any pending hardlinks */
425                 struct hardlinks *tmp = saved_hardlinks, *oldtmp = NULL;
426                 while (tmp) {
427                         if (tmp->entry->link_name) { /* Found a hardlink ready to be extracted */
428                                 cpio_entry = tmp->entry;
429                                 if (oldtmp) oldtmp->next = tmp->next; /* Remove item from linked list */
430                                 else saved_hardlinks = tmp->next;
431                                 free(tmp);
432                                 return (cpio_entry);
433                         }
434                         oldtmp = tmp;
435                         tmp = tmp->next;
436                 }
437                 pending_hardlinks = 0; /* No more pending hardlinks, read next file entry */
438         }
439   
440         /* There can be padding before archive header */
441         seek_sub_file(src_stream, (4 - (archive_offset % 4)) % 4);
442         if (fread(cpio_header, 1, 110, src_stream) == 110) {
443                 archive_offset += 110;
444                 if (strncmp(cpio_header, "07070", 5) != 0) {
445                         error_msg("Unsupported format or invalid magic");
446                         return(NULL);
447                 }
448                 switch (cpio_header[5]) {
449                         case '2': /* "crc" header format */
450                                 /* Doesnt do the crc check yet */
451                         case '1': /* "newc" header format */
452                                 cpio_entry = (file_header_t *) xcalloc(1, sizeof(file_header_t));
453                                 sscanf(cpio_header, "%6c%8x%8x%8x%8x%8x%8lx%8lx%16c%8x%8x%8x%8c",
454                                         dummy, &inode, (unsigned int*)&cpio_entry->mode, 
455                                         (unsigned int*)&cpio_entry->uid, (unsigned int*)&cpio_entry->gid,
456                                         &nlink, &cpio_entry->mtime, (unsigned long*)&cpio_entry->size,
457                                         dummy, &major, &minor, &namesize, dummy);
458
459                                 cpio_entry->name = (char *) xcalloc(1, namesize);
460                                 fread(cpio_entry->name, 1, namesize, src_stream); /* Read in filename */
461                                 archive_offset += namesize;
462                                 /* Skip padding before file contents */
463                                 seek_sub_file(src_stream, (4 - (archive_offset % 4)) % 4);
464                                 if (strcmp(cpio_entry->name, "TRAILER!!!") == 0) {
465                                         printf("%d blocks\n", (int) (archive_offset % 512 ? (archive_offset / 512) + 1 : archive_offset / 512)); /* Always round up */
466                                         if (saved_hardlinks) { /* Bummer - we still have unresolved hardlinks */
467                                                 struct hardlinks *tmp = saved_hardlinks, *oldtmp = NULL;
468                                                 while (tmp) {
469                                                         error_msg("%s not created: cannot resolve hardlink", tmp->entry->name);
470                                                         oldtmp = tmp;
471                                                         tmp = tmp->next;
472                                                         free (oldtmp->entry->name);
473                                                         free (oldtmp->entry);
474                                                         free (oldtmp);
475                                                 }
476                                                 saved_hardlinks = NULL;
477                                                 pending_hardlinks = 0;
478                                         }
479                                         return(NULL);
480                                 }
481
482                                 if (S_ISLNK(cpio_entry->mode)) {
483                                         cpio_entry->link_name = (char *) xcalloc(1, cpio_entry->size + 1);
484                                         fread(cpio_entry->link_name, 1, cpio_entry->size, src_stream);
485                                         archive_offset += cpio_entry->size;
486                                         cpio_entry->size = 0; /* Stop possiable seeks in future */
487                                 }
488                                 if (nlink > 1 && !S_ISDIR(cpio_entry->mode)) {
489                                         if (cpio_entry->size == 0) { /* Put file on a linked list for later */
490                                                 struct hardlinks *new = xmalloc(sizeof(struct hardlinks));
491                                                 new->next = saved_hardlinks;
492                                                 new->inode = inode;
493                                                 new->entry = cpio_entry;
494                                                 saved_hardlinks = new;
495                                         return(get_header_cpio(src_stream)); /* Recurse to next file */
496                                         } else { /* Found the file with data in */
497                                                 struct hardlinks *tmp = saved_hardlinks;
498                                                 pending_hardlinks = 1;
499                                                 while (tmp) {
500                                                         if (tmp->inode == inode) {
501                                                                 tmp->entry->link_name = xstrdup(cpio_entry->name);
502                                                                 nlink--;
503                                                         }
504                                                         tmp = tmp->next;
505                                                 }
506                                                 if (nlink > 1) error_msg("error resolving hardlink: did you create the archive with GNU cpio 2.0-2.2?");
507                                         }
508                                 }
509                                 cpio_entry->device = (major << 8) | minor;
510                                 break;
511                         default:
512                                 error_msg("Unsupported format");
513                                 return(NULL);
514                 }
515                 if (ferror(src_stream) || feof(src_stream)) {
516                         perror_msg("Stream error");
517                         return(NULL);
518                 }
519         }
520         return(cpio_entry);
521 }
522 #endif
523
524 #ifdef L_get_header_tar
525 file_header_t *get_header_tar(FILE *tar_stream)
526 {
527
528         union {
529                 unsigned char raw[512];
530                 struct {
531                         char name[100];         /*   0-99 */
532                         char mode[8];           /* 100-107 */
533                         char uid[8];            /* 108-115 */
534                         char gid[8];            /* 116-123 */
535                         char size[12];          /* 124-135 */
536                         char mtime[12];         /* 136-147 */
537                         char chksum[8];         /* 148-155 */
538                         char typeflag;          /* 156-156 */
539                         char linkname[100];     /* 157-256 */
540                         char magic[6];          /* 257-262 */
541                         char version[2];        /* 263-264 */
542                         char uname[32];         /* 265-296 */
543                         char gname[32];         /* 297-328 */
544                         char devmajor[8];       /* 329-336 */
545                         char devminor[8];       /* 337-344 */
546                         char prefix[155];       /* 345-499 */
547                         char padding[12];       /* 500-512 */
548                 } formated;
549         } tar;
550         file_header_t *tar_entry = NULL;
551         long i;
552         long sum = 0;
553
554         if (archive_offset % 512 != 0) {
555                 seek_sub_file(tar_stream, 512 - (archive_offset % 512));
556         }
557
558         if (fread(tar.raw, 1, 512, tar_stream) != 512) {
559                 /* Unfortunatly its common for tar files to have all sorts of
560                  * trailing garbage, fail silently */
561 //              error_msg("Couldnt read header");
562                 return(NULL);
563         }
564         archive_offset += 512;
565
566         /* Check header has valid magic, unfortunately some tar files
567          * have empty (0'ed) tar entries at the end, which will
568          * cause this to fail, so fail silently for now
569          */
570         if (strncmp(tar.formated.magic, "ustar", 5) != 0) {
571 #ifdef CONFIG_FEATURE_TAR_OLDGNU_COMPATABILITY
572                 if (strncmp(tar.formated.magic, "\0\0\0\0\0", 5) != 0)
573 #endif
574                 return(NULL);
575         }
576
577         /* Do checksum on headers */
578         for (i =  0; i < 148 ; i++) {
579                 sum += tar.raw[i];
580         }
581         sum += ' ' * 8;
582         for (i =  156; i < 512 ; i++) {
583                 sum += tar.raw[i];
584         }
585         if (sum != strtol(tar.formated.chksum, NULL, 8)) {
586                 if ( strtol(tar.formated.chksum,NULL,8) != 0 )
587                         error_msg("Invalid tar header checksum");
588                 return(NULL);
589         }
590
591         /* convert to type'ed variables */
592         tar_entry = xcalloc(1, sizeof(file_header_t));
593
594 #ifdef CONFIG_FEATURE_TAR_GNU_EXTENSIONS
595         if (longname) {
596                 tar_entry->name = longname;
597                 longname = NULL;
598         }
599         else if (linkname) {
600                 tar_entry->name = linkname;
601                 linkname = NULL;
602         } else
603 #endif
604         if (tar.formated.prefix[0] == 0) {
605                 tar_entry->name = strdup(tar.formated.name);
606         } else {                                              
607                 tar_entry->name = concat_path_file(tar.formated.prefix, tar.formated.name);
608         }
609
610         // tar_entry->name = xstrdup(tar.formated.name);
611
612 /*
613         parse_mode(tar.formated.mode, &tar_entry->mode);
614 */
615         tar_entry->mode = 07777 & strtol(tar.formated.mode, NULL, 8);
616
617         tar_entry->uid   = strtol(tar.formated.uid, NULL, 8);
618         tar_entry->gid   = strtol(tar.formated.gid, NULL, 8);
619         tar_entry->size  = strtol(tar.formated.size, NULL, 8);
620         tar_entry->mtime = strtol(tar.formated.mtime, NULL, 8);
621         tar_entry->link_name  = strlen(tar.formated.linkname) ? 
622             xstrdup(tar.formated.linkname) : NULL;
623         tar_entry->device = (strtol(tar.formated.devmajor, NULL, 8) << 8) +
624                 strtol(tar.formated.devminor, NULL, 8);
625
626         /* Fix mode, used by the old format */
627         switch (tar.formated.typeflag) {
628         /* hard links are detected as regular files with 0 size and a link name */
629         case '1':
630                 tar_entry->mode |= S_IFREG ;
631                 break;
632         case 0:
633         case '0':
634
635 # ifdef CONFIG_FEATURE_TAR_OLDGNU_COMPATABILITY
636                 if (last_char_is(tar_entry->name, '/')) {
637                         tar_entry->mode |= S_IFDIR;
638                 } else 
639 # endif
640                         tar_entry->mode |= S_IFREG;
641                 break;
642         case '2':
643                 tar_entry->mode |= S_IFLNK;
644                 break;
645         case '3':
646                 tar_entry->mode |= S_IFCHR;
647                 break;
648         case '4':
649                 tar_entry->mode |= S_IFBLK;
650                 break;
651         case '5':
652                 tar_entry->mode |= S_IFDIR;
653                 break;
654         case '6':
655                 tar_entry->mode |= S_IFIFO;
656                 break;
657 # ifdef CONFIG_FEATURE_TAR_GNU_EXTENSIONS
658         case 'L': {
659                         longname = xmalloc(tar_entry->size + 1);
660                         if(fread(longname, tar_entry->size, 1, tar_stream) != 1)
661                                 return NULL;
662                         longname[tar_entry->size] = '\0';
663                         archive_offset += tar_entry->size;
664
665                         return(get_header_tar(tar_stream));
666                 }
667         case 'K': {
668                         linkname = xmalloc(tar_entry->size + 1);
669                         if(fread(linkname, tar_entry->size, 1, tar_stream) != 1)
670                                 return NULL;
671                         linkname[tar_entry->size] = '\0';
672                         archive_offset += tar_entry->size;
673
674                         tar_entry->name = linkname;
675                         return(get_header_tar(tar_stream));
676                 }
677         case 'D':
678         case 'M':
679         case 'N':
680         case 'S':
681         case 'V':
682                 perror_msg("Ignoring GNU extension type %c", tar.formated.typeflag);
683 # endif
684         default:
685                 perror_msg("Unknown typeflag: 0x%x", tar.formated.typeflag);
686                 break;
687
688         }
689
690         return(tar_entry);
691 }
692
693 void free_header_tar(file_header_t *tar_entry)
694 {
695     if (tar_entry == NULL) {
696         return;
697     }
698
699     free(tar_entry->name);
700     free(tar_entry->link_name);
701
702     free(tar_entry);
703 }
704
705 #endif
706
707 #ifdef L_deb_extract
708 char *deb_extract(const char *package_filename, FILE *out_stream, 
709                   const int extract_function, const char *prefix, const char *filename)
710 {
711         FILE *deb_stream;
712         FILE *uncompressed_stream = NULL;
713         file_header_t *ar_header = NULL;
714         char **file_list = NULL;
715         char *output_buffer = NULL;
716         char *ared_file = NULL;
717         char ar_magic[8];
718         int gunzip_pid = 0;
719
720         if (filename != NULL) {
721                 file_list = xmalloc(sizeof(char *) * 2);
722                 file_list[0] = xstrdup(filename);
723                 file_list[1] = NULL;
724         }
725         
726         if (extract_function & extract_control_tar_gz) {
727                 ared_file = xstrdup("control.tar.gz");
728         }
729         else if (extract_function & extract_data_tar_gz) {              
730                 ared_file = xstrdup("data.tar.gz");
731         } else {
732                 fprintf(stderr, "no file specified to extract -- extract_function=%x\n", extract_function);
733                 return NULL;
734         }
735
736         /* open the debian package to be worked on */
737         deb_stream = wfopen(package_filename, "r");
738         if (deb_stream == NULL) {
739                 return(NULL);
740         }
741         /* set the buffer size */
742         setvbuf(deb_stream, NULL, _IOFBF, 0x8000);
743
744         /* check ar magic */
745         fread(ar_magic, 1, 8, deb_stream);
746         /*fprintf(stderr, "deb_extract ar_magic=%08x\n", *(long *)ar_magic);*/
747         if (strncmp(ar_magic,"!<arch>",7) == 0) {
748                 archive_offset = 8;
749
750                 while ((ar_header = get_header_ar(deb_stream)) != NULL) {
751                         if (strcmp(ared_file, ar_header->name) == 0) {
752                                 /* open a stream of decompressed data */
753                                 uncompressed_stream = gz_open(deb_stream, &gunzip_pid);
754                                 archive_offset = 0;
755                                 output_buffer = unarchive(uncompressed_stream, out_stream, get_header_tar, free_header_tar, extract_function, prefix, file_list);
756                         }
757                         seek_sub_file(deb_stream, ar_header->size);
758                         free (ar_header->name);
759                         free (ar_header);
760                 }
761                 gz_close(gunzip_pid);
762                 fclose(deb_stream);
763                 fclose(uncompressed_stream);
764                 free(ared_file);
765                 return(output_buffer);
766         } else if (strncmp(ar_magic, "\037\213", 2) == 0) {
767                 /* it's a gz file, let's assume it's an opkg */
768                 int unzipped_opkg_pid;
769                 FILE *unzipped_opkg_stream;
770                 file_header_t *tar_header;
771                 archive_offset = 0;
772                 fseek(deb_stream, 0, SEEK_SET);
773                 unzipped_opkg_stream = gz_open(deb_stream, &unzipped_opkg_pid);
774                 
775                 /*fprintf(stderr, __FUNCTION__ ": processing opkg %s -- ared_file=%s\n", package_filename, ared_file);*/
776                 /* walk through outer tar file to find ared_file */
777                 while ((tar_header = get_header_tar(unzipped_opkg_stream)) != NULL) {
778                         int name_offset = 0;
779                         if (strncmp(tar_header->name, "./", 2) == 0)
780                                 name_offset = 2;
781                         if (strcmp(ared_file, tar_header->name+name_offset) == 0) {
782                                 /* open a stream of decompressed data */
783                                 uncompressed_stream = gz_open(unzipped_opkg_stream, &gunzip_pid);
784                                 archive_offset = 0;
785                                 /*fprintf(stderr, __FUNCTION__ ":%d: here -- found file\n", __LINE__);*/
786                                 output_buffer = unarchive(uncompressed_stream, 
787                                                           out_stream, 
788                                                           get_header_tar,
789                                                           free_header_tar,
790                                                           extract_function, 
791                                                           prefix, 
792                                                           file_list);
793                                 /*fprintf(stderr, __FUNCTION__ ":%d: unarchive complete\n", __LINE__);*/
794                                 free_header_tar(tar_header);
795                                 gz_close(gunzip_pid);
796                                 fclose(uncompressed_stream);
797                                 break;
798                         }
799                         seek_sub_file(unzipped_opkg_stream, tar_header->size);
800                         free_header_tar(tar_header);
801                 }
802                 gz_close(unzipped_opkg_pid);
803                 fclose(unzipped_opkg_stream);
804                 fclose(deb_stream);
805                 free(ared_file);
806                 /*fprintf(stderr, __FUNCTION__ ":%d: done\n", __LINE__);*/
807                 return output_buffer; 
808         } else {
809                 error_msg_and_die("invalid magic");
810         }
811
812 }
813 #endif