11556Srgrimes/*-
21556Srgrimes * Copyright (c) 1992 Keith Muller.
31556Srgrimes * Copyright (c) 1992, 1993
41556Srgrimes *	The Regents of the University of California.  All rights reserved.
51556Srgrimes *
61556Srgrimes * This code is derived from software contributed to Berkeley by
71556Srgrimes * Keith Muller of the University of California, San Diego.
81556Srgrimes *
91556Srgrimes * Redistribution and use in source and binary forms, with or without
101556Srgrimes * modification, are permitted provided that the following conditions
111556Srgrimes * are met:
121556Srgrimes * 1. Redistributions of source code must retain the above copyright
131556Srgrimes *    notice, this list of conditions and the following disclaimer.
141556Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
151556Srgrimes *    notice, this list of conditions and the following disclaimer in the
161556Srgrimes *    documentation and/or other materials provided with the distribution.
171556Srgrimes * 4. Neither the name of the University nor the names of its contributors
181556Srgrimes *    may be used to endorse or promote products derived from this software
191556Srgrimes *    without specific prior written permission.
201556Srgrimes *
211556Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
221556Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
231556Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
241556Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
251556Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261556Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271556Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281556Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291556Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301556Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311556Srgrimes * SUCH DAMAGE.
321556Srgrimes *
331556Srgrimes *	@(#)pax.h	8.2 (Berkeley) 4/18/94
3450471Speter * $FreeBSD$
351556Srgrimes */
361556Srgrimes
371556Srgrimes/*
381556Srgrimes * BSD PAX global data structures and constants.
391556Srgrimes */
401556Srgrimes
4176351Skris#define	MAXBLK		64512	/* MAX blocksize supported (posix SPEC) */
421556Srgrimes				/* WARNING: increasing MAXBLK past 32256 */
431556Srgrimes				/* will violate posix spec. */
4476351Skris#define	MAXBLK_POSIX	32256	/* MAX blocksize supported as per POSIX */
451556Srgrimes#define BLKMULT		512	/* blocksize must be even mult of 512 bytes */
461556Srgrimes				/* Don't even think of changing this */
471556Srgrimes#define DEVBLK		8192	/* default read blksize for devices */
481556Srgrimes#define FILEBLK		10240	/* default read blksize for files */
4946684Skris#define PAXPATHLEN	3072	/* maximum path length for pax. MUST be */
5077458Simp				/* longer than the system PATH_MAX */
511556Srgrimes
521556Srgrimes/*
531556Srgrimes * Pax modes of operation
541556Srgrimes */
551556Srgrimes#define	LIST		0	/* List the file in an archive */
561556Srgrimes#define	EXTRACT		1	/* extract the files in an archive */
571556Srgrimes#define ARCHIVE		2	/* write a new archive */
581556Srgrimes#define APPND		3	/* append to the end of an archive */
591556Srgrimes#define	COPY		4	/* copy files to destination dir */
601556Srgrimes#define DEFOP		LIST	/* if no flags default is to LIST */
611556Srgrimes
621556Srgrimes/*
638855Srgrimes * Device type of the current archive volume
641556Srgrimes */
651556Srgrimes#define ISREG		0	/* regular file */
661556Srgrimes#define ISCHR		1	/* character device */
671556Srgrimes#define ISBLK		2	/* block device */
681556Srgrimes#define ISTAPE		3	/* tape drive */
691556Srgrimes#define ISPIPE		4	/* pipe/socket */
701556Srgrimes
71114583Smarkmtypedef struct archd ARCHD;
72114583Smarkmtypedef struct fsub FSUB;
73114583Smarkmtypedef struct oplist OPLIST;
74114583Smarkmtypedef struct pattern PATTERN;
75114583Smarkm
761556Srgrimes/*
771556Srgrimes * Format Specific Routine Table
781556Srgrimes *
791556Srgrimes * The format specific routine table allows new archive formats to be quickly
801556Srgrimes * added. Overall pax operation is independent of the actual format used to
818855Srgrimes * form the archive. Only those routines which deal directly with the archive
8246684Skris * are tailored to the oddities of the specific format. All other routines are
831556Srgrimes * independent of the archive format. Data flow in and out of the format
841556Srgrimes * dependent routines pass pointers to ARCHD structure (described below).
851556Srgrimes */
86114583Smarkmstruct fsub {
87114583Smarkm	const char *name;	/* name of format, this is the name the user */
881556Srgrimes				/* gives to -x option to select it. */
891556Srgrimes	int bsz;		/* default block size. used when the user */
901556Srgrimes				/* does not specify a blocksize for writing */
911556Srgrimes				/* Appends continue to with the blocksize */
9276017Skris				/* the archive is currently using. */
931556Srgrimes	int hsz;		/* Header size in bytes. this is the size of */
941556Srgrimes				/* the smallest header this format supports. */
951556Srgrimes				/* Headers are assumed to fit in a BLKMULT. */
961556Srgrimes				/* If they are bigger, get_head() and */
971556Srgrimes				/* get_arc() must be adjusted */
981556Srgrimes	int udev;		/* does append require unique dev/ino? some */
991556Srgrimes				/* formats use the device and inode fields */
1001556Srgrimes				/* to specify hard links. when members in */
1011556Srgrimes				/* the archive have the same inode/dev they */
1021556Srgrimes				/* are assumed to be hard links. During */
1031556Srgrimes				/* append we may have to generate unique ids */
1041556Srgrimes				/* to avoid creating incorrect hard links */
1051556Srgrimes	int hlk;		/* does archive store hard links info? if */
1061556Srgrimes				/* not, we do not bother to look for them */
1071556Srgrimes				/* during archive write operations */
10846684Skris	int blkalgn;		/* writes must be aligned to blkalgn boundary */
1091556Srgrimes	int inhead;		/* is the trailer encoded in a valid header? */
1101556Srgrimes				/* if not, trailers are assumed to be found */
1111556Srgrimes				/* in invalid headers (i.e like tar) */
112114583Smarkm	int (*id)(char *, int);	/* checks if a buffer is a valid header */
1131556Srgrimes				/* returns 1 if it is, o.w. returns a 0 */
114114583Smarkm	int (*st_rd)(void);	/* initialize routine for read. so format */
1151556Srgrimes				/* can set up tables etc before it starts */
1161556Srgrimes				/* reading an archive */
117114583Smarkm	int (*rd)(ARCHD *, char *);
118114583Smarkm				/* read header routine. passed a pointer to */
1191556Srgrimes				/* ARCHD. It must extract the info from the */
1201556Srgrimes				/* format and store it in the ARCHD struct. */
1211556Srgrimes				/* This routine is expected to fill all the */
1221556Srgrimes				/* fields in the ARCHD (including stat buf) */
1231556Srgrimes				/* 0 is returned when a valid header is */
1241556Srgrimes				/* found. -1 when not valid. This routine */
1251556Srgrimes				/* set the skip and pad fields so the format */
1261556Srgrimes				/* independent routines know the amount of */
1271556Srgrimes				/* padding and the number of bytes of data */
1281556Srgrimes				/* which follow the header. This info is */
1291556Srgrimes				/* used skip to the next file header */
130114583Smarkm	off_t (*end_rd)(void);	/* read cleanup. Allows format to clean up */
1311556Srgrimes				/* and MUST RETURN THE LENGTH OF THE TRAILER */
1321556Srgrimes				/* RECORD (so append knows how many bytes */
1331556Srgrimes				/* to move back to rewrite the trailer) */
134114583Smarkm	int (*st_wr)(void);	/* initialize routine for write operations */
135114583Smarkm	int (*wr)(ARCHD *);	/* write archive header. Passed an ARCHD */
1361556Srgrimes				/* filled with the specs on the next file to */
1371556Srgrimes				/* archived. Returns a 1 if no file data is */
1381556Srgrimes				/* is to be stored; 0 if file data is to be */
1391556Srgrimes				/* added. A -1 is returned if a write */
1401556Srgrimes				/* operation to the archive failed. this */
1411556Srgrimes				/* function sets the skip and pad fields so */
1421556Srgrimes				/* the proper padding can be added after */
1431556Srgrimes				/* file data. This routine must NEVER write */
1441556Srgrimes				/* a flawed archive header. */
145114583Smarkm	int (*end_wr)(void);	/* end write. write the trailer and do any */
1461556Srgrimes				/* other format specific functions needed */
147108533Sschweikh				/* at the end of an archive write */
148114583Smarkm	int (*trail_cpio)(ARCHD *);
149114583Smarkm	int (*trail_tar)(char *, int, int *);
150114583Smarkm				/* returns 0 if a valid trailer, -1 if not */
1511556Srgrimes				/* For formats which encode the trailer */
1521556Srgrimes				/* outside of a valid header, a return value */
1531556Srgrimes				/* of 1 indicates that the block passed to */
1541556Srgrimes				/* it can never contain a valid header (skip */
1551556Srgrimes				/* this block, no point in looking at it)  */
156114583Smarkm	int (*rd_data)(ARCHD *, int, off_t *);
157114583Smarkm				/* read/process file data from the archive */
158114583Smarkm	int (*wr_data)(ARCHD *, int, off_t *);
159114583Smarkm				/* write/process file data to the archive */
160114583Smarkm	int (*options)(void);	/* process format specific options (-o) */
161114583Smarkm};
1621556Srgrimes
1631556Srgrimes/*
1641556Srgrimes * Pattern matching structure
1651556Srgrimes *
1661556Srgrimes * Used to store command line patterns
1671556Srgrimes */
168114583Smarkmstruct pattern {
1691556Srgrimes	char		*pstr;		/* pattern to match, user supplied */
1701556Srgrimes	char		*pend;		/* end of a prefix match */
17176351Skris	char		*chdname;	/* the dir to change to if not NULL.  */
1721556Srgrimes	int		plen;		/* length of pstr */
1731556Srgrimes	int		flgs;		/* processing/state flags */
1741556Srgrimes#define MTCH		0x1		/* pattern has been matched */
1751556Srgrimes#define DIR_MTCH	0x2		/* pattern matched a directory */
1761556Srgrimes	struct pattern	*fow;		/* next pattern */
177114583Smarkm};
1781556Srgrimes
1791556Srgrimes/*
1801556Srgrimes * General Archive Structure (used internal to pax)
1811556Srgrimes *
1821556Srgrimes * This structure is used to pass information about archive members between
1831556Srgrimes * the format independent routines and the format specific routines. When
1841556Srgrimes * new archive formats are added, they must accept requests and supply info
1851556Srgrimes * encoded in a structure of this type. The name fields are declared statically
1861556Srgrimes * here, as there is only ONE of these floating around, size is not a major
1871556Srgrimes * consideration. Eventually converting the name fields to a dynamic length
1881556Srgrimes * may be required if and when the supporting operating system removes all
1891556Srgrimes * restrictions on the length of pathnames it will resolve.
1901556Srgrimes */
191114583Smarkmstruct archd {
1921556Srgrimes	int nlen;			/* file name length */
1931556Srgrimes	char name[PAXPATHLEN+1];	/* file name */
1941556Srgrimes	int ln_nlen;			/* link name length */
1951556Srgrimes	char ln_name[PAXPATHLEN+1];	/* name to link to (if any) */
196102230Strhodes	char *org_name;			/* orig name in file system */
1971556Srgrimes	PATTERN *pat;			/* ptr to pattern match (if any) */
1981556Srgrimes	struct stat sb;			/* stat buffer see stat(2) */
1991556Srgrimes	off_t pad;			/* bytes of padding after file xfer */
2001556Srgrimes	off_t skip;			/* bytes of real data after header */
2011556Srgrimes					/* IMPORTANT. The st_size field does */
2021556Srgrimes					/* not always indicate the amount of */
2031556Srgrimes					/* data following the header. */
2041556Srgrimes	u_long crc;			/* file crc */
2051556Srgrimes	int type;			/* type of file node */
2061556Srgrimes#define PAX_DIR		1		/* directory */
2071556Srgrimes#define PAX_CHR		2		/* character device */
2081556Srgrimes#define PAX_BLK		3		/* block device */
2091556Srgrimes#define PAX_REG		4		/* regular file */
2101556Srgrimes#define PAX_SLK		5		/* symbolic link */
2111556Srgrimes#define PAX_SCK		6		/* socket */
2121556Srgrimes#define PAX_FIF		7		/* fifo */
2131556Srgrimes#define PAX_HLK		8		/* hard link */
2141556Srgrimes#define PAX_HRG		9		/* hard link to a regular file */
2158855Srgrimes#define PAX_CTG		10		/* high performance file */
216114583Smarkm};
2171556Srgrimes
2181556Srgrimes/*
2191556Srgrimes * Format Specific Options List
2201556Srgrimes *
2211556Srgrimes * Used to pass format options to the format options handler
2221556Srgrimes */
223114583Smarkmstruct oplist {
2241556Srgrimes	char		*name;		/* option variable name e.g. name= */
2251556Srgrimes	char		*value;		/* value for option variable */
2261556Srgrimes	struct oplist	*fow;		/* next option */
227114583Smarkm};
2281556Srgrimes
2291556Srgrimes/*
2301556Srgrimes * General Macros
2311556Srgrimes */
2321556Srgrimes#ifndef MIN
23376019Skris#define	       MIN(a,b) (((a)<(b))?(a):(b))
2341556Srgrimes#endif
2359322Sbde#define MAJOR(x)	major(x)
2369322Sbde#define MINOR(x)	minor(x)
2379322Sbde#define TODEV(x, y)	makedev((x), (y))
2381556Srgrimes
2391556Srgrimes/*
2401556Srgrimes * General Defines
2411556Srgrimes */
24276019Skris#define HEX		16
24376019Skris#define OCT		8
24476019Skris#define _PAX_		1
24576016Skris#define _TFILE_BASE	"paxXXXXXXXXXX"
246