1 /* $OpenBSD: tar.c,v 1.13 1998/09/26 21:29:41 millert Exp $ */
2 /* $NetBSD: tar.c,v 1.5 1995/03/21 09:07:49 cgd Exp $ */
5 * Copyright (c) 1992 Keith Muller.
6 * Copyright (c) 1992, 1993
7 * The Regents of the University of California. All rights reserved.
9 * This code is derived from software contributed to Berkeley by
10 * Keith Muller of the University of California, San Diego.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the University of
23 * California, Berkeley and its contributors.
24 * 4. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
43 static char sccsid[] = "@(#)tar.c 8.2 (Berkeley) 4/18/94";
45 static char rcsid[] = "$OpenBSD: tar.c,v 1.13 1998/09/26 21:29:41 millert Exp $";
49 #include <sys/types.h>
50 #include <sys/sysmacros.h>
53 #include <sys/param.h>
63 * Routines for reading, writing and header identify of various versions of tar
66 static u_long tar_chksm __P((register char *, register int));
67 static char *name_split __P((register char *, register int));
68 static int ul_oct __P((u_long, register char *, register int, int));
70 static int uqd_oct __P((u_quad_t, register char *, register int, int));
74 * Routines common to all versions of tar
77 static int tar_nodir; /* do not write dirs under old tar */
81 * add the tar trailer of two null blocks
83 * 0 if ok, -1 otherwise (what wr_skip returns)
94 return(wr_skip((off_t)(NULLCNT*BLKMULT)));
99 * no cleanup needed here, just return size of trailer (for append)
101 * size of trailer (2 * BLKMULT)
112 return((off_t)(NULLCNT*BLKMULT));
117 * Called to determine if a header block is a valid trailer. We are passed
118 * the block, the in_sync flag (which tells us we are in resync mode;
119 * looking for a valid header), and cnt (which starts at zero) which is
120 * used to count the number of empty blocks we have seen so far.
122 * 0 if a valid trailer, -1 if not a valid trailer, or 1 if the block
123 * could never contain a header.
128 tar_trail(register char *buf, register int in_resync, register int *cnt)
131 tar_trail(buf, in_resync, cnt)
133 register int in_resync;
140 * look for all zero, trailer is two consecutive blocks of zero
142 for (i = 0; i < BLKMULT; ++i) {
148 * if not all zero it is not a trailer, but MIGHT be a header.
154 * When given a zero block, we must be careful!
155 * If we are not in resync mode, check for the trailer. Have to watch
156 * out that we do not mis-identify file data as the trailer, so we do
157 * NOT try to id a trailer during resync mode. During resync mode we
158 * might as well throw this block out since a valid header can NEVER be
159 * a block of all 0 (we must have a valid file name).
161 if (!in_resync && (++*cnt >= NULLCNT))
168 * convert an unsigned long to an octal string. many oddball field
169 * termination characters are used by the various versions of tar in the
170 * different fields. term selects which kind to use. str is '0' padded
171 * at the front to len. we are unable to use only one format as many old
172 * tar readers are very cranky about this.
174 * 0 if the number fit into the string, -1 otherwise
179 ul_oct(u_long val, register char *str, register int len, int term)
182 ul_oct(val, str, len, term)
192 * term selects the appropriate character(s) for the end of the string
214 * convert and blank pad if there is space
217 *pt-- = '0' + (char)(val & 0x7);
218 if ((val = val >> 3) == (u_long)0)
224 if (val != (u_long)0)
232 * convert an u_quad_t to an octal string. one of many oddball field
233 * termination characters are used by the various versions of tar in the
234 * different fields. term selects which kind to use. str is '0' padded
235 * at the front to len. we are unable to use only one format as many old
236 * tar readers are very cranky about this.
238 * 0 if the number fit into the string, -1 otherwise
243 uqd_oct(u_quad_t val, register char *str, register int len, int term)
246 uqd_oct(val, str, len, term)
256 * term selects the appropriate character(s) for the end of the string
278 * convert and blank pad if there is space
281 *pt-- = '0' + (char)(val & 0x7);
282 if ((val = val >> 3) == 0)
288 if (val != (u_quad_t)0)
296 * calculate the checksum for a tar block counting the checksum field as
297 * all blanks (BLNKSUM is that value pre-calculated, the sume of 8 blanks).
298 * NOTE: we use len to short circuit summing 0's on write since we ALWAYS
299 * pad headers with 0.
301 * unsigned long checksum
306 tar_chksm(register char *blk, register int len)
316 u_long chksm = BLNKSUM; /* inital value is checksum field sum */
319 * add the part of the block before the checksum field
322 stop = blk + CHK_OFFSET;
324 chksm += (u_long)(*pt++ & 0xff);
326 * move past the checksum field and keep going, spec counts the
327 * checksum field as the sum of 8 blanks (which is pre-computed as
329 * ASSUMED: len is greater than CHK_OFFSET. (len is where our 0 padding
330 * starts, no point in summing zero's)
335 chksm += (u_long)(*pt++ & 0xff);
340 * Routines for old BSD style tar (also made portable to sysV tar)
345 * determine if a block given to us is a valid tar header (and not a USTAR
346 * header). We have to be on the lookout for those pesky blocks of all
349 * 0 if a tar header, -1 otherwise
354 tar_id(register char *blk, int size)
363 register HD_USTAR *uhd;
368 uhd = (HD_USTAR *)blk;
371 * check for block of zero's first, a simple and fast test, then make
372 * sure this is not a ustar header by looking for the ustar magic
373 * cookie. We should use TMAGLEN, but some USTAR archive programs are
374 * wrong and create archives missing the \0. Last we check the
375 * checksum. If this is ok we have to assume it is a valid header.
377 if (hd->name[0] == '\0')
379 if (strncmp(uhd->magic, TMAGIC, TMAGLEN - 1) == 0)
381 if (asc_ul(hd->chksum,sizeof(hd->chksum),OCT) != tar_chksm(blk,BLKMULT))
388 * handle tar format specific -o options
390 * 0 if ok -1 otherwise
403 while ((opt = opt_next()) != NULL) {
404 if (strcmp(opt->name, TAR_OPTION) ||
405 strcmp(opt->value, TAR_NODIR)) {
406 paxwarn(1, "Unknown tar format -o option/value pair %s=%s",
407 opt->name, opt->value);
408 paxwarn(1,"%s=%s is the only supported tar format option",
409 TAR_OPTION, TAR_NODIR);
414 * we only support one option, and only when writing
416 if ((act != APPND) && (act != ARCHIVE)) {
417 paxwarn(1, "%s=%s is only supported when writing.",
418 opt->name, opt->value);
429 * extract the values out of block already determined to be a tar header.
430 * store the values in the ARCHD parameter.
437 tar_rd(register ARCHD *arcn, register char *buf)
441 register ARCHD *arcn;
449 * we only get proper sized buffers passed to us
451 if (tar_id(buf, BLKMULT) < 0)
453 arcn->org_name = arcn->name;
454 arcn->sb.st_nlink = 1;
458 * copy out the name and values in the stat buffer
461 arcn->nlen = l_strncpy(arcn->name, hd->name, sizeof(arcn->name) - 1);
462 arcn->name[arcn->nlen] = '\0';
463 arcn->sb.st_mode = (mode_t)(asc_ul(hd->mode,sizeof(hd->mode),OCT) &
465 arcn->sb.st_uid = (uid_t)asc_ul(hd->uid, sizeof(hd->uid), OCT);
466 arcn->sb.st_gid = (gid_t)asc_ul(hd->gid, sizeof(hd->gid), OCT);
467 arcn->sb.st_size = (size_t)asc_ul(hd->size, sizeof(hd->size), OCT);
468 arcn->sb.st_mtime = (time_t)asc_ul(hd->mtime, sizeof(hd->mtime), OCT);
469 arcn->sb.st_ctime = arcn->sb.st_atime = arcn->sb.st_mtime;
472 * have to look at the last character, it may be a '/' and that is used
473 * to encode this as a directory
475 pt = &(arcn->name[arcn->nlen - 1]);
478 switch(hd->linkflag) {
481 * symbolic link, need to get the link name and set the type in
482 * the st_mode so -v printing will look correct.
484 arcn->type = PAX_SLK;
485 arcn->ln_nlen = l_strncpy(arcn->ln_name, hd->linkname,
486 sizeof(arcn->ln_name) - 1);
487 arcn->ln_name[arcn->ln_nlen] = '\0';
488 arcn->sb.st_mode |= S_IFLNK;
492 * hard link, need to get the link name, set the type in the
493 * st_mode and st_nlink so -v printing will look better.
495 arcn->type = PAX_HLK;
496 arcn->sb.st_nlink = 2;
497 arcn->ln_nlen = l_strncpy(arcn->ln_name, hd->linkname,
498 sizeof(arcn->ln_name) - 1);
499 arcn->ln_name[arcn->ln_nlen] = '\0';
502 * no idea of what type this thing really points at, but
503 * we set something for printing only.
505 arcn->sb.st_mode |= S_IFREG;
509 * It is a directory, set the mode for -v printing
511 arcn->type = PAX_DIR;
512 arcn->sb.st_mode |= S_IFDIR;
513 arcn->sb.st_nlink = 2;
514 arcn->ln_name[0] = '\0';
521 * If we have a trailing / this is a directory and NOT a file.
523 arcn->ln_name[0] = '\0';
527 * it is a directory, set the mode for -v printing
529 arcn->type = PAX_DIR;
530 arcn->sb.st_mode |= S_IFDIR;
531 arcn->sb.st_nlink = 2;
534 * have a file that will be followed by data. Set the
535 * skip value to the size field and caluculate the size
538 arcn->type = PAX_REG;
539 arcn->sb.st_mode |= S_IFREG;
540 arcn->pad = TAR_PAD(arcn->sb.st_size);
541 arcn->skip = arcn->sb.st_size;
547 * strip off any trailing slash.
558 * write a tar header for the file specified in the ARCHD to the archive.
559 * Have to check for file types that cannot be stored and file names that
560 * are too long. Be careful of the term (last arg) to ul_oct, each field
561 * of tar has it own spec for the termination character(s).
562 * ASSUMED: space after header in header block is zero filled
564 * 0 if file has data to be written after the header, 1 if file has NO
565 * data to write after the header, -1 if archive write failed
570 tar_wr(register ARCHD *arcn)
574 register ARCHD *arcn;
579 char hdblk[sizeof(HD_TAR)];
582 * check for those file system types which tar cannot store
587 * user asked that dirs not be written to the archive
593 paxwarn(1, "Tar cannot archive a character device %s",
597 paxwarn(1, "Tar cannot archive a block device %s", arcn->org_name);
600 paxwarn(1, "Tar cannot archive a socket %s", arcn->org_name);
603 paxwarn(1, "Tar cannot archive a fifo %s", arcn->org_name);
608 if (arcn->ln_nlen > sizeof(hd->linkname)) {
609 paxwarn(1,"Link name too long for tar %s", arcn->ln_name);
620 * check file name len, remember extra char for dirs (the / at the end)
623 if (arcn->type == PAX_DIR)
625 if (len >= sizeof(hd->name)) {
626 paxwarn(1, "File name too long for tar %s", arcn->name);
631 * copy the data out of the ARCHD into the tar header based on the type
632 * of the file. Remember many tar readers want the unused fields to be
633 * padded with zero. We set the linkflag field (type), the linkname
634 * (or zero if not used),the size, and set the padding (if any) to be
635 * added after the file data (0 for all other types, as they only have
638 hd = (HD_TAR *)hdblk;
639 l_strncpy(hd->name, arcn->name, sizeof(hd->name) - 1);
640 hd->name[sizeof(hd->name) - 1] = '\0';
643 if (arcn->type == PAX_DIR) {
645 * directories are the same as files, except have a filename
646 * that ends with a /, we add the slash here. No data follows,
649 hd->linkflag = AREGTYPE;
650 memset(hd->linkname, 0, sizeof(hd->linkname));
651 hd->name[len-1] = '/';
652 if (ul_oct((u_long)0L, hd->size, sizeof(hd->size), 1))
654 } else if (arcn->type == PAX_SLK) {
656 * no data follows this file, so no pad
658 hd->linkflag = SYMTYPE;
659 l_strncpy(hd->linkname,arcn->ln_name, sizeof(hd->linkname) - 1);
660 hd->linkname[sizeof(hd->linkname) - 1] = '\0';
661 if (ul_oct((u_long)0L, hd->size, sizeof(hd->size), 1))
663 } else if ((arcn->type == PAX_HLK) || (arcn->type == PAX_HRG)) {
665 * no data follows this file, so no pad
667 hd->linkflag = LNKTYPE;
668 l_strncpy(hd->linkname,arcn->ln_name, sizeof(hd->linkname) - 1);
669 hd->linkname[sizeof(hd->linkname) - 1] = '\0';
670 if (ul_oct((u_long)0L, hd->size, sizeof(hd->size), 1))
674 * data follows this file, so set the pad
676 hd->linkflag = AREGTYPE;
677 memset(hd->linkname, 0, sizeof(hd->linkname));
679 if (ul_oct((u_long)arcn->sb.st_size, hd->size,
680 sizeof(hd->size), 1)) {
682 if (uqd_oct((u_quad_t)arcn->sb.st_size, hd->size,
683 sizeof(hd->size), 1)) {
685 paxwarn(1,"File is too large for tar %s", arcn->org_name);
688 arcn->pad = TAR_PAD(arcn->sb.st_size);
692 * copy those fields that are independent of the type
694 if (ul_oct((u_long)arcn->sb.st_mode, hd->mode, sizeof(hd->mode), 0) ||
695 ul_oct((u_long)arcn->sb.st_uid, hd->uid, sizeof(hd->uid), 0) ||
696 ul_oct((u_long)arcn->sb.st_gid, hd->gid, sizeof(hd->gid), 0) ||
697 ul_oct((u_long)arcn->sb.st_mtime, hd->mtime, sizeof(hd->mtime), 1))
701 * calculate and add the checksum, then write the header. A return of
702 * 0 tells the caller to now write the file data, 1 says no data needs
705 if (ul_oct(tar_chksm(hdblk, sizeof(HD_TAR)), hd->chksum,
706 sizeof(hd->chksum), 3))
708 if (wr_rdbuf(hdblk, sizeof(HD_TAR)) < 0)
710 if (wr_skip((off_t)(BLKMULT - sizeof(HD_TAR))) < 0)
712 if ((arcn->type == PAX_CTG) || (arcn->type == PAX_REG))
718 * header field is out of range
720 paxwarn(1, "Tar header field is too small for %s", arcn->org_name);
725 * Routines for POSIX ustar
730 * initialization for ustar read
732 * 0 if ok, -1 otherwise
743 if ((usrtb_start() < 0) || (grptb_start() < 0))
750 * initialization for ustar write
752 * 0 if ok, -1 otherwise
763 if ((uidtb_start() < 0) || (gidtb_start() < 0))
770 * determine if a block given to us is a valid ustar header. We have to
771 * be on the lookout for those pesky blocks of all zero's
773 * 0 if a ustar header, -1 otherwise
778 ustar_id(char *blk, int size)
786 register HD_USTAR *hd;
790 hd = (HD_USTAR *)blk;
793 * check for block of zero's first, a simple and fast test then check
794 * ustar magic cookie. We should use TMAGLEN, but some USTAR archive
795 * programs are fouled up and create archives missing the \0. Last we
796 * check the checksum. If ok we have to assume it is a valid header.
798 if (hd->name[0] == '\0')
800 if (strncmp(hd->magic, TMAGIC, TMAGLEN - 1) != 0)
802 if (asc_ul(hd->chksum,sizeof(hd->chksum),OCT) != tar_chksm(blk,BLKMULT))
809 * extract the values out of block already determined to be a ustar header.
810 * store the values in the ARCHD parameter.
817 ustar_rd(register ARCHD *arcn, register char *buf)
821 register ARCHD *arcn;
825 register HD_USTAR *hd;
827 register int cnt = 0;
832 * we only get proper sized buffers
834 if (ustar_id(buf, BLKMULT) < 0)
836 arcn->org_name = arcn->name;
837 arcn->sb.st_nlink = 1;
840 hd = (HD_USTAR *)buf;
843 * see if the filename is split into two parts. if, so joint the parts.
844 * we copy the prefix first and add a / between the prefix and name.
846 * the length passed to l_strncpy must be the length of the field
847 * being copied *from*, since these fields are NOT null terminated
848 * when full. the destination buffer is plenty big enough to hold
849 * the longest supported ustar path length, so there's no need
850 * to check against that.
853 if (*(hd->prefix) != '\0') {
854 cnt = l_strncpy(dest, hd->prefix, sizeof(hd->prefix));
859 arcn->nlen = cnt + l_strncpy(dest, hd->name, sizeof(hd->name));
860 arcn->name[arcn->nlen] = '\0';
863 * follow the spec to the letter. we should only have mode bits, strip
864 * off all other crud we may be passed.
866 arcn->sb.st_mode = (mode_t)(asc_ul(hd->mode, sizeof(hd->mode), OCT) &
868 arcn->sb.st_size = (size_t)asc_ul(hd->size, sizeof(hd->size), OCT);
869 arcn->sb.st_mtime = (time_t)asc_ul(hd->mtime, sizeof(hd->mtime), OCT);
870 arcn->sb.st_ctime = arcn->sb.st_atime = arcn->sb.st_mtime;
873 * If we can find the ascii names for gname and uname in the password
874 * and group files we will use the uid's and gid they bind. Otherwise
875 * we use the uid and gid values stored in the header. (This is what
876 * the posix spec wants).
878 hd->gname[sizeof(hd->gname) - 1] = '\0';
879 if (gid_name(hd->gname, &(arcn->sb.st_gid)) < 0)
880 arcn->sb.st_gid = (gid_t)asc_ul(hd->gid, sizeof(hd->gid), OCT);
881 hd->uname[sizeof(hd->uname) - 1] = '\0';
882 if (uid_name(hd->uname, &(arcn->sb.st_uid)) < 0)
883 arcn->sb.st_uid = (uid_t)asc_ul(hd->uid, sizeof(hd->uid), OCT);
886 * set the defaults, these may be changed depending on the file type
888 arcn->ln_name[0] = '\0';
892 arcn->sb.st_rdev = (dev_t)0;
895 * set the mode and PAX type according to the typeflag in the header
897 switch(hd->typeflag) {
899 arcn->type = PAX_FIF;
900 arcn->sb.st_mode |= S_IFIFO;
903 arcn->type = PAX_DIR;
904 arcn->sb.st_mode |= S_IFDIR;
905 arcn->sb.st_nlink = 2;
908 * Some programs that create ustar archives append a '/'
909 * to the pathname for directories. This clearly violates
910 * ustar specs, but we will silently strip it off anyway.
912 if (arcn->name[arcn->nlen - 1] == '/')
913 arcn->name[--arcn->nlen] = '\0';
918 * this type requires the rdev field to be set.
920 if (hd->typeflag == BLKTYPE) {
921 arcn->type = PAX_BLK;
922 arcn->sb.st_mode |= S_IFBLK;
924 arcn->type = PAX_CHR;
925 arcn->sb.st_mode |= S_IFCHR;
927 devmajor = (dev_t)asc_ul(hd->devmajor,sizeof(hd->devmajor),OCT);
928 devminor = (dev_t)asc_ul(hd->devminor,sizeof(hd->devminor),OCT);
929 arcn->sb.st_rdev = TODEV(devmajor, devminor);
933 if (hd->typeflag == SYMTYPE) {
934 arcn->type = PAX_SLK;
935 arcn->sb.st_mode |= S_IFLNK;
937 arcn->type = PAX_HLK;
939 * so printing looks better
941 arcn->sb.st_mode |= S_IFREG;
942 arcn->sb.st_nlink = 2;
947 * see comment above (for hd->name) regarding the length
948 * passed to l_strncpy
950 arcn->ln_nlen = l_strncpy(arcn->ln_name, hd->linkname,
951 sizeof(hd->linkname));
952 arcn->ln_name[arcn->ln_nlen] = '\0';
959 * these types have file data that follows. Set the skip and
962 arcn->type = PAX_REG;
963 arcn->pad = TAR_PAD(arcn->sb.st_size);
964 arcn->skip = arcn->sb.st_size;
965 arcn->sb.st_mode |= S_IFREG;
973 * write a ustar header for the file specified in the ARCHD to the archive
974 * Have to check for file types that cannot be stored and file names that
975 * are too long. Be careful of the term (last arg) to ul_oct, we only use
976 * '\0' for the termination character (this is different than picky tar)
977 * ASSUMED: space after header in header block is zero filled
979 * 0 if file has data to be written after the header, 1 if file has NO
980 * data to write after the header, -1 if archive write failed
985 ustar_wr(register ARCHD *arcn)
989 register ARCHD *arcn;
992 register HD_USTAR *hd;
994 char hdblk[sizeof(HD_USTAR)];
997 * check for those file system types ustar cannot store
999 if (arcn->type == PAX_SCK) {
1000 paxwarn(1, "Ustar cannot archive a socket %s", arcn->org_name);
1005 * check the length of the linkname
1007 if (((arcn->type == PAX_SLK) || (arcn->type == PAX_HLK) ||
1008 (arcn->type == PAX_HRG)) && (arcn->ln_nlen > sizeof(hd->linkname))){
1009 paxwarn(1, "Link name too long for ustar %s", arcn->ln_name);
1014 * split the path name into prefix and name fields (if needed). if
1015 * pt != arcn->name, the name has to be split
1017 if ((pt = name_split(arcn->name, arcn->nlen)) == NULL) {
1018 paxwarn(1, "File name too long for ustar %s", arcn->name);
1021 hd = (HD_USTAR *)hdblk;
1025 * split the name, or zero out the prefix
1027 if (pt != arcn->name) {
1029 * name was split, pt points at the / where the split is to
1030 * occur, we remove the / and copy the first part to the prefix
1033 l_strncpy(hd->prefix, arcn->name, sizeof(hd->prefix));
1036 memset(hd->prefix, 0, sizeof(hd->prefix));
1039 * copy the name part. this may be the whole path or the part after
1042 l_strncpy(hd->name, pt, sizeof(hd->name));
1045 * set the fields in the header that are type dependent
1047 switch(arcn->type) {
1049 hd->typeflag = DIRTYPE;
1050 memset(hd->linkname, 0, sizeof(hd->linkname));
1051 memset(hd->devmajor, 0, sizeof(hd->devmajor));
1052 hd->devmajor[0] = '0';
1053 memset(hd->devminor, 0, sizeof(hd->devminor));
1054 hd->devminor[0] = '0';
1055 if (ul_oct((u_long)0L, hd->size, sizeof(hd->size), 3))
1060 if (arcn->type == PAX_CHR)
1061 hd->typeflag = CHRTYPE;
1063 hd->typeflag = BLKTYPE;
1064 memset(hd->linkname, 0, sizeof(hd->linkname));
1065 if (ul_oct((u_long)MAJOR(arcn->sb.st_rdev), hd->devmajor,
1066 sizeof(hd->devmajor), 3) ||
1067 ul_oct((u_long)MINOR(arcn->sb.st_rdev), hd->devminor,
1068 sizeof(hd->devminor), 3) ||
1069 ul_oct((u_long)0L, hd->size, sizeof(hd->size), 3))
1073 hd->typeflag = FIFOTYPE;
1074 memset(hd->linkname, 0, sizeof(hd->linkname));
1075 memset(hd->devmajor, 0, sizeof(hd->devmajor));
1076 hd->devmajor[0] = '0';
1077 memset(hd->devminor, 0, sizeof(hd->devminor));
1078 hd->devminor[0] = '0';
1079 if (ul_oct((u_long)0L, hd->size, sizeof(hd->size), 3))
1085 if (arcn->type == PAX_SLK)
1086 hd->typeflag = SYMTYPE;
1088 hd->typeflag = LNKTYPE;
1089 l_strncpy(hd->linkname,arcn->ln_name, sizeof(hd->linkname));
1090 memset(hd->devmajor, 0, sizeof(hd->devmajor));
1091 hd->devmajor[0] = '0';
1092 memset(hd->devminor, 0, sizeof(hd->devminor));
1093 hd->devminor[0] = '0';
1094 if (ul_oct((u_long)0L, hd->size, sizeof(hd->size), 3))
1101 * file data with this type, set the padding
1103 if (arcn->type == PAX_CTG)
1104 hd->typeflag = CONTTYPE;
1106 hd->typeflag = REGTYPE;
1107 memset(hd->linkname, 0, sizeof(hd->linkname));
1108 memset(hd->devmajor, 0, sizeof(hd->devmajor));
1109 hd->devmajor[0] = '0';
1110 memset(hd->devminor, 0, sizeof(hd->devminor));
1111 hd->devminor[0] = '0';
1112 arcn->pad = TAR_PAD(arcn->sb.st_size);
1114 if (ul_oct((u_long)arcn->sb.st_size, hd->size,
1115 sizeof(hd->size), 3)) {
1117 if (uqd_oct((u_quad_t)arcn->sb.st_size, hd->size,
1118 sizeof(hd->size), 3)) {
1120 paxwarn(1,"File is too long for ustar %s",arcn->org_name);
1126 l_strncpy(hd->magic, TMAGIC, TMAGLEN);
1127 l_strncpy(hd->version, TVERSION, TVERSLEN);
1130 * set the remaining fields. Some versions want all 16 bits of mode
1131 * we better humor them (they really do not meet spec though)....
1133 if (ul_oct((u_long)arcn->sb.st_mode, hd->mode, sizeof(hd->mode), 3) ||
1134 ul_oct((u_long)arcn->sb.st_uid, hd->uid, sizeof(hd->uid), 3) ||
1135 ul_oct((u_long)arcn->sb.st_gid, hd->gid, sizeof(hd->gid), 3) ||
1136 ul_oct((u_long)arcn->sb.st_mtime,hd->mtime,sizeof(hd->mtime),3))
1138 l_strncpy(hd->uname,name_uid(arcn->sb.st_uid, 0),sizeof(hd->uname));
1139 l_strncpy(hd->gname,name_gid(arcn->sb.st_gid, 0),sizeof(hd->gname));
1142 * calculate and store the checksum write the header to the archive
1143 * return 0 tells the caller to now write the file data, 1 says no data
1144 * needs to be written
1146 if (ul_oct(tar_chksm(hdblk, sizeof(HD_USTAR)), hd->chksum,
1147 sizeof(hd->chksum), 3))
1149 if (wr_rdbuf(hdblk, sizeof(HD_USTAR)) < 0)
1151 if (wr_skip((off_t)(BLKMULT - sizeof(HD_USTAR))) < 0)
1153 if ((arcn->type == PAX_CTG) || (arcn->type == PAX_REG))
1159 * header field is out of range
1161 paxwarn(1, "Ustar header field is too small for %s", arcn->org_name);
1167 * see if the name has to be split for storage in a ustar header. We try
1168 * to fit the entire name in the name field without splitting if we can.
1169 * The split point is always at a /
1171 * character pointer to split point (always the / that is to be removed
1172 * if the split is not needed, the points is set to the start of the file
1173 * name (it would violate the spec to split there). A NULL is returned if
1174 * the file name is too long
1179 name_split(register char *name, register int len)
1182 name_split(name, len)
1183 register char *name;
1187 register char *start;
1190 * check to see if the file name is small enough to fit in the name
1191 * field. if so just return a pointer to the name.
1195 if (len > (TPFSZ + TNMSZ + 1))
1199 * we start looking at the biggest sized piece that fits in the name
1200 * field. We walk foward looking for a slash to split at. The idea is
1201 * to find the biggest piece to fit in the name field (or the smallest
1202 * prefix we can find) (the -1 is correct the biggest piece would
1203 * include the slash between the two parts that gets thrown away)
1205 start = name + len - TNMSZ - 1;
1206 while ((*start != '\0') && (*start != '/'))
1210 * if we hit the end of the string, this name cannot be split, so we
1211 * cannot store this file.
1218 * NOTE: /str where the length of str == TNMSZ can not be stored under
1219 * the p1003.1-1990 spec for ustar. We could force a prefix of / and
1220 * the file would then expand on extract to //str. The len == 0 below
1221 * makes this special case follow the spec to the letter.
1223 if ((len > TPFSZ) || (len == 0))
1227 * ok have a split point, return it to the caller