xref: /freebsd/bin/pax/tar.c (revision e043f372)
14b88c807SRodney W. Grimes /*-
28a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
38a16b7a1SPedro F. Giffuni  *
44b88c807SRodney W. Grimes  * Copyright (c) 1992 Keith Muller.
54b88c807SRodney W. Grimes  * Copyright (c) 1992, 1993
64b88c807SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
74b88c807SRodney W. Grimes  *
84b88c807SRodney W. Grimes  * This code is derived from software contributed to Berkeley by
94b88c807SRodney W. Grimes  * Keith Muller of the University of California, San Diego.
104b88c807SRodney W. Grimes  *
114b88c807SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
124b88c807SRodney W. Grimes  * modification, are permitted provided that the following conditions
134b88c807SRodney W. Grimes  * are met:
144b88c807SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
154b88c807SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
164b88c807SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
174b88c807SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
184b88c807SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
19fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
204b88c807SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
214b88c807SRodney W. Grimes  *    without specific prior written permission.
224b88c807SRodney W. Grimes  *
234b88c807SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
244b88c807SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
254b88c807SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
264b88c807SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
274b88c807SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
284b88c807SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
294b88c807SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
304b88c807SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
314b88c807SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
324b88c807SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
334b88c807SRodney W. Grimes  * SUCH DAMAGE.
344b88c807SRodney W. Grimes  */
354b88c807SRodney W. Grimes 
364b88c807SRodney W. Grimes #include <sys/types.h>
374b88c807SRodney W. Grimes #include <sys/time.h>
384b88c807SRodney W. Grimes #include <sys/stat.h>
394b88c807SRodney W. Grimes #include <string.h>
404b88c807SRodney W. Grimes #include <stdio.h>
414b88c807SRodney W. Grimes #include "pax.h"
424b88c807SRodney W. Grimes #include "extern.h"
434b88c807SRodney W. Grimes #include "tar.h"
444b88c807SRodney W. Grimes 
454b88c807SRodney W. Grimes /*
464b88c807SRodney W. Grimes  * Routines for reading, writing and header identify of various versions of tar
474b88c807SRodney W. Grimes  */
484b88c807SRodney W. Grimes 
49f789b261SWarner Losh static u_long tar_chksm(char *, int);
50f789b261SWarner Losh static char *name_split(char *, int);
51f789b261SWarner Losh static int ul_oct(u_long, char *, int, int);
52f789b261SWarner Losh static int uqd_oct(u_quad_t, char *, int, int);
534b88c807SRodney W. Grimes 
544b88c807SRodney W. Grimes /*
554b88c807SRodney W. Grimes  * Routines common to all versions of tar
564b88c807SRodney W. Grimes  */
574b88c807SRodney W. Grimes 
584b88c807SRodney W. Grimes static int tar_nodir;			/* do not write dirs under old tar */
594b88c807SRodney W. Grimes 
604b88c807SRodney W. Grimes /*
614b88c807SRodney W. Grimes  * tar_endwr()
624b88c807SRodney W. Grimes  *	add the tar trailer of two null blocks
634b88c807SRodney W. Grimes  * Return:
644b88c807SRodney W. Grimes  *	0 if ok, -1 otherwise (what wr_skip returns)
654b88c807SRodney W. Grimes  */
664b88c807SRodney W. Grimes 
674b88c807SRodney W. Grimes int
tar_endwr(void)684b88c807SRodney W. Grimes tar_endwr(void)
694b88c807SRodney W. Grimes {
704b88c807SRodney W. Grimes 	return(wr_skip((off_t)(NULLCNT*BLKMULT)));
714b88c807SRodney W. Grimes }
724b88c807SRodney W. Grimes 
734b88c807SRodney W. Grimes /*
744b88c807SRodney W. Grimes  * tar_endrd()
754b88c807SRodney W. Grimes  *	no cleanup needed here, just return size of trailer (for append)
764b88c807SRodney W. Grimes  * Return:
774b88c807SRodney W. Grimes  *	size of trailer (2 * BLKMULT)
784b88c807SRodney W. Grimes  */
794b88c807SRodney W. Grimes 
804b88c807SRodney W. Grimes off_t
tar_endrd(void)814b88c807SRodney W. Grimes tar_endrd(void)
824b88c807SRodney W. Grimes {
834b88c807SRodney W. Grimes 	return((off_t)(NULLCNT*BLKMULT));
844b88c807SRodney W. Grimes }
854b88c807SRodney W. Grimes 
864b88c807SRodney W. Grimes /*
874b88c807SRodney W. Grimes  * tar_trail()
884b88c807SRodney W. Grimes  *	Called to determine if a header block is a valid trailer. We are passed
894b88c807SRodney W. Grimes  *	the block, the in_sync flag (which tells us we are in resync mode;
904b88c807SRodney W. Grimes  *	looking for a valid header), and cnt (which starts at zero) which is
914b88c807SRodney W. Grimes  *	used to count the number of empty blocks we have seen so far.
924b88c807SRodney W. Grimes  * Return:
934b88c807SRodney W. Grimes  *	0 if a valid trailer, -1 if not a valid trailer, or 1 if the block
944b88c807SRodney W. Grimes  *	could never contain a header.
954b88c807SRodney W. Grimes  */
964b88c807SRodney W. Grimes 
974b88c807SRodney W. Grimes int
tar_trail(char * buf,int in_resync,int * cnt)98f789b261SWarner Losh tar_trail(char *buf, int in_resync, int *cnt)
994b88c807SRodney W. Grimes {
100f789b261SWarner Losh 	int i;
1014b88c807SRodney W. Grimes 
1024b88c807SRodney W. Grimes 	/*
1034b88c807SRodney W. Grimes 	 * look for all zero, trailer is two consecutive blocks of zero
1044b88c807SRodney W. Grimes 	 */
1054b88c807SRodney W. Grimes 	for (i = 0; i < BLKMULT; ++i) {
1064b88c807SRodney W. Grimes 		if (buf[i] != '\0')
1074b88c807SRodney W. Grimes 			break;
1084b88c807SRodney W. Grimes 	}
1094b88c807SRodney W. Grimes 
1104b88c807SRodney W. Grimes 	/*
1114b88c807SRodney W. Grimes 	 * if not all zero it is not a trailer, but MIGHT be a header.
1124b88c807SRodney W. Grimes 	 */
1134b88c807SRodney W. Grimes 	if (i != BLKMULT)
1144b88c807SRodney W. Grimes 		return(-1);
1154b88c807SRodney W. Grimes 
1164b88c807SRodney W. Grimes 	/*
1174b88c807SRodney W. Grimes 	 * When given a zero block, we must be careful!
1184b88c807SRodney W. Grimes 	 * If we are not in resync mode, check for the trailer. Have to watch
1194b88c807SRodney W. Grimes 	 * out that we do not mis-identify file data as the trailer, so we do
1204b88c807SRodney W. Grimes 	 * NOT try to id a trailer during resync mode. During resync mode we
1214b88c807SRodney W. Grimes 	 * might as well throw this block out since a valid header can NEVER be
1224b88c807SRodney W. Grimes 	 * a block of all 0 (we must have a valid file name).
1234b88c807SRodney W. Grimes 	 */
1244b88c807SRodney W. Grimes 	if (!in_resync && (++*cnt >= NULLCNT))
1254b88c807SRodney W. Grimes 		return(0);
1264b88c807SRodney W. Grimes 	return(1);
1274b88c807SRodney W. Grimes }
1284b88c807SRodney W. Grimes 
1294b88c807SRodney W. Grimes /*
1304b88c807SRodney W. Grimes  * ul_oct()
1314b88c807SRodney W. Grimes  *	convert an unsigned long to an octal string. many oddball field
1324b88c807SRodney W. Grimes  *	termination characters are used by the various versions of tar in the
133b1787decSKris Kennaway  *	different fields. term selects which kind to use. str is '0' padded
1344b88c807SRodney W. Grimes  *	at the front to len. we are unable to use only one format as many old
1354b88c807SRodney W. Grimes  *	tar readers are very cranky about this.
1364b88c807SRodney W. Grimes  * Return:
1374b88c807SRodney W. Grimes  *	0 if the number fit into the string, -1 otherwise
1384b88c807SRodney W. Grimes  */
1394b88c807SRodney W. Grimes 
1404b88c807SRodney W. Grimes static int
ul_oct(u_long val,char * str,int len,int term)141f789b261SWarner Losh ul_oct(u_long val, char *str, int len, int term)
1424b88c807SRodney W. Grimes {
143f789b261SWarner Losh 	char *pt;
1444b88c807SRodney W. Grimes 
1454b88c807SRodney W. Grimes 	/*
1464b88c807SRodney W. Grimes 	 * term selects the appropriate character(s) for the end of the string
1474b88c807SRodney W. Grimes 	 */
1484b88c807SRodney W. Grimes 	pt = str + len - 1;
1494b88c807SRodney W. Grimes 	switch(term) {
1504b88c807SRodney W. Grimes 	case 3:
1514b88c807SRodney W. Grimes 		*pt-- = '\0';
1524b88c807SRodney W. Grimes 		break;
1534b88c807SRodney W. Grimes 	case 2:
1544b88c807SRodney W. Grimes 		*pt-- = ' ';
1554b88c807SRodney W. Grimes 		*pt-- = '\0';
1564b88c807SRodney W. Grimes 		break;
1574b88c807SRodney W. Grimes 	case 1:
1584b88c807SRodney W. Grimes 		*pt-- = ' ';
1594b88c807SRodney W. Grimes 		break;
1604b88c807SRodney W. Grimes 	case 0:
1614b88c807SRodney W. Grimes 	default:
1624b88c807SRodney W. Grimes 		*pt-- = '\0';
1634b88c807SRodney W. Grimes 		*pt-- = ' ';
1644b88c807SRodney W. Grimes 		break;
1654b88c807SRodney W. Grimes 	}
1664b88c807SRodney W. Grimes 
1674b88c807SRodney W. Grimes 	/*
1684b88c807SRodney W. Grimes 	 * convert and blank pad if there is space
1694b88c807SRodney W. Grimes 	 */
1704b88c807SRodney W. Grimes 	while (pt >= str) {
1714b88c807SRodney W. Grimes 		*pt-- = '0' + (char)(val & 0x7);
1724b88c807SRodney W. Grimes 		if ((val = val >> 3) == (u_long)0)
1734b88c807SRodney W. Grimes 			break;
1744b88c807SRodney W. Grimes 	}
1754b88c807SRodney W. Grimes 
1764b88c807SRodney W. Grimes 	while (pt >= str)
177b1787decSKris Kennaway 		*pt-- = '0';
1784b88c807SRodney W. Grimes 	if (val != (u_long)0)
1794b88c807SRodney W. Grimes 		return(-1);
1804b88c807SRodney W. Grimes 	return(0);
1814b88c807SRodney W. Grimes }
1824b88c807SRodney W. Grimes 
1834b88c807SRodney W. Grimes /*
1844b88c807SRodney W. Grimes  * uqd_oct()
1854b88c807SRodney W. Grimes  *	convert an u_quad_t to an octal string. one of many oddball field
1864b88c807SRodney W. Grimes  *	termination characters are used by the various versions of tar in the
187b1787decSKris Kennaway  *	different fields. term selects which kind to use. str is '0' padded
1884b88c807SRodney W. Grimes  *	at the front to len. we are unable to use only one format as many old
1894b88c807SRodney W. Grimes  *	tar readers are very cranky about this.
1904b88c807SRodney W. Grimes  * Return:
1914b88c807SRodney W. Grimes  *	0 if the number fit into the string, -1 otherwise
1924b88c807SRodney W. Grimes  */
1934b88c807SRodney W. Grimes 
1944b88c807SRodney W. Grimes static int
uqd_oct(u_quad_t val,char * str,int len,int term)195f789b261SWarner Losh uqd_oct(u_quad_t val, char *str, int len, int term)
1964b88c807SRodney W. Grimes {
197f789b261SWarner Losh 	char *pt;
1984b88c807SRodney W. Grimes 
1994b88c807SRodney W. Grimes 	/*
2004b88c807SRodney W. Grimes 	 * term selects the appropriate character(s) for the end of the string
2014b88c807SRodney W. Grimes 	 */
2024b88c807SRodney W. Grimes 	pt = str + len - 1;
2034b88c807SRodney W. Grimes 	switch(term) {
2044b88c807SRodney W. Grimes 	case 3:
2054b88c807SRodney W. Grimes 		*pt-- = '\0';
2064b88c807SRodney W. Grimes 		break;
2074b88c807SRodney W. Grimes 	case 2:
2084b88c807SRodney W. Grimes 		*pt-- = ' ';
2094b88c807SRodney W. Grimes 		*pt-- = '\0';
2104b88c807SRodney W. Grimes 		break;
2114b88c807SRodney W. Grimes 	case 1:
2124b88c807SRodney W. Grimes 		*pt-- = ' ';
2134b88c807SRodney W. Grimes 		break;
2144b88c807SRodney W. Grimes 	case 0:
2154b88c807SRodney W. Grimes 	default:
2164b88c807SRodney W. Grimes 		*pt-- = '\0';
2174b88c807SRodney W. Grimes 		*pt-- = ' ';
2184b88c807SRodney W. Grimes 		break;
2194b88c807SRodney W. Grimes 	}
2204b88c807SRodney W. Grimes 
2214b88c807SRodney W. Grimes 	/*
2224b88c807SRodney W. Grimes 	 * convert and blank pad if there is space
2234b88c807SRodney W. Grimes 	 */
2244b88c807SRodney W. Grimes 	while (pt >= str) {
2254b88c807SRodney W. Grimes 		*pt-- = '0' + (char)(val & 0x7);
2264b88c807SRodney W. Grimes 		if ((val = val >> 3) == 0)
2274b88c807SRodney W. Grimes 			break;
2284b88c807SRodney W. Grimes 	}
2294b88c807SRodney W. Grimes 
2304b88c807SRodney W. Grimes 	while (pt >= str)
231b1787decSKris Kennaway 		*pt-- = '0';
2324b88c807SRodney W. Grimes 	if (val != (u_quad_t)0)
2334b88c807SRodney W. Grimes 		return(-1);
2344b88c807SRodney W. Grimes 	return(0);
2354b88c807SRodney W. Grimes }
2364b88c807SRodney W. Grimes 
2374b88c807SRodney W. Grimes /*
2384b88c807SRodney W. Grimes  * tar_chksm()
2394b88c807SRodney W. Grimes  *	calculate the checksum for a tar block counting the checksum field as
24046be34b9SKris Kennaway  *	all blanks (BLNKSUM is that value pre-calculated, the sum of 8 blanks).
2414b88c807SRodney W. Grimes  *	NOTE: we use len to short circuit summing 0's on write since we ALWAYS
2424b88c807SRodney W. Grimes  *	pad headers with 0.
2434b88c807SRodney W. Grimes  * Return:
2444b88c807SRodney W. Grimes  *	unsigned long checksum
2454b88c807SRodney W. Grimes  */
2464b88c807SRodney W. Grimes 
2474b88c807SRodney W. Grimes static u_long
tar_chksm(char * blk,int len)248f789b261SWarner Losh tar_chksm(char *blk, int len)
2494b88c807SRodney W. Grimes {
250f789b261SWarner Losh 	char *stop;
251f789b261SWarner Losh 	char *pt;
25246be34b9SKris Kennaway 	u_long chksm = BLNKSUM;	/* initial value is checksum field sum */
2534b88c807SRodney W. Grimes 
2544b88c807SRodney W. Grimes 	/*
2554b88c807SRodney W. Grimes 	 * add the part of the block before the checksum field
2564b88c807SRodney W. Grimes 	 */
2574b88c807SRodney W. Grimes 	pt = blk;
2584b88c807SRodney W. Grimes 	stop = blk + CHK_OFFSET;
2594b88c807SRodney W. Grimes 	while (pt < stop)
2604b88c807SRodney W. Grimes 		chksm += (u_long)(*pt++ & 0xff);
2614b88c807SRodney W. Grimes 	/*
2624b88c807SRodney W. Grimes 	 * move past the checksum field and keep going, spec counts the
2634b88c807SRodney W. Grimes 	 * checksum field as the sum of 8 blanks (which is pre-computed as
2644b88c807SRodney W. Grimes 	 * BLNKSUM).
2654b88c807SRodney W. Grimes 	 * ASSUMED: len is greater than CHK_OFFSET. (len is where our 0 padding
2664b88c807SRodney W. Grimes 	 * starts, no point in summing zero's)
2674b88c807SRodney W. Grimes 	 */
2684b88c807SRodney W. Grimes 	pt += CHK_LEN;
2694b88c807SRodney W. Grimes 	stop = blk + len;
2704b88c807SRodney W. Grimes 	while (pt < stop)
2714b88c807SRodney W. Grimes 		chksm += (u_long)(*pt++ & 0xff);
2724b88c807SRodney W. Grimes 	return(chksm);
2734b88c807SRodney W. Grimes }
2744b88c807SRodney W. Grimes 
2754b88c807SRodney W. Grimes /*
2764b88c807SRodney W. Grimes  * Routines for old BSD style tar (also made portable to sysV tar)
2774b88c807SRodney W. Grimes  */
2784b88c807SRodney W. Grimes 
2794b88c807SRodney W. Grimes /*
2804b88c807SRodney W. Grimes  * tar_id()
2814b88c807SRodney W. Grimes  *	determine if a block given to us is a valid tar header (and not a USTAR
2824b88c807SRodney W. Grimes  *	header). We have to be on the lookout for those pesky blocks of all
2834b88c807SRodney W. Grimes  *	zero's.
2844b88c807SRodney W. Grimes  * Return:
2854b88c807SRodney W. Grimes  *	0 if a tar header, -1 otherwise
2864b88c807SRodney W. Grimes  */
2874b88c807SRodney W. Grimes 
2884b88c807SRodney W. Grimes int
tar_id(char * blk,int size)289f789b261SWarner Losh tar_id(char *blk, int size)
2904b88c807SRodney W. Grimes {
291f789b261SWarner Losh 	HD_TAR *hd;
292f789b261SWarner Losh 	HD_USTAR *uhd;
2934b88c807SRodney W. Grimes 
2944b88c807SRodney W. Grimes 	if (size < BLKMULT)
2954b88c807SRodney W. Grimes 		return(-1);
2964b88c807SRodney W. Grimes 	hd = (HD_TAR *)blk;
2974b88c807SRodney W. Grimes 	uhd = (HD_USTAR *)blk;
2984b88c807SRodney W. Grimes 
2994b88c807SRodney W. Grimes 	/*
3004b88c807SRodney W. Grimes 	 * check for block of zero's first, a simple and fast test, then make
3014b88c807SRodney W. Grimes 	 * sure this is not a ustar header by looking for the ustar magic
3024b88c807SRodney W. Grimes 	 * cookie. We should use TMAGLEN, but some USTAR archive programs are
3034b88c807SRodney W. Grimes 	 * wrong and create archives missing the \0. Last we check the
3044b88c807SRodney W. Grimes 	 * checksum. If this is ok we have to assume it is a valid header.
3054b88c807SRodney W. Grimes 	 */
3064b88c807SRodney W. Grimes 	if (hd->name[0] == '\0')
3074b88c807SRodney W. Grimes 		return(-1);
3084b88c807SRodney W. Grimes 	if (strncmp(uhd->magic, TMAGIC, TMAGLEN - 1) == 0)
3094b88c807SRodney W. Grimes 		return(-1);
3104b88c807SRodney W. Grimes 	if (asc_ul(hd->chksum,sizeof(hd->chksum),OCT) != tar_chksm(blk,BLKMULT))
3114b88c807SRodney W. Grimes 		return(-1);
3124b88c807SRodney W. Grimes 	return(0);
3134b88c807SRodney W. Grimes }
3144b88c807SRodney W. Grimes 
3154b88c807SRodney W. Grimes /*
3164b88c807SRodney W. Grimes  * tar_opt()
3174b88c807SRodney W. Grimes  *	handle tar format specific -o options
3184b88c807SRodney W. Grimes  * Return:
3194b88c807SRodney W. Grimes  *	0 if ok -1 otherwise
3204b88c807SRodney W. Grimes  */
3214b88c807SRodney W. Grimes 
3224b88c807SRodney W. Grimes int
tar_opt(void)3234b88c807SRodney W. Grimes tar_opt(void)
3244b88c807SRodney W. Grimes {
3254b88c807SRodney W. Grimes 	OPLIST *opt;
3264b88c807SRodney W. Grimes 
3274b88c807SRodney W. Grimes 	while ((opt = opt_next()) != NULL) {
3284b88c807SRodney W. Grimes 		if (strcmp(opt->name, TAR_OPTION) ||
3294b88c807SRodney W. Grimes 		    strcmp(opt->value, TAR_NODIR)) {
330778766feSKris Kennaway 			paxwarn(1, "Unknown tar format -o option/value pair %s=%s",
3314b88c807SRodney W. Grimes 			    opt->name, opt->value);
332778766feSKris Kennaway 			paxwarn(1,"%s=%s is the only supported tar format option",
3334b88c807SRodney W. Grimes 			    TAR_OPTION, TAR_NODIR);
3344b88c807SRodney W. Grimes 			return(-1);
3354b88c807SRodney W. Grimes 		}
3364b88c807SRodney W. Grimes 
3374b88c807SRodney W. Grimes 		/*
3384b88c807SRodney W. Grimes 		 * we only support one option, and only when writing
3394b88c807SRodney W. Grimes 		 */
3404b88c807SRodney W. Grimes 		if ((act != APPND) && (act != ARCHIVE)) {
341778766feSKris Kennaway 			paxwarn(1, "%s=%s is only supported when writing.",
3424b88c807SRodney W. Grimes 			    opt->name, opt->value);
3434b88c807SRodney W. Grimes 			return(-1);
3444b88c807SRodney W. Grimes 		}
3454b88c807SRodney W. Grimes 		tar_nodir = 1;
3464b88c807SRodney W. Grimes 	}
3474b88c807SRodney W. Grimes 	return(0);
3484b88c807SRodney W. Grimes }
3494b88c807SRodney W. Grimes 
3504b88c807SRodney W. Grimes 
3514b88c807SRodney W. Grimes /*
3524b88c807SRodney W. Grimes  * tar_rd()
3534b88c807SRodney W. Grimes  *	extract the values out of block already determined to be a tar header.
3544b88c807SRodney W. Grimes  *	store the values in the ARCHD parameter.
3554b88c807SRodney W. Grimes  * Return:
3564b88c807SRodney W. Grimes  *	0
3574b88c807SRodney W. Grimes  */
3584b88c807SRodney W. Grimes 
3594b88c807SRodney W. Grimes int
tar_rd(ARCHD * arcn,char * buf)360f789b261SWarner Losh tar_rd(ARCHD *arcn, char *buf)
3614b88c807SRodney W. Grimes {
362f789b261SWarner Losh 	HD_TAR *hd;
363f789b261SWarner Losh 	char *pt;
3644b88c807SRodney W. Grimes 
3654b88c807SRodney W. Grimes 	/*
3664b88c807SRodney W. Grimes 	 * we only get proper sized buffers passed to us
3674b88c807SRodney W. Grimes 	 */
3684b88c807SRodney W. Grimes 	if (tar_id(buf, BLKMULT) < 0)
3694b88c807SRodney W. Grimes 		return(-1);
3704b88c807SRodney W. Grimes 	arcn->org_name = arcn->name;
3714b88c807SRodney W. Grimes 	arcn->sb.st_nlink = 1;
3724b88c807SRodney W. Grimes 	arcn->pat = NULL;
3734b88c807SRodney W. Grimes 
3744b88c807SRodney W. Grimes 	/*
3754b88c807SRodney W. Grimes 	 * copy out the name and values in the stat buffer
3764b88c807SRodney W. Grimes 	 */
3774b88c807SRodney W. Grimes 	hd = (HD_TAR *)buf;
3785512dc54SYaroslav Tykhiy 	/*
3795512dc54SYaroslav Tykhiy 	 * old tar format specifies the name always be null-terminated,
3805512dc54SYaroslav Tykhiy 	 * but let's be robust to broken archives.
3815512dc54SYaroslav Tykhiy 	 * the same applies to handling links below.
3825512dc54SYaroslav Tykhiy 	 */
3835512dc54SYaroslav Tykhiy 	arcn->nlen = l_strncpy(arcn->name, hd->name,
3845512dc54SYaroslav Tykhiy 	    MIN(sizeof(hd->name), sizeof(arcn->name)) - 1);
3854b88c807SRodney W. Grimes 	arcn->name[arcn->nlen] = '\0';
3864b88c807SRodney W. Grimes 	arcn->sb.st_mode = (mode_t)(asc_ul(hd->mode,sizeof(hd->mode),OCT) &
3874b88c807SRodney W. Grimes 	    0xfff);
3884b88c807SRodney W. Grimes 	arcn->sb.st_uid = (uid_t)asc_ul(hd->uid, sizeof(hd->uid), OCT);
3894b88c807SRodney W. Grimes 	arcn->sb.st_gid = (gid_t)asc_ul(hd->gid, sizeof(hd->gid), OCT);
390b1787decSKris Kennaway 	arcn->sb.st_size = (off_t)asc_uqd(hd->size, sizeof(hd->size), OCT);
391dbd9746fSMatthew Dillon 	arcn->sb.st_mtime = (time_t)asc_uqd(hd->mtime, sizeof(hd->mtime), OCT);
3924b88c807SRodney W. Grimes 	arcn->sb.st_ctime = arcn->sb.st_atime = arcn->sb.st_mtime;
3934b88c807SRodney W. Grimes 
3944b88c807SRodney W. Grimes 	/*
3954b88c807SRodney W. Grimes 	 * have to look at the last character, it may be a '/' and that is used
3964b88c807SRodney W. Grimes 	 * to encode this as a directory
3974b88c807SRodney W. Grimes 	 */
3984b88c807SRodney W. Grimes 	pt = &(arcn->name[arcn->nlen - 1]);
3994b88c807SRodney W. Grimes 	arcn->pad = 0;
4004b88c807SRodney W. Grimes 	arcn->skip = 0;
4014b88c807SRodney W. Grimes 	switch(hd->linkflag) {
4024b88c807SRodney W. Grimes 	case SYMTYPE:
4034b88c807SRodney W. Grimes 		/*
4044b88c807SRodney W. Grimes 		 * symbolic link, need to get the link name and set the type in
4054b88c807SRodney W. Grimes 		 * the st_mode so -v printing will look correct.
4064b88c807SRodney W. Grimes 		 */
4074b88c807SRodney W. Grimes 		arcn->type = PAX_SLK;
4084b88c807SRodney W. Grimes 		arcn->ln_nlen = l_strncpy(arcn->ln_name, hd->linkname,
4095512dc54SYaroslav Tykhiy 		    MIN(sizeof(hd->linkname), sizeof(arcn->ln_name)) - 1);
4104b88c807SRodney W. Grimes 		arcn->ln_name[arcn->ln_nlen] = '\0';
4114b88c807SRodney W. Grimes 		arcn->sb.st_mode |= S_IFLNK;
4124b88c807SRodney W. Grimes 		break;
4134b88c807SRodney W. Grimes 	case LNKTYPE:
4144b88c807SRodney W. Grimes 		/*
4154b88c807SRodney W. Grimes 		 * hard link, need to get the link name, set the type in the
4164b88c807SRodney W. Grimes 		 * st_mode and st_nlink so -v printing will look better.
4174b88c807SRodney W. Grimes 		 */
4184b88c807SRodney W. Grimes 		arcn->type = PAX_HLK;
4194b88c807SRodney W. Grimes 		arcn->sb.st_nlink = 2;
4204b88c807SRodney W. Grimes 		arcn->ln_nlen = l_strncpy(arcn->ln_name, hd->linkname,
4215512dc54SYaroslav Tykhiy 		    MIN(sizeof(hd->linkname), sizeof(arcn->ln_name)) - 1);
4224b88c807SRodney W. Grimes 		arcn->ln_name[arcn->ln_nlen] = '\0';
4234b88c807SRodney W. Grimes 
4244b88c807SRodney W. Grimes 		/*
4254b88c807SRodney W. Grimes 		 * no idea of what type this thing really points at, but
4264b88c807SRodney W. Grimes 		 * we set something for printing only.
4274b88c807SRodney W. Grimes 		 */
4284b88c807SRodney W. Grimes 		arcn->sb.st_mode |= S_IFREG;
4294b88c807SRodney W. Grimes 		break;
430b1787decSKris Kennaway 	case DIRTYPE:
431b1787decSKris Kennaway 		/*
432b1787decSKris Kennaway 		 * It is a directory, set the mode for -v printing
433b1787decSKris Kennaway 		 */
434b1787decSKris Kennaway 		arcn->type = PAX_DIR;
435b1787decSKris Kennaway 		arcn->sb.st_mode |= S_IFDIR;
436b1787decSKris Kennaway 		arcn->sb.st_nlink = 2;
437b1787decSKris Kennaway 		arcn->ln_name[0] = '\0';
438b1787decSKris Kennaway 		arcn->ln_nlen = 0;
439b1787decSKris Kennaway 		break;
4404b88c807SRodney W. Grimes 	case AREGTYPE:
4414b88c807SRodney W. Grimes 	case REGTYPE:
4424b88c807SRodney W. Grimes 	default:
4434b88c807SRodney W. Grimes 		/*
4444b88c807SRodney W. Grimes 		 * If we have a trailing / this is a directory and NOT a file.
4454b88c807SRodney W. Grimes 		 */
4464b88c807SRodney W. Grimes 		arcn->ln_name[0] = '\0';
4474b88c807SRodney W. Grimes 		arcn->ln_nlen = 0;
4484b88c807SRodney W. Grimes 		if (*pt == '/') {
4494b88c807SRodney W. Grimes 			/*
4504b88c807SRodney W. Grimes 			 * it is a directory, set the mode for -v printing
4514b88c807SRodney W. Grimes 			 */
4524b88c807SRodney W. Grimes 			arcn->type = PAX_DIR;
4534b88c807SRodney W. Grimes 			arcn->sb.st_mode |= S_IFDIR;
4544b88c807SRodney W. Grimes 			arcn->sb.st_nlink = 2;
4554b88c807SRodney W. Grimes 		} else {
4564b88c807SRodney W. Grimes 			/*
4574b88c807SRodney W. Grimes 			 * have a file that will be followed by data. Set the
45846be34b9SKris Kennaway 			 * skip value to the size field and calculate the size
4594b88c807SRodney W. Grimes 			 * of the padding.
4604b88c807SRodney W. Grimes 			 */
4614b88c807SRodney W. Grimes 			arcn->type = PAX_REG;
4624b88c807SRodney W. Grimes 			arcn->sb.st_mode |= S_IFREG;
4634b88c807SRodney W. Grimes 			arcn->pad = TAR_PAD(arcn->sb.st_size);
4644b88c807SRodney W. Grimes 			arcn->skip = arcn->sb.st_size;
4654b88c807SRodney W. Grimes 		}
4664b88c807SRodney W. Grimes 		break;
4674b88c807SRodney W. Grimes 	}
4684b88c807SRodney W. Grimes 
4694b88c807SRodney W. Grimes 	/*
4704b88c807SRodney W. Grimes 	 * strip off any trailing slash.
4714b88c807SRodney W. Grimes 	 */
4724b88c807SRodney W. Grimes 	if (*pt == '/') {
4734b88c807SRodney W. Grimes 		*pt = '\0';
4744b88c807SRodney W. Grimes 		--arcn->nlen;
4754b88c807SRodney W. Grimes 	}
4764b88c807SRodney W. Grimes 	return(0);
4774b88c807SRodney W. Grimes }
4784b88c807SRodney W. Grimes 
4794b88c807SRodney W. Grimes /*
4804b88c807SRodney W. Grimes  * tar_wr()
4814b88c807SRodney W. Grimes  *	write a tar header for the file specified in the ARCHD to the archive.
4824b88c807SRodney W. Grimes  *	Have to check for file types that cannot be stored and file names that
4834b88c807SRodney W. Grimes  *	are too long. Be careful of the term (last arg) to ul_oct, each field
4844b88c807SRodney W. Grimes  *	of tar has it own spec for the termination character(s).
4854b88c807SRodney W. Grimes  *	ASSUMED: space after header in header block is zero filled
4864b88c807SRodney W. Grimes  * Return:
4874b88c807SRodney W. Grimes  *	0 if file has data to be written after the header, 1 if file has NO
4884b88c807SRodney W. Grimes  *	data to write after the header, -1 if archive write failed
4894b88c807SRodney W. Grimes  */
4904b88c807SRodney W. Grimes 
4914b88c807SRodney W. Grimes int
tar_wr(ARCHD * arcn)492f789b261SWarner Losh tar_wr(ARCHD *arcn)
4934b88c807SRodney W. Grimes {
494f789b261SWarner Losh 	HD_TAR *hd;
4954b88c807SRodney W. Grimes 	int len;
496c65bb135SRuslan Ermilov 	HD_TAR hdblk;
4974b88c807SRodney W. Grimes 
4984b88c807SRodney W. Grimes 	/*
4994b88c807SRodney W. Grimes 	 * check for those file system types which tar cannot store
5004b88c807SRodney W. Grimes 	 */
5014b88c807SRodney W. Grimes 	switch(arcn->type) {
5024b88c807SRodney W. Grimes 	case PAX_DIR:
5034b88c807SRodney W. Grimes 		/*
5044b88c807SRodney W. Grimes 		 * user asked that dirs not be written to the archive
5054b88c807SRodney W. Grimes 		 */
5064b88c807SRodney W. Grimes 		if (tar_nodir)
5074b88c807SRodney W. Grimes 			return(1);
5084b88c807SRodney W. Grimes 		break;
5094b88c807SRodney W. Grimes 	case PAX_CHR:
510778766feSKris Kennaway 		paxwarn(1, "Tar cannot archive a character device %s",
5114b88c807SRodney W. Grimes 		    arcn->org_name);
5124b88c807SRodney W. Grimes 		return(1);
5134b88c807SRodney W. Grimes 	case PAX_BLK:
514778766feSKris Kennaway 		paxwarn(1, "Tar cannot archive a block device %s", arcn->org_name);
5154b88c807SRodney W. Grimes 		return(1);
5164b88c807SRodney W. Grimes 	case PAX_SCK:
517778766feSKris Kennaway 		paxwarn(1, "Tar cannot archive a socket %s", arcn->org_name);
5184b88c807SRodney W. Grimes 		return(1);
5194b88c807SRodney W. Grimes 	case PAX_FIF:
520778766feSKris Kennaway 		paxwarn(1, "Tar cannot archive a fifo %s", arcn->org_name);
5214b88c807SRodney W. Grimes 		return(1);
5224b88c807SRodney W. Grimes 	case PAX_SLK:
5234b88c807SRodney W. Grimes 	case PAX_HLK:
5244b88c807SRodney W. Grimes 	case PAX_HRG:
5255512dc54SYaroslav Tykhiy 		if (arcn->ln_nlen >= (int)sizeof(hd->linkname)) {
526778766feSKris Kennaway 			paxwarn(1,"Link name too long for tar %s", arcn->ln_name);
5274b88c807SRodney W. Grimes 			return(1);
5284b88c807SRodney W. Grimes 		}
5294b88c807SRodney W. Grimes 		break;
5304b88c807SRodney W. Grimes 	case PAX_REG:
5314b88c807SRodney W. Grimes 	case PAX_CTG:
5324b88c807SRodney W. Grimes 	default:
5334b88c807SRodney W. Grimes 		break;
5344b88c807SRodney W. Grimes 	}
5354b88c807SRodney W. Grimes 
5364b88c807SRodney W. Grimes 	/*
5374b88c807SRodney W. Grimes 	 * check file name len, remember extra char for dirs (the / at the end)
5384b88c807SRodney W. Grimes 	 */
5394b88c807SRodney W. Grimes 	len = arcn->nlen;
5404b88c807SRodney W. Grimes 	if (arcn->type == PAX_DIR)
5414b88c807SRodney W. Grimes 		++len;
542cee22cbdSDavid E. O'Brien 	if (len >= (int)sizeof(hd->name)) {
543778766feSKris Kennaway 		paxwarn(1, "File name too long for tar %s", arcn->name);
5444b88c807SRodney W. Grimes 		return(1);
5454b88c807SRodney W. Grimes 	}
5464b88c807SRodney W. Grimes 
5474b88c807SRodney W. Grimes 	/*
5480266a5d6SDag-Erling Smørgrav 	 * Copy the data out of the ARCHD into the tar header based on the type
5490266a5d6SDag-Erling Smørgrav 	 * of the file. Remember, many tar readers want all fields to be
5500266a5d6SDag-Erling Smørgrav 	 * padded with zero so we zero the header first.  We then set the
5510266a5d6SDag-Erling Smørgrav 	 * linkflag field (type), the linkname, the size, and set the padding
5520266a5d6SDag-Erling Smørgrav 	 * (if any) to be added after the file data (0 for all other types,
5530266a5d6SDag-Erling Smørgrav 	 * as they only have a header).
5544b88c807SRodney W. Grimes 	 */
555c65bb135SRuslan Ermilov 	hd = &hdblk;
556b1787decSKris Kennaway 	l_strncpy(hd->name, arcn->name, sizeof(hd->name) - 1);
557b1787decSKris Kennaway 	hd->name[sizeof(hd->name) - 1] = '\0';
5584b88c807SRodney W. Grimes 	arcn->pad = 0;
5594b88c807SRodney W. Grimes 
5604b88c807SRodney W. Grimes 	if (arcn->type == PAX_DIR) {
5614b88c807SRodney W. Grimes 		/*
5624b88c807SRodney W. Grimes 		 * directories are the same as files, except have a filename
5634b88c807SRodney W. Grimes 		 * that ends with a /, we add the slash here. No data follows,
5644b88c807SRodney W. Grimes 		 * dirs, so no pad.
5654b88c807SRodney W. Grimes 		 */
5664b88c807SRodney W. Grimes 		hd->linkflag = AREGTYPE;
567778766feSKris Kennaway 		memset(hd->linkname, 0, sizeof(hd->linkname));
5684b88c807SRodney W. Grimes 		hd->name[len-1] = '/';
5694b88c807SRodney W. Grimes 		if (ul_oct((u_long)0L, hd->size, sizeof(hd->size), 1))
5704b88c807SRodney W. Grimes 			goto out;
5714b88c807SRodney W. Grimes 	} else if (arcn->type == PAX_SLK) {
5724b88c807SRodney W. Grimes 		/*
5734b88c807SRodney W. Grimes 		 * no data follows this file, so no pad
5744b88c807SRodney W. Grimes 		 */
5754b88c807SRodney W. Grimes 		hd->linkflag = SYMTYPE;
576b1787decSKris Kennaway 		l_strncpy(hd->linkname,arcn->ln_name, sizeof(hd->linkname) - 1);
577b1787decSKris Kennaway 		hd->linkname[sizeof(hd->linkname) - 1] = '\0';
5784b88c807SRodney W. Grimes 		if (ul_oct((u_long)0L, hd->size, sizeof(hd->size), 1))
5794b88c807SRodney W. Grimes 			goto out;
5804b88c807SRodney W. Grimes 	} else if ((arcn->type == PAX_HLK) || (arcn->type == PAX_HRG)) {
5814b88c807SRodney W. Grimes 		/*
5824b88c807SRodney W. Grimes 		 * no data follows this file, so no pad
5834b88c807SRodney W. Grimes 		 */
5844b88c807SRodney W. Grimes 		hd->linkflag = LNKTYPE;
585b1787decSKris Kennaway 		l_strncpy(hd->linkname,arcn->ln_name, sizeof(hd->linkname) - 1);
586b1787decSKris Kennaway 		hd->linkname[sizeof(hd->linkname) - 1] = '\0';
5874b88c807SRodney W. Grimes 		if (ul_oct((u_long)0L, hd->size, sizeof(hd->size), 1))
5884b88c807SRodney W. Grimes 			goto out;
5894b88c807SRodney W. Grimes 	} else {
5904b88c807SRodney W. Grimes 		/*
5914b88c807SRodney W. Grimes 		 * data follows this file, so set the pad
5924b88c807SRodney W. Grimes 		 */
5934b88c807SRodney W. Grimes 		hd->linkflag = AREGTYPE;
594778766feSKris Kennaway 		memset(hd->linkname, 0, sizeof(hd->linkname));
5954b88c807SRodney W. Grimes 		if (uqd_oct((u_quad_t)arcn->sb.st_size, hd->size,
5964b88c807SRodney W. Grimes 		    sizeof(hd->size), 1)) {
597778766feSKris Kennaway 			paxwarn(1,"File is too large for tar %s", arcn->org_name);
5984b88c807SRodney W. Grimes 			return(1);
5994b88c807SRodney W. Grimes 		}
6004b88c807SRodney W. Grimes 		arcn->pad = TAR_PAD(arcn->sb.st_size);
6014b88c807SRodney W. Grimes 	}
6024b88c807SRodney W. Grimes 
6034b88c807SRodney W. Grimes 	/*
6044b88c807SRodney W. Grimes 	 * copy those fields that are independent of the type
6054b88c807SRodney W. Grimes 	 */
6064b88c807SRodney W. Grimes 	if (ul_oct((u_long)arcn->sb.st_mode, hd->mode, sizeof(hd->mode), 0) ||
6074b88c807SRodney W. Grimes 	    ul_oct((u_long)arcn->sb.st_uid, hd->uid, sizeof(hd->uid), 0) ||
6084b88c807SRodney W. Grimes 	    ul_oct((u_long)arcn->sb.st_gid, hd->gid, sizeof(hd->gid), 0) ||
6094b88c807SRodney W. Grimes 	    ul_oct((u_long)arcn->sb.st_mtime, hd->mtime, sizeof(hd->mtime), 1))
6104b88c807SRodney W. Grimes 		goto out;
6114b88c807SRodney W. Grimes 
6124b88c807SRodney W. Grimes 	/*
6134b88c807SRodney W. Grimes 	 * calculate and add the checksum, then write the header. A return of
6144b88c807SRodney W. Grimes 	 * 0 tells the caller to now write the file data, 1 says no data needs
6154b88c807SRodney W. Grimes 	 * to be written
6164b88c807SRodney W. Grimes 	 */
617c65bb135SRuslan Ermilov 	if (ul_oct(tar_chksm((char *)&hdblk, sizeof(HD_TAR)), hd->chksum,
618b1787decSKris Kennaway 	    sizeof(hd->chksum), 3))
6194b88c807SRodney W. Grimes 		goto out;
620c65bb135SRuslan Ermilov 	if (wr_rdbuf((char *)&hdblk, sizeof(HD_TAR)) < 0)
6214b88c807SRodney W. Grimes 		return(-1);
6224b88c807SRodney W. Grimes 	if (wr_skip((off_t)(BLKMULT - sizeof(HD_TAR))) < 0)
6234b88c807SRodney W. Grimes 		return(-1);
6244b88c807SRodney W. Grimes 	if ((arcn->type == PAX_CTG) || (arcn->type == PAX_REG))
6254b88c807SRodney W. Grimes 		return(0);
6264b88c807SRodney W. Grimes 	return(1);
6274b88c807SRodney W. Grimes 
6284b88c807SRodney W. Grimes     out:
6294b88c807SRodney W. Grimes 	/*
6304b88c807SRodney W. Grimes 	 * header field is out of range
6314b88c807SRodney W. Grimes 	 */
632778766feSKris Kennaway 	paxwarn(1, "Tar header field is too small for %s", arcn->org_name);
6334b88c807SRodney W. Grimes 	return(1);
6344b88c807SRodney W. Grimes }
6354b88c807SRodney W. Grimes 
6364b88c807SRodney W. Grimes /*
6374b88c807SRodney W. Grimes  * Routines for POSIX ustar
6384b88c807SRodney W. Grimes  */
6394b88c807SRodney W. Grimes 
6404b88c807SRodney W. Grimes /*
6414b88c807SRodney W. Grimes  * ustar_strd()
6424b88c807SRodney W. Grimes  *	initialization for ustar read
6434b88c807SRodney W. Grimes  * Return:
6444b88c807SRodney W. Grimes  *	0 if ok, -1 otherwise
6454b88c807SRodney W. Grimes  */
6464b88c807SRodney W. Grimes 
6474b88c807SRodney W. Grimes int
ustar_strd(void)6484b88c807SRodney W. Grimes ustar_strd(void)
6494b88c807SRodney W. Grimes {
6504b88c807SRodney W. Grimes 	if ((usrtb_start() < 0) || (grptb_start() < 0))
6514b88c807SRodney W. Grimes 		return(-1);
6524b88c807SRodney W. Grimes 	return(0);
6534b88c807SRodney W. Grimes }
6544b88c807SRodney W. Grimes 
6554b88c807SRodney W. Grimes /*
6564b88c807SRodney W. Grimes  * ustar_stwr()
6574b88c807SRodney W. Grimes  *	initialization for ustar write
6584b88c807SRodney W. Grimes  * Return:
6594b88c807SRodney W. Grimes  *	0 if ok, -1 otherwise
6604b88c807SRodney W. Grimes  */
6614b88c807SRodney W. Grimes 
6624b88c807SRodney W. Grimes int
ustar_stwr(void)6634b88c807SRodney W. Grimes ustar_stwr(void)
6644b88c807SRodney W. Grimes {
6654b88c807SRodney W. Grimes 	if ((uidtb_start() < 0) || (gidtb_start() < 0))
6664b88c807SRodney W. Grimes 		return(-1);
6674b88c807SRodney W. Grimes 	return(0);
6684b88c807SRodney W. Grimes }
6694b88c807SRodney W. Grimes 
6704b88c807SRodney W. Grimes /*
6714b88c807SRodney W. Grimes  * ustar_id()
6724b88c807SRodney W. Grimes  *	determine if a block given to us is a valid ustar header. We have to
6734b88c807SRodney W. Grimes  *	be on the lookout for those pesky blocks of all zero's
6744b88c807SRodney W. Grimes  * Return:
6754b88c807SRodney W. Grimes  *	0 if a ustar header, -1 otherwise
6764b88c807SRodney W. Grimes  */
6774b88c807SRodney W. Grimes 
6784b88c807SRodney W. Grimes int
ustar_id(char * blk,int size)6794b88c807SRodney W. Grimes ustar_id(char *blk, int size)
6804b88c807SRodney W. Grimes {
681f789b261SWarner Losh 	HD_USTAR *hd;
6824b88c807SRodney W. Grimes 
6834b88c807SRodney W. Grimes 	if (size < BLKMULT)
6844b88c807SRodney W. Grimes 		return(-1);
6854b88c807SRodney W. Grimes 	hd = (HD_USTAR *)blk;
6864b88c807SRodney W. Grimes 
6874b88c807SRodney W. Grimes 	/*
6884b88c807SRodney W. Grimes 	 * check for block of zero's first, a simple and fast test then check
6894b88c807SRodney W. Grimes 	 * ustar magic cookie. We should use TMAGLEN, but some USTAR archive
6904b88c807SRodney W. Grimes 	 * programs are fouled up and create archives missing the \0. Last we
6914b88c807SRodney W. Grimes 	 * check the checksum. If ok we have to assume it is a valid header.
6924b88c807SRodney W. Grimes 	 */
6934b88c807SRodney W. Grimes 	if (hd->name[0] == '\0')
6944b88c807SRodney W. Grimes 		return(-1);
6954b88c807SRodney W. Grimes 	if (strncmp(hd->magic, TMAGIC, TMAGLEN - 1) != 0)
6964b88c807SRodney W. Grimes 		return(-1);
6974b88c807SRodney W. Grimes 	if (asc_ul(hd->chksum,sizeof(hd->chksum),OCT) != tar_chksm(blk,BLKMULT))
6984b88c807SRodney W. Grimes 		return(-1);
6994b88c807SRodney W. Grimes 	return(0);
7004b88c807SRodney W. Grimes }
7014b88c807SRodney W. Grimes 
7024b88c807SRodney W. Grimes /*
7034b88c807SRodney W. Grimes  * ustar_rd()
7044b88c807SRodney W. Grimes  *	extract the values out of block already determined to be a ustar header.
7054b88c807SRodney W. Grimes  *	store the values in the ARCHD parameter.
7064b88c807SRodney W. Grimes  * Return:
7074b88c807SRodney W. Grimes  *	0
7084b88c807SRodney W. Grimes  */
7094b88c807SRodney W. Grimes 
7104b88c807SRodney W. Grimes int
ustar_rd(ARCHD * arcn,char * buf)711f789b261SWarner Losh ustar_rd(ARCHD *arcn, char *buf)
7124b88c807SRodney W. Grimes {
713f789b261SWarner Losh 	HD_USTAR *hd;
714f789b261SWarner Losh 	char *dest;
715f789b261SWarner Losh 	int cnt = 0;
7164b88c807SRodney W. Grimes 	dev_t devmajor;
7174b88c807SRodney W. Grimes 	dev_t devminor;
7184b88c807SRodney W. Grimes 
7194b88c807SRodney W. Grimes 	/*
7204b88c807SRodney W. Grimes 	 * we only get proper sized buffers
7214b88c807SRodney W. Grimes 	 */
7224b88c807SRodney W. Grimes 	if (ustar_id(buf, BLKMULT) < 0)
7234b88c807SRodney W. Grimes 		return(-1);
7244b88c807SRodney W. Grimes 	arcn->org_name = arcn->name;
7254b88c807SRodney W. Grimes 	arcn->sb.st_nlink = 1;
7264b88c807SRodney W. Grimes 	arcn->pat = NULL;
727b1787decSKris Kennaway 	arcn->nlen = 0;
7284b88c807SRodney W. Grimes 	hd = (HD_USTAR *)buf;
7294b88c807SRodney W. Grimes 
7304b88c807SRodney W. Grimes 	/*
7314b88c807SRodney W. Grimes 	 * see if the filename is split into two parts. if, so joint the parts.
7324b88c807SRodney W. Grimes 	 * we copy the prefix first and add a / between the prefix and name.
7334b88c807SRodney W. Grimes 	 */
7344b88c807SRodney W. Grimes 	dest = arcn->name;
7354b88c807SRodney W. Grimes 	if (*(hd->prefix) != '\0') {
7365512dc54SYaroslav Tykhiy 		cnt = l_strncpy(dest, hd->prefix,
7375512dc54SYaroslav Tykhiy 		    MIN(sizeof(hd->prefix), sizeof(arcn->name) - 2));
738b1787decSKris Kennaway 		dest += cnt;
7394b88c807SRodney W. Grimes 		*dest++ = '/';
740b1787decSKris Kennaway 		cnt++;
7414b88c807SRodney W. Grimes 	}
7425512dc54SYaroslav Tykhiy 	/*
7435512dc54SYaroslav Tykhiy 	 * ustar format specifies the name may be unterminated
7445512dc54SYaroslav Tykhiy 	 * if it fills the entire field.  this also applies to
7455512dc54SYaroslav Tykhiy 	 * the prefix and the linkname.
7465512dc54SYaroslav Tykhiy 	 */
7475512dc54SYaroslav Tykhiy 	arcn->nlen = cnt + l_strncpy(dest, hd->name,
7485512dc54SYaroslav Tykhiy 	    MIN(sizeof(hd->name), sizeof(arcn->name) - cnt - 1));
7494b88c807SRodney W. Grimes 	arcn->name[arcn->nlen] = '\0';
7504b88c807SRodney W. Grimes 
7514b88c807SRodney W. Grimes 	/*
7524b88c807SRodney W. Grimes 	 * follow the spec to the letter. we should only have mode bits, strip
7534b88c807SRodney W. Grimes 	 * off all other crud we may be passed.
7544b88c807SRodney W. Grimes 	 */
7554b88c807SRodney W. Grimes 	arcn->sb.st_mode = (mode_t)(asc_ul(hd->mode, sizeof(hd->mode), OCT) &
7564b88c807SRodney W. Grimes 	    0xfff);
757b1787decSKris Kennaway 	arcn->sb.st_size = (off_t)asc_uqd(hd->size, sizeof(hd->size), OCT);
758dbd9746fSMatthew Dillon 	arcn->sb.st_mtime = (time_t)asc_uqd(hd->mtime, sizeof(hd->mtime), OCT);
7594b88c807SRodney W. Grimes 	arcn->sb.st_ctime = arcn->sb.st_atime = arcn->sb.st_mtime;
7604b88c807SRodney W. Grimes 
7614b88c807SRodney W. Grimes 	/*
7624b88c807SRodney W. Grimes 	 * If we can find the ascii names for gname and uname in the password
7634b88c807SRodney W. Grimes 	 * and group files we will use the uid's and gid they bind. Otherwise
7644b88c807SRodney W. Grimes 	 * we use the uid and gid values stored in the header. (This is what
76546be34b9SKris Kennaway 	 * the POSIX spec wants).
7664b88c807SRodney W. Grimes 	 */
7674b88c807SRodney W. Grimes 	hd->gname[sizeof(hd->gname) - 1] = '\0';
7684b88c807SRodney W. Grimes 	if (gid_name(hd->gname, &(arcn->sb.st_gid)) < 0)
7694b88c807SRodney W. Grimes 		arcn->sb.st_gid = (gid_t)asc_ul(hd->gid, sizeof(hd->gid), OCT);
7704b88c807SRodney W. Grimes 	hd->uname[sizeof(hd->uname) - 1] = '\0';
7714b88c807SRodney W. Grimes 	if (uid_name(hd->uname, &(arcn->sb.st_uid)) < 0)
7724b88c807SRodney W. Grimes 		arcn->sb.st_uid = (uid_t)asc_ul(hd->uid, sizeof(hd->uid), OCT);
7734b88c807SRodney W. Grimes 
7744b88c807SRodney W. Grimes 	/*
7754b88c807SRodney W. Grimes 	 * set the defaults, these may be changed depending on the file type
7764b88c807SRodney W. Grimes 	 */
7774b88c807SRodney W. Grimes 	arcn->ln_name[0] = '\0';
7784b88c807SRodney W. Grimes 	arcn->ln_nlen = 0;
7794b88c807SRodney W. Grimes 	arcn->pad = 0;
7804b88c807SRodney W. Grimes 	arcn->skip = 0;
7814b88c807SRodney W. Grimes 	arcn->sb.st_rdev = (dev_t)0;
7824b88c807SRodney W. Grimes 
7834b88c807SRodney W. Grimes 	/*
7844b88c807SRodney W. Grimes 	 * set the mode and PAX type according to the typeflag in the header
7854b88c807SRodney W. Grimes 	 */
7864b88c807SRodney W. Grimes 	switch(hd->typeflag) {
7874b88c807SRodney W. Grimes 	case FIFOTYPE:
7884b88c807SRodney W. Grimes 		arcn->type = PAX_FIF;
7894b88c807SRodney W. Grimes 		arcn->sb.st_mode |= S_IFIFO;
7904b88c807SRodney W. Grimes 		break;
7914b88c807SRodney W. Grimes 	case DIRTYPE:
7924b88c807SRodney W. Grimes 		arcn->type = PAX_DIR;
7934b88c807SRodney W. Grimes 		arcn->sb.st_mode |= S_IFDIR;
7944b88c807SRodney W. Grimes 		arcn->sb.st_nlink = 2;
7954b88c807SRodney W. Grimes 
7964b88c807SRodney W. Grimes 		/*
7974b88c807SRodney W. Grimes 		 * Some programs that create ustar archives append a '/'
7984b88c807SRodney W. Grimes 		 * to the pathname for directories. This clearly violates
7994b88c807SRodney W. Grimes 		 * ustar specs, but we will silently strip it off anyway.
8004b88c807SRodney W. Grimes 		 */
8014b88c807SRodney W. Grimes 		if (arcn->name[arcn->nlen - 1] == '/')
8024b88c807SRodney W. Grimes 			arcn->name[--arcn->nlen] = '\0';
8034b88c807SRodney W. Grimes 		break;
8044b88c807SRodney W. Grimes 	case BLKTYPE:
8054b88c807SRodney W. Grimes 	case CHRTYPE:
8064b88c807SRodney W. Grimes 		/*
8074b88c807SRodney W. Grimes 		 * this type requires the rdev field to be set.
8084b88c807SRodney W. Grimes 		 */
8094b88c807SRodney W. Grimes 		if (hd->typeflag == BLKTYPE) {
8104b88c807SRodney W. Grimes 			arcn->type = PAX_BLK;
8114b88c807SRodney W. Grimes 			arcn->sb.st_mode |= S_IFBLK;
8124b88c807SRodney W. Grimes 		} else {
8134b88c807SRodney W. Grimes 			arcn->type = PAX_CHR;
8144b88c807SRodney W. Grimes 			arcn->sb.st_mode |= S_IFCHR;
8154b88c807SRodney W. Grimes 		}
8164b88c807SRodney W. Grimes 		devmajor = (dev_t)asc_ul(hd->devmajor,sizeof(hd->devmajor),OCT);
8174b88c807SRodney W. Grimes 		devminor = (dev_t)asc_ul(hd->devminor,sizeof(hd->devminor),OCT);
8184b88c807SRodney W. Grimes 		arcn->sb.st_rdev = TODEV(devmajor, devminor);
8194b88c807SRodney W. Grimes 		break;
8204b88c807SRodney W. Grimes 	case SYMTYPE:
8214b88c807SRodney W. Grimes 	case LNKTYPE:
8224b88c807SRodney W. Grimes 		if (hd->typeflag == SYMTYPE) {
8234b88c807SRodney W. Grimes 			arcn->type = PAX_SLK;
8244b88c807SRodney W. Grimes 			arcn->sb.st_mode |= S_IFLNK;
8254b88c807SRodney W. Grimes 		} else {
8264b88c807SRodney W. Grimes 			arcn->type = PAX_HLK;
8274b88c807SRodney W. Grimes 			/*
8284b88c807SRodney W. Grimes 			 * so printing looks better
8294b88c807SRodney W. Grimes 			 */
8304b88c807SRodney W. Grimes 			arcn->sb.st_mode |= S_IFREG;
8314b88c807SRodney W. Grimes 			arcn->sb.st_nlink = 2;
8324b88c807SRodney W. Grimes 		}
8334b88c807SRodney W. Grimes 		/*
8344b88c807SRodney W. Grimes 		 * copy the link name
8354b88c807SRodney W. Grimes 		 */
8364b88c807SRodney W. Grimes 		arcn->ln_nlen = l_strncpy(arcn->ln_name, hd->linkname,
8375512dc54SYaroslav Tykhiy 		    MIN(sizeof(hd->linkname), sizeof(arcn->ln_name) - 1));
8384b88c807SRodney W. Grimes 		arcn->ln_name[arcn->ln_nlen] = '\0';
8394b88c807SRodney W. Grimes 		break;
8404b88c807SRodney W. Grimes 	case CONTTYPE:
8414b88c807SRodney W. Grimes 	case AREGTYPE:
8424b88c807SRodney W. Grimes 	case REGTYPE:
8434b88c807SRodney W. Grimes 	default:
8444b88c807SRodney W. Grimes 		/*
8454b88c807SRodney W. Grimes 		 * these types have file data that follows. Set the skip and
8464b88c807SRodney W. Grimes 		 * pad fields.
8474b88c807SRodney W. Grimes 		 */
8484b88c807SRodney W. Grimes 		arcn->type = PAX_REG;
8494b88c807SRodney W. Grimes 		arcn->pad = TAR_PAD(arcn->sb.st_size);
8504b88c807SRodney W. Grimes 		arcn->skip = arcn->sb.st_size;
8514b88c807SRodney W. Grimes 		arcn->sb.st_mode |= S_IFREG;
8524b88c807SRodney W. Grimes 		break;
8534b88c807SRodney W. Grimes 	}
8544b88c807SRodney W. Grimes 	return(0);
8554b88c807SRodney W. Grimes }
8564b88c807SRodney W. Grimes 
8574b88c807SRodney W. Grimes /*
8584b88c807SRodney W. Grimes  * ustar_wr()
8594b88c807SRodney W. Grimes  *	write a ustar header for the file specified in the ARCHD to the archive
8604b88c807SRodney W. Grimes  *	Have to check for file types that cannot be stored and file names that
8614b88c807SRodney W. Grimes  *	are too long. Be careful of the term (last arg) to ul_oct, we only use
8624b88c807SRodney W. Grimes  *	'\0' for the termination character (this is different than picky tar)
8634b88c807SRodney W. Grimes  *	ASSUMED: space after header in header block is zero filled
8644b88c807SRodney W. Grimes  * Return:
8654b88c807SRodney W. Grimes  *	0 if file has data to be written after the header, 1 if file has NO
8664b88c807SRodney W. Grimes  *	data to write after the header, -1 if archive write failed
8674b88c807SRodney W. Grimes  */
8684b88c807SRodney W. Grimes 
8694b88c807SRodney W. Grimes int
ustar_wr(ARCHD * arcn)870f789b261SWarner Losh ustar_wr(ARCHD *arcn)
8714b88c807SRodney W. Grimes {
872f789b261SWarner Losh 	HD_USTAR *hd;
873f789b261SWarner Losh 	char *pt;
874c65bb135SRuslan Ermilov 	HD_USTAR hdblk;
8754b88c807SRodney W. Grimes 
8764b88c807SRodney W. Grimes 	/*
8774b88c807SRodney W. Grimes 	 * check for those file system types ustar cannot store
8784b88c807SRodney W. Grimes 	 */
8794b88c807SRodney W. Grimes 	if (arcn->type == PAX_SCK) {
880778766feSKris Kennaway 		paxwarn(1, "Ustar cannot archive a socket %s", arcn->org_name);
8814b88c807SRodney W. Grimes 		return(1);
8824b88c807SRodney W. Grimes 	}
8834b88c807SRodney W. Grimes 
8844b88c807SRodney W. Grimes 	/*
8854b88c807SRodney W. Grimes 	 * check the length of the linkname
8864b88c807SRodney W. Grimes 	 */
8874b88c807SRodney W. Grimes 	if (((arcn->type == PAX_SLK) || (arcn->type == PAX_HLK) ||
888cee22cbdSDavid E. O'Brien 	    (arcn->type == PAX_HRG)) &&
8895512dc54SYaroslav Tykhiy 	    (arcn->ln_nlen > (int)sizeof(hd->linkname))) {
890778766feSKris Kennaway 		paxwarn(1, "Link name too long for ustar %s", arcn->ln_name);
8914b88c807SRodney W. Grimes 		return(1);
8924b88c807SRodney W. Grimes 	}
8934b88c807SRodney W. Grimes 
8944b88c807SRodney W. Grimes 	/*
8954b88c807SRodney W. Grimes 	 * split the path name into prefix and name fields (if needed). if
8964b88c807SRodney W. Grimes 	 * pt != arcn->name, the name has to be split
8974b88c807SRodney W. Grimes 	 */
8984b88c807SRodney W. Grimes 	if ((pt = name_split(arcn->name, arcn->nlen)) == NULL) {
899778766feSKris Kennaway 		paxwarn(1, "File name too long for ustar %s", arcn->name);
9004b88c807SRodney W. Grimes 		return(1);
9014b88c807SRodney W. Grimes 	}
902c65bb135SRuslan Ermilov 	hd = &hdblk;
9034b88c807SRodney W. Grimes 	arcn->pad = 0L;
9044b88c807SRodney W. Grimes 
9054b88c807SRodney W. Grimes 	/*
9064b88c807SRodney W. Grimes 	 * split the name, or zero out the prefix
9074b88c807SRodney W. Grimes 	 */
9084b88c807SRodney W. Grimes 	if (pt != arcn->name) {
9094b88c807SRodney W. Grimes 		/*
9104b88c807SRodney W. Grimes 		 * name was split, pt points at the / where the split is to
9114b88c807SRodney W. Grimes 		 * occur, we remove the / and copy the first part to the prefix
9124b88c807SRodney W. Grimes 		 */
9134b88c807SRodney W. Grimes 		*pt = '\0';
9145512dc54SYaroslav Tykhiy 		l_strncpy(hd->prefix, arcn->name, sizeof(hd->prefix));
9154b88c807SRodney W. Grimes 		*pt++ = '/';
9164b88c807SRodney W. Grimes 	} else
917778766feSKris Kennaway 		memset(hd->prefix, 0, sizeof(hd->prefix));
9184b88c807SRodney W. Grimes 
9194b88c807SRodney W. Grimes 	/*
9204b88c807SRodney W. Grimes 	 * copy the name part. this may be the whole path or the part after
9215512dc54SYaroslav Tykhiy 	 * the prefix.  both the name and prefix may fill the entire field.
9224b88c807SRodney W. Grimes 	 */
9235512dc54SYaroslav Tykhiy 	l_strncpy(hd->name, pt, sizeof(hd->name));
9244b88c807SRodney W. Grimes 
9254b88c807SRodney W. Grimes 	/*
9264b88c807SRodney W. Grimes 	 * set the fields in the header that are type dependent
9274b88c807SRodney W. Grimes 	 */
9284b88c807SRodney W. Grimes 	switch(arcn->type) {
9294b88c807SRodney W. Grimes 	case PAX_DIR:
9304b88c807SRodney W. Grimes 		hd->typeflag = DIRTYPE;
931778766feSKris Kennaway 		memset(hd->linkname, 0, sizeof(hd->linkname));
932778766feSKris Kennaway 		memset(hd->devmajor, 0, sizeof(hd->devmajor));
933778766feSKris Kennaway 		memset(hd->devminor, 0, sizeof(hd->devminor));
9344b88c807SRodney W. Grimes 		if (ul_oct((u_long)0L, hd->size, sizeof(hd->size), 3))
9354b88c807SRodney W. Grimes 			goto out;
9364b88c807SRodney W. Grimes 		break;
9374b88c807SRodney W. Grimes 	case PAX_CHR:
9384b88c807SRodney W. Grimes 	case PAX_BLK:
9394b88c807SRodney W. Grimes 		if (arcn->type == PAX_CHR)
9404b88c807SRodney W. Grimes 			hd->typeflag = CHRTYPE;
9414b88c807SRodney W. Grimes 		else
9424b88c807SRodney W. Grimes 			hd->typeflag = BLKTYPE;
943778766feSKris Kennaway 		memset(hd->linkname, 0, sizeof(hd->linkname));
9444b88c807SRodney W. Grimes 		if (ul_oct((u_long)MAJOR(arcn->sb.st_rdev), hd->devmajor,
9454b88c807SRodney W. Grimes 		   sizeof(hd->devmajor), 3) ||
9464b88c807SRodney W. Grimes 		   ul_oct((u_long)MINOR(arcn->sb.st_rdev), hd->devminor,
9474b88c807SRodney W. Grimes 		   sizeof(hd->devminor), 3) ||
9484b88c807SRodney W. Grimes 		   ul_oct((u_long)0L, hd->size, sizeof(hd->size), 3))
9494b88c807SRodney W. Grimes 			goto out;
9504b88c807SRodney W. Grimes 		break;
9514b88c807SRodney W. Grimes 	case PAX_FIF:
9524b88c807SRodney W. Grimes 		hd->typeflag = FIFOTYPE;
953778766feSKris Kennaway 		memset(hd->linkname, 0, sizeof(hd->linkname));
954778766feSKris Kennaway 		memset(hd->devmajor, 0, sizeof(hd->devmajor));
955778766feSKris Kennaway 		memset(hd->devminor, 0, sizeof(hd->devminor));
9564b88c807SRodney W. Grimes 		if (ul_oct((u_long)0L, hd->size, sizeof(hd->size), 3))
9574b88c807SRodney W. Grimes 			goto out;
9584b88c807SRodney W. Grimes 		break;
9594b88c807SRodney W. Grimes 	case PAX_SLK:
9604b88c807SRodney W. Grimes 	case PAX_HLK:
9614b88c807SRodney W. Grimes 	case PAX_HRG:
9624b88c807SRodney W. Grimes 		if (arcn->type == PAX_SLK)
9634b88c807SRodney W. Grimes 			hd->typeflag = SYMTYPE;
9644b88c807SRodney W. Grimes 		else
9654b88c807SRodney W. Grimes 			hd->typeflag = LNKTYPE;
9665512dc54SYaroslav Tykhiy 		/* the link name may occupy the entire field in ustar */
9675512dc54SYaroslav Tykhiy 		l_strncpy(hd->linkname,arcn->ln_name, sizeof(hd->linkname));
968778766feSKris Kennaway 		memset(hd->devmajor, 0, sizeof(hd->devmajor));
969778766feSKris Kennaway 		memset(hd->devminor, 0, sizeof(hd->devminor));
9704b88c807SRodney W. Grimes 		if (ul_oct((u_long)0L, hd->size, sizeof(hd->size), 3))
9714b88c807SRodney W. Grimes 			goto out;
9724b88c807SRodney W. Grimes 		break;
9734b88c807SRodney W. Grimes 	case PAX_REG:
9744b88c807SRodney W. Grimes 	case PAX_CTG:
9754b88c807SRodney W. Grimes 	default:
9764b88c807SRodney W. Grimes 		/*
9774b88c807SRodney W. Grimes 		 * file data with this type, set the padding
9784b88c807SRodney W. Grimes 		 */
9794b88c807SRodney W. Grimes 		if (arcn->type == PAX_CTG)
9804b88c807SRodney W. Grimes 			hd->typeflag = CONTTYPE;
9814b88c807SRodney W. Grimes 		else
9824b88c807SRodney W. Grimes 			hd->typeflag = REGTYPE;
983778766feSKris Kennaway 		memset(hd->linkname, 0, sizeof(hd->linkname));
984778766feSKris Kennaway 		memset(hd->devmajor, 0, sizeof(hd->devmajor));
985778766feSKris Kennaway 		memset(hd->devminor, 0, sizeof(hd->devminor));
9864b88c807SRodney W. Grimes 		arcn->pad = TAR_PAD(arcn->sb.st_size);
9874b88c807SRodney W. Grimes 		if (uqd_oct((u_quad_t)arcn->sb.st_size, hd->size,
9884b88c807SRodney W. Grimes 		    sizeof(hd->size), 3)) {
989778766feSKris Kennaway 			paxwarn(1,"File is too long for ustar %s",arcn->org_name);
9904b88c807SRodney W. Grimes 			return(1);
9914b88c807SRodney W. Grimes 		}
9924b88c807SRodney W. Grimes 		break;
9934b88c807SRodney W. Grimes 	}
9944b88c807SRodney W. Grimes 
995b1787decSKris Kennaway 	l_strncpy(hd->magic, TMAGIC, TMAGLEN);
996b1787decSKris Kennaway 	l_strncpy(hd->version, TVERSION, TVERSLEN);
9974b88c807SRodney W. Grimes 
9984b88c807SRodney W. Grimes 	/*
9994b88c807SRodney W. Grimes 	 * set the remaining fields. Some versions want all 16 bits of mode
10004b88c807SRodney W. Grimes 	 * we better humor them (they really do not meet spec though)....
10014b88c807SRodney W. Grimes 	 */
10024b88c807SRodney W. Grimes 	if (ul_oct((u_long)arcn->sb.st_mode, hd->mode, sizeof(hd->mode), 3) ||
10034b88c807SRodney W. Grimes 	    ul_oct((u_long)arcn->sb.st_uid, hd->uid, sizeof(hd->uid), 3)  ||
10044b88c807SRodney W. Grimes 	    ul_oct((u_long)arcn->sb.st_gid, hd->gid, sizeof(hd->gid), 3) ||
10054b88c807SRodney W. Grimes 	    ul_oct((u_long)arcn->sb.st_mtime,hd->mtime,sizeof(hd->mtime),3))
10064b88c807SRodney W. Grimes 		goto out;
1007b1787decSKris Kennaway 	l_strncpy(hd->uname,name_uid(arcn->sb.st_uid, 0),sizeof(hd->uname));
1008b1787decSKris Kennaway 	l_strncpy(hd->gname,name_gid(arcn->sb.st_gid, 0),sizeof(hd->gname));
10094b88c807SRodney W. Grimes 
10104b88c807SRodney W. Grimes 	/*
10114b88c807SRodney W. Grimes 	 * calculate and store the checksum write the header to the archive
10124b88c807SRodney W. Grimes 	 * return 0 tells the caller to now write the file data, 1 says no data
10134b88c807SRodney W. Grimes 	 * needs to be written
10144b88c807SRodney W. Grimes 	 */
1015c65bb135SRuslan Ermilov 	if (ul_oct(tar_chksm((char *)&hdblk, sizeof(HD_USTAR)), hd->chksum,
10164b88c807SRodney W. Grimes 	   sizeof(hd->chksum), 3))
10174b88c807SRodney W. Grimes 		goto out;
1018c65bb135SRuslan Ermilov 	if (wr_rdbuf((char *)&hdblk, sizeof(HD_USTAR)) < 0)
10194b88c807SRodney W. Grimes 		return(-1);
10204b88c807SRodney W. Grimes 	if (wr_skip((off_t)(BLKMULT - sizeof(HD_USTAR))) < 0)
10214b88c807SRodney W. Grimes 		return(-1);
10224b88c807SRodney W. Grimes 	if ((arcn->type == PAX_CTG) || (arcn->type == PAX_REG))
10234b88c807SRodney W. Grimes 		return(0);
10244b88c807SRodney W. Grimes 	return(1);
10254b88c807SRodney W. Grimes 
10264b88c807SRodney W. Grimes     out:
10274b88c807SRodney W. Grimes     	/*
10284b88c807SRodney W. Grimes 	 * header field is out of range
10294b88c807SRodney W. Grimes 	 */
1030778766feSKris Kennaway 	paxwarn(1, "Ustar header field is too small for %s", arcn->org_name);
10314b88c807SRodney W. Grimes 	return(1);
10324b88c807SRodney W. Grimes }
10334b88c807SRodney W. Grimes 
10344b88c807SRodney W. Grimes /*
10354b88c807SRodney W. Grimes  * name_split()
10364b88c807SRodney W. Grimes  *	see if the name has to be split for storage in a ustar header. We try
10374b88c807SRodney W. Grimes  *	to fit the entire name in the name field without splitting if we can.
10384b88c807SRodney W. Grimes  *	The split point is always at a /
10394b88c807SRodney W. Grimes  * Return
10404b88c807SRodney W. Grimes  *	character pointer to split point (always the / that is to be removed
10414b88c807SRodney W. Grimes  *	if the split is not needed, the points is set to the start of the file
10424b88c807SRodney W. Grimes  *	name (it would violate the spec to split there). A NULL is returned if
10434b88c807SRodney W. Grimes  *	the file name is too long
10444b88c807SRodney W. Grimes  */
10454b88c807SRodney W. Grimes 
10464b88c807SRodney W. Grimes static char *
name_split(char * name,int len)1047f789b261SWarner Losh name_split(char *name, int len)
10484b88c807SRodney W. Grimes {
1049f789b261SWarner Losh 	char *start;
10504b88c807SRodney W. Grimes 
10514b88c807SRodney W. Grimes 	/*
10524b88c807SRodney W. Grimes 	 * check to see if the file name is small enough to fit in the name
10534b88c807SRodney W. Grimes 	 * field. if so just return a pointer to the name.
10544b88c807SRodney W. Grimes 	 */
10555512dc54SYaroslav Tykhiy 	if (len <= TNMSZ)
10564b88c807SRodney W. Grimes 		return(name);
1057cd14b457SBrian Somers 	if (len > TPFSZ + TNMSZ)
10584b88c807SRodney W. Grimes 		return(NULL);
10594b88c807SRodney W. Grimes 
10604b88c807SRodney W. Grimes 	/*
10614b88c807SRodney W. Grimes 	 * we start looking at the biggest sized piece that fits in the name
106246be34b9SKris Kennaway 	 * field. We walk forward looking for a slash to split at. The idea is
10634b88c807SRodney W. Grimes 	 * to find the biggest piece to fit in the name field (or the smallest
10644757e526SMike Smith 	 * prefix we can find)
10654b88c807SRodney W. Grimes 	 */
1066f77b6c6aSBrian Somers 	start = name + len - TNMSZ;
10674b88c807SRodney W. Grimes 	while ((*start != '\0') && (*start != '/'))
10684b88c807SRodney W. Grimes 		++start;
10694b88c807SRodney W. Grimes 
10704b88c807SRodney W. Grimes 	/*
10714b88c807SRodney W. Grimes 	 * if we hit the end of the string, this name cannot be split, so we
10724b88c807SRodney W. Grimes 	 * cannot store this file.
10734b88c807SRodney W. Grimes 	 */
10744b88c807SRodney W. Grimes 	if (*start == '\0')
10754b88c807SRodney W. Grimes 		return(NULL);
10764b88c807SRodney W. Grimes 	len = start - name;
10774b88c807SRodney W. Grimes 
10784b88c807SRodney W. Grimes 	/*
10794b88c807SRodney W. Grimes 	 * NOTE: /str where the length of str == TNMSZ can not be stored under
10804b88c807SRodney W. Grimes 	 * the p1003.1-1990 spec for ustar. We could force a prefix of / and
10814b88c807SRodney W. Grimes 	 * the file would then expand on extract to //str. The len == 0 below
10824b88c807SRodney W. Grimes 	 * makes this special case follow the spec to the letter.
10834b88c807SRodney W. Grimes 	 */
10845512dc54SYaroslav Tykhiy 	if ((len > TPFSZ) || (len == 0))
10854b88c807SRodney W. Grimes 		return(NULL);
10864b88c807SRodney W. Grimes 
10874b88c807SRodney W. Grimes 	/*
10884b88c807SRodney W. Grimes 	 * ok have a split point, return it to the caller
10894b88c807SRodney W. Grimes 	 */
10904b88c807SRodney W. Grimes 	return(start);
10914b88c807SRodney W. Grimes }
1092