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 51465Smarks * Common Development and Distribution License (the "License"). 61465Smarks * 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 /* 223819Sas145665 * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 230Sstevel@tonic-gate * Use is subject to license terms. 240Sstevel@tonic-gate */ 250Sstevel@tonic-gate 260Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 270Sstevel@tonic-gate /* All Rights Reserved */ 280Sstevel@tonic-gate 290Sstevel@tonic-gate /* 300Sstevel@tonic-gate * University Copyright- Copyright (c) 1982, 1986, 1988 310Sstevel@tonic-gate * The Regents of the University of California 320Sstevel@tonic-gate * All Rights Reserved 330Sstevel@tonic-gate * 340Sstevel@tonic-gate * University Acknowledgment- Portions of this document are derived from 350Sstevel@tonic-gate * software developed by the University of California, Berkeley, and its 360Sstevel@tonic-gate * contributors. 370Sstevel@tonic-gate */ 380Sstevel@tonic-gate 390Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 400Sstevel@tonic-gate 410Sstevel@tonic-gate /* 420Sstevel@tonic-gate * Combined mv/cp/ln command: 430Sstevel@tonic-gate * mv file1 file2 440Sstevel@tonic-gate * mv dir1 dir2 450Sstevel@tonic-gate * mv file1 ... filen dir1 460Sstevel@tonic-gate */ 470Sstevel@tonic-gate #include <sys/time.h> 480Sstevel@tonic-gate #include <signal.h> 490Sstevel@tonic-gate #include <locale.h> 500Sstevel@tonic-gate #include <stdarg.h> 510Sstevel@tonic-gate #include <sys/acl.h> 520Sstevel@tonic-gate #include <libcmdutils.h> 53789Sahrens #include <aclutils.h> 544774Sas145665 #include "getresponse.h" 550Sstevel@tonic-gate 560Sstevel@tonic-gate #define FTYPE(A) (A.st_mode) 570Sstevel@tonic-gate #define FMODE(A) (A.st_mode) 580Sstevel@tonic-gate #define UID(A) (A.st_uid) 590Sstevel@tonic-gate #define GID(A) (A.st_gid) 600Sstevel@tonic-gate #define IDENTICAL(A, B) (A.st_dev == B.st_dev && A.st_ino == B.st_ino) 610Sstevel@tonic-gate #define ISBLK(A) ((A.st_mode & S_IFMT) == S_IFBLK) 620Sstevel@tonic-gate #define ISCHR(A) ((A.st_mode & S_IFMT) == S_IFCHR) 630Sstevel@tonic-gate #define ISDIR(A) ((A.st_mode & S_IFMT) == S_IFDIR) 640Sstevel@tonic-gate #define ISDOOR(A) ((A.st_mode & S_IFMT) == S_IFDOOR) 650Sstevel@tonic-gate #define ISFIFO(A) ((A.st_mode & S_IFMT) == S_IFIFO) 660Sstevel@tonic-gate #define ISLNK(A) ((A.st_mode & S_IFMT) == S_IFLNK) 670Sstevel@tonic-gate #define ISREG(A) (((A).st_mode & S_IFMT) == S_IFREG) 680Sstevel@tonic-gate #define ISDEV(A) ((A.st_mode & S_IFMT) == S_IFCHR || \ 690Sstevel@tonic-gate (A.st_mode & S_IFMT) == S_IFBLK || \ 700Sstevel@tonic-gate (A.st_mode & S_IFMT) == S_IFIFO) 710Sstevel@tonic-gate 720Sstevel@tonic-gate #define BLKSIZE 4096 730Sstevel@tonic-gate #define PATHSIZE 1024 740Sstevel@tonic-gate #define DOT "." 750Sstevel@tonic-gate #define DOTDOT ".." 760Sstevel@tonic-gate #define DELIM '/' 770Sstevel@tonic-gate #define EQ(x, y) (strcmp(x, y) == 0) 780Sstevel@tonic-gate #define FALSE 0 790Sstevel@tonic-gate #define MODEBITS (S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO) 800Sstevel@tonic-gate #define TRUE 1 810Sstevel@tonic-gate #define MAXMAPSIZE (1024*1024*8) /* map at most 8MB */ 820Sstevel@tonic-gate #define SMALLFILESIZE (32*1024) /* don't use mmap on little files */ 830Sstevel@tonic-gate 840Sstevel@tonic-gate static char *dname(char *); 850Sstevel@tonic-gate static int lnkfil(char *, char *); 860Sstevel@tonic-gate static int cpymve(char *, char *); 870Sstevel@tonic-gate static int chkfiles(char *, char **); 880Sstevel@tonic-gate static int rcopy(char *, char *); 890Sstevel@tonic-gate static int chk_different(char *, char *); 900Sstevel@tonic-gate static int chg_time(char *, struct stat); 910Sstevel@tonic-gate static int chg_mode(char *, uid_t, gid_t, mode_t); 920Sstevel@tonic-gate static int copydir(char *, char *); 930Sstevel@tonic-gate static int copyspecial(char *); 940Sstevel@tonic-gate static int getrealpath(char *, char *); 950Sstevel@tonic-gate static void usage(void); 960Sstevel@tonic-gate static void Perror(char *); 970Sstevel@tonic-gate static void Perror2(char *, char *); 980Sstevel@tonic-gate static int use_stdin(void); 990Sstevel@tonic-gate static int copyattributes(char *, char *); 100*5331Samw static int copy_sysattr(char *, char *); 1010Sstevel@tonic-gate static void timestruc_to_timeval(timestruc_t *, struct timeval *); 1020Sstevel@tonic-gate static tree_node_t *create_tnode(dev_t, ino_t); 1030Sstevel@tonic-gate 104*5331Samw static struct stat s1, s2, s3, s4; 1050Sstevel@tonic-gate static int cpy = FALSE; 1060Sstevel@tonic-gate static int mve = FALSE; 1070Sstevel@tonic-gate static int lnk = FALSE; 1080Sstevel@tonic-gate static char *cmd; 1090Sstevel@tonic-gate static int silent = 0; 1100Sstevel@tonic-gate static int fflg = 0; 1110Sstevel@tonic-gate static int iflg = 0; 1120Sstevel@tonic-gate static int pflg = 0; 1130Sstevel@tonic-gate static int Rflg = 0; /* recursive copy */ 1140Sstevel@tonic-gate static int rflg = 0; /* recursive copy */ 1150Sstevel@tonic-gate static int sflg = 0; 1160Sstevel@tonic-gate static int Hflg = 0; /* follow cmd line arg symlink to dir */ 1170Sstevel@tonic-gate static int Lflg = 0; /* follow symlinks */ 1180Sstevel@tonic-gate static int Pflg = 0; /* do not follow symlinks */ 1190Sstevel@tonic-gate static int atflg = 0; 1200Sstevel@tonic-gate static int attrsilent = 0; 1210Sstevel@tonic-gate static int targetexists = 0; 1220Sstevel@tonic-gate static int cmdarg; /* command line argument */ 1230Sstevel@tonic-gate static avl_tree_t *stree = NULL; /* source file inode search tree */ 124789Sahrens static acl_t *s1acl; 125*5331Samw static int saflg = 0; /* 'cp' extended system attr. */ 126*5331Samw static int srcfd; 127*5331Samw static int targfd; 128*5331Samw static int sourcedirfd; 129*5331Samw static int targetdirfd; 130*5331Samw static DIR *srcdirp; 131*5331Samw static int srcattrfd; 132*5331Samw static int targattrfd; 133*5331Samw static struct stat attrdir; 134*5331Samw 135*5331Samw /* Extended system attributes support */ 136*5331Samw 137*5331Samw static int open_source(char *); 138*5331Samw static int open_target_srctarg_attrdirs(char *, char *); 139*5331Samw static int open_attrdirp(char *); 140*5331Samw static int traverse_attrfile(struct dirent *, char *, char *, int); 141*5331Samw static void rewind_attrdir(DIR *); 142*5331Samw static void close_all(); 143*5331Samw 1440Sstevel@tonic-gate 145212Scf46844 int 1460Sstevel@tonic-gate main(int argc, char *argv[]) 1470Sstevel@tonic-gate { 1480Sstevel@tonic-gate int c, i, r, errflg = 0; 1490Sstevel@tonic-gate char target[PATH_MAX]; 1500Sstevel@tonic-gate int (*move)(char *, char *); 1510Sstevel@tonic-gate 1520Sstevel@tonic-gate /* 1530Sstevel@tonic-gate * Determine command invoked (mv, cp, or ln) 1540Sstevel@tonic-gate */ 1550Sstevel@tonic-gate 1560Sstevel@tonic-gate if (cmd = strrchr(argv[0], '/')) 1570Sstevel@tonic-gate ++cmd; 1580Sstevel@tonic-gate else 1590Sstevel@tonic-gate cmd = argv[0]; 1600Sstevel@tonic-gate 1610Sstevel@tonic-gate /* 1620Sstevel@tonic-gate * Set flags based on command. 1630Sstevel@tonic-gate */ 1640Sstevel@tonic-gate 1650Sstevel@tonic-gate (void) setlocale(LC_ALL, ""); 1660Sstevel@tonic-gate #if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */ 1670Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it weren't */ 1680Sstevel@tonic-gate #endif 1690Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN); 1704774Sas145665 if (init_yes() < 0) { 1714774Sas145665 (void) fprintf(stderr, gettext(ERR_MSG_INIT_YES), 1724774Sas145665 strerror(errno)); 1734774Sas145665 exit(3); 1744774Sas145665 } 1750Sstevel@tonic-gate 1760Sstevel@tonic-gate if (EQ(cmd, "mv")) 1770Sstevel@tonic-gate mve = TRUE; 1780Sstevel@tonic-gate else if (EQ(cmd, "ln")) 1790Sstevel@tonic-gate lnk = TRUE; 1800Sstevel@tonic-gate else if (EQ(cmd, "cp")) 1810Sstevel@tonic-gate cpy = TRUE; 1820Sstevel@tonic-gate else { 1830Sstevel@tonic-gate (void) fprintf(stderr, 1840Sstevel@tonic-gate gettext("Invalid command name (%s); expecting " 1850Sstevel@tonic-gate "mv, cp, or ln.\n"), cmd); 1860Sstevel@tonic-gate exit(1); 1870Sstevel@tonic-gate } 1880Sstevel@tonic-gate 1890Sstevel@tonic-gate /* 1900Sstevel@tonic-gate * Check for options: 191*5331Samw * cp -r|-R [-H|-L|-P] [-fip@/] file1 [file2 ...] target 192*5331Samw * cp [-fiprR@/] file1 [file2 ...] target 1930Sstevel@tonic-gate * ln [-f] [-n] [-s] file1 [file2 ...] target 1940Sstevel@tonic-gate * ln [-f] [-n] [-s] file1 [file2 ...] 1950Sstevel@tonic-gate * mv [-f|i] file1 [file2 ...] target 1960Sstevel@tonic-gate * mv [-f|i] dir1 target 1970Sstevel@tonic-gate */ 1980Sstevel@tonic-gate 1990Sstevel@tonic-gate if (cpy) { 200*5331Samw while ((c = getopt(argc, argv, "fHiLpPrR@/")) != EOF) 2010Sstevel@tonic-gate switch (c) { 2020Sstevel@tonic-gate case 'f': 2030Sstevel@tonic-gate fflg++; 2040Sstevel@tonic-gate break; 2050Sstevel@tonic-gate case 'i': 2060Sstevel@tonic-gate iflg++; 2070Sstevel@tonic-gate break; 2080Sstevel@tonic-gate case 'p': 2090Sstevel@tonic-gate pflg++; 2100Sstevel@tonic-gate #ifdef XPG4 2110Sstevel@tonic-gate attrsilent = 1; 2120Sstevel@tonic-gate atflg = 0; 213*5331Samw saflg = 0; 2140Sstevel@tonic-gate #else 215*5331Samw if ((atflg == 0) && (saflg == 0)) 2160Sstevel@tonic-gate attrsilent = 1; 2170Sstevel@tonic-gate #endif 2180Sstevel@tonic-gate break; 2190Sstevel@tonic-gate case 'H': 2200Sstevel@tonic-gate /* 2210Sstevel@tonic-gate * If more than one of -H, -L, or -P are 2220Sstevel@tonic-gate * specified, only the last option specified 2230Sstevel@tonic-gate * determines the behavior. 2240Sstevel@tonic-gate */ 2250Sstevel@tonic-gate Lflg = Pflg = 0; 2260Sstevel@tonic-gate Hflg++; 2270Sstevel@tonic-gate break; 2280Sstevel@tonic-gate case 'L': 2290Sstevel@tonic-gate Hflg = Pflg = 0; 2300Sstevel@tonic-gate Lflg++; 2310Sstevel@tonic-gate break; 2320Sstevel@tonic-gate case 'P': 2330Sstevel@tonic-gate Lflg = Hflg = 0; 2340Sstevel@tonic-gate Pflg++; 2350Sstevel@tonic-gate break; 2360Sstevel@tonic-gate case 'R': 2370Sstevel@tonic-gate /* 2380Sstevel@tonic-gate * The default behavior of cp -R|-r 2390Sstevel@tonic-gate * when specified without -H|-L|-P 2400Sstevel@tonic-gate * is -L. 2410Sstevel@tonic-gate */ 2420Sstevel@tonic-gate Rflg++; 2430Sstevel@tonic-gate /*FALLTHROUGH*/ 2440Sstevel@tonic-gate case 'r': 2450Sstevel@tonic-gate rflg++; 2460Sstevel@tonic-gate break; 2470Sstevel@tonic-gate case '@': 2480Sstevel@tonic-gate atflg++; 2490Sstevel@tonic-gate attrsilent = 0; 2500Sstevel@tonic-gate #ifdef XPG4 2510Sstevel@tonic-gate pflg = 0; 2520Sstevel@tonic-gate #endif 2530Sstevel@tonic-gate break; 254*5331Samw case '/': 255*5331Samw saflg++; 256*5331Samw attrsilent = 0; 257*5331Samw #ifdef XPG4 258*5331Samw pflg = 0; 259*5331Samw #endif 260*5331Samw break; 2610Sstevel@tonic-gate default: 2620Sstevel@tonic-gate errflg++; 2630Sstevel@tonic-gate } 2640Sstevel@tonic-gate 2650Sstevel@tonic-gate /* -R or -r must be specified with -H, -L, or -P */ 2660Sstevel@tonic-gate if ((Hflg || Lflg || Pflg) && !(Rflg || rflg)) { 2670Sstevel@tonic-gate errflg++; 2680Sstevel@tonic-gate } 2690Sstevel@tonic-gate 2700Sstevel@tonic-gate } else if (mve) { 2710Sstevel@tonic-gate while ((c = getopt(argc, argv, "fis")) != EOF) 2720Sstevel@tonic-gate switch (c) { 2730Sstevel@tonic-gate case 'f': 2740Sstevel@tonic-gate silent++; 2750Sstevel@tonic-gate #ifdef XPG4 2760Sstevel@tonic-gate iflg = 0; 2770Sstevel@tonic-gate #endif 2780Sstevel@tonic-gate break; 2790Sstevel@tonic-gate case 'i': 2800Sstevel@tonic-gate iflg++; 2810Sstevel@tonic-gate #ifdef XPG4 2820Sstevel@tonic-gate silent = 0; 2830Sstevel@tonic-gate #endif 2840Sstevel@tonic-gate break; 2850Sstevel@tonic-gate default: 2860Sstevel@tonic-gate errflg++; 2870Sstevel@tonic-gate } 2880Sstevel@tonic-gate } else { /* ln */ 2890Sstevel@tonic-gate while ((c = getopt(argc, argv, "fns")) != EOF) 2900Sstevel@tonic-gate switch (c) { 2910Sstevel@tonic-gate case 'f': 2920Sstevel@tonic-gate silent++; 2930Sstevel@tonic-gate break; 2940Sstevel@tonic-gate case 'n': 2950Sstevel@tonic-gate /* silently ignored; this is the default */ 2960Sstevel@tonic-gate break; 2970Sstevel@tonic-gate case 's': 2980Sstevel@tonic-gate sflg++; 2990Sstevel@tonic-gate break; 3000Sstevel@tonic-gate default: 3010Sstevel@tonic-gate errflg++; 3020Sstevel@tonic-gate } 3030Sstevel@tonic-gate } 3040Sstevel@tonic-gate 3050Sstevel@tonic-gate /* 3060Sstevel@tonic-gate * For BSD compatibility allow - to delimit the end of 3070Sstevel@tonic-gate * options for mv. 3080Sstevel@tonic-gate */ 3090Sstevel@tonic-gate if (mve && optind < argc && (strcmp(argv[optind], "-") == 0)) 3100Sstevel@tonic-gate optind++; 3110Sstevel@tonic-gate 3120Sstevel@tonic-gate /* 3130Sstevel@tonic-gate * Check for sufficient arguments 3140Sstevel@tonic-gate * or a usage error. 3150Sstevel@tonic-gate */ 3160Sstevel@tonic-gate 3170Sstevel@tonic-gate argc -= optind; 3180Sstevel@tonic-gate argv = &argv[optind]; 3190Sstevel@tonic-gate 3200Sstevel@tonic-gate if ((argc < 2 && lnk != TRUE) || (argc < 1 && lnk == TRUE)) { 3210Sstevel@tonic-gate (void) fprintf(stderr, 3220Sstevel@tonic-gate gettext("%s: Insufficient arguments (%d)\n"), 3230Sstevel@tonic-gate cmd, argc); 3240Sstevel@tonic-gate usage(); 3250Sstevel@tonic-gate } 3260Sstevel@tonic-gate 3270Sstevel@tonic-gate if (errflg != 0) 3280Sstevel@tonic-gate usage(); 3290Sstevel@tonic-gate 3300Sstevel@tonic-gate /* 3310Sstevel@tonic-gate * If there is more than a source and target, 3320Sstevel@tonic-gate * the last argument (the target) must be a directory 3330Sstevel@tonic-gate * which really exists. 3340Sstevel@tonic-gate */ 3350Sstevel@tonic-gate 3360Sstevel@tonic-gate if (argc > 2) { 3370Sstevel@tonic-gate if (stat(argv[argc-1], &s2) < 0) { 3380Sstevel@tonic-gate (void) fprintf(stderr, 3390Sstevel@tonic-gate gettext("%s: %s not found\n"), 3400Sstevel@tonic-gate cmd, argv[argc-1]); 3410Sstevel@tonic-gate exit(2); 3420Sstevel@tonic-gate } 3430Sstevel@tonic-gate 3440Sstevel@tonic-gate if (!ISDIR(s2)) { 3450Sstevel@tonic-gate (void) fprintf(stderr, 3460Sstevel@tonic-gate gettext("%s: Target %s must be a directory\n"), 3470Sstevel@tonic-gate cmd, argv[argc-1]); 3480Sstevel@tonic-gate usage(); 3490Sstevel@tonic-gate } 3500Sstevel@tonic-gate } 3510Sstevel@tonic-gate 3520Sstevel@tonic-gate if (strlen(argv[argc-1]) >= PATH_MAX) { 3530Sstevel@tonic-gate (void) fprintf(stderr, 3540Sstevel@tonic-gate gettext("%s: Target %s file name length exceeds PATH_MAX" 3550Sstevel@tonic-gate " %d\n"), cmd, argv[argc-1], PATH_MAX); 3560Sstevel@tonic-gate exit(78); 3570Sstevel@tonic-gate } 3580Sstevel@tonic-gate 3590Sstevel@tonic-gate if (argc == 1) { 3600Sstevel@tonic-gate if (!lnk) 3610Sstevel@tonic-gate usage(); 3620Sstevel@tonic-gate (void) strcpy(target, "."); 3630Sstevel@tonic-gate } else { 3640Sstevel@tonic-gate (void) strcpy(target, argv[--argc]); 3650Sstevel@tonic-gate } 3660Sstevel@tonic-gate 3670Sstevel@tonic-gate /* 3680Sstevel@tonic-gate * Perform a multiple argument mv|cp|ln by 3690Sstevel@tonic-gate * multiple invocations of cpymve() or lnkfil(). 3700Sstevel@tonic-gate */ 3710Sstevel@tonic-gate if (lnk) 3720Sstevel@tonic-gate move = lnkfil; 3730Sstevel@tonic-gate else 3740Sstevel@tonic-gate move = cpymve; 3750Sstevel@tonic-gate 3760Sstevel@tonic-gate r = 0; 3770Sstevel@tonic-gate for (i = 0; i < argc; i++) { 3780Sstevel@tonic-gate stree = NULL; 3790Sstevel@tonic-gate cmdarg = 1; 3800Sstevel@tonic-gate r += move(argv[i], target); 3810Sstevel@tonic-gate } 3820Sstevel@tonic-gate 3830Sstevel@tonic-gate /* 3840Sstevel@tonic-gate * Show errors by nonzero exit code. 3850Sstevel@tonic-gate */ 3860Sstevel@tonic-gate 387212Scf46844 return (r?2:0); 3880Sstevel@tonic-gate } 3890Sstevel@tonic-gate 3900Sstevel@tonic-gate static int 3910Sstevel@tonic-gate lnkfil(char *source, char *target) 3920Sstevel@tonic-gate { 3930Sstevel@tonic-gate char *buf = NULL; 3940Sstevel@tonic-gate 3950Sstevel@tonic-gate if (sflg) { 3960Sstevel@tonic-gate 3970Sstevel@tonic-gate /* 3980Sstevel@tonic-gate * If target is a directory make complete 3990Sstevel@tonic-gate * name of the new symbolic link within that 4000Sstevel@tonic-gate * directory. 4010Sstevel@tonic-gate */ 4020Sstevel@tonic-gate 4030Sstevel@tonic-gate if ((stat(target, &s2) >= 0) && ISDIR(s2)) { 4040Sstevel@tonic-gate size_t len; 4050Sstevel@tonic-gate 4060Sstevel@tonic-gate len = strlen(target) + strlen(dname(source)) + 4; 4070Sstevel@tonic-gate if ((buf = (char *)malloc(len)) == NULL) { 4080Sstevel@tonic-gate (void) fprintf(stderr, 4090Sstevel@tonic-gate gettext("%s: Insufficient memory " 4100Sstevel@tonic-gate "to %s %s\n"), cmd, cmd, source); 4110Sstevel@tonic-gate exit(3); 4120Sstevel@tonic-gate } 4130Sstevel@tonic-gate (void) snprintf(buf, len, "%s/%s", 4140Sstevel@tonic-gate target, dname(source)); 4150Sstevel@tonic-gate target = buf; 4160Sstevel@tonic-gate } 4170Sstevel@tonic-gate 4180Sstevel@tonic-gate /* 4190Sstevel@tonic-gate * Check to see if the file exists already 4200Sstevel@tonic-gate */ 4210Sstevel@tonic-gate 4220Sstevel@tonic-gate if ((stat(target, &s2) == 0)) { 4230Sstevel@tonic-gate /* 4240Sstevel@tonic-gate * Check if the silent flag is set ie. the -f option 4250Sstevel@tonic-gate * is used. If so, use unlink to remove the current 4260Sstevel@tonic-gate * target to replace with the new target, specified 4270Sstevel@tonic-gate * on the command line. Proceed with symlink. 4280Sstevel@tonic-gate */ 4290Sstevel@tonic-gate if (silent) { 4300Sstevel@tonic-gate if (unlink(target) < 0) { 4310Sstevel@tonic-gate (void) fprintf(stderr, 4320Sstevel@tonic-gate gettext("%s: cannot unlink %s: "), 4330Sstevel@tonic-gate cmd, target); 4340Sstevel@tonic-gate perror(""); 4350Sstevel@tonic-gate return (1); 4360Sstevel@tonic-gate } 4370Sstevel@tonic-gate } 4380Sstevel@tonic-gate } 4390Sstevel@tonic-gate 4400Sstevel@tonic-gate 4410Sstevel@tonic-gate /* 4420Sstevel@tonic-gate * Create a symbolic link to the source. 4430Sstevel@tonic-gate */ 4440Sstevel@tonic-gate 4450Sstevel@tonic-gate if (symlink(source, target) < 0) { 4460Sstevel@tonic-gate (void) fprintf(stderr, 4470Sstevel@tonic-gate gettext("%s: cannot create %s: "), 4480Sstevel@tonic-gate cmd, target); 4490Sstevel@tonic-gate perror(""); 4500Sstevel@tonic-gate if (buf != NULL) 4510Sstevel@tonic-gate free(buf); 4520Sstevel@tonic-gate return (1); 4530Sstevel@tonic-gate } 4540Sstevel@tonic-gate if (buf != NULL) 4550Sstevel@tonic-gate free(buf); 4560Sstevel@tonic-gate return (0); 4570Sstevel@tonic-gate } 4580Sstevel@tonic-gate 4590Sstevel@tonic-gate switch (chkfiles(source, &target)) { 4600Sstevel@tonic-gate case 1: return (1); 4610Sstevel@tonic-gate case 2: return (0); 4620Sstevel@tonic-gate /* default - fall through */ 4630Sstevel@tonic-gate } 4640Sstevel@tonic-gate 4650Sstevel@tonic-gate /* 4660Sstevel@tonic-gate * Make sure source file is not a directory, 4670Sstevel@tonic-gate * we can't link directories... 4680Sstevel@tonic-gate */ 4690Sstevel@tonic-gate 4700Sstevel@tonic-gate if (ISDIR(s1)) { 4710Sstevel@tonic-gate (void) fprintf(stderr, 4720Sstevel@tonic-gate gettext("%s: %s is a directory\n"), cmd, source); 4730Sstevel@tonic-gate return (1); 4740Sstevel@tonic-gate } 4750Sstevel@tonic-gate 4760Sstevel@tonic-gate /* 4770Sstevel@tonic-gate * hard link, call link() and return. 4780Sstevel@tonic-gate */ 4790Sstevel@tonic-gate 4800Sstevel@tonic-gate if (link(source, target) < 0) { 4810Sstevel@tonic-gate if (errno == EXDEV) 4820Sstevel@tonic-gate (void) fprintf(stderr, 4830Sstevel@tonic-gate gettext("%s: %s is on a different file system\n"), 4840Sstevel@tonic-gate cmd, target); 4850Sstevel@tonic-gate else { 4860Sstevel@tonic-gate (void) fprintf(stderr, 4870Sstevel@tonic-gate gettext("%s: cannot create link %s: "), 4880Sstevel@tonic-gate cmd, target); 4890Sstevel@tonic-gate perror(""); 4900Sstevel@tonic-gate } 4910Sstevel@tonic-gate if (buf != NULL) 4920Sstevel@tonic-gate free(buf); 4930Sstevel@tonic-gate return (1); 4940Sstevel@tonic-gate } else { 4950Sstevel@tonic-gate if (buf != NULL) 4960Sstevel@tonic-gate free(buf); 4970Sstevel@tonic-gate return (0); 4980Sstevel@tonic-gate } 4990Sstevel@tonic-gate } 5000Sstevel@tonic-gate 5010Sstevel@tonic-gate static int 5020Sstevel@tonic-gate cpymve(char *source, char *target) 5030Sstevel@tonic-gate { 5040Sstevel@tonic-gate int n; 5050Sstevel@tonic-gate int fi, fo; 5060Sstevel@tonic-gate int ret = 0; 5070Sstevel@tonic-gate int attret = 0; 508*5331Samw int sattret = 0; 5090Sstevel@tonic-gate int errno_save; 5100Sstevel@tonic-gate 5110Sstevel@tonic-gate switch (chkfiles(source, &target)) { 5120Sstevel@tonic-gate case 1: return (1); 5130Sstevel@tonic-gate case 2: return (0); 5140Sstevel@tonic-gate /* default - fall through */ 5150Sstevel@tonic-gate } 5160Sstevel@tonic-gate 5170Sstevel@tonic-gate /* 5180Sstevel@tonic-gate * If it's a recursive copy and source 5190Sstevel@tonic-gate * is a directory, then call rcopy (from copydir). 5200Sstevel@tonic-gate */ 5210Sstevel@tonic-gate if (cpy) { 5220Sstevel@tonic-gate if (ISDIR(s1)) { 5230Sstevel@tonic-gate int rc; 5240Sstevel@tonic-gate avl_index_t where = 0; 5250Sstevel@tonic-gate tree_node_t *tnode; 5260Sstevel@tonic-gate tree_node_t *tptr; 5270Sstevel@tonic-gate dev_t save_dev = s1.st_dev; 5280Sstevel@tonic-gate ino_t save_ino = s1.st_ino; 5290Sstevel@tonic-gate 5300Sstevel@tonic-gate /* 5310Sstevel@tonic-gate * We will be recursing into the directory so 5320Sstevel@tonic-gate * save the inode information to a search tree 5330Sstevel@tonic-gate * to avoid getting into an endless loop. 5340Sstevel@tonic-gate */ 5350Sstevel@tonic-gate if ((rc = add_tnode(&stree, save_dev, save_ino)) != 1) { 5360Sstevel@tonic-gate if (rc == 0) { 5370Sstevel@tonic-gate /* 5380Sstevel@tonic-gate * We've already visited this directory. 5390Sstevel@tonic-gate * Don't remove the search tree entry 5400Sstevel@tonic-gate * to make sure we don't get into an 5410Sstevel@tonic-gate * endless loop if revisited from a 5420Sstevel@tonic-gate * different part of the hierarchy. 5430Sstevel@tonic-gate */ 5440Sstevel@tonic-gate (void) fprintf(stderr, gettext( 5450Sstevel@tonic-gate "%s: cycle detected: %s\n"), 5460Sstevel@tonic-gate cmd, source); 5470Sstevel@tonic-gate } else { 5480Sstevel@tonic-gate Perror(source); 5490Sstevel@tonic-gate } 5500Sstevel@tonic-gate return (1); 5510Sstevel@tonic-gate } 5520Sstevel@tonic-gate 5530Sstevel@tonic-gate cmdarg = 0; 5540Sstevel@tonic-gate rc = copydir(source, target); 5550Sstevel@tonic-gate 5560Sstevel@tonic-gate /* 5570Sstevel@tonic-gate * Create a tnode to get an index to the matching 5580Sstevel@tonic-gate * node (same dev and inode) in the search tree, 5590Sstevel@tonic-gate * then use the index to remove the matching node 5600Sstevel@tonic-gate * so it we do not wrongly detect a cycle when 5610Sstevel@tonic-gate * revisiting this directory from another part of 5620Sstevel@tonic-gate * the hierarchy. 5630Sstevel@tonic-gate */ 5640Sstevel@tonic-gate if ((tnode = create_tnode(save_dev, 5650Sstevel@tonic-gate save_ino)) == NULL) { 5660Sstevel@tonic-gate Perror(source); 5670Sstevel@tonic-gate return (1); 5680Sstevel@tonic-gate } 5690Sstevel@tonic-gate if ((tptr = avl_find(stree, tnode, &where)) != NULL) { 5700Sstevel@tonic-gate avl_remove(stree, tptr); 5710Sstevel@tonic-gate } 5720Sstevel@tonic-gate free(tptr); 5730Sstevel@tonic-gate free(tnode); 5740Sstevel@tonic-gate return (rc); 5750Sstevel@tonic-gate 5760Sstevel@tonic-gate } else if (ISDEV(s1) && Rflg) { 5770Sstevel@tonic-gate return (copyspecial(target)); 5780Sstevel@tonic-gate } else { 5790Sstevel@tonic-gate goto copy; 5800Sstevel@tonic-gate } 5810Sstevel@tonic-gate } 5820Sstevel@tonic-gate 5830Sstevel@tonic-gate if (mve) { 5840Sstevel@tonic-gate if (rename(source, target) >= 0) 5850Sstevel@tonic-gate return (0); 5860Sstevel@tonic-gate if (errno != EXDEV) { 5870Sstevel@tonic-gate if (errno == ENOTDIR && ISDIR(s1)) { 5880Sstevel@tonic-gate (void) fprintf(stderr, 5890Sstevel@tonic-gate gettext("%s: %s is a directory\n"), 5900Sstevel@tonic-gate cmd, source); 5910Sstevel@tonic-gate return (1); 5920Sstevel@tonic-gate } 5930Sstevel@tonic-gate (void) fprintf(stderr, 5940Sstevel@tonic-gate gettext("%s: cannot rename %s to %s: "), 5950Sstevel@tonic-gate cmd, source, target); 5960Sstevel@tonic-gate perror(""); 5970Sstevel@tonic-gate return (1); 5980Sstevel@tonic-gate } 5990Sstevel@tonic-gate 6000Sstevel@tonic-gate /* 6010Sstevel@tonic-gate * cannot move a non-directory (source) onto an existing 6020Sstevel@tonic-gate * directory (target) 6030Sstevel@tonic-gate * 6040Sstevel@tonic-gate */ 6050Sstevel@tonic-gate if (targetexists && ISDIR(s2) && (!ISDIR(s1))) { 6060Sstevel@tonic-gate (void) fprintf(stderr, 6070Sstevel@tonic-gate gettext("%s: cannot mv a non directory %s " 6080Sstevel@tonic-gate "over existing directory" 6090Sstevel@tonic-gate " %s \n"), cmd, source, target); 6100Sstevel@tonic-gate return (1); 6110Sstevel@tonic-gate } 6120Sstevel@tonic-gate if (ISDIR(s1)) { 6130Sstevel@tonic-gate #ifdef XPG4 6140Sstevel@tonic-gate if (targetexists && ISDIR(s2)) { 6150Sstevel@tonic-gate /* existing target dir must be empty */ 6160Sstevel@tonic-gate if (rmdir(target) < 0) { 6170Sstevel@tonic-gate errno_save = errno; 6180Sstevel@tonic-gate (void) fprintf(stderr, 6190Sstevel@tonic-gate gettext("%s: cannot rmdir %s: "), 6200Sstevel@tonic-gate cmd, target); 6210Sstevel@tonic-gate errno = errno_save; 6220Sstevel@tonic-gate perror(""); 6230Sstevel@tonic-gate return (1); 6240Sstevel@tonic-gate } 6250Sstevel@tonic-gate } 6260Sstevel@tonic-gate #endif 6270Sstevel@tonic-gate if ((n = copydir(source, target)) == 0) 6280Sstevel@tonic-gate (void) rmdir(source); 6290Sstevel@tonic-gate return (n); 6300Sstevel@tonic-gate } 6310Sstevel@tonic-gate 6320Sstevel@tonic-gate /* doors can't be moved across filesystems */ 6330Sstevel@tonic-gate if (ISDOOR(s1)) { 6340Sstevel@tonic-gate (void) fprintf(stderr, 6350Sstevel@tonic-gate gettext("%s: %s: can't move door " 6360Sstevel@tonic-gate "across file systems\n"), cmd, source); 6370Sstevel@tonic-gate return (1); 6380Sstevel@tonic-gate } 6390Sstevel@tonic-gate /* 6400Sstevel@tonic-gate * File can't be renamed, try to recreate the symbolic 6410Sstevel@tonic-gate * link or special device, or copy the file wholesale 6420Sstevel@tonic-gate * between file systems. 6430Sstevel@tonic-gate */ 6440Sstevel@tonic-gate if (ISLNK(s1)) { 6450Sstevel@tonic-gate register int m; 6460Sstevel@tonic-gate register mode_t md; 6470Sstevel@tonic-gate char symln[PATH_MAX + 1]; 6480Sstevel@tonic-gate 6490Sstevel@tonic-gate if (targetexists && unlink(target) < 0) { 6500Sstevel@tonic-gate (void) fprintf(stderr, 6510Sstevel@tonic-gate gettext("%s: cannot unlink %s: "), 6520Sstevel@tonic-gate cmd, target); 6530Sstevel@tonic-gate perror(""); 6540Sstevel@tonic-gate return (1); 6550Sstevel@tonic-gate } 6560Sstevel@tonic-gate 6570Sstevel@tonic-gate if ((m = readlink(source, symln, 6580Sstevel@tonic-gate sizeof (symln) - 1)) < 0) { 6590Sstevel@tonic-gate Perror(source); 6600Sstevel@tonic-gate return (1); 6610Sstevel@tonic-gate } 6620Sstevel@tonic-gate symln[m] = '\0'; 6630Sstevel@tonic-gate 6640Sstevel@tonic-gate md = umask(~(s1.st_mode & MODEBITS)); 6650Sstevel@tonic-gate if (symlink(symln, target) < 0) { 6660Sstevel@tonic-gate Perror(target); 6670Sstevel@tonic-gate return (1); 6680Sstevel@tonic-gate } 6690Sstevel@tonic-gate (void) umask(md); 6700Sstevel@tonic-gate m = lchown(target, UID(s1), GID(s1)); 6710Sstevel@tonic-gate #ifdef XPG4 6720Sstevel@tonic-gate if (m < 0) { 6730Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s: cannot" 6740Sstevel@tonic-gate " change owner and group of" 6750Sstevel@tonic-gate " %s: "), cmd, target); 6760Sstevel@tonic-gate perror(""); 6770Sstevel@tonic-gate } 6780Sstevel@tonic-gate #endif 6790Sstevel@tonic-gate goto cleanup; 6800Sstevel@tonic-gate } 6810Sstevel@tonic-gate if (ISDEV(s1)) { 6820Sstevel@tonic-gate 6830Sstevel@tonic-gate if (targetexists && unlink(target) < 0) { 6840Sstevel@tonic-gate (void) fprintf(stderr, 6850Sstevel@tonic-gate gettext("%s: cannot unlink %s: "), 6860Sstevel@tonic-gate cmd, target); 6870Sstevel@tonic-gate perror(""); 6880Sstevel@tonic-gate return (1); 6890Sstevel@tonic-gate } 6900Sstevel@tonic-gate 6910Sstevel@tonic-gate if (mknod(target, s1.st_mode, s1.st_rdev) < 0) { 6920Sstevel@tonic-gate Perror(target); 6930Sstevel@tonic-gate return (1); 6940Sstevel@tonic-gate } 6950Sstevel@tonic-gate 6960Sstevel@tonic-gate (void) chg_mode(target, UID(s1), GID(s1), FMODE(s1)); 6970Sstevel@tonic-gate (void) chg_time(target, s1); 6980Sstevel@tonic-gate goto cleanup; 6990Sstevel@tonic-gate } 7000Sstevel@tonic-gate 7010Sstevel@tonic-gate if (ISREG(s1)) { 7020Sstevel@tonic-gate if (ISDIR(s2)) { 7030Sstevel@tonic-gate if (targetexists && rmdir(target) < 0) { 7040Sstevel@tonic-gate (void) fprintf(stderr, 7050Sstevel@tonic-gate gettext("%s: cannot rmdir %s: "), 7060Sstevel@tonic-gate cmd, target); 7070Sstevel@tonic-gate perror(""); 7080Sstevel@tonic-gate return (1); 7090Sstevel@tonic-gate } 7100Sstevel@tonic-gate } else { 7110Sstevel@tonic-gate if (targetexists && unlink(target) < 0) { 7120Sstevel@tonic-gate (void) fprintf(stderr, 7130Sstevel@tonic-gate gettext("%s: cannot unlink %s: "), 7140Sstevel@tonic-gate cmd, target); 7150Sstevel@tonic-gate perror(""); 7160Sstevel@tonic-gate return (1); 7170Sstevel@tonic-gate } 7180Sstevel@tonic-gate } 7190Sstevel@tonic-gate 7200Sstevel@tonic-gate 7210Sstevel@tonic-gate copy: 7220Sstevel@tonic-gate /* 7230Sstevel@tonic-gate * If the source file is a symlink, and either 7240Sstevel@tonic-gate * -P or -H flag (only if -H is specified and the 7250Sstevel@tonic-gate * source file is not a command line argument) 7260Sstevel@tonic-gate * were specified, then action is taken on the symlink 7270Sstevel@tonic-gate * itself, not the file referenced by the symlink. 7280Sstevel@tonic-gate * Note: this is executed for 'cp' only. 7290Sstevel@tonic-gate */ 7300Sstevel@tonic-gate if (cpy && (Pflg || (Hflg && !cmdarg)) && (ISLNK(s1))) { 7310Sstevel@tonic-gate int m; 7320Sstevel@tonic-gate mode_t md; 7330Sstevel@tonic-gate char symln[PATH_MAX + 1]; 7340Sstevel@tonic-gate 7350Sstevel@tonic-gate m = readlink(source, symln, sizeof (symln) - 1); 7360Sstevel@tonic-gate 7370Sstevel@tonic-gate if (m < 0) { 7380Sstevel@tonic-gate Perror(source); 7390Sstevel@tonic-gate return (1); 7400Sstevel@tonic-gate } 7410Sstevel@tonic-gate symln[m] = '\0'; 7420Sstevel@tonic-gate 7430Sstevel@tonic-gate /* 7440Sstevel@tonic-gate * Copy the sym link to the target. 7450Sstevel@tonic-gate * Note: If the target exists, write a 7460Sstevel@tonic-gate * diagnostic message, do nothing more 7470Sstevel@tonic-gate * with the source file, and return to 7480Sstevel@tonic-gate * process any remaining files. 7490Sstevel@tonic-gate */ 7500Sstevel@tonic-gate md = umask(~(s1.st_mode & MODEBITS)); 7510Sstevel@tonic-gate if (symlink(symln, target) < 0) { 7520Sstevel@tonic-gate Perror(target); 7530Sstevel@tonic-gate return (1); 7540Sstevel@tonic-gate } 7550Sstevel@tonic-gate (void) umask(md); 7560Sstevel@tonic-gate m = lchown(target, UID(s1), GID(s1)); 7570Sstevel@tonic-gate 7580Sstevel@tonic-gate if (m < 0) { 7590Sstevel@tonic-gate (void) fprintf(stderr, gettext( 7600Sstevel@tonic-gate "cp: cannot change owner and " 7610Sstevel@tonic-gate "group of %s:"), target); 7620Sstevel@tonic-gate perror(""); 7630Sstevel@tonic-gate } 7640Sstevel@tonic-gate } else { 7650Sstevel@tonic-gate /* 7660Sstevel@tonic-gate * Copy the file. If it happens to be a 7670Sstevel@tonic-gate * symlink, copy the file referenced 7680Sstevel@tonic-gate * by the symlink. 7690Sstevel@tonic-gate */ 7700Sstevel@tonic-gate fi = open(source, O_RDONLY); 7710Sstevel@tonic-gate if (fi < 0) { 7720Sstevel@tonic-gate (void) fprintf(stderr, 7730Sstevel@tonic-gate gettext("%s: cannot open %s: "), 7740Sstevel@tonic-gate cmd, source); 7750Sstevel@tonic-gate perror(""); 7760Sstevel@tonic-gate return (1); 7770Sstevel@tonic-gate } 7780Sstevel@tonic-gate 7790Sstevel@tonic-gate fo = creat(target, s1.st_mode & MODEBITS); 7800Sstevel@tonic-gate if (fo < 0) { 7810Sstevel@tonic-gate /* 7820Sstevel@tonic-gate * If -f and creat() failed, unlink 7830Sstevel@tonic-gate * and try again. 7840Sstevel@tonic-gate */ 7850Sstevel@tonic-gate if (fflg) { 7860Sstevel@tonic-gate (void) unlink(target); 7870Sstevel@tonic-gate fo = creat(target, 7880Sstevel@tonic-gate s1.st_mode & MODEBITS); 7890Sstevel@tonic-gate } 7900Sstevel@tonic-gate } 7910Sstevel@tonic-gate if (fo < 0) { 7920Sstevel@tonic-gate (void) fprintf(stderr, 7930Sstevel@tonic-gate gettext("%s: cannot create %s: "), 7940Sstevel@tonic-gate cmd, target); 7950Sstevel@tonic-gate perror(""); 7960Sstevel@tonic-gate (void) close(fi); 7970Sstevel@tonic-gate return (1); 7980Sstevel@tonic-gate } else { 7990Sstevel@tonic-gate /* stat the new file, its used below */ 8000Sstevel@tonic-gate (void) stat(target, &s2); 8010Sstevel@tonic-gate } 8020Sstevel@tonic-gate 8030Sstevel@tonic-gate /* 8040Sstevel@tonic-gate * Set target's permissions to the source 8050Sstevel@tonic-gate * before any copying so that any partially 8060Sstevel@tonic-gate * copied file will have the source's 8070Sstevel@tonic-gate * permissions (at most) or umask permissions 8080Sstevel@tonic-gate * whichever is the most restrictive. 8090Sstevel@tonic-gate * 8100Sstevel@tonic-gate * ACL for regular files 8110Sstevel@tonic-gate */ 8120Sstevel@tonic-gate 8130Sstevel@tonic-gate if (pflg || mve) { 8140Sstevel@tonic-gate (void) chmod(target, FMODE(s1)); 815789Sahrens if (s1acl != NULL) { 816789Sahrens if ((acl_set(target, 817789Sahrens s1acl)) < 0) { 8180Sstevel@tonic-gate if (pflg || mve) { 8190Sstevel@tonic-gate (void) fprintf( 8200Sstevel@tonic-gate stderr, 8210Sstevel@tonic-gate "%s: failed to set acl entries on %s\n", 8220Sstevel@tonic-gate cmd, 8230Sstevel@tonic-gate target); 8240Sstevel@tonic-gate } 8251465Smarks acl_free(s1acl); 8261465Smarks s1acl = NULL; 8270Sstevel@tonic-gate /* 8280Sstevel@tonic-gate * else: silent and 8290Sstevel@tonic-gate * continue 8300Sstevel@tonic-gate */ 8310Sstevel@tonic-gate } 8320Sstevel@tonic-gate } 8330Sstevel@tonic-gate } 8340Sstevel@tonic-gate 8350Sstevel@tonic-gate if (fstat(fi, &s1) < 0) { 8360Sstevel@tonic-gate (void) fprintf(stderr, 8370Sstevel@tonic-gate gettext("%s: cannot access %s\n"), 8380Sstevel@tonic-gate cmd, source); 8390Sstevel@tonic-gate return (1); 8400Sstevel@tonic-gate } 8410Sstevel@tonic-gate if (IDENTICAL(s1, s2)) { 8420Sstevel@tonic-gate (void) fprintf(stderr, 8430Sstevel@tonic-gate gettext( 8440Sstevel@tonic-gate "%s: %s and %s are identical\n"), 8450Sstevel@tonic-gate cmd, source, target); 8460Sstevel@tonic-gate return (1); 8470Sstevel@tonic-gate } 8480Sstevel@tonic-gate 849*5331Samw if (writefile(fi, fo, source, target, NULL, 850*5331Samw NULL, &s1, &s2) != 0) { 8510Sstevel@tonic-gate return (1); 8520Sstevel@tonic-gate } 8530Sstevel@tonic-gate 8540Sstevel@tonic-gate (void) close(fi); 8550Sstevel@tonic-gate if (close(fo) < 0) { 8560Sstevel@tonic-gate Perror2(target, "write"); 8570Sstevel@tonic-gate return (1); 8580Sstevel@tonic-gate } 8590Sstevel@tonic-gate } 860*5331Samw /* Copy regular extended attributes */ 861*5331Samw if (pflg || atflg || mve || saflg) { 8620Sstevel@tonic-gate attret = copyattributes(source, target); 8630Sstevel@tonic-gate if (attret != 0 && !attrsilent) { 8640Sstevel@tonic-gate (void) fprintf(stderr, gettext( 8654774Sas145665 "%s: Failed to preserve" 8664774Sas145665 " extended attributes of file" 8674774Sas145665 " %s\n"), cmd, source); 8680Sstevel@tonic-gate } 869*5331Samw /* Copy extended system attributes */ 870*5331Samw if (pflg || mve || saflg) { 871*5331Samw sattret = copy_sysattr(source, target); 872*5331Samw if (sattret != 0 && !attrsilent) { 873*5331Samw (void) fprintf(stderr, gettext( 874*5331Samw "%s: Failed to preserve " 875*5331Samw "extended system " 876*5331Samw "attributes of file " 877*5331Samw "%s\n"), cmd, source); 878*5331Samw } 879*5331Samw } 8800Sstevel@tonic-gate if (mve && attret != 0) { 8810Sstevel@tonic-gate (void) unlink(target); 8820Sstevel@tonic-gate return (1); 8830Sstevel@tonic-gate } 884*5331Samw if (attrsilent) { 8850Sstevel@tonic-gate attret = 0; 886*5331Samw sattret = 0; 887*5331Samw } 8880Sstevel@tonic-gate } 8890Sstevel@tonic-gate 8900Sstevel@tonic-gate /* 8910Sstevel@tonic-gate * XPG4: the write system call will clear setgid 8920Sstevel@tonic-gate * and setuid bits, so set them again. 8930Sstevel@tonic-gate */ 8940Sstevel@tonic-gate if (pflg || mve) { 8950Sstevel@tonic-gate if ((ret = chg_mode(target, UID(s1), GID(s1), 8960Sstevel@tonic-gate FMODE(s1))) > 0) 8970Sstevel@tonic-gate return (1); 8981231Smarks /* 8991231Smarks * Reapply ACL, since chmod may have 9001231Smarks * altered ACL 9011231Smarks */ 9021231Smarks if (s1acl != NULL) { 9031231Smarks if ((acl_set(target, s1acl)) < 0) { 9041231Smarks if (pflg || mve) { 9051231Smarks (void) fprintf( 9061231Smarks stderr, 9071231Smarks "%s: failed to set acl entries on %s\n", 9081231Smarks cmd, 9091231Smarks target); 9101231Smarks } 9111231Smarks /* 9121231Smarks * else: silent and 9131231Smarks * continue 9141231Smarks */ 9151231Smarks } 9161231Smarks } 9170Sstevel@tonic-gate if ((ret = chg_time(target, s1)) > 0) 9180Sstevel@tonic-gate return (1); 9190Sstevel@tonic-gate } 9200Sstevel@tonic-gate if (cpy) { 9210Sstevel@tonic-gate if (attret != 0) 9220Sstevel@tonic-gate return (1); 9230Sstevel@tonic-gate return (0); 9240Sstevel@tonic-gate } 9250Sstevel@tonic-gate goto cleanup; 9260Sstevel@tonic-gate } 9270Sstevel@tonic-gate (void) fprintf(stderr, 9280Sstevel@tonic-gate gettext("%s: %s: unknown file type 0x%x\n"), cmd, 9294774Sas145665 source, (s1.st_mode & S_IFMT)); 9300Sstevel@tonic-gate return (1); 9310Sstevel@tonic-gate 9320Sstevel@tonic-gate cleanup: 9330Sstevel@tonic-gate if (unlink(source) < 0) { 9340Sstevel@tonic-gate (void) unlink(target); 9350Sstevel@tonic-gate (void) fprintf(stderr, 9360Sstevel@tonic-gate gettext("%s: cannot unlink %s: "), 9370Sstevel@tonic-gate cmd, source); 9380Sstevel@tonic-gate perror(""); 9390Sstevel@tonic-gate return (1); 9400Sstevel@tonic-gate } 9410Sstevel@tonic-gate if (attret != 0) 9420Sstevel@tonic-gate return (attret); 9430Sstevel@tonic-gate return (ret); 9440Sstevel@tonic-gate } 9450Sstevel@tonic-gate /*NOTREACHED*/ 946212Scf46844 return (ret); 9470Sstevel@tonic-gate } 9480Sstevel@tonic-gate 9490Sstevel@tonic-gate /* 9500Sstevel@tonic-gate * create_tnode() 9510Sstevel@tonic-gate * 9520Sstevel@tonic-gate * Create a node for use with the search tree which contains the 9530Sstevel@tonic-gate * inode information (device id and inode number). 9540Sstevel@tonic-gate * 9550Sstevel@tonic-gate * Input 9560Sstevel@tonic-gate * dev - device id 9570Sstevel@tonic-gate * ino - inode number 9580Sstevel@tonic-gate * 9590Sstevel@tonic-gate * Output 9600Sstevel@tonic-gate * tnode - NULL on error, otherwise returns a tnode structure 9610Sstevel@tonic-gate * which contains the input device id and inode number. 9620Sstevel@tonic-gate */ 9630Sstevel@tonic-gate static tree_node_t * 9640Sstevel@tonic-gate create_tnode(dev_t dev, ino_t ino) 9650Sstevel@tonic-gate { 9660Sstevel@tonic-gate tree_node_t *tnode; 9670Sstevel@tonic-gate 9680Sstevel@tonic-gate if ((tnode = (tree_node_t *)malloc(sizeof (tree_node_t))) != NULL) { 9690Sstevel@tonic-gate tnode->node_dev = dev; 9700Sstevel@tonic-gate tnode->node_ino = ino; 9710Sstevel@tonic-gate } 9720Sstevel@tonic-gate 9730Sstevel@tonic-gate return (tnode); 9740Sstevel@tonic-gate } 9750Sstevel@tonic-gate 9760Sstevel@tonic-gate static int 9770Sstevel@tonic-gate chkfiles(char *source, char **to) 9780Sstevel@tonic-gate { 9790Sstevel@tonic-gate char *buf = (char *)NULL; 9800Sstevel@tonic-gate int (*statf)() = (cpy && 9814774Sas145665 !(Pflg || (Hflg && !cmdarg))) ? stat : lstat; 9820Sstevel@tonic-gate char *target = *to; 983789Sahrens int error; 9840Sstevel@tonic-gate 9850Sstevel@tonic-gate /* 9860Sstevel@tonic-gate * Make sure source file exists. 9870Sstevel@tonic-gate */ 9880Sstevel@tonic-gate if ((*statf)(source, &s1) < 0) { 9890Sstevel@tonic-gate /* 9900Sstevel@tonic-gate * Keep the old error message except when someone tries to 9910Sstevel@tonic-gate * mv/cp/ln a symbolic link that has a trailing slash and 9920Sstevel@tonic-gate * points to a file. 9930Sstevel@tonic-gate */ 9940Sstevel@tonic-gate if (errno == ENOTDIR) 9950Sstevel@tonic-gate (void) fprintf(stderr, "%s: %s: %s\n", cmd, source, 9960Sstevel@tonic-gate strerror(errno)); 9970Sstevel@tonic-gate else 9980Sstevel@tonic-gate (void) fprintf(stderr, 9990Sstevel@tonic-gate gettext("%s: cannot access %s\n"), cmd, source); 10000Sstevel@tonic-gate return (1); 10010Sstevel@tonic-gate } 10020Sstevel@tonic-gate 10030Sstevel@tonic-gate /* 10040Sstevel@tonic-gate * Get ACL info: don't bother with ln or mv'ing symlinks 10050Sstevel@tonic-gate */ 10060Sstevel@tonic-gate if ((!lnk) && !(mve && ISLNK(s1))) { 1007789Sahrens if (s1acl != NULL) { 1008789Sahrens acl_free(s1acl); 1009789Sahrens s1acl = NULL; 10100Sstevel@tonic-gate } 1011789Sahrens if ((error = acl_get(source, ACL_NO_TRIVIAL, &s1acl)) != 0) { 1012789Sahrens (void) fprintf(stderr, 1013789Sahrens "%s: failed to get acl entries: %s\n", source, 1014789Sahrens acl_strerror(error)); 1015789Sahrens return (1); 10160Sstevel@tonic-gate } 10170Sstevel@tonic-gate /* else: just permission bits */ 10180Sstevel@tonic-gate } 10190Sstevel@tonic-gate 10200Sstevel@tonic-gate /* 10210Sstevel@tonic-gate * If stat fails, then the target doesn't exist, 10220Sstevel@tonic-gate * we will create a new target with default file type of regular. 10230Sstevel@tonic-gate */ 10240Sstevel@tonic-gate 10250Sstevel@tonic-gate FTYPE(s2) = S_IFREG; 10260Sstevel@tonic-gate targetexists = 0; 10270Sstevel@tonic-gate if ((*statf)(target, &s2) >= 0) { 10280Sstevel@tonic-gate if (ISLNK(s2)) 10290Sstevel@tonic-gate (void) stat(target, &s2); 10300Sstevel@tonic-gate /* 10310Sstevel@tonic-gate * If target is a directory, 10320Sstevel@tonic-gate * make complete name of new file 10330Sstevel@tonic-gate * within that directory. 10340Sstevel@tonic-gate */ 10350Sstevel@tonic-gate if (ISDIR(s2)) { 10360Sstevel@tonic-gate size_t len; 10370Sstevel@tonic-gate 10380Sstevel@tonic-gate len = strlen(target) + strlen(dname(source)) + 4; 10390Sstevel@tonic-gate if ((buf = (char *)malloc(len)) == NULL) { 10400Sstevel@tonic-gate (void) fprintf(stderr, 10410Sstevel@tonic-gate gettext("%s: Insufficient memory to " 10424774Sas145665 "%s %s\n "), cmd, cmd, source); 10430Sstevel@tonic-gate exit(3); 10440Sstevel@tonic-gate } 10450Sstevel@tonic-gate (void) snprintf(buf, len, "%s/%s", 10460Sstevel@tonic-gate target, dname(source)); 10470Sstevel@tonic-gate *to = target = buf; 10480Sstevel@tonic-gate } 10490Sstevel@tonic-gate 10500Sstevel@tonic-gate if ((*statf)(target, &s2) >= 0) { 10510Sstevel@tonic-gate int overwrite = FALSE; 10520Sstevel@tonic-gate int override = FALSE; 10530Sstevel@tonic-gate 10540Sstevel@tonic-gate targetexists++; 10550Sstevel@tonic-gate if (cpy || mve) { 10560Sstevel@tonic-gate /* 10570Sstevel@tonic-gate * For cp and mv, it is an error if the 10580Sstevel@tonic-gate * source and target are the same file. 10590Sstevel@tonic-gate * Check for the same inode and file 10600Sstevel@tonic-gate * system, but don't check for the same 10610Sstevel@tonic-gate * absolute pathname because it is an 10620Sstevel@tonic-gate * error when the source and target are 10630Sstevel@tonic-gate * hard links to the same file. 10640Sstevel@tonic-gate */ 10650Sstevel@tonic-gate if (IDENTICAL(s1, s2)) { 10660Sstevel@tonic-gate (void) fprintf(stderr, 10670Sstevel@tonic-gate gettext( 10680Sstevel@tonic-gate "%s: %s and %s are identical\n"), 10690Sstevel@tonic-gate cmd, source, target); 10700Sstevel@tonic-gate if (buf != NULL) 10710Sstevel@tonic-gate free(buf); 10720Sstevel@tonic-gate return (1); 10730Sstevel@tonic-gate } 10740Sstevel@tonic-gate } 10750Sstevel@tonic-gate if (lnk) { 10760Sstevel@tonic-gate /* 10770Sstevel@tonic-gate * For ln, it is an error if the source and 10780Sstevel@tonic-gate * target are identical files (same inode, 10790Sstevel@tonic-gate * same file system, and filenames resolve 10800Sstevel@tonic-gate * to same absolute pathname). 10810Sstevel@tonic-gate */ 10820Sstevel@tonic-gate if (!chk_different(source, target)) { 10830Sstevel@tonic-gate if (buf != NULL) 10840Sstevel@tonic-gate free(buf); 10850Sstevel@tonic-gate return (1); 10860Sstevel@tonic-gate } 10870Sstevel@tonic-gate } 10880Sstevel@tonic-gate if (lnk && !silent) { 10890Sstevel@tonic-gate (void) fprintf(stderr, 10900Sstevel@tonic-gate gettext("%s: %s: File exists\n"), 10910Sstevel@tonic-gate cmd, target); 10920Sstevel@tonic-gate if (buf != NULL) 10930Sstevel@tonic-gate free(buf); 10940Sstevel@tonic-gate return (1); 10950Sstevel@tonic-gate } 10960Sstevel@tonic-gate 10970Sstevel@tonic-gate /* 10980Sstevel@tonic-gate * overwrite: 10990Sstevel@tonic-gate * If the user does not have access to 11000Sstevel@tonic-gate * the target, ask ----if it is not 11010Sstevel@tonic-gate * silent and user invoked command 11020Sstevel@tonic-gate * interactively. 11030Sstevel@tonic-gate * 11040Sstevel@tonic-gate * override: 11050Sstevel@tonic-gate * If not silent, and stdin is a terminal, and 11060Sstevel@tonic-gate * there's no write access, and the file isn't a 11070Sstevel@tonic-gate * symbolic link, ask for permission. 11080Sstevel@tonic-gate * 11090Sstevel@tonic-gate * XPG4: both overwrite and override: 11100Sstevel@tonic-gate * ask only one question. 11110Sstevel@tonic-gate * 11120Sstevel@tonic-gate * TRANSLATION_NOTE - The following messages will 11130Sstevel@tonic-gate * contain the first character of the strings for 11140Sstevel@tonic-gate * "yes" and "no" defined in the file 11150Sstevel@tonic-gate * "nl_langinfo.po". After substitution, the 11160Sstevel@tonic-gate * message will appear as follows: 11170Sstevel@tonic-gate * <cmd>: overwrite <filename> (y/n)? 11180Sstevel@tonic-gate * where <cmd> is the name of the command 11190Sstevel@tonic-gate * (cp, mv) and <filename> is the destination file 11200Sstevel@tonic-gate */ 11210Sstevel@tonic-gate 11220Sstevel@tonic-gate 11230Sstevel@tonic-gate overwrite = iflg && !silent && use_stdin(); 11240Sstevel@tonic-gate override = !cpy && (access(target, 2) < 0) && 11250Sstevel@tonic-gate !silent && use_stdin() && !ISLNK(s2); 11260Sstevel@tonic-gate 11273819Sas145665 if (overwrite && override) { 11280Sstevel@tonic-gate (void) fprintf(stderr, 11290Sstevel@tonic-gate gettext("%s: overwrite %s and override " 11300Sstevel@tonic-gate "protection %o (%s/%s)? "), cmd, target, 11314774Sas145665 FMODE(s2) & MODEBITS, yesstr, nostr); 11324774Sas145665 if (yes() == 0) { 11333819Sas145665 if (buf != NULL) 11343819Sas145665 free(buf); 11353819Sas145665 return (2); 11363819Sas145665 } 11373819Sas145665 } else if (overwrite && ISREG(s2)) { 11380Sstevel@tonic-gate (void) fprintf(stderr, 11390Sstevel@tonic-gate gettext("%s: overwrite %s (%s/%s)? "), 11404774Sas145665 cmd, target, yesstr, nostr); 11414774Sas145665 if (yes() == 0) { 11423819Sas145665 if (buf != NULL) 11433819Sas145665 free(buf); 11443819Sas145665 return (2); 11453819Sas145665 } 11463819Sas145665 } else if (override) { 11470Sstevel@tonic-gate (void) fprintf(stderr, 11480Sstevel@tonic-gate gettext("%s: %s: override protection " 11490Sstevel@tonic-gate /*CSTYLED*/ 11500Sstevel@tonic-gate "%o (%s/%s)? "), 11510Sstevel@tonic-gate /*CSTYLED*/ 11520Sstevel@tonic-gate cmd, target, FMODE(s2) & MODEBITS, 11534774Sas145665 yesstr, nostr); 11544774Sas145665 if (yes() == 0) { 11553819Sas145665 if (buf != NULL) 11563819Sas145665 free(buf); 11573819Sas145665 return (2); 11580Sstevel@tonic-gate } 11590Sstevel@tonic-gate } 11603819Sas145665 11610Sstevel@tonic-gate if (lnk && unlink(target) < 0) { 11620Sstevel@tonic-gate (void) fprintf(stderr, 11630Sstevel@tonic-gate gettext("%s: cannot unlink %s: "), 11640Sstevel@tonic-gate cmd, target); 11650Sstevel@tonic-gate perror(""); 11660Sstevel@tonic-gate return (1); 11670Sstevel@tonic-gate } 11680Sstevel@tonic-gate } 11690Sstevel@tonic-gate } 11700Sstevel@tonic-gate return (0); 11710Sstevel@tonic-gate } 11720Sstevel@tonic-gate 11730Sstevel@tonic-gate /* 11740Sstevel@tonic-gate * check whether source and target are different 11750Sstevel@tonic-gate * return 1 when they are different 11760Sstevel@tonic-gate * return 0 when they are identical, or when unable to resolve a pathname 11770Sstevel@tonic-gate */ 11780Sstevel@tonic-gate static int 11790Sstevel@tonic-gate chk_different(char *source, char *target) 11800Sstevel@tonic-gate { 11810Sstevel@tonic-gate char rtarget[PATH_MAX], rsource[PATH_MAX]; 11820Sstevel@tonic-gate 11830Sstevel@tonic-gate if (IDENTICAL(s1, s2)) { 11840Sstevel@tonic-gate /* 11850Sstevel@tonic-gate * IDENTICAL will be true for hard links, therefore 11860Sstevel@tonic-gate * check whether the filenames are different 11870Sstevel@tonic-gate */ 11880Sstevel@tonic-gate if ((getrealpath(source, rsource) == 0) || 11890Sstevel@tonic-gate (getrealpath(target, rtarget) == 0)) { 11900Sstevel@tonic-gate return (0); 11910Sstevel@tonic-gate } 11920Sstevel@tonic-gate if (strncmp(rsource, rtarget, PATH_MAX) == 0) { 11930Sstevel@tonic-gate (void) fprintf(stderr, gettext( 11940Sstevel@tonic-gate "%s: %s and %s are identical\n"), 11950Sstevel@tonic-gate cmd, source, target); 11960Sstevel@tonic-gate return (0); 11970Sstevel@tonic-gate } 11980Sstevel@tonic-gate } 11990Sstevel@tonic-gate return (1); 12000Sstevel@tonic-gate } 12010Sstevel@tonic-gate 12020Sstevel@tonic-gate /* 12030Sstevel@tonic-gate * get real path (resolved absolute pathname) 12040Sstevel@tonic-gate * return 1 on success, 0 on failure 12050Sstevel@tonic-gate */ 12060Sstevel@tonic-gate static int 12070Sstevel@tonic-gate getrealpath(char *path, char *rpath) 12080Sstevel@tonic-gate { 12090Sstevel@tonic-gate if (realpath(path, rpath) == NULL) { 12100Sstevel@tonic-gate int errno_save = errno; 12110Sstevel@tonic-gate (void) fprintf(stderr, gettext( 12120Sstevel@tonic-gate "%s: can't resolve path %s: "), cmd, path); 12130Sstevel@tonic-gate errno = errno_save; 12140Sstevel@tonic-gate perror(""); 12150Sstevel@tonic-gate return (0); 12160Sstevel@tonic-gate } 12170Sstevel@tonic-gate return (1); 12180Sstevel@tonic-gate } 12190Sstevel@tonic-gate 12200Sstevel@tonic-gate static int 12210Sstevel@tonic-gate rcopy(char *from, char *to) 12220Sstevel@tonic-gate { 12230Sstevel@tonic-gate DIR *fold = opendir(from); 12240Sstevel@tonic-gate struct dirent *dp; 12250Sstevel@tonic-gate struct stat statb, s1save; 12260Sstevel@tonic-gate int errs = 0; 12270Sstevel@tonic-gate char fromname[PATH_MAX]; 12280Sstevel@tonic-gate 12290Sstevel@tonic-gate if (fold == 0 || ((pflg || mve) && fstat(fold->dd_fd, &statb) < 0)) { 12300Sstevel@tonic-gate Perror(from); 12310Sstevel@tonic-gate return (1); 12320Sstevel@tonic-gate } 12330Sstevel@tonic-gate if (pflg || mve) { 12340Sstevel@tonic-gate /* 12350Sstevel@tonic-gate * Save s1 (stat information for source dir) so that 12360Sstevel@tonic-gate * mod and access times can be reserved during "cp -p" 12370Sstevel@tonic-gate * or mv, since s1 gets overwritten. 12380Sstevel@tonic-gate */ 12390Sstevel@tonic-gate s1save = s1; 12400Sstevel@tonic-gate } 12410Sstevel@tonic-gate for (;;) { 12420Sstevel@tonic-gate dp = readdir(fold); 12430Sstevel@tonic-gate if (dp == 0) { 12440Sstevel@tonic-gate (void) closedir(fold); 12450Sstevel@tonic-gate if (pflg || mve) 12460Sstevel@tonic-gate return (chg_time(to, s1save) + errs); 12470Sstevel@tonic-gate return (errs); 12480Sstevel@tonic-gate } 12490Sstevel@tonic-gate if (dp->d_ino == 0) 12500Sstevel@tonic-gate continue; 12510Sstevel@tonic-gate if ((strcmp(dp->d_name, ".") == 0) || 12520Sstevel@tonic-gate (strcmp(dp->d_name, "..") == 0)) 12530Sstevel@tonic-gate continue; 12540Sstevel@tonic-gate if (strlen(from)+1+strlen(dp->d_name) >= 12550Sstevel@tonic-gate sizeof (fromname) - 1) { 12560Sstevel@tonic-gate (void) fprintf(stderr, 12570Sstevel@tonic-gate gettext("%s : %s/%s: Name too long\n"), 12580Sstevel@tonic-gate cmd, from, dp->d_name); 12590Sstevel@tonic-gate errs++; 12600Sstevel@tonic-gate continue; 12610Sstevel@tonic-gate } 12620Sstevel@tonic-gate (void) snprintf(fromname, sizeof (fromname), 12630Sstevel@tonic-gate "%s/%s", from, dp->d_name); 12640Sstevel@tonic-gate errs += cpymve(fromname, to); 12650Sstevel@tonic-gate } 12660Sstevel@tonic-gate } 12670Sstevel@tonic-gate 12680Sstevel@tonic-gate static char * 12690Sstevel@tonic-gate dname(char *name) 12700Sstevel@tonic-gate { 12710Sstevel@tonic-gate register char *p; 12720Sstevel@tonic-gate 12730Sstevel@tonic-gate /* 12740Sstevel@tonic-gate * Return just the file name given the complete path. 12750Sstevel@tonic-gate * Like basename(1). 12760Sstevel@tonic-gate */ 12770Sstevel@tonic-gate 12780Sstevel@tonic-gate p = name; 12790Sstevel@tonic-gate 12800Sstevel@tonic-gate /* 12810Sstevel@tonic-gate * While there are characters left, 12820Sstevel@tonic-gate * set name to start after last 12830Sstevel@tonic-gate * delimiter. 12840Sstevel@tonic-gate */ 12850Sstevel@tonic-gate 12860Sstevel@tonic-gate while (*p) 12870Sstevel@tonic-gate if (*p++ == DELIM && *p) 12880Sstevel@tonic-gate name = p; 12890Sstevel@tonic-gate return (name); 12900Sstevel@tonic-gate } 12910Sstevel@tonic-gate 12920Sstevel@tonic-gate static void 12930Sstevel@tonic-gate usage(void) 12940Sstevel@tonic-gate { 12950Sstevel@tonic-gate /* 12960Sstevel@tonic-gate * Display usage message. 12970Sstevel@tonic-gate */ 12980Sstevel@tonic-gate 12990Sstevel@tonic-gate if (mve) { 13000Sstevel@tonic-gate (void) fprintf(stderr, gettext( 13010Sstevel@tonic-gate "Usage: mv [-f] [-i] f1 f2\n" 13020Sstevel@tonic-gate " mv [-f] [-i] f1 ... fn d1\n" 13030Sstevel@tonic-gate " mv [-f] [-i] d1 d2\n")); 13040Sstevel@tonic-gate } else if (lnk) { 13054774Sas145665 #ifdef XPG4 13060Sstevel@tonic-gate (void) fprintf(stderr, gettext( 13070Sstevel@tonic-gate "Usage: ln [-f] [-s] f1 [f2]\n" 13080Sstevel@tonic-gate " ln [-f] [-s] f1 ... fn d1\n" 13090Sstevel@tonic-gate " ln [-f] -s d1 d2\n")); 13100Sstevel@tonic-gate #else 13114774Sas145665 (void) fprintf(stderr, gettext( 13120Sstevel@tonic-gate "Usage: ln [-f] [-n] [-s] f1 [f2]\n" 13130Sstevel@tonic-gate " ln [-f] [-n] [-s] f1 ... fn d1\n" 13140Sstevel@tonic-gate " ln [-f] [-n] -s d1 d2\n")); 13150Sstevel@tonic-gate #endif 13160Sstevel@tonic-gate } else if (cpy) { 13170Sstevel@tonic-gate (void) fprintf(stderr, gettext( 1318*5331Samw "Usage: cp [-f] [-i] [-p] [-@] [-/] f1 f2\n" 1319*5331Samw " cp [-f] [-i] [-p] [-@] [-/] f1 ... fn d1\n" 1320*5331Samw " cp -r|-R [-H|-L|-P] [-f] [-i] [-p] [-@] [-/] " 13210Sstevel@tonic-gate "d1 ... dn-1 dn\n")); 13220Sstevel@tonic-gate } 13230Sstevel@tonic-gate exit(2); 13240Sstevel@tonic-gate } 13250Sstevel@tonic-gate 13260Sstevel@tonic-gate /* 13270Sstevel@tonic-gate * chg_time() 13280Sstevel@tonic-gate * 13290Sstevel@tonic-gate * Try to preserve modification and access time. 13300Sstevel@tonic-gate * If 1) pflg is not set, or 2) pflg is set and this is the Solaris version, 13310Sstevel@tonic-gate * don't report a utime() failure. 13320Sstevel@tonic-gate * If this is the XPG4 version and utime fails, if 1) pflg is set (cp -p) 13330Sstevel@tonic-gate * or 2) we are doing a mv, print a diagnostic message; arrange for a non-zero 13340Sstevel@tonic-gate * exit status only if pflg is set. 13350Sstevel@tonic-gate * utimes(2) is being used to achieve granularity in 13360Sstevel@tonic-gate * microseconds while setting file times. 13370Sstevel@tonic-gate */ 13380Sstevel@tonic-gate static int 13390Sstevel@tonic-gate chg_time(char *to, struct stat ss) 13400Sstevel@tonic-gate { 13410Sstevel@tonic-gate struct timeval times[2]; 13420Sstevel@tonic-gate int rc; 13430Sstevel@tonic-gate 13440Sstevel@tonic-gate timestruc_to_timeval(&ss.st_atim, times); 13450Sstevel@tonic-gate timestruc_to_timeval(&ss.st_mtim, times + 1); 13460Sstevel@tonic-gate 13470Sstevel@tonic-gate rc = utimes(to, times); 13480Sstevel@tonic-gate #ifdef XPG4 13490Sstevel@tonic-gate if ((pflg || mve) && rc != 0) { 13500Sstevel@tonic-gate (void) fprintf(stderr, 13510Sstevel@tonic-gate gettext("%s: cannot set times for %s: "), cmd, to); 13520Sstevel@tonic-gate perror(""); 13530Sstevel@tonic-gate if (pflg) 13540Sstevel@tonic-gate return (1); 13550Sstevel@tonic-gate } 13560Sstevel@tonic-gate #endif 13570Sstevel@tonic-gate 13580Sstevel@tonic-gate return (0); 13590Sstevel@tonic-gate 13600Sstevel@tonic-gate } 13610Sstevel@tonic-gate 13620Sstevel@tonic-gate /* 13630Sstevel@tonic-gate * chg_mode() 13640Sstevel@tonic-gate * 13650Sstevel@tonic-gate * This function is called upon "cp -p" or mv across filesystems. 13660Sstevel@tonic-gate * 13670Sstevel@tonic-gate * Try to preserve the owner and group id. If chown() fails, 13680Sstevel@tonic-gate * only print a diagnostic message if doing a mv in the XPG4 version; 13690Sstevel@tonic-gate * try to clear S_ISUID and S_ISGID bits in the target. If unable to clear 13700Sstevel@tonic-gate * S_ISUID and S_ISGID bits, print a diagnostic message and arrange for a 13710Sstevel@tonic-gate * non-zero exit status because this is a security violation. 13720Sstevel@tonic-gate * Try to preserve permissions. 13730Sstevel@tonic-gate * If this is the XPG4 version and chmod() fails, print a diagnostic message 13740Sstevel@tonic-gate * and arrange for a non-zero exit status. 13750Sstevel@tonic-gate * If this is the Solaris version and chmod() fails, do not print a 13760Sstevel@tonic-gate * diagnostic message or exit with a non-zero value. 13770Sstevel@tonic-gate */ 13780Sstevel@tonic-gate static int 13790Sstevel@tonic-gate chg_mode(char *target, uid_t uid, gid_t gid, mode_t mode) 13800Sstevel@tonic-gate { 13810Sstevel@tonic-gate int clearflg = 0; /* controls message printed upon chown() error */ 13820Sstevel@tonic-gate 13830Sstevel@tonic-gate if (chown(target, uid, gid) != 0) { 13840Sstevel@tonic-gate #ifdef XPG4 13850Sstevel@tonic-gate if (mve) { 13860Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s: cannot change" 13870Sstevel@tonic-gate " owner and group of %s: "), cmd, target); 13880Sstevel@tonic-gate perror(""); 13890Sstevel@tonic-gate } 13900Sstevel@tonic-gate #endif 13910Sstevel@tonic-gate if (mode & (S_ISUID | S_ISGID)) { 13920Sstevel@tonic-gate /* try to clear S_ISUID and S_ISGID */ 13930Sstevel@tonic-gate mode &= ~S_ISUID & ~S_ISGID; 13940Sstevel@tonic-gate ++clearflg; 13950Sstevel@tonic-gate } 13960Sstevel@tonic-gate } 13970Sstevel@tonic-gate if (chmod(target, mode) != 0) { 13980Sstevel@tonic-gate if (clearflg) { 13990Sstevel@tonic-gate (void) fprintf(stderr, gettext( 14000Sstevel@tonic-gate "%s: cannot clear S_ISUID and S_ISGID bits in" 14010Sstevel@tonic-gate " %s: "), cmd, target); 14020Sstevel@tonic-gate perror(""); 14030Sstevel@tonic-gate /* cp -p should get non-zero exit; mv should not */ 14040Sstevel@tonic-gate if (pflg) 14050Sstevel@tonic-gate return (1); 14060Sstevel@tonic-gate } 14070Sstevel@tonic-gate #ifdef XPG4 14080Sstevel@tonic-gate else { 14090Sstevel@tonic-gate (void) fprintf(stderr, gettext( 14100Sstevel@tonic-gate "%s: cannot set permissions for %s: "), cmd, target); 14110Sstevel@tonic-gate perror(""); 14120Sstevel@tonic-gate /* cp -p should get non-zero exit; mv should not */ 14130Sstevel@tonic-gate if (pflg) 14140Sstevel@tonic-gate return (1); 14150Sstevel@tonic-gate } 14160Sstevel@tonic-gate #endif 14170Sstevel@tonic-gate } 14180Sstevel@tonic-gate return (0); 14190Sstevel@tonic-gate 14200Sstevel@tonic-gate } 14210Sstevel@tonic-gate 14220Sstevel@tonic-gate static void 14230Sstevel@tonic-gate Perror(char *s) 14240Sstevel@tonic-gate { 14250Sstevel@tonic-gate char buf[PATH_MAX + 10]; 14260Sstevel@tonic-gate 14270Sstevel@tonic-gate (void) snprintf(buf, sizeof (buf), "%s: %s", cmd, s); 14280Sstevel@tonic-gate perror(buf); 14290Sstevel@tonic-gate } 14300Sstevel@tonic-gate 14310Sstevel@tonic-gate static void 14320Sstevel@tonic-gate Perror2(char *s1, char *s2) 14330Sstevel@tonic-gate { 14340Sstevel@tonic-gate char buf[PATH_MAX + 20]; 14350Sstevel@tonic-gate 14360Sstevel@tonic-gate (void) snprintf(buf, sizeof (buf), "%s: %s: %s", 14370Sstevel@tonic-gate cmd, gettext(s1), gettext(s2)); 14380Sstevel@tonic-gate perror(buf); 14390Sstevel@tonic-gate } 14400Sstevel@tonic-gate 14410Sstevel@tonic-gate /* 14420Sstevel@tonic-gate * used for cp -R and for mv across file systems 14430Sstevel@tonic-gate */ 14440Sstevel@tonic-gate static int 14450Sstevel@tonic-gate copydir(char *source, char *target) 14460Sstevel@tonic-gate { 14470Sstevel@tonic-gate int ret, attret = 0; 14480Sstevel@tonic-gate int pret = 0; /* need separate flag if -p is specified */ 14490Sstevel@tonic-gate mode_t fixmode = (mode_t)0; /* cleanup mode after copy */ 14500Sstevel@tonic-gate struct stat s1save; 1451789Sahrens acl_t *s1acl_save; 1452789Sahrens 1453789Sahrens s1acl_save = NULL; 14540Sstevel@tonic-gate 14550Sstevel@tonic-gate if (cpy && !rflg) { 14560Sstevel@tonic-gate (void) fprintf(stderr, 14570Sstevel@tonic-gate gettext("%s: %s: is a directory\n"), cmd, source); 14580Sstevel@tonic-gate return (1); 14590Sstevel@tonic-gate } 14600Sstevel@tonic-gate 14610Sstevel@tonic-gate if (stat(target, &s2) < 0) { 14620Sstevel@tonic-gate if (mkdir(target, (s1.st_mode & MODEBITS)) < 0) { 14630Sstevel@tonic-gate (void) fprintf(stderr, "%s: ", cmd); 14640Sstevel@tonic-gate perror(target); 14650Sstevel@tonic-gate return (1); 14660Sstevel@tonic-gate } 14670Sstevel@tonic-gate if (stat(target, &s2) == 0) { 14680Sstevel@tonic-gate fixmode = s2.st_mode; 14690Sstevel@tonic-gate } else { 14700Sstevel@tonic-gate fixmode = s1.st_mode; 14710Sstevel@tonic-gate } 14720Sstevel@tonic-gate (void) chmod(target, ((fixmode & MODEBITS) | S_IRWXU)); 14730Sstevel@tonic-gate } else if (!(ISDIR(s2))) { 14740Sstevel@tonic-gate (void) fprintf(stderr, 14750Sstevel@tonic-gate gettext("%s: %s: not a directory.\n"), cmd, target); 14760Sstevel@tonic-gate return (1); 14770Sstevel@tonic-gate } 14780Sstevel@tonic-gate if (pflg || mve) { 14790Sstevel@tonic-gate /* 14800Sstevel@tonic-gate * Save s1 (stat information for source dir) and acl info, 14810Sstevel@tonic-gate * if any, so that ownership, modes, times, and acl's can 14820Sstevel@tonic-gate * be reserved during "cp -p" or mv. 14830Sstevel@tonic-gate * s1 gets overwritten when doing the recursive copy. 14840Sstevel@tonic-gate */ 14850Sstevel@tonic-gate s1save = s1; 1486789Sahrens if (s1acl != NULL) { 1487789Sahrens s1acl_save = acl_dup(s1acl); 1488789Sahrens if (s1acl_save == NULL) { 1489789Sahrens (void) fprintf(stderr, gettext("%s: " 1490789Sahrens "Insufficient memory to save acl" 1491789Sahrens " entry\n"), cmd); 1492789Sahrens if (pflg) 1493789Sahrens return (1); 1494789Sahrens 14950Sstevel@tonic-gate } 14964774Sas145665 #ifdef XPG4 14974774Sas145665 else { 14984774Sas145665 (void) fprintf(stderr, gettext("%s: " 14994774Sas145665 "Insufficient memory to save acl" 15004774Sas145665 " entry\n"), cmd); 15014774Sas145665 if (pflg) 15024774Sas145665 return (1); 15034774Sas145665 } 15040Sstevel@tonic-gate #endif 15050Sstevel@tonic-gate } 15060Sstevel@tonic-gate } 15070Sstevel@tonic-gate 15080Sstevel@tonic-gate ret = rcopy(source, target); 15090Sstevel@tonic-gate 15100Sstevel@tonic-gate /* 15110Sstevel@tonic-gate * Once we created a directory, go ahead and set 15120Sstevel@tonic-gate * its attributes, e.g. acls and time. The info 15130Sstevel@tonic-gate * may get overwritten if we continue traversing 15140Sstevel@tonic-gate * down the tree. 15150Sstevel@tonic-gate * 15160Sstevel@tonic-gate * ACL for directory 15170Sstevel@tonic-gate */ 15180Sstevel@tonic-gate if (pflg || mve) { 15191231Smarks if ((pret = chg_mode(target, UID(s1save), GID(s1save), 15201231Smarks FMODE(s1save))) == 0) 15211231Smarks pret = chg_time(target, s1save); 15221231Smarks ret += pret; 1523789Sahrens if (s1acl_save != NULL) { 1524789Sahrens if (acl_set(target, s1acl_save) < 0) { 15250Sstevel@tonic-gate #ifdef XPG4 15260Sstevel@tonic-gate if (pflg || mve) { 15270Sstevel@tonic-gate #else 15280Sstevel@tonic-gate if (pflg) { 15290Sstevel@tonic-gate #endif 15300Sstevel@tonic-gate (void) fprintf(stderr, gettext( 15310Sstevel@tonic-gate "%s: failed to set acl entries " 15320Sstevel@tonic-gate "on %s\n"), cmd, target); 15330Sstevel@tonic-gate if (pflg) { 1534789Sahrens acl_free(s1acl_save); 1535789Sahrens s1acl_save = NULL; 15360Sstevel@tonic-gate ret++; 15370Sstevel@tonic-gate } 15380Sstevel@tonic-gate } 15390Sstevel@tonic-gate /* else: silent and continue */ 15400Sstevel@tonic-gate } 1541789Sahrens acl_free(s1acl_save); 1542789Sahrens s1acl_save = NULL; 15430Sstevel@tonic-gate } 15440Sstevel@tonic-gate } else if (fixmode != (mode_t)0) 15450Sstevel@tonic-gate (void) chmod(target, fixmode & MODEBITS); 15460Sstevel@tonic-gate 1547*5331Samw if (pflg || atflg || mve || saflg) { 15480Sstevel@tonic-gate attret = copyattributes(source, target); 15490Sstevel@tonic-gate if (!attrsilent && attret != 0) { 15500Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s: Failed to preserve" 15510Sstevel@tonic-gate " extended attributes of directory" 15520Sstevel@tonic-gate " %s\n"), cmd, source); 15530Sstevel@tonic-gate } else { 15540Sstevel@tonic-gate /* 15550Sstevel@tonic-gate * Otherwise ignore failure. 15560Sstevel@tonic-gate */ 15570Sstevel@tonic-gate attret = 0; 15580Sstevel@tonic-gate } 1559*5331Samw /* Copy extended system attributes */ 1560*5331Samw if (pflg || mve || saflg) { 1561*5331Samw attret = copy_sysattr(source, target); 1562*5331Samw if (attret != 0 && !attrsilent) { 1563*5331Samw (void) fprintf(stderr, gettext( 1564*5331Samw "%s: Failed to preserve " 1565*5331Samw "extended system attributes " 1566*5331Samw "of directory %s\n"), cmd, source); 1567*5331Samw } else { 1568*5331Samw attret = 0; 1569*5331Samw } 1570*5331Samw } 15710Sstevel@tonic-gate } 15720Sstevel@tonic-gate if (attret != 0) 15730Sstevel@tonic-gate return (attret); 15740Sstevel@tonic-gate return (ret); 15750Sstevel@tonic-gate } 15760Sstevel@tonic-gate 15770Sstevel@tonic-gate static int 15780Sstevel@tonic-gate copyspecial(char *target) 15790Sstevel@tonic-gate { 15800Sstevel@tonic-gate int ret = 0; 15810Sstevel@tonic-gate 15820Sstevel@tonic-gate if (mknod(target, s1.st_mode, s1.st_rdev) != 0) { 15830Sstevel@tonic-gate (void) fprintf(stderr, gettext( 15840Sstevel@tonic-gate "cp: cannot create special file %s: "), target); 15850Sstevel@tonic-gate perror(""); 15860Sstevel@tonic-gate return (1); 15870Sstevel@tonic-gate } 15880Sstevel@tonic-gate 15890Sstevel@tonic-gate if (pflg) { 15900Sstevel@tonic-gate if ((ret = chg_mode(target, UID(s1), GID(s1), FMODE(s1))) == 0) 15910Sstevel@tonic-gate ret = chg_time(target, s1); 15920Sstevel@tonic-gate } 15930Sstevel@tonic-gate 15940Sstevel@tonic-gate return (ret); 15950Sstevel@tonic-gate } 15960Sstevel@tonic-gate 15970Sstevel@tonic-gate static int 15980Sstevel@tonic-gate use_stdin(void) 15990Sstevel@tonic-gate { 16000Sstevel@tonic-gate #ifdef XPG4 16010Sstevel@tonic-gate return (1); 16020Sstevel@tonic-gate #else 16030Sstevel@tonic-gate return (isatty(fileno(stdin))); 16040Sstevel@tonic-gate #endif 16050Sstevel@tonic-gate } 16060Sstevel@tonic-gate 1607*5331Samw /* Copy non-system extended attributes */ 1608*5331Samw 16090Sstevel@tonic-gate static int 16100Sstevel@tonic-gate copyattributes(char *source, char *target) 16110Sstevel@tonic-gate { 16120Sstevel@tonic-gate struct dirent *dp; 16130Sstevel@tonic-gate char *srcbuf, *targbuf; 16140Sstevel@tonic-gate int error = 0; 1615789Sahrens int aclerror; 16160Sstevel@tonic-gate mode_t mode; 16170Sstevel@tonic-gate int clearflg = 0; 1618789Sahrens acl_t *xacl = NULL; 1619789Sahrens acl_t *attrdiracl = NULL; 16200Sstevel@tonic-gate struct timeval times[2]; 16210Sstevel@tonic-gate 16220Sstevel@tonic-gate srcbuf = targbuf = NULL; 16230Sstevel@tonic-gate 1624*5331Samw 1625*5331Samw if (pathconf(source, _PC_XATTR_EXISTS) != 1) 16260Sstevel@tonic-gate return (0); 16270Sstevel@tonic-gate 16280Sstevel@tonic-gate if (pathconf(target, _PC_XATTR_ENABLED) != 1) { 16290Sstevel@tonic-gate if (!attrsilent) { 16300Sstevel@tonic-gate (void) fprintf(stderr, 16310Sstevel@tonic-gate gettext( 16320Sstevel@tonic-gate "%s: cannot preserve extended attributes, " 16330Sstevel@tonic-gate "operation not supported on file" 16340Sstevel@tonic-gate " %s\n"), cmd, target); 16350Sstevel@tonic-gate } 16360Sstevel@tonic-gate return (1); 16370Sstevel@tonic-gate } 1638*5331Samw if (open_source(source) != 0) 1639*5331Samw return (1); 1640*5331Samw if (open_target_srctarg_attrdirs(source, target) != 0) 1641*5331Samw return (1); 1642*5331Samw if (open_attrdirp(source) != 0) 1643*5331Samw return (1); 16440Sstevel@tonic-gate 16450Sstevel@tonic-gate if (pflg || mve) { 16460Sstevel@tonic-gate if (fchmod(targetdirfd, attrdir.st_mode) == -1) { 16470Sstevel@tonic-gate if (!attrsilent) { 16480Sstevel@tonic-gate (void) fprintf(stderr, 16494774Sas145665 gettext("%s: failed to set file mode" 16504774Sas145665 " correctly on attribute directory of" 16514774Sas145665 " file %s: "), cmd, target); 16520Sstevel@tonic-gate perror(""); 16530Sstevel@tonic-gate ++error; 16540Sstevel@tonic-gate } 16550Sstevel@tonic-gate } 16560Sstevel@tonic-gate 16570Sstevel@tonic-gate if (fchown(targetdirfd, attrdir.st_uid, attrdir.st_gid) == -1) { 16580Sstevel@tonic-gate if (!attrsilent) { 16590Sstevel@tonic-gate (void) fprintf(stderr, 16600Sstevel@tonic-gate gettext("%s: failed to set file" 16610Sstevel@tonic-gate " ownership correctly on attribute" 16620Sstevel@tonic-gate " directory of file %s: "), cmd, target); 16630Sstevel@tonic-gate perror(""); 16640Sstevel@tonic-gate ++error; 16650Sstevel@tonic-gate } 16660Sstevel@tonic-gate } 16670Sstevel@tonic-gate /* 16680Sstevel@tonic-gate * Now that we are the owner we can update st_ctime by calling 16690Sstevel@tonic-gate * futimesat. 16700Sstevel@tonic-gate */ 16710Sstevel@tonic-gate times[0].tv_sec = attrdir.st_atime; 16720Sstevel@tonic-gate times[0].tv_usec = 0; 16730Sstevel@tonic-gate times[1].tv_sec = attrdir.st_mtime; 16740Sstevel@tonic-gate times[1].tv_usec = 0; 16750Sstevel@tonic-gate if (futimesat(targetdirfd, ".", times) < 0) { 16760Sstevel@tonic-gate if (!attrsilent) { 16770Sstevel@tonic-gate (void) fprintf(stderr, 16784774Sas145665 gettext("%s: cannot set attribute times" 16794774Sas145665 " for %s: "), cmd, target); 16800Sstevel@tonic-gate perror(""); 16810Sstevel@tonic-gate ++error; 16820Sstevel@tonic-gate } 16830Sstevel@tonic-gate } 16840Sstevel@tonic-gate 16850Sstevel@tonic-gate /* 16860Sstevel@tonic-gate * Now set owner and group of attribute directory, implies 16870Sstevel@tonic-gate * changing the ACL of the hidden attribute directory first. 16880Sstevel@tonic-gate */ 1689789Sahrens if ((aclerror = facl_get(sourcedirfd, 1690789Sahrens ACL_NO_TRIVIAL, &attrdiracl)) != 0) { 16910Sstevel@tonic-gate if (!attrsilent) { 16920Sstevel@tonic-gate (void) fprintf(stderr, gettext( 16930Sstevel@tonic-gate "%s: failed to get acl entries of" 16940Sstevel@tonic-gate " attribute directory for" 1695789Sahrens " %s : %s\n"), cmd, 1696789Sahrens source, acl_strerror(aclerror)); 16970Sstevel@tonic-gate ++error; 16980Sstevel@tonic-gate } 16990Sstevel@tonic-gate } 1700789Sahrens 1701789Sahrens if (attrdiracl) { 1702789Sahrens if (facl_set(targetdirfd, attrdiracl) != 0) { 17030Sstevel@tonic-gate if (!attrsilent) { 17040Sstevel@tonic-gate (void) fprintf(stderr, gettext( 1705789Sahrens "%s: failed to set acl entries" 1706789Sahrens " on attribute directory " 1707789Sahrens "for %s\n"), cmd, target); 17080Sstevel@tonic-gate ++error; 17090Sstevel@tonic-gate } 1710789Sahrens acl_free(attrdiracl); 1711789Sahrens attrdiracl = NULL; 17120Sstevel@tonic-gate } 17130Sstevel@tonic-gate } 17140Sstevel@tonic-gate } 17150Sstevel@tonic-gate 1716*5331Samw while ((dp = readdir(srcdirp)) != NULL) { 1717*5331Samw int ret; 17180Sstevel@tonic-gate 1719*5331Samw if ((ret = traverse_attrfile(dp, source, target, 1)) == -1) 1720*5331Samw continue; 1721*5331Samw else if (ret > 0) { 17220Sstevel@tonic-gate ++error; 1723*5331Samw goto out; 17240Sstevel@tonic-gate } 17250Sstevel@tonic-gate 17260Sstevel@tonic-gate if (pflg || mve) { 1727789Sahrens if ((aclerror = facl_get(srcattrfd, 1728789Sahrens ACL_NO_TRIVIAL, &xacl)) != 0) { 17290Sstevel@tonic-gate if (!attrsilent) { 17300Sstevel@tonic-gate (void) fprintf(stderr, gettext( 17310Sstevel@tonic-gate "%s: failed to get acl entries of" 17320Sstevel@tonic-gate " attribute %s for" 1733789Sahrens " %s: %s"), cmd, dp->d_name, 1734789Sahrens source, acl_strerror(aclerror)); 17350Sstevel@tonic-gate ++error; 17360Sstevel@tonic-gate } 17370Sstevel@tonic-gate } 17380Sstevel@tonic-gate } 17390Sstevel@tonic-gate 17400Sstevel@tonic-gate /* 17410Sstevel@tonic-gate * preserve ACL 17420Sstevel@tonic-gate */ 1743789Sahrens if ((pflg || mve) && xacl != NULL) { 1744789Sahrens if ((facl_set(targattrfd, xacl)) < 0) { 17450Sstevel@tonic-gate if (!attrsilent) { 17460Sstevel@tonic-gate (void) fprintf(stderr, gettext( 17470Sstevel@tonic-gate "%s: failed to set acl entries on" 17480Sstevel@tonic-gate " attribute %s for" 17490Sstevel@tonic-gate "%s\n"), cmd, dp->d_name, target); 17500Sstevel@tonic-gate ++error; 17510Sstevel@tonic-gate } 1752789Sahrens acl_free(xacl); 1753789Sahrens xacl = NULL; 17540Sstevel@tonic-gate } 17550Sstevel@tonic-gate } 17560Sstevel@tonic-gate 1757*5331Samw if (writefile(srcattrfd, targattrfd, source, target, 1758*5331Samw dp->d_name, dp->d_name, &s3, &s4) != 0) { 17590Sstevel@tonic-gate if (!attrsilent) { 17600Sstevel@tonic-gate ++error; 17610Sstevel@tonic-gate } 17620Sstevel@tonic-gate goto next; 17630Sstevel@tonic-gate } 17640Sstevel@tonic-gate 17650Sstevel@tonic-gate if (pflg || mve) { 17660Sstevel@tonic-gate mode = FMODE(s3); 17670Sstevel@tonic-gate 17680Sstevel@tonic-gate if (fchown(targattrfd, UID(s3), GID(s3)) != 0) { 17690Sstevel@tonic-gate if (!attrsilent) { 17700Sstevel@tonic-gate (void) fprintf(stderr, 17710Sstevel@tonic-gate gettext("%s: cannot change" 17720Sstevel@tonic-gate " owner and group of" 17730Sstevel@tonic-gate " attribute %s for" " file" 17740Sstevel@tonic-gate " %s: "), cmd, dp->d_name, target); 17750Sstevel@tonic-gate perror(""); 17760Sstevel@tonic-gate ++error; 17770Sstevel@tonic-gate } 17780Sstevel@tonic-gate if (mode & (S_ISUID | S_ISGID)) { 17790Sstevel@tonic-gate /* try to clear S_ISUID and S_ISGID */ 17800Sstevel@tonic-gate mode &= ~S_ISUID & ~S_ISGID; 17810Sstevel@tonic-gate ++clearflg; 17820Sstevel@tonic-gate } 17830Sstevel@tonic-gate } 17840Sstevel@tonic-gate /* tv_usec were cleared above */ 17850Sstevel@tonic-gate times[0].tv_sec = s3.st_atime; 17860Sstevel@tonic-gate times[1].tv_sec = s3.st_mtime; 17870Sstevel@tonic-gate if (futimesat(targetdirfd, dp->d_name, times) < 0) { 17880Sstevel@tonic-gate if (!attrsilent) { 17890Sstevel@tonic-gate (void) fprintf(stderr, 17900Sstevel@tonic-gate gettext("%s: cannot set attribute" 17910Sstevel@tonic-gate " times for %s: "), cmd, target); 17920Sstevel@tonic-gate perror(""); 17930Sstevel@tonic-gate ++error; 17940Sstevel@tonic-gate } 17950Sstevel@tonic-gate } 17960Sstevel@tonic-gate if (fchmod(targattrfd, mode) != 0) { 17970Sstevel@tonic-gate if (clearflg) { 17980Sstevel@tonic-gate (void) fprintf(stderr, gettext( 17990Sstevel@tonic-gate "%s: cannot clear S_ISUID and " 18000Sstevel@tonic-gate "S_ISGID bits in attribute %s" 18010Sstevel@tonic-gate " for file" 18020Sstevel@tonic-gate " %s: "), cmd, dp->d_name, target); 18030Sstevel@tonic-gate } else { 18040Sstevel@tonic-gate if (!attrsilent) { 18050Sstevel@tonic-gate (void) fprintf(stderr, 18064774Sas145665 gettext( 18070Sstevel@tonic-gate "%s: cannot set permissions of attribute" 18080Sstevel@tonic-gate " %s for %s: "), cmd, dp->d_name, target); 18090Sstevel@tonic-gate perror(""); 18100Sstevel@tonic-gate ++error; 18110Sstevel@tonic-gate } 18120Sstevel@tonic-gate } 18130Sstevel@tonic-gate } 18141231Smarks if (xacl && ((facl_set(targattrfd, xacl)) < 0)) { 18151231Smarks if (!attrsilent) { 18161231Smarks (void) fprintf(stderr, gettext( 18171231Smarks "%s: failed to set acl entries on" 18181231Smarks " attribute %s for" 18191231Smarks "%s\n"), cmd, dp->d_name, target); 18201231Smarks ++error; 18211231Smarks } 18221231Smarks acl_free(xacl); 18231231Smarks xacl = NULL; 18241231Smarks } 18250Sstevel@tonic-gate } 18260Sstevel@tonic-gate next: 1827789Sahrens if (xacl != NULL) { 1828789Sahrens acl_free(xacl); 1829789Sahrens xacl = NULL; 18300Sstevel@tonic-gate } 18310Sstevel@tonic-gate if (srcbuf != NULL) 18320Sstevel@tonic-gate free(srcbuf); 18330Sstevel@tonic-gate if (targbuf != NULL) 18340Sstevel@tonic-gate free(targbuf); 18350Sstevel@tonic-gate if (srcattrfd != -1) 18360Sstevel@tonic-gate (void) close(srcattrfd); 18370Sstevel@tonic-gate if (targattrfd != -1) 18380Sstevel@tonic-gate (void) close(targattrfd); 18390Sstevel@tonic-gate srcattrfd = targattrfd = -1; 18400Sstevel@tonic-gate srcbuf = targbuf = NULL; 18410Sstevel@tonic-gate } 18420Sstevel@tonic-gate out: 1843789Sahrens if (xacl != NULL) { 1844789Sahrens acl_free(xacl); 1845789Sahrens xacl = NULL; 1846789Sahrens } 1847789Sahrens if (attrdiracl != NULL) { 1848789Sahrens acl_free(attrdiracl); 1849789Sahrens attrdiracl = NULL; 1850789Sahrens } 18510Sstevel@tonic-gate if (srcbuf) 18520Sstevel@tonic-gate free(srcbuf); 18530Sstevel@tonic-gate if (targbuf) 18540Sstevel@tonic-gate free(targbuf); 1855*5331Samw 1856*5331Samw if (!saflg && !pflg && !mve) 1857*5331Samw close_all(); 1858*5331Samw return (error == 0 ? 0 : 1); 1859*5331Samw } 1860*5331Samw 1861*5331Samw /* Copy extended system attributes from source to target */ 1862*5331Samw 1863*5331Samw static int 1864*5331Samw copy_sysattr(char *source, char *target) 1865*5331Samw { 1866*5331Samw struct dirent *dp; 1867*5331Samw nvlist_t *response; 1868*5331Samw int error = 0; 1869*5331Samw int chk_support = 0; 1870*5331Samw int sa_support = 1; 1871*5331Samw 1872*5331Samw if (sysattr_support(source, _PC_SATTR_EXISTS) != 1) 1873*5331Samw return (0); 1874*5331Samw 1875*5331Samw if (open_source(source) != 0) 1876*5331Samw return (1); 1877*5331Samw 1878*5331Samw /* 1879*5331Samw * Gets non default extended system attributes from the 1880*5331Samw * source file to copy to the target. The target has 1881*5331Samw * the defaults set when its created and thus no need 1882*5331Samw * to copy the defaults. 1883*5331Samw */ 1884*5331Samw response = sysattr_list(cmd, srcfd, source); 1885*5331Samw 1886*5331Samw if (response != NULL && 1887*5331Samw sysattr_support(target, _PC_SATTR_ENABLED) != 1) { 1888*5331Samw if (!attrsilent) { 1889*5331Samw (void) fprintf(stderr, 1890*5331Samw gettext( 1891*5331Samw "%s: cannot preserve extended system " 1892*5331Samw "attribute, operation not supported on file" 1893*5331Samw " %s\n"), cmd, target); 1894*5331Samw } 1895*5331Samw error++; 1896*5331Samw goto out; 1897*5331Samw } 1898*5331Samw 1899*5331Samw if (srcdirp == NULL) { 1900*5331Samw if (open_target_srctarg_attrdirs(source, target) != 0) { 1901*5331Samw error++; 1902*5331Samw goto out; 1903*5331Samw } 1904*5331Samw if (open_attrdirp(source) != 0) { 1905*5331Samw error++; 1906*5331Samw goto out; 1907*5331Samw } 1908*5331Samw } else { 1909*5331Samw rewind_attrdir(srcdirp); 1910*5331Samw } 1911*5331Samw while (sa_support && (dp = readdir(srcdirp)) != NULL) { 1912*5331Samw nvlist_t *res; 1913*5331Samw int ret; 1914*5331Samw 1915*5331Samw if ((ret = traverse_attrfile(dp, source, target, 0)) == -1) 1916*5331Samw continue; 1917*5331Samw else if (ret > 0) { 1918*5331Samw ++error; 1919*5331Samw goto out; 1920*5331Samw } 1921*5331Samw /* 1922*5331Samw * Gets non default extended system attributes from the 1923*5331Samw * attribute file to copy to the target. The target has 1924*5331Samw * the defaults set when its created and thus no need 1925*5331Samw * to copy the defaults. 1926*5331Samw */ 1927*5331Samw if (dp->d_name != NULL) { 1928*5331Samw res = sysattr_list(cmd, srcattrfd, dp->d_name); 1929*5331Samw if (res == NULL) 1930*5331Samw goto next; 1931*5331Samw 1932*5331Samw if (!chk_support && 1933*5331Samw sysattr_support(target, _PC_SATTR_ENABLED) != 1) { 1934*5331Samw if (!attrsilent) { 1935*5331Samw (void) fprintf(stderr, 1936*5331Samw gettext( 1937*5331Samw "%s: cannot preserve extended " 1938*5331Samw "system attribute, operation not " 1939*5331Samw " %s\n"), cmd, target); 1940*5331Samw } 1941*5331Samw error++; 1942*5331Samw sa_support = 0; 1943*5331Samw } else { 1944*5331Samw chk_support = 1; 1945*5331Samw } 1946*5331Samw 1947*5331Samw /* 1948*5331Samw * Copy non default extended system attributes of named 1949*5331Samw * attribute file. 1950*5331Samw */ 1951*5331Samw if (sa_support && fsetattr(targattrfd, 1952*5331Samw XATTR_VIEW_READWRITE, res) != 0) { 1953*5331Samw ++error; 1954*5331Samw if (!attrsilent) { 1955*5331Samw (void) fprintf(stderr, gettext("%s: " 1956*5331Samw "Failed to copy extended system " 1957*5331Samw "attributes from attribute file " 1958*5331Samw "%s of %s to %s\n"), cmd, 1959*5331Samw dp->d_name, source, target); 1960*5331Samw } 1961*5331Samw } 1962*5331Samw } 1963*5331Samw next: 1964*5331Samw if (srcattrfd != -1) 1965*5331Samw (void) close(srcattrfd); 1966*5331Samw if (targattrfd != -1) 1967*5331Samw (void) close(targattrfd); 1968*5331Samw srcattrfd = targattrfd = -1; 1969*5331Samw if (res != NULL) 1970*5331Samw nvlist_free(res); 1971*5331Samw } 1972*5331Samw 1973*5331Samw /* Copy source file non default extended system attributes to target */ 1974*5331Samw if ((response != NULL) && 1975*5331Samw (fsetattr(targfd, XATTR_VIEW_READWRITE, response)) != 0) { 1976*5331Samw ++error; 1977*5331Samw if (!attrsilent) { 1978*5331Samw (void) fprintf(stderr, gettext("%s: Failed to " 1979*5331Samw "copy extended system attributes from " 1980*5331Samw "%s to %s\n"), cmd, source, target); 1981*5331Samw } 1982*5331Samw } 1983*5331Samw out: 1984*5331Samw if (response != NULL) 1985*5331Samw nvlist_free(response); 1986*5331Samw close_all(); 19870Sstevel@tonic-gate return (error == 0 ? 0 : 1); 19880Sstevel@tonic-gate } 19890Sstevel@tonic-gate 19900Sstevel@tonic-gate /* 19910Sstevel@tonic-gate * nanoseconds are rounded off to microseconds by flooring. 19920Sstevel@tonic-gate */ 19930Sstevel@tonic-gate static void 19940Sstevel@tonic-gate timestruc_to_timeval(timestruc_t *ts, struct timeval *tv) 19950Sstevel@tonic-gate { 19960Sstevel@tonic-gate tv->tv_sec = ts->tv_sec; 19970Sstevel@tonic-gate tv->tv_usec = ts->tv_nsec / 1000; 19980Sstevel@tonic-gate } 1999*5331Samw 2000*5331Samw /* Open the source file */ 2001*5331Samw 2002*5331Samw int 2003*5331Samw open_source(char *src) 2004*5331Samw { 2005*5331Samw int error = 0; 2006*5331Samw 2007*5331Samw srcfd = -1; 2008*5331Samw if ((srcfd = open(src, O_RDONLY)) == -1) { 2009*5331Samw if (pflg && attrsilent) { 2010*5331Samw error++; 2011*5331Samw goto out; 2012*5331Samw } 2013*5331Samw if (!attrsilent) { 2014*5331Samw (void) fprintf(stderr, 2015*5331Samw gettext("%s: cannot open file" 2016*5331Samw " %s: "), cmd, src); 2017*5331Samw perror(""); 2018*5331Samw } 2019*5331Samw ++error; 2020*5331Samw } 2021*5331Samw out: 2022*5331Samw if (error) 2023*5331Samw close_all(); 2024*5331Samw return (error == 0 ? 0 : 1); 2025*5331Samw } 2026*5331Samw 2027*5331Samw /* Open source attribute dir, target and target attribute dir. */ 2028*5331Samw 2029*5331Samw int 2030*5331Samw open_target_srctarg_attrdirs(char *src, char *targ) 2031*5331Samw { 2032*5331Samw int error = 0; 2033*5331Samw 2034*5331Samw targfd = sourcedirfd = targetdirfd = -1; 2035*5331Samw 2036*5331Samw if ((targfd = open(targ, O_RDONLY)) == -1) { 2037*5331Samw if (pflg && attrsilent) { 2038*5331Samw error++; 2039*5331Samw goto out; 2040*5331Samw } 2041*5331Samw if (!attrsilent) { 2042*5331Samw (void) fprintf(stderr, 2043*5331Samw gettext("%s: cannot open file" 2044*5331Samw " %s: "), cmd, targ); 2045*5331Samw perror(""); 2046*5331Samw } 2047*5331Samw ++error; 2048*5331Samw goto out; 2049*5331Samw } 2050*5331Samw 2051*5331Samw if ((sourcedirfd = openat(srcfd, ".", O_RDONLY|O_XATTR)) == -1) { 2052*5331Samw if (pflg && attrsilent) { 2053*5331Samw error++; 2054*5331Samw goto out; 2055*5331Samw } 2056*5331Samw if (!attrsilent) { 2057*5331Samw (void) fprintf(stderr, 2058*5331Samw gettext("%s: cannot open attribute" 2059*5331Samw " directory for %s: "), cmd, src); 2060*5331Samw perror(""); 2061*5331Samw } 2062*5331Samw ++error; 2063*5331Samw goto out; 2064*5331Samw } 2065*5331Samw 2066*5331Samw if (fstat(sourcedirfd, &attrdir) == -1) { 2067*5331Samw if (pflg && attrsilent) { 2068*5331Samw error++; 2069*5331Samw goto out; 2070*5331Samw } 2071*5331Samw 2072*5331Samw if (!attrsilent) { 2073*5331Samw (void) fprintf(stderr, 2074*5331Samw gettext("%s: could not retrieve stat" 2075*5331Samw " information for attribute directory" 2076*5331Samw "of file %s: "), cmd, src); 2077*5331Samw perror(""); 2078*5331Samw } 2079*5331Samw ++error; 2080*5331Samw goto out; 2081*5331Samw } 2082*5331Samw if ((targetdirfd = openat(targfd, ".", O_RDONLY|O_XATTR)) == -1) { 2083*5331Samw if (pflg && attrsilent) { 2084*5331Samw error++; 2085*5331Samw goto out; 2086*5331Samw } 2087*5331Samw if (!attrsilent) { 2088*5331Samw (void) fprintf(stderr, 2089*5331Samw gettext("%s: cannot open attribute" 2090*5331Samw " directory for %s: "), cmd, targ); 2091*5331Samw perror(""); 2092*5331Samw } 2093*5331Samw ++error; 2094*5331Samw } 2095*5331Samw out: 2096*5331Samw if (error) 2097*5331Samw close_all(); 2098*5331Samw return (error == 0 ? 0 : 1); 2099*5331Samw } 2100*5331Samw 2101*5331Samw int 2102*5331Samw open_attrdirp(char *source) 2103*5331Samw { 2104*5331Samw int tmpfd = -1; 2105*5331Samw int error = 0; 2106*5331Samw 2107*5331Samw /* 2108*5331Samw * dup sourcedirfd for use by fdopendir(). 2109*5331Samw * fdopendir will take ownership of given fd and will close 2110*5331Samw * it when closedir() is called. 2111*5331Samw */ 2112*5331Samw 2113*5331Samw if ((tmpfd = dup(sourcedirfd)) == -1) { 2114*5331Samw if (pflg && attrsilent) { 2115*5331Samw error++; 2116*5331Samw goto out; 2117*5331Samw } 2118*5331Samw if (!attrsilent) { 2119*5331Samw (void) fprintf(stderr, 2120*5331Samw gettext( 2121*5331Samw "%s: unable to dup attribute directory" 2122*5331Samw " file descriptor for %s: "), cmd, source); 2123*5331Samw perror(""); 2124*5331Samw ++error; 2125*5331Samw } 2126*5331Samw goto out; 2127*5331Samw } 2128*5331Samw if ((srcdirp = fdopendir(tmpfd)) == NULL) { 2129*5331Samw if (pflg && attrsilent) { 2130*5331Samw error++; 2131*5331Samw goto out; 2132*5331Samw } 2133*5331Samw if (!attrsilent) { 2134*5331Samw (void) fprintf(stderr, 2135*5331Samw gettext("%s: failed to open attribute" 2136*5331Samw " directory for %s: "), cmd, source); 2137*5331Samw perror(""); 2138*5331Samw ++error; 2139*5331Samw } 2140*5331Samw } 2141*5331Samw out: 2142*5331Samw if (error) 2143*5331Samw close_all(); 2144*5331Samw return (error == 0 ? 0 : 1); 2145*5331Samw } 2146*5331Samw 2147*5331Samw /* Skips through ., .., and system attribute 'view' files */ 2148*5331Samw int 2149*5331Samw traverse_attrfile(struct dirent *dp, char *source, char *target, int first) 2150*5331Samw { 2151*5331Samw int error = 0; 2152*5331Samw 2153*5331Samw if ((dp->d_name[0] == '.' && dp->d_name[1] == '\0') || 2154*5331Samw (dp->d_name[0] == '.' && dp->d_name[1] == '.' && 2155*5331Samw dp->d_name[2] == '\0') || 2156*5331Samw (sysattr_type(dp->d_name) == _RO_SATTR) || 2157*5331Samw (sysattr_type(dp->d_name) == _RW_SATTR)) 2158*5331Samw return (-1); 2159*5331Samw 2160*5331Samw if ((srcattrfd = openat(sourcedirfd, dp->d_name, 2161*5331Samw O_RDONLY)) == -1) { 2162*5331Samw if (!attrsilent) { 2163*5331Samw (void) fprintf(stderr, 2164*5331Samw gettext("%s: cannot open attribute %s on" 2165*5331Samw " file %s: "), cmd, dp->d_name, source); 2166*5331Samw perror(""); 2167*5331Samw ++error; 2168*5331Samw goto out; 2169*5331Samw } 2170*5331Samw } 2171*5331Samw 2172*5331Samw if (fstat(srcattrfd, &s3) < 0) { 2173*5331Samw if (!attrsilent) { 2174*5331Samw (void) fprintf(stderr, 2175*5331Samw gettext("%s: could not stat attribute" 2176*5331Samw " %s on file" 2177*5331Samw " %s: "), cmd, dp->d_name, source); 2178*5331Samw perror(""); 2179*5331Samw ++error; 2180*5331Samw } 2181*5331Samw goto out; 2182*5331Samw } 2183*5331Samw 2184*5331Samw if (first) { 2185*5331Samw (void) unlinkat(targetdirfd, dp->d_name, 0); 2186*5331Samw targattrfd = openat(targetdirfd, dp->d_name, 2187*5331Samw O_RDWR|O_CREAT|O_TRUNC, s3.st_mode & MODEBITS); 2188*5331Samw } else { 2189*5331Samw targattrfd = openat(targetdirfd, dp->d_name, O_RDWR); 2190*5331Samw } 2191*5331Samw 2192*5331Samw if (targattrfd == -1) { 2193*5331Samw if (!attrsilent) { 2194*5331Samw (void) fprintf(stderr, 2195*5331Samw gettext("%s: could not create attribute" 2196*5331Samw " %s on file %s: "), cmd, dp->d_name, 2197*5331Samw target); 2198*5331Samw perror(""); 2199*5331Samw ++error; 2200*5331Samw } 2201*5331Samw goto out; 2202*5331Samw } 2203*5331Samw 2204*5331Samw if (fstat(targattrfd, &s4) < 0) { 2205*5331Samw if (!attrsilent) { 2206*5331Samw (void) fprintf(stderr, 2207*5331Samw gettext("%s: could not stat attribute" 2208*5331Samw " %s on file" 2209*5331Samw " %s: "), cmd, dp->d_name, target); 2210*5331Samw perror(""); 2211*5331Samw ++error; 2212*5331Samw } 2213*5331Samw } 2214*5331Samw 2215*5331Samw out: 2216*5331Samw if (error) { 2217*5331Samw if (srcattrfd != -1) 2218*5331Samw (void) close(srcattrfd); 2219*5331Samw if (targattrfd != -1) 2220*5331Samw (void) close(targattrfd); 2221*5331Samw } 2222*5331Samw return (error == 0 ? 0 :1); 2223*5331Samw } 2224*5331Samw 2225*5331Samw void 2226*5331Samw rewind_attrdir(DIR * sdp) 2227*5331Samw { 2228*5331Samw int pwdfd; 2229*5331Samw 2230*5331Samw pwdfd = open(".", O_RDONLY); 2231*5331Samw if ((pwdfd != -1) && (fchdir(sourcedirfd) == 0)) { 2232*5331Samw rewinddir(sdp); 2233*5331Samw (void) fchdir(pwdfd); 2234*5331Samw (void) close(pwdfd); 2235*5331Samw } else { 2236*5331Samw if (!attrsilent) { 2237*5331Samw (void) fprintf(stderr, gettext("%s: " 2238*5331Samw "failed to rewind attribute dir\n"), 2239*5331Samw cmd); 2240*5331Samw } 2241*5331Samw } 2242*5331Samw } 2243*5331Samw 2244*5331Samw void 2245*5331Samw close_all() 2246*5331Samw { 2247*5331Samw if (srcattrfd != -1) 2248*5331Samw (void) close(srcattrfd); 2249*5331Samw if (targattrfd != -1) 2250*5331Samw (void) close(targattrfd); 2251*5331Samw if (sourcedirfd != -1) 2252*5331Samw (void) close(sourcedirfd); 2253*5331Samw if (targetdirfd != -1) 2254*5331Samw (void) close(targetdirfd); 2255*5331Samw if (srcdirp != NULL) { 2256*5331Samw (void) closedir(srcdirp); 2257*5331Samw srcdirp = NULL; 2258*5331Samw } 2259*5331Samw if (srcfd != -1) 2260*5331Samw (void) close(srcfd); 2261*5331Samw if (targfd != -1) 2262*5331Samw (void) close(targfd); 2263*5331Samw } 2264