"Robert P. J. Day" | 63fc1a9 | 2006-07-02 19:47:05 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Rob Landley | 06249fe | 2006-02-20 19:28:53 +0000 | [diff] [blame] | 2 | /* Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
Glenn L McGrath | 6aa5223 | 2004-02-17 11:55:06 +0000 | [diff] [blame] | 3 | * |
Glenn L McGrath | c9f1fce | 2004-02-20 02:25:18 +0000 | [diff] [blame] | 4 | * FIXME: |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 5 | * In privileged mode if uname and gname map to a uid and gid then use the |
Glenn L McGrath | c9f1fce | 2004-02-20 02:25:18 +0000 | [diff] [blame] | 6 | * mapped value instead of the uid/gid values in tar header |
Glenn L McGrath | b0e163a | 2004-02-19 08:48:30 +0000 | [diff] [blame] | 7 | * |
| 8 | * References: |
| 9 | * GNU tar and star man pages, |
| 10 | * Opengroup's ustar interchange format, |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 11 | * http://www.opengroup.org/onlinepubs/007904975/utilities/pax.html |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 12 | */ |
| 13 | |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 14 | #include "libbb.h" |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 15 | #include "unarchive.h" |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 16 | |
Glenn L McGrath | 21110a0 | 2003-01-28 01:45:48 +0000 | [diff] [blame] | 17 | #ifdef CONFIG_FEATURE_TAR_GNU_EXTENSIONS |
Glenn L McGrath | b7a76df | 2002-11-23 10:44:47 +0000 | [diff] [blame] | 18 | static char *longname = NULL; |
| 19 | static char *linkname = NULL; |
| 20 | #endif |
| 21 | |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 22 | char get_header_tar(archive_handle_t *archive_handle) |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 23 | { |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 24 | file_header_t *file_header = archive_handle->file_header; |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 25 | union { |
Glenn L McGrath | ef91bf6 | 2003-09-12 06:31:28 +0000 | [diff] [blame] | 26 | /* ustar header, Posix 1003.1 */ |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 27 | unsigned char raw[512]; |
| 28 | struct { |
Glenn L McGrath | 99b1254 | 2002-08-22 17:47:09 +0000 | [diff] [blame] | 29 | char name[100]; /* 0-99 */ |
| 30 | char mode[8]; /* 100-107 */ |
| 31 | char uid[8]; /* 108-115 */ |
| 32 | char gid[8]; /* 116-123 */ |
| 33 | char size[12]; /* 124-135 */ |
| 34 | char mtime[12]; /* 136-147 */ |
| 35 | char chksum[8]; /* 148-155 */ |
| 36 | char typeflag; /* 156-156 */ |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 37 | char linkname[100]; /* 157-256 */ |
Glenn L McGrath | 99b1254 | 2002-08-22 17:47:09 +0000 | [diff] [blame] | 38 | char magic[6]; /* 257-262 */ |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 39 | char version[2]; /* 263-264 */ |
Glenn L McGrath | 99b1254 | 2002-08-22 17:47:09 +0000 | [diff] [blame] | 40 | char uname[32]; /* 265-296 */ |
| 41 | char gname[32]; /* 297-328 */ |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 42 | char devmajor[8]; /* 329-336 */ |
| 43 | char devminor[8]; /* 337-344 */ |
| 44 | char prefix[155]; /* 345-499 */ |
| 45 | char padding[12]; /* 500-512 */ |
"Robert P. J. Day" | eea5618 | 2006-07-20 19:02:24 +0000 | [diff] [blame] | 46 | } formatted; |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 47 | } tar; |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 48 | long sum = 0; |
| 49 | long i; |
Paul Fox | 94ff9f1 | 2005-07-20 19:24:13 +0000 | [diff] [blame] | 50 | static int end = 0; |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 51 | |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 52 | /* Align header */ |
Glenn L McGrath | 237ae42 | 2002-11-03 14:05:15 +0000 | [diff] [blame] | 53 | data_align(archive_handle, 512); |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 54 | |
Rob Landley | 5343747 | 2006-07-16 08:14:35 +0000 | [diff] [blame] | 55 | xread(archive_handle->src_fd, tar.raw, 512); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 56 | archive_handle->offset += 512; |
| 57 | |
| 58 | /* If there is no filename its an empty header */ |
"Robert P. J. Day" | eea5618 | 2006-07-20 19:02:24 +0000 | [diff] [blame] | 59 | if (tar.formatted.name[0] == 0) { |
Paul Fox | 94ff9f1 | 2005-07-20 19:24:13 +0000 | [diff] [blame] | 60 | if (end) { |
| 61 | /* This is the second consecutive empty header! End of archive! |
| 62 | * Read until the end to empty the pipe from gz or bz2 |
| 63 | */ |
Rob Landley | 5343747 | 2006-07-16 08:14:35 +0000 | [diff] [blame] | 64 | while (full_read(archive_handle->src_fd, tar.raw, 512) == 512); |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 65 | return EXIT_FAILURE; |
Paul Fox | 94ff9f1 | 2005-07-20 19:24:13 +0000 | [diff] [blame] | 66 | } |
| 67 | end = 1; |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 68 | return EXIT_SUCCESS; |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 69 | } |
Paul Fox | 94ff9f1 | 2005-07-20 19:24:13 +0000 | [diff] [blame] | 70 | end = 0; |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 71 | |
Glenn L McGrath | 1d23f3a | 2002-08-13 05:06:43 +0000 | [diff] [blame] | 72 | /* Check header has valid magic, "ustar" is for the proper tar |
| 73 | * 0's are for the old tar format |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 74 | */ |
"Robert P. J. Day" | eea5618 | 2006-07-20 19:02:24 +0000 | [diff] [blame] | 75 | if (strncmp(tar.formatted.magic, "ustar", 5) != 0) { |
Bernhard Reutner-Fischer | a1bccc0 | 2006-04-02 20:17:55 +0000 | [diff] [blame] | 76 | #ifdef CONFIG_FEATURE_TAR_OLDGNU_COMPATIBILITY |
Denis Vlasenko | cba9ef5 | 2006-10-10 21:00:47 +0000 | [diff] [blame] | 77 | if (memcmp(tar.formatted.magic, "\0\0\0\0", 5) != 0) |
Glenn L McGrath | 1d23f3a | 2002-08-13 05:06:43 +0000 | [diff] [blame] | 78 | #endif |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 79 | bb_error_msg_and_die("invalid tar magic"); |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 80 | } |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 81 | /* Do checksum on headers */ |
Denis Vlasenko | cba9ef5 | 2006-10-10 21:00:47 +0000 | [diff] [blame] | 82 | for (i = 0; i < 148 ; i++) { |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 83 | sum += tar.raw[i]; |
| 84 | } |
| 85 | sum += ' ' * 8; |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 86 | for (i = 156; i < 512 ; i++) { |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 87 | sum += tar.raw[i]; |
| 88 | } |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 89 | if (sum != xstrtoul(tar.formatted.chksum, 8)) { |
| 90 | bb_error_msg_and_die("invalid tar header checksum"); |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 91 | } |
| 92 | |
Glenn L McGrath | 21110a0 | 2003-01-28 01:45:48 +0000 | [diff] [blame] | 93 | #ifdef CONFIG_FEATURE_TAR_GNU_EXTENSIONS |
Glenn L McGrath | b7a76df | 2002-11-23 10:44:47 +0000 | [diff] [blame] | 94 | if (longname) { |
| 95 | file_header->name = longname; |
| 96 | longname = NULL; |
| 97 | } |
| 98 | else if (linkname) { |
| 99 | file_header->name = linkname; |
| 100 | linkname = NULL; |
| 101 | } else |
| 102 | #endif |
Rob Landley | 06249fe | 2006-02-20 19:28:53 +0000 | [diff] [blame] | 103 | { |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 104 | file_header->name = xstrndup(tar.formatted.name, 100); |
"Robert P. J. Day" | eea5618 | 2006-07-20 19:02:24 +0000 | [diff] [blame] | 105 | if (tar.formatted.prefix[0]) { |
Rob Landley | 06249fe | 2006-02-20 19:28:53 +0000 | [diff] [blame] | 106 | char *temp = file_header->name; |
"Robert P. J. Day" | eea5618 | 2006-07-20 19:02:24 +0000 | [diff] [blame] | 107 | file_header->name = concat_path_file(tar.formatted.prefix, temp); |
Rob Landley | 06249fe | 2006-02-20 19:28:53 +0000 | [diff] [blame] | 108 | free(temp); |
| 109 | } |
Glenn L McGrath | 7576270 | 2002-08-22 11:50:31 +0000 | [diff] [blame] | 110 | } |
Glenn L McGrath | c5c1a8a | 2002-10-19 06:19:22 +0000 | [diff] [blame] | 111 | |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 112 | file_header->uid = xstrtoul(tar.formatted.uid, 8); |
| 113 | file_header->gid = xstrtoul(tar.formatted.gid, 8); |
Denis Vlasenko | cba9ef5 | 2006-10-10 21:00:47 +0000 | [diff] [blame] | 114 | file_header->size = XSTRTOUOFF(tar.formatted.size, 8); |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 115 | file_header->mtime = xstrtoul(tar.formatted.mtime, 8); |
| 116 | file_header->link_name = tar.formatted.linkname[0] ? |
| 117 | xstrdup(tar.formatted.linkname) : NULL; |
Denis Vlasenko | cba9ef5 | 2006-10-10 21:00:47 +0000 | [diff] [blame] | 118 | if (tar.formatted.devmajor[0]) { |
| 119 | file_header->device = makedev(xstrtoul(tar.formatted.devmajor, 8), |
| 120 | xstrtoul(tar.formatted.devminor, 8)); |
| 121 | } |
Glenn L McGrath | b3c4e9a | 2002-09-15 16:54:49 +0000 | [diff] [blame] | 122 | |
Glenn L McGrath | 916ba53 | 2004-02-20 02:34:42 +0000 | [diff] [blame] | 123 | /* Set bits 0-11 of the files mode */ |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 124 | file_header->mode = 07777 & xstrtoul(tar.formatted.mode, 8); |
Glenn L McGrath | 916ba53 | 2004-02-20 02:34:42 +0000 | [diff] [blame] | 125 | |
| 126 | /* Set bits 12-15 of the files mode */ |
"Robert P. J. Day" | eea5618 | 2006-07-20 19:02:24 +0000 | [diff] [blame] | 127 | switch (tar.formatted.typeflag) { |
Glenn L McGrath | b0e163a | 2004-02-19 08:48:30 +0000 | [diff] [blame] | 128 | /* busybox identifies hard links as being regular files with 0 size and a link name */ |
| 129 | case '1': |
Glenn L McGrath | 916ba53 | 2004-02-20 02:34:42 +0000 | [diff] [blame] | 130 | file_header->mode |= S_IFREG; |
Glenn L McGrath | b0e163a | 2004-02-19 08:48:30 +0000 | [diff] [blame] | 131 | break; |
Glenn L McGrath | c9f1fce | 2004-02-20 02:25:18 +0000 | [diff] [blame] | 132 | case '7': |
| 133 | /* Reserved for high performance files, treat as normal file */ |
Glenn L McGrath | 99b1254 | 2002-08-22 17:47:09 +0000 | [diff] [blame] | 134 | case 0: |
Glenn L McGrath | 21110a0 | 2003-01-28 01:45:48 +0000 | [diff] [blame] | 135 | case '0': |
Bernhard Reutner-Fischer | a1bccc0 | 2006-04-02 20:17:55 +0000 | [diff] [blame] | 136 | #ifdef CONFIG_FEATURE_TAR_OLDGNU_COMPATIBILITY |
Glenn L McGrath | 87af49f | 2003-09-09 17:41:03 +0000 | [diff] [blame] | 137 | if (last_char_is(file_header->name, '/')) { |
| 138 | file_header->mode |= S_IFDIR; |
Glenn L McGrath | c9f1fce | 2004-02-20 02:25:18 +0000 | [diff] [blame] | 139 | } else |
| 140 | #endif |
Glenn L McGrath | 87af49f | 2003-09-09 17:41:03 +0000 | [diff] [blame] | 141 | file_header->mode |= S_IFREG; |
Glenn L McGrath | 99b1254 | 2002-08-22 17:47:09 +0000 | [diff] [blame] | 142 | break; |
Glenn L McGrath | 21110a0 | 2003-01-28 01:45:48 +0000 | [diff] [blame] | 143 | case '2': |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 144 | file_header->mode |= S_IFLNK; |
Glenn L McGrath | 99b1254 | 2002-08-22 17:47:09 +0000 | [diff] [blame] | 145 | break; |
Glenn L McGrath | 21110a0 | 2003-01-28 01:45:48 +0000 | [diff] [blame] | 146 | case '3': |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 147 | file_header->mode |= S_IFCHR; |
Glenn L McGrath | 99b1254 | 2002-08-22 17:47:09 +0000 | [diff] [blame] | 148 | break; |
Glenn L McGrath | 21110a0 | 2003-01-28 01:45:48 +0000 | [diff] [blame] | 149 | case '4': |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 150 | file_header->mode |= S_IFBLK; |
Glenn L McGrath | 99b1254 | 2002-08-22 17:47:09 +0000 | [diff] [blame] | 151 | break; |
Glenn L McGrath | 21110a0 | 2003-01-28 01:45:48 +0000 | [diff] [blame] | 152 | case '5': |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 153 | file_header->mode |= S_IFDIR; |
Glenn L McGrath | 99b1254 | 2002-08-22 17:47:09 +0000 | [diff] [blame] | 154 | break; |
Glenn L McGrath | 21110a0 | 2003-01-28 01:45:48 +0000 | [diff] [blame] | 155 | case '6': |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 156 | file_header->mode |= S_IFIFO; |
Glenn L McGrath | 99b1254 | 2002-08-22 17:47:09 +0000 | [diff] [blame] | 157 | break; |
Glenn L McGrath | 6aa5223 | 2004-02-17 11:55:06 +0000 | [diff] [blame] | 158 | #ifdef CONFIG_FEATURE_TAR_GNU_EXTENSIONS |
Glenn L McGrath | b3c4e9a | 2002-09-15 16:54:49 +0000 | [diff] [blame] | 159 | case 'L': { |
Rob Landley | 1ec5b29 | 2006-05-29 07:42:02 +0000 | [diff] [blame] | 160 | longname = xzalloc(file_header->size + 1); |
Rob Landley | 5343747 | 2006-07-16 08:14:35 +0000 | [diff] [blame] | 161 | xread(archive_handle->src_fd, longname, file_header->size); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 162 | archive_handle->offset += file_header->size; |
Glenn L McGrath | b3c4e9a | 2002-09-15 16:54:49 +0000 | [diff] [blame] | 163 | |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 164 | return get_header_tar(archive_handle); |
Glenn L McGrath | b3c4e9a | 2002-09-15 16:54:49 +0000 | [diff] [blame] | 165 | } |
| 166 | case 'K': { |
Rob Landley | 1ec5b29 | 2006-05-29 07:42:02 +0000 | [diff] [blame] | 167 | linkname = xzalloc(file_header->size + 1); |
Rob Landley | 5343747 | 2006-07-16 08:14:35 +0000 | [diff] [blame] | 168 | xread(archive_handle->src_fd, linkname, file_header->size); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 169 | archive_handle->offset += file_header->size; |
Glenn L McGrath | b3c4e9a | 2002-09-15 16:54:49 +0000 | [diff] [blame] | 170 | |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 171 | file_header->name = linkname; |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 172 | return get_header_tar(archive_handle); |
Glenn L McGrath | b3c4e9a | 2002-09-15 16:54:49 +0000 | [diff] [blame] | 173 | } |
Glenn L McGrath | b0e163a | 2004-02-19 08:48:30 +0000 | [diff] [blame] | 174 | case 'D': /* GNU dump dir */ |
| 175 | case 'M': /* Continuation of multi volume archive*/ |
| 176 | case 'N': /* Old GNU for names > 100 characters */ |
| 177 | case 'S': /* Sparse file */ |
| 178 | case 'V': /* Volume header */ |
Glenn L McGrath | 6aa5223 | 2004-02-17 11:55:06 +0000 | [diff] [blame] | 179 | #endif |
Rob Landley | ff6e21c | 2006-07-06 20:30:19 +0000 | [diff] [blame] | 180 | case 'g': /* pax global header */ |
| 181 | case 'x': /* pax extended header */ |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 182 | bb_error_msg("ignoring extension type %c", tar.formatted.typeflag); |
Rob Landley | ff6e21c | 2006-07-06 20:30:19 +0000 | [diff] [blame] | 183 | break; |
Glenn L McGrath | b0e163a | 2004-02-19 08:48:30 +0000 | [diff] [blame] | 184 | default: |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 185 | bb_error_msg("unknown typeflag: 0x%x", tar.formatted.typeflag); |
Glenn L McGrath | 6aa5223 | 2004-02-17 11:55:06 +0000 | [diff] [blame] | 186 | } |
| 187 | { /* Strip trailing '/' in directories */ |
| 188 | /* Must be done after mode is set as '/' is used to check if its a directory */ |
| 189 | char *tmp = last_char_is(file_header->name, '/'); |
| 190 | if (tmp) { |
| 191 | *tmp = '\0'; |
| 192 | } |
Glenn L McGrath | 1d23f3a | 2002-08-13 05:06:43 +0000 | [diff] [blame] | 193 | } |
Glenn L McGrath | 3d5828f | 2003-08-14 02:55:15 +0000 | [diff] [blame] | 194 | |
Glenn L McGrath | 8e94098 | 2002-11-04 23:47:31 +0000 | [diff] [blame] | 195 | if (archive_handle->filter(archive_handle) == EXIT_SUCCESS) { |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 196 | archive_handle->action_header(archive_handle->file_header); |
| 197 | archive_handle->flags |= ARCHIVE_EXTRACT_QUIET; |
| 198 | archive_handle->action_data(archive_handle); |
Rob Landley | 8bb5078 | 2006-05-26 23:44:51 +0000 | [diff] [blame] | 199 | llist_add_to(&(archive_handle->passed), file_header->name); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 200 | } else { |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 201 | data_skip(archive_handle); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 202 | } |
| 203 | archive_handle->offset += file_header->size; |
| 204 | |
Glenn L McGrath | 54ac057 | 2003-11-15 00:24:43 +0000 | [diff] [blame] | 205 | free(file_header->link_name); |
| 206 | |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 207 | return EXIT_SUCCESS; |
Eric Andersen | 2276d83 | 2002-07-11 11:11:56 +0000 | [diff] [blame] | 208 | } |