blob: f78377e28a68485e9eed1029614440228e5e8f8d [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
Denis Vlasenkocba9ef52006-10-10 21:00:47 +000077 if (memcmp(tar.formatted.magic, "\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 */
Denis Vlasenkocba9ef52006-10-10 21:00:47 +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);
Denis Vlasenkocba9ef52006-10-10 21:00:47 +0000114 file_header->size = XSTRTOUOFF(tar.formatted.size, 8);
Denis Vlasenko13858992006-10-08 12:49:22 +0000115 file_header->mtime = xstrtoul(tar.formatted.mtime, 8);
116 file_header->link_name = tar.formatted.linkname[0] ?
117 xstrdup(tar.formatted.linkname) : NULL;
Denis Vlasenkocba9ef52006-10-10 21:00:47 +0000118 if (tar.formatted.devmajor[0]) {
119 file_header->device = makedev(xstrtoul(tar.formatted.devmajor, 8),
120 xstrtoul(tar.formatted.devminor, 8));
121 }
Glenn L McGrathb3c4e9a2002-09-15 16:54:49 +0000122
Glenn L McGrath916ba532004-02-20 02:34:42 +0000123 /* Set bits 0-11 of the files mode */
Denis Vlasenko13858992006-10-08 12:49:22 +0000124 file_header->mode = 07777 & xstrtoul(tar.formatted.mode, 8);
Glenn L McGrath916ba532004-02-20 02:34:42 +0000125
126 /* Set bits 12-15 of the files mode */
"Robert P. J. Day"eea56182006-07-20 19:02:24 +0000127 switch (tar.formatted.typeflag) {
Glenn L McGrathb0e163a2004-02-19 08:48:30 +0000128 /* busybox identifies hard links as being regular files with 0 size and a link name */
129 case '1':
Glenn L McGrath916ba532004-02-20 02:34:42 +0000130 file_header->mode |= S_IFREG;
Glenn L McGrathb0e163a2004-02-19 08:48:30 +0000131 break;
Glenn L McGrathc9f1fce2004-02-20 02:25:18 +0000132 case '7':
133 /* Reserved for high performance files, treat as normal file */
Glenn L McGrath99b12542002-08-22 17:47:09 +0000134 case 0:
Glenn L McGrath21110a02003-01-28 01:45:48 +0000135 case '0':
Bernhard Reutner-Fischera1bccc02006-04-02 20:17:55 +0000136#ifdef CONFIG_FEATURE_TAR_OLDGNU_COMPATIBILITY
Glenn L McGrath87af49f2003-09-09 17:41:03 +0000137 if (last_char_is(file_header->name, '/')) {
138 file_header->mode |= S_IFDIR;
Glenn L McGrathc9f1fce2004-02-20 02:25:18 +0000139 } else
140#endif
Glenn L McGrath87af49f2003-09-09 17:41:03 +0000141 file_header->mode |= S_IFREG;
Glenn L McGrath99b12542002-08-22 17:47:09 +0000142 break;
Glenn L McGrath21110a02003-01-28 01:45:48 +0000143 case '2':
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000144 file_header->mode |= S_IFLNK;
Glenn L McGrath99b12542002-08-22 17:47:09 +0000145 break;
Glenn L McGrath21110a02003-01-28 01:45:48 +0000146 case '3':
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000147 file_header->mode |= S_IFCHR;
Glenn L McGrath99b12542002-08-22 17:47:09 +0000148 break;
Glenn L McGrath21110a02003-01-28 01:45:48 +0000149 case '4':
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000150 file_header->mode |= S_IFBLK;
Glenn L McGrath99b12542002-08-22 17:47:09 +0000151 break;
Glenn L McGrath21110a02003-01-28 01:45:48 +0000152 case '5':
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000153 file_header->mode |= S_IFDIR;
Glenn L McGrath99b12542002-08-22 17:47:09 +0000154 break;
Glenn L McGrath21110a02003-01-28 01:45:48 +0000155 case '6':
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000156 file_header->mode |= S_IFIFO;
Glenn L McGrath99b12542002-08-22 17:47:09 +0000157 break;
Glenn L McGrath6aa52232004-02-17 11:55:06 +0000158#ifdef CONFIG_FEATURE_TAR_GNU_EXTENSIONS
Glenn L McGrathb3c4e9a2002-09-15 16:54:49 +0000159 case 'L': {
Rob Landley1ec5b292006-05-29 07:42:02 +0000160 longname = xzalloc(file_header->size + 1);
Rob Landley53437472006-07-16 08:14:35 +0000161 xread(archive_handle->src_fd, longname, file_header->size);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000162 archive_handle->offset += file_header->size;
Glenn L McGrathb3c4e9a2002-09-15 16:54:49 +0000163
Denis Vlasenko13858992006-10-08 12:49:22 +0000164 return get_header_tar(archive_handle);
Glenn L McGrathb3c4e9a2002-09-15 16:54:49 +0000165 }
166 case 'K': {
Rob Landley1ec5b292006-05-29 07:42:02 +0000167 linkname = xzalloc(file_header->size + 1);
Rob Landley53437472006-07-16 08:14:35 +0000168 xread(archive_handle->src_fd, linkname, file_header->size);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000169 archive_handle->offset += file_header->size;
Glenn L McGrathb3c4e9a2002-09-15 16:54:49 +0000170
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000171 file_header->name = linkname;
Denis Vlasenko13858992006-10-08 12:49:22 +0000172 return get_header_tar(archive_handle);
Glenn L McGrathb3c4e9a2002-09-15 16:54:49 +0000173 }
Glenn L McGrathb0e163a2004-02-19 08:48:30 +0000174 case 'D': /* GNU dump dir */
175 case 'M': /* Continuation of multi volume archive*/
176 case 'N': /* Old GNU for names > 100 characters */
177 case 'S': /* Sparse file */
178 case 'V': /* Volume header */
Glenn L McGrath6aa52232004-02-17 11:55:06 +0000179#endif
Rob Landleyff6e21c2006-07-06 20:30:19 +0000180 case 'g': /* pax global header */
181 case 'x': /* pax extended header */
Denis Vlasenko13858992006-10-08 12:49:22 +0000182 bb_error_msg("ignoring extension type %c", tar.formatted.typeflag);
Rob Landleyff6e21c2006-07-06 20:30:19 +0000183 break;
Glenn L McGrathb0e163a2004-02-19 08:48:30 +0000184 default:
Denis Vlasenko13858992006-10-08 12:49:22 +0000185 bb_error_msg("unknown typeflag: 0x%x", tar.formatted.typeflag);
Glenn L McGrath6aa52232004-02-17 11:55:06 +0000186 }
187 { /* Strip trailing '/' in directories */
188 /* Must be done after mode is set as '/' is used to check if its a directory */
189 char *tmp = last_char_is(file_header->name, '/');
190 if (tmp) {
191 *tmp = '\0';
192 }
Glenn L McGrath1d23f3a2002-08-13 05:06:43 +0000193 }
Glenn L McGrath3d5828f2003-08-14 02:55:15 +0000194
Glenn L McGrath8e940982002-11-04 23:47:31 +0000195 if (archive_handle->filter(archive_handle) == EXIT_SUCCESS) {
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000196 archive_handle->action_header(archive_handle->file_header);
197 archive_handle->flags |= ARCHIVE_EXTRACT_QUIET;
198 archive_handle->action_data(archive_handle);
Rob Landley8bb50782006-05-26 23:44:51 +0000199 llist_add_to(&(archive_handle->passed), file_header->name);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000200 } else {
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000201 data_skip(archive_handle);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000202 }
203 archive_handle->offset += file_header->size;
204
Glenn L McGrath54ac0572003-11-15 00:24:43 +0000205 free(file_header->link_name);
206
Denis Vlasenko13858992006-10-08 12:49:22 +0000207 return EXIT_SUCCESS;
Eric Andersen2276d832002-07-11 11:11:56 +0000208}