1 /* tar.c - read in write tar headers for cpio
2 Copyright (C) 1992 Free Software Foundation, Inc.
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)
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.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
19 #include <sys/types.h>
21 #include "filetypes.h"
29 static void to_oct ();
30 static char *stash_tar_linkname ();
31 static char *stash_tar_filename ();
33 /* Compute and return a checksum for TAR_HDR,
34 counting the checksum bytes as if they were spaces. */
37 tar_checksum (tar_hdr
)
38 struct tar_header
*tar_hdr
;
40 unsigned long sum
= 0;
41 char *p
= (char *) tar_hdr
;
42 char *q
= p
+ TARRECORDSIZE
;
45 while (p
< tar_hdr
->chksum
)
47 for (i
= 0; i
< 8; ++i
)
57 /* Write out header FILE_HDR, including the file name, to file
58 descriptor OUT_DES. */
61 write_out_tar_header (file_hdr
, out_des
)
62 struct new_cpio_header
*file_hdr
;
66 union tar_record tar_rec
;
67 struct tar_header
*tar_hdr
= (struct tar_header
*) &tar_rec
;
69 bzero ((char *) &tar_rec
, TARRECORDSIZE
);
71 /* process_copy_out must ensure that file_hdr->c_name is short enough,
72 or we will lose here. */
74 name_len
= strlen (file_hdr
->c_name
);
75 if (name_len
<= TARNAMESIZE
)
77 strncpy (tar_hdr
->name
, file_hdr
->c_name
, name_len
);
81 /* Fit as much as we can into `name', the rest into `prefix'. */
82 char *suffix
= file_hdr
->c_name
+ name_len
- TARNAMESIZE
;
84 /* We have to put the boundary at a slash. */
85 name_len
= TARNAMESIZE
;
86 while (*suffix
!= '/')
91 strncpy (tar_hdr
->name
, suffix
+ 1, name_len
);
92 strncpy (tar_hdr
->prefix
, file_hdr
->c_name
, suffix
- file_hdr
->c_name
);
95 /* SVR4 seems to want the whole mode, not just protection modes.
96 Nobody else seems to care, so we might as well put it all in. */
97 to_oct (file_hdr
->c_mode
, 8, tar_hdr
->mode
);
98 to_oct (file_hdr
->c_uid
, 8, tar_hdr
->uid
);
99 to_oct (file_hdr
->c_gid
, 8, tar_hdr
->gid
);
100 to_oct (file_hdr
->c_filesize
, 12, tar_hdr
->size
);
101 to_oct (file_hdr
->c_mtime
, 12, tar_hdr
->mtime
);
103 switch (file_hdr
->c_mode
& CP_IFMT
)
106 if (file_hdr
->c_tar_linkname
)
108 /* process_copy_out makes sure that c_tar_linkname is shorter
109 than TARLINKNAMESIZE. */
110 strncpy (tar_hdr
->linkname
, file_hdr
->c_tar_linkname
,
112 tar_hdr
->typeflag
= LNKTYPE
;
113 to_oct (0, 12, tar_hdr
->size
);
116 tar_hdr
->typeflag
= REGTYPE
;
119 tar_hdr
->typeflag
= DIRTYPE
;
123 tar_hdr
->typeflag
= CHRTYPE
;
126 tar_hdr
->typeflag
= BLKTYPE
;
130 tar_hdr
->typeflag
= FIFOTYPE
;
132 #endif /* CP_IFIFO */
135 tar_hdr
->typeflag
= SYMTYPE
;
136 /* process_copy_out makes sure that c_tar_linkname is shorter
137 than TARLINKNAMESIZE. */
138 strncpy (tar_hdr
->linkname
, file_hdr
->c_tar_linkname
,
140 to_oct (0, 12, tar_hdr
->size
);
142 #endif /* CP_IFLNK */
143 #endif /* !__MSDOS__ */
146 if (archive_format
== arf_ustar
)
150 strncpy (tar_hdr
->magic
, TMAGIC
, TMAGLEN
);
151 strncpy (tar_hdr
->magic
+ TMAGLEN
, TVERSION
, TVERSLEN
);
154 name
= getuser (file_hdr
->c_uid
);
156 strcpy (tar_hdr
->uname
, name
);
157 name
= getgroup (file_hdr
->c_gid
);
159 strcpy (tar_hdr
->gname
, name
);
162 to_oct (file_hdr
->c_rdev_maj
, 8, tar_hdr
->devmajor
);
163 to_oct (file_hdr
->c_rdev_min
, 8, tar_hdr
->devminor
);
166 to_oct (tar_checksum (tar_hdr
), 8, tar_hdr
->chksum
);
168 tape_buffered_write ((char *) &tar_rec
, out_des
, TARRECORDSIZE
);
171 /* Return nonzero iff all the bytes in BLOCK are NUL.
172 SIZE is the number of bytes to check in BLOCK; it must be a
173 multiple of sizeof (long). */
176 null_block (block
, size
)
180 register long *p
= block
;
181 register int i
= size
/ sizeof (long);
189 /* Read a tar header, including the file name, from file descriptor IN_DES
193 read_in_tar_header (file_hdr
, in_des
)
194 struct new_cpio_header
*file_hdr
;
197 long bytes_skipped
= 0;
199 union tar_record tar_rec
;
200 struct tar_header
*tar_hdr
= (struct tar_header
*) &tar_rec
;
206 tape_buffered_read ((char *) &tar_rec
, in_des
, TARRECORDSIZE
);
208 /* Check for a block of 0's. */
209 if (null_block ((long *) &tar_rec
, TARRECORDSIZE
))
212 /* Found one block of 512 0's. If the next block is also all 0's
213 then this is the end of the archive. If not, assume the
214 previous block was all corruption and continue reading
216 /* Commented out because GNU tar sometimes creates archives with
217 only one block of 0's at the end. This happened for the
218 cpio 2.0 distribution! */
219 tape_buffered_read ((char *) &tar_rec
, in_des
, TARRECORDSIZE
);
220 if (null_block ((long *) &tar_rec
, TARRECORDSIZE
))
223 file_hdr
->c_name
= "TRAILER!!!";
227 bytes_skipped
= TARRECORDSIZE
;
233 otoa (tar_hdr
->chksum
, &file_hdr
->c_chksum
);
235 if (file_hdr
->c_chksum
!= tar_checksum (tar_hdr
))
237 /* If the checksum is bad, skip 1 byte and try again. When
238 we try again we do not look for an EOF record (all zeros),
239 because when we start skipping bytes in a corrupted archive
240 the chances are pretty good that we might stumble across
241 2 blocks of 512 zeros (that probably is not really the last
242 record) and it is better to miss the EOF and give the user
243 a "premature EOF" error than to give up too soon on a corrupted
247 error (0, 0, "invalid header: checksum error");
250 bcopy (((char *) &tar_rec
) + 1, (char *) &tar_rec
,
252 tape_buffered_read (((char *) &tar_rec
) + (TARRECORDSIZE
- 1), in_des
, 1);
257 if (archive_format
!= arf_ustar
)
258 file_hdr
->c_name
= stash_tar_filename (NULL
, tar_hdr
->name
);
260 file_hdr
->c_name
= stash_tar_filename (tar_hdr
->prefix
, tar_hdr
->name
);
261 file_hdr
->c_nlink
= 1;
262 otoa (tar_hdr
->mode
, &file_hdr
->c_mode
);
263 file_hdr
->c_mode
= file_hdr
->c_mode
& 07777;
265 if (archive_format
== arf_ustar
266 && (uidp
= getuidbyname (tar_hdr
->uname
)))
267 file_hdr
->c_uid
= *uidp
;
270 otoa (tar_hdr
->uid
, &file_hdr
->c_uid
);
272 if (archive_format
== arf_ustar
273 && (gidp
= getgidbyname (tar_hdr
->gname
)))
274 file_hdr
->c_gid
= *gidp
;
277 otoa (tar_hdr
->gid
, &file_hdr
->c_gid
);
278 otoa (tar_hdr
->size
, &file_hdr
->c_filesize
);
279 otoa (tar_hdr
->mtime
, &file_hdr
->c_mtime
);
280 otoa (tar_hdr
->devmajor
, (unsigned long *) &file_hdr
->c_rdev_maj
);
281 otoa (tar_hdr
->devminor
, (unsigned long *) &file_hdr
->c_rdev_min
);
282 file_hdr
->c_tar_linkname
= NULL
;
284 switch (tar_hdr
->typeflag
)
287 case CONTTYPE
: /* For now, punt. */
289 file_hdr
->c_mode
|= CP_IFREG
;
292 file_hdr
->c_mode
|= CP_IFDIR
;
296 file_hdr
->c_mode
|= CP_IFCHR
;
297 /* If a POSIX tar header has a valid linkname it's always supposed
298 to set typeflag to be LNKTYPE. System V.4 tar seems to
299 be broken, and for device files with multiple links it
300 puts the name of the link into linkname, but leaves typeflag
301 as CHRTYPE, BLKTYPE, FIFOTYPE, etc. */
302 file_hdr
->c_tar_linkname
= stash_tar_linkname (tar_hdr
->linkname
);
304 /* Does POSIX say that the filesize must be 0 for devices? We
305 assume so, but HPUX's POSIX tar sets it to be 1 which causes
306 us problems (when reading an archive we assume we can always
307 skip to the next file by skipping filesize bytes). For
308 now at least, it's easier to clear filesize for devices,
309 rather than check everywhere we skip in copyin.c. */
310 file_hdr
->c_filesize
= 0;
313 file_hdr
->c_mode
|= CP_IFBLK
;
314 file_hdr
->c_tar_linkname
= stash_tar_linkname (tar_hdr
->linkname
);
315 file_hdr
->c_filesize
= 0;
319 file_hdr
->c_mode
|= CP_IFIFO
;
320 file_hdr
->c_tar_linkname
= stash_tar_linkname (tar_hdr
->linkname
);
321 file_hdr
->c_filesize
= 0;
326 file_hdr
->c_mode
|= CP_IFLNK
;
327 file_hdr
->c_tar_linkname
= stash_tar_linkname (tar_hdr
->linkname
);
328 file_hdr
->c_filesize
= 0;
330 /* Else fall through. */
333 file_hdr
->c_mode
|= CP_IFREG
;
334 file_hdr
->c_tar_linkname
= stash_tar_linkname (tar_hdr
->linkname
);
335 file_hdr
->c_filesize
= 0;
337 #endif /* !__MSDOS__ */
339 /* Old tar format; if the last char in filename is '/' then it is
340 a directory, otherwise it's a regular file. */
341 if (file_hdr
->c_name
[strlen (file_hdr
->c_name
) - 1] == '/')
342 file_hdr
->c_mode
|= CP_IFDIR
;
344 file_hdr
->c_mode
|= CP_IFREG
;
349 if (bytes_skipped
> 0)
350 error (0, 0, "warning: skipped %ld bytes of junk", bytes_skipped
);
353 /* Stash the tar linkname in static storage. */
356 stash_tar_linkname (linkname
)
359 static char hold_tar_linkname
[TARLINKNAMESIZE
+ 1];
361 strncpy (hold_tar_linkname
, linkname
, TARLINKNAMESIZE
);
362 hold_tar_linkname
[TARLINKNAMESIZE
] = '\0';
363 return hold_tar_linkname
;
366 /* Stash the tar filename and optional prefix in static storage. */
369 stash_tar_filename (prefix
, filename
)
373 static char hold_tar_filename
[TARNAMESIZE
+ TARPREFIXSIZE
+ 2];
374 if (prefix
== NULL
|| *prefix
== '\0')
376 strncpy (hold_tar_filename
, filename
, TARNAMESIZE
);
377 hold_tar_filename
[TARNAMESIZE
] = '\0';
381 strncpy (hold_tar_filename
, prefix
, TARPREFIXSIZE
);
382 hold_tar_filename
[TARPREFIXSIZE
] = '\0';
383 strcat (hold_tar_filename
, "/");
384 strncat (hold_tar_filename
, filename
, TARNAMESIZE
);
385 hold_tar_filename
[TARPREFIXSIZE
+ TARNAMESIZE
] = '\0';
387 return hold_tar_filename
;
390 /* Convert the string of octal digits S into a number and store
391 it in *N. Return nonzero if the whole string was converted,
392 zero if there was something after the number.
393 Skip leading and trailing spaces. */
400 unsigned long val
= 0;
404 while (*s
>= '0' && *s
<= '7')
405 val
= 8 * val
+ *s
++ - '0';
412 /* Convert a number into a string of octal digits.
413 Convert long VALUE into a DIGITS-digit field at WHERE,
414 including a trailing space and room for a NUL. DIGITS==3 means
415 1 digit, a space, and room for a NUL.
417 We assume the trailing NUL is already there and don't fill it in.
418 This fact is used by start_header and finish_header, so don't change it!
420 This is be equivalent to:
421 sprintf (where, "%*lo ", digits - 2, value);
422 except that sprintf fills in the trailing NUL and we don't. */
425 to_oct (value
, digits
, where
)
428 register char *where
;
430 --digits
; /* Leave the trailing NUL slot alone. */
431 where
[--digits
] = ' '; /* Put in the space, though. */
433 /* Produce the digits -- at least one. */
436 where
[--digits
] = '0' + (char) (value
& 7); /* One octal digit. */
439 while (digits
> 0 && value
!= 0);
441 /* Add leading spaces, if necessary. */
443 where
[--digits
] = ' ';
447 2 if BUF is a valid POSIX tar header (the checksum is correct
448 and it has the "ustar" magic string),
449 1 if BUF is a valid old tar header (the checksum is correct),
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 (name
)
494 whole_name_len
= strlen (name
);
495 if (whole_name_len
<= TARNAMESIZE
)
498 if (archive_format
!= arf_ustar
)
501 if (whole_name_len
> TARNAMESIZE
+ TARPREFIXSIZE
+ 1)
504 /* See whether we can split up the name into acceptably-sized
505 `prefix' and `name' (`p') pieces. Start out by making `name'
506 as big as possible, then shrink it by looking for the first '/'. */
507 p
= name
+ whole_name_len
- TARNAMESIZE
;
508 while (*p
!= '/' && *p
!= '\0')
511 /* The last component of the path is longer than TARNAMESIZE. */
514 prefix_name_len
= p
- name
- 1;
515 /* Interestingly, a name consisting of a slash followed by
516 TARNAMESIZE characters can't be stored, because the prefix
517 would be empty, and thus ignored. */
518 if (prefix_name_len
> TARPREFIXSIZE
|| prefix_name_len
== 0)