2 * Copyright (c) 1992 Keith Muller.
3 * Copyright (c) 1992, 1993
4 * The Regents of the University of California. All rights reserved.
6 * This code is derived from software contributed to Berkeley by
7 * Keith Muller of the University of California, San Diego.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * @(#)tar.c 8.2 (Berkeley) 4/18/94
34 * $FreeBSD: src/bin/pax/tar.c,v 1.13.2.1 2001/08/01 05:03:12 obrien Exp $
35 * $DragonFly: src/bin/pax/tar.c,v 1.7 2006/09/27 21:58:08 pavalos Exp $
38 #include <sys/types.h>
50 * Routines for reading, writing and header identify of various versions of tar
53 static u_long
tar_chksm (char *, int);
54 static char *name_split (char *, int);
55 static int ul_oct (u_long
, char *, int, int);
56 static int uqd_oct (u_quad_t
, char *, int, int);
59 * Routines common to all versions of tar
62 static int tar_nodir
; /* do not write dirs under old tar */
66 * add the tar trailer of two null blocks
68 * 0 if ok, -1 otherwise (what wr_skip returns)
74 return(wr_skip((off_t
)(NULLCNT
*BLKMULT
)));
79 * no cleanup needed here, just return size of trailer (for append)
81 * size of trailer (2 * BLKMULT)
87 return((off_t
)(NULLCNT
*BLKMULT
));
92 * Called to determine if a header block is a valid trailer. We are passed
93 * the block, the in_sync flag (which tells us we are in resync mode;
94 * looking for a valid header), and cnt (which starts at zero) which is
95 * used to count the number of empty blocks we have seen so far.
97 * 0 if a valid trailer, -1 if not a valid trailer, or 1 if the block
98 * could never contain a header.
102 tar_trail(char *buf
, int in_resync
, int *cnt
)
107 * look for all zero, trailer is two consecutive blocks of zero
109 for (i
= 0; i
< BLKMULT
; ++i
) {
115 * if not all zero it is not a trailer, but MIGHT be a header.
121 * When given a zero block, we must be careful!
122 * If we are not in resync mode, check for the trailer. Have to watch
123 * out that we do not mis-identify file data as the trailer, so we do
124 * NOT try to id a trailer during resync mode. During resync mode we
125 * might as well throw this block out since a valid header can NEVER be
126 * a block of all 0 (we must have a valid file name).
128 if (!in_resync
&& (++*cnt
>= NULLCNT
))
135 * convert an unsigned long to an octal string. many oddball field
136 * termination characters are used by the various versions of tar in the
137 * different fields. term selects which kind to use. str is '0' padded
138 * at the front to len. we are unable to use only one format as many old
139 * tar readers are very cranky about this.
141 * 0 if the number fit into the string, -1 otherwise
145 ul_oct(u_long val
, char *str
, int len
, int term
)
150 * term selects the appropriate character(s) for the end of the string
172 * convert and blank pad if there is space
175 *pt
-- = '0' + (char)(val
& 0x7);
176 if ((val
= val
>> 3) == (u_long
)0)
182 if (val
!= (u_long
)0)
189 * convert an u_quad_t to an octal string. one of many oddball field
190 * termination characters are used by the various versions of tar in the
191 * different fields. term selects which kind to use. str is '0' padded
192 * at the front to len. we are unable to use only one format as many old
193 * tar readers are very cranky about this.
195 * 0 if the number fit into the string, -1 otherwise
199 uqd_oct(u_quad_t val
, char *str
, int len
, int term
)
204 * term selects the appropriate character(s) for the end of the string
226 * convert and blank pad if there is space
229 *pt
-- = '0' + (char)(val
& 0x7);
230 if ((val
= val
>> 3) == 0)
236 if (val
!= (u_quad_t
)0)
243 * calculate the checksum for a tar block counting the checksum field as
244 * all blanks (BLNKSUM is that value pre-calculated, the sum of 8 blanks).
245 * NOTE: we use len to short circuit summing 0's on write since we ALWAYS
246 * pad headers with 0.
248 * unsigned long checksum
252 tar_chksm(char *blk
, int len
)
256 u_long chksm
= BLNKSUM
; /* initial value is checksum field sum */
259 * add the part of the block before the checksum field
262 stop
= blk
+ CHK_OFFSET
;
264 chksm
+= (u_long
)(*pt
++ & 0xff);
266 * move past the checksum field and keep going, spec counts the
267 * checksum field as the sum of 8 blanks (which is pre-computed as
269 * ASSUMED: len is greater than CHK_OFFSET. (len is where our 0 padding
270 * starts, no point in summing zero's)
275 chksm
+= (u_long
)(*pt
++ & 0xff);
280 * Routines for old BSD style tar (also made portable to sysV tar)
285 * determine if a block given to us is a valid tar header (and not a USTAR
286 * header). We have to be on the lookout for those pesky blocks of all
289 * 0 if a tar header, -1 otherwise
293 tar_id(char *blk
, int size
)
301 uhd
= (HD_USTAR
*)blk
;
304 * check for block of zero's first, a simple and fast test, then make
305 * sure this is not a ustar header by looking for the ustar magic
306 * cookie. We should use TMAGLEN, but some USTAR archive programs are
307 * wrong and create archives missing the \0. Last we check the
308 * checksum. If this is ok we have to assume it is a valid header.
310 if (hd
->name
[0] == '\0')
312 if (strncmp(uhd
->magic
, TMAGIC
, TMAGLEN
- 1) == 0)
314 if (asc_ul(hd
->chksum
,sizeof(hd
->chksum
),OCT
) != tar_chksm(blk
,BLKMULT
))
321 * handle tar format specific -o options
323 * 0 if ok -1 otherwise
331 while ((opt
= opt_next()) != NULL
) {
332 if (strcmp(opt
->name
, TAR_OPTION
) ||
333 strcmp(opt
->value
, TAR_NODIR
)) {
334 paxwarn(1, "Unknown tar format -o option/value pair %s=%s",
335 opt
->name
, opt
->value
);
336 paxwarn(1,"%s=%s is the only supported tar format option",
337 TAR_OPTION
, TAR_NODIR
);
342 * we only support one option, and only when writing
344 if ((act
!= APPND
) && (act
!= ARCHIVE
)) {
345 paxwarn(1, "%s=%s is only supported when writing.",
346 opt
->name
, opt
->value
);
357 * extract the values out of block already determined to be a tar header.
358 * store the values in the ARCHD parameter.
364 tar_rd(ARCHD
*arcn
, char *buf
)
370 * we only get proper sized buffers passed to us
372 if (tar_id(buf
, BLKMULT
) < 0)
374 arcn
->org_name
= arcn
->name
;
375 arcn
->sb
.st_nlink
= 1;
379 * copy out the name and values in the stat buffer
382 arcn
->nlen
= l_strncpy(arcn
->name
, hd
->name
, sizeof(arcn
->name
) - 1);
383 arcn
->name
[arcn
->nlen
] = '\0';
384 arcn
->sb
.st_mode
= (mode_t
)(asc_ul(hd
->mode
,sizeof(hd
->mode
),OCT
) &
386 arcn
->sb
.st_uid
= (uid_t
)asc_ul(hd
->uid
, sizeof(hd
->uid
), OCT
);
387 arcn
->sb
.st_gid
= (gid_t
)asc_ul(hd
->gid
, sizeof(hd
->gid
), OCT
);
388 arcn
->sb
.st_size
= (off_t
)asc_uqd(hd
->size
, sizeof(hd
->size
), OCT
);
389 arcn
->sb
.st_mtime
= (time_t)asc_ul(hd
->mtime
, sizeof(hd
->mtime
), OCT
);
390 arcn
->sb
.st_ctime
= arcn
->sb
.st_atime
= arcn
->sb
.st_mtime
;
393 * have to look at the last character, it may be a '/' and that is used
394 * to encode this as a directory
396 pt
= &(arcn
->name
[arcn
->nlen
- 1]);
399 switch(hd
->linkflag
) {
402 * symbolic link, need to get the link name and set the type in
403 * the st_mode so -v printing will look correct.
405 arcn
->type
= PAX_SLK
;
406 arcn
->ln_nlen
= l_strncpy(arcn
->ln_name
, hd
->linkname
,
407 sizeof(arcn
->ln_name
) - 1);
408 arcn
->ln_name
[arcn
->ln_nlen
] = '\0';
409 arcn
->sb
.st_mode
|= S_IFLNK
;
413 * hard link, need to get the link name, set the type in the
414 * st_mode and st_nlink so -v printing will look better.
416 arcn
->type
= PAX_HLK
;
417 arcn
->sb
.st_nlink
= 2;
418 arcn
->ln_nlen
= l_strncpy(arcn
->ln_name
, hd
->linkname
,
419 sizeof(arcn
->ln_name
) - 1);
420 arcn
->ln_name
[arcn
->ln_nlen
] = '\0';
423 * no idea of what type this thing really points at, but
424 * we set something for printing only.
426 arcn
->sb
.st_mode
|= S_IFREG
;
430 * It is a directory, set the mode for -v printing
432 arcn
->type
= PAX_DIR
;
433 arcn
->sb
.st_mode
|= S_IFDIR
;
434 arcn
->sb
.st_nlink
= 2;
435 arcn
->ln_name
[0] = '\0';
442 * If we have a trailing / this is a directory and NOT a file.
444 arcn
->ln_name
[0] = '\0';
448 * it is a directory, set the mode for -v printing
450 arcn
->type
= PAX_DIR
;
451 arcn
->sb
.st_mode
|= S_IFDIR
;
452 arcn
->sb
.st_nlink
= 2;
455 * have a file that will be followed by data. Set the
456 * skip value to the size field and calculate the size
459 arcn
->type
= PAX_REG
;
460 arcn
->sb
.st_mode
|= S_IFREG
;
461 arcn
->pad
= TAR_PAD(arcn
->sb
.st_size
);
462 arcn
->skip
= arcn
->sb
.st_size
;
468 * strip off any trailing slash.
479 * write a tar header for the file specified in the ARCHD to the archive.
480 * Have to check for file types that cannot be stored and file names that
481 * are too long. Be careful of the term (last arg) to ul_oct, each field
482 * of tar has it own spec for the termination character(s).
483 * ASSUMED: space after header in header block is zero filled
485 * 0 if file has data to be written after the header, 1 if file has NO
486 * data to write after the header, -1 if archive write failed
494 char hdblk
[sizeof(HD_TAR
)];
497 * check for those file system types which tar cannot store
502 * user asked that dirs not be written to the archive
508 paxwarn(1, "Tar cannot archive a character device %s",
512 paxwarn(1, "Tar cannot archive a block device %s", arcn
->org_name
);
515 paxwarn(1, "Tar cannot archive a socket %s", arcn
->org_name
);
518 paxwarn(1, "Tar cannot archive a fifo %s", arcn
->org_name
);
523 if (arcn
->ln_nlen
> sizeof(hd
->linkname
)) {
524 paxwarn(1,"Link name too long for tar %s", arcn
->ln_name
);
535 * check file name len, remember extra char for dirs (the / at the end)
538 if (arcn
->type
== PAX_DIR
)
540 if (len
>= sizeof(hd
->name
)) {
541 paxwarn(1, "File name too long for tar %s", arcn
->name
);
546 * copy the data out of the ARCHD into the tar header based on the type
547 * of the file. Remember many tar readers want the unused fields to be
548 * padded with zero. We set the linkflag field (type), the linkname
549 * (or zero if not used),the size, and set the padding (if any) to be
550 * added after the file data (0 for all other types, as they only have
553 hd
= (HD_TAR
*)hdblk
;
554 l_strncpy(hd
->name
, arcn
->name
, sizeof(hd
->name
) - 1);
555 hd
->name
[sizeof(hd
->name
) - 1] = '\0';
558 if (arcn
->type
== PAX_DIR
) {
560 * directories are the same as files, except have a filename
561 * that ends with a /, we add the slash here. No data follows
564 hd
->linkflag
= AREGTYPE
;
565 memset(hd
->linkname
, 0, sizeof(hd
->linkname
));
566 hd
->name
[len
-1] = '/';
567 if (ul_oct((u_long
)0L, hd
->size
, sizeof(hd
->size
), 1))
569 } else if (arcn
->type
== PAX_SLK
) {
571 * no data follows this file, so no pad
573 hd
->linkflag
= SYMTYPE
;
574 l_strncpy(hd
->linkname
,arcn
->ln_name
, sizeof(hd
->linkname
) - 1);
575 hd
->linkname
[sizeof(hd
->linkname
) - 1] = '\0';
576 if (ul_oct((u_long
)0L, hd
->size
, sizeof(hd
->size
), 1))
578 } else if ((arcn
->type
== PAX_HLK
) || (arcn
->type
== PAX_HRG
)) {
580 * no data follows this file, so no pad
582 hd
->linkflag
= LNKTYPE
;
583 l_strncpy(hd
->linkname
,arcn
->ln_name
, sizeof(hd
->linkname
) - 1);
584 hd
->linkname
[sizeof(hd
->linkname
) - 1] = '\0';
585 if (ul_oct((u_long
)0L, hd
->size
, sizeof(hd
->size
), 1))
589 * data follows this file, so set the pad
591 hd
->linkflag
= AREGTYPE
;
592 memset(hd
->linkname
, 0, sizeof(hd
->linkname
));
593 if (uqd_oct((u_quad_t
)arcn
->sb
.st_size
, hd
->size
,
594 sizeof(hd
->size
), 1)) {
595 paxwarn(1,"File is too large for tar %s", arcn
->org_name
);
598 arcn
->pad
= TAR_PAD(arcn
->sb
.st_size
);
602 * copy those fields that are independent of the type
604 if (ul_oct((u_long
)arcn
->sb
.st_mode
, hd
->mode
, sizeof(hd
->mode
), 0) ||
605 ul_oct((u_long
)arcn
->sb
.st_uid
, hd
->uid
, sizeof(hd
->uid
), 0) ||
606 ul_oct((u_long
)arcn
->sb
.st_gid
, hd
->gid
, sizeof(hd
->gid
), 0) ||
607 ul_oct((u_long
)arcn
->sb
.st_mtime
, hd
->mtime
, sizeof(hd
->mtime
), 1))
611 * calculate and add the checksum, then write the header. A return of
612 * 0 tells the caller to now write the file data, 1 says no data needs
615 if (ul_oct(tar_chksm(hdblk
, sizeof(HD_TAR
)), hd
->chksum
,
616 sizeof(hd
->chksum
), 3))
618 if (wr_rdbuf(hdblk
, sizeof(HD_TAR
)) < 0)
620 if (wr_skip((off_t
)(BLKMULT
- sizeof(HD_TAR
))) < 0)
622 if ((arcn
->type
== PAX_CTG
) || (arcn
->type
== PAX_REG
))
628 * header field is out of range
630 paxwarn(1, "Tar header field is too small for %s", arcn
->org_name
);
635 * Routines for POSIX ustar
640 * initialization for ustar read
642 * 0 if ok, -1 otherwise
648 if ((usrtb_start() < 0) || (grptb_start() < 0))
655 * initialization for ustar write
657 * 0 if ok, -1 otherwise
663 if ((uidtb_start() < 0) || (gidtb_start() < 0))
670 * determine if a block given to us is a valid ustar header. We have to
671 * be on the lookout for those pesky blocks of all zero's
673 * 0 if a ustar header, -1 otherwise
677 ustar_id(char *blk
, int size
)
683 hd
= (HD_USTAR
*)blk
;
686 * check for block of zero's first, a simple and fast test then check
687 * ustar magic cookie. We should use TMAGLEN, but some USTAR archive
688 * programs are fouled up and create archives missing the \0. Last we
689 * check the checksum. If ok we have to assume it is a valid header.
691 if (hd
->name
[0] == '\0')
693 if (strncmp(hd
->magic
, TMAGIC
, TMAGLEN
- 1) != 0)
695 if (asc_ul(hd
->chksum
,sizeof(hd
->chksum
),OCT
) != tar_chksm(blk
,BLKMULT
))
702 * extract the values out of block already determined to be a ustar header.
703 * store the values in the ARCHD parameter.
709 ustar_rd(ARCHD
*arcn
, char *buf
)
718 * we only get proper sized buffers
720 if (ustar_id(buf
, BLKMULT
) < 0)
722 arcn
->org_name
= arcn
->name
;
723 arcn
->sb
.st_nlink
= 1;
726 hd
= (HD_USTAR
*)buf
;
729 * see if the filename is split into two parts. if, so joint the parts.
730 * we copy the prefix first and add a / between the prefix and name.
733 if (*(hd
->prefix
) != '\0') {
734 cnt
= l_strncpy(dest
, hd
->prefix
, sizeof(arcn
->name
) - 2);
739 arcn
->nlen
= cnt
+ l_strncpy(dest
, hd
->name
, sizeof(arcn
->name
) - cnt
);
740 arcn
->name
[arcn
->nlen
] = '\0';
743 * follow the spec to the letter. we should only have mode bits, strip
744 * off all other crud we may be passed.
746 arcn
->sb
.st_mode
= (mode_t
)(asc_ul(hd
->mode
, sizeof(hd
->mode
), OCT
) &
748 arcn
->sb
.st_size
= (off_t
)asc_uqd(hd
->size
, sizeof(hd
->size
), OCT
);
749 arcn
->sb
.st_mtime
= (time_t)asc_ul(hd
->mtime
, sizeof(hd
->mtime
), OCT
);
750 arcn
->sb
.st_ctime
= arcn
->sb
.st_atime
= arcn
->sb
.st_mtime
;
753 * If we can find the ascii names for gname and uname in the password
754 * and group files we will use the uid's and gid they bind. Otherwise
755 * we use the uid and gid values stored in the header. (This is what
756 * the POSIX spec wants).
758 hd
->gname
[sizeof(hd
->gname
) - 1] = '\0';
759 if (gid_name(hd
->gname
, &(arcn
->sb
.st_gid
)) < 0)
760 arcn
->sb
.st_gid
= (gid_t
)asc_ul(hd
->gid
, sizeof(hd
->gid
), OCT
);
761 hd
->uname
[sizeof(hd
->uname
) - 1] = '\0';
762 if (uid_name(hd
->uname
, &(arcn
->sb
.st_uid
)) < 0)
763 arcn
->sb
.st_uid
= (uid_t
)asc_ul(hd
->uid
, sizeof(hd
->uid
), OCT
);
766 * set the defaults, these may be changed depending on the file type
768 arcn
->ln_name
[0] = '\0';
772 arcn
->sb
.st_rdev
= (dev_t
)0;
775 * set the mode and PAX type according to the typeflag in the header
777 switch(hd
->typeflag
) {
779 arcn
->type
= PAX_FIF
;
780 arcn
->sb
.st_mode
|= S_IFIFO
;
783 arcn
->type
= PAX_DIR
;
784 arcn
->sb
.st_mode
|= S_IFDIR
;
785 arcn
->sb
.st_nlink
= 2;
788 * Some programs that create ustar archives append a '/'
789 * to the pathname for directories. This clearly violates
790 * ustar specs, but we will silently strip it off anyway.
792 if (arcn
->name
[arcn
->nlen
- 1] == '/')
793 arcn
->name
[--arcn
->nlen
] = '\0';
798 * this type requires the rdev field to be set.
800 if (hd
->typeflag
== BLKTYPE
) {
801 arcn
->type
= PAX_BLK
;
802 arcn
->sb
.st_mode
|= S_IFBLK
;
804 arcn
->type
= PAX_CHR
;
805 arcn
->sb
.st_mode
|= S_IFCHR
;
807 devmajor
= (dev_t
)asc_ul(hd
->devmajor
,sizeof(hd
->devmajor
),OCT
);
808 devminor
= (dev_t
)asc_ul(hd
->devminor
,sizeof(hd
->devminor
),OCT
);
809 arcn
->sb
.st_rdev
= TODEV(devmajor
, devminor
);
813 if (hd
->typeflag
== SYMTYPE
) {
814 arcn
->type
= PAX_SLK
;
815 arcn
->sb
.st_mode
|= S_IFLNK
;
817 arcn
->type
= PAX_HLK
;
819 * so printing looks better
821 arcn
->sb
.st_mode
|= S_IFREG
;
822 arcn
->sb
.st_nlink
= 2;
827 arcn
->ln_nlen
= l_strncpy(arcn
->ln_name
, hd
->linkname
,
828 sizeof(arcn
->ln_name
) - 1);
829 arcn
->ln_name
[arcn
->ln_nlen
] = '\0';
836 * these types have file data that follows. Set the skip and
839 arcn
->type
= PAX_REG
;
840 arcn
->pad
= TAR_PAD(arcn
->sb
.st_size
);
841 arcn
->skip
= arcn
->sb
.st_size
;
842 arcn
->sb
.st_mode
|= S_IFREG
;
850 * write a ustar header for the file specified in the ARCHD to the archive
851 * Have to check for file types that cannot be stored and file names that
852 * are too long. Be careful of the term (last arg) to ul_oct, we only use
853 * '\0' for the termination character (this is different than picky tar)
854 * ASSUMED: space after header in header block is zero filled
856 * 0 if file has data to be written after the header, 1 if file has NO
857 * data to write after the header, -1 if archive write failed
861 ustar_wr(ARCHD
*arcn
)
865 char hdblk
[sizeof(HD_USTAR
)];
868 * check for those file system types ustar cannot store
870 if (arcn
->type
== PAX_SCK
) {
871 paxwarn(1, "Ustar cannot archive a socket %s", arcn
->org_name
);
876 * check the length of the linkname
878 if (((arcn
->type
== PAX_SLK
) || (arcn
->type
== PAX_HLK
) ||
879 (arcn
->type
== PAX_HRG
)) && (arcn
->ln_nlen
>= sizeof(hd
->linkname
))){
880 paxwarn(1, "Link name too long for ustar %s", arcn
->ln_name
);
885 * split the path name into prefix and name fields (if needed). if
886 * pt != arcn->name, the name has to be split
888 if ((pt
= name_split(arcn
->name
, arcn
->nlen
)) == NULL
) {
889 paxwarn(1, "File name too long for ustar %s", arcn
->name
);
892 hd
= (HD_USTAR
*)hdblk
;
896 * split the name, or zero out the prefix
898 if (pt
!= arcn
->name
) {
900 * name was split, pt points at the / where the split is to
901 * occur, we remove the / and copy the first part to the prefix
904 l_strncpy(hd
->prefix
, arcn
->name
, sizeof(hd
->prefix
) - 1);
907 memset(hd
->prefix
, 0, sizeof(hd
->prefix
));
910 * copy the name part. this may be the whole path or the part after
913 l_strncpy(hd
->name
, pt
, sizeof(hd
->name
) - 1);
914 hd
->name
[sizeof(hd
->name
) - 1] = '\0';
917 * set the fields in the header that are type dependent
921 hd
->typeflag
= DIRTYPE
;
922 memset(hd
->linkname
, 0, sizeof(hd
->linkname
));
923 memset(hd
->devmajor
, 0, sizeof(hd
->devmajor
));
924 memset(hd
->devminor
, 0, sizeof(hd
->devminor
));
925 if (ul_oct((u_long
)0L, hd
->size
, sizeof(hd
->size
), 3))
930 if (arcn
->type
== PAX_CHR
)
931 hd
->typeflag
= CHRTYPE
;
933 hd
->typeflag
= BLKTYPE
;
934 memset(hd
->linkname
, 0, sizeof(hd
->linkname
));
935 if (ul_oct((u_long
)MAJOR(arcn
->sb
.st_rdev
), hd
->devmajor
,
936 sizeof(hd
->devmajor
), 3) ||
937 ul_oct((u_long
)MINOR(arcn
->sb
.st_rdev
), hd
->devminor
,
938 sizeof(hd
->devminor
), 3) ||
939 ul_oct((u_long
)0L, hd
->size
, sizeof(hd
->size
), 3))
943 hd
->typeflag
= FIFOTYPE
;
944 memset(hd
->linkname
, 0, sizeof(hd
->linkname
));
945 memset(hd
->devmajor
, 0, sizeof(hd
->devmajor
));
946 memset(hd
->devminor
, 0, sizeof(hd
->devminor
));
947 if (ul_oct((u_long
)0L, hd
->size
, sizeof(hd
->size
), 3))
953 if (arcn
->type
== PAX_SLK
)
954 hd
->typeflag
= SYMTYPE
;
956 hd
->typeflag
= LNKTYPE
;
957 l_strncpy(hd
->linkname
,arcn
->ln_name
, sizeof(hd
->linkname
) - 1);
958 hd
->linkname
[sizeof(hd
->linkname
) - 1] = '\0';
959 memset(hd
->devmajor
, 0, sizeof(hd
->devmajor
));
960 memset(hd
->devminor
, 0, sizeof(hd
->devminor
));
961 if (ul_oct((u_long
)0L, hd
->size
, sizeof(hd
->size
), 3))
968 * file data with this type, set the padding
970 if (arcn
->type
== PAX_CTG
)
971 hd
->typeflag
= CONTTYPE
;
973 hd
->typeflag
= REGTYPE
;
974 memset(hd
->linkname
, 0, sizeof(hd
->linkname
));
975 memset(hd
->devmajor
, 0, sizeof(hd
->devmajor
));
976 memset(hd
->devminor
, 0, sizeof(hd
->devminor
));
977 arcn
->pad
= TAR_PAD(arcn
->sb
.st_size
);
978 if (uqd_oct((u_quad_t
)arcn
->sb
.st_size
, hd
->size
,
979 sizeof(hd
->size
), 3)) {
980 paxwarn(1,"File is too long for ustar %s",arcn
->org_name
);
986 l_strncpy(hd
->magic
, TMAGIC
, TMAGLEN
);
987 l_strncpy(hd
->version
, TVERSION
, TVERSLEN
);
990 * set the remaining fields. Some versions want all 16 bits of mode
991 * we better humor them (they really do not meet spec though)....
993 if (ul_oct((u_long
)arcn
->sb
.st_mode
, hd
->mode
, sizeof(hd
->mode
), 3) ||
994 ul_oct((u_long
)arcn
->sb
.st_uid
, hd
->uid
, sizeof(hd
->uid
), 3) ||
995 ul_oct((u_long
)arcn
->sb
.st_gid
, hd
->gid
, sizeof(hd
->gid
), 3) ||
996 ul_oct((u_long
)arcn
->sb
.st_mtime
,hd
->mtime
,sizeof(hd
->mtime
),3))
998 l_strncpy(hd
->uname
,name_uid(arcn
->sb
.st_uid
, 0),sizeof(hd
->uname
));
999 l_strncpy(hd
->gname
,name_gid(arcn
->sb
.st_gid
, 0),sizeof(hd
->gname
));
1002 * calculate and store the checksum write the header to the archive
1003 * return 0 tells the caller to now write the file data, 1 says no data
1004 * needs to be written
1006 if (ul_oct(tar_chksm(hdblk
, sizeof(HD_USTAR
)), hd
->chksum
,
1007 sizeof(hd
->chksum
), 3))
1009 if (wr_rdbuf(hdblk
, sizeof(HD_USTAR
)) < 0)
1011 if (wr_skip((off_t
)(BLKMULT
- sizeof(HD_USTAR
))) < 0)
1013 if ((arcn
->type
== PAX_CTG
) || (arcn
->type
== PAX_REG
))
1019 * header field is out of range
1021 paxwarn(1, "Ustar header field is too small for %s", arcn
->org_name
);
1027 * see if the name has to be split for storage in a ustar header. We try
1028 * to fit the entire name in the name field without splitting if we can.
1029 * The split point is always at a /
1031 * character pointer to split point (always the / that is to be removed
1032 * if the split is not needed, the points is set to the start of the file
1033 * name (it would violate the spec to split there). A NULL is returned if
1034 * the file name is too long
1038 name_split(char *name
, int len
)
1043 * check to see if the file name is small enough to fit in the name
1044 * field. if so just return a pointer to the name.
1048 if (len
> (TPFSZ
+ TNMSZ
))
1052 * we start looking at the biggest sized piece that fits in the name
1053 * field. We walk forward looking for a slash to split at. The idea is
1054 * to find the biggest piece to fit in the name field (or the smallest
1055 * prefix we can find)
1057 start
= name
+ len
- TNMSZ
;
1058 while ((*start
!= '\0') && (*start
!= '/'))
1062 * if we hit the end of the string, this name cannot be split, so we
1063 * cannot store this file.
1070 * NOTE: /str where the length of str == TNMSZ can not be stored under
1071 * the p1003.1-1990 spec for ustar. We could force a prefix of / and
1072 * the file would then expand on extract to //str. The len == 0 below
1073 * makes this special case follow the spec to the letter.
1075 if ((len
>= TPFSZ
) || (len
== 0))
1079 * ok have a split point, return it to the caller