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;
89 /* prefix doesnt have to be a proper path it may prepend
90 * the filename as well */
92 /* strip leading '/' in filename to extract as prefix may not be dir */
93 /* Cant use concat_path_file here as prefix might not be a directory */
94 char *path = file_entry->name;
95 if (strncmp("./", path, 2) == 0) {
97 if (strlen(path) == 0) {
101 full_name = xmalloc(strlen(prefix) + strlen(path) + 1);
102 strcpy(full_name, prefix);
103 strcat(full_name, path);
104 if ( file_entry->link_name ){
105 full_link_name = xmalloc(strlen(prefix) + strlen(file_entry->link_name) + 1);
106 strcpy(full_link_name, prefix);
107 strcat(full_link_name, file_entry->link_name);
110 full_name = strdup(file_entry->name);
111 if ( file_entry->link_name )
112 full_link_name = strdup(file_entry->link_name);
116 if (function & extract_to_stdout) {
117 if (S_ISREG(file_entry->mode)) {
118 copy_file_chunk(src_stream, out_stream, file_entry->size);
119 archive_offset += file_entry->size;
122 else if (function & extract_one_to_buffer) {
123 if (S_ISREG(file_entry->mode)) {
124 buffer = (char *) xmalloc(file_entry->size + 1);
125 fread(buffer, 1, file_entry->size, src_stream);
126 buffer[file_entry->size] = '\0';
127 archive_offset += file_entry->size;
131 else if (function & extract_all_to_fs) {
134 stat_res = lstat (full_name, &oldfile);
135 if (stat_res == 0) { /* The file already exists */
136 if ((function & extract_unconditional) || (oldfile.st_mtime < file_entry->mtime)) {
137 if (!S_ISDIR(oldfile.st_mode)) {
138 unlink(full_name); /* Directories might not be empty etc */
141 if ((function & extract_quiet) != extract_quiet) {
142 error_msg("%s not created: newer or same age file exists", file_entry->name);
144 seek_sub_file(src_stream, file_entry->size);
148 if (function & extract_create_leading_dirs) { /* Create leading directories with default umask */
150 buf = xstrdup(full_name);
151 parent = dirname(buf);
152 if (make_directory (parent, -1, FILEUTILS_RECUR) != 0) {
153 if ((function & extract_quiet) != extract_quiet) {
154 error_msg("couldn't create leading directories");
159 switch(file_entry->mode & S_IFMT) {
161 if (file_entry->link_name) { /* Found a cpio hard link */
162 if (link(full_link_name, full_name) != 0) {
163 if ((function & extract_quiet) != extract_quiet) {
164 perror_msg("Cannot link from %s to '%s'",
165 file_entry->name, file_entry->link_name);
169 if ((dst_stream = wfopen(full_name, "w")) == NULL) {
170 seek_sub_file(src_stream, file_entry->size);
173 archive_offset += file_entry->size;
174 copy_file_chunk(src_stream, dst_stream, file_entry->size);
180 if (mkdir(full_name, file_entry->mode) < 0) {
181 if ((function & extract_quiet) != extract_quiet) {
182 perror_msg("extract_archive: %s", full_name);
188 if (symlink(file_entry->link_name, full_name) < 0) {
189 if ((function & extract_quiet) != extract_quiet) {
190 perror_msg("Cannot create symlink from %s to '%s'", file_entry->name, file_entry->link_name);
199 if (mknod(full_name, file_entry->mode, file_entry->device) == -1) {
200 if ((function & extract_quiet) != extract_quiet) {
201 perror_msg("Cannot create node %s", file_entry->name);
207 perror_msg("Don't know how to handle %s", full_name);
211 /* Changing a symlink's properties normally changes the properties of the
212 * file pointed to, so dont try and change the date or mode, lchown does
213 * does the right thing, but isnt available in older versions of libc */
214 if (S_ISLNK(file_entry->mode)) {
215 #if (__GLIBC__ > 2) && (__GLIBC_MINOR__ > 1)
216 lchown(full_name, file_entry->uid, file_entry->gid);
219 if (function & extract_preserve_date) {
220 t.actime = file_entry->mtime;
221 t.modtime = file_entry->mtime;
222 utime(full_name, &t);
224 chown(full_name, file_entry->uid, file_entry->gid);
225 chmod(full_name, file_entry->mode);
228 /* If we arent extracting data we have to skip it,
229 * if data size is 0 then then just do it anyway
230 * (saves testing for it) */
231 seek_sub_file(src_stream, file_entry->size);
234 /* extract_list and extract_verbose_list can be used in conjunction
235 * with one of the above four extraction functions, so do this seperately */
236 if (function & extract_verbose_list) {
237 fprintf(out_stream, "%s %d/%d %8d %s ", mode_string(file_entry->mode),
238 file_entry->uid, file_entry->gid,
239 (int) file_entry->size, time_string(file_entry->mtime));
241 if ((function & extract_list) || (function & extract_verbose_list)){
242 /* fputs doesnt add a trailing \n, so use fprintf */
243 fprintf(out_stream, "%s\n", file_entry->name);
247 if ( full_link_name )
248 free(full_link_name);
250 return(NULL); /* Maybe we should say if failed */
255 char *unarchive(FILE *src_stream, FILE *out_stream,
256 file_header_t *(*get_headers)(FILE *),
257 void (*free_headers)(file_header_t *),
258 const int extract_function, const char *prefix, char **extract_names)
260 file_header_t *file_entry;
266 while ((file_entry = get_headers(src_stream)) != NULL) {
268 /* fprintf(stderr, __FUNCTION__ " getting headers\n"); */
269 if (extract_names != NULL) {
270 int found_flag = FALSE;
271 for(i = 0; extract_names[i] != 0; i++) {
272 if (strcmp(extract_names[i], file_entry->name) == 0) {
277 if (extract_function & extract_exclude_list) {
278 if (found_flag == TRUE) {
279 extract_flag = FALSE;
282 /* If its not found in the include list dont extract it */
283 if (found_flag == FALSE) {
284 extract_flag = FALSE;
290 if (extract_flag == TRUE) {
291 /* fprintf(stderr, __FUNCTION__ " extract?\n"); */
292 buffer = extract_archive(src_stream, out_stream, file_entry, extract_function, prefix);
293 /* fprintf(stderr, __FUNCTION__ " extracted\n"); */
295 /* seek past the data entry */
296 seek_sub_file(src_stream, file_entry->size);
298 free_headers(file_entry);
300 /*fprintf(stderr, __FUNCTION__ " goin home\n");*/
306 #ifdef L_get_header_ar
307 file_header_t *get_header_ar(FILE *src_stream)
309 file_header_t *typed;
322 static char *ar_long_names;
324 if (fread(ar.raw, 1, 60, src_stream) != 60) {
327 archive_offset += 60;
328 /* align the headers based on the header magic */
329 if ((ar.formated.magic[0] != '`') || (ar.formated.magic[1] != '\n')) {
330 /* some version of ar, have an extra '\n' after each data entry,
331 * this puts the next header out by 1 */
332 if (ar.formated.magic[1] != '`') {
333 error_msg("Invalid magic");
336 /* read the next char out of what would be the data section,
337 * if its a '\n' then it is a valid header offset by 1*/
339 if (fgetc(src_stream) != '\n') {
340 error_msg("Invalid magic");
343 /* fix up the header, we started reading 1 byte too early */
344 /* raw_header[60] wont be '\n' as it should, but it doesnt matter */
345 memmove(ar.raw, &ar.raw[1], 59);
348 typed = (file_header_t *) xcalloc(1, sizeof(file_header_t));
350 typed->size = (size_t) atoi(ar.formated.size);
351 /* long filenames have '/' as the first character */
352 if (ar.formated.name[0] == '/') {
353 if (ar.formated.name[1] == '/') {
354 /* If the second char is a '/' then this entries data section
355 * stores long filename for multiple entries, they are stored
356 * in static variable long_names for use in future entries */
357 ar_long_names = (char *) xrealloc(ar_long_names, typed->size);
358 fread(ar_long_names, 1, typed->size, src_stream);
359 archive_offset += typed->size;
360 /* This ar entries data section only contained filenames for other records
361 * they are stored in the static ar_long_names for future reference */
362 return (get_header_ar(src_stream)); /* Return next header */
363 } else if (ar.formated.name[1] == ' ') {
364 /* This is the index of symbols in the file for compilers */
365 seek_sub_file(src_stream, typed->size);
366 return (get_header_ar(src_stream)); /* Return next header */
368 /* The number after the '/' indicates the offset in the ar data section
369 (saved in variable long_name) that conatains the real filename */
370 if (!ar_long_names) {
371 error_msg("Cannot resolve long file name");
374 typed->name = xstrdup(ar_long_names + atoi(&ar.formated.name[1]));
377 /* short filenames */
378 typed->name = xcalloc(1, 16);
379 strncpy(typed->name, ar.formated.name, 16);
381 typed->name[strcspn(typed->name, " /")]='\0';
383 /* convert the rest of the now valid char header to its typed struct */
384 parse_mode(ar.formated.mode, &typed->mode);
385 typed->mtime = atoi(ar.formated.date);
386 typed->uid = atoi(ar.formated.uid);
387 typed->gid = atoi(ar.formated.gid);
392 void free_header_ar(file_header_t *ar_entry)
394 if (ar_entry == NULL) {
398 free(ar_entry->name);
399 free(ar_entry->link_name);
405 #ifdef L_get_header_cpio
407 file_header_t *entry;
409 struct hardlinks *next;
412 file_header_t *get_header_cpio(FILE *src_stream)
414 file_header_t *cpio_entry = NULL;
415 char cpio_header[110];
418 int major, minor, nlink, inode;
419 static struct hardlinks *saved_hardlinks = NULL;
420 static int pending_hardlinks = 0;
423 if (pending_hardlinks) { /* Deal with any pending hardlinks */
424 struct hardlinks *tmp = saved_hardlinks, *oldtmp = NULL;
426 if (tmp->entry->link_name) { /* Found a hardlink ready to be extracted */
427 cpio_entry = tmp->entry;
428 if (oldtmp) oldtmp->next = tmp->next; /* Remove item from linked list */
429 else saved_hardlinks = tmp->next;
436 pending_hardlinks = 0; /* No more pending hardlinks, read next file entry */
439 /* There can be padding before archive header */
440 seek_sub_file(src_stream, (4 - (archive_offset % 4)) % 4);
441 if (fread(cpio_header, 1, 110, src_stream) == 110) {
442 archive_offset += 110;
443 if (strncmp(cpio_header, "07070", 5) != 0) {
444 error_msg("Unsupported format or invalid magic");
447 switch (cpio_header[5]) {
448 case '2': /* "crc" header format */
449 /* Doesnt do the crc check yet */
450 case '1': /* "newc" header format */
451 cpio_entry = (file_header_t *) xcalloc(1, sizeof(file_header_t));
452 sscanf(cpio_header, "%6c%8x%8x%8x%8x%8x%8lx%8lx%16c%8x%8x%8x%8c",
453 dummy, &inode, (unsigned int*)&cpio_entry->mode,
454 (unsigned int*)&cpio_entry->uid, (unsigned int*)&cpio_entry->gid,
455 &nlink, &cpio_entry->mtime, (unsigned long*)&cpio_entry->size,
456 dummy, &major, &minor, &namesize, dummy);
458 cpio_entry->name = (char *) xcalloc(1, namesize);
459 fread(cpio_entry->name, 1, namesize, src_stream); /* Read in filename */
460 archive_offset += namesize;
461 /* Skip padding before file contents */
462 seek_sub_file(src_stream, (4 - (archive_offset % 4)) % 4);
463 if (strcmp(cpio_entry->name, "TRAILER!!!") == 0) {
464 printf("%d blocks\n", (int) (archive_offset % 512 ? (archive_offset / 512) + 1 : archive_offset / 512)); /* Always round up */
465 if (saved_hardlinks) { /* Bummer - we still have unresolved hardlinks */
466 struct hardlinks *tmp = saved_hardlinks, *oldtmp = NULL;
468 error_msg("%s not created: cannot resolve hardlink", tmp->entry->name);
471 free (oldtmp->entry->name);
472 free (oldtmp->entry);
475 saved_hardlinks = NULL;
476 pending_hardlinks = 0;
481 if (S_ISLNK(cpio_entry->mode)) {
482 cpio_entry->link_name = (char *) xcalloc(1, cpio_entry->size + 1);
483 fread(cpio_entry->link_name, 1, cpio_entry->size, src_stream);
484 archive_offset += cpio_entry->size;
485 cpio_entry->size = 0; /* Stop possiable seeks in future */
487 if (nlink > 1 && !S_ISDIR(cpio_entry->mode)) {
488 if (cpio_entry->size == 0) { /* Put file on a linked list for later */
489 struct hardlinks *new = xmalloc(sizeof(struct hardlinks));
490 new->next = saved_hardlinks;
492 new->entry = cpio_entry;
493 saved_hardlinks = new;
494 return(get_header_cpio(src_stream)); /* Recurse to next file */
495 } else { /* Found the file with data in */
496 struct hardlinks *tmp = saved_hardlinks;
497 pending_hardlinks = 1;
499 if (tmp->inode == inode) {
500 tmp->entry->link_name = xstrdup(cpio_entry->name);
505 if (nlink > 1) error_msg("error resolving hardlink: did you create the archive with GNU cpio 2.0-2.2?");
508 cpio_entry->device = (major << 8) | minor;
511 error_msg("Unsupported format");
514 if (ferror(src_stream) || feof(src_stream)) {
515 perror_msg("Stream error");
523 #ifdef L_get_header_tar
524 file_header_t *get_header_tar(FILE *tar_stream)
528 unsigned char raw[512];
530 char name[100]; /* 0-99 */
531 char mode[8]; /* 100-107 */
532 char uid[8]; /* 108-115 */
533 char gid[8]; /* 116-123 */
534 char size[12]; /* 124-135 */
535 char mtime[12]; /* 136-147 */
536 char chksum[8]; /* 148-155 */
537 char typeflag; /* 156-156 */
538 char linkname[100]; /* 157-256 */
539 char magic[6]; /* 257-262 */
540 char version[2]; /* 263-264 */
541 char uname[32]; /* 265-296 */
542 char gname[32]; /* 297-328 */
543 char devmajor[8]; /* 329-336 */
544 char devminor[8]; /* 337-344 */
545 char prefix[155]; /* 345-499 */
546 char padding[12]; /* 500-512 */
549 file_header_t *tar_entry = NULL;
553 if (archive_offset % 512 != 0) {
554 seek_sub_file(tar_stream, 512 - (archive_offset % 512));
557 if (fread(tar.raw, 1, 512, tar_stream) != 512) {
558 /* Unfortunatly its common for tar files to have all sorts of
559 * trailing garbage, fail silently */
560 // error_msg("Couldnt read header");
563 archive_offset += 512;
565 /* Check header has valid magic, unfortunately some tar files
566 * have empty (0'ed) tar entries at the end, which will
567 * cause this to fail, so fail silently for now
569 if (strncmp(tar.formated.magic, "ustar", 5) != 0) {
570 #ifdef CONFIG_FEATURE_TAR_OLDGNU_COMPATABILITY
571 if (strncmp(tar.formated.magic, "\0\0\0\0\0", 5) != 0)
576 /* Do checksum on headers */
577 for (i = 0; i < 148 ; i++) {
581 for (i = 156; i < 512 ; i++) {
584 if (sum != strtol(tar.formated.chksum, NULL, 8)) {
585 if ( strtol(tar.formated.chksum,NULL,8) != 0 )
586 error_msg("Invalid tar header checksum");
590 /* convert to type'ed variables */
591 tar_entry = xcalloc(1, sizeof(file_header_t));
593 #ifdef CONFIG_FEATURE_TAR_GNU_EXTENSIONS
595 tar_entry->name = longname;
599 tar_entry->name = linkname;
603 if (tar.formated.prefix[0] == 0) {
604 tar_entry->name = strdup(tar.formated.name);
606 tar_entry->name = concat_path_file(tar.formated.prefix, tar.formated.name);
609 // tar_entry->name = xstrdup(tar.formated.name);
612 parse_mode(tar.formated.mode, &tar_entry->mode);
614 tar_entry->mode = 07777 & strtol(tar.formated.mode, NULL, 8);
616 tar_entry->uid = strtol(tar.formated.uid, NULL, 8);
617 tar_entry->gid = strtol(tar.formated.gid, NULL, 8);
618 tar_entry->size = strtol(tar.formated.size, NULL, 8);
619 tar_entry->mtime = strtol(tar.formated.mtime, NULL, 8);
620 tar_entry->link_name = strlen(tar.formated.linkname) ?
621 xstrdup(tar.formated.linkname) : NULL;
622 tar_entry->device = (strtol(tar.formated.devmajor, NULL, 8) << 8) +
623 strtol(tar.formated.devminor, NULL, 8);
625 /* Fix mode, used by the old format */
626 switch (tar.formated.typeflag) {
627 /* hard links are detected as regular files with 0 size and a link name */
629 tar_entry->mode |= S_IFREG ;
634 # ifdef CONFIG_FEATURE_TAR_OLDGNU_COMPATABILITY
635 if (last_char_is(tar_entry->name, '/')) {
636 tar_entry->mode |= S_IFDIR;
639 tar_entry->mode |= S_IFREG;
642 tar_entry->mode |= S_IFLNK;
645 tar_entry->mode |= S_IFCHR;
648 tar_entry->mode |= S_IFBLK;
651 tar_entry->mode |= S_IFDIR;
654 tar_entry->mode |= S_IFIFO;
656 # ifdef CONFIG_FEATURE_TAR_GNU_EXTENSIONS
658 longname = xmalloc(tar_entry->size + 1);
659 if(fread(longname, tar_entry->size, 1, tar_stream) != 1)
661 longname[tar_entry->size] = '\0';
662 archive_offset += tar_entry->size;
664 return(get_header_tar(tar_stream));
667 linkname = xmalloc(tar_entry->size + 1);
668 if(fread(linkname, tar_entry->size, 1, tar_stream) != 1)
670 linkname[tar_entry->size] = '\0';
671 archive_offset += tar_entry->size;
673 tar_entry->name = linkname;
674 return(get_header_tar(tar_stream));
681 perror_msg("Ignoring GNU extension type %c", tar.formated.typeflag);
684 perror_msg("Unknown typeflag: 0x%x", tar.formated.typeflag);
692 void free_header_tar(file_header_t *tar_entry)
694 if (tar_entry == NULL) {
698 free(tar_entry->name);
699 free(tar_entry->link_name);
707 char *deb_extract(const char *package_filename, FILE *out_stream,
708 const int extract_function, const char *prefix, const char *filename)
711 FILE *uncompressed_stream = NULL;
712 file_header_t *ar_header = NULL;
713 char **file_list = NULL;
714 char *output_buffer = NULL;
715 char *ared_file = NULL;
719 if (filename != NULL) {
720 file_list = xmalloc(sizeof(char *) * 2);
721 file_list[0] = xstrdup(filename);
725 if (extract_function & extract_control_tar_gz) {
726 ared_file = xstrdup("control.tar.gz");
728 else if (extract_function & extract_data_tar_gz) {
729 ared_file = xstrdup("data.tar.gz");
731 fprintf(stderr, "no file specified to extract -- extract_function=%x\n", extract_function);
735 /* open the debian package to be worked on */
736 deb_stream = wfopen(package_filename, "r");
737 if (deb_stream == NULL) {
740 /* set the buffer size */
741 setvbuf(deb_stream, NULL, _IOFBF, 0x8000);
744 fread(ar_magic, 1, 8, deb_stream);
745 /*fprintf(stderr, "deb_extract ar_magic=%08x\n", *(long *)ar_magic);*/
746 if (strncmp(ar_magic,"!<arch>",7) == 0) {
749 while ((ar_header = get_header_ar(deb_stream)) != NULL) {
750 if (strcmp(ared_file, ar_header->name) == 0) {
751 /* open a stream of decompressed data */
752 uncompressed_stream = gzvopen(deb_stream, &gunzip_pid);
754 output_buffer = unarchive(uncompressed_stream, out_stream, get_header_tar, free_header_tar, extract_function, prefix, file_list);
756 seek_sub_file(deb_stream, ar_header->size);
757 free (ar_header->name);
760 gzvclose(gunzip_pid);
762 fclose(uncompressed_stream);
764 return(output_buffer);
765 } else if (strncmp(ar_magic, "\037\213", 2) == 0) {
766 /* it's a gz file, let's assume it's an opkg */
767 int unzipped_opkg_pid;
768 FILE *unzipped_opkg_stream;
769 file_header_t *tar_header;
771 fseek(deb_stream, 0, SEEK_SET);
772 unzipped_opkg_stream = gzvopen(deb_stream, &unzipped_opkg_pid);
774 /*fprintf(stderr, __FUNCTION__ ": processing opkg %s -- ared_file=%s\n", package_filename, ared_file);*/
775 /* walk through outer tar file to find ared_file */
776 while ((tar_header = get_header_tar(unzipped_opkg_stream)) != NULL) {
778 if (strncmp(tar_header->name, "./", 2) == 0)
780 if (strcmp(ared_file, tar_header->name+name_offset) == 0) {
781 /* open a stream of decompressed data */
782 uncompressed_stream = gzvopen(unzipped_opkg_stream, &gunzip_pid);
784 /*fprintf(stderr, __FUNCTION__ ":%d: here -- found file\n", __LINE__);*/
785 output_buffer = unarchive(uncompressed_stream,
792 /*fprintf(stderr, __FUNCTION__ ":%d: unarchive complete\n", __LINE__);*/
793 free_header_tar(tar_header);
794 gzvclose(gunzip_pid);
795 fclose(uncompressed_stream);
798 seek_sub_file(unzipped_opkg_stream, tar_header->size);
799 free_header_tar(tar_header);
801 gzvclose(unzipped_opkg_pid);
802 fclose(unzipped_opkg_stream);
805 /*fprintf(stderr, __FUNCTION__ ":%d: done\n", __LINE__);*/
806 return output_buffer;
808 error_msg_and_die("invalid magic");