blob: d3ff1608a25c1ecf82e4eb36a04033c5f1fb8e7a [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 McGrath7ca04f32002-09-25 02:47:48 +000023extern char get_header_tar(archive_handle_t *archive_handle)
Glenn L McGrath95ebf612001-10-25 14:18:08 +000024{
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000025 file_header_t *file_header = archive_handle->file_header;
Glenn L McGrath95ebf612001-10-25 14:18:08 +000026 union {
27 unsigned char raw[512];
28 struct {
Glenn L McGrath99b12542002-08-22 17:47:09 +000029 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 McGrath95ebf612001-10-25 14:18:08 +000037 char linkname[100]; /* 157-256 */
Glenn L McGrath99b12542002-08-22 17:47:09 +000038 char magic[6]; /* 257-262 */
Glenn L McGrath95ebf612001-10-25 14:18:08 +000039 char version[2]; /* 263-264 */
Glenn L McGrath99b12542002-08-22 17:47:09 +000040 char uname[32]; /* 265-296 */
41 char gname[32]; /* 297-328 */
Glenn L McGrath95ebf612001-10-25 14:18:08 +000042 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 McGrath95ebf612001-10-25 14:18:08 +000048 long sum = 0;
49 long i;
Glenn L McGrathc5c1a8a2002-10-19 06:19:22 +000050 char *tmp;
Glenn L McGrath95ebf612001-10-25 14:18:08 +000051
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000052 /* Align header */
Glenn L McGrath237ae422002-11-03 14:05:15 +000053 data_align(archive_handle, 512);
Glenn L McGrath95ebf612001-10-25 14:18:08 +000054
Glenn L McGrath7f2a9532002-11-05 02:56:57 +000055 if (archive_xread(archive_handle, tar.raw, 512) != 512) {
56 /* Assume end of file */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000057 return(EXIT_FAILURE);
Glenn L McGrath95ebf612001-10-25 14:18:08 +000058 }
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000059 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 McGrath95ebf612001-10-25 14:18:08 +000065
Glenn L McGrath1d23f3a2002-08-13 05:06:43 +000066 /* Check header has valid magic, "ustar" is for the proper tar
67 * 0's are for the old tar format
Glenn L McGrath95ebf612001-10-25 14:18:08 +000068 */
69 if (strncmp(tar.formated.magic, "ustar", 5) != 0) {
Glenn L McGrath1d23f3a2002-08-13 05:06:43 +000070#ifdef CONFIG_FEATURE_TAR_OLD_FORMAT
71 if (strncmp(tar.formated.magic, "\0\0\0\0\0", 5) != 0)
72#endif
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000073 error_msg_and_die("Invalid tar magic");
Glenn L McGrath95ebf612001-10-25 14:18:08 +000074 }
Glenn L McGrath95ebf612001-10-25 14:18:08 +000075 /* Do checksum on headers */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000076 for (i = 0; i < 148 ; i++) {
Glenn L McGrath95ebf612001-10-25 14:18:08 +000077 sum += tar.raw[i];
78 }
79 sum += ' ' * 8;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000080 for (i = 156; i < 512 ; i++) {
Glenn L McGrath95ebf612001-10-25 14:18:08 +000081 sum += tar.raw[i];
82 }
83 if (sum != strtol(tar.formated.chksum, NULL, 8)) {
84 error_msg("Invalid tar header checksum");
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000085 return(EXIT_FAILURE);
Glenn L McGrath95ebf612001-10-25 14:18:08 +000086 }
87
88 /* convert to type'ed variables */
Glenn L McGrath75762702002-08-22 11:50:31 +000089 if (tar.formated.prefix[0] == 0) {
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000090 file_header->name = strdup(tar.formated.name);
Glenn L McGrath75762702002-08-22 11:50:31 +000091 } else {
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000092 file_header->name = concat_path_file(tar.formated.prefix, tar.formated.name);
Glenn L McGrath75762702002-08-22 11:50:31 +000093 }
Glenn L McGrathc5c1a8a2002-10-19 06:19:22 +000094 tmp = last_char_is(archive_handle->file_header->name, '/');
95 if (tmp) {
96 *tmp = '\0';
97 }
98
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000099 file_header->mode = strtol(tar.formated.mode, NULL, 8);
100 file_header->uid = strtol(tar.formated.uid, NULL, 8);
101 file_header->gid = strtol(tar.formated.gid, NULL, 8);
102 file_header->size = strtol(tar.formated.size, NULL, 8);
103 file_header->mtime = strtol(tar.formated.mtime, NULL, 8);
104 file_header->link_name = (tar.formated.linkname[0] != '\0') ?
105 xstrdup(tar.formated.linkname) : NULL;
106 file_header->device = (dev_t) ((strtol(tar.formated.devmajor, NULL, 8) << 8) +
Glenn L McGrathb3c4e9a2002-09-15 16:54:49 +0000107 strtol(tar.formated.devminor, NULL, 8));
108
109#if defined CONFIG_FEATURE_TAR_OLD_FORMAT || defined CONFIG_FEATURE_GNUTAR_LONG_FILENAME
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000110 /* Fix mode, used by the old format */
Glenn L McGrath1d23f3a2002-08-13 05:06:43 +0000111 switch (tar.formated.typeflag) {
Glenn L McGrathb3c4e9a2002-09-15 16:54:49 +0000112# ifdef CONFIG_FEATURE_TAR_OLD_FORMAT
Glenn L McGrath99b12542002-08-22 17:47:09 +0000113 case 0:
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000114 file_header->mode |= S_IFREG;
Glenn L McGrath99b12542002-08-22 17:47:09 +0000115 break;
116 case 1:
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000117 error_msg("Internal hard link not supported");
Glenn L McGrath99b12542002-08-22 17:47:09 +0000118 break;
119 case 2:
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000120 file_header->mode |= S_IFLNK;
Glenn L McGrath99b12542002-08-22 17:47:09 +0000121 break;
122 case 3:
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000123 file_header->mode |= S_IFCHR;
Glenn L McGrath99b12542002-08-22 17:47:09 +0000124 break;
125 case 4:
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000126 file_header->mode |= S_IFBLK;
Glenn L McGrath99b12542002-08-22 17:47:09 +0000127 break;
128 case 5:
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000129 file_header->mode |= S_IFDIR;
Glenn L McGrath99b12542002-08-22 17:47:09 +0000130 break;
131 case 6:
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000132 file_header->mode |= S_IFIFO;
Glenn L McGrath99b12542002-08-22 17:47:09 +0000133 break;
Glenn L McGrathb3c4e9a2002-09-15 16:54:49 +0000134# endif
135# ifdef CONFIG_FEATURE_GNUTAR_LONG_FILENAME
136 case 'L': {
137 char *longname;
138
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000139 longname = xmalloc(file_header->size + 1);
Glenn L McGrath237ae422002-11-03 14:05:15 +0000140 archive_xread_all(archive_handle, longname, file_header->size);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000141 longname[file_header->size] = '\0';
142 archive_handle->offset += file_header->size;
Glenn L McGrathb3c4e9a2002-09-15 16:54:49 +0000143
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000144 get_header_tar(archive_handle);
145 file_header->name = longname;
Glenn L McGrathb3c4e9a2002-09-15 16:54:49 +0000146 break;
147 }
148 case 'K': {
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000149 char *linkname;
Glenn L McGrathb3c4e9a2002-09-15 16:54:49 +0000150
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000151 linkname = xmalloc(file_header->size + 1);
Glenn L McGrath237ae422002-11-03 14:05:15 +0000152 archive_xread_all(archive_handle, linkname, file_header->size);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000153 linkname[file_header->size] = '\0';
154 archive_handle->offset += file_header->size;
Glenn L McGrathb3c4e9a2002-09-15 16:54:49 +0000155
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000156 get_header_tar(archive_handle);
157 file_header->name = linkname;
Glenn L McGrathb3c4e9a2002-09-15 16:54:49 +0000158 break;
159 }
160# endif
Glenn L McGrath1d23f3a2002-08-13 05:06:43 +0000161 }
162#endif
Glenn L McGrath8e940982002-11-04 23:47:31 +0000163 if (archive_handle->filter(archive_handle) == EXIT_SUCCESS) {
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000164 archive_handle->action_header(archive_handle->file_header);
165 archive_handle->flags |= ARCHIVE_EXTRACT_QUIET;
166 archive_handle->action_data(archive_handle);
Glenn L McGrathc5c1a8a2002-10-19 06:19:22 +0000167 archive_handle->passed = add_to_list(archive_handle->passed, archive_handle->file_header->name);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000168 } else {
169 data_skip(archive_handle);
170 }
171 archive_handle->offset += file_header->size;
172
173 return(EXIT_SUCCESS);
Eric Andersen2276d832002-07-11 11:11:56 +0000174}
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000175