1 /* $NetBSD: tar.c,v 1.70 2012/08/09 08:09:22 christos 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.70 2012/08/09 08:09:22 christos 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 uint32_t tar_chksm(char *, int);
74 static char *name_split(char *, int);
75 static int u32_oct(uintmax_t, char *, int, int);
76 static int umax_oct(uintmax_t, char *, int, int);
77 static int tar_gnutar_exclude_one(const char *, size_t);
78 static int check_sum(char *, size_t, char *, size_t, int);
81 * Routines common to all versions of tar
84 static int tar_nodir
; /* do not write dirs under old tar */
85 int is_gnutar
; /* behave like gnu tar; enable gnu
86 * extensions and skip end-of-volume
89 static int seen_gnu_warning
; /* Have we warned yet? */
90 static char *gnu_hack_string
; /* ././@LongLink hackery */
91 static int gnu_hack_len
; /* len of gnu_hack_string */
92 char *gnu_name_string
; /* ././@LongLink hackery name */
93 char *gnu_link_string
; /* ././@LongLink hackery link */
94 size_t gnu_name_length
; /* ././@LongLink hackery name */
95 size_t gnu_link_length
; /* ././@LongLink hackery link */
96 static int gnu_short_trailer
; /* gnu short trailer */
98 static const char LONG_LINK
[] = "././@LongLink";
101 char DEV_0
[] = "/dev/rst0";
102 char DEV_1
[] = "/dev/rst1";
103 char DEV_4
[] = "/dev/rst4";
104 char DEV_5
[] = "/dev/rst5";
105 char DEV_7
[] = "/dev/rst7";
106 char DEV_8
[] = "/dev/rst8";
110 check_sum(char *hd
, size_t hdlen
, char *bl
, size_t bllen
, int quiet
)
114 hdck
= asc_u32(hd
, hdlen
, OCT
);
115 blck
= tar_chksm(bl
, bllen
);
119 tty_warn(0, "Header checksum %o does not match %o",
129 * add the tar trailer of two null blocks
131 * 0 if ok, -1 otherwise (what wr_skip returns)
137 return wr_skip((off_t
)(NULLCNT
* BLKMULT
));
142 * no cleanup needed here, just return size of trailer (for append)
144 * size of trailer BLKMULT
150 return (off_t
)((gnu_short_trailer
? 1 : NULLCNT
) * BLKMULT
);
155 * Called to determine if a header block is a valid trailer. We are passed
156 * the block, the in_sync flag (which tells us we are in resync mode;
157 * looking for a valid header), and cnt (which starts at zero) which is
158 * used to count the number of empty blocks we have seen so far.
160 * 0 if a valid trailer, -1 if not a valid trailer, or 1 if the block
161 * could never contain a header.
165 tar_trail(char *buf
, int in_resync
, int *cnt
)
169 gnu_short_trailer
= 0;
171 * look for all zero, trailer is two consecutive blocks of zero
173 for (i
= 0; i
< BLKMULT
; ++i
) {
179 * if not all zero it is not a trailer, but MIGHT be a header.
185 * When given a zero block, we must be careful!
186 * If we are not in resync mode, check for the trailer. Have to watch
187 * out that we do not mis-identify file data as the trailer, so we do
188 * NOT try to id a trailer during resync mode. During resync mode we
189 * might as well throw this block out since a valid header can NEVER be
190 * a block of all 0 (we must have a valid file name).
195 * old GNU tar (up through 1.13) only writes one block of
196 * trailers, so we pretend we got another
199 gnu_short_trailer
= 1;
210 * convert an uintmax_t to an octal string. many oddball field
211 * termination characters are used by the various versions of tar in the
212 * different fields. term selects which kind to use. str is '0' padded
213 * at the front to len. we are unable to use only one format as many old
214 * tar readers are very cranky about this.
216 * 0 if the number fit into the string, -1 otherwise
220 u32_oct(uintmax_t val
, char *str
, int len
, int term
)
226 if (p
&& p
!= TOP_HALF
)
232 * term selects the appropriate character(s) for the end of the string
254 * convert and blank pad if there is space
257 *pt
-- = '0' + (char)(val
& 0x7);
258 if ((val
= val
>> 3) == 0)
271 * convert an unsigned long long to an octal string. one of many oddball
272 * field termination characters are used by the various versions of tar
273 * in the different fields. term selects which kind to use. str is '0'
274 * padded at the front to len. we are unable to use only one format as
275 * many old tar readers are very cranky about this.
277 * 0 if the number fit into the string, -1 otherwise
281 umax_oct(uintmax_t val
, char *str
, int len
, int term
)
286 * term selects the appropriate character(s) for the end of the string
308 * convert and blank pad if there is space
311 *pt
-- = '0' + (char)(val
& 0x7);
312 if ((val
= val
>> 3) == 0)
325 * calculate the checksum for a tar block counting the checksum field as
326 * all blanks (BLNKSUM is that value pre-calculated, the sum of 8 blanks).
327 * NOTE: we use len to short circuit summing 0's on write since we ALWAYS
328 * pad headers with 0.
330 * unsigned long checksum
334 tar_chksm(char *blk
, int len
)
338 uint32_t chksm
= BLNKSUM
; /* initial value is checksum field sum */
341 * add the part of the block before the checksum field
344 stop
= blk
+ CHK_OFFSET
;
346 chksm
+= (uint32_t)(*pt
++ & 0xff);
348 * move past the checksum field and keep going, spec counts the
349 * checksum field as the sum of 8 blanks (which is pre-computed as
351 * ASSUMED: len is greater than CHK_OFFSET. (len is where our 0 padding
352 * starts, no point in summing zero's)
357 chksm
+= (uint32_t)(*pt
++ & 0xff);
362 * Routines for old BSD style tar (also made portable to sysV tar)
367 * determine if a block given to us is a valid tar header (and not a USTAR
368 * header). We have to be on the lookout for those pesky blocks of all
371 * 0 if a tar header, -1 otherwise
375 tar_id(char *blk
, int size
)
379 static int is_ustar
= -1;
384 uhd
= (HD_USTAR
*)blk
;
387 * check for block of zero's first, a simple and fast test, then make
388 * sure this is not a ustar header by looking for the ustar magic
389 * cookie. We should use TMAGLEN, but some USTAR archive programs are
390 * wrong and create archives missing the \0. Last we check the
391 * checksum. If this is ok we have to assume it is a valid header.
393 if (hd
->name
[0] == '\0')
395 if (strncmp(uhd
->magic
, TMAGIC
, TMAGLEN
- 1) == 0) {
396 if (is_ustar
== -1) {
401 "Busted tar archive: has both ustar and old tar "
405 return check_sum(hd
->chksum
, sizeof(hd
->chksum
), blk
, BLKMULT
, 1);
410 * handle tar format specific -o options
412 * 0 if ok -1 otherwise
420 while ((opt
= opt_next()) != NULL
) {
421 if (strcmp(opt
->name
, TAR_OPTION
) ||
422 strcmp(opt
->value
, TAR_NODIR
)) {
424 "Unknown tar format -o option/value pair %s=%s",
425 opt
->name
, opt
->value
);
427 "%s=%s is the only supported tar format option",
428 TAR_OPTION
, TAR_NODIR
);
433 * we only support one option, and only when writing
435 if ((act
!= APPND
) && (act
!= ARCHIVE
)) {
436 tty_warn(1, "%s=%s is only supported when writing.",
437 opt
->name
, opt
->value
);
448 * extract the values out of block already determined to be a tar header.
449 * store the values in the ARCHD parameter.
455 tar_rd(ARCHD
*arcn
, char *buf
)
461 * we only get proper sized buffers passed to us
463 if (tar_id(buf
, BLKMULT
) < 0)
465 memset(arcn
, 0, sizeof(*arcn
));
466 arcn
->org_name
= arcn
->name
;
468 arcn
->sb
.st_nlink
= 1;
471 * copy out the name and values in the stat buffer
474 if (hd
->linkflag
!= LONGLINKTYPE
&& hd
->linkflag
!= LONGNAMETYPE
) {
475 arcn
->nlen
= expandname(arcn
->name
, sizeof(arcn
->name
),
476 &gnu_name_string
, &gnu_name_length
, hd
->name
,
478 arcn
->ln_nlen
= expandname(arcn
->ln_name
, sizeof(arcn
->ln_name
),
479 &gnu_link_string
, &gnu_link_length
, hd
->linkname
,
480 sizeof(hd
->linkname
));
482 arcn
->sb
.st_mode
= (mode_t
)(asc_u32(hd
->mode
,sizeof(hd
->mode
),OCT
) &
484 arcn
->sb
.st_uid
= (uid_t
)asc_u32(hd
->uid
, sizeof(hd
->uid
), OCT
);
485 arcn
->sb
.st_gid
= (gid_t
)asc_u32(hd
->gid
, sizeof(hd
->gid
), OCT
);
486 arcn
->sb
.st_size
= (off_t
)ASC_OFFT(hd
->size
, sizeof(hd
->size
), OCT
);
487 arcn
->sb
.st_mtime
= (time_t)(int32_t)asc_u32(hd
->mtime
, sizeof(hd
->mtime
), OCT
);
488 arcn
->sb
.st_ctime
= arcn
->sb
.st_atime
= arcn
->sb
.st_mtime
;
491 * have to look at the last character, it may be a '/' and that is used
492 * to encode this as a directory
494 pt
= &(arcn
->name
[arcn
->nlen
- 1]);
497 switch(hd
->linkflag
) {
500 * symbolic link, need to get the link name and set the type in
501 * the st_mode so -v printing will look correct.
503 arcn
->type
= PAX_SLK
;
504 arcn
->sb
.st_mode
|= S_IFLNK
;
508 * hard link, need to get the link name, set the type in the
509 * st_mode and st_nlink so -v printing will look better.
511 arcn
->type
= PAX_HLK
;
512 arcn
->sb
.st_nlink
= 2;
515 * no idea of what type this thing really points at, but
516 * we set something for printing only.
518 arcn
->sb
.st_mode
|= S_IFREG
;
523 * GNU long link/file; we tag these here and let the
524 * pax internals deal with it -- too ugly otherwise.
526 if (hd
->linkflag
!= LONGLINKTYPE
)
527 arcn
->type
= PAX_GLF
;
529 arcn
->type
= PAX_GLL
;
530 arcn
->pad
= TAR_PAD(arcn
->sb
.st_size
);
531 arcn
->skip
= arcn
->sb
.st_size
;
535 case DIRTYPE
: /* see below */
538 * If we have a trailing / this is a directory and NOT a file.
539 * Note: V7 tar doesn't actually have DIRTYPE, but it was
540 * reported that V7 archives using USTAR directories do exist.
542 if (*pt
== '/' || hd
->linkflag
== DIRTYPE
) {
544 * it is a directory, set the mode for -v printing
546 arcn
->type
= PAX_DIR
;
547 arcn
->sb
.st_mode
|= S_IFDIR
;
548 arcn
->sb
.st_nlink
= 2;
551 * have a file that will be followed by data. Set the
552 * skip value to the size field and calculate the size
555 arcn
->type
= PAX_REG
;
556 arcn
->sb
.st_mode
|= S_IFREG
;
557 arcn
->pad
= TAR_PAD(arcn
->sb
.st_size
);
558 arcn
->skip
= arcn
->sb
.st_size
;
564 * strip off any trailing slash.
575 * write a tar header for the file specified in the ARCHD to the archive.
576 * Have to check for file types that cannot be stored and file names that
577 * are too long. Be careful of the term (last arg) to u32_oct, each field
578 * of tar has it own spec for the termination character(s).
579 * ASSUMED: space after header in header block is zero filled
581 * 0 if file has data to be written after the header, 1 if file has NO
582 * data to write after the header, -1 if archive write failed
590 char hdblk
[sizeof(HD_TAR
)];
593 * check for those file system types which tar cannot store
598 * user asked that dirs not be written to the archive
604 tty_warn(1, "Tar cannot archive a character device %s",
609 "Tar cannot archive a block device %s", arcn
->org_name
);
612 tty_warn(1, "Tar cannot archive a socket %s", arcn
->org_name
);
615 tty_warn(1, "Tar cannot archive a fifo %s", arcn
->org_name
);
620 if (arcn
->ln_nlen
> (int)sizeof(hd
->linkname
)) {
621 tty_warn(1,"Link name too long for tar %s",
633 * check file name len, remember extra char for dirs (the / at the end)
636 if (arcn
->type
== PAX_DIR
)
638 if (len
>= (int)sizeof(hd
->name
)) {
639 tty_warn(1, "File name too long for tar %s", arcn
->name
);
644 * copy the data out of the ARCHD into the tar header based on the type
645 * of the file. Remember many tar readers want the unused fields to be
646 * padded with zero. We set the linkflag field (type), the linkname
647 * (or zero if not used),the size, and set the padding (if any) to be
648 * added after the file data (0 for all other types, as they only have
651 memset(hdblk
, 0, sizeof(hdblk
));
652 hd
= (HD_TAR
*)hdblk
;
653 strlcpy(hd
->name
, arcn
->name
, sizeof(hd
->name
));
656 if (arcn
->type
== PAX_DIR
) {
658 * directories are the same as files, except have a filename
659 * that ends with a /, we add the slash here. No data follows,
662 hd
->linkflag
= AREGTYPE
;
663 hd
->name
[len
-1] = '/';
664 if (u32_oct((uintmax_t)0L, hd
->size
, sizeof(hd
->size
), 1))
666 } else if (arcn
->type
== PAX_SLK
) {
668 * no data follows this file, so no pad
670 hd
->linkflag
= SYMTYPE
;
671 strlcpy(hd
->linkname
, arcn
->ln_name
, sizeof(hd
->linkname
));
672 if (u32_oct((uintmax_t)0L, hd
->size
, sizeof(hd
->size
), 1))
674 } else if ((arcn
->type
== PAX_HLK
) || (arcn
->type
== PAX_HRG
)) {
676 * no data follows this file, so no pad
678 hd
->linkflag
= LNKTYPE
;
679 strlcpy(hd
->linkname
, arcn
->ln_name
, sizeof(hd
->linkname
));
680 if (u32_oct((uintmax_t)0L, hd
->size
, sizeof(hd
->size
), 1))
684 * data follows this file, so set the pad
686 hd
->linkflag
= AREGTYPE
;
687 if (OFFT_OCT(arcn
->sb
.st_size
, hd
->size
, sizeof(hd
->size
), 1)) {
688 tty_warn(1,"File is too large for tar %s",
692 arcn
->pad
= TAR_PAD(arcn
->sb
.st_size
);
696 * copy those fields that are independent of the type
698 if (u32_oct((uintmax_t)arcn
->sb
.st_mode
, hd
->mode
, sizeof(hd
->mode
), 0) ||
699 u32_oct((uintmax_t)arcn
->sb
.st_uid
, hd
->uid
, sizeof(hd
->uid
), 0) ||
700 u32_oct((uintmax_t)arcn
->sb
.st_gid
, hd
->gid
, sizeof(hd
->gid
), 0) ||
701 u32_oct((uintmax_t)arcn
->sb
.st_mtime
, hd
->mtime
, sizeof(hd
->mtime
), 1))
705 * calculate and add the checksum, then write the header. A return of
706 * 0 tells the caller to now write the file data, 1 says no data needs
709 if (u32_oct(tar_chksm(hdblk
, sizeof(HD_TAR
)), hd
->chksum
,
710 sizeof(hd
->chksum
), 3))
711 goto out
; /* XXX Something's wrong here
712 * because a zero-byte file can
713 * cause this to be done and
714 * yet the resulting warning
717 if (wr_rdbuf(hdblk
, sizeof(HD_TAR
)) < 0)
719 if (wr_skip((off_t
)(BLKMULT
- sizeof(HD_TAR
))) < 0)
721 if ((arcn
->type
== PAX_CTG
) || (arcn
->type
== PAX_REG
))
727 * header field is out of range
729 tty_warn(1, "Tar header field is too small for %s", arcn
->org_name
);
734 * Routines for POSIX ustar
739 * initialization for ustar read
741 * 0 if ok, -1 otherwise
752 * initialization for ustar write
754 * 0 if ok, -1 otherwise
765 * determine if a block given to us is a valid ustar header. We have to
766 * be on the lookout for those pesky blocks of all zero's
768 * 0 if a ustar header, -1 otherwise
772 ustar_id(char *blk
, int size
)
778 hd
= (HD_USTAR
*)blk
;
781 * check for block of zero's first, a simple and fast test then check
782 * ustar magic cookie. We should use TMAGLEN, but some USTAR archive
783 * programs are fouled up and create archives missing the \0. Last we
784 * check the checksum. If ok we have to assume it is a valid header.
786 if (hd
->name
[0] == '\0')
788 if (strncmp(hd
->magic
, TMAGIC
, TMAGLEN
- 1) != 0)
790 /* This is GNU tar */
791 if (strncmp(hd
->magic
, "ustar ", 8) == 0 && !is_gnutar
&&
793 seen_gnu_warning
= 1;
795 "Trying to read GNU tar archive with GNU extensions and end-of-volume checks off");
797 return check_sum(hd
->chksum
, sizeof(hd
->chksum
), blk
, BLKMULT
, 0);
802 * extract the values out of block already determined to be a ustar header.
803 * store the values in the ARCHD parameter.
809 ustar_rd(ARCHD
*arcn
, char *buf
)
818 * we only get proper sized buffers
820 if (ustar_id(buf
, BLKMULT
) < 0)
823 memset(arcn
, 0, sizeof(*arcn
));
824 arcn
->org_name
= arcn
->name
;
826 arcn
->sb
.st_nlink
= 1;
827 hd
= (HD_USTAR
*)buf
;
830 * see if the filename is split into two parts. if, so joint the parts.
831 * we copy the prefix first and add a / between the prefix and name.
834 if (*(hd
->prefix
) != '\0') {
835 cnt
= strlcpy(arcn
->name
, hd
->prefix
, sizeof(arcn
->name
));
843 if (hd
->typeflag
!= LONGLINKTYPE
&& hd
->typeflag
!= LONGNAMETYPE
) {
844 arcn
->nlen
= expandname(dest
, sizeof(arcn
->name
) - cnt
,
845 &gnu_name_string
, &gnu_name_length
, hd
->name
,
846 sizeof(hd
->name
)) + cnt
;
847 arcn
->ln_nlen
= expandname(arcn
->ln_name
,
848 sizeof(arcn
->ln_name
), &gnu_link_string
, &gnu_link_length
,
849 hd
->linkname
, sizeof(hd
->linkname
));
853 * follow the spec to the letter. we should only have mode bits, strip
854 * off all other crud we may be passed.
856 arcn
->sb
.st_mode
= (mode_t
)(asc_u32(hd
->mode
, sizeof(hd
->mode
), OCT
) &
858 arcn
->sb
.st_size
= (off_t
)ASC_OFFT(hd
->size
, sizeof(hd
->size
), OCT
);
859 arcn
->sb
.st_mtime
= (time_t)(int32_t)asc_u32(hd
->mtime
, sizeof(hd
->mtime
), OCT
);
860 arcn
->sb
.st_ctime
= arcn
->sb
.st_atime
= arcn
->sb
.st_mtime
;
863 * If we can find the ascii names for gname and uname in the password
864 * and group files we will use the uid's and gid they bind. Otherwise
865 * we use the uid and gid values stored in the header. (This is what
866 * the posix spec wants).
868 hd
->gname
[sizeof(hd
->gname
) - 1] = '\0';
869 if (gid_from_group(hd
->gname
, &(arcn
->sb
.st_gid
)) < 0)
870 arcn
->sb
.st_gid
= (gid_t
)asc_u32(hd
->gid
, sizeof(hd
->gid
), OCT
);
871 hd
->uname
[sizeof(hd
->uname
) - 1] = '\0';
872 if (uid_from_user(hd
->uname
, &(arcn
->sb
.st_uid
)) < 0)
873 arcn
->sb
.st_uid
= (uid_t
)asc_u32(hd
->uid
, sizeof(hd
->uid
), OCT
);
876 * set the defaults, these may be changed depending on the file type
880 arcn
->sb
.st_rdev
= (dev_t
)0;
883 * set the mode and PAX type according to the typeflag in the header
885 switch(hd
->typeflag
) {
887 arcn
->type
= PAX_FIF
;
888 arcn
->sb
.st_mode
|= S_IFIFO
;
891 arcn
->type
= PAX_DIR
;
892 arcn
->sb
.st_mode
|= S_IFDIR
;
893 arcn
->sb
.st_nlink
= 2;
896 * Some programs that create ustar archives append a '/'
897 * to the pathname for directories. This clearly violates
898 * ustar specs, but we will silently strip it off anyway.
900 if (arcn
->name
[arcn
->nlen
- 1] == '/')
901 arcn
->name
[--arcn
->nlen
] = '\0';
906 * this type requires the rdev field to be set.
908 if (hd
->typeflag
== BLKTYPE
) {
909 arcn
->type
= PAX_BLK
;
910 arcn
->sb
.st_mode
|= S_IFBLK
;
912 arcn
->type
= PAX_CHR
;
913 arcn
->sb
.st_mode
|= S_IFCHR
;
915 devmajor
= (dev_t
)asc_u32(hd
->devmajor
,sizeof(hd
->devmajor
),OCT
);
916 devminor
= (dev_t
)asc_u32(hd
->devminor
,sizeof(hd
->devminor
),OCT
);
917 arcn
->sb
.st_rdev
= TODEV(devmajor
, devminor
);
921 if (hd
->typeflag
== SYMTYPE
) {
922 arcn
->type
= PAX_SLK
;
923 arcn
->sb
.st_mode
|= S_IFLNK
;
925 arcn
->type
= PAX_HLK
;
927 * so printing looks better
929 arcn
->sb
.st_mode
|= S_IFREG
;
930 arcn
->sb
.st_nlink
= 2;
937 * GNU long link/file; we tag these here and let the
938 * pax internals deal with it -- too ugly otherwise.
940 if (hd
->typeflag
!= LONGLINKTYPE
)
941 arcn
->type
= PAX_GLF
;
943 arcn
->type
= PAX_GLL
;
944 arcn
->pad
= TAR_PAD(arcn
->sb
.st_size
);
945 arcn
->skip
= arcn
->sb
.st_size
;
947 tty_warn(1, "GNU Long %s found in posix ustar archive.",
948 hd
->typeflag
== LONGLINKTYPE
? "Link" : "File");
956 * these types have file data that follows. Set the skip and
959 arcn
->type
= PAX_REG
;
960 arcn
->pad
= TAR_PAD(arcn
->sb
.st_size
);
961 arcn
->skip
= arcn
->sb
.st_size
;
962 arcn
->sb
.st_mode
|= S_IFREG
;
969 expandname(char *buf
, size_t len
, char **gnu_name
, size_t *gnu_length
,
970 const char *name
, size_t nlen
)
973 len
= strlcpy(buf
, *gnu_name
, len
);
980 len
= strlcpy(buf
, name
, len
);
986 longlink(ARCHD
*arcn
, int type
)
990 (void)memset(&larc
, 0, sizeof(larc
));
993 larc
.nlen
= strlcpy(larc
.name
, LONG_LINK
, sizeof(larc
.name
));
997 gnu_hack_string
= arcn
->ln_name
;
998 gnu_hack_len
= arcn
->ln_nlen
+ 1;
1001 gnu_hack_string
= arcn
->name
;
1002 gnu_hack_len
= arcn
->nlen
+ 1;
1005 errx(1, "Invalid type in GNU longlink %d\n", type
);
1009 * We need a longlink now.
1016 * write a ustar header for the file specified in the ARCHD to the archive
1017 * Have to check for file types that cannot be stored and file names that
1018 * are too long. Be careful of the term (last arg) to u32_oct, we only use
1019 * '\0' for the termination character (this is different than picky tar)
1020 * ASSUMED: space after header in header block is zero filled
1022 * 0 if file has data to be written after the header, 1 if file has NO
1023 * data to write after the header, -1 if archive write failed
1027 size_err(const char *what
, ARCHD
*arcn
)
1030 * header field is out of range
1032 tty_warn(1, "Ustar %s header field is too small for %s",
1033 what
, arcn
->org_name
);
1038 ustar_wr(ARCHD
*arcn
)
1042 char hdblk
[sizeof(HD_USTAR
)];
1043 const char *user
, *group
;
1045 switch (arcn
->type
) {
1048 * check for those file system types ustar cannot store
1051 tty_warn(1, "Ustar cannot archive a socket %s",
1059 * check the length of the linkname
1061 if (arcn
->ln_nlen
>= (int)sizeof(hd
->linkname
)) {
1063 longlink(arcn
, PAX_GLL
);
1065 tty_warn(1, "Link name too long for ustar %s",
1076 * split the path name into prefix and name fields (if needed). if
1077 * pt != arcn->name, the name has to be split
1079 if ((pt
= name_split(arcn
->name
, arcn
->nlen
)) == NULL
) {
1081 longlink(arcn
, PAX_GLF
);
1084 tty_warn(1, "File name too long for ustar %s",
1091 * zero out the header so we don't have to worry about zero fill below
1093 memset(hdblk
, 0, sizeof(hdblk
));
1094 hd
= (HD_USTAR
*)hdblk
;
1098 * split the name, or zero out the prefix
1100 if (pt
!= arcn
->name
) {
1102 * name was split, pt points at the / where the split is to
1103 * occur, we remove the / and copy the first part to the prefix
1106 strlcpy(hd
->prefix
, arcn
->name
, sizeof(hd
->prefix
));
1111 * copy the name part. this may be the whole path or the part after
1114 strlcpy(hd
->name
, pt
, sizeof(hd
->name
));
1117 * set the fields in the header that are type dependent
1119 switch(arcn
->type
) {
1121 hd
->typeflag
= DIRTYPE
;
1122 if (u32_oct((uintmax_t)0L, hd
->size
, sizeof(hd
->size
), 3))
1123 return size_err("DIRTYPE", arcn
);
1127 if (arcn
->type
== PAX_CHR
)
1128 hd
->typeflag
= CHRTYPE
;
1130 hd
->typeflag
= BLKTYPE
;
1131 if (u32_oct((uintmax_t)MAJOR(arcn
->sb
.st_rdev
), hd
->devmajor
,
1132 sizeof(hd
->devmajor
), 3) ||
1133 u32_oct((uintmax_t)MINOR(arcn
->sb
.st_rdev
), hd
->devminor
,
1134 sizeof(hd
->devminor
), 3) ||
1135 u32_oct((uintmax_t)0L, hd
->size
, sizeof(hd
->size
), 3))
1136 return size_err("DEVTYPE", arcn
);
1139 hd
->typeflag
= FIFOTYPE
;
1140 if (u32_oct((uintmax_t)0L, hd
->size
, sizeof(hd
->size
), 3))
1141 return size_err("FIFOTYPE", arcn
);
1147 if (arcn
->type
== PAX_SLK
)
1148 hd
->typeflag
= SYMTYPE
;
1149 else if (arcn
->type
== PAX_GLL
)
1150 hd
->typeflag
= LONGLINKTYPE
;
1152 hd
->typeflag
= LNKTYPE
;
1153 strlcpy(hd
->linkname
, arcn
->ln_name
, sizeof(hd
->linkname
));
1154 if (u32_oct((uintmax_t)gnu_hack_len
, hd
->size
,
1155 sizeof(hd
->size
), 3))
1156 return size_err("LINKTYPE", arcn
);
1163 * file data with this type, set the padding
1165 if (arcn
->type
== PAX_GLF
) {
1166 hd
->typeflag
= LONGNAMETYPE
;
1167 arcn
->pad
= TAR_PAD(gnu_hack_len
);
1168 if (OFFT_OCT((uint32_t)gnu_hack_len
, hd
->size
,
1169 sizeof(hd
->size
), 3)) {
1170 tty_warn(1,"File is too long for ustar %s",
1175 if (arcn
->type
== PAX_CTG
)
1176 hd
->typeflag
= CONTTYPE
;
1178 hd
->typeflag
= REGTYPE
;
1179 arcn
->pad
= TAR_PAD(arcn
->sb
.st_size
);
1180 if (OFFT_OCT(arcn
->sb
.st_size
, hd
->size
,
1181 sizeof(hd
->size
), 3)) {
1182 tty_warn(1,"File is too long for ustar %s",
1190 strncpy(hd
->magic
, TMAGIC
, TMAGLEN
);
1192 hd
->magic
[TMAGLEN
- 1] = hd
->version
[0] = ' ';
1194 strncpy(hd
->version
, TVERSION
, TVERSLEN
);
1197 * set the remaining fields. Some versions want all 16 bits of mode
1198 * we better humor them (they really do not meet spec though)....
1200 if (u32_oct((uintmax_t)arcn
->sb
.st_mode
, hd
->mode
, sizeof(hd
->mode
), 3))
1201 return size_err("MODE", arcn
);
1202 if (u32_oct((uintmax_t)arcn
->sb
.st_uid
, hd
->uid
, sizeof(hd
->uid
), 3))
1203 return size_err("UID", arcn
);
1204 if (u32_oct((uintmax_t)arcn
->sb
.st_gid
, hd
->gid
, sizeof(hd
->gid
), 3))
1205 return size_err("GID", arcn
);
1206 if (u32_oct((uintmax_t)arcn
->sb
.st_mtime
,hd
->mtime
,sizeof(hd
->mtime
),3))
1207 return size_err("MTIME", arcn
);
1208 user
= user_from_uid(arcn
->sb
.st_uid
, 1);
1209 group
= group_from_gid(arcn
->sb
.st_gid
, 1);
1210 strncpy(hd
->uname
, user
? user
: "", sizeof(hd
->uname
));
1211 strncpy(hd
->gname
, group
? group
: "", sizeof(hd
->gname
));
1214 * calculate and store the checksum write the header to the archive
1215 * return 0 tells the caller to now write the file data, 1 says no data
1216 * needs to be written
1218 if (u32_oct(tar_chksm(hdblk
, sizeof(HD_USTAR
)), hd
->chksum
,
1219 sizeof(hd
->chksum
), 3))
1220 return size_err("CHKSUM", arcn
);
1221 if (wr_rdbuf(hdblk
, sizeof(HD_USTAR
)) < 0)
1223 if (wr_skip((off_t
)(BLKMULT
- sizeof(HD_USTAR
))) < 0)
1225 if (gnu_hack_string
) {
1226 int res
= wr_rdbuf(gnu_hack_string
, gnu_hack_len
);
1227 int pad
= gnu_hack_len
;
1228 gnu_hack_string
= NULL
;
1232 if (wr_skip((off_t
)(BLKMULT
- (pad
% BLKMULT
))) < 0)
1235 if ((arcn
->type
== PAX_CTG
) || (arcn
->type
== PAX_REG
))
1242 * see if the name has to be split for storage in a ustar header. We try
1243 * to fit the entire name in the name field without splitting if we can.
1244 * The split point is always at a /
1246 * character pointer to split point (always the / that is to be removed
1247 * if the split is not needed, the points is set to the start of the file
1248 * name (it would violate the spec to split there). A NULL is returned if
1249 * the file name is too long
1253 name_split(char *name
, int len
)
1258 * check to see if the file name is small enough to fit in the name
1259 * field. if so just return a pointer to the name.
1264 * GNU tar does not honor the prefix+name mode if the magic
1265 * is not "ustar\0". So in GNU tar compatibility mode, we don't
1266 * split the filename into prefix+name because we are setting
1267 * the magic to "ustar " as GNU tar does. This of course will
1268 * end up creating a LongLink record in cases where it does not
1269 * really need do, but we are behaving like GNU tar after all.
1271 if (is_gnutar
|| len
> (TPFSZ
+ TNMSZ
))
1275 * we start looking at the biggest sized piece that fits in the name
1276 * field. We walk forward looking for a slash to split at. The idea is
1277 * to find the biggest piece to fit in the name field (or the smallest
1278 * prefix we can find) (the -1 is correct the biggest piece would
1279 * include the slash between the two parts that gets thrown away)
1281 start
= name
+ len
- TNMSZ
;
1282 while ((*start
!= '\0') && (*start
!= '/'))
1286 * if we hit the end of the string, this name cannot be split, so we
1287 * cannot store this file.
1294 * NOTE: /str where the length of str == TNMSZ cannot be stored under
1295 * the p1003.1-1990 spec for ustar. We could force a prefix of / and
1296 * the file would then expand on extract to //str. The len == 0 below
1297 * makes this special case follow the spec to the letter.
1299 if ((len
>= TPFSZ
) || (len
== 0))
1303 * ok have a split point, return it to the caller
1309 * convert a glob into a RE, and add it to the list. we convert to
1310 * four different RE's (because we're using BRE's and can't use |
1311 * alternation :-() with this padding:
1318 tar_gnutar_exclude_one(const char *line
, size_t len
)
1320 /* 2 * buffer len + nul */
1321 char sbuf
[MAXPATHLEN
* 2 + 1];
1322 /* + / + // + .*""/\/ + \/.* */
1323 char rabuf
[MAXPATHLEN
* 2 + 1 + 1 + 2 + 4 + 4];
1327 if (line
[len
- 1] == '\n')
1329 for (i
= 0; i
< len
; i
++) {
1331 * convert glob to regexp, escaping everything
1335 else if (line
[i
] == '?') {
1338 } else if (!isalnum((unsigned char)line
[i
]) &&
1339 !isblank((unsigned char)line
[i
]))
1341 sbuf
[j
++] = line
[i
];
1344 /* don't need the .*\/ ones if we start with /, i guess */
1345 if (line
[0] != '/') {
1346 (void)snprintf(rabuf
, sizeof rabuf
, "/.*\\/%s$//", sbuf
);
1347 if (rep_add(rabuf
) < 0)
1349 (void)snprintf(rabuf
, sizeof rabuf
, "/.*\\/%s\\/.*//", sbuf
);
1350 if (rep_add(rabuf
) < 0)
1354 (void)snprintf(rabuf
, sizeof rabuf
, "/^%s$//", sbuf
);
1355 if (rep_add(rabuf
) < 0)
1357 (void)snprintf(rabuf
, sizeof rabuf
, "/^%s\\/.*//", sbuf
);
1358 if (rep_add(rabuf
) < 0)
1365 * deal with GNU tar -X/--exclude-from & --exclude switchs. basically,
1366 * we go through each line of the file, building a string from the "glob"
1367 * lines in the file into RE lines, of the form `/^RE$//', which we pass
1368 * to rep_add(), which will add a empty replacement (exclusion), for the
1372 tar_gnutar_minus_minus_exclude(const char *path
)
1374 size_t len
= strlen(path
);
1376 if (len
> MAXPATHLEN
)
1377 tty_warn(0, "pathname too long: %s", path
);
1379 return (tar_gnutar_exclude_one(path
, len
));
1383 tar_gnutar_X_compat(const char *path
)
1390 if (path
[0] == '-' && path
[1] == '\0')
1393 fp
= fopen(path
, "r");
1395 tty_warn(1, "cannot open %s: %s", path
,
1401 while ((line
= fgetln(fp
, &len
))) {
1403 if (len
> MAXPATHLEN
) {
1404 tty_warn(0, "pathname too long, line %d of %s",
1407 if (tar_gnutar_exclude_one(line
, len
))