2 * Copyright (c) 1992 Keith Muller.
3 * Copyright (c) 1992, 1993
4 * The Regents of the University of California. All rights reserved.
6 * This code is derived from software contributed to Berkeley by
7 * Keith Muller of the University of California, San Diego.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * @(#)tar.c 8.2 (Berkeley) 4/18/94
34 * $FreeBSD: src/bin/pax/tar.c,v 1.13.2.1 2001/08/01 05:03:12 obrien Exp $
37 #include <sys/types.h>
51 * Routines for reading, writing and header identify of various versions of tar
54 static u_long tar_chksm (char *, int);
55 static char *name_split (char *, int);
56 static int ul_oct (u_long, char *, int, int);
57 static int uqd_oct (u_quad_t, char *, int, int);
60 * Routines common to all versions of tar
63 static int tar_nodir; /* do not write dirs under old tar */
67 * add the tar trailer of two null blocks
69 * 0 if ok, -1 otherwise (what wr_skip returns)
75 return(wr_skip((off_t)(NULLCNT*BLKMULT)));
80 * no cleanup needed here, just return size of trailer (for append)
82 * size of trailer (2 * BLKMULT)
88 return((off_t)(NULLCNT*BLKMULT));
93 * Called to determine if a header block is a valid trailer. We are passed
94 * the block, the in_sync flag (which tells us we are in resync mode;
95 * looking for a valid header), and cnt (which starts at zero) which is
96 * used to count the number of empty blocks we have seen so far.
98 * 0 if a valid trailer, -1 if not a valid trailer, or 1 if the block
99 * could never contain a header.
103 tar_trail(char *buf, int in_resync, int *cnt)
108 * look for all zero, trailer is two consecutive blocks of zero
110 for (i = 0; i < BLKMULT; ++i) {
116 * if not all zero it is not a trailer, but MIGHT be a header.
122 * When given a zero block, we must be careful!
123 * If we are not in resync mode, check for the trailer. Have to watch
124 * out that we do not mis-identify file data as the trailer, so we do
125 * NOT try to id a trailer during resync mode. During resync mode we
126 * might as well throw this block out since a valid header can NEVER be
127 * a block of all 0 (we must have a valid file name).
129 if (!in_resync && (++*cnt >= NULLCNT))
136 * convert an unsigned long to an octal string. many oddball field
137 * termination characters are used by the various versions of tar in the
138 * different fields. term selects which kind to use. str is '0' padded
139 * at the front to len. we are unable to use only one format as many old
140 * tar readers are very cranky about this.
142 * 0 if the number fit into the string, -1 otherwise
146 ul_oct(u_long val, char *str, int len, int term)
151 * term selects the appropriate character(s) for the end of the string
173 * convert and blank pad if there is space
176 *pt-- = '0' + (char)(val & 0x7);
177 if ((val = val >> 3) == (u_long)0)
183 if (val != (u_long)0)
190 * convert an u_quad_t to an octal string. one of many oddball field
191 * termination characters are used by the various versions of tar in the
192 * different fields. term selects which kind to use. str is '0' padded
193 * at the front to len. we are unable to use only one format as many old
194 * tar readers are very cranky about this.
196 * 0 if the number fit into the string, -1 otherwise
200 uqd_oct(u_quad_t val, char *str, int len, int term)
205 * term selects the appropriate character(s) for the end of the string
227 * convert and blank pad if there is space
230 *pt-- = '0' + (char)(val & 0x7);
231 if ((val = val >> 3) == 0)
237 if (val != (u_quad_t)0)
244 * calculate the checksum for a tar block counting the checksum field as
245 * all blanks (BLNKSUM is that value pre-calculated, the sum of 8 blanks).
246 * NOTE: we use len to short circuit summing 0's on write since we ALWAYS
247 * pad headers with 0.
249 * unsigned long checksum
253 tar_chksm(char *blk, int len)
257 u_long chksm = BLNKSUM; /* initial value is checksum field sum */
260 * add the part of the block before the checksum field
263 stop = blk + CHK_OFFSET;
265 chksm += (u_long)(*pt++ & 0xff);
267 * move past the checksum field and keep going, spec counts the
268 * checksum field as the sum of 8 blanks (which is pre-computed as
270 * ASSUMED: len is greater than CHK_OFFSET. (len is where our 0 padding
271 * starts, no point in summing zero's)
276 chksm += (u_long)(*pt++ & 0xff);
281 * Routines for old BSD style tar (also made portable to sysV tar)
286 * determine if a block given to us is a valid tar header (and not a USTAR
287 * header). We have to be on the lookout for those pesky blocks of all
290 * 0 if a tar header, -1 otherwise
294 tar_id(char *blk, int size)
302 uhd = (HD_USTAR *)blk;
305 * check for block of zero's first, a simple and fast test, then make
306 * sure this is not a ustar header by looking for the ustar magic
307 * cookie. We should use TMAGLEN, but some USTAR archive programs are
308 * wrong and create archives missing the \0. Last we check the
309 * checksum. If this is ok we have to assume it is a valid header.
311 if (hd->name[0] == '\0')
313 if (strncmp(uhd->magic, TMAGIC, TMAGLEN - 1) == 0)
315 if (asc_ul(hd->chksum,sizeof(hd->chksum),OCT) != tar_chksm(blk,BLKMULT))
322 * handle tar format specific -o options
324 * 0 if ok -1 otherwise
332 while ((opt = opt_next()) != NULL) {
333 if (strcmp(opt->name, TAR_OPTION) ||
334 strcmp(opt->value, TAR_NODIR)) {
335 paxwarn(1, "Unknown tar format -o option/value pair %s=%s",
336 opt->name, opt->value);
337 paxwarn(1,"%s=%s is the only supported tar format option",
338 TAR_OPTION, TAR_NODIR);
343 * we only support one option, and only when writing
345 if ((act != APPND) && (act != ARCHIVE)) {
346 paxwarn(1, "%s=%s is only supported when writing.",
347 opt->name, opt->value);
358 * extract the values out of block already determined to be a tar header.
359 * store the values in the ARCHD parameter.
365 tar_rd(ARCHD *arcn, char *buf)
371 * we only get proper sized buffers passed to us
373 if (tar_id(buf, BLKMULT) < 0)
375 arcn->org_name = arcn->name;
376 arcn->sb.st_nlink = 1;
380 * copy out the name and values in the stat buffer
383 arcn->nlen = l_strncpy(arcn->name, hd->name, sizeof(arcn->name) - 1);
384 arcn->name[arcn->nlen] = '\0';
385 arcn->sb.st_mode = (mode_t)(asc_ul(hd->mode,sizeof(hd->mode),OCT) &
387 arcn->sb.st_uid = (uid_t)asc_ul(hd->uid, sizeof(hd->uid), OCT);
388 arcn->sb.st_gid = (gid_t)asc_ul(hd->gid, sizeof(hd->gid), OCT);
389 arcn->sb.st_size = (off_t)asc_uqd(hd->size, sizeof(hd->size), OCT);
390 arcn->sb.st_mtime = (time_t)asc_ul(hd->mtime, sizeof(hd->mtime), OCT);
391 arcn->sb.st_ctime = arcn->sb.st_atime = arcn->sb.st_mtime;
394 * have to look at the last character, it may be a '/' and that is used
395 * to encode this as a directory
397 pt = &(arcn->name[arcn->nlen - 1]);
400 switch(hd->linkflag) {
403 * symbolic link, need to get the link name and set the type in
404 * the st_mode so -v printing will look correct.
406 arcn->type = PAX_SLK;
407 arcn->ln_nlen = l_strncpy(arcn->ln_name, hd->linkname,
408 sizeof(arcn->ln_name) - 1);
409 arcn->ln_name[arcn->ln_nlen] = '\0';
410 arcn->sb.st_mode |= S_IFLNK;
414 * hard link, need to get the link name, set the type in the
415 * st_mode and st_nlink so -v printing will look better.
417 arcn->type = PAX_HLK;
418 arcn->sb.st_nlink = 2;
419 arcn->ln_nlen = l_strncpy(arcn->ln_name, hd->linkname,
420 sizeof(arcn->ln_name) - 1);
421 arcn->ln_name[arcn->ln_nlen] = '\0';
424 * no idea of what type this thing really points at, but
425 * we set something for printing only.
427 arcn->sb.st_mode |= S_IFREG;
431 * It is a directory, set the mode for -v printing
433 arcn->type = PAX_DIR;
434 arcn->sb.st_mode |= S_IFDIR;
435 arcn->sb.st_nlink = 2;
436 arcn->ln_name[0] = '\0';
443 * If we have a trailing / this is a directory and NOT a file.
445 arcn->ln_name[0] = '\0';
449 * it is a directory, set the mode for -v printing
451 arcn->type = PAX_DIR;
452 arcn->sb.st_mode |= S_IFDIR;
453 arcn->sb.st_nlink = 2;
456 * have a file that will be followed by data. Set the
457 * skip value to the size field and calculate the size
460 arcn->type = PAX_REG;
461 arcn->sb.st_mode |= S_IFREG;
462 arcn->pad = TAR_PAD(arcn->sb.st_size);
463 arcn->skip = arcn->sb.st_size;
469 * strip off any trailing slash.
480 * write a tar header for the file specified in the ARCHD to the archive.
481 * Have to check for file types that cannot be stored and file names that
482 * are too long. Be careful of the term (last arg) to ul_oct, each field
483 * of tar has it own spec for the termination character(s).
484 * ASSUMED: space after header in header block is zero filled
486 * 0 if file has data to be written after the header, 1 if file has NO
487 * data to write after the header, -1 if archive write failed
495 char hdblk[sizeof(HD_TAR)];
498 * check for those file system types which tar cannot store
503 * user asked that dirs not be written to the archive
509 paxwarn(1, "Tar cannot archive a character device %s",
513 paxwarn(1, "Tar cannot archive a block device %s", arcn->org_name);
516 paxwarn(1, "Tar cannot archive a socket %s", arcn->org_name);
519 paxwarn(1, "Tar cannot archive a fifo %s", arcn->org_name);
524 if (arcn->ln_nlen > sizeof(hd->linkname)) {
525 paxwarn(1,"Link name too long for tar %s", arcn->ln_name);
536 * check file name len, remember extra char for dirs (the / at the end)
539 if (arcn->type == PAX_DIR)
541 if (len >= sizeof(hd->name)) {
542 paxwarn(1, "File name too long for tar %s", arcn->name);
547 * copy the data out of the ARCHD into the tar header based on the type
548 * of the file. Remember many tar readers want the unused fields to be
549 * padded with zero. We set the linkflag field (type), the linkname
550 * (or zero if not used),the size, and set the padding (if any) to be
551 * added after the file data (0 for all other types, as they only have
554 hd = (HD_TAR *)hdblk;
555 l_strncpy(hd->name, arcn->name, sizeof(hd->name) - 1);
556 hd->name[sizeof(hd->name) - 1] = '\0';
559 if (arcn->type == PAX_DIR) {
561 * directories are the same as files, except have a filename
562 * that ends with a /, we add the slash here. No data follows
565 hd->linkflag = AREGTYPE;
566 memset(hd->linkname, 0, sizeof(hd->linkname));
567 hd->name[len-1] = '/';
568 if (ul_oct((u_long)0L, hd->size, sizeof(hd->size), 1))
570 } else if (arcn->type == PAX_SLK) {
572 * no data follows this file, so no pad
574 hd->linkflag = SYMTYPE;
575 l_strncpy(hd->linkname,arcn->ln_name, sizeof(hd->linkname) - 1);
576 hd->linkname[sizeof(hd->linkname) - 1] = '\0';
577 if (ul_oct((u_long)0L, hd->size, sizeof(hd->size), 1))
579 } else if ((arcn->type == PAX_HLK) || (arcn->type == PAX_HRG)) {
581 * no data follows this file, so no pad
583 hd->linkflag = LNKTYPE;
584 l_strncpy(hd->linkname,arcn->ln_name, sizeof(hd->linkname) - 1);
585 hd->linkname[sizeof(hd->linkname) - 1] = '\0';
586 if (ul_oct((u_long)0L, hd->size, sizeof(hd->size), 1))
590 * data follows this file, so set the pad
592 hd->linkflag = AREGTYPE;
593 memset(hd->linkname, 0, sizeof(hd->linkname));
594 if (uqd_oct((u_quad_t)arcn->sb.st_size, hd->size,
595 sizeof(hd->size), 1)) {
596 paxwarn(1,"File is too large for tar %s", arcn->org_name);
599 arcn->pad = TAR_PAD(arcn->sb.st_size);
603 * copy those fields that are independent of the type
605 if (ul_oct((u_long)arcn->sb.st_mode, hd->mode, sizeof(hd->mode), 0) ||
606 ul_oct((u_long)arcn->sb.st_uid, hd->uid, sizeof(hd->uid), 0) ||
607 ul_oct((u_long)arcn->sb.st_gid, hd->gid, sizeof(hd->gid), 0) ||
608 ul_oct((u_long)arcn->sb.st_mtime, hd->mtime, sizeof(hd->mtime), 1))
612 * calculate and add the checksum, then write the header. A return of
613 * 0 tells the caller to now write the file data, 1 says no data needs
616 if (ul_oct(tar_chksm(hdblk, sizeof(HD_TAR)), hd->chksum,
617 sizeof(hd->chksum), 3))
619 if (wr_rdbuf(hdblk, sizeof(HD_TAR)) < 0)
621 if (wr_skip((off_t)(BLKMULT - sizeof(HD_TAR))) < 0)
623 if ((arcn->type == PAX_CTG) || (arcn->type == PAX_REG))
629 * header field is out of range
631 paxwarn(1, "Tar header field is too small for %s", arcn->org_name);
636 * Routines for POSIX ustar
641 * initialization for ustar read
643 * 0 if ok, -1 otherwise
654 * initialization for ustar write
656 * 0 if ok, -1 otherwise
667 * determine if a block given to us is a valid ustar header. We have to
668 * be on the lookout for those pesky blocks of all zero's
670 * 0 if a ustar header, -1 otherwise
674 ustar_id(char *blk, int size)
680 hd = (HD_USTAR *)blk;
683 * check for block of zero's first, a simple and fast test then check
684 * ustar magic cookie. We should use TMAGLEN, but some USTAR archive
685 * programs are fouled up and create archives missing the \0. Last we
686 * check the checksum. If ok we have to assume it is a valid header.
688 if (hd->name[0] == '\0')
690 if (strncmp(hd->magic, TMAGIC, TMAGLEN - 1) != 0)
692 if (asc_ul(hd->chksum,sizeof(hd->chksum),OCT) != tar_chksm(blk,BLKMULT))
699 * extract the values out of block already determined to be a ustar header.
700 * store the values in the ARCHD parameter.
706 ustar_rd(ARCHD *arcn, char *buf)
715 * we only get proper sized buffers
717 if (ustar_id(buf, BLKMULT) < 0)
719 arcn->org_name = arcn->name;
720 arcn->sb.st_nlink = 1;
723 hd = (HD_USTAR *)buf;
726 * see if the filename is split into two parts. if, so joint the parts.
727 * we copy the prefix first and add a / between the prefix and name.
730 if (*(hd->prefix) != '\0') {
731 cnt = l_strncpy(dest, hd->prefix, sizeof(arcn->name) - 2);
736 arcn->nlen = cnt + l_strncpy(dest, hd->name, sizeof(arcn->name) - cnt);
737 arcn->name[arcn->nlen] = '\0';
740 * follow the spec to the letter. we should only have mode bits, strip
741 * off all other crud we may be passed.
743 arcn->sb.st_mode = (mode_t)(asc_ul(hd->mode, sizeof(hd->mode), OCT) &
745 arcn->sb.st_size = (off_t)asc_uqd(hd->size, sizeof(hd->size), OCT);
746 arcn->sb.st_mtime = (time_t)asc_ul(hd->mtime, sizeof(hd->mtime), OCT);
747 arcn->sb.st_ctime = arcn->sb.st_atime = arcn->sb.st_mtime;
750 * If we can find the ascii names for gname and uname in the password
751 * and group files we will use the uid's and gid they bind. Otherwise
752 * we use the uid and gid values stored in the header. (This is what
753 * the POSIX spec wants).
755 hd->gname[sizeof(hd->gname) - 1] = '\0';
756 if (gid_from_group(hd->gname, &(arcn->sb.st_gid)) < 0)
757 arcn->sb.st_gid = (gid_t)asc_ul(hd->gid, sizeof(hd->gid), OCT);
758 hd->uname[sizeof(hd->uname) - 1] = '\0';
759 if (uid_from_user(hd->uname, &(arcn->sb.st_uid)) < 0)
760 arcn->sb.st_uid = (uid_t)asc_ul(hd->uid, sizeof(hd->uid), OCT);
763 * set the defaults, these may be changed depending on the file type
765 arcn->ln_name[0] = '\0';
769 arcn->sb.st_rdev = (dev_t)0;
772 * set the mode and PAX type according to the typeflag in the header
774 switch(hd->typeflag) {
776 arcn->type = PAX_FIF;
777 arcn->sb.st_mode |= S_IFIFO;
780 arcn->type = PAX_DIR;
781 arcn->sb.st_mode |= S_IFDIR;
782 arcn->sb.st_nlink = 2;
785 * Some programs that create ustar archives append a '/'
786 * to the pathname for directories. This clearly violates
787 * ustar specs, but we will silently strip it off anyway.
789 if (arcn->name[arcn->nlen - 1] == '/')
790 arcn->name[--arcn->nlen] = '\0';
795 * this type requires the rdev field to be set.
797 if (hd->typeflag == BLKTYPE) {
798 arcn->type = PAX_BLK;
799 arcn->sb.st_mode |= S_IFBLK;
801 arcn->type = PAX_CHR;
802 arcn->sb.st_mode |= S_IFCHR;
804 devmajor = (dev_t)asc_ul(hd->devmajor,sizeof(hd->devmajor),OCT);
805 devminor = (dev_t)asc_ul(hd->devminor,sizeof(hd->devminor),OCT);
806 arcn->sb.st_rdev = TODEV(devmajor, devminor);
810 if (hd->typeflag == SYMTYPE) {
811 arcn->type = PAX_SLK;
812 arcn->sb.st_mode |= S_IFLNK;
814 arcn->type = PAX_HLK;
816 * so printing looks better
818 arcn->sb.st_mode |= S_IFREG;
819 arcn->sb.st_nlink = 2;
824 arcn->ln_nlen = l_strncpy(arcn->ln_name, hd->linkname,
825 sizeof(arcn->ln_name) - 1);
826 arcn->ln_name[arcn->ln_nlen] = '\0';
833 * these types have file data that follows. Set the skip and
836 arcn->type = PAX_REG;
837 arcn->pad = TAR_PAD(arcn->sb.st_size);
838 arcn->skip = arcn->sb.st_size;
839 arcn->sb.st_mode |= S_IFREG;
847 * write a ustar header for the file specified in the ARCHD to the archive
848 * Have to check for file types that cannot be stored and file names that
849 * are too long. Be careful of the term (last arg) to ul_oct, we only use
850 * '\0' for the termination character (this is different than picky tar)
851 * ASSUMED: space after header in header block is zero filled
853 * 0 if file has data to be written after the header, 1 if file has NO
854 * data to write after the header, -1 if archive write failed
858 ustar_wr(ARCHD *arcn)
862 char hdblk[sizeof(HD_USTAR)];
863 const char *user, *group;
866 * check for those file system types ustar cannot store
868 if (arcn->type == PAX_SCK) {
869 paxwarn(1, "Ustar cannot archive a socket %s", arcn->org_name);
874 * check the length of the linkname
876 if (((arcn->type == PAX_SLK) || (arcn->type == PAX_HLK) ||
877 (arcn->type == PAX_HRG)) && (arcn->ln_nlen >= sizeof(hd->linkname))){
878 paxwarn(1, "Link name too long for ustar %s", arcn->ln_name);
883 * split the path name into prefix and name fields (if needed). if
884 * pt != arcn->name, the name has to be split
886 if ((pt = name_split(arcn->name, arcn->nlen)) == NULL) {
887 paxwarn(1, "File name too long for ustar %s", arcn->name);
890 hd = (HD_USTAR *)hdblk;
894 * split the name, or zero out the prefix
896 if (pt != arcn->name) {
898 * name was split, pt points at the / where the split is to
899 * occur, we remove the / and copy the first part to the prefix
902 l_strncpy(hd->prefix, arcn->name, sizeof(hd->prefix) - 1);
905 memset(hd->prefix, 0, sizeof(hd->prefix));
908 * copy the name part. this may be the whole path or the part after
911 l_strncpy(hd->name, pt, sizeof(hd->name) - 1);
912 hd->name[sizeof(hd->name) - 1] = '\0';
915 * set the fields in the header that are type dependent
919 hd->typeflag = DIRTYPE;
920 memset(hd->linkname, 0, sizeof(hd->linkname));
921 memset(hd->devmajor, 0, sizeof(hd->devmajor));
922 memset(hd->devminor, 0, sizeof(hd->devminor));
923 if (ul_oct((u_long)0L, hd->size, sizeof(hd->size), 3))
928 if (arcn->type == PAX_CHR)
929 hd->typeflag = CHRTYPE;
931 hd->typeflag = BLKTYPE;
932 memset(hd->linkname, 0, sizeof(hd->linkname));
933 if (ul_oct((u_long)MAJOR(arcn->sb.st_rdev), hd->devmajor,
934 sizeof(hd->devmajor), 3) ||
935 ul_oct((u_long)MINOR(arcn->sb.st_rdev), hd->devminor,
936 sizeof(hd->devminor), 3) ||
937 ul_oct((u_long)0L, hd->size, sizeof(hd->size), 3))
941 hd->typeflag = FIFOTYPE;
942 memset(hd->linkname, 0, sizeof(hd->linkname));
943 memset(hd->devmajor, 0, sizeof(hd->devmajor));
944 memset(hd->devminor, 0, sizeof(hd->devminor));
945 if (ul_oct((u_long)0L, hd->size, sizeof(hd->size), 3))
951 if (arcn->type == PAX_SLK)
952 hd->typeflag = SYMTYPE;
954 hd->typeflag = LNKTYPE;
955 l_strncpy(hd->linkname,arcn->ln_name, sizeof(hd->linkname) - 1);
956 hd->linkname[sizeof(hd->linkname) - 1] = '\0';
957 memset(hd->devmajor, 0, sizeof(hd->devmajor));
958 memset(hd->devminor, 0, sizeof(hd->devminor));
959 if (ul_oct((u_long)0L, hd->size, sizeof(hd->size), 3))
966 * file data with this type, set the padding
968 if (arcn->type == PAX_CTG)
969 hd->typeflag = CONTTYPE;
971 hd->typeflag = REGTYPE;
972 memset(hd->linkname, 0, sizeof(hd->linkname));
973 memset(hd->devmajor, 0, sizeof(hd->devmajor));
974 memset(hd->devminor, 0, sizeof(hd->devminor));
975 arcn->pad = TAR_PAD(arcn->sb.st_size);
976 if (uqd_oct((u_quad_t)arcn->sb.st_size, hd->size,
977 sizeof(hd->size), 3)) {
978 paxwarn(1,"File is too long for ustar %s",arcn->org_name);
984 l_strncpy(hd->magic, TMAGIC, TMAGLEN);
985 l_strncpy(hd->version, TVERSION, TVERSLEN);
988 * set the remaining fields. Some versions want all 16 bits of mode
989 * we better humor them (they really do not meet spec though)....
991 if (ul_oct((u_long)arcn->sb.st_mode, hd->mode, sizeof(hd->mode), 3) ||
992 ul_oct((u_long)arcn->sb.st_uid, hd->uid, sizeof(hd->uid), 3) ||
993 ul_oct((u_long)arcn->sb.st_gid, hd->gid, sizeof(hd->gid), 3) ||
994 ul_oct((u_long)arcn->sb.st_mtime,hd->mtime,sizeof(hd->mtime),3))
996 user = user_from_uid(arcn->sb.st_uid, 1);
997 group = group_from_gid(arcn->sb.st_gid, 1);
998 l_strncpy(hd->uname, user ? user : "", sizeof(hd->uname));
999 l_strncpy(hd->gname, group ? group : "", sizeof(hd->gname));
1002 * calculate and store the checksum write the header to the archive
1003 * return 0 tells the caller to now write the file data, 1 says no data
1004 * needs to be written
1006 if (ul_oct(tar_chksm(hdblk, sizeof(HD_USTAR)), hd->chksum,
1007 sizeof(hd->chksum), 3))
1009 if (wr_rdbuf(hdblk, sizeof(HD_USTAR)) < 0)
1011 if (wr_skip((off_t)(BLKMULT - sizeof(HD_USTAR))) < 0)
1013 if ((arcn->type == PAX_CTG) || (arcn->type == PAX_REG))
1019 * header field is out of range
1021 paxwarn(1, "Ustar header field is too small for %s", arcn->org_name);
1027 * see if the name has to be split for storage in a ustar header. We try
1028 * to fit the entire name in the name field without splitting if we can.
1029 * The split point is always at a /
1031 * character pointer to split point (always the / that is to be removed
1032 * if the split is not needed, the points is set to the start of the file
1033 * name (it would violate the spec to split there). A NULL is returned if
1034 * the file name is too long
1038 name_split(char *name, int len)
1043 * check to see if the file name is small enough to fit in the name
1044 * field. if so just return a pointer to the name.
1048 if (len > (TPFSZ + TNMSZ))
1052 * we start looking at the biggest sized piece that fits in the name
1053 * field. We walk forward looking for a slash to split at. The idea is
1054 * to find the biggest piece to fit in the name field (or the smallest
1055 * prefix we can find)
1057 start = name + len - TNMSZ;
1058 while ((*start != '\0') && (*start != '/'))
1062 * if we hit the end of the string, this name cannot be split, so we
1063 * cannot store this file.
1070 * NOTE: /str where the length of str == TNMSZ can not be stored under
1071 * the p1003.1-1990 spec for ustar. We could force a prefix of / and
1072 * the file would then expand on extract to //str. The len == 0 below
1073 * makes this special case follow the spec to the letter.
1075 if ((len >= TPFSZ) || (len == 0))
1079 * ok have a split point, return it to the caller