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 | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 23 | extern char get_header_tar(archive_handle_t *archive_handle) |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 24 | { |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 25 | file_header_t *file_header = archive_handle->file_header; |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 26 | union { |
| 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 */ |
| 46 | } formated; |
| 47 | } tar; |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 48 | long sum = 0; |
| 49 | long i; |
Glenn L McGrath | c5c1a8a | 2002-10-19 06:19:22 +0000 | [diff] [blame] | 50 | char *tmp; |
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 */ |
| 53 | archive_handle->offset += data_align(archive_handle->src_fd, archive_handle->offset, 512); |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 54 | |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 55 | if (xread_all_eof(archive_handle->src_fd, tar.raw, 512) == 0) { |
| 56 | /* End of file */ |
| 57 | return(EXIT_FAILURE); |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 58 | } |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 59 | archive_handle->offset += 512; |
| 60 | |
| 61 | /* If there is no filename its an empty header */ |
| 62 | if (tar.formated.name[0] == 0) { |
| 63 | return(EXIT_SUCCESS); |
| 64 | } |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 65 | |
Glenn L McGrath | 1d23f3a | 2002-08-13 05:06:43 +0000 | [diff] [blame] | 66 | /* Check header has valid magic, "ustar" is for the proper tar |
| 67 | * 0's are for the old tar format |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 68 | */ |
| 69 | if (strncmp(tar.formated.magic, "ustar", 5) != 0) { |
Glenn L McGrath | 1d23f3a | 2002-08-13 05:06:43 +0000 | [diff] [blame] | 70 | #ifdef CONFIG_FEATURE_TAR_OLD_FORMAT |
| 71 | if (strncmp(tar.formated.magic, "\0\0\0\0\0", 5) != 0) |
| 72 | #endif |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 73 | error_msg_and_die("Invalid tar magic"); |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | /* Do checksum on headers */ |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 77 | for (i = 0; i < 148 ; i++) { |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 78 | sum += tar.raw[i]; |
| 79 | } |
| 80 | sum += ' ' * 8; |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 81 | for (i = 156; i < 512 ; i++) { |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 82 | sum += tar.raw[i]; |
| 83 | } |
| 84 | if (sum != strtol(tar.formated.chksum, NULL, 8)) { |
| 85 | error_msg("Invalid tar header checksum"); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 86 | return(EXIT_FAILURE); |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | /* convert to type'ed variables */ |
Glenn L McGrath | 7576270 | 2002-08-22 11:50:31 +0000 | [diff] [blame] | 90 | if (tar.formated.prefix[0] == 0) { |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 91 | file_header->name = strdup(tar.formated.name); |
Glenn L McGrath | 7576270 | 2002-08-22 11:50:31 +0000 | [diff] [blame] | 92 | } else { |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 93 | 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] | 94 | } |
Glenn L McGrath | c5c1a8a | 2002-10-19 06:19:22 +0000 | [diff] [blame] | 95 | tmp = last_char_is(archive_handle->file_header->name, '/'); |
| 96 | if (tmp) { |
| 97 | *tmp = '\0'; |
| 98 | } |
| 99 | |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 100 | file_header->mode = strtol(tar.formated.mode, NULL, 8); |
| 101 | file_header->uid = strtol(tar.formated.uid, NULL, 8); |
| 102 | file_header->gid = strtol(tar.formated.gid, NULL, 8); |
| 103 | file_header->size = strtol(tar.formated.size, NULL, 8); |
| 104 | file_header->mtime = strtol(tar.formated.mtime, NULL, 8); |
| 105 | file_header->link_name = (tar.formated.linkname[0] != '\0') ? |
| 106 | xstrdup(tar.formated.linkname) : NULL; |
| 107 | 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] | 108 | strtol(tar.formated.devminor, NULL, 8)); |
| 109 | |
| 110 | #if defined CONFIG_FEATURE_TAR_OLD_FORMAT || defined CONFIG_FEATURE_GNUTAR_LONG_FILENAME |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 111 | /* Fix mode, used by the old format */ |
Glenn L McGrath | 1d23f3a | 2002-08-13 05:06:43 +0000 | [diff] [blame] | 112 | switch (tar.formated.typeflag) { |
Glenn L McGrath | b3c4e9a | 2002-09-15 16:54:49 +0000 | [diff] [blame] | 113 | # ifdef CONFIG_FEATURE_TAR_OLD_FORMAT |
Glenn L McGrath | 99b1254 | 2002-08-22 17:47:09 +0000 | [diff] [blame] | 114 | case 0: |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 115 | file_header->mode |= S_IFREG; |
Glenn L McGrath | 99b1254 | 2002-08-22 17:47:09 +0000 | [diff] [blame] | 116 | break; |
| 117 | case 1: |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 118 | error_msg("Internal hard link not supported"); |
Glenn L McGrath | 99b1254 | 2002-08-22 17:47:09 +0000 | [diff] [blame] | 119 | break; |
| 120 | case 2: |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 121 | file_header->mode |= S_IFLNK; |
Glenn L McGrath | 99b1254 | 2002-08-22 17:47:09 +0000 | [diff] [blame] | 122 | break; |
| 123 | case 3: |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 124 | file_header->mode |= S_IFCHR; |
Glenn L McGrath | 99b1254 | 2002-08-22 17:47:09 +0000 | [diff] [blame] | 125 | break; |
| 126 | case 4: |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 127 | file_header->mode |= S_IFBLK; |
Glenn L McGrath | 99b1254 | 2002-08-22 17:47:09 +0000 | [diff] [blame] | 128 | break; |
| 129 | case 5: |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 130 | file_header->mode |= S_IFDIR; |
Glenn L McGrath | 99b1254 | 2002-08-22 17:47:09 +0000 | [diff] [blame] | 131 | break; |
| 132 | case 6: |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 133 | file_header->mode |= S_IFIFO; |
Glenn L McGrath | 99b1254 | 2002-08-22 17:47:09 +0000 | [diff] [blame] | 134 | break; |
Glenn L McGrath | b3c4e9a | 2002-09-15 16:54:49 +0000 | [diff] [blame] | 135 | # endif |
| 136 | # ifdef CONFIG_FEATURE_GNUTAR_LONG_FILENAME |
| 137 | case 'L': { |
| 138 | char *longname; |
| 139 | |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 140 | longname = xmalloc(file_header->size + 1); |
| 141 | xread_all(archive_handle->src_fd, longname, file_header->size); |
| 142 | longname[file_header->size] = '\0'; |
| 143 | archive_handle->offset += file_header->size; |
Glenn L McGrath | b3c4e9a | 2002-09-15 16:54:49 +0000 | [diff] [blame] | 144 | |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 145 | get_header_tar(archive_handle); |
| 146 | file_header->name = longname; |
Glenn L McGrath | b3c4e9a | 2002-09-15 16:54:49 +0000 | [diff] [blame] | 147 | break; |
| 148 | } |
| 149 | case 'K': { |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 150 | char *linkname; |
Glenn L McGrath | b3c4e9a | 2002-09-15 16:54:49 +0000 | [diff] [blame] | 151 | |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 152 | linkname = xmalloc(file_header->size + 1); |
| 153 | xread_all(archive_handle->src_fd, linkname, file_header->size); |
| 154 | linkname[file_header->size] = '\0'; |
| 155 | archive_handle->offset += file_header->size; |
Glenn L McGrath | b3c4e9a | 2002-09-15 16:54:49 +0000 | [diff] [blame] | 156 | |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 157 | get_header_tar(archive_handle); |
| 158 | file_header->name = linkname; |
Glenn L McGrath | b3c4e9a | 2002-09-15 16:54:49 +0000 | [diff] [blame] | 159 | break; |
| 160 | } |
| 161 | # endif |
Glenn L McGrath | 1d23f3a | 2002-08-13 05:06:43 +0000 | [diff] [blame] | 162 | } |
| 163 | #endif |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 164 | if (archive_handle->filter(archive_handle->accept, archive_handle->reject, archive_handle->file_header->name) == EXIT_SUCCESS) { |
| 165 | archive_handle->action_header(archive_handle->file_header); |
| 166 | archive_handle->flags |= ARCHIVE_EXTRACT_QUIET; |
| 167 | archive_handle->action_data(archive_handle); |
Glenn L McGrath | c5c1a8a | 2002-10-19 06:19:22 +0000 | [diff] [blame] | 168 | archive_handle->passed = add_to_list(archive_handle->passed, archive_handle->file_header->name); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 169 | } else { |
| 170 | data_skip(archive_handle); |
| 171 | } |
| 172 | archive_handle->offset += file_header->size; |
| 173 | |
| 174 | return(EXIT_SUCCESS); |
Eric Andersen | 2276d83 | 2002-07-11 11:11:56 +0000 | [diff] [blame] | 175 | } |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 176 | |