blob: 29aed184c19c1bd65ec6dd26816e6bb693e8c4c9 [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
Denis Vlasenkod6772502006-11-24 17:21:44 +000017#if ENABLE_FEATURE_TAR_GNU_EXTENSIONS
18static char *longname;
19static char *linkname;
20#else
21enum {
22 longname = 0,
23 linkname = 0,
24};
Glenn L McGrathb7a76df2002-11-23 10:44:47 +000025#endif
26
Denis Vlasenko376ce1e2006-11-24 14:51:01 +000027/* NB: _DESTROYS_ str[len] character! */
28static unsigned long long getOctal(char *str, int len)
29{
30 unsigned long long v;
31 /* Actually, tar header allows leading spaces also.
32 * Oh well, we will be liberal and skip this...
33 * The only downside probably is that we allow "-123" too :)
34 if (*str < '0' || *str > '7')
35 bb_error_msg_and_die("corrupted octal value in tar header");
36 */
37 str[len] = '\0';
38 v = strtoull(str, &str, 8);
Denis Vlasenkoa80b4a02007-06-21 12:41:59 +000039 if (*str && (!ENABLE_FEATURE_TAR_OLDGNU_COMPATIBILITY || *str != ' '))
Denis Vlasenko376ce1e2006-11-24 14:51:01 +000040 bb_error_msg_and_die("corrupted octal value in tar header");
41 return v;
42}
Denis Vlasenkob5963352006-11-26 01:46:59 +000043#define GET_OCTAL(a) getOctal((a), sizeof(a))
Denis Vlasenko376ce1e2006-11-24 14:51:01 +000044
45void BUG_tar_header_size(void);
Rob Landleydfba7412006-03-06 20:47:33 +000046char get_header_tar(archive_handle_t *archive_handle)
Glenn L McGrath95ebf612001-10-25 14:18:08 +000047{
Denis Vlasenkoa80b4a02007-06-21 12:41:59 +000048 static smallint end;
Denis Vlasenko431a7c92008-02-19 11:26:28 +000049#if ENABLE_FEATURE_TAR_AUTODETECT
50 static smallint not_first;
51#endif
Glenn L McGrath95ebf612001-10-25 14:18:08 +000052
Denis Vlasenko376ce1e2006-11-24 14:51:01 +000053 file_header_t *file_header = archive_handle->file_header;
54 struct {
55 /* ustar header, Posix 1003.1 */
56 char name[100]; /* 0-99 */
57 char mode[8]; /* 100-107 */
58 char uid[8]; /* 108-115 */
59 char gid[8]; /* 116-123 */
60 char size[12]; /* 124-135 */
61 char mtime[12]; /* 136-147 */
62 char chksum[8]; /* 148-155 */
63 char typeflag; /* 156-156 */
64 char linkname[100]; /* 157-256 */
Denis Vlasenko85128622007-11-16 20:35:30 +000065 /* POSIX: "ustar" NUL "00" */
66 /* GNU tar: "ustar " NUL */
Paul Fox16aec392007-11-17 19:11:05 +000067 /* Normally it's defined as magic[6] followed by
68 * version[2], but we put them together to save code.
69 */
Denis Vlasenko85128622007-11-16 20:35:30 +000070 char magic[8]; /* 257-264 */
Denis Vlasenko376ce1e2006-11-24 14:51:01 +000071 char uname[32]; /* 265-296 */
72 char gname[32]; /* 297-328 */
73 char devmajor[8]; /* 329-336 */
74 char devminor[8]; /* 337-344 */
75 char prefix[155]; /* 345-499 */
76 char padding[12]; /* 500-512 */
77 } tar;
78 char *cp;
Denis Vlasenkoa80b4a02007-06-21 12:41:59 +000079 int i, sum_u, sum;
80#if ENABLE_FEATURE_TAR_OLDSUN_COMPATIBILITY
81 int sum_s;
82#endif
Denis Vlasenkod6772502006-11-24 17:21:44 +000083 int parse_names;
Denis Vlasenko376ce1e2006-11-24 14:51:01 +000084
85 if (sizeof(tar) != 512)
86 BUG_tar_header_size();
87
Denis Vlasenko666da5e2006-12-26 18:17:42 +000088#if ENABLE_FEATURE_TAR_GNU_EXTENSIONS
89 again:
90#endif
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000091 /* Align header */
Glenn L McGrath237ae422002-11-03 14:05:15 +000092 data_align(archive_handle, 512);
Glenn L McGrath95ebf612001-10-25 14:18:08 +000093
Denis Vlasenkob5963352006-11-26 01:46:59 +000094 again_after_align:
95
Denis Vlasenko12c06222008-02-14 08:52:30 +000096#if ENABLE_DESKTOP
Denis Vlasenko23ffb6a2008-02-13 17:52:42 +000097 i = full_read(archive_handle->src_fd, &tar, 512);
98 /* if GNU tar sees EOF in above read, it says:
99 * "tar: A lone zero block at N", where N = kilobyte
100 * where EOF was met (not EOF block, actual EOF!),
Denis Vlasenko12c06222008-02-14 08:52:30 +0000101 * and tar will exit with error code 0.
102 * We will mimic exit(0), although we will not mimic
103 * the message and we don't check whether we indeed
104 * saw zero block directly before this. */
Denis Vlasenko23ffb6a2008-02-13 17:52:42 +0000105 if (i == 0)
106 xfunc_error_retval = 0;
107 if (i != 512)
108 bb_error_msg_and_die("short read");
Denis Vlasenko12c06222008-02-14 08:52:30 +0000109#else
110 xread(archive_handle->src_fd, &tar, 512);
111#endif
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000112 archive_handle->offset += 512;
113
114 /* If there is no filename its an empty header */
Denis Vlasenko05efca92008-04-29 04:12:58 +0000115 if (tar.name[0] == 0 && tar.prefix[0] == 0) {
Paul Fox94ff9f12005-07-20 19:24:13 +0000116 if (end) {
117 /* This is the second consecutive empty header! End of archive!
118 * Read until the end to empty the pipe from gz or bz2
119 */
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000120 while (full_read(archive_handle->src_fd, &tar, 512) == 512)
Denis Vlasenko431a7c92008-02-19 11:26:28 +0000121 continue;
Denis Vlasenko13858992006-10-08 12:49:22 +0000122 return EXIT_FAILURE;
Paul Fox94ff9f12005-07-20 19:24:13 +0000123 }
124 end = 1;
Denis Vlasenko13858992006-10-08 12:49:22 +0000125 return EXIT_SUCCESS;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000126 }
Paul Fox94ff9f12005-07-20 19:24:13 +0000127 end = 0;
Glenn L McGrath95ebf612001-10-25 14:18:08 +0000128
Denis Vlasenko431a7c92008-02-19 11:26:28 +0000129 /* Check header has valid magic, "ustar" is for the proper tar,
130 * five NULs are for the old tar format */
131 if (strncmp(tar.magic, "ustar", 5) != 0
132 && (!ENABLE_FEATURE_TAR_OLDGNU_COMPATIBILITY
133 || memcmp(tar.magic, "\0\0\0\0", 5) != 0)
134 ) {
135#if ENABLE_FEATURE_TAR_AUTODETECT
136 char (*get_header_ptr)(archive_handle_t *);
137
138 /* tar gz/bz autodetect: check for gz/bz2 magic.
139 * If it is the very first block, and we see the magic,
140 * we can switch to get_header_tar_gz/bz2/lzma().
141 * Needs seekable fd. I wish recv(MSG_PEEK) would work
142 * on any fd... */
143 if (not_first)
144 goto err;
145#if ENABLE_FEATURE_TAR_GZIP
146 if (tar.name[0] == 0x1f && tar.name[1] == 0x8b) { /* gzip */
147 get_header_ptr = get_header_tar_gz;
148 } else
Glenn L McGrath1d23f3a2002-08-13 05:06:43 +0000149#endif
Denis Vlasenko431a7c92008-02-19 11:26:28 +0000150#if ENABLE_FEATURE_TAR_BZIP2
151 if (tar.name[0] == 'B' && tar.name[1] == 'Z'
152 && tar.name[2] == 'h' && isdigit(tar.name[3])
153 ) { /* bzip2 */
154 get_header_ptr = get_header_tar_bz2;
155 } else
156#endif
157 goto err;
158 if (lseek(archive_handle->src_fd, -512, SEEK_CUR) != 0)
159 goto err;
160 while (get_header_ptr(archive_handle) == EXIT_SUCCESS)
161 continue;
162 return EXIT_FAILURE;
163 err:
164#endif /* FEATURE_TAR_AUTODETECT */
165 bb_error_msg_and_die("invalid tar magic");
Glenn L McGrath95ebf612001-10-25 14:18:08 +0000166 }
Denis Vlasenkodcbd51d2007-03-03 20:06:59 +0000167
Denis Vlasenko431a7c92008-02-19 11:26:28 +0000168#if ENABLE_FEATURE_TAR_AUTODETECT
169 not_first = 1;
170#endif
171
Denis Vlasenkodcbd51d2007-03-03 20:06:59 +0000172 /* Do checksum on headers.
173 * POSIX says that checksum is done on unsigned bytes, but
Denis Vlasenko940494f2007-03-04 18:09:50 +0000174 * Sun and HP-UX gets it wrong... more details in
Denis Vlasenkodcbd51d2007-03-03 20:06:59 +0000175 * GNU tar source. */
Denis Vlasenkoa80b4a02007-06-21 12:41:59 +0000176#if ENABLE_FEATURE_TAR_OLDSUN_COMPATIBILITY
177 sum_s = ' ' * sizeof(tar.chksum);
178#endif
179 sum_u = ' ' * sizeof(tar.chksum);
Denis Vlasenkob71c6682007-07-21 15:08:09 +0000180 for (i = 0; i < 148; i++) {
Denis Vlasenkodcbd51d2007-03-03 20:06:59 +0000181 sum_u += ((unsigned char*)&tar)[i];
Denis Vlasenkoa80b4a02007-06-21 12:41:59 +0000182#if ENABLE_FEATURE_TAR_OLDSUN_COMPATIBILITY
Denis Vlasenkodcbd51d2007-03-03 20:06:59 +0000183 sum_s += ((signed char*)&tar)[i];
Denis Vlasenkoa80b4a02007-06-21 12:41:59 +0000184#endif
Glenn L McGrath95ebf612001-10-25 14:18:08 +0000185 }
Denis Vlasenkob71c6682007-07-21 15:08:09 +0000186 for (i = 156; i < 512; i++) {
Denis Vlasenkodcbd51d2007-03-03 20:06:59 +0000187 sum_u += ((unsigned char*)&tar)[i];
Denis Vlasenkoa80b4a02007-06-21 12:41:59 +0000188#if ENABLE_FEATURE_TAR_OLDSUN_COMPATIBILITY
Denis Vlasenkodcbd51d2007-03-03 20:06:59 +0000189 sum_s += ((signed char*)&tar)[i];
Denis Vlasenkoa80b4a02007-06-21 12:41:59 +0000190#endif
Glenn L McGrath95ebf612001-10-25 14:18:08 +0000191 }
Denis Vlasenkoa80b4a02007-06-21 12:41:59 +0000192#if ENABLE_FEATURE_TAR_OLDGNU_COMPATIBILITY
193 sum = strtoul(tar.chksum, &cp, 8);
194 if ((*cp && *cp != ' ')
195 || (sum_u != sum USE_FEATURE_TAR_OLDSUN_COMPATIBILITY(&& sum_s != sum))
196 ) {
Denis Vlasenko13858992006-10-08 12:49:22 +0000197 bb_error_msg_and_die("invalid tar header checksum");
Glenn L McGrath95ebf612001-10-25 14:18:08 +0000198 }
Denis Vlasenkoa80b4a02007-06-21 12:41:59 +0000199#else
200 /* This field does not need special treatment (getOctal) */
201 sum = xstrtoul(tar.chksum, 8);
202 if (sum_u != sum USE_FEATURE_TAR_OLDSUN_COMPATIBILITY(&& sum_s != sum)) {
203 bb_error_msg_and_die("invalid tar header checksum");
204 }
205#endif
Glenn L McGrath95ebf612001-10-25 14:18:08 +0000206
Denis Vlasenkob5963352006-11-26 01:46:59 +0000207 /* 0 is reserved for high perf file, treat as normal file */
208 if (!tar.typeflag) tar.typeflag = '0';
209 parse_names = (tar.typeflag >= '0' && tar.typeflag <= '7');
Denis Vlasenkod6772502006-11-24 17:21:44 +0000210
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000211 /* getOctal trashes subsequent field, therefore we call it
212 * on fields in reverse order */
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000213 if (tar.devmajor[0]) {
Denis Vlasenkod93400b2008-04-29 03:54:16 +0000214 char t = tar.prefix[0];
215 /* we trash prefix[0] here, but we DO need it later! */
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000216 unsigned minor = GET_OCTAL(tar.devminor);
217 unsigned major = GET_OCTAL(tar.devmajor);
218 file_header->device = makedev(major, minor);
Denis Vlasenkod93400b2008-04-29 03:54:16 +0000219 tar.prefix[0] = t;
Denis Vlasenkocba9ef52006-10-10 21:00:47 +0000220 }
Denis Vlasenko75103842007-06-20 14:49:47 +0000221 file_header->link_target = NULL;
Denis Vlasenkod6772502006-11-24 17:21:44 +0000222 if (!linkname && parse_names && tar.linkname[0]) {
Denis Vlasenkobc1918a2008-04-15 01:17:50 +0000223 file_header->link_target = xstrndup(tar.linkname, sizeof(tar.linkname));
Denis Vlasenko75103842007-06-20 14:49:47 +0000224 /* FIXME: what if we have non-link object with link_target? */
225 /* Will link_target be free()ed? */
Denis Vlasenkod6772502006-11-24 17:21:44 +0000226 }
Denis Vlasenkoe00e5022008-02-14 20:37:54 +0000227#if ENABLE_FEATURE_TAR_UNAME_GNAME
228 file_header->uname = tar.uname[0] ? xstrndup(tar.uname, sizeof(tar.uname)) : NULL;
229 file_header->gname = tar.gname[0] ? xstrndup(tar.gname, sizeof(tar.gname)) : NULL;
230#endif
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000231 file_header->mtime = GET_OCTAL(tar.mtime);
232 file_header->size = GET_OCTAL(tar.size);
233 file_header->gid = GET_OCTAL(tar.gid);
234 file_header->uid = GET_OCTAL(tar.uid);
Glenn L McGrath916ba532004-02-20 02:34:42 +0000235 /* Set bits 0-11 of the files mode */
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000236 file_header->mode = 07777 & GET_OCTAL(tar.mode);
Glenn L McGrath916ba532004-02-20 02:34:42 +0000237
Denis Vlasenkob5963352006-11-26 01:46:59 +0000238 file_header->name = NULL;
Denis Vlasenkod6772502006-11-24 17:21:44 +0000239 if (!longname && parse_names) {
240 /* we trash mode[0] here, it's ok */
Denis Vlasenkobc1918a2008-04-15 01:17:50 +0000241 //tar.name[sizeof(tar.name)] = '\0'; - gcc 4.3.0 would complain
242 tar.mode[0] = '\0';
Denis Vlasenko87cd4a82006-11-25 23:47:32 +0000243 if (tar.prefix[0]) {
244 /* and padding[0] */
Denis Vlasenkobc1918a2008-04-15 01:17:50 +0000245 //tar.prefix[sizeof(tar.prefix)] = '\0'; - gcc 4.3.0 would complain
246 tar.padding[0] = '\0';
Denis Vlasenkocf30cc82006-11-24 14:53:18 +0000247 file_header->name = concat_path_file(tar.prefix, tar.name);
Denis Vlasenko87cd4a82006-11-25 23:47:32 +0000248 } else
Denis Vlasenkocf30cc82006-11-24 14:53:18 +0000249 file_header->name = xstrdup(tar.name);
250 }
251
Glenn L McGrath916ba532004-02-20 02:34:42 +0000252 /* Set bits 12-15 of the files mode */
Denis Vlasenkod6772502006-11-24 17:21:44 +0000253 /* (typeflag was not trashed because chksum does not use getOctal) */
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000254 switch (tar.typeflag) {
Glenn L McGrathb0e163a2004-02-19 08:48:30 +0000255 /* busybox identifies hard links as being regular files with 0 size and a link name */
256 case '1':
Glenn L McGrath916ba532004-02-20 02:34:42 +0000257 file_header->mode |= S_IFREG;
Glenn L McGrathb0e163a2004-02-19 08:48:30 +0000258 break;
Glenn L McGrathc9f1fce2004-02-20 02:25:18 +0000259 case '7':
Denis Vlasenkob5963352006-11-26 01:46:59 +0000260 /* case 0: */
Glenn L McGrath21110a02003-01-28 01:45:48 +0000261 case '0':
Denis Vlasenkod6772502006-11-24 17:21:44 +0000262#if ENABLE_FEATURE_TAR_OLDGNU_COMPATIBILITY
Glenn L McGrath87af49f2003-09-09 17:41:03 +0000263 if (last_char_is(file_header->name, '/')) {
264 file_header->mode |= S_IFDIR;
Glenn L McGrathc9f1fce2004-02-20 02:25:18 +0000265 } else
266#endif
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000267 file_header->mode |= S_IFREG;
Glenn L McGrath99b12542002-08-22 17:47:09 +0000268 break;
Glenn L McGrath21110a02003-01-28 01:45:48 +0000269 case '2':
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000270 file_header->mode |= S_IFLNK;
Glenn L McGrath99b12542002-08-22 17:47:09 +0000271 break;
Glenn L McGrath21110a02003-01-28 01:45:48 +0000272 case '3':
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000273 file_header->mode |= S_IFCHR;
Glenn L McGrath99b12542002-08-22 17:47:09 +0000274 break;
Glenn L McGrath21110a02003-01-28 01:45:48 +0000275 case '4':
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000276 file_header->mode |= S_IFBLK;
Glenn L McGrath99b12542002-08-22 17:47:09 +0000277 break;
Glenn L McGrath21110a02003-01-28 01:45:48 +0000278 case '5':
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000279 file_header->mode |= S_IFDIR;
Glenn L McGrath99b12542002-08-22 17:47:09 +0000280 break;
Glenn L McGrath21110a02003-01-28 01:45:48 +0000281 case '6':
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000282 file_header->mode |= S_IFIFO;
Glenn L McGrath99b12542002-08-22 17:47:09 +0000283 break;
Denis Vlasenkod6772502006-11-24 17:21:44 +0000284#if ENABLE_FEATURE_TAR_GNU_EXTENSIONS
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000285 case 'L':
Denis Vlasenkob5963352006-11-26 01:46:59 +0000286 /* free: paranoia: tar with several consecutive longnames */
Denis Vlasenkod6772502006-11-24 17:21:44 +0000287 free(longname);
Denis Vlasenkob5963352006-11-26 01:46:59 +0000288 /* For paranoia reasons we allocate extra NUL char */
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000289 longname = xzalloc(file_header->size + 1);
Denis Vlasenkob5963352006-11-26 01:46:59 +0000290 /* We read ASCIZ string, including NUL */
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000291 xread(archive_handle->src_fd, longname, file_header->size);
292 archive_handle->offset += file_header->size;
Denis Vlasenkob5963352006-11-26 01:46:59 +0000293 /* return get_header_tar(archive_handle); */
294 /* gcc 4.1.1 didn't optimize it into jump */
295 /* so we will do it ourself, this also saves stack */
296 goto again;
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000297 case 'K':
Denis Vlasenkod6772502006-11-24 17:21:44 +0000298 free(linkname);
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000299 linkname = xzalloc(file_header->size + 1);
300 xread(archive_handle->src_fd, linkname, file_header->size);
301 archive_handle->offset += file_header->size;
Denis Vlasenkob5963352006-11-26 01:46:59 +0000302 /* return get_header_tar(archive_handle); */
303 goto again;
Glenn L McGrathb0e163a2004-02-19 08:48:30 +0000304 case 'D': /* GNU dump dir */
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000305 case 'M': /* Continuation of multi volume archive */
Glenn L McGrathb0e163a2004-02-19 08:48:30 +0000306 case 'N': /* Old GNU for names > 100 characters */
307 case 'S': /* Sparse file */
308 case 'V': /* Volume header */
Glenn L McGrath6aa52232004-02-17 11:55:06 +0000309#endif
Rob Landleyff6e21c2006-07-06 20:30:19 +0000310 case 'g': /* pax global header */
Denis Vlasenkob5963352006-11-26 01:46:59 +0000311 case 'x': { /* pax extended header */
312 off_t sz;
313 bb_error_msg("warning: skipping header '%c'", tar.typeflag);
314 sz = (file_header->size + 511) & ~(off_t)511;
315 archive_handle->offset += sz;
316 sz >>= 9; /* sz /= 512 but w/o contortions for signed div */
317 while (sz--)
318 xread(archive_handle->src_fd, &tar, 512);
319 /* return get_header_tar(archive_handle); */
320 goto again_after_align;
321 }
Glenn L McGrathb0e163a2004-02-19 08:48:30 +0000322 default:
Denis Vlasenkob5963352006-11-26 01:46:59 +0000323 bb_error_msg_and_die("unknown typeflag: 0x%x", tar.typeflag);
Glenn L McGrath6aa52232004-02-17 11:55:06 +0000324 }
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000325
Denis Vlasenkod6772502006-11-24 17:21:44 +0000326#if ENABLE_FEATURE_TAR_GNU_EXTENSIONS
327 if (longname) {
328 file_header->name = longname;
329 longname = NULL;
330 }
331 if (linkname) {
Denis Vlasenko75103842007-06-20 14:49:47 +0000332 file_header->link_target = linkname;
Denis Vlasenkod6772502006-11-24 17:21:44 +0000333 linkname = NULL;
334 }
335#endif
Denis Vlasenkoc1660fe2006-11-26 15:42:03 +0000336 if (!strncmp(file_header->name, "/../"+1, 3)
337 || strstr(file_header->name, "/../")
338 ) {
339 bb_error_msg_and_die("name with '..' encountered: '%s'",
340 file_header->name);
341 }
Denis Vlasenkod6772502006-11-24 17:21:44 +0000342
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000343 /* Strip trailing '/' in directories */
Denis Vlasenko714701c2006-12-22 00:21:07 +0000344 /* Must be done after mode is set as '/' is used to check if it's a directory */
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000345 cp = last_char_is(file_header->name, '/');
Glenn L McGrath3d5828f2003-08-14 02:55:15 +0000346
Glenn L McGrath8e940982002-11-04 23:47:31 +0000347 if (archive_handle->filter(archive_handle) == EXIT_SUCCESS) {
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000348 archive_handle->action_header(archive_handle->file_header);
Denis Vlasenkocf30cc82006-11-24 14:53:18 +0000349 /* Note that we kill the '/' only after action_header() */
350 /* (like GNU tar 1.15.1: verbose mode outputs "dir/dir/") */
351 if (cp) *cp = '\0';
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000352 archive_handle->flags |= ARCHIVE_EXTRACT_QUIET;
353 archive_handle->action_data(archive_handle);
Rob Landley8bb50782006-05-26 23:44:51 +0000354 llist_add_to(&(archive_handle->passed), file_header->name);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000355 } else {
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000356 data_skip(archive_handle);
Denis Vlasenkob5963352006-11-26 01:46:59 +0000357 free(file_header->name);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000358 }
359 archive_handle->offset += file_header->size;
360
Denis Vlasenko75103842007-06-20 14:49:47 +0000361 free(file_header->link_target);
Denis Vlasenkob5963352006-11-26 01:46:59 +0000362 /* Do not free(file_header->name)! */
Denis Vlasenkoe00e5022008-02-14 20:37:54 +0000363#if ENABLE_FEATURE_TAR_UNAME_GNAME
364 free(file_header->uname);
365 free(file_header->gname);
366#endif
Denis Vlasenko13858992006-10-08 12:49:22 +0000367 return EXIT_SUCCESS;
Eric Andersen2276d832002-07-11 11:11:56 +0000368}