3 /* tar.c - read in write tar headers for cpio
4 Copyright (C) 1992, 2001, 2004, 2006 Free Software Foundation, Inc.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public
17 License along with this program; if not, write to the Free
18 Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 Boston, MA 02110-1301 USA. */
24 #include <sys/types.h>
26 #include "filetypes.h"
33 /* Stash the tar linkname in static storage. */
36 stash_tar_linkname (char *linkname)
38 static char hold_tar_linkname[TARLINKNAMESIZE + 1];
40 strncpy (hold_tar_linkname, linkname, TARLINKNAMESIZE);
41 hold_tar_linkname[TARLINKNAMESIZE] = '\0';
42 return hold_tar_linkname;
45 /* Try to split a long file name into prefix and suffix parts separated
46 by a slash. Return the length of the prefix (not counting the slash). */
49 split_long_name (const char *name, size_t length)
53 if (length > TARPREFIXSIZE)
54 length = TARPREFIXSIZE+2;
55 for (i = length - 1; i > 0; i--)
61 /* Stash the tar filename and optional prefix in static storage. */
64 stash_tar_filename (char *prefix, char *filename)
66 static char hold_tar_filename[TARNAMESIZE + TARPREFIXSIZE + 2];
67 if (prefix == NULL || *prefix == '\0')
69 strncpy (hold_tar_filename, filename, TARNAMESIZE);
70 hold_tar_filename[TARNAMESIZE] = '\0';
74 strncpy (hold_tar_filename, prefix, TARPREFIXSIZE);
75 hold_tar_filename[TARPREFIXSIZE] = '\0';
76 strcat (hold_tar_filename, "/");
77 strncat (hold_tar_filename, filename, TARNAMESIZE);
78 hold_tar_filename[TARPREFIXSIZE + TARNAMESIZE] = '\0';
80 return hold_tar_filename;
83 /* Convert a number into a string of octal digits.
84 Convert long VALUE into a DIGITS-digit field at WHERE,
85 including a trailing space and room for a NUL. DIGITS==3 means
86 1 digit, a space, and room for a NUL.
88 We assume the trailing NUL is already there and don't fill it in.
89 This fact is used by start_header and finish_header, so don't change it!
91 This is be equivalent to:
92 sprintf (where, "%*lo ", digits - 2, value);
93 except that sprintf fills in the trailing NUL and we don't. */
96 to_oct (register long value, register int digits, register char *where)
98 --digits; /* Leave the trailing NUL slot alone. */
100 /* Produce the digits -- at least one. */
103 where[--digits] = '0' + (char) (value & 7); /* One octal digit. */
106 while (digits > 0 && value != 0);
108 /* Add leading zeroes, if necessary. */
110 where[--digits] = '0';
115 /* Compute and return a checksum for TAR_HDR,
116 counting the checksum bytes as if they were spaces. */
119 tar_checksum (struct tar_header *tar_hdr)
121 unsigned int sum = 0;
122 char *p = (char *) tar_hdr;
123 char *q = p + TARRECORDSIZE;
126 while (p < tar_hdr->chksum)
128 for (i = 0; i < 8; ++i)
138 /* Write out header FILE_HDR, including the file name, to file
139 descriptor OUT_DES. */
142 write_out_tar_header (struct cpio_file_stat *file_hdr, int out_des)
145 union tar_record tar_rec;
146 struct tar_header *tar_hdr = (struct tar_header *) &tar_rec;
148 memset (&tar_rec, 0, sizeof tar_rec);
150 /* process_copy_out must ensure that file_hdr->c_name is short enough,
151 or we will lose here. */
153 name_len = strlen (file_hdr->c_name);
154 if (name_len <= TARNAMESIZE)
156 strncpy (tar_hdr->name, file_hdr->c_name, name_len);
160 /* Fit as much as we can into `name', the rest into `prefix'. */
161 int prefix_len = split_long_name (file_hdr->c_name, name_len);
163 strncpy (tar_hdr->prefix, file_hdr->c_name, prefix_len);
164 strncpy (tar_hdr->name, file_hdr->c_name + prefix_len + 1,
165 name_len - prefix_len - 1);
168 /* Ustar standard (POSIX.1-1988) requires the mode to contain only 3 octal
170 to_oct (file_hdr->c_mode & MODE_ALL, 8, tar_hdr->mode);
171 to_oct (file_hdr->c_uid, 8, tar_hdr->uid);
172 to_oct (file_hdr->c_gid, 8, tar_hdr->gid);
173 to_oct (file_hdr->c_filesize, 12, tar_hdr->size);
174 to_oct (file_hdr->c_mtime, 12, tar_hdr->mtime);
176 switch (file_hdr->c_mode & CP_IFMT)
179 if (file_hdr->c_tar_linkname)
181 /* process_copy_out makes sure that c_tar_linkname is shorter
182 than TARLINKNAMESIZE. */
183 strncpy (tar_hdr->linkname, file_hdr->c_tar_linkname,
185 tar_hdr->typeflag = LNKTYPE;
186 to_oct (0, 12, tar_hdr->size);
189 tar_hdr->typeflag = REGTYPE;
192 tar_hdr->typeflag = DIRTYPE;
195 tar_hdr->typeflag = CHRTYPE;
198 tar_hdr->typeflag = BLKTYPE;
202 tar_hdr->typeflag = FIFOTYPE;
204 #endif /* CP_IFIFO */
207 tar_hdr->typeflag = SYMTYPE;
208 /* process_copy_out makes sure that c_tar_linkname is shorter
209 than TARLINKNAMESIZE. */
210 strncpy (tar_hdr->linkname, file_hdr->c_tar_linkname,
212 to_oct (0, 12, tar_hdr->size);
214 #endif /* CP_IFLNK */
217 if (archive_format == arf_ustar)
221 strncpy (tar_hdr->magic, TMAGIC, TMAGLEN);
222 strncpy (tar_hdr->magic + TMAGLEN, TVERSION, TVERSLEN);
224 name = getuser (file_hdr->c_uid);
226 strcpy (tar_hdr->uname, name);
227 name = getgroup (file_hdr->c_gid);
229 strcpy (tar_hdr->gname, name);
231 to_oct (file_hdr->c_rdev_maj, 8, tar_hdr->devmajor);
232 to_oct (file_hdr->c_rdev_min, 8, tar_hdr->devminor);
235 to_oct (tar_checksum (tar_hdr), 8, tar_hdr->chksum);
237 tape_buffered_write ((char *) &tar_rec, out_des, TARRECORDSIZE);
240 /* Return nonzero iff all the bytes in BLOCK are NUL.
241 SIZE is the number of bytes to check in BLOCK; it must be a
242 multiple of sizeof (long). */
245 null_block (long *block, int size)
247 register long *p = block;
248 register int i = size / sizeof (long);
256 /* Read a tar header, including the file name, from file descriptor IN_DES
260 read_in_tar_header (struct cpio_file_stat *file_hdr, int in_des)
262 long bytes_skipped = 0;
264 union tar_record tar_rec;
265 struct tar_header *tar_hdr = (struct tar_header *) &tar_rec;
269 tape_buffered_read ((char *) &tar_rec, in_des, TARRECORDSIZE);
271 /* Check for a block of 0's. */
272 if (null_block ((long *) &tar_rec, TARRECORDSIZE))
275 /* Found one block of 512 0's. If the next block is also all 0's
276 then this is the end of the archive. If not, assume the
277 previous block was all corruption and continue reading
279 /* Commented out because GNU tar sometimes creates archives with
280 only one block of 0's at the end. This happened for the
281 cpio 2.0 distribution! */
282 tape_buffered_read ((char *) &tar_rec, in_des, TARRECORDSIZE);
283 if (null_block ((long *) &tar_rec, TARRECORDSIZE))
286 file_hdr->c_name = CPIO_TRAILER_NAME;
290 bytes_skipped = TARRECORDSIZE;
296 file_hdr->c_chksum = FROM_OCTAL (tar_hdr->chksum);
298 if (file_hdr->c_chksum != tar_checksum (tar_hdr))
300 /* If the checksum is bad, skip 1 byte and try again. When
301 we try again we do not look for an EOF record (all zeros),
302 because when we start skipping bytes in a corrupted archive
303 the chances are pretty good that we might stumble across
304 2 blocks of 512 zeros (that probably is not really the last
305 record) and it is better to miss the EOF and give the user
306 a "premature EOF" error than to give up too soon on a corrupted
310 error (0, 0, _("invalid header: checksum error"));
313 memmove (&tar_rec, ((char *) &tar_rec) + 1, TARRECORDSIZE - 1);
314 tape_buffered_read (((char *) &tar_rec) + (TARRECORDSIZE - 1), in_des, 1);
319 if (archive_format != arf_ustar)
320 file_hdr->c_name = stash_tar_filename (NULL, tar_hdr->name);
322 file_hdr->c_name = stash_tar_filename (tar_hdr->prefix, tar_hdr->name);
323 file_hdr->c_nlink = 1;
324 file_hdr->c_mode = FROM_OCTAL (tar_hdr->mode);
325 file_hdr->c_mode = file_hdr->c_mode & 07777;
326 /* Debian hack: This version of cpio uses the -n flag also to extract
327 tar archives using the numeric UID/GID instead of the user/group
328 names in /etc/passwd and /etc/groups. (98/10/15) -BEM */
329 if (archive_format == arf_ustar && !numeric_uid
330 && (uidp = getuidbyname (tar_hdr->uname)))
331 file_hdr->c_uid = *uidp;
333 file_hdr->c_uid = FROM_OCTAL (tar_hdr->uid);
335 if (archive_format == arf_ustar && !numeric_uid
336 && (gidp = getgidbyname (tar_hdr->gname)))
337 file_hdr->c_gid = *gidp;
339 file_hdr->c_gid = FROM_OCTAL (tar_hdr->gid);
340 file_hdr->c_filesize = FROM_OCTAL (tar_hdr->size);
341 file_hdr->c_mtime = FROM_OCTAL (tar_hdr->mtime);
342 file_hdr->c_rdev_maj = FROM_OCTAL (tar_hdr->devmajor);
343 file_hdr->c_rdev_min = FROM_OCTAL (tar_hdr->devminor);
344 file_hdr->c_tar_linkname = NULL;
346 switch (tar_hdr->typeflag)
349 case CONTTYPE: /* For now, punt. */
351 file_hdr->c_mode |= CP_IFREG;
354 file_hdr->c_mode |= CP_IFDIR;
357 file_hdr->c_mode |= CP_IFCHR;
358 /* If a POSIX tar header has a valid linkname it's always supposed
359 to set typeflag to be LNKTYPE. System V.4 tar seems to
360 be broken, and for device files with multiple links it
361 puts the name of the link into linkname, but leaves typeflag
362 as CHRTYPE, BLKTYPE, FIFOTYPE, etc. */
363 file_hdr->c_tar_linkname = stash_tar_linkname (tar_hdr->linkname);
365 /* Does POSIX say that the filesize must be 0 for devices? We
366 assume so, but HPUX's POSIX tar sets it to be 1 which causes
367 us problems (when reading an archive we assume we can always
368 skip to the next file by skipping filesize bytes). For
369 now at least, it's easier to clear filesize for devices,
370 rather than check everywhere we skip in copyin.c. */
371 file_hdr->c_filesize = 0;
374 file_hdr->c_mode |= CP_IFBLK;
375 file_hdr->c_tar_linkname = stash_tar_linkname (tar_hdr->linkname);
376 file_hdr->c_filesize = 0;
380 file_hdr->c_mode |= CP_IFIFO;
381 file_hdr->c_tar_linkname = stash_tar_linkname (tar_hdr->linkname);
382 file_hdr->c_filesize = 0;
387 file_hdr->c_mode |= CP_IFLNK;
388 file_hdr->c_tar_linkname = stash_tar_linkname (tar_hdr->linkname);
389 file_hdr->c_filesize = 0;
391 /* Else fall through. */
394 file_hdr->c_mode |= CP_IFREG;
395 file_hdr->c_tar_linkname = stash_tar_linkname (tar_hdr->linkname);
396 file_hdr->c_filesize = 0;
400 /* Old tar format; if the last char in filename is '/' then it is
401 a directory, otherwise it's a regular file. */
402 if (file_hdr->c_name[strlen (file_hdr->c_name) - 1] == '/')
403 file_hdr->c_mode |= CP_IFDIR;
405 file_hdr->c_mode |= CP_IFREG;
408 /* Ignore pax 'x' and 'g' extension entries. */
409 /* Skip body of this entry. */
410 while (file_hdr->c_filesize > 0) {
411 tape_buffered_read(((char *) &tar_rec), in_des, TARRECORDSIZE);
412 if (file_hdr->c_filesize > TARRECORDSIZE)
413 file_hdr->c_filesize -= TARRECORDSIZE;
415 file_hdr->c_filesize = 0;
417 /* Read next header and return that instead. */
418 read_in_tar_header(file_hdr, in_des);
423 if (bytes_skipped > 0)
424 warn_junk_bytes (bytes_skipped);
428 2 if BUF is a valid POSIX tar header (the checksum is correct
429 and it has the "ustar" magic string),
430 1 if BUF is a valid old tar header (the checksum is correct),
434 is_tar_header (char *buf)
436 struct tar_header *tar_hdr = (struct tar_header *) buf;
437 unsigned long chksum;
439 chksum = FROM_OCTAL (tar_hdr->chksum);
441 if (chksum != tar_checksum (tar_hdr))
444 /* GNU tar 1.10 and previous set the magic field to be "ustar " instead
445 of "ustar\0". Only look at the first 5 characters of the magic
446 field so we can recognize old GNU tar ustar archives. */
447 if (!strncmp (tar_hdr->magic, TMAGIC, TMAGLEN - 1))
452 /* Return true if the filename is too long to fit in a tar header.
453 For old tar headers, if the filename's length is less than or equal
454 to 100 then it will fit, otherwise it will not. For POSIX tar headers,
455 if the filename's length is less than or equal to 100 then it
456 will definitely fit, and if it is greater than 256 then it
457 will definitely not fit. If the length is between 100 and 256,
458 then the filename will fit only if it is possible to break it
459 into a 155 character "prefix" and 100 character "name". There
460 must be a slash between the "prefix" and the "name", although
461 the slash is not stored or counted in either the "prefix" or
462 the "name", and there must be at least one character in both
463 the "prefix" and the "name". If it is not possible to break down
464 the filename like this then it will not fit. */
467 is_tar_filename_too_long (char *name)
472 whole_name_len = strlen (name);
473 if (whole_name_len <= TARNAMESIZE)
476 if (archive_format != arf_ustar)
479 if (whole_name_len > TARNAMESIZE + TARPREFIXSIZE + 1)
482 /* See whether we can split up the name into acceptably-sized
483 `prefix' and `name' (`p') pieces. */
484 prefix_name_len = split_long_name (name, whole_name_len);
486 /* Interestingly, a name consisting of a slash followed by
487 TARNAMESIZE characters can't be stored, because the prefix
488 would be empty, and thus ignored. */
489 if (prefix_name_len == 0
490 || whole_name_len - prefix_name_len - 1 > TARNAMESIZE)