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