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