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.
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.
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.
17 * In privelidged mode if uname and gname map to a uid amd gid then use the
18 * mapped value instead of the uid/gid values in tar header
21 * GNU tar and star man pages,
22 * Opengroup's ustar interchange format,
23 * http://www.opengroup.org/onlinepubs/007904975/utilities/pax.html
29 #include "unarchive.h"
32 #ifdef CONFIG_FEATURE_TAR_GNU_EXTENSIONS
33 static char *longname = NULL;
34 static char *linkname = NULL;
37 extern char get_header_tar(archive_handle_t *archive_handle)
39 file_header_t *file_header = archive_handle->file_header;
41 /* ustar header, Posix 1003.1 */
42 unsigned char raw[512];
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 */
52 char linkname[100]; /* 157-256 */
53 char magic[6]; /* 257-262 */
54 char version[2]; /* 263-264 */
55 char uname[32]; /* 265-296 */
56 char gname[32]; /* 297-328 */
57 char devmajor[8]; /* 329-336 */
58 char devminor[8]; /* 337-344 */
59 char prefix[155]; /* 345-499 */
60 char padding[12]; /* 500-512 */
67 data_align(archive_handle, 512);
69 if (bb_full_read(archive_handle->src_fd, tar.raw, 512) != 512) {
70 /* Assume end of file */
73 archive_handle->offset += 512;
75 /* If there is no filename its an empty header */
76 if (tar.formated.name[0] == 0) {
80 /* Check header has valid magic, "ustar" is for the proper tar
81 * 0's are for the old tar format
83 if (strncmp(tar.formated.magic, "ustar", 5) != 0) {
84 #ifdef CONFIG_FEATURE_TAR_OLDGNU_COMPATABILITY
85 if (strncmp(tar.formated.magic, "\0\0\0\0\0", 5) != 0)
87 bb_error_msg_and_die("Invalid tar magic");
89 /* Do checksum on headers */
90 for (i = 0; i < 148 ; i++) {
94 for (i = 156; i < 512 ; i++) {
97 if (sum != strtol(tar.formated.chksum, NULL, 8)) {
98 bb_error_msg("Invalid tar header checksum");
102 #ifdef CONFIG_FEATURE_TAR_GNU_EXTENSIONS
104 file_header->name = longname;
108 file_header->name = linkname;
112 if (tar.formated.prefix[0] == 0) {
113 file_header->name = strdup(tar.formated.name);
115 file_header->name = concat_path_file(tar.formated.prefix, tar.formated.name);
118 file_header->uid = strtol(tar.formated.uid, NULL, 8);
119 file_header->gid = strtol(tar.formated.gid, NULL, 8);
120 file_header->size = strtol(tar.formated.size, NULL, 8);
121 file_header->mtime = strtol(tar.formated.mtime, NULL, 8);
122 file_header->link_name = (tar.formated.linkname[0] != '\0') ?
123 bb_xstrdup(tar.formated.linkname) : NULL;
124 file_header->device = (dev_t) ((strtol(tar.formated.devmajor, NULL, 8) << 8) +
125 strtol(tar.formated.devminor, NULL, 8));
127 /* Set bits 0-11 of the files mode */
128 file_header->mode = 07777 & strtol(tar.formated.mode, NULL, 8);
130 /* Set bits 12-15 of the files mode */
131 switch (tar.formated.typeflag) {
132 /* busybox identifies hard links as being regular files with 0 size and a link name */
134 file_header->mode |= S_IFREG;
138 bb_error_msg_and_die("pax is not tar");
141 /* Reserved for high performance files, treat as normal file */
144 #ifdef CONFIG_FEATURE_TAR_OLDGNU_COMPATABILITY
145 if (last_char_is(file_header->name, '/')) {
146 file_header->mode |= S_IFDIR;
149 file_header->mode |= S_IFREG;
152 file_header->mode |= S_IFLNK;
155 file_header->mode |= S_IFCHR;
158 file_header->mode |= S_IFBLK;
161 file_header->mode |= S_IFDIR;
164 file_header->mode |= S_IFIFO;
166 #ifdef CONFIG_FEATURE_TAR_GNU_EXTENSIONS
168 longname = xmalloc(file_header->size + 1);
169 archive_xread_all(archive_handle, longname, file_header->size);
170 longname[file_header->size] = '\0';
171 archive_handle->offset += file_header->size;
173 return(get_header_tar(archive_handle));
176 linkname = xmalloc(file_header->size + 1);
177 archive_xread_all(archive_handle, linkname, file_header->size);
178 linkname[file_header->size] = '\0';
179 archive_handle->offset += file_header->size;
181 file_header->name = linkname;
182 return(get_header_tar(archive_handle));
184 case 'D': /* GNU dump dir */
185 case 'M': /* Continuation of multi volume archive*/
186 case 'N': /* Old GNU for names > 100 characters */
187 case 'S': /* Sparse file */
188 case 'V': /* Volume header */
189 bb_error_msg("Ignoring GNU extension type %c", tar.formated.typeflag);
192 bb_error_msg("Unknown typeflag: 0x%x", tar.formated.typeflag);
194 { /* Strip trailing '/' in directories */
195 /* Must be done after mode is set as '/' is used to check if its a directory */
196 char *tmp = last_char_is(file_header->name, '/');
202 if (archive_handle->filter(archive_handle) == EXIT_SUCCESS) {
203 archive_handle->action_header(archive_handle->file_header);
204 archive_handle->flags |= ARCHIVE_EXTRACT_QUIET;
205 archive_handle->action_data(archive_handle);
206 archive_handle->passed = llist_add_to(archive_handle->passed, file_header->name);
208 data_skip(archive_handle);
210 archive_handle->offset += file_header->size;
212 free(file_header->link_name);
214 return(EXIT_SUCCESS);