xref: /freebsd/bin/pax/pax.h (revision 90aea514c6249118e880d75972d063362f4bf492)
14b88c807SRodney W. Grimes /*-
2*8a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
3*8a16b7a1SPedro 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 /*
374b88c807SRodney W. Grimes  * BSD PAX global data structures and constants.
384b88c807SRodney W. Grimes  */
394b88c807SRodney W. Grimes 
40b1787decSKris Kennaway #define	MAXBLK		64512	/* MAX blocksize supported (posix SPEC) */
414b88c807SRodney W. Grimes 				/* WARNING: increasing MAXBLK past 32256 */
424b88c807SRodney W. Grimes 				/* will violate posix spec. */
43b1787decSKris Kennaway #define	MAXBLK_POSIX	32256	/* MAX blocksize supported as per POSIX */
444b88c807SRodney W. Grimes #define BLKMULT		512	/* blocksize must be even mult of 512 bytes */
454b88c807SRodney W. Grimes 				/* Don't even think of changing this */
464b88c807SRodney W. Grimes #define DEVBLK		8192	/* default read blksize for devices */
474b88c807SRodney W. Grimes #define FILEBLK		10240	/* default read blksize for files */
4846be34b9SKris Kennaway #define PAXPATHLEN	3072	/* maximum path length for pax. MUST be */
49c619c9a3SWarner Losh 				/* longer than the system PATH_MAX */
504b88c807SRodney W. Grimes 
514b88c807SRodney W. Grimes /*
524b88c807SRodney W. Grimes  * Pax modes of operation
534b88c807SRodney W. Grimes  */
544b88c807SRodney W. Grimes #define	LIST		0	/* List the file in an archive */
554b88c807SRodney W. Grimes #define	EXTRACT		1	/* extract the files in an archive */
564b88c807SRodney W. Grimes #define ARCHIVE		2	/* write a new archive */
574b88c807SRodney W. Grimes #define APPND		3	/* append to the end of an archive */
584b88c807SRodney W. Grimes #define	COPY		4	/* copy files to destination dir */
594b88c807SRodney W. Grimes #define DEFOP		LIST	/* if no flags default is to LIST */
604b88c807SRodney W. Grimes 
614b88c807SRodney W. Grimes /*
624b88c807SRodney W. Grimes  * Device type of the current archive volume
634b88c807SRodney W. Grimes  */
644b88c807SRodney W. Grimes #define ISREG		0	/* regular file */
654b88c807SRodney W. Grimes #define ISCHR		1	/* character device */
664b88c807SRodney W. Grimes #define ISBLK		2	/* block device */
674b88c807SRodney W. Grimes #define ISTAPE		3	/* tape drive */
684b88c807SRodney W. Grimes #define ISPIPE		4	/* pipe/socket */
694b88c807SRodney W. Grimes 
7040feca3aSMark Murray typedef struct archd ARCHD;
7140feca3aSMark Murray typedef struct fsub FSUB;
7240feca3aSMark Murray typedef struct oplist OPLIST;
7340feca3aSMark Murray typedef struct pattern PATTERN;
7440feca3aSMark Murray 
754b88c807SRodney W. Grimes /*
764b88c807SRodney W. Grimes  * Format Specific Routine Table
774b88c807SRodney W. Grimes  *
784b88c807SRodney W. Grimes  * The format specific routine table allows new archive formats to be quickly
794b88c807SRodney W. Grimes  * added. Overall pax operation is independent of the actual format used to
804b88c807SRodney W. Grimes  * form the archive. Only those routines which deal directly with the archive
8146be34b9SKris Kennaway  * are tailored to the oddities of the specific format. All other routines are
824b88c807SRodney W. Grimes  * independent of the archive format. Data flow in and out of the format
834b88c807SRodney W. Grimes  * dependent routines pass pointers to ARCHD structure (described below).
844b88c807SRodney W. Grimes  */
8540feca3aSMark Murray struct fsub {
8640feca3aSMark Murray 	const char *name;	/* name of format, this is the name the user */
874b88c807SRodney W. Grimes 				/* gives to -x option to select it. */
884b88c807SRodney W. Grimes 	int bsz;		/* default block size. used when the user */
894b88c807SRodney W. Grimes 				/* does not specify a blocksize for writing */
904b88c807SRodney W. Grimes 				/* Appends continue to with the blocksize */
914b88c807SRodney W. Grimes 				/* the archive is currently using. */
924b88c807SRodney W. Grimes 	int hsz;		/* Header size in bytes. this is the size of */
934b88c807SRodney W. Grimes 				/* the smallest header this format supports. */
944b88c807SRodney W. Grimes 				/* Headers are assumed to fit in a BLKMULT. */
954b88c807SRodney W. Grimes 				/* If they are bigger, get_head() and */
964b88c807SRodney W. Grimes 				/* get_arc() must be adjusted */
974b88c807SRodney W. Grimes 	int udev;		/* does append require unique dev/ino? some */
984b88c807SRodney W. Grimes 				/* formats use the device and inode fields */
994b88c807SRodney W. Grimes 				/* to specify hard links. when members in */
1004b88c807SRodney W. Grimes 				/* the archive have the same inode/dev they */
1014b88c807SRodney W. Grimes 				/* are assumed to be hard links. During */
1024b88c807SRodney W. Grimes 				/* append we may have to generate unique ids */
1034b88c807SRodney W. Grimes 				/* to avoid creating incorrect hard links */
1044b88c807SRodney W. Grimes 	int hlk;		/* does archive store hard links info? if */
1054b88c807SRodney W. Grimes 				/* not, we do not bother to look for them */
1064b88c807SRodney W. Grimes 				/* during archive write operations */
10746be34b9SKris Kennaway 	int blkalgn;		/* writes must be aligned to blkalgn boundary */
1084b88c807SRodney W. Grimes 	int inhead;		/* is the trailer encoded in a valid header? */
1094b88c807SRodney W. Grimes 				/* if not, trailers are assumed to be found */
1104b88c807SRodney W. Grimes 				/* in invalid headers (i.e like tar) */
11140feca3aSMark Murray 	int (*id)(char *, int);	/* checks if a buffer is a valid header */
1124b88c807SRodney W. Grimes 				/* returns 1 if it is, o.w. returns a 0 */
11340feca3aSMark Murray 	int (*st_rd)(void);	/* initialize routine for read. so format */
1144b88c807SRodney W. Grimes 				/* can set up tables etc before it starts */
1154b88c807SRodney W. Grimes 				/* reading an archive */
11640feca3aSMark Murray 	int (*rd)(ARCHD *, char *);
11740feca3aSMark Murray 				/* read header routine. passed a pointer to */
1184b88c807SRodney W. Grimes 				/* ARCHD. It must extract the info from the */
1194b88c807SRodney W. Grimes 				/* format and store it in the ARCHD struct. */
1204b88c807SRodney W. Grimes 				/* This routine is expected to fill all the */
1214b88c807SRodney W. Grimes 				/* fields in the ARCHD (including stat buf) */
1224b88c807SRodney W. Grimes 				/* 0 is returned when a valid header is */
1234b88c807SRodney W. Grimes 				/* found. -1 when not valid. This routine */
1244b88c807SRodney W. Grimes 				/* set the skip and pad fields so the format */
1254b88c807SRodney W. Grimes 				/* independent routines know the amount of */
1264b88c807SRodney W. Grimes 				/* padding and the number of bytes of data */
1274b88c807SRodney W. Grimes 				/* which follow the header. This info is */
1284b88c807SRodney W. Grimes 				/* used skip to the next file header */
12940feca3aSMark Murray 	off_t (*end_rd)(void);	/* read cleanup. Allows format to clean up */
1304b88c807SRodney W. Grimes 				/* and MUST RETURN THE LENGTH OF THE TRAILER */
1314b88c807SRodney W. Grimes 				/* RECORD (so append knows how many bytes */
1324b88c807SRodney W. Grimes 				/* to move back to rewrite the trailer) */
13340feca3aSMark Murray 	int (*st_wr)(void);	/* initialize routine for write operations */
13440feca3aSMark Murray 	int (*wr)(ARCHD *);	/* write archive header. Passed an ARCHD */
1354b88c807SRodney W. Grimes 				/* filled with the specs on the next file to */
1364b88c807SRodney W. Grimes 				/* archived. Returns a 1 if no file data is */
1374b88c807SRodney W. Grimes 				/* is to be stored; 0 if file data is to be */
1384b88c807SRodney W. Grimes 				/* added. A -1 is returned if a write */
1394b88c807SRodney W. Grimes 				/* operation to the archive failed. this */
1404b88c807SRodney W. Grimes 				/* function sets the skip and pad fields so */
1414b88c807SRodney W. Grimes 				/* the proper padding can be added after */
1424b88c807SRodney W. Grimes 				/* file data. This routine must NEVER write */
1434b88c807SRodney W. Grimes 				/* a flawed archive header. */
14440feca3aSMark Murray 	int (*end_wr)(void);	/* end write. write the trailer and do any */
1454b88c807SRodney W. Grimes 				/* other format specific functions needed */
1469d5abbddSJens Schweikhardt 				/* at the end of an archive write */
14740feca3aSMark Murray 	int (*trail_cpio)(ARCHD *);
14840feca3aSMark Murray 	int (*trail_tar)(char *, int, int *);
14940feca3aSMark Murray 				/* returns 0 if a valid trailer, -1 if not */
1504b88c807SRodney W. Grimes 				/* For formats which encode the trailer */
1514b88c807SRodney W. Grimes 				/* outside of a valid header, a return value */
1524b88c807SRodney W. Grimes 				/* of 1 indicates that the block passed to */
1534b88c807SRodney W. Grimes 				/* it can never contain a valid header (skip */
1544b88c807SRodney W. Grimes 				/* this block, no point in looking at it)  */
15540feca3aSMark Murray 	int (*rd_data)(ARCHD *, int, off_t *);
15640feca3aSMark Murray 				/* read/process file data from the archive */
15740feca3aSMark Murray 	int (*wr_data)(ARCHD *, int, off_t *);
15840feca3aSMark Murray 				/* write/process file data to the archive */
15940feca3aSMark Murray 	int (*options)(void);	/* process format specific options (-o) */
16040feca3aSMark Murray };
1614b88c807SRodney W. Grimes 
1624b88c807SRodney W. Grimes /*
1634b88c807SRodney W. Grimes  * Pattern matching structure
1644b88c807SRodney W. Grimes  *
1654b88c807SRodney W. Grimes  * Used to store command line patterns
1664b88c807SRodney W. Grimes  */
16740feca3aSMark Murray struct pattern {
1684b88c807SRodney W. Grimes 	char		*pstr;		/* pattern to match, user supplied */
1694b88c807SRodney W. Grimes 	char		*pend;		/* end of a prefix match */
170b1787decSKris Kennaway 	char		*chdname;	/* the dir to change to if not NULL.  */
1714b88c807SRodney W. Grimes 	int		plen;		/* length of pstr */
1724b88c807SRodney W. Grimes 	int		flgs;		/* processing/state flags */
1734b88c807SRodney W. Grimes #define MTCH		0x1		/* pattern has been matched */
1744b88c807SRodney W. Grimes #define DIR_MTCH	0x2		/* pattern matched a directory */
1754b88c807SRodney W. Grimes 	struct pattern	*fow;		/* next pattern */
17640feca3aSMark Murray };
1774b88c807SRodney W. Grimes 
1784b88c807SRodney W. Grimes /*
1794b88c807SRodney W. Grimes  * General Archive Structure (used internal to pax)
1804b88c807SRodney W. Grimes  *
1814b88c807SRodney W. Grimes  * This structure is used to pass information about archive members between
1824b88c807SRodney W. Grimes  * the format independent routines and the format specific routines. When
1834b88c807SRodney W. Grimes  * new archive formats are added, they must accept requests and supply info
1844b88c807SRodney W. Grimes  * encoded in a structure of this type. The name fields are declared statically
1854b88c807SRodney W. Grimes  * here, as there is only ONE of these floating around, size is not a major
1864b88c807SRodney W. Grimes  * consideration. Eventually converting the name fields to a dynamic length
1874b88c807SRodney W. Grimes  * may be required if and when the supporting operating system removes all
1884b88c807SRodney W. Grimes  * restrictions on the length of pathnames it will resolve.
1894b88c807SRodney W. Grimes  */
19040feca3aSMark Murray struct archd {
1914b88c807SRodney W. Grimes 	int nlen;			/* file name length */
1924b88c807SRodney W. Grimes 	char name[PAXPATHLEN+1];	/* file name */
1934b88c807SRodney W. Grimes 	int ln_nlen;			/* link name length */
1944b88c807SRodney W. Grimes 	char ln_name[PAXPATHLEN+1];	/* name to link to (if any) */
1954b88c807SRodney W. Grimes 	char *org_name;			/* orig name in file system */
1964b88c807SRodney W. Grimes 	PATTERN *pat;			/* ptr to pattern match (if any) */
1974b88c807SRodney W. Grimes 	struct stat sb;			/* stat buffer see stat(2) */
1984b88c807SRodney W. Grimes 	off_t pad;			/* bytes of padding after file xfer */
1994b88c807SRodney W. Grimes 	off_t skip;			/* bytes of real data after header */
2004b88c807SRodney W. Grimes 					/* IMPORTANT. The st_size field does */
2014b88c807SRodney W. Grimes 					/* not always indicate the amount of */
2024b88c807SRodney W. Grimes 					/* data following the header. */
2034b88c807SRodney W. Grimes 	u_long crc;			/* file crc */
2044b88c807SRodney W. Grimes 	int type;			/* type of file node */
2054b88c807SRodney W. Grimes #define PAX_DIR		1		/* directory */
2064b88c807SRodney W. Grimes #define PAX_CHR		2		/* character device */
2074b88c807SRodney W. Grimes #define PAX_BLK		3		/* block device */
2084b88c807SRodney W. Grimes #define PAX_REG		4		/* regular file */
2094b88c807SRodney W. Grimes #define PAX_SLK		5		/* symbolic link */
2104b88c807SRodney W. Grimes #define PAX_SCK		6		/* socket */
2114b88c807SRodney W. Grimes #define PAX_FIF		7		/* fifo */
2124b88c807SRodney W. Grimes #define PAX_HLK		8		/* hard link */
2134b88c807SRodney W. Grimes #define PAX_HRG		9		/* hard link to a regular file */
2144b88c807SRodney W. Grimes #define PAX_CTG		10		/* high performance file */
21540feca3aSMark Murray };
2164b88c807SRodney W. Grimes 
2174b88c807SRodney W. Grimes /*
2184b88c807SRodney W. Grimes  * Format Specific Options List
2194b88c807SRodney W. Grimes  *
2204b88c807SRodney W. Grimes  * Used to pass format options to the format options handler
2214b88c807SRodney W. Grimes  */
22240feca3aSMark Murray struct oplist {
2234b88c807SRodney W. Grimes 	char		*name;		/* option variable name e.g. name= */
2244b88c807SRodney W. Grimes 	char		*value;		/* value for option variable */
2254b88c807SRodney W. Grimes 	struct oplist	*fow;		/* next option */
22640feca3aSMark Murray };
2274b88c807SRodney W. Grimes 
2284b88c807SRodney W. Grimes /*
2294b88c807SRodney W. Grimes  * General Macros
2304b88c807SRodney W. Grimes  */
2314b88c807SRodney W. Grimes #ifndef MIN
2324b88c807SRodney W. Grimes #define	       MIN(a,b) (((a)<(b))?(a):(b))
2334b88c807SRodney W. Grimes #endif
234d1e00e93SBruce Evans #define MAJOR(x)	major(x)
235d1e00e93SBruce Evans #define MINOR(x)	minor(x)
236d1e00e93SBruce Evans #define TODEV(x, y)	makedev((x), (y))
2374b88c807SRodney W. Grimes 
2384b88c807SRodney W. Grimes /*
2394b88c807SRodney W. Grimes  * General Defines
2404b88c807SRodney W. Grimes  */
2414b88c807SRodney W. Grimes #define HEX		16
2424b88c807SRodney W. Grimes #define OCT		8
2434b88c807SRodney W. Grimes #define _PAX_		1
244ffbef1cdSKris Kennaway #define _TFILE_BASE	"paxXXXXXXXXXX"
245