]>
git.saurik.com Git - apple/file_cmds.git/blob - pax/tar.c
2cc800c6a7ec05e42c736bc5d4057ac2b8a653b6
1 /* $OpenBSD: tar.c,v 1.41 2006/03/04 20:24:55 otto Exp $ */
2 /* $NetBSD: tar.c,v 1.5 1995/03/21 09:07:49 cgd Exp $ */
5 * Copyright (c) 1992 Keith Muller.
6 * Copyright (c) 1992, 1993
7 * The Regents of the University of California. All rights reserved.
9 * This code is derived from software contributed to Berkeley by
10 * Keith Muller of the University of California, San Diego.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 static const char sccsid
[] = "@(#)tar.c 8.2 (Berkeley) 4/18/94";
41 static const char rcsid
[] = "$OpenBSD: tar.c,v 1.41 2006/03/04 20:24:55 otto Exp $";
45 #include <sys/types.h>
48 #include <sys/param.h>
58 * Routines for reading, writing and header identify of various versions of tar
61 static size_t expandname(char *, size_t, char **, const char *, size_t);
62 static u_long
tar_chksm(char *, int);
63 static char *name_split(char *, int);
64 static int ul_oct(u_long
, char *, int, int);
66 static int uqd_oct(u_quad_t
, char *, int, int);
69 static uid_t uid_nobody
;
70 static uid_t uid_warn
;
71 static gid_t gid_nobody
;
72 static gid_t gid_warn
;
75 * Routines common to all versions of tar
78 static int tar_nodir
; /* do not write dirs under old tar */
79 char *gnu_name_string
; /* GNU ././@LongLink hackery name */
80 char *gnu_link_string
; /* GNU ././@LongLink hackery link */
84 * add the tar trailer of two null blocks
86 * 0 if ok, -1 otherwise (what wr_skip returns)
92 return(wr_skip((off_t
)(NULLCNT
*BLKMULT
)));
97 * no cleanup needed here, just return size of trailer (for append)
99 * size of trailer (2 * BLKMULT)
105 return((off_t
)(NULLCNT
*BLKMULT
));
110 * Called to determine if a header block is a valid trailer. We are passed
111 * the block, the in_sync flag (which tells us we are in resync mode;
112 * looking for a valid header), and cnt (which starts at zero) which is
113 * used to count the number of empty blocks we have seen so far.
115 * 0 if a valid trailer, -1 if not a valid trailer, or 1 if the block
116 * could never contain a header.
120 tar_trail(ARCHD
*ignore
, char *buf
, int in_resync
, int *cnt
)
125 * look for all zero, trailer is two consecutive blocks of zero
127 for (i
= 0; i
< BLKMULT
; ++i
) {
133 * if not all zero it is not a trailer, but MIGHT be a header.
139 * When given a zero block, we must be careful!
140 * If we are not in resync mode, check for the trailer. Have to watch
141 * out that we do not mis-identify file data as the trailer, so we do
142 * NOT try to id a trailer during resync mode. During resync mode we
143 * might as well throw this block out since a valid header can NEVER be
144 * a block of all 0 (we must have a valid file name).
146 if (!in_resync
&& (++*cnt
>= NULLCNT
))
153 * convert an unsigned long to an octal string. many oddball field
154 * termination characters are used by the various versions of tar in the
155 * different fields. term selects which kind to use. str is '0' padded
156 * at the front to len. we are unable to use only one format as many old
157 * tar readers are very cranky about this.
159 * 0 if the number fit into the string, -1 otherwise
163 ul_oct(u_long val
, char *str
, int len
, int term
)
168 * term selects the appropriate character(s) for the end of the string
190 * convert and blank pad if there is space
193 *pt
-- = '0' + (char)(val
& 0x7);
194 if ((val
= val
>> 3) == (u_long
)0)
200 if (val
!= (u_long
)0)
208 * convert an u_quad_t to an octal string. one of many oddball field
209 * termination characters are used by the various versions of tar in the
210 * different fields. term selects which kind to use. str is '0' padded
211 * at the front to len. we are unable to use only one format as many old
212 * tar readers are very cranky about this.
214 * 0 if the number fit into the string, -1 otherwise
218 uqd_oct(u_quad_t val
, char *str
, int len
, int term
)
223 * term selects the appropriate character(s) for the end of the string
245 * convert and blank pad if there is space
248 *pt
-- = '0' + (char)(val
& 0x7);
249 if ((val
= val
>> 3) == 0)
255 if (val
!= (u_quad_t
)0)
263 * calculate the checksum for a tar block counting the checksum field as
264 * all blanks (BLNKSUM is that value pre-calculated, the sum of 8 blanks).
265 * NOTE: we use len to short circuit summing 0's on write since we ALWAYS
266 * pad headers with 0.
268 * unsigned long checksum
272 tar_chksm(char *blk
, int len
)
276 u_long chksm
= BLNKSUM
; /* initial value is checksum field sum */
279 * add the part of the block before the checksum field
282 stop
= blk
+ CHK_OFFSET
;
284 chksm
+= (u_long
)(*pt
++ & 0xff);
286 * move past the checksum field and keep going, spec counts the
287 * checksum field as the sum of 8 blanks (which is pre-computed as
289 * ASSUMED: len is greater than CHK_OFFSET. (len is where our 0 padding
290 * starts, no point in summing zero's)
295 chksm
+= (u_long
)(*pt
++ & 0xff);
300 * Routines for old BSD style tar (also made portable to sysV tar)
305 * determine if a block given to us is a valid tar header (and not a USTAR
306 * header). We have to be on the lookout for those pesky blocks of all
309 * 0 if a tar header, -1 otherwise
313 tar_id(char *blk
, int size
)
321 uhd
= (HD_USTAR
*)blk
;
324 * check for block of zero's first, a simple and fast test, then make
325 * sure this is not a ustar header by looking for the ustar magic
326 * cookie. We should use TMAGLEN, but some USTAR archive programs are
327 * wrong and create archives missing the \0. Last we check the
328 * checksum. If this is ok we have to assume it is a valid header.
330 if (hd
->name
[0] == '\0')
332 if (strncmp(uhd
->magic
, TMAGIC
, TMAGLEN
- 1) == 0)
334 if (asc_ul(hd
->chksum
,sizeof(hd
->chksum
),OCT
) != tar_chksm(blk
,BLKMULT
))
336 force_one_volume
= 1;
342 * handle tar format specific -o options
344 * 0 if ok -1 otherwise
352 while ((opt
= opt_next()) != NULL
) {
353 if (strcmp(opt
->name
, TAR_OPTION
) ||
354 strcmp(opt
->value
, TAR_NODIR
)) {
355 paxwarn(1, "Unknown tar format -o option/value pair %s=%s",
356 opt
->name
, opt
->value
);
357 paxwarn(1,"%s=%s is the only supported tar format option",
358 TAR_OPTION
, TAR_NODIR
);
363 * we only support one option, and only when writing
365 if ((act
!= APPND
) && (act
!= ARCHIVE
)) {
366 paxwarn(1, "%s=%s is only supported when writing.",
367 opt
->name
, opt
->value
);
378 * extract the values out of block already determined to be a tar header.
379 * store the values in the ARCHD parameter.
385 tar_rd(ARCHD
*arcn
, char *buf
)
391 * we only get proper sized buffers passed to us
393 if (tar_id(buf
, BLKMULT
) < 0)
395 memset(arcn
, 0, sizeof(*arcn
));
396 arcn
->org_name
= arcn
->name
;
397 arcn
->sb
.st_nlink
= 1;
400 * copy out the name and values in the stat buffer
403 if (hd
->linkflag
!= LONGLINKTYPE
&& hd
->linkflag
!= LONGNAMETYPE
) {
404 arcn
->nlen
= expandname(arcn
->name
, sizeof(arcn
->name
),
405 &gnu_name_string
, hd
->name
, sizeof(hd
->name
));
406 arcn
->ln_nlen
= expandname(arcn
->ln_name
, sizeof(arcn
->ln_name
),
407 &gnu_link_string
, hd
->linkname
, sizeof(hd
->linkname
));
409 arcn
->sb
.st_mode
= (mode_t
)(asc_ul(hd
->mode
,sizeof(hd
->mode
),OCT
) &
411 arcn
->sb
.st_uid
= (uid_t
)asc_ul(hd
->uid
, sizeof(hd
->uid
), OCT
);
412 arcn
->sb
.st_gid
= (gid_t
)asc_ul(hd
->gid
, sizeof(hd
->gid
), OCT
);
414 arcn
->sb
.st_size
= (off_t
)asc_ul(hd
->size
, sizeof(hd
->size
), OCT
);
416 arcn
->sb
.st_size
= (off_t
)asc_uqd(hd
->size
, sizeof(hd
->size
), OCT
);
418 arcn
->sb
.st_mtime
= (time_t)asc_ul(hd
->mtime
, sizeof(hd
->mtime
), OCT
);
419 arcn
->sb
.st_ctime
= arcn
->sb
.st_atime
= arcn
->sb
.st_mtime
;
422 * have to look at the last character, it may be a '/' and that is used
423 * to encode this as a directory
425 pt
= &(arcn
->name
[arcn
->nlen
- 1]);
428 switch (hd
->linkflag
) {
431 * symbolic link, need to get the link name and set the type in
432 * the st_mode so -v printing will look correct.
434 arcn
->type
= PAX_SLK
;
435 arcn
->sb
.st_mode
|= S_IFLNK
;
436 arcn
->ln_nlen
= strlcpy(arcn
->ln_name
, hd
->linkname
, sizeof(arcn
->ln_name
));
440 * hard link, need to get the link name, set the type in the
441 * st_mode and st_nlink so -v printing will look better.
443 arcn
->type
= PAX_HLK
;
444 arcn
->sb
.st_nlink
= 2;
445 arcn
->ln_nlen
= strlcpy(arcn
->ln_name
, hd
->linkname
, sizeof(arcn
->ln_name
));
448 * no idea of what type this thing really points at, but
449 * we set something for printing only.
451 arcn
->sb
.st_mode
|= S_IFREG
;
456 * GNU long link/file; we tag these here and let the
457 * pax internals deal with it -- too ugly otherwise.
460 hd
->linkflag
== LONGLINKTYPE
? PAX_GLL
: PAX_GLF
;
461 arcn
->pad
= TAR_PAD(arcn
->sb
.st_size
);
462 arcn
->skip
= arcn
->sb
.st_size
;
466 * It is a directory, set the mode for -v printing
468 arcn
->type
= PAX_DIR
;
469 arcn
->sb
.st_mode
|= S_IFDIR
;
470 arcn
->sb
.st_nlink
= 2;
476 * If we have a trailing / this is a directory and NOT a file.
478 arcn
->ln_name
[0] = '\0';
482 * it is a directory, set the mode for -v printing
484 arcn
->type
= PAX_DIR
;
485 arcn
->sb
.st_mode
|= S_IFDIR
;
486 arcn
->sb
.st_nlink
= 2;
489 * have a file that will be followed by data. Set the
490 * skip value to the size field and calculate the size
493 arcn
->type
= PAX_REG
;
494 arcn
->sb
.st_mode
|= S_IFREG
;
495 arcn
->pad
= TAR_PAD(arcn
->sb
.st_size
);
496 arcn
->skip
= arcn
->sb
.st_size
;
502 * strip off any trailing slash.
513 * write a tar header for the file specified in the ARCHD to the archive.
514 * Have to check for file types that cannot be stored and file names that
515 * are too long. Be careful of the term (last arg) to ul_oct, each field
516 * of tar has it own spec for the termination character(s).
517 * ASSUMED: space after header in header block is zero filled
519 * 0 if file has data to be written after the header, 1 if file has NO
520 * data to write after the header, -1 if archive write failed
531 * check for those file system types which tar cannot store
533 switch (arcn
->type
) {
536 * user asked that dirs not be written to the archive
542 paxwarn(1, "Tar cannot archive a character device %s",
546 paxwarn(1, "Tar cannot archive a block device %s", arcn
->org_name
);
549 paxwarn(1, "Tar cannot archive a socket %s", arcn
->org_name
);
552 paxwarn(1, "Tar cannot archive a fifo %s", arcn
->org_name
);
557 if (arcn
->ln_nlen
>= sizeof(hd
->linkname
)) {
558 paxwarn(1, "Link name too long for tar %s",
570 * check file name len, remember extra char for dirs (the / at the end)
573 if (arcn
->type
== PAX_DIR
)
575 if (len
>= sizeof(hd
->name
)) {
576 paxwarn(1, "File name too long for tar %s", arcn
->name
);
581 * Copy the data out of the ARCHD into the tar header based on the type
582 * of the file. Remember, many tar readers want all fields to be
583 * padded with zero so we zero the header first. We then set the
584 * linkflag field (type), the linkname, the size, and set the padding
585 * (if any) to be added after the file data (0 for all other types,
586 * as they only have a header).
588 memset(&hdblk
, 0, sizeof(hdblk
));
589 hd
= (HD_TAR
*)&hdblk
;
590 strlcpy(hd
->name
, arcn
->name
, sizeof(hd
->name
));
593 if (arcn
->type
== PAX_DIR
) {
595 * directories are the same as files, except have a filename
596 * that ends with a /, we add the slash here. No data follows
599 hd
->linkflag
= AREGTYPE
;
600 hd
->name
[len
-1] = '/';
601 if (ul_oct((u_long
)0L, hd
->size
, sizeof(hd
->size
), 1))
603 } else if (arcn
->type
== PAX_SLK
) {
605 * no data follows this file, so no pad
607 hd
->linkflag
= SYMTYPE
;
608 strlcpy(hd
->linkname
, arcn
->ln_name
, sizeof(hd
->linkname
));
609 if (ul_oct((u_long
)0L, hd
->size
, sizeof(hd
->size
), 1))
611 } else if ((arcn
->type
== PAX_HLK
) || (arcn
->type
== PAX_HRG
)) {
613 * no data follows this file, so no pad
615 hd
->linkflag
= LNKTYPE
;
616 strlcpy(hd
->linkname
, arcn
->ln_name
, sizeof(hd
->linkname
));
617 if (ul_oct((u_long
)0L, hd
->size
, sizeof(hd
->size
), 1))
621 * data follows this file, so set the pad
623 hd
->linkflag
= AREGTYPE
;
625 if (ul_oct((u_long
)arcn
->sb
.st_size
, hd
->size
,
626 sizeof(hd
->size
), 1)) {
628 if (uqd_oct((u_quad_t
)arcn
->sb
.st_size
, hd
->size
,
629 sizeof(hd
->size
), 1)) {
631 paxwarn(1,"File is too large for tar %s", arcn
->org_name
);
634 arcn
->pad
= TAR_PAD(arcn
->sb
.st_size
);
638 * copy those fields that are independent of the type
640 if (ul_oct((u_long
)arcn
->sb
.st_mode
, hd
->mode
, sizeof(hd
->mode
), 0) ||
641 ul_oct((u_long
)arcn
->sb
.st_uid
, hd
->uid
, sizeof(hd
->uid
), 0) ||
642 ul_oct((u_long
)arcn
->sb
.st_gid
, hd
->gid
, sizeof(hd
->gid
), 0) ||
643 ul_oct((u_long
)arcn
->sb
.st_mtime
, hd
->mtime
, sizeof(hd
->mtime
), 1))
647 * calculate and add the checksum, then write the header. A return of
648 * 0 tells the caller to now write the file data, 1 says no data needs
651 if (ul_oct(tar_chksm((char *)&hdblk
, sizeof(HD_TAR
)), hd
->chksum
,
652 sizeof(hd
->chksum
), 3))
654 if (wr_rdbuf((char *)&hdblk
, sizeof(HD_TAR
)) < 0)
656 if (wr_skip((off_t
)(BLKMULT
- sizeof(HD_TAR
))) < 0)
658 if ((arcn
->type
== PAX_CTG
) || (arcn
->type
== PAX_REG
))
664 * header field is out of range
666 paxwarn(1, "Tar header field is too small for %s", arcn
->org_name
);
671 * Routines for POSIX ustar
676 * initialization for ustar read
678 * 0 if ok, -1 otherwise
684 if ((usrtb_start() < 0) || (grptb_start() < 0))
691 * initialization for ustar write
693 * 0 if ok, -1 otherwise
699 if ((uidtb_start() < 0) || (gidtb_start() < 0))
706 * determine if a block given to us is a valid ustar header. We have to
707 * be on the lookout for those pesky blocks of all zero's
709 * 0 if a ustar header, -1 otherwise
713 ustar_id(char *blk
, int size
)
719 hd
= (HD_USTAR
*)blk
;
722 * check for block of zero's first, a simple and fast test then check
723 * ustar magic cookie. We should use TMAGLEN, but some USTAR archive
724 * programs are fouled up and create archives missing the \0. Last we
725 * check the checksum. If ok we have to assume it is a valid header.
727 if (hd
->name
[0] == '\0')
729 if (strncmp(hd
->magic
, TMAGIC
, TMAGLEN
- 1) != 0)
731 if (asc_ul(hd
->chksum
,sizeof(hd
->chksum
),OCT
) != tar_chksm(blk
,BLKMULT
))
738 * extract the values out of block already determined to be a ustar header.
739 * store the values in the ARCHD parameter.
745 ustar_rd(ARCHD
*arcn
, char *buf
)
754 * we only get proper sized buffers
756 if (ustar_id(buf
, BLKMULT
) < 0)
758 memset(arcn
, 0, sizeof(*arcn
));
759 arcn
->org_name
= arcn
->name
;
760 arcn
->sb
.st_nlink
= 1;
761 hd
= (HD_USTAR
*)buf
;
764 * see if the filename is split into two parts. if, so joint the parts.
765 * we copy the prefix first and add a / between the prefix and name.
768 if (*(hd
->prefix
) != '\0') {
769 cnt
= strlcpy(dest
, hd
->prefix
, sizeof(arcn
->name
) - 1);
777 if (hd
->typeflag
!= LONGLINKTYPE
&& hd
->typeflag
!= LONGNAMETYPE
) {
778 arcn
->nlen
= cnt
+ expandname(dest
, sizeof(arcn
->name
) - cnt
,
779 &gnu_name_string
, hd
->name
, sizeof(hd
->name
));
780 arcn
->ln_nlen
= expandname(arcn
->ln_name
, sizeof(arcn
->ln_name
),
781 &gnu_link_string
, hd
->linkname
, sizeof(hd
->linkname
));
785 * follow the spec to the letter. we should only have mode bits, strip
786 * off all other crud we may be passed.
788 arcn
->sb
.st_mode
= (mode_t
)(asc_ul(hd
->mode
, sizeof(hd
->mode
), OCT
) &
791 arcn
->sb
.st_size
= (off_t
)asc_ul(hd
->size
, sizeof(hd
->size
), OCT
);
793 arcn
->sb
.st_size
= (off_t
)asc_uqd(hd
->size
, sizeof(hd
->size
), OCT
);
795 arcn
->sb
.st_mtime
= (time_t)asc_ul(hd
->mtime
, sizeof(hd
->mtime
), OCT
);
796 arcn
->sb
.st_ctime
= arcn
->sb
.st_atime
= arcn
->sb
.st_mtime
;
799 * If we can find the ascii names for gname and uname in the password
800 * and group files we will use the uid's and gid they bind. Otherwise
801 * we use the uid and gid values stored in the header. (This is what
802 * the POSIX spec wants).
804 hd
->gname
[sizeof(hd
->gname
) - 1] = '\0';
805 if (gid_name(hd
->gname
, &(arcn
->sb
.st_gid
)) < 0)
806 arcn
->sb
.st_gid
= (gid_t
)asc_ul(hd
->gid
, sizeof(hd
->gid
), OCT
);
807 hd
->uname
[sizeof(hd
->uname
) - 1] = '\0';
808 if (uid_name(hd
->uname
, &(arcn
->sb
.st_uid
)) < 0)
809 arcn
->sb
.st_uid
= (uid_t
)asc_ul(hd
->uid
, sizeof(hd
->uid
), OCT
);
812 * set the defaults, these may be changed depending on the file type
816 arcn
->sb
.st_rdev
= (dev_t
)0;
819 * set the mode and PAX type according to the typeflag in the header
821 switch (hd
->typeflag
) {
823 arcn
->type
= PAX_FIF
;
824 arcn
->sb
.st_mode
|= S_IFIFO
;
827 arcn
->type
= PAX_DIR
;
828 arcn
->sb
.st_mode
|= S_IFDIR
;
829 arcn
->sb
.st_nlink
= 2;
832 * Some programs that create ustar archives append a '/'
833 * to the pathname for directories. This clearly violates
834 * ustar specs, but we will silently strip it off anyway.
836 if (arcn
->name
[arcn
->nlen
- 1] == '/')
837 arcn
->name
[--arcn
->nlen
] = '\0';
842 * this type requires the rdev field to be set.
844 if (hd
->typeflag
== BLKTYPE
) {
845 arcn
->type
= PAX_BLK
;
846 arcn
->sb
.st_mode
|= S_IFBLK
;
848 arcn
->type
= PAX_CHR
;
849 arcn
->sb
.st_mode
|= S_IFCHR
;
851 devmajor
= (dev_t
)asc_ul(hd
->devmajor
,sizeof(hd
->devmajor
),OCT
);
852 devminor
= (dev_t
)asc_ul(hd
->devminor
,sizeof(hd
->devminor
),OCT
);
853 arcn
->sb
.st_rdev
= TODEV(devmajor
, devminor
);
857 if (hd
->typeflag
== SYMTYPE
) {
858 arcn
->type
= PAX_SLK
;
859 arcn
->sb
.st_mode
|= S_IFLNK
;
861 arcn
->type
= PAX_HLK
;
863 * so printing looks better
865 arcn
->sb
.st_mode
|= S_IFREG
;
866 arcn
->sb
.st_nlink
= 2;
872 * GNU long link/file; we tag these here and let the
873 * pax internals deal with it -- too ugly otherwise.
876 hd
->typeflag
== LONGLINKTYPE
? PAX_GLL
: PAX_GLF
;
877 arcn
->pad
= TAR_PAD(arcn
->sb
.st_size
);
878 arcn
->skip
= arcn
->sb
.st_size
;
885 * these types have file data that follows. Set the skip and
888 arcn
->type
= PAX_REG
;
889 arcn
->pad
= TAR_PAD(arcn
->sb
.st_size
);
890 arcn
->skip
= arcn
->sb
.st_size
;
891 arcn
->sb
.st_mode
|= S_IFREG
;
899 * write a ustar header for the file specified in the ARCHD to the archive
900 * Have to check for file types that cannot be stored and file names that
901 * are too long. Be careful of the term (last arg) to ul_oct, we only use
902 * '\0' for the termination character (this is different than picky tar)
903 * ASSUMED: space after header in header block is zero filled
905 * 0 if file has data to be written after the header, 1 if file has NO
906 * data to write after the header, -1 if archive write failed
910 ustar_wr(ARCHD
*arcn
)
914 char hdblk
[sizeof(HD_USTAR
)];
916 int term_char
=3; /* orignal setting */
917 term_char
=1; /* To pass conformance tests 274, 301 */
920 * check for those file system types ustar cannot store
922 if (arcn
->type
== PAX_SCK
) {
923 paxwarn(1, "Ustar cannot archive a socket %s", arcn
->org_name
);
928 * check the length of the linkname
930 if (((arcn
->type
== PAX_SLK
) || (arcn
->type
== PAX_HLK
) ||
931 (arcn
->type
== PAX_HRG
)) && (arcn
->ln_nlen
> sizeof(hd
->linkname
))){
932 paxwarn(1, "Link name too long for ustar %s", arcn
->ln_name
);
934 * Conformance: test pax:285 wants error code to be non-zero, and
935 * test tar:12 wants error code from pax to be 0
941 * split the path name into prefix and name fields (if needed). if
942 * pt != arcn->name, the name has to be split
944 if ((pt
= name_split(arcn
->name
, arcn
->nlen
)) == NULL
) {
945 paxwarn(1, "File name too long for ustar %s", arcn
->name
);
950 * zero out the header so we don't have to worry about zero fill below
952 memset(hdblk
, 0, sizeof(hdblk
));
953 hd
= (HD_USTAR
*)hdblk
;
956 /* To pass conformance tests 274/301, always set these fields to "zero" */
957 ul_oct(0, hd
->devmajor
, sizeof(hd
->devmajor
), term_char
);
958 ul_oct(0, hd
->devminor
, sizeof(hd
->devminor
), term_char
);
961 * split the name, or zero out the prefix
963 if (pt
!= arcn
->name
) {
965 * name was split, pt points at the / where the split is to
966 * occur, we remove the / and copy the first part to the prefix
969 strlcpy(hd
->prefix
, arcn
->name
, sizeof(hd
->prefix
));
974 * copy the name part. this may be the whole path or the part after
975 * the prefix. both the name and prefix may fill the entire field.
977 if (strlen(pt
) == sizeof(hd
->name
)) { /* must account for name just fits in buffer */
978 strncpy(hd
->name
, pt
, sizeof(hd
->name
));
980 strlcpy(hd
->name
, pt
, sizeof(hd
->name
));
984 * set the fields in the header that are type dependent
986 switch (arcn
->type
) {
988 hd
->typeflag
= DIRTYPE
;
989 if (ul_oct((u_long
)0L, hd
->size
, sizeof(hd
->size
), term_char
))
994 if (arcn
->type
== PAX_CHR
)
995 hd
->typeflag
= CHRTYPE
;
997 hd
->typeflag
= BLKTYPE
;
998 if (ul_oct((u_long
)MAJOR(arcn
->sb
.st_rdev
), hd
->devmajor
,
999 sizeof(hd
->devmajor
), term_char
) ||
1000 ul_oct((u_long
)MINOR(arcn
->sb
.st_rdev
), hd
->devminor
,
1001 sizeof(hd
->devminor
), term_char
) ||
1002 ul_oct((u_long
)0L, hd
->size
, sizeof(hd
->size
), term_char
))
1006 hd
->typeflag
= FIFOTYPE
;
1007 if (ul_oct((u_long
)0L, hd
->size
, sizeof(hd
->size
), term_char
))
1013 if (arcn
->type
== PAX_SLK
)
1014 hd
->typeflag
= SYMTYPE
;
1016 hd
->typeflag
= LNKTYPE
;
1017 if (strlen(arcn
->ln_name
) == sizeof(hd
->linkname
)) { /* must account for name just fits in buffer */
1018 strncpy(hd
->linkname
, arcn
->ln_name
, sizeof(hd
->linkname
));
1020 strlcpy(hd
->linkname
, arcn
->ln_name
, sizeof(hd
->linkname
));
1022 if (ul_oct((u_long
)0L, hd
->size
, sizeof(hd
->size
), term_char
))
1029 * file data with this type, set the padding
1031 if (arcn
->type
== PAX_CTG
)
1032 hd
->typeflag
= CONTTYPE
;
1034 hd
->typeflag
= REGTYPE
;
1035 arcn
->pad
= TAR_PAD(arcn
->sb
.st_size
);
1037 if (ul_oct((u_long
)arcn
->sb
.st_size
, hd
->size
,
1038 sizeof(hd
->size
), term_char
)) {
1040 if (uqd_oct((u_quad_t
)arcn
->sb
.st_size
, hd
->size
,
1041 sizeof(hd
->size
), term_char
)) {
1043 paxwarn(1,"File is too long for ustar %s",arcn
->org_name
);
1049 strncpy(hd
->magic
, TMAGIC
, TMAGLEN
);
1050 strncpy(hd
->version
, TVERSION
, TVERSLEN
);
1053 * set the remaining fields. Some versions want all 16 bits of mode
1054 * we better humor them (they really do not meet spec though)....
1056 if (ul_oct((u_long
)arcn
->sb
.st_uid
, hd
->uid
, sizeof(hd
->uid
), term_char
)) {
1057 if (uid_nobody
== 0) {
1058 if (uid_name("nobody", &uid_nobody
) == -1)
1061 if (uid_warn
!= arcn
->sb
.st_uid
) {
1062 uid_warn
= arcn
->sb
.st_uid
;
1064 "Ustar header field is too small for uid %lu, "
1065 "using nobody", (u_long
)arcn
->sb
.st_uid
);
1067 if (ul_oct((u_long
)uid_nobody
, hd
->uid
, sizeof(hd
->uid
), term_char
))
1070 if (ul_oct((u_long
)arcn
->sb
.st_gid
, hd
->gid
, sizeof(hd
->gid
), term_char
)) {
1071 if (gid_nobody
== 0) {
1072 if (gid_name("nobody", &gid_nobody
) == -1)
1075 if (gid_warn
!= arcn
->sb
.st_gid
) {
1076 gid_warn
= arcn
->sb
.st_gid
;
1078 "Ustar header field is too small for gid %lu, "
1079 "using nobody", (u_long
)arcn
->sb
.st_gid
);
1081 if (ul_oct((u_long
)gid_nobody
, hd
->gid
, sizeof(hd
->gid
), term_char
))
1084 /* However, Unix conformance tests do not like MORE than 12 mode bits:
1085 remove all beyond (see definition of stat.st_mode structure) */
1086 mode12only
= ((u_long
)arcn
->sb
.st_mode
) & 0x00000fff;
1087 if (ul_oct((u_long
)mode12only
, hd
->mode
, sizeof(hd
->mode
), term_char
) ||
1088 ul_oct((u_long
)arcn
->sb
.st_mtime
,hd
->mtime
,sizeof(hd
->mtime
),term_char
))
1090 strncpy(hd
->uname
, name_uid(arcn
->sb
.st_uid
, 0), sizeof(hd
->uname
));
1091 strncpy(hd
->gname
, name_gid(arcn
->sb
.st_gid
, 0), sizeof(hd
->gname
));
1094 * calculate and store the checksum write the header to the archive
1095 * return 0 tells the caller to now write the file data, 1 says no data
1096 * needs to be written
1098 if (ul_oct(tar_chksm(hdblk
, sizeof(HD_USTAR
)), hd
->chksum
,
1099 sizeof(hd
->chksum
), term_char
))
1101 if (wr_rdbuf((char *)&hdblk
, sizeof(HD_USTAR
)) < 0)
1103 if (wr_skip((off_t
)(BLKMULT
- sizeof(HD_USTAR
))) < 0)
1105 if ((arcn
->type
== PAX_CTG
) || (arcn
->type
== PAX_REG
))
1111 * header field is out of range
1113 paxwarn(1, "Ustar header field is too small for %s", arcn
->org_name
);
1119 * see if the name has to be split for storage in a ustar header. We try
1120 * to fit the entire name in the name field without splitting if we can.
1121 * The split point is always at a /
1123 * character pointer to split point (always the / that is to be removed
1124 * if the split is not needed, the points is set to the start of the file
1125 * name (it would violate the spec to split there). A NULL is returned if
1126 * the file name is too long
1130 name_split(char *name
, int len
)
1135 * check to see if the file name is small enough to fit in the name
1136 * field. if so just return a pointer to the name.
1137 * The strings can fill the complete name and prefix fields
1138 * without a NUL terminator.
1142 if (len
> (TPFSZ
+ TNMSZ
+ 1))
1146 * we start looking at the biggest sized piece that fits in the name
1147 * field. We walk forward looking for a slash to split at. The idea is
1148 * to find the biggest piece to fit in the name field (or the smallest
1149 * prefix we can find) (the -1 is correct the biggest piece would
1150 * include the slash between the two parts that gets thrown away)
1152 start
= name
+ len
- TNMSZ
- 1;
1153 if ((*start
== '/') && (start
== name
))
1154 ++start
; /* 101 byte paths with leading '/' are dinged otherwise */
1155 while ((*start
!= '\0') && (*start
!= '/'))
1159 * if we hit the end of the string, this name cannot be split, so we
1160 * cannot store this file.
1167 * NOTE: /str where the length of str == TNMSZ can not be stored under
1168 * the p1003.1-1990 spec for ustar. We could force a prefix of / and
1169 * the file would then expand on extract to //str. The len == 0 below
1170 * makes this special case follow the spec to the letter.
1172 if ((len
> TPFSZ
) || (len
== 0))
1176 * ok have a split point, return it to the caller
1182 expandname(char *buf
, size_t len
, char **gnu_name
, const char *name
,
1188 /* *gnu_name is NUL terminated */
1189 if ((nlen
= strlcpy(buf
, *gnu_name
, len
)) >= len
)
1194 if (name_len
< len
) {
1195 /* name may not be null terminated: it might be as big as the
1196 field, so copy is limited to the max size of the header field */
1197 if ((nlen
= strlcpy(buf
, name
, name_len
+1)) >= name_len
+1)
1200 if ((nlen
= strlcpy(buf
, name
, len
)) >= len
)