source: trunk/essentials/app-arch/cpio/src/tar.c

Last change on this file was 3332, checked in by bird, 18 years ago

cpio 2.7

File size: 14.2 KB
Line 
1/* tar.c - read in write tar headers for cpio
2 Copyright (C) 1992, 2001, 2004, 2006 Free Software Foundation, Inc.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
7 any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public
15 License along with this program; if not, write to the Free
16 Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 Boston, MA 02110-1301 USA. */
18
19#include <system.h>
20
21#include <stdio.h>
22#include <sys/types.h>
23#include <sys/stat.h>
24#include "filetypes.h"
25#include "cpiohdr.h"
26#include "dstring.h"
27#include "extern.h"
28#include <rmt.h>
29#include "tarhdr.h"
30
31/* Stash the tar linkname in static storage. */
32
33static char *
34stash_tar_linkname (char *linkname)
35{
36 static char hold_tar_linkname[TARLINKNAMESIZE + 1];
37
38 strncpy (hold_tar_linkname, linkname, TARLINKNAMESIZE);
39 hold_tar_linkname[TARLINKNAMESIZE] = '\0';
40 return hold_tar_linkname;
41}
42
43/* Try to split a long file name into prefix and suffix parts separated
44 by a slash. Return the length of the prefix (not counting the slash). */
45
46static size_t
47split_long_name (const char *name, size_t length)
48{
49 size_t i;
50
51 if (length > TARPREFIXSIZE)
52 length = TARPREFIXSIZE+2;
53 for (i = length - 1; i > 0; i--)
54 if (name[i] == '/')
55 break;
56 return i;
57}
58
59/* Stash the tar filename and optional prefix in static storage. */
60
61static char *
62stash_tar_filename (char *prefix, char *filename)
63{
64 static char hold_tar_filename[TARNAMESIZE + TARPREFIXSIZE + 2];
65 if (prefix == NULL || *prefix == '\0')
66 {
67 strncpy (hold_tar_filename, filename, TARNAMESIZE);
68 hold_tar_filename[TARNAMESIZE] = '\0';
69 }
70 else
71 {
72 strncpy (hold_tar_filename, prefix, TARPREFIXSIZE);
73 hold_tar_filename[TARPREFIXSIZE] = '\0';
74 strcat (hold_tar_filename, "/");
75 strncat (hold_tar_filename, filename, TARNAMESIZE);
76 hold_tar_filename[TARPREFIXSIZE + TARNAMESIZE] = '\0';
77 }
78 return hold_tar_filename;
79}
80
81/* Convert a number into a string of octal digits.
82 Convert long VALUE into a DIGITS-digit field at WHERE,
83 including a trailing space and room for a NUL. DIGITS==3 means
84 1 digit, a space, and room for a NUL.
85
86 We assume the trailing NUL is already there and don't fill it in.
87 This fact is used by start_header and finish_header, so don't change it!
88
89 This is be equivalent to:
90 sprintf (where, "%*lo ", digits - 2, value);
91 except that sprintf fills in the trailing NUL and we don't. */
92
93static void
94to_oct (register long value, register int digits, register char *where)
95{
96 --digits; /* Leave the trailing NUL slot alone. */
97
98 /* Produce the digits -- at least one. */
99 do
100 {
101 where[--digits] = '0' + (char) (value & 7); /* One octal digit. */
102 value >>= 3;
103 }
104 while (digits > 0 && value != 0);
105
106 /* Add leading zeroes, if necessary. */
107 while (digits > 0)
108 where[--digits] = '0';
109}
110
111
112
113
114/* Compute and return a checksum for TAR_HDR,
115 counting the checksum bytes as if they were spaces. */
116
117unsigned int
118tar_checksum (struct tar_header *tar_hdr)
119{
120 unsigned int sum = 0;
121 char *p = (char *) tar_hdr;
122 char *q = p + TARRECORDSIZE;
123 int i;
124
125 while (p < tar_hdr->chksum)
126 sum += *p++ & 0xff;
127 for (i = 0; i < 8; ++i)
128 {
129 sum += ' ';
130 ++p;
131 }
132 while (p < q)
133 sum += *p++ & 0xff;
134 return sum;
135}
136
137/* Write out header FILE_HDR, including the file name, to file
138 descriptor OUT_DES. */
139
140void
141write_out_tar_header (struct cpio_file_stat *file_hdr, int out_des)
142{
143 int name_len;
144 union tar_record tar_rec;
145 struct tar_header *tar_hdr = (struct tar_header *) &tar_rec;
146
147 memset (&tar_rec, 0, sizeof tar_rec);
148
149 /* process_copy_out must ensure that file_hdr->c_name is short enough,
150 or we will lose here. */
151
152 name_len = strlen (file_hdr->c_name);
153 if (name_len <= TARNAMESIZE)
154 {
155 strncpy (tar_hdr->name, file_hdr->c_name, name_len);
156 }
157 else
158 {
159 /* Fit as much as we can into `name', the rest into `prefix'. */
160 int prefix_len = split_long_name (file_hdr->c_name, name_len);
161
162 strncpy (tar_hdr->prefix, file_hdr->c_name, prefix_len);
163 strncpy (tar_hdr->name, file_hdr->c_name + prefix_len + 1,
164 name_len - prefix_len - 1);
165 }
166
167 /* Ustar standard (POSIX.1-1988) requires the mode to contain only 3 octal
168 digits */
169 to_oct (file_hdr->c_mode & MODE_ALL, 8, tar_hdr->mode);
170 to_oct (file_hdr->c_uid, 8, tar_hdr->uid);
171 to_oct (file_hdr->c_gid, 8, tar_hdr->gid);
172 to_oct (file_hdr->c_filesize, 12, tar_hdr->size);
173 to_oct (file_hdr->c_mtime, 12, tar_hdr->mtime);
174
175 switch (file_hdr->c_mode & CP_IFMT)
176 {
177 case CP_IFREG:
178 if (file_hdr->c_tar_linkname)
179 {
180 /* process_copy_out makes sure that c_tar_linkname is shorter
181 than TARLINKNAMESIZE. */
182 strncpy (tar_hdr->linkname, file_hdr->c_tar_linkname,
183 TARLINKNAMESIZE);
184 tar_hdr->typeflag = LNKTYPE;
185 to_oct (0, 12, tar_hdr->size);
186 }
187 else
188 tar_hdr->typeflag = REGTYPE;
189 break;
190 case CP_IFDIR:
191 tar_hdr->typeflag = DIRTYPE;
192 break;
193 case CP_IFCHR:
194 tar_hdr->typeflag = CHRTYPE;
195 break;
196 case CP_IFBLK:
197 tar_hdr->typeflag = BLKTYPE;
198 break;
199#ifdef CP_IFIFO
200 case CP_IFIFO:
201 tar_hdr->typeflag = FIFOTYPE;
202 break;
203#endif /* CP_IFIFO */
204#ifdef CP_IFLNK
205 case CP_IFLNK:
206 tar_hdr->typeflag = SYMTYPE;
207 /* process_copy_out makes sure that c_tar_linkname is shorter
208 than TARLINKNAMESIZE. */
209 strncpy (tar_hdr->linkname, file_hdr->c_tar_linkname,
210 TARLINKNAMESIZE);
211 to_oct (0, 12, tar_hdr->size);
212 break;
213#endif /* CP_IFLNK */
214 }
215
216 if (archive_format == arf_ustar)
217 {
218 char *name;
219
220 strncpy (tar_hdr->magic, TMAGIC, TMAGLEN);
221 strncpy (tar_hdr->magic + TMAGLEN, TVERSION, TVERSLEN);
222
223 name = getuser (file_hdr->c_uid);
224 if (name)
225 strcpy (tar_hdr->uname, name);
226 name = getgroup (file_hdr->c_gid);
227 if (name)
228 strcpy (tar_hdr->gname, name);
229
230 to_oct (file_hdr->c_rdev_maj, 8, tar_hdr->devmajor);
231 to_oct (file_hdr->c_rdev_min, 8, tar_hdr->devminor);
232 }
233
234 to_oct (tar_checksum (tar_hdr), 8, tar_hdr->chksum);
235
236 tape_buffered_write ((char *) &tar_rec, out_des, TARRECORDSIZE);
237}
238
239/* Return nonzero iff all the bytes in BLOCK are NUL.
240 SIZE is the number of bytes to check in BLOCK; it must be a
241 multiple of sizeof (long). */
242
243int
244null_block (long *block, int size)
245{
246 register long *p = block;
247 register int i = size / sizeof (long);
248
249 while (i--)
250 if (*p++)
251 return 0;
252 return 1;
253}
254
255/* Read a tar header, including the file name, from file descriptor IN_DES
256 into FILE_HDR. */
257
258void
259read_in_tar_header (struct cpio_file_stat *file_hdr, int in_des)
260{
261 long bytes_skipped = 0;
262 int warned = false;
263 union tar_record tar_rec;
264 struct tar_header *tar_hdr = (struct tar_header *) &tar_rec;
265 uid_t *uidp;
266 gid_t *gidp;
267
268 tape_buffered_read ((char *) &tar_rec, in_des, TARRECORDSIZE);
269
270 /* Check for a block of 0's. */
271 if (null_block ((long *) &tar_rec, TARRECORDSIZE))
272 {
273#if 0
274 /* Found one block of 512 0's. If the next block is also all 0's
275 then this is the end of the archive. If not, assume the
276 previous block was all corruption and continue reading
277 the archive. */
278 /* Commented out because GNU tar sometimes creates archives with
279 only one block of 0's at the end. This happened for the
280 cpio 2.0 distribution! */
281 tape_buffered_read ((char *) &tar_rec, in_des, TARRECORDSIZE);
282 if (null_block ((long *) &tar_rec, TARRECORDSIZE))
283#endif
284 {
285 file_hdr->c_name = CPIO_TRAILER_NAME;
286 return;
287 }
288#if 0
289 bytes_skipped = TARRECORDSIZE;
290#endif
291 }
292
293 while (1)
294 {
295 file_hdr->c_chksum = FROM_OCTAL (tar_hdr->chksum);
296
297 if (file_hdr->c_chksum != tar_checksum (tar_hdr))
298 {
299 /* If the checksum is bad, skip 1 byte and try again. When
300 we try again we do not look for an EOF record (all zeros),
301 because when we start skipping bytes in a corrupted archive
302 the chances are pretty good that we might stumble across
303 2 blocks of 512 zeros (that probably is not really the last
304 record) and it is better to miss the EOF and give the user
305 a "premature EOF" error than to give up too soon on a corrupted
306 archive. */
307 if (!warned)
308 {
309 error (0, 0, _("invalid header: checksum error"));
310 warned = true;
311 }
312 memmove (&tar_rec, ((char *) &tar_rec) + 1, TARRECORDSIZE - 1);
313 tape_buffered_read (((char *) &tar_rec) + (TARRECORDSIZE - 1), in_des, 1);
314 ++bytes_skipped;
315 continue;
316 }
317
318 if (archive_format != arf_ustar)
319 file_hdr->c_name = stash_tar_filename (NULL, tar_hdr->name);
320 else
321 file_hdr->c_name = stash_tar_filename (tar_hdr->prefix, tar_hdr->name);
322 file_hdr->c_nlink = 1;
323 file_hdr->c_mode = FROM_OCTAL (tar_hdr->mode);
324 file_hdr->c_mode = file_hdr->c_mode & 07777;
325 /* Debian hack: This version of cpio uses the -n flag also to extract
326 tar archives using the numeric UID/GID instead of the user/group
327 names in /etc/passwd and /etc/groups. (98/10/15) -BEM */
328 if (archive_format == arf_ustar && !numeric_uid
329 && (uidp = getuidbyname (tar_hdr->uname)))
330 file_hdr->c_uid = *uidp;
331 else
332 file_hdr->c_uid = FROM_OCTAL (tar_hdr->uid);
333
334 if (archive_format == arf_ustar && !numeric_uid
335 && (gidp = getgidbyname (tar_hdr->gname)))
336 file_hdr->c_gid = *gidp;
337 else
338 file_hdr->c_gid = FROM_OCTAL (tar_hdr->gid);
339 file_hdr->c_filesize = FROM_OCTAL (tar_hdr->size);
340 file_hdr->c_mtime = FROM_OCTAL (tar_hdr->mtime);
341 file_hdr->c_rdev_maj = FROM_OCTAL (tar_hdr->devmajor);
342 file_hdr->c_rdev_min = FROM_OCTAL (tar_hdr->devminor);
343 file_hdr->c_tar_linkname = NULL;
344
345 switch (tar_hdr->typeflag)
346 {
347 case REGTYPE:
348 case CONTTYPE: /* For now, punt. */
349 default:
350 file_hdr->c_mode |= CP_IFREG;
351 break;
352 case DIRTYPE:
353 file_hdr->c_mode |= CP_IFDIR;
354 break;
355 case CHRTYPE:
356 file_hdr->c_mode |= CP_IFCHR;
357 /* If a POSIX tar header has a valid linkname it's always supposed
358 to set typeflag to be LNKTYPE. System V.4 tar seems to
359 be broken, and for device files with multiple links it
360 puts the name of the link into linkname, but leaves typeflag
361 as CHRTYPE, BLKTYPE, FIFOTYPE, etc. */
362 file_hdr->c_tar_linkname = stash_tar_linkname (tar_hdr->linkname);
363
364 /* Does POSIX say that the filesize must be 0 for devices? We
365 assume so, but HPUX's POSIX tar sets it to be 1 which causes
366 us problems (when reading an archive we assume we can always
367 skip to the next file by skipping filesize bytes). For
368 now at least, it's easier to clear filesize for devices,
369 rather than check everywhere we skip in copyin.c. */
370 file_hdr->c_filesize = 0;
371 break;
372 case BLKTYPE:
373 file_hdr->c_mode |= CP_IFBLK;
374 file_hdr->c_tar_linkname = stash_tar_linkname (tar_hdr->linkname);
375 file_hdr->c_filesize = 0;
376 break;
377#ifdef CP_IFIFO
378 case FIFOTYPE:
379 file_hdr->c_mode |= CP_IFIFO;
380 file_hdr->c_tar_linkname = stash_tar_linkname (tar_hdr->linkname);
381 file_hdr->c_filesize = 0;
382 break;
383#endif
384 case SYMTYPE:
385#ifdef CP_IFLNK
386 file_hdr->c_mode |= CP_IFLNK;
387 file_hdr->c_tar_linkname = stash_tar_linkname (tar_hdr->linkname);
388 file_hdr->c_filesize = 0;
389 break;
390 /* Else fall through. */
391#endif
392 case LNKTYPE:
393 file_hdr->c_mode |= CP_IFREG;
394 file_hdr->c_tar_linkname = stash_tar_linkname (tar_hdr->linkname);
395 file_hdr->c_filesize = 0;
396 break;
397
398 case AREGTYPE:
399 /* Old tar format; if the last char in filename is '/' then it is
400 a directory, otherwise it's a regular file. */
401 if (file_hdr->c_name[strlen (file_hdr->c_name) - 1] == '/')
402 file_hdr->c_mode |= CP_IFDIR;
403 else
404 file_hdr->c_mode |= CP_IFREG;
405 break;
406 }
407 break;
408 }
409 if (bytes_skipped > 0)
410 warn_junk_bytes (bytes_skipped);
411}
412
413/* Return
414 2 if BUF is a valid POSIX tar header (the checksum is correct
415 and it has the "ustar" magic string),
416 1 if BUF is a valid old tar header (the checksum is correct),
417 0 otherwise. */
418
419int
420is_tar_header (char *buf)
421{
422 struct tar_header *tar_hdr = (struct tar_header *) buf;
423 unsigned long chksum;
424
425 chksum = FROM_OCTAL (tar_hdr->chksum);
426
427 if (chksum != tar_checksum (tar_hdr))
428 return 0;
429
430 /* GNU tar 1.10 and previous set the magic field to be "ustar " instead
431 of "ustar\0". Only look at the first 5 characters of the magic
432 field so we can recognize old GNU tar ustar archives. */
433 if (!strncmp (tar_hdr->magic, TMAGIC, TMAGLEN - 1))
434 return 2;
435 return 1;
436}
437
438/* Return true if the filename is too long to fit in a tar header.
439 For old tar headers, if the filename's length is less than or equal
440 to 100 then it will fit, otherwise it will not. For POSIX tar headers,
441 if the filename's length is less than or equal to 100 then it
442 will definitely fit, and if it is greater than 256 then it
443 will definitely not fit. If the length is between 100 and 256,
444 then the filename will fit only if it is possible to break it
445 into a 155 character "prefix" and 100 character "name". There
446 must be a slash between the "prefix" and the "name", although
447 the slash is not stored or counted in either the "prefix" or
448 the "name", and there must be at least one character in both
449 the "prefix" and the "name". If it is not possible to break down
450 the filename like this then it will not fit. */
451
452int
453is_tar_filename_too_long (char *name)
454{
455 int whole_name_len;
456 int prefix_name_len;
457
458 whole_name_len = strlen (name);
459 if (whole_name_len <= TARNAMESIZE)
460 return false;
461
462 if (archive_format != arf_ustar)
463 return true;
464
465 if (whole_name_len > TARNAMESIZE + TARPREFIXSIZE + 1)
466 return true;
467
468 /* See whether we can split up the name into acceptably-sized
469 `prefix' and `name' (`p') pieces. */
470 prefix_name_len = split_long_name (name, whole_name_len);
471
472 /* Interestingly, a name consisting of a slash followed by
473 TARNAMESIZE characters can't be stored, because the prefix
474 would be empty, and thus ignored. */
475 if (prefix_name_len == 0
476 || whole_name_len - prefix_name_len - 1 > TARNAMESIZE)
477 return true;
478
479 return false;
480}
Note: See TracBrowser for help on using the repository browser.