Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This program is free software; you can redistribute it and/or modify |
| 3 | * it under the terms of the GNU General Public License as published by |
| 4 | * the Free Software Foundation; either version 2 of the License, or |
| 5 | * (at your option) any later version. |
| 6 | * |
| 7 | * This program is distributed in the hope that it will be useful, |
| 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | * GNU Library General Public License for more details. |
| 11 | * |
| 12 | * You should have received a copy of the GNU General Public License |
| 13 | * along with this program; if not, write to the Free Software |
| 14 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 15 | */ |
| 16 | |
| 17 | #include <stdio.h> |
| 18 | #include <stdlib.h> |
| 19 | #include <string.h> |
| 20 | #include "unarchive.h" |
| 21 | #include "libbb.h" |
| 22 | |
Glenn L McGrath | 21110a0 | 2003-01-28 01:45:48 +0000 | [diff] [blame] | 23 | #ifdef CONFIG_FEATURE_TAR_GNU_EXTENSIONS |
Glenn L McGrath | b7a76df | 2002-11-23 10:44:47 +0000 | [diff] [blame] | 24 | static char *longname = NULL; |
| 25 | static char *linkname = NULL; |
| 26 | #endif |
| 27 | |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 28 | extern char get_header_tar(archive_handle_t *archive_handle) |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 29 | { |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 30 | file_header_t *file_header = archive_handle->file_header; |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 31 | union { |
| 32 | unsigned char raw[512]; |
| 33 | struct { |
Glenn L McGrath | 99b1254 | 2002-08-22 17:47:09 +0000 | [diff] [blame] | 34 | char name[100]; /* 0-99 */ |
| 35 | char mode[8]; /* 100-107 */ |
| 36 | char uid[8]; /* 108-115 */ |
| 37 | char gid[8]; /* 116-123 */ |
| 38 | char size[12]; /* 124-135 */ |
| 39 | char mtime[12]; /* 136-147 */ |
| 40 | char chksum[8]; /* 148-155 */ |
| 41 | char typeflag; /* 156-156 */ |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 42 | char linkname[100]; /* 157-256 */ |
Glenn L McGrath | 99b1254 | 2002-08-22 17:47:09 +0000 | [diff] [blame] | 43 | char magic[6]; /* 257-262 */ |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 44 | char version[2]; /* 263-264 */ |
Glenn L McGrath | 99b1254 | 2002-08-22 17:47:09 +0000 | [diff] [blame] | 45 | char uname[32]; /* 265-296 */ |
| 46 | char gname[32]; /* 297-328 */ |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 47 | char devmajor[8]; /* 329-336 */ |
| 48 | char devminor[8]; /* 337-344 */ |
| 49 | char prefix[155]; /* 345-499 */ |
| 50 | char padding[12]; /* 500-512 */ |
| 51 | } formated; |
| 52 | } tar; |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 53 | long sum = 0; |
| 54 | long i; |
Glenn L McGrath | c5c1a8a | 2002-10-19 06:19:22 +0000 | [diff] [blame] | 55 | char *tmp; |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 56 | |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 57 | /* Align header */ |
Glenn L McGrath | 237ae42 | 2002-11-03 14:05:15 +0000 | [diff] [blame] | 58 | data_align(archive_handle, 512); |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 59 | |
Glenn L McGrath | 7f2a953 | 2002-11-05 02:56:57 +0000 | [diff] [blame] | 60 | if (archive_xread(archive_handle, tar.raw, 512) != 512) { |
| 61 | /* Assume end of file */ |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 62 | return(EXIT_FAILURE); |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 63 | } |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 64 | archive_handle->offset += 512; |
| 65 | |
| 66 | /* If there is no filename its an empty header */ |
| 67 | if (tar.formated.name[0] == 0) { |
| 68 | return(EXIT_SUCCESS); |
| 69 | } |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 70 | |
Glenn L McGrath | 1d23f3a | 2002-08-13 05:06:43 +0000 | [diff] [blame] | 71 | /* Check header has valid magic, "ustar" is for the proper tar |
| 72 | * 0's are for the old tar format |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 73 | */ |
| 74 | if (strncmp(tar.formated.magic, "ustar", 5) != 0) { |
Glenn L McGrath | 21110a0 | 2003-01-28 01:45:48 +0000 | [diff] [blame] | 75 | #ifdef CONFIG_FEATURE_TAR_OLDGNU_COMPATABILITY |
Glenn L McGrath | 1d23f3a | 2002-08-13 05:06:43 +0000 | [diff] [blame] | 76 | if (strncmp(tar.formated.magic, "\0\0\0\0\0", 5) != 0) |
| 77 | #endif |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 78 | bb_error_msg_and_die("Invalid tar magic"); |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 79 | } |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 80 | /* Do checksum on headers */ |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 81 | for (i = 0; i < 148 ; i++) { |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 82 | sum += tar.raw[i]; |
| 83 | } |
| 84 | sum += ' ' * 8; |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 85 | for (i = 156; i < 512 ; i++) { |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 86 | sum += tar.raw[i]; |
| 87 | } |
| 88 | if (sum != strtol(tar.formated.chksum, NULL, 8)) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 89 | bb_error_msg("Invalid tar header checksum"); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 90 | return(EXIT_FAILURE); |
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 |
Glenn L McGrath | 7576270 | 2002-08-22 11:50:31 +0000 | [diff] [blame] | 103 | if (tar.formated.prefix[0] == 0) { |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 104 | file_header->name = strdup(tar.formated.name); |
Glenn L McGrath | 7576270 | 2002-08-22 11:50:31 +0000 | [diff] [blame] | 105 | } else { |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 106 | file_header->name = concat_path_file(tar.formated.prefix, tar.formated.name); |
Glenn L McGrath | 7576270 | 2002-08-22 11:50:31 +0000 | [diff] [blame] | 107 | } |
Glenn L McGrath | c5c1a8a | 2002-10-19 06:19:22 +0000 | [diff] [blame] | 108 | tmp = last_char_is(archive_handle->file_header->name, '/'); |
| 109 | if (tmp) { |
| 110 | *tmp = '\0'; |
| 111 | } |
| 112 | |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 113 | file_header->mode = strtol(tar.formated.mode, NULL, 8); |
| 114 | file_header->uid = strtol(tar.formated.uid, NULL, 8); |
| 115 | file_header->gid = strtol(tar.formated.gid, NULL, 8); |
| 116 | file_header->size = strtol(tar.formated.size, NULL, 8); |
| 117 | file_header->mtime = strtol(tar.formated.mtime, NULL, 8); |
| 118 | file_header->link_name = (tar.formated.linkname[0] != '\0') ? |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 119 | bb_xstrdup(tar.formated.linkname) : NULL; |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 120 | file_header->device = (dev_t) ((strtol(tar.formated.devmajor, NULL, 8) << 8) + |
Glenn L McGrath | b3c4e9a | 2002-09-15 16:54:49 +0000 | [diff] [blame] | 121 | strtol(tar.formated.devminor, NULL, 8)); |
| 122 | |
Glenn L McGrath | 21110a0 | 2003-01-28 01:45:48 +0000 | [diff] [blame] | 123 | #if defined CONFIG_FEATURE_TAR_OLDGNU_COMPATABILITY || defined CONFIG_FEATURE_TAR_GNU_EXTENSIONS |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 124 | /* Fix mode, used by the old format */ |
Glenn L McGrath | 1d23f3a | 2002-08-13 05:06:43 +0000 | [diff] [blame] | 125 | switch (tar.formated.typeflag) { |
Glenn L McGrath | 21110a0 | 2003-01-28 01:45:48 +0000 | [diff] [blame] | 126 | # ifdef CONFIG_FEATURE_TAR_OLDGNU_COMPATABILITY |
Glenn L McGrath | 99b1254 | 2002-08-22 17:47:09 +0000 | [diff] [blame] | 127 | case 0: |
Glenn L McGrath | 21110a0 | 2003-01-28 01:45:48 +0000 | [diff] [blame] | 128 | case '0': |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 129 | file_header->mode |= S_IFREG; |
Glenn L McGrath | 99b1254 | 2002-08-22 17:47:09 +0000 | [diff] [blame] | 130 | break; |
Glenn L McGrath | 3d5828f | 2003-08-14 02:55:15 +0000 | [diff] [blame] | 131 | #if 0 |
| 132 | /* hard links are detected as entries with 0 size, a link name, |
| 133 | * and not being a symlink, hence we have nothing to do here */ |
Glenn L McGrath | 21110a0 | 2003-01-28 01:45:48 +0000 | [diff] [blame] | 134 | case '1': |
Glenn L McGrath | 3d5828f | 2003-08-14 02:55:15 +0000 | [diff] [blame] | 135 | break; |
| 136 | #endif |
Glenn L McGrath | 21110a0 | 2003-01-28 01:45:48 +0000 | [diff] [blame] | 137 | case '2': |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 138 | file_header->mode |= S_IFLNK; |
Glenn L McGrath | 99b1254 | 2002-08-22 17:47:09 +0000 | [diff] [blame] | 139 | break; |
Glenn L McGrath | 21110a0 | 2003-01-28 01:45:48 +0000 | [diff] [blame] | 140 | case '3': |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 141 | file_header->mode |= S_IFCHR; |
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 '4': |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 144 | file_header->mode |= S_IFBLK; |
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 '5': |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 147 | file_header->mode |= S_IFDIR; |
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 '6': |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 150 | file_header->mode |= S_IFIFO; |
Glenn L McGrath | 99b1254 | 2002-08-22 17:47:09 +0000 | [diff] [blame] | 151 | break; |
Glenn L McGrath | b3c4e9a | 2002-09-15 16:54:49 +0000 | [diff] [blame] | 152 | # endif |
Glenn L McGrath | 21110a0 | 2003-01-28 01:45:48 +0000 | [diff] [blame] | 153 | # ifdef CONFIG_FEATURE_TAR_GNU_EXTENSIONS |
Glenn L McGrath | b3c4e9a | 2002-09-15 16:54:49 +0000 | [diff] [blame] | 154 | case 'L': { |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 155 | longname = xmalloc(file_header->size + 1); |
Glenn L McGrath | 237ae42 | 2002-11-03 14:05:15 +0000 | [diff] [blame] | 156 | archive_xread_all(archive_handle, longname, file_header->size); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 157 | longname[file_header->size] = '\0'; |
| 158 | archive_handle->offset += file_header->size; |
Glenn L McGrath | b3c4e9a | 2002-09-15 16:54:49 +0000 | [diff] [blame] | 159 | |
Glenn L McGrath | b7a76df | 2002-11-23 10:44:47 +0000 | [diff] [blame] | 160 | return(get_header_tar(archive_handle)); |
Glenn L McGrath | b3c4e9a | 2002-09-15 16:54:49 +0000 | [diff] [blame] | 161 | } |
| 162 | case 'K': { |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 163 | linkname = xmalloc(file_header->size + 1); |
Glenn L McGrath | 237ae42 | 2002-11-03 14:05:15 +0000 | [diff] [blame] | 164 | archive_xread_all(archive_handle, linkname, file_header->size); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 165 | linkname[file_header->size] = '\0'; |
| 166 | archive_handle->offset += file_header->size; |
Glenn L McGrath | b3c4e9a | 2002-09-15 16:54:49 +0000 | [diff] [blame] | 167 | |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 168 | file_header->name = linkname; |
Glenn L McGrath | b7a76df | 2002-11-23 10:44:47 +0000 | [diff] [blame] | 169 | return(get_header_tar(archive_handle)); |
Glenn L McGrath | b3c4e9a | 2002-09-15 16:54:49 +0000 | [diff] [blame] | 170 | } |
Glenn L McGrath | 21110a0 | 2003-01-28 01:45:48 +0000 | [diff] [blame] | 171 | case 'D': |
| 172 | case 'M': |
| 173 | case 'N': |
| 174 | case 'S': |
| 175 | case 'V': |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 176 | bb_error_msg("Ignoring GNU extension type %c", tar.formated.typeflag); |
Glenn L McGrath | b3c4e9a | 2002-09-15 16:54:49 +0000 | [diff] [blame] | 177 | # endif |
Glenn L McGrath | 1d23f3a | 2002-08-13 05:06:43 +0000 | [diff] [blame] | 178 | } |
| 179 | #endif |
Glenn L McGrath | 3d5828f | 2003-08-14 02:55:15 +0000 | [diff] [blame] | 180 | |
Glenn L McGrath | 8e94098 | 2002-11-04 23:47:31 +0000 | [diff] [blame] | 181 | if (archive_handle->filter(archive_handle) == EXIT_SUCCESS) { |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 182 | archive_handle->action_header(archive_handle->file_header); |
| 183 | archive_handle->flags |= ARCHIVE_EXTRACT_QUIET; |
| 184 | archive_handle->action_data(archive_handle); |
Glenn L McGrath | 66125c8 | 2002-12-08 00:54:33 +0000 | [diff] [blame] | 185 | archive_handle->passed = llist_add_to(archive_handle->passed, archive_handle->file_header->name); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 186 | } else { |
| 187 | data_skip(archive_handle); |
| 188 | } |
| 189 | archive_handle->offset += file_header->size; |
| 190 | |
| 191 | return(EXIT_SUCCESS); |
Eric Andersen | 2276d83 | 2002-07-11 11:11:56 +0000 | [diff] [blame] | 192 | } |