3 /* tar.c - read in write tar headers for cpio
4 Copyright (C) 1992, 2001, 2004 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 License along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
23 #include <sys/types.h>
25 #include "filetypes.h"
32 /* Stash the tar linkname in static storage. */
35 stash_tar_linkname (char *linkname)
37 static char hold_tar_linkname[TARLINKNAMESIZE + 1];
39 strncpy (hold_tar_linkname, linkname, TARLINKNAMESIZE);
40 hold_tar_linkname[TARLINKNAMESIZE] = '\0';
41 return hold_tar_linkname;
44 /* Try to split a long file name into prefix and suffix parts separated
45 by a slash. Return the length of the prefix (not counting the slash). */
48 split_long_name (const char *name, size_t length)
52 if (length > TARPREFIXSIZE)
53 length = TARPREFIXSIZE+2;
54 for (i = length - 1; i > 0; i--)
60 /* Stash the tar filename and optional prefix in static storage. */
63 stash_tar_filename (char *prefix, char *filename)
65 static char hold_tar_filename[TARNAMESIZE + TARPREFIXSIZE + 2];
66 if (prefix == NULL || *prefix == '\0')
68 strncpy (hold_tar_filename, filename, TARNAMESIZE);
69 hold_tar_filename[TARNAMESIZE] = '\0';
73 strncpy (hold_tar_filename, prefix, TARPREFIXSIZE);
74 hold_tar_filename[TARPREFIXSIZE] = '\0';
75 strcat (hold_tar_filename, "/");
76 strncat (hold_tar_filename, filename, TARNAMESIZE);
77 hold_tar_filename[TARPREFIXSIZE + TARNAMESIZE] = '\0';
79 return hold_tar_filename;
82 /* Convert a number into a string of octal digits.
83 Convert long VALUE into a DIGITS-digit field at WHERE,
84 including a trailing space and room for a NUL. DIGITS==3 means
85 1 digit, a space, and room for a NUL.
87 We assume the trailing NUL is already there and don't fill it in.
88 This fact is used by start_header and finish_header, so don't change it!
90 This is be equivalent to:
91 sprintf (where, "%*lo ", digits - 2, value);
92 except that sprintf fills in the trailing NUL and we don't. */
95 to_oct (register long value, register int digits, register char *where)
97 --digits; /* Leave the trailing NUL slot alone. */
99 /* Produce the digits -- at least one. */
102 where[--digits] = '0' + (char) (value & 7); /* One octal digit. */
105 while (digits > 0 && value != 0);
107 /* Add leading zeroes, if necessary. */
109 where[--digits] = '0';
114 /* Compute and return a checksum for TAR_HDR,
115 counting the checksum bytes as if they were spaces. */
118 tar_checksum (struct tar_header *tar_hdr)
120 unsigned long sum = 0;
121 char *p = (char *) tar_hdr;
122 char *q = p + TARRECORDSIZE;
125 while (p < tar_hdr->chksum)
127 for (i = 0; i < 8; ++i)
137 /* Write out header FILE_HDR, including the file name, to file
138 descriptor OUT_DES. */
141 write_out_tar_header (struct new_cpio_header *file_hdr, int out_des)
144 union tar_record tar_rec;
145 struct tar_header *tar_hdr = (struct tar_header *) &tar_rec;
147 bzero ((char *) &tar_rec, TARRECORDSIZE);
149 /* process_copy_out must ensure that file_hdr->c_name is short enough,
150 or we will lose here. */
152 name_len = strlen (file_hdr->c_name);
153 if (name_len <= TARNAMESIZE)
155 strncpy (tar_hdr->name, file_hdr->c_name, name_len);
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);
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);
167 /* Ustar standard (POSIX.1-1988) requires the mode to contain only 3 octal
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);
175 switch (file_hdr->c_mode & CP_IFMT)
178 if (file_hdr->c_tar_linkname)
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,
184 tar_hdr->typeflag = LNKTYPE;
185 to_oct (0, 12, tar_hdr->size);
188 tar_hdr->typeflag = REGTYPE;
191 tar_hdr->typeflag = DIRTYPE;
194 tar_hdr->typeflag = CHRTYPE;
197 tar_hdr->typeflag = BLKTYPE;
201 tar_hdr->typeflag = FIFOTYPE;
203 #endif /* CP_IFIFO */
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,
211 to_oct (0, 12, tar_hdr->size);
213 #endif /* CP_IFLNK */
216 if (archive_format == arf_ustar)
220 strncpy (tar_hdr->magic, TMAGIC, TMAGLEN);
221 strncpy (tar_hdr->magic + TMAGLEN, TVERSION, TVERSLEN);
223 name = getuser (file_hdr->c_uid);
225 strcpy (tar_hdr->uname, name);
226 name = getgroup (file_hdr->c_gid);
228 strcpy (tar_hdr->gname, name);
230 to_oct (file_hdr->c_rdev_maj, 8, tar_hdr->devmajor);
231 to_oct (file_hdr->c_rdev_min, 8, tar_hdr->devminor);
234 to_oct (tar_checksum (tar_hdr), 8, tar_hdr->chksum);
236 tape_buffered_write ((char *) &tar_rec, out_des, TARRECORDSIZE);
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). */
244 null_block (long *block, int size)
246 register long *p = block;
247 register int i = size / sizeof (long);
255 /* Read a tar header, including the file name, from file descriptor IN_DES
259 read_in_tar_header (struct new_cpio_header *file_hdr, int in_des)
261 long bytes_skipped = 0;
263 union tar_record tar_rec;
264 struct tar_header *tar_hdr = (struct tar_header *) &tar_rec;
268 tape_buffered_read ((char *) &tar_rec, in_des, TARRECORDSIZE);
270 /* Check for a block of 0's. */
271 if (null_block ((long *) &tar_rec, TARRECORDSIZE))
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
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))
285 file_hdr->c_name = "TRAILER!!!";
289 bytes_skipped = TARRECORDSIZE;
295 otoa (tar_hdr->chksum, &file_hdr->c_chksum);
297 if (file_hdr->c_chksum != tar_checksum (tar_hdr))
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
309 error (0, 0, _("invalid header: checksum error"));
312 bcopy (((char *) &tar_rec) + 1, (char *) &tar_rec,
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 otoa (tar_hdr->mode, &file_hdr->c_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 otoa (tar_hdr->uid, &file_hdr->c_uid);
335 if (archive_format == arf_ustar && !numeric_uid
336 && (gidp = getgidbyname (tar_hdr->gname)))
337 file_hdr->c_gid = *gidp;
339 otoa (tar_hdr->gid, &file_hdr->c_gid);
340 otoa (tar_hdr->size, &file_hdr->c_filesize);
341 otoa (tar_hdr->mtime, &file_hdr->c_mtime);
342 otoa (tar_hdr->devmajor, (unsigned long *) &file_hdr->c_rdev_maj);
343 otoa (tar_hdr->devminor, (unsigned long *) &file_hdr->c_rdev_min);
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);
427 /* Convert the string of octal digits S into a number and store
428 it in *N. Return nonzero if the whole string was converted,
429 zero if there was something after the number.
430 Skip leading and trailing spaces. */
433 otoa (char *s, unsigned long *n)
435 unsigned long val = 0;
439 while (*s >= '0' && *s <= '7')
440 val = 8 * val + *s++ - '0';
448 2 if BUF is a valid POSIX tar header (the checksum is correct
449 and it has the "ustar" magic string),
450 1 if BUF is a valid old tar header (the checksum is correct),
454 is_tar_header (char *buf)
456 struct tar_header *tar_hdr = (struct tar_header *) buf;
457 unsigned long chksum;
459 otoa (tar_hdr->chksum, &chksum);
461 if (chksum != tar_checksum (tar_hdr))
464 /* GNU tar 1.10 and previous set the magic field to be "ustar " instead
465 of "ustar\0". Only look at the first 5 characters of the magic
466 field so we can recognize old GNU tar ustar archives. */
467 if (!strncmp (tar_hdr->magic, TMAGIC, TMAGLEN - 1))
472 /* Return true if the filename is too long to fit in a tar header.
473 For old tar headers, if the filename's length is less than or equal
474 to 100 then it will fit, otherwise it will not. For POSIX tar headers,
475 if the filename's length is less than or equal to 100 then it
476 will definitely fit, and if it is greater than 256 then it
477 will definitely not fit. If the length is between 100 and 256,
478 then the filename will fit only if it is possible to break it
479 into a 155 character "prefix" and 100 character "name". There
480 must be a slash between the "prefix" and the "name", although
481 the slash is not stored or counted in either the "prefix" or
482 the "name", and there must be at least one character in both
483 the "prefix" and the "name". If it is not possible to break down
484 the filename like this then it will not fit. */
487 is_tar_filename_too_long (char *name)
493 whole_name_len = strlen (name);
494 if (whole_name_len <= TARNAMESIZE)
497 if (archive_format != arf_ustar)
500 if (whole_name_len > TARNAMESIZE + TARPREFIXSIZE + 1)
503 /* See whether we can split up the name into acceptably-sized
504 `prefix' and `name' (`p') pieces. */
505 prefix_name_len = split_long_name (name, whole_name_len);
507 /* Interestingly, a name consisting of a slash followed by
508 TARNAMESIZE characters can't be stored, because the prefix
509 would be empty, and thus ignored. */
510 if (prefix_name_len == 0
511 || whole_name_len - prefix_name_len - 1 > TARNAMESIZE)