1 /**	$MirOS: src/bin/pax/pax.h,v 1.12 2012/02/16 17:27:32 tg Exp $ */
2 /*	$OpenBSD: pax.h,v 1.17 2005/11/09 19:59:06 otto Exp $	*/
3 /*	$NetBSD: pax.h,v 1.3 1995/03/21 09:07:41 cgd Exp $	*/
4 
5 /*-
6  * Copyright (c) 2011, 2012
7  *	Thorsten Glaser <tg@debian.org>
8  * Copyright (c) 1992 Keith Muller.
9  * Copyright (c) 1992, 1993
10  *	The Regents of the University of California.  All rights reserved.
11  *
12  * This code is derived from software contributed to Berkeley by
13  * Keith Muller of the University of California, San Diego.
14  *
15  * Redistribution and use in source and binary forms, with or without
16  * modification, are permitted provided that the following conditions
17  * are met:
18  * 1. Redistributions of source code must retain the above copyright
19  *    notice, this list of conditions and the following disclaimer.
20  * 2. Redistributions in binary form must reproduce the above copyright
21  *    notice, this list of conditions and the following disclaimer in the
22  *    documentation and/or other materials provided with the distribution.
23  * 3. 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 /*
43  * BSD PAX global data structures and constants.
44  */
45 
46 #define	MAXBLK		64512	/* MAX blocksize supported (posix SPEC) */
47 				/* WARNING: increasing MAXBLK past 32256 */
48 				/* will violate posix spec. */
49 #define	MAXBLK_POSIX	32256	/* MAX blocksize supported as per POSIX */
50 #define BLKMULT		512	/* blocksize must be even mult of 512 bytes */
51 				/* Don't even think of changing this */
52 #define DEVBLK		8192	/* default read blksize for devices */
53 #define FILEBLK		10240	/* default read blksize for files */
54 #define PAXPATHLEN	3072	/* maximum path length for pax. MUST be */
55 				/* longer than the system MAXPATHLEN */
56 
57 /*
58  * pax modes of operation
59  */
60 #define ERROR		-1	/* nothing selected */
61 #define	LIST		0	/* List the file in an archive */
62 #define	EXTRACT		1	/* extract the files in an archive */
63 #define ARCHIVE		2	/* write a new archive */
64 #define APPND		3	/* append to the end of an archive */
65 #define	COPY		4	/* copy files to destination dir */
66 
67 /*
68  * Device type of the current archive volume
69  */
70 #define ISREG		0	/* regular file */
71 #define ISCHR		1	/* character device */
72 #define ISBLK		2	/* block device */
73 #define ISTAPE		3	/* tape drive */
74 #define ISPIPE		4	/* pipe/socket */
75 
76 /*
77  * Pattern matching structure
78  *
79  * Used to store command line patterns
80  */
81 typedef struct pattern {
82 	char		*pstr;		/* pattern to match, user supplied */
83 	char		*pend;		/* end of a prefix match */
84 	char		*chdname;	/* the dir to change to if not NULL.  */
85 	int		plen;		/* length of pstr */
86 	int		flgs;		/* processing/state flags */
87 #define MTCH		0x1		/* pattern has been matched */
88 #define DIR_MTCH	0x2		/* pattern matched a directory */
89 	struct pattern	*fow;		/* next pattern */
90 } PATTERN;
91 
92 /*
93  * General Archive Structure (used internal to pax)
94  *
95  * This structure is used to pass information about archive members between
96  * the format independent routines and the format specific routines. When
97  * new archive formats are added, they must accept requests and supply info
98  * encoded in a structure of this type. The name fields are declared statically
99  * here, as there is only ONE of these floating around, size is not a major
100  * consideration. Eventually converting the name fields to a dynamic length
101  * may be required if and when the supporting operating system removes all
102  * restrictions on the length of pathnames it will resolve.
103  */
104 typedef struct {
105 	int nlen;			/* file name length */
106 	char name[PAXPATHLEN+1];	/* file name */
107 	int ln_nlen;			/* link name length */
108 	char ln_name[PAXPATHLEN+1];	/* name to link to (if any) */
109 	char *org_name;			/* orig name in file system */
110 	PATTERN *pat;			/* ptr to pattern match (if any) */
111 	struct stat sb;			/* stat buffer see stat(2) */
112 	off_t pad;			/* bytes of padding after file xfer */
113 	off_t skip;			/* bytes of real data after header */
114 					/* IMPORTANT. The st_size field does */
115 					/* not always indicate the amount of */
116 					/* data following the header. */
117 	u_int32_t crc;			/* file crc */
118 	int type;			/* type of file node */
119 #define PAX_DIR		1		/* directory */
120 #define PAX_CHR		2		/* character device */
121 #define PAX_BLK		3		/* block device */
122 #define PAX_REG		4		/* regular file */
123 #define PAX_SLK		5		/* symbolic link */
124 #define PAX_SCK		6		/* socket */
125 #define PAX_FIF		7		/* fifo */
126 #define PAX_HLK		8		/* hard link */
127 #define PAX_HRG		9		/* hard link to a regular file */
128 #define PAX_CTG		10		/* high performance file */
129 #define PAX_GLL		11		/* GNU long symlink */
130 #define PAX_GLF		12		/* GNU long file */
131 #define PAX_LINKOR	0x80000000	/* hard link detection OR */
132 } ARCHD;
133 
134 /*
135  * Format Specific Routine Table
136  *
137  * The format specific routine table allows new archive formats to be quickly
138  * added. Overall pax operation is independent of the actual format used to
139  * form the archive. Only those routines which deal directly with the archive
140  * are tailored to the oddities of the specific format. All other routines are
141  * independent of the archive format. Data flow in and out of the format
142  * dependent routines pass pointers to ARCHD structure (described below).
143  */
144 typedef struct {
145 	const char *name;	/* name of format, this is the name the user */
146 				/* gives to -x option to select it. */
147 	int bsz;		/* default block size. used when the user */
148 				/* does not specify a blocksize for writing */
149 				/* Appends continue to with the blocksize */
150 				/* the archive is currently using. */
151 	int hsz;		/* Header size in bytes. this is the size of */
152 				/* the smallest header this format supports. */
153 				/* Headers are assumed to fit in a BLKMULT. */
154 				/* If they are bigger, get_head() and */
155 				/* get_arc() must be adjusted */
156 	int udev;		/* does append require unique dev/ino? some */
157 				/* formats use the device and inode fields */
158 				/* to specify hard links. when members in */
159 				/* the archive have the same inode/dev they */
160 				/* are assumed to be hard links. During */
161 				/* append we may have to generate unique ids */
162 				/* to avoid creating incorrect hard links */
163 	int hlk;		/* does archive store hard links info? if */
164 				/* not, we do not bother to look for them */
165 				/* during archive write operations */
166 	int blkalgn;		/* writes must be aligned to blkalgn boundary */
167 	int inhead;		/* is the trailer encoded in a valid header? */
168 				/* if not, trailers are assumed to be found */
169 				/* in invalid headers (i.e like tar) */
170 	int (*id)(char *,	/* checks if a buffer is a valid header */
171 	    int);		/* returns 1 if it is, o.w. returns a 0 */
172 	int (*st_rd)(void);	/* initialise routine for read. so format */
173 				/* can set up tables etc before it starts */
174 				/* reading an archive */
175 	int (*rd)(ARCHD *,	/* read header routine. passed a pointer to */
176 	    char *);		/* ARCHD. It must extract the info from the */
177 				/* format and store it in the ARCHD struct. */
178 				/* This routine is expected to fill all the */
179 				/* fields in the ARCHD (including stat buf) */
180 				/* 0 is returned when a valid header is */
181 				/* found. -1 when not valid. This routine */
182 				/* set the skip and pad fields so the format */
183 				/* independent routines know the amount of */
184 				/* padding and the number of bytes of data */
185 				/* which follow the header. This info is */
186 				/* used skip to the next file header */
187 	off_t (*end_rd)(void);	/* read cleanup. Allows format to clean up */
188 				/* and MUST RETURN THE LENGTH OF THE TRAILER */
189 				/* RECORD (so append knows how many bytes */
190 				/* to move back to rewrite the trailer) */
191 	int (*st_wr)(int);	/* initialise routine for write operations */
192 	int (*wr)(ARCHD *);	/* write archive header. Passed an ARCHD */
193 				/* filled with the specs on the next file to */
194 				/* archived. Returns a 1 if no file data is */
195 				/* is to be stored; 0 if file data is to be */
196 				/* added. A -1 is returned if a write */
197 				/* operation to the archive failed. this */
198 				/* function sets the skip and pad fields so */
199 				/* the proper padding can be added after */
200 				/* file data. This routine must NEVER write */
201 				/* a flawed archive header. */
202 	int (*end_wr)(void);	/* end write. write the trailer and do any */
203 				/* other format specific functions needed */
204 				/* at the end of an archive write */
205 	int (*trail)(ARCHD *,	/* returns 0 if a valid trailer, -1 if not */
206 	    char *, int,	/* For formats which encode the trailer */
207 	    int *);		/* outside of a valid header, a return value */
208 				/* of 1 indicates that the block passed to */
209 				/* it can never contain a valid header (skip */
210 				/* this block, no point in looking at it)  */
211 				/* CAUTION: parameters to this function are */
212 				/* different for trailers inside or outside */
213 				/* of headers. See get_head() for details */
214 	int (*rd_data)(ARCHD *,	/* read/process file data from the archive */
215 	    int, off_t *);
216 	int (*wr_data)(ARCHD *,	/* write/process file data to the archive */
217 	    int, off_t *);
218 	int (*options)(void);	/* process format specific options (-o) */
219 	char is_uar;		/* is Unix Archiver (sequential, no trailer) */
220 } FSUB;
221 
222 /*
223  * Format Specific Options List
224  *
225  * Used to pass format options to the format options handler
226  */
227 typedef struct oplist {
228 	char		*name;		/* option variable name e.g. name= */
229 	char		*value;		/* value for option variable */
230 	struct oplist	*fow;		/* next option */
231 } OPLIST;
232 
233 /*
234  * General Macros
235  */
236 #ifndef MIN
237 #define MIN(a,b)	(((a)<(b))?(a):(b))
238 #endif
239 #ifdef __INTERIX
240 #include <sys/mkdev.h>
241 #endif
242 #define MAJOR(x)	major(x)
243 #define MINOR(x)	minor(x)
244 #ifdef __INTERIX
245 #define TODEV		mkdev
246 #else
247 #define TODEV		makedev
248 #endif
249 
250 #if !defined(__INTERIX) && !defined(__APPLE__)
251 #define HAS_TAPE	1
252 #else
253 #define HAS_TAPE	0
254 #endif
255 
256 #if defined(MirBSD) && (MirBSD >= 0x09A1) && \
257     defined(__ELF__) && defined(__GNUC__) && \
258     !defined(__llvm__) && !defined(__NWCC__)
259 /*
260  * We got usable __IDSTRING __COPYRIGHT __RCSID __SCCSID macros
261  * which work for all cases; no need to redefine them using the
262  * "portable" macros from below when we might have the "better"
263  * gcc+ELF specific macros or other system dependent ones.
264  */
265 #else
266 #undef __IDSTRING
267 #undef __IDSTRING_CONCAT
268 #undef __IDSTRING_EXPAND
269 #undef __COPYRIGHT
270 #undef __RCSID
271 #undef __SCCSID
272 #define __IDSTRING_CONCAT(l,p)		__LINTED__ ## l ## _ ## p
273 #define __IDSTRING_EXPAND(l,p)		__IDSTRING_CONCAT(l,p)
274 #ifdef MKSH_DONT_EMIT_IDSTRING
275 #define __IDSTRING(prefix, string)	/* nothing */
276 #else
277 #define __IDSTRING(prefix, string)				\
278 	static const char __IDSTRING_EXPAND(__LINE__,prefix) []	\
279 	    __attribute__((__used__)) = "@(""#)" #prefix ": " string
280 #endif
281 #define __COPYRIGHT(x)		__IDSTRING(copyright,x)
282 #define __RCSID(x)		__IDSTRING(rcsid,x)
283 #define __SCCSID(x)		__IDSTRING(sccsid,x)
284 #endif
285 
286 /*
287  * General Defines
288  */
289 #define HEX		16
290 #define OCT		8
291 #define _PAX_		1
292 #define _TFILE_BASE	"paxXXXXXXXXXX"
293 
294 /* copied from <tzfile.h> */
295 #define SECSPERMIN	60
296 #define MINSPERHOUR	60
297 #define HOURSPERDAY	24
298 #define DAYSPERNYEAR	365
299 #define SECSPERHOUR	(SECSPERMIN * MINSPERHOUR)
300 #define SECSPERDAY	((long)SECSPERHOUR * HOURSPERDAY)
301 #define TM_YEAR_BASE	1900
302 
303 #ifndef LONG_OFF_T
304 #define OT_FMT		"llu"
305 typedef unsigned long long ot_type;
306 #else
307 #define OT_FMT		"lu"
308 typedef unsigned long ot_type;
309 #endif
310