xref: /illumos-gate/usr/src/cmd/cpio/cpio.h (revision 598f4ceed9327d2d6c2325dd67cae3aa06f7fea6)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright (c) 2012 Gary Mills
23  *
24  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
25  * Use is subject to license terms.
26  */
27 
28 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
29 /*	  All Rights Reserved  	*/
30 
31 #ifndef	_CPIO_H
32 #define	_CPIO_H
33 
34 #ifdef	__cplusplus
35 extern "C" {
36 #endif
37 
38 #include <stdio.h>
39 #include <archives.h>
40 
41 /* Option Character keys (OC#), where '#' is the option character specified. */
42 
43 #define	OCa	0x1
44 #define	OCb	0x2
45 #define	OCc	0x4
46 #define	OCd	0x8
47 #define	OCf	0x10
48 #define	OCi	0x20
49 #define	OCk	0x40
50 #define	OCl	0x80
51 #define	OCm	0x100
52 #define	OCo	0x200
53 #define	OCp	0x400
54 #define	OCr	0x800
55 #define	OCs	0x1000
56 #define	OCt	0x2000
57 #define	OCu	0x4000
58 #define	OCv	0x8000
59 #define	OCA	0x10000
60 #define	OCB	0x20000
61 #define	OCC	0x40000
62 #define	OCE	0x80000
63 #define	OCH	0x100000
64 #define	OCI	0x200000
65 #define	OCL	0x400000
66 #define	OCM	0x800000
67 #define	OCO	0x1000000
68 #define	OCR	0x2000000
69 #define	OCS	0x4000000
70 #define	OCV	0x8000000
71 #define	OC6	0x10000000
72 #define	BSM	0x20000000
73 #define	OCP	0x40000000
74 #define	OCq	0x80000000
75 
76 /* Sparse file support */
77 #define	C_ISSPARSE	0200000
78 #define	S_IFSPARSE	0x10000
79 #define	HIGH_ORD_MASK	0x30000
80 #define	S_ISSPARSE(mode) \
81 	(S_ISREG(mode) && (mode & HIGH_ORD_MASK) == S_IFSPARSE)
82 
83 /* Invalid option masks for each action option (-i, -o or -p). */
84 
85 #define	INV_MSK4i	(OCo | OCp | OCA | OCL | OCO)
86 
87 #define	INV_MSK4o	(OCi | OCp | OCE | OCI | OCR)
88 
89 #define	INV_MSK4p	(OCf | OCi | OCo | OCr | OCt | OCA \
90 			| OCE | OCH | OCI | OCO)
91 
92 /* Header types */
93 
94 #define	NONE	0	/* No header value verified */
95 #define	BIN	1	/* Binary */
96 #define	CHR	2	/* ASCII character (-c) */
97 #define	ASC	3	/* ASCII with expanded maj/min numbers */
98 #define	CRC	4	/* CRC with expanded maj/min numbers */
99 #define	TARTYP	5	/* Tar or USTAR */
100 #define	SECURE	6	/* Secure system */
101 
102 /* Differentiate between TAR and USTAR */
103 
104 #define	TAR	7	/* Regular tar */
105 #define	USTAR	8	/* IEEE data interchange standard */
106 
107 #define	ULL_MAX_SIZE	20
108 #define	UL_MAX_SIZE	10
109 
110 /* constants for bar, used for extracting bar archives */
111 #define	BAR	9
112 #define	BAR_VOLUME_MAGIC	'V'
113 #define	BARTYP	7
114 #define	BARSZ	512
115 #define	BAR_TAPE_SIZE	(126*BARSZ)
116 #define	BAR_FLOPPY_SIZE	(18*BARSZ)
117 
118 /* the pathname lengths for the USTAR header */
119 
120 #define	MAXNAM	256	/* The maximum pathname length */
121 #define	NAMSIZ	100	/* The maximum length of the name field */
122 #define	PRESIZ	155	/* The maximum length of the prefix */
123 
124 /* HDRSZ: header size minus filename field length */
125 
126 #define	HDRSZ (Hdr.h_name - (char *)&Hdr)
127 
128 /*
129  * IDENT: Determine if two stat() structures represent identical files.
130  * Assumes that if the device and inode are the same the files are
131  * identical (prevents the archive file from appearing in the archive).
132  */
133 
134 #define	IDENT(a, b) ((a.st_ino == b.st_ino && a.st_dev == b.st_dev) ? 1 : 0)
135 
136 /*
137  * FLUSH: Determine if enough space remains in the buffer to hold
138  * cnt bytes, if not, call bflush() to flush the buffer to the archive.
139  */
140 
141 #define	FLUSH(cnt) if ((Buffr.b_end_p - Buffr.b_in_p) < cnt) bflush()
142 
143 /*
144  * FILL: Determine if enough bytes remain in the buffer to meet current needs,
145  * if not, call rstbuf() to reset and refill the buffer from the archive.
146  */
147 
148 #define	FILL(cnt) while (Buffr.b_cnt < cnt) rstbuf()
149 
150 /*
151  * VERBOSE: If x is non-zero, call verbose().
152  */
153 
154 #define	VERBOSE(x, name) if (x) verbose(name)
155 
156 /*
157  * FORMAT: Date time formats
158  * b - abbreviated month name
159  * e - day of month (1 - 31)
160  * H - hour (00 - 23)
161  * M - minute (00 - 59)
162  * Y - year as ccyy
163  */
164 
165 #define	FORMAT	"%b %e %H:%M %Y"
166 
167 /* Extended system attributes */
168 #ifndef	VIEW_READONLY
169 #define	VIEW_READONLY	"SUNWattr_ro"
170 #endif
171 
172 #ifndef	VIEW_READWRITE
173 #define	VIEW_READWRITE	"SUNWattr_rw"
174 #endif
175 
176 #define	min(a, b)	((a) < (b) ? (a) : (b))
177 
178 /* Values used in typeflag field */
179 #define	REGTYPE		'0'		/* Regular File */
180 #define	LNKTYPE		'1'		/* Link */
181 #define	SYMTYPE		'2'		/* Reserved */
182 #define	CHRTYPE		'3'		/* Character Special File */
183 #define	BLKTYPE		'4'		/* Block Special File */
184 #define	DIRTYPE		'5'		/* Directory */
185 #define	FIFOTYPE	'6'		/* FIFO */
186 #define	CONTTYPE	'7'		/* Reserved */
187 #define	XHDRTYPE	'X'		/* Extended header */
188 
189 #define	INPUT	0	/* -i mode (used for chgreel() */
190 #define	OUTPUT	1	/* -o mode (used for chgreel() */
191 #define	APATH	1024	/* maximum ASC or CRC header path length */
192 #define	CPATH	256	/* maximum -c and binary path length */
193 #define	BUFSZ	512	/* default buffer size for archive I/O */
194 #define	CPIOBSZ	8192	/* buffer size for file system I/O */
195 #define	LNK_INC	500	/* link allocation increment */
196 #define	MX_BUFS	10	/* max. number of buffers to allocate */
197 
198 #define	F_SKIP	0	/* an object did not match the patterns */
199 #define	F_LINK	1	/* linked file */
200 #define	F_EXTR	2	/* extract non-linked object that matched patterns */
201 
202 #define	MX_SEEKS	10	/* max. number of lseek attempts after error */
203 #define	SEEK_ABS	0	/* lseek absolute */
204 #define	SEEK_REL	1	/* lseek relative */
205 
206 /*
207  * xxx_CNT represents the number of sscanf items that will be matched
208  * if the sscanf to read a header is successful.  If sscanf returns a number
209  * that is not equal to this, an error occured (which indicates that this
210  * is not a valid header of the type assumed.
211  */
212 
213 #define	ASC_CNT	14	/* ASC and CRC headers */
214 #define	CHR_CNT	11	/* CHR header */
215 
216 /* These defines determine the severity of the message sent to the user. */
217 
218 #define	ERR	1	/* Error message (warning) - not fatal */
219 #define	EXT	2	/* Error message - fatal, causes exit */
220 #define	ERRN	3	/* Error message with errno (warning) - not fatal */
221 #define	EXTN	4	/* Error message with errno - fatal, causes exit */
222 #define	POST	5	/* Information message, not an error */
223 #define	EPOST	6	/* Information message to stderr */
224 
225 #define	SIXTH	060000	/* UNIX 6th edition files */
226 
227 #define	P_SKIP	0	/* File should be skipped */
228 #define	P_PROC	1	/* File should be processed */
229 
230 #define	U_KEEP	0	/* Keep the existing version of a file (-u) */
231 #define	U_OVER	1	/* Overwrite the existing version of a file (-u) */
232 
233 /*
234  * _20K: Allocate the maximum of (20K or (MX_BUFS * Bufsize)) bytes
235  * for the main I/O buffer.  Therefore if a user specifies a small buffer
236  * size, they still get decent performance due to the buffering strategy.
237  */
238 
239 #define	_20K	20480
240 
241 #define	HALFWD	1	/* Pad headers/data to halfword boundaries */
242 #define	FULLWD	3	/* Pad headers/data to word boundaries */
243 #define	FULLBK	511	/* Pad headers/data to 512 byte boundaries */
244 
245 /* bar structure */
246 union b_block {
247 	char dummy[TBLOCK];
248 	struct bar_header {
249 		char mode[8];
250 		char uid[8];
251 		char gid[8];
252 		char size[12];
253 		char mtime[12];
254 		char chksum[8];
255 		char rdev[8];
256 		char linkflag;
257 
258 		/*
259 		 * The following fields are specific to the volume
260 		 * header.  They are set to zero in all file headers
261 		 * in the archive.
262 		 */
263 		char bar_magic[2];	/* magic number */
264 		char volume_num[4];	/* volume number */
265 		char compressed;	/* files compressed = 1 */
266 		char date[12];		/* date of archive mmddhhmm */
267 		char start_of_name;	/* start of the filename */
268 	} dbuf;
269 };
270 
271 /* svr32 stat structure -- for -Hodc headers */
272 
273 typedef struct cpioinfo {
274 	o_dev_t st_dev;
275 	o_ino_t st_ino;
276 	o_mode_t	st_mode;
277 	o_nlink_t	st_nlink;
278 	uid_t st_uid;		/* actual uid */
279 	gid_t st_gid;		/* actual gid */
280 	o_dev_t st_rdev;
281 	off_t	st_size;
282 	time_t	st_modtime;
283 } cpioinfo_t;
284 
285 extern void msg(int severity, const char *fmt, ...);
286 extern void stat_to_svr32_stat(cpioinfo_t *TmpSt, struct stat *FromStat);
287 
288 /*
289  * Allocation wrappers and their flags
290  */
291 #define	E_NORMAL	0x0	/* Return NULL if allocation fails */
292 #define	E_EXIT		0x1	/* Exit if allocation fails */
293 
294 extern void *e_realloc(int flag, void *old, size_t newsize);
295 extern char *e_strdup(int flag, const char *arg);
296 extern void *e_valloc(int flag, size_t size);
297 extern void *e_zalloc(int flag, size_t size);
298 
299 /*
300  * If compiling on a system that doesn't
301  * support extended attributes, then
302  * define a couple of things so we can compile.
303  */
304 #if !defined(O_XATTR)
305 #define	AT_SYMLINK_NOFOLLOW	0x1000
306 #define	AT_REMOVEDIR		0x0001
307 #define	_XATTR_CPIO_MODE	0xB000
308 #define	_XATTR_HDRTYPE		'E'
309 #endif /* O_XATTR */
310 
311 /*
312  * Sparse file support
313  */
314 #define	MIN_HOLES_HDRSIZE	(UL_MAX_SIZE + 1 + ULL_MAX_SIZE + 1)
315 
316 typedef struct holes_list {
317 	off_t	hl_data;
318 	off_t	hl_hole;
319 	struct holes_list *hl_next;
320 } holes_list_t;
321 
322 typedef struct holes_info {
323 	holes_list_t	*holes_list;	/* linked list of holes_list */
324 	off_t		orig_size;	/* original file size */
325 	off_t		data_size;	/* compressed file size */
326 	char		*holesdata;	/* holesdata string */
327 	size_t		holesdata_sz;	/* string size */
328 } holes_info_t;
329 
330 extern	holes_info_t *get_holes_info(int, off_t, boolean_t);
331 extern	holes_info_t *read_holes_header(const char *, off_t);
332 extern	int	parse_holesdata(holes_info_t *, const char *);
333 extern	void	free_holes_info(holes_info_t *);
334 
335 extern	void	str_fprintf(FILE *, const char *, ...);
336 
337 #ifdef	__cplusplus
338 }
339 #endif
340 
341 #endif /* _CPIO_H */
342