tar: small fix and small optimization
[oweals/busybox.git] / archival / libunarchive / get_header_tar.c
1 /* vi: set sw=4 ts=4: */
2 /* Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
3  *
4  *  FIXME:
5  *    In privileged mode if uname and gname map to a uid and gid then use the
6  *    mapped value instead of the uid/gid values in tar header
7  *
8  *  References:
9  *    GNU tar and star man pages,
10  *    Opengroup's ustar interchange format,
11  *      http://www.opengroup.org/onlinepubs/007904975/utilities/pax.html
12  */
13
14 #include "libbb.h"
15 #include "unarchive.h"
16
17 #if ENABLE_FEATURE_TAR_GNU_EXTENSIONS
18 static char *longname;
19 static char *linkname;
20 #else
21 enum {
22         longname = 0,
23         linkname = 0,
24 };
25 #endif
26
27 /* NB: _DESTROYS_ str[len] character! */
28 static 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);
39         if (*str)
40                 bb_error_msg_and_die("corrupted octal value in tar header");
41         return v;
42 }
43
44 void BUG_tar_header_size(void);
45 char get_header_tar(archive_handle_t *archive_handle)
46 {
47         static int end = 0;
48
49         file_header_t *file_header = archive_handle->file_header;
50         struct {
51                 /* ustar header, Posix 1003.1 */
52                 char name[100];     /*   0-99 */
53                 char mode[8];       /* 100-107 */
54                 char uid[8];        /* 108-115 */
55                 char gid[8];        /* 116-123 */
56                 char size[12];      /* 124-135 */
57                 char mtime[12];     /* 136-147 */
58                 char chksum[8];     /* 148-155 */
59                 char typeflag;      /* 156-156 */
60                 char linkname[100]; /* 157-256 */
61                 char magic[6];      /* 257-262 */
62                 char version[2];    /* 263-264 */
63                 char uname[32];     /* 265-296 */
64                 char gname[32];     /* 297-328 */
65                 char devmajor[8];   /* 329-336 */
66                 char devminor[8];   /* 337-344 */
67                 char prefix[155];   /* 345-499 */
68                 char padding[12];   /* 500-512 */
69         } tar;
70         char *cp;
71         int sum, i;
72 #if ENABLE_FEATURE_TAR_GNU_EXTENSIONS
73         int parse_names;
74 #else
75         enum { parse_names = 1 };
76 #endif
77
78         if (sizeof(tar) != 512)
79                 BUG_tar_header_size();
80
81         /* Align header */
82         data_align(archive_handle, 512);
83
84         xread(archive_handle->src_fd, &tar, 512);
85         archive_handle->offset += 512;
86
87         /* If there is no filename its an empty header */
88         if (tar.name[0] == 0) {
89                 if (end) {
90                         /* This is the second consecutive empty header! End of archive!
91                          * Read until the end to empty the pipe from gz or bz2
92                          */
93                         while (full_read(archive_handle->src_fd, &tar, 512) == 512)
94                                 /* repeat */;
95                         return EXIT_FAILURE;
96                 }
97                 end = 1;
98                 return EXIT_SUCCESS;
99         }
100         end = 0;
101
102         /* Check header has valid magic, "ustar" is for the proper tar
103          * 0's are for the old tar format
104          */
105         if (strncmp(tar.magic, "ustar", 5) != 0) {
106 #if ENABLE_FEATURE_TAR_OLDGNU_COMPATIBILITY
107                 if (memcmp(tar.magic, "\0\0\0\0", 5) != 0)
108 #endif
109                         bb_error_msg_and_die("invalid tar magic");
110         }
111         /* Do checksum on headers */
112         sum = ' ' * sizeof(tar.chksum);
113         for (i = 0; i < 148 ; i++) {
114                 sum += ((char*)&tar)[i];
115         }
116         for (i = 156; i < 512 ; i++) {
117                 sum += ((char*)&tar)[i];
118         }
119         /* This field does not need special treatment (getOctal) */
120         if (sum != xstrtoul(tar.chksum, 8)) {
121                 bb_error_msg_and_die("invalid tar header checksum");
122         }
123
124 #if ENABLE_FEATURE_TAR_GNU_EXTENSIONS
125         parse_names = (tar.typeflag != 'L' && tar.typeflag != 'K');
126 #endif
127
128         /* getOctal trashes subsequent field, therefore we call it
129          * on fields in reverse order */
130 #define GET_OCTAL(a) getOctal((a), sizeof(a))
131         if (tar.devmajor[0]) {
132                 unsigned minor = GET_OCTAL(tar.devminor);
133                 unsigned major = GET_OCTAL(tar.devmajor);
134                 file_header->device = makedev(major, minor);
135         }
136         file_header->link_name = NULL;
137         if (!linkname && parse_names && tar.linkname[0]) {
138                 /* we trash magic[0] here, it's ok */
139                 tar.linkname[sizeof(tar.linkname)] = '\0';
140                 file_header->link_name = xstrdup(tar.linkname);
141                 /* FIXME: what if we have non-link object with link_name? */
142                 /* Will link_name be free()ed? */
143         }
144         file_header->mtime = GET_OCTAL(tar.mtime);
145         file_header->size = GET_OCTAL(tar.size);
146         file_header->gid = GET_OCTAL(tar.gid);
147         file_header->uid = GET_OCTAL(tar.uid);
148         /* Set bits 0-11 of the files mode */
149         file_header->mode = 07777 & GET_OCTAL(tar.mode);
150 #undef GET_OCTAL
151
152         if (!longname && parse_names) {
153                 /* we trash mode[0] here, it's ok */
154                 tar.name[sizeof(tar.name)] = '\0';
155                 if (tar.prefix[0]) {
156                         /* and padding[0] */
157                         tar.prefix[sizeof(tar.prefix)] = '\0';
158                         file_header->name = concat_path_file(tar.prefix, tar.name);
159                 } else
160                         file_header->name = xstrdup(tar.name);
161         }
162
163         /* Set bits 12-15 of the files mode */
164         /* (typeflag was not trashed because chksum does not use getOctal) */
165         switch (tar.typeflag) {
166         /* busybox identifies hard links as being regular files with 0 size and a link name */
167         case '1':
168                 file_header->mode |= S_IFREG;
169                 break;
170         case '7':
171                 /* Reserved for high performance files, treat as normal file */
172         case 0:
173         case '0':
174 #if ENABLE_FEATURE_TAR_OLDGNU_COMPATIBILITY
175                 if (last_char_is(file_header->name, '/')) {
176                         file_header->mode |= S_IFDIR;
177                 } else
178 #endif
179                 file_header->mode |= S_IFREG;
180                 break;
181         case '2':
182                 file_header->mode |= S_IFLNK;
183                 break;
184         case '3':
185                 file_header->mode |= S_IFCHR;
186                 break;
187         case '4':
188                 file_header->mode |= S_IFBLK;
189                 break;
190         case '5':
191                 file_header->mode |= S_IFDIR;
192                 break;
193         case '6':
194                 file_header->mode |= S_IFIFO;
195                 break;
196 #if ENABLE_FEATURE_TAR_GNU_EXTENSIONS
197         case 'L':
198                 /* paranoia: tar with several consecutive longnames */
199                 free(longname);
200                 longname = xzalloc(file_header->size + 1);
201                 xread(archive_handle->src_fd, longname, file_header->size);
202                 archive_handle->offset += file_header->size;
203                 return get_header_tar(archive_handle);
204         case 'K':
205                 free(linkname);
206                 linkname = xzalloc(file_header->size + 1);
207                 xread(archive_handle->src_fd, linkname, file_header->size);
208                 archive_handle->offset += file_header->size;
209                 return get_header_tar(archive_handle);
210         case 'D':       /* GNU dump dir */
211         case 'M':       /* Continuation of multi volume archive */
212         case 'N':       /* Old GNU for names > 100 characters */
213         case 'S':       /* Sparse file */
214         case 'V':       /* Volume header */
215 #endif
216         case 'g':       /* pax global header */
217         case 'x':       /* pax extended header */
218                 bb_error_msg("ignoring extension type %c", tar.typeflag);
219                 break;
220         default:
221                 bb_error_msg("unknown typeflag: 0x%x", tar.typeflag);
222         }
223
224 #if ENABLE_FEATURE_TAR_GNU_EXTENSIONS
225         if (longname) {
226                 file_header->name = longname;
227                 longname = NULL;
228         }
229         if (linkname) {
230                 file_header->link_name = linkname;
231                 linkname = NULL;
232         }
233 #endif
234
235         /* Strip trailing '/' in directories */
236         /* Must be done after mode is set as '/' is used to check if its a directory */
237         cp = last_char_is(file_header->name, '/');
238
239         if (archive_handle->filter(archive_handle) == EXIT_SUCCESS) {
240                 archive_handle->action_header(archive_handle->file_header);
241                 /* Note that we kill the '/' only after action_header() */
242                 /* (like GNU tar 1.15.1: verbose mode outputs "dir/dir/") */
243                 if (cp) *cp = '\0';
244                 archive_handle->flags |= ARCHIVE_EXTRACT_QUIET;
245                 archive_handle->action_data(archive_handle);
246                 llist_add_to(&(archive_handle->passed), file_header->name);
247         } else {
248                 data_skip(archive_handle);
249         }
250         archive_handle->offset += file_header->size;
251
252         free(file_header->link_name);
253
254         return EXIT_SUCCESS;
255 }