1 /* $NetBSD: tar.c,v 1.66 2008/02/24 20:42:46 joerg Exp $ */
4 * Copyright (c) 1992 Keith Muller.
5 * Copyright (c) 1992, 1993
6 * The Regents of the University of California. All rights reserved.
8 * This code is derived from software contributed to Berkeley by
9 * Keith Muller of the University of California, San Diego.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 #if HAVE_NBTOOL_CONFIG_H
37 #include "nbtool_config.h"
40 #include <sys/cdefs.h>
43 static char sccsid
[] = "@(#)tar.c 8.2 (Berkeley) 4/18/94";
45 __RCSID("$NetBSD: tar.c,v 1.66 2008/02/24 20:42:46 joerg Exp $");
49 #include <sys/types.h>
52 #include <sys/param.h>
68 * Routines for reading, writing and header identify of various versions of tar
71 static int expandname(char *, size_t, char **, size_t *, const char *, size_t);
72 static void longlink(ARCHD
*, int);
73 static u_long
tar_chksm(char *, int);
74 static char *name_split(char *, int);
75 static int ul_oct(u_long
, char *, int, int);
77 static int ull_oct(unsigned long long, char *, int, int);
79 static int tar_gnutar_exclude_one(const char *, size_t);
80 static int check_sum(char *, size_t, char *, size_t, int);
83 * Routines common to all versions of tar
86 static int tar_nodir
; /* do not write dirs under old tar */
87 int is_gnutar
; /* behave like gnu tar; enable gnu
88 * extensions and skip end-of-volume
91 static int seen_gnu_warning
; /* Have we warned yet? */
92 static char *gnu_hack_string
; /* ././@LongLink hackery */
93 static int gnu_hack_len
; /* len of gnu_hack_string */
94 char *gnu_name_string
; /* ././@LongLink hackery name */
95 char *gnu_link_string
; /* ././@LongLink hackery link */
96 size_t gnu_name_length
; /* ././@LongLink hackery name */
97 size_t gnu_link_length
; /* ././@LongLink hackery link */
98 static int gnu_short_trailer
; /* gnu short trailer */
100 static const char LONG_LINK
[] = "././@LongLink";
103 char DEV_0
[] = "/dev/rst0";
104 char DEV_1
[] = "/dev/rst1";
105 char DEV_4
[] = "/dev/rst4";
106 char DEV_5
[] = "/dev/rst5";
107 char DEV_7
[] = "/dev/rst7";
108 char DEV_8
[] = "/dev/rst8";
112 check_sum(char *hd
, size_t hdlen
, char *bl
, size_t bllen
, int quiet
)
116 hdck
= asc_ul(hd
, hdlen
, OCT
);
117 blck
= tar_chksm(bl
, bllen
);
121 tty_warn(0, "Header checksum %lo does not match %lo",
131 * add the tar trailer of two null blocks
133 * 0 if ok, -1 otherwise (what wr_skip returns)
139 return wr_skip((off_t
)(NULLCNT
* BLKMULT
));
144 * no cleanup needed here, just return size of trailer (for append)
146 * size of trailer BLKMULT
152 return (off_t
)((gnu_short_trailer
? 1 : NULLCNT
) * BLKMULT
);
157 * Called to determine if a header block is a valid trailer. We are passed
158 * the block, the in_sync flag (which tells us we are in resync mode;
159 * looking for a valid header), and cnt (which starts at zero) which is
160 * used to count the number of empty blocks we have seen so far.
162 * 0 if a valid trailer, -1 if not a valid trailer, or 1 if the block
163 * could never contain a header.
167 tar_trail(char *buf
, int in_resync
, int *cnt
)
171 gnu_short_trailer
= 0;
173 * look for all zero, trailer is two consecutive blocks of zero
175 for (i
= 0; i
< BLKMULT
; ++i
) {
181 * if not all zero it is not a trailer, but MIGHT be a header.
187 * When given a zero block, we must be careful!
188 * If we are not in resync mode, check for the trailer. Have to watch
189 * out that we do not mis-identify file data as the trailer, so we do
190 * NOT try to id a trailer during resync mode. During resync mode we
191 * might as well throw this block out since a valid header can NEVER be
192 * a block of all 0 (we must have a valid file name).
197 * old GNU tar (up through 1.13) only writes one block of
198 * trailers, so we pretend we got another
201 gnu_short_trailer
= 1;
212 * convert an unsigned long to an octal string. many oddball field
213 * termination characters are used by the various versions of tar in the
214 * different fields. term selects which kind to use. str is '0' padded
215 * at the front to len. we are unable to use only one format as many old
216 * tar readers are very cranky about this.
218 * 0 if the number fit into the string, -1 otherwise
222 ul_oct(u_long val
, char *str
, int len
, int term
)
227 * term selects the appropriate character(s) for the end of the string
249 * convert and blank pad if there is space
252 *pt
-- = '0' + (char)(val
& 0x7);
253 if ((val
= val
>> 3) == (u_long
)0)
259 if (val
!= (u_long
)0)
267 * convert an unsigned long long to an octal string. one of many oddball
268 * field termination characters are used by the various versions of tar
269 * in the different fields. term selects which kind to use. str is '0'
270 * padded at the front to len. we are unable to use only one format as
271 * many old tar readers are very cranky about this.
273 * 0 if the number fit into the string, -1 otherwise
277 ull_oct(unsigned long long val
, char *str
, int len
, int term
)
282 * term selects the appropriate character(s) for the end of the string
304 * convert and blank pad if there is space
307 *pt
-- = '0' + (char)(val
& 0x7);
308 if ((val
= val
>> 3) == 0)
314 if (val
!= (unsigned long long)0)
322 * calculate the checksum for a tar block counting the checksum field as
323 * all blanks (BLNKSUM is that value pre-calculated, the sum of 8 blanks).
324 * NOTE: we use len to short circuit summing 0's on write since we ALWAYS
325 * pad headers with 0.
327 * unsigned long checksum
331 tar_chksm(char *blk
, int len
)
335 u_long chksm
= BLNKSUM
; /* initial value is checksum field sum */
338 * add the part of the block before the checksum field
341 stop
= blk
+ CHK_OFFSET
;
343 chksm
+= (u_long
)(*pt
++ & 0xff);
345 * move past the checksum field and keep going, spec counts the
346 * checksum field as the sum of 8 blanks (which is pre-computed as
348 * ASSUMED: len is greater than CHK_OFFSET. (len is where our 0 padding
349 * starts, no point in summing zero's)
354 chksm
+= (u_long
)(*pt
++ & 0xff);
359 * Routines for old BSD style tar (also made portable to sysV tar)
364 * determine if a block given to us is a valid tar header (and not a USTAR
365 * header). We have to be on the lookout for those pesky blocks of all
368 * 0 if a tar header, -1 otherwise
372 tar_id(char *blk
, int size
)
376 static int is_ustar
= -1;
381 uhd
= (HD_USTAR
*)blk
;
384 * check for block of zero's first, a simple and fast test, then make
385 * sure this is not a ustar header by looking for the ustar magic
386 * cookie. We should use TMAGLEN, but some USTAR archive programs are
387 * wrong and create archives missing the \0. Last we check the
388 * checksum. If this is ok we have to assume it is a valid header.
390 if (hd
->name
[0] == '\0')
392 if (strncmp(uhd
->magic
, TMAGIC
, TMAGLEN
- 1) == 0) {
393 if (is_ustar
== -1) {
398 "Busted tar archive: has both ustar and old tar "
402 return check_sum(hd
->chksum
, sizeof(hd
->chksum
), blk
, BLKMULT
, 1);
407 * handle tar format specific -o options
409 * 0 if ok -1 otherwise
417 while ((opt
= opt_next()) != NULL
) {
418 if (strcmp(opt
->name
, TAR_OPTION
) ||
419 strcmp(opt
->value
, TAR_NODIR
)) {
421 "Unknown tar format -o option/value pair %s=%s",
422 opt
->name
, opt
->value
);
424 "%s=%s is the only supported tar format option",
425 TAR_OPTION
, TAR_NODIR
);
430 * we only support one option, and only when writing
432 if ((act
!= APPND
) && (act
!= ARCHIVE
)) {
433 tty_warn(1, "%s=%s is only supported when writing.",
434 opt
->name
, opt
->value
);
445 * extract the values out of block already determined to be a tar header.
446 * store the values in the ARCHD parameter.
452 tar_rd(ARCHD
*arcn
, char *buf
)
458 * we only get proper sized buffers passed to us
460 if (tar_id(buf
, BLKMULT
) < 0)
462 memset(arcn
, 0, sizeof(*arcn
));
463 arcn
->org_name
= arcn
->name
;
465 arcn
->sb
.st_nlink
= 1;
468 * copy out the name and values in the stat buffer
471 if (hd
->linkflag
!= LONGLINKTYPE
&& hd
->linkflag
!= LONGNAMETYPE
) {
472 arcn
->nlen
= expandname(arcn
->name
, sizeof(arcn
->name
),
473 &gnu_name_string
, &gnu_name_length
, hd
->name
,
475 arcn
->ln_nlen
= expandname(arcn
->ln_name
, sizeof(arcn
->ln_name
),
476 &gnu_link_string
, &gnu_link_length
, hd
->linkname
,
477 sizeof(hd
->linkname
));
479 arcn
->sb
.st_mode
= (mode_t
)(asc_ul(hd
->mode
,sizeof(hd
->mode
),OCT
) &
481 arcn
->sb
.st_uid
= (uid_t
)asc_ul(hd
->uid
, sizeof(hd
->uid
), OCT
);
482 arcn
->sb
.st_gid
= (gid_t
)asc_ul(hd
->gid
, sizeof(hd
->gid
), OCT
);
483 arcn
->sb
.st_size
= (off_t
)ASC_OFFT(hd
->size
, sizeof(hd
->size
), OCT
);
484 arcn
->sb
.st_mtime
= (time_t)asc_ul(hd
->mtime
, sizeof(hd
->mtime
), OCT
);
485 arcn
->sb
.st_ctime
= arcn
->sb
.st_atime
= arcn
->sb
.st_mtime
;
488 * have to look at the last character, it may be a '/' and that is used
489 * to encode this as a directory
491 pt
= &(arcn
->name
[arcn
->nlen
- 1]);
494 switch(hd
->linkflag
) {
497 * symbolic link, need to get the link name and set the type in
498 * the st_mode so -v printing will look correct.
500 arcn
->type
= PAX_SLK
;
501 arcn
->sb
.st_mode
|= S_IFLNK
;
505 * hard link, need to get the link name, set the type in the
506 * st_mode and st_nlink so -v printing will look better.
508 arcn
->type
= PAX_HLK
;
509 arcn
->sb
.st_nlink
= 2;
512 * no idea of what type this thing really points at, but
513 * we set something for printing only.
515 arcn
->sb
.st_mode
|= S_IFREG
;
520 * GNU long link/file; we tag these here and let the
521 * pax internals deal with it -- too ugly otherwise.
523 if (hd
->linkflag
!= LONGLINKTYPE
)
524 arcn
->type
= PAX_GLF
;
526 arcn
->type
= PAX_GLL
;
527 arcn
->pad
= TAR_PAD(arcn
->sb
.st_size
);
528 arcn
->skip
= arcn
->sb
.st_size
;
532 case DIRTYPE
: /* see below */
535 * If we have a trailing / this is a directory and NOT a file.
536 * Note: V7 tar doesn't actually have DIRTYPE, but it was
537 * reported that V7 archives using USTAR directories do exist.
539 if (*pt
== '/' || hd
->linkflag
== DIRTYPE
) {
541 * it is a directory, set the mode for -v printing
543 arcn
->type
= PAX_DIR
;
544 arcn
->sb
.st_mode
|= S_IFDIR
;
545 arcn
->sb
.st_nlink
= 2;
548 * have a file that will be followed by data. Set the
549 * skip value to the size field and calculate the size
552 arcn
->type
= PAX_REG
;
553 arcn
->sb
.st_mode
|= S_IFREG
;
554 arcn
->pad
= TAR_PAD(arcn
->sb
.st_size
);
555 arcn
->skip
= arcn
->sb
.st_size
;
561 * strip off any trailing slash.
572 * write a tar header for the file specified in the ARCHD to the archive.
573 * Have to check for file types that cannot be stored and file names that
574 * are too long. Be careful of the term (last arg) to ul_oct, each field
575 * of tar has it own spec for the termination character(s).
576 * ASSUMED: space after header in header block is zero filled
578 * 0 if file has data to be written after the header, 1 if file has NO
579 * data to write after the header, -1 if archive write failed
587 char hdblk
[sizeof(HD_TAR
)];
590 * check for those file system types which tar cannot store
595 * user asked that dirs not be written to the archive
601 tty_warn(1, "Tar cannot archive a character device %s",
606 "Tar cannot archive a block device %s", arcn
->org_name
);
609 tty_warn(1, "Tar cannot archive a socket %s", arcn
->org_name
);
612 tty_warn(1, "Tar cannot archive a fifo %s", arcn
->org_name
);
617 if (arcn
->ln_nlen
> (int)sizeof(hd
->linkname
)) {
618 tty_warn(1,"Link name too long for tar %s",
630 * check file name len, remember extra char for dirs (the / at the end)
633 if (arcn
->type
== PAX_DIR
)
635 if (len
>= (int)sizeof(hd
->name
)) {
636 tty_warn(1, "File name too long for tar %s", arcn
->name
);
641 * copy the data out of the ARCHD into the tar header based on the type
642 * of the file. Remember many tar readers want the unused fields to be
643 * padded with zero. We set the linkflag field (type), the linkname
644 * (or zero if not used),the size, and set the padding (if any) to be
645 * added after the file data (0 for all other types, as they only have
648 memset(hdblk
, 0, sizeof(hdblk
));
649 hd
= (HD_TAR
*)hdblk
;
650 strlcpy(hd
->name
, arcn
->name
, sizeof(hd
->name
));
653 if (arcn
->type
== PAX_DIR
) {
655 * directories are the same as files, except have a filename
656 * that ends with a /, we add the slash here. No data follows,
659 hd
->linkflag
= AREGTYPE
;
660 hd
->name
[len
-1] = '/';
661 if (ul_oct((u_long
)0L, hd
->size
, sizeof(hd
->size
), 1))
663 } else if (arcn
->type
== PAX_SLK
) {
665 * no data follows this file, so no pad
667 hd
->linkflag
= SYMTYPE
;
668 strlcpy(hd
->linkname
, arcn
->ln_name
, sizeof(hd
->linkname
));
669 if (ul_oct((u_long
)0L, hd
->size
, sizeof(hd
->size
), 1))
671 } else if ((arcn
->type
== PAX_HLK
) || (arcn
->type
== PAX_HRG
)) {
673 * no data follows this file, so no pad
675 hd
->linkflag
= LNKTYPE
;
676 strlcpy(hd
->linkname
, arcn
->ln_name
, sizeof(hd
->linkname
));
677 if (ul_oct((u_long
)0L, hd
->size
, sizeof(hd
->size
), 1))
681 * data follows this file, so set the pad
683 hd
->linkflag
= AREGTYPE
;
684 if (OFFT_OCT(arcn
->sb
.st_size
, hd
->size
, sizeof(hd
->size
), 1)) {
685 tty_warn(1,"File is too large for tar %s",
689 arcn
->pad
= TAR_PAD(arcn
->sb
.st_size
);
693 * copy those fields that are independent of the type
695 if (ul_oct((u_long
)arcn
->sb
.st_mode
, hd
->mode
, sizeof(hd
->mode
), 0) ||
696 ul_oct((u_long
)arcn
->sb
.st_uid
, hd
->uid
, sizeof(hd
->uid
), 0) ||
697 ul_oct((u_long
)arcn
->sb
.st_gid
, hd
->gid
, sizeof(hd
->gid
), 0) ||
698 ul_oct((u_long
)arcn
->sb
.st_mtime
, hd
->mtime
, sizeof(hd
->mtime
), 1))
702 * calculate and add the checksum, then write the header. A return of
703 * 0 tells the caller to now write the file data, 1 says no data needs
706 if (ul_oct(tar_chksm(hdblk
, sizeof(HD_TAR
)), hd
->chksum
,
707 sizeof(hd
->chksum
), 3))
708 goto out
; /* XXX Something's wrong here
709 * because a zero-byte file can
710 * cause this to be done and
711 * yet the resulting warning
714 if (wr_rdbuf(hdblk
, sizeof(HD_TAR
)) < 0)
716 if (wr_skip((off_t
)(BLKMULT
- sizeof(HD_TAR
))) < 0)
718 if ((arcn
->type
== PAX_CTG
) || (arcn
->type
== PAX_REG
))
724 * header field is out of range
726 tty_warn(1, "Tar header field is too small for %s", arcn
->org_name
);
731 * Routines for POSIX ustar
736 * initialization for ustar read
738 * 0 if ok, -1 otherwise
749 * initialization for ustar write
751 * 0 if ok, -1 otherwise
762 * determine if a block given to us is a valid ustar header. We have to
763 * be on the lookout for those pesky blocks of all zero's
765 * 0 if a ustar header, -1 otherwise
769 ustar_id(char *blk
, int size
)
775 hd
= (HD_USTAR
*)blk
;
778 * check for block of zero's first, a simple and fast test then check
779 * ustar magic cookie. We should use TMAGLEN, but some USTAR archive
780 * programs are fouled up and create archives missing the \0. Last we
781 * check the checksum. If ok we have to assume it is a valid header.
783 if (hd
->name
[0] == '\0')
785 if (strncmp(hd
->magic
, TMAGIC
, TMAGLEN
- 1) != 0)
787 /* This is GNU tar */
788 if (strncmp(hd
->magic
, "ustar ", 8) == 0 && !is_gnutar
&&
790 seen_gnu_warning
= 1;
792 "Trying to read GNU tar archive with GNU extensions and end-of-volume checks off");
794 return check_sum(hd
->chksum
, sizeof(hd
->chksum
), blk
, BLKMULT
, 0);
799 * extract the values out of block already determined to be a ustar header.
800 * store the values in the ARCHD parameter.
806 ustar_rd(ARCHD
*arcn
, char *buf
)
815 * we only get proper sized buffers
817 if (ustar_id(buf
, BLKMULT
) < 0)
820 memset(arcn
, 0, sizeof(*arcn
));
821 arcn
->org_name
= arcn
->name
;
823 arcn
->sb
.st_nlink
= 1;
824 hd
= (HD_USTAR
*)buf
;
827 * see if the filename is split into two parts. if, so joint the parts.
828 * we copy the prefix first and add a / between the prefix and name.
831 if (*(hd
->prefix
) != '\0') {
832 cnt
= strlcpy(arcn
->name
, hd
->prefix
, sizeof(arcn
->name
));
840 if (hd
->typeflag
!= LONGLINKTYPE
&& hd
->typeflag
!= LONGNAMETYPE
) {
841 arcn
->nlen
= expandname(dest
, sizeof(arcn
->name
) - cnt
,
842 &gnu_name_string
, &gnu_name_length
, hd
->name
,
843 sizeof(hd
->name
)) + cnt
;
844 arcn
->ln_nlen
= expandname(arcn
->ln_name
,
845 sizeof(arcn
->ln_name
), &gnu_link_string
, &gnu_link_length
,
846 hd
->linkname
, sizeof(hd
->linkname
));
850 * follow the spec to the letter. we should only have mode bits, strip
851 * off all other crud we may be passed.
853 arcn
->sb
.st_mode
= (mode_t
)(asc_ul(hd
->mode
, sizeof(hd
->mode
), OCT
) &
855 arcn
->sb
.st_size
= (off_t
)ASC_OFFT(hd
->size
, sizeof(hd
->size
), OCT
);
856 arcn
->sb
.st_mtime
= (time_t)asc_ul(hd
->mtime
, sizeof(hd
->mtime
), OCT
);
857 arcn
->sb
.st_ctime
= arcn
->sb
.st_atime
= arcn
->sb
.st_mtime
;
860 * If we can find the ascii names for gname and uname in the password
861 * and group files we will use the uid's and gid they bind. Otherwise
862 * we use the uid and gid values stored in the header. (This is what
863 * the posix spec wants).
865 hd
->gname
[sizeof(hd
->gname
) - 1] = '\0';
866 if (gid_from_group(hd
->gname
, &(arcn
->sb
.st_gid
)) < 0)
867 arcn
->sb
.st_gid
= (gid_t
)asc_ul(hd
->gid
, sizeof(hd
->gid
), OCT
);
868 hd
->uname
[sizeof(hd
->uname
) - 1] = '\0';
869 if (uid_from_user(hd
->uname
, &(arcn
->sb
.st_uid
)) < 0)
870 arcn
->sb
.st_uid
= (uid_t
)asc_ul(hd
->uid
, sizeof(hd
->uid
), OCT
);
873 * set the defaults, these may be changed depending on the file type
877 arcn
->sb
.st_rdev
= (dev_t
)0;
880 * set the mode and PAX type according to the typeflag in the header
882 switch(hd
->typeflag
) {
884 arcn
->type
= PAX_FIF
;
885 arcn
->sb
.st_mode
|= S_IFIFO
;
888 arcn
->type
= PAX_DIR
;
889 arcn
->sb
.st_mode
|= S_IFDIR
;
890 arcn
->sb
.st_nlink
= 2;
893 * Some programs that create ustar archives append a '/'
894 * to the pathname for directories. This clearly violates
895 * ustar specs, but we will silently strip it off anyway.
897 if (arcn
->name
[arcn
->nlen
- 1] == '/')
898 arcn
->name
[--arcn
->nlen
] = '\0';
903 * this type requires the rdev field to be set.
905 if (hd
->typeflag
== BLKTYPE
) {
906 arcn
->type
= PAX_BLK
;
907 arcn
->sb
.st_mode
|= S_IFBLK
;
909 arcn
->type
= PAX_CHR
;
910 arcn
->sb
.st_mode
|= S_IFCHR
;
912 devmajor
= (dev_t
)asc_ul(hd
->devmajor
,sizeof(hd
->devmajor
),OCT
);
913 devminor
= (dev_t
)asc_ul(hd
->devminor
,sizeof(hd
->devminor
),OCT
);
914 arcn
->sb
.st_rdev
= TODEV(devmajor
, devminor
);
918 if (hd
->typeflag
== SYMTYPE
) {
919 arcn
->type
= PAX_SLK
;
920 arcn
->sb
.st_mode
|= S_IFLNK
;
922 arcn
->type
= PAX_HLK
;
924 * so printing looks better
926 arcn
->sb
.st_mode
|= S_IFREG
;
927 arcn
->sb
.st_nlink
= 2;
934 * GNU long link/file; we tag these here and let the
935 * pax internals deal with it -- too ugly otherwise.
937 if (hd
->typeflag
!= LONGLINKTYPE
)
938 arcn
->type
= PAX_GLF
;
940 arcn
->type
= PAX_GLL
;
941 arcn
->pad
= TAR_PAD(arcn
->sb
.st_size
);
942 arcn
->skip
= arcn
->sb
.st_size
;
944 tty_warn(1, "GNU Long %s found in posix ustar archive.",
945 hd
->typeflag
== LONGLINKTYPE
? "Link" : "File");
953 * these types have file data that follows. Set the skip and
956 arcn
->type
= PAX_REG
;
957 arcn
->pad
= TAR_PAD(arcn
->sb
.st_size
);
958 arcn
->skip
= arcn
->sb
.st_size
;
959 arcn
->sb
.st_mode
|= S_IFREG
;
966 expandname(char *buf
, size_t len
, char **gnu_name
, size_t *gnu_length
,
967 const char *name
, size_t nlen
)
970 len
= strlcpy(buf
, *gnu_name
, len
);
977 len
= strlcpy(buf
, name
, len
);
983 longlink(ARCHD
*arcn
, int type
)
987 (void)memset(&larc
, 0, sizeof(larc
));
990 larc
.nlen
= strlcpy(larc
.name
, LONG_LINK
, sizeof(larc
.name
));
994 gnu_hack_string
= arcn
->ln_name
;
995 gnu_hack_len
= arcn
->ln_nlen
+ 1;
998 gnu_hack_string
= arcn
->name
;
999 gnu_hack_len
= arcn
->nlen
+ 1;
1002 errx(1, "Invalid type in GNU longlink %d\n", type
);
1006 * We need a longlink now.
1013 * write a ustar header for the file specified in the ARCHD to the archive
1014 * Have to check for file types that cannot be stored and file names that
1015 * are too long. Be careful of the term (last arg) to ul_oct, we only use
1016 * '\0' for the termination character (this is different than picky tar)
1017 * ASSUMED: space after header in header block is zero filled
1019 * 0 if file has data to be written after the header, 1 if file has NO
1020 * data to write after the header, -1 if archive write failed
1024 size_err(const char *what
, ARCHD
*arcn
)
1027 * header field is out of range
1029 tty_warn(1, "Ustar %s header field is too small for %s",
1030 what
, arcn
->org_name
);
1035 ustar_wr(ARCHD
*arcn
)
1039 char hdblk
[sizeof(HD_USTAR
)];
1040 const char *user
, *group
;
1042 switch (arcn
->type
) {
1045 * check for those file system types ustar cannot store
1048 tty_warn(1, "Ustar cannot archive a socket %s",
1056 * check the length of the linkname
1058 if (arcn
->ln_nlen
>= (int)sizeof(hd
->linkname
)) {
1060 longlink(arcn
, PAX_GLL
);
1062 tty_warn(1, "Link name too long for ustar %s",
1073 * split the path name into prefix and name fields (if needed). if
1074 * pt != arcn->name, the name has to be split
1076 if ((pt
= name_split(arcn
->name
, arcn
->nlen
)) == NULL
) {
1078 longlink(arcn
, PAX_GLF
);
1081 tty_warn(1, "File name too long for ustar %s",
1088 * zero out the header so we don't have to worry about zero fill below
1090 memset(hdblk
, 0, sizeof(hdblk
));
1091 hd
= (HD_USTAR
*)hdblk
;
1095 * split the name, or zero out the prefix
1097 if (pt
!= arcn
->name
) {
1099 * name was split, pt points at the / where the split is to
1100 * occur, we remove the / and copy the first part to the prefix
1103 strlcpy(hd
->prefix
, arcn
->name
, sizeof(hd
->prefix
));
1108 * copy the name part. this may be the whole path or the part after
1111 strlcpy(hd
->name
, pt
, sizeof(hd
->name
));
1114 * set the fields in the header that are type dependent
1116 switch(arcn
->type
) {
1118 hd
->typeflag
= DIRTYPE
;
1119 if (ul_oct((u_long
)0L, hd
->size
, sizeof(hd
->size
), 3))
1120 return size_err("DIRTYPE", arcn
);
1124 if (arcn
->type
== PAX_CHR
)
1125 hd
->typeflag
= CHRTYPE
;
1127 hd
->typeflag
= BLKTYPE
;
1128 if (ul_oct((u_long
)MAJOR(arcn
->sb
.st_rdev
), hd
->devmajor
,
1129 sizeof(hd
->devmajor
), 3) ||
1130 ul_oct((u_long
)MINOR(arcn
->sb
.st_rdev
), hd
->devminor
,
1131 sizeof(hd
->devminor
), 3) ||
1132 ul_oct((u_long
)0L, hd
->size
, sizeof(hd
->size
), 3))
1133 return size_err("DEVTYPE", arcn
);
1136 hd
->typeflag
= FIFOTYPE
;
1137 if (ul_oct((u_long
)0L, hd
->size
, sizeof(hd
->size
), 3))
1138 return size_err("FIFOTYPE", arcn
);
1144 if (arcn
->type
== PAX_SLK
)
1145 hd
->typeflag
= SYMTYPE
;
1146 else if (arcn
->type
== PAX_GLL
)
1147 hd
->typeflag
= LONGLINKTYPE
;
1149 hd
->typeflag
= LNKTYPE
;
1150 strlcpy(hd
->linkname
, arcn
->ln_name
, sizeof(hd
->linkname
));
1151 if (ul_oct((u_long
)gnu_hack_len
, hd
->size
,
1152 sizeof(hd
->size
), 3))
1153 return size_err("LINKTYPE", arcn
);
1160 * file data with this type, set the padding
1162 if (arcn
->type
== PAX_GLF
) {
1163 hd
->typeflag
= LONGNAMETYPE
;
1164 arcn
->pad
= TAR_PAD(gnu_hack_len
);
1165 if (OFFT_OCT((u_long
)gnu_hack_len
, hd
->size
,
1166 sizeof(hd
->size
), 3)) {
1167 tty_warn(1,"File is too long for ustar %s",
1172 if (arcn
->type
== PAX_CTG
)
1173 hd
->typeflag
= CONTTYPE
;
1175 hd
->typeflag
= REGTYPE
;
1176 arcn
->pad
= TAR_PAD(arcn
->sb
.st_size
);
1177 if (OFFT_OCT(arcn
->sb
.st_size
, hd
->size
,
1178 sizeof(hd
->size
), 3)) {
1179 tty_warn(1,"File is too long for ustar %s",
1187 strncpy(hd
->magic
, TMAGIC
, TMAGLEN
);
1189 hd
->magic
[TMAGLEN
- 1] = hd
->version
[0] = ' ';
1191 strncpy(hd
->version
, TVERSION
, TVERSLEN
);
1194 * set the remaining fields. Some versions want all 16 bits of mode
1195 * we better humor them (they really do not meet spec though)....
1197 if (ul_oct((u_long
)arcn
->sb
.st_mode
, hd
->mode
, sizeof(hd
->mode
), 3))
1198 return size_err("MODE", arcn
);
1199 if (ul_oct((u_long
)arcn
->sb
.st_uid
, hd
->uid
, sizeof(hd
->uid
), 3))
1200 return size_err("UID", arcn
);
1201 if (ul_oct((u_long
)arcn
->sb
.st_gid
, hd
->gid
, sizeof(hd
->gid
), 3))
1202 return size_err("GID", arcn
);
1203 if (ul_oct((u_long
)arcn
->sb
.st_mtime
,hd
->mtime
,sizeof(hd
->mtime
),3))
1204 return size_err("MTIME", arcn
);
1205 user
= user_from_uid(arcn
->sb
.st_uid
, 1);
1206 group
= group_from_gid(arcn
->sb
.st_gid
, 1);
1207 strncpy(hd
->uname
, user
? user
: "", sizeof(hd
->uname
));
1208 strncpy(hd
->gname
, group
? group
: "", sizeof(hd
->gname
));
1211 * calculate and store the checksum write the header to the archive
1212 * return 0 tells the caller to now write the file data, 1 says no data
1213 * needs to be written
1215 if (ul_oct(tar_chksm(hdblk
, sizeof(HD_USTAR
)), hd
->chksum
,
1216 sizeof(hd
->chksum
), 3))
1217 return size_err("CHKSUM", arcn
);
1218 if (wr_rdbuf(hdblk
, sizeof(HD_USTAR
)) < 0)
1220 if (wr_skip((off_t
)(BLKMULT
- sizeof(HD_USTAR
))) < 0)
1222 if (gnu_hack_string
) {
1223 int res
= wr_rdbuf(gnu_hack_string
, gnu_hack_len
);
1224 int pad
= gnu_hack_len
;
1225 gnu_hack_string
= NULL
;
1229 if (wr_skip((off_t
)(BLKMULT
- (pad
% BLKMULT
))) < 0)
1232 if ((arcn
->type
== PAX_CTG
) || (arcn
->type
== PAX_REG
))
1239 * see if the name has to be split for storage in a ustar header. We try
1240 * to fit the entire name in the name field without splitting if we can.
1241 * The split point is always at a /
1243 * character pointer to split point (always the / that is to be removed
1244 * if the split is not needed, the points is set to the start of the file
1245 * name (it would violate the spec to split there). A NULL is returned if
1246 * the file name is too long
1250 name_split(char *name
, int len
)
1255 * check to see if the file name is small enough to fit in the name
1256 * field. if so just return a pointer to the name.
1261 * GNU tar does not honor the prefix+name mode if the magic
1262 * is not "ustar\0". So in GNU tar compatibility mode, we don't
1263 * split the filename into prefix+name because we are setting
1264 * the magic to "ustar " as GNU tar does. This of course will
1265 * end up creating a LongLink record in cases where it does not
1266 * really need do, but we are behaving like GNU tar after all.
1268 if (is_gnutar
|| len
> (TPFSZ
+ TNMSZ
))
1272 * we start looking at the biggest sized piece that fits in the name
1273 * field. We walk forward looking for a slash to split at. The idea is
1274 * to find the biggest piece to fit in the name field (or the smallest
1275 * prefix we can find) (the -1 is correct the biggest piece would
1276 * include the slash between the two parts that gets thrown away)
1278 start
= name
+ len
- TNMSZ
;
1279 while ((*start
!= '\0') && (*start
!= '/'))
1283 * if we hit the end of the string, this name cannot be split, so we
1284 * cannot store this file.
1291 * NOTE: /str where the length of str == TNMSZ cannot be stored under
1292 * the p1003.1-1990 spec for ustar. We could force a prefix of / and
1293 * the file would then expand on extract to //str. The len == 0 below
1294 * makes this special case follow the spec to the letter.
1296 if ((len
>= TPFSZ
) || (len
== 0))
1300 * ok have a split point, return it to the caller
1306 * convert a glob into a RE, and add it to the list. we convert to
1307 * four different RE's (because we're using BRE's and can't use |
1308 * alternation :-() with this padding:
1315 tar_gnutar_exclude_one(const char *line
, size_t len
)
1317 /* 2 * buffer len + nul */
1318 char sbuf
[MAXPATHLEN
* 2 + 1];
1319 /* + / + // + .*""/\/ + \/.* */
1320 char rabuf
[MAXPATHLEN
* 2 + 1 + 1 + 2 + 4 + 4];
1324 if (line
[len
- 1] == '\n')
1326 for (i
= 0; i
< len
; i
++) {
1328 * convert glob to regexp, escaping everything
1332 else if (line
[i
] == '?') {
1335 } else if (!isalnum((unsigned char)line
[i
]) &&
1336 !isblank((unsigned char)line
[i
]))
1338 sbuf
[j
++] = line
[i
];
1341 /* don't need the .*\/ ones if we start with /, i guess */
1342 if (line
[0] != '/') {
1343 (void)snprintf(rabuf
, sizeof rabuf
, "/.*\\/%s$//", sbuf
);
1344 if (rep_add(rabuf
) < 0)
1346 (void)snprintf(rabuf
, sizeof rabuf
, "/.*\\/%s\\/.*//", sbuf
);
1347 if (rep_add(rabuf
) < 0)
1351 (void)snprintf(rabuf
, sizeof rabuf
, "/^%s$//", sbuf
);
1352 if (rep_add(rabuf
) < 0)
1354 (void)snprintf(rabuf
, sizeof rabuf
, "/^%s\\/.*//", sbuf
);
1355 if (rep_add(rabuf
) < 0)
1362 * deal with GNU tar -X/--exclude-from & --exclude switchs. basically,
1363 * we go through each line of the file, building a string from the "glob"
1364 * lines in the file into RE lines, of the form `/^RE$//', which we pass
1365 * to rep_add(), which will add a empty replacement (exclusion), for the
1369 tar_gnutar_minus_minus_exclude(path
)
1372 size_t len
= strlen(path
);
1374 if (len
> MAXPATHLEN
)
1375 tty_warn(0, "pathname too long: %s", path
);
1377 return (tar_gnutar_exclude_one(path
, len
));
1381 tar_gnutar_X_compat(path
)
1389 fp
= fopen(path
, "r");
1391 tty_warn(1, "cannot open %s: %s", path
,
1396 while ((line
= fgetln(fp
, &len
))) {
1398 if (len
> MAXPATHLEN
) {
1399 tty_warn(0, "pathname too long, line %d of %s",
1402 if (tar_gnutar_exclude_one(line
, len
))