source: MondoRescue/branches/stable/mindi-busybox/archival/libunarchive/get_header_tar.c@ 821

Last change on this file since 821 was 821, checked in by Bruno Cornec, 18 years ago

Addition of busybox 1.2.1 as a mindi-busybox new package
This should avoid delivering binary files in mindi not built there (Fedora and Debian are quite serious about that)

File size: 5.9 KB
Line 
1/* Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
2 *
3 * FIXME:
4 * In privileged mode if uname and gname map to a uid and gid then use the
5 * mapped value instead of the uid/gid values in tar header
6 *
7 * References:
8 * GNU tar and star man pages,
9 * Opengroup's ustar interchange format,
10 * http://www.opengroup.org/onlinepubs/007904975/utilities/pax.html
11 */
12
13#include <stdio.h>
14#include <stdlib.h>
15#include <string.h>
16#include <sys/sysmacros.h> /* For makedev */
17#include "unarchive.h"
18#include "libbb.h"
19
20#ifdef CONFIG_FEATURE_TAR_GNU_EXTENSIONS
21static char *longname = NULL;
22static char *linkname = NULL;
23#endif
24
25char get_header_tar(archive_handle_t *archive_handle)
26{
27 file_header_t *file_header = archive_handle->file_header;
28 union {
29 /* ustar header, Posix 1003.1 */
30 unsigned char raw[512];
31 struct {
32 char name[100]; /* 0-99 */
33 char mode[8]; /* 100-107 */
34 char uid[8]; /* 108-115 */
35 char gid[8]; /* 116-123 */
36 char size[12]; /* 124-135 */
37 char mtime[12]; /* 136-147 */
38 char chksum[8]; /* 148-155 */
39 char typeflag; /* 156-156 */
40 char linkname[100]; /* 157-256 */
41 char magic[6]; /* 257-262 */
42 char version[2]; /* 263-264 */
43 char uname[32]; /* 265-296 */
44 char gname[32]; /* 297-328 */
45 char devmajor[8]; /* 329-336 */
46 char devminor[8]; /* 337-344 */
47 char prefix[155]; /* 345-499 */
48 char padding[12]; /* 500-512 */
49 } formated;
50 } tar;
51 long sum = 0;
52 long i;
53 static int end = 0;
54
55 /* Align header */
56 data_align(archive_handle, 512);
57
58 if (bb_full_read(archive_handle->src_fd, tar.raw, 512) != 512) {
59 /* Assume end of file */
60 bb_error_msg_and_die("Short header");
61 //return(EXIT_FAILURE);
62 }
63 archive_handle->offset += 512;
64
65 /* If there is no filename its an empty header */
66 if (tar.formated.name[0] == 0) {
67 if (end) {
68 /* This is the second consecutive empty header! End of archive!
69 * Read until the end to empty the pipe from gz or bz2
70 */
71 while (bb_full_read(archive_handle->src_fd, tar.raw, 512) == 512);
72 return(EXIT_FAILURE);
73 }
74 end = 1;
75 return(EXIT_SUCCESS);
76 }
77 end = 0;
78
79 /* Check header has valid magic, "ustar" is for the proper tar
80 * 0's are for the old tar format
81 */
82 if (strncmp(tar.formated.magic, "ustar", 5) != 0) {
83#ifdef CONFIG_FEATURE_TAR_OLDGNU_COMPATIBILITY
84 if (strncmp(tar.formated.magic, "\0\0\0\0\0", 5) != 0)
85#endif
86 bb_error_msg_and_die("Invalid tar magic");
87 }
88 /* Do checksum on headers */
89 for (i = 0; i < 148 ; i++) {
90 sum += tar.raw[i];
91 }
92 sum += ' ' * 8;
93 for (i = 156; i < 512 ; i++) {
94 sum += tar.raw[i];
95 }
96 if (sum != strtol(tar.formated.chksum, NULL, 8)) {
97 bb_error_msg("Invalid tar header checksum");
98 return(EXIT_FAILURE);
99 }
100
101#ifdef CONFIG_FEATURE_TAR_GNU_EXTENSIONS
102 if (longname) {
103 file_header->name = longname;
104 longname = NULL;
105 }
106 else if (linkname) {
107 file_header->name = linkname;
108 linkname = NULL;
109 } else
110#endif
111 {
112 file_header->name = bb_xstrndup(tar.formated.name,100);
113
114 if (tar.formated.prefix[0]) {
115 char *temp = file_header->name;
116 file_header->name = concat_path_file(tar.formated.prefix, temp);
117 free(temp);
118 }
119 }
120
121 file_header->uid = strtol(tar.formated.uid, NULL, 8);
122 file_header->gid = strtol(tar.formated.gid, NULL, 8);
123 file_header->size = strtol(tar.formated.size, NULL, 8);
124 file_header->mtime = strtol(tar.formated.mtime, NULL, 8);
125 file_header->link_name = (tar.formated.linkname[0] != '\0') ?
126 bb_xstrdup(tar.formated.linkname) : NULL;
127 file_header->device = makedev(strtol(tar.formated.devmajor, NULL, 8),
128 strtol(tar.formated.devminor, NULL, 8));
129
130 /* Set bits 0-11 of the files mode */
131 file_header->mode = 07777 & strtol(tar.formated.mode, NULL, 8);
132
133 /* Set bits 12-15 of the files mode */
134 switch (tar.formated.typeflag) {
135 /* busybox identifies hard links as being regular files with 0 size and a link name */
136 case '1':
137 file_header->mode |= S_IFREG;
138 break;
139 case '7':
140 /* Reserved for high performance files, treat as normal file */
141 case 0:
142 case '0':
143#ifdef CONFIG_FEATURE_TAR_OLDGNU_COMPATIBILITY
144 if (last_char_is(file_header->name, '/')) {
145 file_header->mode |= S_IFDIR;
146 } else
147#endif
148 file_header->mode |= S_IFREG;
149 break;
150 case '2':
151 file_header->mode |= S_IFLNK;
152 break;
153 case '3':
154 file_header->mode |= S_IFCHR;
155 break;
156 case '4':
157 file_header->mode |= S_IFBLK;
158 break;
159 case '5':
160 file_header->mode |= S_IFDIR;
161 break;
162 case '6':
163 file_header->mode |= S_IFIFO;
164 break;
165#ifdef CONFIG_FEATURE_TAR_GNU_EXTENSIONS
166 case 'L': {
167 longname = xzalloc(file_header->size + 1);
168 archive_xread_all(archive_handle, longname, file_header->size);
169 archive_handle->offset += file_header->size;
170
171 return(get_header_tar(archive_handle));
172 }
173 case 'K': {
174 linkname = xzalloc(file_header->size + 1);
175 archive_xread_all(archive_handle, linkname, file_header->size);
176 archive_handle->offset += file_header->size;
177
178 file_header->name = linkname;
179 return(get_header_tar(archive_handle));
180 }
181 case 'D': /* GNU dump dir */
182 case 'M': /* Continuation of multi volume archive*/
183 case 'N': /* Old GNU for names > 100 characters */
184 case 'S': /* Sparse file */
185 case 'V': /* Volume header */
186#endif
187 case 'g': /* pax global header */
188 case 'x': /* pax extended header */
189 bb_error_msg("Ignoring extension type %c", tar.formated.typeflag);
190 break;
191 default:
192 bb_error_msg("Unknown typeflag: 0x%x", tar.formated.typeflag);
193 }
194 { /* Strip trailing '/' in directories */
195 /* Must be done after mode is set as '/' is used to check if its a directory */
196 char *tmp = last_char_is(file_header->name, '/');
197 if (tmp) {
198 *tmp = '\0';
199 }
200 }
201
202 if (archive_handle->filter(archive_handle) == EXIT_SUCCESS) {
203 archive_handle->action_header(archive_handle->file_header);
204 archive_handle->flags |= ARCHIVE_EXTRACT_QUIET;
205 archive_handle->action_data(archive_handle);
206 llist_add_to(&(archive_handle->passed), file_header->name);
207 } else {
208 data_skip(archive_handle);
209 }
210 archive_handle->offset += file_header->size;
211
212 free(file_header->link_name);
213
214 return(EXIT_SUCCESS);
215}
Note: See TracBrowser for help on using the repository browser.