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 *src_stream, const int count)
66 /* Try to fseek as faster */
67 archive_offset += count;
68 seek_by_read(src_stream, count);
73 /* Extract the data postioned at src_stream to either filesystem, stdout or
74 * buffer depending on the value of 'function' which is defined in libbb.h
76 * prefix doesnt have to be just a directory, it may prefix the filename as well.
78 * e.g. '/var/lib/dpkg/info/dpkg.' will extract all files to the base bath
79 * '/var/lib/dpkg/info/' and all files/dirs created in that dir will have
80 * 'dpkg.' as their prefix
82 * For this reason if prefix does point to a dir then it must end with a
83 * trailing '/' or else the last dir will be assumed to be the file prefix
86 extract_archive(FILE *src_stream, FILE *out_stream,
87 const file_header_t *file_entry, const int function,
91 FILE *dst_stream = NULL;
92 char *full_name = NULL;
93 char *full_link_name = NULL;
99 /* prefix doesnt have to be a proper path it may prepend
100 * the filename as well */
101 if (prefix != NULL) {
102 /* strip leading '/' in filename to extract as prefix may not be dir */
103 /* Cant use concat_path_file here as prefix might not be a directory */
104 char *path = file_entry->name;
105 if (strncmp("./", path, 2) == 0) {
107 if (strlen(path) == 0)
108 /* Do nothing, current dir already exists. */
111 full_name = xmalloc(strlen(prefix) + strlen(path) + 1);
112 strcpy(full_name, prefix);
113 strcat(full_name, path);
114 if ( file_entry->link_name ){
115 full_link_name = xmalloc(strlen(prefix) + strlen(file_entry->link_name) + 1);
116 strcpy(full_link_name, prefix);
117 strcat(full_link_name, file_entry->link_name);
120 full_name = xstrdup(file_entry->name);
121 if ( file_entry->link_name )
122 full_link_name = xstrdup(file_entry->link_name);
126 if (function & extract_to_stream) {
127 if (S_ISREG(file_entry->mode)) {
128 *err = copy_file_chunk(src_stream, out_stream, file_entry->size);
129 archive_offset += file_entry->size;
132 else if (function & extract_one_to_buffer) {
133 if (S_ISREG(file_entry->mode)) {
134 buffer = (char *) xmalloc(file_entry->size + 1);
135 fread(buffer, 1, file_entry->size, src_stream);
136 buffer[file_entry->size] = '\0';
137 archive_offset += file_entry->size;
141 else if (function & extract_all_to_fs) {
144 stat_res = lstat (full_name, &oldfile);
145 if (stat_res == 0) { /* The file already exists */
146 if ((function & extract_unconditional) || (oldfile.st_mtime < file_entry->mtime)) {
147 if (!S_ISDIR(oldfile.st_mode)) {
148 unlink(full_name); /* Directories might not be empty etc */
151 if ((function & extract_quiet) != extract_quiet) {
153 error_msg("%s not created: newer or same age file exists", file_entry->name);
155 seek_sub_file(src_stream, file_entry->size);
159 if (function & extract_create_leading_dirs) { /* Create leading directories with default umask */
161 buf = xstrdup(full_name);
162 parent = dirname(buf);
163 if (make_directory (parent, -1, FILEUTILS_RECUR) != 0) {
164 if ((function & extract_quiet) != extract_quiet) {
166 error_msg("couldn't create leading directories");
171 switch(file_entry->mode & S_IFMT) {
173 if (file_entry->link_name) { /* Found a cpio hard link */
174 if (link(full_link_name, full_name) != 0) {
175 if ((function & extract_quiet) != extract_quiet) {
177 perror_msg("Cannot link from %s to '%s'",
178 file_entry->name, file_entry->link_name);
182 if ((dst_stream = wfopen(full_name, "w")) == NULL) {
184 seek_sub_file(src_stream, file_entry->size);
187 archive_offset += file_entry->size;
188 copy_file_chunk(src_stream, dst_stream, file_entry->size);
194 if (mkdir(full_name, file_entry->mode) < 0) {
195 if ((function & extract_quiet) != extract_quiet) {
197 perror_msg("%s: %s", __FUNCTION__, full_name);
203 if (symlink(file_entry->link_name, full_name) < 0) {
204 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) {
218 perror_msg("Cannot create node %s", file_entry->name);
225 perror_msg("Don't know how to handle %s", full_name);
229 /* Changing a symlink's properties normally changes the properties of the
230 * file pointed to, so dont try and change the date or mode, lchown does
231 * does the right thing, but isnt available in older versions of libc */
232 if (S_ISLNK(file_entry->mode)) {
233 #if (__GLIBC__ > 2) && (__GLIBC_MINOR__ > 1)
234 lchown(full_name, file_entry->uid, file_entry->gid);
237 if (function & extract_preserve_date) {
238 t.actime = file_entry->mtime;
239 t.modtime = file_entry->mtime;
240 utime(full_name, &t);
242 chown(full_name, file_entry->uid, file_entry->gid);
243 chmod(full_name, file_entry->mode);
246 /* If we arent extracting data we have to skip it,
247 * if data size is 0 then then just do it anyway
248 * (saves testing for it) */
249 seek_sub_file(src_stream, file_entry->size);
252 /* extract_list and extract_verbose_list can be used in conjunction
253 * with one of the above four extraction functions, so do this seperately */
254 if (function & extract_verbose_list) {
255 fprintf(out_stream, "%s %d/%d %8d %s ", mode_string(file_entry->mode),
256 file_entry->uid, file_entry->gid,
257 (int) file_entry->size, time_string(file_entry->mtime));
259 if ((function & extract_list) || (function & extract_verbose_list)){
260 /* fputs doesnt add a trailing \n, so use fprintf */
261 fprintf(out_stream, "%s\n", file_entry->name);
266 if ( full_link_name )
267 free(full_link_name);
273 unarchive(FILE *src_stream, FILE *out_stream,
274 file_header_t *(*get_headers)(FILE *),
275 void (*free_headers)(file_header_t *),
276 const int extract_function,
278 const char **extract_names,
281 file_header_t *file_entry;
289 while ((file_entry = get_headers(src_stream)) != NULL) {
292 if (extract_names != NULL) {
293 int found_flag = FALSE;
294 for(i = 0; extract_names[i] != 0; i++) {
295 if (strcmp(extract_names[i], file_entry->name) == 0) {
300 if (extract_function & extract_exclude_list) {
301 if (found_flag == TRUE) {
302 extract_flag = FALSE;
305 /* If its not found in the include list dont extract it */
306 if (found_flag == FALSE) {
307 extract_flag = FALSE;
312 if (extract_flag == TRUE) {
313 buffer = extract_archive(src_stream, out_stream,
314 file_entry, extract_function,
317 free_headers(file_entry);
321 /* seek past the data entry */
322 seek_sub_file(src_stream, file_entry->size);
324 free_headers(file_entry);
330 static file_header_t *
331 get_header_ar(FILE *src_stream)
333 file_header_t *typed;
346 static char *ar_long_names;
348 if (fread(ar.raw, 1, 60, src_stream) != 60) {
351 archive_offset += 60;
352 /* align the headers based on the header magic */
353 if ((ar.formated.magic[0] != '`') || (ar.formated.magic[1] != '\n')) {
354 /* some version of ar, have an extra '\n' after each data entry,
355 * this puts the next header out by 1 */
356 if (ar.formated.magic[1] != '`') {
357 error_msg("Invalid magic");
360 /* read the next char out of what would be the data section,
361 * if its a '\n' then it is a valid header offset by 1*/
363 if (fgetc(src_stream) != '\n') {
364 error_msg("Invalid magic");
367 /* fix up the header, we started reading 1 byte too early */
368 /* raw_header[60] wont be '\n' as it should, but it doesnt matter */
369 memmove(ar.raw, &ar.raw[1], 59);
372 typed = (file_header_t *) xcalloc(1, sizeof(file_header_t));
374 typed->size = (size_t) atoi(ar.formated.size);
375 /* long filenames have '/' as the first character */
376 if (ar.formated.name[0] == '/') {
377 if (ar.formated.name[1] == '/') {
378 /* If the second char is a '/' then this entries data section
379 * stores long filename for multiple entries, they are stored
380 * in static variable long_names for use in future entries */
381 ar_long_names = (char *) xrealloc(ar_long_names, typed->size);
382 fread(ar_long_names, 1, typed->size, src_stream);
383 archive_offset += typed->size;
384 /* This ar entries data section only contained filenames for other records
385 * they are stored in the static ar_long_names for future reference */
386 return (get_header_ar(src_stream)); /* Return next header */
387 } else if (ar.formated.name[1] == ' ') {
388 /* This is the index of symbols in the file for compilers */
389 seek_sub_file(src_stream, typed->size);
390 return (get_header_ar(src_stream)); /* Return next header */
392 /* The number after the '/' indicates the offset in the ar data section
393 (saved in variable long_name) that conatains the real filename */
394 if (!ar_long_names) {
395 error_msg("Cannot resolve long file name");
398 typed->name = xstrdup(ar_long_names + atoi(&ar.formated.name[1]));
401 /* short filenames */
402 typed->name = xcalloc(1, 16);
403 strncpy(typed->name, ar.formated.name, 16);
405 typed->name[strcspn(typed->name, " /")]='\0';
407 /* convert the rest of the now valid char header to its typed struct */
408 parse_mode(ar.formated.mode, &typed->mode);
409 typed->mtime = atoi(ar.formated.date);
410 typed->uid = atoi(ar.formated.uid);
411 typed->gid = atoi(ar.formated.gid);
417 free_header_ar(file_header_t *ar_entry)
419 if (ar_entry == NULL)
422 free(ar_entry->name);
423 if (ar_entry->link_name)
424 free(ar_entry->link_name);
430 static file_header_t *
431 get_header_tar(FILE *tar_stream)
434 unsigned char raw[512];
436 char name[100]; /* 0-99 */
437 char mode[8]; /* 100-107 */
438 char uid[8]; /* 108-115 */
439 char gid[8]; /* 116-123 */
440 char size[12]; /* 124-135 */
441 char mtime[12]; /* 136-147 */
442 char chksum[8]; /* 148-155 */
443 char typeflag; /* 156-156 */
444 char linkname[100]; /* 157-256 */
445 char magic[6]; /* 257-262 */
446 char version[2]; /* 263-264 */
447 char uname[32]; /* 265-296 */
448 char gname[32]; /* 297-328 */
449 char devmajor[8]; /* 329-336 */
450 char devminor[8]; /* 337-344 */
451 char prefix[155]; /* 345-499 */
452 char padding[12]; /* 500-512 */
455 file_header_t *tar_entry = NULL;
459 if (archive_offset % 512 != 0) {
460 seek_sub_file(tar_stream, 512 - (archive_offset % 512));
463 if (fread(tar.raw, 1, 512, tar_stream) != 512) {
464 /* Unfortunatly its common for tar files to have all sorts of
465 * trailing garbage, fail silently */
466 // error_msg("Couldnt read header");
469 archive_offset += 512;
471 /* Check header has valid magic, unfortunately some tar files
472 * have empty (0'ed) tar entries at the end, which will
473 * cause this to fail, so fail silently for now
475 if (strncmp(tar.formated.magic, "ustar", 5) != 0) {
476 #ifdef CONFIG_FEATURE_TAR_OLDGNU_COMPATABILITY
477 if (strncmp(tar.formated.magic, "\0\0\0\0\0", 5) != 0)
482 /* Do checksum on headers */
483 for (i = 0; i < 148 ; i++) {
487 for (i = 156; i < 512 ; i++) {
490 if (sum != strtol(tar.formated.chksum, NULL, 8)) {
491 if ( strtol(tar.formated.chksum,NULL,8) != 0 )
492 error_msg("Invalid tar header checksum");
496 /* convert to type'ed variables */
497 tar_entry = xcalloc(1, sizeof(file_header_t));
499 #ifdef CONFIG_FEATURE_TAR_GNU_EXTENSIONS
501 tar_entry->name = longname;
505 tar_entry->name = linkname;
510 tar_entry->name = xstrndup(tar.formated.name, 100);
512 if (tar.formated.prefix[0]) {
513 char *temp = tar_entry->name;
514 char *prefixTemp = xstrndup(tar.formated.prefix, 155);
515 tar_entry->name = concat_path_file(prefixTemp, temp);
521 // tar_entry->name = xstrdup(tar.formated.name);
524 parse_mode(tar.formated.mode, &tar_entry->mode);
526 tar_entry->mode = 07777 & strtol(tar.formated.mode, NULL, 8);
528 tar_entry->uid = strtol(tar.formated.uid, NULL, 8);
529 tar_entry->gid = strtol(tar.formated.gid, NULL, 8);
530 tar_entry->size = strtol(tar.formated.size, NULL, 8);
531 tar_entry->mtime = strtol(tar.formated.mtime, NULL, 8);
532 tar_entry->link_name = *tar.formated.linkname != '\0' ? xstrndup(tar.formated.linkname, 100) : NULL;
533 tar_entry->device = (strtol(tar.formated.devmajor, NULL, 8) << 8) +
534 strtol(tar.formated.devminor, NULL, 8);
536 /* Fix mode, used by the old format */
537 switch (tar.formated.typeflag) {
538 /* hard links are detected as regular files with 0 size and a link name */
540 tar_entry->mode |= S_IFREG ;
545 # ifdef CONFIG_FEATURE_TAR_OLDGNU_COMPATABILITY
546 if (last_char_is(tar_entry->name, '/')) {
547 tar_entry->mode |= S_IFDIR;
550 tar_entry->mode |= S_IFREG;
553 tar_entry->mode |= S_IFLNK;
556 tar_entry->mode |= S_IFCHR;
559 tar_entry->mode |= S_IFBLK;
562 tar_entry->mode |= S_IFDIR;
565 tar_entry->mode |= S_IFIFO;
567 # ifdef CONFIG_FEATURE_TAR_GNU_EXTENSIONS
569 longname = xmalloc(tar_entry->size + 1);
570 if(fread(longname, tar_entry->size, 1, tar_stream) != 1)
572 longname[tar_entry->size] = '\0';
573 archive_offset += tar_entry->size;
575 return(get_header_tar(tar_stream));
578 linkname = xmalloc(tar_entry->size + 1);
579 if(fread(linkname, tar_entry->size, 1, tar_stream) != 1)
581 linkname[tar_entry->size] = '\0';
582 archive_offset += tar_entry->size;
584 tar_entry->name = linkname;
585 return(get_header_tar(tar_stream));
592 perror_msg("Ignoring GNU extension type %c", tar.formated.typeflag);
595 perror_msg("Unknown typeflag: 0x%x", tar.formated.typeflag);
604 free_header_tar(file_header_t *tar_entry)
606 if (tar_entry == NULL)
609 free(tar_entry->name);
610 if (tar_entry->link_name);
611 free(tar_entry->link_name);
617 deb_extract(const char *package_filename, FILE *out_stream,
618 const int extract_function, const char *prefix,
619 const char *filename, int *err)
621 FILE *deb_stream = NULL;
622 file_header_t *ar_header = NULL;
623 const char **file_list = NULL;
624 char *output_buffer = NULL;
625 char *ared_file = NULL;
631 if (filename != NULL) {
632 file_list = xmalloc(sizeof(char *) * 2);
633 file_list[0] = filename;
637 if (extract_function & extract_control_tar_gz) {
638 ared_file = "control.tar.gz";
640 else if (extract_function & extract_data_tar_gz) {
641 ared_file = "data.tar.gz";
643 fprintf(stderr, "no file specified to extract -- extract_function=%x\n", extract_function);
648 /* open the debian package to be worked on */
649 deb_stream = wfopen(package_filename, "r");
650 if (deb_stream == NULL) {
654 /* set the buffer size */
655 setvbuf(deb_stream, NULL, _IOFBF, 0x8000);
658 fread(ar_magic, 1, 8, deb_stream);
660 if (strncmp(ar_magic,"!<arch>",7) == 0) {
663 while ((ar_header = get_header_ar(deb_stream)) != NULL) {
664 if (strcmp(ared_file, ar_header->name) == 0) {
666 FILE *uncompressed_stream;
667 /* open a stream of decompressed data */
668 uncompressed_stream = gz_open(deb_stream, &gunzip_pid);
669 if (uncompressed_stream == NULL) {
670 printf("%s: %d\n", __FUNCTION__, __LINE__);
676 output_buffer = unarchive(uncompressed_stream,
677 out_stream, get_header_tar,
679 extract_function, prefix,
681 fclose(uncompressed_stream);
682 gz_err = gz_close(gunzip_pid);
685 free_header_ar(ar_header);
688 seek_sub_file(deb_stream, ar_header->size);
689 free_header_ar(ar_header);
692 } else if (strncmp(ar_magic, "\037\213", 2) == 0) {
693 /* it's a gz file, let's assume it's an opkg */
694 int unzipped_opkg_pid;
695 FILE *unzipped_opkg_stream;
696 file_header_t *tar_header;
698 fseek(deb_stream, 0, SEEK_SET);
699 unzipped_opkg_stream = gz_open(deb_stream, &unzipped_opkg_pid);
700 if (unzipped_opkg_stream == NULL) {
705 /* walk through outer tar file to find ared_file */
706 while ((tar_header = get_header_tar(unzipped_opkg_stream)) != NULL) {
708 if (strncmp(tar_header->name, "./", 2) == 0)
710 if (strcmp(ared_file, tar_header->name+name_offset) == 0) {
712 FILE *uncompressed_stream;
713 /* open a stream of decompressed data */
714 uncompressed_stream = gz_open(unzipped_opkg_stream, &gunzip_pid);
715 if (uncompressed_stream == NULL) {
721 output_buffer = unarchive(uncompressed_stream,
730 free_header_tar(tar_header);
731 fclose(uncompressed_stream);
732 gz_err = gz_close(gunzip_pid);
735 free_header_tar(tar_header);
738 seek_sub_file(unzipped_opkg_stream, tar_header->size);
739 free_header_tar(tar_header);
741 fclose(unzipped_opkg_stream);
742 gz_err = gz_close(unzipped_opkg_pid);
749 error_msg("%s: invalid magic", package_filename);
758 return output_buffer;