blob: d3cd96d0cc0c96eb4d0f557d03712b8cd3e15dee [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Rob Landley06249fe2006-02-20 19:28:53 +00002/* Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Glenn L McGrath6aa52232004-02-17 11:55:06 +00003 *
Glenn L McGrathc9f1fce2004-02-20 02:25:18 +00004 * FIXME:
Eric Andersenaff114c2004-04-14 17:51:38 +00005 * In privileged mode if uname and gname map to a uid and gid then use the
Glenn L McGrathc9f1fce2004-02-20 02:25:18 +00006 * mapped value instead of the uid/gid values in tar header
Glenn L McGrathb0e163a2004-02-19 08:48:30 +00007 *
8 * References:
9 * GNU tar and star man pages,
10 * Opengroup's ustar interchange format,
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000011 * http://www.opengroup.org/onlinepubs/007904975/utilities/pax.html
Glenn L McGrath95ebf612001-10-25 14:18:08 +000012 */
13
Glenn L McGrath95ebf612001-10-25 14:18:08 +000014#include "libbb.h"
Rob Landleyd921b2e2006-08-03 15:41:12 +000015#include "unarchive.h"
Glenn L McGrath95ebf612001-10-25 14:18:08 +000016
Glenn L McGrath21110a02003-01-28 01:45:48 +000017#ifdef CONFIG_FEATURE_TAR_GNU_EXTENSIONS
Glenn L McGrathb7a76df2002-11-23 10:44:47 +000018static char *longname = NULL;
19static char *linkname = NULL;
20#endif
21
Rob Landleydfba7412006-03-06 20:47:33 +000022char get_header_tar(archive_handle_t *archive_handle)
Glenn L McGrath95ebf612001-10-25 14:18:08 +000023{
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000024 file_header_t *file_header = archive_handle->file_header;
Glenn L McGrath95ebf612001-10-25 14:18:08 +000025 union {
Glenn L McGrathef91bf62003-09-12 06:31:28 +000026 /* ustar header, Posix 1003.1 */
Glenn L McGrath95ebf612001-10-25 14:18:08 +000027 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 */
"Robert P. J. Day"eea56182006-07-20 19:02:24 +000046 } formatted;
Glenn L McGrath95ebf612001-10-25 14:18:08 +000047 } tar;
Glenn L McGrath95ebf612001-10-25 14:18:08 +000048 long sum = 0;
49 long i;
Paul Fox94ff9f12005-07-20 19:24:13 +000050 static int end = 0;
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
Rob Landley53437472006-07-16 08:14:35 +000055 xread(archive_handle->src_fd, tar.raw, 512);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000056 archive_handle->offset += 512;
57
58 /* If there is no filename its an empty header */
"Robert P. J. Day"eea56182006-07-20 19:02:24 +000059 if (tar.formatted.name[0] == 0) {
Paul Fox94ff9f12005-07-20 19:24:13 +000060 if (end) {
61 /* This is the second consecutive empty header! End of archive!
62 * Read until the end to empty the pipe from gz or bz2
63 */
Rob Landley53437472006-07-16 08:14:35 +000064 while (full_read(archive_handle->src_fd, tar.raw, 512) == 512);
Denis Vlasenko13858992006-10-08 12:49:22 +000065 return EXIT_FAILURE;
Paul Fox94ff9f12005-07-20 19:24:13 +000066 }
67 end = 1;
Denis Vlasenko13858992006-10-08 12:49:22 +000068 return EXIT_SUCCESS;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000069 }
Paul Fox94ff9f12005-07-20 19:24:13 +000070 end = 0;
Glenn L McGrath95ebf612001-10-25 14:18:08 +000071
Glenn L McGrath1d23f3a2002-08-13 05:06:43 +000072 /* Check header has valid magic, "ustar" is for the proper tar
73 * 0's are for the old tar format
Glenn L McGrath95ebf612001-10-25 14:18:08 +000074 */
"Robert P. J. Day"eea56182006-07-20 19:02:24 +000075 if (strncmp(tar.formatted.magic, "ustar", 5) != 0) {
Bernhard Reutner-Fischera1bccc02006-04-02 20:17:55 +000076#ifdef CONFIG_FEATURE_TAR_OLDGNU_COMPATIBILITY
"Robert P. J. Day"eea56182006-07-20 19:02:24 +000077 if (strncmp(tar.formatted.magic, "\0\0\0\0\0", 5) != 0)
Glenn L McGrath1d23f3a2002-08-13 05:06:43 +000078#endif
Denis Vlasenko13858992006-10-08 12:49:22 +000079 bb_error_msg_and_die("invalid tar magic");
Glenn L McGrath95ebf612001-10-25 14:18:08 +000080 }
Glenn L McGrath95ebf612001-10-25 14:18:08 +000081 /* Do checksum on headers */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000082 for (i = 0; i < 148 ; i++) {
Glenn L McGrath95ebf612001-10-25 14:18:08 +000083 sum += tar.raw[i];
84 }
85 sum += ' ' * 8;
Denis Vlasenko13858992006-10-08 12:49:22 +000086 for (i = 156; i < 512 ; i++) {
Glenn L McGrath95ebf612001-10-25 14:18:08 +000087 sum += tar.raw[i];
88 }
Denis Vlasenko13858992006-10-08 12:49:22 +000089 if (sum != xstrtoul(tar.formatted.chksum, 8)) {
90 bb_error_msg_and_die("invalid tar header checksum");
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
Rob Landley06249fe2006-02-20 19:28:53 +0000103 {
Denis Vlasenko13858992006-10-08 12:49:22 +0000104 file_header->name = xstrndup(tar.formatted.name, 100);
"Robert P. J. Day"eea56182006-07-20 19:02:24 +0000105 if (tar.formatted.prefix[0]) {
Rob Landley06249fe2006-02-20 19:28:53 +0000106 char *temp = file_header->name;
"Robert P. J. Day"eea56182006-07-20 19:02:24 +0000107 file_header->name = concat_path_file(tar.formatted.prefix, temp);
Rob Landley06249fe2006-02-20 19:28:53 +0000108 free(temp);
109 }
Glenn L McGrath75762702002-08-22 11:50:31 +0000110 }
Glenn L McGrathc5c1a8a2002-10-19 06:19:22 +0000111
Denis Vlasenko13858992006-10-08 12:49:22 +0000112 file_header->uid = xstrtoul(tar.formatted.uid, 8);
113 file_header->gid = xstrtoul(tar.formatted.gid, 8);
114 // TODO: LFS support
115 file_header->size = xstrtoul(tar.formatted.size, 8);
116 file_header->mtime = xstrtoul(tar.formatted.mtime, 8);
117 file_header->link_name = tar.formatted.linkname[0] ?
118 xstrdup(tar.formatted.linkname) : NULL;
119 file_header->device = makedev(xstrtoul(tar.formatted.devmajor, 8),
120 xstrtoul(tar.formatted.devminor, 8));
Glenn L McGrathb3c4e9a2002-09-15 16:54:49 +0000121
Glenn L McGrath916ba532004-02-20 02:34:42 +0000122 /* Set bits 0-11 of the files mode */
Denis Vlasenko13858992006-10-08 12:49:22 +0000123 file_header->mode = 07777 & xstrtoul(tar.formatted.mode, 8);
Glenn L McGrath916ba532004-02-20 02:34:42 +0000124
125 /* Set bits 12-15 of the files mode */
"Robert P. J. Day"eea56182006-07-20 19:02:24 +0000126 switch (tar.formatted.typeflag) {
Glenn L McGrathb0e163a2004-02-19 08:48:30 +0000127 /* busybox identifies hard links as being regular files with 0 size and a link name */
128 case '1':
Glenn L McGrath916ba532004-02-20 02:34:42 +0000129 file_header->mode |= S_IFREG;
Glenn L McGrathb0e163a2004-02-19 08:48:30 +0000130 break;
Glenn L McGrathc9f1fce2004-02-20 02:25:18 +0000131 case '7':
132 /* Reserved for high performance files, treat as normal file */
Glenn L McGrath99b12542002-08-22 17:47:09 +0000133 case 0:
Glenn L McGrath21110a02003-01-28 01:45:48 +0000134 case '0':
Bernhard Reutner-Fischera1bccc02006-04-02 20:17:55 +0000135#ifdef CONFIG_FEATURE_TAR_OLDGNU_COMPATIBILITY
Glenn L McGrath87af49f2003-09-09 17:41:03 +0000136 if (last_char_is(file_header->name, '/')) {
137 file_header->mode |= S_IFDIR;
Glenn L McGrathc9f1fce2004-02-20 02:25:18 +0000138 } else
139#endif
Glenn L McGrath87af49f2003-09-09 17:41:03 +0000140 file_header->mode |= S_IFREG;
Glenn L McGrath99b12542002-08-22 17:47:09 +0000141 break;
Glenn L McGrath21110a02003-01-28 01:45:48 +0000142 case '2':
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000143 file_header->mode |= S_IFLNK;
Glenn L McGrath99b12542002-08-22 17:47:09 +0000144 break;
Glenn L McGrath21110a02003-01-28 01:45:48 +0000145 case '3':
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000146 file_header->mode |= S_IFCHR;
Glenn L McGrath99b12542002-08-22 17:47:09 +0000147 break;
Glenn L McGrath21110a02003-01-28 01:45:48 +0000148 case '4':
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000149 file_header->mode |= S_IFBLK;
Glenn L McGrath99b12542002-08-22 17:47:09 +0000150 break;
Glenn L McGrath21110a02003-01-28 01:45:48 +0000151 case '5':
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000152 file_header->mode |= S_IFDIR;
Glenn L McGrath99b12542002-08-22 17:47:09 +0000153 break;
Glenn L McGrath21110a02003-01-28 01:45:48 +0000154 case '6':
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000155 file_header->mode |= S_IFIFO;
Glenn L McGrath99b12542002-08-22 17:47:09 +0000156 break;
Glenn L McGrath6aa52232004-02-17 11:55:06 +0000157#ifdef CONFIG_FEATURE_TAR_GNU_EXTENSIONS
Glenn L McGrathb3c4e9a2002-09-15 16:54:49 +0000158 case 'L': {
Rob Landley1ec5b292006-05-29 07:42:02 +0000159 longname = xzalloc(file_header->size + 1);
Rob Landley53437472006-07-16 08:14:35 +0000160 xread(archive_handle->src_fd, longname, file_header->size);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000161 archive_handle->offset += file_header->size;
Glenn L McGrathb3c4e9a2002-09-15 16:54:49 +0000162
Denis Vlasenko13858992006-10-08 12:49:22 +0000163 return get_header_tar(archive_handle);
Glenn L McGrathb3c4e9a2002-09-15 16:54:49 +0000164 }
165 case 'K': {
Rob Landley1ec5b292006-05-29 07:42:02 +0000166 linkname = xzalloc(file_header->size + 1);
Rob Landley53437472006-07-16 08:14:35 +0000167 xread(archive_handle->src_fd, linkname, file_header->size);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000168 archive_handle->offset += file_header->size;
Glenn L McGrathb3c4e9a2002-09-15 16:54:49 +0000169
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000170 file_header->name = linkname;
Denis Vlasenko13858992006-10-08 12:49:22 +0000171 return get_header_tar(archive_handle);
Glenn L McGrathb3c4e9a2002-09-15 16:54:49 +0000172 }
Glenn L McGrathb0e163a2004-02-19 08:48:30 +0000173 case 'D': /* GNU dump dir */
174 case 'M': /* Continuation of multi volume archive*/
175 case 'N': /* Old GNU for names > 100 characters */
176 case 'S': /* Sparse file */
177 case 'V': /* Volume header */
Glenn L McGrath6aa52232004-02-17 11:55:06 +0000178#endif
Rob Landleyff6e21c2006-07-06 20:30:19 +0000179 case 'g': /* pax global header */
180 case 'x': /* pax extended header */
Denis Vlasenko13858992006-10-08 12:49:22 +0000181 bb_error_msg("ignoring extension type %c", tar.formatted.typeflag);
Rob Landleyff6e21c2006-07-06 20:30:19 +0000182 break;
Glenn L McGrathb0e163a2004-02-19 08:48:30 +0000183 default:
Denis Vlasenko13858992006-10-08 12:49:22 +0000184 bb_error_msg("unknown typeflag: 0x%x", tar.formatted.typeflag);
Glenn L McGrath6aa52232004-02-17 11:55:06 +0000185 }
186 { /* Strip trailing '/' in directories */
187 /* Must be done after mode is set as '/' is used to check if its a directory */
188 char *tmp = last_char_is(file_header->name, '/');
189 if (tmp) {
190 *tmp = '\0';
191 }
Glenn L McGrath1d23f3a2002-08-13 05:06:43 +0000192 }
Glenn L McGrath3d5828f2003-08-14 02:55:15 +0000193
Glenn L McGrath8e940982002-11-04 23:47:31 +0000194 if (archive_handle->filter(archive_handle) == EXIT_SUCCESS) {
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000195 archive_handle->action_header(archive_handle->file_header);
196 archive_handle->flags |= ARCHIVE_EXTRACT_QUIET;
197 archive_handle->action_data(archive_handle);
Rob Landley8bb50782006-05-26 23:44:51 +0000198 llist_add_to(&(archive_handle->passed), file_header->name);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000199 } else {
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000200 data_skip(archive_handle);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000201 }
202 archive_handle->offset += file_header->size;
203
Glenn L McGrath54ac0572003-11-15 00:24:43 +0000204 free(file_header->link_name);
205
Denis Vlasenko13858992006-10-08 12:49:22 +0000206 return EXIT_SUCCESS;
Eric Andersen2276d832002-07-11 11:11:56 +0000207}