blob: d55189f42fc47f264d9efedf3f7ecc1a97d749e0 [file] [log] [blame]
Glenn L McGrath95ebf612001-10-25 14:18:08 +00001/*
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 McGrath21110a02003-01-28 01:45:48 +000023#ifdef CONFIG_FEATURE_TAR_GNU_EXTENSIONS
Glenn L McGrathb7a76df2002-11-23 10:44:47 +000024static char *longname = NULL;
25static char *linkname = NULL;
26#endif
27
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000028extern char get_header_tar(archive_handle_t *archive_handle)
Glenn L McGrath95ebf612001-10-25 14:18:08 +000029{
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000030 file_header_t *file_header = archive_handle->file_header;
Glenn L McGrath95ebf612001-10-25 14:18:08 +000031 union {
Glenn L McGrathef91bf62003-09-12 06:31:28 +000032 /* ustar header, Posix 1003.1 */
Glenn L McGrath95ebf612001-10-25 14:18:08 +000033 unsigned char raw[512];
34 struct {
Glenn L McGrath99b12542002-08-22 17:47:09 +000035 char name[100]; /* 0-99 */
36 char mode[8]; /* 100-107 */
37 char uid[8]; /* 108-115 */
38 char gid[8]; /* 116-123 */
39 char size[12]; /* 124-135 */
40 char mtime[12]; /* 136-147 */
41 char chksum[8]; /* 148-155 */
42 char typeflag; /* 156-156 */
Glenn L McGrath95ebf612001-10-25 14:18:08 +000043 char linkname[100]; /* 157-256 */
Glenn L McGrath99b12542002-08-22 17:47:09 +000044 char magic[6]; /* 257-262 */
Glenn L McGrath95ebf612001-10-25 14:18:08 +000045 char version[2]; /* 263-264 */
Glenn L McGrath99b12542002-08-22 17:47:09 +000046 char uname[32]; /* 265-296 */
47 char gname[32]; /* 297-328 */
Glenn L McGrath95ebf612001-10-25 14:18:08 +000048 char devmajor[8]; /* 329-336 */
49 char devminor[8]; /* 337-344 */
50 char prefix[155]; /* 345-499 */
51 char padding[12]; /* 500-512 */
52 } formated;
53 } tar;
Glenn L McGrath95ebf612001-10-25 14:18:08 +000054 long sum = 0;
55 long i;
56
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000057 /* Align header */
Glenn L McGrath237ae422002-11-03 14:05:15 +000058 data_align(archive_handle, 512);
Glenn L McGrath95ebf612001-10-25 14:18:08 +000059
Glenn L McGrath7f2a9532002-11-05 02:56:57 +000060 if (archive_xread(archive_handle, tar.raw, 512) != 512) {
61 /* Assume end of file */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000062 return(EXIT_FAILURE);
Glenn L McGrath95ebf612001-10-25 14:18:08 +000063 }
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000064 archive_handle->offset += 512;
65
66 /* If there is no filename its an empty header */
67 if (tar.formated.name[0] == 0) {
Glenn L McGrathe57feeb2003-11-14 12:57:14 +000068 return(EXIT_SUCCESS);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000069 }
Glenn L McGrath95ebf612001-10-25 14:18:08 +000070
Glenn L McGrath1d23f3a2002-08-13 05:06:43 +000071 /* Check header has valid magic, "ustar" is for the proper tar
72 * 0's are for the old tar format
Glenn L McGrath95ebf612001-10-25 14:18:08 +000073 */
74 if (strncmp(tar.formated.magic, "ustar", 5) != 0) {
Glenn L McGrath21110a02003-01-28 01:45:48 +000075#ifdef CONFIG_FEATURE_TAR_OLDGNU_COMPATABILITY
Glenn L McGrath1d23f3a2002-08-13 05:06:43 +000076 if (strncmp(tar.formated.magic, "\0\0\0\0\0", 5) != 0)
77#endif
Manuel Novoa III cad53642003-03-19 09:13:01 +000078 bb_error_msg_and_die("Invalid tar magic");
Glenn L McGrath95ebf612001-10-25 14:18:08 +000079 }
Glenn L McGrath95ebf612001-10-25 14:18:08 +000080 /* Do checksum on headers */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000081 for (i = 0; i < 148 ; i++) {
Glenn L McGrath95ebf612001-10-25 14:18:08 +000082 sum += tar.raw[i];
83 }
84 sum += ' ' * 8;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000085 for (i = 156; i < 512 ; i++) {
Glenn L McGrath95ebf612001-10-25 14:18:08 +000086 sum += tar.raw[i];
87 }
88 if (sum != strtol(tar.formated.chksum, NULL, 8)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000089 bb_error_msg("Invalid tar header checksum");
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000090 return(EXIT_FAILURE);
Glenn L McGrath95ebf612001-10-25 14:18:08 +000091 }
92
Glenn L McGrath21110a02003-01-28 01:45:48 +000093#ifdef CONFIG_FEATURE_TAR_GNU_EXTENSIONS
Glenn L McGrathb7a76df2002-11-23 10:44:47 +000094 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 McGrath75762702002-08-22 11:50:31 +0000103 if (tar.formated.prefix[0] == 0) {
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000104 file_header->name = strdup(tar.formated.name);
Glenn L McGrath75762702002-08-22 11:50:31 +0000105 } else {
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000106 file_header->name = concat_path_file(tar.formated.prefix, tar.formated.name);
Glenn L McGrath75762702002-08-22 11:50:31 +0000107 }
Glenn L McGrathc5c1a8a2002-10-19 06:19:22 +0000108
Glenn L McGrathe8571222003-11-20 10:47:06 +0000109 { /* Strip trailing '/' in directories */
110 char *tmp = last_char_is(file_header->name, '/');
111 if (tmp) {
112 *tmp = '\0';
113 }
114 }
115
116
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000117 file_header->mode = strtol(tar.formated.mode, NULL, 8);
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') ?
Manuel Novoa III cad53642003-03-19 09:13:01 +0000123 bb_xstrdup(tar.formated.linkname) : NULL;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000124 file_header->device = (dev_t) ((strtol(tar.formated.devmajor, NULL, 8) << 8) +
Glenn L McGrathb3c4e9a2002-09-15 16:54:49 +0000125 strtol(tar.formated.devminor, NULL, 8));
126
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000127 /* Fix mode, used by the old format */
Glenn L McGrath1d23f3a2002-08-13 05:06:43 +0000128 switch (tar.formated.typeflag) {
Glenn L McGrath21110a02003-01-28 01:45:48 +0000129# ifdef CONFIG_FEATURE_TAR_OLDGNU_COMPATABILITY
Glenn L McGrath99b12542002-08-22 17:47:09 +0000130 case 0:
Glenn L McGrath21110a02003-01-28 01:45:48 +0000131 case '0':
Glenn L McGrath87af49f2003-09-09 17:41:03 +0000132 if (last_char_is(file_header->name, '/')) {
133 file_header->mode |= S_IFDIR;
134 } else {
135 file_header->mode |= S_IFREG;
136 }
Glenn L McGrath99b12542002-08-22 17:47:09 +0000137 break;
Glenn L McGrath21110a02003-01-28 01:45:48 +0000138 case '2':
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000139 file_header->mode |= S_IFLNK;
Glenn L McGrath99b12542002-08-22 17:47:09 +0000140 break;
Glenn L McGrath21110a02003-01-28 01:45:48 +0000141 case '3':
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000142 file_header->mode |= S_IFCHR;
Glenn L McGrath99b12542002-08-22 17:47:09 +0000143 break;
Glenn L McGrath21110a02003-01-28 01:45:48 +0000144 case '4':
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000145 file_header->mode |= S_IFBLK;
Glenn L McGrath99b12542002-08-22 17:47:09 +0000146 break;
Glenn L McGrath21110a02003-01-28 01:45:48 +0000147 case '5':
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000148 file_header->mode |= S_IFDIR;
Glenn L McGrath99b12542002-08-22 17:47:09 +0000149 break;
Glenn L McGrath21110a02003-01-28 01:45:48 +0000150 case '6':
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000151 file_header->mode |= S_IFIFO;
Glenn L McGrath99b12542002-08-22 17:47:09 +0000152 break;
Glenn L McGrathb3c4e9a2002-09-15 16:54:49 +0000153# endif
Glenn L McGrathef91bf62003-09-12 06:31:28 +0000154 /* hard links are detected as entries with 0 size, a link name,
155 * and not being a symlink, hence we have nothing to do here */
156 case '1':
157 file_header->mode |= ~S_IFLNK;
158 break;
Glenn L McGrath21110a02003-01-28 01:45:48 +0000159# ifdef CONFIG_FEATURE_TAR_GNU_EXTENSIONS
Glenn L McGrathb3c4e9a2002-09-15 16:54:49 +0000160 case 'L': {
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000161 longname = xmalloc(file_header->size + 1);
Glenn L McGrath237ae422002-11-03 14:05:15 +0000162 archive_xread_all(archive_handle, longname, file_header->size);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000163 longname[file_header->size] = '\0';
164 archive_handle->offset += file_header->size;
Glenn L McGrathb3c4e9a2002-09-15 16:54:49 +0000165
Glenn L McGrathb7a76df2002-11-23 10:44:47 +0000166 return(get_header_tar(archive_handle));
Glenn L McGrathb3c4e9a2002-09-15 16:54:49 +0000167 }
168 case 'K': {
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000169 linkname = xmalloc(file_header->size + 1);
Glenn L McGrath237ae422002-11-03 14:05:15 +0000170 archive_xread_all(archive_handle, linkname, file_header->size);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000171 linkname[file_header->size] = '\0';
172 archive_handle->offset += file_header->size;
Glenn L McGrathb3c4e9a2002-09-15 16:54:49 +0000173
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000174 file_header->name = linkname;
Glenn L McGrathb7a76df2002-11-23 10:44:47 +0000175 return(get_header_tar(archive_handle));
Glenn L McGrathb3c4e9a2002-09-15 16:54:49 +0000176 }
Glenn L McGrath21110a02003-01-28 01:45:48 +0000177 case 'D':
178 case 'M':
179 case 'N':
180 case 'S':
181 case 'V':
Manuel Novoa III cad53642003-03-19 09:13:01 +0000182 bb_error_msg("Ignoring GNU extension type %c", tar.formated.typeflag);
Glenn L McGrathb3c4e9a2002-09-15 16:54:49 +0000183# endif
Glenn L McGrath1d23f3a2002-08-13 05:06:43 +0000184 }
Glenn L McGrath3d5828f2003-08-14 02:55:15 +0000185
Glenn L McGrath8e940982002-11-04 23:47:31 +0000186 if (archive_handle->filter(archive_handle) == EXIT_SUCCESS) {
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000187 archive_handle->action_header(archive_handle->file_header);
188 archive_handle->flags |= ARCHIVE_EXTRACT_QUIET;
189 archive_handle->action_data(archive_handle);
Glenn L McGrathf074afc2003-11-17 21:58:00 +0000190 archive_handle->passed = llist_add_to(archive_handle->passed, file_header->name);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000191 } else {
192 data_skip(archive_handle);
193 }
194 archive_handle->offset += file_header->size;
195
Glenn L McGrath54ac0572003-11-15 00:24:43 +0000196 free(file_header->link_name);
197
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000198 return(EXIT_SUCCESS);
Eric Andersen2276d832002-07-11 11:11:56 +0000199}