2 * Copyright (C) 2000 by Glenn McGrath
3 * Copyright (C) 2001 by Laurence Anderson
5 * Based on previous work by busybox developers and others.
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.
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.
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.
30 #define CONFIG_FEATURE_TAR_OLDGNU_COMPATABILITY 1
31 #define CONFIG_FEATURE_TAR_GNU_EXTENSIONS
33 #ifdef CONFIG_FEATURE_TAR_GNU_EXTENSIONS
34 static char *longname
= NULL
;
35 static char *linkname
= NULL
;
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
);
44 #ifdef L_archive_offset
47 extern off_t archive_offset
;
50 #ifdef L_seek_sub_file
52 ssize_t
seek_by_read(FILE* fd
, size_t len
)
54 ssize_t cc
, total
= 0;
58 cc
= fread(buf
, sizeof(buf
[0]),
59 len
> SEEK_BUF
? SEEK_BUF
: len
,
65 if(feof(fd
) || ferror(fd
))
71 void seek_sub_file(FILE *src_stream
, const int count
)
73 /* Try to fseek as faster */
74 archive_offset
+= count
;
75 seek_by_read(src_stream
, count
);
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
86 * prefix doesnt have to be just a directory, it may prefix the filename as well.
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
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
95 char *extract_archive(FILE *src_stream
, FILE *out_stream
, const file_header_t
*file_entry
,
96 const int function
, const char *prefix
)
98 FILE *dst_stream
= NULL
;
99 char *full_name
= NULL
;
100 char *full_link_name
= NULL
;
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) {
113 if (strlen(path
) == 0) {
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
);
126 full_name
= strdup(file_entry
->name
);
127 if ( file_entry
->link_name
)
128 full_link_name
= strdup(file_entry
->link_name
);
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
;
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
;
147 else if (function
& extract_all_to_fs
) {
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 */
157 if ((function
& extract_quiet
) != extract_quiet
) {
158 error_msg("%s not created: newer or same age file exists", file_entry
->name
);
160 seek_sub_file(src_stream
, file_entry
->size
);
164 if (function
& extract_create_leading_dirs
) { /* Create leading directories with default umask */
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");
175 switch(file_entry
->mode
& S_IFMT
) {
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
);
185 if ((dst_stream
= wfopen(full_name
, "w")) == NULL
) {
186 seek_sub_file(src_stream
, file_entry
->size
);
189 archive_offset
+= file_entry
->size
;
190 copy_file_chunk(src_stream
, dst_stream
, file_entry
->size
);
196 if (mkdir(full_name
, file_entry
->mode
) < 0) {
197 if ((function
& extract_quiet
) != extract_quiet
) {
198 perror_msg("extract_archive: %s", full_name
);
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
);
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
);
223 perror_msg("Don't know how to handle %s", full_name
);
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
);
235 if (function
& extract_preserve_date
) {
236 t
.actime
= file_entry
->mtime
;
237 t
.modtime
= file_entry
->mtime
;
238 utime(full_name
, &t
);
240 chown(full_name
, file_entry
->uid
, file_entry
->gid
);
241 chmod(full_name
, file_entry
->mode
);
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
);
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
));
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
);
264 if ( full_link_name
)
265 free(full_link_name
);
267 return(buffer
); /* Maybe we should say if failed */
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
, char **extract_names
)
277 file_header_t
*file_entry
;
283 while ((file_entry
= get_headers(src_stream
)) != NULL
) {
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) {
294 if (extract_function
& extract_exclude_list
) {
295 if (found_flag
== TRUE
) {
296 extract_flag
= FALSE
;
299 /* If its not found in the include list dont extract it */
300 if (found_flag
== FALSE
) {
301 extract_flag
= FALSE
;
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"); */
312 /* seek past the data entry */
313 seek_sub_file(src_stream
, file_entry
->size
);
315 free_headers(file_entry
);
317 /*fprintf(stderr, __FUNCTION__ " goin home\n");*/
323 #ifdef L_get_header_ar
324 file_header_t
*get_header_ar(FILE *src_stream
)
326 file_header_t
*typed
;
339 static char *ar_long_names
;
341 if (fread(ar
.raw
, 1, 60, src_stream
) != 60) {
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");
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*/
356 if (fgetc(src_stream
) != '\n') {
357 error_msg("Invalid magic");
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);
365 typed
= (file_header_t
*) xcalloc(1, sizeof(file_header_t
));
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 */
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");
391 typed
->name
= xstrdup(ar_long_names
+ atoi(&ar
.formated
.name
[1]));
394 /* short filenames */
395 typed
->name
= xcalloc(1, 16);
396 strncpy(typed
->name
, ar
.formated
.name
, 16);
398 typed
->name
[strcspn(typed
->name
, " /")]='\0';
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
);
409 void free_header_ar(file_header_t
*ar_entry
)
411 if (ar_entry
== NULL
) {
415 free(ar_entry
->name
);
416 free(ar_entry
->link_name
);
422 #ifdef L_get_header_cpio
424 file_header_t
*entry
;
426 struct hardlinks
*next
;
429 file_header_t
*get_header_cpio(FILE *src_stream
)
431 file_header_t
*cpio_entry
= NULL
;
432 char cpio_header
[110];
435 int major
, minor
, nlink
, inode
;
436 static struct hardlinks
*saved_hardlinks
= NULL
;
437 static int pending_hardlinks
= 0;
440 if (pending_hardlinks
) { /* Deal with any pending hardlinks */
441 struct hardlinks
*tmp
= saved_hardlinks
, *oldtmp
= NULL
;
443 if (tmp
->entry
->link_name
) { /* Found a hardlink ready to be extracted */
444 cpio_entry
= tmp
->entry
;
445 if (oldtmp
) oldtmp
->next
= tmp
->next
; /* Remove item from linked list */
446 else saved_hardlinks
= tmp
->next
;
453 pending_hardlinks
= 0; /* No more pending hardlinks, read next file entry */
456 /* There can be padding before archive header */
457 seek_sub_file(src_stream
, (4 - (archive_offset
% 4)) % 4);
458 if (fread(cpio_header
, 1, 110, src_stream
) == 110) {
459 archive_offset
+= 110;
460 if (strncmp(cpio_header
, "07070", 5) != 0) {
461 error_msg("Unsupported format or invalid magic");
464 switch (cpio_header
[5]) {
465 case '2': /* "crc" header format */
466 /* Doesnt do the crc check yet */
467 case '1': /* "newc" header format */
468 cpio_entry
= (file_header_t
*) xcalloc(1, sizeof(file_header_t
));
469 sscanf(cpio_header
, "%6c%8x%8x%8x%8x%8x%8lx%8lx%16c%8x%8x%8x%8c",
470 dummy
, &inode
, (unsigned int*)&cpio_entry
->mode
,
471 (unsigned int*)&cpio_entry
->uid
, (unsigned int*)&cpio_entry
->gid
,
472 &nlink
, &cpio_entry
->mtime
, (unsigned long*)&cpio_entry
->size
,
473 dummy
, &major
, &minor
, &namesize
, dummy
);
475 cpio_entry
->name
= (char *) xcalloc(1, namesize
);
476 fread(cpio_entry
->name
, 1, namesize
, src_stream
); /* Read in filename */
477 archive_offset
+= namesize
;
478 /* Skip padding before file contents */
479 seek_sub_file(src_stream
, (4 - (archive_offset
% 4)) % 4);
480 if (strcmp(cpio_entry
->name
, "TRAILER!!!") == 0) {
481 printf("%d blocks\n", (int) (archive_offset
% 512 ? (archive_offset
/ 512) + 1 : archive_offset
/ 512)); /* Always round up */
482 if (saved_hardlinks
) { /* Bummer - we still have unresolved hardlinks */
483 struct hardlinks
*tmp
= saved_hardlinks
, *oldtmp
= NULL
;
485 error_msg("%s not created: cannot resolve hardlink", tmp
->entry
->name
);
488 free (oldtmp
->entry
->name
);
489 free (oldtmp
->entry
);
492 saved_hardlinks
= NULL
;
493 pending_hardlinks
= 0;
498 if (S_ISLNK(cpio_entry
->mode
)) {
499 cpio_entry
->link_name
= (char *) xcalloc(1, cpio_entry
->size
+ 1);
500 fread(cpio_entry
->link_name
, 1, cpio_entry
->size
, src_stream
);
501 archive_offset
+= cpio_entry
->size
;
502 cpio_entry
->size
= 0; /* Stop possiable seeks in future */
504 if (nlink
> 1 && !S_ISDIR(cpio_entry
->mode
)) {
505 if (cpio_entry
->size
== 0) { /* Put file on a linked list for later */
506 struct hardlinks
*new = xmalloc(sizeof(struct hardlinks
));
507 new->next
= saved_hardlinks
;
509 new->entry
= cpio_entry
;
510 saved_hardlinks
= new;
511 return(get_header_cpio(src_stream
)); /* Recurse to next file */
512 } else { /* Found the file with data in */
513 struct hardlinks
*tmp
= saved_hardlinks
;
514 pending_hardlinks
= 1;
516 if (tmp
->inode
== inode
) {
517 tmp
->entry
->link_name
= xstrdup(cpio_entry
->name
);
522 if (nlink
> 1) error_msg("error resolving hardlink: did you create the archive with GNU cpio 2.0-2.2?");
525 cpio_entry
->device
= (major
<< 8) | minor
;
528 error_msg("Unsupported format");
531 if (ferror(src_stream
) || feof(src_stream
)) {
532 perror_msg("Stream error");
540 #ifdef L_get_header_tar
541 file_header_t
*get_header_tar(FILE *tar_stream
)
545 unsigned char raw
[512];
547 char name
[100]; /* 0-99 */
548 char mode
[8]; /* 100-107 */
549 char uid
[8]; /* 108-115 */
550 char gid
[8]; /* 116-123 */
551 char size
[12]; /* 124-135 */
552 char mtime
[12]; /* 136-147 */
553 char chksum
[8]; /* 148-155 */
554 char typeflag
; /* 156-156 */
555 char linkname
[100]; /* 157-256 */
556 char magic
[6]; /* 257-262 */
557 char version
[2]; /* 263-264 */
558 char uname
[32]; /* 265-296 */
559 char gname
[32]; /* 297-328 */
560 char devmajor
[8]; /* 329-336 */
561 char devminor
[8]; /* 337-344 */
562 char prefix
[155]; /* 345-499 */
563 char padding
[12]; /* 500-512 */
566 file_header_t
*tar_entry
= NULL
;
570 if (archive_offset
% 512 != 0) {
571 seek_sub_file(tar_stream
, 512 - (archive_offset
% 512));
574 if (fread(tar
.raw
, 1, 512, tar_stream
) != 512) {
575 /* Unfortunatly its common for tar files to have all sorts of
576 * trailing garbage, fail silently */
577 // error_msg("Couldnt read header");
580 archive_offset
+= 512;
582 /* Check header has valid magic, unfortunately some tar files
583 * have empty (0'ed) tar entries at the end, which will
584 * cause this to fail, so fail silently for now
586 if (strncmp(tar
.formated
.magic
, "ustar", 5) != 0) {
587 #ifdef CONFIG_FEATURE_TAR_OLDGNU_COMPATABILITY
588 if (strncmp(tar
.formated
.magic
, "\0\0\0\0\0", 5) != 0)
593 /* Do checksum on headers */
594 for (i
= 0; i
< 148 ; i
++) {
598 for (i
= 156; i
< 512 ; i
++) {
601 if (sum
!= strtol(tar
.formated
.chksum
, NULL
, 8)) {
602 if ( strtol(tar
.formated
.chksum
,NULL
,8) != 0 )
603 error_msg("Invalid tar header checksum");
607 /* convert to type'ed variables */
608 tar_entry
= xcalloc(1, sizeof(file_header_t
));
610 #ifdef CONFIG_FEATURE_TAR_GNU_EXTENSIONS
612 tar_entry
->name
= longname
;
616 tar_entry
->name
= linkname
;
621 tar_entry
->name
= xstrndup(tar
.formated
.name
, 100);
623 if (tar
.formated
.prefix
[0]) {
624 char *temp
= tar_entry
->name
;
625 char *prefixTemp
= xstrndup(tar
.formated
.prefix
, 155);
626 tar_entry
->name
= concat_path_file(prefixTemp
, temp
);
632 // tar_entry->name = xstrdup(tar.formated.name);
635 parse_mode(tar.formated.mode, &tar_entry->mode);
637 tar_entry
->mode
= 07777 & strtol(tar
.formated
.mode
, NULL
, 8);
639 tar_entry
->uid
= strtol(tar
.formated
.uid
, NULL
, 8);
640 tar_entry
->gid
= strtol(tar
.formated
.gid
, NULL
, 8);
641 tar_entry
->size
= strtol(tar
.formated
.size
, NULL
, 8);
642 tar_entry
->mtime
= strtol(tar
.formated
.mtime
, NULL
, 8);
643 tar_entry
->link_name
= *tar
.formated
.linkname
!= '\0' ? xstrndup(tar
.formated
.linkname
, 100) : NULL
;
644 tar_entry
->device
= (strtol(tar
.formated
.devmajor
, NULL
, 8) << 8) +
645 strtol(tar
.formated
.devminor
, NULL
, 8);
647 /* Fix mode, used by the old format */
648 switch (tar
.formated
.typeflag
) {
649 /* hard links are detected as regular files with 0 size and a link name */
651 tar_entry
->mode
|= S_IFREG
;
656 # ifdef CONFIG_FEATURE_TAR_OLDGNU_COMPATABILITY
657 if (last_char_is(tar_entry
->name
, '/')) {
658 tar_entry
->mode
|= S_IFDIR
;
661 tar_entry
->mode
|= S_IFREG
;
664 tar_entry
->mode
|= S_IFLNK
;
667 tar_entry
->mode
|= S_IFCHR
;
670 tar_entry
->mode
|= S_IFBLK
;
673 tar_entry
->mode
|= S_IFDIR
;
676 tar_entry
->mode
|= S_IFIFO
;
678 # ifdef CONFIG_FEATURE_TAR_GNU_EXTENSIONS
680 longname
= xmalloc(tar_entry
->size
+ 1);
681 if(fread(longname
, tar_entry
->size
, 1, tar_stream
) != 1)
683 longname
[tar_entry
->size
] = '\0';
684 archive_offset
+= tar_entry
->size
;
686 return(get_header_tar(tar_stream
));
689 linkname
= xmalloc(tar_entry
->size
+ 1);
690 if(fread(linkname
, tar_entry
->size
, 1, tar_stream
) != 1)
692 linkname
[tar_entry
->size
] = '\0';
693 archive_offset
+= tar_entry
->size
;
695 tar_entry
->name
= linkname
;
696 return(get_header_tar(tar_stream
));
703 perror_msg("Ignoring GNU extension type %c", tar
.formated
.typeflag
);
706 perror_msg("Unknown typeflag: 0x%x", tar
.formated
.typeflag
);
714 void free_header_tar(file_header_t
*tar_entry
)
716 if (tar_entry
== NULL
) {
720 free(tar_entry
->name
);
721 free(tar_entry
->link_name
);
729 char *deb_extract(const char *package_filename
, FILE *out_stream
,
730 const int extract_function
, const char *prefix
, const char *filename
)
733 FILE *uncompressed_stream
= NULL
;
734 file_header_t
*ar_header
= NULL
;
735 char **file_list
= NULL
;
736 char *output_buffer
= NULL
;
737 char *ared_file
= NULL
;
741 if (filename
!= NULL
) {
742 file_list
= xmalloc(sizeof(char *) * 2);
743 file_list
[0] = xstrdup(filename
);
747 if (extract_function
& extract_control_tar_gz
) {
748 ared_file
= xstrdup("control.tar.gz");
750 else if (extract_function
& extract_data_tar_gz
) {
751 ared_file
= xstrdup("data.tar.gz");
753 fprintf(stderr
, "no file specified to extract -- extract_function=%x\n", extract_function
);
757 /* open the debian package to be worked on */
758 deb_stream
= wfopen(package_filename
, "r");
759 if (deb_stream
== NULL
) {
762 /* set the buffer size */
763 setvbuf(deb_stream
, NULL
, _IOFBF
, 0x8000);
766 fread(ar_magic
, 1, 8, deb_stream
);
767 /*fprintf(stderr, "deb_extract ar_magic=%08x\n", *(long *)ar_magic);*/
768 if (strncmp(ar_magic
,"!<arch>",7) == 0) {
771 while ((ar_header
= get_header_ar(deb_stream
)) != NULL
) {
772 if (strcmp(ared_file
, ar_header
->name
) == 0) {
773 /* open a stream of decompressed data */
774 uncompressed_stream
= gz_open(deb_stream
, &gunzip_pid
);
775 if (uncompressed_stream
== NULL
) {
780 output_buffer
= unarchive(uncompressed_stream
, out_stream
, get_header_tar
, free_header_tar
, extract_function
, prefix
, file_list
);
782 seek_sub_file(deb_stream
, ar_header
->size
);
783 free (ar_header
->name
);
786 gz_close(gunzip_pid
);
787 fclose(uncompressed_stream
);
789 } else if (strncmp(ar_magic
, "\037\213", 2) == 0) {
790 /* it's a gz file, let's assume it's an opkg */
791 int unzipped_opkg_pid
;
792 FILE *unzipped_opkg_stream
;
793 file_header_t
*tar_header
;
795 fseek(deb_stream
, 0, SEEK_SET
);
796 unzipped_opkg_stream
= gz_open(deb_stream
, &unzipped_opkg_pid
);
797 if (unzipped_opkg_stream
== NULL
) {
801 /*fprintf(stderr, __FUNCTION__ ": processing opkg %s -- ared_file=%s\n", package_filename, ared_file);*/
802 /* walk through outer tar file to find ared_file */
803 while ((tar_header
= get_header_tar(unzipped_opkg_stream
)) != NULL
) {
805 if (strncmp(tar_header
->name
, "./", 2) == 0)
807 if (strcmp(ared_file
, tar_header
->name
+name_offset
) == 0) {
808 /* open a stream of decompressed data */
809 uncompressed_stream
= gz_open(unzipped_opkg_stream
, &gunzip_pid
);
810 if (uncompressed_stream
== NULL
) {
814 /*fprintf(stderr, __FUNCTION__ ":%d: here -- found file\n", __LINE__);*/
815 output_buffer
= unarchive(uncompressed_stream
,
822 /*fprintf(stderr, __FUNCTION__ ":%d: unarchive complete\n", __LINE__);*/
823 free_header_tar(tar_header
);
824 gz_close(gunzip_pid
);
825 fclose(uncompressed_stream
);
828 seek_sub_file(unzipped_opkg_stream
, tar_header
->size
);
829 free_header_tar(tar_header
);
831 gz_close(unzipped_opkg_pid
);
832 fclose(unzipped_opkg_stream
);
833 /*fprintf(stderr, __FUNCTION__ ":%d: done\n", __LINE__);*/
836 error_msg_and_die("invalid magic");
848 return output_buffer
;