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
53919Sceastha * Common Development and Distribution License (the "License").
63919Sceastha * 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 */
210Sstevel@tonic-gate /*
22*12835Srich.burridge@oracle.com * Copyright (c) 1988, 2010, Oracle and/or its affiliates. All rights reserved.
230Sstevel@tonic-gate */
240Sstevel@tonic-gate
250Sstevel@tonic-gate /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
260Sstevel@tonic-gate /* All Rights Reserved */
270Sstevel@tonic-gate
280Sstevel@tonic-gate /*
290Sstevel@tonic-gate * Portions of this source code were derived from Berkeley 4.3 BSD
300Sstevel@tonic-gate * under license from the Regents of the University of California.
310Sstevel@tonic-gate */
320Sstevel@tonic-gate
330Sstevel@tonic-gate #include <stdio.h>
340Sstevel@tonic-gate #include <sys/types.h>
350Sstevel@tonic-gate #include <errno.h>
360Sstevel@tonic-gate #include <unistd.h>
370Sstevel@tonic-gate #include <stdlib.h>
380Sstevel@tonic-gate #include <fcntl.h>
390Sstevel@tonic-gate #include <memory.h>
400Sstevel@tonic-gate #include <string.h>
410Sstevel@tonic-gate #include <stdarg.h>
420Sstevel@tonic-gate #include <sys/stat.h>
430Sstevel@tonic-gate #include <sys/statvfs.h>
440Sstevel@tonic-gate #include <sys/mkdev.h>
450Sstevel@tonic-gate #include <sys/param.h>
460Sstevel@tonic-gate #include <utime.h>
470Sstevel@tonic-gate #include <pwd.h>
480Sstevel@tonic-gate #include <grp.h>
490Sstevel@tonic-gate #include <signal.h>
500Sstevel@tonic-gate #include <ctype.h>
510Sstevel@tonic-gate #include <locale.h>
520Sstevel@tonic-gate #include <sys/ioctl.h>
530Sstevel@tonic-gate #include <sys/mtio.h>
540Sstevel@tonic-gate #include <sys/fdio.h>
550Sstevel@tonic-gate #include "cpio.h"
560Sstevel@tonic-gate #include <sys/acl.h>
570Sstevel@tonic-gate #include <sys/time.h>
580Sstevel@tonic-gate #include <sys/resource.h>
590Sstevel@tonic-gate #include <fnmatch.h>
600Sstevel@tonic-gate #include <libgen.h>
610Sstevel@tonic-gate #include <libintl.h>
620Sstevel@tonic-gate #include <dirent.h>
630Sstevel@tonic-gate #include <limits.h>
64789Sahrens #include <aclutils.h>
655750Sceastha #if defined(_PC_SATTR_ENABLED)
665750Sceastha #include <libnvpair.h>
675750Sceastha #include <attr.h>
685750Sceastha #include <libcmdutils.h>
695750Sceastha #endif /* _PC_SATTR_ENABLED */
701134Sceastha #ifdef SOLARIS_PRIVS
711134Sceastha #include <priv.h>
721134Sceastha #endif /* SOLARIS_PRIVS */
730Sstevel@tonic-gate
740Sstevel@tonic-gate /*
750Sstevel@tonic-gate * Special kludge for off_t being a signed quantity.
760Sstevel@tonic-gate */
770Sstevel@tonic-gate #if _FILE_OFFSET_BITS == 64
780Sstevel@tonic-gate typedef u_longlong_t u_off_t;
790Sstevel@tonic-gate #else
800Sstevel@tonic-gate typedef ulong_t u_off_t;
810Sstevel@tonic-gate #endif
820Sstevel@tonic-gate
830Sstevel@tonic-gate #define SECMODE 0xe080
840Sstevel@tonic-gate
850Sstevel@tonic-gate #define DEVNULL "/dev/null"
860Sstevel@tonic-gate #define XATTRHDR ".hdr"
870Sstevel@tonic-gate
880Sstevel@tonic-gate #define NAMELEN 32
890Sstevel@tonic-gate #define TYPELEN 16
900Sstevel@tonic-gate #define PERMLEN 4
910Sstevel@tonic-gate
920Sstevel@tonic-gate #define FILE_COPIED 1
930Sstevel@tonic-gate #define FILE_LINKED 2
940Sstevel@tonic-gate #define FILE_PASS_ERR -1
950Sstevel@tonic-gate
960Sstevel@tonic-gate #define ARCHIVE_NORMAL 0
970Sstevel@tonic-gate #define ARCHIVE_ACL 1
980Sstevel@tonic-gate #define ARCHIVE_XATTR 2
9910202SNobutomo.Nakano@Sun.COM #define ARCHIVE_SPARSE 3
1000Sstevel@tonic-gate
1015750Sceastha #ifndef VIEW_READONLY
1025750Sceastha #define VIEW_READONLY "SUNWattr_ro"
1035750Sceastha #endif
1045750Sceastha
1055750Sceastha #ifndef VIEW_READWRITE
1065750Sceastha #define VIEW_READWRITE "SUNWattr_rw"
1075750Sceastha #endif
1085750Sceastha
1095750Sceastha
1100Sstevel@tonic-gate #define LSTAT(dir, path, statbuf) fstatat(dir, \
1115750Sceastha get_component((Gen.g_attrnam_p == NULL) ? \
1120Sstevel@tonic-gate path : Gen.g_attrnam_p), statbuf, AT_SYMLINK_NOFOLLOW)
1130Sstevel@tonic-gate #define STAT(dir, path, statbuf) fstatat(dir, \
1145750Sceastha get_component((Gen.g_attrnam_p == NULL) ? \
1150Sstevel@tonic-gate path : Gen.g_attrnam_p), statbuf, 0)
1160Sstevel@tonic-gate
1170Sstevel@tonic-gate /*
1180Sstevel@tonic-gate * These limits reflect the maximum size regular file that
1190Sstevel@tonic-gate * can be archived, depending on the archive type. For archives
1200Sstevel@tonic-gate * with character-format headers (odc, tar, ustar) we use
12136Schin * CHAR_OFFSET_MAX. For archives with SVR4 ASCII headers (-c, -H crc)
12236Schin * we store filesize in an 8-char hexadecimal string and use
12336Schin * ASC_OFFSET_MAX. Otherwise, we are limited to the size that will
12436Schin * fit in a signed long value.
1250Sstevel@tonic-gate */
12636Schin #define CHAR_OFFSET_MAX 077777777777ULL /* 11 octal digits */
12736Schin #define ASC_OFFSET_MAX 0XFFFFFFFF /* 8 hexadecimal digits */
1280Sstevel@tonic-gate #define BIN_OFFSET_MAX LONG_MAX /* signed long max value */
1290Sstevel@tonic-gate
1301231Smarks #define POSIXMODES 07777
1311231Smarks
1320Sstevel@tonic-gate static char aclchar = ' ';
1330Sstevel@tonic-gate
1340Sstevel@tonic-gate static struct Lnk *add_lnk(struct Lnk **);
1350Sstevel@tonic-gate static int bfill(void);
1360Sstevel@tonic-gate static void bflush(void);
1370Sstevel@tonic-gate static int chgreel(int dir);
1380Sstevel@tonic-gate static int ckname(int);
1390Sstevel@tonic-gate static void ckopts(long mask);
1400Sstevel@tonic-gate static long cksum(char hdr, int byt_cnt, int *err);
1410Sstevel@tonic-gate static int creat_hdr(void);
1420Sstevel@tonic-gate static int creat_lnk(int dirfd, char *name1_p, char *name2_p);
1430Sstevel@tonic-gate static int creat_spec(int dirfd);
1440Sstevel@tonic-gate static int creat_tmp(char *nam_p);
1450Sstevel@tonic-gate static void data_in(int proc_mode);
1460Sstevel@tonic-gate static void data_out(void);
1470Sstevel@tonic-gate static void data_pass(void);
1480Sstevel@tonic-gate static void file_in(void);
1490Sstevel@tonic-gate static int file_out(void);
1500Sstevel@tonic-gate static int file_pass(void);
1510Sstevel@tonic-gate static void flush_lnks(void);
1520Sstevel@tonic-gate static int gethdr(void);
1530Sstevel@tonic-gate static int getname(void);
1540Sstevel@tonic-gate static void getpats(int largc, char **largv);
1550Sstevel@tonic-gate static void ioerror(int dir);
1560Sstevel@tonic-gate static int matched(void);
1570Sstevel@tonic-gate static int missdir(char *nam_p);
1580Sstevel@tonic-gate static long mklong(short v[]);
1590Sstevel@tonic-gate static void mkshort(short sval[], long v);
1600Sstevel@tonic-gate static int openout(int dirfd);
1610Sstevel@tonic-gate static int read_hdr(int hdr);
1620Sstevel@tonic-gate static void reclaim(struct Lnk *l_p);
1630Sstevel@tonic-gate static void rstbuf(void);
1640Sstevel@tonic-gate static void setpasswd(char *nam);
1650Sstevel@tonic-gate static void rstfiles(int over, int dirfd);
1660Sstevel@tonic-gate static void scan4trail(void);
1670Sstevel@tonic-gate static void setup(int largc, char **largv);
1680Sstevel@tonic-gate static void set_tym(int dirfd, char *nam_p, time_t atime, time_t mtime);
1690Sstevel@tonic-gate static void sigint(int sig);
1700Sstevel@tonic-gate static void swap(char *buf_p, int cnt);
1710Sstevel@tonic-gate static void usage(void);
1720Sstevel@tonic-gate static void verbose(char *nam_p);
17310202SNobutomo.Nakano@Sun.COM static void write_hdr(int arcflag, off_t len);
1740Sstevel@tonic-gate static void write_trail(void);
1750Sstevel@tonic-gate static int ustar_dir(void);
1760Sstevel@tonic-gate static int ustar_spec(void);
1770Sstevel@tonic-gate static struct stat *convert_to_old_stat(struct stat *, char *, char *);
1780Sstevel@tonic-gate static void read_bar_vol_hdr(void);
1790Sstevel@tonic-gate static void read_bar_file_hdr(void);
1800Sstevel@tonic-gate static void setup_uncompress(FILE **);
1810Sstevel@tonic-gate static void skip_bar_volhdr(void);
1820Sstevel@tonic-gate static void bar_file_in(void);
1830Sstevel@tonic-gate static int g_init(int *devtype, int *fdes);
1840Sstevel@tonic-gate static int g_read(int, int, char *, unsigned);
1850Sstevel@tonic-gate static int g_write(int, int, char *, unsigned);
1860Sstevel@tonic-gate static int is_floppy(int);
1870Sstevel@tonic-gate static int is_tape(int);
18810202SNobutomo.Nakano@Sun.COM static void write_ancillary(char *buf, size_t len, boolean_t padding);
1890Sstevel@tonic-gate static int remove_dir(char *);
1900Sstevel@tonic-gate static int save_cwd(void);
1910Sstevel@tonic-gate static void rest_cwd(int cwd);
1920Sstevel@tonic-gate
1930Sstevel@tonic-gate static void xattrs_out(int (*func)());
1940Sstevel@tonic-gate static void get_parent(char *path, char *dir);
1950Sstevel@tonic-gate static void prepare_xattr_hdr(char **attrbuf, char *filename,
1960Sstevel@tonic-gate char *attrname, char typeflag, struct Lnk *linkinfo, int *rlen);
1970Sstevel@tonic-gate static char tartype(int type);
1980Sstevel@tonic-gate static int openfile(int omode);
1990Sstevel@tonic-gate static mode_t attrmode(char type);
2000Sstevel@tonic-gate static char *get_component(char *path);
2010Sstevel@tonic-gate static int open_dir(char *name);
2020Sstevel@tonic-gate static int open_dirfd();
2030Sstevel@tonic-gate static void close_dirfd();
2040Sstevel@tonic-gate static void write_xattr_hdr();
2050Sstevel@tonic-gate static char *skipslashes(char *string, char *start);
2060Sstevel@tonic-gate static int read_xattr_hdr();
2070Sstevel@tonic-gate static void chop_endslashes(char *path);
2080Sstevel@tonic-gate
2090Sstevel@tonic-gate
2100Sstevel@tonic-gate /* helpful types */
2110Sstevel@tonic-gate
2120Sstevel@tonic-gate static
2130Sstevel@tonic-gate struct passwd *Curpw_p, /* Current password entry for -t option */
2140Sstevel@tonic-gate *Rpw_p, /* Password entry for -R option */
2150Sstevel@tonic-gate *dpasswd;
2160Sstevel@tonic-gate
2170Sstevel@tonic-gate static
2180Sstevel@tonic-gate struct group *Curgr_p, /* Current group entry for -t option */
2190Sstevel@tonic-gate *dgroup;
2200Sstevel@tonic-gate
2210Sstevel@tonic-gate /* Data structure for buffered I/O. */
2220Sstevel@tonic-gate
2230Sstevel@tonic-gate static
2240Sstevel@tonic-gate struct buf_info {
2250Sstevel@tonic-gate char *b_base_p, /* Pointer to base of buffer */
2260Sstevel@tonic-gate *b_out_p, /* Position to take bytes from buffer at */
2270Sstevel@tonic-gate *b_in_p, /* Position to put bytes into buffer at */
2280Sstevel@tonic-gate *b_end_p; /* Pointer to end of buffer */
2290Sstevel@tonic-gate long b_cnt, /* Count of unprocessed bytes */
2300Sstevel@tonic-gate b_size; /* Size of buffer in bytes */
2310Sstevel@tonic-gate } Buffr;
2320Sstevel@tonic-gate
2330Sstevel@tonic-gate /* Generic header format */
2340Sstevel@tonic-gate
2350Sstevel@tonic-gate static
2360Sstevel@tonic-gate struct gen_hdr {
2370Sstevel@tonic-gate ulong_t g_magic, /* Magic number field */
2380Sstevel@tonic-gate g_ino, /* Inode number of file */
2390Sstevel@tonic-gate g_mode, /* Mode of file */
2400Sstevel@tonic-gate g_uid, /* Uid of file */
2410Sstevel@tonic-gate g_gid, /* Gid of file */
2420Sstevel@tonic-gate g_nlink, /* Number of links */
2430Sstevel@tonic-gate g_mtime; /* Modification time */
2440Sstevel@tonic-gate off_t g_filesz; /* Length of file */
2450Sstevel@tonic-gate ulong_t g_dev, /* File system of file */
2460Sstevel@tonic-gate g_rdev, /* Major/minor numbers of special files */
2470Sstevel@tonic-gate g_namesz, /* Length of filename */
2480Sstevel@tonic-gate g_cksum; /* Checksum of file */
2490Sstevel@tonic-gate char g_gname[32],
2500Sstevel@tonic-gate g_uname[32],
2510Sstevel@tonic-gate g_version[2],
2520Sstevel@tonic-gate g_tmagic[6],
2530Sstevel@tonic-gate g_typeflag;
2540Sstevel@tonic-gate char *g_tname,
2550Sstevel@tonic-gate *g_prefix,
2560Sstevel@tonic-gate *g_nam_p, /* Filename */
2575750Sceastha *g_attrparent_p, /* attribute parent */
2585750Sceastha *g_attrpath_p, /* attribute path */
2590Sstevel@tonic-gate *g_attrnam_p, /* attribute */
2600Sstevel@tonic-gate *g_attrfnam_p, /* Real file name attr belongs to */
2610Sstevel@tonic-gate *g_linktoattrfnam_p, /* file linked attribute belongs to */
2620Sstevel@tonic-gate *g_linktoattrnam_p, /* attribute g_attrnam_p is linked to */
2630Sstevel@tonic-gate *g_dirpath; /* dirname currently opened */
2640Sstevel@tonic-gate int g_dirfd; /* directory file descriptor */
2650Sstevel@tonic-gate int g_passdirfd; /* directory fd to pass to */
2665750Sceastha int g_rw_sysattr; /* read-write system attribute */
2675750Sceastha int g_baseparent_fd; /* base file's parent fd */
26810202SNobutomo.Nakano@Sun.COM holes_info_t *g_holes; /* sparse file information */
2690Sstevel@tonic-gate
2700Sstevel@tonic-gate } Gen, *G_p;
2710Sstevel@tonic-gate
2720Sstevel@tonic-gate /* Data structure for handling multiply-linked files */
2730Sstevel@tonic-gate static
2740Sstevel@tonic-gate char prebuf[PRESIZ+1],
2750Sstevel@tonic-gate nambuf[NAMSIZ+1],
2760Sstevel@tonic-gate fullnam[MAXNAM+1];
2770Sstevel@tonic-gate
2780Sstevel@tonic-gate
2790Sstevel@tonic-gate static
2800Sstevel@tonic-gate struct Lnk {
2810Sstevel@tonic-gate short L_cnt, /* Number of links encountered */
2820Sstevel@tonic-gate L_data; /* Data has been encountered if 1 */
2830Sstevel@tonic-gate struct gen_hdr L_gen; /* gen_hdr information for this file */
2840Sstevel@tonic-gate struct Lnk *L_nxt_p, /* Next file in list */
2850Sstevel@tonic-gate *L_bck_p, /* Previous file in list */
2860Sstevel@tonic-gate *L_lnk_p; /* Next link for this file */
2870Sstevel@tonic-gate } Lnk_hd;
2880Sstevel@tonic-gate
2890Sstevel@tonic-gate static
2900Sstevel@tonic-gate struct hdr_cpio Hdr;
2910Sstevel@tonic-gate
2920Sstevel@tonic-gate /*
2930Sstevel@tonic-gate * -------------------------------------------------------------------------
2940Sstevel@tonic-gate * Stuff needed to pre-view the name stream
2950Sstevel@tonic-gate *
2960Sstevel@tonic-gate * issymlink is used to remember that the current file is a symlink between
2970Sstevel@tonic-gate * getname() and file_pass(); the former trashes this information immediately
2980Sstevel@tonic-gate * when -L is specified.
2990Sstevel@tonic-gate */
3000Sstevel@tonic-gate
3010Sstevel@tonic-gate static
3020Sstevel@tonic-gate int issymlink = 0;
3030Sstevel@tonic-gate
3040Sstevel@tonic-gate static
3050Sstevel@tonic-gate FILE *In_p = stdin; /* Where the input comes from */
3060Sstevel@tonic-gate
3070Sstevel@tonic-gate typedef struct sl_info
3080Sstevel@tonic-gate {
3090Sstevel@tonic-gate struct sl_info *llink; /* Left subtree ptr (tree depth in *sl_head) */
3100Sstevel@tonic-gate struct sl_info *rlink; /* Right subtree ptr */
3110Sstevel@tonic-gate int bal; /* Subtree balance factor */
3120Sstevel@tonic-gate ulong_t sl_count; /* Number of symlinks */
3135750Sceastha int sl_ftype; /* file type of inode */
3140Sstevel@tonic-gate ino_t sl_ino; /* Inode of file */
3150Sstevel@tonic-gate ino_t sl_ino2; /* alternate inode for -Hodc */
3160Sstevel@tonic-gate } sl_info_t;
3170Sstevel@tonic-gate
3185750Sceastha typedef struct data_in
3195750Sceastha {
32010202SNobutomo.Nakano@Sun.COM int data_in_errno;
32110389SNobutomo.Nakano@Sun.COM char data_in_swapfile;
32210389SNobutomo.Nakano@Sun.COM char data_in_proc_mode;
32310389SNobutomo.Nakano@Sun.COM char data_in_rd_eof;
32410389SNobutomo.Nakano@Sun.COM char data_in_wr_part;
32510389SNobutomo.Nakano@Sun.COM char data_in_compress_flag;
32610202SNobutomo.Nakano@Sun.COM long data_in_cksumval;
32710202SNobutomo.Nakano@Sun.COM FILE *data_in_pipef;
3285750Sceastha } data_in_t;
3295750Sceastha
3300Sstevel@tonic-gate /*
3310Sstevel@tonic-gate * The following structure maintains a hash entry for the
3320Sstevel@tonic-gate * balancing trees which are allocated for each device nodes.
3330Sstevel@tonic-gate */
3340Sstevel@tonic-gate typedef struct sl_info_link
3350Sstevel@tonic-gate {
3360Sstevel@tonic-gate dev_t dev;
3370Sstevel@tonic-gate sl_info_t *head;
3380Sstevel@tonic-gate struct sl_info_link *next;
3390Sstevel@tonic-gate } sl_info_link_t;
3400Sstevel@tonic-gate
3410Sstevel@tonic-gate #define SL_INFO_ALLOC_CHUNK 1024
3420Sstevel@tonic-gate #define NDEVHENTRY 0x40
3430Sstevel@tonic-gate #define DEV_HASHKEY(x) ((x) & (NDEVHENTRY -1))
3440Sstevel@tonic-gate
3450Sstevel@tonic-gate /*
3460Sstevel@tonic-gate * For remapping dev,inode for -Hodc archives.
3470Sstevel@tonic-gate */
3480Sstevel@tonic-gate
3490Sstevel@tonic-gate typedef struct sl_remap
3500Sstevel@tonic-gate {
3510Sstevel@tonic-gate dev_t dev; /* device */
3520Sstevel@tonic-gate int inode_count; /* # inodes seen on dev */
3530Sstevel@tonic-gate struct sl_remap *next; /* next in the chain */
3540Sstevel@tonic-gate } sl_remap_t;
3550Sstevel@tonic-gate
3560Sstevel@tonic-gate /* forward declarations */
3570Sstevel@tonic-gate
3580Sstevel@tonic-gate static sl_info_t *sl_info_alloc(void);
3595750Sceastha static sl_info_t *sl_insert(dev_t, ino_t, int);
3605750Sceastha static ulong_t sl_numlinks(dev_t, ino_t, int);
3610Sstevel@tonic-gate static void sl_preview_synonyms(void);
3625750Sceastha static void sl_remember_tgt(const struct stat *, int, int);
3635750Sceastha static sl_info_t *sl_search(dev_t, ino_t, int);
3640Sstevel@tonic-gate static sl_info_t *sl_devhash_lookup(dev_t);
3650Sstevel@tonic-gate static void sl_devhash_insert(dev_t, sl_info_t *);
3660Sstevel@tonic-gate
3675750Sceastha extern int sl_compare(ino_t, int, ino_t, int);
3685750Sceastha #define sl_compare(lino, lftype, rino, rftype) (lino < rino ? -1 : \
3695750Sceastha (lino > rino ? 1 : (lftype < rftype ? -1 : \
3705750Sceastha (lftype > rftype ? 1 : 0))))
3710Sstevel@tonic-gate
3720Sstevel@tonic-gate /* global storage */
3730Sstevel@tonic-gate
3740Sstevel@tonic-gate static sl_remap_t *sl_remap_head = NULL; /* head of the inode-remap list */
3750Sstevel@tonic-gate static sl_info_link_t *sl_devhash[NDEVHENTRY]; /* hash table */
3760Sstevel@tonic-gate
3770Sstevel@tonic-gate /*
3780Sstevel@tonic-gate * -------------------------------------------------------------------------
3790Sstevel@tonic-gate */
3800Sstevel@tonic-gate
3810Sstevel@tonic-gate static
3820Sstevel@tonic-gate struct stat ArchSt, /* stat(2) information of the archive */
3830Sstevel@tonic-gate SrcSt, /* stat(2) information of source file */
3840Sstevel@tonic-gate DesSt, /* stat(2) of destination file */
3850Sstevel@tonic-gate *OldSt = NULL; /* stat info converted to svr32 format */
3860Sstevel@tonic-gate
3870Sstevel@tonic-gate /*
3880Sstevel@tonic-gate * bin_mag: Used to validate a binary magic number,
3890Sstevel@tonic-gate * by combining to bytes into an unsigned short.
3900Sstevel@tonic-gate */
3910Sstevel@tonic-gate
3920Sstevel@tonic-gate static
3930Sstevel@tonic-gate union bin_mag {
3940Sstevel@tonic-gate unsigned char b_byte[2];
3950Sstevel@tonic-gate ushort_t b_half;
3960Sstevel@tonic-gate } Binmag;
3970Sstevel@tonic-gate
3980Sstevel@tonic-gate static
3990Sstevel@tonic-gate union tblock *Thdr_p; /* TAR header pointer */
4000Sstevel@tonic-gate
4010Sstevel@tonic-gate static union b_block *bar_Vhdr;
4020Sstevel@tonic-gate static struct gen_hdr Gen_bar_vol;
4030Sstevel@tonic-gate
4040Sstevel@tonic-gate /*
4050Sstevel@tonic-gate * swpbuf: Used in swap() to swap bytes within a halfword,
4060Sstevel@tonic-gate * halfwords within a word, or to reverse the order of the
4070Sstevel@tonic-gate * bytes within a word. Also used in mklong() and mkshort().
4080Sstevel@tonic-gate */
4090Sstevel@tonic-gate
4100Sstevel@tonic-gate static
4110Sstevel@tonic-gate union swpbuf {
4120Sstevel@tonic-gate unsigned char s_byte[4];
4130Sstevel@tonic-gate ushort_t s_half[2];
4140Sstevel@tonic-gate ulong_t s_word;
4150Sstevel@tonic-gate } *Swp_p;
4160Sstevel@tonic-gate
4170Sstevel@tonic-gate static
4185750Sceastha char *myname, /* program name */
4195750Sceastha Adir, /* Flags object as a directory */
4200Sstevel@tonic-gate Hiddendir, /* Processing hidden attribute directory */
4210Sstevel@tonic-gate Aspec, /* Flags object as a special file */
4220Sstevel@tonic-gate Do_rename, /* Indicates rename() is to be used */
4230Sstevel@tonic-gate Time[50], /* Array to hold date and time */
4240Sstevel@tonic-gate Ttyname[] = "/dev/tty", /* Controlling console */
4250Sstevel@tonic-gate T_lname[MAXPATHLEN], /* Array to hold links name for tar */
4260Sstevel@tonic-gate *Buf_p, /* Buffer for file system I/O */
4270Sstevel@tonic-gate *Full_p, /* Pointer to full pathname */
4280Sstevel@tonic-gate *Efil_p, /* -E pattern file string */
4290Sstevel@tonic-gate *Eom_p = "Change to part %d and press RETURN key. [q] ",
4300Sstevel@tonic-gate *Fullnam_p, /* Full pathname */
4310Sstevel@tonic-gate *Attrfile_p, /* attribute file */
4320Sstevel@tonic-gate *Hdr_p, /* -H header type string */
4330Sstevel@tonic-gate *IOfil_p, /* -I/-O input/output archive string */
4340Sstevel@tonic-gate *Lnkend_p, /* Pointer to end of Lnknam_p */
4350Sstevel@tonic-gate *Lnknam_p, /* Buffer for linking files with -p option */
4360Sstevel@tonic-gate *Nam_p, /* Array to hold filename */
4370Sstevel@tonic-gate *Savenam_p, /* copy of filename xattr belongs to */
4380Sstevel@tonic-gate *Own_p, /* New owner login id string */
4390Sstevel@tonic-gate *Renam_p, /* Buffer for renaming files */
4405750Sceastha *Renam_attr_p, /* Buffer for renaming attr with sys attrs */
4410Sstevel@tonic-gate *Renametmp_p, /* Tmp Buffer for renaming files */
4420Sstevel@tonic-gate *Symlnk_p, /* Buffer for holding symbolic link name */
4430Sstevel@tonic-gate *Over_p, /* Holds temporary filename when overwriting */
4440Sstevel@tonic-gate **Pat_pp = 0, /* Pattern strings */
4450Sstevel@tonic-gate bar_linkflag, /* flag to indicate if the file is a link */
4460Sstevel@tonic-gate bar_linkname[MAXPATHLEN]; /* store the name of the link */
4470Sstevel@tonic-gate
4480Sstevel@tonic-gate static
4490Sstevel@tonic-gate int Append = 0, /* Flag set while searching to end of archive */
4500Sstevel@tonic-gate Archive, /* File descriptor of the archive */
4515331Samw Buf_error = 0, /* I/O error occurred during buffer fill */
45210202SNobutomo.Nakano@Sun.COM Compress_sparse = 0, /* Compress sparse files */
4530Sstevel@tonic-gate Def_mode = 0777, /* Default file/directory protection modes */
4540Sstevel@tonic-gate Device, /* Device type being accessed (used with libgenIO) */
4550Sstevel@tonic-gate Error_cnt = 0, /* Cumulative count of I/O errors */
4560Sstevel@tonic-gate Finished = 1, /* Indicates that a file transfer has completed */
4570Sstevel@tonic-gate Hdrsz = ASCSZ, /* Fixed length portion of the header */
4580Sstevel@tonic-gate Hdr_type, /* Flag to indicate type of header selected */
4590Sstevel@tonic-gate Ifile, /* File des. of file being archived */
4600Sstevel@tonic-gate Ofile, /* File des. of file being extracted from archive */
4610Sstevel@tonic-gate Use_old_stat = 0, /* Create an old style -Hodc hdr (small dev's) */
4620Sstevel@tonic-gate Onecopy = 0, /* Flags old vs. new link handling */
4630Sstevel@tonic-gate Pad_val = 0, /* Indicates the number of bytes to pad (if any) */
4640Sstevel@tonic-gate PageSize = 0, /* The native page size, used for figuring block size */
4650Sstevel@tonic-gate Volcnt = 1, /* Number of archive volumes processed */
4660Sstevel@tonic-gate Verbcnt = 0, /* Count of number of dots '.' output */
4670Sstevel@tonic-gate Eomflag = 0,
4680Sstevel@tonic-gate Dflag = 0,
4690Sstevel@tonic-gate Atflag = 0, /* Archive/restore extended attributes */
4705750Sceastha SysAtflag = 0, /* Archive/restore extended system attributes */
4710Sstevel@tonic-gate Compressed, /* Flag to indicate if the bar archive is compressed */
4721134Sceastha Bar_vol_num = 0, /* Volume number count for bar archive */
4735750Sceastha privileged = 0, /* Flag set if running with higher privileges */
4745750Sceastha attr_baseparent_fd = -1; /* attribute's base file descriptor */
4750Sstevel@tonic-gate
4760Sstevel@tonic-gate
4770Sstevel@tonic-gate static
4784321Scasper gid_t Lastgid = (gid_t)-1; /* Used with -t & -v to record current gid */
4790Sstevel@tonic-gate
4800Sstevel@tonic-gate static
4814321Scasper uid_t Lastuid = (uid_t)-1; /* Used with -t & -v to record current uid */
4820Sstevel@tonic-gate
4830Sstevel@tonic-gate static
4840Sstevel@tonic-gate long Args, /* Mask of selected options */
4850Sstevel@tonic-gate Max_namesz = CPATH; /* Maximum size of pathnames/filenames */
4860Sstevel@tonic-gate
4870Sstevel@tonic-gate static
4880Sstevel@tonic-gate int Bufsize = BUFSZ; /* Default block size */
4890Sstevel@tonic-gate
4900Sstevel@tonic-gate
4910Sstevel@tonic-gate static u_longlong_t Blocks; /* full blocks transferred */
4920Sstevel@tonic-gate static u_longlong_t SBlocks; /* cumulative char count from short reads */
4930Sstevel@tonic-gate
4940Sstevel@tonic-gate
4950Sstevel@tonic-gate static off_t Max_offset = BIN_OFFSET_MAX; /* largest file size */
4960Sstevel@tonic-gate static off_t Max_filesz; /* from getrlimit */
4970Sstevel@tonic-gate
4985750Sceastha static ulong_t Savedev;
4995750Sceastha
5000Sstevel@tonic-gate static
5010Sstevel@tonic-gate FILE *Ef_p, /* File pointer of pattern input file */
5020Sstevel@tonic-gate *Err_p = stderr, /* File pointer for error reporting */
5030Sstevel@tonic-gate *Out_p = stdout, /* File pointer for non-archive output */
5040Sstevel@tonic-gate *Rtty_p, /* Input file pointer for interactive rename */
5050Sstevel@tonic-gate *Wtty_p; /* Output file ptr for interactive rename */
5060Sstevel@tonic-gate
5070Sstevel@tonic-gate static
5080Sstevel@tonic-gate ushort_t Ftype = S_IFMT; /* File type mask */
5090Sstevel@tonic-gate
5100Sstevel@tonic-gate /* ACL support */
5110Sstevel@tonic-gate static struct sec_attr {
5120Sstevel@tonic-gate char attr_type;
5130Sstevel@tonic-gate char attr_len[7];
5140Sstevel@tonic-gate char attr_info[1];
5150Sstevel@tonic-gate } *attr;
5160Sstevel@tonic-gate
5170Sstevel@tonic-gate static int Pflag = 0; /* flag indicates that acl is preserved */
518789Sahrens static int acl_is_set = 0; /* True if an acl was set on the file */
519789Sahrens
520789Sahrens acl_t *aclp;
5210Sstevel@tonic-gate
5225750Sceastha #if defined(O_XATTR)
5235750Sceastha typedef enum {
5245750Sceastha ATTR_OK,
5255750Sceastha ATTR_SKIP,
5265750Sceastha ATTR_CHDIR_ERR,
5275750Sceastha ATTR_OPEN_ERR,
5285750Sceastha ATTR_XATTR_ERR,
5295750Sceastha ATTR_SATTR_ERR
5305750Sceastha } attr_status_t;
5315750Sceastha #endif
5325750Sceastha
5335750Sceastha #if defined(O_XATTR)
5345750Sceastha typedef enum {
5355750Sceastha ARC_CREATE,
5365750Sceastha ARC_RESTORE
5375750Sceastha } arc_action_t;
5385750Sceastha #endif
5395750Sceastha
5405750Sceastha
5410Sstevel@tonic-gate /*
5420Sstevel@tonic-gate *
5430Sstevel@tonic-gate * cpio has been changed to support extended attributes.
5440Sstevel@tonic-gate *
5450Sstevel@tonic-gate * As part of this change cpio has been changed to use the new *at() syscalls
5460Sstevel@tonic-gate * such as openat, fchownat(), unlinkat()...
5470Sstevel@tonic-gate *
5480Sstevel@tonic-gate * This was done so that attributes can be handled with as few code changes
5490Sstevel@tonic-gate * as possible.
5500Sstevel@tonic-gate *
5510Sstevel@tonic-gate * What this means is that cpio now opens the directory that a file or directory
5520Sstevel@tonic-gate * resides in and then performs *at() functions to manipulate the entry.
5530Sstevel@tonic-gate *
5540Sstevel@tonic-gate * For example a new file is now created like this:
5550Sstevel@tonic-gate *
5560Sstevel@tonic-gate * dfd = open(<some dir path>)
5570Sstevel@tonic-gate * fd = openat(dfd, <name>,....);
5580Sstevel@tonic-gate *
5590Sstevel@tonic-gate * or in the case of an extended attribute
5600Sstevel@tonic-gate *
5610Sstevel@tonic-gate * dfd = attropen(<pathname>, ".", ....)
5620Sstevel@tonic-gate *
5630Sstevel@tonic-gate * Once we have a directory file descriptor all of the *at() functions can
5640Sstevel@tonic-gate * be applied to it.
5650Sstevel@tonic-gate *
5660Sstevel@tonic-gate * unlinkat(dfd, <component name>,...)
5670Sstevel@tonic-gate * fchownat(dfd, <component name>,..)
5680Sstevel@tonic-gate *
5690Sstevel@tonic-gate * This works for both normal namespace files and extended attribute file
5700Sstevel@tonic-gate *
5710Sstevel@tonic-gate */
5720Sstevel@tonic-gate
5730Sstevel@tonic-gate /*
5740Sstevel@tonic-gate * Extended attribute layout
5750Sstevel@tonic-gate *
5760Sstevel@tonic-gate * Extended attributes are stored in two pieces.
5770Sstevel@tonic-gate * 1. An attribute header which has information about
5780Sstevel@tonic-gate * what file the attribute is for and what the attribute
5790Sstevel@tonic-gate * is named.
5800Sstevel@tonic-gate * 2. The attribute record itself. Stored as a normal file type
5810Sstevel@tonic-gate * of entry.
5820Sstevel@tonic-gate * Both the header and attribute record have special modes/typeflags
5830Sstevel@tonic-gate * associated with them.
5840Sstevel@tonic-gate *
5850Sstevel@tonic-gate * The names of the header in the archive look like:
5860Sstevel@tonic-gate * /dev/null/attr.hdr
5870Sstevel@tonic-gate *
5880Sstevel@tonic-gate * The name of the attribute looks like:
5890Sstevel@tonic-gate * /dev/null/attr.
5900Sstevel@tonic-gate *
5910Sstevel@tonic-gate * This is done so that an archiver that doesn't understand these formats
5920Sstevel@tonic-gate * can just dispose of the attribute records unless the user chooses to
5930Sstevel@tonic-gate * rename them via cpio -r or pax -i
5940Sstevel@tonic-gate *
5950Sstevel@tonic-gate * The format is composed of a fixed size header followed
5960Sstevel@tonic-gate * by a variable sized xattr_buf. If the attribute is a hard link
5970Sstevel@tonic-gate * to another attribute, then another xattr_buf section is included
5980Sstevel@tonic-gate * for the link.
5990Sstevel@tonic-gate *
6000Sstevel@tonic-gate * The xattr_buf is used to define the necessary "pathing" steps
6010Sstevel@tonic-gate * to get to the extended attribute. This is necessary to support
6020Sstevel@tonic-gate * a fully recursive attribute model where an attribute may itself
6030Sstevel@tonic-gate * have an attribute.
6040Sstevel@tonic-gate *
6050Sstevel@tonic-gate * The basic layout looks like this.
6060Sstevel@tonic-gate *
6070Sstevel@tonic-gate * --------------------------------
6080Sstevel@tonic-gate * | |
6090Sstevel@tonic-gate * | xattr_hdr |
6100Sstevel@tonic-gate * | |
6110Sstevel@tonic-gate * --------------------------------
6120Sstevel@tonic-gate * --------------------------------
6130Sstevel@tonic-gate * | |
6140Sstevel@tonic-gate * | xattr_buf |
6150Sstevel@tonic-gate * | |
6160Sstevel@tonic-gate * --------------------------------
6170Sstevel@tonic-gate * --------------------------------
6180Sstevel@tonic-gate * | |
6190Sstevel@tonic-gate * | (optional link info) |
6200Sstevel@tonic-gate * | |
6210Sstevel@tonic-gate * --------------------------------
6220Sstevel@tonic-gate * --------------------------------
6230Sstevel@tonic-gate * | |
6240Sstevel@tonic-gate * | attribute itself |
6250Sstevel@tonic-gate * | stored as normal tar |
6260Sstevel@tonic-gate * | or cpio data with |
6270Sstevel@tonic-gate * | special mode or |
6280Sstevel@tonic-gate * | typeflag |
6290Sstevel@tonic-gate * | |
6300Sstevel@tonic-gate * --------------------------------
6310Sstevel@tonic-gate *
6320Sstevel@tonic-gate */
6330Sstevel@tonic-gate
6340Sstevel@tonic-gate /*
6350Sstevel@tonic-gate * Extended attributes structures
6360Sstevel@tonic-gate *
6370Sstevel@tonic-gate * xattrhead is the complete extended attribute header, as read of off
6380Sstevel@tonic-gate * disk/tape. It includes the variable xattr_buf portion.
6390Sstevel@tonic-gate *
6400Sstevel@tonic-gate * xattrp is basically an offset into xattrhead that points to the
6410Sstevel@tonic-gate * "pathing" section which defines how to get to the attribute.
6420Sstevel@tonic-gate *
6430Sstevel@tonic-gate * xattr_linkp is identical to xattrp except that it is used for linked
6440Sstevel@tonic-gate * attributes. It provides the pathing steps to get to the linked
6450Sstevel@tonic-gate * attribute.
6460Sstevel@tonic-gate *
6470Sstevel@tonic-gate * These structures are updated when an extended attribute header is read off
6480Sstevel@tonic-gate * of disk/tape.
6490Sstevel@tonic-gate */
6500Sstevel@tonic-gate static struct xattr_hdr *xattrhead;
6510Sstevel@tonic-gate static struct xattr_buf *xattrp;
6520Sstevel@tonic-gate static struct xattr_buf *xattr_linkp;
6530Sstevel@tonic-gate static int xattrbadhead; /* is extended attribute header bad? */
6540Sstevel@tonic-gate
655789Sahrens static int append_secattr(char **, int *, acl_t *);
6560Sstevel@tonic-gate
6570Sstevel@tonic-gate /*
6580Sstevel@tonic-gate * Note regarding cpio and changes to ensure cpio doesn't try to second
6590Sstevel@tonic-gate * guess whether it runs with sufficient privileges or not:
6600Sstevel@tonic-gate *
6610Sstevel@tonic-gate * cpio has been changed so that it doesn't carry a second implementation of
6620Sstevel@tonic-gate * the kernel's policy with respect to privileges. Instead of attempting
6630Sstevel@tonic-gate * to restore uid and gid from an archive only if cpio is run as uid 0,
6641134Sceastha * cpio now *always* tries to restore the uid and gid from the archive
6651134Sceastha * except when the -R option is specified. When the -R is specified,
6661134Sceastha * the uid and gid of the restored file will be changed to those of the
6671134Sceastha * login id specified. In addition, chown(), set_tym(), and chmod() should
6681134Sceastha * only be executed once during archive extraction, and to ensure
6691134Sceastha * setuid/setgid bits are restored properly, chown() should always be
6701134Sceastha * executed before chmod().
6710Sstevel@tonic-gate *
6720Sstevel@tonic-gate * Note regarding debugging mechanism for cpio:
6730Sstevel@tonic-gate *
6740Sstevel@tonic-gate * The following mechanism is provided to allow us to debug cpio in complicated
6750Sstevel@tonic-gate * situations, like when it is part of a pipe. The idea is that you compile
6760Sstevel@tonic-gate * with -DWAITAROUND defined, and then add the "-z" command line option to the
6770Sstevel@tonic-gate * target cpio invocation. If stderr is available, it will tell you to which
6780Sstevel@tonic-gate * pid to attach the debugger; otherwise, use ps to find it. Attach to the
6790Sstevel@tonic-gate * process from the debugger, and, *PRESTO*, you are there!
6800Sstevel@tonic-gate *
6810Sstevel@tonic-gate * Simply assign "waitaround = 0" once you attach to the process, and then
6820Sstevel@tonic-gate * proceed from there as usual.
6830Sstevel@tonic-gate */
6840Sstevel@tonic-gate
6850Sstevel@tonic-gate #ifdef WAITAROUND
6860Sstevel@tonic-gate int waitaround = 0; /* wait for rendezvous with the debugger */
6870Sstevel@tonic-gate #endif
6880Sstevel@tonic-gate
6890Sstevel@tonic-gate #define EXIT_CODE (Error_cnt > 255 ? 255 : Error_cnt)
6900Sstevel@tonic-gate
6910Sstevel@tonic-gate /*
6920Sstevel@tonic-gate * main: Call setup() to process options and perform initializations,
6930Sstevel@tonic-gate * and then select either copy in (-i), copy out (-o), or pass (-p) action.
6940Sstevel@tonic-gate */
6950Sstevel@tonic-gate
69636Schin int
main(int argc,char ** argv)6970Sstevel@tonic-gate main(int argc, char **argv)
6980Sstevel@tonic-gate {
6990Sstevel@tonic-gate int i;
7000Sstevel@tonic-gate int passret;
7010Sstevel@tonic-gate
7020Sstevel@tonic-gate (void) setlocale(LC_ALL, "");
7030Sstevel@tonic-gate #if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */
7040Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it weren't */
7050Sstevel@tonic-gate #endif
7060Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN);
7070Sstevel@tonic-gate
7080Sstevel@tonic-gate (void) memset(&Gen, 0, sizeof (Gen));
7095750Sceastha myname = e_strdup(E_EXIT, basename(argv[0]));
7100Sstevel@tonic-gate setup(argc, argv);
7110Sstevel@tonic-gate
7120Sstevel@tonic-gate if (signal(SIGINT, sigint) == SIG_IGN)
7130Sstevel@tonic-gate (void) signal(SIGINT, SIG_IGN);
7140Sstevel@tonic-gate switch (Args & (OCi | OCo | OCp)) {
7150Sstevel@tonic-gate case OCi: /* COPY IN */
7160Sstevel@tonic-gate Hdr_type = NONE;
7175750Sceastha if (Atflag || SysAtflag) {
7185750Sceastha /*
7195750Sceastha * Save the current working directory, so
7205750Sceastha * we can change back here after cd'ing into
7215750Sceastha * the attribute directory when processing
7225750Sceastha * attributes.
7235750Sceastha */
7245750Sceastha if ((attr_baseparent_fd = save_cwd()) < 0) {
7255750Sceastha msg(EXT, "Unable to open current directory.");
7265750Sceastha }
7275750Sceastha }
7280Sstevel@tonic-gate while ((i = gethdr()) != 0) {
7290Sstevel@tonic-gate Gen.g_dirfd = -1;
7300Sstevel@tonic-gate if (i == 1) {
7310Sstevel@tonic-gate file_in();
7320Sstevel@tonic-gate /*
7330Sstevel@tonic-gate * Any ACL info for this file would or should
7340Sstevel@tonic-gate * have been used after file_in(); clear out
7350Sstevel@tonic-gate * aclp so it is is not erroneously used on
7360Sstevel@tonic-gate * the next file.
7370Sstevel@tonic-gate */
7380Sstevel@tonic-gate if (aclp != NULL) {
739789Sahrens acl_free(aclp);
7400Sstevel@tonic-gate aclp = NULL;
7410Sstevel@tonic-gate }
742789Sahrens acl_is_set = 0;
7430Sstevel@tonic-gate }
7440Sstevel@tonic-gate (void) memset(&Gen, 0, sizeof (Gen));
7450Sstevel@tonic-gate }
7460Sstevel@tonic-gate /* Do not count "extra" "read-ahead" buffered data */
7470Sstevel@tonic-gate if (Buffr.b_cnt > Bufsize)
7480Sstevel@tonic-gate Blocks -= (u_longlong_t)(Buffr.b_cnt / Bufsize);
7490Sstevel@tonic-gate break;
7500Sstevel@tonic-gate case OCo: /* COPY OUT */
7510Sstevel@tonic-gate if (Args & OCA) {
7520Sstevel@tonic-gate scan4trail();
7530Sstevel@tonic-gate }
7540Sstevel@tonic-gate
7550Sstevel@tonic-gate Gen.g_dirfd = -1;
7560Sstevel@tonic-gate Gen.g_dirpath = NULL;
7570Sstevel@tonic-gate sl_preview_synonyms();
7580Sstevel@tonic-gate
7590Sstevel@tonic-gate while ((i = getname()) != 0) {
7600Sstevel@tonic-gate if (i == 1) {
7610Sstevel@tonic-gate (void) file_out();
7625750Sceastha if (Atflag || SysAtflag) {
7630Sstevel@tonic-gate if (Gen.g_dirfd != -1) {
7640Sstevel@tonic-gate (void) close(Gen.g_dirfd);
7650Sstevel@tonic-gate }
7660Sstevel@tonic-gate Gen.g_dirfd = -1;
7670Sstevel@tonic-gate xattrs_out(file_out);
7680Sstevel@tonic-gate }
7690Sstevel@tonic-gate }
7700Sstevel@tonic-gate if (aclp != NULL) {
771789Sahrens acl_free(aclp);
7720Sstevel@tonic-gate aclp = NULL;
773789Sahrens acl_is_set = 0;
7740Sstevel@tonic-gate }
7750Sstevel@tonic-gate }
7760Sstevel@tonic-gate write_trail();
7770Sstevel@tonic-gate break;
7780Sstevel@tonic-gate case OCp: /* PASS */
7790Sstevel@tonic-gate sl_preview_synonyms();
7800Sstevel@tonic-gate
7810Sstevel@tonic-gate Gen.g_dirfd = -1;
7820Sstevel@tonic-gate Gen.g_passdirfd = -1;
7830Sstevel@tonic-gate Gen.g_dirpath = NULL;
78410202SNobutomo.Nakano@Sun.COM Compress_sparse = 1;
7850Sstevel@tonic-gate while (getname()) {
7860Sstevel@tonic-gate /*
7870Sstevel@tonic-gate * If file is a fully qualified path then
7880Sstevel@tonic-gate * file_pass will strip off the leading '/'
7890Sstevel@tonic-gate * and we need to save off the unstripped
7900Sstevel@tonic-gate * name for attribute traversal.
7910Sstevel@tonic-gate */
7925750Sceastha if (Atflag || SysAtflag) {
7930Sstevel@tonic-gate (void) strcpy(Savenam_p, Gen.g_nam_p);
7940Sstevel@tonic-gate }
7950Sstevel@tonic-gate passret = file_pass();
7960Sstevel@tonic-gate if (aclp != NULL) {
797789Sahrens acl_free(aclp);
7980Sstevel@tonic-gate aclp = NULL;
799789Sahrens acl_is_set = 0;
8000Sstevel@tonic-gate }
8010Sstevel@tonic-gate if (Gen.g_passdirfd != -1)
8020Sstevel@tonic-gate (void) close(Gen.g_passdirfd);
8030Sstevel@tonic-gate Gen.g_passdirfd = -1;
8045750Sceastha if (Atflag || SysAtflag) {
8050Sstevel@tonic-gate if (Gen.g_dirfd != -1) {
8060Sstevel@tonic-gate (void) close(Gen.g_dirfd);
8070Sstevel@tonic-gate }
8080Sstevel@tonic-gate Gen.g_dirfd = -1;
8090Sstevel@tonic-gate if (passret != FILE_LINKED) {
8100Sstevel@tonic-gate Gen.g_nam_p = Savenam_p;
8110Sstevel@tonic-gate xattrs_out(file_pass);
8120Sstevel@tonic-gate }
8130Sstevel@tonic-gate }
8140Sstevel@tonic-gate }
8150Sstevel@tonic-gate break;
8160Sstevel@tonic-gate default:
8170Sstevel@tonic-gate msg(EXT, "Impossible action.");
8180Sstevel@tonic-gate }
8190Sstevel@tonic-gate if (Ofile > 0) {
8200Sstevel@tonic-gate if (close(Ofile) != 0)
8210Sstevel@tonic-gate msg(EXTN, "close error");
8220Sstevel@tonic-gate }
8230Sstevel@tonic-gate if (Archive > 0) {
8240Sstevel@tonic-gate if (close(Archive) != 0)
8250Sstevel@tonic-gate msg(EXTN, "close error");
8260Sstevel@tonic-gate }
8270Sstevel@tonic-gate Blocks = (u_longlong_t)(Blocks * Bufsize + SBlocks + 0x1FF) >> 9;
8280Sstevel@tonic-gate msg(EPOST, "%lld blocks", Blocks);
8290Sstevel@tonic-gate if (Error_cnt)
8300Sstevel@tonic-gate msg(EPOST, "%d error(s)", Error_cnt);
8310Sstevel@tonic-gate return (EXIT_CODE);
8320Sstevel@tonic-gate }
8330Sstevel@tonic-gate
8340Sstevel@tonic-gate /*
8350Sstevel@tonic-gate * add_lnk: Add a linked file's header to the linked file data structure, by
8360Sstevel@tonic-gate * either adding it to the end of an existing sub-list or starting
8370Sstevel@tonic-gate * a new sub-list. Each sub-list saves the links to a given file.
8380Sstevel@tonic-gate *
8390Sstevel@tonic-gate * Directly returns a pointer to the new entry; returns a pointer to the head
8400Sstevel@tonic-gate * of the sub-list in which that entry is located through the argument.
8410Sstevel@tonic-gate */
8420Sstevel@tonic-gate
8430Sstevel@tonic-gate static struct Lnk *
add_lnk(struct Lnk ** sublist_return)8440Sstevel@tonic-gate add_lnk(struct Lnk **sublist_return)
8450Sstevel@tonic-gate {
8460Sstevel@tonic-gate struct Lnk *new_entry, *sublist;
8470Sstevel@tonic-gate
8480Sstevel@tonic-gate for (sublist = Lnk_hd.L_nxt_p;
8490Sstevel@tonic-gate sublist != &Lnk_hd;
8500Sstevel@tonic-gate sublist = sublist->L_nxt_p) {
8510Sstevel@tonic-gate if (sublist->L_gen.g_ino == G_p->g_ino &&
8520Sstevel@tonic-gate sublist->L_gen.g_dev == G_p->g_dev) {
8530Sstevel@tonic-gate /* found */
8540Sstevel@tonic-gate break;
8550Sstevel@tonic-gate }
8560Sstevel@tonic-gate }
8570Sstevel@tonic-gate
8580Sstevel@tonic-gate new_entry = e_zalloc(E_EXIT, sizeof (struct Lnk));
8590Sstevel@tonic-gate
8600Sstevel@tonic-gate new_entry->L_lnk_p = NULL;
8610Sstevel@tonic-gate new_entry->L_gen = *G_p; /* structure copy */
8620Sstevel@tonic-gate
8630Sstevel@tonic-gate new_entry->L_gen.g_nam_p = e_zalloc(E_EXIT, (size_t)G_p->g_namesz);
8640Sstevel@tonic-gate
8650Sstevel@tonic-gate (void) strcpy(new_entry->L_gen.g_nam_p, G_p->g_nam_p);
8660Sstevel@tonic-gate
8670Sstevel@tonic-gate if (sublist == &Lnk_hd) {
8680Sstevel@tonic-gate /* start new sub-list */
8690Sstevel@tonic-gate new_entry->L_nxt_p = &Lnk_hd;
8700Sstevel@tonic-gate new_entry->L_bck_p = Lnk_hd.L_bck_p;
8710Sstevel@tonic-gate Lnk_hd.L_bck_p = new_entry->L_bck_p->L_nxt_p = new_entry;
8720Sstevel@tonic-gate new_entry->L_lnk_p = NULL;
8730Sstevel@tonic-gate new_entry->L_cnt = 1;
8740Sstevel@tonic-gate new_entry->L_data = Onecopy ? 0 : 1;
8750Sstevel@tonic-gate sublist = new_entry;
8760Sstevel@tonic-gate } else {
8770Sstevel@tonic-gate /* add to existing sub-list */
8780Sstevel@tonic-gate struct Lnk *ptr;
8790Sstevel@tonic-gate
8800Sstevel@tonic-gate sublist->L_cnt++;
8810Sstevel@tonic-gate
8820Sstevel@tonic-gate for (ptr = sublist;
8830Sstevel@tonic-gate ptr->L_lnk_p != NULL;
8840Sstevel@tonic-gate ptr = ptr->L_lnk_p) {
8850Sstevel@tonic-gate ptr->L_gen.g_filesz = G_p->g_filesz;
8860Sstevel@tonic-gate }
8870Sstevel@tonic-gate
8880Sstevel@tonic-gate ptr->L_gen.g_filesz = G_p->g_filesz;
8890Sstevel@tonic-gate ptr->L_lnk_p = new_entry;
8900Sstevel@tonic-gate }
8910Sstevel@tonic-gate
8920Sstevel@tonic-gate *sublist_return = sublist;
8930Sstevel@tonic-gate return (new_entry);
8940Sstevel@tonic-gate }
8950Sstevel@tonic-gate
8960Sstevel@tonic-gate /*
8970Sstevel@tonic-gate * bfill: Read req_cnt bytes (out of filelen bytes) from the I/O buffer,
8980Sstevel@tonic-gate * moving them to rd_buf_p. When there are no bytes left in the I/O buffer,
8990Sstevel@tonic-gate * Fillbuf is set and the I/O buffer is filled. The variable dist is the
9000Sstevel@tonic-gate * distance to lseek if an I/O error is encountered with the -k option set
9010Sstevel@tonic-gate * (converted to a multiple of Bufsize).
9020Sstevel@tonic-gate */
9030Sstevel@tonic-gate
9040Sstevel@tonic-gate static int
bfill(void)9050Sstevel@tonic-gate bfill(void)
9060Sstevel@tonic-gate {
9070Sstevel@tonic-gate int i = 0, rv;
9080Sstevel@tonic-gate static int eof = 0;
9090Sstevel@tonic-gate
9100Sstevel@tonic-gate if (!Dflag) {
9110Sstevel@tonic-gate while ((Buffr.b_end_p - Buffr.b_in_p) >= Bufsize) {
9120Sstevel@tonic-gate errno = 0;
9130Sstevel@tonic-gate if ((rv = g_read(Device, Archive, Buffr.b_in_p, Bufsize)) < 0) {
9140Sstevel@tonic-gate if (((Buffr.b_end_p - Buffr.b_in_p) >= Bufsize) &&
9150Sstevel@tonic-gate (Eomflag == 0)) {
9160Sstevel@tonic-gate Eomflag = 1;
9170Sstevel@tonic-gate return (1);
9180Sstevel@tonic-gate }
9190Sstevel@tonic-gate if (errno == ENOSPC) {
9200Sstevel@tonic-gate (void) chgreel(INPUT);
9210Sstevel@tonic-gate if (Hdr_type == BAR) {
9220Sstevel@tonic-gate skip_bar_volhdr();
9230Sstevel@tonic-gate }
9240Sstevel@tonic-gate continue;
9250Sstevel@tonic-gate } else if (Args & OCk) {
9260Sstevel@tonic-gate if (i++ > MX_SEEKS)
9270Sstevel@tonic-gate msg(EXT, "Cannot recover.");
9280Sstevel@tonic-gate if (lseek(Archive, Bufsize, SEEK_REL) < 0)
9290Sstevel@tonic-gate msg(EXTN, "Cannot lseek()");
9300Sstevel@tonic-gate Error_cnt++;
9310Sstevel@tonic-gate Buf_error++;
9320Sstevel@tonic-gate rv = 0;
9330Sstevel@tonic-gate continue;
9340Sstevel@tonic-gate } else
9350Sstevel@tonic-gate ioerror(INPUT);
9360Sstevel@tonic-gate } /* (rv = g_read(Device, Archive ... */
9370Sstevel@tonic-gate if (Hdr_type != BAR || rv == Bufsize) {
9380Sstevel@tonic-gate Buffr.b_in_p += rv;
9390Sstevel@tonic-gate Buffr.b_cnt += (long)rv;
9400Sstevel@tonic-gate }
9410Sstevel@tonic-gate if (rv == Bufsize) {
9420Sstevel@tonic-gate eof = 0;
9430Sstevel@tonic-gate Blocks++;
9440Sstevel@tonic-gate } else if (rv == 0) {
9450Sstevel@tonic-gate if (!eof) {
9460Sstevel@tonic-gate eof = 1;
9470Sstevel@tonic-gate break;
9480Sstevel@tonic-gate }
9490Sstevel@tonic-gate (void) chgreel(INPUT);
9500Sstevel@tonic-gate eof = 0; /* reset the eof after chgreel */
9510Sstevel@tonic-gate
9520Sstevel@tonic-gate /*
9530Sstevel@tonic-gate * if spans multiple volume, skip the volume header of
9540Sstevel@tonic-gate * the next volume so that the file currently being
9550Sstevel@tonic-gate * extracted can continue to be extracted.
9560Sstevel@tonic-gate */
9570Sstevel@tonic-gate if (Hdr_type == BAR) {
9580Sstevel@tonic-gate skip_bar_volhdr();
9590Sstevel@tonic-gate }
9600Sstevel@tonic-gate
9610Sstevel@tonic-gate continue;
9620Sstevel@tonic-gate } else {
9630Sstevel@tonic-gate eof = 0;
9640Sstevel@tonic-gate SBlocks += (u_longlong_t)rv;
9650Sstevel@tonic-gate }
9660Sstevel@tonic-gate } /* (Buffr.b_end_p - Buffr.b_in_p) <= Bufsize */
9670Sstevel@tonic-gate
9680Sstevel@tonic-gate } else { /* Dflag */
9690Sstevel@tonic-gate errno = 0;
9700Sstevel@tonic-gate if ((rv = g_read(Device, Archive, Buffr.b_in_p, Bufsize)) < 0) {
9710Sstevel@tonic-gate return (-1);
9720Sstevel@tonic-gate } /* (rv = g_read(Device, Archive ... */
9730Sstevel@tonic-gate Buffr.b_in_p += rv;
9740Sstevel@tonic-gate Buffr.b_cnt += (long)rv;
9750Sstevel@tonic-gate if (rv == Bufsize) {
9760Sstevel@tonic-gate eof = 0;
9770Sstevel@tonic-gate Blocks++;
9780Sstevel@tonic-gate } else if (!rv) {
9790Sstevel@tonic-gate if (!eof) {
9800Sstevel@tonic-gate eof = 1;
9810Sstevel@tonic-gate return (rv);
9820Sstevel@tonic-gate }
9830Sstevel@tonic-gate return (-1);
9840Sstevel@tonic-gate } else {
9850Sstevel@tonic-gate eof = 0;
9860Sstevel@tonic-gate SBlocks += (u_longlong_t)rv;
9870Sstevel@tonic-gate }
9880Sstevel@tonic-gate }
9890Sstevel@tonic-gate return (rv);
9900Sstevel@tonic-gate }
9910Sstevel@tonic-gate
9920Sstevel@tonic-gate /*
9930Sstevel@tonic-gate * bflush: Move wr_cnt bytes from data_p into the I/O buffer. When the
9940Sstevel@tonic-gate * I/O buffer is full, Flushbuf is set and the buffer is written out.
9950Sstevel@tonic-gate */
9960Sstevel@tonic-gate
9970Sstevel@tonic-gate static void
bflush(void)9980Sstevel@tonic-gate bflush(void)
9990Sstevel@tonic-gate {
10000Sstevel@tonic-gate int rv;
10010Sstevel@tonic-gate
10020Sstevel@tonic-gate while (Buffr.b_cnt >= Bufsize) {
10030Sstevel@tonic-gate errno = 0;
10040Sstevel@tonic-gate if ((rv = g_write(Device, Archive, Buffr.b_out_p,
10050Sstevel@tonic-gate Bufsize)) < 0) {
10060Sstevel@tonic-gate if (errno == ENOSPC && !Dflag)
10070Sstevel@tonic-gate rv = chgreel(OUTPUT);
10080Sstevel@tonic-gate else
10090Sstevel@tonic-gate ioerror(OUTPUT);
10100Sstevel@tonic-gate }
10110Sstevel@tonic-gate Buffr.b_out_p += rv;
10120Sstevel@tonic-gate Buffr.b_cnt -= (long)rv;
10130Sstevel@tonic-gate if (rv == Bufsize)
10140Sstevel@tonic-gate Blocks++;
10150Sstevel@tonic-gate else if (rv > 0)
10160Sstevel@tonic-gate SBlocks += (u_longlong_t)rv;
10170Sstevel@tonic-gate }
10180Sstevel@tonic-gate rstbuf();
10190Sstevel@tonic-gate }
10200Sstevel@tonic-gate
10210Sstevel@tonic-gate /*
10220Sstevel@tonic-gate * chgreel: Determine if end-of-medium has been reached. If it has,
10230Sstevel@tonic-gate * close the current medium and prompt the user for the next medium.
10240Sstevel@tonic-gate */
10250Sstevel@tonic-gate
10260Sstevel@tonic-gate static int
chgreel(int dir)10270Sstevel@tonic-gate chgreel(int dir)
10280Sstevel@tonic-gate {
10290Sstevel@tonic-gate int lastchar, tryagain, askagain, rv;
10300Sstevel@tonic-gate int tmpdev;
10310Sstevel@tonic-gate char str[APATH];
10320Sstevel@tonic-gate struct stat statb;
10330Sstevel@tonic-gate
10340Sstevel@tonic-gate rv = 0;
10350Sstevel@tonic-gate if (fstat(Archive, &statb) < 0)
10360Sstevel@tonic-gate msg(EXTN, "Error during stat() of archive");
10370Sstevel@tonic-gate if ((statb.st_mode & S_IFMT) != S_IFCHR) {
10380Sstevel@tonic-gate if (dir == INPUT) {
10390Sstevel@tonic-gate msg(EXT, "%s%s\n",
10401134Sceastha "Can't read input: end of file encountered ",
10411134Sceastha "prior to expected end of archive.");
10420Sstevel@tonic-gate }
10430Sstevel@tonic-gate }
10440Sstevel@tonic-gate msg(EPOST, "\007End of medium on \"%s\".", dir ? "output" : "input");
10450Sstevel@tonic-gate if (is_floppy(Archive))
10460Sstevel@tonic-gate (void) ioctl(Archive, FDEJECT, NULL);
10470Sstevel@tonic-gate if ((close(Archive) != 0) && (dir == OUTPUT))
10480Sstevel@tonic-gate msg(EXTN, "close error");
10490Sstevel@tonic-gate Archive = 0;
10500Sstevel@tonic-gate Volcnt++;
10510Sstevel@tonic-gate for (;;) {
10525750Sceastha if (Rtty_p == NULL)
10530Sstevel@tonic-gate Rtty_p = fopen(Ttyname, "r");
10540Sstevel@tonic-gate do { /* tryagain */
10550Sstevel@tonic-gate if (IOfil_p) {
10560Sstevel@tonic-gate do {
105710202SNobutomo.Nakano@Sun.COM msg(EPOST, Eom_p, Volcnt);
10580Sstevel@tonic-gate if (!Rtty_p || fgets(str, sizeof (str),
10595750Sceastha Rtty_p) == NULL)
10600Sstevel@tonic-gate msg(EXT, "Cannot read tty.");
10610Sstevel@tonic-gate askagain = 0;
10620Sstevel@tonic-gate switch (*str) {
10630Sstevel@tonic-gate case '\n':
10640Sstevel@tonic-gate (void) strcpy(str, IOfil_p);
10650Sstevel@tonic-gate break;
10660Sstevel@tonic-gate case 'q':
10670Sstevel@tonic-gate exit(EXIT_CODE);
10680Sstevel@tonic-gate default:
10690Sstevel@tonic-gate askagain = 1;
10700Sstevel@tonic-gate }
10710Sstevel@tonic-gate } while (askagain);
10720Sstevel@tonic-gate } else {
10730Sstevel@tonic-gate
10740Sstevel@tonic-gate if (Hdr_type == BAR)
10750Sstevel@tonic-gate Bar_vol_num++;
10760Sstevel@tonic-gate
10770Sstevel@tonic-gate msg(EPOST,
10780Sstevel@tonic-gate "To continue, type device/file name when "
10790Sstevel@tonic-gate "ready.");
10800Sstevel@tonic-gate if (!Rtty_p || fgets(str, sizeof (str),
10815750Sceastha Rtty_p) == NULL)
10820Sstevel@tonic-gate msg(EXT, "Cannot read tty.");
10830Sstevel@tonic-gate lastchar = strlen(str) - 1;
10840Sstevel@tonic-gate if (*(str + lastchar) == '\n') /* remove '\n' */
10850Sstevel@tonic-gate *(str + lastchar) = '\0';
10860Sstevel@tonic-gate if (!*str)
10870Sstevel@tonic-gate exit(EXIT_CODE);
10880Sstevel@tonic-gate }
10890Sstevel@tonic-gate tryagain = 0;
10900Sstevel@tonic-gate if ((Archive = open(str, dir)) < 0) {
10910Sstevel@tonic-gate msg(ERRN, "Cannot open \"%s\"", str);
10920Sstevel@tonic-gate tryagain = 1;
10930Sstevel@tonic-gate }
10940Sstevel@tonic-gate } while (tryagain);
10950Sstevel@tonic-gate (void) g_init(&tmpdev, &Archive);
10960Sstevel@tonic-gate if (tmpdev != Device)
10970Sstevel@tonic-gate msg(EXT, "Cannot change media types in mid-stream.");
10980Sstevel@tonic-gate if (dir == INPUT)
10990Sstevel@tonic-gate break;
11000Sstevel@tonic-gate else { /* dir == OUTPUT */
11010Sstevel@tonic-gate errno = 0;
11020Sstevel@tonic-gate if ((rv = g_write(Device, Archive, Buffr.b_out_p,
11030Sstevel@tonic-gate Bufsize)) == Bufsize)
11040Sstevel@tonic-gate break;
11050Sstevel@tonic-gate else
11060Sstevel@tonic-gate msg(ERR,
11070Sstevel@tonic-gate "Unable to write this medium, try "
11080Sstevel@tonic-gate "another.");
11090Sstevel@tonic-gate }
11100Sstevel@tonic-gate } /* ;; */
11110Sstevel@tonic-gate Eomflag = 0;
11120Sstevel@tonic-gate return (rv);
11130Sstevel@tonic-gate }
11140Sstevel@tonic-gate
11150Sstevel@tonic-gate /*
11160Sstevel@tonic-gate * ckname: Check filenames against user specified patterns,
11170Sstevel@tonic-gate * and/or ask the user for new name when -r is used.
11180Sstevel@tonic-gate */
11190Sstevel@tonic-gate
11200Sstevel@tonic-gate static int
ckname(int flag)11210Sstevel@tonic-gate ckname(int flag)
11220Sstevel@tonic-gate {
11235750Sceastha int lastchar;
11245750Sceastha size_t rename_bufsz = Max_namesz + 1;
11250Sstevel@tonic-gate
11260Sstevel@tonic-gate if (Hdr_type != TAR && Hdr_type != USTAR && Hdr_type != BAR) {
11270Sstevel@tonic-gate /* Re-visit tar size issues later */
11280Sstevel@tonic-gate if (G_p->g_namesz - 1 > Max_namesz) {
11290Sstevel@tonic-gate msg(ERR, "Name exceeds maximum length - skipped.");
11300Sstevel@tonic-gate return (F_SKIP);
11310Sstevel@tonic-gate }
11320Sstevel@tonic-gate }
11330Sstevel@tonic-gate
11340Sstevel@tonic-gate if (Pat_pp && !matched())
11350Sstevel@tonic-gate return (F_SKIP);
11365750Sceastha
11375750Sceastha /* rename interactively */
11385750Sceastha if ((Args & OCr) && !Adir && !G_p->g_rw_sysattr) {
11390Sstevel@tonic-gate (void) fprintf(Wtty_p, gettext("Rename \"%s%s%s\"? "),
11405750Sceastha (G_p->g_attrnam_p == NULL) ? G_p->g_nam_p : Renam_p,
11415750Sceastha (G_p->g_attrnam_p == NULL) ? "" : gettext(" Attribute "),
11425750Sceastha (G_p->g_attrnam_p == NULL) ? "" : G_p->g_attrnam_p);
11430Sstevel@tonic-gate (void) fflush(Wtty_p);
11445750Sceastha if (fgets(Renametmp_p, rename_bufsz, Rtty_p) == NULL)
11450Sstevel@tonic-gate msg(EXT, "Cannot read tty.");
11460Sstevel@tonic-gate if (feof(Rtty_p))
11470Sstevel@tonic-gate exit(EXIT_CODE);
11480Sstevel@tonic-gate lastchar = strlen(Renametmp_p) - 1;
11490Sstevel@tonic-gate
11500Sstevel@tonic-gate /* remove trailing '\n' */
11510Sstevel@tonic-gate if (*(Renametmp_p + lastchar) == '\n')
11520Sstevel@tonic-gate *(Renametmp_p + lastchar) = '\0';
11530Sstevel@tonic-gate if (*Renametmp_p == '\0') {
11540Sstevel@tonic-gate msg(POST, "%s%s%s Skipped.",
11555750Sceastha (G_p->g_attrnam_p == NULL) ? G_p->g_nam_p :
11565750Sceastha G_p->g_attrfnam_p,
11575750Sceastha (G_p->g_attrnam_p == NULL) ? "" :
11585750Sceastha gettext(" Attribute "),
11595750Sceastha (G_p->g_attrnam_p == NULL) ? "" : G_p->g_attrnam_p);
11605750Sceastha if (G_p->g_attrparent_p == NULL) {
11615750Sceastha *G_p->g_nam_p = '\0';
11625750Sceastha }
11635750Sceastha if (Renam_attr_p) {
11645750Sceastha *Renam_attr_p = '\0';
11655750Sceastha }
11660Sstevel@tonic-gate return (F_SKIP);
11670Sstevel@tonic-gate } else if (strcmp(Renametmp_p, ".") != 0) {
11685750Sceastha if (G_p->g_attrnam_p == NULL) {
11695750Sceastha if (strlen(Renametmp_p) > strlen(
11705750Sceastha G_p->g_nam_p)) {
11715750Sceastha if ((G_p->g_nam_p != &nambuf[0]) &&
11725750Sceastha (G_p->g_nam_p != &fullnam[0])) {
11735750Sceastha free(G_p->g_nam_p);
11745750Sceastha G_p->g_nam_p = e_zalloc(E_EXIT,
11755750Sceastha rename_bufsz);
11765750Sceastha }
11775750Sceastha }
11785750Sceastha if (Renam_attr_p) {
11795750Sceastha *Renam_attr_p = '\0';
11805750Sceastha }
11815750Sceastha if ((strlcpy(Renam_p, Renametmp_p,
11825750Sceastha rename_bufsz) > rename_bufsz) ||
11835750Sceastha (strlcpy(G_p->g_nam_p, Renametmp_p,
11845750Sceastha rename_bufsz) > rename_bufsz)) {
11855750Sceastha msg(EXTN, "buffer overflow");
11865750Sceastha }
11870Sstevel@tonic-gate } else {
11885750Sceastha if (G_p->g_attrnam_p != NULL) {
11895750Sceastha free(G_p->g_attrnam_p);
11905750Sceastha G_p->g_attrnam_p = e_strdup(E_EXIT,
11915750Sceastha Renametmp_p);
11925750Sceastha (void) strcpy(G_p->g_nam_p, Renam_p);
11935750Sceastha if (Renam_attr_p) {
11945750Sceastha if (strlcpy(Renam_attr_p,
11955750Sceastha Renametmp_p, rename_bufsz) >
11965750Sceastha rename_bufsz) {
11975750Sceastha msg(EXTN,
11985750Sceastha "buffer overflow");
11995750Sceastha }
12005750Sceastha }
12015750Sceastha }
12025750Sceastha }
12035750Sceastha } else {
12045750Sceastha if (G_p->g_attrnam_p == NULL) {
12055750Sceastha *Renam_p = '\0';
12065750Sceastha }
12075750Sceastha if (Renam_attr_p) {
12085750Sceastha *Renam_attr_p = '\0';
12095750Sceastha }
12100Sstevel@tonic-gate }
12110Sstevel@tonic-gate }
12120Sstevel@tonic-gate if (flag != 0 || Onecopy == 0) {
12130Sstevel@tonic-gate VERBOSE((Args & OCt), G_p->g_nam_p);
12140Sstevel@tonic-gate }
12150Sstevel@tonic-gate if (Args & OCt)
12160Sstevel@tonic-gate return (F_SKIP);
12170Sstevel@tonic-gate return (F_EXTR);
12180Sstevel@tonic-gate }
12190Sstevel@tonic-gate
12200Sstevel@tonic-gate /*
12210Sstevel@tonic-gate * ckopts: Check the validity of all command line options.
12220Sstevel@tonic-gate */
12230Sstevel@tonic-gate
12240Sstevel@tonic-gate static void
ckopts(long mask)12250Sstevel@tonic-gate ckopts(long mask)
12260Sstevel@tonic-gate {
12270Sstevel@tonic-gate int oflag;
12280Sstevel@tonic-gate char *t_p;
12290Sstevel@tonic-gate long errmsk;
12301134Sceastha uid_t Euid = geteuid(); /* Effective uid of invoker */
12311134Sceastha #ifdef SOLARIS_PRIVS
12321134Sceastha priv_set_t *privset;
12333919Sceastha priv_set_t *zones_privset;
12341134Sceastha #endif /* SOLARIS_PRIVS */
12350Sstevel@tonic-gate
12360Sstevel@tonic-gate if (mask & OCi) {
12370Sstevel@tonic-gate errmsk = mask & INV_MSK4i;
12380Sstevel@tonic-gate } else if (mask & OCo) {
12390Sstevel@tonic-gate errmsk = mask & INV_MSK4o;
12400Sstevel@tonic-gate } else if (mask & OCp) {
12410Sstevel@tonic-gate errmsk = mask & INV_MSK4p;
12420Sstevel@tonic-gate } else {
12430Sstevel@tonic-gate msg(ERR, "One of -i, -o or -p must be specified.");
12440Sstevel@tonic-gate errmsk = 0;
12450Sstevel@tonic-gate }
12460Sstevel@tonic-gate
12470Sstevel@tonic-gate if (errmsk) {
12480Sstevel@tonic-gate /* if non-zero, invalid options were specified */
12490Sstevel@tonic-gate Error_cnt++;
12500Sstevel@tonic-gate }
12510Sstevel@tonic-gate
12520Sstevel@tonic-gate if ((mask & OCa) && (mask & OCm) && ((mask & OCi) ||
12531134Sceastha (mask & OCo))) {
12540Sstevel@tonic-gate msg(ERR, "-a and -m are mutually exclusive.");
12550Sstevel@tonic-gate }
12560Sstevel@tonic-gate
125710202SNobutomo.Nakano@Sun.COM if ((mask & OCc) && (mask & OCH) &&
125810202SNobutomo.Nakano@Sun.COM (strcmp("odc", Hdr_p) != 0 && strcmp("odc_sparse", Hdr_p) != 0)) {
12590Sstevel@tonic-gate msg(ERR, "-c and -H are mutually exclusive.");
12600Sstevel@tonic-gate }
12610Sstevel@tonic-gate
12620Sstevel@tonic-gate if ((mask & OCv) && (mask & OCV)) {
12630Sstevel@tonic-gate msg(ERR, "-v and -V are mutually exclusive.");
12640Sstevel@tonic-gate }
12650Sstevel@tonic-gate
12660Sstevel@tonic-gate if ((mask & OCt) && (mask & OCV)) {
12670Sstevel@tonic-gate msg(ERR, "-t and -V are mutually exclusive.");
12680Sstevel@tonic-gate }
12690Sstevel@tonic-gate
12700Sstevel@tonic-gate if ((mask & OCB) && (mask & OCC)) {
12710Sstevel@tonic-gate msg(ERR, "-B and -C are mutually exclusive.");
12720Sstevel@tonic-gate }
12730Sstevel@tonic-gate
12740Sstevel@tonic-gate if ((mask & OCH) && (mask & OC6)) {
12750Sstevel@tonic-gate msg(ERR, "-H and -6 are mutually exclusive.");
12760Sstevel@tonic-gate }
12770Sstevel@tonic-gate
12780Sstevel@tonic-gate if ((mask & OCM) && !((mask & OCI) || (mask & OCO))) {
12790Sstevel@tonic-gate msg(ERR, "-M not meaningful without -O or -I.");
12800Sstevel@tonic-gate }
12810Sstevel@tonic-gate
12820Sstevel@tonic-gate if ((mask & OCA) && !(mask & OCO)) {
12830Sstevel@tonic-gate msg(ERR, "-A requires the -O option.");
12840Sstevel@tonic-gate }
12850Sstevel@tonic-gate
12860Sstevel@tonic-gate if (Bufsize <= 0) {
12870Sstevel@tonic-gate msg(ERR, "Illegal size given for -C option.");
12880Sstevel@tonic-gate }
12890Sstevel@tonic-gate
12900Sstevel@tonic-gate if (mask & OCH) {
12910Sstevel@tonic-gate t_p = Hdr_p;
12920Sstevel@tonic-gate
12930Sstevel@tonic-gate while (*t_p != NULL) {
12940Sstevel@tonic-gate if (isupper(*t_p)) {
12950Sstevel@tonic-gate *t_p = 'a' + (*t_p - 'A');
12960Sstevel@tonic-gate }
12970Sstevel@tonic-gate
12980Sstevel@tonic-gate t_p++;
12990Sstevel@tonic-gate }
13000Sstevel@tonic-gate
13010Sstevel@tonic-gate if (!(strcmp("odc", Hdr_p))) {
13020Sstevel@tonic-gate Hdr_type = CHR;
13030Sstevel@tonic-gate Max_namesz = CPATH;
13040Sstevel@tonic-gate Onecopy = 0;
13050Sstevel@tonic-gate Use_old_stat = 1;
130610202SNobutomo.Nakano@Sun.COM } else if (!(strcmp("odc_sparse", Hdr_p))) {
130710202SNobutomo.Nakano@Sun.COM Hdr_type = CHR;
130810202SNobutomo.Nakano@Sun.COM Max_namesz = CPATH;
130910202SNobutomo.Nakano@Sun.COM Onecopy = 0;
131010202SNobutomo.Nakano@Sun.COM Use_old_stat = 1;
131110202SNobutomo.Nakano@Sun.COM Compress_sparse = 1;
131210202SNobutomo.Nakano@Sun.COM } else if (!(strcmp("ascii_sparse", Hdr_p))) {
131310202SNobutomo.Nakano@Sun.COM Hdr_type = ASC;
131410202SNobutomo.Nakano@Sun.COM Max_namesz = APATH;
131510202SNobutomo.Nakano@Sun.COM Onecopy = 1;
131610202SNobutomo.Nakano@Sun.COM Compress_sparse = 1;
13170Sstevel@tonic-gate } else if (!(strcmp("crc", Hdr_p))) {
13180Sstevel@tonic-gate Hdr_type = CRC;
13190Sstevel@tonic-gate Max_namesz = APATH;
13200Sstevel@tonic-gate Onecopy = 1;
13210Sstevel@tonic-gate } else if (!(strcmp("tar", Hdr_p))) {
13220Sstevel@tonic-gate if (Args & OCo) {
13230Sstevel@tonic-gate Hdr_type = USTAR;
13240Sstevel@tonic-gate Max_namesz = HNAMLEN - 1;
13250Sstevel@tonic-gate } else {
13260Sstevel@tonic-gate Hdr_type = TAR;
13270Sstevel@tonic-gate Max_namesz = TNAMLEN - 1;
13280Sstevel@tonic-gate }
13290Sstevel@tonic-gate Onecopy = 0;
13300Sstevel@tonic-gate } else if (!(strcmp("ustar", Hdr_p))) {
13310Sstevel@tonic-gate Hdr_type = USTAR;
13320Sstevel@tonic-gate Max_namesz = HNAMLEN - 1;
13330Sstevel@tonic-gate Onecopy = 0;
13340Sstevel@tonic-gate } else if (!(strcmp("bar", Hdr_p))) {
13350Sstevel@tonic-gate if ((Args & OCo) || (Args & OCp)) {
13360Sstevel@tonic-gate msg(ERR,
13370Sstevel@tonic-gate "Header type bar can only be used with -i");
13380Sstevel@tonic-gate }
13390Sstevel@tonic-gate
13400Sstevel@tonic-gate if (Args & OCP) {
13410Sstevel@tonic-gate msg(ERR,
13420Sstevel@tonic-gate "Can't preserve using bar header");
13430Sstevel@tonic-gate }
13440Sstevel@tonic-gate
13450Sstevel@tonic-gate Hdr_type = BAR;
13460Sstevel@tonic-gate Max_namesz = TNAMLEN - 1;
13470Sstevel@tonic-gate Onecopy = 0;
13480Sstevel@tonic-gate } else {
13490Sstevel@tonic-gate msg(ERR, "Invalid header \"%s\" specified", Hdr_p);
13500Sstevel@tonic-gate }
13510Sstevel@tonic-gate }
13520Sstevel@tonic-gate
13530Sstevel@tonic-gate if (mask & OCr) {
13540Sstevel@tonic-gate Rtty_p = fopen(Ttyname, "r");
13550Sstevel@tonic-gate Wtty_p = fopen(Ttyname, "w");
13560Sstevel@tonic-gate
13575750Sceastha if (Rtty_p == NULL || Wtty_p == NULL) {
13580Sstevel@tonic-gate msg(ERR, "Cannot rename, \"%s\" missing", Ttyname);
13590Sstevel@tonic-gate }
13600Sstevel@tonic-gate }
13610Sstevel@tonic-gate
13625750Sceastha if ((mask & OCE) && (Ef_p = fopen(Efil_p, "r")) == NULL) {
13630Sstevel@tonic-gate msg(ERR, "Cannot open \"%s\" to read patterns", Efil_p);
13640Sstevel@tonic-gate }
13650Sstevel@tonic-gate
13660Sstevel@tonic-gate if ((mask & OCI) && (Archive = open(IOfil_p, O_RDONLY)) < 0) {
13670Sstevel@tonic-gate msg(ERR, "Cannot open \"%s\" for input", IOfil_p);
13680Sstevel@tonic-gate }
13690Sstevel@tonic-gate
13700Sstevel@tonic-gate if (mask & OCO) {
13710Sstevel@tonic-gate if (mask & OCA) {
13720Sstevel@tonic-gate if ((Archive = open(IOfil_p, O_RDWR)) < 0) {
13730Sstevel@tonic-gate msg(ERR,
13740Sstevel@tonic-gate "Cannot open \"%s\" for append",
13750Sstevel@tonic-gate IOfil_p);
13760Sstevel@tonic-gate }
13770Sstevel@tonic-gate } else {
13780Sstevel@tonic-gate oflag = (O_WRONLY | O_CREAT | O_TRUNC);
13790Sstevel@tonic-gate
13800Sstevel@tonic-gate if ((Archive = open(IOfil_p, oflag, 0777)) < 0) {
13810Sstevel@tonic-gate msg(ERR,
13820Sstevel@tonic-gate "Cannot open \"%s\" for output",
13830Sstevel@tonic-gate IOfil_p);
13840Sstevel@tonic-gate }
13850Sstevel@tonic-gate }
13860Sstevel@tonic-gate }
13870Sstevel@tonic-gate
13881134Sceastha #ifdef SOLARIS_PRIVS
13891134Sceastha if ((privset = priv_allocset()) == NULL) {
13901134Sceastha msg(ERR, "Unable to allocate privilege set");
13911134Sceastha } else if (getppriv(PRIV_EFFECTIVE, privset) != 0) {
13921134Sceastha msg(ERR, "Unable to obtain privilege set");
13931134Sceastha } else {
13943919Sceastha zones_privset = priv_str_to_set("zone", "", NULL);
13953919Sceastha if (zones_privset != NULL) {
13963919Sceastha privileged = (priv_issubset(zones_privset,
13973919Sceastha privset) == B_TRUE);
13983919Sceastha priv_freeset(zones_privset);
13993919Sceastha } else {
14003919Sceastha msg(ERR, "Unable to map privilege to privilege set");
14013919Sceastha }
14021134Sceastha }
14031134Sceastha if (privset != NULL) {
14041134Sceastha priv_freeset(privset);
14051134Sceastha }
14061134Sceastha #else
14071134Sceastha privileged = (Euid == 0);
14081134Sceastha #endif /* SOLARIS_PRIVS */
14091134Sceastha
14100Sstevel@tonic-gate if (mask & OCR) {
14115750Sceastha if ((Rpw_p = getpwnam(Own_p)) == NULL) {
14120Sstevel@tonic-gate msg(ERR, "\"%s\" is not a valid user id", Own_p);
14131134Sceastha } else if ((Euid != Rpw_p->pw_uid) && !privileged) {
14141134Sceastha msg(ERR, "R option only valid for super-user or "
14151134Sceastha "id matches login id of user executing cpio");
14160Sstevel@tonic-gate }
14170Sstevel@tonic-gate }
14180Sstevel@tonic-gate
14190Sstevel@tonic-gate if ((mask & OCo) && !(mask & OCO)) {
14200Sstevel@tonic-gate Out_p = stderr;
14210Sstevel@tonic-gate }
14220Sstevel@tonic-gate
14230Sstevel@tonic-gate if ((mask & OCp) && ((mask & (OCB|OCC)) == 0)) {
14240Sstevel@tonic-gate /*
14250Sstevel@tonic-gate * We are in pass mode with no block size specified. Use the
14260Sstevel@tonic-gate * larger of the native page size and 8192.
14270Sstevel@tonic-gate */
14280Sstevel@tonic-gate
14290Sstevel@tonic-gate Bufsize = (PageSize > 8192) ? PageSize : 8192;
14300Sstevel@tonic-gate }
14310Sstevel@tonic-gate }
14320Sstevel@tonic-gate
14330Sstevel@tonic-gate /*
14340Sstevel@tonic-gate * cksum: Calculate the simple checksum of a file (CRC) or header
14350Sstevel@tonic-gate * (TARTYP (TAR and USTAR)). For -o and the CRC header, the file is opened and
14360Sstevel@tonic-gate * the checksum is calculated. For -i and the CRC header, the checksum
14370Sstevel@tonic-gate * is calculated as each block is transferred from the archive I/O buffer
14380Sstevel@tonic-gate * to the file system I/O buffer. The TARTYP (TAR and USTAR) headers calculate
14390Sstevel@tonic-gate * the simple checksum of the header (with the checksum field of the
14400Sstevel@tonic-gate * header initialized to all spaces (\040).
14410Sstevel@tonic-gate */
14420Sstevel@tonic-gate
14430Sstevel@tonic-gate static long
cksum(char hdr,int byt_cnt,int * err)14440Sstevel@tonic-gate cksum(char hdr, int byt_cnt, int *err)
14450Sstevel@tonic-gate {
14460Sstevel@tonic-gate char *crc_p, *end_p;
14470Sstevel@tonic-gate int cnt;
14480Sstevel@tonic-gate long checksum = 0L, have;
14490Sstevel@tonic-gate off_t lcnt;
14500Sstevel@tonic-gate
14510Sstevel@tonic-gate if (err != NULL)
14520Sstevel@tonic-gate *err = 0;
14530Sstevel@tonic-gate switch (hdr) {
14540Sstevel@tonic-gate case CRC:
14550Sstevel@tonic-gate if (Args & OCi) { /* do running checksum */
14560Sstevel@tonic-gate end_p = Buffr.b_out_p + byt_cnt;
14570Sstevel@tonic-gate for (crc_p = Buffr.b_out_p; crc_p < end_p; crc_p++)
14580Sstevel@tonic-gate checksum += (long)*crc_p;
14590Sstevel@tonic-gate break;
14600Sstevel@tonic-gate }
14610Sstevel@tonic-gate /* OCo - do checksum of file */
14620Sstevel@tonic-gate lcnt = G_p->g_filesz;
14630Sstevel@tonic-gate
14640Sstevel@tonic-gate while (lcnt > 0) {
14650Sstevel@tonic-gate have = (lcnt < Bufsize) ? lcnt : Bufsize;
14660Sstevel@tonic-gate errno = 0;
14670Sstevel@tonic-gate if (read(Ifile, Buf_p, have) != have) {
14680Sstevel@tonic-gate msg(ERR, "Error computing checksum.");
14690Sstevel@tonic-gate if (err != NULL)
14700Sstevel@tonic-gate *err = 1;
14710Sstevel@tonic-gate break;
14720Sstevel@tonic-gate }
14730Sstevel@tonic-gate end_p = Buf_p + have;
14740Sstevel@tonic-gate for (crc_p = Buf_p; crc_p < end_p; crc_p++)
14750Sstevel@tonic-gate checksum += (long)*crc_p;
14760Sstevel@tonic-gate lcnt -= have;
14770Sstevel@tonic-gate }
14780Sstevel@tonic-gate if (lseek(Ifile, (off_t)0, SEEK_ABS) < 0)
14790Sstevel@tonic-gate msg(ERRN, "Cannot reset file after checksum");
14800Sstevel@tonic-gate break;
14810Sstevel@tonic-gate case TARTYP: /* TAR and USTAR */
14820Sstevel@tonic-gate crc_p = Thdr_p->tbuf.t_cksum;
14830Sstevel@tonic-gate for (cnt = 0; cnt < TCRCLEN; cnt++) {
14840Sstevel@tonic-gate *crc_p = '\040';
14850Sstevel@tonic-gate crc_p++;
14860Sstevel@tonic-gate }
14870Sstevel@tonic-gate crc_p = (char *)Thdr_p;
14880Sstevel@tonic-gate for (cnt = 0; cnt < TARSZ; cnt++) {
14890Sstevel@tonic-gate /*
14900Sstevel@tonic-gate * tar uses unsigned checksum, so we must use unsigned
14910Sstevel@tonic-gate * here in order to be able to read tar archives.
14920Sstevel@tonic-gate */
14930Sstevel@tonic-gate checksum += (long)((unsigned char)(*crc_p));
14940Sstevel@tonic-gate crc_p++;
14950Sstevel@tonic-gate }
14960Sstevel@tonic-gate break;
14970Sstevel@tonic-gate default:
14980Sstevel@tonic-gate msg(EXT, "Impossible header type.");
14990Sstevel@tonic-gate } /* hdr */
15000Sstevel@tonic-gate return (checksum);
15010Sstevel@tonic-gate }
15020Sstevel@tonic-gate
15030Sstevel@tonic-gate /*
15040Sstevel@tonic-gate * creat_hdr: Fill in the generic header structure with the specific
15050Sstevel@tonic-gate * header information based on the value of Hdr_type.
15060Sstevel@tonic-gate *
15070Sstevel@tonic-gate * return (1) if this process was successful, and (0) otherwise.
15080Sstevel@tonic-gate */
15090Sstevel@tonic-gate
15100Sstevel@tonic-gate static int
creat_hdr(void)15110Sstevel@tonic-gate creat_hdr(void)
15120Sstevel@tonic-gate {
15130Sstevel@tonic-gate ushort_t ftype;
15140Sstevel@tonic-gate int fullnamesize;
15150Sstevel@tonic-gate dev_t dev;
15160Sstevel@tonic-gate ino_t ino;
15170Sstevel@tonic-gate
15180Sstevel@tonic-gate ftype = SrcSt.st_mode & Ftype;
15190Sstevel@tonic-gate Adir = (ftype == S_IFDIR);
15205276Sceastha Aspec = (ftype == S_IFBLK || ftype == S_IFCHR || ftype == S_IFIFO ||
15215276Sceastha ftype == S_IFSOCK);
15220Sstevel@tonic-gate switch (Hdr_type) {
15230Sstevel@tonic-gate case BIN:
15240Sstevel@tonic-gate Gen.g_magic = CMN_BIN;
15250Sstevel@tonic-gate break;
15260Sstevel@tonic-gate case CHR:
15270Sstevel@tonic-gate Gen.g_magic = CMN_BIN;
15280Sstevel@tonic-gate break;
15290Sstevel@tonic-gate case ASC:
15300Sstevel@tonic-gate Gen.g_magic = CMN_ASC;
15310Sstevel@tonic-gate break;
15320Sstevel@tonic-gate case CRC:
15330Sstevel@tonic-gate Gen.g_magic = CMN_CRC;
15340Sstevel@tonic-gate break;
15350Sstevel@tonic-gate case USTAR:
15360Sstevel@tonic-gate /*
15370Sstevel@tonic-gate * If the length of the full name is greater than 256,
15380Sstevel@tonic-gate * print out a message and return.
15390Sstevel@tonic-gate */
15400Sstevel@tonic-gate if ((fullnamesize = strlen(Gen.g_nam_p)) > MAXNAM) {
15410Sstevel@tonic-gate msg(ERR,
15420Sstevel@tonic-gate "%s: file name too long", Gen.g_nam_p);
15430Sstevel@tonic-gate return (0);
15440Sstevel@tonic-gate } else if (fullnamesize > NAMSIZ) {
15450Sstevel@tonic-gate /*
15460Sstevel@tonic-gate * The length of the full name is greater than
15470Sstevel@tonic-gate * 100, so we must split the filename from the
15480Sstevel@tonic-gate * path
15490Sstevel@tonic-gate */
15500Sstevel@tonic-gate char namebuff[NAMSIZ+1];
15510Sstevel@tonic-gate char prebuff[PRESIZ+1];
15520Sstevel@tonic-gate char *lastslash;
15530Sstevel@tonic-gate int presize, namesize;
15540Sstevel@tonic-gate
15550Sstevel@tonic-gate (void) memset(namebuff, '\0',
15560Sstevel@tonic-gate sizeof (namebuff));
15570Sstevel@tonic-gate (void) memset(prebuff, '\0', sizeof (prebuff));
15580Sstevel@tonic-gate
15590Sstevel@tonic-gate lastslash = strrchr(Gen.g_nam_p, '/');
15600Sstevel@tonic-gate
15610Sstevel@tonic-gate if (lastslash != NULL) {
15620Sstevel@tonic-gate namesize = strlen(++lastslash);
15630Sstevel@tonic-gate presize = fullnamesize - namesize - 1;
15640Sstevel@tonic-gate } else {
15650Sstevel@tonic-gate namesize = fullnamesize;
15660Sstevel@tonic-gate lastslash = Gen.g_nam_p;
15670Sstevel@tonic-gate presize = 0;
15680Sstevel@tonic-gate }
15690Sstevel@tonic-gate
15700Sstevel@tonic-gate /*
15710Sstevel@tonic-gate * If the filename is greater than 100 we can't
15720Sstevel@tonic-gate * archive the file
15730Sstevel@tonic-gate */
15740Sstevel@tonic-gate if (namesize > NAMSIZ) {
15750Sstevel@tonic-gate msg(ERR,
15760Sstevel@tonic-gate "%s: filename is greater than %d",
15770Sstevel@tonic-gate lastslash, NAMSIZ);
15780Sstevel@tonic-gate return (0);
15790Sstevel@tonic-gate }
15800Sstevel@tonic-gate (void) strncpy(&namebuff[0], lastslash,
15810Sstevel@tonic-gate namesize);
15820Sstevel@tonic-gate /*
15830Sstevel@tonic-gate * If the prefix is greater than 155 we can't
15840Sstevel@tonic-gate * archive the file.
15850Sstevel@tonic-gate */
15860Sstevel@tonic-gate if (presize > PRESIZ) {
15870Sstevel@tonic-gate msg(ERR,
15880Sstevel@tonic-gate "%s: prefix is greater than %d",
15890Sstevel@tonic-gate Gen.g_nam_p, PRESIZ);
15900Sstevel@tonic-gate return (0);
15910Sstevel@tonic-gate }
15920Sstevel@tonic-gate (void) strncpy(&prebuff[0], Gen.g_nam_p,
15930Sstevel@tonic-gate presize);
15940Sstevel@tonic-gate
15950Sstevel@tonic-gate Gen.g_tname = e_zalloc(E_EXIT, namesize + 1);
15960Sstevel@tonic-gate (void) strcpy(Gen.g_tname, namebuff);
15970Sstevel@tonic-gate
15980Sstevel@tonic-gate Gen.g_prefix = e_zalloc(E_EXIT, presize + 1);
15990Sstevel@tonic-gate (void) strcpy(Gen.g_prefix, prebuff);
16000Sstevel@tonic-gate } else {
16010Sstevel@tonic-gate Gen.g_tname = Gen.g_nam_p;
16020Sstevel@tonic-gate }
16030Sstevel@tonic-gate (void) strcpy(Gen.g_tmagic, "ustar");
16040Sstevel@tonic-gate (void) strcpy(Gen.g_version, "00");
16050Sstevel@tonic-gate
16060Sstevel@tonic-gate dpasswd = getpwuid(SrcSt.st_uid);
16075750Sceastha if (dpasswd == NULL) {
16080Sstevel@tonic-gate msg(EPOST,
16090Sstevel@tonic-gate "cpio: could not get passwd information "
16100Sstevel@tonic-gate "for %s%s%s",
16115750Sceastha (Gen.g_attrnam_p == NULL) ?
16120Sstevel@tonic-gate Gen.g_nam_p : Gen.g_attrfnam_p,
16135750Sceastha (Gen.g_attrnam_p == NULL) ?
16145750Sceastha "" : Gen.g_rw_sysattr ?
16155750Sceastha gettext(" System Attribute ") :
16165750Sceastha gettext(" Attribute "),
16175750Sceastha (Gen.g_attrnam_p == NULL) ?
16180Sstevel@tonic-gate "" : Gen.g_attrnam_p);
16190Sstevel@tonic-gate /* make name null string */
16200Sstevel@tonic-gate Gen.g_uname[0] = '\0';
16210Sstevel@tonic-gate } else {
16220Sstevel@tonic-gate (void) strncpy(&Gen.g_uname[0],
16230Sstevel@tonic-gate dpasswd->pw_name, 32);
16240Sstevel@tonic-gate }
16250Sstevel@tonic-gate dgroup = getgrgid(SrcSt.st_gid);
16265750Sceastha if (dgroup == NULL) {
16270Sstevel@tonic-gate msg(EPOST,
16280Sstevel@tonic-gate "cpio: could not get group information "
162910202SNobutomo.Nakano@Sun.COM "for %s%s%s",
16305750Sceastha (Gen.g_attrnam_p == NULL) ?
16310Sstevel@tonic-gate Gen.g_nam_p : Gen.g_attrfnam_p,
16325750Sceastha (Gen.g_attrnam_p == NULL) ?
16335750Sceastha "" : Gen.g_rw_sysattr ?
16345750Sceastha gettext(" System Attribute ") :
16355750Sceastha gettext(" Attribute "),
16365750Sceastha (Gen.g_attrnam_p == NULL) ?
16370Sstevel@tonic-gate "" : Gen.g_attrnam_p);
16380Sstevel@tonic-gate /* make name null string */
16390Sstevel@tonic-gate Gen.g_gname[0] = '\0';
16400Sstevel@tonic-gate } else {
16410Sstevel@tonic-gate (void) strncpy(&Gen.g_gname[0],
16420Sstevel@tonic-gate dgroup->gr_name, 32);
16430Sstevel@tonic-gate }
16440Sstevel@tonic-gate Gen.g_typeflag = tartype(ftype);
16450Sstevel@tonic-gate /* FALLTHROUGH */
16460Sstevel@tonic-gate case TAR:
16470Sstevel@tonic-gate (void) memset(T_lname, '\0', sizeof (T_lname));
16480Sstevel@tonic-gate break;
16490Sstevel@tonic-gate default:
16500Sstevel@tonic-gate msg(EXT, "Impossible header type.");
16510Sstevel@tonic-gate }
16520Sstevel@tonic-gate
16535750Sceastha if (Use_old_stat && (Gen.g_attrnam_p != NULL)) {
16545750Sceastha /*
16555750Sceastha * When processing extended attributes, creat_hdr()
16565750Sceastha * can get called multiple times which means that
16575750Sceastha * SrcSt.st.st_dev would have gotten converted to
16585750Sceastha * -Hodc format. We should always use the original
16595750Sceastha * device here as we need to be able to match on
16605750Sceastha * the original device id from the file that was
16615750Sceastha * previewed in sl_preview_synonyms().
16625750Sceastha */
16635750Sceastha dev = Savedev;
16645750Sceastha } else {
16655750Sceastha dev = SrcSt.st_dev;
16665750Sceastha }
16670Sstevel@tonic-gate ino = SrcSt.st_ino;
16680Sstevel@tonic-gate
16690Sstevel@tonic-gate if (Use_old_stat) {
16700Sstevel@tonic-gate SrcSt = *OldSt;
16710Sstevel@tonic-gate }
16720Sstevel@tonic-gate
16730Sstevel@tonic-gate Gen.g_namesz = strlen(Gen.g_nam_p) + 1;
16740Sstevel@tonic-gate Gen.g_uid = SrcSt.st_uid;
16750Sstevel@tonic-gate Gen.g_gid = SrcSt.st_gid;
16760Sstevel@tonic-gate Gen.g_dev = SrcSt.st_dev;
16770Sstevel@tonic-gate
16780Sstevel@tonic-gate if (Use_old_stat) {
16790Sstevel@tonic-gate /* -Hodc */
16800Sstevel@tonic-gate
16815750Sceastha sl_info_t *p = sl_search(dev, ino, ftype);
16820Sstevel@tonic-gate Gen.g_ino = p ? p->sl_ino2 : -1;
16830Sstevel@tonic-gate
16840Sstevel@tonic-gate if (Gen.g_ino == (ulong_t)-1) {
16850Sstevel@tonic-gate msg(ERR, "%s%s%s: cannot be archived - inode too big "
16860Sstevel@tonic-gate "for -Hodc format",
16875750Sceastha (Gen.g_attrnam_p == NULL) ?
16880Sstevel@tonic-gate Gen.g_nam_p : Gen.g_attrfnam_p,
16895750Sceastha (Gen.g_attrnam_p == NULL) ? "" : Gen.g_rw_sysattr ?
16905750Sceastha gettext(" System Attribute ") :
16915750Sceastha gettext(" Attribute "),
16925750Sceastha (Gen.g_attrnam_p == NULL) ? "" : Gen.g_attrnam_p);
16930Sstevel@tonic-gate return (0);
16940Sstevel@tonic-gate }
16950Sstevel@tonic-gate } else {
16960Sstevel@tonic-gate Gen.g_ino = SrcSt.st_ino;
16970Sstevel@tonic-gate }
16980Sstevel@tonic-gate
16990Sstevel@tonic-gate Gen.g_mode = SrcSt.st_mode;
17000Sstevel@tonic-gate Gen.g_mtime = SrcSt.st_mtime;
17015750Sceastha Gen.g_nlink = Adir ? SrcSt.st_nlink : sl_numlinks(dev, ino, ftype);
17020Sstevel@tonic-gate
17030Sstevel@tonic-gate if (ftype == S_IFREG || ftype == S_IFLNK)
17040Sstevel@tonic-gate Gen.g_filesz = (off_t)SrcSt.st_size;
17050Sstevel@tonic-gate else
17060Sstevel@tonic-gate Gen.g_filesz = (off_t)0;
17070Sstevel@tonic-gate Gen.g_rdev = SrcSt.st_rdev;
17080Sstevel@tonic-gate return (1);
17090Sstevel@tonic-gate }
17100Sstevel@tonic-gate
17110Sstevel@tonic-gate /*
17120Sstevel@tonic-gate * creat_lnk: Create a link from the existing name1_p to name2_p.
17130Sstevel@tonic-gate */
17140Sstevel@tonic-gate
17150Sstevel@tonic-gate static
17160Sstevel@tonic-gate int
creat_lnk(int dirfd,char * name1_p,char * name2_p)17170Sstevel@tonic-gate creat_lnk(int dirfd, char *name1_p, char *name2_p)
17180Sstevel@tonic-gate {
17190Sstevel@tonic-gate int cnt = 0;
17200Sstevel@tonic-gate
17210Sstevel@tonic-gate do {
17220Sstevel@tonic-gate errno = 0;
17230Sstevel@tonic-gate if (!link(name1_p, name2_p)) {
17240Sstevel@tonic-gate if (aclp != NULL) {
1725789Sahrens acl_free(aclp);
17260Sstevel@tonic-gate aclp = NULL;
1727789Sahrens acl_is_set = 0;
17280Sstevel@tonic-gate }
17290Sstevel@tonic-gate cnt = 0;
17300Sstevel@tonic-gate break;
17311134Sceastha } else if ((errno == EEXIST) && (cnt == 0)) {
17321134Sceastha struct stat lsb1;
17331134Sceastha struct stat lsb2;
17341134Sceastha
17351134Sceastha /*
17361134Sceastha * Check to see if we are trying to link this
17371134Sceastha * file to itself. If so, count the effort as
17381134Sceastha * successful. If the two files are different,
17391134Sceastha * or if either lstat is unsuccessful, proceed
17401134Sceastha * as we would have otherwise; the appropriate
17411134Sceastha * error will be reported subsequently.
17421134Sceastha */
17431134Sceastha
17441134Sceastha if (lstat(name1_p, &lsb1) != 0) {
17451134Sceastha msg(ERR, "Cannot lstat source file %s",
17461134Sceastha name1_p);
17471134Sceastha } else {
17481134Sceastha if (lstat(name2_p, &lsb2) != 0) {
17491134Sceastha msg(ERR, "Cannot lstat "
17501134Sceastha "destination file %s", name2_p);
17510Sstevel@tonic-gate } else {
17521134Sceastha if (lsb1.st_dev == lsb2.st_dev &&
17531134Sceastha lsb1.st_ino == lsb2.st_ino) {
17541134Sceastha VERBOSE((Args & (OCv | OCV)),
17550Sstevel@tonic-gate name2_p);
17561134Sceastha return (0);
17570Sstevel@tonic-gate }
17580Sstevel@tonic-gate }
17590Sstevel@tonic-gate }
17600Sstevel@tonic-gate
17610Sstevel@tonic-gate if (!(Args & OCu) && G_p->g_mtime <= DesSt.st_mtime)
17621134Sceastha msg(ERR, "Existing \"%s\" same age or newer",
17630Sstevel@tonic-gate name2_p);
17640Sstevel@tonic-gate else if (unlinkat(dirfd, get_component(name2_p), 0) < 0)
17650Sstevel@tonic-gate msg(ERRN, "Error cannot unlink \"%s\"",
17660Sstevel@tonic-gate name2_p);
17670Sstevel@tonic-gate }
17680Sstevel@tonic-gate cnt++;
17690Sstevel@tonic-gate } while ((cnt < 2) && missdir(name2_p) == 0);
17700Sstevel@tonic-gate if (!cnt) {
17710Sstevel@tonic-gate char *newname;
17720Sstevel@tonic-gate char *fromname;
17730Sstevel@tonic-gate char *attrname;
17740Sstevel@tonic-gate
17750Sstevel@tonic-gate newname = name2_p;
17760Sstevel@tonic-gate fromname = name1_p;
17770Sstevel@tonic-gate attrname = Gen.g_attrnam_p;
17780Sstevel@tonic-gate if (attrname) {
17790Sstevel@tonic-gate if (Args & OCp) {
17800Sstevel@tonic-gate newname = fromname = Fullnam_p;
17810Sstevel@tonic-gate } else {
17820Sstevel@tonic-gate newname = Gen.g_attrfnam_p;
17830Sstevel@tonic-gate }
17840Sstevel@tonic-gate }
17850Sstevel@tonic-gate if (Args & OCv) {
17860Sstevel@tonic-gate (void) fprintf(Err_p,
17870Sstevel@tonic-gate gettext("%s%s%s linked to %s%s%s\n"), newname,
17885750Sceastha (attrname == NULL) ? "" : gettext(" attribute "),
17895750Sceastha (attrname == NULL) ? "" : attrname,
17905750Sceastha (attrname == NULL) ? fromname : newname,
17915750Sceastha (attrname == NULL) ? "" : gettext(" attribute "),
17925750Sceastha (attrname == NULL) ? "" : name1_p);
17935750Sceastha } else {
17945750Sceastha VERBOSE((Args & (OCv | OCV)), newname);
17955750Sceastha }
17960Sstevel@tonic-gate } else if (cnt == 1)
17970Sstevel@tonic-gate msg(ERRN,
17980Sstevel@tonic-gate "Unable to create directory for \"%s\"", name2_p);
17990Sstevel@tonic-gate else if (cnt == 2)
18000Sstevel@tonic-gate msg(ERRN,
18010Sstevel@tonic-gate "Cannot link \"%s\" and \"%s\"", name1_p, name2_p);
18020Sstevel@tonic-gate return (cnt);
18030Sstevel@tonic-gate }
18040Sstevel@tonic-gate
18050Sstevel@tonic-gate /*
18060Sstevel@tonic-gate * creat_spec:
18070Sstevel@tonic-gate * Create one of the following:
18080Sstevel@tonic-gate * directory
18090Sstevel@tonic-gate * character special file
18100Sstevel@tonic-gate * block special file
18110Sstevel@tonic-gate * fifo
18125276Sceastha * socket
18130Sstevel@tonic-gate */
18140Sstevel@tonic-gate
18150Sstevel@tonic-gate static int
creat_spec(int dirfd)18160Sstevel@tonic-gate creat_spec(int dirfd)
18170Sstevel@tonic-gate {
18180Sstevel@tonic-gate char *nam_p;
18190Sstevel@tonic-gate int cnt, result, rv = 0;
18200Sstevel@tonic-gate char *curdir;
18210Sstevel@tonic-gate char *lastslash;
18220Sstevel@tonic-gate
18230Sstevel@tonic-gate Do_rename = 0; /* creat_tmp() may reset this */
18240Sstevel@tonic-gate
18250Sstevel@tonic-gate if (Args & OCp) {
18260Sstevel@tonic-gate nam_p = Fullnam_p;
18270Sstevel@tonic-gate } else {
18280Sstevel@tonic-gate nam_p = G_p->g_nam_p;
18290Sstevel@tonic-gate }
18300Sstevel@tonic-gate
18310Sstevel@tonic-gate /*
18320Sstevel@tonic-gate * Is this the extraction of the hidden attribute directory?
18335750Sceastha * If we are processing the hidden attribute directory of an
18345750Sceastha * attribute, then just return as modes and times cannot be set.
18355750Sceastha * Otherwise, if we are processing a hidden attribute, just set
18365750Sceastha * the mode/times correctly and return.
18370Sstevel@tonic-gate */
18380Sstevel@tonic-gate
18390Sstevel@tonic-gate if (Hiddendir) {
18405750Sceastha if (G_p->g_attrparent_p == NULL) {
18415750Sceastha if (Args & OCR) {
18425750Sceastha if (fchownat(dirfd, ".", Rpw_p->pw_uid,
18435750Sceastha Rpw_p->pw_gid, 0) != 0) {
18445750Sceastha msg(ERRN,
18455750Sceastha "Cannot chown() \"attribute "
18465750Sceastha "directory of file %s\"",
18475750Sceastha G_p->g_attrfnam_p);
18485750Sceastha }
18495750Sceastha } else if ((fchownat(dirfd, ".", G_p->g_uid,
18505750Sceastha G_p->g_gid, 0) != 0) && privileged) {
18511134Sceastha msg(ERRN,
18521134Sceastha "Cannot chown() \"attribute directory of "
18531134Sceastha "file %s\"", G_p->g_attrfnam_p);
18541134Sceastha }
18555750Sceastha
18565750Sceastha if (fchmod(dirfd, G_p->g_mode) != 0) {
18570Sstevel@tonic-gate msg(ERRN,
18585750Sceastha "Cannot chmod() \"attribute directory of "
18595750Sceastha "file %s\"", G_p->g_attrfnam_p);
18605750Sceastha }
18615750Sceastha
18625750Sceastha acl_is_set = 0;
18635750Sceastha if (Pflag && aclp != NULL) {
18645750Sceastha if (facl_set(dirfd, aclp) < 0) {
18655750Sceastha msg(ERRN,
18665750Sceastha "failed to set acl on attribute"
18675750Sceastha " directory of %s ",
18685750Sceastha G_p->g_attrfnam_p);
18695750Sceastha } else {
18705750Sceastha acl_is_set = 1;
18715750Sceastha }
18725750Sceastha acl_free(aclp);
18735750Sceastha aclp = NULL;
18745750Sceastha }
18750Sstevel@tonic-gate }
18760Sstevel@tonic-gate
18770Sstevel@tonic-gate return (1);
18780Sstevel@tonic-gate }
18790Sstevel@tonic-gate
18800Sstevel@tonic-gate result = stat(nam_p, &DesSt);
18810Sstevel@tonic-gate
18820Sstevel@tonic-gate if (ustar_dir() || Adir) {
18830Sstevel@tonic-gate /*
18840Sstevel@tonic-gate * The archive file is a directory.
18850Sstevel@tonic-gate * Skip "." and ".."
18860Sstevel@tonic-gate */
18870Sstevel@tonic-gate
18880Sstevel@tonic-gate curdir = strrchr(nam_p, '.');
18890Sstevel@tonic-gate
18900Sstevel@tonic-gate if (curdir != NULL && curdir[1] == NULL) {
18910Sstevel@tonic-gate lastslash = strrchr(nam_p, '/');
18920Sstevel@tonic-gate
18930Sstevel@tonic-gate if (lastslash != NULL) {
18940Sstevel@tonic-gate lastslash++;
18950Sstevel@tonic-gate } else {
18960Sstevel@tonic-gate lastslash = nam_p;
18970Sstevel@tonic-gate }
18980Sstevel@tonic-gate
18990Sstevel@tonic-gate if (!(strcmp(lastslash, ".")) ||
19000Sstevel@tonic-gate !(strcmp(lastslash, ".."))) {
19010Sstevel@tonic-gate return (1);
19020Sstevel@tonic-gate }
19030Sstevel@tonic-gate }
19040Sstevel@tonic-gate
19050Sstevel@tonic-gate if (result == 0) {
19060Sstevel@tonic-gate /* A file by the same name exists. */
19070Sstevel@tonic-gate
19080Sstevel@tonic-gate /* Take care of ACLs */
1909789Sahrens acl_is_set = 0;
19100Sstevel@tonic-gate
19110Sstevel@tonic-gate if (Pflag && aclp != NULL) {
1912789Sahrens if (acl_set(nam_p, aclp) < 0) {
19130Sstevel@tonic-gate msg(ERRN,
19140Sstevel@tonic-gate "\"%s\": failed to set acl",
19150Sstevel@tonic-gate nam_p);
19160Sstevel@tonic-gate } else {
1917789Sahrens acl_is_set = 1;
19180Sstevel@tonic-gate }
19190Sstevel@tonic-gate
1920789Sahrens acl_free(aclp);
19210Sstevel@tonic-gate aclp = NULL;
19220Sstevel@tonic-gate }
19230Sstevel@tonic-gate if (Args & OCd) {
19240Sstevel@tonic-gate /*
19250Sstevel@tonic-gate * We are creating directories. Keep the
19260Sstevel@tonic-gate * existing file.
19270Sstevel@tonic-gate */
19280Sstevel@tonic-gate
19290Sstevel@tonic-gate rstfiles(U_KEEP, dirfd);
19300Sstevel@tonic-gate }
19310Sstevel@tonic-gate
19320Sstevel@tonic-gate /* Report success. */
19330Sstevel@tonic-gate
19340Sstevel@tonic-gate return (1);
19350Sstevel@tonic-gate }
19360Sstevel@tonic-gate } else {
19370Sstevel@tonic-gate /* The archive file is not a directory. */
19380Sstevel@tonic-gate
19390Sstevel@tonic-gate if (result == 0) {
19400Sstevel@tonic-gate /*
19410Sstevel@tonic-gate * A file by the same name exists. Move it to a
19420Sstevel@tonic-gate * temporary file.
19430Sstevel@tonic-gate */
19440Sstevel@tonic-gate
19450Sstevel@tonic-gate if (creat_tmp(nam_p) < 0) {
19460Sstevel@tonic-gate /*
19470Sstevel@tonic-gate * We weren't able to create the temp file.
19480Sstevel@tonic-gate * Report failure.
19490Sstevel@tonic-gate */
19500Sstevel@tonic-gate
19510Sstevel@tonic-gate return (0);
19520Sstevel@tonic-gate }
19530Sstevel@tonic-gate }
19540Sstevel@tonic-gate }
19550Sstevel@tonic-gate
19560Sstevel@tonic-gate /*
19570Sstevel@tonic-gate * This pile tries to create the file directly, and, if there is a
19580Sstevel@tonic-gate * problem, creates missing directories, and then tries to create the
19590Sstevel@tonic-gate * file again. Two strikes and you're out.
19600Sstevel@tonic-gate */
19610Sstevel@tonic-gate
19620Sstevel@tonic-gate cnt = 0;
19630Sstevel@tonic-gate
19640Sstevel@tonic-gate do {
19650Sstevel@tonic-gate if (ustar_dir() || Adir) {
19660Sstevel@tonic-gate /* The archive file is a directory. */
19670Sstevel@tonic-gate
19680Sstevel@tonic-gate result = mkdir(nam_p, G_p->g_mode);
19690Sstevel@tonic-gate } else if (ustar_spec() || Aspec) {
19700Sstevel@tonic-gate /*
19710Sstevel@tonic-gate * The archive file is block special,
19725276Sceastha * char special, socket, or a fifo.
19735276Sceastha * Note that, for a socket, the third
19745276Sceastha * parameter to mknod() is ignored.
19750Sstevel@tonic-gate */
19760Sstevel@tonic-gate
19770Sstevel@tonic-gate result = mknod(nam_p, (int)G_p->g_mode,
19780Sstevel@tonic-gate (int)G_p->g_rdev);
19790Sstevel@tonic-gate }
19800Sstevel@tonic-gate
19810Sstevel@tonic-gate if (result >= 0) {
19820Sstevel@tonic-gate /*
19830Sstevel@tonic-gate * The file creation succeeded. Take care of the ACLs.
19840Sstevel@tonic-gate */
19850Sstevel@tonic-gate
1986789Sahrens acl_is_set = 0;
19870Sstevel@tonic-gate
19880Sstevel@tonic-gate if (Pflag && aclp != NULL) {
1989789Sahrens if (acl_set(nam_p, aclp) < 0) {
19900Sstevel@tonic-gate msg(ERRN,
19910Sstevel@tonic-gate "\"%s\": failed to set acl", nam_p);
19920Sstevel@tonic-gate } else {
1993789Sahrens acl_is_set = 1;
19940Sstevel@tonic-gate }
19950Sstevel@tonic-gate
1996789Sahrens acl_free(aclp);
19970Sstevel@tonic-gate aclp = NULL;
19980Sstevel@tonic-gate }
19990Sstevel@tonic-gate
20000Sstevel@tonic-gate cnt = 0;
20010Sstevel@tonic-gate break;
20020Sstevel@tonic-gate }
20030Sstevel@tonic-gate
20040Sstevel@tonic-gate cnt++;
20050Sstevel@tonic-gate } while (cnt < 2 && missdir(nam_p) == 0);
20060Sstevel@tonic-gate
20070Sstevel@tonic-gate switch (cnt) {
20080Sstevel@tonic-gate case 0:
20090Sstevel@tonic-gate rv = 1;
20100Sstevel@tonic-gate rstfiles(U_OVER, dirfd);
20110Sstevel@tonic-gate break;
20120Sstevel@tonic-gate
20130Sstevel@tonic-gate case 1:
20140Sstevel@tonic-gate msg(ERRN,
20150Sstevel@tonic-gate "Cannot create directory for \"%s\"", nam_p);
20160Sstevel@tonic-gate
20170Sstevel@tonic-gate if (*Over_p == '\0') {
20180Sstevel@tonic-gate rstfiles(U_KEEP, dirfd);
20190Sstevel@tonic-gate }
20200Sstevel@tonic-gate
20210Sstevel@tonic-gate break;
20220Sstevel@tonic-gate
20230Sstevel@tonic-gate case 2:
20240Sstevel@tonic-gate if (ustar_dir() || Adir) {
20250Sstevel@tonic-gate msg(ERRN, "Cannot create directory \"%s\"", nam_p);
20260Sstevel@tonic-gate } else if (ustar_spec() || Aspec) {
20270Sstevel@tonic-gate msg(ERRN, "Cannot mknod() \"%s\"", nam_p);
20280Sstevel@tonic-gate }
20290Sstevel@tonic-gate
20300Sstevel@tonic-gate if (*Over_p == '\0') {
20310Sstevel@tonic-gate rstfiles(U_KEEP, dirfd);
20320Sstevel@tonic-gate }
20330Sstevel@tonic-gate
20340Sstevel@tonic-gate break;
20350Sstevel@tonic-gate
20360Sstevel@tonic-gate default:
20370Sstevel@tonic-gate msg(EXT, "Impossible case.");
20380Sstevel@tonic-gate }
20390Sstevel@tonic-gate
20400Sstevel@tonic-gate return (rv);
20410Sstevel@tonic-gate }
20420Sstevel@tonic-gate
20430Sstevel@tonic-gate /*
20440Sstevel@tonic-gate * creat_tmp:
20450Sstevel@tonic-gate */
20460Sstevel@tonic-gate
20470Sstevel@tonic-gate static int
creat_tmp(char * nam_p)20480Sstevel@tonic-gate creat_tmp(char *nam_p)
20490Sstevel@tonic-gate {
20500Sstevel@tonic-gate char *t_p;
20510Sstevel@tonic-gate int cwd;
20520Sstevel@tonic-gate
20530Sstevel@tonic-gate if ((Args & OCp) && G_p->g_ino == DesSt.st_ino &&
20540Sstevel@tonic-gate G_p->g_dev == DesSt.st_dev) {
20550Sstevel@tonic-gate msg(ERR, "Attempt to pass a file to itself.");
20560Sstevel@tonic-gate return (-1);
20570Sstevel@tonic-gate }
20580Sstevel@tonic-gate
20590Sstevel@tonic-gate if (G_p->g_mtime <= DesSt.st_mtime && !(Args & OCu)) {
20600Sstevel@tonic-gate msg(ERR, "Existing \"%s\" same age or newer", nam_p);
20610Sstevel@tonic-gate return (-1);
20620Sstevel@tonic-gate }
20630Sstevel@tonic-gate
20640Sstevel@tonic-gate /* Make the temporary file name. */
20650Sstevel@tonic-gate
20660Sstevel@tonic-gate (void) strcpy(Over_p, nam_p);
20670Sstevel@tonic-gate t_p = Over_p + strlen(Over_p);
20680Sstevel@tonic-gate
20690Sstevel@tonic-gate while (t_p != Over_p) {
20700Sstevel@tonic-gate if (*(t_p - 1) == '/')
20710Sstevel@tonic-gate break;
20720Sstevel@tonic-gate t_p--;
20730Sstevel@tonic-gate }
20740Sstevel@tonic-gate
20750Sstevel@tonic-gate (void) strcpy(t_p, "XXXXXX");
20760Sstevel@tonic-gate
20775750Sceastha if (G_p->g_attrnam_p != NULL) {
20780Sstevel@tonic-gate /*
20790Sstevel@tonic-gate * Save our current directory, so we can go into
20800Sstevel@tonic-gate * the attribute directory to make the temp file
20810Sstevel@tonic-gate * and then return.
20820Sstevel@tonic-gate */
20830Sstevel@tonic-gate
20840Sstevel@tonic-gate cwd = save_cwd();
20850Sstevel@tonic-gate (void) fchdir(G_p->g_dirfd);
20860Sstevel@tonic-gate }
20870Sstevel@tonic-gate
20880Sstevel@tonic-gate (void) mktemp(Over_p);
20890Sstevel@tonic-gate
20905750Sceastha if (G_p->g_attrnam_p != NULL) {
20910Sstevel@tonic-gate /* Return to the current directory. */
20920Sstevel@tonic-gate
20930Sstevel@tonic-gate rest_cwd(cwd);
20940Sstevel@tonic-gate }
20950Sstevel@tonic-gate
20960Sstevel@tonic-gate if (*Over_p == '\0') {
20970Sstevel@tonic-gate /* mktemp reports a failure. */
20980Sstevel@tonic-gate
20990Sstevel@tonic-gate msg(ERR, "Cannot get temporary file name.");
21000Sstevel@tonic-gate return (-1);
21010Sstevel@tonic-gate }
21020Sstevel@tonic-gate
21030Sstevel@tonic-gate /*
21040Sstevel@tonic-gate * If it's a regular file, write to the temporary file, and then rename
21055331Samw * in order to accommodate potential executables.
21060Sstevel@tonic-gate *
21070Sstevel@tonic-gate * Note: g_typeflag is only defined (set) for USTAR archive types. It
21080Sstevel@tonic-gate * defaults to 0 in the cpio-format-regular file case, so this test
21090Sstevel@tonic-gate * succeeds.
21100Sstevel@tonic-gate */
21110Sstevel@tonic-gate
21120Sstevel@tonic-gate if (G_p->g_typeflag == 0 &&
21130Sstevel@tonic-gate (DesSt.st_mode & (ulong_t)Ftype) == S_IFREG &&
21140Sstevel@tonic-gate (G_p->g_mode & (ulong_t)Ftype) == S_IFREG) {
21150Sstevel@tonic-gate /*
21160Sstevel@tonic-gate * The archive file and the filesystem file are both regular
21170Sstevel@tonic-gate * files. We write to the temporary file in this case.
21180Sstevel@tonic-gate */
21190Sstevel@tonic-gate
21200Sstevel@tonic-gate if (Args & OCp) {
21215750Sceastha if (G_p->g_attrnam_p == NULL) {
21220Sstevel@tonic-gate Fullnam_p = Over_p;
21230Sstevel@tonic-gate } else {
21240Sstevel@tonic-gate Attrfile_p = Over_p;
21250Sstevel@tonic-gate }
21260Sstevel@tonic-gate } else {
21270Sstevel@tonic-gate G_p->g_nam_p = Over_p;
21285750Sceastha if (G_p->g_attrnam_p != NULL) {
21290Sstevel@tonic-gate Attrfile_p = Over_p;
21300Sstevel@tonic-gate }
21310Sstevel@tonic-gate }
21320Sstevel@tonic-gate
21335750Sceastha if (G_p->g_attrnam_p == NULL) {
21340Sstevel@tonic-gate Over_p = nam_p;
21350Sstevel@tonic-gate } else {
21360Sstevel@tonic-gate Over_p = G_p->g_attrnam_p;
21370Sstevel@tonic-gate }
21380Sstevel@tonic-gate
21390Sstevel@tonic-gate Do_rename = 1;
21400Sstevel@tonic-gate } else {
21410Sstevel@tonic-gate /*
21420Sstevel@tonic-gate * Either the archive file or the filesystem file is not a
21430Sstevel@tonic-gate * regular file.
21440Sstevel@tonic-gate */
21450Sstevel@tonic-gate
21460Sstevel@tonic-gate Do_rename = 0;
21470Sstevel@tonic-gate
21480Sstevel@tonic-gate if (S_ISDIR(DesSt.st_mode)) {
21490Sstevel@tonic-gate /*
21500Sstevel@tonic-gate * The filesystem file is a directory.
21510Sstevel@tonic-gate *
21520Sstevel@tonic-gate * Save the current working directory because we will
21530Sstevel@tonic-gate * want to restore it back just in case remove_dir()
21540Sstevel@tonic-gate * fails or get confused about where we should be.
21550Sstevel@tonic-gate */
21560Sstevel@tonic-gate
21570Sstevel@tonic-gate *Over_p = '\0';
21580Sstevel@tonic-gate cwd = save_cwd();
21590Sstevel@tonic-gate
21600Sstevel@tonic-gate if (remove_dir(nam_p) < 0) {
21610Sstevel@tonic-gate msg(ERRN,
21620Sstevel@tonic-gate "Cannot remove the directory \"%s\"",
21630Sstevel@tonic-gate nam_p);
21640Sstevel@tonic-gate /*
21650Sstevel@tonic-gate * Restore working directory back to the one
21660Sstevel@tonic-gate * saved earlier.
21670Sstevel@tonic-gate */
21680Sstevel@tonic-gate
21690Sstevel@tonic-gate rest_cwd(cwd);
21700Sstevel@tonic-gate return (-1);
21710Sstevel@tonic-gate }
21720Sstevel@tonic-gate
21730Sstevel@tonic-gate /*
21740Sstevel@tonic-gate * Restore working directory back to the one
21750Sstevel@tonic-gate * saved earlier
21760Sstevel@tonic-gate */
21770Sstevel@tonic-gate
21780Sstevel@tonic-gate rest_cwd(cwd);
21790Sstevel@tonic-gate } else {
21800Sstevel@tonic-gate /*
21810Sstevel@tonic-gate * The file is not a directory. Will use the original
21820Sstevel@tonic-gate * link/unlink construct, however, if the file is
21830Sstevel@tonic-gate * namefs, link would fail with EXDEV. Therefore, we
21840Sstevel@tonic-gate * use rename() first to back up the file.
21850Sstevel@tonic-gate */
21860Sstevel@tonic-gate if (rename(nam_p, Over_p) < 0) {
21870Sstevel@tonic-gate /*
21880Sstevel@tonic-gate * If rename failed, try old construction
21890Sstevel@tonic-gate * method.
21900Sstevel@tonic-gate */
21910Sstevel@tonic-gate if (link(nam_p, Over_p) < 0) {
21920Sstevel@tonic-gate msg(ERRN,
21935750Sceastha "Cannot rename temporary file "
21945750Sceastha "\"%s\" to \"%s\"", Over_p, nam_p);
21950Sstevel@tonic-gate *Over_p = '\0';
21960Sstevel@tonic-gate return (-1);
21970Sstevel@tonic-gate }
21980Sstevel@tonic-gate
21990Sstevel@tonic-gate if (unlink(nam_p) < 0) {
22000Sstevel@tonic-gate msg(ERRN,
22010Sstevel@tonic-gate "Cannot unlink() current \"%s\"",
22020Sstevel@tonic-gate nam_p);
22030Sstevel@tonic-gate (void) unlink(Over_p);
22040Sstevel@tonic-gate *Over_p = '\0';
22050Sstevel@tonic-gate return (-1);
22060Sstevel@tonic-gate }
22070Sstevel@tonic-gate }
22080Sstevel@tonic-gate }
22090Sstevel@tonic-gate }
22100Sstevel@tonic-gate
22110Sstevel@tonic-gate return (1);
22120Sstevel@tonic-gate }
22130Sstevel@tonic-gate
22140Sstevel@tonic-gate /*
22155750Sceastha * Copy the datasize amount of data from the input file to buffer.
22165750Sceastha *
22175750Sceastha * ifd - Input file descriptor.
22185750Sceastha * buffer - Buffer (allocated by caller) to copy data to.
22195750Sceastha * datasize - The amount of data to read from the input file
22205750Sceastha * and copy to the buffer.
22215750Sceastha * error - When reading from an Archive file, indicates unreadable
22225750Sceastha * data was encountered, otherwise indicates errno.
22235750Sceastha * data_in_info - Information needed when called from data_in().
22245750Sceastha */
22255750Sceastha static ssize_t
read_chunk(int ifd,char * buffer,size_t datasize,data_in_t * data_in_info)22265750Sceastha read_chunk(int ifd, char *buffer, size_t datasize, data_in_t *data_in_info)
22275750Sceastha {
22285750Sceastha if (Args & OCp) {
22295750Sceastha return (read(ifd, buffer, datasize));
22305750Sceastha } else {
22315750Sceastha FILL(datasize);
22325750Sceastha if (data_in_info->data_in_proc_mode != P_SKIP) {
22335750Sceastha if (Hdr_type == CRC)
22345750Sceastha data_in_info->data_in_cksumval += cksum(CRC,
22355750Sceastha datasize, NULL);
22365750Sceastha if (data_in_info->data_in_swapfile)
22375750Sceastha swap(Buffr.b_out_p, datasize);
22385750Sceastha
22395750Sceastha
22405750Sceastha /*
22415750Sceastha * if the bar archive is compressed, set up a pipe and
22425750Sceastha * do the de-compression while reading in the file
22435750Sceastha */
22445750Sceastha if (Hdr_type == BAR) {
22455750Sceastha if (data_in_info->data_in_compress_flag == 0 &&
22465750Sceastha Compressed) {
22475750Sceastha setup_uncompress(
22485750Sceastha &(data_in_info->data_in_pipef));
22495750Sceastha data_in_info->data_in_compress_flag++;
22505750Sceastha }
22515750Sceastha }
22525750Sceastha }
22535750Sceastha (void) memcpy(buffer, Buffr.b_out_p, datasize);
22545750Sceastha Buffr.b_out_p += datasize;
22555750Sceastha Buffr.b_cnt -= datasize;
22565750Sceastha return (datasize);
22575750Sceastha }
22585750Sceastha }
22595750Sceastha
226010202SNobutomo.Nakano@Sun.COM /*
226110202SNobutomo.Nakano@Sun.COM * Read as much data as we can.
226210202SNobutomo.Nakano@Sun.COM *
226310202SNobutomo.Nakano@Sun.COM * ifd - input file descriptor.
226410202SNobutomo.Nakano@Sun.COM * buf - Buffer (allocated by caller) to copy data to.
226510202SNobutomo.Nakano@Sun.COM * bytes - The amount of data to read from the input file
226610202SNobutomo.Nakano@Sun.COM * and copy to the buffer.
226710202SNobutomo.Nakano@Sun.COM * rdblocksz - The size of the chunk of data to read.
226810202SNobutomo.Nakano@Sun.COM *
226910389SNobutomo.Nakano@Sun.COM * Return number of bytes failed to read.
227010389SNobutomo.Nakano@Sun.COM * Return -1 when buffer is empty and read failed.
227110202SNobutomo.Nakano@Sun.COM */
227210202SNobutomo.Nakano@Sun.COM static int
read_bytes(int ifd,char * buf,size_t bytes,size_t rdblocksz,data_in_t * data_in_info)227310202SNobutomo.Nakano@Sun.COM read_bytes(int ifd, char *buf, size_t bytes, size_t rdblocksz,
227410202SNobutomo.Nakano@Sun.COM data_in_t *data_in_info)
227510202SNobutomo.Nakano@Sun.COM {
227610202SNobutomo.Nakano@Sun.COM size_t bytesread;
227710202SNobutomo.Nakano@Sun.COM ssize_t got;
227810202SNobutomo.Nakano@Sun.COM
227910202SNobutomo.Nakano@Sun.COM for (bytesread = 0; bytesread < bytes; bytesread += got) {
228010202SNobutomo.Nakano@Sun.COM /*
228110202SNobutomo.Nakano@Sun.COM * Read the data from either the input file descriptor
228210202SNobutomo.Nakano@Sun.COM * or the archive file. read_chunk() will only return
228310202SNobutomo.Nakano@Sun.COM * <= 0 if data_copy() was called from data_pass().
228410202SNobutomo.Nakano@Sun.COM */
228510202SNobutomo.Nakano@Sun.COM if ((got = read_chunk(ifd, buf + bytesread,
228610202SNobutomo.Nakano@Sun.COM min(bytes - bytesread, rdblocksz),
228710202SNobutomo.Nakano@Sun.COM data_in_info)) <= 0) {
228810202SNobutomo.Nakano@Sun.COM /*
228910389SNobutomo.Nakano@Sun.COM * We come here only in the pass mode.
229010202SNobutomo.Nakano@Sun.COM * If data couldn't be read from the input file
229110389SNobutomo.Nakano@Sun.COM * descriptor, return number of bytes in the buf.
229210389SNobutomo.Nakano@Sun.COM * If buffer is empty, return -1.
229310202SNobutomo.Nakano@Sun.COM */
229410389SNobutomo.Nakano@Sun.COM if (bytesread == 0) {
229510389SNobutomo.Nakano@Sun.COM if (got == 0) /* EOF */
229610389SNobutomo.Nakano@Sun.COM data_in_info->data_in_rd_eof = 1;
229710389SNobutomo.Nakano@Sun.COM return (-1);
229810389SNobutomo.Nakano@Sun.COM }
229910389SNobutomo.Nakano@Sun.COM return (bytes - bytesread);
230010202SNobutomo.Nakano@Sun.COM }
230110202SNobutomo.Nakano@Sun.COM }
230210202SNobutomo.Nakano@Sun.COM return (0);
230310202SNobutomo.Nakano@Sun.COM }
230410202SNobutomo.Nakano@Sun.COM
230510202SNobutomo.Nakano@Sun.COM /*
230610202SNobutomo.Nakano@Sun.COM * Write as much data as we can.
230710202SNobutomo.Nakano@Sun.COM *
230810202SNobutomo.Nakano@Sun.COM * ofd - output file descriptor.
230910202SNobutomo.Nakano@Sun.COM * buf - Source buffer to output data from.
231010202SNobutomo.Nakano@Sun.COM * maxwrite - The amount of data to write to the output.
231110202SNobutomo.Nakano@Sun.COM *
231210202SNobutomo.Nakano@Sun.COM * return 0 upon success.
231310202SNobutomo.Nakano@Sun.COM */
231410202SNobutomo.Nakano@Sun.COM static int
write_bytes(int ofd,char * buf,size_t maxwrite,data_in_t * data_in_info)231510202SNobutomo.Nakano@Sun.COM write_bytes(int ofd, char *buf, size_t maxwrite, data_in_t *data_in_info)
231610202SNobutomo.Nakano@Sun.COM {
231710202SNobutomo.Nakano@Sun.COM ssize_t cnt;
231810202SNobutomo.Nakano@Sun.COM
231910389SNobutomo.Nakano@Sun.COM errno = 0;
232010202SNobutomo.Nakano@Sun.COM if ((cnt = write(ofd, buf, maxwrite)) < (ssize_t)maxwrite) {
232110202SNobutomo.Nakano@Sun.COM data_in_info->data_in_errno = errno;
232210202SNobutomo.Nakano@Sun.COM /*
232310202SNobutomo.Nakano@Sun.COM * data_in() needs to know if it was an actual write(2)
232410202SNobutomo.Nakano@Sun.COM * failure, or if we just couldn't write all of the data
232510202SNobutomo.Nakano@Sun.COM * requested so that we know that the rest of the file's
232610202SNobutomo.Nakano@Sun.COM * data can be read but not written.
232710202SNobutomo.Nakano@Sun.COM */
232810389SNobutomo.Nakano@Sun.COM if (cnt != -1)
232910389SNobutomo.Nakano@Sun.COM data_in_info->data_in_wr_part = 1;
233010202SNobutomo.Nakano@Sun.COM return (1);
233110202SNobutomo.Nakano@Sun.COM } else if (Args & OCp) {
233210202SNobutomo.Nakano@Sun.COM Blocks += (u_longlong_t)((cnt + (Bufsize - 1)) / Bufsize);
233310202SNobutomo.Nakano@Sun.COM }
233410202SNobutomo.Nakano@Sun.COM return (0);
233510202SNobutomo.Nakano@Sun.COM }
23365750Sceastha
23375750Sceastha /*
233810202SNobutomo.Nakano@Sun.COM * Perform I/O for given byte size with using limited i/o block size
233910202SNobutomo.Nakano@Sun.COM * and supplied buffer.
234010202SNobutomo.Nakano@Sun.COM *
234110202SNobutomo.Nakano@Sun.COM * ifd/ofd - i/o file descriptor
234210202SNobutomo.Nakano@Sun.COM * buf - buffer to be used for i/o
234310202SNobutomo.Nakano@Sun.COM * bytes - Amount to read/write
234410202SNobutomo.Nakano@Sun.COM * wrblocksz - Output block size.
234510202SNobutomo.Nakano@Sun.COM * rdblocksz - Read block size.
234610202SNobutomo.Nakano@Sun.COM *
234710202SNobutomo.Nakano@Sun.COM * Return 0 upon success. Return negative if read failed.
234810202SNobutomo.Nakano@Sun.COM * Return positive non-zero if write failed.
234910202SNobutomo.Nakano@Sun.COM */
235010202SNobutomo.Nakano@Sun.COM static int
rdwr_bytes(int ifd,int ofd,char * buf,off_t bytes,size_t wrblocksz,size_t rdblocksz,data_in_t * data_in_info)235110202SNobutomo.Nakano@Sun.COM rdwr_bytes(int ifd, int ofd, char *buf, off_t bytes,
235210202SNobutomo.Nakano@Sun.COM size_t wrblocksz, size_t rdblocksz, data_in_t *data_in_info)
235310202SNobutomo.Nakano@Sun.COM {
235410389SNobutomo.Nakano@Sun.COM int rv, sz;
235510202SNobutomo.Nakano@Sun.COM int error = 0;
235610389SNobutomo.Nakano@Sun.COM int write_it = (data_in_info->data_in_proc_mode != P_SKIP);
235710202SNobutomo.Nakano@Sun.COM
235810202SNobutomo.Nakano@Sun.COM while (bytes > 0) {
235910202SNobutomo.Nakano@Sun.COM /*
236010202SNobutomo.Nakano@Sun.COM * If the number of bytes left to write is smaller than
236110202SNobutomo.Nakano@Sun.COM * the preferred I/O size, then we're about to do our final
236210202SNobutomo.Nakano@Sun.COM * write to the file, so just set wrblocksz to the number of
236310202SNobutomo.Nakano@Sun.COM * bytes left to write.
236410202SNobutomo.Nakano@Sun.COM */
236510202SNobutomo.Nakano@Sun.COM if (bytes < wrblocksz)
236610202SNobutomo.Nakano@Sun.COM wrblocksz = bytes;
236710202SNobutomo.Nakano@Sun.COM
236810202SNobutomo.Nakano@Sun.COM /* Read input till satisfy output block size */
236910389SNobutomo.Nakano@Sun.COM sz = read_bytes(ifd, buf, wrblocksz, rdblocksz, data_in_info);
237010389SNobutomo.Nakano@Sun.COM if (sz < 0)
237110389SNobutomo.Nakano@Sun.COM return (sz);
237210202SNobutomo.Nakano@Sun.COM
237310202SNobutomo.Nakano@Sun.COM if (write_it) {
237410389SNobutomo.Nakano@Sun.COM rv = write_bytes(ofd, buf,
237510389SNobutomo.Nakano@Sun.COM wrblocksz - sz, data_in_info);
237610202SNobutomo.Nakano@Sun.COM if (rv != 0) {
237710202SNobutomo.Nakano@Sun.COM /*
237810202SNobutomo.Nakano@Sun.COM * If we wrote partial, we return and quits.
237910202SNobutomo.Nakano@Sun.COM * Otherwise, read through the rest of input
238010202SNobutomo.Nakano@Sun.COM * to go to the next file.
238110202SNobutomo.Nakano@Sun.COM */
238210202SNobutomo.Nakano@Sun.COM if ((Args & OCp) ||
238310389SNobutomo.Nakano@Sun.COM data_in_info->data_in_wr_part) {
238410202SNobutomo.Nakano@Sun.COM return (rv);
238510202SNobutomo.Nakano@Sun.COM } else {
238610202SNobutomo.Nakano@Sun.COM write_it = 0;
238710202SNobutomo.Nakano@Sun.COM }
238810202SNobutomo.Nakano@Sun.COM error = 1;
238910202SNobutomo.Nakano@Sun.COM }
239010202SNobutomo.Nakano@Sun.COM }
239110389SNobutomo.Nakano@Sun.COM bytes -= (wrblocksz - sz);
239210202SNobutomo.Nakano@Sun.COM }
239310202SNobutomo.Nakano@Sun.COM return (error);
239410202SNobutomo.Nakano@Sun.COM }
239510202SNobutomo.Nakano@Sun.COM
239610202SNobutomo.Nakano@Sun.COM /*
239710202SNobutomo.Nakano@Sun.COM * Write zeros for give size.
239810202SNobutomo.Nakano@Sun.COM *
239910202SNobutomo.Nakano@Sun.COM * ofd - output file descriptor
240010202SNobutomo.Nakano@Sun.COM * buf - buffer to fill with zeros
240110202SNobutomo.Nakano@Sun.COM * bytes - Amount to write
240210202SNobutomo.Nakano@Sun.COM * wrblocksz - Write block size
240310202SNobutomo.Nakano@Sun.COM *
240410202SNobutomo.Nakano@Sun.COM * return 0 upon success.
240510202SNobutomo.Nakano@Sun.COM */
240610202SNobutomo.Nakano@Sun.COM static int
write_zeros(int ofd,char * buf,off_t bytes,size_t wrblocksz,data_in_t * data_in_info)240710202SNobutomo.Nakano@Sun.COM write_zeros(int ofd, char *buf, off_t bytes, size_t wrblocksz,
240810202SNobutomo.Nakano@Sun.COM data_in_t *data_in_info)
240910202SNobutomo.Nakano@Sun.COM {
241010202SNobutomo.Nakano@Sun.COM int rv;
241110202SNobutomo.Nakano@Sun.COM
241210202SNobutomo.Nakano@Sun.COM (void) memset(buf, 0, min(bytes, wrblocksz));
241310202SNobutomo.Nakano@Sun.COM while (bytes > 0) {
241410202SNobutomo.Nakano@Sun.COM if (bytes < wrblocksz)
241510202SNobutomo.Nakano@Sun.COM wrblocksz = bytes;
241610202SNobutomo.Nakano@Sun.COM rv = write_bytes(ofd, buf, wrblocksz, data_in_info);
241710202SNobutomo.Nakano@Sun.COM if (rv != 0)
241810202SNobutomo.Nakano@Sun.COM return (rv);
241910202SNobutomo.Nakano@Sun.COM bytes -= wrblocksz;
242010202SNobutomo.Nakano@Sun.COM }
242110202SNobutomo.Nakano@Sun.COM return (0);
242210202SNobutomo.Nakano@Sun.COM }
242310202SNobutomo.Nakano@Sun.COM
242410202SNobutomo.Nakano@Sun.COM /*
242510202SNobutomo.Nakano@Sun.COM * To figure out the size of the buffer used to accumulate data from
242610202SNobutomo.Nakano@Sun.COM * readtape() and to write to the file, we need to determine the largest
242710202SNobutomo.Nakano@Sun.COM * chunk of data to be written to the file at one time. This is determined
242810202SNobutomo.Nakano@Sun.COM * based on the following three things:
242910202SNobutomo.Nakano@Sun.COM * 1) The size of the archived file.
243010202SNobutomo.Nakano@Sun.COM * 2) The preferred I/O size of the file.
243110202SNobutomo.Nakano@Sun.COM * 3) If the file is a read-write system attribute file.
243210202SNobutomo.Nakano@Sun.COM * If the size of the file is less than the preferred I/O size or it's a
243310202SNobutomo.Nakano@Sun.COM * read-write system attribute file, which must be written in one operation,
243410202SNobutomo.Nakano@Sun.COM * then set the maximum write size to the size of the archived file.
243510202SNobutomo.Nakano@Sun.COM * Otherwise, the maximum write size is preferred I/O size.
243610202SNobutomo.Nakano@Sun.COM */
243710202SNobutomo.Nakano@Sun.COM static int
calc_maxwrite(int ofd,int rw_sysattr,off_t bytes,size_t blocksize)243810202SNobutomo.Nakano@Sun.COM calc_maxwrite(int ofd, int rw_sysattr, off_t bytes, size_t blocksize)
243910202SNobutomo.Nakano@Sun.COM {
244010202SNobutomo.Nakano@Sun.COM struct stat tsbuf;
244110202SNobutomo.Nakano@Sun.COM size_t maxwrite;
244210202SNobutomo.Nakano@Sun.COM size_t piosize; /* preferred I/O size */
244310202SNobutomo.Nakano@Sun.COM
244410202SNobutomo.Nakano@Sun.COM if (rw_sysattr || bytes < blocksize) {
244510202SNobutomo.Nakano@Sun.COM maxwrite = bytes;
244610202SNobutomo.Nakano@Sun.COM } else {
244710202SNobutomo.Nakano@Sun.COM if (fstat(ofd, &tsbuf) == 0) {
244810202SNobutomo.Nakano@Sun.COM piosize = tsbuf.st_blksize;
244910202SNobutomo.Nakano@Sun.COM } else {
245010202SNobutomo.Nakano@Sun.COM piosize = blocksize;
245110202SNobutomo.Nakano@Sun.COM }
245210202SNobutomo.Nakano@Sun.COM maxwrite = min(bytes, piosize);
245310202SNobutomo.Nakano@Sun.COM }
245410202SNobutomo.Nakano@Sun.COM return (maxwrite);
245510202SNobutomo.Nakano@Sun.COM }
245610202SNobutomo.Nakano@Sun.COM /*
245710202SNobutomo.Nakano@Sun.COM * data_copy() and data_copy_with_holes() copy data from the input
245810202SNobutomo.Nakano@Sun.COM * file to output file descriptor. If ifd is -1, then the input file is
245910202SNobutomo.Nakano@Sun.COM * the archive file.
24605750Sceastha *
24615750Sceastha * Parameters
24625750Sceastha * ifd - Input file descriptor to read from.
24635750Sceastha * ofd - Output file descriptor of extracted file.
24645750Sceastha * rw_sysattr - Flag indicating if a file is an extended
24655750Sceastha * system attribute file.
246610202SNobutomo.Nakano@Sun.COM * bytes - Amount of data (file size) of copy/write.
24675750Sceastha * blocksize - Amount of data to read at a time from either
24685750Sceastha * the input file descriptor or from the archive.
24695750Sceastha * data_in_info - information needed while reading data when
24705750Sceastha * called by data_in().
247110202SNobutomo.Nakano@Sun.COM * holes - Information of holes in the input file.
24725750Sceastha *
24735750Sceastha * Return code
24745750Sceastha * 0 Success
24755750Sceastha * < 0 An error occurred during the read of the input
24765750Sceastha * file
24775750Sceastha * > 0 An error occurred during the write of the output
24785750Sceastha * file descriptor.
24795750Sceastha */
24805750Sceastha static int
data_copy(int ifd,int ofd,int rw_sysattr,off_t bytes,size_t blocksize,data_in_t * data_in_info)24818795SNobutomo.Nakano@Sun.COM data_copy(int ifd, int ofd, int rw_sysattr, off_t bytes,
24825750Sceastha size_t blocksize, data_in_t *data_in_info)
24835750Sceastha {
24845750Sceastha char *buf;
24855750Sceastha size_t maxwrite;
248610202SNobutomo.Nakano@Sun.COM int rv;
24875750Sceastha
24885750Sceastha /* No data to copy. */
248910202SNobutomo.Nakano@Sun.COM if (bytes == 0)
249010202SNobutomo.Nakano@Sun.COM return (0);
249110202SNobutomo.Nakano@Sun.COM
249210202SNobutomo.Nakano@Sun.COM maxwrite = calc_maxwrite(ofd, rw_sysattr, bytes, blocksize);
249310202SNobutomo.Nakano@Sun.COM buf = e_zalloc(E_EXIT, maxwrite);
249410202SNobutomo.Nakano@Sun.COM
249510202SNobutomo.Nakano@Sun.COM rv = rdwr_bytes(ifd, ofd, buf, bytes, maxwrite,
249610202SNobutomo.Nakano@Sun.COM blocksize, data_in_info);
249710202SNobutomo.Nakano@Sun.COM
249810202SNobutomo.Nakano@Sun.COM free(buf);
249910202SNobutomo.Nakano@Sun.COM return (rv);
250010202SNobutomo.Nakano@Sun.COM }
250110202SNobutomo.Nakano@Sun.COM
250210202SNobutomo.Nakano@Sun.COM static int
data_copy_with_holes(int ifd,int ofd,int rw_sysattr,off_t bytes,size_t blocksize,data_in_t * data_in_info,holes_info_t * holes)250310202SNobutomo.Nakano@Sun.COM data_copy_with_holes(int ifd, int ofd, int rw_sysattr, off_t bytes,
250410202SNobutomo.Nakano@Sun.COM size_t blocksize, data_in_t *data_in_info, holes_info_t *holes)
250510202SNobutomo.Nakano@Sun.COM {
250610202SNobutomo.Nakano@Sun.COM holes_list_t *hl;
250710202SNobutomo.Nakano@Sun.COM off_t curpos, noff, datasize;
250810202SNobutomo.Nakano@Sun.COM char *buf;
250910202SNobutomo.Nakano@Sun.COM size_t maxwrite;
251010202SNobutomo.Nakano@Sun.COM int rv, error;
251110202SNobutomo.Nakano@Sun.COM
251210202SNobutomo.Nakano@Sun.COM if (bytes == 0)
25135750Sceastha return (0);
251410202SNobutomo.Nakano@Sun.COM
251510202SNobutomo.Nakano@Sun.COM maxwrite = calc_maxwrite(ofd, rw_sysattr, bytes, blocksize);
251610202SNobutomo.Nakano@Sun.COM buf = e_zalloc(E_EXIT, maxwrite);
251710202SNobutomo.Nakano@Sun.COM
251810202SNobutomo.Nakano@Sun.COM error = 0;
251910202SNobutomo.Nakano@Sun.COM curpos = 0;
252010202SNobutomo.Nakano@Sun.COM for (hl = holes->holes_list; hl != NULL; hl = hl->hl_next) {
252110202SNobutomo.Nakano@Sun.COM if (curpos != hl->hl_data) {
252210202SNobutomo.Nakano@Sun.COM /* adjust output position */
252310202SNobutomo.Nakano@Sun.COM noff = lseek(ofd, hl->hl_data, SEEK_SET);
252410202SNobutomo.Nakano@Sun.COM if (noff != hl->hl_data) {
252510202SNobutomo.Nakano@Sun.COM /*
252610202SNobutomo.Nakano@Sun.COM * Can't seek to the target, try to adjust
252710202SNobutomo.Nakano@Sun.COM * position by filling with zeros.
252810202SNobutomo.Nakano@Sun.COM */
252910202SNobutomo.Nakano@Sun.COM datasize = hl->hl_data - curpos;
253010202SNobutomo.Nakano@Sun.COM rv = write_zeros(ofd, buf, datasize,
253110202SNobutomo.Nakano@Sun.COM maxwrite, data_in_info);
253210202SNobutomo.Nakano@Sun.COM if (rv != 0)
253310202SNobutomo.Nakano@Sun.COM goto errout;
253410202SNobutomo.Nakano@Sun.COM }
253510202SNobutomo.Nakano@Sun.COM /*
253610202SNobutomo.Nakano@Sun.COM * Data is contiguous in the archive, but fragmented
253710202SNobutomo.Nakano@Sun.COM * in the regular file, so we also adjust the input
253810202SNobutomo.Nakano@Sun.COM * file position in pass mode.
253910202SNobutomo.Nakano@Sun.COM */
254010202SNobutomo.Nakano@Sun.COM if (Args & OCp) {
254110202SNobutomo.Nakano@Sun.COM /* adjust input position */
254210202SNobutomo.Nakano@Sun.COM (void) lseek(ifd, hl->hl_data, SEEK_SET);
254310202SNobutomo.Nakano@Sun.COM }
254410202SNobutomo.Nakano@Sun.COM curpos = hl->hl_data;
254510202SNobutomo.Nakano@Sun.COM }
254610202SNobutomo.Nakano@Sun.COM datasize = hl->hl_hole - hl->hl_data;
254710202SNobutomo.Nakano@Sun.COM if (datasize == 0) {
254810202SNobutomo.Nakano@Sun.COM /*
254910202SNobutomo.Nakano@Sun.COM * There is a hole at the end of file. To create
255010202SNobutomo.Nakano@Sun.COM * such hole, we append one byte, and truncate the
255110202SNobutomo.Nakano@Sun.COM * last block. This is necessary because ftruncate(2)
255210202SNobutomo.Nakano@Sun.COM * alone allocates one block on the end of file.
255310202SNobutomo.Nakano@Sun.COM */
255410202SNobutomo.Nakano@Sun.COM rv = write_zeros(ofd, buf, 1, maxwrite, data_in_info);
255510202SNobutomo.Nakano@Sun.COM if (rv != 0)
255610202SNobutomo.Nakano@Sun.COM goto errout;
255710202SNobutomo.Nakano@Sun.COM (void) ftruncate(ofd, hl->hl_data);
255810202SNobutomo.Nakano@Sun.COM break;
255910202SNobutomo.Nakano@Sun.COM }
256010202SNobutomo.Nakano@Sun.COM rv = rdwr_bytes(ifd, ofd, buf, datasize, maxwrite,
256110202SNobutomo.Nakano@Sun.COM blocksize, data_in_info);
256210202SNobutomo.Nakano@Sun.COM if (rv != 0) {
256310202SNobutomo.Nakano@Sun.COM errout:
256410202SNobutomo.Nakano@Sun.COM /*
256510202SNobutomo.Nakano@Sun.COM * Return if we got a read error or in pass mode,
256610202SNobutomo.Nakano@Sun.COM * or failed with partial write. Otherwise, we'll
256710389SNobutomo.Nakano@Sun.COM * read through the input stream till next file.
256810202SNobutomo.Nakano@Sun.COM */
256910202SNobutomo.Nakano@Sun.COM if (rv < 0 || (Args & OCp) ||
257010389SNobutomo.Nakano@Sun.COM data_in_info->data_in_wr_part) {
257110202SNobutomo.Nakano@Sun.COM free(buf);
257210202SNobutomo.Nakano@Sun.COM return (rv);
257310202SNobutomo.Nakano@Sun.COM }
257410202SNobutomo.Nakano@Sun.COM error = 1;
257510202SNobutomo.Nakano@Sun.COM hl = hl->hl_next;
257610202SNobutomo.Nakano@Sun.COM break;
257710202SNobutomo.Nakano@Sun.COM }
257810202SNobutomo.Nakano@Sun.COM curpos += datasize;
25795750Sceastha }
25805750Sceastha
25815750Sceastha /*
258210202SNobutomo.Nakano@Sun.COM * We should read through the input data to go to the next
258310202SNobutomo.Nakano@Sun.COM * header when non-fatal error occured.
25845750Sceastha */
258510389SNobutomo.Nakano@Sun.COM if (error && !(Args & OCp)) {
258610202SNobutomo.Nakano@Sun.COM data_in_info->data_in_proc_mode = P_SKIP;
258710202SNobutomo.Nakano@Sun.COM while (hl != NULL) {
258810202SNobutomo.Nakano@Sun.COM datasize = hl->hl_hole - hl->hl_data;
258910202SNobutomo.Nakano@Sun.COM rv = rdwr_bytes(ifd, ofd, buf, datasize, maxwrite,
259010202SNobutomo.Nakano@Sun.COM blocksize, data_in_info);
259110202SNobutomo.Nakano@Sun.COM if (rv != 0)
259210202SNobutomo.Nakano@Sun.COM break;
259310202SNobutomo.Nakano@Sun.COM hl = hl->hl_next;
259410202SNobutomo.Nakano@Sun.COM }
259510202SNobutomo.Nakano@Sun.COM }
259610202SNobutomo.Nakano@Sun.COM
25975750Sceastha free(buf);
259810202SNobutomo.Nakano@Sun.COM return (error);
259910202SNobutomo.Nakano@Sun.COM }
260010202SNobutomo.Nakano@Sun.COM
260110202SNobutomo.Nakano@Sun.COM /*
260210202SNobutomo.Nakano@Sun.COM * Strip off the sparse file information that is prepended to
260310202SNobutomo.Nakano@Sun.COM * the compressed sparse file. The information is in the following
260410202SNobutomo.Nakano@Sun.COM * format:
260510202SNobutomo.Nakano@Sun.COM * <prepended info size><SP><orig file size><SP><holes info>
260610202SNobutomo.Nakano@Sun.COM * where prepended info size is long right justified in 10 bytes.
260710202SNobutomo.Nakano@Sun.COM * Holesdata consists of the series of offset pairs:
260810202SNobutomo.Nakano@Sun.COM * <data offset><SP><hole offset><SP><data offset><SP><hole offset>...
260910202SNobutomo.Nakano@Sun.COM * prepended info size and original file size have been read in gethdr().
261010202SNobutomo.Nakano@Sun.COM * We read the rest of holes information here in this function.
261110202SNobutomo.Nakano@Sun.COM */
261210202SNobutomo.Nakano@Sun.COM static int
read_holesdata(holes_info_t * holes,off_t * fileszp,char * nam_p,data_in_t * data_in_info)261310202SNobutomo.Nakano@Sun.COM read_holesdata(holes_info_t *holes, off_t *fileszp,
261410202SNobutomo.Nakano@Sun.COM char *nam_p, data_in_t *data_in_info)
261510202SNobutomo.Nakano@Sun.COM {
261610202SNobutomo.Nakano@Sun.COM char *holesdata;
261710202SNobutomo.Nakano@Sun.COM size_t holesdata_sz;
261810202SNobutomo.Nakano@Sun.COM
261910202SNobutomo.Nakano@Sun.COM /* We've already read the header. */
262010202SNobutomo.Nakano@Sun.COM holesdata_sz = holes->holesdata_sz - MIN_HOLES_HDRSIZE;
262110202SNobutomo.Nakano@Sun.COM
262210202SNobutomo.Nakano@Sun.COM if ((holesdata = e_zalloc(E_NORMAL, holesdata_sz)) == NULL) {
262310202SNobutomo.Nakano@Sun.COM msg(ERRN, "Could not allocate memory for "
262410202SNobutomo.Nakano@Sun.COM "sparse file information", nam_p);
262510202SNobutomo.Nakano@Sun.COM return (1);
262610202SNobutomo.Nakano@Sun.COM }
262710202SNobutomo.Nakano@Sun.COM /*
262810202SNobutomo.Nakano@Sun.COM * This function is called only in OCi mode. Therefore,
262910202SNobutomo.Nakano@Sun.COM * read_bytes() won't fail, and won't return if error occurs in
263010202SNobutomo.Nakano@Sun.COM * input stream. See rstbuf().
263110202SNobutomo.Nakano@Sun.COM */
263210202SNobutomo.Nakano@Sun.COM (void) read_bytes(-1, holesdata, holesdata_sz, CPIOBSZ, data_in_info);
263310202SNobutomo.Nakano@Sun.COM *fileszp -= holesdata_sz;
263410202SNobutomo.Nakano@Sun.COM
263510202SNobutomo.Nakano@Sun.COM /* The string should be terminated. */
263610202SNobutomo.Nakano@Sun.COM if (holesdata[holesdata_sz - 1] != '\0') {
263710202SNobutomo.Nakano@Sun.COM invalid:
263810202SNobutomo.Nakano@Sun.COM free(holesdata);
263910202SNobutomo.Nakano@Sun.COM msg(ERR, "invalid sparse file information", nam_p);
264010202SNobutomo.Nakano@Sun.COM return (1);
264110202SNobutomo.Nakano@Sun.COM }
264210202SNobutomo.Nakano@Sun.COM if (parse_holesdata(holes, holesdata) != 0)
264310202SNobutomo.Nakano@Sun.COM goto invalid;
264410202SNobutomo.Nakano@Sun.COM
264510202SNobutomo.Nakano@Sun.COM /* sanity check */
264610202SNobutomo.Nakano@Sun.COM if (*fileszp != holes->data_size)
264710202SNobutomo.Nakano@Sun.COM goto invalid;
264810202SNobutomo.Nakano@Sun.COM
264910202SNobutomo.Nakano@Sun.COM free(holesdata);
26505750Sceastha return (0);
26515750Sceastha }
265210202SNobutomo.Nakano@Sun.COM
26535750Sceastha /*
26540Sstevel@tonic-gate * data_in: If proc_mode == P_PROC, bread() the file's data from the archive
26550Sstevel@tonic-gate * and write(2) it to the open fdes gotten from openout(). If proc_mode ==
26560Sstevel@tonic-gate * P_SKIP, or becomes P_SKIP (due to errors etc), bread(2) the file's data
26570Sstevel@tonic-gate * and ignore it. If the user specified any of the "swap" options (b, s or S),
26580Sstevel@tonic-gate * and the length of the file is not appropriate for that action, do not
26590Sstevel@tonic-gate * perform the "swap", otherwise perform the action on a buffer by buffer basis.
26600Sstevel@tonic-gate * If the CRC header was selected, calculate a running checksum as each buffer
26610Sstevel@tonic-gate * is processed.
26620Sstevel@tonic-gate */
26630Sstevel@tonic-gate static void
data_in(int proc_mode)26640Sstevel@tonic-gate data_in(int proc_mode)
26650Sstevel@tonic-gate {
26660Sstevel@tonic-gate char *nam_p;
266710202SNobutomo.Nakano@Sun.COM int pad, rv;
266810202SNobutomo.Nakano@Sun.COM int error = 0;
26695750Sceastha int swapfile = 0;
26700Sstevel@tonic-gate int cstatus = 0;
267110202SNobutomo.Nakano@Sun.COM off_t filesz;
26725750Sceastha data_in_t *data_in_info;
26735750Sceastha
26745750Sceastha if (G_p->g_attrnam_p != NULL) {
26750Sstevel@tonic-gate nam_p = G_p->g_attrnam_p;
26760Sstevel@tonic-gate } else {
26770Sstevel@tonic-gate nam_p = G_p->g_nam_p;
26780Sstevel@tonic-gate }
26790Sstevel@tonic-gate
26800Sstevel@tonic-gate if (((G_p->g_mode & Ftype) == S_IFLNK && proc_mode != P_SKIP) ||
26810Sstevel@tonic-gate (Hdr_type == BAR && bar_linkflag == '2' && proc_mode != P_SKIP)) {
26820Sstevel@tonic-gate proc_mode = P_SKIP;
26830Sstevel@tonic-gate VERBOSE((Args & (OCv | OCV)), nam_p);
26840Sstevel@tonic-gate }
26850Sstevel@tonic-gate if (Args & (OCb | OCs | OCS)) { /* verfify that swapping is possible */
26860Sstevel@tonic-gate swapfile = 1;
26870Sstevel@tonic-gate if (Args & (OCs | OCb) && G_p->g_filesz % 2) {
26880Sstevel@tonic-gate msg(ERR,
26890Sstevel@tonic-gate "Cannot swap bytes of \"%s\", odd number of bytes",
26900Sstevel@tonic-gate nam_p);
26910Sstevel@tonic-gate swapfile = 0;
26920Sstevel@tonic-gate }
26930Sstevel@tonic-gate if (Args & (OCS | OCb) && G_p->g_filesz % 4) {
26940Sstevel@tonic-gate msg(ERR,
26950Sstevel@tonic-gate "Cannot swap halfwords of \"%s\", odd number "
26960Sstevel@tonic-gate "of halfwords", nam_p);
26970Sstevel@tonic-gate swapfile = 0;
26980Sstevel@tonic-gate }
26990Sstevel@tonic-gate }
27005750Sceastha
27015750Sceastha data_in_info = e_zalloc(E_EXIT, sizeof (data_in_t));
27025750Sceastha data_in_info->data_in_swapfile = swapfile;
27035750Sceastha data_in_info->data_in_proc_mode = proc_mode;
27040Sstevel@tonic-gate
270510202SNobutomo.Nakano@Sun.COM filesz = G_p->g_filesz;
270610202SNobutomo.Nakano@Sun.COM
270710202SNobutomo.Nakano@Sun.COM if (S_ISSPARSE(G_p->g_mode) && G_p->g_holes != NULL) {
270810202SNobutomo.Nakano@Sun.COM /* We've already read the header in gethdr() */
270910202SNobutomo.Nakano@Sun.COM filesz -= MIN_HOLES_HDRSIZE;
271010202SNobutomo.Nakano@Sun.COM
271110202SNobutomo.Nakano@Sun.COM /*
271210202SNobutomo.Nakano@Sun.COM * Strip rest of the sparse file information. This includes
271310202SNobutomo.Nakano@Sun.COM * the data/hole offset pairs which will be used to restore
271410202SNobutomo.Nakano@Sun.COM * the holes in the file.
271510202SNobutomo.Nakano@Sun.COM */
271610202SNobutomo.Nakano@Sun.COM if (proc_mode == P_SKIP) {
271710202SNobutomo.Nakano@Sun.COM /* holes info isn't necessary to skip file */
271810202SNobutomo.Nakano@Sun.COM free_holes_info(G_p->g_holes);
271910202SNobutomo.Nakano@Sun.COM G_p->g_holes = NULL;
272010202SNobutomo.Nakano@Sun.COM } else {
272110202SNobutomo.Nakano@Sun.COM rv = read_holesdata(G_p->g_holes, &filesz,
272210202SNobutomo.Nakano@Sun.COM nam_p, data_in_info);
272310202SNobutomo.Nakano@Sun.COM if (rv != 0) {
272410202SNobutomo.Nakano@Sun.COM /*
272510202SNobutomo.Nakano@Sun.COM * We got an error. Skip this file. holes info
272610202SNobutomo.Nakano@Sun.COM * is no longer necessary.
272710202SNobutomo.Nakano@Sun.COM */
272810202SNobutomo.Nakano@Sun.COM free_holes_info(G_p->g_holes);
272910202SNobutomo.Nakano@Sun.COM G_p->g_holes = NULL;
273010202SNobutomo.Nakano@Sun.COM
273110202SNobutomo.Nakano@Sun.COM data_in_info->data_in_proc_mode = P_SKIP;
273210202SNobutomo.Nakano@Sun.COM error = 1;
273310202SNobutomo.Nakano@Sun.COM }
273410202SNobutomo.Nakano@Sun.COM }
273510202SNobutomo.Nakano@Sun.COM }
273610202SNobutomo.Nakano@Sun.COM
273710202SNobutomo.Nakano@Sun.COM if (G_p->g_holes != NULL) {
273810202SNobutomo.Nakano@Sun.COM rv = data_copy_with_holes(-1, Ofile,
273910202SNobutomo.Nakano@Sun.COM (G_p->g_attrnam_p == NULL) ? 0 : G_p->g_rw_sysattr,
274010202SNobutomo.Nakano@Sun.COM G_p->g_holes->orig_size,
274110202SNobutomo.Nakano@Sun.COM CPIOBSZ, data_in_info, G_p->g_holes);
274210202SNobutomo.Nakano@Sun.COM
274310202SNobutomo.Nakano@Sun.COM free_holes_info(G_p->g_holes);
274410202SNobutomo.Nakano@Sun.COM G_p->g_holes = NULL;
274510202SNobutomo.Nakano@Sun.COM } else {
274610202SNobutomo.Nakano@Sun.COM rv = data_copy(-1, Ofile,
274710202SNobutomo.Nakano@Sun.COM (G_p->g_attrnam_p == NULL) ? 0 : G_p->g_rw_sysattr,
274810202SNobutomo.Nakano@Sun.COM filesz, CPIOBSZ, data_in_info);
274910202SNobutomo.Nakano@Sun.COM }
275010202SNobutomo.Nakano@Sun.COM
27510Sstevel@tonic-gate /* This writes out the file from the archive */
275210202SNobutomo.Nakano@Sun.COM if (rv != 0 || error) {
275310202SNobutomo.Nakano@Sun.COM errno = data_in_info->data_in_errno;
275410202SNobutomo.Nakano@Sun.COM
275510389SNobutomo.Nakano@Sun.COM if (!error) {
275610389SNobutomo.Nakano@Sun.COM msg(data_in_info->data_in_wr_part ? EXTN : ERRN,
275710389SNobutomo.Nakano@Sun.COM "Cannot write \"%s%s%s\"",
27585750Sceastha (G_p->g_attrnam_p == NULL) ? "" :
27595750Sceastha G_p->g_attrfnam_p,
27605750Sceastha (G_p->g_attrnam_p == NULL) ? "" :
27615750Sceastha G_p->g_rw_sysattr ?
27625750Sceastha gettext(" System Attribute ") :
27635750Sceastha gettext(" Attribute "), nam_p);
276410389SNobutomo.Nakano@Sun.COM }
27655750Sceastha /*
276610202SNobutomo.Nakano@Sun.COM * We've failed to write to the file, and input data
276710202SNobutomo.Nakano@Sun.COM * has been skiped to the next file. We'll need to restore
276810202SNobutomo.Nakano@Sun.COM * the original file, and skip the rest of work.
27695750Sceastha */
27705750Sceastha proc_mode = P_SKIP;
27715750Sceastha rstfiles(U_KEEP, G_p->g_dirfd);
27725750Sceastha cstatus = close(Ofile);
27735750Sceastha Ofile = 0;
27745750Sceastha if (cstatus != 0) {
27755750Sceastha msg(EXTN, "close error");
27765750Sceastha }
27775750Sceastha }
27780Sstevel@tonic-gate
277910202SNobutomo.Nakano@Sun.COM /* we must use g_filesz for the amount of padding */
27800Sstevel@tonic-gate pad = (Pad_val + 1 - (G_p->g_filesz & Pad_val)) & Pad_val;
27810Sstevel@tonic-gate if (pad != 0) {
27820Sstevel@tonic-gate FILL(pad);
27830Sstevel@tonic-gate Buffr.b_out_p += pad;
27840Sstevel@tonic-gate Buffr.b_cnt -= pad;
27850Sstevel@tonic-gate }
27860Sstevel@tonic-gate if (proc_mode != P_SKIP) {
27875750Sceastha if (Hdr_type == CRC &&
27885750Sceastha Gen.g_cksum != data_in_info->data_in_cksumval) {
27890Sstevel@tonic-gate msg(ERR, "\"%s\" - checksum error", nam_p);
27900Sstevel@tonic-gate rstfiles(U_KEEP, G_p->g_dirfd);
27910Sstevel@tonic-gate } else
27920Sstevel@tonic-gate rstfiles(U_OVER, G_p->g_dirfd);
27935750Sceastha if (Hdr_type == BAR && data_in_info->data_in_compress_flag) {
27945750Sceastha (void) pclose(data_in_info->data_in_pipef);
27950Sstevel@tonic-gate } else {
27960Sstevel@tonic-gate cstatus = close(Ofile);
27970Sstevel@tonic-gate }
27980Sstevel@tonic-gate Ofile = 0;
27990Sstevel@tonic-gate if (cstatus != 0) {
28000Sstevel@tonic-gate msg(EXTN, "close error");
28010Sstevel@tonic-gate }
28020Sstevel@tonic-gate }
28035750Sceastha (void) free(data_in_info);
28045750Sceastha
28055750Sceastha VERBOSE((proc_mode != P_SKIP && (Args & (OCv | OCV))),
28065750Sceastha (G_p->g_attrparent_p == NULL) ? G_p->g_nam_p : G_p->g_attrpath_p);
28070Sstevel@tonic-gate Finished = 1;
28080Sstevel@tonic-gate }
28090Sstevel@tonic-gate
28100Sstevel@tonic-gate /*
281110202SNobutomo.Nakano@Sun.COM * Read regular file. Return number of bytes which weren't read.
281210202SNobutomo.Nakano@Sun.COM * Upon return, real_filesz will be real file size of input file.
281310202SNobutomo.Nakano@Sun.COM * When read_exact is specified, read size is adjusted to the given
281410202SNobutomo.Nakano@Sun.COM * file size.
281510202SNobutomo.Nakano@Sun.COM */
281610202SNobutomo.Nakano@Sun.COM static off_t
read_file(char * nam_p,off_t file_size,off_t * real_filesz,boolean_t read_exact)281710202SNobutomo.Nakano@Sun.COM read_file(char *nam_p, off_t file_size, off_t *real_filesz,
281810202SNobutomo.Nakano@Sun.COM boolean_t read_exact)
281910202SNobutomo.Nakano@Sun.COM {
282010202SNobutomo.Nakano@Sun.COM int amount_read;
282110202SNobutomo.Nakano@Sun.COM off_t amt_to_read;
282210202SNobutomo.Nakano@Sun.COM off_t readsz;
282310202SNobutomo.Nakano@Sun.COM
282410202SNobutomo.Nakano@Sun.COM if (file_size == 0)
282510202SNobutomo.Nakano@Sun.COM return (0);
282610202SNobutomo.Nakano@Sun.COM
282710202SNobutomo.Nakano@Sun.COM amt_to_read = file_size;
282810202SNobutomo.Nakano@Sun.COM do {
282910202SNobutomo.Nakano@Sun.COM if (read_exact && amt_to_read < CPIOBSZ)
283010202SNobutomo.Nakano@Sun.COM readsz = amt_to_read;
283110202SNobutomo.Nakano@Sun.COM else
283210202SNobutomo.Nakano@Sun.COM readsz = CPIOBSZ;
283310202SNobutomo.Nakano@Sun.COM
283410202SNobutomo.Nakano@Sun.COM FLUSH(readsz);
283510202SNobutomo.Nakano@Sun.COM errno = 0;
283610202SNobutomo.Nakano@Sun.COM
283710202SNobutomo.Nakano@Sun.COM if ((amount_read = read(Ifile, Buffr.b_in_p, readsz)) < 0) {
283810202SNobutomo.Nakano@Sun.COM msg(EXTN, "Cannot read \"%s%s%s\"",
283910202SNobutomo.Nakano@Sun.COM (Gen.g_attrnam_p == NULL) ?
284010202SNobutomo.Nakano@Sun.COM nam_p : Gen.g_attrfnam_p,
284110202SNobutomo.Nakano@Sun.COM (Gen.g_attrnam_p == NULL) ? "" : Gen.g_rw_sysattr ?
284210202SNobutomo.Nakano@Sun.COM gettext(" System Attribute ") :
284310202SNobutomo.Nakano@Sun.COM gettext(" Attribute "),
284410202SNobutomo.Nakano@Sun.COM (Gen.g_attrnam_p == NULL) ? "" : nam_p);
284510202SNobutomo.Nakano@Sun.COM break;
284610202SNobutomo.Nakano@Sun.COM }
284710202SNobutomo.Nakano@Sun.COM
284810202SNobutomo.Nakano@Sun.COM if (amount_read == 0) {
284910202SNobutomo.Nakano@Sun.COM /* got EOF. the file has shrunk */
285010202SNobutomo.Nakano@Sun.COM *real_filesz = file_size - amt_to_read;
285110202SNobutomo.Nakano@Sun.COM break;
285210202SNobutomo.Nakano@Sun.COM } else if (amount_read > amt_to_read) {
285310202SNobutomo.Nakano@Sun.COM /* the file has grown */
285410202SNobutomo.Nakano@Sun.COM *real_filesz = file_size +
285510202SNobutomo.Nakano@Sun.COM (amount_read - amt_to_read);
285610202SNobutomo.Nakano@Sun.COM amount_read = amt_to_read;
285710202SNobutomo.Nakano@Sun.COM } else if (amount_read == amt_to_read) {
285810202SNobutomo.Nakano@Sun.COM /* the file is the same size */
285910202SNobutomo.Nakano@Sun.COM *real_filesz = file_size;
286010202SNobutomo.Nakano@Sun.COM }
286110202SNobutomo.Nakano@Sun.COM
286210202SNobutomo.Nakano@Sun.COM Buffr.b_in_p += amount_read;
286310202SNobutomo.Nakano@Sun.COM Buffr.b_cnt += (long)amount_read;
286410202SNobutomo.Nakano@Sun.COM
286510202SNobutomo.Nakano@Sun.COM amt_to_read -= (off_t)amount_read;
286610202SNobutomo.Nakano@Sun.COM if (!read_exact &&
286710202SNobutomo.Nakano@Sun.COM amt_to_read == 0 && amount_read == CPIOBSZ) {
286810202SNobutomo.Nakano@Sun.COM /*
286910202SNobutomo.Nakano@Sun.COM * If the file size is multiple of CPIOBSZ, we may
287010202SNobutomo.Nakano@Sun.COM * be able to read more from the file even though
287110202SNobutomo.Nakano@Sun.COM * amt_to_read already gets 0.
287210202SNobutomo.Nakano@Sun.COM */
287310202SNobutomo.Nakano@Sun.COM FLUSH(CPIOBSZ);
287410202SNobutomo.Nakano@Sun.COM amount_read = read(Ifile, Buffr.b_in_p, CPIOBSZ);
287510202SNobutomo.Nakano@Sun.COM if (amount_read != 0) {
287610202SNobutomo.Nakano@Sun.COM /* the file has grown */
287710202SNobutomo.Nakano@Sun.COM *real_filesz = file_size + amount_read;
287810202SNobutomo.Nakano@Sun.COM }
287910202SNobutomo.Nakano@Sun.COM }
288010202SNobutomo.Nakano@Sun.COM } while (amt_to_read != 0);
288110202SNobutomo.Nakano@Sun.COM
288210202SNobutomo.Nakano@Sun.COM return (amt_to_read);
288310202SNobutomo.Nakano@Sun.COM }
288410202SNobutomo.Nakano@Sun.COM
288510202SNobutomo.Nakano@Sun.COM /*
288610202SNobutomo.Nakano@Sun.COM * Read through the data in files skipping holes.
288710202SNobutomo.Nakano@Sun.COM */
288810202SNobutomo.Nakano@Sun.COM static off_t
read_compress_holes(char * nam_p,off_t file_size,off_t * real_filesz,holes_info_t * holes,int * hole_changed)288910202SNobutomo.Nakano@Sun.COM read_compress_holes(char *nam_p, off_t file_size, off_t *real_filesz,
289010202SNobutomo.Nakano@Sun.COM holes_info_t *holes, int *hole_changed)
289110202SNobutomo.Nakano@Sun.COM {
289210202SNobutomo.Nakano@Sun.COM off_t left;
289310202SNobutomo.Nakano@Sun.COM off_t datasize, realsz;
289410202SNobutomo.Nakano@Sun.COM off_t curpos, npos;
289510202SNobutomo.Nakano@Sun.COM holes_list_t *hl = holes->holes_list;
289610202SNobutomo.Nakano@Sun.COM
289710202SNobutomo.Nakano@Sun.COM curpos = 0;
289810202SNobutomo.Nakano@Sun.COM for (hl = holes->holes_list; hl != NULL; hl = hl->hl_next) {
289910202SNobutomo.Nakano@Sun.COM datasize = hl->hl_hole - hl->hl_data;
290010202SNobutomo.Nakano@Sun.COM
290110202SNobutomo.Nakano@Sun.COM npos = lseek(Ifile, curpos, SEEK_DATA);
290210202SNobutomo.Nakano@Sun.COM if (npos == -1 && errno == ENXIO) {
290310202SNobutomo.Nakano@Sun.COM /*
290410202SNobutomo.Nakano@Sun.COM * No more data. There are two cases.
290510202SNobutomo.Nakano@Sun.COM * - we have a hole toward the end of file.
290610202SNobutomo.Nakano@Sun.COM * - file has been shrunk, and we've reached EOF.
290710202SNobutomo.Nakano@Sun.COM */
290810202SNobutomo.Nakano@Sun.COM *real_filesz = lseek(Ifile, 0, SEEK_END);
290910202SNobutomo.Nakano@Sun.COM if (hl->hl_data == file_size)
291010202SNobutomo.Nakano@Sun.COM return (0);
291110202SNobutomo.Nakano@Sun.COM /*
291210202SNobutomo.Nakano@Sun.COM * File has been shrunk. Check the amount of data
291310202SNobutomo.Nakano@Sun.COM * left.
291410202SNobutomo.Nakano@Sun.COM */
291510202SNobutomo.Nakano@Sun.COM left = 0;
291610202SNobutomo.Nakano@Sun.COM while (hl != NULL) {
291710202SNobutomo.Nakano@Sun.COM left += (hl->hl_hole - hl->hl_data);
291810202SNobutomo.Nakano@Sun.COM hl = hl->hl_next;
291910202SNobutomo.Nakano@Sun.COM }
292010202SNobutomo.Nakano@Sun.COM return (left);
292110202SNobutomo.Nakano@Sun.COM }
292210202SNobutomo.Nakano@Sun.COM
292310202SNobutomo.Nakano@Sun.COM /* found data */
292410202SNobutomo.Nakano@Sun.COM curpos = npos;
292510202SNobutomo.Nakano@Sun.COM if (curpos != hl->hl_data) {
292610202SNobutomo.Nakano@Sun.COM /*
292710202SNobutomo.Nakano@Sun.COM * File has been changed. We shouldn't read data
292810202SNobutomo.Nakano@Sun.COM * from different offset since we've already put
292910202SNobutomo.Nakano@Sun.COM * the holes data.
293010202SNobutomo.Nakano@Sun.COM */
293110202SNobutomo.Nakano@Sun.COM *hole_changed = 1;
293210202SNobutomo.Nakano@Sun.COM (void) lseek(Ifile, hl->hl_data, SEEK_SET);
293310202SNobutomo.Nakano@Sun.COM curpos = hl->hl_data;
293410202SNobutomo.Nakano@Sun.COM }
293510202SNobutomo.Nakano@Sun.COM left = read_file(nam_p, datasize, &realsz, B_TRUE);
293610202SNobutomo.Nakano@Sun.COM if (left != 0) {
293710202SNobutomo.Nakano@Sun.COM /* file has been shrunk */
293810202SNobutomo.Nakano@Sun.COM *real_filesz = curpos + datasize - left;
293910202SNobutomo.Nakano@Sun.COM left = file_size - *real_filesz;
294010202SNobutomo.Nakano@Sun.COM return (left);
294110202SNobutomo.Nakano@Sun.COM }
294210202SNobutomo.Nakano@Sun.COM curpos += datasize;
294310202SNobutomo.Nakano@Sun.COM }
294410202SNobutomo.Nakano@Sun.COM /*
294510202SNobutomo.Nakano@Sun.COM * We've read exact size of holes. We need to make sure
294610202SNobutomo.Nakano@Sun.COM * that file hasn't grown by reading from the EOF.
294710202SNobutomo.Nakano@Sun.COM */
294810202SNobutomo.Nakano@Sun.COM realsz = 0;
294910202SNobutomo.Nakano@Sun.COM (void) read_file(nam_p, CPIOBSZ, &realsz, B_FALSE);
295010202SNobutomo.Nakano@Sun.COM
295110202SNobutomo.Nakano@Sun.COM *real_filesz = curpos + realsz;
295210202SNobutomo.Nakano@Sun.COM return (0);
295310202SNobutomo.Nakano@Sun.COM }
295410202SNobutomo.Nakano@Sun.COM
295510202SNobutomo.Nakano@Sun.COM /*
29560Sstevel@tonic-gate * data_out: open(2) the file to be archived, compute the checksum
29570Sstevel@tonic-gate * of it's data if the CRC header was specified and write the header.
29580Sstevel@tonic-gate * read(2) each block of data and bwrite() it to the archive. For TARTYP (TAR
29590Sstevel@tonic-gate * and USTAR) archives, pad the data with NULLs to the next 512 byte boundary.
29600Sstevel@tonic-gate */
29610Sstevel@tonic-gate static void
data_out(void)29620Sstevel@tonic-gate data_out(void)
29630Sstevel@tonic-gate {
296410202SNobutomo.Nakano@Sun.COM char *nam_p;
296510202SNobutomo.Nakano@Sun.COM int cnt, pad;
296610202SNobutomo.Nakano@Sun.COM off_t amt_to_read;
296710202SNobutomo.Nakano@Sun.COM off_t real_filesz;
296810202SNobutomo.Nakano@Sun.COM int errret = 0;
296910202SNobutomo.Nakano@Sun.COM int hole_changed = 0;
297010202SNobutomo.Nakano@Sun.COM off_t orig_filesz;
297110202SNobutomo.Nakano@Sun.COM holes_info_t *holes = NULL;
29720Sstevel@tonic-gate
29730Sstevel@tonic-gate nam_p = G_p->g_nam_p;
29740Sstevel@tonic-gate if (Aspec) {
29750Sstevel@tonic-gate if (Pflag && aclp != NULL) {
29760Sstevel@tonic-gate char *secinfo = NULL;
29770Sstevel@tonic-gate int len = 0;
29780Sstevel@tonic-gate
29790Sstevel@tonic-gate /* append security attributes */
2980789Sahrens if (append_secattr(&secinfo, &len, aclp) == -1) {
29810Sstevel@tonic-gate msg(ERR,
29820Sstevel@tonic-gate "can create security information");
29830Sstevel@tonic-gate }
29840Sstevel@tonic-gate /* call append_secattr() if more than one */
29850Sstevel@tonic-gate
29860Sstevel@tonic-gate if (len > 0) {
29870Sstevel@tonic-gate /* write ancillary only if there is sec info */
298810202SNobutomo.Nakano@Sun.COM write_hdr(ARCHIVE_ACL, (off_t)len);
298910202SNobutomo.Nakano@Sun.COM write_ancillary(secinfo, len, B_TRUE);
29900Sstevel@tonic-gate }
29910Sstevel@tonic-gate }
29920Sstevel@tonic-gate write_hdr(ARCHIVE_NORMAL, (off_t)0);
29930Sstevel@tonic-gate rstfiles(U_KEEP, G_p->g_dirfd);
29940Sstevel@tonic-gate VERBOSE((Args & (OCv | OCV)), nam_p);
29950Sstevel@tonic-gate return;
29960Sstevel@tonic-gate }
29970Sstevel@tonic-gate if ((G_p->g_mode & Ftype) == S_IFLNK && (Hdr_type !=
29980Sstevel@tonic-gate USTAR && Hdr_type != TAR)) { /* symbolic link */
29990Sstevel@tonic-gate int size;
30000Sstevel@tonic-gate write_hdr(ARCHIVE_NORMAL, (off_t)0);
30010Sstevel@tonic-gate
30020Sstevel@tonic-gate FLUSH(G_p->g_filesz);
30030Sstevel@tonic-gate errno = 0;
30040Sstevel@tonic-gate
30050Sstevel@tonic-gate /* Note that "size" and G_p->g_filesz are the same number */
30060Sstevel@tonic-gate
30070Sstevel@tonic-gate if ((size = readlink(nam_p, Buffr.b_in_p, G_p->g_filesz)) <
30080Sstevel@tonic-gate 0) {
30090Sstevel@tonic-gate msg(ERRN, "Cannot read symbolic link \"%s\"", nam_p);
30100Sstevel@tonic-gate return;
30110Sstevel@tonic-gate }
30120Sstevel@tonic-gate
30130Sstevel@tonic-gate /*
30140Sstevel@tonic-gate * Note that it is OK not to add the NUL after the name read by
30150Sstevel@tonic-gate * readlink, because it is not being used subsequently.
30160Sstevel@tonic-gate */
30170Sstevel@tonic-gate
30180Sstevel@tonic-gate Buffr.b_in_p += size;
30190Sstevel@tonic-gate Buffr.b_cnt += size;
30200Sstevel@tonic-gate pad = (Pad_val + 1 - (size & Pad_val)) & Pad_val;
30210Sstevel@tonic-gate if (pad != 0) {
30220Sstevel@tonic-gate FLUSH(pad);
302310202SNobutomo.Nakano@Sun.COM (void) memset(Buffr.b_in_p, 0, pad);
30240Sstevel@tonic-gate Buffr.b_in_p += pad;
30250Sstevel@tonic-gate Buffr.b_cnt += pad;
30260Sstevel@tonic-gate }
30270Sstevel@tonic-gate VERBOSE((Args & (OCv | OCV)), nam_p);
30280Sstevel@tonic-gate return;
30290Sstevel@tonic-gate } else if ((G_p->g_mode & Ftype) == S_IFLNK &&
30301134Sceastha (Hdr_type == USTAR || Hdr_type == TAR)) {
30310Sstevel@tonic-gate int size;
30320Sstevel@tonic-gate
30330Sstevel@tonic-gate /*
30340Sstevel@tonic-gate * G_p->g_filesz is the length of the right-hand side of
30350Sstevel@tonic-gate * the symlink "x -> y".
30360Sstevel@tonic-gate * The tar link field is only NAMSIZ long.
30370Sstevel@tonic-gate */
30380Sstevel@tonic-gate
30390Sstevel@tonic-gate if (G_p->g_filesz > NAMSIZ) {
30400Sstevel@tonic-gate msg(ERRN,
30410Sstevel@tonic-gate "Symbolic link too long \"%s\"", nam_p);
30420Sstevel@tonic-gate return;
30430Sstevel@tonic-gate }
30440Sstevel@tonic-gate if ((size = readlink(nam_p, T_lname, G_p->g_filesz)) < 0) {
30450Sstevel@tonic-gate msg(ERRN,
30460Sstevel@tonic-gate "Cannot read symbolic link \"%s\"", nam_p);
30470Sstevel@tonic-gate return;
30480Sstevel@tonic-gate }
30490Sstevel@tonic-gate T_lname[size] = '\0';
30500Sstevel@tonic-gate G_p->g_filesz = (off_t)0;
30510Sstevel@tonic-gate write_hdr(ARCHIVE_NORMAL, (off_t)0);
30520Sstevel@tonic-gate VERBOSE((Args & (OCv | OCV)), nam_p);
30530Sstevel@tonic-gate return;
30540Sstevel@tonic-gate }
30550Sstevel@tonic-gate if ((Ifile = openfile(O_RDONLY)) < 0) {
30560Sstevel@tonic-gate msg(ERR, "\"%s%s%s\" ?",
30575750Sceastha (Gen.g_attrnam_p == NULL) ? nam_p : Gen.g_attrfnam_p,
30585750Sceastha (Gen.g_attrnam_p == NULL) ? "" : Gen.g_rw_sysattr ?
30595750Sceastha gettext(" System Attribute ") : gettext(" Attribute "),
30605750Sceastha (Gen.g_attrnam_p == NULL) ? "" :
30615750Sceastha (Gen.g_attrparent_p == NULL) ? Gen.g_attrnam_p :
30625750Sceastha Gen.g_attrparent_p);
30630Sstevel@tonic-gate return;
30640Sstevel@tonic-gate }
30650Sstevel@tonic-gate
306610202SNobutomo.Nakano@Sun.COM /* save original file size */
306710202SNobutomo.Nakano@Sun.COM orig_filesz = G_p->g_filesz;
306810202SNobutomo.Nakano@Sun.COM
306910202SNobutomo.Nakano@Sun.COM /*
307010202SNobutomo.Nakano@Sun.COM * Calculate the new compressed file size of a sparse file
307110202SNobutomo.Nakano@Sun.COM * before any of the header information is written
307210202SNobutomo.Nakano@Sun.COM * to the archive.
307310202SNobutomo.Nakano@Sun.COM */
307410202SNobutomo.Nakano@Sun.COM if (Compress_sparse && S_ISREG(G_p->g_mode)) {
307510202SNobutomo.Nakano@Sun.COM /*
307610202SNobutomo.Nakano@Sun.COM * If the file being processed is a sparse file, gather the
307710202SNobutomo.Nakano@Sun.COM * hole information and the compressed file size.
307810202SNobutomo.Nakano@Sun.COM * G_p->g_filesz will need to be changed to be the size of
307910202SNobutomo.Nakano@Sun.COM * the compressed sparse file plus the the size of the hole
308010202SNobutomo.Nakano@Sun.COM * information that will be prepended to the compressed file
308110202SNobutomo.Nakano@Sun.COM * in the archive.
308210202SNobutomo.Nakano@Sun.COM */
308310202SNobutomo.Nakano@Sun.COM holes = get_holes_info(Ifile, G_p->g_filesz, B_FALSE);
308410202SNobutomo.Nakano@Sun.COM if (holes != NULL)
308510202SNobutomo.Nakano@Sun.COM G_p->g_filesz = holes->holesdata_sz + holes->data_size;
308610202SNobutomo.Nakano@Sun.COM
308710202SNobutomo.Nakano@Sun.COM if (G_p->g_filesz > Max_offset) {
308810202SNobutomo.Nakano@Sun.COM msg(ERR, "%s%s%s: too large to archive "
308910202SNobutomo.Nakano@Sun.COM "in current mode",
309010202SNobutomo.Nakano@Sun.COM G_p->g_nam_p,
309110202SNobutomo.Nakano@Sun.COM (G_p->g_attrnam_p == NULL) ? "" :
309210202SNobutomo.Nakano@Sun.COM G_p->g_rw_sysattr ?
309310202SNobutomo.Nakano@Sun.COM gettext(" System Attribute ") :
309410202SNobutomo.Nakano@Sun.COM gettext(" Attribute "),
309510202SNobutomo.Nakano@Sun.COM (G_p->g_attrnam_p == NULL) ? "" :
309610202SNobutomo.Nakano@Sun.COM ((G_p->g_attrparent_p == NULL) ?
309710202SNobutomo.Nakano@Sun.COM G_p->g_attrnam_p:
309810202SNobutomo.Nakano@Sun.COM G_p->g_attrpath_p));
309910202SNobutomo.Nakano@Sun.COM
310010202SNobutomo.Nakano@Sun.COM (void) close(Ifile);
310110202SNobutomo.Nakano@Sun.COM if (holes != NULL)
310210202SNobutomo.Nakano@Sun.COM free_holes_info(holes);
310310202SNobutomo.Nakano@Sun.COM return; /* do not archive if it's too big */
310410202SNobutomo.Nakano@Sun.COM }
310510202SNobutomo.Nakano@Sun.COM }
310610202SNobutomo.Nakano@Sun.COM
31070Sstevel@tonic-gate /*
31080Sstevel@tonic-gate * Dump extended attribute header.
31090Sstevel@tonic-gate */
31100Sstevel@tonic-gate
31115750Sceastha if (Gen.g_attrnam_p != NULL) {
31120Sstevel@tonic-gate write_xattr_hdr();
31130Sstevel@tonic-gate }
31140Sstevel@tonic-gate
31150Sstevel@tonic-gate if (Hdr_type == CRC) {
31160Sstevel@tonic-gate long csum = cksum(CRC, 0, &errret);
31170Sstevel@tonic-gate if (errret != 0) {
31180Sstevel@tonic-gate G_p->g_cksum = (ulong_t)-1;
31190Sstevel@tonic-gate msg(POST, "\"%s%s%s\" skipped",
31205750Sceastha (Gen.g_attrnam_p == NULL) ?
31210Sstevel@tonic-gate nam_p : Gen.g_attrfnam_p,
31225750Sceastha (Gen.g_attrnam_p == NULL) ? "" : Gen.g_rw_sysattr ?
31235750Sceastha gettext(" System Attribute ") :
31245750Sceastha gettext(" Attribute "),
31255750Sceastha (Gen.g_attrnam_p == NULL) ? "" : nam_p);
312610202SNobutomo.Nakano@Sun.COM if (holes != NULL)
312710202SNobutomo.Nakano@Sun.COM free_holes_info(holes);
31280Sstevel@tonic-gate (void) close(Ifile);
31290Sstevel@tonic-gate return;
31300Sstevel@tonic-gate }
31310Sstevel@tonic-gate G_p->g_cksum = csum;
31320Sstevel@tonic-gate } else {
31330Sstevel@tonic-gate G_p->g_cksum = 0;
31340Sstevel@tonic-gate }
31350Sstevel@tonic-gate
31360Sstevel@tonic-gate /*
31370Sstevel@tonic-gate * ACL has been retrieved in getname().
31380Sstevel@tonic-gate */
31390Sstevel@tonic-gate if (Pflag) {
31400Sstevel@tonic-gate char *secinfo = NULL;
31410Sstevel@tonic-gate int len = 0;
31420Sstevel@tonic-gate
31430Sstevel@tonic-gate /* append security attributes */
3144789Sahrens if ((append_secattr(&secinfo, &len, aclp)) == -1)
31450Sstevel@tonic-gate msg(ERR, "can create security information");
31460Sstevel@tonic-gate
31470Sstevel@tonic-gate /* call append_secattr() if more than one */
31480Sstevel@tonic-gate
31490Sstevel@tonic-gate if (len > 0) {
31500Sstevel@tonic-gate /* write ancillary only if there is sec info */
315110202SNobutomo.Nakano@Sun.COM write_hdr(ARCHIVE_ACL, (off_t)len);
315210202SNobutomo.Nakano@Sun.COM write_ancillary(secinfo, len, B_TRUE);
315310202SNobutomo.Nakano@Sun.COM }
315410202SNobutomo.Nakano@Sun.COM }
315510202SNobutomo.Nakano@Sun.COM
315610202SNobutomo.Nakano@Sun.COM if (holes != NULL) {
315710202SNobutomo.Nakano@Sun.COM /*
315810202SNobutomo.Nakano@Sun.COM * Write the header info with a modified c_mode field to
315910202SNobutomo.Nakano@Sun.COM * indicate a compressed sparse file is being archived,
316010202SNobutomo.Nakano@Sun.COM * as well as the new file size, including the size of the
316110202SNobutomo.Nakano@Sun.COM * compressed file as well as all the prepended data.
316210202SNobutomo.Nakano@Sun.COM */
316310202SNobutomo.Nakano@Sun.COM write_hdr(ARCHIVE_SPARSE, (off_t)0);
316410202SNobutomo.Nakano@Sun.COM /* Prepend sparse file info */
316510202SNobutomo.Nakano@Sun.COM write_ancillary(holes->holesdata,
316610202SNobutomo.Nakano@Sun.COM holes->holesdata_sz, B_FALSE);
316710202SNobutomo.Nakano@Sun.COM } else {
316810202SNobutomo.Nakano@Sun.COM write_hdr(ARCHIVE_NORMAL, (off_t)0);
316910202SNobutomo.Nakano@Sun.COM }
31700Sstevel@tonic-gate
31710Sstevel@tonic-gate real_filesz = 0;
31720Sstevel@tonic-gate
317310202SNobutomo.Nakano@Sun.COM if (holes != NULL) {
317410202SNobutomo.Nakano@Sun.COM amt_to_read = read_compress_holes(nam_p, G_p->g_filesz,
317510202SNobutomo.Nakano@Sun.COM &real_filesz, holes, &hole_changed);
317610202SNobutomo.Nakano@Sun.COM } else {
317710202SNobutomo.Nakano@Sun.COM amt_to_read = read_file(nam_p, G_p->g_filesz,
317810202SNobutomo.Nakano@Sun.COM &real_filesz, B_FALSE);
31790Sstevel@tonic-gate }
31800Sstevel@tonic-gate
31810Sstevel@tonic-gate while (amt_to_read > 0) {
31820Sstevel@tonic-gate cnt = (amt_to_read > CPIOBSZ) ? CPIOBSZ : (int)amt_to_read;
31830Sstevel@tonic-gate FLUSH(cnt);
318410202SNobutomo.Nakano@Sun.COM (void) memset(Buffr.b_in_p, 0, cnt);
31850Sstevel@tonic-gate Buffr.b_in_p += cnt;
31860Sstevel@tonic-gate Buffr.b_cnt += cnt;
31870Sstevel@tonic-gate amt_to_read -= cnt;
31880Sstevel@tonic-gate }
31890Sstevel@tonic-gate
31900Sstevel@tonic-gate pad = (Pad_val + 1 - (G_p->g_filesz & Pad_val)) & Pad_val;
31910Sstevel@tonic-gate if (pad != 0) {
31920Sstevel@tonic-gate FLUSH(pad);
319310202SNobutomo.Nakano@Sun.COM (void) memset(Buffr.b_in_p, 0, pad);
31940Sstevel@tonic-gate Buffr.b_in_p += pad;
31950Sstevel@tonic-gate Buffr.b_cnt += pad;
31960Sstevel@tonic-gate }
31970Sstevel@tonic-gate
319810202SNobutomo.Nakano@Sun.COM if (hole_changed == 1) {
319910202SNobutomo.Nakano@Sun.COM msg(ERR,
320010202SNobutomo.Nakano@Sun.COM "File data and hole offsets of \"%s%s%s\" have changed",
320110202SNobutomo.Nakano@Sun.COM (Gen.g_attrnam_p == NULL) ?
320210202SNobutomo.Nakano@Sun.COM G_p->g_nam_p : Gen.g_attrfnam_p,
320310202SNobutomo.Nakano@Sun.COM (Gen.g_attrnam_p == NULL) ? "" : Gen.g_rw_sysattr ?
320410202SNobutomo.Nakano@Sun.COM gettext(" System Attribute ") : gettext(" Attribute "),
320510202SNobutomo.Nakano@Sun.COM (Gen.g_attrnam_p == NULL) ? "" : G_p->g_nam_p);
320610202SNobutomo.Nakano@Sun.COM }
320710202SNobutomo.Nakano@Sun.COM if (real_filesz > orig_filesz) {
32080Sstevel@tonic-gate msg(ERR, "File size of \"%s%s%s\" has increased by %lld",
32095750Sceastha (Gen.g_attrnam_p == NULL) ?
32100Sstevel@tonic-gate G_p->g_nam_p : Gen.g_attrfnam_p,
32115750Sceastha (Gen.g_attrnam_p == NULL) ? "" : Gen.g_rw_sysattr ?
32125750Sceastha gettext(" System Attribute ") : gettext(" Attribute "),
32135750Sceastha (Gen.g_attrnam_p == NULL) ? "" : G_p->g_nam_p,
321410202SNobutomo.Nakano@Sun.COM (real_filesz - orig_filesz));
321510202SNobutomo.Nakano@Sun.COM }
321610202SNobutomo.Nakano@Sun.COM if (real_filesz < orig_filesz) {
32170Sstevel@tonic-gate msg(ERR, "File size of \"%s%s%s\" has decreased by %lld",
32185750Sceastha (Gen.g_attrnam_p == NULL) ?
32190Sstevel@tonic-gate G_p->g_nam_p : Gen.g_attrfnam_p,
32205750Sceastha (Gen.g_attrnam_p == NULL) ? "" : Gen.g_rw_sysattr ?
32215750Sceastha gettext(" System Attribute ") : gettext(" Attribute "),
32225750Sceastha (Gen.g_attrnam_p == NULL) ? "" : G_p->g_nam_p,
322310202SNobutomo.Nakano@Sun.COM (orig_filesz - real_filesz));
322410202SNobutomo.Nakano@Sun.COM }
322510202SNobutomo.Nakano@Sun.COM
322610202SNobutomo.Nakano@Sun.COM if (holes != NULL)
322710202SNobutomo.Nakano@Sun.COM free_holes_info(holes);
32280Sstevel@tonic-gate
32290Sstevel@tonic-gate (void) close(Ifile);
32300Sstevel@tonic-gate rstfiles(U_KEEP, G_p->g_dirfd);
32310Sstevel@tonic-gate VERBOSE((Args & (OCv | OCV)), G_p->g_nam_p);
32320Sstevel@tonic-gate }
32330Sstevel@tonic-gate
32340Sstevel@tonic-gate /*
32350Sstevel@tonic-gate * data_pass: If not a special file (Aspec), open(2) the file to be
32360Sstevel@tonic-gate * transferred, read(2) each block of data and write(2) it to the output file
32370Sstevel@tonic-gate * Ofile, which was opened in file_pass().
32380Sstevel@tonic-gate */
32390Sstevel@tonic-gate static void
data_pass(void)32400Sstevel@tonic-gate data_pass(void)
32410Sstevel@tonic-gate {
32425750Sceastha int rv;
32430Sstevel@tonic-gate int cstatus;
32440Sstevel@tonic-gate char *namep = Nam_p;
324510202SNobutomo.Nakano@Sun.COM holes_info_t *holes = NULL;
324610389SNobutomo.Nakano@Sun.COM data_in_t *data_in_info;
32470Sstevel@tonic-gate
32485750Sceastha if (G_p->g_attrnam_p != NULL) {
32490Sstevel@tonic-gate namep = G_p->g_attrnam_p;
32500Sstevel@tonic-gate }
32510Sstevel@tonic-gate if (Aspec) {
32520Sstevel@tonic-gate rstfiles(U_KEEP, G_p->g_passdirfd);
32530Sstevel@tonic-gate cstatus = close(Ofile);
32540Sstevel@tonic-gate Ofile = 0;
32550Sstevel@tonic-gate VERBOSE((Args & (OCv | OCV)), Nam_p);
32560Sstevel@tonic-gate if (cstatus != 0) {
32570Sstevel@tonic-gate msg(EXTN, "close error");
32580Sstevel@tonic-gate }
32590Sstevel@tonic-gate return;
32600Sstevel@tonic-gate }
32610Sstevel@tonic-gate if ((Ifile = openat(G_p->g_dirfd, get_component(namep), 0)) < 0) {
32620Sstevel@tonic-gate msg(ERRN, "Cannot open \"%s%s%s\", skipped",
32635750Sceastha (G_p->g_attrnam_p == NULL) ? Nam_p : G_p->g_attrfnam_p,
32645750Sceastha (G_p->g_attrnam_p == NULL) ? "" : G_p->g_rw_sysattr ?
32655750Sceastha gettext(" System Attribute ") : gettext(" Attribute "),
32665750Sceastha (G_p->g_attrnam_p == NULL) ? "" : G_p->g_attrnam_p);
32670Sstevel@tonic-gate rstfiles(U_KEEP, G_p->g_passdirfd);
32680Sstevel@tonic-gate cstatus = close(Ofile);
32690Sstevel@tonic-gate Ofile = 0;
32700Sstevel@tonic-gate if (cstatus != 0) {
32710Sstevel@tonic-gate msg(EXTN, "close error");
32720Sstevel@tonic-gate }
32730Sstevel@tonic-gate return;
32740Sstevel@tonic-gate }
32755750Sceastha
327610389SNobutomo.Nakano@Sun.COM data_in_info = e_zalloc(E_EXIT, sizeof (data_in_t));
327710389SNobutomo.Nakano@Sun.COM data_in_info->data_in_proc_mode = P_PROC;
327810389SNobutomo.Nakano@Sun.COM
327910202SNobutomo.Nakano@Sun.COM if (S_ISREG(G_p->g_mode))
328010202SNobutomo.Nakano@Sun.COM holes = get_holes_info(Ifile, G_p->g_filesz, B_TRUE);
328110202SNobutomo.Nakano@Sun.COM
328210202SNobutomo.Nakano@Sun.COM if (holes != NULL) {
328310202SNobutomo.Nakano@Sun.COM rv = data_copy_with_holes(Ifile, Ofile,
328410202SNobutomo.Nakano@Sun.COM (G_p->g_attrnam_p == NULL) ? 0 : G_p->g_rw_sysattr,
328510389SNobutomo.Nakano@Sun.COM G_p->g_filesz, Bufsize, data_in_info, holes);
328610202SNobutomo.Nakano@Sun.COM
328710202SNobutomo.Nakano@Sun.COM free_holes_info(holes);
328810202SNobutomo.Nakano@Sun.COM } else {
328910202SNobutomo.Nakano@Sun.COM rv = data_copy(Ifile, Ofile,
329010202SNobutomo.Nakano@Sun.COM (G_p->g_attrnam_p == NULL) ? 0 : G_p->g_rw_sysattr,
329110389SNobutomo.Nakano@Sun.COM G_p->g_filesz, Bufsize, data_in_info);
329210389SNobutomo.Nakano@Sun.COM }
329310389SNobutomo.Nakano@Sun.COM
329410389SNobutomo.Nakano@Sun.COM if (rv < 0) {
329510389SNobutomo.Nakano@Sun.COM /* read error or unexpected EOF */
329610389SNobutomo.Nakano@Sun.COM if (data_in_info->data_in_rd_eof) {
329710389SNobutomo.Nakano@Sun.COM /*
329810389SNobutomo.Nakano@Sun.COM * read has reached EOF unexpectedly, but this isn't
329910389SNobutomo.Nakano@Sun.COM * an error since it's the latest shape of the file.
330010389SNobutomo.Nakano@Sun.COM */
330110389SNobutomo.Nakano@Sun.COM msg(EPOST, "File size of \"%s%s%s\" has decreased",
330210389SNobutomo.Nakano@Sun.COM (G_p->g_attrnam_p == NULL) ?
330310389SNobutomo.Nakano@Sun.COM Nam_p : G_p->g_attrfnam_p,
330410389SNobutomo.Nakano@Sun.COM (G_p->g_attrnam_p == NULL) ? "" :
330510389SNobutomo.Nakano@Sun.COM G_p->g_rw_sysattr ? gettext(" System Attribute ") :
330610389SNobutomo.Nakano@Sun.COM gettext(" Attribute "),
330710389SNobutomo.Nakano@Sun.COM (G_p->g_attrnam_p == NULL) ? "" : G_p->g_attrnam_p);
330810389SNobutomo.Nakano@Sun.COM
330910389SNobutomo.Nakano@Sun.COM /* It's not error. We'll use the new file */
331010389SNobutomo.Nakano@Sun.COM rv = 0;
331110389SNobutomo.Nakano@Sun.COM } else {
331210389SNobutomo.Nakano@Sun.COM /* read error */
33130Sstevel@tonic-gate msg(ERRN, "Cannot read \"%s%s%s\"",
33145750Sceastha (G_p->g_attrnam_p == NULL) ?
33150Sstevel@tonic-gate Nam_p : G_p->g_attrfnam_p,
33165750Sceastha (G_p->g_attrnam_p == NULL) ? "" :
33175750Sceastha G_p->g_rw_sysattr ? gettext(" System Attribute ") :
33185750Sceastha gettext(" Attribute "),
33195750Sceastha (G_p->g_attrnam_p == NULL) ? "" : G_p->g_attrnam_p);
332010389SNobutomo.Nakano@Sun.COM }
332110389SNobutomo.Nakano@Sun.COM } else if (rv > 0) {
33225750Sceastha /* write error */
332310389SNobutomo.Nakano@Sun.COM if (Do_rename) {
332410389SNobutomo.Nakano@Sun.COM msg(ERRN, "Cannot write \"%s%s%s\"", Over_p,
332510389SNobutomo.Nakano@Sun.COM (G_p->g_attrnam_p == NULL) ? "" :
332610389SNobutomo.Nakano@Sun.COM G_p->g_rw_sysattr ? gettext(" System Attribute ") :
332710389SNobutomo.Nakano@Sun.COM gettext(" Attribute "),
332810389SNobutomo.Nakano@Sun.COM (G_p->g_attrnam_p == NULL) ? "" : Over_p);
332910389SNobutomo.Nakano@Sun.COM } else {
333010389SNobutomo.Nakano@Sun.COM msg(ERRN, "Cannot write \"%s%s%s\"",
333110389SNobutomo.Nakano@Sun.COM Fullnam_p,
333210389SNobutomo.Nakano@Sun.COM (G_p->g_attrnam_p == NULL) ? "" :
333310389SNobutomo.Nakano@Sun.COM G_p->g_rw_sysattr ? gettext(" System Attribute ") :
333410389SNobutomo.Nakano@Sun.COM gettext(" Attribute "),
333510389SNobutomo.Nakano@Sun.COM (G_p->g_attrnam_p == NULL) ? "" : G_p->g_attrnam_p);
333610389SNobutomo.Nakano@Sun.COM }
333710389SNobutomo.Nakano@Sun.COM }
333810389SNobutomo.Nakano@Sun.COM
333910389SNobutomo.Nakano@Sun.COM free(data_in_info);
334010202SNobutomo.Nakano@Sun.COM
334110202SNobutomo.Nakano@Sun.COM if (rv == 0) {
33420Sstevel@tonic-gate rstfiles(U_OVER, G_p->g_passdirfd);
33430Sstevel@tonic-gate } else {
33440Sstevel@tonic-gate rstfiles(U_KEEP, G_p->g_passdirfd);
33450Sstevel@tonic-gate }
33465750Sceastha
33470Sstevel@tonic-gate (void) close(Ifile);
33480Sstevel@tonic-gate cstatus = close(Ofile);
33490Sstevel@tonic-gate Ofile = 0;
33500Sstevel@tonic-gate if (cstatus != 0) {
33510Sstevel@tonic-gate msg(EXTN, "close error");
33520Sstevel@tonic-gate }
33530Sstevel@tonic-gate VERBOSE((Args & (OCv | OCV)), Fullnam_p);
33540Sstevel@tonic-gate Finished = 1;
33550Sstevel@tonic-gate }
33560Sstevel@tonic-gate
33570Sstevel@tonic-gate /*
33580Sstevel@tonic-gate * file_in: Process an object from the archive. If a TARTYP (TAR or USTAR)
33590Sstevel@tonic-gate * archive and g_nlink == 1, link this file to the file name in t_linkname
33600Sstevel@tonic-gate * and return. Handle linked files in one of two ways. If Onecopy == 0, this
33610Sstevel@tonic-gate * is an old style (binary or -c) archive, create and extract the data for the
33620Sstevel@tonic-gate * first link found, link all subsequent links to this file and skip their data.
33630Sstevel@tonic-gate * If Oncecopy == 1, save links until all have been processed, and then
33640Sstevel@tonic-gate * process the links first to last checking their names against the patterns
33650Sstevel@tonic-gate * and/or asking the user to rename them. The first link that is accepted
33660Sstevel@tonic-gate * for xtraction is created and the data is read from the archive.
33670Sstevel@tonic-gate * All subsequent links that are accepted are linked to this file.
33680Sstevel@tonic-gate */
33690Sstevel@tonic-gate static void
file_in(void)33700Sstevel@tonic-gate file_in(void)
33710Sstevel@tonic-gate {
33720Sstevel@tonic-gate struct Lnk *l_p, *tl_p;
33730Sstevel@tonic-gate int lnkem = 0, cleanup = 0;
33740Sstevel@tonic-gate int proc_file;
33750Sstevel@tonic-gate struct Lnk *ttl_p;
33760Sstevel@tonic-gate int typeflag;
33770Sstevel@tonic-gate char savacl;
33780Sstevel@tonic-gate int cwd;
33790Sstevel@tonic-gate
33800Sstevel@tonic-gate G_p = &Gen;
33810Sstevel@tonic-gate
33820Sstevel@tonic-gate /*
33835750Sceastha * Now that we've read the extended header,
33845750Sceastha * determine if we should restore attributes.
33855750Sceastha * Don't restore the attribute if we are extracting
33865750Sceastha * a file from an archive (as opposed to doing a table of
33875750Sceastha * contents) and any of the following are true:
33885750Sceastha * 1. neither -@ or -/ was specified.
33895750Sceastha * 2. -@ was specified, -/ wasn't specified, and we're
33905750Sceastha * processing a hidden attribute directory of an attribute
33915750Sceastha * or we're processing a read-write system attribute file.
33925750Sceastha * 3. -@ wasn't specified, -/ was specified, and the file
33935750Sceastha * we're processing it not a read-write system attribute file,
33945750Sceastha * or we're processing the hidden attribute directory of an
33955750Sceastha * attribute.
33965750Sceastha *
33975750Sceastha * We always process the attributes if we're just generating
33985750Sceastha * generating a table of contents, or if both -@ and -/ were
33995750Sceastha * specified.
34005750Sceastha */
34015750Sceastha if (G_p->g_attrnam_p != NULL) {
34025750Sceastha if (((Args & OCt) == 0) &&
34035750Sceastha ((!Atflag && !SysAtflag) ||
34045750Sceastha (Atflag && !SysAtflag && ((G_p->g_attrparent_p != NULL) ||
34055750Sceastha G_p->g_rw_sysattr)) ||
34065750Sceastha (!Atflag && SysAtflag && ((G_p->g_attrparent_p != NULL) ||
34075750Sceastha !G_p->g_rw_sysattr)))) {
34085750Sceastha proc_file = F_SKIP;
34095750Sceastha data_in(P_SKIP);
34105750Sceastha return;
34115750Sceastha }
34125750Sceastha }
34135750Sceastha
34145750Sceastha /*
34150Sstevel@tonic-gate * Open target directory if this isn't a skipped file
34160Sstevel@tonic-gate * and g_nlink == 1
34170Sstevel@tonic-gate *
34180Sstevel@tonic-gate * Links are handled further down in this function.
34190Sstevel@tonic-gate */
34200Sstevel@tonic-gate
34210Sstevel@tonic-gate proc_file = ckname(0);
34220Sstevel@tonic-gate
34230Sstevel@tonic-gate if (proc_file == F_SKIP && G_p->g_nlink == 1) {
34240Sstevel@tonic-gate /*
34250Sstevel@tonic-gate * Normally ckname() prints out the file as a side
34260Sstevel@tonic-gate * effect except for table of contents listing
34270Sstevel@tonic-gate * when its parameter is zero and Onecopy isn't
34280Sstevel@tonic-gate * Zero. Due to this we need to force the name
34290Sstevel@tonic-gate * to be printed here.
34300Sstevel@tonic-gate */
34310Sstevel@tonic-gate if (Onecopy == 1) {
34320Sstevel@tonic-gate VERBOSE((Args & OCt), G_p->g_nam_p);
34330Sstevel@tonic-gate }
34340Sstevel@tonic-gate data_in(P_SKIP);
34350Sstevel@tonic-gate return;
34360Sstevel@tonic-gate }
34370Sstevel@tonic-gate
34380Sstevel@tonic-gate if (proc_file != F_SKIP && open_dirfd() != 0) {
34390Sstevel@tonic-gate data_in(P_SKIP);
34400Sstevel@tonic-gate return;
34410Sstevel@tonic-gate }
34420Sstevel@tonic-gate
34430Sstevel@tonic-gate if (Hdr_type == BAR) {
34440Sstevel@tonic-gate bar_file_in();
34450Sstevel@tonic-gate close_dirfd();
34460Sstevel@tonic-gate return;
34470Sstevel@tonic-gate }
34480Sstevel@tonic-gate
34490Sstevel@tonic-gate /*
34500Sstevel@tonic-gate * For archives in USTAR format, the files are extracted according
34510Sstevel@tonic-gate * to the typeflag.
34520Sstevel@tonic-gate */
34530Sstevel@tonic-gate if (Hdr_type == USTAR || Hdr_type == TAR) {
34540Sstevel@tonic-gate typeflag = Thdr_p->tbuf.t_typeflag;
34550Sstevel@tonic-gate if (G_p->g_nlink == 1) { /* hard link */
34560Sstevel@tonic-gate if (proc_file != F_SKIP) {
34571134Sceastha int i;
34581134Sceastha char lname[NAMSIZ+1];
34591134Sceastha (void) memset(lname, '\0', sizeof (lname));
34601134Sceastha
34611134Sceastha (void) strncpy(lname, Thdr_p->tbuf.t_linkname,
34621134Sceastha NAMSIZ);
34631134Sceastha for (i = 0; i <= NAMSIZ && lname[i] != 0; i++)
34641134Sceastha ;
34651134Sceastha
34661134Sceastha lname[i] = 0;
34671134Sceastha (void) creat_lnk(G_p->g_dirfd,
34681134Sceastha &lname[0], G_p->g_nam_p);
34690Sstevel@tonic-gate }
34700Sstevel@tonic-gate close_dirfd();
34710Sstevel@tonic-gate return;
34720Sstevel@tonic-gate }
34730Sstevel@tonic-gate if (typeflag == '3' || typeflag == '4' || typeflag == '5' ||
34740Sstevel@tonic-gate typeflag == '6') {
34750Sstevel@tonic-gate if (proc_file != F_SKIP &&
34760Sstevel@tonic-gate creat_spec(G_p->g_dirfd) > 0) {
34775750Sceastha VERBOSE((Args & (OCv | OCV)),
34785750Sceastha (G_p->g_attrparent_p == NULL) ?
34795750Sceastha G_p->g_nam_p : G_p->g_attrpath_p);
34800Sstevel@tonic-gate }
34810Sstevel@tonic-gate close_dirfd();
34820Sstevel@tonic-gate return;
34830Sstevel@tonic-gate } else if (Adir || Aspec) {
34840Sstevel@tonic-gate if ((proc_file == F_SKIP) ||
34851134Sceastha (Ofile = openout(G_p->g_dirfd)) < 0) {
34860Sstevel@tonic-gate data_in(P_SKIP);
34870Sstevel@tonic-gate } else {
34880Sstevel@tonic-gate data_in(P_PROC);
34890Sstevel@tonic-gate }
34900Sstevel@tonic-gate close_dirfd();
34910Sstevel@tonic-gate return;
34920Sstevel@tonic-gate }
34930Sstevel@tonic-gate }
34940Sstevel@tonic-gate
34950Sstevel@tonic-gate if (Adir) {
34960Sstevel@tonic-gate if (proc_file != F_SKIP && creat_spec(G_p->g_dirfd) > 0) {
34970Sstevel@tonic-gate VERBOSE((Args & (OCv | OCV)), G_p->g_nam_p);
34980Sstevel@tonic-gate }
34990Sstevel@tonic-gate close_dirfd();
35000Sstevel@tonic-gate if (Onecopy == 1) {
35010Sstevel@tonic-gate VERBOSE((Args & OCt), G_p->g_nam_p);
35020Sstevel@tonic-gate }
35030Sstevel@tonic-gate return;
35040Sstevel@tonic-gate }
35050Sstevel@tonic-gate if (G_p->g_nlink == 1 || (Hdr_type == TAR ||
35060Sstevel@tonic-gate Hdr_type == USTAR)) {
35070Sstevel@tonic-gate if (Aspec) {
35080Sstevel@tonic-gate if (proc_file != F_SKIP && creat_spec(G_p->g_dirfd) > 0)
35090Sstevel@tonic-gate VERBOSE((Args & (OCv | OCV)), G_p->g_nam_p);
35100Sstevel@tonic-gate } else {
35110Sstevel@tonic-gate if ((proc_file == F_SKIP) ||
35121134Sceastha (Ofile = openout(G_p->g_dirfd)) < 0) {
35130Sstevel@tonic-gate data_in(P_SKIP);
35140Sstevel@tonic-gate } else {
35150Sstevel@tonic-gate data_in(P_PROC);
35160Sstevel@tonic-gate }
35170Sstevel@tonic-gate }
35180Sstevel@tonic-gate close_dirfd();
35190Sstevel@tonic-gate return;
35200Sstevel@tonic-gate }
35210Sstevel@tonic-gate close_dirfd();
35220Sstevel@tonic-gate
35230Sstevel@tonic-gate tl_p = add_lnk(&ttl_p);
35240Sstevel@tonic-gate l_p = ttl_p;
35250Sstevel@tonic-gate if (l_p->L_cnt == l_p->L_gen.g_nlink)
35260Sstevel@tonic-gate cleanup = 1;
35275750Sceastha if (!Onecopy || G_p->g_attrnam_p != NULL) {
35280Sstevel@tonic-gate lnkem = (tl_p != l_p) ? 1 : 0;
35290Sstevel@tonic-gate G_p = &tl_p->L_gen;
35300Sstevel@tonic-gate if (proc_file == F_SKIP) {
35310Sstevel@tonic-gate data_in(P_SKIP);
35320Sstevel@tonic-gate } else {
35330Sstevel@tonic-gate if (open_dirfd() != 0)
35340Sstevel@tonic-gate return;
35350Sstevel@tonic-gate if (!lnkem) {
35360Sstevel@tonic-gate if (Aspec) {
35370Sstevel@tonic-gate if (creat_spec(G_p->g_dirfd) > 0)
35380Sstevel@tonic-gate VERBOSE((Args & (OCv | OCV)),
35390Sstevel@tonic-gate G_p->g_nam_p);
35400Sstevel@tonic-gate } else if ((Ofile =
35411134Sceastha openout(G_p->g_dirfd)) < 0) {
35420Sstevel@tonic-gate data_in(P_SKIP);
35430Sstevel@tonic-gate close_dirfd();
35440Sstevel@tonic-gate reclaim(l_p);
35450Sstevel@tonic-gate } else {
35460Sstevel@tonic-gate data_in(P_PROC);
35470Sstevel@tonic-gate close_dirfd();
35480Sstevel@tonic-gate }
35490Sstevel@tonic-gate } else {
35500Sstevel@tonic-gate /*
35510Sstevel@tonic-gate * Are we linking an attribute?
35520Sstevel@tonic-gate */
35530Sstevel@tonic-gate cwd = -1;
35545750Sceastha if (l_p->L_gen.g_attrnam_p != NULL) {
35550Sstevel@tonic-gate (void) strcpy(Lnkend_p,
35560Sstevel@tonic-gate l_p->L_gen.g_attrnam_p);
35570Sstevel@tonic-gate (void) strcpy(Full_p,
35580Sstevel@tonic-gate tl_p->L_gen.g_attrnam_p);
35590Sstevel@tonic-gate cwd = save_cwd();
35600Sstevel@tonic-gate (void) fchdir(G_p->g_dirfd);
35610Sstevel@tonic-gate } else {
35620Sstevel@tonic-gate (void) strcpy(Lnkend_p,
35630Sstevel@tonic-gate l_p->L_gen.g_nam_p);
35640Sstevel@tonic-gate (void) strcpy(Full_p,
35650Sstevel@tonic-gate tl_p->L_gen.g_nam_p);
35660Sstevel@tonic-gate }
35670Sstevel@tonic-gate (void) creat_lnk(G_p->g_dirfd,
35680Sstevel@tonic-gate Lnkend_p, Full_p);
35690Sstevel@tonic-gate data_in(P_SKIP);
35700Sstevel@tonic-gate close_dirfd();
35715750Sceastha l_p->L_lnk_p = NULL;
35720Sstevel@tonic-gate free(tl_p->L_gen.g_nam_p);
35730Sstevel@tonic-gate free(tl_p);
35740Sstevel@tonic-gate if (cwd != -1)
35750Sstevel@tonic-gate rest_cwd(cwd);
35760Sstevel@tonic-gate }
35770Sstevel@tonic-gate }
35780Sstevel@tonic-gate } else { /* Onecopy */
35790Sstevel@tonic-gate if (tl_p->L_gen.g_filesz)
35800Sstevel@tonic-gate cleanup = 1;
35810Sstevel@tonic-gate if (!cleanup) {
35820Sstevel@tonic-gate close_dirfd();
35830Sstevel@tonic-gate return; /* don't do anything yet */
35840Sstevel@tonic-gate }
35850Sstevel@tonic-gate tl_p = l_p;
35860Sstevel@tonic-gate /*
35870Sstevel@tonic-gate * ckname will clear aclchar. We need to keep aclchar for
35880Sstevel@tonic-gate * all links.
35890Sstevel@tonic-gate */
35900Sstevel@tonic-gate savacl = aclchar;
35915750Sceastha while (tl_p != NULL) {
35920Sstevel@tonic-gate G_p = &tl_p->L_gen;
35930Sstevel@tonic-gate aclchar = savacl;
35940Sstevel@tonic-gate if ((proc_file = ckname(1)) != F_SKIP) {
35950Sstevel@tonic-gate if (open_dirfd() != 0) {
35960Sstevel@tonic-gate return;
35970Sstevel@tonic-gate }
35980Sstevel@tonic-gate if (l_p->L_data) {
35990Sstevel@tonic-gate (void) creat_lnk(G_p->g_dirfd,
36000Sstevel@tonic-gate l_p->L_gen.g_nam_p,
36010Sstevel@tonic-gate G_p->g_nam_p);
36020Sstevel@tonic-gate } else if (Aspec) {
36030Sstevel@tonic-gate (void) creat_spec(G_p->g_dirfd);
36040Sstevel@tonic-gate l_p->L_data = 1;
36050Sstevel@tonic-gate VERBOSE((Args & (OCv | OCV)),
36060Sstevel@tonic-gate G_p->g_nam_p);
36070Sstevel@tonic-gate } else if ((Ofile =
36080Sstevel@tonic-gate openout(G_p->g_dirfd)) < 0) {
36090Sstevel@tonic-gate proc_file = F_SKIP;
36100Sstevel@tonic-gate } else {
36110Sstevel@tonic-gate data_in(P_PROC);
36120Sstevel@tonic-gate l_p->L_data = 1;
36130Sstevel@tonic-gate }
36140Sstevel@tonic-gate } /* (proc_file = ckname(1)) != F_SKIP */
36150Sstevel@tonic-gate
36160Sstevel@tonic-gate tl_p = tl_p->L_lnk_p;
36170Sstevel@tonic-gate
36180Sstevel@tonic-gate close_dirfd();
36190Sstevel@tonic-gate
36200Sstevel@tonic-gate if (proc_file == F_SKIP && !cleanup) {
36210Sstevel@tonic-gate tl_p->L_nxt_p = l_p->L_nxt_p;
36220Sstevel@tonic-gate tl_p->L_bck_p = l_p->L_bck_p;
36230Sstevel@tonic-gate l_p->L_bck_p->L_nxt_p = tl_p;
36240Sstevel@tonic-gate l_p->L_nxt_p->L_bck_p = tl_p;
36250Sstevel@tonic-gate free(l_p->L_gen.g_nam_p);
36260Sstevel@tonic-gate free(l_p);
36270Sstevel@tonic-gate }
36285750Sceastha } /* tl_p->L_lnk_p != NULL */
36290Sstevel@tonic-gate if (l_p->L_data == 0) {
36300Sstevel@tonic-gate data_in(P_SKIP);
36310Sstevel@tonic-gate }
36320Sstevel@tonic-gate }
36330Sstevel@tonic-gate if (cleanup) {
36340Sstevel@tonic-gate reclaim(l_p);
36350Sstevel@tonic-gate }
36360Sstevel@tonic-gate }
36370Sstevel@tonic-gate
36380Sstevel@tonic-gate /*
36390Sstevel@tonic-gate * file_out: If the current file is not a special file (!Aspec) and it
36400Sstevel@tonic-gate * is identical to the archive, skip it (do not archive the archive if it
36410Sstevel@tonic-gate * is a regular file). If creating a TARTYP (TAR or USTAR) archive, the first
36420Sstevel@tonic-gate * time a link to a file is encountered, write the header and file out normally.
36430Sstevel@tonic-gate * Subsequent links to this file put this file name in their t_linkname field.
36440Sstevel@tonic-gate * Otherwise, links are handled in one of two ways, for the old headers
36450Sstevel@tonic-gate * (i.e. binary and -c), linked files are written out as they are encountered.
36460Sstevel@tonic-gate * For the new headers (ASC and CRC), links are saved up until all the links
36470Sstevel@tonic-gate * to each file are found. For a file with n links, write n - 1 headers with
36480Sstevel@tonic-gate * g_filesz set to 0, write the final (nth) header with the correct g_filesz
36490Sstevel@tonic-gate * value and write the data for the file to the archive.
36500Sstevel@tonic-gate */
36510Sstevel@tonic-gate static
36520Sstevel@tonic-gate int
file_out(void)36530Sstevel@tonic-gate file_out(void)
36540Sstevel@tonic-gate {
36550Sstevel@tonic-gate struct Lnk *l_p, *tl_p;
36560Sstevel@tonic-gate int cleanup = 0;
36570Sstevel@tonic-gate struct Lnk *ttl_p;
36580Sstevel@tonic-gate
36590Sstevel@tonic-gate G_p = &Gen;
36600Sstevel@tonic-gate if (!Aspec && IDENT(SrcSt, ArchSt))
36610Sstevel@tonic-gate return (1); /* do not archive the archive if it's a reg file */
366210202SNobutomo.Nakano@Sun.COM /*
366310202SNobutomo.Nakano@Sun.COM * If compressing sparse files, wait until the compressed file size
366410202SNobutomo.Nakano@Sun.COM * is known to check if file size is too big.
366510202SNobutomo.Nakano@Sun.COM */
366610202SNobutomo.Nakano@Sun.COM if (Compress_sparse == 0 && G_p->g_filesz > Max_offset) {
366710202SNobutomo.Nakano@Sun.COM msg(ERR, "%s%s%s: too large to archive in current mode",
36680Sstevel@tonic-gate G_p->g_nam_p,
36695750Sceastha (G_p->g_attrnam_p == NULL) ? "" : G_p->g_rw_sysattr ?
36705750Sceastha gettext(" System Attribute ") : gettext(" Attribute "),
36715750Sceastha (G_p->g_attrnam_p == NULL) ? "" :
36725750Sceastha ((G_p->g_attrparent_p == NULL) ? G_p->g_attrnam_p:
36735750Sceastha G_p->g_attrpath_p));
36740Sstevel@tonic-gate return (1); /* do not archive if it's too big */
36750Sstevel@tonic-gate }
36760Sstevel@tonic-gate if (Hdr_type == TAR || Hdr_type == USTAR) { /* TAR and USTAR */
36770Sstevel@tonic-gate if (Adir) {
36785750Sceastha if (Gen.g_attrnam_p != NULL) {
36790Sstevel@tonic-gate write_xattr_hdr();
36800Sstevel@tonic-gate }
36810Sstevel@tonic-gate write_hdr(ARCHIVE_NORMAL, 0);
36820Sstevel@tonic-gate return (0);
36830Sstevel@tonic-gate }
36840Sstevel@tonic-gate if (G_p->g_nlink == 1) {
36850Sstevel@tonic-gate data_out();
36860Sstevel@tonic-gate return (0);
36870Sstevel@tonic-gate }
36880Sstevel@tonic-gate tl_p = add_lnk(&ttl_p);
36890Sstevel@tonic-gate l_p = ttl_p;
36900Sstevel@tonic-gate if (tl_p == l_p) { /* first link to this file encountered */
36910Sstevel@tonic-gate data_out();
36920Sstevel@tonic-gate return (0);
36930Sstevel@tonic-gate }
36940Sstevel@tonic-gate (void) strncpy(T_lname, l_p->L_gen.g_nam_p,
36950Sstevel@tonic-gate l_p->L_gen.g_namesz);
36960Sstevel@tonic-gate
36970Sstevel@tonic-gate /*
36980Sstevel@tonic-gate * check if linkname is greater than 100 characters
36990Sstevel@tonic-gate */
37000Sstevel@tonic-gate if (strlen(T_lname) > NAMSIZ) {
37010Sstevel@tonic-gate msg(EPOST, "cpio: %s: linkname %s is greater than %d",
37020Sstevel@tonic-gate G_p->g_nam_p, T_lname, NAMSIZ);
37030Sstevel@tonic-gate return (1);
37040Sstevel@tonic-gate }
37050Sstevel@tonic-gate
37060Sstevel@tonic-gate write_hdr(ARCHIVE_NORMAL, (off_t)0);
37070Sstevel@tonic-gate VERBOSE((Args & (OCv | OCV)), tl_p->L_gen.g_nam_p);
37080Sstevel@tonic-gate
37090Sstevel@tonic-gate /* find the lnk entry in sublist, unlink it, and free it */
37100Sstevel@tonic-gate for (; ttl_p->L_lnk_p != NULL;
37110Sstevel@tonic-gate ttl_p = ttl_p->L_lnk_p) {
37120Sstevel@tonic-gate if (ttl_p->L_lnk_p == tl_p) {
37130Sstevel@tonic-gate ttl_p->L_lnk_p = tl_p->L_lnk_p;
37140Sstevel@tonic-gate free(tl_p->L_gen.g_nam_p);
37150Sstevel@tonic-gate free(tl_p);
37160Sstevel@tonic-gate break;
37170Sstevel@tonic-gate }
37180Sstevel@tonic-gate }
37190Sstevel@tonic-gate
37200Sstevel@tonic-gate return (0);
37210Sstevel@tonic-gate }
37220Sstevel@tonic-gate if (Adir) {
37230Sstevel@tonic-gate /*
37240Sstevel@tonic-gate * ACL has been retrieved in getname().
37250Sstevel@tonic-gate */
37260Sstevel@tonic-gate if (Pflag) {
37270Sstevel@tonic-gate char *secinfo = NULL;
37280Sstevel@tonic-gate int len = 0;
37290Sstevel@tonic-gate
37300Sstevel@tonic-gate /* append security attributes */
3731789Sahrens if ((append_secattr(&secinfo, &len, aclp)) == -1)
37320Sstevel@tonic-gate msg(ERR, "can create security information");
37330Sstevel@tonic-gate
37340Sstevel@tonic-gate /* call append_secattr() if more than one */
37350Sstevel@tonic-gate
37360Sstevel@tonic-gate if (len > 0) {
37370Sstevel@tonic-gate /* write ancillary */
373810202SNobutomo.Nakano@Sun.COM write_hdr(ARCHIVE_ACL, (off_t)len);
373910202SNobutomo.Nakano@Sun.COM write_ancillary(secinfo, len, B_TRUE);
37400Sstevel@tonic-gate }
37410Sstevel@tonic-gate }
37420Sstevel@tonic-gate
37435750Sceastha if (Gen.g_attrnam_p != NULL) {
37440Sstevel@tonic-gate write_xattr_hdr();
37450Sstevel@tonic-gate }
37460Sstevel@tonic-gate write_hdr(ARCHIVE_NORMAL, (off_t)0);
37470Sstevel@tonic-gate VERBOSE((Args & (OCv | OCV)), G_p->g_nam_p);
37480Sstevel@tonic-gate return (0);
37490Sstevel@tonic-gate }
37500Sstevel@tonic-gate if (G_p->g_nlink == 1) {
37510Sstevel@tonic-gate data_out();
37520Sstevel@tonic-gate return (0);
37530Sstevel@tonic-gate } else {
37540Sstevel@tonic-gate tl_p = add_lnk(&ttl_p);
37550Sstevel@tonic-gate l_p = ttl_p;
37560Sstevel@tonic-gate
37570Sstevel@tonic-gate if (l_p->L_cnt == l_p->L_gen.g_nlink)
37580Sstevel@tonic-gate cleanup = 1;
37595750Sceastha else if (Onecopy && G_p->g_attrnam_p == NULL) {
37600Sstevel@tonic-gate return (0); /* don't process data yet */
37610Sstevel@tonic-gate }
37620Sstevel@tonic-gate }
37635750Sceastha if (Onecopy && G_p->g_attrnam_p == NULL) {
37640Sstevel@tonic-gate tl_p = l_p;
37655750Sceastha while (tl_p->L_lnk_p != NULL) {
37660Sstevel@tonic-gate G_p = &tl_p->L_gen;
37670Sstevel@tonic-gate G_p->g_filesz = (off_t)0;
37680Sstevel@tonic-gate /* one link with the acl is sufficient */
37690Sstevel@tonic-gate write_hdr(ARCHIVE_NORMAL, (off_t)0);
37700Sstevel@tonic-gate VERBOSE((Args & (OCv | OCV)), G_p->g_nam_p);
37710Sstevel@tonic-gate tl_p = tl_p->L_lnk_p;
37720Sstevel@tonic-gate }
37730Sstevel@tonic-gate G_p = &tl_p->L_gen;
37740Sstevel@tonic-gate if (open_dirfd() != 0)
37750Sstevel@tonic-gate return (1);
37760Sstevel@tonic-gate }
37770Sstevel@tonic-gate /* old style: has acl and data for every link */
37780Sstevel@tonic-gate data_out();
37790Sstevel@tonic-gate if (cleanup)
37800Sstevel@tonic-gate reclaim(l_p);
37810Sstevel@tonic-gate return (0);
37820Sstevel@tonic-gate }
37830Sstevel@tonic-gate
37840Sstevel@tonic-gate /*
37855750Sceastha * Verify the underlying file system supports the attribute type.
37865750Sceastha * Only archive extended attribute files when '-@' was specified.
37875750Sceastha * Only archive system extended attribute files if '-/' was specified.
37885750Sceastha */
37895750Sceastha #if defined(O_XATTR)
37905750Sceastha static attr_status_t
verify_attr_support(char * filename,int attrflg,arc_action_t actflag,int * ext_attrflg)37915750Sceastha verify_attr_support(char *filename, int attrflg, arc_action_t actflag,
37925750Sceastha int *ext_attrflg)
37935750Sceastha {
37945750Sceastha /*
37955750Sceastha * Verify extended attributes are supported/exist. We only
37965750Sceastha * need to check if we are processing a base file, not an
37975750Sceastha * extended attribute.
37985750Sceastha */
37995750Sceastha if (attrflg) {
38005750Sceastha *ext_attrflg = (pathconf(filename, (actflag == ARC_CREATE) ?
38015750Sceastha _PC_XATTR_EXISTS : _PC_XATTR_ENABLED) == 1);
38025750Sceastha }
38035750Sceastha if (Atflag) {
38045750Sceastha #if defined(_PC_SATTR_ENABLED)
38055750Sceastha if (!*ext_attrflg) {
38065750Sceastha if (SysAtflag) {
38075750Sceastha /* Verify system attributes are supported */
38085750Sceastha if (sysattr_support(filename,
38095750Sceastha (actflag == ARC_CREATE) ?_PC_SATTR_EXISTS :
38105750Sceastha _PC_SATTR_ENABLED) != 1) {
38115750Sceastha return (ATTR_SATTR_ERR);
38125750Sceastha }
38135750Sceastha } else
38145750Sceastha return (ATTR_XATTR_ERR);
38155750Sceastha #else
38165750Sceastha return (ATTR_XATTR_ERR);
38175750Sceastha #endif /* _PC_SATTR_ENABLED */
38185750Sceastha }
38195750Sceastha
38205750Sceastha #if defined(_PC_SATTR_ENABLED)
38215750Sceastha } else if (SysAtflag) {
38225750Sceastha /* Verify system attributes are supported */
38235750Sceastha if (sysattr_support(filename, (actflag == ARC_CREATE) ?
38245750Sceastha _PC_SATTR_EXISTS : _PC_SATTR_ENABLED) != 1) {
38255750Sceastha return (ATTR_SATTR_ERR);
38265750Sceastha }
38275750Sceastha #endif /* _PC_SATTR_ENABLED */
38285750Sceastha } else {
38295750Sceastha return (ATTR_SKIP);
38305750Sceastha }
38315750Sceastha
38325750Sceastha return (ATTR_OK);
38335750Sceastha }
38345750Sceastha #endif
38355750Sceastha
38365750Sceastha #if defined(O_XATTR)
38375750Sceastha /*
38385750Sceastha * Verify the attribute, attrname, is an attribute we want to restore.
38395750Sceastha * Never restore read-only system attribute files. Only restore read-write
38405750Sceastha * system attributes files when -/ was specified, and only traverse into
38415750Sceastha * the 2nd level attribute directory containing only system attributes if
38425750Sceastha * -@ was specified. This keeps us from archiving
38435750Sceastha * <attribute name>/<read-write system attribute file>
38445750Sceastha * when -/ was specified without -@.
38455750Sceastha *
38465750Sceastha * attrname - attribute file name
38475750Sceastha * attrparent - attribute's parent name within the base file's
38485750Sceastha * attribute digrectory hierarchy
38495750Sceastha * arc_rwsysattr - flag that indicates that read-write system attribute
38505750Sceastha * file should be archived as it contains other than
38515750Sceastha * the default system attributes.
38525750Sceastha * rw_sysattr - on return, flag will indicate if attrname is a
38535750Sceastha * read-write system attribute file.
38545750Sceastha */
38555750Sceastha static attr_status_t
verify_attr(char * attrname,char * attrparent,int arc_rwsysattr,int * rw_sysattr)38565750Sceastha verify_attr(char *attrname, char *attrparent, int arc_rwsysattr,
38575750Sceastha int *rw_sysattr)
38585750Sceastha {
38595750Sceastha #if defined(_PC_SATTR_ENABLED)
38605750Sceastha int attr_supported;
38615750Sceastha
38625750Sceastha /* Never restore read-only system attribute files */
38635750Sceastha if ((attr_supported = sysattr_type(attrname)) == _RO_SATTR) {
38645750Sceastha *rw_sysattr = 0;
38655750Sceastha return (ATTR_SKIP);
38665750Sceastha } else {
38675750Sceastha *rw_sysattr = (attr_supported == _RW_SATTR);
38685750Sceastha }
38695750Sceastha
38705750Sceastha /*
38715750Sceastha * Don't archive a read-write system attribute file if
38725750Sceastha * it contains only the default system attributes.
38735750Sceastha */
38745750Sceastha if (*rw_sysattr && !arc_rwsysattr) {
38755750Sceastha return (ATTR_SKIP);
38765750Sceastha }
38775750Sceastha
38785750Sceastha #else
38795750Sceastha /* Never restore read-only system attribute files */
38805750Sceastha if ((*rw_sysattr = is_sysattr(attrname)) == 1) {
38815750Sceastha return (ATTR_SKIP);
38825750Sceastha }
38835750Sceastha #endif /* _PC_SATTR_ENABLED */
38845750Sceastha
38855750Sceastha /*
38865750Sceastha * Only restore read-write system attribute files
38875750Sceastha * when -/ was specified. Only restore extended
38885750Sceastha * attributes when -@ was specified.
38895750Sceastha */
38905750Sceastha if (Atflag) {
38915750Sceastha if (!SysAtflag) {
38925750Sceastha /*
38935750Sceastha * Only archive/restore the hidden directory "." if
38945750Sceastha * we're processing the top level hidden attribute
38955750Sceastha * directory. We don't want to process the
38965750Sceastha * hidden attribute directory of the attribute
38975750Sceastha * directory that contains only extended system
38985750Sceastha * attributes.
38995750Sceastha */
39005750Sceastha if (*rw_sysattr || (Hiddendir &&
39015750Sceastha (attrparent != NULL))) {
39025750Sceastha return (ATTR_SKIP);
39035750Sceastha }
39045750Sceastha }
39055750Sceastha } else if (SysAtflag) {
39065750Sceastha /*
39075750Sceastha * Only archive/restore read-write extended system attribute
39085750Sceastha * files of the base file.
39095750Sceastha */
39105750Sceastha if (!*rw_sysattr || (attrparent != NULL)) {
39115750Sceastha return (ATTR_SKIP);
39125750Sceastha }
39135750Sceastha } else {
39145750Sceastha return (ATTR_SKIP);
39155750Sceastha }
39165750Sceastha
39175750Sceastha return (ATTR_OK);
39185750Sceastha }
39195750Sceastha #endif
39205750Sceastha
39215750Sceastha #if defined(O_XATTR)
39225750Sceastha static int
retry_open_attr(int pdirfd,int cwd,char * fullname,char * pattr,char * name,int oflag,mode_t mode)39235750Sceastha retry_open_attr(int pdirfd, int cwd, char *fullname, char *pattr, char *name,
39245750Sceastha int oflag, mode_t mode)
39255750Sceastha {
39265750Sceastha int dirfd;
39275750Sceastha int ofilefd = -1;
39285750Sceastha struct timeval times[2];
39295750Sceastha mode_t newmode;
39305750Sceastha struct stat parentstat;
39315750Sceastha acl_t *aclp = NULL;
39325750Sceastha int error;
39335750Sceastha
39345750Sceastha /*
39355750Sceastha * We couldn't get to attrdir. See if its
39365750Sceastha * just a mode problem on the parent file.
39375750Sceastha * for example: a mode such as r-xr--r--
39385750Sceastha * on a ufs file system without extended
39395750Sceastha * system attribute support won't let us
39405750Sceastha * create an attribute dir if it doesn't
39415750Sceastha * already exist, and on a ufs file system
39425750Sceastha * with extended system attribute support
39435750Sceastha * won't let us open the attribute for
39445750Sceastha * write.
39455750Sceastha *
39465750Sceastha * If file has a non-trivial ACL, then save it
39475750Sceastha * off so that we can place it back on after doing
39485750Sceastha * chmod's.
39495750Sceastha */
39505750Sceastha if ((dirfd = openat(cwd, (pattr == NULL) ? fullname : pattr,
39515750Sceastha O_RDONLY)) == -1) {
39525750Sceastha return (-1);
39535750Sceastha }
39545750Sceastha if (fstat(dirfd, &parentstat) == -1) {
39555750Sceastha msg(ERRN, "Cannot stat %sfile %s",
39565750Sceastha (pdirfd == -1) ? "" : gettext("parent of "),
39575750Sceastha (pdirfd == -1) ? fullname : name);
39585750Sceastha (void) close(dirfd);
39595750Sceastha return (-1);
39605750Sceastha }
39615750Sceastha if ((error = facl_get(dirfd, ACL_NO_TRIVIAL, &aclp)) != 0) {
39625750Sceastha msg(ERRN, "Failed to retrieve ACL on %sfile %s",
39635750Sceastha (pdirfd == -1) ? "" : gettext("parent of "),
39645750Sceastha (pdirfd == -1) ? fullname : name);
39655750Sceastha (void) close(dirfd);
39665750Sceastha return (-1);
39675750Sceastha }
39685750Sceastha
39695750Sceastha newmode = S_IWUSR | parentstat.st_mode;
39705750Sceastha if (fchmod(dirfd, newmode) == -1) {
39715750Sceastha msg(ERRN, "Cannot change mode of %sfile %s to %o",
39725750Sceastha (pdirfd == -1) ? "" : gettext("parent of "),
39735750Sceastha (pdirfd == -1) ? fullname : name, newmode);
39745750Sceastha if (aclp)
39755750Sceastha acl_free(aclp);
39765750Sceastha (void) close(dirfd);
39775750Sceastha return (-1);
39785750Sceastha }
39795750Sceastha
39805750Sceastha
39815750Sceastha if (pdirfd == -1) {
39825750Sceastha /*
39835750Sceastha * We weren't able to create the attribute directory before.
39845750Sceastha * Now try again.
39855750Sceastha */
39865750Sceastha ofilefd = attropen(fullname, ".", oflag);
39875750Sceastha } else {
39885750Sceastha /*
39895750Sceastha * We weren't able to create open the attribute before.
39905750Sceastha * Now try again.
39915750Sceastha */
39925750Sceastha ofilefd = openat(pdirfd, name, oflag, mode);
39935750Sceastha }
39945750Sceastha
39955750Sceastha /*
39965750Sceastha * Put mode back to original
39975750Sceastha */
39985750Sceastha if (fchmod(dirfd, parentstat.st_mode) == -1) {
39995750Sceastha msg(ERRN, "Cannot restore permissions of %sfile %s to %o",
40005750Sceastha (pdirfd == -1) ? "" : gettext("parent of "),
40015750Sceastha (pdirfd == -1) ? fullname : name, newmode);
40025750Sceastha }
40035750Sceastha
40045750Sceastha if (aclp) {
40055750Sceastha error = facl_set(dirfd, aclp);
40065750Sceastha if (error) {
40075750Sceastha msg(ERRN, "failed to set acl entries on %sfile %s\n",
40085750Sceastha (pdirfd == -1) ? "" : gettext("parent of "),
40095750Sceastha (pdirfd == -1) ? fullname : name);
40105750Sceastha }
40115750Sceastha acl_free(aclp);
40125750Sceastha }
40135750Sceastha
40145750Sceastha /*
40155750Sceastha * Put back time stamps
40165750Sceastha */
40175750Sceastha
40185750Sceastha times[0].tv_sec = parentstat.st_atime;
40195750Sceastha times[0].tv_usec = 0;
40205750Sceastha times[1].tv_sec = parentstat.st_mtime;
40215750Sceastha times[1].tv_usec = 0;
40225750Sceastha
40235750Sceastha (void) futimesat(cwd, (pattr == NULL) ? fullname : pattr, times);
40245750Sceastha
40255750Sceastha (void) close(dirfd);
40265750Sceastha
40275750Sceastha return (ofilefd);
40285750Sceastha }
40295750Sceastha #endif
40305750Sceastha
40315750Sceastha #if defined(O_XATTR)
40325750Sceastha /*
40335750Sceastha * Recursively open attribute directories until the attribute directory
40345750Sceastha * containing the specified attribute, attrname, is opened.
40355750Sceastha *
40365750Sceastha * Currently, only 2 directory levels of attributes are supported, (i.e.,
40375750Sceastha * extended system attributes on extended attributes). The following are
40385750Sceastha * the possible input combinations:
40395750Sceastha * 1. Open the attribute directory of the base file (don't change
40405750Sceastha * into it).
40415750Sceastha * attr_parent = NULL
40425750Sceastha * attrname = '.'
40435750Sceastha * 2. Open the attribute directory of the base file and change into it.
40445750Sceastha * attr_parent = NULL
40455750Sceastha * attrname = <attr> | <sys_attr>
40465750Sceastha * 3. Open the attribute directory of the base file, change into it,
40475750Sceastha * then recursively call open_attr_dir() to open the attribute's
40485750Sceastha * parent directory (don't change into it).
40495750Sceastha * attr_parent = <attr>
40505750Sceastha * attrname = '.'
40515750Sceastha * 4. Open the attribute directory of the base file, change into it,
40525750Sceastha * then recursively call open_attr_dir() to open the attribute's
40535750Sceastha * parent directory and change into it.
40545750Sceastha * attr_parent = <attr>
40555750Sceastha * attrname = <attr> | <sys_attr>
40565750Sceastha *
40575750Sceastha * An attribute directory will be opened only if the underlying file system
40585750Sceastha * supports the attribute type, and if the command line specifications
40595750Sceastha * (f_extended_attr and f_sys_attr) enable the processing of the attribute
40605750Sceastha * type.
40615750Sceastha *
40625750Sceastha * On succesful return, attr_parentfd will be the file descriptor of the
40635750Sceastha * opened attribute directory. In addition, if the attribute is a read-write
40645750Sceastha * extended system attribute, rw_sysattr will be set to 1, otherwise
40655750Sceastha * it will be set to 0.
40665750Sceastha *
40675750Sceastha * Possible return values:
40685750Sceastha * ATTR_OK Successfully opened and, if needed, changed into the
40695750Sceastha * attribute directory containing attrname.
40705750Sceastha * ATTR_SKIP The command line specifications don't enable the
40715750Sceastha * processing of the attribute type.
40725750Sceastha * ATTR_CHDIR_ERR An error occurred while trying to change into an
40735750Sceastha * attribute directory.
40745750Sceastha * ATTR_OPEN_ERR An error occurred while trying to open an
40755750Sceastha * attribute directory.
40765750Sceastha * ATTR_XATTR_ERR The underlying file system doesn't support extended
40775750Sceastha * attributes.
40785750Sceastha * ATTR_SATTR_ERR The underlying file system doesn't support extended
40795750Sceastha * system attributes.
40805750Sceastha */
40815750Sceastha static int
open_attr_dir(char * attrname,char * dirp,int cwd,char * attr_parent,int * attr_parentfd,int * rw_sysattr)40825750Sceastha open_attr_dir(char *attrname, char *dirp, int cwd, char *attr_parent,
40835750Sceastha int *attr_parentfd, int *rw_sysattr)
40845750Sceastha {
40855750Sceastha attr_status_t rc;
40865750Sceastha int firsttime = (*attr_parentfd == -1);
40875750Sceastha int saveerrno;
40885750Sceastha int ext_attr;
40895750Sceastha
40905750Sceastha /*
40915750Sceastha * open_attr_dir() was recursively called (input combination number 4),
40925750Sceastha * close the previously opened file descriptor as we've already changed
40935750Sceastha * into it.
40945750Sceastha */
40955750Sceastha if (!firsttime) {
40965750Sceastha (void) close(*attr_parentfd);
40975750Sceastha *attr_parentfd = -1;
40985750Sceastha }
40995750Sceastha
41005750Sceastha /*
41015750Sceastha * Verify that the underlying file system supports the restoration
41025750Sceastha * of the attribute.
41035750Sceastha */
41045750Sceastha if ((rc = verify_attr_support(dirp, firsttime, ARC_RESTORE,
41055750Sceastha &ext_attr)) != ATTR_OK) {
41065750Sceastha return (rc);
41075750Sceastha }
41085750Sceastha
41095750Sceastha /* Open the base file's attribute directory */
41105750Sceastha if ((*attr_parentfd = attropen(dirp, ".", O_RDONLY)) == -1) {
41115750Sceastha /*
41125750Sceastha * Save the errno from the attropen so it can be reported
41135750Sceastha * if the retry of the attropen fails.
41145750Sceastha */
41155750Sceastha saveerrno = errno;
41165750Sceastha if ((*attr_parentfd = retry_open_attr(-1, cwd, dirp,
41175750Sceastha NULL, ".", O_RDONLY, 0)) == -1) {
41185750Sceastha (void) close(*attr_parentfd);
41195750Sceastha *attr_parentfd = -1;
41205750Sceastha errno = saveerrno;
41215750Sceastha return (ATTR_OPEN_ERR);
41225750Sceastha }
41235750Sceastha }
41245750Sceastha
41255750Sceastha /*
41265750Sceastha * Change into the parent attribute's directory unless we are
41275750Sceastha * processing the hidden attribute directory of the base file itself.
41285750Sceastha */
41295750Sceastha if ((Hiddendir == 0) || (firsttime && (attr_parent != NULL))) {
41305750Sceastha if (fchdir(*attr_parentfd) != 0) {
41315750Sceastha saveerrno = errno;
41325750Sceastha (void) close(*attr_parentfd);
41335750Sceastha *attr_parentfd = -1;
41345750Sceastha errno = saveerrno;
41355750Sceastha return (ATTR_CHDIR_ERR);
41365750Sceastha }
41375750Sceastha }
41385750Sceastha
41395750Sceastha /* Determine if the attribute should be processed */
41405750Sceastha if ((rc = verify_attr(attrname, attr_parent, 1,
41415750Sceastha rw_sysattr)) != ATTR_OK) {
41425750Sceastha saveerrno = errno;
41435750Sceastha (void) close(*attr_parentfd);
41445750Sceastha *attr_parentfd = -1;
41455750Sceastha errno = saveerrno;
41465750Sceastha return (rc);
41475750Sceastha }
41485750Sceastha
41495750Sceastha /*
41505750Sceastha * If the attribute is an extended system attribute of an attribute
41515750Sceastha * (i.e., <attr>/<sys_attr>), then recursively call open_attr_dir() to
41525750Sceastha * open the attribute directory of the parent attribute.
41535750Sceastha */
41545750Sceastha if (firsttime && (attr_parent != NULL)) {
41555750Sceastha return (open_attr_dir(attrname, attr_parent, *attr_parentfd,
41565750Sceastha attr_parent, attr_parentfd, rw_sysattr));
41575750Sceastha }
41585750Sceastha
41595750Sceastha return (ATTR_OK);
41605750Sceastha }
41615750Sceastha #endif
41625750Sceastha
41635750Sceastha /*
41640Sstevel@tonic-gate * file_pass: If the -l option is set (link files when possible), and the
41650Sstevel@tonic-gate * source and destination file systems are the same, link the source file
41660Sstevel@tonic-gate * (G_p->g_nam_p) to the destination file (Fullnam) and return. If not a
41670Sstevel@tonic-gate * linked file, transfer the data. Otherwise, the first link to a file
41680Sstevel@tonic-gate * encountered is transferred normally and subsequent links are linked to it.
41690Sstevel@tonic-gate */
41700Sstevel@tonic-gate
41710Sstevel@tonic-gate static int
file_pass(void)41720Sstevel@tonic-gate file_pass(void)
41730Sstevel@tonic-gate {
41740Sstevel@tonic-gate struct Lnk *l_p, *tl_p;
41750Sstevel@tonic-gate struct Lnk *ttl_p;
41760Sstevel@tonic-gate char *save_name;
41770Sstevel@tonic-gate int size;
41780Sstevel@tonic-gate int cwd;
41790Sstevel@tonic-gate char *lfrom, *lto;
41800Sstevel@tonic-gate
41810Sstevel@tonic-gate G_p = &Gen;
41820Sstevel@tonic-gate
41830Sstevel@tonic-gate if (Adir && !(Args & OCd)) {
41840Sstevel@tonic-gate msg(ERR, "Use -d option to copy \"%s\"", G_p->g_nam_p);
41850Sstevel@tonic-gate return (FILE_PASS_ERR);
41860Sstevel@tonic-gate }
41870Sstevel@tonic-gate
41880Sstevel@tonic-gate save_name = G_p->g_nam_p;
41890Sstevel@tonic-gate
41900Sstevel@tonic-gate while (*(G_p->g_nam_p) == '/') {
41910Sstevel@tonic-gate G_p->g_nam_p++;
41920Sstevel@tonic-gate }
41930Sstevel@tonic-gate
41945750Sceastha (void) strcpy(Full_p, (G_p->g_attrfnam_p == NULL) ?
41950Sstevel@tonic-gate G_p->g_nam_p : G_p->g_attrfnam_p);
41960Sstevel@tonic-gate
41975750Sceastha if (G_p->g_attrnam_p == NULL) {
41980Sstevel@tonic-gate G_p->g_passdirfd = open_dir(Fullnam_p);
41990Sstevel@tonic-gate
42000Sstevel@tonic-gate if (G_p->g_passdirfd == -1) {
42010Sstevel@tonic-gate msg(ERRN,
42020Sstevel@tonic-gate "Cannot open/create \"%s\"", Fullnam_p);
42030Sstevel@tonic-gate return (FILE_PASS_ERR);
42040Sstevel@tonic-gate }
42050Sstevel@tonic-gate } else {
42065750Sceastha int rw_sysattr;
42075750Sceastha
42085750Sceastha /*
42095750Sceastha * Open the file's attribute directory.
42105750Sceastha * Change into the base file's starting directory then call
42115750Sceastha * open_attr_dir() to open the attribute directory of either
42125750Sceastha * the base file (if G_p->g_attrparent_p is NULL) or the
42135750Sceastha * attribute (if G_p->g_attrparent_p is set) of the base file.
42145750Sceastha */
42155750Sceastha
42165750Sceastha G_p->g_passdirfd = -1;
42175750Sceastha (void) fchdir(G_p->g_baseparent_fd);
42185750Sceastha (void) open_attr_dir(G_p->g_attrnam_p, Fullnam_p,
42195750Sceastha G_p->g_baseparent_fd, (G_p->g_attrparent_p == NULL) ? NULL :
42205750Sceastha G_p->g_attrparent_p, &G_p->g_passdirfd, &rw_sysattr);
42210Sstevel@tonic-gate if (G_p->g_passdirfd == -1) {
42225750Sceastha msg(ERRN,
42235750Sceastha "Cannot open attribute directory of "
42245750Sceastha "%s%s%sfile \"%s\"",
42255750Sceastha (G_p->g_attrparent_p == NULL) ? "" :
42265750Sceastha gettext("attribute \""),
42275750Sceastha (G_p->g_attrparent_p == NULL) ? "" :
42285750Sceastha G_p->g_attrparent_p,
42295750Sceastha (G_p->g_attrparent_p == NULL) ? "" :
42305750Sceastha gettext("\" of "), Fullnam_p);
42315750Sceastha return (FILE_PASS_ERR);
42320Sstevel@tonic-gate }
42330Sstevel@tonic-gate }
42340Sstevel@tonic-gate
42350Sstevel@tonic-gate if (Args & OCl) {
42360Sstevel@tonic-gate /* We are linking back to the source directory. */
42370Sstevel@tonic-gate
42380Sstevel@tonic-gate if (!Adir) {
42390Sstevel@tonic-gate char *existingfile = save_name;
42400Sstevel@tonic-gate
42410Sstevel@tonic-gate if ((Args & OCL) && issymlink) {
42420Sstevel@tonic-gate /* We are chasing symlinks. */
42430Sstevel@tonic-gate
42440Sstevel@tonic-gate if ((size = readlink(save_name, Symlnk_p,
42450Sstevel@tonic-gate MAXPATHLEN)) < 0) {
42460Sstevel@tonic-gate msg(ERRN,
42470Sstevel@tonic-gate "Cannot read symbolic link \"%s\"",
42480Sstevel@tonic-gate save_name);
42490Sstevel@tonic-gate return (FILE_PASS_ERR);
42500Sstevel@tonic-gate }
42510Sstevel@tonic-gate
42520Sstevel@tonic-gate Symlnk_p[size] = '\0';
42530Sstevel@tonic-gate existingfile = Symlnk_p;
42540Sstevel@tonic-gate }
42550Sstevel@tonic-gate
42565750Sceastha if (G_p->g_attrnam_p == NULL) {
42570Sstevel@tonic-gate if (creat_lnk(G_p->g_passdirfd,
42580Sstevel@tonic-gate existingfile, Fullnam_p) == 0) {
42590Sstevel@tonic-gate return (FILE_LINKED);
42600Sstevel@tonic-gate }
42610Sstevel@tonic-gate }
42620Sstevel@tonic-gate }
42630Sstevel@tonic-gate }
42640Sstevel@tonic-gate
42650Sstevel@tonic-gate if ((G_p->g_mode & Ftype) == S_IFLNK && !(Args & OCL)) {
42660Sstevel@tonic-gate /* The archive file is a symlink. */
42670Sstevel@tonic-gate
42680Sstevel@tonic-gate errno = 0;
42690Sstevel@tonic-gate
42700Sstevel@tonic-gate if ((size = readlink(save_name, Symlnk_p, MAXPATHLEN)) < 0) {
42710Sstevel@tonic-gate msg(ERRN,
42720Sstevel@tonic-gate "Cannot read symbolic link \"%s\"", save_name);
42730Sstevel@tonic-gate return (FILE_PASS_ERR);
42740Sstevel@tonic-gate }
42750Sstevel@tonic-gate
42760Sstevel@tonic-gate errno = 0;
42770Sstevel@tonic-gate (void) missdir(Fullnam_p);
42780Sstevel@tonic-gate *(Symlnk_p + size) = '\0';
42790Sstevel@tonic-gate
42800Sstevel@tonic-gate if (symlink(Symlnk_p, Fullnam_p) < 0) {
42810Sstevel@tonic-gate if (errno == EEXIST) {
42820Sstevel@tonic-gate if (openout(G_p->g_passdirfd) < 0) {
42831134Sceastha if (errno != EEXIST) {
42841134Sceastha msg(ERRN,
42851134Sceastha "Cannot create \"%s\"",
42861134Sceastha Fullnam_p);
42871134Sceastha }
42880Sstevel@tonic-gate return (FILE_PASS_ERR);
42890Sstevel@tonic-gate }
42900Sstevel@tonic-gate } else {
42910Sstevel@tonic-gate msg(ERRN, "Cannot create \"%s\"", Fullnam_p);
42920Sstevel@tonic-gate return (FILE_PASS_ERR);
42930Sstevel@tonic-gate }
42941134Sceastha } else {
42951134Sceastha if (Args & OCR) {
42961134Sceastha if (lchown(Fullnam_p, (int)Rpw_p->pw_uid,
42971134Sceastha (int)Rpw_p->pw_gid) < 0) {
42981134Sceastha msg(ERRN,
42991134Sceastha "Error during chown() of \"%s\"",
43001134Sceastha Fullnam_p);
43011134Sceastha }
43021134Sceastha } else if ((lchown(Fullnam_p, (int)G_p->g_uid,
43031134Sceastha (int)G_p->g_gid) < 0) && privileged) {
43040Sstevel@tonic-gate msg(ERRN,
43050Sstevel@tonic-gate "Error during chown() of \"%s\"",
43060Sstevel@tonic-gate Fullnam_p);
43070Sstevel@tonic-gate }
43080Sstevel@tonic-gate }
43090Sstevel@tonic-gate
43100Sstevel@tonic-gate VERBOSE((Args & (OCv | OCV)), Fullnam_p);
43110Sstevel@tonic-gate return (FILE_PASS_ERR);
43120Sstevel@tonic-gate }
43130Sstevel@tonic-gate
43140Sstevel@tonic-gate if (!Adir && G_p->g_nlink > 1) {
43150Sstevel@tonic-gate /* The archive file has hard links. */
43160Sstevel@tonic-gate
43170Sstevel@tonic-gate tl_p = add_lnk(&ttl_p);
43180Sstevel@tonic-gate l_p = ttl_p;
43190Sstevel@tonic-gate
43200Sstevel@tonic-gate if (tl_p == l_p) {
43210Sstevel@tonic-gate /* The archive file was not found. */
43220Sstevel@tonic-gate
43230Sstevel@tonic-gate G_p = &tl_p->L_gen;
43240Sstevel@tonic-gate } else {
43250Sstevel@tonic-gate /* The archive file was found. */
43260Sstevel@tonic-gate
43270Sstevel@tonic-gate cwd = -1;
43280Sstevel@tonic-gate
43295750Sceastha if (l_p->L_gen.g_attrnam_p != NULL) {
43300Sstevel@tonic-gate /* We are linking an attribute */
43310Sstevel@tonic-gate
43320Sstevel@tonic-gate (void) strcpy(Lnkend_p, l_p->L_gen.g_attrnam_p);
43330Sstevel@tonic-gate cwd = save_cwd();
43340Sstevel@tonic-gate (void) fchdir(G_p->g_passdirfd);
43350Sstevel@tonic-gate lfrom = get_component(Lnknam_p);
43360Sstevel@tonic-gate lto = tl_p->L_gen.g_attrnam_p;
43370Sstevel@tonic-gate } else {
43380Sstevel@tonic-gate /* We are not linking an attribute */
43390Sstevel@tonic-gate
43400Sstevel@tonic-gate (void) strcpy(Lnkend_p, l_p->L_gen.g_nam_p);
43410Sstevel@tonic-gate (void) strcpy(Full_p, tl_p->L_gen.g_nam_p);
43420Sstevel@tonic-gate lfrom = Lnknam_p;
43430Sstevel@tonic-gate lto = Fullnam_p;
43440Sstevel@tonic-gate }
43450Sstevel@tonic-gate
43460Sstevel@tonic-gate (void) creat_lnk(G_p->g_passdirfd, lfrom, lto);
43470Sstevel@tonic-gate
43480Sstevel@tonic-gate if (cwd) {
43490Sstevel@tonic-gate rest_cwd(cwd);
43500Sstevel@tonic-gate }
43510Sstevel@tonic-gate
43525750Sceastha l_p->L_lnk_p = NULL;
43530Sstevel@tonic-gate free(tl_p->L_gen.g_nam_p);
43540Sstevel@tonic-gate free(tl_p);
43550Sstevel@tonic-gate
43560Sstevel@tonic-gate if (l_p->L_cnt == G_p->g_nlink) {
43570Sstevel@tonic-gate reclaim(l_p);
43580Sstevel@tonic-gate }
43590Sstevel@tonic-gate
43600Sstevel@tonic-gate return (FILE_LINKED);
43610Sstevel@tonic-gate }
43620Sstevel@tonic-gate }
43630Sstevel@tonic-gate
43640Sstevel@tonic-gate if (Adir || Aspec) {
43650Sstevel@tonic-gate /*
43660Sstevel@tonic-gate * The archive file is a directory, block special, char
43670Sstevel@tonic-gate * special or a fifo.
43680Sstevel@tonic-gate */
43690Sstevel@tonic-gate
43700Sstevel@tonic-gate if (creat_spec(G_p->g_passdirfd) > 0) {
43710Sstevel@tonic-gate VERBOSE((Args & (OCv | OCV)), Fullnam_p);
43720Sstevel@tonic-gate }
43730Sstevel@tonic-gate } else if ((Ofile = openout(G_p->g_passdirfd)) > 0) {
43740Sstevel@tonic-gate data_pass();
43750Sstevel@tonic-gate }
43760Sstevel@tonic-gate
43770Sstevel@tonic-gate return (FILE_COPIED);
43780Sstevel@tonic-gate }
43790Sstevel@tonic-gate
43800Sstevel@tonic-gate /*
43810Sstevel@tonic-gate * flush_lnks: With new linked file handling, linked files are not archived
43820Sstevel@tonic-gate * until all links have been collected. When the end of the list of filenames
43830Sstevel@tonic-gate * to archive has been reached, all files that did not encounter all their links
43840Sstevel@tonic-gate * are written out with actual (encountered) link counts. A file with n links
43850Sstevel@tonic-gate * (that are archived) will be represented by n headers (one for each link (the
43860Sstevel@tonic-gate * first n - 1 have g_filesz set to 0)) followed by the data for the file.
43870Sstevel@tonic-gate */
43880Sstevel@tonic-gate
43890Sstevel@tonic-gate static void
flush_lnks(void)43900Sstevel@tonic-gate flush_lnks(void)
43910Sstevel@tonic-gate {
43920Sstevel@tonic-gate struct Lnk *l_p, *tl_p;
43930Sstevel@tonic-gate off_t tfsize;
43940Sstevel@tonic-gate
43950Sstevel@tonic-gate l_p = Lnk_hd.L_nxt_p;
43960Sstevel@tonic-gate while (l_p != &Lnk_hd) {
43970Sstevel@tonic-gate (void) strcpy(Gen.g_nam_p, l_p->L_gen.g_nam_p);
43980Sstevel@tonic-gate if (stat(Gen.g_nam_p, &SrcSt) == 0) { /* check if file exists */
43990Sstevel@tonic-gate tl_p = l_p;
44000Sstevel@tonic-gate (void) creat_hdr();
44010Sstevel@tonic-gate Gen.g_nlink = l_p->L_cnt; /* "actual" link count */
44020Sstevel@tonic-gate tfsize = Gen.g_filesz;
44030Sstevel@tonic-gate Gen.g_filesz = (off_t)0;
44040Sstevel@tonic-gate G_p = &Gen;
44055750Sceastha while (tl_p != NULL) {
44060Sstevel@tonic-gate Gen.g_nam_p = tl_p->L_gen.g_nam_p;
44070Sstevel@tonic-gate Gen.g_namesz = tl_p->L_gen.g_namesz;
44085750Sceastha if (tl_p->L_lnk_p == NULL) {
44090Sstevel@tonic-gate Gen.g_filesz = tfsize;
44100Sstevel@tonic-gate if (open_dirfd() != 0) {
44110Sstevel@tonic-gate break;
44120Sstevel@tonic-gate }
44130Sstevel@tonic-gate data_out();
44140Sstevel@tonic-gate break;
44150Sstevel@tonic-gate }
44160Sstevel@tonic-gate write_hdr(ARCHIVE_NORMAL,
44170Sstevel@tonic-gate (off_t)0); /* header only */
44180Sstevel@tonic-gate VERBOSE((Args & (OCv | OCV)), Gen.g_nam_p);
44190Sstevel@tonic-gate tl_p = tl_p->L_lnk_p;
44200Sstevel@tonic-gate }
44210Sstevel@tonic-gate Gen.g_nam_p = Nam_p;
44220Sstevel@tonic-gate } else /* stat(Gen.g_nam_p, &SrcSt) == 0 */
44230Sstevel@tonic-gate msg(ERR, "\"%s%s%s\" has disappeared",
44245750Sceastha (Gen.g_attrnam_p == NULL) ?
44250Sstevel@tonic-gate Gen.g_nam_p : Gen.g_attrfnam_p,
44265750Sceastha (Gen.g_attrnam_p == NULL) ?
44275750Sceastha "" : Gen.g_rw_sysattr ?
44285750Sceastha gettext(" System Attribute ") :
44295750Sceastha gettext(" Attribute "),
44305750Sceastha (Gen.g_attrnam_p == NULL) ?
44310Sstevel@tonic-gate "" : Gen.g_attrnam_p);
44320Sstevel@tonic-gate tl_p = l_p;
44330Sstevel@tonic-gate l_p = l_p->L_nxt_p;
44340Sstevel@tonic-gate reclaim(tl_p);
44350Sstevel@tonic-gate } /* l_p != &Lnk_hd */
44360Sstevel@tonic-gate }
44370Sstevel@tonic-gate
44385331Samw #if defined(O_XATTR)
44395331Samw static int
is_sysattr(char * name)44405331Samw is_sysattr(char *name)
44415331Samw {
44425331Samw return ((strcmp(name, VIEW_READONLY) == 0) ||
44435331Samw (strcmp(name, VIEW_READWRITE) == 0));
44445331Samw }
44455331Samw #endif
44465331Samw
44470Sstevel@tonic-gate /*
44480Sstevel@tonic-gate * gethdr: Get a header from the archive, validate it and check for the trailer.
44490Sstevel@tonic-gate * Any user specified Hdr_type is ignored (set to NONE in main). Hdr_type is
44500Sstevel@tonic-gate * set appropriately after a valid header is found. Unless the -k option is
44510Sstevel@tonic-gate * set a corrupted header causes an exit with an error. I/O errors during
44520Sstevel@tonic-gate * examination of any part of the header cause gethdr to throw away any current
44530Sstevel@tonic-gate * data and start over. Other errors during examination of any part of the
44540Sstevel@tonic-gate * header cause gethdr to advance one byte and continue the examination.
44550Sstevel@tonic-gate */
44560Sstevel@tonic-gate
44570Sstevel@tonic-gate static int
gethdr(void)44580Sstevel@tonic-gate gethdr(void)
44590Sstevel@tonic-gate {
44600Sstevel@tonic-gate ushort_t ftype;
44610Sstevel@tonic-gate int hit = NONE, cnt = 0;
44620Sstevel@tonic-gate int goodhdr, hsize, offset;
44630Sstevel@tonic-gate int bswap = 0;
44640Sstevel@tonic-gate char *preptr;
44650Sstevel@tonic-gate int k = 0;
44660Sstevel@tonic-gate int j;
4467789Sahrens int error;
4468789Sahrens int aclcnt;
44690Sstevel@tonic-gate
44700Sstevel@tonic-gate Gen.g_nam_p = Nam_p;
44710Sstevel@tonic-gate do { /* hit == NONE && (Args & OCk) && Buffr.b_cnt > 0 */
44720Sstevel@tonic-gate FILL(Hdrsz);
44730Sstevel@tonic-gate switch (Hdr_type) {
44740Sstevel@tonic-gate case NONE:
44750Sstevel@tonic-gate case BIN:
44760Sstevel@tonic-gate Binmag.b_byte[0] = Buffr.b_out_p[0];
44770Sstevel@tonic-gate Binmag.b_byte[1] = Buffr.b_out_p[1];
44780Sstevel@tonic-gate if ((Binmag.b_half == CMN_BIN) ||
44790Sstevel@tonic-gate (Binmag.b_half == CMN_BBS)) {
44800Sstevel@tonic-gate hit = read_hdr(BIN);
44810Sstevel@tonic-gate if (Hdr_type == NONE)
44820Sstevel@tonic-gate bswap = 1;
44830Sstevel@tonic-gate hsize = HDRSZ + Gen.g_namesz;
44840Sstevel@tonic-gate break;
44850Sstevel@tonic-gate }
44860Sstevel@tonic-gate if (Hdr_type != NONE)
44870Sstevel@tonic-gate break;
44880Sstevel@tonic-gate /*FALLTHROUGH*/
44890Sstevel@tonic-gate case CHR:
44900Sstevel@tonic-gate if (!(strncmp(Buffr.b_out_p, CMS_CHR, CMS_LEN))) {
44910Sstevel@tonic-gate hit = read_hdr(CHR);
44920Sstevel@tonic-gate hsize = CHRSZ + Gen.g_namesz;
44930Sstevel@tonic-gate break;
44940Sstevel@tonic-gate }
44950Sstevel@tonic-gate if (Hdr_type != NONE)
44960Sstevel@tonic-gate break;
44970Sstevel@tonic-gate /*FALLTHROUGH*/
44980Sstevel@tonic-gate case ASC:
44990Sstevel@tonic-gate if (!(strncmp(Buffr.b_out_p, CMS_ASC, CMS_LEN))) {
45000Sstevel@tonic-gate hit = read_hdr(ASC);
45010Sstevel@tonic-gate hsize = ASCSZ + Gen.g_namesz;
45020Sstevel@tonic-gate Max_namesz = APATH;
45030Sstevel@tonic-gate break;
45040Sstevel@tonic-gate }
45050Sstevel@tonic-gate if (Hdr_type != NONE)
45060Sstevel@tonic-gate break;
45070Sstevel@tonic-gate /*FALLTHROUGH*/
45080Sstevel@tonic-gate case CRC:
45090Sstevel@tonic-gate if (!(strncmp(Buffr.b_out_p, CMS_CRC, CMS_LEN))) {
45100Sstevel@tonic-gate hit = read_hdr(CRC);
45110Sstevel@tonic-gate hsize = ASCSZ + Gen.g_namesz;
45120Sstevel@tonic-gate Max_namesz = APATH;
45130Sstevel@tonic-gate break;
45140Sstevel@tonic-gate }
45150Sstevel@tonic-gate if (Hdr_type != NONE)
45160Sstevel@tonic-gate break;
45170Sstevel@tonic-gate /*FALLTHROUGH*/
45180Sstevel@tonic-gate
45190Sstevel@tonic-gate case BAR:
45200Sstevel@tonic-gate if (Hdr_p != NULL && strcmp(Hdr_p, "bar") == 0) {
45210Sstevel@tonic-gate Hdrsz = BARSZ;
45220Sstevel@tonic-gate FILL(Hdrsz);
45230Sstevel@tonic-gate if ((hit = read_hdr(BAR)) == NONE) {
45240Sstevel@tonic-gate Hdrsz = ASCSZ;
45250Sstevel@tonic-gate break;
45260Sstevel@tonic-gate }
45270Sstevel@tonic-gate hit = BAR;
45280Sstevel@tonic-gate hsize = BARSZ;
45290Sstevel@tonic-gate break;
45300Sstevel@tonic-gate }
45310Sstevel@tonic-gate /*FALLTHROUGH*/
45320Sstevel@tonic-gate
45330Sstevel@tonic-gate case USTAR:
45340Sstevel@tonic-gate if (Hdr_p != NULL && strcmp(Hdr_p, "ustar") == 0) {
45350Sstevel@tonic-gate Hdrsz = TARSZ;
45360Sstevel@tonic-gate FILL(Hdrsz);
45370Sstevel@tonic-gate if ((hit = read_hdr(USTAR)) == NONE) {
45380Sstevel@tonic-gate Hdrsz = ASCSZ;
45390Sstevel@tonic-gate break;
45400Sstevel@tonic-gate }
45410Sstevel@tonic-gate hit = USTAR;
45420Sstevel@tonic-gate hsize = TARSZ;
45430Sstevel@tonic-gate break;
45440Sstevel@tonic-gate }
45450Sstevel@tonic-gate /*FALLTHROUGH*/
45460Sstevel@tonic-gate case TAR:
45470Sstevel@tonic-gate if (Hdr_p != NULL && strcmp(Hdr_p, "tar") == 0) {
45480Sstevel@tonic-gate Hdrsz = TARSZ;
45490Sstevel@tonic-gate FILL(Hdrsz);
45500Sstevel@tonic-gate if ((hit = read_hdr(TAR)) == NONE) {
45510Sstevel@tonic-gate Hdrsz = ASCSZ;
45520Sstevel@tonic-gate break;
45530Sstevel@tonic-gate }
45540Sstevel@tonic-gate hit = TAR;
45550Sstevel@tonic-gate hsize = TARSZ;
45560Sstevel@tonic-gate break;
45570Sstevel@tonic-gate }
45580Sstevel@tonic-gate /*FALLTHROUGH*/
45590Sstevel@tonic-gate default:
45600Sstevel@tonic-gate msg(EXT, "Impossible header type.");
45610Sstevel@tonic-gate } /* Hdr_type */
45620Sstevel@tonic-gate
45630Sstevel@tonic-gate if (hit == TAR || hit == USTAR) {
45640Sstevel@tonic-gate Gen.g_nam_p = &nambuf[0];
45650Sstevel@tonic-gate }
45660Sstevel@tonic-gate
45670Sstevel@tonic-gate if (hit != NONE) {
45680Sstevel@tonic-gate FILL(hsize);
45690Sstevel@tonic-gate goodhdr = 1;
45700Sstevel@tonic-gate if (Gen.g_filesz < (off_t)0 || Gen.g_namesz < 1)
45710Sstevel@tonic-gate goodhdr = 0;
45720Sstevel@tonic-gate if ((hit != USTAR) && (hit != TAR))
45730Sstevel@tonic-gate if (Gen.g_namesz - 1 > Max_namesz)
45740Sstevel@tonic-gate goodhdr = 0;
45750Sstevel@tonic-gate /* TAR and USTAR */
45760Sstevel@tonic-gate if ((hit == USTAR) || (hit == TAR)) {
45770Sstevel@tonic-gate if (*Gen.g_nam_p == '\0') { /* tar trailer */
45780Sstevel@tonic-gate goodhdr = 1;
45790Sstevel@tonic-gate } else {
45800Sstevel@tonic-gate
45810Sstevel@tonic-gate G_p = &Gen;
45820Sstevel@tonic-gate if (G_p->g_cksum !=
45830Sstevel@tonic-gate cksum(TARTYP, 0, NULL)) {
45840Sstevel@tonic-gate goodhdr = 0;
45850Sstevel@tonic-gate msg(ERR,
45860Sstevel@tonic-gate "Bad header - checksum "
45870Sstevel@tonic-gate "error.");
45880Sstevel@tonic-gate }
45890Sstevel@tonic-gate }
45900Sstevel@tonic-gate } else if (hit != BAR) { /* binary, -c, ASC and CRC */
45910Sstevel@tonic-gate if (Gen.g_nlink <= (ulong_t)0)
45920Sstevel@tonic-gate goodhdr = 0;
45930Sstevel@tonic-gate if (*(Buffr.b_out_p + hsize - 1) != '\0')
45940Sstevel@tonic-gate goodhdr = 0;
45950Sstevel@tonic-gate }
45960Sstevel@tonic-gate if (!goodhdr) {
45970Sstevel@tonic-gate hit = NONE;
45980Sstevel@tonic-gate if (!(Args & OCk))
45990Sstevel@tonic-gate break;
46000Sstevel@tonic-gate msg(ERR,
46010Sstevel@tonic-gate "Corrupt header, file(s) may be lost.");
46020Sstevel@tonic-gate } else {
46030Sstevel@tonic-gate FILL(hsize);
46040Sstevel@tonic-gate }
46050Sstevel@tonic-gate } /* hit != NONE */
46060Sstevel@tonic-gate if (hit == NONE) {
46070Sstevel@tonic-gate Buffr.b_out_p++;
46080Sstevel@tonic-gate Buffr.b_cnt--;
46090Sstevel@tonic-gate if (!(Args & OCk))
46100Sstevel@tonic-gate break;
46110Sstevel@tonic-gate if (!cnt++)
46120Sstevel@tonic-gate msg(ERR, "Searching for magic number/header.");
46130Sstevel@tonic-gate }
46140Sstevel@tonic-gate } while (hit == NONE);
46150Sstevel@tonic-gate if (hit == NONE) {
46160Sstevel@tonic-gate if (Hdr_type == NONE)
46170Sstevel@tonic-gate msg(EXT, "Not a cpio file, bad header.");
46180Sstevel@tonic-gate else
46190Sstevel@tonic-gate msg(EXT, "Bad magic number/header.");
46200Sstevel@tonic-gate } else if (cnt > 0) {
46210Sstevel@tonic-gate msg(EPOST, "Re-synchronized on magic number/header.");
46220Sstevel@tonic-gate }
46230Sstevel@tonic-gate if (Hdr_type == NONE) {
46240Sstevel@tonic-gate Hdr_type = hit;
46250Sstevel@tonic-gate switch (Hdr_type) {
46260Sstevel@tonic-gate case BIN:
46270Sstevel@tonic-gate if (bswap)
46280Sstevel@tonic-gate Args |= BSM;
46290Sstevel@tonic-gate Hdrsz = HDRSZ;
46300Sstevel@tonic-gate Max_namesz = CPATH;
46310Sstevel@tonic-gate Pad_val = HALFWD;
46320Sstevel@tonic-gate Onecopy = 0;
46330Sstevel@tonic-gate break;
46340Sstevel@tonic-gate case CHR:
46350Sstevel@tonic-gate Hdrsz = CHRSZ;
46360Sstevel@tonic-gate Max_namesz = CPATH;
46370Sstevel@tonic-gate Pad_val = 0;
46380Sstevel@tonic-gate Onecopy = 0;
46390Sstevel@tonic-gate break;
46400Sstevel@tonic-gate case ASC:
46410Sstevel@tonic-gate case CRC:
46420Sstevel@tonic-gate Hdrsz = ASCSZ;
46430Sstevel@tonic-gate Max_namesz = APATH;
46440Sstevel@tonic-gate Pad_val = FULLWD;
46450Sstevel@tonic-gate Onecopy = 1;
46460Sstevel@tonic-gate break;
46470Sstevel@tonic-gate case USTAR:
46480Sstevel@tonic-gate Hdrsz = TARSZ;
46490Sstevel@tonic-gate Max_namesz = HNAMLEN - 1;
46500Sstevel@tonic-gate Pad_val = FULLBK;
46510Sstevel@tonic-gate Onecopy = 0;
46520Sstevel@tonic-gate break;
46530Sstevel@tonic-gate case BAR:
46540Sstevel@tonic-gate case TAR:
46550Sstevel@tonic-gate Hdrsz = TARSZ;
46560Sstevel@tonic-gate Max_namesz = TNAMLEN - 1;
46570Sstevel@tonic-gate Pad_val = FULLBK;
46580Sstevel@tonic-gate Onecopy = 0;
46590Sstevel@tonic-gate break;
46600Sstevel@tonic-gate default:
46610Sstevel@tonic-gate msg(EXT, "Impossible header type.");
46620Sstevel@tonic-gate } /* Hdr_type */
46630Sstevel@tonic-gate } /* Hdr_type == NONE */
46640Sstevel@tonic-gate if ((Hdr_type == USTAR) || (Hdr_type == TAR) ||
46650Sstevel@tonic-gate (Hdr_type == BAR)) { /* TAR, USTAR, BAR */
46660Sstevel@tonic-gate Gen.g_namesz = 0;
46670Sstevel@tonic-gate if (Gen.g_nam_p[0] == '\0')
46680Sstevel@tonic-gate return (0);
46690Sstevel@tonic-gate else {
46700Sstevel@tonic-gate preptr = &prebuf[0];
46715750Sceastha if (*preptr != NULL) {
46720Sstevel@tonic-gate k = strlen(&prebuf[0]);
46730Sstevel@tonic-gate if (k < PRESIZ) {
46740Sstevel@tonic-gate (void) strcpy(&fullnam[0], &prebuf[0]);
46750Sstevel@tonic-gate j = 0;
46760Sstevel@tonic-gate fullnam[k++] = '/';
46770Sstevel@tonic-gate while ((j < NAMSIZ) && (&nambuf[j] !=
46785750Sceastha '\0')) {
46790Sstevel@tonic-gate fullnam[k] = nambuf[j];
46800Sstevel@tonic-gate k++; j++;
46810Sstevel@tonic-gate }
46820Sstevel@tonic-gate fullnam[k] = '\0';
46830Sstevel@tonic-gate } else if (k >= PRESIZ) {
46840Sstevel@tonic-gate k = 0;
46850Sstevel@tonic-gate while ((k < PRESIZ) && (prebuf[k] !=
46860Sstevel@tonic-gate '\0')) {
46870Sstevel@tonic-gate fullnam[k] = prebuf[k];
46880Sstevel@tonic-gate k++;
46890Sstevel@tonic-gate }
46900Sstevel@tonic-gate fullnam[k++] = '/';
46910Sstevel@tonic-gate j = 0;
46920Sstevel@tonic-gate while ((j < NAMSIZ) && (nambuf[j] !=
46930Sstevel@tonic-gate '\0')) {
46940Sstevel@tonic-gate fullnam[k] = nambuf[j];
46950Sstevel@tonic-gate k++; j++;
46960Sstevel@tonic-gate }
46970Sstevel@tonic-gate fullnam[k] = '\0';
46980Sstevel@tonic-gate }
46990Sstevel@tonic-gate Gen.g_nam_p = &fullnam[0];
47000Sstevel@tonic-gate } else
47010Sstevel@tonic-gate Gen.g_nam_p = &nambuf[0];
47020Sstevel@tonic-gate
47030Sstevel@tonic-gate /*
47040Sstevel@tonic-gate * initialize the buffer so that the prefix will not
47050Sstevel@tonic-gate * applied to the next entry in the archive
47060Sstevel@tonic-gate */
47070Sstevel@tonic-gate (void) memset(prebuf, 0, sizeof (prebuf));
47080Sstevel@tonic-gate }
47090Sstevel@tonic-gate } else if (Hdr_type != BAR) {
47100Sstevel@tonic-gate (void) memcpy(Gen.g_nam_p, Buffr.b_out_p + Hdrsz, Gen.g_namesz);
47110Sstevel@tonic-gate if (!(strcmp(Gen.g_nam_p, "TRAILER!!!")))
47120Sstevel@tonic-gate return (0);
47130Sstevel@tonic-gate }
47140Sstevel@tonic-gate offset = ((hsize + Pad_val) & ~Pad_val);
47150Sstevel@tonic-gate FILL(offset + Hdrsz);
47160Sstevel@tonic-gate Thdr_p = (union tblock *)Buffr.b_out_p;
47170Sstevel@tonic-gate Buffr.b_out_p += offset;
47180Sstevel@tonic-gate Buffr.b_cnt -= (off_t)offset;
47190Sstevel@tonic-gate ftype = Gen.g_mode & Ftype;
47200Sstevel@tonic-gate
47210Sstevel@tonic-gate #if defined(O_XATTR)
47220Sstevel@tonic-gate /* extended attribute support */
47230Sstevel@tonic-gate if (((Gen.g_mode & S_IFMT) == _XATTR_CPIO_MODE) ||
47240Sstevel@tonic-gate ((Hdr_type == USTAR || Hdr_type == TAR) &&
47250Sstevel@tonic-gate Thdr_p->tbuf.t_typeflag == _XATTR_HDRTYPE)) {
47265750Sceastha char *aname;
47275750Sceastha char *attrparent = NULL;
47285750Sceastha char *attrpath = NULL;
47295331Samw char *tapath;
47305750Sceastha char *taname;
47315750Sceastha
47325750Sceastha if (xattrp != NULL) {
47330Sstevel@tonic-gate if (xattrbadhead) {
47340Sstevel@tonic-gate free(xattrhead);
47350Sstevel@tonic-gate xattrp = NULL;
47360Sstevel@tonic-gate xattr_linkp = NULL;
47370Sstevel@tonic-gate xattrhead = NULL;
47380Sstevel@tonic-gate return (1);
47390Sstevel@tonic-gate }
47405331Samw
47415331Samw /*
47425750Sceastha * At this point, the attribute path contains
47435750Sceastha * the path to the attribute rooted at the hidden
47445750Sceastha * attribute directory of the base file. This can
47455750Sceastha * be a simple attribute or extended attribute name,
47465750Sceastha * or it can be something like <attr>/<sys attr> if
47475750Sceastha * we are processing a system attribute of an attribute.
47485750Sceastha * Determine the attribute name and attribute parent
47495750Sceastha * (if there is one). When we are processing a simple
47505750Sceastha * attribute or extended attribute name, the attribute
47515750Sceastha * parent will be set to NULL. When we are processing
47525750Sceastha * something like <attr>/<sys attr>, the attribute
47535750Sceastha * parent will be contain <attr>, and the attribute
47545750Sceastha * name will contain <sys attr>.
47555331Samw */
47565750Sceastha tapath = xattrp->h_names +
47575750Sceastha strlen(xattrp->h_names) + 1;
47585750Sceastha attrpath = e_strdup(E_EXIT, tapath);
47595750Sceastha if ((taname = strpbrk(tapath, "/")) != NULL) {
47605750Sceastha aname = taname + 1;
47615750Sceastha *taname = '\0';
47625750Sceastha attrparent = tapath;
47635750Sceastha } else {
47645750Sceastha aname = tapath;
47655750Sceastha }
47665750Sceastha
47675750Sceastha Gen.g_rw_sysattr = is_sysattr(aname);
47685750Sceastha Gen.g_baseparent_fd = attr_baseparent_fd;
47695750Sceastha
47705750Sceastha if (Gen.g_attrfnam_p != NULL) {
47710Sstevel@tonic-gate free(Gen.g_attrfnam_p);
47725750Sceastha Gen.g_attrfnam_p = NULL;
47735750Sceastha }
47745750Sceastha if (Gen.g_attrnam_p != NULL) {
47750Sstevel@tonic-gate free(Gen.g_attrnam_p);
47765750Sceastha Gen.g_attrnam_p = NULL;
47775750Sceastha }
47785750Sceastha if (Gen.g_attrparent_p != NULL) {
47795750Sceastha free(Gen.g_attrparent_p);
47805750Sceastha Gen.g_attrparent_p = NULL;
47815750Sceastha }
47825750Sceastha if (Gen.g_attrpath_p != NULL) {
47835750Sceastha free(Gen.g_attrpath_p);
47845750Sceastha Gen.g_attrpath_p = NULL;
47850Sstevel@tonic-gate }
47860Sstevel@tonic-gate if (Renam_p && Renam_p[0] != '\0') {
47870Sstevel@tonic-gate Gen.g_attrfnam_p = e_strdup(E_EXIT, Renam_p);
47880Sstevel@tonic-gate } else {
47890Sstevel@tonic-gate Gen.g_attrfnam_p = e_strdup(E_EXIT,
47900Sstevel@tonic-gate xattrp->h_names);
47910Sstevel@tonic-gate }
47925750Sceastha Gen.g_attrnam_p = e_strdup(E_EXIT, aname);
47935750Sceastha
47945750Sceastha if (attrparent != NULL) {
47955750Sceastha if (Renam_attr_p && Renam_attr_p[0] != '\0') {
47965750Sceastha size_t apathlen = strlen(attrparent) +
47975750Sceastha strlen(aname) + 2;
47985750Sceastha Gen.g_attrparent_p = e_strdup(E_EXIT,
47995750Sceastha Renam_attr_p);
48005750Sceastha Gen.g_attrpath_p = e_zalloc(E_EXIT,
48015750Sceastha apathlen);
48025750Sceastha (void) snprintf(Gen.g_attrpath_p,
48035750Sceastha apathlen, "%s/%s", Renam_attr_p,
48045750Sceastha aname);
48055750Sceastha (void) free(attrparent);
48065750Sceastha (void) free(attrpath);
48075750Sceastha } else {
48085750Sceastha Gen.g_attrparent_p = attrparent;
48095750Sceastha Gen.g_attrpath_p = attrpath;
48105750Sceastha }
48115750Sceastha } else {
48125750Sceastha Gen.g_attrpath_p = attrpath;
48135750Sceastha }
48145750Sceastha
48155750Sceastha if (xattr_linkp != NULL) {
48165750Sceastha if (Gen.g_linktoattrfnam_p != NULL) {
48175750Sceastha free(Gen.g_linktoattrfnam_p);
48185750Sceastha Gen.g_linktoattrfnam_p = NULL;
48195750Sceastha }
48205750Sceastha if (Gen.g_linktoattrnam_p != NULL) {
48215750Sceastha free(Gen.g_linktoattrnam_p);
48225750Sceastha Gen.g_linktoattrnam_p = NULL;
48235750Sceastha }
48245750Sceastha if (Renam_attr_p && Renam_attr_p[0] != '\0') {
48255750Sceastha Gen.g_linktoattrfnam_p = e_strdup(
48265750Sceastha E_EXIT, Renam_attr_p);
48275750Sceastha } else {
48285750Sceastha Gen.g_linktoattrfnam_p = e_strdup(
48295750Sceastha E_EXIT, xattr_linkp->h_names);
48305750Sceastha }
48315750Sceastha Gen.g_linktoattrnam_p = e_strdup(E_EXIT,
48325750Sceastha aname);
48335750Sceastha xattr_linkp = NULL;
48345750Sceastha }
48350Sstevel@tonic-gate if (Hdr_type != USTAR && Hdr_type != TAR) {
48360Sstevel@tonic-gate Gen.g_mode = Gen.g_mode & (~_XATTR_CPIO_MODE);
48370Sstevel@tonic-gate Gen.g_mode |= attrmode(xattrp->h_typeflag);
48380Sstevel@tonic-gate } else if (Hdr_type == USTAR || Hdr_type == TAR) {
48390Sstevel@tonic-gate Thdr_p->tbuf.t_typeflag = xattrp->h_typeflag;
48400Sstevel@tonic-gate }
48415750Sceastha
48420Sstevel@tonic-gate ftype = Gen.g_mode & Ftype;
48430Sstevel@tonic-gate Adir = ftype == S_IFDIR;
48445276Sceastha Aspec = (ftype == S_IFBLK || ftype == S_IFCHR ||
48455276Sceastha ftype == S_IFIFO || ftype == S_IFSOCK);
48460Sstevel@tonic-gate
48470Sstevel@tonic-gate if (Gen.g_attrnam_p[0] == '.' &&
48480Sstevel@tonic-gate Gen.g_attrnam_p[1] == '\0' &&
48490Sstevel@tonic-gate xattrp->h_typeflag == DIRTYPE) {
48500Sstevel@tonic-gate Hiddendir = 1;
48510Sstevel@tonic-gate } else {
48520Sstevel@tonic-gate Hiddendir = 0;
48530Sstevel@tonic-gate }
48540Sstevel@tonic-gate
48550Sstevel@tonic-gate free(xattrhead);
48560Sstevel@tonic-gate xattrhead = NULL;
48570Sstevel@tonic-gate xattrp = NULL;
48580Sstevel@tonic-gate } else {
48590Sstevel@tonic-gate if (xattrbadhead == 0) {
48600Sstevel@tonic-gate (void) read_xattr_hdr();
48610Sstevel@tonic-gate return (2);
48620Sstevel@tonic-gate }
48630Sstevel@tonic-gate }
48645750Sceastha } else {
48655750Sceastha Hiddendir = 0;
48660Sstevel@tonic-gate }
48670Sstevel@tonic-gate #endif /* O_XATTR */
48680Sstevel@tonic-gate
48690Sstevel@tonic-gate /* acl support: grab acl info */
48700Sstevel@tonic-gate if ((Gen.g_mode == SECMODE) || ((Hdr_type == USTAR ||
48710Sstevel@tonic-gate Hdr_type == TAR) && Thdr_p->tbuf.t_typeflag == 'A')) {
48720Sstevel@tonic-gate /* this is an ancillary file */
48730Sstevel@tonic-gate off_t bytes;
48740Sstevel@tonic-gate char *secp;
48750Sstevel@tonic-gate int pad;
48760Sstevel@tonic-gate int cnt;
48770Sstevel@tonic-gate char *tp;
48780Sstevel@tonic-gate int attrsize;
48790Sstevel@tonic-gate
48800Sstevel@tonic-gate if (Pflag) {
48810Sstevel@tonic-gate bytes = Gen.g_filesz;
48820Sstevel@tonic-gate secp = e_zalloc(E_EXIT, (uint_t)bytes);
48830Sstevel@tonic-gate tp = secp;
48840Sstevel@tonic-gate
48850Sstevel@tonic-gate while (bytes > 0) {
48860Sstevel@tonic-gate cnt = (int)(bytes > CPIOBSZ) ? CPIOBSZ : bytes;
48870Sstevel@tonic-gate FILL(cnt);
48880Sstevel@tonic-gate (void) memcpy(tp, Buffr.b_out_p, cnt);
48890Sstevel@tonic-gate tp += cnt;
48900Sstevel@tonic-gate Buffr.b_out_p += cnt;
48910Sstevel@tonic-gate Buffr.b_cnt -= (off_t)cnt;
48920Sstevel@tonic-gate bytes -= (off_t)cnt;
48930Sstevel@tonic-gate }
48940Sstevel@tonic-gate
48950Sstevel@tonic-gate pad = (Pad_val + 1 - (Gen.g_filesz & Pad_val)) &
48960Sstevel@tonic-gate Pad_val;
48970Sstevel@tonic-gate if (pad != 0) {
48980Sstevel@tonic-gate FILL(pad);
48990Sstevel@tonic-gate Buffr.b_out_p += pad;
49000Sstevel@tonic-gate Buffr.b_cnt -= (off_t)pad;
49010Sstevel@tonic-gate }
49020Sstevel@tonic-gate
49030Sstevel@tonic-gate /* got all attributes in secp */
49040Sstevel@tonic-gate tp = secp;
49050Sstevel@tonic-gate do {
49060Sstevel@tonic-gate attr = (struct sec_attr *)tp;
49070Sstevel@tonic-gate switch (attr->attr_type) {
49080Sstevel@tonic-gate case UFSD_ACL:
4909789Sahrens case ACE_ACL:
49100Sstevel@tonic-gate (void) sscanf(attr->attr_len, "%7lo",
49110Sstevel@tonic-gate (ulong_t *)&aclcnt);
4912789Sahrens /* header is 8 */
49130Sstevel@tonic-gate attrsize = 8 +
49140Sstevel@tonic-gate strlen(&attr->attr_info[0])
49150Sstevel@tonic-gate + 1;
4916789Sahrens
4917789Sahrens error =
4918789Sahrens acl_fromtext(&attr->attr_info[0],
4919789Sahrens &aclp);
4920789Sahrens
4921789Sahrens if (error != 0) {
4922789Sahrens msg(ERR,
4923789Sahrens "aclfromtext failed: %s",
4924789Sahrens acl_strerror(error));
4925789Sahrens bytes -= attrsize;
49260Sstevel@tonic-gate break;
49270Sstevel@tonic-gate }
4928789Sahrens
4929789Sahrens if (aclcnt != acl_cnt(aclp)) {
49300Sstevel@tonic-gate msg(ERR, "acl count error");
4931789Sahrens bytes -= attrsize;
49320Sstevel@tonic-gate break;
49330Sstevel@tonic-gate }
49340Sstevel@tonic-gate bytes -= attrsize;
49350Sstevel@tonic-gate break;
49360Sstevel@tonic-gate
49370Sstevel@tonic-gate /* SunFed case goes here */
49380Sstevel@tonic-gate
49390Sstevel@tonic-gate default:
49400Sstevel@tonic-gate msg(EXT, "unrecognized attr type");
49410Sstevel@tonic-gate break;
49420Sstevel@tonic-gate }
49430Sstevel@tonic-gate /* next attributes */
49440Sstevel@tonic-gate tp += attrsize;
49450Sstevel@tonic-gate } while (bytes > 0);
49460Sstevel@tonic-gate free(secp);
49470Sstevel@tonic-gate } else {
49480Sstevel@tonic-gate /* skip security info */
49490Sstevel@tonic-gate G_p = &Gen;
49500Sstevel@tonic-gate data_in(P_SKIP);
49510Sstevel@tonic-gate }
49520Sstevel@tonic-gate /*
49530Sstevel@tonic-gate * We already got the file content, dont call file_in()
49540Sstevel@tonic-gate * when return. The new return code(2) is used to
49550Sstevel@tonic-gate * indicate that.
49560Sstevel@tonic-gate */
49570Sstevel@tonic-gate VERBOSE((Args & OCt), Gen.g_nam_p);
49580Sstevel@tonic-gate return (2);
49590Sstevel@tonic-gate } /* acl */
49600Sstevel@tonic-gate
496110202SNobutomo.Nakano@Sun.COM /*
496210202SNobutomo.Nakano@Sun.COM * Sparse file support
496310202SNobutomo.Nakano@Sun.COM * Read header of holesdata to get original file size.
496410202SNobutomo.Nakano@Sun.COM * This is necessary because ckname() or file_in() shows file size
496510202SNobutomo.Nakano@Sun.COM * with OCt before data_in() extracts the holesdata. data_in()
496610202SNobutomo.Nakano@Sun.COM * actually doesn't extract the holesdata since proc_mode will be
496710202SNobutomo.Nakano@Sun.COM * P_SKIP in the OCt mode.
496810202SNobutomo.Nakano@Sun.COM */
496910202SNobutomo.Nakano@Sun.COM if ((Hdr_type == CHR || Hdr_type == ASC) &&
497010202SNobutomo.Nakano@Sun.COM S_ISSPARSE(Gen.g_mode) && Gen.g_filesz > MIN_HOLES_HDRSIZE) {
497110202SNobutomo.Nakano@Sun.COM char holesdata[MIN_HOLES_HDRSIZE + 1];
497210202SNobutomo.Nakano@Sun.COM
497310202SNobutomo.Nakano@Sun.COM FILL(MIN_HOLES_HDRSIZE);
497410202SNobutomo.Nakano@Sun.COM (void) memcpy(holesdata, Buffr.b_out_p, MIN_HOLES_HDRSIZE);
497510202SNobutomo.Nakano@Sun.COM holesdata[MIN_HOLES_HDRSIZE] = '\0';
497610202SNobutomo.Nakano@Sun.COM
497710202SNobutomo.Nakano@Sun.COM Gen.g_holes = read_holes_header(holesdata, Gen.g_filesz);
497810202SNobutomo.Nakano@Sun.COM if (Gen.g_holes == NULL) {
497910202SNobutomo.Nakano@Sun.COM msg(EXT, "invalid sparse file information");
498010202SNobutomo.Nakano@Sun.COM } else {
498110202SNobutomo.Nakano@Sun.COM Buffr.b_out_p += MIN_HOLES_HDRSIZE;
498210202SNobutomo.Nakano@Sun.COM Buffr.b_cnt -= MIN_HOLES_HDRSIZE;
498310202SNobutomo.Nakano@Sun.COM }
498410202SNobutomo.Nakano@Sun.COM }
498510202SNobutomo.Nakano@Sun.COM
49860Sstevel@tonic-gate Adir = (ftype == S_IFDIR);
49875276Sceastha Aspec = (ftype == S_IFBLK || ftype == S_IFCHR || ftype == S_IFIFO ||
49885276Sceastha ftype == S_IFSOCK);
49890Sstevel@tonic-gate
49900Sstevel@tonic-gate /*
49910Sstevel@tonic-gate * Skip any trailing slashes
49920Sstevel@tonic-gate */
49930Sstevel@tonic-gate chop_endslashes(Gen.g_nam_p);
49940Sstevel@tonic-gate return (1);
49950Sstevel@tonic-gate }
49960Sstevel@tonic-gate
49970Sstevel@tonic-gate /*
49980Sstevel@tonic-gate * getname: Get file names for inclusion in the archive. When end of file
49990Sstevel@tonic-gate * on the input stream of file names is reached, flush the link buffer out.
50000Sstevel@tonic-gate * For each filename, remove leading "./"s and multiple "/"s, and remove
50015331Samw * any trailing newline "\n". Finally, verify the existence of the file,
50020Sstevel@tonic-gate * and call creat_hdr() to fill in the gen_hdr structure.
50030Sstevel@tonic-gate */
50040Sstevel@tonic-gate
50050Sstevel@tonic-gate static int
getname(void)50060Sstevel@tonic-gate getname(void)
50070Sstevel@tonic-gate {
50080Sstevel@tonic-gate int goodfile = 0, lastchar, err;
50090Sstevel@tonic-gate char *s;
50100Sstevel@tonic-gate char *dir;
50110Sstevel@tonic-gate
50120Sstevel@tonic-gate Gen.g_nam_p = Nam_p;
50135750Sceastha Hiddendir = 0;
50140Sstevel@tonic-gate
50150Sstevel@tonic-gate while (!goodfile) {
50160Sstevel@tonic-gate err = 0;
50170Sstevel@tonic-gate
50185750Sceastha while ((s = fgets(Gen.g_nam_p, APATH+1, In_p)) != NULL) {
50190Sstevel@tonic-gate lastchar = strlen(s) - 1;
50200Sstevel@tonic-gate issymlink = 0;
50210Sstevel@tonic-gate
50220Sstevel@tonic-gate if (s[lastchar] != '\n') {
50230Sstevel@tonic-gate if (lastchar == APATH - 1) {
50240Sstevel@tonic-gate if (!err) {
50250Sstevel@tonic-gate msg(ERR,
50260Sstevel@tonic-gate "%s name too long.",
50270Sstevel@tonic-gate Nam_p);
50280Sstevel@tonic-gate }
50290Sstevel@tonic-gate goodfile = 0;
50300Sstevel@tonic-gate err = 1;
50310Sstevel@tonic-gate } else {
50320Sstevel@tonic-gate break;
50330Sstevel@tonic-gate }
50340Sstevel@tonic-gate } else {
50350Sstevel@tonic-gate s[lastchar] = '\0';
50360Sstevel@tonic-gate break;
50370Sstevel@tonic-gate }
50380Sstevel@tonic-gate }
50390Sstevel@tonic-gate
50405750Sceastha if (s == NULL) {
50410Sstevel@tonic-gate if (Gen.g_dirfd != -1) {
50420Sstevel@tonic-gate (void) close(Gen.g_dirfd);
50430Sstevel@tonic-gate Gen.g_dirfd = -1;
50440Sstevel@tonic-gate }
50450Sstevel@tonic-gate if (Onecopy && (Args & OCo)) {
50460Sstevel@tonic-gate flush_lnks();
50470Sstevel@tonic-gate }
50480Sstevel@tonic-gate return (0);
50490Sstevel@tonic-gate }
50500Sstevel@tonic-gate
50510Sstevel@tonic-gate while (*Gen.g_nam_p == '.' && Gen.g_nam_p[1] == '/') {
50520Sstevel@tonic-gate Gen.g_nam_p += 2;
50530Sstevel@tonic-gate while (*Gen.g_nam_p == '/')
50540Sstevel@tonic-gate Gen.g_nam_p++;
50550Sstevel@tonic-gate }
50560Sstevel@tonic-gate
50570Sstevel@tonic-gate /*
50580Sstevel@tonic-gate * Skip any trailing slashes
50590Sstevel@tonic-gate */
50600Sstevel@tonic-gate chop_endslashes(Gen.g_nam_p);
50610Sstevel@tonic-gate
50620Sstevel@tonic-gate /*
50630Sstevel@tonic-gate * Figure out parent directory
50640Sstevel@tonic-gate */
50650Sstevel@tonic-gate
50665750Sceastha if (Gen.g_attrnam_p != NULL) {
50670Sstevel@tonic-gate if (Gen.g_dirfd != -1) {
50680Sstevel@tonic-gate (void) close(Gen.g_dirfd);
50690Sstevel@tonic-gate }
50700Sstevel@tonic-gate Gen.g_dirfd = attropen(Gen.g_attrfnam_p, ".", O_RDONLY);
50710Sstevel@tonic-gate if (Gen.g_dirfd == -1) {
50720Sstevel@tonic-gate msg(ERRN,
50730Sstevel@tonic-gate "Cannot open attribute directory"
50740Sstevel@tonic-gate " of file %s", Gen.g_attrfnam_p);
50750Sstevel@tonic-gate continue;
50760Sstevel@tonic-gate }
50770Sstevel@tonic-gate } else {
50780Sstevel@tonic-gate #ifdef O_XATTR
50790Sstevel@tonic-gate char dirpath[PATH_MAX];
50800Sstevel@tonic-gate
50810Sstevel@tonic-gate get_parent(Gen.g_nam_p, dirpath);
50825750Sceastha if (Atflag || SysAtflag) {
50830Sstevel@tonic-gate dir = dirpath;
50840Sstevel@tonic-gate if (Gen.g_dirfd != -1) {
50850Sstevel@tonic-gate (void) close(Gen.g_dirfd);
50860Sstevel@tonic-gate }
50870Sstevel@tonic-gate Gen.g_dirfd = open(dir, O_RDONLY);
50880Sstevel@tonic-gate if (Gen.g_dirfd == -1) {
50890Sstevel@tonic-gate msg(ERRN,
50900Sstevel@tonic-gate "Cannot open directory %s", dir);
50910Sstevel@tonic-gate continue;
50920Sstevel@tonic-gate }
50930Sstevel@tonic-gate } else {
50940Sstevel@tonic-gate /*
50950Sstevel@tonic-gate * g_dirpath is the pathname cache maintaining
50960Sstevel@tonic-gate * the dirname which is currently opened.
50970Sstevel@tonic-gate * We first check the g_dirpath to see if the
50980Sstevel@tonic-gate * given dirname matches. If so, we don't need
50990Sstevel@tonic-gate * to open the dir, but we can use the g_dirfd
51000Sstevel@tonic-gate * as is if it is still available.
51010Sstevel@tonic-gate */
51020Sstevel@tonic-gate dir = NULL;
51030Sstevel@tonic-gate if (Gen.g_dirpath == NULL ||
51040Sstevel@tonic-gate Gen.g_dirfd == -1) {
51050Sstevel@tonic-gate /*
51060Sstevel@tonic-gate * It's the first time or it has
51070Sstevel@tonic-gate * all gone.
51080Sstevel@tonic-gate */
51090Sstevel@tonic-gate dir = e_strdup(E_EXIT, dirpath);
51100Sstevel@tonic-gate } else {
51110Sstevel@tonic-gate if (strcmp(Gen.g_dirpath,
51120Sstevel@tonic-gate dirpath) != 0) {
51130Sstevel@tonic-gate /* different directory */
51140Sstevel@tonic-gate dir = e_strdup(E_EXIT, dirpath);
51150Sstevel@tonic-gate }
51160Sstevel@tonic-gate }
51170Sstevel@tonic-gate if (dir != NULL) {
51180Sstevel@tonic-gate /*
51190Sstevel@tonic-gate * We need to open the new directory.
51200Sstevel@tonic-gate * discard the pathname and dirfd
51210Sstevel@tonic-gate * for the previous directory.
51220Sstevel@tonic-gate */
51230Sstevel@tonic-gate if (Gen.g_dirpath != NULL) {
51240Sstevel@tonic-gate free(Gen.g_dirpath);
51250Sstevel@tonic-gate Gen.g_dirpath = NULL;
51260Sstevel@tonic-gate }
51270Sstevel@tonic-gate if (Gen.g_dirfd != -1) {
51280Sstevel@tonic-gate (void) close(Gen.g_dirfd);
51290Sstevel@tonic-gate }
51300Sstevel@tonic-gate /* open the new dir */
51310Sstevel@tonic-gate Gen.g_dirfd = open(dir, O_RDONLY);
51320Sstevel@tonic-gate if (Gen.g_dirfd == -1) {
51330Sstevel@tonic-gate msg(ERRN, "Cannot open "
51341134Sceastha "directory %s", dir);
51350Sstevel@tonic-gate continue;
51360Sstevel@tonic-gate }
51370Sstevel@tonic-gate Gen.g_dirpath = dir;
51380Sstevel@tonic-gate }
51390Sstevel@tonic-gate }
51400Sstevel@tonic-gate #else
51410Sstevel@tonic-gate Gen.g_dirfd = -1;
51420Sstevel@tonic-gate #endif
51430Sstevel@tonic-gate }
51440Sstevel@tonic-gate
51450Sstevel@tonic-gate /* creat_hdr checks for USTAR filename length */
51460Sstevel@tonic-gate
51470Sstevel@tonic-gate if (Hdr_type != USTAR && strlen(Gen.g_nam_p) >
51480Sstevel@tonic-gate Max_namesz) {
51490Sstevel@tonic-gate if (!err) {
51500Sstevel@tonic-gate msg(ERR, "%s%s%s name too long.",
51515750Sceastha (Gen.g_attrnam_p == NULL) ?
51520Sstevel@tonic-gate Nam_p : Gen.g_attrfnam_p,
51535750Sceastha (Gen.g_attrnam_p == NULL) ?
51545750Sceastha "" : Gen.g_rw_sysattr ?
51555750Sceastha gettext(" System Attribute ") :
51565750Sceastha gettext(" Attribute "),
51575750Sceastha (Gen.g_attrnam_p == NULL) ?
51580Sstevel@tonic-gate "" : Gen.g_attrnam_p);
51590Sstevel@tonic-gate }
51600Sstevel@tonic-gate goodfile = 0;
51610Sstevel@tonic-gate err = 1;
51620Sstevel@tonic-gate }
51630Sstevel@tonic-gate
51640Sstevel@tonic-gate if (err) {
51650Sstevel@tonic-gate continue;
51660Sstevel@tonic-gate } else {
51670Sstevel@tonic-gate G_p = &Gen;
51680Sstevel@tonic-gate if (!LSTAT(Gen.g_dirfd, Gen.g_nam_p, &SrcSt)) {
51690Sstevel@tonic-gate goodfile = 1;
51700Sstevel@tonic-gate
51710Sstevel@tonic-gate if ((SrcSt.st_mode & Ftype) == S_IFLNK) {
51720Sstevel@tonic-gate issymlink = 1;
51730Sstevel@tonic-gate
51740Sstevel@tonic-gate if ((Args & OCL)) {
51750Sstevel@tonic-gate errno = 0;
51760Sstevel@tonic-gate if (STAT(Gen.g_dirfd,
51770Sstevel@tonic-gate G_p->g_nam_p,
51780Sstevel@tonic-gate &SrcSt) < 0) {
51790Sstevel@tonic-gate msg(ERRN,
51800Sstevel@tonic-gate "Cannot follow"
51810Sstevel@tonic-gate " \"%s%s%s\"",
51820Sstevel@tonic-gate (Gen.g_attrnam_p ==
51835750Sceastha NULL) ?
51840Sstevel@tonic-gate Gen.g_nam_p :
51850Sstevel@tonic-gate Gen.g_attrfnam_p,
51860Sstevel@tonic-gate (Gen.g_attrnam_p ==
51875750Sceastha NULL) ? "" :
51885750Sceastha Gen.g_rw_sysattr ?
51895750Sceastha gettext(
51905750Sceastha " System "
51915750Sceastha "Attribute ") :
51920Sstevel@tonic-gate gettext(
51930Sstevel@tonic-gate " Attribute "),
51940Sstevel@tonic-gate (Gen.g_attrnam_p ==
51955750Sceastha NULL) ? "" :
51960Sstevel@tonic-gate Gen.g_attrnam_p);
51970Sstevel@tonic-gate goodfile = 0;
51980Sstevel@tonic-gate }
51990Sstevel@tonic-gate }
52000Sstevel@tonic-gate }
52010Sstevel@tonic-gate
52020Sstevel@tonic-gate if (Use_old_stat) {
52030Sstevel@tonic-gate OldSt = convert_to_old_stat(&SrcSt,
52040Sstevel@tonic-gate Gen.g_nam_p, Gen.g_attrnam_p);
52050Sstevel@tonic-gate
52060Sstevel@tonic-gate if (OldSt == NULL) {
52070Sstevel@tonic-gate goodfile = 0;
52080Sstevel@tonic-gate }
52090Sstevel@tonic-gate }
52100Sstevel@tonic-gate } else {
52110Sstevel@tonic-gate msg(ERRN,
52120Sstevel@tonic-gate "Error with fstatat() of \"%s%s%s\"",
52135750Sceastha (Gen.g_attrnam_p == NULL) ?
52140Sstevel@tonic-gate Gen.g_nam_p : Gen.g_attrfnam_p,
52155750Sceastha (Gen.g_attrnam_p == NULL) ? "" :
52165750Sceastha Gen.g_rw_sysattr ?
52175750Sceastha gettext(" System Attribute ") :
52185750Sceastha gettext(" Attribute "),
52195750Sceastha (Gen.g_attrnam_p == NULL) ?
52200Sstevel@tonic-gate "" : Gen.g_attrnam_p);
52210Sstevel@tonic-gate }
52220Sstevel@tonic-gate }
52230Sstevel@tonic-gate }
52240Sstevel@tonic-gate
52250Sstevel@tonic-gate /*
52260Sstevel@tonic-gate * Get ACL info: dont bother allocating space if there are only
52270Sstevel@tonic-gate * standard permissions, i.e. ACL count < 4
52280Sstevel@tonic-gate */
52290Sstevel@tonic-gate if ((SrcSt.st_mode & Ftype) != S_IFLNK && Pflag) {
5230789Sahrens if (acl_get(Gen.g_nam_p, ACL_NO_TRIVIAL, &aclp) != 0)
52310Sstevel@tonic-gate msg(ERRN, "Error with acl() of \"%s\"", Gen.g_nam_p);
5232789Sahrens }
52330Sstevel@tonic-gate /* else: only traditional permissions, so proceed as usual */
52340Sstevel@tonic-gate if (creat_hdr())
52350Sstevel@tonic-gate return (1);
52360Sstevel@tonic-gate else return (2);
52370Sstevel@tonic-gate }
52380Sstevel@tonic-gate
52390Sstevel@tonic-gate /*
52400Sstevel@tonic-gate * getpats: Save any filenames/patterns specified as arguments.
52410Sstevel@tonic-gate * Read additional filenames/patterns from the file specified by the
52420Sstevel@tonic-gate * user. The filenames/patterns must occur one per line.
52430Sstevel@tonic-gate */
52440Sstevel@tonic-gate
52450Sstevel@tonic-gate static void
getpats(int largc,char ** largv)52460Sstevel@tonic-gate getpats(int largc, char **largv)
52470Sstevel@tonic-gate {
52480Sstevel@tonic-gate char **t_pp;
52490Sstevel@tonic-gate size_t len;
52500Sstevel@tonic-gate unsigned numpat = largc, maxpat = largc + 2;
52510Sstevel@tonic-gate
52520Sstevel@tonic-gate Pat_pp = e_zalloc(E_EXIT, maxpat * sizeof (char *));
52530Sstevel@tonic-gate t_pp = Pat_pp;
52540Sstevel@tonic-gate while (*largv) {
52550Sstevel@tonic-gate *t_pp = e_zalloc(E_EXIT, strlen(*largv) + 1);
52560Sstevel@tonic-gate (void) strcpy(*t_pp, *largv);
52570Sstevel@tonic-gate t_pp++;
52580Sstevel@tonic-gate largv++;
52590Sstevel@tonic-gate }
52605750Sceastha while (fgets(Nam_p, Max_namesz + 1, Ef_p) != NULL) {
52610Sstevel@tonic-gate if (numpat == maxpat - 1) {
52620Sstevel@tonic-gate maxpat += 10;
52630Sstevel@tonic-gate Pat_pp = e_realloc(E_EXIT, Pat_pp,
52640Sstevel@tonic-gate maxpat * sizeof (char *));
52650Sstevel@tonic-gate t_pp = Pat_pp + numpat;
52660Sstevel@tonic-gate }
52670Sstevel@tonic-gate len = strlen(Nam_p); /* includes the \n */
52680Sstevel@tonic-gate *(Nam_p + len - 1) = '\0'; /* remove the \n */
52690Sstevel@tonic-gate *t_pp = e_zalloc(E_EXIT, len);
52700Sstevel@tonic-gate (void) strcpy(*t_pp, Nam_p);
52710Sstevel@tonic-gate t_pp++;
52720Sstevel@tonic-gate numpat++;
52730Sstevel@tonic-gate }
52745750Sceastha *t_pp = NULL;
52750Sstevel@tonic-gate }
52760Sstevel@tonic-gate
52770Sstevel@tonic-gate static void
ioerror(int dir)52780Sstevel@tonic-gate ioerror(int dir)
52790Sstevel@tonic-gate {
52800Sstevel@tonic-gate int t_errno;
52810Sstevel@tonic-gate
52820Sstevel@tonic-gate t_errno = errno;
52830Sstevel@tonic-gate errno = 0;
52840Sstevel@tonic-gate if (fstat(Archive, &ArchSt) < 0)
52850Sstevel@tonic-gate msg(EXTN, "Error during stat() of archive");
52860Sstevel@tonic-gate errno = t_errno;
52870Sstevel@tonic-gate if ((ArchSt.st_mode & Ftype) != S_IFCHR) {
52880Sstevel@tonic-gate if (dir) {
52890Sstevel@tonic-gate if (errno == EFBIG)
52900Sstevel@tonic-gate msg(EXT, "ulimit reached for output file.");
52910Sstevel@tonic-gate else if (errno == ENOSPC)
52920Sstevel@tonic-gate msg(EXT, "No space left for output file.");
52930Sstevel@tonic-gate else
52940Sstevel@tonic-gate msg(EXTN, "I/O error - cannot continue");
52950Sstevel@tonic-gate } else
52960Sstevel@tonic-gate msg(EXT, "Unexpected end-of-file encountered.");
52970Sstevel@tonic-gate } else
52980Sstevel@tonic-gate msg(EXTN, "\007I/O error on \"%s\"", dir ? "output" : "input");
52990Sstevel@tonic-gate }
53000Sstevel@tonic-gate
53010Sstevel@tonic-gate /*
53020Sstevel@tonic-gate * matched: Determine if a filename matches the specified pattern(s). If the
53030Sstevel@tonic-gate * pattern is matched (the second return), return 0 if -f was specified, else
53040Sstevel@tonic-gate * return != 0. If the pattern is not matched (the first and third
53050Sstevel@tonic-gate * returns), return 0 if -f was not specified, else return != 0.
53060Sstevel@tonic-gate */
53070Sstevel@tonic-gate
53080Sstevel@tonic-gate static int
matched(void)53090Sstevel@tonic-gate matched(void)
53100Sstevel@tonic-gate {
53110Sstevel@tonic-gate char *str_p = G_p->g_nam_p;
53120Sstevel@tonic-gate char **pat_pp = Pat_pp;
53130Sstevel@tonic-gate int negatep, result;
53140Sstevel@tonic-gate
53150Sstevel@tonic-gate /*
53160Sstevel@tonic-gate * Check for attribute
53170Sstevel@tonic-gate */
53185750Sceastha if (G_p->g_attrfnam_p != NULL)
53190Sstevel@tonic-gate str_p = G_p->g_attrfnam_p;
53200Sstevel@tonic-gate
53210Sstevel@tonic-gate for (pat_pp = Pat_pp; *pat_pp; pat_pp++) {
53220Sstevel@tonic-gate negatep = (**pat_pp == '!');
53230Sstevel@tonic-gate
53240Sstevel@tonic-gate result = fnmatch(negatep ? (*pat_pp+1) : *pat_pp, str_p, 0);
53250Sstevel@tonic-gate
53260Sstevel@tonic-gate if (result != 0 && result != FNM_NOMATCH) {
53270Sstevel@tonic-gate msg(POST, "error matching file %s with pattern"
53280Sstevel@tonic-gate " %s\n", str_p, *pat_pp);
53290Sstevel@tonic-gate return (Args & OCf);
53300Sstevel@tonic-gate }
53310Sstevel@tonic-gate
53320Sstevel@tonic-gate if ((result == 0 && ! negatep) ||
53330Sstevel@tonic-gate (result == FNM_NOMATCH && negatep)) {
53345331Samw /* match occurred */
53350Sstevel@tonic-gate return (!(Args & OCf));
53360Sstevel@tonic-gate }
53370Sstevel@tonic-gate }
53380Sstevel@tonic-gate return (Args & OCf); /* not matched */
53390Sstevel@tonic-gate }
53400Sstevel@tonic-gate
53410Sstevel@tonic-gate /*
53420Sstevel@tonic-gate * missdir: Create missing directories for files.
53430Sstevel@tonic-gate * (Possible future performance enhancement, if missdir is called, we know
53440Sstevel@tonic-gate * that at least the very last directory of the path does not exist, therefore,
53450Sstevel@tonic-gate * scan the path from the end
53460Sstevel@tonic-gate */
53470Sstevel@tonic-gate
53480Sstevel@tonic-gate static int
missdir(char * nam_p)53490Sstevel@tonic-gate missdir(char *nam_p)
53500Sstevel@tonic-gate {
53510Sstevel@tonic-gate char *c_p;
53520Sstevel@tonic-gate int cnt = 2;
53530Sstevel@tonic-gate char *lastp;
53540Sstevel@tonic-gate
53550Sstevel@tonic-gate if (*(c_p = nam_p) == '/') /* skip over 'root slash' */
53560Sstevel@tonic-gate c_p++;
53570Sstevel@tonic-gate
53580Sstevel@tonic-gate lastp = c_p + strlen(nam_p) - 1;
53590Sstevel@tonic-gate if (*lastp == '/')
53600Sstevel@tonic-gate *lastp = '\0';
53610Sstevel@tonic-gate
53620Sstevel@tonic-gate for (; *c_p; ++c_p) {
53630Sstevel@tonic-gate if (*c_p == '/') {
53640Sstevel@tonic-gate *c_p = '\0';
53650Sstevel@tonic-gate if (stat(nam_p, &DesSt) < 0) {
53660Sstevel@tonic-gate if (Args & OCd) {
53670Sstevel@tonic-gate cnt = mkdir(nam_p, Def_mode);
53680Sstevel@tonic-gate if (cnt != 0) {
53690Sstevel@tonic-gate *c_p = '/';
53700Sstevel@tonic-gate return (cnt);
53710Sstevel@tonic-gate }
53720Sstevel@tonic-gate } else {
53730Sstevel@tonic-gate msg(ERR, "Missing -d option.");
53740Sstevel@tonic-gate *c_p = '/';
53750Sstevel@tonic-gate return (-1);
53760Sstevel@tonic-gate }
53770Sstevel@tonic-gate }
53780Sstevel@tonic-gate *c_p = '/';
53790Sstevel@tonic-gate }
53800Sstevel@tonic-gate }
53810Sstevel@tonic-gate if (cnt == 2) /* the file already exists */
53820Sstevel@tonic-gate cnt = 0;
53830Sstevel@tonic-gate return (cnt);
53840Sstevel@tonic-gate }
53850Sstevel@tonic-gate
53860Sstevel@tonic-gate /*
53870Sstevel@tonic-gate * mklong: Convert two shorts into one long. For VAX, Interdata ...
53880Sstevel@tonic-gate */
53890Sstevel@tonic-gate
53900Sstevel@tonic-gate static long
mklong(short v[])53910Sstevel@tonic-gate mklong(short v[])
53920Sstevel@tonic-gate {
53930Sstevel@tonic-gate
53940Sstevel@tonic-gate union swpbuf swp_b;
53950Sstevel@tonic-gate
53960Sstevel@tonic-gate swp_b.s_word = 1;
53970Sstevel@tonic-gate if (swp_b.s_byte[0]) {
53980Sstevel@tonic-gate swp_b.s_half[0] = v[1];
53990Sstevel@tonic-gate swp_b.s_half[1] = v[0];
54000Sstevel@tonic-gate } else {
54010Sstevel@tonic-gate swp_b.s_half[0] = v[0];
54020Sstevel@tonic-gate swp_b.s_half[1] = v[1];
54030Sstevel@tonic-gate }
54040Sstevel@tonic-gate return (swp_b.s_word);
54050Sstevel@tonic-gate }
54060Sstevel@tonic-gate
54070Sstevel@tonic-gate /*
54080Sstevel@tonic-gate * mkshort: Convert a long into 2 shorts, for VAX, Interdata ...
54090Sstevel@tonic-gate */
54100Sstevel@tonic-gate
54110Sstevel@tonic-gate static void
mkshort(short sval[],long v)54120Sstevel@tonic-gate mkshort(short sval[], long v)
54130Sstevel@tonic-gate {
54140Sstevel@tonic-gate union swpbuf *swp_p, swp_b;
54150Sstevel@tonic-gate
54160Sstevel@tonic-gate /* LINTED alignment */
54170Sstevel@tonic-gate swp_p = (union swpbuf *)sval;
54180Sstevel@tonic-gate swp_b.s_word = 1;
54190Sstevel@tonic-gate if (swp_b.s_byte[0]) {
54200Sstevel@tonic-gate swp_b.s_word = v;
54210Sstevel@tonic-gate swp_p->s_half[0] = swp_b.s_half[1];
54220Sstevel@tonic-gate swp_p->s_half[1] = swp_b.s_half[0];
54230Sstevel@tonic-gate } else {
54240Sstevel@tonic-gate swp_b.s_word = v;
54250Sstevel@tonic-gate swp_p->s_half[0] = swp_b.s_half[0];
54260Sstevel@tonic-gate swp_p->s_half[1] = swp_b.s_half[1];
54270Sstevel@tonic-gate }
54280Sstevel@tonic-gate }
54290Sstevel@tonic-gate
54300Sstevel@tonic-gate /*
54310Sstevel@tonic-gate * msg: Print either a message (no error) (POST), an error message with or
54320Sstevel@tonic-gate * without the errno (ERRN or ERR), or print an error message with or without
54330Sstevel@tonic-gate * the errno and exit (EXTN or EXT).
54340Sstevel@tonic-gate */
543510202SNobutomo.Nakano@Sun.COM void
msg(int severity,const char * fmt,...)54360Sstevel@tonic-gate msg(int severity, const char *fmt, ...)
54370Sstevel@tonic-gate {
54380Sstevel@tonic-gate FILE *file_p;
54390Sstevel@tonic-gate va_list ap;
54400Sstevel@tonic-gate
54410Sstevel@tonic-gate if ((Args & OCV) && Verbcnt) { /* clear current line of dots */
54420Sstevel@tonic-gate (void) fputc('\n', Out_p);
54430Sstevel@tonic-gate Verbcnt = 0;
54440Sstevel@tonic-gate }
54450Sstevel@tonic-gate va_start(ap, fmt);
54460Sstevel@tonic-gate if (severity == POST)
54470Sstevel@tonic-gate file_p = Out_p;
54480Sstevel@tonic-gate else
54490Sstevel@tonic-gate if (severity == EPOST)
54500Sstevel@tonic-gate file_p = Err_p;
54510Sstevel@tonic-gate else {
54520Sstevel@tonic-gate file_p = Err_p;
54530Sstevel@tonic-gate Error_cnt++;
54540Sstevel@tonic-gate }
54550Sstevel@tonic-gate (void) fflush(Out_p);
54560Sstevel@tonic-gate (void) fflush(Err_p);
54570Sstevel@tonic-gate if ((severity != POST) && (severity != EPOST))
54580Sstevel@tonic-gate (void) fprintf(file_p, "cpio: ");
54590Sstevel@tonic-gate
54600Sstevel@tonic-gate /* gettext replaces version of string */
54610Sstevel@tonic-gate
54620Sstevel@tonic-gate (void) vfprintf(file_p, gettext(fmt), ap);
54630Sstevel@tonic-gate if (severity == ERRN || severity == EXTN) {
54646511Schin if (G_p && (G_p->g_attrnam_p != NULL) && G_p->g_rw_sysattr) {
54655750Sceastha if (errno == EPERM) {
54665750Sceastha (void) fprintf(file_p, ", errno %d, %s", errno,
54675750Sceastha gettext("insufficient privileges\n"));
54685750Sceastha } else if (errno == EINVAL) {
54695750Sceastha (void) fprintf(file_p, ", errno %d, %s",
54705750Sceastha errno, gettext(
54715750Sceastha "unsupported on underlying file system\n"));
54725750Sceastha } else {
54735750Sceastha (void) fprintf(file_p, ", errno %d, ", errno);
54745750Sceastha perror("");
54755750Sceastha }
54765750Sceastha } else {
54775750Sceastha (void) fprintf(file_p, ", errno %d, ", errno);
54785750Sceastha perror("");
54795750Sceastha }
54800Sstevel@tonic-gate } else
54810Sstevel@tonic-gate (void) fprintf(file_p, "\n");
54820Sstevel@tonic-gate (void) fflush(file_p);
54830Sstevel@tonic-gate va_end(ap);
54840Sstevel@tonic-gate if (severity == EXT || severity == EXTN) {
54850Sstevel@tonic-gate (void) fprintf(file_p, gettext("%d errors\n"), Error_cnt);
54860Sstevel@tonic-gate exit(EXIT_CODE);
54870Sstevel@tonic-gate }
54880Sstevel@tonic-gate }
54890Sstevel@tonic-gate
54900Sstevel@tonic-gate /*
54910Sstevel@tonic-gate * openout: Open files for output and set all necessary information.
54920Sstevel@tonic-gate * If the u option is set (unconditionally overwrite existing files),
54930Sstevel@tonic-gate * and the current file exists, get a temporary file name from mktemp(3C),
54940Sstevel@tonic-gate * link the temporary file to the existing file, and remove the existing file.
54950Sstevel@tonic-gate * Finally either creat(2), mkdir(2) or mknod(2) as appropriate.
54960Sstevel@tonic-gate *
54970Sstevel@tonic-gate */
54980Sstevel@tonic-gate
54990Sstevel@tonic-gate static int
openout(int dirfd)55000Sstevel@tonic-gate openout(int dirfd)
55010Sstevel@tonic-gate {
55020Sstevel@tonic-gate char *nam_p;
55030Sstevel@tonic-gate int cnt, result;
55040Sstevel@tonic-gate
55050Sstevel@tonic-gate Do_rename = 0; /* creat_tmp() may reset this */
55060Sstevel@tonic-gate
55075750Sceastha if (G_p->g_attrnam_p != NULL) {
55080Sstevel@tonic-gate nam_p = G_p->g_attrnam_p;
55090Sstevel@tonic-gate } else {
55100Sstevel@tonic-gate if (Args & OCp) {
55110Sstevel@tonic-gate nam_p = Fullnam_p;
55120Sstevel@tonic-gate } else {
55130Sstevel@tonic-gate nam_p = G_p->g_nam_p;
55140Sstevel@tonic-gate }
55150Sstevel@tonic-gate }
55160Sstevel@tonic-gate
55170Sstevel@tonic-gate
55180Sstevel@tonic-gate if ((Max_filesz != RLIM_INFINITY) &&
55190Sstevel@tonic-gate (Max_filesz < (G_p->g_filesz >> 9))) {
55200Sstevel@tonic-gate /* ... divided by 512 ... */
55210Sstevel@tonic-gate msg(ERR, "Skipping \"%s%s%s\": exceeds ulimit by %lld bytes",
55225750Sceastha (G_p->g_attrnam_p == NULL) ? nam_p : G_p->g_attrfnam_p,
55235750Sceastha (G_p->g_attrnam_p == NULL) ? "" : G_p->g_rw_sysattr ?
55245750Sceastha gettext(" System Attribute ") : gettext(" Attribute "),
55255750Sceastha (G_p->g_attrnam_p == NULL) ? "" : nam_p,
55260Sstevel@tonic-gate (off_t)(G_p->g_filesz - (Max_filesz << 9)));
55270Sstevel@tonic-gate return (-1);
55280Sstevel@tonic-gate }
55290Sstevel@tonic-gate
55300Sstevel@tonic-gate if (LSTAT(dirfd, nam_p, &DesSt) == 0) {
55310Sstevel@tonic-gate /*
55320Sstevel@tonic-gate * A file by the same name exists. Move it to a temporary
55335750Sceastha * file unless it's a system attribute file. If we are
55345750Sceastha * restoring a system attribute file on a file system that
55355750Sceastha * supports system attributes, then the system attribute file
55365750Sceastha * will already exist (a default system attribute file will
55375750Sceastha * get created when the file it is associated with is created).
55385750Sceastha * If we create a temporary system attribute file, we can't
55395750Sceastha * overwrite the existing system attribute file using
55405750Sceastha * renameat(). In addition, only system attributes can exist
55415750Sceastha * for an attribute of a file, therefore, a temporary file
55425750Sceastha * cannot be created for a system attribute of an attribute.
55435750Sceastha * Thus, when restoring a system attribute, we won't move it
55445750Sceastha * to a temporary file, but will attempt to process it as if
55455750Sceastha * it didn't already exist.
55460Sstevel@tonic-gate */
55470Sstevel@tonic-gate
55485750Sceastha #if defined(_PC_SATTR_ENABLED)
55495750Sceastha if (G_p->g_rw_sysattr == 0)
55505750Sceastha #endif /* _PC_SATTR_ENABLED */
55515750Sceastha if (creat_tmp(nam_p) < 0) {
55525750Sceastha /*
55535750Sceastha * We weren't able to create the temp file.
55545750Sceastha * Report failure.
55555750Sceastha */
55565750Sceastha
55575750Sceastha return (-1);
55585750Sceastha }
55590Sstevel@tonic-gate }
55600Sstevel@tonic-gate
55610Sstevel@tonic-gate if (Do_rename) {
55620Sstevel@tonic-gate /* nam_p was changed by creat_tmp() above. */
55630Sstevel@tonic-gate
55640Sstevel@tonic-gate if (Args & OCp) {
55655750Sceastha if (G_p->g_attrnam_p != NULL) {
55660Sstevel@tonic-gate nam_p = Attrfile_p;
55670Sstevel@tonic-gate } else {
55680Sstevel@tonic-gate nam_p = Fullnam_p;
55690Sstevel@tonic-gate }
55700Sstevel@tonic-gate } else {
55710Sstevel@tonic-gate nam_p = G_p->g_nam_p;
55720Sstevel@tonic-gate }
55730Sstevel@tonic-gate }
55740Sstevel@tonic-gate
55750Sstevel@tonic-gate /*
55760Sstevel@tonic-gate * This pile tries to create the file directly, and, if there is a
55770Sstevel@tonic-gate * problem, creates missing directories, and then tries to create the
55780Sstevel@tonic-gate * file again. Two strikes and you're out.
55790Sstevel@tonic-gate *
55800Sstevel@tonic-gate * On XATTR system, the directory has already been created by
55810Sstevel@tonic-gate * open_dirfd(), so error shouldn't happen in the loop. However,
55820Sstevel@tonic-gate * on non-XATTR system, symlink/open may fail with ENOENT. In such
55830Sstevel@tonic-gate * case, we go to create missing directories.
55840Sstevel@tonic-gate */
55850Sstevel@tonic-gate
55860Sstevel@tonic-gate cnt = 0;
55870Sstevel@tonic-gate
55880Sstevel@tonic-gate do {
55890Sstevel@tonic-gate errno = 0;
55900Sstevel@tonic-gate
55910Sstevel@tonic-gate if (Hdr_type == TAR && Thdr_p->tbuf.t_typeflag == SYMTYPE) {
55920Sstevel@tonic-gate /* The archive file is a TAR symlink. */
55930Sstevel@tonic-gate if ((result =
55940Sstevel@tonic-gate symlink(Thdr_p->tbuf.t_linkname, nam_p)) >= 0) {
55950Sstevel@tonic-gate cnt = 0;
55960Sstevel@tonic-gate if (Over_p != NULL) {
55970Sstevel@tonic-gate (void) unlinkat(dirfd,
55980Sstevel@tonic-gate get_component(Over_p), 0);
55990Sstevel@tonic-gate *Over_p = '\0';
56000Sstevel@tonic-gate }
56010Sstevel@tonic-gate break;
56020Sstevel@tonic-gate } else if (errno != ENOENT) {
56030Sstevel@tonic-gate /* The attempt to symlink failed. */
56040Sstevel@tonic-gate msg(ERRN,
56050Sstevel@tonic-gate "Cannot create symbolic link \"%s\" -> "
56060Sstevel@tonic-gate "\"%s\"",
56070Sstevel@tonic-gate Thdr_p->tbuf.t_linkname, nam_p);
56080Sstevel@tonic-gate
56090Sstevel@tonic-gate if (*Over_p != '\0') {
56100Sstevel@tonic-gate rstfiles(U_KEEP, dirfd);
56110Sstevel@tonic-gate }
56120Sstevel@tonic-gate return (-1);
56130Sstevel@tonic-gate }
56140Sstevel@tonic-gate } else if (Hdr_type == BAR && bar_linkflag == SYMTYPE) {
56150Sstevel@tonic-gate if ((result = symlink(bar_linkname, nam_p)) >= 0) {
56160Sstevel@tonic-gate cnt = 0;
56170Sstevel@tonic-gate if (Over_p != NULL) {
56180Sstevel@tonic-gate (void) unlinkat(dirfd,
56190Sstevel@tonic-gate get_component(Over_p), 0);
56200Sstevel@tonic-gate *Over_p = '\0';
56210Sstevel@tonic-gate }
56220Sstevel@tonic-gate break;
56230Sstevel@tonic-gate } else if (errno != ENOENT) {
56240Sstevel@tonic-gate /* The attempt to symlink failed. */
56250Sstevel@tonic-gate msg(ERRN,
56260Sstevel@tonic-gate "Cannot create symbolic link \"%s\" -> "
56270Sstevel@tonic-gate "\"%s\"",
56280Sstevel@tonic-gate bar_linkname, nam_p);
56290Sstevel@tonic-gate if (*Over_p != '\0') {
56300Sstevel@tonic-gate rstfiles(U_KEEP, dirfd);
56310Sstevel@tonic-gate }
56320Sstevel@tonic-gate return (-1);
56330Sstevel@tonic-gate }
56340Sstevel@tonic-gate } else if ((G_p->g_mode & Ftype) == S_IFLNK) {
56350Sstevel@tonic-gate if ((!(Args & OCp)) && !(Hdr_type == USTAR)) {
56366246Snakanon FILL(G_p->g_filesz);
56370Sstevel@tonic-gate (void) strncpy(Symlnk_p,
56380Sstevel@tonic-gate Buffr.b_out_p, G_p->g_filesz);
56390Sstevel@tonic-gate *(Symlnk_p + G_p->g_filesz) = '\0';
56400Sstevel@tonic-gate } else if ((!(Args & OCp)) && (Hdr_type == USTAR)) {
56410Sstevel@tonic-gate Symlnk_p[NAMSIZ] = '\0';
56420Sstevel@tonic-gate (void) strncpy(Symlnk_p,
56430Sstevel@tonic-gate &Thdr_p->tbuf.t_linkname[0], NAMSIZ);
56440Sstevel@tonic-gate }
56450Sstevel@tonic-gate if ((result = symlink(Symlnk_p, nam_p)) >= 0) {
56460Sstevel@tonic-gate cnt = 0;
56470Sstevel@tonic-gate if (Over_p != NULL) {
56480Sstevel@tonic-gate (void) unlinkat(dirfd,
56490Sstevel@tonic-gate get_component(Over_p), 0);
56500Sstevel@tonic-gate *Over_p = '\0';
56510Sstevel@tonic-gate }
56520Sstevel@tonic-gate break;
56530Sstevel@tonic-gate } else if (errno != ENOENT) {
56540Sstevel@tonic-gate /* The attempt to symlink failed. */
56550Sstevel@tonic-gate msg(ERRN,
56560Sstevel@tonic-gate "Cannot create symbolic link \"%s\" -> "
56570Sstevel@tonic-gate "\"%s\"",
56580Sstevel@tonic-gate Symlnk_p, nam_p);
56590Sstevel@tonic-gate
56600Sstevel@tonic-gate if (*Over_p != '\0') {
56610Sstevel@tonic-gate rstfiles(U_KEEP, dirfd);
56620Sstevel@tonic-gate }
56630Sstevel@tonic-gate return (-1);
56640Sstevel@tonic-gate }
56650Sstevel@tonic-gate } else {
56665750Sceastha int saveerrno;
56675750Sceastha
56680Sstevel@tonic-gate if ((result = openat(dirfd, get_component(nam_p),
56695750Sceastha O_CREAT|O_RDWR|O_TRUNC, (int)G_p->g_mode)) < 0) {
56705750Sceastha saveerrno = errno;
56715750Sceastha if (G_p->g_attrnam_p != NULL) {
56725750Sceastha result = retry_open_attr(dirfd,
56735750Sceastha Gen.g_baseparent_fd, Fullnam_p,
56745750Sceastha (G_p->g_attrparent_p == NULL) ?
56755750Sceastha NULL : G_p->g_attrparent_p, nam_p,
56765750Sceastha O_CREAT|O_RDWR|O_TRUNC,
56775750Sceastha (int)G_p->g_mode);
56785750Sceastha }
56795750Sceastha }
56805750Sceastha if (result < 0) {
56815750Sceastha errno = saveerrno;
56825750Sceastha if (errno != ENOENT) {
56835750Sceastha /* The attempt to open failed. */
56845750Sceastha msg(ERRN, "Cannot open file \"%s\"",
56855750Sceastha nam_p);
56865750Sceastha if (*Over_p != '\0') {
56875750Sceastha rstfiles(U_KEEP, dirfd);
56885750Sceastha }
56895750Sceastha return (-1);
56905750Sceastha }
56915750Sceastha } else {
56920Sstevel@tonic-gate /* acl support */
5693789Sahrens acl_is_set = 0;
56940Sstevel@tonic-gate if (Pflag && aclp != NULL) {
5695789Sahrens if (facl_set(result, aclp) < 0) {
56960Sstevel@tonic-gate msg(ERRN,
56970Sstevel@tonic-gate "\"%s\": failed to set acl",
56980Sstevel@tonic-gate nam_p);
56990Sstevel@tonic-gate } else {
5700789Sahrens acl_is_set = 1;
57010Sstevel@tonic-gate }
5702789Sahrens acl_free(aclp);
57030Sstevel@tonic-gate aclp = NULL;
57040Sstevel@tonic-gate }
57050Sstevel@tonic-gate cnt = 0;
57060Sstevel@tonic-gate break;
57070Sstevel@tonic-gate }
57080Sstevel@tonic-gate }
57090Sstevel@tonic-gate cnt++;
57100Sstevel@tonic-gate } while (cnt < 2 && missdir(nam_p) == 0);
57110Sstevel@tonic-gate
57120Sstevel@tonic-gate switch (cnt) {
57130Sstevel@tonic-gate case 0:
57140Sstevel@tonic-gate if ((Args & OCi) && (Hdr_type == USTAR)) {
57150Sstevel@tonic-gate setpasswd(nam_p);
57160Sstevel@tonic-gate }
57170Sstevel@tonic-gate if ((G_p->g_mode & Ftype) == S_IFLNK ||
57180Sstevel@tonic-gate (Hdr_type == BAR && bar_linkflag == SYMTYPE)) {
57191134Sceastha if (Args & OCR) {
57201134Sceastha if (fchownat(dirfd,
57211134Sceastha get_component(nam_p),
57221134Sceastha (int)Rpw_p->pw_uid,
57231134Sceastha (int)Rpw_p->pw_gid,
57241134Sceastha AT_SYMLINK_NOFOLLOW) < 0) {
57250Sstevel@tonic-gate msg(ERRN,
57260Sstevel@tonic-gate "Error during chown() of "
57270Sstevel@tonic-gate "\"%s%s%s\"",
57285750Sceastha (G_p->g_attrnam_p == NULL) ?
57290Sstevel@tonic-gate nam_p : G_p->g_attrfnam_p,
57305750Sceastha (G_p->g_attrnam_p == NULL) ?
57315750Sceastha "" : G_p->g_rw_sysattr ?
57325750Sceastha gettext(" System Attribute ") :
57335750Sceastha gettext(" Attribute "),
57345750Sceastha (G_p->g_attrnam_p == NULL) ?
57350Sstevel@tonic-gate "" : nam_p);
57360Sstevel@tonic-gate }
57371134Sceastha } else if ((fchownat(dirfd, get_component(nam_p),
57381134Sceastha (int)G_p->g_uid, (int)G_p->g_gid,
57391134Sceastha AT_SYMLINK_NOFOLLOW) < 0) && privileged) {
57400Sstevel@tonic-gate msg(ERRN,
57410Sstevel@tonic-gate "Error during chown() of \"%s%s%s\"",
57425750Sceastha (G_p->g_attrnam_p == NULL) ?
57430Sstevel@tonic-gate nam_p : G_p->g_attrfnam_p,
57445750Sceastha (G_p->g_attrnam_p == NULL) ? "" :
57455750Sceastha G_p->g_rw_sysattr ?
57465750Sceastha gettext(" System Attribute ") :
57475750Sceastha gettext(" Attribute "),
57485750Sceastha (G_p->g_attrnam_p == NULL) ? "" : nam_p);
57490Sstevel@tonic-gate }
57500Sstevel@tonic-gate }
57510Sstevel@tonic-gate break;
57520Sstevel@tonic-gate
57530Sstevel@tonic-gate case 1:
57540Sstevel@tonic-gate if (Do_rename) {
57550Sstevel@tonic-gate msg(ERRN, "Cannot create directory for \"%s%s%s\"",
57565750Sceastha (G_p->g_attrnam_p == NULL) ? Over_p :
57570Sstevel@tonic-gate G_p->g_attrfnam_p,
57585750Sceastha (G_p->g_attrnam_p == NULL) ? "" :
57595750Sceastha G_p->g_rw_sysattr ?
57605750Sceastha gettext(" System Attribute ") :
57610Sstevel@tonic-gate gettext(" Attribute "),
57625750Sceastha (G_p->g_attrnam_p == NULL) ? "" : Over_p);
57630Sstevel@tonic-gate } else {
57640Sstevel@tonic-gate msg(ERRN, "Cannot create directory for \"%s%s%s\"",
57655750Sceastha (G_p->g_attrnam_p == NULL) ? nam_p :
57660Sstevel@tonic-gate G_p->g_attrfnam_p,
57675750Sceastha (G_p->g_attrnam_p == NULL) ? "" :
57685750Sceastha G_p->g_rw_sysattr ?
57695750Sceastha gettext(" System Attribute ") :
57700Sstevel@tonic-gate gettext(" Attribute "),
57715750Sceastha (G_p->g_attrnam_p == NULL) ? "" : nam_p);
57720Sstevel@tonic-gate }
57730Sstevel@tonic-gate break;
57740Sstevel@tonic-gate
57750Sstevel@tonic-gate case 2:
57760Sstevel@tonic-gate if (Do_rename) {
57770Sstevel@tonic-gate msg(ERRN, "Cannot create \"%s%s%s\"",
57785750Sceastha (G_p->g_attrnam_p == NULL) ? Over_p :
57790Sstevel@tonic-gate G_p->g_attrfnam_p,
57805750Sceastha (G_p->g_attrnam_p == NULL) ? "" :
57815750Sceastha G_p->g_rw_sysattr ?
57825750Sceastha gettext(" System Attribute ") :
57830Sstevel@tonic-gate gettext(" Attribute "),
57845750Sceastha (G_p->g_attrnam_p == NULL) ? "" :
57850Sstevel@tonic-gate Over_p);
57860Sstevel@tonic-gate } else {
57870Sstevel@tonic-gate msg(ERRN, "Cannot create \"%s%s%s\"",
57885750Sceastha (G_p->g_attrnam_p == NULL) ? nam_p :
57890Sstevel@tonic-gate G_p->g_attrfnam_p,
57905750Sceastha (G_p->g_attrnam_p == NULL) ? "" :
57915750Sceastha G_p->g_rw_sysattr ?
57925750Sceastha gettext(" System Attribute ") :
57930Sstevel@tonic-gate gettext(" Attribute "),
57945750Sceastha (G_p->g_attrnam_p == NULL) ? "" : nam_p);
57950Sstevel@tonic-gate }
57960Sstevel@tonic-gate break;
57970Sstevel@tonic-gate
57980Sstevel@tonic-gate default:
57990Sstevel@tonic-gate msg(EXT, "Impossible case.");
58000Sstevel@tonic-gate }
58010Sstevel@tonic-gate
58020Sstevel@tonic-gate Finished = 0;
58030Sstevel@tonic-gate return (result);
58040Sstevel@tonic-gate }
58050Sstevel@tonic-gate
58060Sstevel@tonic-gate /*
58070Sstevel@tonic-gate * read_hdr: Transfer headers from the selected format
58080Sstevel@tonic-gate * in the archive I/O buffer to the generic structure.
58090Sstevel@tonic-gate */
58100Sstevel@tonic-gate
58110Sstevel@tonic-gate static
58120Sstevel@tonic-gate int
read_hdr(int hdr)58130Sstevel@tonic-gate read_hdr(int hdr)
58140Sstevel@tonic-gate {
58150Sstevel@tonic-gate int rv = NONE;
58160Sstevel@tonic-gate major_t maj, rmaj;
58170Sstevel@tonic-gate minor_t min, rmin;
58180Sstevel@tonic-gate char tmpnull;
58190Sstevel@tonic-gate static int bar_read_cnt = 0;
58200Sstevel@tonic-gate
58210Sstevel@tonic-gate if (hdr != BAR) {
58220Sstevel@tonic-gate if (Buffr.b_end_p != (Buffr.b_out_p + Hdrsz)) {
58230Sstevel@tonic-gate tmpnull = *(Buffr.b_out_p + Hdrsz);
58240Sstevel@tonic-gate *(Buffr.b_out_p + Hdrsz) = '\0';
58250Sstevel@tonic-gate }
58260Sstevel@tonic-gate }
58270Sstevel@tonic-gate
58280Sstevel@tonic-gate switch (hdr) {
58290Sstevel@tonic-gate case BIN:
58300Sstevel@tonic-gate (void) memcpy(&Hdr, Buffr.b_out_p, HDRSZ);
58310Sstevel@tonic-gate if (Hdr.h_magic == (short)CMN_BBS) {
58320Sstevel@tonic-gate swap((char *)&Hdr, HDRSZ);
58330Sstevel@tonic-gate }
58340Sstevel@tonic-gate Gen.g_magic = Hdr.h_magic;
58350Sstevel@tonic-gate Gen.g_mode = Hdr.h_mode;
58360Sstevel@tonic-gate Gen.g_uid = Hdr.h_uid;
58370Sstevel@tonic-gate Gen.g_gid = Hdr.h_gid;
58380Sstevel@tonic-gate Gen.g_nlink = Hdr.h_nlink;
58390Sstevel@tonic-gate Gen.g_mtime = mklong(Hdr.h_mtime);
58400Sstevel@tonic-gate Gen.g_ino = Hdr.h_ino;
58410Sstevel@tonic-gate Gen.g_dev = Hdr.h_dev;
58420Sstevel@tonic-gate Gen.g_rdev = Hdr.h_rdev;
58430Sstevel@tonic-gate Gen.g_cksum = 0L;
58440Sstevel@tonic-gate Gen.g_filesz = (off_t)mklong(Hdr.h_filesize);
58450Sstevel@tonic-gate Gen.g_namesz = Hdr.h_namesize;
58460Sstevel@tonic-gate rv = BIN;
58470Sstevel@tonic-gate break;
58480Sstevel@tonic-gate case CHR:
58490Sstevel@tonic-gate if (sscanf(Buffr.b_out_p,
58500Sstevel@tonic-gate "%6lo%6lo%6lo%6lo%6lo%6lo%6lo%6lo%11lo%6o%11llo",
58510Sstevel@tonic-gate &Gen.g_magic, &Gen.g_dev, &Gen.g_ino, &Gen.g_mode,
58520Sstevel@tonic-gate &Gen.g_uid, &Gen.g_gid, &Gen.g_nlink, &Gen.g_rdev,
58530Sstevel@tonic-gate (ulong_t *)&Gen.g_mtime, (uint_t *)&Gen.g_namesz,
58540Sstevel@tonic-gate (u_off_t *)&Gen.g_filesz) == CHR_CNT) {
58550Sstevel@tonic-gate rv = CHR;
58560Sstevel@tonic-gate #define cpioMAJOR(x) (int)(((unsigned)x >> 8) & 0x7F)
58570Sstevel@tonic-gate #define cpioMINOR(x) (int)(x & 0xFF)
58580Sstevel@tonic-gate maj = cpioMAJOR(Gen.g_dev);
58590Sstevel@tonic-gate rmaj = cpioMAJOR(Gen.g_rdev);
58600Sstevel@tonic-gate min = cpioMINOR(Gen.g_dev);
58610Sstevel@tonic-gate rmin = cpioMINOR(Gen.g_rdev);
58620Sstevel@tonic-gate if (Use_old_stat) {
58630Sstevel@tonic-gate /* needs error checking */
58640Sstevel@tonic-gate Gen.g_dev = (maj << 8) | min;
58650Sstevel@tonic-gate Gen.g_rdev = (rmaj << 8) | rmin;
58660Sstevel@tonic-gate } else {
58670Sstevel@tonic-gate Gen.g_dev = makedev(maj, min);
58680Sstevel@tonic-gate Gen.g_rdev = makedev(rmaj, rmin);
58690Sstevel@tonic-gate }
58700Sstevel@tonic-gate }
58710Sstevel@tonic-gate break;
58720Sstevel@tonic-gate case ASC:
58730Sstevel@tonic-gate case CRC:
58740Sstevel@tonic-gate if (sscanf(Buffr.b_out_p,
58750Sstevel@tonic-gate "%6lx%8lx%8lx%8lx%8lx%8lx%8lx%8llx%8x%8x%8x%8x%8x%8lx",
58760Sstevel@tonic-gate &Gen.g_magic, &Gen.g_ino, &Gen.g_mode, &Gen.g_uid,
58770Sstevel@tonic-gate &Gen.g_gid, &Gen.g_nlink, &Gen.g_mtime,
58780Sstevel@tonic-gate (u_off_t *)&Gen.g_filesz, (uint_t *)&maj, (uint_t *)&min,
58790Sstevel@tonic-gate (uint_t *)&rmaj, (uint_t *)&rmin, (uint_t *)&Gen.g_namesz,
58800Sstevel@tonic-gate &Gen.g_cksum) == ASC_CNT) {
58810Sstevel@tonic-gate Gen.g_dev = makedev(maj, min);
58820Sstevel@tonic-gate Gen.g_rdev = makedev(rmaj, rmin);
58830Sstevel@tonic-gate rv = hdr;
58840Sstevel@tonic-gate }
58850Sstevel@tonic-gate break;
58860Sstevel@tonic-gate case USTAR: /* TAR and USTAR */
58870Sstevel@tonic-gate if (*Buffr.b_out_p == '\0') {
58880Sstevel@tonic-gate *Gen.g_nam_p = '\0';
58890Sstevel@tonic-gate nambuf[0] = '\0';
58900Sstevel@tonic-gate } else {
58910Sstevel@tonic-gate Thdr_p = (union tblock *)Buffr.b_out_p;
58920Sstevel@tonic-gate Gen.g_nam_p[0] = '\0';
58930Sstevel@tonic-gate (void) strncpy((char *)&nambuf,
58940Sstevel@tonic-gate Thdr_p->tbuf.t_name, NAMSIZ);
58950Sstevel@tonic-gate (void) sscanf(Thdr_p->tbuf.t_mode, "%8lo",
58960Sstevel@tonic-gate &Gen.g_mode);
58970Sstevel@tonic-gate (void) sscanf(Thdr_p->tbuf.t_uid, "%8lo", &Gen.g_uid);
58980Sstevel@tonic-gate (void) sscanf(Thdr_p->tbuf.t_gid, "%8lo", &Gen.g_gid);
58990Sstevel@tonic-gate (void) sscanf(Thdr_p->tbuf.t_size, "%11llo",
59000Sstevel@tonic-gate (u_off_t *)&Gen.g_filesz);
59010Sstevel@tonic-gate (void) sscanf(Thdr_p->tbuf.t_mtime, "%12lo",
59020Sstevel@tonic-gate (ulong_t *)&Gen.g_mtime);
59030Sstevel@tonic-gate (void) sscanf(Thdr_p->tbuf.t_cksum, "%8lo",
59040Sstevel@tonic-gate (ulong_t *)&Gen.g_cksum);
59055750Sceastha if (Thdr_p->tbuf.t_linkname[0] != '\0')
59060Sstevel@tonic-gate Gen.g_nlink = 1;
59070Sstevel@tonic-gate else
59080Sstevel@tonic-gate Gen.g_nlink = 0;
59090Sstevel@tonic-gate
59100Sstevel@tonic-gate switch (Thdr_p->tbuf.t_typeflag) {
59110Sstevel@tonic-gate case SYMTYPE:
59120Sstevel@tonic-gate /* Symbolic Link */
59130Sstevel@tonic-gate Gen.g_nlink = 2;
59140Sstevel@tonic-gate break;
59150Sstevel@tonic-gate case CHRTYPE:
59160Sstevel@tonic-gate Gen.g_mode |= (S_IFMT & S_IFCHR);
59170Sstevel@tonic-gate break;
59180Sstevel@tonic-gate case BLKTYPE:
59190Sstevel@tonic-gate Gen.g_mode |= (S_IFMT & S_IFBLK);
59200Sstevel@tonic-gate break;
59210Sstevel@tonic-gate case DIRTYPE:
59220Sstevel@tonic-gate Gen.g_mode |= (S_IFMT & S_IFDIR);
59230Sstevel@tonic-gate break;
59240Sstevel@tonic-gate case FIFOTYPE:
59250Sstevel@tonic-gate Gen.g_mode |= (S_IFMT & S_IFIFO);
59260Sstevel@tonic-gate break;
59270Sstevel@tonic-gate }
59280Sstevel@tonic-gate
59290Sstevel@tonic-gate (void) sscanf(Thdr_p->tbuf.t_magic, "%8lo",
59300Sstevel@tonic-gate /* LINTED alignment */
59310Sstevel@tonic-gate (ulong_t *)&Gen.g_tmagic);
59320Sstevel@tonic-gate (void) sscanf(Thdr_p->tbuf.t_version, "%8lo",
59330Sstevel@tonic-gate /* LINTED alignment */
59340Sstevel@tonic-gate (ulong_t *)&Gen.g_version);
59350Sstevel@tonic-gate (void) sscanf(Thdr_p->tbuf.t_uname, "%32s",
59360Sstevel@tonic-gate (char *)&Gen.g_uname);
59370Sstevel@tonic-gate (void) sscanf(Thdr_p->tbuf.t_gname, "%32s",
59380Sstevel@tonic-gate (char *)&Gen.g_gname);
59390Sstevel@tonic-gate (void) sscanf(Thdr_p->tbuf.t_devmajor, "%8lo",
59400Sstevel@tonic-gate &Gen.g_dev);
59410Sstevel@tonic-gate (void) sscanf(Thdr_p->tbuf.t_devminor, "%8lo",
59420Sstevel@tonic-gate &Gen.g_rdev);
59430Sstevel@tonic-gate (void) strncpy((char *)&prebuf,
59440Sstevel@tonic-gate Thdr_p->tbuf.t_prefix, PRESIZ);
59450Sstevel@tonic-gate Gen.g_namesz = strlen(Gen.g_nam_p) + 1;
59460Sstevel@tonic-gate Gen.g_dev = makedev(maj, min);
59470Sstevel@tonic-gate }
59480Sstevel@tonic-gate rv = USTAR;
59490Sstevel@tonic-gate break;
59500Sstevel@tonic-gate case TAR:
59510Sstevel@tonic-gate if (*Buffr.b_out_p == '\0') {
59520Sstevel@tonic-gate *Gen.g_nam_p = '\0';
59530Sstevel@tonic-gate nambuf[0] = '\0';
59540Sstevel@tonic-gate } else {
59550Sstevel@tonic-gate Thdr_p = (union tblock *)Buffr.b_out_p;
59560Sstevel@tonic-gate Gen.g_nam_p[0] = '\0';
59570Sstevel@tonic-gate (void) sscanf(Thdr_p->tbuf.t_mode, "%lo", &Gen.g_mode);
59580Sstevel@tonic-gate (void) sscanf(Thdr_p->tbuf.t_uid, "%lo", &Gen.g_uid);
59590Sstevel@tonic-gate (void) sscanf(Thdr_p->tbuf.t_gid, "%lo", &Gen.g_gid);
59600Sstevel@tonic-gate (void) sscanf(Thdr_p->tbuf.t_size, "%llo",
59610Sstevel@tonic-gate (u_off_t *)&Gen.g_filesz);
59620Sstevel@tonic-gate (void) sscanf(Thdr_p->tbuf.t_mtime, "%lo",
59630Sstevel@tonic-gate &Gen.g_mtime);
59640Sstevel@tonic-gate (void) sscanf(Thdr_p->tbuf.t_cksum, "%lo",
59650Sstevel@tonic-gate &Gen.g_cksum);
59660Sstevel@tonic-gate if (Thdr_p->tbuf.t_typeflag == '1') /* hardlink */
59670Sstevel@tonic-gate Gen.g_nlink = 1;
59680Sstevel@tonic-gate else
59690Sstevel@tonic-gate Gen.g_nlink = 0;
59700Sstevel@tonic-gate (void) strncpy(Gen.g_nam_p,
59710Sstevel@tonic-gate Thdr_p->tbuf.t_name, NAMSIZ);
59720Sstevel@tonic-gate Gen.g_namesz = strlen(Gen.g_nam_p) + 1;
59730Sstevel@tonic-gate (void) strcpy(nambuf, Gen.g_nam_p);
59740Sstevel@tonic-gate }
59750Sstevel@tonic-gate rv = TAR;
59760Sstevel@tonic-gate break;
59770Sstevel@tonic-gate case BAR:
59780Sstevel@tonic-gate if (Bar_vol_num == 0 && bar_read_cnt == 0) {
59790Sstevel@tonic-gate read_bar_vol_hdr();
59800Sstevel@tonic-gate bar_read_cnt++;
59810Sstevel@tonic-gate }
59820Sstevel@tonic-gate else
59830Sstevel@tonic-gate read_bar_file_hdr();
59840Sstevel@tonic-gate rv = BAR;
59850Sstevel@tonic-gate break;
59860Sstevel@tonic-gate default:
59870Sstevel@tonic-gate msg(EXT, "Impossible header type.");
59880Sstevel@tonic-gate }
59890Sstevel@tonic-gate
59900Sstevel@tonic-gate if (hdr != BAR) {
59910Sstevel@tonic-gate if (Buffr.b_end_p != (Buffr.b_out_p + Hdrsz))
59920Sstevel@tonic-gate *(Buffr.b_out_p + Hdrsz) = tmpnull;
59930Sstevel@tonic-gate }
59940Sstevel@tonic-gate
59950Sstevel@tonic-gate return (rv);
59960Sstevel@tonic-gate }
59970Sstevel@tonic-gate
59980Sstevel@tonic-gate /*
59990Sstevel@tonic-gate * reclaim: Reclaim linked file structure storage.
60000Sstevel@tonic-gate */
60010Sstevel@tonic-gate
60020Sstevel@tonic-gate static void
reclaim(struct Lnk * p)60030Sstevel@tonic-gate reclaim(struct Lnk *p)
60040Sstevel@tonic-gate {
60050Sstevel@tonic-gate p->L_bck_p->L_nxt_p = p->L_nxt_p;
60060Sstevel@tonic-gate p->L_nxt_p->L_bck_p = p->L_bck_p;
60070Sstevel@tonic-gate
60080Sstevel@tonic-gate while (p != NULL) {
60090Sstevel@tonic-gate struct Lnk *new_p = p->L_lnk_p;
60100Sstevel@tonic-gate
60110Sstevel@tonic-gate free(p->L_gen.g_nam_p);
60120Sstevel@tonic-gate free(p);
60130Sstevel@tonic-gate p = new_p;
60140Sstevel@tonic-gate }
60150Sstevel@tonic-gate }
60160Sstevel@tonic-gate
60170Sstevel@tonic-gate /*
60180Sstevel@tonic-gate * rstbuf: Reset the I/O buffer, move incomplete potential headers to
60190Sstevel@tonic-gate * the front of the buffer and force bread() to refill the buffer. The
60200Sstevel@tonic-gate * return value from bread() is returned (to identify I/O errors). On the
60210Sstevel@tonic-gate * 3B2, reads must begin on a word boundary, therefore, with the -i option,
60220Sstevel@tonic-gate * any remaining bytes in the buffer must be moved to the base of the buffer
60230Sstevel@tonic-gate * in such a way that the destination locations of subsequent reads are
60240Sstevel@tonic-gate * word aligned.
60250Sstevel@tonic-gate */
60260Sstevel@tonic-gate
60270Sstevel@tonic-gate static void
rstbuf(void)60280Sstevel@tonic-gate rstbuf(void)
60290Sstevel@tonic-gate {
60300Sstevel@tonic-gate int pad;
60310Sstevel@tonic-gate
60320Sstevel@tonic-gate if ((Args & OCi) || Append) {
60330Sstevel@tonic-gate if (Buffr.b_out_p != Buffr.b_base_p) {
60340Sstevel@tonic-gate pad = ((Buffr.b_cnt + FULLWD) & ~FULLWD);
60350Sstevel@tonic-gate Buffr.b_in_p = Buffr.b_base_p + pad;
60360Sstevel@tonic-gate pad -= Buffr.b_cnt;
60370Sstevel@tonic-gate (void) memcpy(Buffr.b_base_p + pad, Buffr.b_out_p,
60380Sstevel@tonic-gate (int)Buffr.b_cnt);
60390Sstevel@tonic-gate Buffr.b_out_p = Buffr.b_base_p + pad;
60400Sstevel@tonic-gate }
60410Sstevel@tonic-gate if (bfill() < 0)
60420Sstevel@tonic-gate msg(EXT, "Unexpected end-of-archive encountered.");
60430Sstevel@tonic-gate } else { /* OCo */
60440Sstevel@tonic-gate (void) memcpy(Buffr.b_base_p, Buffr.b_out_p, (int)Buffr.b_cnt);
60450Sstevel@tonic-gate Buffr.b_out_p = Buffr.b_base_p;
60460Sstevel@tonic-gate Buffr.b_in_p = Buffr.b_base_p + Buffr.b_cnt;
60470Sstevel@tonic-gate }
60480Sstevel@tonic-gate }
60490Sstevel@tonic-gate
60500Sstevel@tonic-gate static void
setpasswd(char * nam)60510Sstevel@tonic-gate setpasswd(char *nam)
60520Sstevel@tonic-gate {
60535750Sceastha if ((dpasswd = getpwnam(&Gen.g_uname[0])) == NULL) {
60540Sstevel@tonic-gate msg(EPOST, "cpio: problem reading passwd entry");
60550Sstevel@tonic-gate msg(EPOST, "cpio: %s: owner not changed", nam);
60560Sstevel@tonic-gate if (Gen.g_uid == UID_NOBODY && S_ISREG(Gen.g_mode))
60570Sstevel@tonic-gate Gen.g_mode &= ~S_ISUID;
60580Sstevel@tonic-gate } else
60590Sstevel@tonic-gate Gen.g_uid = dpasswd->pw_uid;
60600Sstevel@tonic-gate
60615750Sceastha if ((dgroup = getgrnam(&Gen.g_gname[0])) == NULL) {
60620Sstevel@tonic-gate msg(EPOST, "cpio: problem reading group entry");
60630Sstevel@tonic-gate msg(EPOST, "cpio: %s: group not changed", nam);
60640Sstevel@tonic-gate if (Gen.g_gid == GID_NOBODY && S_ISREG(Gen.g_mode))
60650Sstevel@tonic-gate Gen.g_mode &= ~S_ISGID;
60660Sstevel@tonic-gate } else
60670Sstevel@tonic-gate Gen.g_gid = dgroup->gr_gid;
60680Sstevel@tonic-gate G_p = &Gen;
60690Sstevel@tonic-gate }
60700Sstevel@tonic-gate
60710Sstevel@tonic-gate /*
60720Sstevel@tonic-gate * rstfiles: Perform final changes to the file. If the -u option is set,
60730Sstevel@tonic-gate * and overwrite == U_OVER, remove the temporary file, else if overwrite
60740Sstevel@tonic-gate * == U_KEEP, unlink the current file, and restore the existing version
60750Sstevel@tonic-gate * of the file. In addition, where appropriate, set the access or modification
60760Sstevel@tonic-gate * times, change the owner and change the modes of the file.
60770Sstevel@tonic-gate *
60780Sstevel@tonic-gate * Note that if Do_rename is set, then the roles of original and temporary
60790Sstevel@tonic-gate * file are reversed. If all went well, we will rename() the temporary file
60805331Samw * over the original in order to accommodate potentially executing files.
60810Sstevel@tonic-gate */
60820Sstevel@tonic-gate static void
rstfiles(int over,int dirfd)60830Sstevel@tonic-gate rstfiles(int over, int dirfd)
60840Sstevel@tonic-gate {
60850Sstevel@tonic-gate char *inam_p, *onam_p, *nam_p;
60860Sstevel@tonic-gate int error;
60870Sstevel@tonic-gate
60885750Sceastha #if defined(_PC_SATTR_ENABLED)
60895750Sceastha /* Time or permissions cannot be set on system attribute files */
60905750Sceastha if ((Gen.g_attrnam_p != NULL) && (Gen.g_rw_sysattr == 1)) {
60915750Sceastha return;
60925750Sceastha }
60935750Sceastha #endif /* _PC_SATTR_ENABLED */
60945750Sceastha
60950Sstevel@tonic-gate if (Args & OCp) {
60965750Sceastha if (G_p->g_attrnam_p == NULL) {
60970Sstevel@tonic-gate nam_p = Fullnam_p;
60980Sstevel@tonic-gate } else {
60990Sstevel@tonic-gate nam_p = G_p->g_attrnam_p;
61000Sstevel@tonic-gate }
61010Sstevel@tonic-gate } else {
61020Sstevel@tonic-gate if (Gen.g_nlink > (ulong_t)0) {
61030Sstevel@tonic-gate nam_p = G_p->g_nam_p;
61040Sstevel@tonic-gate } else {
61050Sstevel@tonic-gate nam_p = Gen.g_nam_p;
61060Sstevel@tonic-gate }
61070Sstevel@tonic-gate }
61085750Sceastha if (Gen.g_attrnam_p != NULL) {
61090Sstevel@tonic-gate nam_p = Gen.g_attrnam_p;
61100Sstevel@tonic-gate }
61110Sstevel@tonic-gate
61120Sstevel@tonic-gate if ((Args & OCi) && (Hdr_type == USTAR)) {
61130Sstevel@tonic-gate setpasswd(nam_p);
61140Sstevel@tonic-gate }
61150Sstevel@tonic-gate if (over == U_KEEP && *Over_p != '\0') {
61160Sstevel@tonic-gate if (Do_rename) {
61170Sstevel@tonic-gate msg(POST, "Restoring existing \"%s%s%s\"",
61185750Sceastha (G_p->g_attrnam_p == NULL) ? Over_p : Fullnam_p,
61195750Sceastha (G_p->g_attrnam_p == NULL) ? "" :
61205750Sceastha G_p->g_rw_sysattr ? gettext(" System Attribute ") :
61215750Sceastha gettext(" Attribute "),
61225750Sceastha (G_p->g_attrnam_p == NULL) ? "" : Over_p);
61230Sstevel@tonic-gate } else {
61240Sstevel@tonic-gate msg(POST, "Restoring existing \"%s%s%s\"",
61255750Sceastha (G_p->g_attrnam_p == NULL) ? nam_p : Fullnam_p,
61265750Sceastha (G_p->g_attrnam_p == NULL) ? "" :
61275750Sceastha G_p->g_rw_sysattr ? gettext(" System Attribute ") :
61285750Sceastha gettext(" Attribute "),
61295750Sceastha (G_p->g_attrnam_p == NULL) ? "" : nam_p);
61300Sstevel@tonic-gate }
61310Sstevel@tonic-gate
61320Sstevel@tonic-gate /* delete what we just built */
61330Sstevel@tonic-gate (void) unlinkat(dirfd, get_component(nam_p), 0);
61340Sstevel@tonic-gate
61350Sstevel@tonic-gate /* If the old file needs restoring, do the necessary links */
61360Sstevel@tonic-gate if (Do_rename) {
61370Sstevel@tonic-gate char *tmp_ptr;
61380Sstevel@tonic-gate
61390Sstevel@tonic-gate if (Args & OCp) {
61400Sstevel@tonic-gate tmp_ptr = Fullnam_p;
61410Sstevel@tonic-gate Fullnam_p = Over_p;
61420Sstevel@tonic-gate } else {
61430Sstevel@tonic-gate tmp_ptr = G_p->g_nam_p;
61440Sstevel@tonic-gate G_p->g_nam_p = Over_p;
61450Sstevel@tonic-gate }
61460Sstevel@tonic-gate Over_p = tmp_ptr;
61470Sstevel@tonic-gate
61480Sstevel@tonic-gate Do_rename = 0; /* names now have original values */
61490Sstevel@tonic-gate } else {
61500Sstevel@tonic-gate if (rename(Over_p, nam_p) < 0) {
61510Sstevel@tonic-gate if (link(Over_p, nam_p) < 0) {
61520Sstevel@tonic-gate msg(EXTN,
61530Sstevel@tonic-gate "Cannot recover original version"
61540Sstevel@tonic-gate " of \"%s%s%s\"",
61555750Sceastha (G_p->g_attrnam_p == NULL) ?
61560Sstevel@tonic-gate nam_p : Fullnam_p,
61575750Sceastha (G_p->g_attrnam_p == NULL) ? "" :
61585750Sceastha G_p->g_rw_sysattr ?
61595750Sceastha gettext(" System Attribute ") :
61605750Sceastha gettext(" Attribute "),
61615750Sceastha (G_p->g_attrnam_p == NULL) ?
61620Sstevel@tonic-gate "" : nam_p);
61630Sstevel@tonic-gate }
61640Sstevel@tonic-gate if (unlinkat(dirfd, get_component(Over_p), 0)) {
61650Sstevel@tonic-gate msg(ERRN,
61660Sstevel@tonic-gate "Cannot remove temp file "
61670Sstevel@tonic-gate "\"%s%s%s\"",
61685750Sceastha (G_p->g_attrnam_p == NULL) ?
61690Sstevel@tonic-gate Over_p : Fullnam_p,
61705750Sceastha (G_p->g_attrnam_p == NULL) ? "" :
61715750Sceastha G_p->g_rw_sysattr ?
61725750Sceastha gettext(" System Attribute ") :
61735750Sceastha gettext(" Attribute "),
61745750Sceastha (G_p->g_attrnam_p == NULL) ?
61750Sstevel@tonic-gate "" : Over_p);
61760Sstevel@tonic-gate }
61770Sstevel@tonic-gate }
61780Sstevel@tonic-gate }
61790Sstevel@tonic-gate *Over_p = '\0';
61800Sstevel@tonic-gate return;
61810Sstevel@tonic-gate } else if (over == U_OVER && *Over_p != '\0') {
61820Sstevel@tonic-gate if (Do_rename) {
61830Sstevel@tonic-gate char *tmp_ptr;
61840Sstevel@tonic-gate
61850Sstevel@tonic-gate (void) renameat(dirfd, get_component(nam_p),
61860Sstevel@tonic-gate dirfd, get_component(Over_p));
61870Sstevel@tonic-gate if (Args & OCp) {
61885750Sceastha if (G_p->g_attrnam_p == NULL) {
61890Sstevel@tonic-gate tmp_ptr = Fullnam_p;
61900Sstevel@tonic-gate Fullnam_p = Over_p;
61910Sstevel@tonic-gate Over_p = tmp_ptr;
61920Sstevel@tonic-gate } else {
61930Sstevel@tonic-gate /*
61940Sstevel@tonic-gate * Over_p is pointing at g_attrnam_p
61950Sstevel@tonic-gate * which must be preserved.
61960Sstevel@tonic-gate *
61970Sstevel@tonic-gate * We don't want the tmp_ptr and so
61980Sstevel@tonic-gate * on to throw away our only copy of
61990Sstevel@tonic-gate * the name.
62000Sstevel@tonic-gate */
62010Sstevel@tonic-gate Over_p = Attrfile_p;
62020Sstevel@tonic-gate }
62030Sstevel@tonic-gate } else {
62040Sstevel@tonic-gate tmp_ptr = G_p->g_nam_p;
62050Sstevel@tonic-gate G_p->g_nam_p = Over_p;
62060Sstevel@tonic-gate Over_p = tmp_ptr;
62070Sstevel@tonic-gate }
62080Sstevel@tonic-gate Do_rename = 0; /* names now have original values */
62090Sstevel@tonic-gate } else {
62100Sstevel@tonic-gate if (unlinkat(dirfd, get_component(Over_p), 0) < 0) {
62110Sstevel@tonic-gate msg(ERRN,
62120Sstevel@tonic-gate "Cannot unlink() temp file \"%s%s%s\"",
62135750Sceastha (G_p->g_attrnam_p == NULL) ?
62140Sstevel@tonic-gate Over_p : Fullnam_p,
62155750Sceastha (G_p->g_attrnam_p == NULL) ? "" :
62165750Sceastha G_p->g_rw_sysattr ?
62175750Sceastha gettext(" System Attribute ") :
62185750Sceastha gettext(" Attribute "),
62195750Sceastha (G_p->g_attrnam_p == NULL) ? "" : Over_p);
62200Sstevel@tonic-gate }
62210Sstevel@tonic-gate }
62220Sstevel@tonic-gate *Over_p = '\0';
62230Sstevel@tonic-gate }
62240Sstevel@tonic-gate if (Args & OCp) {
62255750Sceastha if (G_p->g_attrnam_p != NULL) {
62260Sstevel@tonic-gate inam_p = G_p->g_attrfnam_p;
62270Sstevel@tonic-gate onam_p = G_p->g_attrnam_p;
62280Sstevel@tonic-gate } else {
62290Sstevel@tonic-gate inam_p = Nam_p;
62300Sstevel@tonic-gate onam_p = Fullnam_p;
62310Sstevel@tonic-gate }
62320Sstevel@tonic-gate } else /* OCi only uses onam_p, OCo only uses inam_p */
62335750Sceastha if (G_p->g_attrnam_p != NULL) {
62340Sstevel@tonic-gate inam_p = onam_p = G_p->g_attrnam_p;
62350Sstevel@tonic-gate } else {
62360Sstevel@tonic-gate inam_p = onam_p = G_p->g_nam_p;
62370Sstevel@tonic-gate }
62380Sstevel@tonic-gate
62390Sstevel@tonic-gate /*
62401134Sceastha * Change the owner, time, and mode to those of the file
62411134Sceastha * originally created in the archive. Note: time and
62421134Sceastha * mode do not need to be restored for a symbolic link
62431134Sceastha * since rstfiles() is not called when the archived file
62441134Sceastha * is a symlink.
62450Sstevel@tonic-gate */
62460Sstevel@tonic-gate if (!(Args & OCo)) {
62471134Sceastha if (Args & OCR) {
62481134Sceastha if (fchownat(dirfd, get_component(onam_p),
62491134Sceastha Rpw_p->pw_uid, Rpw_p->pw_gid,
62501134Sceastha AT_SYMLINK_NOFOLLOW) < 0) {
62511134Sceastha msg(ERRN, "Cannot chown() \"%s%s%s\"",
62521134Sceastha onam_p,
62535750Sceastha (G_p->g_attrnam_p == NULL) ? "" :
62545750Sceastha G_p->g_rw_sysattr ?
62555750Sceastha gettext(" System Attribute ") :
62565750Sceastha gettext(" Attribute "),
62575750Sceastha (G_p->g_attrnam_p == NULL) ? "" : onam_p);
62581134Sceastha }
62591134Sceastha } else {
62601134Sceastha if ((fchownat(dirfd, get_component(onam_p),
62611134Sceastha G_p->g_uid, G_p->g_gid,
62621134Sceastha AT_SYMLINK_NOFOLLOW) < 0) && privileged) {
62631134Sceastha msg(ERRN, "Cannot chown() \"%s%s%s\"",
62641134Sceastha onam_p,
62655750Sceastha (G_p->g_attrnam_p == NULL) ? "" :
62665750Sceastha G_p->g_rw_sysattr ?
62675750Sceastha gettext(" System Attribute ") :
62685750Sceastha gettext(" Attribute "),
62695750Sceastha (G_p->g_attrnam_p == NULL) ? "" : onam_p);
62700Sstevel@tonic-gate }
62710Sstevel@tonic-gate }
62721134Sceastha
62731134Sceastha if (Args & OCm) {
62741134Sceastha set_tym(dirfd, get_component(onam_p),
62751134Sceastha G_p->g_mtime, G_p->g_mtime);
62761134Sceastha }
62771134Sceastha
62781134Sceastha /* Acl was not set, so we must chmod */
62791134Sceastha if (!acl_is_set) {
62801134Sceastha mode_t orig_mask, new_mask;
62811134Sceastha
62821134Sceastha /*
62831134Sceastha * use fchmod for attributes, since
62841134Sceastha * we known they are always regular
62851134Sceastha * files, whereas when it isn't an
62861134Sceastha * attribute it could be for a fifo
62871134Sceastha * or something other that we don't
62881134Sceastha * open and don't have a valid Ofile
62891134Sceastha * for.
62901134Sceastha */
62911134Sceastha if (privileged) {
62921134Sceastha new_mask = G_p->g_mode;
62931134Sceastha } else {
62941134Sceastha orig_mask = umask(0);
62951134Sceastha new_mask = G_p->g_mode & ~orig_mask;
62961134Sceastha }
62971134Sceastha
62985750Sceastha if (G_p->g_attrnam_p != NULL) {
62991134Sceastha error = fchmod(Ofile, new_mask);
63001134Sceastha } else {
63011134Sceastha error = chmod(onam_p, new_mask);
63021134Sceastha }
63031134Sceastha if (error < 0) {
63041134Sceastha msg(ERRN,
63051134Sceastha "Cannot chmod() \"%s%s%s\"",
63065750Sceastha (G_p->g_attrnam_p == NULL) ?
63071134Sceastha onam_p : G_p->g_attrfnam_p,
63085750Sceastha (G_p->g_attrnam_p == NULL) ? "" :
63095750Sceastha G_p->g_rw_sysattr ?
63105750Sceastha gettext(" System Attribute ") :
63115750Sceastha gettext(" Attribute "),
63125750Sceastha (G_p->g_attrnam_p == NULL) ? "" : onam_p);
63131134Sceastha }
63141134Sceastha if (!privileged) {
63151134Sceastha (void) umask(orig_mask);
63161134Sceastha }
63170Sstevel@tonic-gate }
63180Sstevel@tonic-gate }
63190Sstevel@tonic-gate
63200Sstevel@tonic-gate if (!(Args & OCi) && (Args & OCa)) {
63210Sstevel@tonic-gate /*
63220Sstevel@tonic-gate * Use dirfd since we are updating original file
63230Sstevel@tonic-gate * and not just created file
63240Sstevel@tonic-gate */
63250Sstevel@tonic-gate set_tym(G_p->g_dirfd, get_component(inam_p),
63260Sstevel@tonic-gate (ulong_t)SrcSt.st_atime, (ulong_t)SrcSt.st_mtime);
63270Sstevel@tonic-gate }
63280Sstevel@tonic-gate }
63290Sstevel@tonic-gate
63300Sstevel@tonic-gate /*
63310Sstevel@tonic-gate * scan4trail: Scan the archive looking for the trailer.
63320Sstevel@tonic-gate * When found, back the archive up over the trailer and overwrite
63330Sstevel@tonic-gate * the trailer with the files to be added to the archive.
63340Sstevel@tonic-gate */
63350Sstevel@tonic-gate
63360Sstevel@tonic-gate static void
scan4trail(void)63370Sstevel@tonic-gate scan4trail(void)
63380Sstevel@tonic-gate {
63390Sstevel@tonic-gate int rv;
63400Sstevel@tonic-gate off_t off1, off2;
63410Sstevel@tonic-gate
63420Sstevel@tonic-gate Append = 1;
63430Sstevel@tonic-gate Hdr_type = NONE;
63445750Sceastha G_p = NULL;
63450Sstevel@tonic-gate while (gethdr()) {
63460Sstevel@tonic-gate G_p = &Gen;
63470Sstevel@tonic-gate data_in(P_SKIP);
63480Sstevel@tonic-gate }
63490Sstevel@tonic-gate off1 = Buffr.b_cnt;
63500Sstevel@tonic-gate off2 = Bufsize - (Buffr.b_cnt % Bufsize);
63510Sstevel@tonic-gate Buffr.b_out_p = Buffr.b_in_p = Buffr.b_base_p;
63520Sstevel@tonic-gate Buffr.b_cnt = (off_t)0;
63530Sstevel@tonic-gate if (lseek(Archive, -(off1 + off2), SEEK_REL) < 0)
63540Sstevel@tonic-gate msg(EXTN, "Unable to append to this archive");
63550Sstevel@tonic-gate if ((rv = g_read(Device, Archive, Buffr.b_in_p, Bufsize)) < 0)
63560Sstevel@tonic-gate msg(EXTN, "Cannot append to this archive");
63570Sstevel@tonic-gate if (lseek(Archive, (off_t)-rv, SEEK_REL) < 0)
63580Sstevel@tonic-gate msg(EXTN, "Unable to append to this archive");
63590Sstevel@tonic-gate Buffr.b_cnt = off2;
63600Sstevel@tonic-gate Buffr.b_in_p = Buffr.b_base_p + Buffr.b_cnt;
63610Sstevel@tonic-gate Append = 0;
63620Sstevel@tonic-gate }
63630Sstevel@tonic-gate
63640Sstevel@tonic-gate /*
63650Sstevel@tonic-gate * setup: Perform setup and initialization functions. Parse the options
63660Sstevel@tonic-gate * using getopt(3C), call ckopts to check the options and initialize various
63670Sstevel@tonic-gate * structures and pointers. Specifically, for the -i option, save any
63680Sstevel@tonic-gate * patterns, for the -o option, check (via stat(2)) the archive, and for
63690Sstevel@tonic-gate * the -p option, validate the destination directory.
63700Sstevel@tonic-gate */
63710Sstevel@tonic-gate
63720Sstevel@tonic-gate static void
setup(int largc,char ** largv)63730Sstevel@tonic-gate setup(int largc, char **largv)
63740Sstevel@tonic-gate {
63750Sstevel@tonic-gate extern int optind;
63760Sstevel@tonic-gate extern char *optarg;
63770Sstevel@tonic-gate
63780Sstevel@tonic-gate #if defined(O_XATTR)
63795750Sceastha #if defined(_PC_SATTR_ENABLED)
63805750Sceastha #ifdef WAITAROUND
63815750Sceastha char *opts_p = "zabcdfiklmoprstuvABC:DE:H:I:LM:O:PR:SV6@/";
63825750Sceastha #else
63835750Sceastha char *opts_p = "abcdfiklmoprstuvABC:DE:H:I:LM:O:PR:SV6@/";
63845750Sceastha #endif /* WAITAROUND */
63855750Sceastha
63865750Sceastha #else /* _PC_SATTR_ENABLED */
63870Sstevel@tonic-gate #ifdef WAITAROUND
63880Sstevel@tonic-gate char *opts_p = "zabcdfiklmoprstuvABC:DE:H:I:LM:O:PR:SV6@";
63890Sstevel@tonic-gate #else
63900Sstevel@tonic-gate char *opts_p = "abcdfiklmoprstuvABC:DE:H:I:LM:O:PR:SV6@";
63915750Sceastha #endif /* WAITAROUND */
63925750Sceastha #endif /* _PC_SATTR_ENABLED */
63935750Sceastha
63945750Sceastha #else /* O_XATTR */
63950Sstevel@tonic-gate #ifdef WAITAROUND
63960Sstevel@tonic-gate char *opts_p = "zabcdfiklmoprstuvABC:DE:H:I:LM:O:PR:SV6";
63970Sstevel@tonic-gate #else
63980Sstevel@tonic-gate char *opts_p = "abcdfiklmoprstuvABC:DE:H:I:LM:O:PR:SV6";
63995750Sceastha #endif /* WAITAROUND */
64005750Sceastha #endif /* O_XATTR */
64010Sstevel@tonic-gate
64020Sstevel@tonic-gate char *dupl_p = "Only one occurrence of -%c allowed";
64030Sstevel@tonic-gate int option;
64040Sstevel@tonic-gate int blk_cnt, blk_cnt_max;
64050Sstevel@tonic-gate struct rlimit rlim;
64060Sstevel@tonic-gate
64070Sstevel@tonic-gate /* Remember the native page size. */
64080Sstevel@tonic-gate
64090Sstevel@tonic-gate PageSize = sysconf(_SC_PAGESIZE);
64100Sstevel@tonic-gate
64110Sstevel@tonic-gate if (PageSize == -1) {
64120Sstevel@tonic-gate /*
64130Sstevel@tonic-gate * This sysconf call will almost certainly never fail. The
64140Sstevel@tonic-gate * symbol PAGESIZE itself resolves to the above sysconf call,
64150Sstevel@tonic-gate * so we should go ahead and define our own constant.
64160Sstevel@tonic-gate */
64170Sstevel@tonic-gate PageSize = 8192;
64180Sstevel@tonic-gate }
64190Sstevel@tonic-gate
64200Sstevel@tonic-gate Hdr_type = BIN;
64210Sstevel@tonic-gate Max_offset = (off_t)(BIN_OFFSET_MAX);
64220Sstevel@tonic-gate Efil_p = Hdr_p = Own_p = IOfil_p = NULL;
64230Sstevel@tonic-gate while ((option = getopt(largc, largv, opts_p)) != EOF) {
64240Sstevel@tonic-gate switch (option) {
64250Sstevel@tonic-gate #ifdef WAITAROUND
64260Sstevel@tonic-gate case 'z':
64270Sstevel@tonic-gate /* rendezvous with the debugger */
64280Sstevel@tonic-gate waitaround = 1;
64290Sstevel@tonic-gate break;
64300Sstevel@tonic-gate #endif
64310Sstevel@tonic-gate case 'a': /* reset access time */
64320Sstevel@tonic-gate Args |= OCa;
64330Sstevel@tonic-gate break;
64340Sstevel@tonic-gate case 'b': /* swap bytes and halfwords */
64350Sstevel@tonic-gate Args |= OCb;
64360Sstevel@tonic-gate break;
64370Sstevel@tonic-gate case 'c': /* select character header */
64380Sstevel@tonic-gate Args |= OCc;
64390Sstevel@tonic-gate Hdr_type = ASC;
64400Sstevel@tonic-gate Max_namesz = APATH;
64410Sstevel@tonic-gate Onecopy = 1;
64420Sstevel@tonic-gate break;
64430Sstevel@tonic-gate case 'd': /* create directories as needed */
64440Sstevel@tonic-gate Args |= OCd;
64450Sstevel@tonic-gate break;
64460Sstevel@tonic-gate case 'f': /* select files not in patterns */
64470Sstevel@tonic-gate Args |= OCf;
64480Sstevel@tonic-gate break;
64490Sstevel@tonic-gate case 'i': /* "copy in" */
64500Sstevel@tonic-gate Args |= OCi;
64510Sstevel@tonic-gate Archive = 0;
64520Sstevel@tonic-gate break;
64530Sstevel@tonic-gate case 'k': /* retry after I/O errors */
64540Sstevel@tonic-gate Args |= OCk;
64550Sstevel@tonic-gate break;
64560Sstevel@tonic-gate case 'l': /* link files when possible */
64570Sstevel@tonic-gate Args |= OCl;
64580Sstevel@tonic-gate break;
64590Sstevel@tonic-gate case 'm': /* retain modification time */
64600Sstevel@tonic-gate Args |= OCm;
64610Sstevel@tonic-gate break;
64620Sstevel@tonic-gate case 'o': /* "copy out" */
64630Sstevel@tonic-gate Args |= OCo;
64640Sstevel@tonic-gate Archive = 1;
64650Sstevel@tonic-gate break;
64660Sstevel@tonic-gate case 'p': /* "pass" */
64670Sstevel@tonic-gate Max_namesz = APATH;
64680Sstevel@tonic-gate Args |= OCp;
64690Sstevel@tonic-gate break;
64700Sstevel@tonic-gate case 'r': /* rename files interactively */
64710Sstevel@tonic-gate Args |= OCr;
64720Sstevel@tonic-gate break;
64730Sstevel@tonic-gate case 's': /* swap bytes */
64740Sstevel@tonic-gate Args |= OCs;
64750Sstevel@tonic-gate break;
64760Sstevel@tonic-gate case 't': /* table of contents */
64770Sstevel@tonic-gate Args |= OCt;
64780Sstevel@tonic-gate break;
64790Sstevel@tonic-gate case 'u': /* copy unconditionally */
64800Sstevel@tonic-gate Args |= OCu;
64810Sstevel@tonic-gate break;
64820Sstevel@tonic-gate case 'v': /* verbose - print file names */
64830Sstevel@tonic-gate Args |= OCv;
64840Sstevel@tonic-gate break;
64850Sstevel@tonic-gate case 'A': /* append to existing archive */
64860Sstevel@tonic-gate Args |= OCA;
64870Sstevel@tonic-gate break;
64880Sstevel@tonic-gate case 'B': /* set block size to 5120 bytes */
64890Sstevel@tonic-gate Args |= OCB;
64900Sstevel@tonic-gate Bufsize = 5120;
64910Sstevel@tonic-gate break;
64920Sstevel@tonic-gate case 'C': /* set arbitrary block size */
64930Sstevel@tonic-gate if (Args & OCC)
64940Sstevel@tonic-gate msg(ERR, dupl_p, 'C');
64950Sstevel@tonic-gate else {
64960Sstevel@tonic-gate Args |= OCC;
64970Sstevel@tonic-gate Bufsize = atoi(optarg);
64980Sstevel@tonic-gate }
64990Sstevel@tonic-gate break;
65000Sstevel@tonic-gate case 'D':
65010Sstevel@tonic-gate Dflag = 1;
65020Sstevel@tonic-gate break;
65030Sstevel@tonic-gate case 'E': /* alternate file for pattern input */
65040Sstevel@tonic-gate if (Args & OCE)
65050Sstevel@tonic-gate msg(ERR, dupl_p, 'E');
65060Sstevel@tonic-gate else {
65070Sstevel@tonic-gate Args |= OCE;
65080Sstevel@tonic-gate Efil_p = optarg;
65090Sstevel@tonic-gate }
65100Sstevel@tonic-gate break;
65110Sstevel@tonic-gate case 'H': /* select header type */
65120Sstevel@tonic-gate if (Args & OCH)
65130Sstevel@tonic-gate msg(ERR, dupl_p, 'H');
65140Sstevel@tonic-gate else {
65150Sstevel@tonic-gate Args |= OCH;
65160Sstevel@tonic-gate Hdr_p = optarg;
65170Sstevel@tonic-gate }
65180Sstevel@tonic-gate break;
65190Sstevel@tonic-gate case 'I': /* alternate file for archive input */
65200Sstevel@tonic-gate if (Args & OCI)
65210Sstevel@tonic-gate msg(ERR, dupl_p, 'I');
65220Sstevel@tonic-gate else {
65230Sstevel@tonic-gate Args |= OCI;
65240Sstevel@tonic-gate IOfil_p = optarg;
65250Sstevel@tonic-gate }
65260Sstevel@tonic-gate break;
65270Sstevel@tonic-gate case 'L': /* follow symbolic links */
65280Sstevel@tonic-gate Args |= OCL;
65290Sstevel@tonic-gate break;
65300Sstevel@tonic-gate case 'M': /* specify new end-of-media message */
65310Sstevel@tonic-gate if (Args & OCM)
65320Sstevel@tonic-gate msg(ERR, dupl_p, 'M');
65330Sstevel@tonic-gate else {
65340Sstevel@tonic-gate Args |= OCM;
65350Sstevel@tonic-gate Eom_p = optarg;
65360Sstevel@tonic-gate }
65370Sstevel@tonic-gate break;
65380Sstevel@tonic-gate case 'O': /* alternate file for archive output */
65390Sstevel@tonic-gate if (Args & OCO)
65400Sstevel@tonic-gate msg(ERR, dupl_p, 'O');
65410Sstevel@tonic-gate else {
65420Sstevel@tonic-gate Args |= OCO;
65430Sstevel@tonic-gate IOfil_p = optarg;
65440Sstevel@tonic-gate }
65450Sstevel@tonic-gate break;
65460Sstevel@tonic-gate case 'P': /* preserve acls */
65470Sstevel@tonic-gate Args |= OCP;
65480Sstevel@tonic-gate Pflag++;
65490Sstevel@tonic-gate break;
65500Sstevel@tonic-gate case 'R': /* change owner/group of files */
65510Sstevel@tonic-gate if (Args & OCR)
65520Sstevel@tonic-gate msg(ERR, dupl_p, 'R');
65530Sstevel@tonic-gate else {
65540Sstevel@tonic-gate Args |= OCR;
65550Sstevel@tonic-gate Own_p = optarg;
65560Sstevel@tonic-gate }
65570Sstevel@tonic-gate break;
65580Sstevel@tonic-gate case 'S': /* swap halfwords */
65590Sstevel@tonic-gate Args |= OCS;
65600Sstevel@tonic-gate break;
65610Sstevel@tonic-gate case 'V': /* print a dot '.' for each file */
65620Sstevel@tonic-gate Args |= OCV;
65630Sstevel@tonic-gate break;
65640Sstevel@tonic-gate case '6': /* for old, sixth-edition files */
65650Sstevel@tonic-gate Args |= OC6;
65660Sstevel@tonic-gate Ftype = SIXTH;
65670Sstevel@tonic-gate break;
65680Sstevel@tonic-gate #if defined(O_XATTR)
65690Sstevel@tonic-gate case '@':
65700Sstevel@tonic-gate Atflag++;
65710Sstevel@tonic-gate break;
65725750Sceastha #if defined(_PC_SATTR_ENABLED)
65735750Sceastha case '/':
65745750Sceastha SysAtflag++;
65755750Sceastha break;
65765750Sceastha #endif /* _PC_SATTR_ENABLED */
65775750Sceastha #endif /* O_XATTR */
65780Sstevel@tonic-gate default:
65790Sstevel@tonic-gate Error_cnt++;
65800Sstevel@tonic-gate } /* option */
65810Sstevel@tonic-gate } /* (option = getopt(largc, largv, opts_p)) != EOF */
65820Sstevel@tonic-gate
65830Sstevel@tonic-gate #ifdef WAITAROUND
65840Sstevel@tonic-gate if (waitaround) {
65850Sstevel@tonic-gate (void) fprintf(stderr, gettext("Rendezvous with cpio on pid"
65860Sstevel@tonic-gate " %d\n"), getpid());
65870Sstevel@tonic-gate
65880Sstevel@tonic-gate while (waitaround) {
65890Sstevel@tonic-gate (void) sleep(10);
65900Sstevel@tonic-gate }
65910Sstevel@tonic-gate }
65920Sstevel@tonic-gate #endif
65930Sstevel@tonic-gate
65940Sstevel@tonic-gate largc -= optind;
65950Sstevel@tonic-gate largv += optind;
65960Sstevel@tonic-gate ckopts(Args);
65970Sstevel@tonic-gate if (!Error_cnt) {
65980Sstevel@tonic-gate if (Args & OCr) {
65990Sstevel@tonic-gate Renam_p = e_zalloc(E_EXIT, APATH + 1);
66000Sstevel@tonic-gate Renametmp_p = e_zalloc(E_EXIT, APATH + 1);
66015750Sceastha #if defined(_PC_SATTR_ENABLED)
66025750Sceastha Renam_attr_p = e_zalloc(E_EXIT, APATH + 1);
66035750Sceastha #endif
66040Sstevel@tonic-gate }
66050Sstevel@tonic-gate Symlnk_p = e_zalloc(E_EXIT, APATH);
66060Sstevel@tonic-gate Over_p = e_zalloc(E_EXIT, APATH);
66070Sstevel@tonic-gate Nam_p = e_zalloc(E_EXIT, APATH + 1);
66080Sstevel@tonic-gate if (Args & OCp) {
66090Sstevel@tonic-gate Savenam_p = e_zalloc(E_EXIT, APATH + 1);
66100Sstevel@tonic-gate }
66110Sstevel@tonic-gate Fullnam_p = e_zalloc(E_EXIT, APATH);
66120Sstevel@tonic-gate Lnknam_p = e_zalloc(E_EXIT, APATH);
66130Sstevel@tonic-gate Gen.g_nam_p = Nam_p;
66145750Sceastha if ((Fullnam_p = getcwd(NULL, APATH)) == NULL)
66150Sstevel@tonic-gate msg(EXT, "Unable to determine current directory.");
66160Sstevel@tonic-gate if (Args & OCi) {
66170Sstevel@tonic-gate if (largc > 0) /* save patterns for -i option, if any */
66180Sstevel@tonic-gate Pat_pp = largv;
66190Sstevel@tonic-gate if (Args & OCE)
66200Sstevel@tonic-gate getpats(largc, largv);
66210Sstevel@tonic-gate } else if (Args & OCo) {
66220Sstevel@tonic-gate if (largc != 0) /* error if arguments left with -o */
66230Sstevel@tonic-gate Error_cnt++;
66240Sstevel@tonic-gate else if (fstat(Archive, &ArchSt) < 0)
66250Sstevel@tonic-gate msg(ERRN, "Error during stat() of archive");
66260Sstevel@tonic-gate switch (Hdr_type) {
66270Sstevel@tonic-gate case BIN:
66280Sstevel@tonic-gate Hdrsz = HDRSZ;
66290Sstevel@tonic-gate Pad_val = HALFWD;
66300Sstevel@tonic-gate break;
66310Sstevel@tonic-gate case CHR:
66320Sstevel@tonic-gate Hdrsz = CHRSZ;
66330Sstevel@tonic-gate Pad_val = 0;
66340Sstevel@tonic-gate Max_offset = (off_t)(CHAR_OFFSET_MAX);
66350Sstevel@tonic-gate break;
66360Sstevel@tonic-gate case ASC:
66370Sstevel@tonic-gate case CRC:
66380Sstevel@tonic-gate Hdrsz = ASCSZ;
66390Sstevel@tonic-gate Pad_val = FULLWD;
664036Schin Max_offset = (off_t)(ASC_OFFSET_MAX);
66410Sstevel@tonic-gate break;
66420Sstevel@tonic-gate case TAR:
66430Sstevel@tonic-gate /* FALLTHROUGH */
66440Sstevel@tonic-gate case USTAR: /* TAR and USTAR */
66450Sstevel@tonic-gate Hdrsz = TARSZ;
66460Sstevel@tonic-gate Pad_val = FULLBK;
66470Sstevel@tonic-gate Max_offset = (off_t)(CHAR_OFFSET_MAX);
66480Sstevel@tonic-gate break;
66490Sstevel@tonic-gate default:
66500Sstevel@tonic-gate msg(EXT, "Impossible header type.");
66510Sstevel@tonic-gate }
66520Sstevel@tonic-gate } else { /* directory must be specified */
66530Sstevel@tonic-gate if (largc != 1)
66540Sstevel@tonic-gate Error_cnt++;
66554301Sjs198686 else if (access(*largv, 2) < 0 && (errno != EACCES))
66564301Sjs198686 /*
66574301Sjs198686 * EACCES is ignored here as it may occur
66584301Sjs198686 * when any directory component of the path
66594301Sjs198686 * does not have write permission, even though
66604301Sjs198686 * the destination subdirectory has write
66614301Sjs198686 * access. Writing to a read only directory
66624301Sjs198686 * is handled later, as in "copy in" mode.
66634301Sjs198686 */
66640Sstevel@tonic-gate msg(ERRN,
66650Sstevel@tonic-gate "Error during access() of \"%s\"", *largv);
66660Sstevel@tonic-gate }
66670Sstevel@tonic-gate }
66680Sstevel@tonic-gate if (Error_cnt)
66690Sstevel@tonic-gate usage(); /* exits! */
66700Sstevel@tonic-gate if (Args & (OCi | OCo)) {
66710Sstevel@tonic-gate if (!Dflag) {
66720Sstevel@tonic-gate if (Args & (OCB | OCC)) {
66730Sstevel@tonic-gate if (g_init(&Device, &Archive) < 0)
66740Sstevel@tonic-gate msg(EXTN,
66750Sstevel@tonic-gate "Error during initialization");
66760Sstevel@tonic-gate } else {
66770Sstevel@tonic-gate if ((Bufsize = g_init(&Device, &Archive)) < 0)
66780Sstevel@tonic-gate msg(EXTN,
66790Sstevel@tonic-gate "Error during initialization");
66800Sstevel@tonic-gate }
66810Sstevel@tonic-gate }
66820Sstevel@tonic-gate
66830Sstevel@tonic-gate blk_cnt_max = _20K / Bufsize;
66840Sstevel@tonic-gate if (blk_cnt_max < MX_BUFS) {
66850Sstevel@tonic-gate blk_cnt_max = MX_BUFS;
66860Sstevel@tonic-gate }
66870Sstevel@tonic-gate
66880Sstevel@tonic-gate Buffr.b_base_p = NULL;
66890Sstevel@tonic-gate
66900Sstevel@tonic-gate for (blk_cnt = blk_cnt_max; blk_cnt > 1; blk_cnt--) {
66910Sstevel@tonic-gate Buffr.b_size = (size_t)(Bufsize * blk_cnt);
66920Sstevel@tonic-gate Buffr.b_base_p = e_valloc(E_NORMAL, Buffr.b_size);
66930Sstevel@tonic-gate if (Buffr.b_base_p != NULL) {
66940Sstevel@tonic-gate break;
66950Sstevel@tonic-gate }
66960Sstevel@tonic-gate }
66970Sstevel@tonic-gate if (Buffr.b_base_p == NULL || Buffr.b_size < (2 * CPIOBSZ)) {
66980Sstevel@tonic-gate msg(EXT, "Out of memory");
66990Sstevel@tonic-gate }
67000Sstevel@tonic-gate
67010Sstevel@tonic-gate Buffr.b_out_p = Buffr.b_in_p = Buffr.b_base_p;
67020Sstevel@tonic-gate Buffr.b_cnt = 0L;
67030Sstevel@tonic-gate Buffr.b_end_p = Buffr.b_base_p + Buffr.b_size;
67040Sstevel@tonic-gate }
67050Sstevel@tonic-gate
67060Sstevel@tonic-gate /*
67070Sstevel@tonic-gate * Now that Bufsize has stabilized, we can allocate our i/o buffer
67080Sstevel@tonic-gate */
67090Sstevel@tonic-gate Buf_p = e_valloc(E_EXIT, Bufsize);
67100Sstevel@tonic-gate
67110Sstevel@tonic-gate if (Args & OCp) { /* get destination directory */
67120Sstevel@tonic-gate (void) strcpy(Fullnam_p, *largv);
67130Sstevel@tonic-gate if (stat(Fullnam_p, &DesSt) < 0)
67140Sstevel@tonic-gate msg(EXTN, "Error during stat() of \"%s\"", Fullnam_p);
67150Sstevel@tonic-gate if ((DesSt.st_mode & Ftype) != S_IFDIR)
67160Sstevel@tonic-gate msg(EXT, "\"%s\" is not a directory", Fullnam_p);
67170Sstevel@tonic-gate }
67180Sstevel@tonic-gate Full_p = Fullnam_p + strlen(Fullnam_p) - 1;
67190Sstevel@tonic-gate if (*Full_p != '/') {
67200Sstevel@tonic-gate Full_p++;
67210Sstevel@tonic-gate *Full_p = '/';
67220Sstevel@tonic-gate }
67230Sstevel@tonic-gate Full_p++;
67240Sstevel@tonic-gate *Full_p = '\0';
67250Sstevel@tonic-gate (void) strcpy(Lnknam_p, Fullnam_p);
67260Sstevel@tonic-gate Lnkend_p = Lnknam_p + strlen(Lnknam_p);
67270Sstevel@tonic-gate (void) getrlimit(RLIMIT_FSIZE, &rlim);
67280Sstevel@tonic-gate Max_filesz = (off_t)rlim.rlim_cur;
67290Sstevel@tonic-gate Lnk_hd.L_nxt_p = Lnk_hd.L_bck_p = &Lnk_hd;
67305750Sceastha Lnk_hd.L_lnk_p = NULL;
67310Sstevel@tonic-gate }
67320Sstevel@tonic-gate
67330Sstevel@tonic-gate /*
67340Sstevel@tonic-gate * set_tym: Set the access and/or modification times for a file.
67350Sstevel@tonic-gate */
67360Sstevel@tonic-gate
67370Sstevel@tonic-gate static void
set_tym(int dirfd,char * nam_p,time_t atime,time_t mtime)67380Sstevel@tonic-gate set_tym(int dirfd, char *nam_p, time_t atime, time_t mtime)
67390Sstevel@tonic-gate {
67400Sstevel@tonic-gate struct timeval times[2];
67410Sstevel@tonic-gate
67420Sstevel@tonic-gate times[0].tv_sec = atime;
67430Sstevel@tonic-gate times[0].tv_usec = 0;
67440Sstevel@tonic-gate times[1].tv_sec = mtime;
67450Sstevel@tonic-gate times[1].tv_usec = 0;
67460Sstevel@tonic-gate
67470Sstevel@tonic-gate if (futimesat(dirfd, nam_p, times) < 0) {
67480Sstevel@tonic-gate if (Args & OCa) {
67490Sstevel@tonic-gate msg(ERRN,
67500Sstevel@tonic-gate "Unable to reset access time for \"%s%s%s\"",
67515750Sceastha (G_p->g_attrnam_p == NULL) ? nam_p : Fullnam_p,
67525750Sceastha (G_p->g_attrnam_p == NULL) ? "" :
67535750Sceastha G_p->g_rw_sysattr ? gettext(" System Attribute ") :
67545750Sceastha gettext(" Attribute "),
67555750Sceastha (G_p->g_attrnam_p == NULL) ? "" : nam_p);
67560Sstevel@tonic-gate } else {
67570Sstevel@tonic-gate msg(ERRN,
67580Sstevel@tonic-gate "Unable to reset modification time for \"%s%s%s\"",
67595750Sceastha (G_p->g_attrnam_p == NULL) ? nam_p : Fullnam_p,
67605750Sceastha (G_p->g_attrnam_p == NULL) ? "" :
67615750Sceastha G_p->g_rw_sysattr ? gettext(" System Attribute ") :
67625750Sceastha gettext(" Attribute "),
67635750Sceastha (G_p->g_attrnam_p == NULL) ? "" : nam_p);
67640Sstevel@tonic-gate }
67650Sstevel@tonic-gate }
67660Sstevel@tonic-gate }
67670Sstevel@tonic-gate
67680Sstevel@tonic-gate /*
67690Sstevel@tonic-gate * sigint: Catch interrupts. If an interrupt occurs during the extraction
67700Sstevel@tonic-gate * of a file from the archive with the -u option set, and the filename did
67710Sstevel@tonic-gate * exist, remove the current file and restore the original file. Then exit.
67720Sstevel@tonic-gate */
67730Sstevel@tonic-gate
67740Sstevel@tonic-gate /*ARGSUSED*/
67750Sstevel@tonic-gate static void
sigint(int sig)67760Sstevel@tonic-gate sigint(int sig)
67770Sstevel@tonic-gate {
67780Sstevel@tonic-gate char *nam_p;
67790Sstevel@tonic-gate
67800Sstevel@tonic-gate (void) signal(SIGINT, SIG_IGN); /* block further signals */
67810Sstevel@tonic-gate if (!Finished) {
67820Sstevel@tonic-gate if (Args & OCi)
67830Sstevel@tonic-gate nam_p = G_p->g_nam_p;
67840Sstevel@tonic-gate else /* OCp */
67850Sstevel@tonic-gate nam_p = Fullnam_p;
67860Sstevel@tonic-gate if (*Over_p != '\0') { /* There is a temp file */
67870Sstevel@tonic-gate if (unlink(nam_p) < 0) {
67880Sstevel@tonic-gate msg(ERRN,
67890Sstevel@tonic-gate "Cannot remove incomplete \"%s\"", nam_p);
67900Sstevel@tonic-gate }
67910Sstevel@tonic-gate if (rename(Over_p, nam_p) < 0) {
67920Sstevel@tonic-gate if (link(Over_p, nam_p) < 0) {
67930Sstevel@tonic-gate msg(ERRN,
67940Sstevel@tonic-gate "Cannot recover original \"%s\"",
67950Sstevel@tonic-gate nam_p);
67960Sstevel@tonic-gate }
67970Sstevel@tonic-gate if (unlink(Over_p)) {
67980Sstevel@tonic-gate msg(ERRN,
67990Sstevel@tonic-gate "Cannot remove temp file \"%s\"",
68000Sstevel@tonic-gate Over_p);
68010Sstevel@tonic-gate }
68020Sstevel@tonic-gate }
68030Sstevel@tonic-gate } else if (unlink(nam_p))
68040Sstevel@tonic-gate msg(ERRN,
68050Sstevel@tonic-gate "Cannot remove incomplete \"%s\"", nam_p);
68060Sstevel@tonic-gate *Over_p = '\0';
68070Sstevel@tonic-gate }
68080Sstevel@tonic-gate exit(EXIT_CODE);
68090Sstevel@tonic-gate }
68100Sstevel@tonic-gate
68110Sstevel@tonic-gate /*
68120Sstevel@tonic-gate * swap: Swap bytes (-s), halfwords (-S) or or both halfwords and bytes (-b).
68130Sstevel@tonic-gate */
68140Sstevel@tonic-gate
68150Sstevel@tonic-gate static void
swap(char * buf_p,int cnt)68160Sstevel@tonic-gate swap(char *buf_p, int cnt)
68170Sstevel@tonic-gate {
68180Sstevel@tonic-gate unsigned char tbyte;
68190Sstevel@tonic-gate int tcnt;
68200Sstevel@tonic-gate int rcnt;
68210Sstevel@tonic-gate ushort_t thalf;
68220Sstevel@tonic-gate
68230Sstevel@tonic-gate rcnt = cnt % 4;
68240Sstevel@tonic-gate cnt /= 4;
68250Sstevel@tonic-gate if (Args & (OCb | OCs | BSM)) {
68260Sstevel@tonic-gate tcnt = cnt;
68270Sstevel@tonic-gate /* LINTED alignment */
68280Sstevel@tonic-gate Swp_p = (union swpbuf *)buf_p;
68290Sstevel@tonic-gate while (tcnt-- > 0) {
68300Sstevel@tonic-gate tbyte = Swp_p->s_byte[0];
68310Sstevel@tonic-gate Swp_p->s_byte[0] = Swp_p->s_byte[1];
68320Sstevel@tonic-gate Swp_p->s_byte[1] = tbyte;
68330Sstevel@tonic-gate tbyte = Swp_p->s_byte[2];
68340Sstevel@tonic-gate Swp_p->s_byte[2] = Swp_p->s_byte[3];
68350Sstevel@tonic-gate Swp_p->s_byte[3] = tbyte;
68360Sstevel@tonic-gate Swp_p++;
68370Sstevel@tonic-gate }
68380Sstevel@tonic-gate if (rcnt >= 2) {
68390Sstevel@tonic-gate tbyte = Swp_p->s_byte[0];
68400Sstevel@tonic-gate Swp_p->s_byte[0] = Swp_p->s_byte[1];
68410Sstevel@tonic-gate Swp_p->s_byte[1] = tbyte;
68420Sstevel@tonic-gate tbyte = Swp_p->s_byte[2];
68430Sstevel@tonic-gate }
68440Sstevel@tonic-gate }
68450Sstevel@tonic-gate if (Args & (OCb | OCS)) {
68460Sstevel@tonic-gate tcnt = cnt;
68470Sstevel@tonic-gate /* LINTED alignment */
68480Sstevel@tonic-gate Swp_p = (union swpbuf *)buf_p;
68490Sstevel@tonic-gate while (tcnt-- > 0) {
68500Sstevel@tonic-gate thalf = Swp_p->s_half[0];
68510Sstevel@tonic-gate Swp_p->s_half[0] = Swp_p->s_half[1];
68520Sstevel@tonic-gate Swp_p->s_half[1] = thalf;
68530Sstevel@tonic-gate Swp_p++;
68540Sstevel@tonic-gate }
68550Sstevel@tonic-gate }
68560Sstevel@tonic-gate }
68570Sstevel@tonic-gate
68580Sstevel@tonic-gate /*
68590Sstevel@tonic-gate * usage: Print the usage message on stderr and exit.
68600Sstevel@tonic-gate */
68610Sstevel@tonic-gate
68620Sstevel@tonic-gate static void
usage(void)68630Sstevel@tonic-gate usage(void)
68640Sstevel@tonic-gate {
68650Sstevel@tonic-gate
68660Sstevel@tonic-gate (void) fflush(stdout);
68670Sstevel@tonic-gate #if defined(O_XATTR)
68680Sstevel@tonic-gate (void) fprintf(stderr, gettext("USAGE:\n"
68691134Sceastha "\tcpio -i[bcdfkmrstuv@BSV6] [-C size] "
68701134Sceastha "[-E file] [-H hdr] [-I file [-M msg]] "
68711134Sceastha "[-R id] [patterns]\n"
68721134Sceastha "\tcpio -o[acv@ABLV] [-C size] "
68731134Sceastha "[-H hdr] [-O file [-M msg]]\n"
68741134Sceastha "\tcpio -p[adlmuv@LV] [-R id] directory\n"));
68750Sstevel@tonic-gate #else
68760Sstevel@tonic-gate (void) fprintf(stderr, gettext("USAGE:\n"
68771134Sceastha "\tcpio -i[bcdfkmrstuvBSV6] [-C size] "
68781134Sceastha "[-E file] [-H hdr] [-I file [-M msg]] "
68791134Sceastha "[-R id] [patterns]\n"
68801134Sceastha "\tcpio -o[acvABLV] [-C size] "
68811134Sceastha "[-H hdr] [-O file [-M msg]]\n"
68821134Sceastha "\tcpio -p[adlmuvLV] [-R id] directory\n"));
68830Sstevel@tonic-gate #endif
68840Sstevel@tonic-gate (void) fflush(stderr);
68850Sstevel@tonic-gate exit(EXIT_CODE);
68860Sstevel@tonic-gate }
68870Sstevel@tonic-gate
68880Sstevel@tonic-gate /*
68890Sstevel@tonic-gate * verbose: For each file, print either the filename (-v) or a dot (-V).
68900Sstevel@tonic-gate * If the -t option (table of contents) is set, print either the filename,
68910Sstevel@tonic-gate * or if the -v option is also set, print an "ls -l"-like listing.
68920Sstevel@tonic-gate */
68930Sstevel@tonic-gate
68940Sstevel@tonic-gate static void
verbose(char * nam_p)68950Sstevel@tonic-gate verbose(char *nam_p)
68960Sstevel@tonic-gate {
68970Sstevel@tonic-gate int i, j, temp;
68980Sstevel@tonic-gate mode_t mode;
68990Sstevel@tonic-gate char modestr[12];
690010202SNobutomo.Nakano@Sun.COM time_t ttime;
69010Sstevel@tonic-gate
69020Sstevel@tonic-gate /*
69030Sstevel@tonic-gate * The printf format and associated arguments to print the current
69040Sstevel@tonic-gate * filename. Normally, just nam_p. If we're processing an extended
69050Sstevel@tonic-gate * attribute, these are overridden.
69060Sstevel@tonic-gate */
69070Sstevel@tonic-gate char *name_fmt = "%s";
69080Sstevel@tonic-gate const char *name = nam_p;
69090Sstevel@tonic-gate const char *attribute = NULL;
69100Sstevel@tonic-gate
69115750Sceastha if (Gen.g_attrnam_p != NULL) {
69120Sstevel@tonic-gate /*
69130Sstevel@tonic-gate * Translation note:
69140Sstevel@tonic-gate * 'attribute' is a noun.
69150Sstevel@tonic-gate */
69165331Samw
69175750Sceastha if (Gen.g_rw_sysattr) {
69185750Sceastha name_fmt = gettext("%s system attribute %s");
69195750Sceastha } else if ((Args & OCt) &&
69205750Sceastha (is_sysattr(basename(Gen.g_attrnam_p)))) {
69215331Samw name_fmt = gettext("%s system attribute %s");
69225331Samw } else {
69235331Samw name_fmt = gettext("%s attribute %s");
69245331Samw }
69250Sstevel@tonic-gate
69260Sstevel@tonic-gate name = (Args & OCp) ? nam_p : Gen.g_attrfnam_p;
69275750Sceastha if (Gen.g_attrparent_p == NULL) {
69285750Sceastha attribute = Gen.g_attrnam_p;
69295750Sceastha } else {
69305750Sceastha attribute = Gen.g_attrpath_p;
69315750Sceastha }
69320Sstevel@tonic-gate }
69330Sstevel@tonic-gate
69340Sstevel@tonic-gate if ((Gen.g_mode == SECMODE) || ((Hdr_type == USTAR ||
69350Sstevel@tonic-gate Hdr_type == TAR) && Thdr_p->tbuf.t_typeflag == 'A')) {
69360Sstevel@tonic-gate /* dont print ancillary file */
69370Sstevel@tonic-gate aclchar = '+';
69380Sstevel@tonic-gate return;
69390Sstevel@tonic-gate }
69400Sstevel@tonic-gate for (i = 0; i < 11; i++)
69410Sstevel@tonic-gate modestr[i] = '-';
69420Sstevel@tonic-gate modestr[i] = '\0';
69430Sstevel@tonic-gate modestr[i-1] = aclchar;
69440Sstevel@tonic-gate aclchar = ' ';
69450Sstevel@tonic-gate
69460Sstevel@tonic-gate if ((Args & OCt) && (Args & OCv)) {
69470Sstevel@tonic-gate mode = Gen.g_mode;
69480Sstevel@tonic-gate for (i = 0; i < 3; i++) {
69490Sstevel@tonic-gate temp = (mode >> (6 - (i * 3)));
69500Sstevel@tonic-gate j = (i * 3) + 1;
69510Sstevel@tonic-gate if (S_IROTH & temp)
69520Sstevel@tonic-gate modestr[j] = 'r';
69530Sstevel@tonic-gate if (S_IWOTH & temp)
69540Sstevel@tonic-gate modestr[j + 1] = 'w';
69550Sstevel@tonic-gate if (S_IXOTH & temp)
69560Sstevel@tonic-gate modestr[j + 2] = 'x';
69570Sstevel@tonic-gate }
69580Sstevel@tonic-gate
69590Sstevel@tonic-gate if (Hdr_type != BAR) {
69600Sstevel@tonic-gate temp = Gen.g_mode & Ftype;
69610Sstevel@tonic-gate switch (temp) {
69620Sstevel@tonic-gate case (S_IFIFO):
69630Sstevel@tonic-gate modestr[0] = 'p';
69640Sstevel@tonic-gate break;
69655750Sceastha case (S_IFSOCK):
69665750Sceastha modestr[0] = 's';
69675750Sceastha break;
69680Sstevel@tonic-gate case (S_IFCHR):
69690Sstevel@tonic-gate modestr[0] = 'c';
69700Sstevel@tonic-gate break;
69710Sstevel@tonic-gate case (S_IFDIR):
69720Sstevel@tonic-gate modestr[0] = 'd';
69730Sstevel@tonic-gate break;
69740Sstevel@tonic-gate case (S_IFBLK):
69750Sstevel@tonic-gate modestr[0] = 'b';
69760Sstevel@tonic-gate break;
69770Sstevel@tonic-gate case (S_IFREG): /* was initialized to '-' */
69780Sstevel@tonic-gate break;
69790Sstevel@tonic-gate case (S_IFLNK):
69800Sstevel@tonic-gate modestr[0] = 'l';
69810Sstevel@tonic-gate break;
69820Sstevel@tonic-gate default:
69830Sstevel@tonic-gate msg(ERR, "Impossible file type");
69840Sstevel@tonic-gate }
69850Sstevel@tonic-gate } else { /* bar */
69860Sstevel@tonic-gate temp = Gen.g_mode & Ftype;
69870Sstevel@tonic-gate switch (temp) {
69880Sstevel@tonic-gate case (S_IFIFO):
69890Sstevel@tonic-gate modestr[0] = 'p';
69900Sstevel@tonic-gate break;
69915750Sceastha case (S_IFSOCK):
69925750Sceastha modestr[0] = 's';
69935750Sceastha break;
69940Sstevel@tonic-gate case (S_IFCHR):
69950Sstevel@tonic-gate modestr[0] = 'c';
69960Sstevel@tonic-gate break;
69970Sstevel@tonic-gate case (S_IFDIR):
69980Sstevel@tonic-gate modestr[0] = 'd';
69990Sstevel@tonic-gate break;
70000Sstevel@tonic-gate case (S_IFBLK):
70010Sstevel@tonic-gate modestr[0] = 'b';
70020Sstevel@tonic-gate break;
70030Sstevel@tonic-gate }
70040Sstevel@tonic-gate if (bar_linkflag == SYMTYPE)
70050Sstevel@tonic-gate modestr[0] = 'l';
70060Sstevel@tonic-gate }
70070Sstevel@tonic-gate if ((S_ISUID & Gen.g_mode) == S_ISUID)
70080Sstevel@tonic-gate modestr[3] = 's';
70090Sstevel@tonic-gate if ((S_ISVTX & Gen.g_mode) == S_ISVTX)
70100Sstevel@tonic-gate modestr[9] = 't';
70110Sstevel@tonic-gate if ((S_ISGID & G_p->g_mode) == S_ISGID && modestr[6] == 'x')
70120Sstevel@tonic-gate modestr[6] = 's';
70130Sstevel@tonic-gate else if ((S_ENFMT & Gen.g_mode) == S_ENFMT && modestr[6] != 'x')
70140Sstevel@tonic-gate modestr[6] = 'l';
70150Sstevel@tonic-gate if ((Hdr_type == TAR || Hdr_type == USTAR) && Gen.g_nlink == 0)
70160Sstevel@tonic-gate (void) printf("%s%4d ", modestr, (int)Gen.g_nlink+1);
70170Sstevel@tonic-gate else
70180Sstevel@tonic-gate (void) printf("%s%4d ", modestr, (int)Gen.g_nlink);
701910202SNobutomo.Nakano@Sun.COM if (Lastuid == (uid_t)Gen.g_uid) {
702010202SNobutomo.Nakano@Sun.COM if (Lastuid == (uid_t)-1)
70210Sstevel@tonic-gate (void) printf("-1 ");
70220Sstevel@tonic-gate else
70230Sstevel@tonic-gate (void) printf("%-9s", Curpw_p->pw_name);
70240Sstevel@tonic-gate } else {
70250Sstevel@tonic-gate if (Curpw_p = getpwuid((int)Gen.g_uid)) {
70260Sstevel@tonic-gate (void) printf("%-9s", Curpw_p->pw_name);
702710202SNobutomo.Nakano@Sun.COM Lastuid = (uid_t)Gen.g_uid;
70280Sstevel@tonic-gate } else {
70290Sstevel@tonic-gate (void) printf("%-9d", (int)Gen.g_uid);
703010202SNobutomo.Nakano@Sun.COM Lastuid = (uid_t)-1;
703110202SNobutomo.Nakano@Sun.COM }
703210202SNobutomo.Nakano@Sun.COM }
703310202SNobutomo.Nakano@Sun.COM if (Lastgid == (gid_t)Gen.g_gid) {
703410202SNobutomo.Nakano@Sun.COM if (Lastgid == (gid_t)-1)
70350Sstevel@tonic-gate (void) printf("-1 ");
70360Sstevel@tonic-gate else
70370Sstevel@tonic-gate (void) printf("%-9s", Curgr_p->gr_name);
70380Sstevel@tonic-gate } else {
70390Sstevel@tonic-gate if (Curgr_p = getgrgid((int)Gen.g_gid)) {
70400Sstevel@tonic-gate (void) printf("%-9s", Curgr_p->gr_name);
704110202SNobutomo.Nakano@Sun.COM Lastgid = (gid_t)Gen.g_gid;
70420Sstevel@tonic-gate } else {
70430Sstevel@tonic-gate (void) printf("%-9d", (int)Gen.g_gid);
704410202SNobutomo.Nakano@Sun.COM Lastgid = (gid_t)-1;
70450Sstevel@tonic-gate }
70460Sstevel@tonic-gate }
70470Sstevel@tonic-gate
70480Sstevel@tonic-gate /* print file size */
70490Sstevel@tonic-gate if (!Aspec || ((Gen.g_mode & Ftype) == S_IFIFO) ||
70505750Sceastha ((Gen.g_mode & Ftype) == S_IFSOCK) ||
70510Sstevel@tonic-gate (Hdr_type == BAR && bar_linkflag == SYMTYPE)) {
705210202SNobutomo.Nakano@Sun.COM off_t filesz = Gen.g_filesz;
705310202SNobutomo.Nakano@Sun.COM
705410202SNobutomo.Nakano@Sun.COM if (S_ISSPARSE(Gen.g_mode) && Gen.g_holes != NULL)
705510202SNobutomo.Nakano@Sun.COM filesz = Gen.g_holes->orig_size;
705610202SNobutomo.Nakano@Sun.COM
705710202SNobutomo.Nakano@Sun.COM if (filesz < (1LL << 31))
705810202SNobutomo.Nakano@Sun.COM (void) printf("%7lld ", (offset_t)filesz);
70590Sstevel@tonic-gate else
706010202SNobutomo.Nakano@Sun.COM (void) printf("%11lld ", (offset_t)filesz);
70610Sstevel@tonic-gate } else
70620Sstevel@tonic-gate (void) printf("%3d,%3d ", (int)major(Gen.g_rdev),
70630Sstevel@tonic-gate (int)minor(Gen.g_rdev));
706410202SNobutomo.Nakano@Sun.COM ttime = Gen.g_mtime;
706510202SNobutomo.Nakano@Sun.COM (void) strftime(Time, sizeof (Time),
706610202SNobutomo.Nakano@Sun.COM dcgettext(NULL, FORMAT, LC_TIME), localtime(&ttime));
70670Sstevel@tonic-gate (void) printf("%s, ", Time);
706810202SNobutomo.Nakano@Sun.COM str_fprintf(stdout, name_fmt, name, attribute);
70690Sstevel@tonic-gate if ((Gen.g_mode & Ftype) == S_IFLNK) {
70700Sstevel@tonic-gate if (Hdr_type == USTAR || Hdr_type == TAR)
70710Sstevel@tonic-gate (void) strcpy(Symlnk_p,
70720Sstevel@tonic-gate Thdr_p->tbuf.t_linkname);
70730Sstevel@tonic-gate else {
70746246Snakanon FILL(Gen.g_filesz);
70750Sstevel@tonic-gate (void) strncpy(Symlnk_p, Buffr.b_out_p,
70760Sstevel@tonic-gate Gen.g_filesz);
70770Sstevel@tonic-gate *(Symlnk_p + Gen.g_filesz) = '\0';
70780Sstevel@tonic-gate }
70790Sstevel@tonic-gate (void) printf(" -> %s", Symlnk_p);
70800Sstevel@tonic-gate }
70810Sstevel@tonic-gate if (Hdr_type == BAR) {
70820Sstevel@tonic-gate if (bar_linkflag == SYMTYPE)
70830Sstevel@tonic-gate (void) printf(gettext(" symbolic link to %s"),
70840Sstevel@tonic-gate bar_linkname);
70850Sstevel@tonic-gate else if (bar_linkflag == '1')
70860Sstevel@tonic-gate (void) printf(gettext(" linked to %s"),
70870Sstevel@tonic-gate bar_linkname);
70880Sstevel@tonic-gate }
70890Sstevel@tonic-gate if ((Hdr_type == USTAR || Hdr_type == TAR) &&
70900Sstevel@tonic-gate Thdr_p->tbuf.t_typeflag == '1') {
70910Sstevel@tonic-gate (void) printf(gettext(" linked to %s%s%s"),
70925750Sceastha (Gen.g_attrnam_p == NULL) ?
70930Sstevel@tonic-gate Thdr_p->tbuf.t_linkname : Gen.g_attrfnam_p,
70945750Sceastha (Gen.g_attrnam_p == NULL) ? "" :
70955750Sceastha gettext(" attribute "),
70965750Sceastha (Gen.g_attrnam_p == NULL) ?
70970Sstevel@tonic-gate "" : Gen.g_linktoattrnam_p);
70980Sstevel@tonic-gate }
70990Sstevel@tonic-gate (void) printf("\n");
71000Sstevel@tonic-gate } else if ((Args & OCt) || (Args & OCv)) {
710110202SNobutomo.Nakano@Sun.COM str_fprintf(Out_p, name_fmt, name, attribute);
71020Sstevel@tonic-gate (void) fputc('\n', Out_p);
71030Sstevel@tonic-gate } else { /* OCV */
71040Sstevel@tonic-gate (void) fputc('.', Out_p);
71050Sstevel@tonic-gate if (Verbcnt++ >= 49) { /* start a new line of dots */
71060Sstevel@tonic-gate Verbcnt = 0;
71070Sstevel@tonic-gate (void) fputc('\n', Out_p);
71080Sstevel@tonic-gate }
71090Sstevel@tonic-gate }
71100Sstevel@tonic-gate (void) fflush(Out_p);
71110Sstevel@tonic-gate }
71120Sstevel@tonic-gate
71130Sstevel@tonic-gate #define MK_USHORT(a) (a & 00000177777)
71140Sstevel@tonic-gate
71150Sstevel@tonic-gate /*
71160Sstevel@tonic-gate * write_hdr: Transfer header information for the generic structure
71170Sstevel@tonic-gate * into the format for the selected header and bwrite() the header.
71180Sstevel@tonic-gate */
71190Sstevel@tonic-gate
71200Sstevel@tonic-gate static void
write_hdr(int arcflag,off_t len)712110202SNobutomo.Nakano@Sun.COM write_hdr(int arcflag, off_t len)
71220Sstevel@tonic-gate {
71230Sstevel@tonic-gate int cnt, pad;
71240Sstevel@tonic-gate mode_t mode;
71250Sstevel@tonic-gate uid_t uid;
71260Sstevel@tonic-gate gid_t gid;
71270Sstevel@tonic-gate const char warnfmt[] = "%s%s%s : %s";
71280Sstevel@tonic-gate
712910202SNobutomo.Nakano@Sun.COM switch (arcflag) {
713010202SNobutomo.Nakano@Sun.COM case ARCHIVE_ACL:
71310Sstevel@tonic-gate mode = SECMODE;
713210202SNobutomo.Nakano@Sun.COM break;
713310202SNobutomo.Nakano@Sun.COM
713410202SNobutomo.Nakano@Sun.COM case ARCHIVE_XATTR:
713510202SNobutomo.Nakano@Sun.COM case ARCHIVE_NORMAL:
71360Sstevel@tonic-gate /*
71370Sstevel@tonic-gate * If attribute is being archived in cpio format then
71380Sstevel@tonic-gate * zap off the file type bits since those are truly a
71390Sstevel@tonic-gate * mask and reset them with _XATTR_CPIO_MODE
71400Sstevel@tonic-gate */
71410Sstevel@tonic-gate /*
71420Sstevel@tonic-gate * len is the value of g_filesz for normal files
71430Sstevel@tonic-gate * and the length of the special header buffer in
71440Sstevel@tonic-gate * the case of acl and xattr headers.
71450Sstevel@tonic-gate */
71465750Sceastha if (G_p->g_attrnam_p != NULL && Hdr_type != USTAR &&
71470Sstevel@tonic-gate Hdr_type != TAR) {
71481231Smarks mode = (G_p->g_mode & POSIXMODES) | _XATTR_CPIO_MODE;
71490Sstevel@tonic-gate } else {
71500Sstevel@tonic-gate mode = G_p->g_mode;
71510Sstevel@tonic-gate }
715210202SNobutomo.Nakano@Sun.COM if (arcflag != ARCHIVE_XATTR) {
71530Sstevel@tonic-gate len = G_p->g_filesz;
71540Sstevel@tonic-gate }
715510202SNobutomo.Nakano@Sun.COM break;
715610202SNobutomo.Nakano@Sun.COM
715710202SNobutomo.Nakano@Sun.COM case ARCHIVE_SPARSE:
715810202SNobutomo.Nakano@Sun.COM mode = G_p->g_mode | C_ISSPARSE;
715910202SNobutomo.Nakano@Sun.COM len = G_p->g_filesz;
716010202SNobutomo.Nakano@Sun.COM break;
71610Sstevel@tonic-gate }
71620Sstevel@tonic-gate
71630Sstevel@tonic-gate uid = G_p->g_uid;
71640Sstevel@tonic-gate gid = G_p->g_gid;
71650Sstevel@tonic-gate /*
71660Sstevel@tonic-gate * Handle EFT uids and gids. If they get too big
71670Sstevel@tonic-gate * to be represented in a particular format, force 'em to 'nobody'.
71680Sstevel@tonic-gate */
71690Sstevel@tonic-gate switch (Hdr_type) {
71700Sstevel@tonic-gate case BIN: /* 16-bits of u_short */
71710Sstevel@tonic-gate if ((ulong_t)uid > (ulong_t)USHRT_MAX)
71720Sstevel@tonic-gate uid = UID_NOBODY;
71730Sstevel@tonic-gate if ((ulong_t)gid > (ulong_t)USHRT_MAX)
71740Sstevel@tonic-gate gid = GID_NOBODY;
71750Sstevel@tonic-gate break;
71760Sstevel@tonic-gate case CHR: /* %.6lo => 262143 base 10 */
71770Sstevel@tonic-gate if ((ulong_t)uid > (ulong_t)0777777)
71780Sstevel@tonic-gate uid = UID_NOBODY;
71790Sstevel@tonic-gate if ((ulong_t)gid > (ulong_t)0777777)
71800Sstevel@tonic-gate gid = GID_NOBODY;
71810Sstevel@tonic-gate break;
71820Sstevel@tonic-gate case ASC: /* %.8lx => full 32 bits */
71830Sstevel@tonic-gate case CRC:
71840Sstevel@tonic-gate break;
71850Sstevel@tonic-gate case USTAR:
71860Sstevel@tonic-gate case TAR: /* %.7lo => 2097151 base 10 */
71870Sstevel@tonic-gate if ((ulong_t)uid > (ulong_t)07777777)
71880Sstevel@tonic-gate uid = UID_NOBODY;
71890Sstevel@tonic-gate if ((ulong_t)gid > (ulong_t)07777777)
71900Sstevel@tonic-gate gid = GID_NOBODY;
71910Sstevel@tonic-gate break;
71920Sstevel@tonic-gate default:
71930Sstevel@tonic-gate msg(EXT, "Impossible header type.");
71940Sstevel@tonic-gate }
71950Sstevel@tonic-gate
71960Sstevel@tonic-gate /*
71970Sstevel@tonic-gate * Since cpio formats -don't- encode the symbolic names, print
71980Sstevel@tonic-gate * a warning message when we map the uid or gid this way.
71990Sstevel@tonic-gate * Also, if the ownership just changed, clear set[ug]id bits
72000Sstevel@tonic-gate *
72010Sstevel@tonic-gate * (Except for USTAR format of course, where we have a string
72020Sstevel@tonic-gate * representation of the username embedded in the header)
72030Sstevel@tonic-gate */
72040Sstevel@tonic-gate if (uid != G_p->g_uid && Hdr_type != USTAR) {
72050Sstevel@tonic-gate msg(ERR, warnfmt,
72065750Sceastha (G_p->g_attrnam_p == NULL) ?
72070Sstevel@tonic-gate G_p->g_nam_p : G_p->g_attrfnam_p,
72085750Sceastha (G_p->g_attrnam_p == NULL) ? "" : G_p->g_rw_sysattr ?
72095750Sceastha gettext(" System Attribute ") : gettext(" Attribute "),
72105750Sceastha (G_p->g_attrnam_p == NULL) ? "" : G_p->g_attrnam_p,
72110Sstevel@tonic-gate gettext("uid too large for archive format"));
72120Sstevel@tonic-gate if (S_ISREG(mode))
72130Sstevel@tonic-gate mode &= ~S_ISUID;
72140Sstevel@tonic-gate }
72150Sstevel@tonic-gate if (gid != G_p->g_gid && Hdr_type != USTAR) {
72160Sstevel@tonic-gate msg(ERR, warnfmt,
72175750Sceastha (G_p->g_attrnam_p == NULL) ?
72180Sstevel@tonic-gate G_p->g_nam_p : G_p->g_attrfnam_p,
72195750Sceastha (G_p->g_attrnam_p == NULL) ? "" : G_p->g_rw_sysattr ?
72205750Sceastha gettext(" System Attribute ") : gettext(" Attribute "),
72215750Sceastha (G_p->g_attrnam_p == NULL) ? "" : G_p->g_attrnam_p,
72220Sstevel@tonic-gate gettext("gid too large for archive format"));
72230Sstevel@tonic-gate if (S_ISREG(mode))
72240Sstevel@tonic-gate mode &= ~S_ISGID;
72250Sstevel@tonic-gate }
72260Sstevel@tonic-gate
72270Sstevel@tonic-gate switch (Hdr_type) {
72280Sstevel@tonic-gate case BIN:
72290Sstevel@tonic-gate case CHR:
72300Sstevel@tonic-gate case ASC:
72310Sstevel@tonic-gate case CRC:
72320Sstevel@tonic-gate cnt = Hdrsz + G_p->g_namesz;
72330Sstevel@tonic-gate break;
72340Sstevel@tonic-gate case TAR:
72350Sstevel@tonic-gate /*FALLTHROUGH*/
72360Sstevel@tonic-gate case USTAR: /* TAR and USTAR */
72370Sstevel@tonic-gate cnt = TARSZ;
72380Sstevel@tonic-gate break;
72390Sstevel@tonic-gate default:
72400Sstevel@tonic-gate msg(EXT, "Impossible header type.");
72410Sstevel@tonic-gate }
72420Sstevel@tonic-gate FLUSH(cnt);
72430Sstevel@tonic-gate
72440Sstevel@tonic-gate switch (Hdr_type) {
72450Sstevel@tonic-gate case BIN:
72460Sstevel@tonic-gate Hdr.h_magic = (short)G_p->g_magic;
72470Sstevel@tonic-gate Hdr.h_dev = G_p->g_dev;
72480Sstevel@tonic-gate Hdr.h_ino = G_p->g_ino;
72490Sstevel@tonic-gate Hdr.h_uid = uid;
72500Sstevel@tonic-gate Hdr.h_gid = gid;
72510Sstevel@tonic-gate Hdr.h_mode = mode;
72520Sstevel@tonic-gate Hdr.h_nlink = G_p->g_nlink;
72530Sstevel@tonic-gate Hdr.h_rdev = G_p->g_rdev;
72540Sstevel@tonic-gate mkshort(Hdr.h_mtime, (long)G_p->g_mtime);
72550Sstevel@tonic-gate Hdr.h_namesize = (short)G_p->g_namesz;
72560Sstevel@tonic-gate mkshort(Hdr.h_filesize, (long)len);
72570Sstevel@tonic-gate (void) strcpy(Hdr.h_name, G_p->g_nam_p);
72580Sstevel@tonic-gate (void) memcpy(Buffr.b_in_p, &Hdr, cnt);
72590Sstevel@tonic-gate break;
72600Sstevel@tonic-gate case CHR:
726110202SNobutomo.Nakano@Sun.COM /*LINTED*/
72620Sstevel@tonic-gate (void) sprintf(Buffr.b_in_p,
72630Sstevel@tonic-gate "%.6lo%.6lo%.6lo%.6lo%.6lo%.6lo%.6lo%.6lo%.11lo%.6lo%."
72640Sstevel@tonic-gate "11llo%s", G_p->g_magic, G_p->g_dev, G_p->g_ino, mode,
726510202SNobutomo.Nakano@Sun.COM (long)uid, (long)gid, G_p->g_nlink, MK_USHORT(G_p->g_rdev),
72660Sstevel@tonic-gate G_p->g_mtime, (long)G_p->g_namesz, (offset_t)len,
72670Sstevel@tonic-gate G_p->g_nam_p);
72680Sstevel@tonic-gate break;
72690Sstevel@tonic-gate case ASC:
72700Sstevel@tonic-gate case CRC:
727110202SNobutomo.Nakano@Sun.COM /*LINTED*/
72720Sstevel@tonic-gate (void) sprintf(Buffr.b_in_p,
72730Sstevel@tonic-gate "%.6lx%.8lx%.8lx%.8lx%.8lx%.8lx%.8lx%.8lx%.8lx%.8lx%."
72740Sstevel@tonic-gate "8lx%.8lx%.8lx%.8lx%s",
72751134Sceastha G_p->g_magic, G_p->g_ino, mode, G_p->g_uid,
72761134Sceastha G_p->g_gid, G_p->g_nlink, G_p->g_mtime, (ulong_t)len,
72771134Sceastha major(G_p->g_dev), minor(G_p->g_dev),
72781134Sceastha major(G_p->g_rdev), minor(G_p->g_rdev),
72791134Sceastha G_p->g_namesz, G_p->g_cksum, G_p->g_nam_p);
72800Sstevel@tonic-gate break;
72810Sstevel@tonic-gate case USTAR:
72820Sstevel@tonic-gate Thdr_p = (union tblock *)Buffr.b_in_p;
728310202SNobutomo.Nakano@Sun.COM (void) memset(Thdr_p, 0, TARSZ);
72840Sstevel@tonic-gate (void) strncpy(Thdr_p->tbuf.t_name, G_p->g_tname,
72850Sstevel@tonic-gate (int)strlen(G_p->g_tname));
72860Sstevel@tonic-gate (void) sprintf(Thdr_p->tbuf.t_mode, "%07o", (int)mode);
72870Sstevel@tonic-gate (void) sprintf(Thdr_p->tbuf.t_uid, "%07o", (int)uid);
72880Sstevel@tonic-gate (void) sprintf(Thdr_p->tbuf.t_gid, "%07o", (int)gid);
72890Sstevel@tonic-gate (void) sprintf(Thdr_p->tbuf.t_size, "%011llo",
72900Sstevel@tonic-gate (offset_t)len);
72910Sstevel@tonic-gate (void) sprintf(Thdr_p->tbuf.t_mtime, "%011lo", G_p->g_mtime);
729210202SNobutomo.Nakano@Sun.COM if (arcflag == ARCHIVE_ACL) {
72930Sstevel@tonic-gate Thdr_p->tbuf.t_typeflag = 'A'; /* ACL file type */
729410202SNobutomo.Nakano@Sun.COM } else if (arcflag == ARCHIVE_XATTR ||
72955750Sceastha (G_p->g_attrnam_p != NULL)) {
72960Sstevel@tonic-gate Thdr_p->tbuf.t_typeflag = _XATTR_HDRTYPE;
72970Sstevel@tonic-gate } else {
72980Sstevel@tonic-gate Thdr_p->tbuf.t_typeflag = G_p->g_typeflag;
72990Sstevel@tonic-gate }
73000Sstevel@tonic-gate if (T_lname[0] != '\0') {
73010Sstevel@tonic-gate /*
73020Sstevel@tonic-gate * if not a symbolic link
73030Sstevel@tonic-gate */
73040Sstevel@tonic-gate if (((G_p->g_mode & Ftype) != S_IFLNK) &&
73055750Sceastha (G_p->g_attrnam_p == NULL)) {
73060Sstevel@tonic-gate Thdr_p->tbuf.t_typeflag = LNKTYPE;
73070Sstevel@tonic-gate (void) sprintf(Thdr_p->tbuf.t_size,
73080Sstevel@tonic-gate "%011lo", 0L);
73090Sstevel@tonic-gate }
73100Sstevel@tonic-gate (void) strncpy(Thdr_p->tbuf.t_linkname, T_lname,
73110Sstevel@tonic-gate strlen(T_lname));
73120Sstevel@tonic-gate }
731310202SNobutomo.Nakano@Sun.COM (void) strcpy(Thdr_p->tbuf.t_magic, TMAGIC);
731410202SNobutomo.Nakano@Sun.COM (void) strcpy(Thdr_p->tbuf.t_version, TVERSION);
731510202SNobutomo.Nakano@Sun.COM (void) strcpy(Thdr_p->tbuf.t_uname, G_p->g_uname);
731610202SNobutomo.Nakano@Sun.COM (void) strcpy(Thdr_p->tbuf.t_gname, G_p->g_gname);
73170Sstevel@tonic-gate (void) sprintf(Thdr_p->tbuf.t_devmajor, "%07o",
73180Sstevel@tonic-gate (int)major(G_p->g_rdev));
73190Sstevel@tonic-gate (void) sprintf(Thdr_p->tbuf.t_devminor, "%07o",
73200Sstevel@tonic-gate (int)minor(G_p->g_rdev));
73210Sstevel@tonic-gate if (Gen.g_prefix) {
732210202SNobutomo.Nakano@Sun.COM (void) strcpy(Thdr_p->tbuf.t_prefix, Gen.g_prefix);
73230Sstevel@tonic-gate free(Gen.g_prefix);
73240Sstevel@tonic-gate Gen.g_prefix = NULL;
73250Sstevel@tonic-gate } else {
732610202SNobutomo.Nakano@Sun.COM Thdr_p->tbuf.t_prefix[0] = '\0';
73270Sstevel@tonic-gate }
73280Sstevel@tonic-gate (void) sprintf(Thdr_p->tbuf.t_cksum, "%07o",
73290Sstevel@tonic-gate (int)cksum(TARTYP, 0, NULL));
73300Sstevel@tonic-gate break;
73310Sstevel@tonic-gate case TAR:
73320Sstevel@tonic-gate Thdr_p = (union tblock *)Buffr.b_in_p;
733310202SNobutomo.Nakano@Sun.COM (void) memset(Thdr_p, 0, TARSZ);
73340Sstevel@tonic-gate (void) strncpy(Thdr_p->tbuf.t_name, G_p->g_nam_p,
73350Sstevel@tonic-gate G_p->g_namesz);
73360Sstevel@tonic-gate (void) sprintf(Thdr_p->tbuf.t_mode, "%07o ", (int)mode);
73370Sstevel@tonic-gate (void) sprintf(Thdr_p->tbuf.t_uid, "%07o ", (int)uid);
73380Sstevel@tonic-gate (void) sprintf(Thdr_p->tbuf.t_gid, "%07o ", (int)gid);
73390Sstevel@tonic-gate (void) sprintf(Thdr_p->tbuf.t_size, "%011llo ",
73400Sstevel@tonic-gate (offset_t)len);
73410Sstevel@tonic-gate (void) sprintf(Thdr_p->tbuf.t_mtime, "%011o ",
73420Sstevel@tonic-gate (int)G_p->g_mtime);
73430Sstevel@tonic-gate if (T_lname[0] != '\0') {
73440Sstevel@tonic-gate Thdr_p->tbuf.t_typeflag = '1';
73450Sstevel@tonic-gate } else {
73460Sstevel@tonic-gate Thdr_p->tbuf.t_typeflag = '\0';
73470Sstevel@tonic-gate }
73480Sstevel@tonic-gate (void) strncpy(Thdr_p->tbuf.t_linkname, T_lname,
73490Sstevel@tonic-gate strlen(T_lname));
73500Sstevel@tonic-gate break;
73510Sstevel@tonic-gate default:
73520Sstevel@tonic-gate msg(EXT, "Impossible header type.");
73530Sstevel@tonic-gate } /* Hdr_type */
73540Sstevel@tonic-gate
73550Sstevel@tonic-gate Buffr.b_in_p += cnt;
73560Sstevel@tonic-gate Buffr.b_cnt += cnt;
73570Sstevel@tonic-gate pad = ((cnt + Pad_val) & ~Pad_val) - cnt;
73580Sstevel@tonic-gate if (pad != 0) {
73590Sstevel@tonic-gate FLUSH(pad);
736010202SNobutomo.Nakano@Sun.COM (void) memset(Buffr.b_in_p, 0, pad);
73610Sstevel@tonic-gate Buffr.b_in_p += pad;
73620Sstevel@tonic-gate Buffr.b_cnt += pad;
73630Sstevel@tonic-gate }
73640Sstevel@tonic-gate }
73650Sstevel@tonic-gate
73660Sstevel@tonic-gate /*
73670Sstevel@tonic-gate * write_trail: Create the appropriate trailer for the selected header type
73680Sstevel@tonic-gate * and bwrite the trailer. Pad the buffer with nulls out to the next Bufsize
73690Sstevel@tonic-gate * boundary, and force a write. If the write completes, or if the trailer is
73700Sstevel@tonic-gate * completely written (but not all of the padding nulls (as can happen on end
73710Sstevel@tonic-gate * of medium)) return. Otherwise, the trailer was not completely written out,
73720Sstevel@tonic-gate * so re-pad the buffer with nulls and try again.
73730Sstevel@tonic-gate */
73740Sstevel@tonic-gate
73750Sstevel@tonic-gate static void
write_trail(void)73760Sstevel@tonic-gate write_trail(void)
73770Sstevel@tonic-gate {
73780Sstevel@tonic-gate int cnt, need;
73790Sstevel@tonic-gate
73800Sstevel@tonic-gate switch (Hdr_type) {
73810Sstevel@tonic-gate case BIN:
73820Sstevel@tonic-gate Gen.g_magic = CMN_BIN;
73830Sstevel@tonic-gate break;
73840Sstevel@tonic-gate case CHR:
73850Sstevel@tonic-gate Gen.g_magic = CMN_BIN;
73860Sstevel@tonic-gate break;
73870Sstevel@tonic-gate case ASC:
73880Sstevel@tonic-gate Gen.g_magic = CMN_ASC;
73890Sstevel@tonic-gate break;
73900Sstevel@tonic-gate case CRC:
73910Sstevel@tonic-gate Gen.g_magic = CMN_CRC;
73920Sstevel@tonic-gate break;
73930Sstevel@tonic-gate }
73940Sstevel@tonic-gate
73950Sstevel@tonic-gate switch (Hdr_type) {
73960Sstevel@tonic-gate case BIN:
73970Sstevel@tonic-gate case CHR:
73980Sstevel@tonic-gate case ASC:
73990Sstevel@tonic-gate case CRC:
74000Sstevel@tonic-gate Gen.g_mode = Gen.g_uid = Gen.g_gid = 0;
74010Sstevel@tonic-gate Gen.g_nlink = 1;
74020Sstevel@tonic-gate Gen.g_mtime = Gen.g_ino = Gen.g_dev = 0;
74030Sstevel@tonic-gate Gen.g_rdev = Gen.g_cksum = 0;
74040Sstevel@tonic-gate Gen.g_filesz = (off_t)0;
74050Sstevel@tonic-gate Gen.g_namesz = strlen("TRAILER!!!") + 1;
74060Sstevel@tonic-gate (void) strcpy(Gen.g_nam_p, "TRAILER!!!");
74070Sstevel@tonic-gate G_p = &Gen;
74080Sstevel@tonic-gate write_hdr(ARCHIVE_NORMAL, (off_t)0);
74090Sstevel@tonic-gate break;
74100Sstevel@tonic-gate case TAR:
74110Sstevel@tonic-gate /*FALLTHROUGH*/
74120Sstevel@tonic-gate case USTAR: /* TAR and USTAR */
74130Sstevel@tonic-gate for (cnt = 0; cnt < 3; cnt++) {
74140Sstevel@tonic-gate FLUSH(TARSZ);
741510202SNobutomo.Nakano@Sun.COM (void) memset(Buffr.b_in_p, 0, TARSZ);
74160Sstevel@tonic-gate Buffr.b_in_p += TARSZ;
74170Sstevel@tonic-gate Buffr.b_cnt += TARSZ;
74180Sstevel@tonic-gate }
74190Sstevel@tonic-gate break;
74200Sstevel@tonic-gate default:
74210Sstevel@tonic-gate msg(EXT, "Impossible header type.");
74220Sstevel@tonic-gate }
74230Sstevel@tonic-gate need = Bufsize - (Buffr.b_cnt % Bufsize);
74240Sstevel@tonic-gate if (need == Bufsize)
74250Sstevel@tonic-gate need = 0;
74260Sstevel@tonic-gate
74270Sstevel@tonic-gate while (Buffr.b_cnt > 0) {
74280Sstevel@tonic-gate while (need > 0) {
74290Sstevel@tonic-gate cnt = (need < TARSZ) ? need : TARSZ;
74300Sstevel@tonic-gate need -= cnt;
74310Sstevel@tonic-gate FLUSH(cnt);
743210202SNobutomo.Nakano@Sun.COM (void) memset(Buffr.b_in_p, 0, cnt);
74330Sstevel@tonic-gate Buffr.b_in_p += cnt;
74340Sstevel@tonic-gate Buffr.b_cnt += cnt;
74350Sstevel@tonic-gate }
74360Sstevel@tonic-gate bflush();
74370Sstevel@tonic-gate }
74380Sstevel@tonic-gate }
74390Sstevel@tonic-gate
74400Sstevel@tonic-gate /*
74410Sstevel@tonic-gate * if archives in USTAR format, check if typeflag == '5' for directories
74420Sstevel@tonic-gate */
74430Sstevel@tonic-gate static int
ustar_dir(void)74440Sstevel@tonic-gate ustar_dir(void)
74450Sstevel@tonic-gate {
74460Sstevel@tonic-gate if (Hdr_type == USTAR || Hdr_type == TAR) {
74470Sstevel@tonic-gate if (Thdr_p->tbuf.t_typeflag == '5')
74480Sstevel@tonic-gate return (1);
74490Sstevel@tonic-gate }
74500Sstevel@tonic-gate return (0);
74510Sstevel@tonic-gate }
74520Sstevel@tonic-gate
74530Sstevel@tonic-gate /*
74540Sstevel@tonic-gate * if archives in USTAR format, check if typeflag == '3' || '4' || '6'
74550Sstevel@tonic-gate * for character, block, fifo special files
74560Sstevel@tonic-gate */
74570Sstevel@tonic-gate static int
ustar_spec(void)74580Sstevel@tonic-gate ustar_spec(void)
74590Sstevel@tonic-gate {
74600Sstevel@tonic-gate int typeflag;
74610Sstevel@tonic-gate
74620Sstevel@tonic-gate if (Hdr_type == USTAR || Hdr_type == TAR) {
74630Sstevel@tonic-gate typeflag = Thdr_p->tbuf.t_typeflag;
74640Sstevel@tonic-gate if (typeflag == '3' || typeflag == '4' || typeflag == '6')
74650Sstevel@tonic-gate return (1);
74660Sstevel@tonic-gate }
74670Sstevel@tonic-gate return (0);
74680Sstevel@tonic-gate }
74690Sstevel@tonic-gate
74700Sstevel@tonic-gate /*
74710Sstevel@tonic-gate * The return value is a pointer to a converted copy of the information in
74720Sstevel@tonic-gate * FromStat if the file is representable in -Hodc format, and NULL otherwise.
74730Sstevel@tonic-gate */
74740Sstevel@tonic-gate
74750Sstevel@tonic-gate static struct stat *
convert_to_old_stat(struct stat * FromStat,char * namep,char * attrp)74760Sstevel@tonic-gate convert_to_old_stat(struct stat *FromStat, char *namep, char *attrp)
74770Sstevel@tonic-gate {
74780Sstevel@tonic-gate static struct stat ToSt;
74790Sstevel@tonic-gate cpioinfo_t TmpSt;
74800Sstevel@tonic-gate
74810Sstevel@tonic-gate (void) memset(&TmpSt, 0, sizeof (cpioinfo_t));
74820Sstevel@tonic-gate stat_to_svr32_stat(&TmpSt, FromStat);
74830Sstevel@tonic-gate (void) memset(&ToSt, 0, sizeof (ToSt));
74840Sstevel@tonic-gate
74850Sstevel@tonic-gate if (TmpSt.st_rdev == (o_dev_t)NODEV &&
74860Sstevel@tonic-gate (((TmpSt.st_mode & Ftype) == S_IFCHR) ||
74870Sstevel@tonic-gate ((TmpSt.st_mode & Ftype) == S_IFBLK))) {
74880Sstevel@tonic-gate /*
74890Sstevel@tonic-gate * Encountered a problem representing the rdev information.
74900Sstevel@tonic-gate * Don't archive it.
74910Sstevel@tonic-gate */
74920Sstevel@tonic-gate
74930Sstevel@tonic-gate msg(ERR, "Error -Hodc format can't support expanded"
74940Sstevel@tonic-gate "types on %s%s%s",
74950Sstevel@tonic-gate namep,
74960Sstevel@tonic-gate (attrp == NULL) ? "" : gettext(" Attribute"),
74970Sstevel@tonic-gate (attrp == NULL) ? "" : attrp);
74980Sstevel@tonic-gate return (NULL);
74990Sstevel@tonic-gate }
75000Sstevel@tonic-gate
75010Sstevel@tonic-gate if (TmpSt.st_dev == (o_dev_t)NODEV) {
75020Sstevel@tonic-gate /*
75030Sstevel@tonic-gate * Having trouble representing the device/inode pair. We can't
75040Sstevel@tonic-gate * track links in this case; break them all into separate
75050Sstevel@tonic-gate * files.
75060Sstevel@tonic-gate */
75070Sstevel@tonic-gate
75080Sstevel@tonic-gate TmpSt.st_ino = 0;
75090Sstevel@tonic-gate
75100Sstevel@tonic-gate if (((TmpSt.st_mode & Ftype) != S_IFDIR) &&
75110Sstevel@tonic-gate TmpSt.st_nlink > 1)
75120Sstevel@tonic-gate msg(POST,
75130Sstevel@tonic-gate "Warning: file %s%s%s has large "
75140Sstevel@tonic-gate "device number - linked "
75150Sstevel@tonic-gate "files will be restored as "
75160Sstevel@tonic-gate "separate files",
75170Sstevel@tonic-gate namep,
75180Sstevel@tonic-gate (attrp == NULL) ? "" : gettext(" Attribute"),
75190Sstevel@tonic-gate (attrp == NULL) ? "" : attrp);
75200Sstevel@tonic-gate
75210Sstevel@tonic-gate /* ensure no links */
75220Sstevel@tonic-gate
75230Sstevel@tonic-gate TmpSt.st_nlink = 1;
75240Sstevel@tonic-gate }
75250Sstevel@tonic-gate
75260Sstevel@tonic-gate /* Start converting values */
75270Sstevel@tonic-gate
75280Sstevel@tonic-gate if (TmpSt.st_dev < 0) {
75290Sstevel@tonic-gate ToSt.st_dev = 0;
75300Sstevel@tonic-gate } else {
75310Sstevel@tonic-gate ToSt.st_dev = (dev_t)TmpSt.st_dev;
75320Sstevel@tonic-gate }
75330Sstevel@tonic-gate
75340Sstevel@tonic-gate /* -actual- not truncated uid */
75350Sstevel@tonic-gate
75360Sstevel@tonic-gate ToSt.st_uid = TmpSt.st_uid;
75370Sstevel@tonic-gate
75380Sstevel@tonic-gate /* -actual- not truncated gid */
75390Sstevel@tonic-gate
75400Sstevel@tonic-gate ToSt.st_gid = TmpSt.st_gid;
75410Sstevel@tonic-gate ToSt.st_ino = (ino_t)TmpSt.st_ino;
75420Sstevel@tonic-gate ToSt.st_mode = (mode_t)TmpSt.st_mode;
75430Sstevel@tonic-gate ToSt.st_mtime = (ulong_t)TmpSt.st_modtime;
75440Sstevel@tonic-gate ToSt.st_nlink = (nlink_t)TmpSt.st_nlink;
75450Sstevel@tonic-gate ToSt.st_size = (off_t)TmpSt.st_size;
75460Sstevel@tonic-gate ToSt.st_rdev = (dev_t)TmpSt.st_rdev;
75470Sstevel@tonic-gate
75480Sstevel@tonic-gate return (&ToSt);
75490Sstevel@tonic-gate }
75500Sstevel@tonic-gate
75510Sstevel@tonic-gate /*
75520Sstevel@tonic-gate * In the beginning of each bar archive, there is a header which describes the
75530Sstevel@tonic-gate * current volume being created, followed by a header which describes the
75540Sstevel@tonic-gate * current file being created, followed by the file itself. If there is
75550Sstevel@tonic-gate * more than one file to be created, a separate header will be created for
75560Sstevel@tonic-gate * each additional file. This structure may be repeated if the bar archive
75570Sstevel@tonic-gate * contains multiple volumes. If a file spans across volumes, its header
75580Sstevel@tonic-gate * will not be repeated in the next volume.
75590Sstevel@tonic-gate * +------------------+
75600Sstevel@tonic-gate * | vol header |
75610Sstevel@tonic-gate * |------------------|
75620Sstevel@tonic-gate * | file header i | i = 0
75630Sstevel@tonic-gate * |------------------|
75640Sstevel@tonic-gate * | <file i> |
75650Sstevel@tonic-gate * |------------------|
75660Sstevel@tonic-gate * | file header i+1 |
75670Sstevel@tonic-gate * |------------------|
75680Sstevel@tonic-gate * | <file i+1> |
75690Sstevel@tonic-gate * |------------------|
75700Sstevel@tonic-gate * | . |
75710Sstevel@tonic-gate * | . |
75720Sstevel@tonic-gate * | . |
75730Sstevel@tonic-gate * +------------------+
75740Sstevel@tonic-gate */
75750Sstevel@tonic-gate
75760Sstevel@tonic-gate /*
75770Sstevel@tonic-gate * read in the header that describes the current volume of the bar archive
75780Sstevel@tonic-gate * to be extracted.
75790Sstevel@tonic-gate */
75800Sstevel@tonic-gate static void
read_bar_vol_hdr(void)75810Sstevel@tonic-gate read_bar_vol_hdr(void)
75820Sstevel@tonic-gate {
75830Sstevel@tonic-gate union b_block *tmp_hdr;
75840Sstevel@tonic-gate
75850Sstevel@tonic-gate tmp_hdr = (union b_block *)Buffr.b_out_p;
75860Sstevel@tonic-gate if (tmp_hdr->dbuf.bar_magic[0] == BAR_VOLUME_MAGIC) {
75870Sstevel@tonic-gate
75880Sstevel@tonic-gate if (bar_Vhdr == NULL) {
75890Sstevel@tonic-gate bar_Vhdr = e_zalloc(E_EXIT, TBLOCK);
75900Sstevel@tonic-gate }
75910Sstevel@tonic-gate (void) memcpy(&(bar_Vhdr->dbuf), &(tmp_hdr->dbuf), TBLOCK);
75920Sstevel@tonic-gate } else {
75930Sstevel@tonic-gate (void) fprintf(stderr, gettext(
75940Sstevel@tonic-gate "bar error: cannot read volume header\n"));
75950Sstevel@tonic-gate exit(1);
75960Sstevel@tonic-gate }
75970Sstevel@tonic-gate
75980Sstevel@tonic-gate (void) sscanf(bar_Vhdr->dbuf.mode, "%8lo", &Gen_bar_vol.g_mode);
75990Sstevel@tonic-gate (void) sscanf(bar_Vhdr->dbuf.uid, "%8d", (int *)&Gen_bar_vol.g_uid);
76000Sstevel@tonic-gate (void) sscanf(bar_Vhdr->dbuf.gid, "%8d", (int *)&Gen_bar_vol.g_gid);
76010Sstevel@tonic-gate (void) sscanf(bar_Vhdr->dbuf.size, "%12llo",
76020Sstevel@tonic-gate (u_off_t *)&Gen_bar_vol.g_filesz);
76030Sstevel@tonic-gate (void) sscanf(bar_Vhdr->dbuf.mtime, "%12lo", &Gen_bar_vol.g_mtime);
76040Sstevel@tonic-gate (void) sscanf(bar_Vhdr->dbuf.chksum, "%8lo", &Gen_bar_vol.g_cksum);
76050Sstevel@tonic-gate
76060Sstevel@tonic-gate /* set the compress flag */
76070Sstevel@tonic-gate if (bar_Vhdr->dbuf.compressed == '1')
76080Sstevel@tonic-gate Compressed = 1;
76090Sstevel@tonic-gate else
76100Sstevel@tonic-gate Compressed = 0;
76110Sstevel@tonic-gate
76120Sstevel@tonic-gate Buffr.b_out_p += 512;
76130Sstevel@tonic-gate Buffr.b_cnt -= 512;
76140Sstevel@tonic-gate
76150Sstevel@tonic-gate /*
76160Sstevel@tonic-gate * not the first volume; exit
76170Sstevel@tonic-gate */
76180Sstevel@tonic-gate if (strcmp(bar_Vhdr->dbuf.volume_num, "1") != 0) {
76190Sstevel@tonic-gate (void) fprintf(stderr,
76200Sstevel@tonic-gate gettext("error: This is not volume 1. "));
76210Sstevel@tonic-gate (void) fprintf(stderr, gettext("This is volume %s. "),
76220Sstevel@tonic-gate bar_Vhdr->dbuf.volume_num);
76230Sstevel@tonic-gate (void) fprintf(stderr, gettext("Please insert volume 1.\n"));
76240Sstevel@tonic-gate exit(1);
76250Sstevel@tonic-gate }
76260Sstevel@tonic-gate
76270Sstevel@tonic-gate read_bar_file_hdr();
76280Sstevel@tonic-gate }
76290Sstevel@tonic-gate
76300Sstevel@tonic-gate /*
76310Sstevel@tonic-gate * read in the header that describes the current file to be extracted
76320Sstevel@tonic-gate */
76330Sstevel@tonic-gate static void
read_bar_file_hdr(void)76340Sstevel@tonic-gate read_bar_file_hdr(void)
76350Sstevel@tonic-gate {
76360Sstevel@tonic-gate union b_block *tmp_hdr;
76370Sstevel@tonic-gate char *start_of_name, *name_p;
76380Sstevel@tonic-gate char *tmp;
76390Sstevel@tonic-gate
76400Sstevel@tonic-gate if (*Buffr.b_out_p == '\0') {
76410Sstevel@tonic-gate *Gen.g_nam_p = '\0';
76420Sstevel@tonic-gate exit(0);
76430Sstevel@tonic-gate }
76440Sstevel@tonic-gate
76450Sstevel@tonic-gate tmp_hdr = (union b_block *)Buffr.b_out_p;
76460Sstevel@tonic-gate
76470Sstevel@tonic-gate tmp = &tmp_hdr->dbuf.mode[1];
76480Sstevel@tonic-gate (void) sscanf(tmp, "%8lo", &Gen.g_mode);
76490Sstevel@tonic-gate (void) sscanf(tmp_hdr->dbuf.uid, "%8lo", &Gen.g_uid);
76500Sstevel@tonic-gate (void) sscanf(tmp_hdr->dbuf.gid, "%8lo", &Gen.g_gid);
76510Sstevel@tonic-gate (void) sscanf(tmp_hdr->dbuf.size, "%12llo",
76520Sstevel@tonic-gate (u_off_t *)&Gen.g_filesz);
76530Sstevel@tonic-gate (void) sscanf(tmp_hdr->dbuf.mtime, "%12lo", &Gen.g_mtime);
76540Sstevel@tonic-gate (void) sscanf(tmp_hdr->dbuf.chksum, "%8lo", &Gen.g_cksum);
76550Sstevel@tonic-gate (void) sscanf(tmp_hdr->dbuf.rdev, "%8lo", &Gen.g_rdev);
76560Sstevel@tonic-gate
76570Sstevel@tonic-gate #define to_new_major(x) (int)((unsigned)((x) & OMAXMAJ) << NBITSMINOR)
76580Sstevel@tonic-gate #define to_new_minor(x) (int)((x) & OMAXMIN)
76590Sstevel@tonic-gate Gen.g_rdev = to_new_major(Gen.g_rdev) | to_new_minor(Gen.g_rdev);
76600Sstevel@tonic-gate bar_linkflag = tmp_hdr->dbuf.linkflag;
76610Sstevel@tonic-gate start_of_name = &tmp_hdr->dbuf.start_of_name;
76620Sstevel@tonic-gate
76630Sstevel@tonic-gate
76640Sstevel@tonic-gate name_p = Gen.g_nam_p;
76650Sstevel@tonic-gate while (*name_p++ = *start_of_name++)
76660Sstevel@tonic-gate ;
76670Sstevel@tonic-gate *name_p = '\0';
76680Sstevel@tonic-gate if (bar_linkflag == LNKTYPE || bar_linkflag == SYMTYPE)
76690Sstevel@tonic-gate (void) strcpy(bar_linkname, start_of_name);
76700Sstevel@tonic-gate
76710Sstevel@tonic-gate Gen.g_namesz = strlen(Gen.g_nam_p) + 1;
76720Sstevel@tonic-gate (void) strcpy(nambuf, Gen.g_nam_p);
76730Sstevel@tonic-gate }
76740Sstevel@tonic-gate
76750Sstevel@tonic-gate /*
76760Sstevel@tonic-gate * if the bar archive is compressed, set up a pipe and do the de-compression
76770Sstevel@tonic-gate * as the compressed file is read in.
76780Sstevel@tonic-gate */
76790Sstevel@tonic-gate static void
setup_uncompress(FILE ** pipef)76800Sstevel@tonic-gate setup_uncompress(FILE **pipef)
76810Sstevel@tonic-gate {
76820Sstevel@tonic-gate char *cmd_buf;
76830Sstevel@tonic-gate size_t cmdlen;
76840Sstevel@tonic-gate
76850Sstevel@tonic-gate cmd_buf = e_zalloc(E_EXIT, MAXPATHLEN * 2);
76860Sstevel@tonic-gate
76870Sstevel@tonic-gate if (access(Gen.g_nam_p, W_OK) != 0) {
76880Sstevel@tonic-gate cmdlen = snprintf(cmd_buf, MAXPATHLEN * 2,
76890Sstevel@tonic-gate "chmod +w '%s'; uncompress -c > '%s'; "
76900Sstevel@tonic-gate "chmod 0%o '%s'",
76910Sstevel@tonic-gate Gen.g_nam_p, Gen.g_nam_p, (int)G_p->g_mode, Gen.g_nam_p);
76920Sstevel@tonic-gate } else {
76930Sstevel@tonic-gate cmdlen = snprintf(cmd_buf, MAXPATHLEN * 2,
76940Sstevel@tonic-gate "uncompress -c > '%s'", Gen.g_nam_p);
76950Sstevel@tonic-gate }
76960Sstevel@tonic-gate
76970Sstevel@tonic-gate if (cmdlen >= MAXPATHLEN * 2 ||
76980Sstevel@tonic-gate (*pipef = popen(cmd_buf, "w")) == NULL) {
76990Sstevel@tonic-gate (void) fprintf(stderr, gettext("error\n"));
77000Sstevel@tonic-gate exit(1);
77010Sstevel@tonic-gate }
77020Sstevel@tonic-gate
77030Sstevel@tonic-gate if (close(Ofile) != 0)
77040Sstevel@tonic-gate msg(EXTN, "close error");
77050Sstevel@tonic-gate Ofile = fileno(*pipef);
77060Sstevel@tonic-gate
77070Sstevel@tonic-gate free(cmd_buf);
77080Sstevel@tonic-gate }
77090Sstevel@tonic-gate
77100Sstevel@tonic-gate /*
77110Sstevel@tonic-gate * if the bar archive spans multiple volumes, read in the header that
77120Sstevel@tonic-gate * describes the next volume.
77130Sstevel@tonic-gate */
77140Sstevel@tonic-gate static void
skip_bar_volhdr(void)77150Sstevel@tonic-gate skip_bar_volhdr(void)
77160Sstevel@tonic-gate {
77170Sstevel@tonic-gate char *buff;
77180Sstevel@tonic-gate union b_block *tmp_hdr;
77190Sstevel@tonic-gate
77200Sstevel@tonic-gate buff = e_zalloc(E_EXIT, (uint_t)Bufsize);
77210Sstevel@tonic-gate
77220Sstevel@tonic-gate if (g_read(Device, Archive, buff, Bufsize) < 0) {
77230Sstevel@tonic-gate (void) fprintf(stderr, gettext(
77240Sstevel@tonic-gate "error in skip_bar_volhdr\n"));
77250Sstevel@tonic-gate } else {
77260Sstevel@tonic-gate
77270Sstevel@tonic-gate tmp_hdr = (union b_block *)buff;
77280Sstevel@tonic-gate if (tmp_hdr->dbuf.bar_magic[0] == BAR_VOLUME_MAGIC) {
77290Sstevel@tonic-gate
77300Sstevel@tonic-gate if (bar_Vhdr == NULL) {
77310Sstevel@tonic-gate bar_Vhdr = e_zalloc(E_EXIT, TBLOCK);
77320Sstevel@tonic-gate }
77330Sstevel@tonic-gate (void) memcpy(&(bar_Vhdr->dbuf),
77340Sstevel@tonic-gate &(tmp_hdr->dbuf), TBLOCK);
77350Sstevel@tonic-gate } else {
77360Sstevel@tonic-gate (void) fprintf(stderr,
77370Sstevel@tonic-gate gettext("cpio error: cannot read bar volume "
77380Sstevel@tonic-gate "header\n"));
77390Sstevel@tonic-gate exit(1);
77400Sstevel@tonic-gate }
77410Sstevel@tonic-gate
77420Sstevel@tonic-gate (void) sscanf(bar_Vhdr->dbuf.mode, "%8lo",
77430Sstevel@tonic-gate &Gen_bar_vol.g_mode);
77440Sstevel@tonic-gate (void) sscanf(bar_Vhdr->dbuf.uid, "%8lo",
77450Sstevel@tonic-gate &Gen_bar_vol.g_uid);
77460Sstevel@tonic-gate (void) sscanf(bar_Vhdr->dbuf.gid, "%8lo",
77470Sstevel@tonic-gate &Gen_bar_vol.g_gid);
77480Sstevel@tonic-gate (void) sscanf(bar_Vhdr->dbuf.size, "%12llo",
77490Sstevel@tonic-gate (u_off_t *)&Gen_bar_vol.g_filesz);
77500Sstevel@tonic-gate (void) sscanf(bar_Vhdr->dbuf.mtime, "%12lo",
77510Sstevel@tonic-gate &Gen_bar_vol.g_mtime);
77520Sstevel@tonic-gate (void) sscanf(bar_Vhdr->dbuf.chksum, "%8lo",
77530Sstevel@tonic-gate &Gen_bar_vol.g_cksum);
77540Sstevel@tonic-gate if (bar_Vhdr->dbuf.compressed == '1')
77550Sstevel@tonic-gate Compressed = 1;
77560Sstevel@tonic-gate else
77570Sstevel@tonic-gate Compressed = 0;
77580Sstevel@tonic-gate }
77590Sstevel@tonic-gate
77600Sstevel@tonic-gate /*
77610Sstevel@tonic-gate * Now put the rest of the bytes read in into the data buffer.
77620Sstevel@tonic-gate */
77630Sstevel@tonic-gate (void) memcpy(Buffr.b_in_p, &buff[512], (Bufsize - 512));
77640Sstevel@tonic-gate Buffr.b_in_p += (Bufsize - 512);
77650Sstevel@tonic-gate Buffr.b_cnt += (long)(Bufsize - 512);
77660Sstevel@tonic-gate
77670Sstevel@tonic-gate free(buff);
77680Sstevel@tonic-gate }
77690Sstevel@tonic-gate
77700Sstevel@tonic-gate /*
77710Sstevel@tonic-gate * check the linkflag which indicates the type of the file to be extracted,
77720Sstevel@tonic-gate * invoke the corresponding routine to extract the file.
77730Sstevel@tonic-gate */
77740Sstevel@tonic-gate static void
bar_file_in(void)77750Sstevel@tonic-gate bar_file_in(void)
77760Sstevel@tonic-gate {
77770Sstevel@tonic-gate /*
77780Sstevel@tonic-gate * the file is a directory
77790Sstevel@tonic-gate */
77800Sstevel@tonic-gate if (Adir) {
77810Sstevel@tonic-gate if (ckname(1) != F_SKIP && creat_spec(G_p->g_dirfd) > 0) {
77820Sstevel@tonic-gate VERBOSE((Args & (OCv | OCV)), G_p->g_nam_p);
77830Sstevel@tonic-gate }
77840Sstevel@tonic-gate return;
77850Sstevel@tonic-gate }
77860Sstevel@tonic-gate
77870Sstevel@tonic-gate switch (bar_linkflag) {
77880Sstevel@tonic-gate case REGTYPE:
77890Sstevel@tonic-gate /* regular file */
77900Sstevel@tonic-gate if ((ckname(1) == F_SKIP) ||
77910Sstevel@tonic-gate (Ofile = openout(G_p->g_dirfd)) < 0) {
77920Sstevel@tonic-gate data_in(P_SKIP);
77930Sstevel@tonic-gate } else {
77940Sstevel@tonic-gate data_in(P_PROC);
77950Sstevel@tonic-gate }
77960Sstevel@tonic-gate break;
77970Sstevel@tonic-gate case LNKTYPE:
77980Sstevel@tonic-gate /* hard link */
77990Sstevel@tonic-gate if (ckname(1) == F_SKIP) {
78000Sstevel@tonic-gate break;
78010Sstevel@tonic-gate }
78020Sstevel@tonic-gate (void) creat_lnk(G_p->g_dirfd, bar_linkname, G_p->g_nam_p);
78030Sstevel@tonic-gate break;
78040Sstevel@tonic-gate case SYMTYPE:
78050Sstevel@tonic-gate /* symbolic link */
78060Sstevel@tonic-gate if ((ckname(1) == F_SKIP) ||
78070Sstevel@tonic-gate (Ofile = openout(G_p->g_dirfd)) < 0) {
78080Sstevel@tonic-gate data_in(P_SKIP);
78090Sstevel@tonic-gate } else {
78100Sstevel@tonic-gate data_in(P_PROC);
78110Sstevel@tonic-gate }
78120Sstevel@tonic-gate break;
78130Sstevel@tonic-gate case CHRTYPE:
78140Sstevel@tonic-gate /* character device or FIFO */
78150Sstevel@tonic-gate if (ckname(1) != F_SKIP && creat_spec(G_p->g_dirfd) > 0) {
78160Sstevel@tonic-gate VERBOSE((Args & (OCv | OCV)), G_p->g_nam_p);
78170Sstevel@tonic-gate }
78180Sstevel@tonic-gate break;
78190Sstevel@tonic-gate default:
78200Sstevel@tonic-gate (void) fprintf(stderr, gettext("error: unknown file type\n"));
78210Sstevel@tonic-gate break;
78220Sstevel@tonic-gate }
78230Sstevel@tonic-gate }
78240Sstevel@tonic-gate
78250Sstevel@tonic-gate
78260Sstevel@tonic-gate /*
78270Sstevel@tonic-gate * This originally came from libgenIO/g_init.c
78280Sstevel@tonic-gate * XXX And it is very broken.
78290Sstevel@tonic-gate */
78300Sstevel@tonic-gate
78310Sstevel@tonic-gate /* #include <sys/statvfs.h> */
78320Sstevel@tonic-gate #include <ftw.h>
78330Sstevel@tonic-gate /* #include <libgenIO.h> */
78340Sstevel@tonic-gate #define G_TM_TAPE 1 /* Tapemaster controller */
78350Sstevel@tonic-gate #define G_XY_DISK 3 /* xy disks */
78360Sstevel@tonic-gate #define G_SD_DISK 7 /* scsi sd disk */
78370Sstevel@tonic-gate #define G_XT_TAPE 8 /* xt tapes */
78380Sstevel@tonic-gate #define G_SF_FLOPPY 9 /* sf floppy */
78390Sstevel@tonic-gate #define G_XD_DISK 10 /* xd disks */
78400Sstevel@tonic-gate #define G_ST_TAPE 11 /* scsi tape */
78410Sstevel@tonic-gate #define G_NS 12 /* noswap pseudo-dev */
78420Sstevel@tonic-gate #define G_RAM 13 /* ram pseudo-dev */
78430Sstevel@tonic-gate #define G_FT 14 /* tftp */
78440Sstevel@tonic-gate #define G_HD 15 /* 386 network disk */
78450Sstevel@tonic-gate #define G_FD 16 /* 386 AT disk */
78460Sstevel@tonic-gate #define G_FILE 28 /* file, not a device */
78470Sstevel@tonic-gate #define G_NO_DEV 29 /* device does not require special treatment */
78480Sstevel@tonic-gate #define G_DEV_MAX 30 /* last valid device type */
78490Sstevel@tonic-gate
78500Sstevel@tonic-gate /*
78510Sstevel@tonic-gate * g_init: Determine the device being accessed, set the buffer size,
78520Sstevel@tonic-gate * and perform any device specific initialization. Since at this point
78530Sstevel@tonic-gate * Sun has no system call to read the configuration, the major numbers
78540Sstevel@tonic-gate * are assumed to be static and types are figured out as such. However,
78550Sstevel@tonic-gate * as a rough estimate, the buffer size for all types is set to 512
78560Sstevel@tonic-gate * as a default.
78570Sstevel@tonic-gate */
78580Sstevel@tonic-gate
78590Sstevel@tonic-gate static int
g_init(int * devtype,int * fdes)78600Sstevel@tonic-gate g_init(int *devtype, int *fdes)
78610Sstevel@tonic-gate {
78620Sstevel@tonic-gate int bufsize;
78630Sstevel@tonic-gate struct stat st_buf;
78640Sstevel@tonic-gate struct statvfs stfs_buf;
78650Sstevel@tonic-gate
78660Sstevel@tonic-gate *devtype = G_NO_DEV;
78670Sstevel@tonic-gate bufsize = -1;
78680Sstevel@tonic-gate if (fstat(*fdes, &st_buf) == -1)
78690Sstevel@tonic-gate return (-1);
7870871Scasper if (!S_ISCHR(st_buf.st_mode) && !S_ISBLK(st_buf.st_mode)) {
7871871Scasper if (S_ISFIFO(st_buf.st_mode)) {
78720Sstevel@tonic-gate bufsize = 512;
78730Sstevel@tonic-gate } else {
78740Sstevel@tonic-gate /* find block size for this file system */
78750Sstevel@tonic-gate *devtype = G_FILE;
78760Sstevel@tonic-gate if (fstatvfs(*fdes, &stfs_buf) < 0) {
78770Sstevel@tonic-gate bufsize = -1;
78780Sstevel@tonic-gate errno = ENODEV;
78790Sstevel@tonic-gate } else
78800Sstevel@tonic-gate bufsize = stfs_buf.f_bsize;
78810Sstevel@tonic-gate }
78820Sstevel@tonic-gate
78830Sstevel@tonic-gate return (bufsize);
78840Sstevel@tonic-gate
78850Sstevel@tonic-gate /*
78860Sstevel@tonic-gate * We'll have to add a remote attribute to stat but this
78870Sstevel@tonic-gate * should work for now.
78880Sstevel@tonic-gate */
78890Sstevel@tonic-gate } else if (st_buf.st_dev & 0x8000) /* if remote rdev */
78900Sstevel@tonic-gate return (512);
78910Sstevel@tonic-gate
78920Sstevel@tonic-gate bufsize = 512;
78930Sstevel@tonic-gate
78940Sstevel@tonic-gate if (Hdr_type == BAR) {
78950Sstevel@tonic-gate if (is_tape(*fdes)) {
78960Sstevel@tonic-gate bufsize = BAR_TAPE_SIZE;
7897*12835Srich.burridge@oracle.com msg(EPOST, "Archiving to tape blocking factor 126");
78980Sstevel@tonic-gate } else if (is_floppy(*fdes)) {
78990Sstevel@tonic-gate bufsize = BAR_FLOPPY_SIZE;
7900*12835Srich.burridge@oracle.com msg(EPOST, "Archiving to floppy blocking factor 18");
79010Sstevel@tonic-gate }
79020Sstevel@tonic-gate }
79030Sstevel@tonic-gate
79040Sstevel@tonic-gate return (bufsize);
79050Sstevel@tonic-gate }
79060Sstevel@tonic-gate
79070Sstevel@tonic-gate /*
79080Sstevel@tonic-gate * This originally came from libgenIO/g_read.c
79090Sstevel@tonic-gate */
79100Sstevel@tonic-gate
79110Sstevel@tonic-gate /*
79120Sstevel@tonic-gate * g_read: Read nbytes of data from fdes (of type devtype) and place
79130Sstevel@tonic-gate * data in location pointed to by buf. In case of end of medium,
79140Sstevel@tonic-gate * translate (where necessary) device specific EOM indications into
79150Sstevel@tonic-gate * the generic EOM indication of rv = -1, errno = ENOSPC.
79160Sstevel@tonic-gate */
79170Sstevel@tonic-gate
79180Sstevel@tonic-gate static int
g_read(int devtype,int fdes,char * buf,unsigned nbytes)79190Sstevel@tonic-gate g_read(int devtype, int fdes, char *buf, unsigned nbytes)
79200Sstevel@tonic-gate {
79210Sstevel@tonic-gate int rv;
79220Sstevel@tonic-gate
79230Sstevel@tonic-gate if (devtype < 0 || devtype >= G_DEV_MAX) {
79240Sstevel@tonic-gate errno = ENODEV;
79250Sstevel@tonic-gate return (-1);
79260Sstevel@tonic-gate }
79270Sstevel@tonic-gate
79280Sstevel@tonic-gate rv = read(fdes, buf, nbytes);
79290Sstevel@tonic-gate
79300Sstevel@tonic-gate /* st devices return 0 when no space left */
79310Sstevel@tonic-gate if ((rv == 0 && errno == 0 && Hdr_type != BAR) ||
79320Sstevel@tonic-gate (rv == -1 && errno == EIO)) {
79330Sstevel@tonic-gate errno = 0;
79340Sstevel@tonic-gate rv = 0;
79350Sstevel@tonic-gate }
79360Sstevel@tonic-gate
79370Sstevel@tonic-gate return (rv);
79380Sstevel@tonic-gate }
79390Sstevel@tonic-gate
79400Sstevel@tonic-gate /*
79410Sstevel@tonic-gate * This originally came from libgenIO/g_write.c
79420Sstevel@tonic-gate */
79430Sstevel@tonic-gate
79440Sstevel@tonic-gate /*
79450Sstevel@tonic-gate * g_write: Write nbytes of data to fdes (of type devtype) from
79460Sstevel@tonic-gate * the location pointed to by buf. In case of end of medium,
79470Sstevel@tonic-gate * translate (where necessary) device specific EOM indications into
79480Sstevel@tonic-gate * the generic EOM indication of rv = -1, errno = ENOSPC.
79490Sstevel@tonic-gate */
79500Sstevel@tonic-gate
79510Sstevel@tonic-gate static int
g_write(int devtype,int fdes,char * buf,unsigned nbytes)79520Sstevel@tonic-gate g_write(int devtype, int fdes, char *buf, unsigned nbytes)
79530Sstevel@tonic-gate {
79540Sstevel@tonic-gate int rv;
79550Sstevel@tonic-gate
79560Sstevel@tonic-gate if (devtype < 0 || devtype >= G_DEV_MAX) {
79570Sstevel@tonic-gate errno = ENODEV;
79580Sstevel@tonic-gate return (-1);
79590Sstevel@tonic-gate }
79600Sstevel@tonic-gate
79610Sstevel@tonic-gate rv = write(fdes, buf, nbytes);
79620Sstevel@tonic-gate
79630Sstevel@tonic-gate /* st devices return 0 when no more space left */
79640Sstevel@tonic-gate if ((rv == 0 && errno == 0) || (rv == -1 && errno == EIO)) {
79650Sstevel@tonic-gate errno = ENOSPC;
79660Sstevel@tonic-gate rv = -1;
79670Sstevel@tonic-gate }
79680Sstevel@tonic-gate
79690Sstevel@tonic-gate return (rv);
79700Sstevel@tonic-gate }
79710Sstevel@tonic-gate
79720Sstevel@tonic-gate /*
79730Sstevel@tonic-gate * Test for tape
79740Sstevel@tonic-gate */
79750Sstevel@tonic-gate
79760Sstevel@tonic-gate static int
is_tape(int fd)79770Sstevel@tonic-gate is_tape(int fd)
79780Sstevel@tonic-gate {
79790Sstevel@tonic-gate struct mtget stuff;
79800Sstevel@tonic-gate
79810Sstevel@tonic-gate /*
79820Sstevel@tonic-gate * try to do a generic tape ioctl, just to see if
79830Sstevel@tonic-gate * the thing is in fact a tape drive(er).
79840Sstevel@tonic-gate */
79850Sstevel@tonic-gate if (ioctl(fd, MTIOCGET, &stuff) != -1) {
79860Sstevel@tonic-gate /* the ioctl succeeded, must have been a tape */
79870Sstevel@tonic-gate return (1);
79880Sstevel@tonic-gate }
79890Sstevel@tonic-gate return (0);
79900Sstevel@tonic-gate }
79910Sstevel@tonic-gate
79920Sstevel@tonic-gate /*
79930Sstevel@tonic-gate * Test for floppy
79940Sstevel@tonic-gate */
79950Sstevel@tonic-gate
79960Sstevel@tonic-gate static int
is_floppy(int fd)79970Sstevel@tonic-gate is_floppy(int fd)
79980Sstevel@tonic-gate {
79990Sstevel@tonic-gate struct fd_char stuff;
80000Sstevel@tonic-gate
80010Sstevel@tonic-gate /*
80020Sstevel@tonic-gate * try to get the floppy drive characteristics, just to see if
80030Sstevel@tonic-gate * the thing is in fact a floppy drive(er).
80040Sstevel@tonic-gate */
80050Sstevel@tonic-gate if (ioctl(fd, FDIOGCHAR, &stuff) != -1) {
80060Sstevel@tonic-gate /* the ioctl succeeded, must have been a floppy */
80070Sstevel@tonic-gate return (1);
80080Sstevel@tonic-gate }
80090Sstevel@tonic-gate
80100Sstevel@tonic-gate return (0);
80110Sstevel@tonic-gate }
80120Sstevel@tonic-gate
80130Sstevel@tonic-gate /*
80140Sstevel@tonic-gate * New functions for ACLs and other security attributes
80150Sstevel@tonic-gate */
80160Sstevel@tonic-gate
80170Sstevel@tonic-gate /*
80180Sstevel@tonic-gate * The function appends the new security attribute info to the end of
80190Sstevel@tonic-gate * existing secinfo.
80200Sstevel@tonic-gate */
80210Sstevel@tonic-gate static int
append_secattr(char ** secinfo,int * secinfo_len,acl_t * aclp)80220Sstevel@tonic-gate append_secattr(
8023789Sahrens char **secinfo, /* existing security info */
8024789Sahrens int *secinfo_len, /* length of existing security info */
8025789Sahrens acl_t *aclp) /* new attribute data pointer */
80260Sstevel@tonic-gate {
80270Sstevel@tonic-gate char *new_secinfo;
80280Sstevel@tonic-gate char *attrtext;
80290Sstevel@tonic-gate size_t newattrsize;
80300Sstevel@tonic-gate int oldsize;
80310Sstevel@tonic-gate
80320Sstevel@tonic-gate /* no need to add */
8033789Sahrens if (aclp == NULL) {
80340Sstevel@tonic-gate return (0);
80350Sstevel@tonic-gate }
80360Sstevel@tonic-gate
8037789Sahrens switch (acl_type(aclp)) {
8038789Sahrens case ACLENT_T:
8039789Sahrens case ACE_T:
80407057Smarks attrtext = acl_totext(aclp, ACL_APPEND_ID | ACL_COMPACT_FMT |
80417057Smarks ACL_SID_FMT);
80420Sstevel@tonic-gate if (attrtext == NULL) {
8043*12835Srich.burridge@oracle.com msg(EPOST, "acltotext failed");
80440Sstevel@tonic-gate return (-1);
80450Sstevel@tonic-gate }
80460Sstevel@tonic-gate /* header: type + size = 8 */
80470Sstevel@tonic-gate newattrsize = 8 + strlen(attrtext) + 1;
80480Sstevel@tonic-gate attr = e_zalloc(E_NORMAL, newattrsize);
80490Sstevel@tonic-gate if (attr == NULL) {
8050*12835Srich.burridge@oracle.com msg(EPOST, "can't allocate memory");
80510Sstevel@tonic-gate return (-1);
80520Sstevel@tonic-gate }
8053789Sahrens attr->attr_type = (acl_type(aclp) == ACLENT_T) ?
8054789Sahrens UFSD_ACL : ACE_ACL;
80550Sstevel@tonic-gate /* acl entry count */
8056789Sahrens (void) sprintf(attr->attr_len, "%06o", acl_cnt(aclp));
80570Sstevel@tonic-gate (void) strcpy((char *)&attr->attr_info[0], attrtext);
80580Sstevel@tonic-gate free(attrtext);
80590Sstevel@tonic-gate break;
80600Sstevel@tonic-gate
80610Sstevel@tonic-gate /* SunFed's case goes here */
80620Sstevel@tonic-gate
80630Sstevel@tonic-gate default:
8064*12835Srich.burridge@oracle.com msg(EPOST, "unrecognized attribute type");
80650Sstevel@tonic-gate return (-1);
80660Sstevel@tonic-gate }
80670Sstevel@tonic-gate
80680Sstevel@tonic-gate /* old security info + new attr header(8) + new attr */
80690Sstevel@tonic-gate oldsize = *secinfo_len;
80700Sstevel@tonic-gate *secinfo_len += newattrsize;
80710Sstevel@tonic-gate new_secinfo = e_zalloc(E_NORMAL, (uint_t)*secinfo_len);
80720Sstevel@tonic-gate if (new_secinfo == NULL) {
8073*12835Srich.burridge@oracle.com msg(EPOST, "can't allocate memory");
80740Sstevel@tonic-gate *secinfo_len -= newattrsize;
80750Sstevel@tonic-gate return (-1);
80760Sstevel@tonic-gate }
80770Sstevel@tonic-gate
80780Sstevel@tonic-gate (void) memcpy(new_secinfo, *secinfo, oldsize);
80790Sstevel@tonic-gate (void) memcpy(new_secinfo + oldsize, attr, newattrsize);
80800Sstevel@tonic-gate
80810Sstevel@tonic-gate free(*secinfo);
80820Sstevel@tonic-gate *secinfo = new_secinfo;
80830Sstevel@tonic-gate return (0);
80840Sstevel@tonic-gate }
80850Sstevel@tonic-gate
808610202SNobutomo.Nakano@Sun.COM /*
808710202SNobutomo.Nakano@Sun.COM * Append size amount of data from buf to the archive.
808810202SNobutomo.Nakano@Sun.COM */
80890Sstevel@tonic-gate static void
write_ancillary(char * buf,size_t len,boolean_t padding)809010202SNobutomo.Nakano@Sun.COM write_ancillary(char *buf, size_t len, boolean_t padding)
809110202SNobutomo.Nakano@Sun.COM {
809210202SNobutomo.Nakano@Sun.COM int pad, cnt;
809310202SNobutomo.Nakano@Sun.COM
809410202SNobutomo.Nakano@Sun.COM if (len == 0)
80950Sstevel@tonic-gate return;
809610202SNobutomo.Nakano@Sun.COM
80970Sstevel@tonic-gate while (len > 0) {
80980Sstevel@tonic-gate cnt = (unsigned)(len > CPIOBSZ) ? CPIOBSZ : len;
80990Sstevel@tonic-gate FLUSH(cnt);
81000Sstevel@tonic-gate errno = 0;
810110202SNobutomo.Nakano@Sun.COM (void) memcpy(Buffr.b_in_p, buf, (unsigned)cnt);
81020Sstevel@tonic-gate Buffr.b_in_p += cnt;
810310202SNobutomo.Nakano@Sun.COM Buffr.b_cnt += cnt;
810410202SNobutomo.Nakano@Sun.COM len -= cnt;
810510202SNobutomo.Nakano@Sun.COM buf += cnt;
810610202SNobutomo.Nakano@Sun.COM }
810710202SNobutomo.Nakano@Sun.COM if (padding) {
810810202SNobutomo.Nakano@Sun.COM pad = (Pad_val + 1 - (cnt & Pad_val)) & Pad_val;
810910202SNobutomo.Nakano@Sun.COM if (pad != 0) {
811010202SNobutomo.Nakano@Sun.COM FLUSH(pad);
811110202SNobutomo.Nakano@Sun.COM (void) memset(Buffr.b_in_p, 0, pad);
811210202SNobutomo.Nakano@Sun.COM Buffr.b_in_p += pad;
811310202SNobutomo.Nakano@Sun.COM Buffr.b_cnt += pad;
811410202SNobutomo.Nakano@Sun.COM }
81150Sstevel@tonic-gate }
81160Sstevel@tonic-gate }
81170Sstevel@tonic-gate
81180Sstevel@tonic-gate static int
remove_dir(char * path)81190Sstevel@tonic-gate remove_dir(char *path)
81200Sstevel@tonic-gate {
81210Sstevel@tonic-gate DIR *name;
81220Sstevel@tonic-gate struct dirent *direct;
81230Sstevel@tonic-gate struct stat sbuf;
81240Sstevel@tonic-gate char *path_copy;
81250Sstevel@tonic-gate
81260Sstevel@tonic-gate #define MSG1 "remove_dir() failed to stat(\"%s\") "
81270Sstevel@tonic-gate #define MSG2 "remove_dir() failed to remove_dir(\"%s\") "
81280Sstevel@tonic-gate #define MSG3 "remove_dir() failed to unlink(\"%s\") "
81290Sstevel@tonic-gate
81300Sstevel@tonic-gate /*
81310Sstevel@tonic-gate * Open the directory for reading.
81320Sstevel@tonic-gate */
81330Sstevel@tonic-gate if ((name = opendir(path)) == NULL) {
81340Sstevel@tonic-gate msg(ERRN, "remove_dir() failed to opendir(\"%s\") ", path);
81350Sstevel@tonic-gate return (-1);
81360Sstevel@tonic-gate }
81370Sstevel@tonic-gate
81380Sstevel@tonic-gate if (chdir(path) == -1) {
81390Sstevel@tonic-gate msg(ERRN, "remove_dir() failed to chdir(\"%s\") ", path);
81400Sstevel@tonic-gate return (-1);
81410Sstevel@tonic-gate }
81420Sstevel@tonic-gate
81430Sstevel@tonic-gate /*
81440Sstevel@tonic-gate * Read every directory entry.
81450Sstevel@tonic-gate */
81460Sstevel@tonic-gate while ((direct = readdir(name)) != NULL) {
81470Sstevel@tonic-gate /*
81480Sstevel@tonic-gate * Ignore "." and ".." entries.
81490Sstevel@tonic-gate */
81500Sstevel@tonic-gate if (strcmp(direct->d_name, ".") == 0 ||
81510Sstevel@tonic-gate strcmp(direct->d_name, "..") == 0)
81520Sstevel@tonic-gate continue;
81530Sstevel@tonic-gate
81540Sstevel@tonic-gate if (lstat(direct->d_name, &sbuf) == -1) {
81550Sstevel@tonic-gate msg(ERRN, MSG1, direct->d_name);
81560Sstevel@tonic-gate (void) closedir(name);
81570Sstevel@tonic-gate return (-1);
81580Sstevel@tonic-gate }
81590Sstevel@tonic-gate
81600Sstevel@tonic-gate if (S_ISDIR(sbuf.st_mode)) {
81610Sstevel@tonic-gate if (remove_dir(direct->d_name) == -1) {
81620Sstevel@tonic-gate msg(ERRN, MSG2, direct->d_name);
81630Sstevel@tonic-gate (void) closedir(name);
81640Sstevel@tonic-gate return (-1);
81650Sstevel@tonic-gate }
81660Sstevel@tonic-gate } else {
81670Sstevel@tonic-gate if (unlink(direct->d_name) == -1) {
81680Sstevel@tonic-gate msg(ERRN, MSG3, direct->d_name);
81690Sstevel@tonic-gate (void) closedir(name);
81700Sstevel@tonic-gate return (-1);
81710Sstevel@tonic-gate }
81720Sstevel@tonic-gate }
81730Sstevel@tonic-gate
81740Sstevel@tonic-gate }
81750Sstevel@tonic-gate
81760Sstevel@tonic-gate /*
81770Sstevel@tonic-gate * Close the directory we just finished reading.
81780Sstevel@tonic-gate */
81790Sstevel@tonic-gate (void) closedir(name);
81800Sstevel@tonic-gate
81810Sstevel@tonic-gate /*
81820Sstevel@tonic-gate * Change directory to the parent directory...
81830Sstevel@tonic-gate */
81840Sstevel@tonic-gate if (chdir("..") == -1) {
81850Sstevel@tonic-gate msg(ERRN, "remove_dir() failed to chdir(\"..\") ");
81860Sstevel@tonic-gate return (-1);
81870Sstevel@tonic-gate }
81880Sstevel@tonic-gate
81890Sstevel@tonic-gate /*
81900Sstevel@tonic-gate * ...and finally remove the directory; note we have to
81910Sstevel@tonic-gate * make a copy since basename is free to modify its input.
81920Sstevel@tonic-gate */
81930Sstevel@tonic-gate path_copy = e_strdup(E_NORMAL, path);
81940Sstevel@tonic-gate if (path_copy == NULL) {
81950Sstevel@tonic-gate msg(ERRN, "cannot strdup() the directory pathname ");
81960Sstevel@tonic-gate return (-1);
81970Sstevel@tonic-gate }
81980Sstevel@tonic-gate
81990Sstevel@tonic-gate if (rmdir(basename(path_copy)) == -1) {
82000Sstevel@tonic-gate free(path_copy);
82010Sstevel@tonic-gate msg(ERRN, "remove_dir() failed to rmdir(\"%s\") ", path);
82020Sstevel@tonic-gate return (-1);
82030Sstevel@tonic-gate }
82040Sstevel@tonic-gate
82050Sstevel@tonic-gate free(path_copy);
82060Sstevel@tonic-gate return (0);
82070Sstevel@tonic-gate
82080Sstevel@tonic-gate }
82090Sstevel@tonic-gate
82100Sstevel@tonic-gate static int
save_cwd(void)82110Sstevel@tonic-gate save_cwd(void)
82120Sstevel@tonic-gate {
82130Sstevel@tonic-gate return (open(".", O_RDONLY));
82140Sstevel@tonic-gate }
82150Sstevel@tonic-gate
82160Sstevel@tonic-gate static void
rest_cwd(int cwd)82170Sstevel@tonic-gate rest_cwd(int cwd)
82180Sstevel@tonic-gate {
82190Sstevel@tonic-gate (void) fchdir(cwd);
82200Sstevel@tonic-gate (void) close(cwd);
82210Sstevel@tonic-gate }
82220Sstevel@tonic-gate
82230Sstevel@tonic-gate #if defined(O_XATTR)
82240Sstevel@tonic-gate static void
xattrs_out(int (* func)())82250Sstevel@tonic-gate xattrs_out(int (*func)())
82260Sstevel@tonic-gate {
82270Sstevel@tonic-gate int dirpfd;
82280Sstevel@tonic-gate int filefd;
82295750Sceastha int arc_rwsysattr = 0;
82305750Sceastha int rw_sysattr = 0;
82315750Sceastha int ext_attr = 0;
82320Sstevel@tonic-gate DIR *dirp;
82330Sstevel@tonic-gate struct dirent *dp;
82340Sstevel@tonic-gate int slen;
82355750Sceastha int plen;
82360Sstevel@tonic-gate char *namep, *savenamep;
82375750Sceastha char *apathp;
82385750Sceastha char *attrparent = Gen.g_attrparent_p;
82395750Sceastha char *filename;
82405750Sceastha
82415750Sceastha if (attrparent == NULL) {
82425750Sceastha filename = Gen.g_nam_p;
82435750Sceastha } else {
82445750Sceastha filename = Gen.g_attrnam_p;
82455750Sceastha }
82465750Sceastha
82475750Sceastha /*
82485750Sceastha * If the underlying file system supports it, then
82495750Sceastha * archive the extended attributes if -@ was specified,
82505750Sceastha * and the extended system attributes if -/ was
82515750Sceastha * specified.
82525750Sceastha */
82535750Sceastha if (verify_attr_support(filename, (attrparent == NULL), ARC_CREATE,
82545750Sceastha &ext_attr) != ATTR_OK) {
82550Sstevel@tonic-gate return;
82560Sstevel@tonic-gate }
82570Sstevel@tonic-gate
82585750Sceastha #if defined(_PC_SATTR_ENABLED)
82595750Sceastha if (SysAtflag) {
82605750Sceastha int filefd;
82615750Sceastha nvlist_t *slist = NULL;
82625750Sceastha
82635750Sceastha /*
82645750Sceastha * Determine if there are non-transient system
82655750Sceastha * attributes.
82665750Sceastha */
82675750Sceastha errno = 0;
82685750Sceastha if ((filefd = open(filename, O_RDONLY)) == -1) {
82695750Sceastha if (attrparent == NULL) {
82705750Sceastha msg(EXTN,
82715750Sceastha "unable to open %s%s%sfile %s",
82725750Sceastha (attrparent == NULL) ? "" :
82735750Sceastha gettext("attribute "),
82745750Sceastha (attrparent == NULL) ? "" : attrparent,
82755750Sceastha (attrparent == NULL) ? "" : gettext(" of "),
82765750Sceastha (attrparent == NULL) ? G_p->g_nam_p :
82775750Sceastha G_p->g_attrfnam_p);
82785750Sceastha }
82795750Sceastha }
82805750Sceastha if (((slist = sysattr_list(myname, filefd,
82815750Sceastha filename)) != NULL) || (errno != 0)) {
82825750Sceastha arc_rwsysattr = 1;
82835750Sceastha }
82845750Sceastha if (slist != NULL) {
82855750Sceastha (void) nvlist_free(slist);
82865750Sceastha slist = NULL;
82875750Sceastha }
82885750Sceastha (void) close(filefd);
82895750Sceastha }
82905750Sceastha
82915750Sceastha /*
82925750Sceastha * If we aren't archiving extended system attributes, and we are
82935750Sceastha * processing an attribute, or if we are archiving extended system
82945750Sceastha * attributes, and there are are no extended attributes, then there's
82955750Sceastha * no need to open up the attribute directory of the file unless the
82965750Sceastha * extended system attributes are not transient (i.e, the system
82975750Sceastha * attributes are not the default values).
82985750Sceastha */
82995750Sceastha if ((arc_rwsysattr == 0) && ((attrparent != NULL) ||
83005750Sceastha (SysAtflag && !ext_attr))) {
83015750Sceastha return;
83025750Sceastha }
83035750Sceastha
83045750Sceastha #endif /* _PC_SATTR_ENABLED */
83055750Sceastha
83060Sstevel@tonic-gate /*
83070Sstevel@tonic-gate * If aclp still exists then free it since it is was set when base
83080Sstevel@tonic-gate * file was extracted.
83090Sstevel@tonic-gate */
8310789Sahrens if (aclp != NULL) {
8311789Sahrens acl_free(aclp);
83120Sstevel@tonic-gate aclp = NULL;
8313789Sahrens acl_is_set = 0;
83140Sstevel@tonic-gate }
83150Sstevel@tonic-gate
83165750Sceastha Gen.g_dirfd = attropen(filename, ".", O_RDONLY);
83170Sstevel@tonic-gate if (Gen.g_dirfd == -1) {
83185750Sceastha msg(ERRN, "Cannot open attribute directory of file \"%s%s%s\"",
83195750Sceastha (attrparent == NULL) ? "" : gettext("attribute "),
83205750Sceastha (attrparent == NULL) ? "" : attrparent,
83215750Sceastha (attrparent == NULL) ? "" : gettext(" of "), filename);
83220Sstevel@tonic-gate return;
83230Sstevel@tonic-gate
83240Sstevel@tonic-gate }
83255750Sceastha
83265750Sceastha if (attrparent == NULL) {
83275750Sceastha savenamep = G_p->g_nam_p;
83285750Sceastha } else {
83295750Sceastha savenamep = G_p->g_attrfnam_p;
83305750Sceastha }
83310Sstevel@tonic-gate
83320Sstevel@tonic-gate if ((dirpfd = dup(Gen.g_dirfd)) == -1) {
83330Sstevel@tonic-gate msg(ERRN, "Cannot dup(2) attribute directory descriptor");
83340Sstevel@tonic-gate return;
83350Sstevel@tonic-gate }
83360Sstevel@tonic-gate
83375750Sceastha if ((dirp = fdopendir(dirpfd)) == NULL) {
83380Sstevel@tonic-gate msg(ERRN, "Cannot fdopendir(2) directory file descriptor");
83390Sstevel@tonic-gate return;
83400Sstevel@tonic-gate }
83410Sstevel@tonic-gate
83425750Sceastha if (attrparent == NULL) {
83435750Sceastha Gen.g_baseparent_fd = save_cwd();
83445750Sceastha }
83455750Sceastha
83465750Sceastha while ((dp = readdir(dirp)) != NULL) {
83475750Sceastha if (strcmp(dp->d_name, "..") == 0) {
83485750Sceastha continue;
83495750Sceastha }
83505750Sceastha if (verify_attr(dp->d_name, attrparent,
83515750Sceastha arc_rwsysattr, &rw_sysattr) != ATTR_OK) {
83520Sstevel@tonic-gate continue;
83530Sstevel@tonic-gate }
83540Sstevel@tonic-gate
83550Sstevel@tonic-gate if (strcmp(dp->d_name, ".") == 0) {
83560Sstevel@tonic-gate Hiddendir = 1;
83570Sstevel@tonic-gate } else {
83580Sstevel@tonic-gate Hiddendir = 0;
83590Sstevel@tonic-gate }
83600Sstevel@tonic-gate
83615750Sceastha Gen.g_rw_sysattr = rw_sysattr;
83620Sstevel@tonic-gate Gen.g_attrnam_p = dp->d_name;
83630Sstevel@tonic-gate
83640Sstevel@tonic-gate if (STAT(Gen.g_dirfd, Gen.g_nam_p, &SrcSt) == -1) {
83650Sstevel@tonic-gate msg(ERRN,
83660Sstevel@tonic-gate "Could not fstatat(2) attribute \"%s\" of"
83675750Sceastha " file \"%s\"", dp->d_name, (attrparent == NULL) ?
83685750Sceastha savenamep : Gen.g_attrfnam_p);
83690Sstevel@tonic-gate continue;
83700Sstevel@tonic-gate }
83710Sstevel@tonic-gate
83720Sstevel@tonic-gate if (Use_old_stat) {
83735750Sceastha Savedev = SrcSt.st_dev;
83740Sstevel@tonic-gate OldSt = convert_to_old_stat(&SrcSt,
83750Sstevel@tonic-gate Gen.g_nam_p, Gen.g_attrnam_p);
83760Sstevel@tonic-gate
83770Sstevel@tonic-gate if (OldSt == NULL) {
83780Sstevel@tonic-gate msg(ERRN,
83790Sstevel@tonic-gate "Could not convert to old stat format");
83800Sstevel@tonic-gate continue;
83810Sstevel@tonic-gate }
83820Sstevel@tonic-gate }
83830Sstevel@tonic-gate
83840Sstevel@tonic-gate Gen.g_attrfnam_p = savenamep;
83850Sstevel@tonic-gate
83860Sstevel@tonic-gate /*
83870Sstevel@tonic-gate * Set up dummy header name
83880Sstevel@tonic-gate *
83890Sstevel@tonic-gate * One piece is written with .hdr, which
83900Sstevel@tonic-gate * contains the actual xattr hdr or pathing information
83910Sstevel@tonic-gate * then the name is updated to drop the .hdr off
83920Sstevel@tonic-gate * and the actual file itself is archived.
83930Sstevel@tonic-gate */
83940Sstevel@tonic-gate slen = strlen(Gen.g_attrnam_p) + strlen(DEVNULL) +
83955750Sceastha strlen(XATTRHDR) + 2; /* add one for '/' */
83965750Sceastha if ((namep = e_zalloc(E_NORMAL, slen)) == NULL) {
83970Sstevel@tonic-gate msg(ERRN, "Could not calloc memory for attribute name");
83980Sstevel@tonic-gate continue;
83990Sstevel@tonic-gate }
84005750Sceastha (void) snprintf(namep, slen, "%s/%s%s",
84010Sstevel@tonic-gate DEVNULL, Gen.g_attrnam_p, XATTRHDR);
84020Sstevel@tonic-gate Gen.g_nam_p = namep;
84030Sstevel@tonic-gate
84045750Sceastha plen = strlen(Gen.g_attrnam_p) + 1;
84055750Sceastha if (Gen.g_attrparent_p != NULL) {
84065750Sceastha plen += strlen(Gen.g_attrparent_p) + 1;
84075750Sceastha }
84085750Sceastha if ((apathp = e_zalloc(E_NORMAL, plen)) == NULL) {
84095750Sceastha msg(ERRN, "Could not calloc memory for attribute name");
84105750Sceastha continue;
84115750Sceastha }
84125750Sceastha (void) snprintf(apathp, plen, "%s%s%s",
84135750Sceastha (Gen.g_attrparent_p == NULL) ? "" : Gen.g_attrparent_p,
84145750Sceastha (Gen.g_attrparent_p == NULL) ? "" : "/", Gen.g_attrnam_p);
84155750Sceastha
84165750Sceastha if (Gen.g_attrpath_p != NULL) {
84175750Sceastha free(Gen.g_attrpath_p);
84185750Sceastha }
84195750Sceastha Gen.g_attrpath_p = apathp;
84205750Sceastha
84210Sstevel@tonic-gate /*
84220Sstevel@tonic-gate * Get attribute's ACL info: don't bother allocating space
84230Sstevel@tonic-gate * if there are only standard permissions, i.e. ACL count < 4
84240Sstevel@tonic-gate */
84250Sstevel@tonic-gate if (Pflag) {
84260Sstevel@tonic-gate filefd = openat(Gen.g_dirfd, dp->d_name, O_RDONLY);
84270Sstevel@tonic-gate if (filefd == -1) {
84280Sstevel@tonic-gate msg(ERRN,
84290Sstevel@tonic-gate "Could not open attribute \"%s\" of"
84300Sstevel@tonic-gate " file \"%s\"", dp->d_name, savenamep);
84310Sstevel@tonic-gate free(namep);
84320Sstevel@tonic-gate continue;
84330Sstevel@tonic-gate }
8434789Sahrens if (facl_get(filefd, ACL_NO_TRIVIAL, &aclp) != 0) {
84350Sstevel@tonic-gate msg(ERRN,
84360Sstevel@tonic-gate "Error with acl() on %s",
84370Sstevel@tonic-gate Gen.g_nam_p);
84380Sstevel@tonic-gate }
84390Sstevel@tonic-gate (void) close(filefd);
84400Sstevel@tonic-gate }
84415750Sceastha
84420Sstevel@tonic-gate (void) creat_hdr();
84430Sstevel@tonic-gate (void) (*func)();
84445750Sceastha
84455750Sceastha #if defined(_PC_SATTR_ENABLED)
84465750Sceastha /*
84475750Sceastha * Recursively call xattrs_out() to process the attribute's
84485750Sceastha * hidden attribute directory and read-write system attributes.
84495750Sceastha */
84505750Sceastha if (SysAtflag && !Hiddendir && !rw_sysattr) {
84515750Sceastha int savedirfd = Gen.g_dirfd;
84525750Sceastha
84535750Sceastha (void) fchdir(Gen.g_dirfd);
84545750Sceastha Gen.g_attrparent_p = dp->d_name;
84555750Sceastha xattrs_out(func);
84565750Sceastha Gen.g_dirfd = savedirfd;
84575750Sceastha Gen.g_attrparent_p = NULL;
84585750Sceastha }
84595750Sceastha #endif /* _PC_SATTR_ENABLED */
84605750Sceastha
84610Sstevel@tonic-gate if (Gen.g_passdirfd != -1) {
84620Sstevel@tonic-gate (void) close(Gen.g_passdirfd);
84630Sstevel@tonic-gate Gen.g_passdirfd = -1;
84640Sstevel@tonic-gate }
84655750Sceastha Gen.g_attrnam_p = NULL;
84665750Sceastha Gen.g_attrfnam_p = NULL;
84675750Sceastha Gen.g_linktoattrfnam_p = NULL;
84685750Sceastha Gen.g_linktoattrnam_p = NULL;
84695750Sceastha Gen.g_rw_sysattr = 0;
84705750Sceastha if (Gen.g_attrpath_p != NULL) {
84715750Sceastha free(Gen.g_attrpath_p);
84725750Sceastha Gen.g_attrpath_p = NULL;
84735750Sceastha }
84745750Sceastha
8475789Sahrens if (aclp != NULL) {
8476789Sahrens acl_free(aclp);
84770Sstevel@tonic-gate aclp = NULL;
8478789Sahrens acl_is_set = 0;
84790Sstevel@tonic-gate }
84800Sstevel@tonic-gate free(namep);
84810Sstevel@tonic-gate }
84820Sstevel@tonic-gate
84830Sstevel@tonic-gate (void) closedir(dirp);
84840Sstevel@tonic-gate (void) close(Gen.g_dirfd);
84855750Sceastha if (attrparent == NULL) {
84865750Sceastha rest_cwd(Gen.g_baseparent_fd);
84875750Sceastha Gen.g_dirfd = -1;
84885750Sceastha }
84895750Sceastha Hiddendir = 0;
84900Sstevel@tonic-gate }
84910Sstevel@tonic-gate #else
84920Sstevel@tonic-gate static void
xattrs_out(int (* func)())84930Sstevel@tonic-gate xattrs_out(int (*func)())
84940Sstevel@tonic-gate {
84950Sstevel@tonic-gate }
84960Sstevel@tonic-gate #endif
84970Sstevel@tonic-gate
84980Sstevel@tonic-gate /*
84990Sstevel@tonic-gate * Return the parent directory of a given path.
85000Sstevel@tonic-gate *
85010Sstevel@tonic-gate * Examples:
85020Sstevel@tonic-gate * /usr/tmp return /usr
85030Sstevel@tonic-gate * /usr/tmp/file return /usr/tmp
85040Sstevel@tonic-gate * / returns .
85050Sstevel@tonic-gate * /usr returns /
85060Sstevel@tonic-gate * file returns .
85070Sstevel@tonic-gate *
85080Sstevel@tonic-gate * dir is assumed to be at least as big as path.
85090Sstevel@tonic-gate */
85100Sstevel@tonic-gate static void
get_parent(char * path,char * dir)85110Sstevel@tonic-gate get_parent(char *path, char *dir)
85120Sstevel@tonic-gate {
85130Sstevel@tonic-gate char *s;
85140Sstevel@tonic-gate char tmpdir[PATH_MAX + 1];
85150Sstevel@tonic-gate
85160Sstevel@tonic-gate if (strlen(path) > PATH_MAX) {
85170Sstevel@tonic-gate msg(EXT, "pathname is too long");
85180Sstevel@tonic-gate }
85190Sstevel@tonic-gate (void) strcpy(tmpdir, path);
85200Sstevel@tonic-gate chop_endslashes(tmpdir);
85210Sstevel@tonic-gate
85220Sstevel@tonic-gate if ((s = strrchr(tmpdir, '/')) == NULL) {
85230Sstevel@tonic-gate (void) strcpy(dir, ".");
85240Sstevel@tonic-gate } else {
85250Sstevel@tonic-gate s = skipslashes(s, tmpdir);
85260Sstevel@tonic-gate *s = '\0';
85270Sstevel@tonic-gate if (s == tmpdir)
85280Sstevel@tonic-gate (void) strcpy(dir, "/");
85290Sstevel@tonic-gate else
85300Sstevel@tonic-gate (void) strcpy(dir, tmpdir);
85310Sstevel@tonic-gate }
85320Sstevel@tonic-gate }
85330Sstevel@tonic-gate
85340Sstevel@tonic-gate #if defined(O_XATTR)
85350Sstevel@tonic-gate #define ROUNDTOTBLOCK(a) ((a + (TBLOCK -1)) & ~(TBLOCK -1))
85360Sstevel@tonic-gate
85370Sstevel@tonic-gate static void
prepare_xattr_hdr(char ** attrbuf,char * filename,char * attrpath,char typeflag,struct Lnk * linkinfo,int * rlen)85380Sstevel@tonic-gate prepare_xattr_hdr(
85390Sstevel@tonic-gate char **attrbuf,
85400Sstevel@tonic-gate char *filename,
85415750Sceastha char *attrpath,
85420Sstevel@tonic-gate char typeflag,
85430Sstevel@tonic-gate struct Lnk *linkinfo,
85440Sstevel@tonic-gate int *rlen)
85450Sstevel@tonic-gate {
85460Sstevel@tonic-gate char *bufhead; /* ptr to full buffer */
85475750Sceastha char *aptr;
85480Sstevel@tonic-gate struct xattr_hdr *hptr; /* ptr to header in bufhead */
85490Sstevel@tonic-gate struct xattr_buf *tptr; /* ptr to pathing pieces */
85500Sstevel@tonic-gate int totalen; /* total buffer length */
85510Sstevel@tonic-gate int len; /* length returned to user */
85520Sstevel@tonic-gate int stringlen; /* length of filename + attr */
85535750Sceastha /*
85545750Sceastha * length of filename + attr
85555750Sceastha * in link section
85565750Sceastha */
85575750Sceastha int linkstringlen;
85580Sstevel@tonic-gate int complen; /* length of pathing section */
85590Sstevel@tonic-gate int linklen; /* length of link section */
85605750Sceastha int attrnames_index; /* attrnames starting index */
85610Sstevel@tonic-gate
85620Sstevel@tonic-gate /*
85630Sstevel@tonic-gate * Release previous buffer if any.
85640Sstevel@tonic-gate */
85650Sstevel@tonic-gate
85665750Sceastha if (*attrbuf != NULL) {
85670Sstevel@tonic-gate free(*attrbuf);
85680Sstevel@tonic-gate *attrbuf = NULL;
85690Sstevel@tonic-gate }
85700Sstevel@tonic-gate
85710Sstevel@tonic-gate /*
85720Sstevel@tonic-gate * First add in fixed size stuff
85730Sstevel@tonic-gate */
85740Sstevel@tonic-gate len = sizeof (struct xattr_hdr) + sizeof (struct xattr_buf);
85750Sstevel@tonic-gate
85760Sstevel@tonic-gate /*
85770Sstevel@tonic-gate * Add space for two nulls
85780Sstevel@tonic-gate */
85795750Sceastha stringlen = strlen(attrpath) + strlen(filename) + 2;
85800Sstevel@tonic-gate complen = stringlen + sizeof (struct xattr_buf);
85810Sstevel@tonic-gate
85820Sstevel@tonic-gate len += stringlen;
85830Sstevel@tonic-gate
85840Sstevel@tonic-gate /*
85850Sstevel@tonic-gate * Now add on space for link info if any
85860Sstevel@tonic-gate */
85870Sstevel@tonic-gate
85880Sstevel@tonic-gate if (linkinfo != NULL) {
85890Sstevel@tonic-gate /*
85900Sstevel@tonic-gate * Again add space for two nulls
85910Sstevel@tonic-gate */
85920Sstevel@tonic-gate linkstringlen = strlen(linkinfo->L_gen.g_attrfnam_p) +
85930Sstevel@tonic-gate strlen(linkinfo->L_gen.g_attrnam_p) + 2;
85945750Sceastha linklen = linkstringlen + sizeof (struct xattr_buf);
85955750Sceastha len += linklen;
85965750Sceastha } else {
85975750Sceastha linklen = 0;
85980Sstevel@tonic-gate }
85990Sstevel@tonic-gate
86000Sstevel@tonic-gate /*
86010Sstevel@tonic-gate * Now add padding to end to fill out TBLOCK
86020Sstevel@tonic-gate *
86030Sstevel@tonic-gate * Function returns size of real data and not size + padding.
86040Sstevel@tonic-gate */
86050Sstevel@tonic-gate
86060Sstevel@tonic-gate totalen = ROUNDTOTBLOCK(len);
86070Sstevel@tonic-gate bufhead = e_zalloc(E_EXIT, totalen);
86080Sstevel@tonic-gate
86090Sstevel@tonic-gate /*
86100Sstevel@tonic-gate * Now we can fill in the necessary pieces
86110Sstevel@tonic-gate */
86120Sstevel@tonic-gate
86130Sstevel@tonic-gate /*
86140Sstevel@tonic-gate * first fill in the fixed header
86150Sstevel@tonic-gate */
86160Sstevel@tonic-gate hptr = (struct xattr_hdr *)bufhead;
861710202SNobutomo.Nakano@Sun.COM (void) strcpy(hptr->h_version, XATTR_ARCH_VERS);
86180Sstevel@tonic-gate (void) sprintf(hptr->h_component_len, "%0*d",
86190Sstevel@tonic-gate sizeof (hptr->h_component_len) - 1, complen);
86200Sstevel@tonic-gate (void) sprintf(hptr->h_link_component_len, "%0*d",
86210Sstevel@tonic-gate sizeof (hptr->h_link_component_len) - 1, linklen);
86220Sstevel@tonic-gate (void) sprintf(hptr->h_size, "%0*d", sizeof (hptr->h_size) - 1, len);
86230Sstevel@tonic-gate
86240Sstevel@tonic-gate /*
86250Sstevel@tonic-gate * Now fill in the filename + attrnames section
86265750Sceastha * The filename and attrnames section can be composed of two or more
86275750Sceastha * path segments separated by a null character. The first segment
86285750Sceastha * is the path to the parent file that roots the entire sequence in
86295750Sceastha * the normal name space. The remaining segments describes a path
86305750Sceastha * rooted at the hidden extended attribute directory of the leaf file of
86315750Sceastha * the previous segment, making it possible to name attributes on
86325750Sceastha * attributes. Thus, if we are just archiving an extended attribute,
86335750Sceastha * the second segment will contain the attribute name. If we are
86345750Sceastha * archiving a system attribute of an extended attribute, then the
86355750Sceastha * second segment will contain the attribute name, and a third segment
86365750Sceastha * will contain the system attribute name. The attribute pathing
86375750Sceastha * information is obtained from 'attrpath'.
86380Sstevel@tonic-gate */
86390Sstevel@tonic-gate
86400Sstevel@tonic-gate tptr = (struct xattr_buf *)(bufhead + sizeof (struct xattr_hdr));
86410Sstevel@tonic-gate (void) sprintf(tptr->h_namesz, "%0*d", sizeof (tptr->h_namesz) - 1,
86420Sstevel@tonic-gate stringlen);
86430Sstevel@tonic-gate (void) strcpy(tptr->h_names, filename);
86445750Sceastha attrnames_index = strlen(filename) + 1;
86455750Sceastha (void) strcpy(&tptr->h_names[attrnames_index], attrpath);
86460Sstevel@tonic-gate tptr->h_typeflag = typeflag;
86470Sstevel@tonic-gate
86480Sstevel@tonic-gate /*
86495750Sceastha * Split the attrnames section into two segments if 'attrpath'
86505750Sceastha * contains pathing information for a system attribute of an
86515750Sceastha * extended attribute. We split them by replacing the '/' with
86525750Sceastha * a '\0'.
86535750Sceastha */
86545750Sceastha if ((aptr = strpbrk(&tptr->h_names[attrnames_index], "/")) != NULL) {
86555750Sceastha *aptr = '\0';
86565750Sceastha }
86575750Sceastha
86585750Sceastha /*
86590Sstevel@tonic-gate * Now fill in the optional link section if we have one
86600Sstevel@tonic-gate */
86610Sstevel@tonic-gate
86625750Sceastha if (linkinfo != NULL) {
86630Sstevel@tonic-gate tptr = (struct xattr_buf *)(bufhead +
86640Sstevel@tonic-gate sizeof (struct xattr_hdr) + complen);
86650Sstevel@tonic-gate
86660Sstevel@tonic-gate (void) sprintf(tptr->h_namesz, "%0*d",
86670Sstevel@tonic-gate sizeof (tptr->h_namesz) - 1, linkstringlen);
86680Sstevel@tonic-gate (void) strcpy(tptr->h_names, linkinfo->L_gen.g_attrfnam_p);
86690Sstevel@tonic-gate (void) strcpy(
86700Sstevel@tonic-gate &tptr->h_names[strlen(linkinfo->L_gen.g_attrfnam_p) + 1],
86710Sstevel@tonic-gate linkinfo->L_gen.g_attrnam_p);
86720Sstevel@tonic-gate tptr->h_typeflag = typeflag;
86730Sstevel@tonic-gate }
86740Sstevel@tonic-gate *attrbuf = (char *)bufhead;
86750Sstevel@tonic-gate *rlen = len;
86760Sstevel@tonic-gate }
86775750Sceastha #endif /* O_XATTR */
86780Sstevel@tonic-gate
86790Sstevel@tonic-gate static char
tartype(int type)86800Sstevel@tonic-gate tartype(int type)
86810Sstevel@tonic-gate {
86820Sstevel@tonic-gate switch (type) {
86830Sstevel@tonic-gate
86840Sstevel@tonic-gate case S_IFDIR:
86850Sstevel@tonic-gate return (DIRTYPE);
86860Sstevel@tonic-gate
86870Sstevel@tonic-gate case S_IFLNK:
86880Sstevel@tonic-gate return (SYMTYPE);
86890Sstevel@tonic-gate
86900Sstevel@tonic-gate case S_IFIFO:
86910Sstevel@tonic-gate return (FIFOTYPE);
86920Sstevel@tonic-gate
86930Sstevel@tonic-gate case S_IFCHR:
86940Sstevel@tonic-gate return (CHRTYPE);
86950Sstevel@tonic-gate
86960Sstevel@tonic-gate case S_IFBLK:
86970Sstevel@tonic-gate return (BLKTYPE);
86980Sstevel@tonic-gate
86990Sstevel@tonic-gate case S_IFREG:
87000Sstevel@tonic-gate return (REGTYPE);
87010Sstevel@tonic-gate
87020Sstevel@tonic-gate default:
87030Sstevel@tonic-gate return ('\0');
87040Sstevel@tonic-gate }
87050Sstevel@tonic-gate }
87060Sstevel@tonic-gate
87070Sstevel@tonic-gate #if defined(O_XATTR)
87080Sstevel@tonic-gate static int
openfile(int omode)87090Sstevel@tonic-gate openfile(int omode)
87100Sstevel@tonic-gate {
87115750Sceastha if (G_p->g_attrnam_p != NULL) {
87125750Sceastha return (openat(G_p->g_dirfd, G_p->g_attrnam_p, omode));
87130Sstevel@tonic-gate } else {
87140Sstevel@tonic-gate return (openat(G_p->g_dirfd,
87150Sstevel@tonic-gate get_component(G_p->g_nam_p), omode));
87160Sstevel@tonic-gate }
87170Sstevel@tonic-gate }
87180Sstevel@tonic-gate #else
87190Sstevel@tonic-gate static int
openfile(int omode)87200Sstevel@tonic-gate openfile(int omode)
87210Sstevel@tonic-gate {
87220Sstevel@tonic-gate return (openat(G_p->g_dirfd, get_component(G_p->g_nam_p), omode));
87230Sstevel@tonic-gate }
87240Sstevel@tonic-gate #endif
87250Sstevel@tonic-gate
87260Sstevel@tonic-gate #if defined(O_XATTR)
87270Sstevel@tonic-gate static int
read_xattr_hdr()87280Sstevel@tonic-gate read_xattr_hdr()
87290Sstevel@tonic-gate {
87300Sstevel@tonic-gate off_t bytes;
87310Sstevel@tonic-gate int comp_len, link_len;
87320Sstevel@tonic-gate int namelen;
87335331Samw int asz;
87340Sstevel@tonic-gate int cnt;
87350Sstevel@tonic-gate char *tp;
87365331Samw char *xattrapath;
87370Sstevel@tonic-gate int pad;
87385331Samw int parentfilelen;
87390Sstevel@tonic-gate
87400Sstevel@tonic-gate /*
87410Sstevel@tonic-gate * Include any padding in the read. We need to be positioned
87420Sstevel@tonic-gate * at beginning of next header.
87430Sstevel@tonic-gate */
87440Sstevel@tonic-gate
87450Sstevel@tonic-gate bytes = Gen.g_filesz;
87460Sstevel@tonic-gate
87470Sstevel@tonic-gate if ((xattrhead = e_zalloc(E_NORMAL, (size_t)bytes)) == NULL) {
87480Sstevel@tonic-gate (void) fprintf(stderr, gettext(
87490Sstevel@tonic-gate "Insufficient memory for extended attribute\n"));
87500Sstevel@tonic-gate return (1);
87510Sstevel@tonic-gate }
87520Sstevel@tonic-gate
87530Sstevel@tonic-gate tp = (char *)xattrhead;
87540Sstevel@tonic-gate while (bytes > 0) {
87550Sstevel@tonic-gate cnt = (int)(bytes > CPIOBSZ) ? CPIOBSZ : bytes;
87560Sstevel@tonic-gate FILL(cnt);
87570Sstevel@tonic-gate (void) memcpy(tp, Buffr.b_out_p, cnt);
87580Sstevel@tonic-gate tp += cnt;
87590Sstevel@tonic-gate Buffr.b_out_p += cnt;
87600Sstevel@tonic-gate Buffr.b_cnt -= (off_t)cnt;
87610Sstevel@tonic-gate bytes -= (off_t)cnt;
87620Sstevel@tonic-gate }
87630Sstevel@tonic-gate
87640Sstevel@tonic-gate pad = (Pad_val + 1 - (Gen.g_filesz & Pad_val)) &
87650Sstevel@tonic-gate Pad_val;
87660Sstevel@tonic-gate if (pad != 0) {
87670Sstevel@tonic-gate FILL(pad);
87680Sstevel@tonic-gate Buffr.b_out_p += pad;
87690Sstevel@tonic-gate Buffr.b_cnt -= (off_t)pad;
87700Sstevel@tonic-gate }
87710Sstevel@tonic-gate
87720Sstevel@tonic-gate /*
87730Sstevel@tonic-gate * Validate that we can handle header format
87740Sstevel@tonic-gate */
87750Sstevel@tonic-gate
87760Sstevel@tonic-gate if (strcmp(xattrhead->h_version, XATTR_ARCH_VERS) != 0) {
87770Sstevel@tonic-gate (void) fprintf(stderr,
87780Sstevel@tonic-gate gettext("Unknown extended attribute format encountered\n"));
87790Sstevel@tonic-gate (void) fprintf(stderr,
87800Sstevel@tonic-gate gettext("Disabling extended attribute header parsing\n"));
87810Sstevel@tonic-gate xattrbadhead = 1;
87820Sstevel@tonic-gate return (1);
87830Sstevel@tonic-gate }
87840Sstevel@tonic-gate (void) sscanf(xattrhead->h_component_len, "%10d", &comp_len);
87850Sstevel@tonic-gate (void) sscanf(xattrhead->h_link_component_len, "%10d", &link_len);
87860Sstevel@tonic-gate xattrp = (struct xattr_buf *)(((char *)xattrhead) +
87870Sstevel@tonic-gate sizeof (struct xattr_hdr));
87880Sstevel@tonic-gate (void) sscanf(xattrp->h_namesz, "%7d", &namelen);
87890Sstevel@tonic-gate if (link_len > 0) {
87900Sstevel@tonic-gate xattr_linkp = (struct xattr_buf *)((int)xattrp + (int)comp_len);
87910Sstevel@tonic-gate } else {
87920Sstevel@tonic-gate xattr_linkp = NULL;
87930Sstevel@tonic-gate }
87940Sstevel@tonic-gate
87955331Samw /*
87965331Samw * Gather the attribute path from the filename and attrnames section.
87975331Samw * The filename and attrnames section can be composed of two or more
87985331Samw * path segments separated by a null character. The first segment
87995331Samw * is the path to the parent file that roots the entire sequence in
88005331Samw * the normal name space. The remaining segments describes a path
88015331Samw * rooted at the hidden extended attribute directory of the leaf file of
88025331Samw * the previous segment, making it possible to name attributes on
88035331Samw * attributes.
88045331Samw */
88055331Samw parentfilelen = strlen(xattrp->h_names);
88065331Samw xattrapath = xattrp->h_names + parentfilelen + 1;
88075331Samw asz = strlen(xattrapath);
88085331Samw if ((asz + parentfilelen + 2) < namelen) {
88095331Samw /*
88105331Samw * The attrnames section contains a system attribute on an
88115331Samw * attribute. Save the name of the attribute for use later,
88125331Samw * and replace the null separating the attribute name from
88135331Samw * the system attribute name with a '/' so that xattrapath can
88145331Samw * be used to display messages with the full attribute path name
88155331Samw * rooted at the hidden attribute directory of the base file
88165331Samw * in normal name space.
88175331Samw */
88185331Samw xattrapath[asz] = '/';
88195331Samw }
88205331Samw
88210Sstevel@tonic-gate return (0);
88220Sstevel@tonic-gate }
88230Sstevel@tonic-gate #endif
88240Sstevel@tonic-gate
88250Sstevel@tonic-gate static mode_t
attrmode(char type)88260Sstevel@tonic-gate attrmode(char type)
88270Sstevel@tonic-gate {
88280Sstevel@tonic-gate mode_t mode;
88290Sstevel@tonic-gate
88300Sstevel@tonic-gate switch (type) {
88310Sstevel@tonic-gate case '\0':
88320Sstevel@tonic-gate case REGTYPE:
88330Sstevel@tonic-gate case LNKTYPE:
88340Sstevel@tonic-gate mode = S_IFREG;
88350Sstevel@tonic-gate break;
88360Sstevel@tonic-gate
88370Sstevel@tonic-gate case SYMTYPE:
88380Sstevel@tonic-gate mode = S_IFLNK;
88390Sstevel@tonic-gate break;
88400Sstevel@tonic-gate
88410Sstevel@tonic-gate case CHRTYPE:
88420Sstevel@tonic-gate mode = S_IFCHR;
88430Sstevel@tonic-gate break;
88440Sstevel@tonic-gate case BLKTYPE:
88450Sstevel@tonic-gate mode = S_IFBLK;
88460Sstevel@tonic-gate break;
88470Sstevel@tonic-gate case DIRTYPE:
88480Sstevel@tonic-gate mode = S_IFDIR;
88490Sstevel@tonic-gate break;
88500Sstevel@tonic-gate case FIFOTYPE:
88510Sstevel@tonic-gate mode = S_IFIFO;
88520Sstevel@tonic-gate break;
88530Sstevel@tonic-gate case CONTTYPE:
88540Sstevel@tonic-gate default:
88550Sstevel@tonic-gate mode = 0;
88560Sstevel@tonic-gate }
88570Sstevel@tonic-gate
88580Sstevel@tonic-gate return (mode);
88590Sstevel@tonic-gate }
88600Sstevel@tonic-gate
88610Sstevel@tonic-gate #if defined(O_XATTR)
88620Sstevel@tonic-gate static char *
get_component(char * path)88630Sstevel@tonic-gate get_component(char *path)
88640Sstevel@tonic-gate {
88650Sstevel@tonic-gate char *ptr;
88660Sstevel@tonic-gate
88670Sstevel@tonic-gate ptr = strrchr(path, '/');
88680Sstevel@tonic-gate if (ptr == NULL) {
88690Sstevel@tonic-gate return (path);
88700Sstevel@tonic-gate } else {
88710Sstevel@tonic-gate /*
88720Sstevel@tonic-gate * Handle trailing slash
88730Sstevel@tonic-gate */
88740Sstevel@tonic-gate if (*(ptr + 1) == '\0')
88750Sstevel@tonic-gate return (ptr);
88760Sstevel@tonic-gate else
88770Sstevel@tonic-gate return (ptr + 1);
88780Sstevel@tonic-gate }
88790Sstevel@tonic-gate }
88800Sstevel@tonic-gate #else
88810Sstevel@tonic-gate static char *
get_component(char * path)88820Sstevel@tonic-gate get_component(char *path)
88830Sstevel@tonic-gate {
88840Sstevel@tonic-gate return (path);
88850Sstevel@tonic-gate }
88860Sstevel@tonic-gate #endif
88870Sstevel@tonic-gate
88880Sstevel@tonic-gate static int
open_dir(char * name)88890Sstevel@tonic-gate open_dir(char *name)
88900Sstevel@tonic-gate {
88910Sstevel@tonic-gate int fd = -1;
88920Sstevel@tonic-gate int cnt = 0;
88930Sstevel@tonic-gate char *dir;
88940Sstevel@tonic-gate
88950Sstevel@tonic-gate dir = e_zalloc(E_EXIT, strlen(name) + 1);
88960Sstevel@tonic-gate
88970Sstevel@tonic-gate /*
88980Sstevel@tonic-gate * open directory; creating missing directories along the way.
88990Sstevel@tonic-gate */
89000Sstevel@tonic-gate get_parent(name, dir);
89010Sstevel@tonic-gate do {
89020Sstevel@tonic-gate fd = open(dir, O_RDONLY);
89030Sstevel@tonic-gate if (fd != -1) {
89040Sstevel@tonic-gate free(dir);
89050Sstevel@tonic-gate return (fd);
89060Sstevel@tonic-gate }
89070Sstevel@tonic-gate cnt++;
89080Sstevel@tonic-gate } while (cnt <= 1 && missdir(name) == 0);
89090Sstevel@tonic-gate
89100Sstevel@tonic-gate free(dir);
89110Sstevel@tonic-gate return (-1);
89120Sstevel@tonic-gate }
89130Sstevel@tonic-gate
89140Sstevel@tonic-gate static int
open_dirfd()89150Sstevel@tonic-gate open_dirfd()
89160Sstevel@tonic-gate {
89170Sstevel@tonic-gate #ifdef O_XATTR
89180Sstevel@tonic-gate if ((Args & OCt) == 0) {
89190Sstevel@tonic-gate close_dirfd();
89205750Sceastha if (G_p->g_attrnam_p != NULL) {
89215750Sceastha int rw_sysattr;
89225750Sceastha
89235750Sceastha /*
89245750Sceastha * Open the file's attribute directory.
89255750Sceastha * Change into the base file's starting directory then
89265750Sceastha * call open_attr_dir() to open the attribute directory
89275750Sceastha * of either the base file (if G_p->g_attrparent_p is
89285750Sceastha * NULL) or the attribute (if G_p->g_attrparent_p is
89295750Sceastha * set) of the base file.
89305750Sceastha */
89315750Sceastha (void) fchdir(G_p->g_baseparent_fd);
89325750Sceastha (void) open_attr_dir(G_p->g_attrnam_p,
89335750Sceastha G_p->g_attrfnam_p, G_p->g_baseparent_fd,
89345750Sceastha (G_p->g_attrparent_p == NULL) ? NULL :
89355750Sceastha G_p->g_attrparent_p, &G_p->g_dirfd, &rw_sysattr);
89365750Sceastha if (Args & OCi) {
89375750Sceastha int saveerrno = errno;
89385750Sceastha
89395750Sceastha (void) fchdir(G_p->g_baseparent_fd);
89405750Sceastha errno = saveerrno;
89415750Sceastha }
89425750Sceastha if ((G_p->g_dirfd == -1) && (Args & (OCi | OCp))) {
89435750Sceastha msg(ERRN,
89445750Sceastha "Cannot open attribute directory "
89455750Sceastha "of %s%s%sfile \"%s\"",
89465750Sceastha (G_p->g_attrparent_p == NULL) ? "" :
89475750Sceastha gettext("attribute \""),
89485750Sceastha (G_p->g_attrparent_p == NULL) ? "" :
89495750Sceastha G_p->g_attrparent_p,
89505750Sceastha (G_p->g_attrparent_p == NULL) ? "" :
89515750Sceastha gettext("\" of "),
89525750Sceastha G_p->g_attrfnam_p);
89535750Sceastha return (FILE_PASS_ERR);
89540Sstevel@tonic-gate }
89550Sstevel@tonic-gate } else {
89560Sstevel@tonic-gate G_p->g_dirfd = open_dir(G_p->g_nam_p);
89570Sstevel@tonic-gate if (G_p->g_dirfd == -1) {
89580Sstevel@tonic-gate msg(ERRN,
89590Sstevel@tonic-gate "Cannot open/create %s", G_p->g_nam_p);
89600Sstevel@tonic-gate return (1);
89610Sstevel@tonic-gate }
89620Sstevel@tonic-gate }
89630Sstevel@tonic-gate } else {
89640Sstevel@tonic-gate G_p->g_dirfd = -1;
89650Sstevel@tonic-gate }
89660Sstevel@tonic-gate #else
89670Sstevel@tonic-gate G_p->g_dirfd = -1;
89680Sstevel@tonic-gate #endif
89690Sstevel@tonic-gate return (0);
89700Sstevel@tonic-gate }
89710Sstevel@tonic-gate
89720Sstevel@tonic-gate static void
close_dirfd()89730Sstevel@tonic-gate close_dirfd()
89740Sstevel@tonic-gate {
89750Sstevel@tonic-gate if (G_p->g_dirfd != -1) {
89760Sstevel@tonic-gate (void) close(G_p->g_dirfd);
89770Sstevel@tonic-gate G_p->g_dirfd = -1;
89780Sstevel@tonic-gate }
89790Sstevel@tonic-gate }
89800Sstevel@tonic-gate
89810Sstevel@tonic-gate static void
write_xattr_hdr()89820Sstevel@tonic-gate write_xattr_hdr()
89830Sstevel@tonic-gate {
89840Sstevel@tonic-gate char *attrbuf = NULL;
89850Sstevel@tonic-gate int attrlen = 0;
89860Sstevel@tonic-gate char *namep;
89870Sstevel@tonic-gate struct Lnk *tl_p, *linkinfo;
89880Sstevel@tonic-gate
89890Sstevel@tonic-gate /*
89900Sstevel@tonic-gate * namep was allocated in xattrs_out. It is big enough to hold
89910Sstevel@tonic-gate * either the name + .hdr on the end or just the attr name
89920Sstevel@tonic-gate */
89930Sstevel@tonic-gate
89940Sstevel@tonic-gate #if defined(O_XATTR)
89950Sstevel@tonic-gate namep = Gen.g_nam_p;
89960Sstevel@tonic-gate (void) creat_hdr();
89970Sstevel@tonic-gate
89980Sstevel@tonic-gate if (Args & OCo) {
89990Sstevel@tonic-gate linkinfo = NULL;
90000Sstevel@tonic-gate tl_p = Lnk_hd.L_nxt_p;
90010Sstevel@tonic-gate while (tl_p != &Lnk_hd) {
90020Sstevel@tonic-gate if (tl_p->L_gen.g_ino == G_p->g_ino &&
90031134Sceastha tl_p->L_gen.g_dev == G_p->g_dev) {
90040Sstevel@tonic-gate linkinfo = tl_p;
90050Sstevel@tonic-gate break; /* found */
90060Sstevel@tonic-gate }
90070Sstevel@tonic-gate tl_p = tl_p->L_nxt_p;
90080Sstevel@tonic-gate }
90090Sstevel@tonic-gate prepare_xattr_hdr(&attrbuf, Gen.g_attrfnam_p,
90105750Sceastha Gen.g_attrpath_p,
90115750Sceastha (linkinfo == NULL) ?
90120Sstevel@tonic-gate tartype(Gen.g_mode & Ftype) : LNKTYPE,
90130Sstevel@tonic-gate linkinfo, &attrlen);
90140Sstevel@tonic-gate Gen.g_filesz = attrlen;
90150Sstevel@tonic-gate write_hdr(ARCHIVE_XATTR, (off_t)attrlen);
901610202SNobutomo.Nakano@Sun.COM /*LINTED*/
90170Sstevel@tonic-gate (void) sprintf(namep, "%s/%s", DEVNULL, Gen.g_attrnam_p);
901810202SNobutomo.Nakano@Sun.COM write_ancillary(attrbuf, attrlen, B_TRUE);
90190Sstevel@tonic-gate }
90200Sstevel@tonic-gate
90210Sstevel@tonic-gate (void) creat_hdr();
90220Sstevel@tonic-gate #endif
90230Sstevel@tonic-gate }
90240Sstevel@tonic-gate
90250Sstevel@tonic-gate /*
90260Sstevel@tonic-gate * skip over extra slashes in string.
90270Sstevel@tonic-gate *
90280Sstevel@tonic-gate * For example:
90290Sstevel@tonic-gate * /usr/tmp/////
90300Sstevel@tonic-gate *
90310Sstevel@tonic-gate * would return pointer at
90320Sstevel@tonic-gate * /usr/tmp/////
90330Sstevel@tonic-gate * ^
90340Sstevel@tonic-gate */
90350Sstevel@tonic-gate static char *
skipslashes(char * string,char * start)90360Sstevel@tonic-gate skipslashes(char *string, char *start)
90370Sstevel@tonic-gate {
90380Sstevel@tonic-gate while ((string > start) && *(string - 1) == '/') {
90390Sstevel@tonic-gate string--;
90400Sstevel@tonic-gate }
90410Sstevel@tonic-gate
90420Sstevel@tonic-gate return (string);
90430Sstevel@tonic-gate }
90440Sstevel@tonic-gate
90450Sstevel@tonic-gate static sl_info_t *
sl_info_alloc(void)90460Sstevel@tonic-gate sl_info_alloc(void)
90470Sstevel@tonic-gate {
90480Sstevel@tonic-gate static int num_left;
90490Sstevel@tonic-gate static sl_info_t *slipool;
90500Sstevel@tonic-gate
90510Sstevel@tonic-gate if (num_left > 0) {
90520Sstevel@tonic-gate return (&slipool[--num_left]);
90530Sstevel@tonic-gate }
90540Sstevel@tonic-gate num_left = SL_INFO_ALLOC_CHUNK;
90550Sstevel@tonic-gate slipool = e_zalloc(E_EXIT, sizeof (sl_info_t) * num_left);
90560Sstevel@tonic-gate return (&slipool[--num_left]);
90570Sstevel@tonic-gate }
90580Sstevel@tonic-gate
90590Sstevel@tonic-gate /*
90600Sstevel@tonic-gate * If a match for the key values was found in the tree, return a pointer to it.
90610Sstevel@tonic-gate * If a match was not found, insert it and return a pointer to it. This is
90620Sstevel@tonic-gate * based on Knuth's Algorithm A in Vol 3, section 6.2.3.
90630Sstevel@tonic-gate */
90640Sstevel@tonic-gate
90650Sstevel@tonic-gate sl_info_t *
sl_insert(dev_t device,ino_t inode,int ftype)90665750Sceastha sl_insert(dev_t device, ino_t inode, int ftype)
90670Sstevel@tonic-gate {
90680Sstevel@tonic-gate sl_info_t *p; /* moves down the tree */
90690Sstevel@tonic-gate sl_info_t *q; /* scratch */
90700Sstevel@tonic-gate sl_info_t *r; /* scratch */
90710Sstevel@tonic-gate sl_info_t *s; /* pt where rebalancing may be needed */
90720Sstevel@tonic-gate sl_info_t *t; /* father of s */
90730Sstevel@tonic-gate sl_info_t *head;
90740Sstevel@tonic-gate
90750Sstevel@tonic-gate int a; /* used to hold balance factors */
90760Sstevel@tonic-gate int done; /* loop control */
90770Sstevel@tonic-gate int cmpflg; /* used to hold the result of a comparison */
90780Sstevel@tonic-gate
90790Sstevel@tonic-gate /* initialize */
90800Sstevel@tonic-gate
90810Sstevel@tonic-gate head = sl_devhash_lookup(device);
90820Sstevel@tonic-gate
90830Sstevel@tonic-gate if (head == NULL) {
90840Sstevel@tonic-gate head = sl_info_alloc();
90850Sstevel@tonic-gate head->llink = NULL;
90860Sstevel@tonic-gate head->bal = 0;
90870Sstevel@tonic-gate
90880Sstevel@tonic-gate p = head->rlink = sl_info_alloc();
90890Sstevel@tonic-gate p->sl_ino = inode;
90905750Sceastha p->sl_ftype = ftype;
90910Sstevel@tonic-gate p->sl_count = 0;
90920Sstevel@tonic-gate p->bal = 0;
90930Sstevel@tonic-gate p->llink = NULL;
90940Sstevel@tonic-gate p->rlink = NULL;
90950Sstevel@tonic-gate sl_devhash_insert(device, head);
90960Sstevel@tonic-gate return (p);
90970Sstevel@tonic-gate }
90980Sstevel@tonic-gate
90990Sstevel@tonic-gate t = head;
91000Sstevel@tonic-gate s = p = head->rlink;
91010Sstevel@tonic-gate
91020Sstevel@tonic-gate /* compare */
91030Sstevel@tonic-gate
91040Sstevel@tonic-gate for (done = 0; ! done; ) {
91055750Sceastha switch (sl_compare(inode, ftype, p->sl_ino, p->sl_ftype)) {
91060Sstevel@tonic-gate case -1:
91070Sstevel@tonic-gate /* move left */
91080Sstevel@tonic-gate
91090Sstevel@tonic-gate q = p->llink;
91100Sstevel@tonic-gate
91110Sstevel@tonic-gate if (q == NULL) {
91120Sstevel@tonic-gate q = sl_info_alloc();
91130Sstevel@tonic-gate p->llink = q;
91140Sstevel@tonic-gate done = 1;
91150Sstevel@tonic-gate continue;
91160Sstevel@tonic-gate }
91170Sstevel@tonic-gate
91180Sstevel@tonic-gate break;
91190Sstevel@tonic-gate
91200Sstevel@tonic-gate case 0:
91210Sstevel@tonic-gate /* found it */
91220Sstevel@tonic-gate return (p);
91230Sstevel@tonic-gate break;
91240Sstevel@tonic-gate
91250Sstevel@tonic-gate case 1:
91260Sstevel@tonic-gate /* move right */
91270Sstevel@tonic-gate
91280Sstevel@tonic-gate q = p->rlink;
91290Sstevel@tonic-gate
91300Sstevel@tonic-gate if (q == NULL) {
91310Sstevel@tonic-gate q = sl_info_alloc();
91320Sstevel@tonic-gate p->rlink = q;
91330Sstevel@tonic-gate done = 1;
91340Sstevel@tonic-gate continue;
91350Sstevel@tonic-gate }
91360Sstevel@tonic-gate
91370Sstevel@tonic-gate break;
91380Sstevel@tonic-gate }
91390Sstevel@tonic-gate
91400Sstevel@tonic-gate if (q->bal != 0) {
91410Sstevel@tonic-gate t = p;
91420Sstevel@tonic-gate s = q;
91430Sstevel@tonic-gate }
91440Sstevel@tonic-gate
91450Sstevel@tonic-gate p = q;
91460Sstevel@tonic-gate }
91470Sstevel@tonic-gate
91480Sstevel@tonic-gate /* insert */
91490Sstevel@tonic-gate
91500Sstevel@tonic-gate q->sl_ino = inode;
91515750Sceastha q->sl_ftype = ftype;
91520Sstevel@tonic-gate q->sl_count = 0;
91530Sstevel@tonic-gate q->llink = q->rlink = NULL;
91540Sstevel@tonic-gate q->bal = 0;
91550Sstevel@tonic-gate
91560Sstevel@tonic-gate /* adjust balance factors */
91570Sstevel@tonic-gate
91585750Sceastha if ((cmpflg = sl_compare(inode, ftype, s->sl_ino, s->sl_ftype)) < 0) {
91590Sstevel@tonic-gate r = p = s->llink;
91600Sstevel@tonic-gate } else {
91610Sstevel@tonic-gate r = p = s->rlink;
91620Sstevel@tonic-gate }
91630Sstevel@tonic-gate
91640Sstevel@tonic-gate while (p != q) {
91655750Sceastha switch (sl_compare(inode, ftype, p->sl_ino, p->sl_ftype)) {
91660Sstevel@tonic-gate case -1:
91670Sstevel@tonic-gate p->bal = -1;
91680Sstevel@tonic-gate p = p->llink;
91690Sstevel@tonic-gate break;
91700Sstevel@tonic-gate
91710Sstevel@tonic-gate case 0:
91720Sstevel@tonic-gate break;
91730Sstevel@tonic-gate
91740Sstevel@tonic-gate case 1:
91750Sstevel@tonic-gate p->bal = 1;
91760Sstevel@tonic-gate p = p->rlink;
91770Sstevel@tonic-gate break;
91780Sstevel@tonic-gate }
91790Sstevel@tonic-gate }
91800Sstevel@tonic-gate
91810Sstevel@tonic-gate /* balancing act */
91820Sstevel@tonic-gate
91830Sstevel@tonic-gate if (cmpflg < 0) {
91840Sstevel@tonic-gate a = -1;
91850Sstevel@tonic-gate } else {
91860Sstevel@tonic-gate a = 1;
91870Sstevel@tonic-gate }
91880Sstevel@tonic-gate
91890Sstevel@tonic-gate if (s->bal == 0) {
91900Sstevel@tonic-gate s->bal = a;
91910Sstevel@tonic-gate head->llink = (sl_info_t *)((int)head->llink + 1);
91920Sstevel@tonic-gate return (q);
91930Sstevel@tonic-gate } else if (s->bal == -a) {
91940Sstevel@tonic-gate s->bal = 0;
91950Sstevel@tonic-gate return (q);
91960Sstevel@tonic-gate }
91970Sstevel@tonic-gate
91980Sstevel@tonic-gate /*
91990Sstevel@tonic-gate * (s->bal == a)
92000Sstevel@tonic-gate */
92010Sstevel@tonic-gate
92020Sstevel@tonic-gate if (r->bal == a) {
92030Sstevel@tonic-gate /* single rotation */
92040Sstevel@tonic-gate
92050Sstevel@tonic-gate p = r;
92060Sstevel@tonic-gate
92070Sstevel@tonic-gate if (a == -1) {
92080Sstevel@tonic-gate s->llink = r->rlink;
92090Sstevel@tonic-gate r->rlink = s;
92100Sstevel@tonic-gate } else if (a == 1) {
92110Sstevel@tonic-gate s->rlink = r->llink;
92120Sstevel@tonic-gate r->llink = s;
92130Sstevel@tonic-gate }
92140Sstevel@tonic-gate
92150Sstevel@tonic-gate s->bal = r->bal = 0;
92160Sstevel@tonic-gate
92170Sstevel@tonic-gate } else if (r->bal == -a) {
92180Sstevel@tonic-gate /* double rotation */
92190Sstevel@tonic-gate
92200Sstevel@tonic-gate if (a == -1) {
92210Sstevel@tonic-gate p = r->rlink;
92220Sstevel@tonic-gate r->rlink = p->llink;
92230Sstevel@tonic-gate p->llink = r;
92240Sstevel@tonic-gate s->llink = p->rlink;
92250Sstevel@tonic-gate p->rlink = s;
92260Sstevel@tonic-gate } else if (a == 1) {
92270Sstevel@tonic-gate p = r->llink;
92280Sstevel@tonic-gate r->llink = p->rlink;
92290Sstevel@tonic-gate p->rlink = r;
92300Sstevel@tonic-gate s->rlink = p->llink;
92310Sstevel@tonic-gate p->llink = s;
92320Sstevel@tonic-gate }
92330Sstevel@tonic-gate
92340Sstevel@tonic-gate if (p->bal == 0) {
92350Sstevel@tonic-gate s->bal = 0;
92360Sstevel@tonic-gate r->bal = 0;
92370Sstevel@tonic-gate } else if (p->bal == -a) {
92380Sstevel@tonic-gate s->bal = 0;
92390Sstevel@tonic-gate r->bal = a;
92400Sstevel@tonic-gate } else if (p->bal == a) {
92410Sstevel@tonic-gate s->bal = -a;
92420Sstevel@tonic-gate r->bal = 0;
92430Sstevel@tonic-gate }
92440Sstevel@tonic-gate
92450Sstevel@tonic-gate p->bal = 0;
92460Sstevel@tonic-gate }
92470Sstevel@tonic-gate
92480Sstevel@tonic-gate /* finishing touch */
92490Sstevel@tonic-gate
92500Sstevel@tonic-gate if (s == t->rlink) {
92510Sstevel@tonic-gate t->rlink = p;
92520Sstevel@tonic-gate } else {
92530Sstevel@tonic-gate t->llink = p;
92540Sstevel@tonic-gate }
92550Sstevel@tonic-gate
92560Sstevel@tonic-gate return (q);
92570Sstevel@tonic-gate }
92580Sstevel@tonic-gate
92590Sstevel@tonic-gate /*
92600Sstevel@tonic-gate * sl_numlinks: return the number of links that we saw during our preview.
92610Sstevel@tonic-gate */
92620Sstevel@tonic-gate
92630Sstevel@tonic-gate static ulong_t
sl_numlinks(dev_t device,ino_t inode,int ftype)92645750Sceastha sl_numlinks(dev_t device, ino_t inode, int ftype)
92655750Sceastha {
92665750Sceastha sl_info_t *p = sl_search(device, inode, ftype);
92670Sstevel@tonic-gate
92680Sstevel@tonic-gate if (p) {
92691134Sceastha return (p->sl_count);
92700Sstevel@tonic-gate } else {
92711134Sceastha return (1);
92720Sstevel@tonic-gate }
92730Sstevel@tonic-gate }
92740Sstevel@tonic-gate
92750Sstevel@tonic-gate /*
92765750Sceastha * Preview extended and extended system attributes.
92775750Sceastha *
92785750Sceastha * Return 0 if successful, otherwise return 1.
92795750Sceastha */
92805750Sceastha #if defined(O_XATTR)
92815750Sceastha static int
preview_attrs(char * s,char * attrparent)92825750Sceastha preview_attrs(char *s, char *attrparent)
92835750Sceastha {
92845750Sceastha char *filename = (attrparent == NULL) ? s : attrparent;
92855750Sceastha int dirfd;
92865750Sceastha int tmpfd;
92875750Sceastha int islnk;
92885750Sceastha int rc = 0;
92895750Sceastha int arc_rwsysattr = 0;
92905750Sceastha int rw_sysattr = 0;
92915750Sceastha int ext_attr = 0;
92925750Sceastha DIR *dirp;
92935750Sceastha struct dirent *dp;
92945750Sceastha struct stat sb;
92955750Sceastha
92965750Sceastha /*
92975750Sceastha * If the underlying file system supports it, then
92985750Sceastha * archive the extended attributes if -@ was specified,
92995750Sceastha * and the extended system attributes if -/ was
93005750Sceastha * specified.
93015750Sceastha */
93025750Sceastha if (verify_attr_support(filename, (attrparent == NULL), ARC_CREATE,
93035750Sceastha &ext_attr) != ATTR_OK) {
93045750Sceastha return (1);
93055750Sceastha }
93065750Sceastha
93075750Sceastha #if defined(_PC_SATTR_ENABLED)
93085750Sceastha if (SysAtflag) {
93095750Sceastha int filefd;
93105750Sceastha nvlist_t *slist = NULL;
93115750Sceastha
93125750Sceastha /* Determine if there are non-transient system attributes. */
93135750Sceastha errno = 0;
93145750Sceastha if ((filefd = open(filename, O_RDONLY)) < 0) {
93155750Sceastha return (1);
93165750Sceastha }
93175750Sceastha if (((slist = sysattr_list(myname, filefd,
93185750Sceastha filename)) != NULL) || (errno != 0)) {
93195750Sceastha arc_rwsysattr = 1;
93205750Sceastha }
93215750Sceastha if (slist != NULL) {
93225750Sceastha (void) nvlist_free(slist);
93235750Sceastha slist = NULL;
93245750Sceastha }
93255750Sceastha (void) close(filefd);
93265750Sceastha }
93275750Sceastha
93285750Sceastha if ((arc_rwsysattr == 0) && ((attrparent != NULL) ||
93295750Sceastha (SysAtflag && !ext_attr))) {
93305750Sceastha return (1);
93315750Sceastha }
93325750Sceastha #endif /* _PC_SATTR_ENABLED */
93335750Sceastha /*
93345750Sceastha * We need to open the attribute directory of the
93355750Sceastha * file, and preview all of the file's attributes as
93365750Sceastha * attributes of the file can be hard links to other
93375750Sceastha * attributes of the file.
93385750Sceastha */
93395750Sceastha dirfd = attropen(filename, ".", O_RDONLY);
93405750Sceastha if (dirfd == -1)
93415750Sceastha return (1);
93425750Sceastha
93435750Sceastha tmpfd = dup(dirfd);
93445750Sceastha if (tmpfd == -1) {
93455750Sceastha (void) close(dirfd);
93465750Sceastha return (1);
93475750Sceastha }
93485750Sceastha dirp = fdopendir(tmpfd);
93495750Sceastha if (dirp == NULL) {
93505750Sceastha (void) close(dirfd);
93515750Sceastha (void) close(tmpfd);
93525750Sceastha return (1);
93535750Sceastha }
93545750Sceastha
93555750Sceastha while (dp = readdir(dirp)) {
93565750Sceastha if (dp->d_name[0] == '.') {
93575750Sceastha if (dp->d_name[1] == '\0') {
93585750Sceastha Hiddendir = 1;
93595750Sceastha } else if ((dp->d_name[1] == '.') &&
93605750Sceastha (dp->d_name[2] == '\0')) {
93615750Sceastha continue;
93625750Sceastha } else {
93635750Sceastha Hiddendir = 0;
93645750Sceastha }
93655750Sceastha } else {
93665750Sceastha Hiddendir = 0;
93675750Sceastha }
93685750Sceastha
93695750Sceastha if (fstatat(dirfd, dp->d_name, &sb,
93705750Sceastha AT_SYMLINK_NOFOLLOW) < 0) {
93715750Sceastha continue;
93725750Sceastha }
93735750Sceastha
93745750Sceastha if (verify_attr(dp->d_name, attrparent,
93755750Sceastha arc_rwsysattr, &rw_sysattr) != ATTR_OK) {
93765750Sceastha continue;
93775750Sceastha }
93785750Sceastha
93795750Sceastha islnk = 0;
93805750Sceastha if (S_ISLNK(sb.st_mode)) {
93815750Sceastha islnk = 1;
93825750Sceastha if (Args & OCL) {
93835750Sceastha if (fstatat(dirfd, dp->d_name,
93845750Sceastha &sb, 0) < 0) {
93855750Sceastha continue;
93865750Sceastha }
93875750Sceastha }
93885750Sceastha }
93895750Sceastha sl_remember_tgt(&sb, islnk, rw_sysattr);
93905750Sceastha
93915750Sceastha /*
93925750Sceastha * Recursively call preview_attrs() to preview extended
93935750Sceastha * system attributes of attributes.
93945750Sceastha */
93955750Sceastha if (SysAtflag && !Hiddendir && !rw_sysattr) {
93965750Sceastha int my_cwd = save_cwd();
93975750Sceastha
93985750Sceastha (void) fchdir(dirfd);
93995750Sceastha rc = preview_attrs(s, dp->d_name);
94005750Sceastha rest_cwd(my_cwd);
94015750Sceastha }
94025750Sceastha }
94035750Sceastha (void) closedir(dirp);
94045750Sceastha (void) close(dirfd);
94055750Sceastha return (rc);
94065750Sceastha }
94075750Sceastha #endif /* O_XATTR */
94085750Sceastha
94095750Sceastha /*
94100Sstevel@tonic-gate * sl_preview_synonyms: Read the file list from the input stream, remembering
94110Sstevel@tonic-gate * each reference to each file.
94120Sstevel@tonic-gate */
94130Sstevel@tonic-gate
94140Sstevel@tonic-gate static void
sl_preview_synonyms(void)94150Sstevel@tonic-gate sl_preview_synonyms(void)
94160Sstevel@tonic-gate {
94170Sstevel@tonic-gate char buf [APATH+1];
94180Sstevel@tonic-gate char *s;
94190Sstevel@tonic-gate
94200Sstevel@tonic-gate char *suffix = "/cpioXXXXXX";
94210Sstevel@tonic-gate char *tmpdir = getenv("TMPDIR");
94220Sstevel@tonic-gate int tmpfd, islnk;
94230Sstevel@tonic-gate FILE *tmpfile;
94240Sstevel@tonic-gate char *tmpfname;
94250Sstevel@tonic-gate
94260Sstevel@tonic-gate if (tmpdir == NULL || *tmpdir == '\0' ||
94270Sstevel@tonic-gate (strlen(tmpdir) + strlen(suffix)) > APATH) {
94280Sstevel@tonic-gate struct statvfs tdsb;
94290Sstevel@tonic-gate
94300Sstevel@tonic-gate tmpdir = "/var/tmp";
94310Sstevel@tonic-gate
94320Sstevel@tonic-gate /* /var/tmp is read-only in the mini-root environment */
94330Sstevel@tonic-gate
94340Sstevel@tonic-gate if (statvfs(tmpdir, &tdsb) == -1 || tdsb.f_flag & ST_RDONLY) {
94350Sstevel@tonic-gate tmpdir = "/tmp";
94360Sstevel@tonic-gate }
94370Sstevel@tonic-gate }
94380Sstevel@tonic-gate
94390Sstevel@tonic-gate tmpfname = e_zalloc(E_EXIT, strlen(tmpdir) + strlen(suffix) + 1);
94400Sstevel@tonic-gate
94410Sstevel@tonic-gate (void) strcpy(tmpfname, tmpdir);
94420Sstevel@tonic-gate (void) strcat(tmpfname, suffix);
94430Sstevel@tonic-gate
94440Sstevel@tonic-gate if ((tmpfd = mkstemp(tmpfname)) == -1) {
94455750Sceastha msg(EXTN, "cannot open tmpfile %s%s", tmpdir, suffix);
94460Sstevel@tonic-gate }
94470Sstevel@tonic-gate
94480Sstevel@tonic-gate if (unlink(tmpfname) == -1) {
94490Sstevel@tonic-gate msg(EXTN, "cannot unlink tmpfile %s", tmpfname);
94500Sstevel@tonic-gate }
94510Sstevel@tonic-gate
94520Sstevel@tonic-gate if ((tmpfile = fdopen(tmpfd, "w+")) == NULL) {
94530Sstevel@tonic-gate msg(EXTN, "cannot fdopen tmpfile %s", tmpfname);
94540Sstevel@tonic-gate }
94550Sstevel@tonic-gate
94560Sstevel@tonic-gate while ((s = fgets(buf, APATH+1, In_p)) != NULL) {
94570Sstevel@tonic-gate size_t lastchar;
94580Sstevel@tonic-gate struct stat sb;
94590Sstevel@tonic-gate
94600Sstevel@tonic-gate if (fputs(buf, tmpfile) == EOF) {
94610Sstevel@tonic-gate msg(EXTN, "problem writing to tmpfile %s", tmpfname);
94620Sstevel@tonic-gate }
94630Sstevel@tonic-gate
94640Sstevel@tonic-gate /* pre-process the name */
94650Sstevel@tonic-gate
94660Sstevel@tonic-gate lastchar = strlen(s) - 1;
94670Sstevel@tonic-gate
94680Sstevel@tonic-gate if (s[lastchar] != '\n' && lastchar == APATH - 1) {
94690Sstevel@tonic-gate continue;
94700Sstevel@tonic-gate } else {
94710Sstevel@tonic-gate s[lastchar] = '\0';
94720Sstevel@tonic-gate }
94730Sstevel@tonic-gate
94740Sstevel@tonic-gate while (s[0] == '.' && s[1] == '/') {
94750Sstevel@tonic-gate s += 2;
94760Sstevel@tonic-gate while (s[0] == '/') {
94770Sstevel@tonic-gate s++;
94780Sstevel@tonic-gate }
94790Sstevel@tonic-gate }
94800Sstevel@tonic-gate
94810Sstevel@tonic-gate if (lstat(s, &sb) < 0) {
94820Sstevel@tonic-gate continue;
94830Sstevel@tonic-gate }
94840Sstevel@tonic-gate islnk = 0;
94850Sstevel@tonic-gate if (S_ISLNK(sb.st_mode)) {
94860Sstevel@tonic-gate islnk = 1;
94870Sstevel@tonic-gate if (Args & OCL) {
94880Sstevel@tonic-gate if (stat(s, &sb) < 0) {
94890Sstevel@tonic-gate continue;
94900Sstevel@tonic-gate }
94910Sstevel@tonic-gate }
94920Sstevel@tonic-gate }
94935750Sceastha sl_remember_tgt(&sb, islnk, 0);
94940Sstevel@tonic-gate
94950Sstevel@tonic-gate #if defined(O_XATTR)
94965750Sceastha if (Atflag || SysAtflag) {
94975750Sceastha (void) preview_attrs(s, NULL);
94985750Sceastha }
94995750Sceastha #endif /* O_XATTR */
95000Sstevel@tonic-gate }
95010Sstevel@tonic-gate
95020Sstevel@tonic-gate if (ferror(In_p)) {
95030Sstevel@tonic-gate msg(EXTN, "error reading stdin");
95040Sstevel@tonic-gate }
95050Sstevel@tonic-gate
95060Sstevel@tonic-gate if (fseek(tmpfile, 0L, SEEK_SET) == -1) {
95070Sstevel@tonic-gate msg(EXTN, "cannot fseek on tmpfile %s", tmpfname);
95080Sstevel@tonic-gate }
95090Sstevel@tonic-gate
95100Sstevel@tonic-gate In_p = tmpfile;
95110Sstevel@tonic-gate free(tmpfname);
95120Sstevel@tonic-gate }
95130Sstevel@tonic-gate
95140Sstevel@tonic-gate /*
95150Sstevel@tonic-gate * sl_remember_tgt: Add the device/inode for lstat or stat info to the list of
95160Sstevel@tonic-gate * those we've seen before.
95170Sstevel@tonic-gate *
95180Sstevel@tonic-gate * This tree (rooted under head) is keyed by the device/inode of the file
95190Sstevel@tonic-gate * being pointed to. A count is kept of the number of references encountered
95200Sstevel@tonic-gate * so far.
95210Sstevel@tonic-gate */
95220Sstevel@tonic-gate
95230Sstevel@tonic-gate static void
sl_remember_tgt(const struct stat * sbp,int isSymlink,int is_sysattr)95245750Sceastha sl_remember_tgt(const struct stat *sbp, int isSymlink, int is_sysattr)
95250Sstevel@tonic-gate {
95260Sstevel@tonic-gate sl_info_t *p;
95270Sstevel@tonic-gate dev_t device;
95280Sstevel@tonic-gate ino_t inode;
95295750Sceastha int ftype;
95300Sstevel@tonic-gate
95310Sstevel@tonic-gate device = sbp->st_dev;
95320Sstevel@tonic-gate inode = sbp->st_ino;
95335750Sceastha ftype = sbp->st_mode & Ftype;
95340Sstevel@tonic-gate
95350Sstevel@tonic-gate /* Determine whether we've seen this one before */
95360Sstevel@tonic-gate
95375750Sceastha p = sl_insert(device, inode, ftype);
95380Sstevel@tonic-gate
95390Sstevel@tonic-gate if (p->sl_count > 0) {
95400Sstevel@tonic-gate /*
95415750Sceastha * It appears as if have seen this file before as we found a
95425750Sceastha * matching device, inode, and file type as a file already
95435750Sceastha * processed. Since there can possibly be files with the
95445750Sceastha * same device, inode, and file type, but aren't hard links
95455750Sceastha * (e.g., read-write system attribute files will always have
95465750Sceastha * the same inode), we need to only attempt to add one to the
95475750Sceastha * link count if the file we are processing is a hard link
95485750Sceastha * (i.e., st_nlink > 1).
95495750Sceastha *
95500Sstevel@tonic-gate * Note that if we are not chasing symlinks, and this one is a
95510Sstevel@tonic-gate * symlink, it is identically the one we saw before (you cannot
95520Sstevel@tonic-gate * have hard links to symlinks); in this case, we leave the
95530Sstevel@tonic-gate * count alone, so that we don't wind up archiving a symlink to
95540Sstevel@tonic-gate * itself.
95550Sstevel@tonic-gate */
95560Sstevel@tonic-gate
95575750Sceastha if (((Args & OCL) || (! isSymlink)) && !is_sysattr) {
95580Sstevel@tonic-gate p->sl_count++;
95590Sstevel@tonic-gate }
95600Sstevel@tonic-gate } else {
95610Sstevel@tonic-gate /* We have not seen this file before */
95620Sstevel@tonic-gate
95630Sstevel@tonic-gate p->sl_count = 1;
95640Sstevel@tonic-gate
95650Sstevel@tonic-gate if (Use_old_stat) {
95660Sstevel@tonic-gate /* -Hodc: remap inode (-1 on overflow) */
95670Sstevel@tonic-gate
95680Sstevel@tonic-gate sl_remap_t *q;
95690Sstevel@tonic-gate
95700Sstevel@tonic-gate for (q = sl_remap_head; q && (q->dev != device);
95710Sstevel@tonic-gate q = q->next) {
95720Sstevel@tonic-gate /* do nothing */
95730Sstevel@tonic-gate }
95740Sstevel@tonic-gate
95750Sstevel@tonic-gate if (q == NULL) {
95760Sstevel@tonic-gate q = e_zalloc(E_EXIT, sizeof (sl_remap_t));
95770Sstevel@tonic-gate q->dev = device;
95780Sstevel@tonic-gate p->sl_ino2 = q->inode_count = 1;
95790Sstevel@tonic-gate
95800Sstevel@tonic-gate q->next = (sl_remap_head) ?
95810Sstevel@tonic-gate sl_remap_head->next : NULL;
95820Sstevel@tonic-gate sl_remap_head = q;
95830Sstevel@tonic-gate } else {
95840Sstevel@tonic-gate if ((size_t)q->inode_count <=
95850Sstevel@tonic-gate ((1 << (sizeof (o_ino_t) * 8)) - 1)) {
95860Sstevel@tonic-gate /* fits in o_ino_t */
95870Sstevel@tonic-gate p->sl_ino2 = ++(q->inode_count);
95880Sstevel@tonic-gate } else {
95890Sstevel@tonic-gate p->sl_ino2 = (ino_t)-1;
95900Sstevel@tonic-gate }
95910Sstevel@tonic-gate }
95920Sstevel@tonic-gate }
95930Sstevel@tonic-gate }
95940Sstevel@tonic-gate }
95950Sstevel@tonic-gate
95960Sstevel@tonic-gate /*
95970Sstevel@tonic-gate * A faster search, which does not insert the key values into the tree.
95980Sstevel@tonic-gate * If the a match was found in the tree, return a pointer to it. If it was not
95990Sstevel@tonic-gate * found, return NULL.
96000Sstevel@tonic-gate */
96010Sstevel@tonic-gate
96020Sstevel@tonic-gate sl_info_t *
sl_search(dev_t device,ino_t inode,int ftype)96035750Sceastha sl_search(dev_t device, ino_t inode, int ftype)
96040Sstevel@tonic-gate {
96050Sstevel@tonic-gate sl_info_t *p; /* moves down the tree */
96060Sstevel@tonic-gate int c; /* comparison value */
96070Sstevel@tonic-gate sl_info_t *retval = NULL; /* return value */
96080Sstevel@tonic-gate sl_info_t *head;
96090Sstevel@tonic-gate
96100Sstevel@tonic-gate head = sl_devhash_lookup(device);
96110Sstevel@tonic-gate if (head != NULL) {
96120Sstevel@tonic-gate for (p = head->rlink; p; ) {
96135750Sceastha if ((c = sl_compare(inode, ftype, p->sl_ino,
96145750Sceastha p->sl_ftype)) == 0) {
96150Sstevel@tonic-gate retval = p;
96160Sstevel@tonic-gate break;
96170Sstevel@tonic-gate } else if (c < 0) {
96180Sstevel@tonic-gate p = p->llink;
96190Sstevel@tonic-gate } else {
96200Sstevel@tonic-gate p = p->rlink;
96210Sstevel@tonic-gate }
96220Sstevel@tonic-gate }
96230Sstevel@tonic-gate }
96240Sstevel@tonic-gate
96250Sstevel@tonic-gate return (retval);
96260Sstevel@tonic-gate }
96270Sstevel@tonic-gate
96280Sstevel@tonic-gate static sl_info_t *
sl_devhash_lookup(dev_t device)96290Sstevel@tonic-gate sl_devhash_lookup(dev_t device)
96300Sstevel@tonic-gate {
96310Sstevel@tonic-gate int key;
96320Sstevel@tonic-gate sl_info_link_t *lp;
96330Sstevel@tonic-gate static sl_info_link_t *devcache;
96340Sstevel@tonic-gate
96350Sstevel@tonic-gate if (devcache != NULL && devcache->dev == device) {
96360Sstevel@tonic-gate return (devcache->head);
96370Sstevel@tonic-gate }
96380Sstevel@tonic-gate
96390Sstevel@tonic-gate key = DEV_HASHKEY(device);
96400Sstevel@tonic-gate for (lp = sl_devhash[key]; lp; lp = lp->next) {
96410Sstevel@tonic-gate if (lp->dev == device) {
96420Sstevel@tonic-gate devcache = lp;
96430Sstevel@tonic-gate return (lp->head);
96440Sstevel@tonic-gate }
96450Sstevel@tonic-gate }
96460Sstevel@tonic-gate return (NULL);
96470Sstevel@tonic-gate }
96480Sstevel@tonic-gate
96490Sstevel@tonic-gate static void
sl_devhash_insert(dev_t device,sl_info_t * head)96500Sstevel@tonic-gate sl_devhash_insert(dev_t device, sl_info_t *head)
96510Sstevel@tonic-gate {
96520Sstevel@tonic-gate int key = DEV_HASHKEY(device);
96530Sstevel@tonic-gate sl_info_link_t *lp;
96540Sstevel@tonic-gate
96550Sstevel@tonic-gate lp = e_zalloc(E_EXIT, sizeof (sl_info_link_t));
96560Sstevel@tonic-gate lp->dev = device;
96570Sstevel@tonic-gate lp->head = head;
96580Sstevel@tonic-gate lp->next = sl_devhash[key];
96590Sstevel@tonic-gate sl_devhash[key] = lp;
96600Sstevel@tonic-gate }
96610Sstevel@tonic-gate
96620Sstevel@tonic-gate static void
chop_endslashes(char * path)96630Sstevel@tonic-gate chop_endslashes(char *path)
96640Sstevel@tonic-gate {
96650Sstevel@tonic-gate char *end, *ptr;
96660Sstevel@tonic-gate
96670Sstevel@tonic-gate end = &path[strlen(path) -1];
96680Sstevel@tonic-gate if (*end == '/' && end != path) {
96690Sstevel@tonic-gate ptr = skipslashes(end, path);
96700Sstevel@tonic-gate if (ptr != NULL && ptr != path) {
96710Sstevel@tonic-gate *ptr = '\0';
96720Sstevel@tonic-gate }
96730Sstevel@tonic-gate }
96740Sstevel@tonic-gate }
96750Sstevel@tonic-gate
96760Sstevel@tonic-gate #if !defined(O_XATTR)
96770Sstevel@tonic-gate int
openat64(int fd,char * name,int oflag,mode_t cmode)96780Sstevel@tonic-gate openat64(int fd, char *name, int oflag, mode_t cmode)
96790Sstevel@tonic-gate {
96800Sstevel@tonic-gate return (open64(name, oflag, cmode));
96810Sstevel@tonic-gate }
96820Sstevel@tonic-gate
96830Sstevel@tonic-gate int
openat(int fd,char * name,int oflag,mode_t cmode)96840Sstevel@tonic-gate openat(int fd, char *name, int oflag, mode_t cmode)
96850Sstevel@tonic-gate {
96860Sstevel@tonic-gate return (open(name, oflag, cmode));
96870Sstevel@tonic-gate }
96880Sstevel@tonic-gate
96890Sstevel@tonic-gate int
fchownat(int fd,char * name,uid_t owner,gid_t group,int flag)96900Sstevel@tonic-gate fchownat(int fd, char *name, uid_t owner, gid_t group, int flag)
96910Sstevel@tonic-gate {
96920Sstevel@tonic-gate if (flag == AT_SYMLINK_NOFOLLOW)
96930Sstevel@tonic-gate return (lchown(name, owner, group));
96940Sstevel@tonic-gate else
96950Sstevel@tonic-gate return (chown(name, owner, group));
96960Sstevel@tonic-gate }
96970Sstevel@tonic-gate
96980Sstevel@tonic-gate int
renameat(int fromfd,char * old,int tofd,char * new)96990Sstevel@tonic-gate renameat(int fromfd, char *old, int tofd, char *new)
97000Sstevel@tonic-gate {
97010Sstevel@tonic-gate return (rename(old, new));
97020Sstevel@tonic-gate }
97030Sstevel@tonic-gate
97040Sstevel@tonic-gate int
futimesat(int fd,char * path,struct timeval times[2])97050Sstevel@tonic-gate futimesat(int fd, char *path, struct timeval times[2])
97060Sstevel@tonic-gate {
97070Sstevel@tonic-gate return (utimes(path, times));
97080Sstevel@tonic-gate }
97090Sstevel@tonic-gate
97100Sstevel@tonic-gate int
unlinkat(int dirfd,char * path,int flag)97110Sstevel@tonic-gate unlinkat(int dirfd, char *path, int flag)
97120Sstevel@tonic-gate {
97130Sstevel@tonic-gate if (flag == AT_REMOVEDIR) {
97140Sstevel@tonic-gate return (rmdir(path));
97150Sstevel@tonic-gate } else {
97160Sstevel@tonic-gate return (unlink(path));
97170Sstevel@tonic-gate }
97180Sstevel@tonic-gate }
97190Sstevel@tonic-gate
97200Sstevel@tonic-gate int
fstatat(int fd,char * path,struct stat * buf,int flag)97210Sstevel@tonic-gate fstatat(int fd, char *path, struct stat *buf, int flag)
97220Sstevel@tonic-gate {
97230Sstevel@tonic-gate if (flag == AT_SYMLINK_NOFOLLOW)
97240Sstevel@tonic-gate return (lstat(path, buf));
97250Sstevel@tonic-gate else
97260Sstevel@tonic-gate return (stat(path, buf));
97270Sstevel@tonic-gate }
97280Sstevel@tonic-gate
97290Sstevel@tonic-gate int
attropen(char * file,char * attr,int omode,mode_t cmode)97300Sstevel@tonic-gate attropen(char *file, char *attr, int omode, mode_t cmode)
97310Sstevel@tonic-gate {
97320Sstevel@tonic-gate errno = ENOTSUP;
97330Sstevel@tonic-gate return (-1);
97340Sstevel@tonic-gate }
97350Sstevel@tonic-gate #endif
9736