* Copyright (c) 1992 Keith Muller.
* Copyright (c) 1992, 1993
* The Regents of the University of California. All rights reserved.
* This code is derived from software contributed to Berkeley by
* Keith Muller of the University of California, San Diego.
* %sccs.include.redist.c%
static char sccsid
[] = "@(#)tar.c 8.1 (Berkeley) %G%";
* Routines for reading, writing and header identify of various versions of tar
static u_long tar_chksm
__P((register char *, register int));
static char *name_split
__P((register char *, register int));
static int ul_oct
__P((u_long
, register char *, register int, int));
static int uqd_oct
__P((u_quad_t
, register char *, register int, int));
* Routines common to all versions of tar
static int tar_nodir
; /* do not write dirs under old tar */
* add the tar trailer of two null blocks
* 0 if ok, -1 otherwise (what wr_skip returns)
return(wr_skip((off_t
)(NULLCNT
*BLKMULT
)));
* no cleanup needed here, just return size of trailer (for append)
* size of trailer (2 * BLKMULT)
return((off_t
)(NULLCNT
*BLKMULT
));
* Called to determine if a header block is a valid trailer. We are passed
* the block, the in_sync flag (which tells us we are in resync mode;
* looking for a valid header), and cnt (which starts at zero) which is
* used to count the number of empty blocks we have seen so far.
* 0 if a valid trailer, -1 if not a valid trailer, or 1 if the block
* could never contain a header.
tar_trail(register char *buf
, register int in_resync
, register int *cnt
)
tar_trail(buf
, in_resync
, cnt
)
* look for all zero, trailer is two consecutive blocks of zero
for (i
= 0; i
< BLKMULT
; ++i
) {
* if not all zero it is not a trailer, but MIGHT be a header.
* When given a zero block, we must be careful!
* If we are not in resync mode, check for the trailer. Have to watch
* out that we do not mis-identify file data as the trailer, so we do
* NOT try to id a trailer during resync mode. During resync mode we
* might as well throw this block out since a valid header can NEVER be
* a block of all 0 (we must have a valid file name).
if (!in_resync
&& (++*cnt
>= NULLCNT
))
* convert an unsigned long to an octal string. many oddball field
* termination characters are used by the various versions of tar in the
* different fields. term selects which kind to use. str is BLANK padded
* at the front to len. we are unable to use only one format as many old
* tar readers are very cranky about this.
* 0 if the number fit into the string, -1 otherwise
ul_oct(u_long val
, register char *str
, register int len
, int term
)
ul_oct(val
, str
, len
, term
)
* term selects the appropriate character(s) for the end of the string
* convert and blank pad if there is space
*pt
-- = '0' + (char)(val
& 0x7);
if ((val
= val
>> 3) == (u_long
)0)
* convert an u_quad_t to an octal string. one of many oddball field
* termination characters are used by the various versions of tar in the
* different fields. term selects which kind to use. str is BLANK padded
* at the front to len. we are unable to use only one format as many old
* tar readers are very cranky about this.
* 0 if the number fit into the string, -1 otherwise
uqd_oct(u_quad_t val
, register char *str
, register int len
, int term
)
uqd_oct(val
, str
, len
, term
)
* term selects the appropriate character(s) for the end of the string
* convert and blank pad if there is space
*pt
-- = '0' + (char)(val
& 0x7);
if ((val
= val
>> 3) == 0)
* calculate the checksum for a tar block counting the checksum field as
* all blanks (BLNKSUM is that value pre-calculated, the sume of 8 blanks).
* NOTE: we use len to short circuit summing 0's on write since we ALWAYS
tar_chksm(register char *blk
, register int len
)
u_long chksm
= BLNKSUM
; /* inital value is checksum field sum */
* add the part of the block before the checksum field
chksm
+= (u_long
)(*pt
++ & 0xff);
* move past the checksum field and keep going, spec counts the
* checksum field as the sum of 8 blanks (which is pre-computed as
* ASSUMED: len is greater than CHK_OFFSET. (len is where our 0 padding
* starts, no point in summing zero's)
chksm
+= (u_long
)(*pt
++ & 0xff);
* Routines for old BSD style tar (also made portable to sysV tar)
* determine if a block given to us is a valid tar header (and not a USTAR
* header). We have to be on the lookout for those pesky blocks of all
* 0 if a tar header, -1 otherwise
tar_id(register char *blk
, int size
)
* check for block of zero's first, a simple and fast test, then make
* sure this is not a ustar header by looking for the ustar magic
* cookie. We should use TMAGLEN, but some USTAR archive programs are
* wrong and create archives missing the \0. Last we check the
* checksum. If this is ok we have to assume it is a valid header.
if (strncmp(uhd
->magic
, TMAGIC
, TMAGLEN
- 1) == 0)
if (asc_ul(hd
->chksum
,sizeof(hd
->chksum
),OCT
) != tar_chksm(blk
,BLKMULT
))
* handle tar format specific -o options
while ((opt
= opt_next()) != NULL
) {
if (strcmp(opt
->name
, TAR_OPTION
) ||
strcmp(opt
->value
, TAR_NODIR
)) {
warn(1, "Unknown tar format -o option/value pair %s=%s",
warn(1,"%s=%s is the only supported tar format option",
* we only support one option, and only when writing
if ((act
!= APPND
) && (act
!= ARCHIVE
)) {
warn(1, "%s=%s is only supported when writing.",
* extract the values out of block already determined to be a tar header.
* store the values in the ARCHD parameter.
tar_rd(register ARCHD
*arcn
, register char *buf
)
* we only get proper sized buffers passed to us
if (tar_id(buf
, BLKMULT
) < 0)
arcn
->org_name
= arcn
->name
;
* copy out the name and values in the stat buffer
arcn
->nlen
= l_strncpy(arcn
->name
, hd
->name
, sizeof(hd
->name
));
arcn
->name
[arcn
->nlen
] = '\0';
arcn
->sb
.st_mode
= (mode_t
)(asc_ul(hd
->mode
,sizeof(hd
->mode
),OCT
) &
arcn
->sb
.st_uid
= (uid_t
)asc_ul(hd
->uid
, sizeof(hd
->uid
), OCT
);
arcn
->sb
.st_gid
= (gid_t
)asc_ul(hd
->gid
, sizeof(hd
->gid
), OCT
);
arcn
->sb
.st_size
= (size_t)asc_ul(hd
->size
, sizeof(hd
->size
), OCT
);
arcn
->sb
.st_mtime
= (time_t)asc_ul(hd
->mtime
, sizeof(hd
->mtime
), OCT
);
arcn
->sb
.st_ctime
= arcn
->sb
.st_atime
= arcn
->sb
.st_mtime
;
* have to look at the last character, it may be a '/' and that is used
* to encode this as a directory
pt
= &(arcn
->name
[arcn
->nlen
- 1]);
* symbolic link, need to get the link name and set the type in
* the st_mode so -v printing will look correct.
arcn
->ln_nlen
= l_strncpy(arcn
->ln_name
, hd
->linkname
,
arcn
->ln_name
[arcn
->ln_nlen
] = '\0';
arcn
->sb
.st_mode
|= S_IFLNK
;
* hard link, need to get the link name, set the type in the
* st_mode and st_nlink so -v printing will look better.
arcn
->ln_nlen
= l_strncpy(arcn
->ln_name
, hd
->linkname
,
arcn
->ln_name
[arcn
->ln_nlen
] = '\0';
* no idea of what type this thing really points at, but
* we set something for printing only.
arcn
->sb
.st_mode
|= S_IFREG
;
* If we have a trailing / this is a directory and NOT a file.
* it is a directory, set the mode for -v printing
arcn
->sb
.st_mode
|= S_IFDIR
;
* have a file that will be followed by data. Set the
* skip value to the size field and caluculate the size
arcn
->sb
.st_mode
|= S_IFREG
;
arcn
->pad
= TAR_PAD(arcn
->sb
.st_size
);
arcn
->skip
= arcn
->sb
.st_size
;
* strip off any trailing slash.
* write a tar header for the file specified in the ARCHD to the archive.
* Have to check for file types that cannot be stored and file names that
* are too long. Be careful of the term (last arg) to ul_oct, each field
* of tar has it own spec for the termination character(s).
* ASSUMED: space after header in header block is zero filled
* 0 if file has data to be written after the header, 1 if file has NO
* data to write after the header, -1 if archive write failed
tar_wr(register ARCHD
*arcn
)
char hdblk
[sizeof(HD_TAR
)];
* check for those file system types which tar cannot store
* user asked that dirs not be written to the archive
warn(1, "Tar cannot archive a character device %s",
warn(1, "Tar cannot archive a block device %s", arcn
->org_name
);
warn(1, "Tar cannot archive a socket %s", arcn
->org_name
);
warn(1, "Tar cannot archive a fifo %s", arcn
->org_name
);
if (arcn
->ln_nlen
> sizeof(hd
->linkname
)) {
warn(1,"Link name too long for tar %s", arcn
->ln_name
);
* check file name len, remember extra char for dirs (the / at the end)
if (arcn
->type
== PAX_DIR
)
if (len
> sizeof(hd
->name
)) {
warn(1, "File name too long for tar %s", arcn
->name
);
* copy the data out of the ARCHD into the tar header based on the type
* of the file. Remember many tar readers want the unused fields to be
* padded with zero. We set the linkflag field (type), the linkname
* (or zero if not used),the size, and set the padding (if any) to be
* added after the file data (0 for all other types, as they only have
zf_strncpy(hd
->name
, arcn
->name
, sizeof(hd
->name
));
if (arcn
->type
== PAX_DIR
) {
* directories are the same as files, except have a filename
* that ends with a /, we add the slash here. No data follows,
bzero(hd
->linkname
, sizeof(hd
->linkname
));
if (ul_oct((u_long
)0L, hd
->size
, sizeof(hd
->size
), 1))
} else if (arcn
->type
== PAX_SLK
) {
* no data follows this file, so no pad
zf_strncpy(hd
->linkname
,arcn
->ln_name
, sizeof(hd
->linkname
));
if (ul_oct((u_long
)0L, hd
->size
, sizeof(hd
->size
), 1))
} else if ((arcn
->type
== PAX_HLK
) || (arcn
->type
== PAX_HRG
)) {
* no data follows this file, so no pad
zf_strncpy(hd
->linkname
,arcn
->ln_name
, sizeof(hd
->linkname
));
if (ul_oct((u_long
)0L, hd
->size
, sizeof(hd
->size
), 1))
* data follows this file, so set the pad
bzero(hd
->linkname
, sizeof(hd
->linkname
));
if (ul_oct((u_long
)arcn
->sb
.st_size
, hd
->size
,
if (uqd_oct((u_quad_t
)arcn
->sb
.st_size
, hd
->size
,
warn(1,"File is too large for tar %s", arcn
->org_name
);
arcn
->pad
= TAR_PAD(arcn
->sb
.st_size
);
* copy those fields that are independent of the type
if (ul_oct((u_long
)arcn
->sb
.st_mode
, hd
->mode
, sizeof(hd
->mode
), 0) ||
ul_oct((u_long
)arcn
->sb
.st_uid
, hd
->uid
, sizeof(hd
->uid
), 0) ||
ul_oct((u_long
)arcn
->sb
.st_gid
, hd
->gid
, sizeof(hd
->gid
), 0) ||
ul_oct((u_long
)arcn
->sb
.st_mtime
, hd
->mtime
, sizeof(hd
->mtime
), 1))
* calculate and add the checksum, then write the header. A return of
* 0 tells the caller to now write the file data, 1 says no data needs
if (ul_oct(tar_chksm(hdblk
, sizeof(HD_TAR
)), hd
->chksum
,
if (wr_rdbuf(hdblk
, sizeof(HD_TAR
)) < 0)
if (wr_skip((off_t
)(BLKMULT
- sizeof(HD_TAR
))) < 0)
if ((arcn
->type
== PAX_CTG
) || (arcn
->type
== PAX_REG
))
* header field is out of range
warn(1, "Tar header field is too small for %s", arcn
->org_name
);
* Routines for POSIX ustar
* initialization for ustar read
if ((usrtb_start() < 0) || (grptb_start() < 0))
* initialization for ustar write
if ((uidtb_start() < 0) || (gidtb_start() < 0))
* determine if a block given to us is a valid ustar header. We have to
* be on the lookout for those pesky blocks of all zero's
* 0 if a ustar header, -1 otherwise
ustar_id(char *blk
, int size
)
* check for block of zero's first, a simple and fast test then check
* ustar magic cookie. We should use TMAGLEN, but some USTAR archive
* programs are fouled up and create archives missing the \0. Last we
* check the checksum. If ok we have to assume it is a valid header.
if (strncmp(hd
->magic
, TMAGIC
, TMAGLEN
- 1) != 0)
if (asc_ul(hd
->chksum
,sizeof(hd
->chksum
),OCT
) != tar_chksm(blk
,BLKMULT
))
* extract the values out of block already determined to be a ustar header.
* store the values in the ARCHD parameter.
ustar_rd(register ARCHD
*arcn
, register char *buf
)
* we only get proper sized buffers
if (ustar_id(buf
, BLKMULT
) < 0)
arcn
->org_name
= arcn
->name
;
* see if the filename is split into two parts. if, so joint the parts.
* we copy the prefix first and add a / between the prefix and name.
if (*(hd
->prefix
) != '\0') {
cnt
= l_strncpy(arcn
->name
, hd
->prefix
, sizeof(hd
->prefix
));
dest
= arcn
->name
+ arcn
->nlen
;
arcn
->nlen
= l_strncpy(dest
, hd
->name
, sizeof(hd
->name
));
arcn
->name
[arcn
->nlen
] = '\0';
* follow the spec to the letter. we should only have mode bits, strip
* off all other crud we may be passed.
arcn
->sb
.st_mode
= (mode_t
)(asc_ul(hd
->mode
, sizeof(hd
->mode
), OCT
) &
arcn
->sb
.st_size
= (size_t)asc_ul(hd
->size
, sizeof(hd
->size
), OCT
);
arcn
->sb
.st_mtime
= (time_t)asc_ul(hd
->mtime
, sizeof(hd
->mtime
), OCT
);
arcn
->sb
.st_ctime
= arcn
->sb
.st_atime
= arcn
->sb
.st_mtime
;
* If we can find the ascii names for gname and uname in the password
* and group files we will use the uid's and gid they bind. Otherwise
* we use the uid and gid values stored in the header. (This is what
hd
->gname
[sizeof(hd
->gname
) - 1] = '\0';
if (gid_name(hd
->gname
, &(arcn
->sb
.st_gid
)) < 0)
arcn
->sb
.st_gid
= (gid_t
)asc_ul(hd
->gid
, sizeof(hd
->gid
), OCT
);
hd
->uname
[sizeof(hd
->uname
) - 1] = '\0';
if (uid_name(hd
->uname
, &(arcn
->sb
.st_uid
)) < 0)
arcn
->sb
.st_uid
= (uid_t
)asc_ul(hd
->uid
, sizeof(hd
->uid
), OCT
);
* set the defaults, these may be changed depending on the file type
arcn
->sb
.st_rdev
= (dev_t
)0;
* set the mode and PAX type according to the typeflag in the header
arcn
->sb
.st_mode
|= S_IFIFO
;
arcn
->sb
.st_mode
|= S_IFDIR
;
* Some programs that create ustar archives append a '/'
* to the pathname for directories. This clearly violates
* ustar specs, but we will silently strip it off anyway.
if (arcn
->name
[arcn
->nlen
- 1] == '/')
arcn
->name
[--arcn
->nlen
] = '\0';
* this type requires the rdev field to be set.
if (hd
->typeflag
== BLKTYPE
) {
arcn
->sb
.st_mode
|= S_IFBLK
;
arcn
->sb
.st_mode
|= S_IFCHR
;
devmajor
= (dev_t
)asc_ul(hd
->devmajor
,sizeof(hd
->devmajor
),OCT
);
devminor
= (dev_t
)asc_ul(hd
->devminor
,sizeof(hd
->devminor
),OCT
);
arcn
->sb
.st_rdev
= TODEV(devmajor
, devminor
);
if (hd
->typeflag
== SYMTYPE
) {
arcn
->sb
.st_mode
|= S_IFLNK
;
* so printing looks better
arcn
->sb
.st_mode
|= S_IFREG
;
arcn
->ln_nlen
= l_strncpy(arcn
->ln_name
, hd
->linkname
,
arcn
->ln_name
[arcn
->ln_nlen
] = '\0';
* these types have file data that follows. Set the skip and
arcn
->pad
= TAR_PAD(arcn
->sb
.st_size
);
arcn
->skip
= arcn
->sb
.st_size
;
arcn
->sb
.st_mode
|= S_IFREG
;
* write a ustar header for the file specified in the ARCHD to the archive
* Have to check for file types that cannot be stored and file names that
* are too long. Be careful of the term (last arg) to ul_oct, we only use
* '\0' for the termination character (this is different than picky tar)
* ASSUMED: space after header in header block is zero filled
* 0 if file has data to be written after the header, 1 if file has NO
* data to write after the header, -1 if archive write failed
ustar_wr(register ARCHD
*arcn
)
char hdblk
[sizeof(HD_USTAR
)];
* check for those file system types ustar cannot store
if (arcn
->type
== PAX_SCK
) {
warn(1, "Ustar cannot archive a socket %s", arcn
->org_name
);
* check the length of the linkname
if (((arcn
->type
== PAX_SLK
) || (arcn
->type
== PAX_HLK
) ||
(arcn
->type
== PAX_HRG
)) && (arcn
->ln_nlen
> sizeof(hd
->linkname
))){
warn(1, "Link name too long for ustar %s", arcn
->ln_name
);
* split the path name into prefix and name fields (if needed). if
* pt != arcn->name, the name has to be split
if ((pt
= name_split(arcn
->name
, arcn
->nlen
)) == NULL
) {
warn(1, "File name too long for ustar %s", arcn
->name
);
* split the name, or zero out the prefix
* name was split, pt points at the / where the split is to
* occur, we remove the / and copy the first part to the prefix
zf_strncpy(hd
->prefix
, arcn
->name
, sizeof(hd
->prefix
));
bzero(hd
->prefix
, sizeof(hd
->prefix
));
* copy the name part. this may be the whole path or the part after
zf_strncpy(hd
->name
, pt
, sizeof(hd
->name
));
* set the fields in the header that are type dependent
bzero(hd
->linkname
, sizeof(hd
->linkname
));
bzero(hd
->devmajor
, sizeof(hd
->devmajor
));
bzero(hd
->devminor
, sizeof(hd
->devminor
));
if (ul_oct((u_long
)0L, hd
->size
, sizeof(hd
->size
), 3))
if (arcn
->type
== PAX_CHR
)
bzero(hd
->linkname
, sizeof(hd
->linkname
));
if (ul_oct((u_long
)MAJOR(arcn
->sb
.st_rdev
), hd
->devmajor
,
sizeof(hd
->devmajor
), 3) ||
ul_oct((u_long
)MINOR(arcn
->sb
.st_rdev
), hd
->devminor
,
sizeof(hd
->devminor
), 3) ||
ul_oct((u_long
)0L, hd
->size
, sizeof(hd
->size
), 3))
bzero(hd
->linkname
, sizeof(hd
->linkname
));
bzero(hd
->devmajor
, sizeof(hd
->devmajor
));
bzero(hd
->devminor
, sizeof(hd
->devminor
));
if (ul_oct((u_long
)0L, hd
->size
, sizeof(hd
->size
), 3))
if (arcn
->type
== PAX_SLK
)
zf_strncpy(hd
->linkname
,arcn
->ln_name
, sizeof(hd
->linkname
));
bzero(hd
->devmajor
, sizeof(hd
->devmajor
));
bzero(hd
->devminor
, sizeof(hd
->devminor
));
if (ul_oct((u_long
)0L, hd
->size
, sizeof(hd
->size
), 3))
* file data with this type, set the padding
if (arcn
->type
== PAX_CTG
)
bzero(hd
->linkname
, sizeof(hd
->linkname
));
bzero(hd
->devmajor
, sizeof(hd
->devmajor
));
bzero(hd
->devminor
, sizeof(hd
->devminor
));
arcn
->pad
= TAR_PAD(arcn
->sb
.st_size
);
if (ul_oct((u_long
)arcn
->sb
.st_size
, hd
->size
,
if (uqd_oct((u_quad_t
)arcn
->sb
.st_size
, hd
->size
,
warn(1,"File is too long for ustar %s",arcn
->org_name
);
zf_strncpy(hd
->magic
, TMAGIC
, TMAGLEN
);
zf_strncpy(hd
->version
, TVERSION
, TVERSLEN
);
* set the remaining fields. Some versions want all 16 bits of mode
* we better humor them (they really do not meet spec though)....
if (ul_oct((u_long
)arcn
->sb
.st_mode
, hd
->mode
, sizeof(hd
->mode
), 3) ||
ul_oct((u_long
)arcn
->sb
.st_uid
, hd
->uid
, sizeof(hd
->uid
), 3) ||
ul_oct((u_long
)arcn
->sb
.st_gid
, hd
->gid
, sizeof(hd
->gid
), 3) ||
ul_oct((u_long
)arcn
->sb
.st_mtime
,hd
->mtime
,sizeof(hd
->mtime
),3))
zf_strncpy(hd
->uname
,name_uid(arcn
->sb
.st_uid
, 0),sizeof(hd
->uname
));
zf_strncpy(hd
->gname
,name_gid(arcn
->sb
.st_gid
, 0),sizeof(hd
->gname
));
* calculate and store the checksum write the header to the archive
* return 0 tells the caller to now write the file data, 1 says no data
if (ul_oct(tar_chksm(hdblk
, sizeof(HD_USTAR
)), hd
->chksum
,
if (wr_rdbuf(hdblk
, sizeof(HD_USTAR
)) < 0)
if (wr_skip((off_t
)(BLKMULT
- sizeof(HD_USTAR
))) < 0)
if ((arcn
->type
== PAX_CTG
) || (arcn
->type
== PAX_REG
))
* header field is out of range
warn(1, "Ustar header field is too small for %s", arcn
->org_name
);
* see if the name has to be split for storage in a ustar header. We try
* to fit the entire name in the name field without splitting if we can.
* The split point is always at a /
* character pointer to split point (always the / that is to be removed
* if the split is not needed, the points is set to the start of the file
* name (it would violate the spec to split there). A NULL is returned if
* the file name is too long
name_split(register char *name
, register int len
)
* check to see if the file name is small enough to fit in the name
* field. if so just return a pointer to the name.
if (len
> (TPFSZ
+ TNMSZ
+ 1))
* we start looking at the biggest sized piece that fits in the name
* field. We walk foward looking for a slash to split at. The idea is
* to find the biggest piece to fit in the name field (or the smallest
* prefix we can find) (the -1 is correct the biggest piece would
* include the slash between the two parts that gets thrown away)
start
= name
+ len
- TNMSZ
- 1;
while ((*start
!= '\0') && (*start
!= '/'))
* if we hit the end of the string, this name cannot be split, so we
* cannot store this file.
* NOTE: /str where the length of str == TNMSZ can not be stored under
* the p1003.1-1990 spec for ustar. We could force a prefix of / and
* the file would then expand on extract to //str. The len == 0 below
* makes this special case follow the spec to the letter.
if ((len
> TPFSZ
) || (len
== 0))
* ok have a split point, return it to the caller