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