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.
32 #define CONFIG_FEATURE_TAR_OLDGNU_COMPATABILITY 1
33 #define CONFIG_FEATURE_TAR_GNU_EXTENSIONS
35 #ifdef CONFIG_FEATURE_TAR_GNU_EXTENSIONS
36 static char *longname
= NULL
;
37 static char *linkname
= NULL
;
44 seek_by_read(FILE* fd
, size_t len
)
46 ssize_t cc
, total
= 0;
50 cc
= fread(buf
, sizeof(buf
[0]),
51 len
> SEEK_BUF
? SEEK_BUF
: len
,
57 if(feof(fd
) || ferror(fd
))
64 seek_sub_file(FILE *fd
, const int count
)
66 archive_offset
+= count
;
68 /* Do not use fseek() on a pipe. It may fail with ESPIPE, leaving the
69 * stream at an undefined location.
71 seek_by_read(fd
, count
);
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
80 * prefix doesnt have to be just a directory, it may prefix the filename as well.
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
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
90 extract_archive(FILE *src_stream
, FILE *out_stream
,
91 const file_header_t
*file_entry
, const int function
,
95 FILE *dst_stream
= NULL
;
96 char *full_name
= NULL
;
97 char *full_link_name
= NULL
;
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) {
111 if (strlen(path
) == 0)
112 /* Do nothing, current dir already exists. */
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
);
124 full_name
= xstrdup(file_entry
->name
);
125 if ( file_entry
->link_name
)
126 full_link_name
= xstrdup(file_entry
->link_name
);
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
;
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
;
145 else if (function
& extract_all_to_fs
) {
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 */
155 if ((function
& extract_quiet
) != extract_quiet
) {
157 error_msg("%s not created: newer or same age file exists", file_entry
->name
);
159 seek_sub_file(src_stream
, file_entry
->size
);
163 if (function
& extract_create_leading_dirs
) { /* Create leading directories with default umask */
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
) {
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
) {
181 perror_msg("Cannot link from %s to '%s'",
182 file_entry
->name
, file_entry
->link_name
);
186 if ((dst_stream
= wfopen(full_name
, "w")) == NULL
) {
188 seek_sub_file(src_stream
, file_entry
->size
);
191 archive_offset
+= file_entry
->size
;
192 *err
= copy_file_chunk(src_stream
, dst_stream
, file_entry
->size
);
198 if (mkdir(full_name
, file_entry
->mode
) < 0) {
199 if ((function
& extract_quiet
) != extract_quiet
) {
201 perror_msg("Cannot make dir %s", full_name
);
207 if (symlink(file_entry
->link_name
, full_name
) < 0) {
208 if ((function
& extract_quiet
) != extract_quiet
) {
210 perror_msg("Cannot create symlink from %s to '%s'", file_entry
->name
, file_entry
->link_name
);
219 if (mknod(full_name
, file_entry
->mode
, file_entry
->device
) == -1) {
220 if ((function
& extract_quiet
) != extract_quiet
) {
222 perror_msg("Cannot create node %s", file_entry
->name
);
229 perror_msg("Don't know how to handle %s", full_name
);
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
);
241 if (function
& extract_preserve_date
) {
242 t
.actime
= file_entry
->mtime
;
243 t
.modtime
= file_entry
->mtime
;
244 utime(full_name
, &t
);
246 chown(full_name
, file_entry
->uid
, file_entry
->gid
);
247 chmod(full_name
, file_entry
->mode
);
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
);
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
));
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
);
270 if ( full_link_name
)
271 free(full_link_name
);
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
,
282 const char **extract_names
,
285 file_header_t
*file_entry
;
293 while ((file_entry
= get_headers(src_stream
)) != NULL
) {
296 if (extract_names
!= NULL
) {
297 int found_flag
= FALSE
;
298 for(i
= 0; extract_names
[i
] != 0; i
++) {
299 if (strcmp(extract_names
[i
], file_entry
->name
) == 0) {
304 if (extract_function
& extract_exclude_list
) {
305 if (found_flag
== TRUE
) {
306 extract_flag
= FALSE
;
309 /* If its not found in the include list dont extract it */
310 if (found_flag
== FALSE
) {
311 extract_flag
= FALSE
;
316 if (extract_flag
== TRUE
) {
317 buffer
= extract_archive(src_stream
, out_stream
,
318 file_entry
, extract_function
,
320 *err
= 0; /* XXX: ignore extraction errors */
322 free_headers(file_entry
);
326 /* seek past the data entry */
327 seek_sub_file(src_stream
, file_entry
->size
);
329 free_headers(file_entry
);
335 static file_header_t
*
336 get_header_ar(FILE *src_stream
)
338 file_header_t
*typed
;
351 static char *ar_long_names
;
353 if (fread(ar
.raw
, 1, 60, src_stream
) != 60) {
356 archive_offset
+= 60;
357 /* align the headers based on the header magic */
358 if ((ar
.formated
.magic
[0] != '`') || (ar
.formated
.magic
[1] != '\n')) {
359 /* some version of ar, have an extra '\n' after each data entry,
360 * this puts the next header out by 1 */
361 if (ar
.formated
.magic
[1] != '`') {
362 error_msg("Invalid magic");
365 /* read the next char out of what would be the data section,
366 * if its a '\n' then it is a valid header offset by 1*/
368 if (fgetc(src_stream
) != '\n') {
369 error_msg("Invalid magic");
372 /* fix up the header, we started reading 1 byte too early */
373 /* raw_header[60] wont be '\n' as it should, but it doesnt matter */
374 memmove(ar
.raw
, &ar
.raw
[1], 59);
377 typed
= (file_header_t
*) xcalloc(1, sizeof(file_header_t
));
379 typed
->size
= (size_t) atoi(ar
.formated
.size
);
380 /* long filenames have '/' as the first character */
381 if (ar
.formated
.name
[0] == '/') {
382 if (ar
.formated
.name
[1] == '/') {
383 /* If the second char is a '/' then this entries data section
384 * stores long filename for multiple entries, they are stored
385 * in static variable long_names for use in future entries */
386 ar_long_names
= (char *) xrealloc(ar_long_names
, typed
->size
);
387 fread(ar_long_names
, 1, typed
->size
, src_stream
);
388 archive_offset
+= typed
->size
;
389 /* This ar entries data section only contained filenames for other records
390 * they are stored in the static ar_long_names for future reference */
391 return (get_header_ar(src_stream
)); /* Return next header */
392 } else if (ar
.formated
.name
[1] == ' ') {
393 /* This is the index of symbols in the file for compilers */
394 seek_sub_file(src_stream
, typed
->size
);
395 return (get_header_ar(src_stream
)); /* Return next header */
397 /* The number after the '/' indicates the offset in the ar data section
398 (saved in variable long_name) that conatains the real filename */
399 if (!ar_long_names
) {
400 error_msg("Cannot resolve long file name");
403 typed
->name
= xstrdup(ar_long_names
+ atoi(&ar
.formated
.name
[1]));
406 /* short filenames */
407 typed
->name
= xcalloc(1, 16);
408 strncpy(typed
->name
, ar
.formated
.name
, 16);
410 typed
->name
[strcspn(typed
->name
, " /")]='\0';
412 /* convert the rest of the now valid char header to its typed struct */
413 parse_mode(ar
.formated
.mode
, &typed
->mode
);
414 typed
->mtime
= atoi(ar
.formated
.date
);
415 typed
->uid
= atoi(ar
.formated
.uid
);
416 typed
->gid
= atoi(ar
.formated
.gid
);
422 free_header_ar(file_header_t
*ar_entry
)
424 if (ar_entry
== NULL
)
427 free(ar_entry
->name
);
428 if (ar_entry
->link_name
)
429 free(ar_entry
->link_name
);
435 static file_header_t
*
436 get_header_tar(FILE *tar_stream
)
439 unsigned char raw
[512];
441 char name
[100]; /* 0-99 */
442 char mode
[8]; /* 100-107 */
443 char uid
[8]; /* 108-115 */
444 char gid
[8]; /* 116-123 */
445 char size
[12]; /* 124-135 */
446 char mtime
[12]; /* 136-147 */
447 char chksum
[8]; /* 148-155 */
448 char typeflag
; /* 156-156 */
449 char linkname
[100]; /* 157-256 */
450 char magic
[6]; /* 257-262 */
451 char version
[2]; /* 263-264 */
452 char uname
[32]; /* 265-296 */
453 char gname
[32]; /* 297-328 */
454 char devmajor
[8]; /* 329-336 */
455 char devminor
[8]; /* 337-344 */
456 char prefix
[155]; /* 345-499 */
457 char padding
[12]; /* 500-512 */
460 file_header_t
*tar_entry
= NULL
;
464 if (archive_offset
% 512 != 0) {
465 seek_sub_file(tar_stream
, 512 - (archive_offset
% 512));
468 if (fread(tar
.raw
, 1, 512, tar_stream
) != 512) {
469 /* Unfortunatly its common for tar files to have all sorts of
470 * trailing garbage, fail silently */
471 // error_msg("Couldnt read header");
474 archive_offset
+= 512;
476 /* Check header has valid magic, unfortunately some tar files
477 * have empty (0'ed) tar entries at the end, which will
478 * cause this to fail, so fail silently for now
480 if (strncmp(tar
.formated
.magic
, "ustar", 5) != 0) {
481 #ifdef CONFIG_FEATURE_TAR_OLDGNU_COMPATABILITY
482 if (strncmp(tar
.formated
.magic
, "\0\0\0\0\0", 5) != 0)
487 /* Do checksum on headers */
488 for (i
= 0; i
< 148 ; i
++) {
492 for (i
= 156; i
< 512 ; i
++) {
495 if (sum
!= strtol(tar
.formated
.chksum
, NULL
, 8)) {
496 if ( strtol(tar
.formated
.chksum
,NULL
,8) != 0 )
497 error_msg("Invalid tar header checksum");
501 /* convert to type'ed variables */
502 tar_entry
= xcalloc(1, sizeof(file_header_t
));
504 #ifdef CONFIG_FEATURE_TAR_GNU_EXTENSIONS
506 tar_entry
->name
= longname
;
510 tar_entry
->name
= linkname
;
515 tar_entry
->name
= xstrndup(tar
.formated
.name
, 100);
517 if (tar
.formated
.prefix
[0]) {
518 char *temp
= tar_entry
->name
;
519 char *prefixTemp
= xstrndup(tar
.formated
.prefix
, 155);
520 tar_entry
->name
= concat_path_file(prefixTemp
, temp
);
526 // tar_entry->name = xstrdup(tar.formated.name);
529 parse_mode(tar.formated.mode, &tar_entry->mode);
531 tar_entry
->mode
= 07777 & strtol(tar
.formated
.mode
, NULL
, 8);
533 tar_entry
->uid
= strtol(tar
.formated
.uid
, NULL
, 8);
534 tar_entry
->gid
= strtol(tar
.formated
.gid
, NULL
, 8);
535 tar_entry
->size
= strtol(tar
.formated
.size
, NULL
, 8);
536 tar_entry
->mtime
= strtol(tar
.formated
.mtime
, NULL
, 8);
537 tar_entry
->link_name
= *tar
.formated
.linkname
!= '\0' ? xstrndup(tar
.formated
.linkname
, 100) : NULL
;
538 tar_entry
->device
= (strtol(tar
.formated
.devmajor
, NULL
, 8) << 8) +
539 strtol(tar
.formated
.devminor
, NULL
, 8);
541 /* Fix mode, used by the old format */
542 switch (tar
.formated
.typeflag
) {
543 /* hard links are detected as regular files with 0 size and a link name */
545 tar_entry
->mode
|= S_IFREG
;
550 # ifdef CONFIG_FEATURE_TAR_OLDGNU_COMPATABILITY
551 if (last_char_is(tar_entry
->name
, '/')) {
552 tar_entry
->mode
|= S_IFDIR
;
555 tar_entry
->mode
|= S_IFREG
;
558 tar_entry
->mode
|= S_IFLNK
;
561 tar_entry
->mode
|= S_IFCHR
;
564 tar_entry
->mode
|= S_IFBLK
;
567 tar_entry
->mode
|= S_IFDIR
;
570 tar_entry
->mode
|= S_IFIFO
;
572 # ifdef CONFIG_FEATURE_TAR_GNU_EXTENSIONS
574 longname
= xmalloc(tar_entry
->size
+ 1);
575 if(fread(longname
, tar_entry
->size
, 1, tar_stream
) != 1)
577 longname
[tar_entry
->size
] = '\0';
578 archive_offset
+= tar_entry
->size
;
580 return(get_header_tar(tar_stream
));
583 linkname
= xmalloc(tar_entry
->size
+ 1);
584 if(fread(linkname
, tar_entry
->size
, 1, tar_stream
) != 1)
586 linkname
[tar_entry
->size
] = '\0';
587 archive_offset
+= tar_entry
->size
;
589 tar_entry
->name
= linkname
;
590 return(get_header_tar(tar_stream
));
597 perror_msg("Ignoring GNU extension type %c", tar
.formated
.typeflag
);
600 perror_msg("Unknown typeflag: 0x%x", tar
.formated
.typeflag
);
609 free_header_tar(file_header_t
*tar_entry
)
611 if (tar_entry
== NULL
)
614 free(tar_entry
->name
);
615 if (tar_entry
->link_name
);
616 free(tar_entry
->link_name
);
622 deb_extract(const char *package_filename
, FILE *out_stream
,
623 const int extract_function
, const char *prefix
,
624 const char *filename
, int *err
)
626 FILE *deb_stream
= NULL
;
627 file_header_t
*ar_header
= NULL
;
628 const char **file_list
= NULL
;
629 char *output_buffer
= NULL
;
630 char *ared_file
= NULL
;
636 if (filename
!= NULL
) {
637 file_list
= xmalloc(sizeof(char *) * 2);
638 file_list
[0] = filename
;
642 if (extract_function
& extract_control_tar_gz
) {
643 ared_file
= "control.tar.gz";
645 else if (extract_function
& extract_data_tar_gz
) {
646 ared_file
= "data.tar.gz";
648 opkg_msg(ERROR
, "Internal error: extract_function=%x\n",
654 /* open the debian package to be worked on */
655 deb_stream
= wfopen(package_filename
, "r");
656 if (deb_stream
== NULL
) {
660 /* set the buffer size */
661 setvbuf(deb_stream
, NULL
, _IOFBF
, 0x8000);
664 fread(ar_magic
, 1, 8, deb_stream
);
666 if (strncmp(ar_magic
,"!<arch>",7) == 0) {
669 while ((ar_header
= get_header_ar(deb_stream
)) != NULL
) {
670 if (strcmp(ared_file
, ar_header
->name
) == 0) {
672 FILE *uncompressed_stream
;
673 /* open a stream of decompressed data */
674 uncompressed_stream
= gz_open(deb_stream
, &gunzip_pid
);
675 if (uncompressed_stream
== NULL
) {
681 output_buffer
= unarchive(uncompressed_stream
,
682 out_stream
, get_header_tar
,
684 extract_function
, prefix
,
686 fclose(uncompressed_stream
);
687 gz_err
= gz_close(gunzip_pid
);
690 free_header_ar(ar_header
);
693 if (fseek(deb_stream
, ar_header
->size
, SEEK_CUR
) == -1) {
694 opkg_perror(ERROR
, "Couldn't fseek into %s", package_filename
);
696 free_header_ar(ar_header
);
699 free_header_ar(ar_header
);
702 } else if (strncmp(ar_magic
, "\037\213", 2) == 0) {
703 /* it's a gz file, let's assume it's an opkg */
704 int unzipped_opkg_pid
;
705 FILE *unzipped_opkg_stream
;
706 file_header_t
*tar_header
;
708 if (fseek(deb_stream
, 0, SEEK_SET
) == -1) {
709 opkg_perror(ERROR
, "Couldn't fseek into %s", package_filename
);
713 unzipped_opkg_stream
= gz_open(deb_stream
, &unzipped_opkg_pid
);
714 if (unzipped_opkg_stream
== NULL
) {
719 /* walk through outer tar file to find ared_file */
720 while ((tar_header
= get_header_tar(unzipped_opkg_stream
)) != NULL
) {
722 if (strncmp(tar_header
->name
, "./", 2) == 0)
724 if (strcmp(ared_file
, tar_header
->name
+name_offset
) == 0) {
726 FILE *uncompressed_stream
;
727 /* open a stream of decompressed data */
728 uncompressed_stream
= gz_open(unzipped_opkg_stream
, &gunzip_pid
);
729 if (uncompressed_stream
== NULL
) {
735 output_buffer
= unarchive(uncompressed_stream
,
744 free_header_tar(tar_header
);
745 fclose(uncompressed_stream
);
746 gz_err
= gz_close(gunzip_pid
);
749 free_header_tar(tar_header
);
752 seek_sub_file(unzipped_opkg_stream
, tar_header
->size
);
753 free_header_tar(tar_header
);
755 fclose(unzipped_opkg_stream
);
756 gz_err
= gz_close(unzipped_opkg_pid
);
763 error_msg("%s: invalid magic", package_filename
);
772 return output_buffer
;