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);
43 #ifdef L_archive_offset
46 extern off_t archive_offset;
49 #ifdef L_seek_sub_file
50 void seek_sub_file(FILE *src_stream, const int count)
53 /* Try to fseek as faster */
54 archive_offset += count;
55 if (fseek(src_stream, count, SEEK_CUR) != 0 && errno == ESPIPE) {
56 for (i = 0; i < count; i++) {
66 #ifdef L_extract_archive
67 /* Extract the data postioned at src_stream to either filesystem, stdout or
68 * buffer depending on the value of 'function' which is defined in libbb.h
70 * prefix doesnt have to be just a directory, it may prefix the filename as well.
72 * e.g. '/var/lib/dpkg/info/dpkg.' will extract all files to the base bath
73 * '/var/lib/dpkg/info/' and all files/dirs created in that dir will have
74 * 'dpkg.' as their prefix
76 * For this reason if prefix does point to a dir then it must end with a
77 * trailing '/' or else the last dir will be assumed to be the file prefix
79 char *extract_archive(FILE *src_stream, FILE *out_stream, const file_header_t *file_entry,
80 const int function, const char *prefix)
82 FILE *dst_stream = NULL;
83 char *full_name = NULL;
84 char *full_link_name = NULL;
90 /* prefix doesnt have to be a proper path it may prepend
91 * the filename as well */
93 /* strip leading '/' in filename to extract as prefix may not be dir */
94 /* Cant use concat_path_file here as prefix might not be a directory */
95 char *path = file_entry->name;
96 if (strncmp("./", path, 2) == 0) {
98 if (strlen(path) == 0) {
102 full_name = xmalloc(strlen(prefix) + strlen(path) + 1);
103 strcpy(full_name, prefix);
104 strcat(full_name, path);
105 if ( file_entry->link_name ){
106 full_link_name = xmalloc(strlen(prefix) + strlen(file_entry->link_name) + 1);
107 strcpy(full_link_name, prefix);
108 strcat(full_link_name, file_entry->link_name);
111 full_name = strdup(file_entry->name);
112 if ( file_entry->link_name )
113 full_link_name = strdup(file_entry->link_name);
117 if (function & extract_to_stdout) {
118 if (S_ISREG(file_entry->mode)) {
119 copy_file_chunk(src_stream, out_stream, file_entry->size);
120 archive_offset += file_entry->size;
123 else if (function & extract_one_to_buffer) {
124 if (S_ISREG(file_entry->mode)) {
125 buffer = (char *) xmalloc(file_entry->size + 1);
126 fread(buffer, 1, file_entry->size, src_stream);
127 buffer[file_entry->size] = '\0';
128 archive_offset += file_entry->size;
132 else if (function & extract_all_to_fs) {
135 stat_res = lstat (full_name, &oldfile);
136 if (stat_res == 0) { /* The file already exists */
137 if ((function & extract_unconditional) || (oldfile.st_mtime < file_entry->mtime)) {
138 if (!S_ISDIR(oldfile.st_mode)) {
139 unlink(full_name); /* Directories might not be empty etc */
142 if ((function & extract_quiet) != extract_quiet) {
143 error_msg("%s not created: newer or same age file exists", file_entry->name);
145 seek_sub_file(src_stream, file_entry->size);
149 if (function & extract_create_leading_dirs) { /* Create leading directories with default umask */
151 buf = xstrdup(full_name);
152 parent = dirname(buf);
153 if (make_directory (parent, -1, FILEUTILS_RECUR) != 0) {
154 if ((function & extract_quiet) != extract_quiet) {
155 error_msg("couldn't create leading directories");
160 switch(file_entry->mode & S_IFMT) {
162 if (file_entry->link_name) { /* Found a cpio hard link */
163 if (link(full_link_name, full_name) != 0) {
164 if ((function & extract_quiet) != extract_quiet) {
165 perror_msg("Cannot link from %s to '%s'",
166 file_entry->name, file_entry->link_name);
170 if ((dst_stream = wfopen(full_name, "w")) == NULL) {
171 seek_sub_file(src_stream, file_entry->size);
174 archive_offset += file_entry->size;
175 copy_file_chunk(src_stream, dst_stream, file_entry->size);
181 if (mkdir(full_name, file_entry->mode) < 0) {
182 if ((function & extract_quiet) != extract_quiet) {
183 perror_msg("extract_archive: %s", full_name);
189 if (symlink(file_entry->link_name, full_name) < 0) {
190 if ((function & extract_quiet) != extract_quiet) {
191 perror_msg("Cannot create symlink from %s to '%s'", file_entry->name, file_entry->link_name);
200 if (mknod(full_name, file_entry->mode, file_entry->device) == -1) {
201 if ((function & extract_quiet) != extract_quiet) {
202 perror_msg("Cannot create node %s", file_entry->name);
208 perror_msg("Don't know how to handle %s", full_name);
212 /* Changing a symlink's properties normally changes the properties of the
213 * file pointed to, so dont try and change the date or mode, lchown does
214 * does the right thing, but isnt available in older versions of libc */
215 if (S_ISLNK(file_entry->mode)) {
216 #if (__GLIBC__ > 2) && (__GLIBC_MINOR__ > 1)
217 lchown(full_name, file_entry->uid, file_entry->gid);
220 if (function & extract_preserve_date) {
221 t.actime = file_entry->mtime;
222 t.modtime = file_entry->mtime;
223 utime(full_name, &t);
225 chown(full_name, file_entry->uid, file_entry->gid);
226 chmod(full_name, file_entry->mode);
229 /* If we arent extracting data we have to skip it,
230 * if data size is 0 then then just do it anyway
231 * (saves testing for it) */
232 seek_sub_file(src_stream, file_entry->size);
235 /* extract_list and extract_verbose_list can be used in conjunction
236 * with one of the above four extraction functions, so do this seperately */
237 if (function & extract_verbose_list) {
238 fprintf(out_stream, "%s %d/%d %8d %s ", mode_string(file_entry->mode),
239 file_entry->uid, file_entry->gid,
240 (int) file_entry->size, time_string(file_entry->mtime));
242 if ((function & extract_list) || (function & extract_verbose_list)){
243 /* fputs doesnt add a trailing \n, so use fprintf */
244 fprintf(out_stream, "%s\n", file_entry->name);
248 if ( full_link_name )
249 free(full_link_name);
251 return(NULL); /* Maybe we should say if failed */
256 char *unarchive(FILE *src_stream, FILE *out_stream,
257 file_header_t *(*get_headers)(FILE *),
258 void (*free_headers)(file_header_t *),
259 const int extract_function, const char *prefix, char **extract_names)
261 file_header_t *file_entry;
267 while ((file_entry = get_headers(src_stream)) != NULL) {
269 /* fprintf(stderr, __FUNCTION__ " getting headers\n"); */
270 if (extract_names != NULL) {
271 int found_flag = FALSE;
272 for(i = 0; extract_names[i] != 0; i++) {
273 if (strcmp(extract_names[i], file_entry->name) == 0) {
278 if (extract_function & extract_exclude_list) {
279 if (found_flag == TRUE) {
280 extract_flag = FALSE;
283 /* If its not found in the include list dont extract it */
284 if (found_flag == FALSE) {
285 extract_flag = FALSE;
291 if (extract_flag == TRUE) {
292 /* fprintf(stderr, __FUNCTION__ " extract?\n"); */
293 buffer = extract_archive(src_stream, out_stream, file_entry, extract_function, prefix);
294 /* fprintf(stderr, __FUNCTION__ " extracted\n"); */
296 /* seek past the data entry */
297 seek_sub_file(src_stream, file_entry->size);
299 free_headers(file_entry);
301 /*fprintf(stderr, __FUNCTION__ " goin home\n");*/
307 #ifdef L_get_header_ar
308 file_header_t *get_header_ar(FILE *src_stream)
310 file_header_t *typed;
323 static char *ar_long_names;
325 if (fread(ar.raw, 1, 60, src_stream) != 60) {
328 archive_offset += 60;
329 /* align the headers based on the header magic */
330 if ((ar.formated.magic[0] != '`') || (ar.formated.magic[1] != '\n')) {
331 /* some version of ar, have an extra '\n' after each data entry,
332 * this puts the next header out by 1 */
333 if (ar.formated.magic[1] != '`') {
334 error_msg("Invalid magic");
337 /* read the next char out of what would be the data section,
338 * if its a '\n' then it is a valid header offset by 1*/
340 if (fgetc(src_stream) != '\n') {
341 error_msg("Invalid magic");
344 /* fix up the header, we started reading 1 byte too early */
345 /* raw_header[60] wont be '\n' as it should, but it doesnt matter */
346 memmove(ar.raw, &ar.raw[1], 59);
349 typed = (file_header_t *) xcalloc(1, sizeof(file_header_t));
351 typed->size = (size_t) atoi(ar.formated.size);
352 /* long filenames have '/' as the first character */
353 if (ar.formated.name[0] == '/') {
354 if (ar.formated.name[1] == '/') {
355 /* If the second char is a '/' then this entries data section
356 * stores long filename for multiple entries, they are stored
357 * in static variable long_names for use in future entries */
358 ar_long_names = (char *) xrealloc(ar_long_names, typed->size);
359 fread(ar_long_names, 1, typed->size, src_stream);
360 archive_offset += typed->size;
361 /* This ar entries data section only contained filenames for other records
362 * they are stored in the static ar_long_names for future reference */
363 return (get_header_ar(src_stream)); /* Return next header */
364 } else if (ar.formated.name[1] == ' ') {
365 /* This is the index of symbols in the file for compilers */
366 seek_sub_file(src_stream, typed->size);
367 return (get_header_ar(src_stream)); /* Return next header */
369 /* The number after the '/' indicates the offset in the ar data section
370 (saved in variable long_name) that conatains the real filename */
371 if (!ar_long_names) {
372 error_msg("Cannot resolve long file name");
375 typed->name = xstrdup(ar_long_names + atoi(&ar.formated.name[1]));
378 /* short filenames */
379 typed->name = xcalloc(1, 16);
380 strncpy(typed->name, ar.formated.name, 16);
382 typed->name[strcspn(typed->name, " /")]='\0';
384 /* convert the rest of the now valid char header to its typed struct */
385 parse_mode(ar.formated.mode, &typed->mode);
386 typed->mtime = atoi(ar.formated.date);
387 typed->uid = atoi(ar.formated.uid);
388 typed->gid = atoi(ar.formated.gid);
393 void free_header_ar(file_header_t *ar_entry)
395 if (ar_entry == NULL) {
399 free(ar_entry->name);
400 free(ar_entry->link_name);
406 #ifdef L_get_header_cpio
408 file_header_t *entry;
410 struct hardlinks *next;
413 file_header_t *get_header_cpio(FILE *src_stream)
415 file_header_t *cpio_entry = NULL;
416 char cpio_header[110];
419 int major, minor, nlink, inode;
420 static struct hardlinks *saved_hardlinks = NULL;
421 static int pending_hardlinks = 0;
424 if (pending_hardlinks) { /* Deal with any pending hardlinks */
425 struct hardlinks *tmp = saved_hardlinks, *oldtmp = NULL;
427 if (tmp->entry->link_name) { /* Found a hardlink ready to be extracted */
428 cpio_entry = tmp->entry;
429 if (oldtmp) oldtmp->next = tmp->next; /* Remove item from linked list */
430 else saved_hardlinks = tmp->next;
437 pending_hardlinks = 0; /* No more pending hardlinks, read next file entry */
440 /* There can be padding before archive header */
441 seek_sub_file(src_stream, (4 - (archive_offset % 4)) % 4);
442 if (fread(cpio_header, 1, 110, src_stream) == 110) {
443 archive_offset += 110;
444 if (strncmp(cpio_header, "07070", 5) != 0) {
445 error_msg("Unsupported format or invalid magic");
448 switch (cpio_header[5]) {
449 case '2': /* "crc" header format */
450 /* Doesnt do the crc check yet */
451 case '1': /* "newc" header format */
452 cpio_entry = (file_header_t *) xcalloc(1, sizeof(file_header_t));
453 sscanf(cpio_header, "%6c%8x%8x%8x%8x%8x%8lx%8lx%16c%8x%8x%8x%8c",
454 dummy, &inode, (unsigned int*)&cpio_entry->mode,
455 (unsigned int*)&cpio_entry->uid, (unsigned int*)&cpio_entry->gid,
456 &nlink, &cpio_entry->mtime, (unsigned long*)&cpio_entry->size,
457 dummy, &major, &minor, &namesize, dummy);
459 cpio_entry->name = (char *) xcalloc(1, namesize);
460 fread(cpio_entry->name, 1, namesize, src_stream); /* Read in filename */
461 archive_offset += namesize;
462 /* Skip padding before file contents */
463 seek_sub_file(src_stream, (4 - (archive_offset % 4)) % 4);
464 if (strcmp(cpio_entry->name, "TRAILER!!!") == 0) {
465 printf("%d blocks\n", (int) (archive_offset % 512 ? (archive_offset / 512) + 1 : archive_offset / 512)); /* Always round up */
466 if (saved_hardlinks) { /* Bummer - we still have unresolved hardlinks */
467 struct hardlinks *tmp = saved_hardlinks, *oldtmp = NULL;
469 error_msg("%s not created: cannot resolve hardlink", tmp->entry->name);
472 free (oldtmp->entry->name);
473 free (oldtmp->entry);
476 saved_hardlinks = NULL;
477 pending_hardlinks = 0;
482 if (S_ISLNK(cpio_entry->mode)) {
483 cpio_entry->link_name = (char *) xcalloc(1, cpio_entry->size + 1);
484 fread(cpio_entry->link_name, 1, cpio_entry->size, src_stream);
485 archive_offset += cpio_entry->size;
486 cpio_entry->size = 0; /* Stop possiable seeks in future */
488 if (nlink > 1 && !S_ISDIR(cpio_entry->mode)) {
489 if (cpio_entry->size == 0) { /* Put file on a linked list for later */
490 struct hardlinks *new = xmalloc(sizeof(struct hardlinks));
491 new->next = saved_hardlinks;
493 new->entry = cpio_entry;
494 saved_hardlinks = new;
495 return(get_header_cpio(src_stream)); /* Recurse to next file */
496 } else { /* Found the file with data in */
497 struct hardlinks *tmp = saved_hardlinks;
498 pending_hardlinks = 1;
500 if (tmp->inode == inode) {
501 tmp->entry->link_name = xstrdup(cpio_entry->name);
506 if (nlink > 1) error_msg("error resolving hardlink: did you create the archive with GNU cpio 2.0-2.2?");
509 cpio_entry->device = (major << 8) | minor;
512 error_msg("Unsupported format");
515 if (ferror(src_stream) || feof(src_stream)) {
516 perror_msg("Stream error");
524 #ifdef L_get_header_tar
525 file_header_t *get_header_tar(FILE *tar_stream)
529 unsigned char raw[512];
531 char name[100]; /* 0-99 */
532 char mode[8]; /* 100-107 */
533 char uid[8]; /* 108-115 */
534 char gid[8]; /* 116-123 */
535 char size[12]; /* 124-135 */
536 char mtime[12]; /* 136-147 */
537 char chksum[8]; /* 148-155 */
538 char typeflag; /* 156-156 */
539 char linkname[100]; /* 157-256 */
540 char magic[6]; /* 257-262 */
541 char version[2]; /* 263-264 */
542 char uname[32]; /* 265-296 */
543 char gname[32]; /* 297-328 */
544 char devmajor[8]; /* 329-336 */
545 char devminor[8]; /* 337-344 */
546 char prefix[155]; /* 345-499 */
547 char padding[12]; /* 500-512 */
550 file_header_t *tar_entry = NULL;
554 if (archive_offset % 512 != 0) {
555 seek_sub_file(tar_stream, 512 - (archive_offset % 512));
558 if (fread(tar.raw, 1, 512, tar_stream) != 512) {
559 /* Unfortunatly its common for tar files to have all sorts of
560 * trailing garbage, fail silently */
561 // error_msg("Couldnt read header");
564 archive_offset += 512;
566 /* Check header has valid magic, unfortunately some tar files
567 * have empty (0'ed) tar entries at the end, which will
568 * cause this to fail, so fail silently for now
570 if (strncmp(tar.formated.magic, "ustar", 5) != 0) {
571 #ifdef CONFIG_FEATURE_TAR_OLDGNU_COMPATABILITY
572 if (strncmp(tar.formated.magic, "\0\0\0\0\0", 5) != 0)
577 /* Do checksum on headers */
578 for (i = 0; i < 148 ; i++) {
582 for (i = 156; i < 512 ; i++) {
585 if (sum != strtol(tar.formated.chksum, NULL, 8)) {
586 if ( strtol(tar.formated.chksum,NULL,8) != 0 )
587 error_msg("Invalid tar header checksum");
591 /* convert to type'ed variables */
592 tar_entry = xcalloc(1, sizeof(file_header_t));
594 #ifdef CONFIG_FEATURE_TAR_GNU_EXTENSIONS
596 tar_entry->name = longname;
600 tar_entry->name = linkname;
604 if (tar.formated.prefix[0] == 0) {
605 tar_entry->name = strdup(tar.formated.name);
607 tar_entry->name = concat_path_file(tar.formated.prefix, tar.formated.name);
610 // tar_entry->name = xstrdup(tar.formated.name);
613 parse_mode(tar.formated.mode, &tar_entry->mode);
615 tar_entry->mode = 07777 & strtol(tar.formated.mode, NULL, 8);
617 tar_entry->uid = strtol(tar.formated.uid, NULL, 8);
618 tar_entry->gid = strtol(tar.formated.gid, NULL, 8);
619 tar_entry->size = strtol(tar.formated.size, NULL, 8);
620 tar_entry->mtime = strtol(tar.formated.mtime, NULL, 8);
621 tar_entry->link_name = strlen(tar.formated.linkname) ?
622 xstrdup(tar.formated.linkname) : NULL;
623 tar_entry->device = (strtol(tar.formated.devmajor, NULL, 8) << 8) +
624 strtol(tar.formated.devminor, NULL, 8);
626 /* Fix mode, used by the old format */
627 switch (tar.formated.typeflag) {
628 /* hard links are detected as regular files with 0 size and a link name */
630 tar_entry->mode |= S_IFREG ;
635 # ifdef CONFIG_FEATURE_TAR_OLDGNU_COMPATABILITY
636 if (last_char_is(tar_entry->name, '/')) {
637 tar_entry->mode |= S_IFDIR;
640 tar_entry->mode |= S_IFREG;
643 tar_entry->mode |= S_IFLNK;
646 tar_entry->mode |= S_IFCHR;
649 tar_entry->mode |= S_IFBLK;
652 tar_entry->mode |= S_IFDIR;
655 tar_entry->mode |= S_IFIFO;
657 # ifdef CONFIG_FEATURE_TAR_GNU_EXTENSIONS
659 longname = xmalloc(tar_entry->size + 1);
660 if(fread(longname, tar_entry->size, 1, tar_stream) != 1)
662 longname[tar_entry->size] = '\0';
663 archive_offset += tar_entry->size;
665 return(get_header_tar(tar_stream));
668 linkname = xmalloc(tar_entry->size + 1);
669 if(fread(linkname, tar_entry->size, 1, tar_stream) != 1)
671 linkname[tar_entry->size] = '\0';
672 archive_offset += tar_entry->size;
674 tar_entry->name = linkname;
675 return(get_header_tar(tar_stream));
682 perror_msg("Ignoring GNU extension type %c", tar.formated.typeflag);
685 perror_msg("Unknown typeflag: 0x%x", tar.formated.typeflag);
693 void free_header_tar(file_header_t *tar_entry)
695 if (tar_entry == NULL) {
699 free(tar_entry->name);
700 free(tar_entry->link_name);
708 char *deb_extract(const char *package_filename, FILE *out_stream,
709 const int extract_function, const char *prefix, const char *filename)
712 FILE *uncompressed_stream = NULL;
713 file_header_t *ar_header = NULL;
714 char **file_list = NULL;
715 char *output_buffer = NULL;
716 char *ared_file = NULL;
720 if (filename != NULL) {
721 file_list = xmalloc(sizeof(char *) * 2);
722 file_list[0] = xstrdup(filename);
726 if (extract_function & extract_control_tar_gz) {
727 ared_file = xstrdup("control.tar.gz");
729 else if (extract_function & extract_data_tar_gz) {
730 ared_file = xstrdup("data.tar.gz");
732 fprintf(stderr, "no file specified to extract -- extract_function=%x\n", extract_function);
736 /* open the debian package to be worked on */
737 deb_stream = wfopen(package_filename, "r");
738 if (deb_stream == NULL) {
741 /* set the buffer size */
742 setvbuf(deb_stream, NULL, _IOFBF, 0x8000);
745 fread(ar_magic, 1, 8, deb_stream);
746 /*fprintf(stderr, "deb_extract ar_magic=%08x\n", *(long *)ar_magic);*/
747 if (strncmp(ar_magic,"!<arch>",7) == 0) {
750 while ((ar_header = get_header_ar(deb_stream)) != NULL) {
751 if (strcmp(ared_file, ar_header->name) == 0) {
752 /* open a stream of decompressed data */
753 uncompressed_stream = gz_open(deb_stream, &gunzip_pid);
755 output_buffer = unarchive(uncompressed_stream, out_stream, get_header_tar, free_header_tar, extract_function, prefix, file_list);
757 seek_sub_file(deb_stream, ar_header->size);
759 gz_close(gunzip_pid);
761 fclose(uncompressed_stream);
763 return(output_buffer);
764 } else if (strncmp(ar_magic, "\037\213", 2) == 0) {
765 /* it's a gz file, let's assume it's an opkg */
766 int unzipped_opkg_pid;
767 FILE *unzipped_opkg_stream;
768 file_header_t *tar_header;
770 fseek(deb_stream, 0, SEEK_SET);
771 unzipped_opkg_stream = gz_open(deb_stream, &unzipped_opkg_pid);
773 /*fprintf(stderr, __FUNCTION__ ": processing opkg %s -- ared_file=%s\n", package_filename, ared_file);*/
774 /* walk through outer tar file to find ared_file */
775 while ((tar_header = get_header_tar(unzipped_opkg_stream)) != NULL) {
777 if (strncmp(tar_header->name, "./", 2) == 0)
779 if (strcmp(ared_file, tar_header->name+name_offset) == 0) {
780 /* open a stream of decompressed data */
781 uncompressed_stream = gz_open(unzipped_opkg_stream, &gunzip_pid);
783 /*fprintf(stderr, __FUNCTION__ ":%d: here -- found file\n", __LINE__);*/
784 output_buffer = unarchive(uncompressed_stream,
791 /*fprintf(stderr, __FUNCTION__ ":%d: unarchive complete\n", __LINE__);*/
792 free_header_tar(tar_header);
793 gz_close(gunzip_pid);
794 fclose(uncompressed_stream);
797 seek_sub_file(unzipped_opkg_stream, tar_header->size);
798 free_header_tar(tar_header);
800 gz_close(unzipped_opkg_pid);
801 fclose(unzipped_opkg_stream);
804 /*fprintf(stderr, __FUNCTION__ ":%d: done\n", __LINE__);*/
805 return output_buffer;
807 error_msg_and_die("invalid magic");