xref: /freebsd/bin/pax/pax.h (revision 5e3190f700637fcfc1a52daeaa4a031fdd2557c7)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1992 Keith Muller.
5  * Copyright (c) 1992, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * This code is derived from software contributed to Berkeley by
9  * Keith Muller of the University of California, San Diego.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  *
35  *	@(#)pax.h	8.2 (Berkeley) 4/18/94
36  */
37 
38 /*
39  * BSD PAX global data structures and constants.
40  */
41 
42 #define	MAXBLK		64512	/* MAX blocksize supported (posix SPEC) */
43 				/* WARNING: increasing MAXBLK past 32256 */
44 				/* will violate posix spec. */
45 #define	MAXBLK_POSIX	32256	/* MAX blocksize supported as per POSIX */
46 #define BLKMULT		512	/* blocksize must be even mult of 512 bytes */
47 				/* Don't even think of changing this */
48 #define DEVBLK		8192	/* default read blksize for devices */
49 #define FILEBLK		10240	/* default read blksize for files */
50 #define PAXPATHLEN	3072	/* maximum path length for pax. MUST be */
51 				/* longer than the system PATH_MAX */
52 
53 /*
54  * Pax modes of operation
55  */
56 #define	LIST		0	/* List the file in an archive */
57 #define	EXTRACT		1	/* extract the files in an archive */
58 #define ARCHIVE		2	/* write a new archive */
59 #define APPND		3	/* append to the end of an archive */
60 #define	COPY		4	/* copy files to destination dir */
61 #define DEFOP		LIST	/* if no flags default is to LIST */
62 
63 /*
64  * Device type of the current archive volume
65  */
66 #define ISREG		0	/* regular file */
67 #define ISCHR		1	/* character device */
68 #define ISBLK		2	/* block device */
69 #define ISTAPE		3	/* tape drive */
70 #define ISPIPE		4	/* pipe/socket */
71 
72 typedef struct archd ARCHD;
73 typedef struct fsub FSUB;
74 typedef struct oplist OPLIST;
75 typedef struct pattern PATTERN;
76 
77 /*
78  * Format Specific Routine Table
79  *
80  * The format specific routine table allows new archive formats to be quickly
81  * added. Overall pax operation is independent of the actual format used to
82  * form the archive. Only those routines which deal directly with the archive
83  * are tailored to the oddities of the specific format. All other routines are
84  * independent of the archive format. Data flow in and out of the format
85  * dependent routines pass pointers to ARCHD structure (described below).
86  */
87 struct fsub {
88 	const char *name;	/* name of format, this is the name the user */
89 				/* gives to -x option to select it. */
90 	int bsz;		/* default block size. used when the user */
91 				/* does not specify a blocksize for writing */
92 				/* Appends continue to with the blocksize */
93 				/* the archive is currently using. */
94 	int hsz;		/* Header size in bytes. this is the size of */
95 				/* the smallest header this format supports. */
96 				/* Headers are assumed to fit in a BLKMULT. */
97 				/* If they are bigger, get_head() and */
98 				/* get_arc() must be adjusted */
99 	int udev;		/* does append require unique dev/ino? some */
100 				/* formats use the device and inode fields */
101 				/* to specify hard links. when members in */
102 				/* the archive have the same inode/dev they */
103 				/* are assumed to be hard links. During */
104 				/* append we may have to generate unique ids */
105 				/* to avoid creating incorrect hard links */
106 	int hlk;		/* does archive store hard links info? if */
107 				/* not, we do not bother to look for them */
108 				/* during archive write operations */
109 	int blkalgn;		/* writes must be aligned to blkalgn boundary */
110 	int inhead;		/* is the trailer encoded in a valid header? */
111 				/* if not, trailers are assumed to be found */
112 				/* in invalid headers (i.e like tar) */
113 	int (*id)(char *, int);	/* checks if a buffer is a valid header */
114 				/* returns 1 if it is, o.w. returns a 0 */
115 	int (*st_rd)(void);	/* initialize routine for read. so format */
116 				/* can set up tables etc before it starts */
117 				/* reading an archive */
118 	int (*rd)(ARCHD *, char *);
119 				/* read header routine. passed a pointer to */
120 				/* ARCHD. It must extract the info from the */
121 				/* format and store it in the ARCHD struct. */
122 				/* This routine is expected to fill all the */
123 				/* fields in the ARCHD (including stat buf) */
124 				/* 0 is returned when a valid header is */
125 				/* found. -1 when not valid. This routine */
126 				/* set the skip and pad fields so the format */
127 				/* independent routines know the amount of */
128 				/* padding and the number of bytes of data */
129 				/* which follow the header. This info is */
130 				/* used skip to the next file header */
131 	off_t (*end_rd)(void);	/* read cleanup. Allows format to clean up */
132 				/* and MUST RETURN THE LENGTH OF THE TRAILER */
133 				/* RECORD (so append knows how many bytes */
134 				/* to move back to rewrite the trailer) */
135 	int (*st_wr)(void);	/* initialize routine for write operations */
136 	int (*wr)(ARCHD *);	/* write archive header. Passed an ARCHD */
137 				/* filled with the specs on the next file to */
138 				/* archived. Returns a 1 if no file data is */
139 				/* is to be stored; 0 if file data is to be */
140 				/* added. A -1 is returned if a write */
141 				/* operation to the archive failed. this */
142 				/* function sets the skip and pad fields so */
143 				/* the proper padding can be added after */
144 				/* file data. This routine must NEVER write */
145 				/* a flawed archive header. */
146 	int (*end_wr)(void);	/* end write. write the trailer and do any */
147 				/* other format specific functions needed */
148 				/* at the end of an archive write */
149 	int (*trail_cpio)(ARCHD *);
150 	int (*trail_tar)(char *, int, int *);
151 				/* returns 0 if a valid trailer, -1 if not */
152 				/* For formats which encode the trailer */
153 				/* outside of a valid header, a return value */
154 				/* of 1 indicates that the block passed to */
155 				/* it can never contain a valid header (skip */
156 				/* this block, no point in looking at it)  */
157 	int (*rd_data)(ARCHD *, int, off_t *);
158 				/* read/process file data from the archive */
159 	int (*wr_data)(ARCHD *, int, off_t *);
160 				/* write/process file data to the archive */
161 	int (*options)(void);	/* process format specific options (-o) */
162 };
163 
164 /*
165  * Pattern matching structure
166  *
167  * Used to store command line patterns
168  */
169 struct pattern {
170 	char		*pstr;		/* pattern to match, user supplied */
171 	char		*pend;		/* end of a prefix match */
172 	char		*chdname;	/* the dir to change to if not NULL.  */
173 	int		plen;		/* length of pstr */
174 	int		flgs;		/* processing/state flags */
175 #define MTCH		0x1		/* pattern has been matched */
176 #define DIR_MTCH	0x2		/* pattern matched a directory */
177 	struct pattern	*fow;		/* next pattern */
178 };
179 
180 /*
181  * General Archive Structure (used internal to pax)
182  *
183  * This structure is used to pass information about archive members between
184  * the format independent routines and the format specific routines. When
185  * new archive formats are added, they must accept requests and supply info
186  * encoded in a structure of this type. The name fields are declared statically
187  * here, as there is only ONE of these floating around, size is not a major
188  * consideration. Eventually converting the name fields to a dynamic length
189  * may be required if and when the supporting operating system removes all
190  * restrictions on the length of pathnames it will resolve.
191  */
192 struct archd {
193 	int nlen;			/* file name length */
194 	char name[PAXPATHLEN+1];	/* file name */
195 	int ln_nlen;			/* link name length */
196 	char ln_name[PAXPATHLEN+1];	/* name to link to (if any) */
197 	char *org_name;			/* orig name in file system */
198 	PATTERN *pat;			/* ptr to pattern match (if any) */
199 	struct stat sb;			/* stat buffer see stat(2) */
200 	off_t pad;			/* bytes of padding after file xfer */
201 	off_t skip;			/* bytes of real data after header */
202 					/* IMPORTANT. The st_size field does */
203 					/* not always indicate the amount of */
204 					/* data following the header. */
205 	u_long crc;			/* file crc */
206 	int type;			/* type of file node */
207 #define PAX_DIR		1		/* directory */
208 #define PAX_CHR		2		/* character device */
209 #define PAX_BLK		3		/* block device */
210 #define PAX_REG		4		/* regular file */
211 #define PAX_SLK		5		/* symbolic link */
212 #define PAX_SCK		6		/* socket */
213 #define PAX_FIF		7		/* fifo */
214 #define PAX_HLK		8		/* hard link */
215 #define PAX_HRG		9		/* hard link to a regular file */
216 #define PAX_CTG		10		/* high performance file */
217 };
218 
219 /*
220  * Format Specific Options List
221  *
222  * Used to pass format options to the format options handler
223  */
224 struct oplist {
225 	char		*name;		/* option variable name e.g. name= */
226 	char		*value;		/* value for option variable */
227 	struct oplist	*fow;		/* next option */
228 };
229 
230 /*
231  * General Macros
232  */
233 #ifndef MIN
234 #define	       MIN(a,b) (((a)<(b))?(a):(b))
235 #endif
236 #define MAJOR(x)	major(x)
237 #define MINOR(x)	minor(x)
238 #define TODEV(x, y)	makedev((x), (y))
239 
240 /*
241  * General Defines
242  */
243 #define HEX		16
244 #define OCT		8
245 #define _PAX_		1
246 #define _TFILE_BASE	"paxXXXXXXXXXX"
247