2 * SPDX-License-Identifier: BSD-3-Clause
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
38 static char sccsid[] = "@(#)tar.c 8.2 (Berkeley) 4/18/94";
41 #include <sys/cdefs.h>
42 #include <sys/types.h>
52 * Routines for reading, writing and header identify of various versions of tar
55 static u_long tar_chksm(char *, int);
56 static char *name_split(char *, int);
57 static int ul_oct(u_long, char *, int, int);
58 static int uqd_oct(u_quad_t, char *, int, int);
61 * Routines common to all versions of tar
64 static int tar_nodir; /* do not write dirs under old tar */
68 * add the tar trailer of two null blocks
70 * 0 if ok, -1 otherwise (what wr_skip returns)
76 return(wr_skip((off_t)(NULLCNT*BLKMULT)));
81 * no cleanup needed here, just return size of trailer (for append)
83 * size of trailer (2 * BLKMULT)
89 return((off_t)(NULLCNT*BLKMULT));
94 * Called to determine if a header block is a valid trailer. We are passed
95 * the block, the in_sync flag (which tells us we are in resync mode;
96 * looking for a valid header), and cnt (which starts at zero) which is
97 * used to count the number of empty blocks we have seen so far.
99 * 0 if a valid trailer, -1 if not a valid trailer, or 1 if the block
100 * could never contain a header.
104 tar_trail(char *buf, int in_resync, int *cnt)
109 * look for all zero, trailer is two consecutive blocks of zero
111 for (i = 0; i < BLKMULT; ++i) {
117 * if not all zero it is not a trailer, but MIGHT be a header.
123 * When given a zero block, we must be careful!
124 * If we are not in resync mode, check for the trailer. Have to watch
125 * out that we do not mis-identify file data as the trailer, so we do
126 * NOT try to id a trailer during resync mode. During resync mode we
127 * might as well throw this block out since a valid header can NEVER be
128 * a block of all 0 (we must have a valid file name).
130 if (!in_resync && (++*cnt >= NULLCNT))
137 * convert an unsigned long to an octal string. many oddball field
138 * termination characters are used by the various versions of tar in the
139 * different fields. term selects which kind to use. str is '0' padded
140 * at the front to len. we are unable to use only one format as many old
141 * tar readers are very cranky about this.
143 * 0 if the number fit into the string, -1 otherwise
147 ul_oct(u_long val, char *str, int len, int term)
152 * term selects the appropriate character(s) for the end of the string
174 * convert and blank pad if there is space
177 *pt-- = '0' + (char)(val & 0x7);
178 if ((val = val >> 3) == (u_long)0)
184 if (val != (u_long)0)
191 * convert an u_quad_t to an octal string. one of many oddball field
192 * termination characters are used by the various versions of tar in the
193 * different fields. term selects which kind to use. str is '0' padded
194 * at the front to len. we are unable to use only one format as many old
195 * tar readers are very cranky about this.
197 * 0 if the number fit into the string, -1 otherwise
201 uqd_oct(u_quad_t val, char *str, int len, int term)
206 * term selects the appropriate character(s) for the end of the string
228 * convert and blank pad if there is space
231 *pt-- = '0' + (char)(val & 0x7);
232 if ((val = val >> 3) == 0)
238 if (val != (u_quad_t)0)
245 * calculate the checksum for a tar block counting the checksum field as
246 * all blanks (BLNKSUM is that value pre-calculated, the sum of 8 blanks).
247 * NOTE: we use len to short circuit summing 0's on write since we ALWAYS
248 * pad headers with 0.
250 * unsigned long checksum
254 tar_chksm(char *blk, int len)
258 u_long chksm = BLNKSUM; /* initial value is checksum field sum */
261 * add the part of the block before the checksum field
264 stop = blk + CHK_OFFSET;
266 chksm += (u_long)(*pt++ & 0xff);
268 * move past the checksum field and keep going, spec counts the
269 * checksum field as the sum of 8 blanks (which is pre-computed as
271 * ASSUMED: len is greater than CHK_OFFSET. (len is where our 0 padding
272 * starts, no point in summing zero's)
277 chksm += (u_long)(*pt++ & 0xff);
282 * Routines for old BSD style tar (also made portable to sysV tar)
287 * determine if a block given to us is a valid tar header (and not a USTAR
288 * header). We have to be on the lookout for those pesky blocks of all
291 * 0 if a tar header, -1 otherwise
295 tar_id(char *blk, int size)
303 uhd = (HD_USTAR *)blk;
306 * check for block of zero's first, a simple and fast test, then make
307 * sure this is not a ustar header by looking for the ustar magic
308 * cookie. We should use TMAGLEN, but some USTAR archive programs are
309 * wrong and create archives missing the \0. Last we check the
310 * checksum. If this is ok we have to assume it is a valid header.
312 if (hd->name[0] == '\0')
314 if (strncmp(uhd->magic, TMAGIC, TMAGLEN - 1) == 0)
316 if (asc_ul(hd->chksum,sizeof(hd->chksum),OCT) != tar_chksm(blk,BLKMULT))
323 * handle tar format specific -o options
325 * 0 if ok -1 otherwise
333 while ((opt = opt_next()) != NULL) {
334 if (strcmp(opt->name, TAR_OPTION) ||
335 strcmp(opt->value, TAR_NODIR)) {
336 paxwarn(1, "Unknown tar format -o option/value pair %s=%s",
337 opt->name, opt->value);
338 paxwarn(1,"%s=%s is the only supported tar format option",
339 TAR_OPTION, TAR_NODIR);
344 * we only support one option, and only when writing
346 if ((act != APPND) && (act != ARCHIVE)) {
347 paxwarn(1, "%s=%s is only supported when writing.",
348 opt->name, opt->value);
359 * extract the values out of block already determined to be a tar header.
360 * store the values in the ARCHD parameter.
366 tar_rd(ARCHD *arcn, char *buf)
372 * we only get proper sized buffers passed to us
374 if (tar_id(buf, BLKMULT) < 0)
376 arcn->org_name = arcn->name;
377 arcn->sb.st_nlink = 1;
381 * copy out the name and values in the stat buffer
385 * old tar format specifies the name always be null-terminated,
386 * but let's be robust to broken archives.
387 * the same applies to handling links below.
389 arcn->nlen = l_strncpy(arcn->name, hd->name,
390 MIN(sizeof(hd->name), sizeof(arcn->name)) - 1);
391 arcn->name[arcn->nlen] = '\0';
392 arcn->sb.st_mode = (mode_t)(asc_ul(hd->mode,sizeof(hd->mode),OCT) &
394 arcn->sb.st_uid = (uid_t)asc_ul(hd->uid, sizeof(hd->uid), OCT);
395 arcn->sb.st_gid = (gid_t)asc_ul(hd->gid, sizeof(hd->gid), OCT);
396 arcn->sb.st_size = (off_t)asc_uqd(hd->size, sizeof(hd->size), OCT);
397 arcn->sb.st_mtime = (time_t)asc_uqd(hd->mtime, sizeof(hd->mtime), OCT);
398 arcn->sb.st_ctime = arcn->sb.st_atime = arcn->sb.st_mtime;
401 * have to look at the last character, it may be a '/' and that is used
402 * to encode this as a directory
404 pt = &(arcn->name[arcn->nlen - 1]);
407 switch(hd->linkflag) {
410 * symbolic link, need to get the link name and set the type in
411 * the st_mode so -v printing will look correct.
413 arcn->type = PAX_SLK;
414 arcn->ln_nlen = l_strncpy(arcn->ln_name, hd->linkname,
415 MIN(sizeof(hd->linkname), sizeof(arcn->ln_name)) - 1);
416 arcn->ln_name[arcn->ln_nlen] = '\0';
417 arcn->sb.st_mode |= S_IFLNK;
421 * hard link, need to get the link name, set the type in the
422 * st_mode and st_nlink so -v printing will look better.
424 arcn->type = PAX_HLK;
425 arcn->sb.st_nlink = 2;
426 arcn->ln_nlen = l_strncpy(arcn->ln_name, hd->linkname,
427 MIN(sizeof(hd->linkname), sizeof(arcn->ln_name)) - 1);
428 arcn->ln_name[arcn->ln_nlen] = '\0';
431 * no idea of what type this thing really points at, but
432 * we set something for printing only.
434 arcn->sb.st_mode |= S_IFREG;
438 * It is a directory, set the mode for -v printing
440 arcn->type = PAX_DIR;
441 arcn->sb.st_mode |= S_IFDIR;
442 arcn->sb.st_nlink = 2;
443 arcn->ln_name[0] = '\0';
450 * If we have a trailing / this is a directory and NOT a file.
452 arcn->ln_name[0] = '\0';
456 * it is a directory, set the mode for -v printing
458 arcn->type = PAX_DIR;
459 arcn->sb.st_mode |= S_IFDIR;
460 arcn->sb.st_nlink = 2;
463 * have a file that will be followed by data. Set the
464 * skip value to the size field and calculate the size
467 arcn->type = PAX_REG;
468 arcn->sb.st_mode |= S_IFREG;
469 arcn->pad = TAR_PAD(arcn->sb.st_size);
470 arcn->skip = arcn->sb.st_size;
476 * strip off any trailing slash.
487 * write a tar header for the file specified in the ARCHD to the archive.
488 * Have to check for file types that cannot be stored and file names that
489 * are too long. Be careful of the term (last arg) to ul_oct, each field
490 * of tar has it own spec for the termination character(s).
491 * ASSUMED: space after header in header block is zero filled
493 * 0 if file has data to be written after the header, 1 if file has NO
494 * data to write after the header, -1 if archive write failed
505 * check for those file system types which tar cannot store
510 * user asked that dirs not be written to the archive
516 paxwarn(1, "Tar cannot archive a character device %s",
520 paxwarn(1, "Tar cannot archive a block device %s", arcn->org_name);
523 paxwarn(1, "Tar cannot archive a socket %s", arcn->org_name);
526 paxwarn(1, "Tar cannot archive a fifo %s", arcn->org_name);
531 if (arcn->ln_nlen >= (int)sizeof(hd->linkname)) {
532 paxwarn(1,"Link name too long for tar %s", arcn->ln_name);
543 * check file name len, remember extra char for dirs (the / at the end)
546 if (arcn->type == PAX_DIR)
548 if (len >= (int)sizeof(hd->name)) {
549 paxwarn(1, "File name too long for tar %s", arcn->name);
554 * Copy the data out of the ARCHD into the tar header based on the type
555 * of the file. Remember, many tar readers want all fields to be
556 * padded with zero so we zero the header first. We then set the
557 * linkflag field (type), the linkname, the size, and set the padding
558 * (if any) to be added after the file data (0 for all other types,
559 * as they only have a header).
562 l_strncpy(hd->name, arcn->name, sizeof(hd->name) - 1);
563 hd->name[sizeof(hd->name) - 1] = '\0';
566 if (arcn->type == PAX_DIR) {
568 * directories are the same as files, except have a filename
569 * that ends with a /, we add the slash here. No data follows,
572 hd->linkflag = AREGTYPE;
573 memset(hd->linkname, 0, sizeof(hd->linkname));
574 hd->name[len-1] = '/';
575 if (ul_oct((u_long)0L, hd->size, sizeof(hd->size), 1))
577 } else if (arcn->type == PAX_SLK) {
579 * no data follows this file, so no pad
581 hd->linkflag = SYMTYPE;
582 l_strncpy(hd->linkname,arcn->ln_name, sizeof(hd->linkname) - 1);
583 hd->linkname[sizeof(hd->linkname) - 1] = '\0';
584 if (ul_oct((u_long)0L, hd->size, sizeof(hd->size), 1))
586 } else if ((arcn->type == PAX_HLK) || (arcn->type == PAX_HRG)) {
588 * no data follows this file, so no pad
590 hd->linkflag = LNKTYPE;
591 l_strncpy(hd->linkname,arcn->ln_name, sizeof(hd->linkname) - 1);
592 hd->linkname[sizeof(hd->linkname) - 1] = '\0';
593 if (ul_oct((u_long)0L, hd->size, sizeof(hd->size), 1))
597 * data follows this file, so set the pad
599 hd->linkflag = AREGTYPE;
600 memset(hd->linkname, 0, sizeof(hd->linkname));
601 if (uqd_oct((u_quad_t)arcn->sb.st_size, hd->size,
602 sizeof(hd->size), 1)) {
603 paxwarn(1,"File is too large for tar %s", arcn->org_name);
606 arcn->pad = TAR_PAD(arcn->sb.st_size);
610 * copy those fields that are independent of the type
612 if (ul_oct((u_long)arcn->sb.st_mode, hd->mode, sizeof(hd->mode), 0) ||
613 ul_oct((u_long)arcn->sb.st_uid, hd->uid, sizeof(hd->uid), 0) ||
614 ul_oct((u_long)arcn->sb.st_gid, hd->gid, sizeof(hd->gid), 0) ||
615 ul_oct((u_long)arcn->sb.st_mtime, hd->mtime, sizeof(hd->mtime), 1))
619 * calculate and add the checksum, then write the header. A return of
620 * 0 tells the caller to now write the file data, 1 says no data needs
623 if (ul_oct(tar_chksm((char *)&hdblk, sizeof(HD_TAR)), hd->chksum,
624 sizeof(hd->chksum), 3))
626 if (wr_rdbuf((char *)&hdblk, sizeof(HD_TAR)) < 0)
628 if (wr_skip((off_t)(BLKMULT - sizeof(HD_TAR))) < 0)
630 if ((arcn->type == PAX_CTG) || (arcn->type == PAX_REG))
636 * header field is out of range
638 paxwarn(1, "Tar header field is too small for %s", arcn->org_name);
643 * Routines for POSIX ustar
648 * initialization for ustar read
650 * 0 if ok, -1 otherwise
656 if ((usrtb_start() < 0) || (grptb_start() < 0))
663 * initialization for ustar write
665 * 0 if ok, -1 otherwise
671 if ((uidtb_start() < 0) || (gidtb_start() < 0))
678 * determine if a block given to us is a valid ustar header. We have to
679 * be on the lookout for those pesky blocks of all zero's
681 * 0 if a ustar header, -1 otherwise
685 ustar_id(char *blk, int size)
691 hd = (HD_USTAR *)blk;
694 * check for block of zero's first, a simple and fast test then check
695 * ustar magic cookie. We should use TMAGLEN, but some USTAR archive
696 * programs are fouled up and create archives missing the \0. Last we
697 * check the checksum. If ok we have to assume it is a valid header.
699 if (hd->name[0] == '\0')
701 if (strncmp(hd->magic, TMAGIC, TMAGLEN - 1) != 0)
703 if (asc_ul(hd->chksum,sizeof(hd->chksum),OCT) != tar_chksm(blk,BLKMULT))
710 * extract the values out of block already determined to be a ustar header.
711 * store the values in the ARCHD parameter.
717 ustar_rd(ARCHD *arcn, char *buf)
726 * we only get proper sized buffers
728 if (ustar_id(buf, BLKMULT) < 0)
730 arcn->org_name = arcn->name;
731 arcn->sb.st_nlink = 1;
734 hd = (HD_USTAR *)buf;
737 * see if the filename is split into two parts. if, so joint the parts.
738 * we copy the prefix first and add a / between the prefix and name.
741 if (*(hd->prefix) != '\0') {
742 cnt = l_strncpy(dest, hd->prefix,
743 MIN(sizeof(hd->prefix), sizeof(arcn->name) - 2));
749 * ustar format specifies the name may be unterminated
750 * if it fills the entire field. this also applies to
751 * the prefix and the linkname.
753 arcn->nlen = cnt + l_strncpy(dest, hd->name,
754 MIN(sizeof(hd->name), sizeof(arcn->name) - cnt - 1));
755 arcn->name[arcn->nlen] = '\0';
758 * follow the spec to the letter. we should only have mode bits, strip
759 * off all other crud we may be passed.
761 arcn->sb.st_mode = (mode_t)(asc_ul(hd->mode, sizeof(hd->mode), OCT) &
763 arcn->sb.st_size = (off_t)asc_uqd(hd->size, sizeof(hd->size), OCT);
764 arcn->sb.st_mtime = (time_t)asc_uqd(hd->mtime, sizeof(hd->mtime), OCT);
765 arcn->sb.st_ctime = arcn->sb.st_atime = arcn->sb.st_mtime;
768 * If we can find the ascii names for gname and uname in the password
769 * and group files we will use the uid's and gid they bind. Otherwise
770 * we use the uid and gid values stored in the header. (This is what
771 * the POSIX spec wants).
773 hd->gname[sizeof(hd->gname) - 1] = '\0';
774 if (gid_name(hd->gname, &(arcn->sb.st_gid)) < 0)
775 arcn->sb.st_gid = (gid_t)asc_ul(hd->gid, sizeof(hd->gid), OCT);
776 hd->uname[sizeof(hd->uname) - 1] = '\0';
777 if (uid_name(hd->uname, &(arcn->sb.st_uid)) < 0)
778 arcn->sb.st_uid = (uid_t)asc_ul(hd->uid, sizeof(hd->uid), OCT);
781 * set the defaults, these may be changed depending on the file type
783 arcn->ln_name[0] = '\0';
787 arcn->sb.st_rdev = (dev_t)0;
790 * set the mode and PAX type according to the typeflag in the header
792 switch(hd->typeflag) {
794 arcn->type = PAX_FIF;
795 arcn->sb.st_mode |= S_IFIFO;
798 arcn->type = PAX_DIR;
799 arcn->sb.st_mode |= S_IFDIR;
800 arcn->sb.st_nlink = 2;
803 * Some programs that create ustar archives append a '/'
804 * to the pathname for directories. This clearly violates
805 * ustar specs, but we will silently strip it off anyway.
807 if (arcn->name[arcn->nlen - 1] == '/')
808 arcn->name[--arcn->nlen] = '\0';
813 * this type requires the rdev field to be set.
815 if (hd->typeflag == BLKTYPE) {
816 arcn->type = PAX_BLK;
817 arcn->sb.st_mode |= S_IFBLK;
819 arcn->type = PAX_CHR;
820 arcn->sb.st_mode |= S_IFCHR;
822 devmajor = (dev_t)asc_ul(hd->devmajor,sizeof(hd->devmajor),OCT);
823 devminor = (dev_t)asc_ul(hd->devminor,sizeof(hd->devminor),OCT);
824 arcn->sb.st_rdev = TODEV(devmajor, devminor);
828 if (hd->typeflag == SYMTYPE) {
829 arcn->type = PAX_SLK;
830 arcn->sb.st_mode |= S_IFLNK;
832 arcn->type = PAX_HLK;
834 * so printing looks better
836 arcn->sb.st_mode |= S_IFREG;
837 arcn->sb.st_nlink = 2;
842 arcn->ln_nlen = l_strncpy(arcn->ln_name, hd->linkname,
843 MIN(sizeof(hd->linkname), sizeof(arcn->ln_name) - 1));
844 arcn->ln_name[arcn->ln_nlen] = '\0';
851 * these types have file data that follows. Set the skip and
854 arcn->type = PAX_REG;
855 arcn->pad = TAR_PAD(arcn->sb.st_size);
856 arcn->skip = arcn->sb.st_size;
857 arcn->sb.st_mode |= S_IFREG;
865 * write a ustar header for the file specified in the ARCHD to the archive
866 * Have to check for file types that cannot be stored and file names that
867 * are too long. Be careful of the term (last arg) to ul_oct, we only use
868 * '\0' for the termination character (this is different than picky tar)
869 * ASSUMED: space after header in header block is zero filled
871 * 0 if file has data to be written after the header, 1 if file has NO
872 * data to write after the header, -1 if archive write failed
876 ustar_wr(ARCHD *arcn)
883 * check for those file system types ustar cannot store
885 if (arcn->type == PAX_SCK) {
886 paxwarn(1, "Ustar cannot archive a socket %s", arcn->org_name);
891 * check the length of the linkname
893 if (((arcn->type == PAX_SLK) || (arcn->type == PAX_HLK) ||
894 (arcn->type == PAX_HRG)) &&
895 (arcn->ln_nlen > (int)sizeof(hd->linkname))) {
896 paxwarn(1, "Link name too long for ustar %s", arcn->ln_name);
901 * split the path name into prefix and name fields (if needed). if
902 * pt != arcn->name, the name has to be split
904 if ((pt = name_split(arcn->name, arcn->nlen)) == NULL) {
905 paxwarn(1, "File name too long for ustar %s", arcn->name);
912 * split the name, or zero out the prefix
914 if (pt != arcn->name) {
916 * name was split, pt points at the / where the split is to
917 * occur, we remove the / and copy the first part to the prefix
920 l_strncpy(hd->prefix, arcn->name, sizeof(hd->prefix));
923 memset(hd->prefix, 0, sizeof(hd->prefix));
926 * copy the name part. this may be the whole path or the part after
927 * the prefix. both the name and prefix may fill the entire field.
929 l_strncpy(hd->name, pt, sizeof(hd->name));
932 * set the fields in the header that are type dependent
936 hd->typeflag = DIRTYPE;
937 memset(hd->linkname, 0, sizeof(hd->linkname));
938 memset(hd->devmajor, 0, sizeof(hd->devmajor));
939 memset(hd->devminor, 0, sizeof(hd->devminor));
940 if (ul_oct((u_long)0L, hd->size, sizeof(hd->size), 3))
945 if (arcn->type == PAX_CHR)
946 hd->typeflag = CHRTYPE;
948 hd->typeflag = BLKTYPE;
949 memset(hd->linkname, 0, sizeof(hd->linkname));
950 if (ul_oct((u_long)MAJOR(arcn->sb.st_rdev), hd->devmajor,
951 sizeof(hd->devmajor), 3) ||
952 ul_oct((u_long)MINOR(arcn->sb.st_rdev), hd->devminor,
953 sizeof(hd->devminor), 3) ||
954 ul_oct((u_long)0L, hd->size, sizeof(hd->size), 3))
958 hd->typeflag = FIFOTYPE;
959 memset(hd->linkname, 0, sizeof(hd->linkname));
960 memset(hd->devmajor, 0, sizeof(hd->devmajor));
961 memset(hd->devminor, 0, sizeof(hd->devminor));
962 if (ul_oct((u_long)0L, hd->size, sizeof(hd->size), 3))
968 if (arcn->type == PAX_SLK)
969 hd->typeflag = SYMTYPE;
971 hd->typeflag = LNKTYPE;
972 /* the link name may occupy the entire field in ustar */
973 l_strncpy(hd->linkname,arcn->ln_name, sizeof(hd->linkname));
974 memset(hd->devmajor, 0, sizeof(hd->devmajor));
975 memset(hd->devminor, 0, sizeof(hd->devminor));
976 if (ul_oct((u_long)0L, hd->size, sizeof(hd->size), 3))
983 * file data with this type, set the padding
985 if (arcn->type == PAX_CTG)
986 hd->typeflag = CONTTYPE;
988 hd->typeflag = REGTYPE;
989 memset(hd->linkname, 0, sizeof(hd->linkname));
990 memset(hd->devmajor, 0, sizeof(hd->devmajor));
991 memset(hd->devminor, 0, sizeof(hd->devminor));
992 arcn->pad = TAR_PAD(arcn->sb.st_size);
993 if (uqd_oct((u_quad_t)arcn->sb.st_size, hd->size,
994 sizeof(hd->size), 3)) {
995 paxwarn(1,"File is too long for ustar %s",arcn->org_name);
1001 l_strncpy(hd->magic, TMAGIC, TMAGLEN);
1002 l_strncpy(hd->version, TVERSION, TVERSLEN);
1005 * set the remaining fields. Some versions want all 16 bits of mode
1006 * we better humor them (they really do not meet spec though)....
1008 if (ul_oct((u_long)arcn->sb.st_mode, hd->mode, sizeof(hd->mode), 3) ||
1009 ul_oct((u_long)arcn->sb.st_uid, hd->uid, sizeof(hd->uid), 3) ||
1010 ul_oct((u_long)arcn->sb.st_gid, hd->gid, sizeof(hd->gid), 3) ||
1011 ul_oct((u_long)arcn->sb.st_mtime,hd->mtime,sizeof(hd->mtime),3))
1013 l_strncpy(hd->uname,name_uid(arcn->sb.st_uid, 0),sizeof(hd->uname));
1014 l_strncpy(hd->gname,name_gid(arcn->sb.st_gid, 0),sizeof(hd->gname));
1017 * calculate and store the checksum write the header to the archive
1018 * return 0 tells the caller to now write the file data, 1 says no data
1019 * needs to be written
1021 if (ul_oct(tar_chksm((char *)&hdblk, sizeof(HD_USTAR)), hd->chksum,
1022 sizeof(hd->chksum), 3))
1024 if (wr_rdbuf((char *)&hdblk, sizeof(HD_USTAR)) < 0)
1026 if (wr_skip((off_t)(BLKMULT - sizeof(HD_USTAR))) < 0)
1028 if ((arcn->type == PAX_CTG) || (arcn->type == PAX_REG))
1034 * header field is out of range
1036 paxwarn(1, "Ustar header field is too small for %s", arcn->org_name);
1042 * see if the name has to be split for storage in a ustar header. We try
1043 * to fit the entire name in the name field without splitting if we can.
1044 * The split point is always at a /
1046 * character pointer to split point (always the / that is to be removed
1047 * if the split is not needed, the points is set to the start of the file
1048 * name (it would violate the spec to split there). A NULL is returned if
1049 * the file name is too long
1053 name_split(char *name, int len)
1058 * check to see if the file name is small enough to fit in the name
1059 * field. if so just return a pointer to the name.
1063 if (len > TPFSZ + TNMSZ)
1067 * we start looking at the biggest sized piece that fits in the name
1068 * field. We walk forward looking for a slash to split at. The idea is
1069 * to find the biggest piece to fit in the name field (or the smallest
1070 * prefix we can find)
1072 start = name + len - TNMSZ;
1073 while ((*start != '\0') && (*start != '/'))
1077 * if we hit the end of the string, this name cannot be split, so we
1078 * cannot store this file.
1085 * NOTE: /str where the length of str == TNMSZ can not be stored under
1086 * the p1003.1-1990 spec for ustar. We could force a prefix of / and
1087 * the file would then expand on extract to //str. The len == 0 below
1088 * makes this special case follow the spec to the letter.
1090 if ((len > TPFSZ) || (len == 0))
1094 * ok have a split point, return it to the caller