xref: /onnv-gate/usr/src/cmd/cpio/cpio.h (revision 10202:dd8df7c8036e)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
55331Samw  * Common Development and Distribution License (the "License").
65331Samw  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
215331Samw /*
22*10202SNobutomo.Nakano@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
235331Samw  * Use is subject to license terms.
245331Samw  */
255331Samw 
260Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
270Sstevel@tonic-gate /*	  All Rights Reserved  	*/
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #ifndef	_CPIO_H
300Sstevel@tonic-gate #define	_CPIO_H
310Sstevel@tonic-gate 
320Sstevel@tonic-gate #ifdef	__cplusplus
330Sstevel@tonic-gate extern "C" {
340Sstevel@tonic-gate #endif
350Sstevel@tonic-gate 
36*10202SNobutomo.Nakano@Sun.COM #include <stdio.h>
37*10202SNobutomo.Nakano@Sun.COM #include <archives.h>
38*10202SNobutomo.Nakano@Sun.COM 
390Sstevel@tonic-gate /* Option Character keys (OC#), where '#' is the option character specified. */
400Sstevel@tonic-gate 
410Sstevel@tonic-gate #define	OCa	0x1
420Sstevel@tonic-gate #define	OCb	0x2
430Sstevel@tonic-gate #define	OCc	0x4
440Sstevel@tonic-gate #define	OCd	0x8
450Sstevel@tonic-gate #define	OCf	0x10
460Sstevel@tonic-gate #define	OCi	0x20
470Sstevel@tonic-gate #define	OCk	0x40
480Sstevel@tonic-gate #define	OCl	0x80
490Sstevel@tonic-gate #define	OCm	0x100
500Sstevel@tonic-gate #define	OCo	0x200
510Sstevel@tonic-gate #define	OCp	0x400
520Sstevel@tonic-gate #define	OCr	0x800
530Sstevel@tonic-gate #define	OCs	0x1000
540Sstevel@tonic-gate #define	OCt	0x2000
550Sstevel@tonic-gate #define	OCu	0x4000
560Sstevel@tonic-gate #define	OCv	0x8000
570Sstevel@tonic-gate #define	OCA	0x10000
580Sstevel@tonic-gate #define	OCB	0x20000
590Sstevel@tonic-gate #define	OCC	0x40000
600Sstevel@tonic-gate #define	OCE	0x80000
610Sstevel@tonic-gate #define	OCH	0x100000
620Sstevel@tonic-gate #define	OCI	0x200000
630Sstevel@tonic-gate #define	OCL	0x400000
640Sstevel@tonic-gate #define	OCM	0x800000
650Sstevel@tonic-gate #define	OCO	0x1000000
660Sstevel@tonic-gate #define	OCR	0x2000000
670Sstevel@tonic-gate #define	OCS	0x4000000
680Sstevel@tonic-gate #define	OCV	0x8000000
690Sstevel@tonic-gate #define	OC6	0x10000000
700Sstevel@tonic-gate #define	BSM	0x20000000
710Sstevel@tonic-gate #define	OCP	0x40000000
720Sstevel@tonic-gate 
73*10202SNobutomo.Nakano@Sun.COM /* Sparse file support */
74*10202SNobutomo.Nakano@Sun.COM #define	C_ISSPARSE	0200000
75*10202SNobutomo.Nakano@Sun.COM #define	S_IFSPARSE	0x10000
76*10202SNobutomo.Nakano@Sun.COM #define	HIGH_ORD_MASK	0x30000
77*10202SNobutomo.Nakano@Sun.COM #define	S_ISSPARSE(mode) \
78*10202SNobutomo.Nakano@Sun.COM 	(S_ISREG(mode) && (mode & HIGH_ORD_MASK) == S_IFSPARSE)
79*10202SNobutomo.Nakano@Sun.COM 
800Sstevel@tonic-gate /* Invalid option masks for each action option (-i, -o or -p). */
810Sstevel@tonic-gate 
820Sstevel@tonic-gate #define	INV_MSK4i	(OCo | OCp | OCA | OCL | OCO)
830Sstevel@tonic-gate 
840Sstevel@tonic-gate #define	INV_MSK4o	(OCi | OCp | OCE | OCI | OCR)
850Sstevel@tonic-gate 
860Sstevel@tonic-gate #define	INV_MSK4p	(OCf | OCi | OCo | OCr | OCt | OCA \
870Sstevel@tonic-gate 			| OCE | OCH | OCI | OCO)
880Sstevel@tonic-gate 
890Sstevel@tonic-gate /* Header types */
900Sstevel@tonic-gate 
910Sstevel@tonic-gate #define	NONE	0	/* No header value verified */
920Sstevel@tonic-gate #define	BIN	1	/* Binary */
930Sstevel@tonic-gate #define	CHR	2	/* ASCII character (-c) */
940Sstevel@tonic-gate #define	ASC	3	/* ASCII with expanded maj/min numbers */
950Sstevel@tonic-gate #define	CRC	4	/* CRC with expanded maj/min numbers */
960Sstevel@tonic-gate #define	TARTYP	5	/* Tar or USTAR */
970Sstevel@tonic-gate #define	SECURE	6	/* Secure system */
980Sstevel@tonic-gate 
990Sstevel@tonic-gate /* Differentiate between TAR and USTAR */
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate #define	TAR	7	/* Regular tar */
1020Sstevel@tonic-gate #define	USTAR	8	/* IEEE data interchange standard */
1030Sstevel@tonic-gate 
104*10202SNobutomo.Nakano@Sun.COM #define	ULL_MAX_SIZE	20
105*10202SNobutomo.Nakano@Sun.COM #define	UL_MAX_SIZE	10
106*10202SNobutomo.Nakano@Sun.COM 
1070Sstevel@tonic-gate /* constants for bar, used for extracting bar archives */
1080Sstevel@tonic-gate #define	BAR	9
1090Sstevel@tonic-gate #define	BAR_VOLUME_MAGIC	'V'
1100Sstevel@tonic-gate #define	BARTYP	7
1110Sstevel@tonic-gate #define	BARSZ	512
1120Sstevel@tonic-gate #define	BAR_TAPE_SIZE	(126*BARSZ)
1130Sstevel@tonic-gate #define	BAR_FLOPPY_SIZE	(18*BARSZ)
1140Sstevel@tonic-gate 
1150Sstevel@tonic-gate /* the pathname lengths for the USTAR header */
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate #define	MAXNAM	256	/* The maximum pathname length */
1180Sstevel@tonic-gate #define	NAMSIZ	100	/* The maximum length of the name field */
1190Sstevel@tonic-gate #define	PRESIZ	155	/* The maximum length of the prefix */
1200Sstevel@tonic-gate 
1210Sstevel@tonic-gate /* HDRSZ: header size minus filename field length */
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate #define	HDRSZ (Hdr.h_name - (char *)&Hdr)
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate /*
1260Sstevel@tonic-gate  * IDENT: Determine if two stat() structures represent identical files.
1270Sstevel@tonic-gate  * Assumes that if the device and inode are the same the files are
1280Sstevel@tonic-gate  * identical (prevents the archive file from appearing in the archive).
1290Sstevel@tonic-gate  */
1300Sstevel@tonic-gate 
1310Sstevel@tonic-gate #define	IDENT(a, b) ((a.st_ino == b.st_ino && a.st_dev == b.st_dev) ? 1 : 0)
1320Sstevel@tonic-gate 
1330Sstevel@tonic-gate /*
1340Sstevel@tonic-gate  * FLUSH: Determine if enough space remains in the buffer to hold
1350Sstevel@tonic-gate  * cnt bytes, if not, call bflush() to flush the buffer to the archive.
1360Sstevel@tonic-gate  */
1370Sstevel@tonic-gate 
1380Sstevel@tonic-gate #define	FLUSH(cnt) if ((Buffr.b_end_p - Buffr.b_in_p) < cnt) bflush()
1390Sstevel@tonic-gate 
1400Sstevel@tonic-gate /*
1410Sstevel@tonic-gate  * FILL: Determine if enough bytes remain in the buffer to meet current needs,
1420Sstevel@tonic-gate  * if not, call rstbuf() to reset and refill the buffer from the archive.
1430Sstevel@tonic-gate  */
1440Sstevel@tonic-gate 
1450Sstevel@tonic-gate #define	FILL(cnt) while (Buffr.b_cnt < cnt) rstbuf()
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate /*
1480Sstevel@tonic-gate  * VERBOSE: If x is non-zero, call verbose().
1490Sstevel@tonic-gate  */
1500Sstevel@tonic-gate 
1510Sstevel@tonic-gate #define	VERBOSE(x, name) if (x) verbose(name)
1520Sstevel@tonic-gate 
1530Sstevel@tonic-gate /*
1540Sstevel@tonic-gate  * FORMAT: Date time formats
1550Sstevel@tonic-gate  * b - abbreviated month name
1560Sstevel@tonic-gate  * e - day of month (1 - 31)
1570Sstevel@tonic-gate  * H - hour (00 - 23)
1580Sstevel@tonic-gate  * M - minute (00 - 59)
1590Sstevel@tonic-gate  * Y - year as ccyy
1600Sstevel@tonic-gate  */
1610Sstevel@tonic-gate 
1620Sstevel@tonic-gate #define	FORMAT	"%b %e %H:%M %Y"
1630Sstevel@tonic-gate 
1645331Samw /* Extended system attributes */
1655331Samw #ifndef	VIEW_READONLY
1665331Samw #define	VIEW_READONLY	"SUNWattr_ro"
1675331Samw #endif
1685331Samw 
1695331Samw #ifndef	VIEW_READWRITE
1705331Samw #define	VIEW_READWRITE	"SUNWattr_rw"
1715331Samw #endif
1725331Samw 
1735750Sceastha #define	min(a, b)	((a) < (b) ? (a) : (b))
1745750Sceastha 
1750Sstevel@tonic-gate /* Values used in typeflag field */
1760Sstevel@tonic-gate #define	REGTYPE		'0'		/* Regular File */
1770Sstevel@tonic-gate #define	LNKTYPE		'1'		/* Link */
1780Sstevel@tonic-gate #define	SYMTYPE		'2'		/* Reserved */
1790Sstevel@tonic-gate #define	CHRTYPE		'3'		/* Character Special File */
1800Sstevel@tonic-gate #define	BLKTYPE		'4'		/* Block Special File */
1810Sstevel@tonic-gate #define	DIRTYPE		'5'		/* Directory */
1820Sstevel@tonic-gate #define	FIFOTYPE	'6'		/* FIFO */
1830Sstevel@tonic-gate #define	CONTTYPE	'7'		/* Reserved */
1840Sstevel@tonic-gate #define	XHDRTYPE	'X'		/* Extended header */
1850Sstevel@tonic-gate 
1860Sstevel@tonic-gate #define	INPUT	0	/* -i mode (used for chgreel() */
1870Sstevel@tonic-gate #define	OUTPUT	1	/* -o mode (used for chgreel() */
1880Sstevel@tonic-gate #define	APATH	1024	/* maximum ASC or CRC header path length */
1890Sstevel@tonic-gate #define	CPATH	256	/* maximum -c and binary path length */
1900Sstevel@tonic-gate #define	BUFSZ	512	/* default buffer size for archive I/O */
1910Sstevel@tonic-gate #define	CPIOBSZ	8192	/* buffer size for file system I/O */
1920Sstevel@tonic-gate #define	LNK_INC	500	/* link allocation increment */
1930Sstevel@tonic-gate #define	MX_BUFS	10	/* max. number of buffers to allocate */
1940Sstevel@tonic-gate 
1950Sstevel@tonic-gate #define	F_SKIP	0	/* an object did not match the patterns */
1960Sstevel@tonic-gate #define	F_LINK	1	/* linked file */
1970Sstevel@tonic-gate #define	F_EXTR	2	/* extract non-linked object that matched patterns */
1980Sstevel@tonic-gate 
1990Sstevel@tonic-gate #define	MX_SEEKS	10	/* max. number of lseek attempts after error */
2000Sstevel@tonic-gate #define	SEEK_ABS	0	/* lseek absolute */
2010Sstevel@tonic-gate #define	SEEK_REL	1	/* lseek relative */
2020Sstevel@tonic-gate 
2030Sstevel@tonic-gate /*
2040Sstevel@tonic-gate  * xxx_CNT represents the number of sscanf items that will be matched
2050Sstevel@tonic-gate  * if the sscanf to read a header is successful.  If sscanf returns a number
2060Sstevel@tonic-gate  * that is not equal to this, an error occured (which indicates that this
2070Sstevel@tonic-gate  * is not a valid header of the type assumed.
2080Sstevel@tonic-gate  */
2090Sstevel@tonic-gate 
2100Sstevel@tonic-gate #define	ASC_CNT	14	/* ASC and CRC headers */
2110Sstevel@tonic-gate #define	CHR_CNT	11	/* CHR header */
2120Sstevel@tonic-gate 
2130Sstevel@tonic-gate /* These defines determine the severity of the message sent to the user. */
2140Sstevel@tonic-gate 
2150Sstevel@tonic-gate #define	ERR	1	/* Error message (warning) - not fatal */
2160Sstevel@tonic-gate #define	EXT	2	/* Error message - fatal, causes exit */
2170Sstevel@tonic-gate #define	ERRN	3	/* Error message with errno (warning) - not fatal */
2180Sstevel@tonic-gate #define	EXTN	4	/* Error message with errno - fatal, causes exit */
2190Sstevel@tonic-gate #define	POST	5	/* Information message, not an error */
2200Sstevel@tonic-gate #define	EPOST	6	/* Information message to stderr */
2210Sstevel@tonic-gate 
2220Sstevel@tonic-gate #define	SIXTH	060000	/* UNIX 6th edition files */
2230Sstevel@tonic-gate 
2240Sstevel@tonic-gate #define	P_SKIP	0	/* File should be skipped */
2250Sstevel@tonic-gate #define	P_PROC	1	/* File should be processed */
2260Sstevel@tonic-gate 
2270Sstevel@tonic-gate #define	U_KEEP	0	/* Keep the existing version of a file (-u) */
2280Sstevel@tonic-gate #define	U_OVER	1	/* Overwrite the existing version of a file (-u) */
2290Sstevel@tonic-gate 
2300Sstevel@tonic-gate /*
2310Sstevel@tonic-gate  * _20K: Allocate the maximum of (20K or (MX_BUFS * Bufsize)) bytes
2320Sstevel@tonic-gate  * for the main I/O buffer.  Therefore if a user specifies a small buffer
2330Sstevel@tonic-gate  * size, they still get decent performance due to the buffering strategy.
2340Sstevel@tonic-gate  */
2350Sstevel@tonic-gate 
2360Sstevel@tonic-gate #define	_20K	20480
2370Sstevel@tonic-gate 
2380Sstevel@tonic-gate #define	HALFWD	1	/* Pad headers/data to halfword boundaries */
2390Sstevel@tonic-gate #define	FULLWD	3	/* Pad headers/data to word boundaries */
2400Sstevel@tonic-gate #define	FULLBK	511	/* Pad headers/data to 512 byte boundaries */
2410Sstevel@tonic-gate 
2420Sstevel@tonic-gate /* bar structure */
2430Sstevel@tonic-gate union b_block {
2440Sstevel@tonic-gate 	char dummy[TBLOCK];
2450Sstevel@tonic-gate 	struct bar_header {
2460Sstevel@tonic-gate 		char mode[8];
2470Sstevel@tonic-gate 		char uid[8];
2480Sstevel@tonic-gate 		char gid[8];
2490Sstevel@tonic-gate 		char size[12];
2500Sstevel@tonic-gate 		char mtime[12];
2510Sstevel@tonic-gate 		char chksum[8];
2520Sstevel@tonic-gate 		char rdev[8];
2530Sstevel@tonic-gate 		char linkflag;
2540Sstevel@tonic-gate 
2550Sstevel@tonic-gate 		/*
2560Sstevel@tonic-gate 		 * The following fields are specific to the volume
2570Sstevel@tonic-gate 		 * header.  They are set to zero in all file headers
2580Sstevel@tonic-gate 		 * in the archive.
2590Sstevel@tonic-gate 		 */
2600Sstevel@tonic-gate 		char bar_magic[2];	/* magic number */
2610Sstevel@tonic-gate 		char volume_num[4];	/* volume number */
2620Sstevel@tonic-gate 		char compressed;	/* files compressed = 1 */
2630Sstevel@tonic-gate 		char date[12];		/* date of archive mmddhhmm */
2640Sstevel@tonic-gate 		char start_of_name;	/* start of the filename */
2650Sstevel@tonic-gate 	} dbuf;
2660Sstevel@tonic-gate };
2670Sstevel@tonic-gate 
2680Sstevel@tonic-gate /* svr32 stat structure -- for -Hodc headers */
2690Sstevel@tonic-gate 
2700Sstevel@tonic-gate typedef struct cpioinfo {
2710Sstevel@tonic-gate 	o_dev_t st_dev;
2720Sstevel@tonic-gate 	o_ino_t st_ino;
2730Sstevel@tonic-gate 	o_mode_t	st_mode;
2740Sstevel@tonic-gate 	o_nlink_t	st_nlink;
2750Sstevel@tonic-gate 	uid_t st_uid;		/* actual uid */
2760Sstevel@tonic-gate 	gid_t st_gid;		/* actual gid */
2770Sstevel@tonic-gate 	o_dev_t st_rdev;
2780Sstevel@tonic-gate 	off_t	st_size;
2790Sstevel@tonic-gate 	time_t	st_modtime;
2800Sstevel@tonic-gate } cpioinfo_t;
2810Sstevel@tonic-gate 
282*10202SNobutomo.Nakano@Sun.COM extern void msg(int severity, const char *fmt, ...);
283*10202SNobutomo.Nakano@Sun.COM extern void stat_to_svr32_stat(cpioinfo_t *TmpSt, struct stat *FromStat);
284*10202SNobutomo.Nakano@Sun.COM 
285*10202SNobutomo.Nakano@Sun.COM /*
286*10202SNobutomo.Nakano@Sun.COM  * Allocation wrappers and their flags
287*10202SNobutomo.Nakano@Sun.COM  */
288*10202SNobutomo.Nakano@Sun.COM #define	E_NORMAL	0x0	/* Return NULL if allocation fails */
289*10202SNobutomo.Nakano@Sun.COM #define	E_EXIT		0x1	/* Exit if allocation fails */
290*10202SNobutomo.Nakano@Sun.COM 
291*10202SNobutomo.Nakano@Sun.COM extern void *e_realloc(int flag, void *old, size_t newsize);
292*10202SNobutomo.Nakano@Sun.COM extern char *e_strdup(int flag, const char *arg);
293*10202SNobutomo.Nakano@Sun.COM extern void *e_valloc(int flag, size_t size);
294*10202SNobutomo.Nakano@Sun.COM extern void *e_zalloc(int flag, size_t size);
2950Sstevel@tonic-gate 
2960Sstevel@tonic-gate /*
2970Sstevel@tonic-gate  * If compiling on a system that doesn't
2980Sstevel@tonic-gate  * support extended attributes, then
2990Sstevel@tonic-gate  * define a couple of things so we can compile.
3000Sstevel@tonic-gate  */
3010Sstevel@tonic-gate #if !defined(O_XATTR)
3020Sstevel@tonic-gate #define	AT_SYMLINK_NOFOLLOW	0x1000
3030Sstevel@tonic-gate #define	AT_REMOVEDIR		0x0001
3040Sstevel@tonic-gate #define	_XATTR_CPIO_MODE	0xB000
3050Sstevel@tonic-gate #define	_XATTR_HDRTYPE		'E'
3060Sstevel@tonic-gate #endif /* O_XATTR */
3070Sstevel@tonic-gate 
308*10202SNobutomo.Nakano@Sun.COM /*
309*10202SNobutomo.Nakano@Sun.COM  * Sparse file support
310*10202SNobutomo.Nakano@Sun.COM  */
311*10202SNobutomo.Nakano@Sun.COM #define	MIN_HOLES_HDRSIZE	(UL_MAX_SIZE + 1 + ULL_MAX_SIZE + 1)
312*10202SNobutomo.Nakano@Sun.COM 
313*10202SNobutomo.Nakano@Sun.COM typedef struct holes_list {
314*10202SNobutomo.Nakano@Sun.COM 	off_t	hl_data;
315*10202SNobutomo.Nakano@Sun.COM 	off_t	hl_hole;
316*10202SNobutomo.Nakano@Sun.COM 	struct holes_list *hl_next;
317*10202SNobutomo.Nakano@Sun.COM } holes_list_t;
318*10202SNobutomo.Nakano@Sun.COM 
319*10202SNobutomo.Nakano@Sun.COM typedef struct holes_info {
320*10202SNobutomo.Nakano@Sun.COM 	holes_list_t	*holes_list;	/* linked list of holes_list */
321*10202SNobutomo.Nakano@Sun.COM 	off_t		orig_size;	/* original file size */
322*10202SNobutomo.Nakano@Sun.COM 	off_t		data_size;	/* compressed file size */
323*10202SNobutomo.Nakano@Sun.COM 	char		*holesdata;	/* holesdata string */
324*10202SNobutomo.Nakano@Sun.COM 	size_t		holesdata_sz;	/* string size */
325*10202SNobutomo.Nakano@Sun.COM } holes_info_t;
326*10202SNobutomo.Nakano@Sun.COM 
327*10202SNobutomo.Nakano@Sun.COM extern	holes_info_t *get_holes_info(int, off_t, boolean_t);
328*10202SNobutomo.Nakano@Sun.COM extern	holes_info_t *read_holes_header(const char *, off_t);
329*10202SNobutomo.Nakano@Sun.COM extern	int	parse_holesdata(holes_info_t *, const char *);
330*10202SNobutomo.Nakano@Sun.COM extern	void	free_holes_info(holes_info_t *);
331*10202SNobutomo.Nakano@Sun.COM 
332*10202SNobutomo.Nakano@Sun.COM extern	void	str_fprintf(FILE *, const char *, ...);
333*10202SNobutomo.Nakano@Sun.COM 
3340Sstevel@tonic-gate #ifdef	__cplusplus
3350Sstevel@tonic-gate }
3360Sstevel@tonic-gate #endif
3370Sstevel@tonic-gate 
3380Sstevel@tonic-gate #endif /* _CPIO_H */
339