17c478bdstevel@tonic-gate/*
27c478bdstevel@tonic-gate * CDDL HEADER START
37c478bdstevel@tonic-gate *
47c478bdstevel@tonic-gate * The contents of this file are subject to the terms of the
5da6c28aamw * Common Development and Distribution License (the "License").
6da6c28aamw * You may not use this file except in compliance with the License.
77c478bdstevel@tonic-gate *
87c478bdstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bdstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bdstevel@tonic-gate * See the License for the specific language governing permissions
117c478bdstevel@tonic-gate * and limitations under the License.
127c478bdstevel@tonic-gate *
137c478bdstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bdstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bdstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bdstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bdstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bdstevel@tonic-gate *
197c478bdstevel@tonic-gate * CDDL HEADER END
207c478bdstevel@tonic-gate */
217c478bdstevel@tonic-gate/*
22b0ee9efGary Mills * Copyright (c) 2012 Gary Mills
23b0ee9efGary Mills *
245fbb809Nobutomo Nakano * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
257c478bdstevel@tonic-gate * Use is subject to license terms.
267c478bdstevel@tonic-gate */
277c478bdstevel@tonic-gate
28da6c28aamw/*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
29da6c28aamw/*	  All Rights Reserved  	*/
30da6c28aamw
317c478bdstevel@tonic-gate#ifndef	_CPIO_H
327c478bdstevel@tonic-gate#define	_CPIO_H
337c478bdstevel@tonic-gate
347c478bdstevel@tonic-gate#ifdef	__cplusplus
357c478bdstevel@tonic-gateextern "C" {
367c478bdstevel@tonic-gate#endif
377c478bdstevel@tonic-gate
385fbb809Nobutomo Nakano#include <stdio.h>
395fbb809Nobutomo Nakano#include <archives.h>
405fbb809Nobutomo Nakano
417c478bdstevel@tonic-gate/* Option Character keys (OC#), where '#' is the option character specified. */
427c478bdstevel@tonic-gate
437c478bdstevel@tonic-gate#define	OCa	0x1
447c478bdstevel@tonic-gate#define	OCb	0x2
457c478bdstevel@tonic-gate#define	OCc	0x4
467c478bdstevel@tonic-gate#define	OCd	0x8
477c478bdstevel@tonic-gate#define	OCf	0x10
487c478bdstevel@tonic-gate#define	OCi	0x20
497c478bdstevel@tonic-gate#define	OCk	0x40
507c478bdstevel@tonic-gate#define	OCl	0x80
517c478bdstevel@tonic-gate#define	OCm	0x100
527c478bdstevel@tonic-gate#define	OCo	0x200
537c478bdstevel@tonic-gate#define	OCp	0x400
547c478bdstevel@tonic-gate#define	OCr	0x800
557c478bdstevel@tonic-gate#define	OCs	0x1000
567c478bdstevel@tonic-gate#define	OCt	0x2000
577c478bdstevel@tonic-gate#define	OCu	0x4000
587c478bdstevel@tonic-gate#define	OCv	0x8000
597c478bdstevel@tonic-gate#define	OCA	0x10000
607c478bdstevel@tonic-gate#define	OCB	0x20000
617c478bdstevel@tonic-gate#define	OCC	0x40000
627c478bdstevel@tonic-gate#define	OCE	0x80000
637c478bdstevel@tonic-gate#define	OCH	0x100000
647c478bdstevel@tonic-gate#define	OCI	0x200000
657c478bdstevel@tonic-gate#define	OCL	0x400000
667c478bdstevel@tonic-gate#define	OCM	0x800000
677c478bdstevel@tonic-gate#define	OCO	0x1000000
687c478bdstevel@tonic-gate#define	OCR	0x2000000
697c478bdstevel@tonic-gate#define	OCS	0x4000000
707c478bdstevel@tonic-gate#define	OCV	0x8000000
717c478bdstevel@tonic-gate#define	OC6	0x10000000
727c478bdstevel@tonic-gate#define	BSM	0x20000000
737c478bdstevel@tonic-gate#define	OCP	0x40000000
74b0ee9efGary Mills#define	OCq	0x80000000
757c478bdstevel@tonic-gate
765fbb809Nobutomo Nakano/* Sparse file support */
775fbb809Nobutomo Nakano#define	C_ISSPARSE	0200000
785fbb809Nobutomo Nakano#define	S_IFSPARSE	0x10000
795fbb809Nobutomo Nakano#define	HIGH_ORD_MASK	0x30000
805fbb809Nobutomo Nakano#define	S_ISSPARSE(mode) \
815fbb809Nobutomo Nakano	(S_ISREG(mode) && (mode & HIGH_ORD_MASK) == S_IFSPARSE)
825fbb809Nobutomo Nakano
837c478bdstevel@tonic-gate/* Invalid option masks for each action option (-i, -o or -p). */
847c478bdstevel@tonic-gate
857c478bdstevel@tonic-gate#define	INV_MSK4i	(OCo | OCp | OCA | OCL | OCO)
867c478bdstevel@tonic-gate
877c478bdstevel@tonic-gate#define	INV_MSK4o	(OCi | OCp | OCE | OCI | OCR)
887c478bdstevel@tonic-gate
897c478bdstevel@tonic-gate#define	INV_MSK4p	(OCf | OCi | OCo | OCr | OCt | OCA \
907c478bdstevel@tonic-gate			| OCE | OCH | OCI | OCO)
917c478bdstevel@tonic-gate
927c478bdstevel@tonic-gate/* Header types */
937c478bdstevel@tonic-gate
947c478bdstevel@tonic-gate#define	NONE	0	/* No header value verified */
957c478bdstevel@tonic-gate#define	BIN	1	/* Binary */
967c478bdstevel@tonic-gate#define	CHR	2	/* ASCII character (-c) */
977c478bdstevel@tonic-gate#define	ASC	3	/* ASCII with expanded maj/min numbers */
987c478bdstevel@tonic-gate#define	CRC	4	/* CRC with expanded maj/min numbers */
997c478bdstevel@tonic-gate#define	TARTYP	5	/* Tar or USTAR */
1007c478bdstevel@tonic-gate#define	SECURE	6	/* Secure system */
1017c478bdstevel@tonic-gate
1027c478bdstevel@tonic-gate/* Differentiate between TAR and USTAR */
1037c478bdstevel@tonic-gate
1047c478bdstevel@tonic-gate#define	TAR	7	/* Regular tar */
1057c478bdstevel@tonic-gate#define	USTAR	8	/* IEEE data interchange standard */
1067c478bdstevel@tonic-gate
1075fbb809Nobutomo Nakano#define	ULL_MAX_SIZE	20
1085fbb809Nobutomo Nakano#define	UL_MAX_SIZE	10
1095fbb809Nobutomo Nakano
1107c478bdstevel@tonic-gate/* constants for bar, used for extracting bar archives */
1117c478bdstevel@tonic-gate#define	BAR	9
1127c478bdstevel@tonic-gate#define	BAR_VOLUME_MAGIC	'V'
1137c478bdstevel@tonic-gate#define	BARTYP	7
1147c478bdstevel@tonic-gate#define	BARSZ	512
1157c478bdstevel@tonic-gate#define	BAR_TAPE_SIZE	(126*BARSZ)
1167c478bdstevel@tonic-gate#define	BAR_FLOPPY_SIZE	(18*BARSZ)
1177c478bdstevel@tonic-gate
1187c478bdstevel@tonic-gate/* the pathname lengths for the USTAR header */
1197c478bdstevel@tonic-gate
1207c478bdstevel@tonic-gate#define	MAXNAM	256	/* The maximum pathname length */
1217c478bdstevel@tonic-gate#define	NAMSIZ	100	/* The maximum length of the name field */
1227c478bdstevel@tonic-gate#define	PRESIZ	155	/* The maximum length of the prefix */
1237c478bdstevel@tonic-gate
1247c478bdstevel@tonic-gate/* HDRSZ: header size minus filename field length */
1257c478bdstevel@tonic-gate
1267c478bdstevel@tonic-gate#define	HDRSZ (Hdr.h_name - (char *)&Hdr)
1277c478bdstevel@tonic-gate
1287c478bdstevel@tonic-gate/*
1297c478bdstevel@tonic-gate * IDENT: Determine if two stat() structures represent identical files.
1307c478bdstevel@tonic-gate * Assumes that if the device and inode are the same the files are
1317c478bdstevel@tonic-gate * identical (prevents the archive file from appearing in the archive).
1327c478bdstevel@tonic-gate */
1337c478bdstevel@tonic-gate
1347c478bdstevel@tonic-gate#define	IDENT(a, b) ((a.st_ino == b.st_ino && a.st_dev == b.st_dev) ? 1 : 0)
1357c478bdstevel@tonic-gate
1367c478bdstevel@tonic-gate/*
1377c478bdstevel@tonic-gate * FLUSH: Determine if enough space remains in the buffer to hold
1387c478bdstevel@tonic-gate * cnt bytes, if not, call bflush() to flush the buffer to the archive.
1397c478bdstevel@tonic-gate */
1407c478bdstevel@tonic-gate
1417c478bdstevel@tonic-gate#define	FLUSH(cnt) if ((Buffr.b_end_p - Buffr.b_in_p) < cnt) bflush()
1427c478bdstevel@tonic-gate
1437c478bdstevel@tonic-gate/*
1447c478bdstevel@tonic-gate * FILL: Determine if enough bytes remain in the buffer to meet current needs,
1457c478bdstevel@tonic-gate * if not, call rstbuf() to reset and refill the buffer from the archive.
1467c478bdstevel@tonic-gate */
1477c478bdstevel@tonic-gate
1487c478bdstevel@tonic-gate#define	FILL(cnt) while (Buffr.b_cnt < cnt) rstbuf()
1497c478bdstevel@tonic-gate
1507c478bdstevel@tonic-gate/*
1517c478bdstevel@tonic-gate * VERBOSE: If x is non-zero, call verbose().
1527c478bdstevel@tonic-gate */
1537c478bdstevel@tonic-gate
1547c478bdstevel@tonic-gate#define	VERBOSE(x, name) if (x) verbose(name)
1557c478bdstevel@tonic-gate
1567c478bdstevel@tonic-gate/*
1577c478bdstevel@tonic-gate * FORMAT: Date time formats
1587c478bdstevel@tonic-gate * b - abbreviated month name
1597c478bdstevel@tonic-gate * e - day of month (1 - 31)
1607c478bdstevel@tonic-gate * H - hour (00 - 23)
1617c478bdstevel@tonic-gate * M - minute (00 - 59)
1627c478bdstevel@tonic-gate * Y - year as ccyy
1637c478bdstevel@tonic-gate */
1647c478bdstevel@tonic-gate
1657c478bdstevel@tonic-gate#define	FORMAT	"%b %e %H:%M %Y"
1667c478bdstevel@tonic-gate
167da6c28aamw/* Extended system attributes */
168da6c28aamw#ifndef	VIEW_READONLY
169da6c28aamw#define	VIEW_READONLY	"SUNWattr_ro"
170da6c28aamw#endif
171da6c28aamw
172da6c28aamw#ifndef	VIEW_READWRITE
173da6c28aamw#define	VIEW_READWRITE	"SUNWattr_rw"
174da6c28aamw#endif
175da6c28aamw
176ced83f9ceastha#define	min(a, b)	((a) < (b) ? (a) : (b))
177ced83f9ceastha
1787c478bdstevel@tonic-gate/* Values used in typeflag field */
1797c478bdstevel@tonic-gate#define	REGTYPE		'0'		/* Regular File */
1807c478bdstevel@tonic-gate#define	LNKTYPE		'1'		/* Link */
1817c478bdstevel@tonic-gate#define	SYMTYPE		'2'		/* Reserved */
1827c478bdstevel@tonic-gate#define	CHRTYPE		'3'		/* Character Special File */
1837c478bdstevel@tonic-gate#define	BLKTYPE		'4'		/* Block Special File */
1847c478bdstevel@tonic-gate#define	DIRTYPE		'5'		/* Directory */
1857c478bdstevel@tonic-gate#define	FIFOTYPE	'6'		/* FIFO */
1867c478bdstevel@tonic-gate#define	CONTTYPE	'7'		/* Reserved */
1877c478bdstevel@tonic-gate#define	XHDRTYPE	'X'		/* Extended header */
1887c478bdstevel@tonic-gate
1897c478bdstevel@tonic-gate#define	INPUT	0	/* -i mode (used for chgreel() */
1907c478bdstevel@tonic-gate#define	OUTPUT	1	/* -o mode (used for chgreel() */
1917c478bdstevel@tonic-gate#define	APATH	1024	/* maximum ASC or CRC header path length */
1927c478bdstevel@tonic-gate#define	CPATH	256	/* maximum -c and binary path length */
1937c478bdstevel@tonic-gate#define	BUFSZ	512	/* default buffer size for archive I/O */
1947c478bdstevel@tonic-gate#define	CPIOBSZ	8192	/* buffer size for file system I/O */
1957c478bdstevel@tonic-gate#define	LNK_INC	500	/* link allocation increment */
1967c478bdstevel@tonic-gate#define	MX_BUFS	10	/* max. number of buffers to allocate */
1977c478bdstevel@tonic-gate
1987c478bdstevel@tonic-gate#define	F_SKIP	0	/* an object did not match the patterns */
1997c478bdstevel@tonic-gate#define	F_LINK	1	/* linked file */
2007c478bdstevel@tonic-gate#define	F_EXTR	2	/* extract non-linked object that matched patterns */
2017c478bdstevel@tonic-gate
2027c478bdstevel@tonic-gate#define	MX_SEEKS	10	/* max. number of lseek attempts after error */
2037c478bdstevel@tonic-gate#define	SEEK_ABS	0	/* lseek absolute */
2047c478bdstevel@tonic-gate#define	SEEK_REL	1	/* lseek relative */
2057c478bdstevel@tonic-gate
2067c478bdstevel@tonic-gate/*
2077c478bdstevel@tonic-gate * xxx_CNT represents the number of sscanf items that will be matched
2087c478bdstevel@tonic-gate * if the sscanf to read a header is successful.  If sscanf returns a number
2097c478bdstevel@tonic-gate * that is not equal to this, an error occured (which indicates that this
2107c478bdstevel@tonic-gate * is not a valid header of the type assumed.
2117c478bdstevel@tonic-gate */
2127c478bdstevel@tonic-gate
2137c478bdstevel@tonic-gate#define	ASC_CNT	14	/* ASC and CRC headers */
2147c478bdstevel@tonic-gate#define	CHR_CNT	11	/* CHR header */
2157c478bdstevel@tonic-gate
2167c478bdstevel@tonic-gate/* These defines determine the severity of the message sent to the user. */
2177c478bdstevel@tonic-gate
2187c478bdstevel@tonic-gate#define	ERR	1	/* Error message (warning) - not fatal */
2197c478bdstevel@tonic-gate#define	EXT	2	/* Error message - fatal, causes exit */
2207c478bdstevel@tonic-gate#define	ERRN	3	/* Error message with errno (warning) - not fatal */
2217c478bdstevel@tonic-gate#define	EXTN	4	/* Error message with errno - fatal, causes exit */
2227c478bdstevel@tonic-gate#define	POST	5	/* Information message, not an error */
2237c478bdstevel@tonic-gate#define	EPOST	6	/* Information message to stderr */
2247c478bdstevel@tonic-gate
2257c478bdstevel@tonic-gate#define	SIXTH	060000	/* UNIX 6th edition files */
2267c478bdstevel@tonic-gate
2277c478bdstevel@tonic-gate#define	P_SKIP	0	/* File should be skipped */
2287c478bdstevel@tonic-gate#define	P_PROC	1	/* File should be processed */
2297c478bdstevel@tonic-gate
2307c478bdstevel@tonic-gate#define	U_KEEP	0	/* Keep the existing version of a file (-u) */
2317c478bdstevel@tonic-gate#define	U_OVER	1	/* Overwrite the existing version of a file (-u) */
2327c478bdstevel@tonic-gate
2337c478bdstevel@tonic-gate/*
2347c478bdstevel@tonic-gate * _20K: Allocate the maximum of (20K or (MX_BUFS * Bufsize)) bytes
2357c478bdstevel@tonic-gate * for the main I/O buffer.  Therefore if a user specifies a small buffer
2367c478bdstevel@tonic-gate * size, they still get decent performance due to the buffering strategy.
2377c478bdstevel@tonic-gate */
2387c478bdstevel@tonic-gate
2397c478bdstevel@tonic-gate#define	_20K	20480
2407c478bdstevel@tonic-gate
2417c478bdstevel@tonic-gate#define	HALFWD	1	/* Pad headers/data to halfword boundaries */
2427c478bdstevel@tonic-gate#define	FULLWD	3	/* Pad headers/data to word boundaries */
2437c478bdstevel@tonic-gate#define	FULLBK	511	/* Pad headers/data to 512 byte boundaries */
2447c478bdstevel@tonic-gate
2457c478bdstevel@tonic-gate/* bar structure */
2467c478bdstevel@tonic-gateunion b_block {
2477c478bdstevel@tonic-gate	char dummy[TBLOCK];
2487c478bdstevel@tonic-gate	struct bar_header {
2497c478bdstevel@tonic-gate		char mode[8];
2507c478bdstevel@tonic-gate		char uid[8];
2517c478bdstevel@tonic-gate		char gid[8];
2527c478bdstevel@tonic-gate		char size[12];
2537c478bdstevel@tonic-gate		char mtime[12];
2547c478bdstevel@tonic-gate		char chksum[8];
2557c478bdstevel@tonic-gate		char rdev[8];
2567c478bdstevel@tonic-gate		char linkflag;
2577c478bdstevel@tonic-gate
2587c478bdstevel@tonic-gate		/*
2597c478bdstevel@tonic-gate		 * The following fields are specific to the volume
2607c478bdstevel@tonic-gate		 * header.  They are set to zero in all file headers
2617c478bdstevel@tonic-gate		 * in the archive.
2627c478bdstevel@tonic-gate		 */
2637c478bdstevel@tonic-gate		char bar_magic[2];	/* magic number */
2647c478bdstevel@tonic-gate		char volume_num[4];	/* volume number */
2657c478bdstevel@tonic-gate		char compressed;	/* files compressed = 1 */
2667c478bdstevel@tonic-gate		char date[12];		/* date of archive mmddhhmm */
2677c478bdstevel@tonic-gate		char start_of_name;	/* start of the filename */
2687c478bdstevel@tonic-gate	} dbuf;
2697c478bdstevel@tonic-gate};
2707c478bdstevel@tonic-gate
2717c478bdstevel@tonic-gate/* svr32 stat structure -- for -Hodc headers */
2727c478bdstevel@tonic-gate
2737c478bdstevel@tonic-gatetypedef struct cpioinfo {
2747c478bdstevel@tonic-gate	o_dev_t st_dev;
2757c478bdstevel@tonic-gate	o_ino_t st_ino;
2767c478bdstevel@tonic-gate	o_mode_t	st_mode;
2777c478bdstevel@tonic-gate	o_nlink_t	st_nlink;
2787c478bdstevel@tonic-gate	uid_t st_uid;		/* actual uid */
2797c478bdstevel@tonic-gate	gid_t st_gid;		/* actual gid */
2807c478bdstevel@tonic-gate	o_dev_t st_rdev;
2817c478bdstevel@tonic-gate	off_t	st_size;
2827c478bdstevel@tonic-gate	time_t	st_modtime;
2837c478bdstevel@tonic-gate} cpioinfo_t;
2847c478bdstevel@tonic-gate
2855fbb809Nobutomo Nakanoextern void msg(int severity, const char *fmt, ...);
2865fbb809Nobutomo Nakanoextern void stat_to_svr32_stat(cpioinfo_t *TmpSt, struct stat *FromStat);
2875fbb809Nobutomo Nakano
2885fbb809Nobutomo Nakano/*
2895fbb809Nobutomo Nakano * Allocation wrappers and their flags
2905fbb809Nobutomo Nakano */
2915fbb809Nobutomo Nakano#define	E_NORMAL	0x0	/* Return NULL if allocation fails */
2925fbb809Nobutomo Nakano#define	E_EXIT		0x1	/* Exit if allocation fails */
2935fbb809Nobutomo Nakano
2945fbb809Nobutomo Nakanoextern void *e_realloc(int flag, void *old, size_t newsize);
2955fbb809Nobutomo Nakanoextern char *e_strdup(int flag, const char *arg);
2965fbb809Nobutomo Nakanoextern void *e_valloc(int flag, size_t size);
2975fbb809Nobutomo Nakanoextern void *e_zalloc(int flag, size_t size);
2987c478bdstevel@tonic-gate
2997c478bdstevel@tonic-gate/*
3007c478bdstevel@tonic-gate * If compiling on a system that doesn't
3017c478bdstevel@tonic-gate * support extended attributes, then
3027c478bdstevel@tonic-gate * define a couple of things so we can compile.
3037c478bdstevel@tonic-gate */
3047c478bdstevel@tonic-gate#if !defined(O_XATTR)
3057c478bdstevel@tonic-gate#define	AT_SYMLINK_NOFOLLOW	0x1000
3067c478bdstevel@tonic-gate#define	AT_REMOVEDIR		0x0001
3077c478bdstevel@tonic-gate#define	_XATTR_CPIO_MODE	0xB000
3087c478bdstevel@tonic-gate#define	_XATTR_HDRTYPE		'E'
3097c478bdstevel@tonic-gate#endif /* O_XATTR */
3107c478bdstevel@tonic-gate
3115fbb809Nobutomo Nakano/*
3125fbb809Nobutomo Nakano * Sparse file support
3135fbb809Nobutomo Nakano */
3145fbb809Nobutomo Nakano#define	MIN_HOLES_HDRSIZE	(UL_MAX_SIZE + 1 + ULL_MAX_SIZE + 1)
3155fbb809Nobutomo Nakano
3165fbb809Nobutomo Nakanotypedef struct holes_list {
3175fbb809Nobutomo Nakano	off_t	hl_data;
3185fbb809Nobutomo Nakano	off_t	hl_hole;
3195fbb809Nobutomo Nakano	struct holes_list *hl_next;
3205fbb809Nobutomo Nakano} holes_list_t;
3215fbb809Nobutomo Nakano
3225fbb809Nobutomo Nakanotypedef struct holes_info {
3235fbb809Nobutomo Nakano	holes_list_t	*holes_list;	/* linked list of holes_list */
3245fbb809Nobutomo Nakano	off_t		orig_size;	/* original file size */
3255fbb809Nobutomo Nakano	off_t		data_size;	/* compressed file size */
3265fbb809Nobutomo Nakano	char		*holesdata;	/* holesdata string */
3275fbb809Nobutomo Nakano	size_t		holesdata_sz;	/* string size */
3285fbb809Nobutomo Nakano} holes_info_t;
3295fbb809Nobutomo Nakano
3305fbb809Nobutomo Nakanoextern	holes_info_t *get_holes_info(int, off_t, boolean_t);
3315fbb809Nobutomo Nakanoextern	holes_info_t *read_holes_header(const char *, off_t);
3325fbb809Nobutomo Nakanoextern	int	parse_holesdata(holes_info_t *, const char *);
3335fbb809Nobutomo Nakanoextern	void	free_holes_info(holes_info_t *);
3345fbb809Nobutomo Nakano
3355fbb809Nobutomo Nakanoextern	void	str_fprintf(FILE *, const char *, ...);
3365fbb809Nobutomo Nakano
3377c478bdstevel@tonic-gate#ifdef	__cplusplus
3387c478bdstevel@tonic-gate}
3397c478bdstevel@tonic-gate#endif
3407c478bdstevel@tonic-gate
3417c478bdstevel@tonic-gate#endif /* _CPIO_H */
342