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