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