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. |
Glenn L McGrath | 6aa5223 | 2004-02-17 11:55:06 +0000 | [diff] [blame] | 15 | * |
| 16 | * FIXME: Better checking required in oldcompatability mode, |
| 17 | * the file db.1.85.tar.gz from sleepycat.com has trailing garbage |
| 18 | * GNU tar can handle it, busybox tar reports invalid tar header. |
Glenn L McGrath | b0e163a | 2004-02-19 08:48:30 +0000 | [diff] [blame^] | 19 | * |
| 20 | * References: |
| 21 | * GNU tar and star man pages, |
| 22 | * Opengroup's ustar interchange format, |
| 23 | * http://www.opengroup.org/onlinepubs/007904975/utilities/pax.html |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 24 | */ |
| 25 | |
| 26 | #include <stdio.h> |
| 27 | #include <stdlib.h> |
| 28 | #include <string.h> |
| 29 | #include "unarchive.h" |
| 30 | #include "libbb.h" |
| 31 | |
Glenn L McGrath | 21110a0 | 2003-01-28 01:45:48 +0000 | [diff] [blame] | 32 | #ifdef CONFIG_FEATURE_TAR_GNU_EXTENSIONS |
Glenn L McGrath | b7a76df | 2002-11-23 10:44:47 +0000 | [diff] [blame] | 33 | static char *longname = NULL; |
| 34 | static char *linkname = NULL; |
| 35 | #endif |
| 36 | |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 37 | extern char get_header_tar(archive_handle_t *archive_handle) |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 38 | { |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 39 | file_header_t *file_header = archive_handle->file_header; |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 40 | union { |
Glenn L McGrath | ef91bf6 | 2003-09-12 06:31:28 +0000 | [diff] [blame] | 41 | /* ustar header, Posix 1003.1 */ |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 42 | unsigned char raw[512]; |
| 43 | struct { |
Glenn L McGrath | 99b1254 | 2002-08-22 17:47:09 +0000 | [diff] [blame] | 44 | char name[100]; /* 0-99 */ |
| 45 | char mode[8]; /* 100-107 */ |
| 46 | char uid[8]; /* 108-115 */ |
| 47 | char gid[8]; /* 116-123 */ |
| 48 | char size[12]; /* 124-135 */ |
| 49 | char mtime[12]; /* 136-147 */ |
| 50 | char chksum[8]; /* 148-155 */ |
| 51 | char typeflag; /* 156-156 */ |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 52 | char linkname[100]; /* 157-256 */ |
Glenn L McGrath | 99b1254 | 2002-08-22 17:47:09 +0000 | [diff] [blame] | 53 | char magic[6]; /* 257-262 */ |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 54 | char version[2]; /* 263-264 */ |
Glenn L McGrath | 99b1254 | 2002-08-22 17:47:09 +0000 | [diff] [blame] | 55 | char uname[32]; /* 265-296 */ |
| 56 | char gname[32]; /* 297-328 */ |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 57 | char devmajor[8]; /* 329-336 */ |
| 58 | char devminor[8]; /* 337-344 */ |
| 59 | char prefix[155]; /* 345-499 */ |
| 60 | char padding[12]; /* 500-512 */ |
| 61 | } formated; |
| 62 | } tar; |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 63 | long sum = 0; |
| 64 | long i; |
| 65 | |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 66 | /* Align header */ |
Glenn L McGrath | 237ae42 | 2002-11-03 14:05:15 +0000 | [diff] [blame] | 67 | data_align(archive_handle, 512); |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 68 | |
Glenn L McGrath | 7ffe133 | 2003-11-21 22:24:57 +0000 | [diff] [blame] | 69 | if (bb_full_read(archive_handle->src_fd, tar.raw, 512) != 512) { |
Glenn L McGrath | 7f2a953 | 2002-11-05 02:56:57 +0000 | [diff] [blame] | 70 | /* Assume end of file */ |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 71 | return(EXIT_FAILURE); |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 72 | } |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 73 | archive_handle->offset += 512; |
| 74 | |
| 75 | /* If there is no filename its an empty header */ |
| 76 | if (tar.formated.name[0] == 0) { |
Glenn L McGrath | e57feeb | 2003-11-14 12:57:14 +0000 | [diff] [blame] | 77 | return(EXIT_SUCCESS); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 78 | } |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 79 | |
Glenn L McGrath | 1d23f3a | 2002-08-13 05:06:43 +0000 | [diff] [blame] | 80 | /* Check header has valid magic, "ustar" is for the proper tar |
| 81 | * 0's are for the old tar format |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 82 | */ |
| 83 | if (strncmp(tar.formated.magic, "ustar", 5) != 0) { |
Glenn L McGrath | 21110a0 | 2003-01-28 01:45:48 +0000 | [diff] [blame] | 84 | #ifdef CONFIG_FEATURE_TAR_OLDGNU_COMPATABILITY |
Glenn L McGrath | 1d23f3a | 2002-08-13 05:06:43 +0000 | [diff] [blame] | 85 | if (strncmp(tar.formated.magic, "\0\0\0\0\0", 5) != 0) |
| 86 | #endif |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 87 | bb_error_msg_and_die("Invalid tar magic"); |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 88 | } |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 89 | /* Do checksum on headers */ |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 90 | for (i = 0; i < 148 ; i++) { |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 91 | sum += tar.raw[i]; |
| 92 | } |
| 93 | sum += ' ' * 8; |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 94 | for (i = 156; i < 512 ; i++) { |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 95 | sum += tar.raw[i]; |
| 96 | } |
| 97 | if (sum != strtol(tar.formated.chksum, NULL, 8)) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 98 | bb_error_msg("Invalid tar header checksum"); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 99 | return(EXIT_FAILURE); |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 100 | } |
| 101 | |
Glenn L McGrath | 21110a0 | 2003-01-28 01:45:48 +0000 | [diff] [blame] | 102 | #ifdef CONFIG_FEATURE_TAR_GNU_EXTENSIONS |
Glenn L McGrath | b7a76df | 2002-11-23 10:44:47 +0000 | [diff] [blame] | 103 | if (longname) { |
| 104 | file_header->name = longname; |
| 105 | longname = NULL; |
| 106 | } |
| 107 | else if (linkname) { |
| 108 | file_header->name = linkname; |
| 109 | linkname = NULL; |
| 110 | } else |
| 111 | #endif |
Glenn L McGrath | 7576270 | 2002-08-22 11:50:31 +0000 | [diff] [blame] | 112 | if (tar.formated.prefix[0] == 0) { |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 113 | file_header->name = strdup(tar.formated.name); |
Glenn L McGrath | 7576270 | 2002-08-22 11:50:31 +0000 | [diff] [blame] | 114 | } else { |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 115 | 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] | 116 | } |
Glenn L McGrath | c5c1a8a | 2002-10-19 06:19:22 +0000 | [diff] [blame] | 117 | |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 118 | file_header->mode = strtol(tar.formated.mode, NULL, 8); |
| 119 | file_header->uid = strtol(tar.formated.uid, NULL, 8); |
| 120 | file_header->gid = strtol(tar.formated.gid, NULL, 8); |
| 121 | file_header->size = strtol(tar.formated.size, NULL, 8); |
| 122 | file_header->mtime = strtol(tar.formated.mtime, NULL, 8); |
| 123 | file_header->link_name = (tar.formated.linkname[0] != '\0') ? |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 124 | bb_xstrdup(tar.formated.linkname) : NULL; |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 125 | 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] | 126 | strtol(tar.formated.devminor, NULL, 8)); |
| 127 | |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 128 | /* Fix mode, used by the old format */ |
Glenn L McGrath | 1d23f3a | 2002-08-13 05:06:43 +0000 | [diff] [blame] | 129 | switch (tar.formated.typeflag) { |
Glenn L McGrath | b0e163a | 2004-02-19 08:48:30 +0000 | [diff] [blame^] | 130 | /* busybox identifies hard links as being regular files with 0 size and a link name */ |
| 131 | case '1': |
| 132 | file_header->mode &= (S_IFREG | 07777); |
| 133 | break; |
| 134 | case 'x': |
| 135 | case 'g': |
| 136 | bb_error_msg_and_die("pax is not tar"); |
| 137 | break; |
Glenn L McGrath | 6aa5223 | 2004-02-17 11:55:06 +0000 | [diff] [blame] | 138 | #ifdef CONFIG_FEATURE_TAR_OLDGNU_COMPATABILITY |
Glenn L McGrath | 99b1254 | 2002-08-22 17:47:09 +0000 | [diff] [blame] | 139 | case 0: |
Glenn L McGrath | 21110a0 | 2003-01-28 01:45:48 +0000 | [diff] [blame] | 140 | case '0': |
Glenn L McGrath | 87af49f | 2003-09-09 17:41:03 +0000 | [diff] [blame] | 141 | if (last_char_is(file_header->name, '/')) { |
| 142 | file_header->mode |= S_IFDIR; |
| 143 | } else { |
| 144 | file_header->mode |= S_IFREG; |
| 145 | } |
Glenn L McGrath | 99b1254 | 2002-08-22 17:47:09 +0000 | [diff] [blame] | 146 | break; |
Glenn L McGrath | 21110a0 | 2003-01-28 01:45:48 +0000 | [diff] [blame] | 147 | case '2': |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 148 | file_header->mode |= S_IFLNK; |
Glenn L McGrath | 99b1254 | 2002-08-22 17:47:09 +0000 | [diff] [blame] | 149 | break; |
Glenn L McGrath | 21110a0 | 2003-01-28 01:45:48 +0000 | [diff] [blame] | 150 | case '3': |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 151 | file_header->mode |= S_IFCHR; |
Glenn L McGrath | 99b1254 | 2002-08-22 17:47:09 +0000 | [diff] [blame] | 152 | break; |
Glenn L McGrath | 21110a0 | 2003-01-28 01:45:48 +0000 | [diff] [blame] | 153 | case '4': |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 154 | file_header->mode |= S_IFBLK; |
Glenn L McGrath | 99b1254 | 2002-08-22 17:47:09 +0000 | [diff] [blame] | 155 | break; |
Glenn L McGrath | 21110a0 | 2003-01-28 01:45:48 +0000 | [diff] [blame] | 156 | case '5': |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 157 | file_header->mode |= S_IFDIR; |
Glenn L McGrath | 99b1254 | 2002-08-22 17:47:09 +0000 | [diff] [blame] | 158 | break; |
Glenn L McGrath | 21110a0 | 2003-01-28 01:45:48 +0000 | [diff] [blame] | 159 | case '6': |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 160 | file_header->mode |= S_IFIFO; |
Glenn L McGrath | 99b1254 | 2002-08-22 17:47:09 +0000 | [diff] [blame] | 161 | break; |
Glenn L McGrath | b0e163a | 2004-02-19 08:48:30 +0000 | [diff] [blame^] | 162 | case '7': |
| 163 | /* Reserved for high performance files, treat as normal file */ |
| 164 | file_header->mode |= S_IFREG; |
Glenn L McGrath | ef91bf6 | 2003-09-12 06:31:28 +0000 | [diff] [blame] | 165 | break; |
Glenn L McGrath | b0e163a | 2004-02-19 08:48:30 +0000 | [diff] [blame^] | 166 | #endif |
Glenn L McGrath | 6aa5223 | 2004-02-17 11:55:06 +0000 | [diff] [blame] | 167 | #ifdef CONFIG_FEATURE_TAR_GNU_EXTENSIONS |
Glenn L McGrath | b3c4e9a | 2002-09-15 16:54:49 +0000 | [diff] [blame] | 168 | case 'L': { |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 169 | longname = xmalloc(file_header->size + 1); |
Glenn L McGrath | 237ae42 | 2002-11-03 14:05:15 +0000 | [diff] [blame] | 170 | archive_xread_all(archive_handle, longname, file_header->size); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 171 | longname[file_header->size] = '\0'; |
| 172 | archive_handle->offset += file_header->size; |
Glenn L McGrath | b3c4e9a | 2002-09-15 16:54:49 +0000 | [diff] [blame] | 173 | |
Glenn L McGrath | b7a76df | 2002-11-23 10:44:47 +0000 | [diff] [blame] | 174 | return(get_header_tar(archive_handle)); |
Glenn L McGrath | b3c4e9a | 2002-09-15 16:54:49 +0000 | [diff] [blame] | 175 | } |
| 176 | case 'K': { |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 177 | linkname = xmalloc(file_header->size + 1); |
Glenn L McGrath | 237ae42 | 2002-11-03 14:05:15 +0000 | [diff] [blame] | 178 | archive_xread_all(archive_handle, linkname, file_header->size); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 179 | linkname[file_header->size] = '\0'; |
| 180 | archive_handle->offset += file_header->size; |
Glenn L McGrath | b3c4e9a | 2002-09-15 16:54:49 +0000 | [diff] [blame] | 181 | |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 182 | file_header->name = linkname; |
Glenn L McGrath | b7a76df | 2002-11-23 10:44:47 +0000 | [diff] [blame] | 183 | return(get_header_tar(archive_handle)); |
Glenn L McGrath | b3c4e9a | 2002-09-15 16:54:49 +0000 | [diff] [blame] | 184 | } |
Glenn L McGrath | b0e163a | 2004-02-19 08:48:30 +0000 | [diff] [blame^] | 185 | case 'D': /* GNU dump dir */ |
| 186 | case 'M': /* Continuation of multi volume archive*/ |
| 187 | case 'N': /* Old GNU for names > 100 characters */ |
| 188 | case 'S': /* Sparse file */ |
| 189 | case 'V': /* Volume header */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 190 | bb_error_msg("Ignoring GNU extension type %c", tar.formated.typeflag); |
Glenn L McGrath | 6aa5223 | 2004-02-17 11:55:06 +0000 | [diff] [blame] | 191 | #endif |
Glenn L McGrath | b0e163a | 2004-02-19 08:48:30 +0000 | [diff] [blame^] | 192 | default: |
| 193 | bb_error_msg("Unknown typeflag: 0x%x", tar.formated.typeflag); |
Glenn L McGrath | 6aa5223 | 2004-02-17 11:55:06 +0000 | [diff] [blame] | 194 | } |
| 195 | { /* Strip trailing '/' in directories */ |
| 196 | /* Must be done after mode is set as '/' is used to check if its a directory */ |
| 197 | char *tmp = last_char_is(file_header->name, '/'); |
| 198 | if (tmp) { |
| 199 | *tmp = '\0'; |
| 200 | } |
Glenn L McGrath | 1d23f3a | 2002-08-13 05:06:43 +0000 | [diff] [blame] | 201 | } |
Glenn L McGrath | 3d5828f | 2003-08-14 02:55:15 +0000 | [diff] [blame] | 202 | |
Glenn L McGrath | 8e94098 | 2002-11-04 23:47:31 +0000 | [diff] [blame] | 203 | if (archive_handle->filter(archive_handle) == EXIT_SUCCESS) { |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 204 | archive_handle->action_header(archive_handle->file_header); |
| 205 | archive_handle->flags |= ARCHIVE_EXTRACT_QUIET; |
| 206 | archive_handle->action_data(archive_handle); |
Glenn L McGrath | f074afc | 2003-11-17 21:58:00 +0000 | [diff] [blame] | 207 | archive_handle->passed = llist_add_to(archive_handle->passed, file_header->name); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 208 | } else { |
| 209 | data_skip(archive_handle); |
| 210 | } |
| 211 | archive_handle->offset += file_header->size; |
| 212 | |
Glenn L McGrath | 54ac057 | 2003-11-15 00:24:43 +0000 | [diff] [blame] | 213 | free(file_header->link_name); |
| 214 | |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 215 | return(EXIT_SUCCESS); |
Eric Andersen | 2276d83 | 2002-07-11 11:11:56 +0000 | [diff] [blame] | 216 | } |