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 *); 1005331Samw 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 1045331Samw 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; 1255331Samw static int saflg = 0; /* 'cp' extended system attr. */ 1265331Samw static int srcfd; 1275331Samw static int targfd; 1285331Samw static int sourcedirfd; 1295331Samw static int targetdirfd; 1305331Samw static DIR *srcdirp; 1315331Samw static int srcattrfd; 1325331Samw static int targattrfd; 1335331Samw static struct stat attrdir; 1345331Samw 1355331Samw /* Extended system attributes support */ 1365331Samw 1375331Samw static int open_source(char *); 1385331Samw static int open_target_srctarg_attrdirs(char *, char *); 1395331Samw static int open_attrdirp(char *); 1405331Samw static int traverse_attrfile(struct dirent *, char *, char *, int); 1415331Samw static void rewind_attrdir(DIR *); 1425331Samw static void close_all(); 1435331Samw 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: 1915331Samw * cp -r|-R [-H|-L|-P] [-fip@/] file1 [file2 ...] target 1925331Samw * 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) { 2005331Samw 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; 2135331Samw saflg = 0; 2140Sstevel@tonic-gate #else 215*5374Sbasabi if (atflg == 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; 2545331Samw case '/': 2555331Samw saflg++; 2565331Samw attrsilent = 0; 2575331Samw #ifdef XPG4 2585331Samw pflg = 0; 2595331Samw #endif 2605331Samw 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; 5085331Samw int sattret = 0; 5090Sstevel@tonic-gate int errno_save; 510*5374Sbasabi int error = 0; 5110Sstevel@tonic-gate 5120Sstevel@tonic-gate switch (chkfiles(source, &target)) { 5130Sstevel@tonic-gate case 1: return (1); 5140Sstevel@tonic-gate case 2: return (0); 5150Sstevel@tonic-gate /* default - fall through */ 5160Sstevel@tonic-gate } 5170Sstevel@tonic-gate 5180Sstevel@tonic-gate /* 5190Sstevel@tonic-gate * If it's a recursive copy and source 5200Sstevel@tonic-gate * is a directory, then call rcopy (from copydir). 5210Sstevel@tonic-gate */ 5220Sstevel@tonic-gate if (cpy) { 5230Sstevel@tonic-gate if (ISDIR(s1)) { 5240Sstevel@tonic-gate int rc; 5250Sstevel@tonic-gate avl_index_t where = 0; 5260Sstevel@tonic-gate tree_node_t *tnode; 5270Sstevel@tonic-gate tree_node_t *tptr; 5280Sstevel@tonic-gate dev_t save_dev = s1.st_dev; 5290Sstevel@tonic-gate ino_t save_ino = s1.st_ino; 5300Sstevel@tonic-gate 5310Sstevel@tonic-gate /* 5320Sstevel@tonic-gate * We will be recursing into the directory so 5330Sstevel@tonic-gate * save the inode information to a search tree 5340Sstevel@tonic-gate * to avoid getting into an endless loop. 5350Sstevel@tonic-gate */ 5360Sstevel@tonic-gate if ((rc = add_tnode(&stree, save_dev, save_ino)) != 1) { 5370Sstevel@tonic-gate if (rc == 0) { 5380Sstevel@tonic-gate /* 5390Sstevel@tonic-gate * We've already visited this directory. 5400Sstevel@tonic-gate * Don't remove the search tree entry 5410Sstevel@tonic-gate * to make sure we don't get into an 5420Sstevel@tonic-gate * endless loop if revisited from a 5430Sstevel@tonic-gate * different part of the hierarchy. 5440Sstevel@tonic-gate */ 5450Sstevel@tonic-gate (void) fprintf(stderr, gettext( 5460Sstevel@tonic-gate "%s: cycle detected: %s\n"), 5470Sstevel@tonic-gate cmd, source); 5480Sstevel@tonic-gate } else { 5490Sstevel@tonic-gate Perror(source); 5500Sstevel@tonic-gate } 5510Sstevel@tonic-gate return (1); 5520Sstevel@tonic-gate } 5530Sstevel@tonic-gate 5540Sstevel@tonic-gate cmdarg = 0; 5550Sstevel@tonic-gate rc = copydir(source, target); 5560Sstevel@tonic-gate 5570Sstevel@tonic-gate /* 5580Sstevel@tonic-gate * Create a tnode to get an index to the matching 5590Sstevel@tonic-gate * node (same dev and inode) in the search tree, 5600Sstevel@tonic-gate * then use the index to remove the matching node 5610Sstevel@tonic-gate * so it we do not wrongly detect a cycle when 5620Sstevel@tonic-gate * revisiting this directory from another part of 5630Sstevel@tonic-gate * the hierarchy. 5640Sstevel@tonic-gate */ 5650Sstevel@tonic-gate if ((tnode = create_tnode(save_dev, 5660Sstevel@tonic-gate save_ino)) == NULL) { 5670Sstevel@tonic-gate Perror(source); 5680Sstevel@tonic-gate return (1); 5690Sstevel@tonic-gate } 5700Sstevel@tonic-gate if ((tptr = avl_find(stree, tnode, &where)) != NULL) { 5710Sstevel@tonic-gate avl_remove(stree, tptr); 5720Sstevel@tonic-gate } 5730Sstevel@tonic-gate free(tptr); 5740Sstevel@tonic-gate free(tnode); 5750Sstevel@tonic-gate return (rc); 5760Sstevel@tonic-gate 5770Sstevel@tonic-gate } else if (ISDEV(s1) && Rflg) { 5780Sstevel@tonic-gate return (copyspecial(target)); 5790Sstevel@tonic-gate } else { 5800Sstevel@tonic-gate goto copy; 5810Sstevel@tonic-gate } 5820Sstevel@tonic-gate } 5830Sstevel@tonic-gate 5840Sstevel@tonic-gate if (mve) { 5850Sstevel@tonic-gate if (rename(source, target) >= 0) 5860Sstevel@tonic-gate return (0); 5870Sstevel@tonic-gate if (errno != EXDEV) { 5880Sstevel@tonic-gate if (errno == ENOTDIR && ISDIR(s1)) { 5890Sstevel@tonic-gate (void) fprintf(stderr, 5900Sstevel@tonic-gate gettext("%s: %s is a directory\n"), 5910Sstevel@tonic-gate cmd, source); 5920Sstevel@tonic-gate return (1); 5930Sstevel@tonic-gate } 5940Sstevel@tonic-gate (void) fprintf(stderr, 5950Sstevel@tonic-gate gettext("%s: cannot rename %s to %s: "), 5960Sstevel@tonic-gate cmd, source, target); 5970Sstevel@tonic-gate perror(""); 5980Sstevel@tonic-gate return (1); 5990Sstevel@tonic-gate } 6000Sstevel@tonic-gate 6010Sstevel@tonic-gate /* 6020Sstevel@tonic-gate * cannot move a non-directory (source) onto an existing 6030Sstevel@tonic-gate * directory (target) 6040Sstevel@tonic-gate * 6050Sstevel@tonic-gate */ 6060Sstevel@tonic-gate if (targetexists && ISDIR(s2) && (!ISDIR(s1))) { 6070Sstevel@tonic-gate (void) fprintf(stderr, 6080Sstevel@tonic-gate gettext("%s: cannot mv a non directory %s " 6090Sstevel@tonic-gate "over existing directory" 6100Sstevel@tonic-gate " %s \n"), cmd, source, target); 6110Sstevel@tonic-gate return (1); 6120Sstevel@tonic-gate } 6130Sstevel@tonic-gate if (ISDIR(s1)) { 6140Sstevel@tonic-gate #ifdef XPG4 6150Sstevel@tonic-gate if (targetexists && ISDIR(s2)) { 6160Sstevel@tonic-gate /* existing target dir must be empty */ 6170Sstevel@tonic-gate if (rmdir(target) < 0) { 6180Sstevel@tonic-gate errno_save = errno; 6190Sstevel@tonic-gate (void) fprintf(stderr, 6200Sstevel@tonic-gate gettext("%s: cannot rmdir %s: "), 6210Sstevel@tonic-gate cmd, target); 6220Sstevel@tonic-gate errno = errno_save; 6230Sstevel@tonic-gate perror(""); 6240Sstevel@tonic-gate return (1); 6250Sstevel@tonic-gate } 6260Sstevel@tonic-gate } 6270Sstevel@tonic-gate #endif 6280Sstevel@tonic-gate if ((n = copydir(source, target)) == 0) 6290Sstevel@tonic-gate (void) rmdir(source); 6300Sstevel@tonic-gate return (n); 6310Sstevel@tonic-gate } 6320Sstevel@tonic-gate 6330Sstevel@tonic-gate /* doors can't be moved across filesystems */ 6340Sstevel@tonic-gate if (ISDOOR(s1)) { 6350Sstevel@tonic-gate (void) fprintf(stderr, 6360Sstevel@tonic-gate gettext("%s: %s: can't move door " 6370Sstevel@tonic-gate "across file systems\n"), cmd, source); 6380Sstevel@tonic-gate return (1); 6390Sstevel@tonic-gate } 6400Sstevel@tonic-gate /* 6410Sstevel@tonic-gate * File can't be renamed, try to recreate the symbolic 6420Sstevel@tonic-gate * link or special device, or copy the file wholesale 6430Sstevel@tonic-gate * between file systems. 6440Sstevel@tonic-gate */ 6450Sstevel@tonic-gate if (ISLNK(s1)) { 6460Sstevel@tonic-gate register int m; 6470Sstevel@tonic-gate register mode_t md; 6480Sstevel@tonic-gate char symln[PATH_MAX + 1]; 6490Sstevel@tonic-gate 6500Sstevel@tonic-gate if (targetexists && unlink(target) < 0) { 6510Sstevel@tonic-gate (void) fprintf(stderr, 6520Sstevel@tonic-gate gettext("%s: cannot unlink %s: "), 6530Sstevel@tonic-gate cmd, target); 6540Sstevel@tonic-gate perror(""); 6550Sstevel@tonic-gate return (1); 6560Sstevel@tonic-gate } 6570Sstevel@tonic-gate 6580Sstevel@tonic-gate if ((m = readlink(source, symln, 6590Sstevel@tonic-gate sizeof (symln) - 1)) < 0) { 6600Sstevel@tonic-gate Perror(source); 6610Sstevel@tonic-gate return (1); 6620Sstevel@tonic-gate } 6630Sstevel@tonic-gate symln[m] = '\0'; 6640Sstevel@tonic-gate 6650Sstevel@tonic-gate md = umask(~(s1.st_mode & MODEBITS)); 6660Sstevel@tonic-gate if (symlink(symln, target) < 0) { 6670Sstevel@tonic-gate Perror(target); 6680Sstevel@tonic-gate return (1); 6690Sstevel@tonic-gate } 6700Sstevel@tonic-gate (void) umask(md); 6710Sstevel@tonic-gate m = lchown(target, UID(s1), GID(s1)); 6720Sstevel@tonic-gate #ifdef XPG4 6730Sstevel@tonic-gate if (m < 0) { 6740Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s: cannot" 6750Sstevel@tonic-gate " change owner and group of" 6760Sstevel@tonic-gate " %s: "), cmd, target); 6770Sstevel@tonic-gate perror(""); 6780Sstevel@tonic-gate } 6790Sstevel@tonic-gate #endif 6800Sstevel@tonic-gate goto cleanup; 6810Sstevel@tonic-gate } 6820Sstevel@tonic-gate if (ISDEV(s1)) { 6830Sstevel@tonic-gate 6840Sstevel@tonic-gate if (targetexists && unlink(target) < 0) { 6850Sstevel@tonic-gate (void) fprintf(stderr, 6860Sstevel@tonic-gate gettext("%s: cannot unlink %s: "), 6870Sstevel@tonic-gate cmd, target); 6880Sstevel@tonic-gate perror(""); 6890Sstevel@tonic-gate return (1); 6900Sstevel@tonic-gate } 6910Sstevel@tonic-gate 6920Sstevel@tonic-gate if (mknod(target, s1.st_mode, s1.st_rdev) < 0) { 6930Sstevel@tonic-gate Perror(target); 6940Sstevel@tonic-gate return (1); 6950Sstevel@tonic-gate } 6960Sstevel@tonic-gate 6970Sstevel@tonic-gate (void) chg_mode(target, UID(s1), GID(s1), FMODE(s1)); 6980Sstevel@tonic-gate (void) chg_time(target, s1); 6990Sstevel@tonic-gate goto cleanup; 7000Sstevel@tonic-gate } 7010Sstevel@tonic-gate 7020Sstevel@tonic-gate if (ISREG(s1)) { 7030Sstevel@tonic-gate if (ISDIR(s2)) { 7040Sstevel@tonic-gate if (targetexists && rmdir(target) < 0) { 7050Sstevel@tonic-gate (void) fprintf(stderr, 7060Sstevel@tonic-gate gettext("%s: cannot rmdir %s: "), 7070Sstevel@tonic-gate cmd, target); 7080Sstevel@tonic-gate perror(""); 7090Sstevel@tonic-gate return (1); 7100Sstevel@tonic-gate } 7110Sstevel@tonic-gate } else { 7120Sstevel@tonic-gate if (targetexists && unlink(target) < 0) { 7130Sstevel@tonic-gate (void) fprintf(stderr, 7140Sstevel@tonic-gate gettext("%s: cannot unlink %s: "), 7150Sstevel@tonic-gate cmd, target); 7160Sstevel@tonic-gate perror(""); 7170Sstevel@tonic-gate return (1); 7180Sstevel@tonic-gate } 7190Sstevel@tonic-gate } 7200Sstevel@tonic-gate 7210Sstevel@tonic-gate 7220Sstevel@tonic-gate copy: 7230Sstevel@tonic-gate /* 7240Sstevel@tonic-gate * If the source file is a symlink, and either 7250Sstevel@tonic-gate * -P or -H flag (only if -H is specified and the 7260Sstevel@tonic-gate * source file is not a command line argument) 7270Sstevel@tonic-gate * were specified, then action is taken on the symlink 7280Sstevel@tonic-gate * itself, not the file referenced by the symlink. 7290Sstevel@tonic-gate * Note: this is executed for 'cp' only. 7300Sstevel@tonic-gate */ 7310Sstevel@tonic-gate if (cpy && (Pflg || (Hflg && !cmdarg)) && (ISLNK(s1))) { 7320Sstevel@tonic-gate int m; 7330Sstevel@tonic-gate mode_t md; 7340Sstevel@tonic-gate char symln[PATH_MAX + 1]; 7350Sstevel@tonic-gate 7360Sstevel@tonic-gate m = readlink(source, symln, sizeof (symln) - 1); 7370Sstevel@tonic-gate 7380Sstevel@tonic-gate if (m < 0) { 7390Sstevel@tonic-gate Perror(source); 7400Sstevel@tonic-gate return (1); 7410Sstevel@tonic-gate } 7420Sstevel@tonic-gate symln[m] = '\0'; 7430Sstevel@tonic-gate 7440Sstevel@tonic-gate /* 7450Sstevel@tonic-gate * Copy the sym link to the target. 7460Sstevel@tonic-gate * Note: If the target exists, write a 7470Sstevel@tonic-gate * diagnostic message, do nothing more 7480Sstevel@tonic-gate * with the source file, and return to 7490Sstevel@tonic-gate * process any remaining files. 7500Sstevel@tonic-gate */ 7510Sstevel@tonic-gate md = umask(~(s1.st_mode & MODEBITS)); 7520Sstevel@tonic-gate if (symlink(symln, target) < 0) { 7530Sstevel@tonic-gate Perror(target); 7540Sstevel@tonic-gate return (1); 7550Sstevel@tonic-gate } 7560Sstevel@tonic-gate (void) umask(md); 7570Sstevel@tonic-gate m = lchown(target, UID(s1), GID(s1)); 7580Sstevel@tonic-gate 7590Sstevel@tonic-gate if (m < 0) { 7600Sstevel@tonic-gate (void) fprintf(stderr, gettext( 7610Sstevel@tonic-gate "cp: cannot change owner and " 7620Sstevel@tonic-gate "group of %s:"), target); 7630Sstevel@tonic-gate perror(""); 7640Sstevel@tonic-gate } 7650Sstevel@tonic-gate } else { 7660Sstevel@tonic-gate /* 7670Sstevel@tonic-gate * Copy the file. If it happens to be a 7680Sstevel@tonic-gate * symlink, copy the file referenced 7690Sstevel@tonic-gate * by the symlink. 7700Sstevel@tonic-gate */ 7710Sstevel@tonic-gate fi = open(source, O_RDONLY); 7720Sstevel@tonic-gate if (fi < 0) { 7730Sstevel@tonic-gate (void) fprintf(stderr, 7740Sstevel@tonic-gate gettext("%s: cannot open %s: "), 7750Sstevel@tonic-gate cmd, source); 7760Sstevel@tonic-gate perror(""); 7770Sstevel@tonic-gate return (1); 7780Sstevel@tonic-gate } 7790Sstevel@tonic-gate 7800Sstevel@tonic-gate fo = creat(target, s1.st_mode & MODEBITS); 7810Sstevel@tonic-gate if (fo < 0) { 7820Sstevel@tonic-gate /* 7830Sstevel@tonic-gate * If -f and creat() failed, unlink 7840Sstevel@tonic-gate * and try again. 7850Sstevel@tonic-gate */ 7860Sstevel@tonic-gate if (fflg) { 7870Sstevel@tonic-gate (void) unlink(target); 7880Sstevel@tonic-gate fo = creat(target, 7890Sstevel@tonic-gate s1.st_mode & MODEBITS); 7900Sstevel@tonic-gate } 7910Sstevel@tonic-gate } 7920Sstevel@tonic-gate if (fo < 0) { 7930Sstevel@tonic-gate (void) fprintf(stderr, 7940Sstevel@tonic-gate gettext("%s: cannot create %s: "), 7950Sstevel@tonic-gate cmd, target); 7960Sstevel@tonic-gate perror(""); 7970Sstevel@tonic-gate (void) close(fi); 7980Sstevel@tonic-gate return (1); 7990Sstevel@tonic-gate } else { 8000Sstevel@tonic-gate /* stat the new file, its used below */ 8010Sstevel@tonic-gate (void) stat(target, &s2); 8020Sstevel@tonic-gate } 8030Sstevel@tonic-gate 8040Sstevel@tonic-gate /* 8050Sstevel@tonic-gate * Set target's permissions to the source 8060Sstevel@tonic-gate * before any copying so that any partially 8070Sstevel@tonic-gate * copied file will have the source's 8080Sstevel@tonic-gate * permissions (at most) or umask permissions 8090Sstevel@tonic-gate * whichever is the most restrictive. 8100Sstevel@tonic-gate * 8110Sstevel@tonic-gate * ACL for regular files 8120Sstevel@tonic-gate */ 8130Sstevel@tonic-gate 8140Sstevel@tonic-gate if (pflg || mve) { 8150Sstevel@tonic-gate (void) chmod(target, FMODE(s1)); 816789Sahrens if (s1acl != NULL) { 817789Sahrens if ((acl_set(target, 818789Sahrens s1acl)) < 0) { 819*5374Sbasabi error++; 820*5374Sbasabi (void) fprintf(stderr, 821*5374Sbasabi gettext("%s: " 822*5374Sbasabi "Failed to set " 823*5374Sbasabi "acl entries " 824*5374Sbasabi "on %s\n"), cmd, 825*5374Sbasabi target); 8261465Smarks acl_free(s1acl); 8271465Smarks s1acl = NULL; 8280Sstevel@tonic-gate /* 8290Sstevel@tonic-gate * else: silent and 8300Sstevel@tonic-gate * continue 8310Sstevel@tonic-gate */ 8320Sstevel@tonic-gate } 8330Sstevel@tonic-gate } 8340Sstevel@tonic-gate } 8350Sstevel@tonic-gate 8360Sstevel@tonic-gate if (fstat(fi, &s1) < 0) { 8370Sstevel@tonic-gate (void) fprintf(stderr, 8380Sstevel@tonic-gate gettext("%s: cannot access %s\n"), 8390Sstevel@tonic-gate cmd, source); 8400Sstevel@tonic-gate return (1); 8410Sstevel@tonic-gate } 8420Sstevel@tonic-gate if (IDENTICAL(s1, s2)) { 8430Sstevel@tonic-gate (void) fprintf(stderr, 8440Sstevel@tonic-gate gettext( 8450Sstevel@tonic-gate "%s: %s and %s are identical\n"), 8460Sstevel@tonic-gate cmd, source, target); 8470Sstevel@tonic-gate return (1); 8480Sstevel@tonic-gate } 8490Sstevel@tonic-gate 8505331Samw if (writefile(fi, fo, source, target, NULL, 8515331Samw NULL, &s1, &s2) != 0) { 8520Sstevel@tonic-gate return (1); 8530Sstevel@tonic-gate } 8540Sstevel@tonic-gate 8550Sstevel@tonic-gate (void) close(fi); 8560Sstevel@tonic-gate if (close(fo) < 0) { 8570Sstevel@tonic-gate Perror2(target, "write"); 8580Sstevel@tonic-gate return (1); 8590Sstevel@tonic-gate } 8600Sstevel@tonic-gate } 8615331Samw /* Copy regular extended attributes */ 8625331Samw if (pflg || atflg || mve || saflg) { 8630Sstevel@tonic-gate attret = copyattributes(source, target); 8640Sstevel@tonic-gate if (attret != 0 && !attrsilent) { 8650Sstevel@tonic-gate (void) fprintf(stderr, gettext( 8664774Sas145665 "%s: Failed to preserve" 8674774Sas145665 " extended attributes of file" 8684774Sas145665 " %s\n"), cmd, source); 8690Sstevel@tonic-gate } 8705331Samw /* Copy extended system attributes */ 871*5374Sbasabi if (pflg || mve || saflg) 8725331Samw sattret = copy_sysattr(source, target); 8730Sstevel@tonic-gate if (mve && attret != 0) { 8740Sstevel@tonic-gate (void) unlink(target); 8750Sstevel@tonic-gate return (1); 8760Sstevel@tonic-gate } 8775331Samw if (attrsilent) { 8780Sstevel@tonic-gate attret = 0; 8795331Samw } 8800Sstevel@tonic-gate } 8810Sstevel@tonic-gate 8820Sstevel@tonic-gate /* 8830Sstevel@tonic-gate * XPG4: the write system call will clear setgid 8840Sstevel@tonic-gate * and setuid bits, so set them again. 8850Sstevel@tonic-gate */ 8860Sstevel@tonic-gate if (pflg || mve) { 8870Sstevel@tonic-gate if ((ret = chg_mode(target, UID(s1), GID(s1), 8880Sstevel@tonic-gate FMODE(s1))) > 0) 8890Sstevel@tonic-gate return (1); 8901231Smarks /* 8911231Smarks * Reapply ACL, since chmod may have 8921231Smarks * altered ACL 8931231Smarks */ 8941231Smarks if (s1acl != NULL) { 8951231Smarks if ((acl_set(target, s1acl)) < 0) { 896*5374Sbasabi error++; 897*5374Sbasabi (void) fprintf(stderr, 898*5374Sbasabi gettext("%s: Failed to " 899*5374Sbasabi "set acl entries " 900*5374Sbasabi "on %s\n"), cmd, target); 9011231Smarks /* 9021231Smarks * else: silent and 9031231Smarks * continue 9041231Smarks */ 9051231Smarks } 9061231Smarks } 9070Sstevel@tonic-gate if ((ret = chg_time(target, s1)) > 0) 9080Sstevel@tonic-gate return (1); 9090Sstevel@tonic-gate } 9100Sstevel@tonic-gate if (cpy) { 911*5374Sbasabi if (error != 0 || attret != 0 || sattret != 0) 9120Sstevel@tonic-gate return (1); 9130Sstevel@tonic-gate return (0); 9140Sstevel@tonic-gate } 9150Sstevel@tonic-gate goto cleanup; 9160Sstevel@tonic-gate } 9170Sstevel@tonic-gate (void) fprintf(stderr, 9180Sstevel@tonic-gate gettext("%s: %s: unknown file type 0x%x\n"), cmd, 9194774Sas145665 source, (s1.st_mode & S_IFMT)); 9200Sstevel@tonic-gate return (1); 9210Sstevel@tonic-gate 9220Sstevel@tonic-gate cleanup: 9230Sstevel@tonic-gate if (unlink(source) < 0) { 9240Sstevel@tonic-gate (void) unlink(target); 9250Sstevel@tonic-gate (void) fprintf(stderr, 9260Sstevel@tonic-gate gettext("%s: cannot unlink %s: "), 9270Sstevel@tonic-gate cmd, source); 9280Sstevel@tonic-gate perror(""); 9290Sstevel@tonic-gate return (1); 9300Sstevel@tonic-gate } 931*5374Sbasabi if (error != 0 || attret != 0 || sattret != 0) 932*5374Sbasabi return (1); 9330Sstevel@tonic-gate return (ret); 9340Sstevel@tonic-gate } 9350Sstevel@tonic-gate /*NOTREACHED*/ 936212Scf46844 return (ret); 9370Sstevel@tonic-gate } 9380Sstevel@tonic-gate 9390Sstevel@tonic-gate /* 9400Sstevel@tonic-gate * create_tnode() 9410Sstevel@tonic-gate * 9420Sstevel@tonic-gate * Create a node for use with the search tree which contains the 9430Sstevel@tonic-gate * inode information (device id and inode number). 9440Sstevel@tonic-gate * 9450Sstevel@tonic-gate * Input 9460Sstevel@tonic-gate * dev - device id 9470Sstevel@tonic-gate * ino - inode number 9480Sstevel@tonic-gate * 9490Sstevel@tonic-gate * Output 9500Sstevel@tonic-gate * tnode - NULL on error, otherwise returns a tnode structure 9510Sstevel@tonic-gate * which contains the input device id and inode number. 9520Sstevel@tonic-gate */ 9530Sstevel@tonic-gate static tree_node_t * 9540Sstevel@tonic-gate create_tnode(dev_t dev, ino_t ino) 9550Sstevel@tonic-gate { 9560Sstevel@tonic-gate tree_node_t *tnode; 9570Sstevel@tonic-gate 9580Sstevel@tonic-gate if ((tnode = (tree_node_t *)malloc(sizeof (tree_node_t))) != NULL) { 9590Sstevel@tonic-gate tnode->node_dev = dev; 9600Sstevel@tonic-gate tnode->node_ino = ino; 9610Sstevel@tonic-gate } 9620Sstevel@tonic-gate 9630Sstevel@tonic-gate return (tnode); 9640Sstevel@tonic-gate } 9650Sstevel@tonic-gate 9660Sstevel@tonic-gate static int 9670Sstevel@tonic-gate chkfiles(char *source, char **to) 9680Sstevel@tonic-gate { 9690Sstevel@tonic-gate char *buf = (char *)NULL; 9700Sstevel@tonic-gate int (*statf)() = (cpy && 9714774Sas145665 !(Pflg || (Hflg && !cmdarg))) ? stat : lstat; 9720Sstevel@tonic-gate char *target = *to; 973789Sahrens int error; 9740Sstevel@tonic-gate 9750Sstevel@tonic-gate /* 9760Sstevel@tonic-gate * Make sure source file exists. 9770Sstevel@tonic-gate */ 9780Sstevel@tonic-gate if ((*statf)(source, &s1) < 0) { 9790Sstevel@tonic-gate /* 9800Sstevel@tonic-gate * Keep the old error message except when someone tries to 9810Sstevel@tonic-gate * mv/cp/ln a symbolic link that has a trailing slash and 9820Sstevel@tonic-gate * points to a file. 9830Sstevel@tonic-gate */ 9840Sstevel@tonic-gate if (errno == ENOTDIR) 9850Sstevel@tonic-gate (void) fprintf(stderr, "%s: %s: %s\n", cmd, source, 9860Sstevel@tonic-gate strerror(errno)); 9870Sstevel@tonic-gate else 9880Sstevel@tonic-gate (void) fprintf(stderr, 9890Sstevel@tonic-gate gettext("%s: cannot access %s\n"), cmd, source); 9900Sstevel@tonic-gate return (1); 9910Sstevel@tonic-gate } 9920Sstevel@tonic-gate 9930Sstevel@tonic-gate /* 9940Sstevel@tonic-gate * Get ACL info: don't bother with ln or mv'ing symlinks 9950Sstevel@tonic-gate */ 9960Sstevel@tonic-gate if ((!lnk) && !(mve && ISLNK(s1))) { 997789Sahrens if (s1acl != NULL) { 998789Sahrens acl_free(s1acl); 999789Sahrens s1acl = NULL; 10000Sstevel@tonic-gate } 1001789Sahrens if ((error = acl_get(source, ACL_NO_TRIVIAL, &s1acl)) != 0) { 1002789Sahrens (void) fprintf(stderr, 1003789Sahrens "%s: failed to get acl entries: %s\n", source, 1004789Sahrens acl_strerror(error)); 1005789Sahrens return (1); 10060Sstevel@tonic-gate } 10070Sstevel@tonic-gate /* else: just permission bits */ 10080Sstevel@tonic-gate } 10090Sstevel@tonic-gate 10100Sstevel@tonic-gate /* 10110Sstevel@tonic-gate * If stat fails, then the target doesn't exist, 10120Sstevel@tonic-gate * we will create a new target with default file type of regular. 10130Sstevel@tonic-gate */ 10140Sstevel@tonic-gate 10150Sstevel@tonic-gate FTYPE(s2) = S_IFREG; 10160Sstevel@tonic-gate targetexists = 0; 10170Sstevel@tonic-gate if ((*statf)(target, &s2) >= 0) { 10180Sstevel@tonic-gate if (ISLNK(s2)) 10190Sstevel@tonic-gate (void) stat(target, &s2); 10200Sstevel@tonic-gate /* 10210Sstevel@tonic-gate * If target is a directory, 10220Sstevel@tonic-gate * make complete name of new file 10230Sstevel@tonic-gate * within that directory. 10240Sstevel@tonic-gate */ 10250Sstevel@tonic-gate if (ISDIR(s2)) { 10260Sstevel@tonic-gate size_t len; 10270Sstevel@tonic-gate 10280Sstevel@tonic-gate len = strlen(target) + strlen(dname(source)) + 4; 10290Sstevel@tonic-gate if ((buf = (char *)malloc(len)) == NULL) { 10300Sstevel@tonic-gate (void) fprintf(stderr, 10310Sstevel@tonic-gate gettext("%s: Insufficient memory to " 10324774Sas145665 "%s %s\n "), cmd, cmd, source); 10330Sstevel@tonic-gate exit(3); 10340Sstevel@tonic-gate } 10350Sstevel@tonic-gate (void) snprintf(buf, len, "%s/%s", 10360Sstevel@tonic-gate target, dname(source)); 10370Sstevel@tonic-gate *to = target = buf; 10380Sstevel@tonic-gate } 10390Sstevel@tonic-gate 10400Sstevel@tonic-gate if ((*statf)(target, &s2) >= 0) { 10410Sstevel@tonic-gate int overwrite = FALSE; 10420Sstevel@tonic-gate int override = FALSE; 10430Sstevel@tonic-gate 10440Sstevel@tonic-gate targetexists++; 10450Sstevel@tonic-gate if (cpy || mve) { 10460Sstevel@tonic-gate /* 10470Sstevel@tonic-gate * For cp and mv, it is an error if the 10480Sstevel@tonic-gate * source and target are the same file. 10490Sstevel@tonic-gate * Check for the same inode and file 10500Sstevel@tonic-gate * system, but don't check for the same 10510Sstevel@tonic-gate * absolute pathname because it is an 10520Sstevel@tonic-gate * error when the source and target are 10530Sstevel@tonic-gate * hard links to the same file. 10540Sstevel@tonic-gate */ 10550Sstevel@tonic-gate if (IDENTICAL(s1, s2)) { 10560Sstevel@tonic-gate (void) fprintf(stderr, 10570Sstevel@tonic-gate gettext( 10580Sstevel@tonic-gate "%s: %s and %s are identical\n"), 10590Sstevel@tonic-gate cmd, source, target); 10600Sstevel@tonic-gate if (buf != NULL) 10610Sstevel@tonic-gate free(buf); 10620Sstevel@tonic-gate return (1); 10630Sstevel@tonic-gate } 10640Sstevel@tonic-gate } 10650Sstevel@tonic-gate if (lnk) { 10660Sstevel@tonic-gate /* 10670Sstevel@tonic-gate * For ln, it is an error if the source and 10680Sstevel@tonic-gate * target are identical files (same inode, 10690Sstevel@tonic-gate * same file system, and filenames resolve 10700Sstevel@tonic-gate * to same absolute pathname). 10710Sstevel@tonic-gate */ 10720Sstevel@tonic-gate if (!chk_different(source, target)) { 10730Sstevel@tonic-gate if (buf != NULL) 10740Sstevel@tonic-gate free(buf); 10750Sstevel@tonic-gate return (1); 10760Sstevel@tonic-gate } 10770Sstevel@tonic-gate } 10780Sstevel@tonic-gate if (lnk && !silent) { 10790Sstevel@tonic-gate (void) fprintf(stderr, 10800Sstevel@tonic-gate gettext("%s: %s: File exists\n"), 10810Sstevel@tonic-gate cmd, target); 10820Sstevel@tonic-gate if (buf != NULL) 10830Sstevel@tonic-gate free(buf); 10840Sstevel@tonic-gate return (1); 10850Sstevel@tonic-gate } 10860Sstevel@tonic-gate 10870Sstevel@tonic-gate /* 10880Sstevel@tonic-gate * overwrite: 10890Sstevel@tonic-gate * If the user does not have access to 10900Sstevel@tonic-gate * the target, ask ----if it is not 10910Sstevel@tonic-gate * silent and user invoked command 10920Sstevel@tonic-gate * interactively. 10930Sstevel@tonic-gate * 10940Sstevel@tonic-gate * override: 10950Sstevel@tonic-gate * If not silent, and stdin is a terminal, and 10960Sstevel@tonic-gate * there's no write access, and the file isn't a 10970Sstevel@tonic-gate * symbolic link, ask for permission. 10980Sstevel@tonic-gate * 10990Sstevel@tonic-gate * XPG4: both overwrite and override: 11000Sstevel@tonic-gate * ask only one question. 11010Sstevel@tonic-gate * 11020Sstevel@tonic-gate * TRANSLATION_NOTE - The following messages will 11030Sstevel@tonic-gate * contain the first character of the strings for 11040Sstevel@tonic-gate * "yes" and "no" defined in the file 11050Sstevel@tonic-gate * "nl_langinfo.po". After substitution, the 11060Sstevel@tonic-gate * message will appear as follows: 11070Sstevel@tonic-gate * <cmd>: overwrite <filename> (y/n)? 11080Sstevel@tonic-gate * where <cmd> is the name of the command 11090Sstevel@tonic-gate * (cp, mv) and <filename> is the destination file 11100Sstevel@tonic-gate */ 11110Sstevel@tonic-gate 11120Sstevel@tonic-gate 11130Sstevel@tonic-gate overwrite = iflg && !silent && use_stdin(); 11140Sstevel@tonic-gate override = !cpy && (access(target, 2) < 0) && 11150Sstevel@tonic-gate !silent && use_stdin() && !ISLNK(s2); 11160Sstevel@tonic-gate 11173819Sas145665 if (overwrite && override) { 11180Sstevel@tonic-gate (void) fprintf(stderr, 11190Sstevel@tonic-gate gettext("%s: overwrite %s and override " 11200Sstevel@tonic-gate "protection %o (%s/%s)? "), cmd, target, 11214774Sas145665 FMODE(s2) & MODEBITS, yesstr, nostr); 11224774Sas145665 if (yes() == 0) { 11233819Sas145665 if (buf != NULL) 11243819Sas145665 free(buf); 11253819Sas145665 return (2); 11263819Sas145665 } 11273819Sas145665 } else if (overwrite && ISREG(s2)) { 11280Sstevel@tonic-gate (void) fprintf(stderr, 11290Sstevel@tonic-gate gettext("%s: overwrite %s (%s/%s)? "), 11304774Sas145665 cmd, target, yesstr, nostr); 11314774Sas145665 if (yes() == 0) { 11323819Sas145665 if (buf != NULL) 11333819Sas145665 free(buf); 11343819Sas145665 return (2); 11353819Sas145665 } 11363819Sas145665 } else if (override) { 11370Sstevel@tonic-gate (void) fprintf(stderr, 11380Sstevel@tonic-gate gettext("%s: %s: override protection " 11390Sstevel@tonic-gate /*CSTYLED*/ 11400Sstevel@tonic-gate "%o (%s/%s)? "), 11410Sstevel@tonic-gate /*CSTYLED*/ 11420Sstevel@tonic-gate cmd, target, FMODE(s2) & MODEBITS, 11434774Sas145665 yesstr, nostr); 11444774Sas145665 if (yes() == 0) { 11453819Sas145665 if (buf != NULL) 11463819Sas145665 free(buf); 11473819Sas145665 return (2); 11480Sstevel@tonic-gate } 11490Sstevel@tonic-gate } 11503819Sas145665 11510Sstevel@tonic-gate if (lnk && unlink(target) < 0) { 11520Sstevel@tonic-gate (void) fprintf(stderr, 11530Sstevel@tonic-gate gettext("%s: cannot unlink %s: "), 11540Sstevel@tonic-gate cmd, target); 11550Sstevel@tonic-gate perror(""); 11560Sstevel@tonic-gate return (1); 11570Sstevel@tonic-gate } 11580Sstevel@tonic-gate } 11590Sstevel@tonic-gate } 11600Sstevel@tonic-gate return (0); 11610Sstevel@tonic-gate } 11620Sstevel@tonic-gate 11630Sstevel@tonic-gate /* 11640Sstevel@tonic-gate * check whether source and target are different 11650Sstevel@tonic-gate * return 1 when they are different 11660Sstevel@tonic-gate * return 0 when they are identical, or when unable to resolve a pathname 11670Sstevel@tonic-gate */ 11680Sstevel@tonic-gate static int 11690Sstevel@tonic-gate chk_different(char *source, char *target) 11700Sstevel@tonic-gate { 11710Sstevel@tonic-gate char rtarget[PATH_MAX], rsource[PATH_MAX]; 11720Sstevel@tonic-gate 11730Sstevel@tonic-gate if (IDENTICAL(s1, s2)) { 11740Sstevel@tonic-gate /* 11750Sstevel@tonic-gate * IDENTICAL will be true for hard links, therefore 11760Sstevel@tonic-gate * check whether the filenames are different 11770Sstevel@tonic-gate */ 11780Sstevel@tonic-gate if ((getrealpath(source, rsource) == 0) || 11790Sstevel@tonic-gate (getrealpath(target, rtarget) == 0)) { 11800Sstevel@tonic-gate return (0); 11810Sstevel@tonic-gate } 11820Sstevel@tonic-gate if (strncmp(rsource, rtarget, PATH_MAX) == 0) { 11830Sstevel@tonic-gate (void) fprintf(stderr, gettext( 11840Sstevel@tonic-gate "%s: %s and %s are identical\n"), 11850Sstevel@tonic-gate cmd, source, target); 11860Sstevel@tonic-gate return (0); 11870Sstevel@tonic-gate } 11880Sstevel@tonic-gate } 11890Sstevel@tonic-gate return (1); 11900Sstevel@tonic-gate } 11910Sstevel@tonic-gate 11920Sstevel@tonic-gate /* 11930Sstevel@tonic-gate * get real path (resolved absolute pathname) 11940Sstevel@tonic-gate * return 1 on success, 0 on failure 11950Sstevel@tonic-gate */ 11960Sstevel@tonic-gate static int 11970Sstevel@tonic-gate getrealpath(char *path, char *rpath) 11980Sstevel@tonic-gate { 11990Sstevel@tonic-gate if (realpath(path, rpath) == NULL) { 12000Sstevel@tonic-gate int errno_save = errno; 12010Sstevel@tonic-gate (void) fprintf(stderr, gettext( 12020Sstevel@tonic-gate "%s: can't resolve path %s: "), cmd, path); 12030Sstevel@tonic-gate errno = errno_save; 12040Sstevel@tonic-gate perror(""); 12050Sstevel@tonic-gate return (0); 12060Sstevel@tonic-gate } 12070Sstevel@tonic-gate return (1); 12080Sstevel@tonic-gate } 12090Sstevel@tonic-gate 12100Sstevel@tonic-gate static int 12110Sstevel@tonic-gate rcopy(char *from, char *to) 12120Sstevel@tonic-gate { 12130Sstevel@tonic-gate DIR *fold = opendir(from); 12140Sstevel@tonic-gate struct dirent *dp; 12150Sstevel@tonic-gate struct stat statb, s1save; 12160Sstevel@tonic-gate int errs = 0; 12170Sstevel@tonic-gate char fromname[PATH_MAX]; 12180Sstevel@tonic-gate 12190Sstevel@tonic-gate if (fold == 0 || ((pflg || mve) && fstat(fold->dd_fd, &statb) < 0)) { 12200Sstevel@tonic-gate Perror(from); 12210Sstevel@tonic-gate return (1); 12220Sstevel@tonic-gate } 12230Sstevel@tonic-gate if (pflg || mve) { 12240Sstevel@tonic-gate /* 12250Sstevel@tonic-gate * Save s1 (stat information for source dir) so that 12260Sstevel@tonic-gate * mod and access times can be reserved during "cp -p" 12270Sstevel@tonic-gate * or mv, since s1 gets overwritten. 12280Sstevel@tonic-gate */ 12290Sstevel@tonic-gate s1save = s1; 12300Sstevel@tonic-gate } 12310Sstevel@tonic-gate for (;;) { 12320Sstevel@tonic-gate dp = readdir(fold); 12330Sstevel@tonic-gate if (dp == 0) { 12340Sstevel@tonic-gate (void) closedir(fold); 12350Sstevel@tonic-gate if (pflg || mve) 12360Sstevel@tonic-gate return (chg_time(to, s1save) + errs); 12370Sstevel@tonic-gate return (errs); 12380Sstevel@tonic-gate } 12390Sstevel@tonic-gate if (dp->d_ino == 0) 12400Sstevel@tonic-gate continue; 12410Sstevel@tonic-gate if ((strcmp(dp->d_name, ".") == 0) || 12420Sstevel@tonic-gate (strcmp(dp->d_name, "..") == 0)) 12430Sstevel@tonic-gate continue; 12440Sstevel@tonic-gate if (strlen(from)+1+strlen(dp->d_name) >= 12450Sstevel@tonic-gate sizeof (fromname) - 1) { 12460Sstevel@tonic-gate (void) fprintf(stderr, 12470Sstevel@tonic-gate gettext("%s : %s/%s: Name too long\n"), 12480Sstevel@tonic-gate cmd, from, dp->d_name); 12490Sstevel@tonic-gate errs++; 12500Sstevel@tonic-gate continue; 12510Sstevel@tonic-gate } 12520Sstevel@tonic-gate (void) snprintf(fromname, sizeof (fromname), 12530Sstevel@tonic-gate "%s/%s", from, dp->d_name); 12540Sstevel@tonic-gate errs += cpymve(fromname, to); 12550Sstevel@tonic-gate } 12560Sstevel@tonic-gate } 12570Sstevel@tonic-gate 12580Sstevel@tonic-gate static char * 12590Sstevel@tonic-gate dname(char *name) 12600Sstevel@tonic-gate { 12610Sstevel@tonic-gate register char *p; 12620Sstevel@tonic-gate 12630Sstevel@tonic-gate /* 12640Sstevel@tonic-gate * Return just the file name given the complete path. 12650Sstevel@tonic-gate * Like basename(1). 12660Sstevel@tonic-gate */ 12670Sstevel@tonic-gate 12680Sstevel@tonic-gate p = name; 12690Sstevel@tonic-gate 12700Sstevel@tonic-gate /* 12710Sstevel@tonic-gate * While there are characters left, 12720Sstevel@tonic-gate * set name to start after last 12730Sstevel@tonic-gate * delimiter. 12740Sstevel@tonic-gate */ 12750Sstevel@tonic-gate 12760Sstevel@tonic-gate while (*p) 12770Sstevel@tonic-gate if (*p++ == DELIM && *p) 12780Sstevel@tonic-gate name = p; 12790Sstevel@tonic-gate return (name); 12800Sstevel@tonic-gate } 12810Sstevel@tonic-gate 12820Sstevel@tonic-gate static void 12830Sstevel@tonic-gate usage(void) 12840Sstevel@tonic-gate { 12850Sstevel@tonic-gate /* 12860Sstevel@tonic-gate * Display usage message. 12870Sstevel@tonic-gate */ 12880Sstevel@tonic-gate 12890Sstevel@tonic-gate if (mve) { 12900Sstevel@tonic-gate (void) fprintf(stderr, gettext( 12910Sstevel@tonic-gate "Usage: mv [-f] [-i] f1 f2\n" 12920Sstevel@tonic-gate " mv [-f] [-i] f1 ... fn d1\n" 12930Sstevel@tonic-gate " mv [-f] [-i] d1 d2\n")); 12940Sstevel@tonic-gate } else if (lnk) { 12954774Sas145665 #ifdef XPG4 12960Sstevel@tonic-gate (void) fprintf(stderr, gettext( 12970Sstevel@tonic-gate "Usage: ln [-f] [-s] f1 [f2]\n" 12980Sstevel@tonic-gate " ln [-f] [-s] f1 ... fn d1\n" 12990Sstevel@tonic-gate " ln [-f] -s d1 d2\n")); 13000Sstevel@tonic-gate #else 13014774Sas145665 (void) fprintf(stderr, gettext( 13020Sstevel@tonic-gate "Usage: ln [-f] [-n] [-s] f1 [f2]\n" 13030Sstevel@tonic-gate " ln [-f] [-n] [-s] f1 ... fn d1\n" 13040Sstevel@tonic-gate " ln [-f] [-n] -s d1 d2\n")); 13050Sstevel@tonic-gate #endif 13060Sstevel@tonic-gate } else if (cpy) { 13070Sstevel@tonic-gate (void) fprintf(stderr, gettext( 13085331Samw "Usage: cp [-f] [-i] [-p] [-@] [-/] f1 f2\n" 13095331Samw " cp [-f] [-i] [-p] [-@] [-/] f1 ... fn d1\n" 13105331Samw " cp -r|-R [-H|-L|-P] [-f] [-i] [-p] [-@] [-/] " 13110Sstevel@tonic-gate "d1 ... dn-1 dn\n")); 13120Sstevel@tonic-gate } 13130Sstevel@tonic-gate exit(2); 13140Sstevel@tonic-gate } 13150Sstevel@tonic-gate 13160Sstevel@tonic-gate /* 13170Sstevel@tonic-gate * chg_time() 13180Sstevel@tonic-gate * 13190Sstevel@tonic-gate * Try to preserve modification and access time. 13200Sstevel@tonic-gate * If 1) pflg is not set, or 2) pflg is set and this is the Solaris version, 13210Sstevel@tonic-gate * don't report a utime() failure. 13220Sstevel@tonic-gate * If this is the XPG4 version and utime fails, if 1) pflg is set (cp -p) 13230Sstevel@tonic-gate * or 2) we are doing a mv, print a diagnostic message; arrange for a non-zero 13240Sstevel@tonic-gate * exit status only if pflg is set. 13250Sstevel@tonic-gate * utimes(2) is being used to achieve granularity in 13260Sstevel@tonic-gate * microseconds while setting file times. 13270Sstevel@tonic-gate */ 13280Sstevel@tonic-gate static int 13290Sstevel@tonic-gate chg_time(char *to, struct stat ss) 13300Sstevel@tonic-gate { 13310Sstevel@tonic-gate struct timeval times[2]; 13320Sstevel@tonic-gate int rc; 13330Sstevel@tonic-gate 13340Sstevel@tonic-gate timestruc_to_timeval(&ss.st_atim, times); 13350Sstevel@tonic-gate timestruc_to_timeval(&ss.st_mtim, times + 1); 13360Sstevel@tonic-gate 13370Sstevel@tonic-gate rc = utimes(to, times); 13380Sstevel@tonic-gate #ifdef XPG4 13390Sstevel@tonic-gate if ((pflg || mve) && rc != 0) { 13400Sstevel@tonic-gate (void) fprintf(stderr, 13410Sstevel@tonic-gate gettext("%s: cannot set times for %s: "), cmd, to); 13420Sstevel@tonic-gate perror(""); 13430Sstevel@tonic-gate if (pflg) 13440Sstevel@tonic-gate return (1); 13450Sstevel@tonic-gate } 13460Sstevel@tonic-gate #endif 13470Sstevel@tonic-gate 13480Sstevel@tonic-gate return (0); 13490Sstevel@tonic-gate 13500Sstevel@tonic-gate } 13510Sstevel@tonic-gate 13520Sstevel@tonic-gate /* 13530Sstevel@tonic-gate * chg_mode() 13540Sstevel@tonic-gate * 13550Sstevel@tonic-gate * This function is called upon "cp -p" or mv across filesystems. 13560Sstevel@tonic-gate * 13570Sstevel@tonic-gate * Try to preserve the owner and group id. If chown() fails, 13580Sstevel@tonic-gate * only print a diagnostic message if doing a mv in the XPG4 version; 13590Sstevel@tonic-gate * try to clear S_ISUID and S_ISGID bits in the target. If unable to clear 13600Sstevel@tonic-gate * S_ISUID and S_ISGID bits, print a diagnostic message and arrange for a 13610Sstevel@tonic-gate * non-zero exit status because this is a security violation. 13620Sstevel@tonic-gate * Try to preserve permissions. 13630Sstevel@tonic-gate * If this is the XPG4 version and chmod() fails, print a diagnostic message 13640Sstevel@tonic-gate * and arrange for a non-zero exit status. 13650Sstevel@tonic-gate * If this is the Solaris version and chmod() fails, do not print a 13660Sstevel@tonic-gate * diagnostic message or exit with a non-zero value. 13670Sstevel@tonic-gate */ 13680Sstevel@tonic-gate static int 13690Sstevel@tonic-gate chg_mode(char *target, uid_t uid, gid_t gid, mode_t mode) 13700Sstevel@tonic-gate { 13710Sstevel@tonic-gate int clearflg = 0; /* controls message printed upon chown() error */ 13720Sstevel@tonic-gate 13730Sstevel@tonic-gate if (chown(target, uid, gid) != 0) { 13740Sstevel@tonic-gate #ifdef XPG4 13750Sstevel@tonic-gate if (mve) { 13760Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s: cannot change" 13770Sstevel@tonic-gate " owner and group of %s: "), cmd, target); 13780Sstevel@tonic-gate perror(""); 13790Sstevel@tonic-gate } 13800Sstevel@tonic-gate #endif 13810Sstevel@tonic-gate if (mode & (S_ISUID | S_ISGID)) { 13820Sstevel@tonic-gate /* try to clear S_ISUID and S_ISGID */ 13830Sstevel@tonic-gate mode &= ~S_ISUID & ~S_ISGID; 13840Sstevel@tonic-gate ++clearflg; 13850Sstevel@tonic-gate } 13860Sstevel@tonic-gate } 13870Sstevel@tonic-gate if (chmod(target, mode) != 0) { 13880Sstevel@tonic-gate if (clearflg) { 13890Sstevel@tonic-gate (void) fprintf(stderr, gettext( 13900Sstevel@tonic-gate "%s: cannot clear S_ISUID and S_ISGID bits in" 13910Sstevel@tonic-gate " %s: "), cmd, target); 13920Sstevel@tonic-gate perror(""); 13930Sstevel@tonic-gate /* cp -p should get non-zero exit; mv should not */ 13940Sstevel@tonic-gate if (pflg) 13950Sstevel@tonic-gate return (1); 13960Sstevel@tonic-gate } 13970Sstevel@tonic-gate #ifdef XPG4 13980Sstevel@tonic-gate else { 13990Sstevel@tonic-gate (void) fprintf(stderr, gettext( 14000Sstevel@tonic-gate "%s: cannot set permissions for %s: "), cmd, target); 14010Sstevel@tonic-gate perror(""); 14020Sstevel@tonic-gate /* cp -p should get non-zero exit; mv should not */ 14030Sstevel@tonic-gate if (pflg) 14040Sstevel@tonic-gate return (1); 14050Sstevel@tonic-gate } 14060Sstevel@tonic-gate #endif 14070Sstevel@tonic-gate } 14080Sstevel@tonic-gate return (0); 14090Sstevel@tonic-gate 14100Sstevel@tonic-gate } 14110Sstevel@tonic-gate 14120Sstevel@tonic-gate static void 14130Sstevel@tonic-gate Perror(char *s) 14140Sstevel@tonic-gate { 14150Sstevel@tonic-gate char buf[PATH_MAX + 10]; 14160Sstevel@tonic-gate 14170Sstevel@tonic-gate (void) snprintf(buf, sizeof (buf), "%s: %s", cmd, s); 14180Sstevel@tonic-gate perror(buf); 14190Sstevel@tonic-gate } 14200Sstevel@tonic-gate 14210Sstevel@tonic-gate static void 14220Sstevel@tonic-gate Perror2(char *s1, char *s2) 14230Sstevel@tonic-gate { 14240Sstevel@tonic-gate char buf[PATH_MAX + 20]; 14250Sstevel@tonic-gate 14260Sstevel@tonic-gate (void) snprintf(buf, sizeof (buf), "%s: %s: %s", 14270Sstevel@tonic-gate cmd, gettext(s1), gettext(s2)); 14280Sstevel@tonic-gate perror(buf); 14290Sstevel@tonic-gate } 14300Sstevel@tonic-gate 14310Sstevel@tonic-gate /* 14320Sstevel@tonic-gate * used for cp -R and for mv across file systems 14330Sstevel@tonic-gate */ 14340Sstevel@tonic-gate static int 14350Sstevel@tonic-gate copydir(char *source, char *target) 14360Sstevel@tonic-gate { 14370Sstevel@tonic-gate int ret, attret = 0; 1438*5374Sbasabi int sattret = 0; 14390Sstevel@tonic-gate int pret = 0; /* need separate flag if -p is specified */ 14400Sstevel@tonic-gate mode_t fixmode = (mode_t)0; /* cleanup mode after copy */ 14410Sstevel@tonic-gate struct stat s1save; 1442789Sahrens acl_t *s1acl_save; 1443*5374Sbasabi int error = 0; 1444789Sahrens 1445789Sahrens s1acl_save = NULL; 14460Sstevel@tonic-gate 14470Sstevel@tonic-gate if (cpy && !rflg) { 14480Sstevel@tonic-gate (void) fprintf(stderr, 14490Sstevel@tonic-gate gettext("%s: %s: is a directory\n"), cmd, source); 14500Sstevel@tonic-gate return (1); 14510Sstevel@tonic-gate } 14520Sstevel@tonic-gate 14530Sstevel@tonic-gate if (stat(target, &s2) < 0) { 14540Sstevel@tonic-gate if (mkdir(target, (s1.st_mode & MODEBITS)) < 0) { 14550Sstevel@tonic-gate (void) fprintf(stderr, "%s: ", cmd); 14560Sstevel@tonic-gate perror(target); 14570Sstevel@tonic-gate return (1); 14580Sstevel@tonic-gate } 14590Sstevel@tonic-gate if (stat(target, &s2) == 0) { 14600Sstevel@tonic-gate fixmode = s2.st_mode; 14610Sstevel@tonic-gate } else { 14620Sstevel@tonic-gate fixmode = s1.st_mode; 14630Sstevel@tonic-gate } 14640Sstevel@tonic-gate (void) chmod(target, ((fixmode & MODEBITS) | S_IRWXU)); 14650Sstevel@tonic-gate } else if (!(ISDIR(s2))) { 14660Sstevel@tonic-gate (void) fprintf(stderr, 14670Sstevel@tonic-gate gettext("%s: %s: not a directory.\n"), cmd, target); 14680Sstevel@tonic-gate return (1); 14690Sstevel@tonic-gate } 14700Sstevel@tonic-gate if (pflg || mve) { 14710Sstevel@tonic-gate /* 14720Sstevel@tonic-gate * Save s1 (stat information for source dir) and acl info, 14730Sstevel@tonic-gate * if any, so that ownership, modes, times, and acl's can 14740Sstevel@tonic-gate * be reserved during "cp -p" or mv. 14750Sstevel@tonic-gate * s1 gets overwritten when doing the recursive copy. 14760Sstevel@tonic-gate */ 14770Sstevel@tonic-gate s1save = s1; 1478789Sahrens if (s1acl != NULL) { 1479789Sahrens s1acl_save = acl_dup(s1acl); 1480789Sahrens if (s1acl_save == NULL) { 1481789Sahrens (void) fprintf(stderr, gettext("%s: " 1482789Sahrens "Insufficient memory to save acl" 1483789Sahrens " entry\n"), cmd); 1484789Sahrens if (pflg) 1485789Sahrens return (1); 1486789Sahrens 14870Sstevel@tonic-gate } 14884774Sas145665 #ifdef XPG4 14894774Sas145665 else { 14904774Sas145665 (void) fprintf(stderr, gettext("%s: " 14914774Sas145665 "Insufficient memory to save acl" 14924774Sas145665 " entry\n"), cmd); 14934774Sas145665 if (pflg) 14944774Sas145665 return (1); 14954774Sas145665 } 14960Sstevel@tonic-gate #endif 14970Sstevel@tonic-gate } 14980Sstevel@tonic-gate } 14990Sstevel@tonic-gate 15000Sstevel@tonic-gate ret = rcopy(source, target); 15010Sstevel@tonic-gate 15020Sstevel@tonic-gate /* 15030Sstevel@tonic-gate * Once we created a directory, go ahead and set 15040Sstevel@tonic-gate * its attributes, e.g. acls and time. The info 15050Sstevel@tonic-gate * may get overwritten if we continue traversing 15060Sstevel@tonic-gate * down the tree. 15070Sstevel@tonic-gate * 15080Sstevel@tonic-gate * ACL for directory 15090Sstevel@tonic-gate */ 15100Sstevel@tonic-gate if (pflg || mve) { 15111231Smarks if ((pret = chg_mode(target, UID(s1save), GID(s1save), 15121231Smarks FMODE(s1save))) == 0) 15131231Smarks pret = chg_time(target, s1save); 15141231Smarks ret += pret; 1515789Sahrens if (s1acl_save != NULL) { 1516789Sahrens if (acl_set(target, s1acl_save) < 0) { 1517*5374Sbasabi error++; 15180Sstevel@tonic-gate #ifdef XPG4 15190Sstevel@tonic-gate if (pflg || mve) { 15200Sstevel@tonic-gate #else 15210Sstevel@tonic-gate if (pflg) { 15220Sstevel@tonic-gate #endif 15230Sstevel@tonic-gate (void) fprintf(stderr, gettext( 15240Sstevel@tonic-gate "%s: failed to set acl entries " 15250Sstevel@tonic-gate "on %s\n"), cmd, target); 15260Sstevel@tonic-gate if (pflg) { 1527789Sahrens acl_free(s1acl_save); 1528789Sahrens s1acl_save = NULL; 15290Sstevel@tonic-gate ret++; 15300Sstevel@tonic-gate } 15310Sstevel@tonic-gate } 15320Sstevel@tonic-gate /* else: silent and continue */ 15330Sstevel@tonic-gate } 1534789Sahrens acl_free(s1acl_save); 1535789Sahrens s1acl_save = NULL; 15360Sstevel@tonic-gate } 15370Sstevel@tonic-gate } else if (fixmode != (mode_t)0) 15380Sstevel@tonic-gate (void) chmod(target, fixmode & MODEBITS); 15390Sstevel@tonic-gate 15405331Samw if (pflg || atflg || mve || saflg) { 15410Sstevel@tonic-gate attret = copyattributes(source, target); 15420Sstevel@tonic-gate if (!attrsilent && attret != 0) { 15430Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s: Failed to preserve" 15440Sstevel@tonic-gate " extended attributes of directory" 15450Sstevel@tonic-gate " %s\n"), cmd, source); 15460Sstevel@tonic-gate } else { 15470Sstevel@tonic-gate /* 15480Sstevel@tonic-gate * Otherwise ignore failure. 15490Sstevel@tonic-gate */ 15500Sstevel@tonic-gate attret = 0; 15510Sstevel@tonic-gate } 15525331Samw /* Copy extended system attributes */ 15535331Samw if (pflg || mve || saflg) { 1554*5374Sbasabi sattret = copy_sysattr(source, target); 1555*5374Sbasabi if (sattret != 0) { 15565331Samw (void) fprintf(stderr, gettext( 15575331Samw "%s: Failed to preserve " 15585331Samw "extended system attributes " 15595331Samw "of directory %s\n"), cmd, source); 15605331Samw } 15615331Samw } 15620Sstevel@tonic-gate } 1563*5374Sbasabi if (attret != 0 || sattret != 0 || error != 0) 1564*5374Sbasabi return (1); 15650Sstevel@tonic-gate return (ret); 15660Sstevel@tonic-gate } 15670Sstevel@tonic-gate 15680Sstevel@tonic-gate static int 15690Sstevel@tonic-gate copyspecial(char *target) 15700Sstevel@tonic-gate { 15710Sstevel@tonic-gate int ret = 0; 15720Sstevel@tonic-gate 15730Sstevel@tonic-gate if (mknod(target, s1.st_mode, s1.st_rdev) != 0) { 15740Sstevel@tonic-gate (void) fprintf(stderr, gettext( 15750Sstevel@tonic-gate "cp: cannot create special file %s: "), target); 15760Sstevel@tonic-gate perror(""); 15770Sstevel@tonic-gate return (1); 15780Sstevel@tonic-gate } 15790Sstevel@tonic-gate 15800Sstevel@tonic-gate if (pflg) { 15810Sstevel@tonic-gate if ((ret = chg_mode(target, UID(s1), GID(s1), FMODE(s1))) == 0) 15820Sstevel@tonic-gate ret = chg_time(target, s1); 15830Sstevel@tonic-gate } 15840Sstevel@tonic-gate 15850Sstevel@tonic-gate return (ret); 15860Sstevel@tonic-gate } 15870Sstevel@tonic-gate 15880Sstevel@tonic-gate static int 15890Sstevel@tonic-gate use_stdin(void) 15900Sstevel@tonic-gate { 15910Sstevel@tonic-gate #ifdef XPG4 15920Sstevel@tonic-gate return (1); 15930Sstevel@tonic-gate #else 15940Sstevel@tonic-gate return (isatty(fileno(stdin))); 15950Sstevel@tonic-gate #endif 15960Sstevel@tonic-gate } 15970Sstevel@tonic-gate 15985331Samw /* Copy non-system extended attributes */ 15995331Samw 16000Sstevel@tonic-gate static int 16010Sstevel@tonic-gate copyattributes(char *source, char *target) 16020Sstevel@tonic-gate { 16030Sstevel@tonic-gate struct dirent *dp; 16040Sstevel@tonic-gate char *srcbuf, *targbuf; 16050Sstevel@tonic-gate int error = 0; 1606789Sahrens int aclerror; 16070Sstevel@tonic-gate mode_t mode; 16080Sstevel@tonic-gate int clearflg = 0; 1609789Sahrens acl_t *xacl = NULL; 1610789Sahrens acl_t *attrdiracl = NULL; 16110Sstevel@tonic-gate struct timeval times[2]; 16120Sstevel@tonic-gate 16130Sstevel@tonic-gate srcbuf = targbuf = NULL; 16140Sstevel@tonic-gate 16155331Samw 16165331Samw if (pathconf(source, _PC_XATTR_EXISTS) != 1) 16170Sstevel@tonic-gate return (0); 16180Sstevel@tonic-gate 16190Sstevel@tonic-gate if (pathconf(target, _PC_XATTR_ENABLED) != 1) { 16200Sstevel@tonic-gate if (!attrsilent) { 16210Sstevel@tonic-gate (void) fprintf(stderr, 16220Sstevel@tonic-gate gettext( 16230Sstevel@tonic-gate "%s: cannot preserve extended attributes, " 16240Sstevel@tonic-gate "operation not supported on file" 16250Sstevel@tonic-gate " %s\n"), cmd, target); 16260Sstevel@tonic-gate } 16270Sstevel@tonic-gate return (1); 16280Sstevel@tonic-gate } 16295331Samw if (open_source(source) != 0) 16305331Samw return (1); 16315331Samw if (open_target_srctarg_attrdirs(source, target) != 0) 16325331Samw return (1); 16335331Samw if (open_attrdirp(source) != 0) 16345331Samw return (1); 16350Sstevel@tonic-gate 16360Sstevel@tonic-gate if (pflg || mve) { 16370Sstevel@tonic-gate if (fchmod(targetdirfd, attrdir.st_mode) == -1) { 16380Sstevel@tonic-gate if (!attrsilent) { 16390Sstevel@tonic-gate (void) fprintf(stderr, 16404774Sas145665 gettext("%s: failed to set file mode" 16414774Sas145665 " correctly on attribute directory of" 16424774Sas145665 " file %s: "), cmd, target); 16430Sstevel@tonic-gate perror(""); 16440Sstevel@tonic-gate ++error; 16450Sstevel@tonic-gate } 16460Sstevel@tonic-gate } 16470Sstevel@tonic-gate 16480Sstevel@tonic-gate if (fchown(targetdirfd, attrdir.st_uid, attrdir.st_gid) == -1) { 16490Sstevel@tonic-gate if (!attrsilent) { 16500Sstevel@tonic-gate (void) fprintf(stderr, 16510Sstevel@tonic-gate gettext("%s: failed to set file" 16520Sstevel@tonic-gate " ownership correctly on attribute" 16530Sstevel@tonic-gate " directory of file %s: "), cmd, target); 16540Sstevel@tonic-gate perror(""); 16550Sstevel@tonic-gate ++error; 16560Sstevel@tonic-gate } 16570Sstevel@tonic-gate } 16580Sstevel@tonic-gate /* 16590Sstevel@tonic-gate * Now that we are the owner we can update st_ctime by calling 16600Sstevel@tonic-gate * futimesat. 16610Sstevel@tonic-gate */ 16620Sstevel@tonic-gate times[0].tv_sec = attrdir.st_atime; 16630Sstevel@tonic-gate times[0].tv_usec = 0; 16640Sstevel@tonic-gate times[1].tv_sec = attrdir.st_mtime; 16650Sstevel@tonic-gate times[1].tv_usec = 0; 16660Sstevel@tonic-gate if (futimesat(targetdirfd, ".", times) < 0) { 16670Sstevel@tonic-gate if (!attrsilent) { 16680Sstevel@tonic-gate (void) fprintf(stderr, 16694774Sas145665 gettext("%s: cannot set attribute times" 16704774Sas145665 " for %s: "), cmd, target); 16710Sstevel@tonic-gate perror(""); 16720Sstevel@tonic-gate ++error; 16730Sstevel@tonic-gate } 16740Sstevel@tonic-gate } 16750Sstevel@tonic-gate 16760Sstevel@tonic-gate /* 16770Sstevel@tonic-gate * Now set owner and group of attribute directory, implies 16780Sstevel@tonic-gate * changing the ACL of the hidden attribute directory first. 16790Sstevel@tonic-gate */ 1680789Sahrens if ((aclerror = facl_get(sourcedirfd, 1681789Sahrens ACL_NO_TRIVIAL, &attrdiracl)) != 0) { 16820Sstevel@tonic-gate if (!attrsilent) { 16830Sstevel@tonic-gate (void) fprintf(stderr, gettext( 16840Sstevel@tonic-gate "%s: failed to get acl entries of" 16850Sstevel@tonic-gate " attribute directory for" 1686789Sahrens " %s : %s\n"), cmd, 1687789Sahrens source, acl_strerror(aclerror)); 16880Sstevel@tonic-gate ++error; 16890Sstevel@tonic-gate } 16900Sstevel@tonic-gate } 1691789Sahrens 1692789Sahrens if (attrdiracl) { 1693789Sahrens if (facl_set(targetdirfd, attrdiracl) != 0) { 16940Sstevel@tonic-gate if (!attrsilent) { 16950Sstevel@tonic-gate (void) fprintf(stderr, gettext( 1696789Sahrens "%s: failed to set acl entries" 1697789Sahrens " on attribute directory " 1698789Sahrens "for %s\n"), cmd, target); 16990Sstevel@tonic-gate ++error; 17000Sstevel@tonic-gate } 1701789Sahrens acl_free(attrdiracl); 1702789Sahrens attrdiracl = NULL; 17030Sstevel@tonic-gate } 17040Sstevel@tonic-gate } 17050Sstevel@tonic-gate } 17060Sstevel@tonic-gate 17075331Samw while ((dp = readdir(srcdirp)) != NULL) { 17085331Samw int ret; 17090Sstevel@tonic-gate 17105331Samw if ((ret = traverse_attrfile(dp, source, target, 1)) == -1) 17115331Samw continue; 17125331Samw else if (ret > 0) { 17130Sstevel@tonic-gate ++error; 17145331Samw goto out; 17150Sstevel@tonic-gate } 17160Sstevel@tonic-gate 17170Sstevel@tonic-gate if (pflg || mve) { 1718789Sahrens if ((aclerror = facl_get(srcattrfd, 1719789Sahrens ACL_NO_TRIVIAL, &xacl)) != 0) { 17200Sstevel@tonic-gate if (!attrsilent) { 17210Sstevel@tonic-gate (void) fprintf(stderr, gettext( 17220Sstevel@tonic-gate "%s: failed to get acl entries of" 17230Sstevel@tonic-gate " attribute %s for" 1724789Sahrens " %s: %s"), cmd, dp->d_name, 1725789Sahrens source, acl_strerror(aclerror)); 17260Sstevel@tonic-gate ++error; 17270Sstevel@tonic-gate } 17280Sstevel@tonic-gate } 17290Sstevel@tonic-gate } 17300Sstevel@tonic-gate 17310Sstevel@tonic-gate /* 17320Sstevel@tonic-gate * preserve ACL 17330Sstevel@tonic-gate */ 1734789Sahrens if ((pflg || mve) && xacl != NULL) { 1735789Sahrens if ((facl_set(targattrfd, xacl)) < 0) { 17360Sstevel@tonic-gate if (!attrsilent) { 17370Sstevel@tonic-gate (void) fprintf(stderr, gettext( 17380Sstevel@tonic-gate "%s: failed to set acl entries on" 17390Sstevel@tonic-gate " attribute %s for" 17400Sstevel@tonic-gate "%s\n"), cmd, dp->d_name, target); 17410Sstevel@tonic-gate ++error; 17420Sstevel@tonic-gate } 1743789Sahrens acl_free(xacl); 1744789Sahrens xacl = NULL; 17450Sstevel@tonic-gate } 17460Sstevel@tonic-gate } 17470Sstevel@tonic-gate 17485331Samw if (writefile(srcattrfd, targattrfd, source, target, 17495331Samw dp->d_name, dp->d_name, &s3, &s4) != 0) { 17500Sstevel@tonic-gate if (!attrsilent) { 17510Sstevel@tonic-gate ++error; 17520Sstevel@tonic-gate } 17530Sstevel@tonic-gate goto next; 17540Sstevel@tonic-gate } 17550Sstevel@tonic-gate 17560Sstevel@tonic-gate if (pflg || mve) { 17570Sstevel@tonic-gate mode = FMODE(s3); 17580Sstevel@tonic-gate 17590Sstevel@tonic-gate if (fchown(targattrfd, UID(s3), GID(s3)) != 0) { 17600Sstevel@tonic-gate if (!attrsilent) { 17610Sstevel@tonic-gate (void) fprintf(stderr, 17620Sstevel@tonic-gate gettext("%s: cannot change" 17630Sstevel@tonic-gate " owner and group of" 17640Sstevel@tonic-gate " attribute %s for" " file" 17650Sstevel@tonic-gate " %s: "), cmd, dp->d_name, target); 17660Sstevel@tonic-gate perror(""); 17670Sstevel@tonic-gate ++error; 17680Sstevel@tonic-gate } 17690Sstevel@tonic-gate if (mode & (S_ISUID | S_ISGID)) { 17700Sstevel@tonic-gate /* try to clear S_ISUID and S_ISGID */ 17710Sstevel@tonic-gate mode &= ~S_ISUID & ~S_ISGID; 17720Sstevel@tonic-gate ++clearflg; 17730Sstevel@tonic-gate } 17740Sstevel@tonic-gate } 17750Sstevel@tonic-gate /* tv_usec were cleared above */ 17760Sstevel@tonic-gate times[0].tv_sec = s3.st_atime; 17770Sstevel@tonic-gate times[1].tv_sec = s3.st_mtime; 17780Sstevel@tonic-gate if (futimesat(targetdirfd, dp->d_name, times) < 0) { 17790Sstevel@tonic-gate if (!attrsilent) { 17800Sstevel@tonic-gate (void) fprintf(stderr, 17810Sstevel@tonic-gate gettext("%s: cannot set attribute" 17820Sstevel@tonic-gate " times for %s: "), cmd, target); 17830Sstevel@tonic-gate perror(""); 17840Sstevel@tonic-gate ++error; 17850Sstevel@tonic-gate } 17860Sstevel@tonic-gate } 17870Sstevel@tonic-gate if (fchmod(targattrfd, mode) != 0) { 17880Sstevel@tonic-gate if (clearflg) { 17890Sstevel@tonic-gate (void) fprintf(stderr, gettext( 17900Sstevel@tonic-gate "%s: cannot clear S_ISUID and " 17910Sstevel@tonic-gate "S_ISGID bits in attribute %s" 17920Sstevel@tonic-gate " for file" 17930Sstevel@tonic-gate " %s: "), cmd, dp->d_name, target); 17940Sstevel@tonic-gate } else { 17950Sstevel@tonic-gate if (!attrsilent) { 17960Sstevel@tonic-gate (void) fprintf(stderr, 17974774Sas145665 gettext( 17980Sstevel@tonic-gate "%s: cannot set permissions of attribute" 17990Sstevel@tonic-gate " %s for %s: "), cmd, dp->d_name, target); 18000Sstevel@tonic-gate perror(""); 18010Sstevel@tonic-gate ++error; 18020Sstevel@tonic-gate } 18030Sstevel@tonic-gate } 18040Sstevel@tonic-gate } 18051231Smarks if (xacl && ((facl_set(targattrfd, xacl)) < 0)) { 18061231Smarks if (!attrsilent) { 18071231Smarks (void) fprintf(stderr, gettext( 18081231Smarks "%s: failed to set acl entries on" 18091231Smarks " attribute %s for" 18101231Smarks "%s\n"), cmd, dp->d_name, target); 18111231Smarks ++error; 18121231Smarks } 18131231Smarks acl_free(xacl); 18141231Smarks xacl = NULL; 18151231Smarks } 18160Sstevel@tonic-gate } 18170Sstevel@tonic-gate next: 1818789Sahrens if (xacl != NULL) { 1819789Sahrens acl_free(xacl); 1820789Sahrens xacl = NULL; 18210Sstevel@tonic-gate } 18220Sstevel@tonic-gate if (srcbuf != NULL) 18230Sstevel@tonic-gate free(srcbuf); 18240Sstevel@tonic-gate if (targbuf != NULL) 18250Sstevel@tonic-gate free(targbuf); 18260Sstevel@tonic-gate if (srcattrfd != -1) 18270Sstevel@tonic-gate (void) close(srcattrfd); 18280Sstevel@tonic-gate if (targattrfd != -1) 18290Sstevel@tonic-gate (void) close(targattrfd); 18300Sstevel@tonic-gate srcattrfd = targattrfd = -1; 18310Sstevel@tonic-gate srcbuf = targbuf = NULL; 18320Sstevel@tonic-gate } 18330Sstevel@tonic-gate out: 1834789Sahrens if (xacl != NULL) { 1835789Sahrens acl_free(xacl); 1836789Sahrens xacl = NULL; 1837789Sahrens } 1838789Sahrens if (attrdiracl != NULL) { 1839789Sahrens acl_free(attrdiracl); 1840789Sahrens attrdiracl = NULL; 1841789Sahrens } 18420Sstevel@tonic-gate if (srcbuf) 18430Sstevel@tonic-gate free(srcbuf); 18440Sstevel@tonic-gate if (targbuf) 18450Sstevel@tonic-gate free(targbuf); 18465331Samw 18475331Samw if (!saflg && !pflg && !mve) 18485331Samw close_all(); 18495331Samw return (error == 0 ? 0 : 1); 18505331Samw } 18515331Samw 18525331Samw /* Copy extended system attributes from source to target */ 18535331Samw 18545331Samw static int 18555331Samw copy_sysattr(char *source, char *target) 18565331Samw { 18575331Samw struct dirent *dp; 18585331Samw nvlist_t *response; 18595331Samw int error = 0; 18605331Samw int chk_support = 0; 18615331Samw int sa_support = 1; 18625331Samw 18635331Samw if (sysattr_support(source, _PC_SATTR_EXISTS) != 1) 18645331Samw return (0); 18655331Samw 18665331Samw if (open_source(source) != 0) 18675331Samw return (1); 18685331Samw 18695331Samw /* 18705331Samw * Gets non default extended system attributes from the 18715331Samw * source file to copy to the target. The target has 18725331Samw * the defaults set when its created and thus no need 18735331Samw * to copy the defaults. 18745331Samw */ 18755331Samw response = sysattr_list(cmd, srcfd, source); 18765331Samw 18775331Samw if (response != NULL && 18785331Samw sysattr_support(target, _PC_SATTR_ENABLED) != 1) { 1879*5374Sbasabi (void) fprintf(stderr, 1880*5374Sbasabi gettext( 1881*5374Sbasabi "%s: cannot preserve extended system " 1882*5374Sbasabi "attribute, operation not supported on file" 1883*5374Sbasabi " %s\n"), cmd, target); 18845331Samw error++; 18855331Samw goto out; 18865331Samw } 18875331Samw 18885331Samw if (srcdirp == NULL) { 18895331Samw if (open_target_srctarg_attrdirs(source, target) != 0) { 18905331Samw error++; 18915331Samw goto out; 18925331Samw } 18935331Samw if (open_attrdirp(source) != 0) { 18945331Samw error++; 18955331Samw goto out; 18965331Samw } 18975331Samw } else { 18985331Samw rewind_attrdir(srcdirp); 18995331Samw } 19005331Samw while (sa_support && (dp = readdir(srcdirp)) != NULL) { 19015331Samw nvlist_t *res; 19025331Samw int ret; 19035331Samw 19045331Samw if ((ret = traverse_attrfile(dp, source, target, 0)) == -1) 19055331Samw continue; 19065331Samw else if (ret > 0) { 19075331Samw ++error; 19085331Samw goto out; 19095331Samw } 19105331Samw /* 19115331Samw * Gets non default extended system attributes from the 19125331Samw * attribute file to copy to the target. The target has 19135331Samw * the defaults set when its created and thus no need 19145331Samw * to copy the defaults. 19155331Samw */ 19165331Samw if (dp->d_name != NULL) { 19175331Samw res = sysattr_list(cmd, srcattrfd, dp->d_name); 19185331Samw if (res == NULL) 19195331Samw goto next; 19205331Samw 19215331Samw if (!chk_support && 19225331Samw sysattr_support(target, _PC_SATTR_ENABLED) != 1) { 1923*5374Sbasabi (void) fprintf(stderr, 1924*5374Sbasabi gettext( 1925*5374Sbasabi "%s: cannot preserve extended " 1926*5374Sbasabi "system attribute, operation not " 1927*5374Sbasabi " %s\n"), cmd, target); 19285331Samw error++; 19295331Samw sa_support = 0; 19305331Samw } else { 19315331Samw chk_support = 1; 19325331Samw } 19335331Samw 19345331Samw /* 19355331Samw * Copy non default extended system attributes of named 19365331Samw * attribute file. 19375331Samw */ 19385331Samw if (sa_support && fsetattr(targattrfd, 19395331Samw XATTR_VIEW_READWRITE, res) != 0) { 19405331Samw ++error; 1941*5374Sbasabi (void) fprintf(stderr, gettext("%s: " 1942*5374Sbasabi "Failed to copy extended system " 1943*5374Sbasabi "attributes from attribute file " 1944*5374Sbasabi "%s of %s to %s\n"), cmd, 1945*5374Sbasabi dp->d_name, source, target); 19465331Samw } 19475331Samw } 19485331Samw next: 19495331Samw if (srcattrfd != -1) 19505331Samw (void) close(srcattrfd); 19515331Samw if (targattrfd != -1) 19525331Samw (void) close(targattrfd); 19535331Samw srcattrfd = targattrfd = -1; 19545331Samw if (res != NULL) 19555331Samw nvlist_free(res); 19565331Samw } 19575331Samw 19585331Samw /* Copy source file non default extended system attributes to target */ 19595331Samw if ((response != NULL) && 19605331Samw (fsetattr(targfd, XATTR_VIEW_READWRITE, response)) != 0) { 19615331Samw ++error; 1962*5374Sbasabi (void) fprintf(stderr, gettext("%s: Failed to " 1963*5374Sbasabi "copy extended system attributes from " 1964*5374Sbasabi "%s to %s\n"), cmd, source, target); 19655331Samw } 19665331Samw out: 19675331Samw if (response != NULL) 19685331Samw nvlist_free(response); 19695331Samw close_all(); 19700Sstevel@tonic-gate return (error == 0 ? 0 : 1); 19710Sstevel@tonic-gate } 19720Sstevel@tonic-gate 19730Sstevel@tonic-gate /* 19740Sstevel@tonic-gate * nanoseconds are rounded off to microseconds by flooring. 19750Sstevel@tonic-gate */ 19760Sstevel@tonic-gate static void 19770Sstevel@tonic-gate timestruc_to_timeval(timestruc_t *ts, struct timeval *tv) 19780Sstevel@tonic-gate { 19790Sstevel@tonic-gate tv->tv_sec = ts->tv_sec; 19800Sstevel@tonic-gate tv->tv_usec = ts->tv_nsec / 1000; 19810Sstevel@tonic-gate } 19825331Samw 19835331Samw /* Open the source file */ 19845331Samw 19855331Samw int 19865331Samw open_source(char *src) 19875331Samw { 19885331Samw int error = 0; 19895331Samw 19905331Samw srcfd = -1; 19915331Samw if ((srcfd = open(src, O_RDONLY)) == -1) { 19925331Samw if (pflg && attrsilent) { 19935331Samw error++; 19945331Samw goto out; 19955331Samw } 19965331Samw if (!attrsilent) { 19975331Samw (void) fprintf(stderr, 19985331Samw gettext("%s: cannot open file" 19995331Samw " %s: "), cmd, src); 20005331Samw perror(""); 20015331Samw } 20025331Samw ++error; 20035331Samw } 20045331Samw out: 20055331Samw if (error) 20065331Samw close_all(); 20075331Samw return (error == 0 ? 0 : 1); 20085331Samw } 20095331Samw 20105331Samw /* Open source attribute dir, target and target attribute dir. */ 20115331Samw 20125331Samw int 20135331Samw open_target_srctarg_attrdirs(char *src, char *targ) 20145331Samw { 20155331Samw int error = 0; 20165331Samw 20175331Samw targfd = sourcedirfd = targetdirfd = -1; 20185331Samw 20195331Samw if ((targfd = open(targ, O_RDONLY)) == -1) { 20205331Samw if (pflg && attrsilent) { 20215331Samw error++; 20225331Samw goto out; 20235331Samw } 20245331Samw if (!attrsilent) { 20255331Samw (void) fprintf(stderr, 20265331Samw gettext("%s: cannot open file" 20275331Samw " %s: "), cmd, targ); 20285331Samw perror(""); 20295331Samw } 20305331Samw ++error; 20315331Samw goto out; 20325331Samw } 20335331Samw 20345331Samw if ((sourcedirfd = openat(srcfd, ".", O_RDONLY|O_XATTR)) == -1) { 20355331Samw if (pflg && attrsilent) { 20365331Samw error++; 20375331Samw goto out; 20385331Samw } 20395331Samw if (!attrsilent) { 20405331Samw (void) fprintf(stderr, 20415331Samw gettext("%s: cannot open attribute" 20425331Samw " directory for %s: "), cmd, src); 20435331Samw perror(""); 20445331Samw } 20455331Samw ++error; 20465331Samw goto out; 20475331Samw } 20485331Samw 20495331Samw if (fstat(sourcedirfd, &attrdir) == -1) { 20505331Samw if (pflg && attrsilent) { 20515331Samw error++; 20525331Samw goto out; 20535331Samw } 20545331Samw 20555331Samw if (!attrsilent) { 20565331Samw (void) fprintf(stderr, 20575331Samw gettext("%s: could not retrieve stat" 20585331Samw " information for attribute directory" 20595331Samw "of file %s: "), cmd, src); 20605331Samw perror(""); 20615331Samw } 20625331Samw ++error; 20635331Samw goto out; 20645331Samw } 20655331Samw if ((targetdirfd = openat(targfd, ".", O_RDONLY|O_XATTR)) == -1) { 20665331Samw if (pflg && attrsilent) { 20675331Samw error++; 20685331Samw goto out; 20695331Samw } 20705331Samw if (!attrsilent) { 20715331Samw (void) fprintf(stderr, 20725331Samw gettext("%s: cannot open attribute" 20735331Samw " directory for %s: "), cmd, targ); 20745331Samw perror(""); 20755331Samw } 20765331Samw ++error; 20775331Samw } 20785331Samw out: 20795331Samw if (error) 20805331Samw close_all(); 20815331Samw return (error == 0 ? 0 : 1); 20825331Samw } 20835331Samw 20845331Samw int 20855331Samw open_attrdirp(char *source) 20865331Samw { 20875331Samw int tmpfd = -1; 20885331Samw int error = 0; 20895331Samw 20905331Samw /* 20915331Samw * dup sourcedirfd for use by fdopendir(). 20925331Samw * fdopendir will take ownership of given fd and will close 20935331Samw * it when closedir() is called. 20945331Samw */ 20955331Samw 20965331Samw if ((tmpfd = dup(sourcedirfd)) == -1) { 20975331Samw if (pflg && attrsilent) { 20985331Samw error++; 20995331Samw goto out; 21005331Samw } 21015331Samw if (!attrsilent) { 21025331Samw (void) fprintf(stderr, 21035331Samw gettext( 21045331Samw "%s: unable to dup attribute directory" 21055331Samw " file descriptor for %s: "), cmd, source); 21065331Samw perror(""); 21075331Samw ++error; 21085331Samw } 21095331Samw goto out; 21105331Samw } 21115331Samw if ((srcdirp = fdopendir(tmpfd)) == NULL) { 21125331Samw if (pflg && attrsilent) { 21135331Samw error++; 21145331Samw goto out; 21155331Samw } 21165331Samw if (!attrsilent) { 21175331Samw (void) fprintf(stderr, 21185331Samw gettext("%s: failed to open attribute" 21195331Samw " directory for %s: "), cmd, source); 21205331Samw perror(""); 21215331Samw ++error; 21225331Samw } 21235331Samw } 21245331Samw out: 21255331Samw if (error) 21265331Samw close_all(); 21275331Samw return (error == 0 ? 0 : 1); 21285331Samw } 21295331Samw 21305331Samw /* Skips through ., .., and system attribute 'view' files */ 21315331Samw int 21325331Samw traverse_attrfile(struct dirent *dp, char *source, char *target, int first) 21335331Samw { 21345331Samw int error = 0; 21355331Samw 21365331Samw if ((dp->d_name[0] == '.' && dp->d_name[1] == '\0') || 21375331Samw (dp->d_name[0] == '.' && dp->d_name[1] == '.' && 21385331Samw dp->d_name[2] == '\0') || 21395331Samw (sysattr_type(dp->d_name) == _RO_SATTR) || 21405331Samw (sysattr_type(dp->d_name) == _RW_SATTR)) 21415331Samw return (-1); 21425331Samw 21435331Samw if ((srcattrfd = openat(sourcedirfd, dp->d_name, 21445331Samw O_RDONLY)) == -1) { 21455331Samw if (!attrsilent) { 21465331Samw (void) fprintf(stderr, 21475331Samw gettext("%s: cannot open attribute %s on" 21485331Samw " file %s: "), cmd, dp->d_name, source); 21495331Samw perror(""); 21505331Samw ++error; 21515331Samw goto out; 21525331Samw } 21535331Samw } 21545331Samw 21555331Samw if (fstat(srcattrfd, &s3) < 0) { 21565331Samw if (!attrsilent) { 21575331Samw (void) fprintf(stderr, 21585331Samw gettext("%s: could not stat attribute" 21595331Samw " %s on file" 21605331Samw " %s: "), cmd, dp->d_name, source); 21615331Samw perror(""); 21625331Samw ++error; 21635331Samw } 21645331Samw goto out; 21655331Samw } 21665331Samw 21675331Samw if (first) { 21685331Samw (void) unlinkat(targetdirfd, dp->d_name, 0); 21695331Samw targattrfd = openat(targetdirfd, dp->d_name, 21705331Samw O_RDWR|O_CREAT|O_TRUNC, s3.st_mode & MODEBITS); 21715331Samw } else { 21725331Samw targattrfd = openat(targetdirfd, dp->d_name, O_RDWR); 21735331Samw } 21745331Samw 21755331Samw if (targattrfd == -1) { 21765331Samw if (!attrsilent) { 21775331Samw (void) fprintf(stderr, 21785331Samw gettext("%s: could not create attribute" 21795331Samw " %s on file %s: "), cmd, dp->d_name, 21805331Samw target); 21815331Samw perror(""); 21825331Samw ++error; 21835331Samw } 21845331Samw goto out; 21855331Samw } 21865331Samw 21875331Samw if (fstat(targattrfd, &s4) < 0) { 21885331Samw if (!attrsilent) { 21895331Samw (void) fprintf(stderr, 21905331Samw gettext("%s: could not stat attribute" 21915331Samw " %s on file" 21925331Samw " %s: "), cmd, dp->d_name, target); 21935331Samw perror(""); 21945331Samw ++error; 21955331Samw } 21965331Samw } 21975331Samw 21985331Samw out: 21995331Samw if (error) { 22005331Samw if (srcattrfd != -1) 22015331Samw (void) close(srcattrfd); 22025331Samw if (targattrfd != -1) 22035331Samw (void) close(targattrfd); 22045331Samw } 22055331Samw return (error == 0 ? 0 :1); 22065331Samw } 22075331Samw 22085331Samw void 22095331Samw rewind_attrdir(DIR * sdp) 22105331Samw { 22115331Samw int pwdfd; 22125331Samw 22135331Samw pwdfd = open(".", O_RDONLY); 22145331Samw if ((pwdfd != -1) && (fchdir(sourcedirfd) == 0)) { 22155331Samw rewinddir(sdp); 22165331Samw (void) fchdir(pwdfd); 22175331Samw (void) close(pwdfd); 22185331Samw } else { 22195331Samw if (!attrsilent) { 22205331Samw (void) fprintf(stderr, gettext("%s: " 22215331Samw "failed to rewind attribute dir\n"), 22225331Samw cmd); 22235331Samw } 22245331Samw } 22255331Samw } 22265331Samw 22275331Samw void 22285331Samw close_all() 22295331Samw { 22305331Samw if (srcattrfd != -1) 22315331Samw (void) close(srcattrfd); 22325331Samw if (targattrfd != -1) 22335331Samw (void) close(targattrfd); 22345331Samw if (sourcedirfd != -1) 22355331Samw (void) close(sourcedirfd); 22365331Samw if (targetdirfd != -1) 22375331Samw (void) close(targetdirfd); 22385331Samw if (srcdirp != NULL) { 22395331Samw (void) closedir(srcdirp); 22405331Samw srcdirp = NULL; 22415331Samw } 22425331Samw if (srcfd != -1) 22435331Samw (void) close(srcfd); 22445331Samw if (targfd != -1) 22455331Samw (void) close(targfd); 22465331Samw } 2247