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