17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 57c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 67c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 77c478bd9Sstevel@tonic-gate * with the License. 87c478bd9Sstevel@tonic-gate * 97c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 107c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 117c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 127c478bd9Sstevel@tonic-gate * and limitations under the License. 137c478bd9Sstevel@tonic-gate * 147c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 157c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 167c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 177c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 187c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 197c478bd9Sstevel@tonic-gate * 207c478bd9Sstevel@tonic-gate * CDDL HEADER END 217c478bd9Sstevel@tonic-gate */ 227c478bd9Sstevel@tonic-gate /* 23a77d64afScf * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 247c478bd9Sstevel@tonic-gate * Use is subject to license terms. 257c478bd9Sstevel@tonic-gate */ 267c478bd9Sstevel@tonic-gate 277c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 287c478bd9Sstevel@tonic-gate /* All Rights Reserved */ 297c478bd9Sstevel@tonic-gate 307c478bd9Sstevel@tonic-gate /* 317c478bd9Sstevel@tonic-gate * University Copyright- Copyright (c) 1982, 1986, 1988 327c478bd9Sstevel@tonic-gate * The Regents of the University of California 337c478bd9Sstevel@tonic-gate * All Rights Reserved 347c478bd9Sstevel@tonic-gate * 357c478bd9Sstevel@tonic-gate * University Acknowledgment- Portions of this document are derived from 367c478bd9Sstevel@tonic-gate * software developed by the University of California, Berkeley, and its 377c478bd9Sstevel@tonic-gate * contributors. 387c478bd9Sstevel@tonic-gate */ 397c478bd9Sstevel@tonic-gate 407c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 417c478bd9Sstevel@tonic-gate 427c478bd9Sstevel@tonic-gate /* 437c478bd9Sstevel@tonic-gate * Combined mv/cp/ln command: 447c478bd9Sstevel@tonic-gate * mv file1 file2 457c478bd9Sstevel@tonic-gate * mv dir1 dir2 467c478bd9Sstevel@tonic-gate * mv file1 ... filen dir1 477c478bd9Sstevel@tonic-gate */ 487c478bd9Sstevel@tonic-gate #include <stdio.h> 497c478bd9Sstevel@tonic-gate #include <sys/types.h> 507c478bd9Sstevel@tonic-gate #include <sys/stat.h> 517c478bd9Sstevel@tonic-gate #include <sys/avl.h> 527c478bd9Sstevel@tonic-gate #include <sys/mman.h> 537c478bd9Sstevel@tonic-gate #include <fcntl.h> 547c478bd9Sstevel@tonic-gate #include <sys/time.h> 557c478bd9Sstevel@tonic-gate #include <signal.h> 567c478bd9Sstevel@tonic-gate #include <errno.h> 577c478bd9Sstevel@tonic-gate #include <dirent.h> 587c478bd9Sstevel@tonic-gate #include <stdlib.h> 597c478bd9Sstevel@tonic-gate #include <locale.h> 607c478bd9Sstevel@tonic-gate #include <langinfo.h> 617c478bd9Sstevel@tonic-gate #include <stdarg.h> 627c478bd9Sstevel@tonic-gate #include <string.h> 637c478bd9Sstevel@tonic-gate #include <unistd.h> 647c478bd9Sstevel@tonic-gate #include <limits.h> 657c478bd9Sstevel@tonic-gate #include <sys/acl.h> 667c478bd9Sstevel@tonic-gate #include <libcmdutils.h> 67*fa9e4066Sahrens #include <aclutils.h> 687c478bd9Sstevel@tonic-gate 697c478bd9Sstevel@tonic-gate #define FTYPE(A) (A.st_mode) 707c478bd9Sstevel@tonic-gate #define FMODE(A) (A.st_mode) 717c478bd9Sstevel@tonic-gate #define UID(A) (A.st_uid) 727c478bd9Sstevel@tonic-gate #define GID(A) (A.st_gid) 737c478bd9Sstevel@tonic-gate #define IDENTICAL(A, B) (A.st_dev == B.st_dev && A.st_ino == B.st_ino) 747c478bd9Sstevel@tonic-gate #define ISBLK(A) ((A.st_mode & S_IFMT) == S_IFBLK) 757c478bd9Sstevel@tonic-gate #define ISCHR(A) ((A.st_mode & S_IFMT) == S_IFCHR) 767c478bd9Sstevel@tonic-gate #define ISDIR(A) ((A.st_mode & S_IFMT) == S_IFDIR) 777c478bd9Sstevel@tonic-gate #define ISDOOR(A) ((A.st_mode & S_IFMT) == S_IFDOOR) 787c478bd9Sstevel@tonic-gate #define ISFIFO(A) ((A.st_mode & S_IFMT) == S_IFIFO) 797c478bd9Sstevel@tonic-gate #define ISLNK(A) ((A.st_mode & S_IFMT) == S_IFLNK) 807c478bd9Sstevel@tonic-gate #define ISREG(A) (((A).st_mode & S_IFMT) == S_IFREG) 817c478bd9Sstevel@tonic-gate #define ISDEV(A) ((A.st_mode & S_IFMT) == S_IFCHR || \ 827c478bd9Sstevel@tonic-gate (A.st_mode & S_IFMT) == S_IFBLK || \ 837c478bd9Sstevel@tonic-gate (A.st_mode & S_IFMT) == S_IFIFO) 847c478bd9Sstevel@tonic-gate 857c478bd9Sstevel@tonic-gate #define BLKSIZE 4096 867c478bd9Sstevel@tonic-gate #define PATHSIZE 1024 877c478bd9Sstevel@tonic-gate #define DOT "." 887c478bd9Sstevel@tonic-gate #define DOTDOT ".." 897c478bd9Sstevel@tonic-gate #define DELIM '/' 907c478bd9Sstevel@tonic-gate #define EQ(x, y) (strcmp(x, y) == 0) 917c478bd9Sstevel@tonic-gate #define FALSE 0 927c478bd9Sstevel@tonic-gate #define MODEBITS (S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO) 937c478bd9Sstevel@tonic-gate #define TRUE 1 947c478bd9Sstevel@tonic-gate #define MAXMAPSIZE (1024*1024*8) /* map at most 8MB */ 957c478bd9Sstevel@tonic-gate #define SMALLFILESIZE (32*1024) /* don't use mmap on little files */ 967c478bd9Sstevel@tonic-gate 977c478bd9Sstevel@tonic-gate static char *dname(char *); 987c478bd9Sstevel@tonic-gate static int getresp(void); 997c478bd9Sstevel@tonic-gate static int lnkfil(char *, char *); 1007c478bd9Sstevel@tonic-gate static int cpymve(char *, char *); 1017c478bd9Sstevel@tonic-gate static int chkfiles(char *, char **); 1027c478bd9Sstevel@tonic-gate static int rcopy(char *, char *); 1037c478bd9Sstevel@tonic-gate static int chk_different(char *, char *); 1047c478bd9Sstevel@tonic-gate static int chg_time(char *, struct stat); 1057c478bd9Sstevel@tonic-gate static int chg_mode(char *, uid_t, gid_t, mode_t); 1067c478bd9Sstevel@tonic-gate static int copydir(char *, char *); 1077c478bd9Sstevel@tonic-gate static int copyspecial(char *); 1087c478bd9Sstevel@tonic-gate static int getrealpath(char *, char *); 1097c478bd9Sstevel@tonic-gate static void usage(void); 1107c478bd9Sstevel@tonic-gate static void Perror(char *); 1117c478bd9Sstevel@tonic-gate static void Perror2(char *, char *); 1127c478bd9Sstevel@tonic-gate static int writefile(int, int, char *, char *, 1137c478bd9Sstevel@tonic-gate struct stat *, struct stat *); 1147c478bd9Sstevel@tonic-gate static int use_stdin(void); 1157c478bd9Sstevel@tonic-gate static int copyattributes(char *, char *); 1167c478bd9Sstevel@tonic-gate static void timestruc_to_timeval(timestruc_t *, struct timeval *); 1177c478bd9Sstevel@tonic-gate static tree_node_t *create_tnode(dev_t, ino_t); 1187c478bd9Sstevel@tonic-gate 1197c478bd9Sstevel@tonic-gate extern int errno; 1207c478bd9Sstevel@tonic-gate extern char *optarg; 1217c478bd9Sstevel@tonic-gate extern int optind, opterr; 1227c478bd9Sstevel@tonic-gate static struct stat s1, s2; 1237c478bd9Sstevel@tonic-gate static int cpy = FALSE; 1247c478bd9Sstevel@tonic-gate static int mve = FALSE; 1257c478bd9Sstevel@tonic-gate static int lnk = FALSE; 1267c478bd9Sstevel@tonic-gate static char *cmd; 1277c478bd9Sstevel@tonic-gate static int silent = 0; 1287c478bd9Sstevel@tonic-gate static int fflg = 0; 1297c478bd9Sstevel@tonic-gate static int iflg = 0; 1307c478bd9Sstevel@tonic-gate static int pflg = 0; 1317c478bd9Sstevel@tonic-gate static int Rflg = 0; /* recursive copy */ 1327c478bd9Sstevel@tonic-gate static int rflg = 0; /* recursive copy */ 1337c478bd9Sstevel@tonic-gate static int sflg = 0; 1347c478bd9Sstevel@tonic-gate static int Hflg = 0; /* follow cmd line arg symlink to dir */ 1357c478bd9Sstevel@tonic-gate static int Lflg = 0; /* follow symlinks */ 1367c478bd9Sstevel@tonic-gate static int Pflg = 0; /* do not follow symlinks */ 1377c478bd9Sstevel@tonic-gate static int atflg = 0; 1387c478bd9Sstevel@tonic-gate static int attrsilent = 0; 1397c478bd9Sstevel@tonic-gate static int targetexists = 0; 1407c478bd9Sstevel@tonic-gate static char yeschr[SCHAR_MAX + 2]; 1417c478bd9Sstevel@tonic-gate static char nochr[SCHAR_MAX + 2]; 1427c478bd9Sstevel@tonic-gate static int cmdarg; /* command line argument */ 1437c478bd9Sstevel@tonic-gate static avl_tree_t *stree = NULL; /* source file inode search tree */ 144*fa9e4066Sahrens static acl_t *s1acl; 1457c478bd9Sstevel@tonic-gate 146a77d64afScf int 1477c478bd9Sstevel@tonic-gate main(int argc, char *argv[]) 1487c478bd9Sstevel@tonic-gate { 1497c478bd9Sstevel@tonic-gate int c, i, r, errflg = 0; 1507c478bd9Sstevel@tonic-gate char target[PATH_MAX]; 1517c478bd9Sstevel@tonic-gate int (*move)(char *, char *); 1527c478bd9Sstevel@tonic-gate 1537c478bd9Sstevel@tonic-gate /* 1547c478bd9Sstevel@tonic-gate * Determine command invoked (mv, cp, or ln) 1557c478bd9Sstevel@tonic-gate */ 1567c478bd9Sstevel@tonic-gate 1577c478bd9Sstevel@tonic-gate if (cmd = strrchr(argv[0], '/')) 1587c478bd9Sstevel@tonic-gate ++cmd; 1597c478bd9Sstevel@tonic-gate else 1607c478bd9Sstevel@tonic-gate cmd = argv[0]; 1617c478bd9Sstevel@tonic-gate 1627c478bd9Sstevel@tonic-gate /* 1637c478bd9Sstevel@tonic-gate * Set flags based on command. 1647c478bd9Sstevel@tonic-gate */ 1657c478bd9Sstevel@tonic-gate 1667c478bd9Sstevel@tonic-gate (void) setlocale(LC_ALL, ""); 1677c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */ 1687c478bd9Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it weren't */ 1697c478bd9Sstevel@tonic-gate #endif 1707c478bd9Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN); 1717c478bd9Sstevel@tonic-gate 1727c478bd9Sstevel@tonic-gate (void) strncpy(yeschr, nl_langinfo(YESSTR), SCHAR_MAX + 2); 1737c478bd9Sstevel@tonic-gate (void) strncpy(nochr, nl_langinfo(NOSTR), SCHAR_MAX + 2); 1747c478bd9Sstevel@tonic-gate 1757c478bd9Sstevel@tonic-gate if (EQ(cmd, "mv")) 1767c478bd9Sstevel@tonic-gate mve = TRUE; 1777c478bd9Sstevel@tonic-gate else if (EQ(cmd, "ln")) 1787c478bd9Sstevel@tonic-gate lnk = TRUE; 1797c478bd9Sstevel@tonic-gate else if (EQ(cmd, "cp")) 1807c478bd9Sstevel@tonic-gate cpy = TRUE; 1817c478bd9Sstevel@tonic-gate else { 1827c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 1837c478bd9Sstevel@tonic-gate gettext("Invalid command name (%s); expecting " 1847c478bd9Sstevel@tonic-gate "mv, cp, or ln.\n"), cmd); 1857c478bd9Sstevel@tonic-gate exit(1); 1867c478bd9Sstevel@tonic-gate } 1877c478bd9Sstevel@tonic-gate 1887c478bd9Sstevel@tonic-gate /* 1897c478bd9Sstevel@tonic-gate * Check for options: 1907c478bd9Sstevel@tonic-gate * cp -r|-R [-H|-L|-P] [-fip@] file1 [file2 ...] target 1917c478bd9Sstevel@tonic-gate * cp [-fiprR@] file1 [file2 ...] target 1927c478bd9Sstevel@tonic-gate * ln [-f] [-n] [-s] file1 [file2 ...] target 1937c478bd9Sstevel@tonic-gate * ln [-f] [-n] [-s] file1 [file2 ...] 1947c478bd9Sstevel@tonic-gate * mv [-f|i] file1 [file2 ...] target 1957c478bd9Sstevel@tonic-gate * mv [-f|i] dir1 target 1967c478bd9Sstevel@tonic-gate */ 1977c478bd9Sstevel@tonic-gate 1987c478bd9Sstevel@tonic-gate if (cpy) { 1997c478bd9Sstevel@tonic-gate while ((c = getopt(argc, argv, "fHiLpPrR@")) != EOF) 2007c478bd9Sstevel@tonic-gate switch (c) { 2017c478bd9Sstevel@tonic-gate case 'f': 2027c478bd9Sstevel@tonic-gate fflg++; 2037c478bd9Sstevel@tonic-gate break; 2047c478bd9Sstevel@tonic-gate case 'i': 2057c478bd9Sstevel@tonic-gate iflg++; 2067c478bd9Sstevel@tonic-gate break; 2077c478bd9Sstevel@tonic-gate case 'p': 2087c478bd9Sstevel@tonic-gate pflg++; 2097c478bd9Sstevel@tonic-gate #ifdef XPG4 2107c478bd9Sstevel@tonic-gate attrsilent = 1; 2117c478bd9Sstevel@tonic-gate atflg = 0; 2127c478bd9Sstevel@tonic-gate #else 2137c478bd9Sstevel@tonic-gate if (atflg == 0) 2147c478bd9Sstevel@tonic-gate attrsilent = 1; 2157c478bd9Sstevel@tonic-gate #endif 2167c478bd9Sstevel@tonic-gate break; 2177c478bd9Sstevel@tonic-gate case 'H': 2187c478bd9Sstevel@tonic-gate /* 2197c478bd9Sstevel@tonic-gate * If more than one of -H, -L, or -P are 2207c478bd9Sstevel@tonic-gate * specified, only the last option specified 2217c478bd9Sstevel@tonic-gate * determines the behavior. 2227c478bd9Sstevel@tonic-gate */ 2237c478bd9Sstevel@tonic-gate Lflg = Pflg = 0; 2247c478bd9Sstevel@tonic-gate Hflg++; 2257c478bd9Sstevel@tonic-gate break; 2267c478bd9Sstevel@tonic-gate case 'L': 2277c478bd9Sstevel@tonic-gate Hflg = Pflg = 0; 2287c478bd9Sstevel@tonic-gate Lflg++; 2297c478bd9Sstevel@tonic-gate break; 2307c478bd9Sstevel@tonic-gate case 'P': 2317c478bd9Sstevel@tonic-gate Lflg = Hflg = 0; 2327c478bd9Sstevel@tonic-gate Pflg++; 2337c478bd9Sstevel@tonic-gate break; 2347c478bd9Sstevel@tonic-gate case 'R': 2357c478bd9Sstevel@tonic-gate /* 2367c478bd9Sstevel@tonic-gate * The default behavior of cp -R|-r 2377c478bd9Sstevel@tonic-gate * when specified without -H|-L|-P 2387c478bd9Sstevel@tonic-gate * is -L. 2397c478bd9Sstevel@tonic-gate */ 2407c478bd9Sstevel@tonic-gate Rflg++; 2417c478bd9Sstevel@tonic-gate /*FALLTHROUGH*/ 2427c478bd9Sstevel@tonic-gate case 'r': 2437c478bd9Sstevel@tonic-gate rflg++; 2447c478bd9Sstevel@tonic-gate break; 2457c478bd9Sstevel@tonic-gate case '@': 2467c478bd9Sstevel@tonic-gate atflg++; 2477c478bd9Sstevel@tonic-gate attrsilent = 0; 2487c478bd9Sstevel@tonic-gate #ifdef XPG4 2497c478bd9Sstevel@tonic-gate pflg = 0; 2507c478bd9Sstevel@tonic-gate #endif 2517c478bd9Sstevel@tonic-gate break; 2527c478bd9Sstevel@tonic-gate default: 2537c478bd9Sstevel@tonic-gate errflg++; 2547c478bd9Sstevel@tonic-gate } 2557c478bd9Sstevel@tonic-gate 2567c478bd9Sstevel@tonic-gate /* -R or -r must be specified with -H, -L, or -P */ 2577c478bd9Sstevel@tonic-gate if ((Hflg || Lflg || Pflg) && !(Rflg || rflg)) { 2587c478bd9Sstevel@tonic-gate errflg++; 2597c478bd9Sstevel@tonic-gate } 2607c478bd9Sstevel@tonic-gate 2617c478bd9Sstevel@tonic-gate } else if (mve) { 2627c478bd9Sstevel@tonic-gate while ((c = getopt(argc, argv, "fis")) != EOF) 2637c478bd9Sstevel@tonic-gate switch (c) { 2647c478bd9Sstevel@tonic-gate case 'f': 2657c478bd9Sstevel@tonic-gate silent++; 2667c478bd9Sstevel@tonic-gate #ifdef XPG4 2677c478bd9Sstevel@tonic-gate iflg = 0; 2687c478bd9Sstevel@tonic-gate #endif 2697c478bd9Sstevel@tonic-gate break; 2707c478bd9Sstevel@tonic-gate case 'i': 2717c478bd9Sstevel@tonic-gate iflg++; 2727c478bd9Sstevel@tonic-gate #ifdef XPG4 2737c478bd9Sstevel@tonic-gate silent = 0; 2747c478bd9Sstevel@tonic-gate #endif 2757c478bd9Sstevel@tonic-gate break; 2767c478bd9Sstevel@tonic-gate default: 2777c478bd9Sstevel@tonic-gate errflg++; 2787c478bd9Sstevel@tonic-gate } 2797c478bd9Sstevel@tonic-gate } else { /* ln */ 2807c478bd9Sstevel@tonic-gate while ((c = getopt(argc, argv, "fns")) != EOF) 2817c478bd9Sstevel@tonic-gate switch (c) { 2827c478bd9Sstevel@tonic-gate case 'f': 2837c478bd9Sstevel@tonic-gate silent++; 2847c478bd9Sstevel@tonic-gate break; 2857c478bd9Sstevel@tonic-gate case 'n': 2867c478bd9Sstevel@tonic-gate /* silently ignored; this is the default */ 2877c478bd9Sstevel@tonic-gate break; 2887c478bd9Sstevel@tonic-gate case 's': 2897c478bd9Sstevel@tonic-gate sflg++; 2907c478bd9Sstevel@tonic-gate break; 2917c478bd9Sstevel@tonic-gate default: 2927c478bd9Sstevel@tonic-gate errflg++; 2937c478bd9Sstevel@tonic-gate } 2947c478bd9Sstevel@tonic-gate } 2957c478bd9Sstevel@tonic-gate 2967c478bd9Sstevel@tonic-gate /* 2977c478bd9Sstevel@tonic-gate * For BSD compatibility allow - to delimit the end of 2987c478bd9Sstevel@tonic-gate * options for mv. 2997c478bd9Sstevel@tonic-gate */ 3007c478bd9Sstevel@tonic-gate if (mve && optind < argc && (strcmp(argv[optind], "-") == 0)) 3017c478bd9Sstevel@tonic-gate optind++; 3027c478bd9Sstevel@tonic-gate 3037c478bd9Sstevel@tonic-gate /* 3047c478bd9Sstevel@tonic-gate * Check for sufficient arguments 3057c478bd9Sstevel@tonic-gate * or a usage error. 3067c478bd9Sstevel@tonic-gate */ 3077c478bd9Sstevel@tonic-gate 3087c478bd9Sstevel@tonic-gate argc -= optind; 3097c478bd9Sstevel@tonic-gate argv = &argv[optind]; 3107c478bd9Sstevel@tonic-gate 3117c478bd9Sstevel@tonic-gate if ((argc < 2 && lnk != TRUE) || (argc < 1 && lnk == TRUE)) { 3127c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 3137c478bd9Sstevel@tonic-gate gettext("%s: Insufficient arguments (%d)\n"), 3147c478bd9Sstevel@tonic-gate cmd, argc); 3157c478bd9Sstevel@tonic-gate usage(); 3167c478bd9Sstevel@tonic-gate } 3177c478bd9Sstevel@tonic-gate 3187c478bd9Sstevel@tonic-gate if (errflg != 0) 3197c478bd9Sstevel@tonic-gate usage(); 3207c478bd9Sstevel@tonic-gate 3217c478bd9Sstevel@tonic-gate /* 3227c478bd9Sstevel@tonic-gate * If there is more than a source and target, 3237c478bd9Sstevel@tonic-gate * the last argument (the target) must be a directory 3247c478bd9Sstevel@tonic-gate * which really exists. 3257c478bd9Sstevel@tonic-gate */ 3267c478bd9Sstevel@tonic-gate 3277c478bd9Sstevel@tonic-gate if (argc > 2) { 3287c478bd9Sstevel@tonic-gate if (stat(argv[argc-1], &s2) < 0) { 3297c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 3307c478bd9Sstevel@tonic-gate gettext("%s: %s not found\n"), 3317c478bd9Sstevel@tonic-gate cmd, argv[argc-1]); 3327c478bd9Sstevel@tonic-gate exit(2); 3337c478bd9Sstevel@tonic-gate } 3347c478bd9Sstevel@tonic-gate 3357c478bd9Sstevel@tonic-gate if (!ISDIR(s2)) { 3367c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 3377c478bd9Sstevel@tonic-gate gettext("%s: Target %s must be a directory\n"), 3387c478bd9Sstevel@tonic-gate cmd, argv[argc-1]); 3397c478bd9Sstevel@tonic-gate usage(); 3407c478bd9Sstevel@tonic-gate } 3417c478bd9Sstevel@tonic-gate } 3427c478bd9Sstevel@tonic-gate 3437c478bd9Sstevel@tonic-gate if (strlen(argv[argc-1]) >= PATH_MAX) { 3447c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 3457c478bd9Sstevel@tonic-gate gettext("%s: Target %s file name length exceeds PATH_MAX" 3467c478bd9Sstevel@tonic-gate " %d\n"), cmd, argv[argc-1], PATH_MAX); 3477c478bd9Sstevel@tonic-gate exit(78); 3487c478bd9Sstevel@tonic-gate } 3497c478bd9Sstevel@tonic-gate 3507c478bd9Sstevel@tonic-gate if (argc == 1) { 3517c478bd9Sstevel@tonic-gate if (!lnk) 3527c478bd9Sstevel@tonic-gate usage(); 3537c478bd9Sstevel@tonic-gate (void) strcpy(target, "."); 3547c478bd9Sstevel@tonic-gate } else { 3557c478bd9Sstevel@tonic-gate (void) strcpy(target, argv[--argc]); 3567c478bd9Sstevel@tonic-gate } 3577c478bd9Sstevel@tonic-gate 3587c478bd9Sstevel@tonic-gate /* 3597c478bd9Sstevel@tonic-gate * Perform a multiple argument mv|cp|ln by 3607c478bd9Sstevel@tonic-gate * multiple invocations of cpymve() or lnkfil(). 3617c478bd9Sstevel@tonic-gate */ 3627c478bd9Sstevel@tonic-gate if (lnk) 3637c478bd9Sstevel@tonic-gate move = lnkfil; 3647c478bd9Sstevel@tonic-gate else 3657c478bd9Sstevel@tonic-gate move = cpymve; 3667c478bd9Sstevel@tonic-gate 3677c478bd9Sstevel@tonic-gate r = 0; 3687c478bd9Sstevel@tonic-gate for (i = 0; i < argc; i++) { 3697c478bd9Sstevel@tonic-gate stree = NULL; 3707c478bd9Sstevel@tonic-gate cmdarg = 1; 3717c478bd9Sstevel@tonic-gate r += move(argv[i], target); 3727c478bd9Sstevel@tonic-gate } 3737c478bd9Sstevel@tonic-gate 3747c478bd9Sstevel@tonic-gate /* 3757c478bd9Sstevel@tonic-gate * Show errors by nonzero exit code. 3767c478bd9Sstevel@tonic-gate */ 3777c478bd9Sstevel@tonic-gate 378a77d64afScf return (r?2:0); 3797c478bd9Sstevel@tonic-gate } 3807c478bd9Sstevel@tonic-gate 3817c478bd9Sstevel@tonic-gate static int 3827c478bd9Sstevel@tonic-gate lnkfil(char *source, char *target) 3837c478bd9Sstevel@tonic-gate { 3847c478bd9Sstevel@tonic-gate char *buf = NULL; 3857c478bd9Sstevel@tonic-gate 3867c478bd9Sstevel@tonic-gate if (sflg) { 3877c478bd9Sstevel@tonic-gate 3887c478bd9Sstevel@tonic-gate /* 3897c478bd9Sstevel@tonic-gate * If target is a directory make complete 3907c478bd9Sstevel@tonic-gate * name of the new symbolic link within that 3917c478bd9Sstevel@tonic-gate * directory. 3927c478bd9Sstevel@tonic-gate */ 3937c478bd9Sstevel@tonic-gate 3947c478bd9Sstevel@tonic-gate if ((stat(target, &s2) >= 0) && ISDIR(s2)) { 3957c478bd9Sstevel@tonic-gate size_t len; 3967c478bd9Sstevel@tonic-gate 3977c478bd9Sstevel@tonic-gate len = strlen(target) + strlen(dname(source)) + 4; 3987c478bd9Sstevel@tonic-gate if ((buf = (char *)malloc(len)) == NULL) { 3997c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 4007c478bd9Sstevel@tonic-gate gettext("%s: Insufficient memory " 4017c478bd9Sstevel@tonic-gate "to %s %s\n"), cmd, cmd, source); 4027c478bd9Sstevel@tonic-gate exit(3); 4037c478bd9Sstevel@tonic-gate } 4047c478bd9Sstevel@tonic-gate (void) snprintf(buf, len, "%s/%s", 4057c478bd9Sstevel@tonic-gate target, dname(source)); 4067c478bd9Sstevel@tonic-gate target = buf; 4077c478bd9Sstevel@tonic-gate } 4087c478bd9Sstevel@tonic-gate 4097c478bd9Sstevel@tonic-gate /* 4107c478bd9Sstevel@tonic-gate * Check to see if the file exists already 4117c478bd9Sstevel@tonic-gate */ 4127c478bd9Sstevel@tonic-gate 4137c478bd9Sstevel@tonic-gate if ((stat(target, &s2) == 0)) { 4147c478bd9Sstevel@tonic-gate /* 4157c478bd9Sstevel@tonic-gate * Check if the silent flag is set ie. the -f option 4167c478bd9Sstevel@tonic-gate * is used. If so, use unlink to remove the current 4177c478bd9Sstevel@tonic-gate * target to replace with the new target, specified 4187c478bd9Sstevel@tonic-gate * on the command line. Proceed with symlink. 4197c478bd9Sstevel@tonic-gate */ 4207c478bd9Sstevel@tonic-gate if (silent) { 4217c478bd9Sstevel@tonic-gate if (unlink(target) < 0) { 4227c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 4237c478bd9Sstevel@tonic-gate gettext("%s: cannot unlink %s: "), 4247c478bd9Sstevel@tonic-gate cmd, target); 4257c478bd9Sstevel@tonic-gate perror(""); 4267c478bd9Sstevel@tonic-gate return (1); 4277c478bd9Sstevel@tonic-gate } 4287c478bd9Sstevel@tonic-gate } 4297c478bd9Sstevel@tonic-gate } 4307c478bd9Sstevel@tonic-gate 4317c478bd9Sstevel@tonic-gate 4327c478bd9Sstevel@tonic-gate /* 4337c478bd9Sstevel@tonic-gate * Create a symbolic link to the source. 4347c478bd9Sstevel@tonic-gate */ 4357c478bd9Sstevel@tonic-gate 4367c478bd9Sstevel@tonic-gate if (symlink(source, target) < 0) { 4377c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 4387c478bd9Sstevel@tonic-gate gettext("%s: cannot create %s: "), 4397c478bd9Sstevel@tonic-gate cmd, target); 4407c478bd9Sstevel@tonic-gate perror(""); 4417c478bd9Sstevel@tonic-gate if (buf != NULL) 4427c478bd9Sstevel@tonic-gate free(buf); 4437c478bd9Sstevel@tonic-gate return (1); 4447c478bd9Sstevel@tonic-gate } 4457c478bd9Sstevel@tonic-gate if (buf != NULL) 4467c478bd9Sstevel@tonic-gate free(buf); 4477c478bd9Sstevel@tonic-gate return (0); 4487c478bd9Sstevel@tonic-gate } 4497c478bd9Sstevel@tonic-gate 4507c478bd9Sstevel@tonic-gate switch (chkfiles(source, &target)) { 4517c478bd9Sstevel@tonic-gate case 1: return (1); 4527c478bd9Sstevel@tonic-gate case 2: return (0); 4537c478bd9Sstevel@tonic-gate /* default - fall through */ 4547c478bd9Sstevel@tonic-gate } 4557c478bd9Sstevel@tonic-gate 4567c478bd9Sstevel@tonic-gate /* 4577c478bd9Sstevel@tonic-gate * Make sure source file is not a directory, 4587c478bd9Sstevel@tonic-gate * we can't link directories... 4597c478bd9Sstevel@tonic-gate */ 4607c478bd9Sstevel@tonic-gate 4617c478bd9Sstevel@tonic-gate if (ISDIR(s1)) { 4627c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 4637c478bd9Sstevel@tonic-gate gettext("%s: %s is a directory\n"), cmd, source); 4647c478bd9Sstevel@tonic-gate return (1); 4657c478bd9Sstevel@tonic-gate } 4667c478bd9Sstevel@tonic-gate 4677c478bd9Sstevel@tonic-gate /* 4687c478bd9Sstevel@tonic-gate * hard link, call link() and return. 4697c478bd9Sstevel@tonic-gate */ 4707c478bd9Sstevel@tonic-gate 4717c478bd9Sstevel@tonic-gate if (link(source, target) < 0) { 4727c478bd9Sstevel@tonic-gate if (errno == EXDEV) 4737c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 4747c478bd9Sstevel@tonic-gate gettext("%s: %s is on a different file system\n"), 4757c478bd9Sstevel@tonic-gate cmd, target); 4767c478bd9Sstevel@tonic-gate else { 4777c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 4787c478bd9Sstevel@tonic-gate gettext("%s: cannot create link %s: "), 4797c478bd9Sstevel@tonic-gate cmd, target); 4807c478bd9Sstevel@tonic-gate perror(""); 4817c478bd9Sstevel@tonic-gate } 4827c478bd9Sstevel@tonic-gate if (buf != NULL) 4837c478bd9Sstevel@tonic-gate free(buf); 4847c478bd9Sstevel@tonic-gate return (1); 4857c478bd9Sstevel@tonic-gate } else { 4867c478bd9Sstevel@tonic-gate if (buf != NULL) 4877c478bd9Sstevel@tonic-gate free(buf); 4887c478bd9Sstevel@tonic-gate return (0); 4897c478bd9Sstevel@tonic-gate } 4907c478bd9Sstevel@tonic-gate } 4917c478bd9Sstevel@tonic-gate 4927c478bd9Sstevel@tonic-gate static int 4937c478bd9Sstevel@tonic-gate cpymve(char *source, char *target) 4947c478bd9Sstevel@tonic-gate { 4957c478bd9Sstevel@tonic-gate int n; 4967c478bd9Sstevel@tonic-gate int fi, fo; 4977c478bd9Sstevel@tonic-gate int ret = 0; 4987c478bd9Sstevel@tonic-gate int attret = 0; 4997c478bd9Sstevel@tonic-gate int errno_save; 5007c478bd9Sstevel@tonic-gate 5017c478bd9Sstevel@tonic-gate switch (chkfiles(source, &target)) { 5027c478bd9Sstevel@tonic-gate case 1: return (1); 5037c478bd9Sstevel@tonic-gate case 2: return (0); 5047c478bd9Sstevel@tonic-gate /* default - fall through */ 5057c478bd9Sstevel@tonic-gate } 5067c478bd9Sstevel@tonic-gate 5077c478bd9Sstevel@tonic-gate /* 5087c478bd9Sstevel@tonic-gate * If it's a recursive copy and source 5097c478bd9Sstevel@tonic-gate * is a directory, then call rcopy (from copydir). 5107c478bd9Sstevel@tonic-gate */ 5117c478bd9Sstevel@tonic-gate if (cpy) { 5127c478bd9Sstevel@tonic-gate if (ISDIR(s1)) { 5137c478bd9Sstevel@tonic-gate int rc; 5147c478bd9Sstevel@tonic-gate avl_index_t where = 0; 5157c478bd9Sstevel@tonic-gate tree_node_t *tnode; 5167c478bd9Sstevel@tonic-gate tree_node_t *tptr; 5177c478bd9Sstevel@tonic-gate dev_t save_dev = s1.st_dev; 5187c478bd9Sstevel@tonic-gate ino_t save_ino = s1.st_ino; 5197c478bd9Sstevel@tonic-gate 5207c478bd9Sstevel@tonic-gate /* 5217c478bd9Sstevel@tonic-gate * We will be recursing into the directory so 5227c478bd9Sstevel@tonic-gate * save the inode information to a search tree 5237c478bd9Sstevel@tonic-gate * to avoid getting into an endless loop. 5247c478bd9Sstevel@tonic-gate */ 5257c478bd9Sstevel@tonic-gate if ((rc = add_tnode(&stree, save_dev, save_ino)) != 1) { 5267c478bd9Sstevel@tonic-gate if (rc == 0) { 5277c478bd9Sstevel@tonic-gate /* 5287c478bd9Sstevel@tonic-gate * We've already visited this directory. 5297c478bd9Sstevel@tonic-gate * Don't remove the search tree entry 5307c478bd9Sstevel@tonic-gate * to make sure we don't get into an 5317c478bd9Sstevel@tonic-gate * endless loop if revisited from a 5327c478bd9Sstevel@tonic-gate * different part of the hierarchy. 5337c478bd9Sstevel@tonic-gate */ 5347c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext( 5357c478bd9Sstevel@tonic-gate "%s: cycle detected: %s\n"), 5367c478bd9Sstevel@tonic-gate cmd, source); 5377c478bd9Sstevel@tonic-gate } else { 5387c478bd9Sstevel@tonic-gate Perror(source); 5397c478bd9Sstevel@tonic-gate } 5407c478bd9Sstevel@tonic-gate return (1); 5417c478bd9Sstevel@tonic-gate } 5427c478bd9Sstevel@tonic-gate 5437c478bd9Sstevel@tonic-gate cmdarg = 0; 5447c478bd9Sstevel@tonic-gate rc = copydir(source, target); 5457c478bd9Sstevel@tonic-gate 5467c478bd9Sstevel@tonic-gate /* 5477c478bd9Sstevel@tonic-gate * Create a tnode to get an index to the matching 5487c478bd9Sstevel@tonic-gate * node (same dev and inode) in the search tree, 5497c478bd9Sstevel@tonic-gate * then use the index to remove the matching node 5507c478bd9Sstevel@tonic-gate * so it we do not wrongly detect a cycle when 5517c478bd9Sstevel@tonic-gate * revisiting this directory from another part of 5527c478bd9Sstevel@tonic-gate * the hierarchy. 5537c478bd9Sstevel@tonic-gate */ 5547c478bd9Sstevel@tonic-gate if ((tnode = create_tnode(save_dev, 5557c478bd9Sstevel@tonic-gate save_ino)) == NULL) { 5567c478bd9Sstevel@tonic-gate Perror(source); 5577c478bd9Sstevel@tonic-gate return (1); 5587c478bd9Sstevel@tonic-gate } 5597c478bd9Sstevel@tonic-gate if ((tptr = avl_find(stree, tnode, &where)) != NULL) { 5607c478bd9Sstevel@tonic-gate avl_remove(stree, tptr); 5617c478bd9Sstevel@tonic-gate } 5627c478bd9Sstevel@tonic-gate free(tptr); 5637c478bd9Sstevel@tonic-gate free(tnode); 5647c478bd9Sstevel@tonic-gate return (rc); 5657c478bd9Sstevel@tonic-gate 5667c478bd9Sstevel@tonic-gate } else if (ISDEV(s1) && Rflg) { 5677c478bd9Sstevel@tonic-gate return (copyspecial(target)); 5687c478bd9Sstevel@tonic-gate } else { 5697c478bd9Sstevel@tonic-gate goto copy; 5707c478bd9Sstevel@tonic-gate } 5717c478bd9Sstevel@tonic-gate } 5727c478bd9Sstevel@tonic-gate 5737c478bd9Sstevel@tonic-gate if (mve) { 5747c478bd9Sstevel@tonic-gate if (rename(source, target) >= 0) 5757c478bd9Sstevel@tonic-gate return (0); 5767c478bd9Sstevel@tonic-gate if (errno != EXDEV) { 5777c478bd9Sstevel@tonic-gate if (errno == ENOTDIR && ISDIR(s1)) { 5787c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 5797c478bd9Sstevel@tonic-gate gettext("%s: %s is a directory\n"), 5807c478bd9Sstevel@tonic-gate cmd, source); 5817c478bd9Sstevel@tonic-gate return (1); 5827c478bd9Sstevel@tonic-gate } 5837c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 5847c478bd9Sstevel@tonic-gate gettext("%s: cannot rename %s to %s: "), 5857c478bd9Sstevel@tonic-gate cmd, source, target); 5867c478bd9Sstevel@tonic-gate perror(""); 5877c478bd9Sstevel@tonic-gate return (1); 5887c478bd9Sstevel@tonic-gate } 5897c478bd9Sstevel@tonic-gate 5907c478bd9Sstevel@tonic-gate /* 5917c478bd9Sstevel@tonic-gate * cannot move a non-directory (source) onto an existing 5927c478bd9Sstevel@tonic-gate * directory (target) 5937c478bd9Sstevel@tonic-gate * 5947c478bd9Sstevel@tonic-gate */ 5957c478bd9Sstevel@tonic-gate if (targetexists && ISDIR(s2) && (!ISDIR(s1))) { 5967c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 5977c478bd9Sstevel@tonic-gate gettext("%s: cannot mv a non directory %s " 5987c478bd9Sstevel@tonic-gate "over existing directory" 5997c478bd9Sstevel@tonic-gate " %s \n"), cmd, source, target); 6007c478bd9Sstevel@tonic-gate return (1); 6017c478bd9Sstevel@tonic-gate } 6027c478bd9Sstevel@tonic-gate if (ISDIR(s1)) { 6037c478bd9Sstevel@tonic-gate #ifdef XPG4 6047c478bd9Sstevel@tonic-gate if (targetexists && ISDIR(s2)) { 6057c478bd9Sstevel@tonic-gate /* existing target dir must be empty */ 6067c478bd9Sstevel@tonic-gate if (rmdir(target) < 0) { 6077c478bd9Sstevel@tonic-gate errno_save = errno; 6087c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 6097c478bd9Sstevel@tonic-gate gettext("%s: cannot rmdir %s: "), 6107c478bd9Sstevel@tonic-gate cmd, target); 6117c478bd9Sstevel@tonic-gate errno = errno_save; 6127c478bd9Sstevel@tonic-gate perror(""); 6137c478bd9Sstevel@tonic-gate return (1); 6147c478bd9Sstevel@tonic-gate } 6157c478bd9Sstevel@tonic-gate } 6167c478bd9Sstevel@tonic-gate #endif 6177c478bd9Sstevel@tonic-gate if ((n = copydir(source, target)) == 0) 6187c478bd9Sstevel@tonic-gate (void) rmdir(source); 6197c478bd9Sstevel@tonic-gate return (n); 6207c478bd9Sstevel@tonic-gate } 6217c478bd9Sstevel@tonic-gate 6227c478bd9Sstevel@tonic-gate /* doors can't be moved across filesystems */ 6237c478bd9Sstevel@tonic-gate if (ISDOOR(s1)) { 6247c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 6257c478bd9Sstevel@tonic-gate gettext("%s: %s: can't move door " 6267c478bd9Sstevel@tonic-gate "across file systems\n"), cmd, source); 6277c478bd9Sstevel@tonic-gate return (1); 6287c478bd9Sstevel@tonic-gate } 6297c478bd9Sstevel@tonic-gate /* 6307c478bd9Sstevel@tonic-gate * File can't be renamed, try to recreate the symbolic 6317c478bd9Sstevel@tonic-gate * link or special device, or copy the file wholesale 6327c478bd9Sstevel@tonic-gate * between file systems. 6337c478bd9Sstevel@tonic-gate */ 6347c478bd9Sstevel@tonic-gate if (ISLNK(s1)) { 6357c478bd9Sstevel@tonic-gate register int m; 6367c478bd9Sstevel@tonic-gate register mode_t md; 6377c478bd9Sstevel@tonic-gate char symln[PATH_MAX + 1]; 6387c478bd9Sstevel@tonic-gate 6397c478bd9Sstevel@tonic-gate if (targetexists && unlink(target) < 0) { 6407c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 6417c478bd9Sstevel@tonic-gate gettext("%s: cannot unlink %s: "), 6427c478bd9Sstevel@tonic-gate cmd, target); 6437c478bd9Sstevel@tonic-gate perror(""); 6447c478bd9Sstevel@tonic-gate return (1); 6457c478bd9Sstevel@tonic-gate } 6467c478bd9Sstevel@tonic-gate 6477c478bd9Sstevel@tonic-gate if ((m = readlink(source, symln, 6487c478bd9Sstevel@tonic-gate sizeof (symln) - 1)) < 0) { 6497c478bd9Sstevel@tonic-gate Perror(source); 6507c478bd9Sstevel@tonic-gate return (1); 6517c478bd9Sstevel@tonic-gate } 6527c478bd9Sstevel@tonic-gate symln[m] = '\0'; 6537c478bd9Sstevel@tonic-gate 6547c478bd9Sstevel@tonic-gate md = umask(~(s1.st_mode & MODEBITS)); 6557c478bd9Sstevel@tonic-gate if (symlink(symln, target) < 0) { 6567c478bd9Sstevel@tonic-gate Perror(target); 6577c478bd9Sstevel@tonic-gate return (1); 6587c478bd9Sstevel@tonic-gate } 6597c478bd9Sstevel@tonic-gate (void) umask(md); 6607c478bd9Sstevel@tonic-gate m = lchown(target, UID(s1), GID(s1)); 6617c478bd9Sstevel@tonic-gate #ifdef XPG4 6627c478bd9Sstevel@tonic-gate if (m < 0) { 6637c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s: cannot" 6647c478bd9Sstevel@tonic-gate " change owner and group of" 6657c478bd9Sstevel@tonic-gate " %s: "), cmd, target); 6667c478bd9Sstevel@tonic-gate perror(""); 6677c478bd9Sstevel@tonic-gate } 6687c478bd9Sstevel@tonic-gate #endif 6697c478bd9Sstevel@tonic-gate goto cleanup; 6707c478bd9Sstevel@tonic-gate } 6717c478bd9Sstevel@tonic-gate if (ISDEV(s1)) { 6727c478bd9Sstevel@tonic-gate 6737c478bd9Sstevel@tonic-gate if (targetexists && unlink(target) < 0) { 6747c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 6757c478bd9Sstevel@tonic-gate gettext("%s: cannot unlink %s: "), 6767c478bd9Sstevel@tonic-gate cmd, target); 6777c478bd9Sstevel@tonic-gate perror(""); 6787c478bd9Sstevel@tonic-gate return (1); 6797c478bd9Sstevel@tonic-gate } 6807c478bd9Sstevel@tonic-gate 6817c478bd9Sstevel@tonic-gate if (mknod(target, s1.st_mode, s1.st_rdev) < 0) { 6827c478bd9Sstevel@tonic-gate Perror(target); 6837c478bd9Sstevel@tonic-gate return (1); 6847c478bd9Sstevel@tonic-gate } 6857c478bd9Sstevel@tonic-gate 6867c478bd9Sstevel@tonic-gate (void) chg_mode(target, UID(s1), GID(s1), FMODE(s1)); 6877c478bd9Sstevel@tonic-gate (void) chg_time(target, s1); 6887c478bd9Sstevel@tonic-gate goto cleanup; 6897c478bd9Sstevel@tonic-gate } 6907c478bd9Sstevel@tonic-gate 6917c478bd9Sstevel@tonic-gate if (ISREG(s1)) { 6927c478bd9Sstevel@tonic-gate if (ISDIR(s2)) { 6937c478bd9Sstevel@tonic-gate if (targetexists && rmdir(target) < 0) { 6947c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 6957c478bd9Sstevel@tonic-gate gettext("%s: cannot rmdir %s: "), 6967c478bd9Sstevel@tonic-gate cmd, target); 6977c478bd9Sstevel@tonic-gate perror(""); 6987c478bd9Sstevel@tonic-gate return (1); 6997c478bd9Sstevel@tonic-gate } 7007c478bd9Sstevel@tonic-gate } else { 7017c478bd9Sstevel@tonic-gate if (targetexists && unlink(target) < 0) { 7027c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 7037c478bd9Sstevel@tonic-gate gettext("%s: cannot unlink %s: "), 7047c478bd9Sstevel@tonic-gate cmd, target); 7057c478bd9Sstevel@tonic-gate perror(""); 7067c478bd9Sstevel@tonic-gate return (1); 7077c478bd9Sstevel@tonic-gate } 7087c478bd9Sstevel@tonic-gate } 7097c478bd9Sstevel@tonic-gate 7107c478bd9Sstevel@tonic-gate 7117c478bd9Sstevel@tonic-gate copy: 7127c478bd9Sstevel@tonic-gate /* 7137c478bd9Sstevel@tonic-gate * If the source file is a symlink, and either 7147c478bd9Sstevel@tonic-gate * -P or -H flag (only if -H is specified and the 7157c478bd9Sstevel@tonic-gate * source file is not a command line argument) 7167c478bd9Sstevel@tonic-gate * were specified, then action is taken on the symlink 7177c478bd9Sstevel@tonic-gate * itself, not the file referenced by the symlink. 7187c478bd9Sstevel@tonic-gate * Note: this is executed for 'cp' only. 7197c478bd9Sstevel@tonic-gate */ 7207c478bd9Sstevel@tonic-gate if (cpy && (Pflg || (Hflg && !cmdarg)) && (ISLNK(s1))) { 7217c478bd9Sstevel@tonic-gate int m; 7227c478bd9Sstevel@tonic-gate mode_t md; 7237c478bd9Sstevel@tonic-gate char symln[PATH_MAX + 1]; 7247c478bd9Sstevel@tonic-gate 7257c478bd9Sstevel@tonic-gate m = readlink(source, symln, sizeof (symln) - 1); 7267c478bd9Sstevel@tonic-gate 7277c478bd9Sstevel@tonic-gate if (m < 0) { 7287c478bd9Sstevel@tonic-gate Perror(source); 7297c478bd9Sstevel@tonic-gate return (1); 7307c478bd9Sstevel@tonic-gate } 7317c478bd9Sstevel@tonic-gate symln[m] = '\0'; 7327c478bd9Sstevel@tonic-gate 7337c478bd9Sstevel@tonic-gate /* 7347c478bd9Sstevel@tonic-gate * Copy the sym link to the target. 7357c478bd9Sstevel@tonic-gate * Note: If the target exists, write a 7367c478bd9Sstevel@tonic-gate * diagnostic message, do nothing more 7377c478bd9Sstevel@tonic-gate * with the source file, and return to 7387c478bd9Sstevel@tonic-gate * process any remaining files. 7397c478bd9Sstevel@tonic-gate */ 7407c478bd9Sstevel@tonic-gate md = umask(~(s1.st_mode & MODEBITS)); 7417c478bd9Sstevel@tonic-gate if (symlink(symln, target) < 0) { 7427c478bd9Sstevel@tonic-gate Perror(target); 7437c478bd9Sstevel@tonic-gate return (1); 7447c478bd9Sstevel@tonic-gate } 7457c478bd9Sstevel@tonic-gate (void) umask(md); 7467c478bd9Sstevel@tonic-gate m = lchown(target, UID(s1), GID(s1)); 7477c478bd9Sstevel@tonic-gate 7487c478bd9Sstevel@tonic-gate if (m < 0) { 7497c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext( 7507c478bd9Sstevel@tonic-gate "cp: cannot change owner and " 7517c478bd9Sstevel@tonic-gate "group of %s:"), target); 7527c478bd9Sstevel@tonic-gate perror(""); 7537c478bd9Sstevel@tonic-gate } 7547c478bd9Sstevel@tonic-gate } else { 7557c478bd9Sstevel@tonic-gate /* 7567c478bd9Sstevel@tonic-gate * Copy the file. If it happens to be a 7577c478bd9Sstevel@tonic-gate * symlink, copy the file referenced 7587c478bd9Sstevel@tonic-gate * by the symlink. 7597c478bd9Sstevel@tonic-gate */ 7607c478bd9Sstevel@tonic-gate fi = open(source, O_RDONLY); 7617c478bd9Sstevel@tonic-gate if (fi < 0) { 7627c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 7637c478bd9Sstevel@tonic-gate gettext("%s: cannot open %s: "), 7647c478bd9Sstevel@tonic-gate cmd, source); 7657c478bd9Sstevel@tonic-gate perror(""); 7667c478bd9Sstevel@tonic-gate return (1); 7677c478bd9Sstevel@tonic-gate } 7687c478bd9Sstevel@tonic-gate 7697c478bd9Sstevel@tonic-gate fo = creat(target, s1.st_mode & MODEBITS); 7707c478bd9Sstevel@tonic-gate if (fo < 0) { 7717c478bd9Sstevel@tonic-gate /* 7727c478bd9Sstevel@tonic-gate * If -f and creat() failed, unlink 7737c478bd9Sstevel@tonic-gate * and try again. 7747c478bd9Sstevel@tonic-gate */ 7757c478bd9Sstevel@tonic-gate if (fflg) { 7767c478bd9Sstevel@tonic-gate (void) unlink(target); 7777c478bd9Sstevel@tonic-gate fo = creat(target, 7787c478bd9Sstevel@tonic-gate s1.st_mode & MODEBITS); 7797c478bd9Sstevel@tonic-gate } 7807c478bd9Sstevel@tonic-gate } 7817c478bd9Sstevel@tonic-gate if (fo < 0) { 7827c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 7837c478bd9Sstevel@tonic-gate gettext("%s: cannot create %s: "), 7847c478bd9Sstevel@tonic-gate cmd, target); 7857c478bd9Sstevel@tonic-gate perror(""); 7867c478bd9Sstevel@tonic-gate (void) close(fi); 7877c478bd9Sstevel@tonic-gate return (1); 7887c478bd9Sstevel@tonic-gate } else { 7897c478bd9Sstevel@tonic-gate /* stat the new file, its used below */ 7907c478bd9Sstevel@tonic-gate (void) stat(target, &s2); 7917c478bd9Sstevel@tonic-gate } 7927c478bd9Sstevel@tonic-gate 7937c478bd9Sstevel@tonic-gate /* 7947c478bd9Sstevel@tonic-gate * Set target's permissions to the source 7957c478bd9Sstevel@tonic-gate * before any copying so that any partially 7967c478bd9Sstevel@tonic-gate * copied file will have the source's 7977c478bd9Sstevel@tonic-gate * permissions (at most) or umask permissions 7987c478bd9Sstevel@tonic-gate * whichever is the most restrictive. 7997c478bd9Sstevel@tonic-gate * 8007c478bd9Sstevel@tonic-gate * ACL for regular files 8017c478bd9Sstevel@tonic-gate */ 8027c478bd9Sstevel@tonic-gate 8037c478bd9Sstevel@tonic-gate if (pflg || mve) { 8047c478bd9Sstevel@tonic-gate (void) chmod(target, FMODE(s1)); 805*fa9e4066Sahrens if (s1acl != NULL) { 806*fa9e4066Sahrens if ((acl_set(target, 807*fa9e4066Sahrens s1acl)) < 0) { 8087c478bd9Sstevel@tonic-gate if (pflg || mve) { 8097c478bd9Sstevel@tonic-gate (void) fprintf( 8107c478bd9Sstevel@tonic-gate stderr, 8117c478bd9Sstevel@tonic-gate "%s: failed to set acl entries on %s\n", 8127c478bd9Sstevel@tonic-gate cmd, 8137c478bd9Sstevel@tonic-gate target); 8147c478bd9Sstevel@tonic-gate } 8157c478bd9Sstevel@tonic-gate /* 8167c478bd9Sstevel@tonic-gate * else: silent and 8177c478bd9Sstevel@tonic-gate * continue 8187c478bd9Sstevel@tonic-gate */ 8197c478bd9Sstevel@tonic-gate } 8207c478bd9Sstevel@tonic-gate } 8217c478bd9Sstevel@tonic-gate } 8227c478bd9Sstevel@tonic-gate 8237c478bd9Sstevel@tonic-gate if (fstat(fi, &s1) < 0) { 8247c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 8257c478bd9Sstevel@tonic-gate gettext("%s: cannot access %s\n"), 8267c478bd9Sstevel@tonic-gate cmd, source); 8277c478bd9Sstevel@tonic-gate return (1); 8287c478bd9Sstevel@tonic-gate } 8297c478bd9Sstevel@tonic-gate if (IDENTICAL(s1, s2)) { 8307c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 8317c478bd9Sstevel@tonic-gate gettext( 8327c478bd9Sstevel@tonic-gate "%s: %s and %s are identical\n"), 8337c478bd9Sstevel@tonic-gate cmd, source, target); 8347c478bd9Sstevel@tonic-gate return (1); 8357c478bd9Sstevel@tonic-gate } 8367c478bd9Sstevel@tonic-gate 8377c478bd9Sstevel@tonic-gate if (writefile(fi, fo, source, target, 8387c478bd9Sstevel@tonic-gate &s1, &s2) != 0) { 8397c478bd9Sstevel@tonic-gate return (1); 8407c478bd9Sstevel@tonic-gate } 8417c478bd9Sstevel@tonic-gate 8427c478bd9Sstevel@tonic-gate (void) close(fi); 8437c478bd9Sstevel@tonic-gate if (close(fo) < 0) { 8447c478bd9Sstevel@tonic-gate Perror2(target, "write"); 8457c478bd9Sstevel@tonic-gate return (1); 8467c478bd9Sstevel@tonic-gate } 8477c478bd9Sstevel@tonic-gate } 8487c478bd9Sstevel@tonic-gate 8497c478bd9Sstevel@tonic-gate if (pflg || atflg || mve) { 8507c478bd9Sstevel@tonic-gate attret = copyattributes(source, target); 8517c478bd9Sstevel@tonic-gate if (attret != 0 && !attrsilent) { 8527c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext( 8537c478bd9Sstevel@tonic-gate "%s: Failed to preserve" 8547c478bd9Sstevel@tonic-gate " extended attributes of file" 8557c478bd9Sstevel@tonic-gate " %s\n"), cmd, source); 8567c478bd9Sstevel@tonic-gate } 8577c478bd9Sstevel@tonic-gate 8587c478bd9Sstevel@tonic-gate if (mve && attret != 0) { 8597c478bd9Sstevel@tonic-gate (void) unlink(target); 8607c478bd9Sstevel@tonic-gate return (1); 8617c478bd9Sstevel@tonic-gate } 8627c478bd9Sstevel@tonic-gate 8637c478bd9Sstevel@tonic-gate if (attrsilent) 8647c478bd9Sstevel@tonic-gate attret = 0; 8657c478bd9Sstevel@tonic-gate } 8667c478bd9Sstevel@tonic-gate 8677c478bd9Sstevel@tonic-gate /* 8687c478bd9Sstevel@tonic-gate * XPG4: the write system call will clear setgid 8697c478bd9Sstevel@tonic-gate * and setuid bits, so set them again. 8707c478bd9Sstevel@tonic-gate */ 8717c478bd9Sstevel@tonic-gate if (pflg || mve) { 8727c478bd9Sstevel@tonic-gate if ((ret = chg_mode(target, UID(s1), GID(s1), 8737c478bd9Sstevel@tonic-gate FMODE(s1))) > 0) 8747c478bd9Sstevel@tonic-gate return (1); 8757c478bd9Sstevel@tonic-gate if ((ret = chg_time(target, s1)) > 0) 8767c478bd9Sstevel@tonic-gate return (1); 8777c478bd9Sstevel@tonic-gate } 8787c478bd9Sstevel@tonic-gate if (cpy) { 8797c478bd9Sstevel@tonic-gate if (attret != 0) 8807c478bd9Sstevel@tonic-gate return (1); 8817c478bd9Sstevel@tonic-gate return (0); 8827c478bd9Sstevel@tonic-gate } 8837c478bd9Sstevel@tonic-gate goto cleanup; 8847c478bd9Sstevel@tonic-gate } 8857c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 8867c478bd9Sstevel@tonic-gate gettext("%s: %s: unknown file type 0x%x\n"), cmd, 8877c478bd9Sstevel@tonic-gate source, (s1.st_mode & S_IFMT)); 8887c478bd9Sstevel@tonic-gate return (1); 8897c478bd9Sstevel@tonic-gate 8907c478bd9Sstevel@tonic-gate cleanup: 8917c478bd9Sstevel@tonic-gate if (unlink(source) < 0) { 8927c478bd9Sstevel@tonic-gate (void) unlink(target); 8937c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 8947c478bd9Sstevel@tonic-gate gettext("%s: cannot unlink %s: "), 8957c478bd9Sstevel@tonic-gate cmd, source); 8967c478bd9Sstevel@tonic-gate perror(""); 8977c478bd9Sstevel@tonic-gate return (1); 8987c478bd9Sstevel@tonic-gate } 8997c478bd9Sstevel@tonic-gate if (attret != 0) 9007c478bd9Sstevel@tonic-gate return (attret); 9017c478bd9Sstevel@tonic-gate return (ret); 9027c478bd9Sstevel@tonic-gate } 9037c478bd9Sstevel@tonic-gate /*NOTREACHED*/ 904a77d64afScf return (ret); 9057c478bd9Sstevel@tonic-gate } 9067c478bd9Sstevel@tonic-gate 9077c478bd9Sstevel@tonic-gate static int 9087c478bd9Sstevel@tonic-gate writefile(int fi, int fo, char *source, char *target, 9097c478bd9Sstevel@tonic-gate struct stat *s1p, struct stat *s2p) 9107c478bd9Sstevel@tonic-gate { 9117c478bd9Sstevel@tonic-gate int mapsize, munmapsize; 9127c478bd9Sstevel@tonic-gate caddr_t cp; 9137c478bd9Sstevel@tonic-gate off_t filesize = s1p->st_size; 9147c478bd9Sstevel@tonic-gate off_t offset; 9157c478bd9Sstevel@tonic-gate int nbytes; 9167c478bd9Sstevel@tonic-gate int remains; 9177c478bd9Sstevel@tonic-gate int n; 9187c478bd9Sstevel@tonic-gate 9197c478bd9Sstevel@tonic-gate if (ISREG(*s1p) && s1p->st_size > SMALLFILESIZE) { 9207c478bd9Sstevel@tonic-gate /* 9217c478bd9Sstevel@tonic-gate * Determine size of initial mapping. This will determine the 9227c478bd9Sstevel@tonic-gate * size of the address space chunk we work with. This initial 9237c478bd9Sstevel@tonic-gate * mapping size will be used to perform munmap() in the future. 9247c478bd9Sstevel@tonic-gate */ 9257c478bd9Sstevel@tonic-gate mapsize = MAXMAPSIZE; 9267c478bd9Sstevel@tonic-gate if (s1p->st_size < mapsize) mapsize = s1p->st_size; 9277c478bd9Sstevel@tonic-gate munmapsize = mapsize; 9287c478bd9Sstevel@tonic-gate 9297c478bd9Sstevel@tonic-gate /* 9307c478bd9Sstevel@tonic-gate * Mmap time! 9317c478bd9Sstevel@tonic-gate */ 9327c478bd9Sstevel@tonic-gate if ((cp = mmap((caddr_t)NULL, mapsize, PROT_READ, 9337c478bd9Sstevel@tonic-gate MAP_SHARED, fi, (off_t)0)) == MAP_FAILED) 9347c478bd9Sstevel@tonic-gate mapsize = 0; /* can't mmap today */ 9357c478bd9Sstevel@tonic-gate } else 9367c478bd9Sstevel@tonic-gate mapsize = 0; 9377c478bd9Sstevel@tonic-gate 9387c478bd9Sstevel@tonic-gate if (mapsize != 0) { 9397c478bd9Sstevel@tonic-gate offset = 0; 9407c478bd9Sstevel@tonic-gate 9417c478bd9Sstevel@tonic-gate for (;;) { 9427c478bd9Sstevel@tonic-gate nbytes = write(fo, cp, mapsize); 9437c478bd9Sstevel@tonic-gate /* 9447c478bd9Sstevel@tonic-gate * if we write less than the mmaped size it's due to a 9457c478bd9Sstevel@tonic-gate * media error on the input file or out of space on 9467c478bd9Sstevel@tonic-gate * the output file. So, try again, and look for errno. 9477c478bd9Sstevel@tonic-gate */ 9487c478bd9Sstevel@tonic-gate if ((nbytes >= 0) && (nbytes != (int)mapsize)) { 9497c478bd9Sstevel@tonic-gate remains = mapsize - nbytes; 9507c478bd9Sstevel@tonic-gate while (remains > 0) { 9517c478bd9Sstevel@tonic-gate nbytes = write(fo, 9527c478bd9Sstevel@tonic-gate cp + mapsize - remains, remains); 9537c478bd9Sstevel@tonic-gate if (nbytes < 0) { 9547c478bd9Sstevel@tonic-gate if (errno == ENOSPC) 9557c478bd9Sstevel@tonic-gate Perror(target); 9567c478bd9Sstevel@tonic-gate else 9577c478bd9Sstevel@tonic-gate Perror(source); 9587c478bd9Sstevel@tonic-gate (void) close(fi); 9597c478bd9Sstevel@tonic-gate (void) close(fo); 9607c478bd9Sstevel@tonic-gate (void) munmap(cp, munmapsize); 9617c478bd9Sstevel@tonic-gate if (ISREG(*s2p)) 9627c478bd9Sstevel@tonic-gate (void) unlink(target); 9637c478bd9Sstevel@tonic-gate return (1); 9647c478bd9Sstevel@tonic-gate } 9657c478bd9Sstevel@tonic-gate remains -= nbytes; 9667c478bd9Sstevel@tonic-gate if (remains == 0) 9677c478bd9Sstevel@tonic-gate nbytes = mapsize; 9687c478bd9Sstevel@tonic-gate } 9697c478bd9Sstevel@tonic-gate } 9707c478bd9Sstevel@tonic-gate /* 9717c478bd9Sstevel@tonic-gate * although the write manual page doesn't specify this 9727c478bd9Sstevel@tonic-gate * as a possible errno, it is set when the nfs read 9737c478bd9Sstevel@tonic-gate * via the mmap'ed file is accessed, so report the 9747c478bd9Sstevel@tonic-gate * problem as a source access problem, not a target file 9757c478bd9Sstevel@tonic-gate * problem 9767c478bd9Sstevel@tonic-gate */ 9777c478bd9Sstevel@tonic-gate if (nbytes < 0) { 9787c478bd9Sstevel@tonic-gate if (errno == EACCES) 9797c478bd9Sstevel@tonic-gate Perror(source); 9807c478bd9Sstevel@tonic-gate else 9817c478bd9Sstevel@tonic-gate Perror(target); 9827c478bd9Sstevel@tonic-gate (void) close(fi); 9837c478bd9Sstevel@tonic-gate (void) close(fo); 9847c478bd9Sstevel@tonic-gate (void) munmap(cp, munmapsize); 9857c478bd9Sstevel@tonic-gate if (ISREG(*s2p)) 9867c478bd9Sstevel@tonic-gate (void) unlink(target); 9877c478bd9Sstevel@tonic-gate return (1); 9887c478bd9Sstevel@tonic-gate } 9897c478bd9Sstevel@tonic-gate filesize -= nbytes; 9907c478bd9Sstevel@tonic-gate if (filesize == 0) 9917c478bd9Sstevel@tonic-gate break; 9927c478bd9Sstevel@tonic-gate offset += nbytes; 9937c478bd9Sstevel@tonic-gate if (filesize < mapsize) 9947c478bd9Sstevel@tonic-gate mapsize = filesize; 9957c478bd9Sstevel@tonic-gate if (mmap(cp, mapsize, PROT_READ, MAP_SHARED | MAP_FIXED, 9967c478bd9Sstevel@tonic-gate fi, offset) == MAP_FAILED) { 9977c478bd9Sstevel@tonic-gate Perror(source); 9987c478bd9Sstevel@tonic-gate (void) close(fi); 9997c478bd9Sstevel@tonic-gate (void) close(fo); 10007c478bd9Sstevel@tonic-gate (void) munmap(cp, munmapsize); 10017c478bd9Sstevel@tonic-gate if (ISREG(*s2p)) 10027c478bd9Sstevel@tonic-gate (void) unlink(target); 10037c478bd9Sstevel@tonic-gate return (1); 10047c478bd9Sstevel@tonic-gate } 10057c478bd9Sstevel@tonic-gate } 10067c478bd9Sstevel@tonic-gate (void) munmap(cp, munmapsize); 10077c478bd9Sstevel@tonic-gate } else { 10087c478bd9Sstevel@tonic-gate char buf[SMALLFILESIZE]; 10097c478bd9Sstevel@tonic-gate for (;;) { 10107c478bd9Sstevel@tonic-gate n = read(fi, buf, sizeof (buf)); 10117c478bd9Sstevel@tonic-gate if (n == 0) { 10127c478bd9Sstevel@tonic-gate return (0); 10137c478bd9Sstevel@tonic-gate } else if (n < 0) { 10147c478bd9Sstevel@tonic-gate Perror2(source, "read"); 10157c478bd9Sstevel@tonic-gate (void) close(fi); 10167c478bd9Sstevel@tonic-gate (void) close(fo); 10177c478bd9Sstevel@tonic-gate if (ISREG(*s2p)) 10187c478bd9Sstevel@tonic-gate (void) unlink(target); 10197c478bd9Sstevel@tonic-gate return (1); 10207c478bd9Sstevel@tonic-gate } else if (write(fo, buf, n) != n) { 10217c478bd9Sstevel@tonic-gate Perror2(target, "write"); 10227c478bd9Sstevel@tonic-gate (void) close(fi); 10237c478bd9Sstevel@tonic-gate (void) close(fo); 10247c478bd9Sstevel@tonic-gate if (ISREG(*s2p)) 10257c478bd9Sstevel@tonic-gate (void) unlink(target); 10267c478bd9Sstevel@tonic-gate return (1); 10277c478bd9Sstevel@tonic-gate } 10287c478bd9Sstevel@tonic-gate } 10297c478bd9Sstevel@tonic-gate } 10307c478bd9Sstevel@tonic-gate return (0); 10317c478bd9Sstevel@tonic-gate } 10327c478bd9Sstevel@tonic-gate 10337c478bd9Sstevel@tonic-gate /* 10347c478bd9Sstevel@tonic-gate * create_tnode() 10357c478bd9Sstevel@tonic-gate * 10367c478bd9Sstevel@tonic-gate * Create a node for use with the search tree which contains the 10377c478bd9Sstevel@tonic-gate * inode information (device id and inode number). 10387c478bd9Sstevel@tonic-gate * 10397c478bd9Sstevel@tonic-gate * Input 10407c478bd9Sstevel@tonic-gate * dev - device id 10417c478bd9Sstevel@tonic-gate * ino - inode number 10427c478bd9Sstevel@tonic-gate * 10437c478bd9Sstevel@tonic-gate * Output 10447c478bd9Sstevel@tonic-gate * tnode - NULL on error, otherwise returns a tnode structure 10457c478bd9Sstevel@tonic-gate * which contains the input device id and inode number. 10467c478bd9Sstevel@tonic-gate */ 10477c478bd9Sstevel@tonic-gate static tree_node_t * 10487c478bd9Sstevel@tonic-gate create_tnode(dev_t dev, ino_t ino) 10497c478bd9Sstevel@tonic-gate { 10507c478bd9Sstevel@tonic-gate tree_node_t *tnode; 10517c478bd9Sstevel@tonic-gate 10527c478bd9Sstevel@tonic-gate if ((tnode = (tree_node_t *)malloc(sizeof (tree_node_t))) != NULL) { 10537c478bd9Sstevel@tonic-gate tnode->node_dev = dev; 10547c478bd9Sstevel@tonic-gate tnode->node_ino = ino; 10557c478bd9Sstevel@tonic-gate } 10567c478bd9Sstevel@tonic-gate 10577c478bd9Sstevel@tonic-gate return (tnode); 10587c478bd9Sstevel@tonic-gate } 10597c478bd9Sstevel@tonic-gate 10607c478bd9Sstevel@tonic-gate static int 10617c478bd9Sstevel@tonic-gate chkfiles(char *source, char **to) 10627c478bd9Sstevel@tonic-gate { 10637c478bd9Sstevel@tonic-gate char *buf = (char *)NULL; 10647c478bd9Sstevel@tonic-gate int (*statf)() = (cpy && 10657c478bd9Sstevel@tonic-gate !(Pflg || (Hflg && !cmdarg))) ? stat : lstat; 10667c478bd9Sstevel@tonic-gate char *target = *to; 1067*fa9e4066Sahrens int error; 10687c478bd9Sstevel@tonic-gate 10697c478bd9Sstevel@tonic-gate /* 10707c478bd9Sstevel@tonic-gate * Make sure source file exists. 10717c478bd9Sstevel@tonic-gate */ 10727c478bd9Sstevel@tonic-gate if ((*statf)(source, &s1) < 0) { 10737c478bd9Sstevel@tonic-gate /* 10747c478bd9Sstevel@tonic-gate * Keep the old error message except when someone tries to 10757c478bd9Sstevel@tonic-gate * mv/cp/ln a symbolic link that has a trailing slash and 10767c478bd9Sstevel@tonic-gate * points to a file. 10777c478bd9Sstevel@tonic-gate */ 10787c478bd9Sstevel@tonic-gate if (errno == ENOTDIR) 10797c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: %s: %s\n", cmd, source, 10807c478bd9Sstevel@tonic-gate strerror(errno)); 10817c478bd9Sstevel@tonic-gate else 10827c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 10837c478bd9Sstevel@tonic-gate gettext("%s: cannot access %s\n"), cmd, source); 10847c478bd9Sstevel@tonic-gate return (1); 10857c478bd9Sstevel@tonic-gate } 10867c478bd9Sstevel@tonic-gate 10877c478bd9Sstevel@tonic-gate /* 10887c478bd9Sstevel@tonic-gate * Get ACL info: don't bother with ln or mv'ing symlinks 10897c478bd9Sstevel@tonic-gate */ 10907c478bd9Sstevel@tonic-gate if ((!lnk) && !(mve && ISLNK(s1))) { 1091*fa9e4066Sahrens if (s1acl != NULL) { 1092*fa9e4066Sahrens acl_free(s1acl); 1093*fa9e4066Sahrens s1acl = NULL; 10947c478bd9Sstevel@tonic-gate } 1095*fa9e4066Sahrens if ((error = acl_get(source, ACL_NO_TRIVIAL, &s1acl)) != 0) { 10967c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 1097*fa9e4066Sahrens "%s: failed to get acl entries: %s\n", source, 1098*fa9e4066Sahrens acl_strerror(error)); 10997c478bd9Sstevel@tonic-gate return (1); 11007c478bd9Sstevel@tonic-gate } 11017c478bd9Sstevel@tonic-gate /* else: just permission bits */ 11027c478bd9Sstevel@tonic-gate } 11037c478bd9Sstevel@tonic-gate 11047c478bd9Sstevel@tonic-gate /* 11057c478bd9Sstevel@tonic-gate * If stat fails, then the target doesn't exist, 11067c478bd9Sstevel@tonic-gate * we will create a new target with default file type of regular. 11077c478bd9Sstevel@tonic-gate */ 11087c478bd9Sstevel@tonic-gate 11097c478bd9Sstevel@tonic-gate FTYPE(s2) = S_IFREG; 11107c478bd9Sstevel@tonic-gate targetexists = 0; 11117c478bd9Sstevel@tonic-gate if ((*statf)(target, &s2) >= 0) { 11127c478bd9Sstevel@tonic-gate if (ISLNK(s2)) 11137c478bd9Sstevel@tonic-gate (void) stat(target, &s2); 11147c478bd9Sstevel@tonic-gate /* 11157c478bd9Sstevel@tonic-gate * If target is a directory, 11167c478bd9Sstevel@tonic-gate * make complete name of new file 11177c478bd9Sstevel@tonic-gate * within that directory. 11187c478bd9Sstevel@tonic-gate */ 11197c478bd9Sstevel@tonic-gate if (ISDIR(s2)) { 11207c478bd9Sstevel@tonic-gate size_t len; 11217c478bd9Sstevel@tonic-gate 11227c478bd9Sstevel@tonic-gate len = strlen(target) + strlen(dname(source)) + 4; 11237c478bd9Sstevel@tonic-gate if ((buf = (char *)malloc(len)) == NULL) { 11247c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 11257c478bd9Sstevel@tonic-gate gettext("%s: Insufficient memory to " 11267c478bd9Sstevel@tonic-gate "%s %s\n "), cmd, cmd, source); 11277c478bd9Sstevel@tonic-gate exit(3); 11287c478bd9Sstevel@tonic-gate } 11297c478bd9Sstevel@tonic-gate (void) snprintf(buf, len, "%s/%s", 11307c478bd9Sstevel@tonic-gate target, dname(source)); 11317c478bd9Sstevel@tonic-gate *to = target = buf; 11327c478bd9Sstevel@tonic-gate } 11337c478bd9Sstevel@tonic-gate 11347c478bd9Sstevel@tonic-gate if ((*statf)(target, &s2) >= 0) { 11357c478bd9Sstevel@tonic-gate int overwrite = FALSE; 11367c478bd9Sstevel@tonic-gate int override = FALSE; 11377c478bd9Sstevel@tonic-gate 11387c478bd9Sstevel@tonic-gate targetexists++; 11397c478bd9Sstevel@tonic-gate if (cpy || mve) { 11407c478bd9Sstevel@tonic-gate /* 11417c478bd9Sstevel@tonic-gate * For cp and mv, it is an error if the 11427c478bd9Sstevel@tonic-gate * source and target are the same file. 11437c478bd9Sstevel@tonic-gate * Check for the same inode and file 11447c478bd9Sstevel@tonic-gate * system, but don't check for the same 11457c478bd9Sstevel@tonic-gate * absolute pathname because it is an 11467c478bd9Sstevel@tonic-gate * error when the source and target are 11477c478bd9Sstevel@tonic-gate * hard links to the same file. 11487c478bd9Sstevel@tonic-gate */ 11497c478bd9Sstevel@tonic-gate if (IDENTICAL(s1, s2)) { 11507c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 11517c478bd9Sstevel@tonic-gate gettext( 11527c478bd9Sstevel@tonic-gate "%s: %s and %s are identical\n"), 11537c478bd9Sstevel@tonic-gate cmd, source, target); 11547c478bd9Sstevel@tonic-gate if (buf != NULL) 11557c478bd9Sstevel@tonic-gate free(buf); 11567c478bd9Sstevel@tonic-gate return (1); 11577c478bd9Sstevel@tonic-gate } 11587c478bd9Sstevel@tonic-gate } 11597c478bd9Sstevel@tonic-gate if (lnk) { 11607c478bd9Sstevel@tonic-gate /* 11617c478bd9Sstevel@tonic-gate * For ln, it is an error if the source and 11627c478bd9Sstevel@tonic-gate * target are identical files (same inode, 11637c478bd9Sstevel@tonic-gate * same file system, and filenames resolve 11647c478bd9Sstevel@tonic-gate * to same absolute pathname). 11657c478bd9Sstevel@tonic-gate */ 11667c478bd9Sstevel@tonic-gate if (!chk_different(source, target)) { 11677c478bd9Sstevel@tonic-gate if (buf != NULL) 11687c478bd9Sstevel@tonic-gate free(buf); 11697c478bd9Sstevel@tonic-gate return (1); 11707c478bd9Sstevel@tonic-gate } 11717c478bd9Sstevel@tonic-gate } 11727c478bd9Sstevel@tonic-gate if (lnk && !silent) { 11737c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 11747c478bd9Sstevel@tonic-gate gettext("%s: %s: File exists\n"), 11757c478bd9Sstevel@tonic-gate cmd, target); 11767c478bd9Sstevel@tonic-gate if (buf != NULL) 11777c478bd9Sstevel@tonic-gate free(buf); 11787c478bd9Sstevel@tonic-gate return (1); 11797c478bd9Sstevel@tonic-gate } 11807c478bd9Sstevel@tonic-gate 11817c478bd9Sstevel@tonic-gate /* 11827c478bd9Sstevel@tonic-gate * overwrite: 11837c478bd9Sstevel@tonic-gate * If the user does not have access to 11847c478bd9Sstevel@tonic-gate * the target, ask ----if it is not 11857c478bd9Sstevel@tonic-gate * silent and user invoked command 11867c478bd9Sstevel@tonic-gate * interactively. 11877c478bd9Sstevel@tonic-gate * 11887c478bd9Sstevel@tonic-gate * override: 11897c478bd9Sstevel@tonic-gate * If not silent, and stdin is a terminal, and 11907c478bd9Sstevel@tonic-gate * there's no write access, and the file isn't a 11917c478bd9Sstevel@tonic-gate * symbolic link, ask for permission. 11927c478bd9Sstevel@tonic-gate * 11937c478bd9Sstevel@tonic-gate * XPG4: both overwrite and override: 11947c478bd9Sstevel@tonic-gate * ask only one question. 11957c478bd9Sstevel@tonic-gate * 11967c478bd9Sstevel@tonic-gate * TRANSLATION_NOTE - The following messages will 11977c478bd9Sstevel@tonic-gate * contain the first character of the strings for 11987c478bd9Sstevel@tonic-gate * "yes" and "no" defined in the file 11997c478bd9Sstevel@tonic-gate * "nl_langinfo.po". After substitution, the 12007c478bd9Sstevel@tonic-gate * message will appear as follows: 12017c478bd9Sstevel@tonic-gate * <cmd>: overwrite <filename> (y/n)? 12027c478bd9Sstevel@tonic-gate * where <cmd> is the name of the command 12037c478bd9Sstevel@tonic-gate * (cp, mv) and <filename> is the destination file 12047c478bd9Sstevel@tonic-gate */ 12057c478bd9Sstevel@tonic-gate 12067c478bd9Sstevel@tonic-gate 12077c478bd9Sstevel@tonic-gate overwrite = iflg && !silent && use_stdin(); 12087c478bd9Sstevel@tonic-gate override = !cpy && (access(target, 2) < 0) && 12097c478bd9Sstevel@tonic-gate !silent && use_stdin() && !ISLNK(s2); 12107c478bd9Sstevel@tonic-gate 12117c478bd9Sstevel@tonic-gate if (overwrite && override) 12127c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 12137c478bd9Sstevel@tonic-gate gettext("%s: overwrite %s and override " 12147c478bd9Sstevel@tonic-gate "protection %o (%s/%s)? "), cmd, target, 12157c478bd9Sstevel@tonic-gate FMODE(s2) & MODEBITS, yeschr, nochr); 12167c478bd9Sstevel@tonic-gate else if (overwrite && ISREG(s2)) 12177c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 12187c478bd9Sstevel@tonic-gate gettext("%s: overwrite %s (%s/%s)? "), 12197c478bd9Sstevel@tonic-gate cmd, target, yeschr, nochr); 12207c478bd9Sstevel@tonic-gate else if (override) 12217c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 12227c478bd9Sstevel@tonic-gate gettext("%s: %s: override protection " 12237c478bd9Sstevel@tonic-gate /*CSTYLED*/ 12247c478bd9Sstevel@tonic-gate "%o (%s/%s)? "), 12257c478bd9Sstevel@tonic-gate /*CSTYLED*/ 12267c478bd9Sstevel@tonic-gate cmd, target, FMODE(s2) & MODEBITS, 12277c478bd9Sstevel@tonic-gate yeschr, nochr); 12287c478bd9Sstevel@tonic-gate if (overwrite || override) { 12297c478bd9Sstevel@tonic-gate if (ISREG(s2)) { 12307c478bd9Sstevel@tonic-gate if (getresp()) { 12317c478bd9Sstevel@tonic-gate if (buf != NULL) 12327c478bd9Sstevel@tonic-gate free(buf); 12337c478bd9Sstevel@tonic-gate return (2); 12347c478bd9Sstevel@tonic-gate } 12357c478bd9Sstevel@tonic-gate } 12367c478bd9Sstevel@tonic-gate } 12377c478bd9Sstevel@tonic-gate if (lnk && unlink(target) < 0) { 12387c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 12397c478bd9Sstevel@tonic-gate gettext("%s: cannot unlink %s: "), 12407c478bd9Sstevel@tonic-gate cmd, target); 12417c478bd9Sstevel@tonic-gate perror(""); 12427c478bd9Sstevel@tonic-gate return (1); 12437c478bd9Sstevel@tonic-gate } 12447c478bd9Sstevel@tonic-gate } 12457c478bd9Sstevel@tonic-gate } 12467c478bd9Sstevel@tonic-gate return (0); 12477c478bd9Sstevel@tonic-gate } 12487c478bd9Sstevel@tonic-gate 12497c478bd9Sstevel@tonic-gate /* 12507c478bd9Sstevel@tonic-gate * check whether source and target are different 12517c478bd9Sstevel@tonic-gate * return 1 when they are different 12527c478bd9Sstevel@tonic-gate * return 0 when they are identical, or when unable to resolve a pathname 12537c478bd9Sstevel@tonic-gate */ 12547c478bd9Sstevel@tonic-gate static int 12557c478bd9Sstevel@tonic-gate chk_different(char *source, char *target) 12567c478bd9Sstevel@tonic-gate { 12577c478bd9Sstevel@tonic-gate char rtarget[PATH_MAX], rsource[PATH_MAX]; 12587c478bd9Sstevel@tonic-gate 12597c478bd9Sstevel@tonic-gate if (IDENTICAL(s1, s2)) { 12607c478bd9Sstevel@tonic-gate /* 12617c478bd9Sstevel@tonic-gate * IDENTICAL will be true for hard links, therefore 12627c478bd9Sstevel@tonic-gate * check whether the filenames are different 12637c478bd9Sstevel@tonic-gate */ 12647c478bd9Sstevel@tonic-gate if ((getrealpath(source, rsource) == 0) || 12657c478bd9Sstevel@tonic-gate (getrealpath(target, rtarget) == 0)) { 12667c478bd9Sstevel@tonic-gate return (0); 12677c478bd9Sstevel@tonic-gate } 12687c478bd9Sstevel@tonic-gate if (strncmp(rsource, rtarget, PATH_MAX) == 0) { 12697c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext( 12707c478bd9Sstevel@tonic-gate "%s: %s and %s are identical\n"), 12717c478bd9Sstevel@tonic-gate cmd, source, target); 12727c478bd9Sstevel@tonic-gate return (0); 12737c478bd9Sstevel@tonic-gate } 12747c478bd9Sstevel@tonic-gate } 12757c478bd9Sstevel@tonic-gate return (1); 12767c478bd9Sstevel@tonic-gate } 12777c478bd9Sstevel@tonic-gate 12787c478bd9Sstevel@tonic-gate /* 12797c478bd9Sstevel@tonic-gate * get real path (resolved absolute pathname) 12807c478bd9Sstevel@tonic-gate * return 1 on success, 0 on failure 12817c478bd9Sstevel@tonic-gate */ 12827c478bd9Sstevel@tonic-gate static int 12837c478bd9Sstevel@tonic-gate getrealpath(char *path, char *rpath) 12847c478bd9Sstevel@tonic-gate { 12857c478bd9Sstevel@tonic-gate if (realpath(path, rpath) == NULL) { 12867c478bd9Sstevel@tonic-gate int errno_save = errno; 12877c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext( 12887c478bd9Sstevel@tonic-gate "%s: can't resolve path %s: "), cmd, path); 12897c478bd9Sstevel@tonic-gate errno = errno_save; 12907c478bd9Sstevel@tonic-gate perror(""); 12917c478bd9Sstevel@tonic-gate return (0); 12927c478bd9Sstevel@tonic-gate } 12937c478bd9Sstevel@tonic-gate return (1); 12947c478bd9Sstevel@tonic-gate } 12957c478bd9Sstevel@tonic-gate 12967c478bd9Sstevel@tonic-gate static int 12977c478bd9Sstevel@tonic-gate rcopy(char *from, char *to) 12987c478bd9Sstevel@tonic-gate { 12997c478bd9Sstevel@tonic-gate DIR *fold = opendir(from); 13007c478bd9Sstevel@tonic-gate struct dirent *dp; 13017c478bd9Sstevel@tonic-gate struct stat statb, s1save; 13027c478bd9Sstevel@tonic-gate int errs = 0; 13037c478bd9Sstevel@tonic-gate char fromname[PATH_MAX]; 13047c478bd9Sstevel@tonic-gate 13057c478bd9Sstevel@tonic-gate if (fold == 0 || ((pflg || mve) && fstat(fold->dd_fd, &statb) < 0)) { 13067c478bd9Sstevel@tonic-gate Perror(from); 13077c478bd9Sstevel@tonic-gate return (1); 13087c478bd9Sstevel@tonic-gate } 13097c478bd9Sstevel@tonic-gate if (pflg || mve) { 13107c478bd9Sstevel@tonic-gate /* 13117c478bd9Sstevel@tonic-gate * Save s1 (stat information for source dir) so that 13127c478bd9Sstevel@tonic-gate * mod and access times can be reserved during "cp -p" 13137c478bd9Sstevel@tonic-gate * or mv, since s1 gets overwritten. 13147c478bd9Sstevel@tonic-gate */ 13157c478bd9Sstevel@tonic-gate s1save = s1; 13167c478bd9Sstevel@tonic-gate } 13177c478bd9Sstevel@tonic-gate for (;;) { 13187c478bd9Sstevel@tonic-gate dp = readdir(fold); 13197c478bd9Sstevel@tonic-gate if (dp == 0) { 13207c478bd9Sstevel@tonic-gate (void) closedir(fold); 13217c478bd9Sstevel@tonic-gate if (pflg || mve) 13227c478bd9Sstevel@tonic-gate return (chg_time(to, s1save) + errs); 13237c478bd9Sstevel@tonic-gate return (errs); 13247c478bd9Sstevel@tonic-gate } 13257c478bd9Sstevel@tonic-gate if (dp->d_ino == 0) 13267c478bd9Sstevel@tonic-gate continue; 13277c478bd9Sstevel@tonic-gate if ((strcmp(dp->d_name, ".") == 0) || 13287c478bd9Sstevel@tonic-gate (strcmp(dp->d_name, "..") == 0)) 13297c478bd9Sstevel@tonic-gate continue; 13307c478bd9Sstevel@tonic-gate if (strlen(from)+1+strlen(dp->d_name) >= 13317c478bd9Sstevel@tonic-gate sizeof (fromname) - 1) { 13327c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 13337c478bd9Sstevel@tonic-gate gettext("%s : %s/%s: Name too long\n"), 13347c478bd9Sstevel@tonic-gate cmd, from, dp->d_name); 13357c478bd9Sstevel@tonic-gate errs++; 13367c478bd9Sstevel@tonic-gate continue; 13377c478bd9Sstevel@tonic-gate } 13387c478bd9Sstevel@tonic-gate (void) snprintf(fromname, sizeof (fromname), 13397c478bd9Sstevel@tonic-gate "%s/%s", from, dp->d_name); 13407c478bd9Sstevel@tonic-gate errs += cpymve(fromname, to); 13417c478bd9Sstevel@tonic-gate } 13427c478bd9Sstevel@tonic-gate } 13437c478bd9Sstevel@tonic-gate 13447c478bd9Sstevel@tonic-gate static char * 13457c478bd9Sstevel@tonic-gate dname(char *name) 13467c478bd9Sstevel@tonic-gate { 13477c478bd9Sstevel@tonic-gate register char *p; 13487c478bd9Sstevel@tonic-gate 13497c478bd9Sstevel@tonic-gate /* 13507c478bd9Sstevel@tonic-gate * Return just the file name given the complete path. 13517c478bd9Sstevel@tonic-gate * Like basename(1). 13527c478bd9Sstevel@tonic-gate */ 13537c478bd9Sstevel@tonic-gate 13547c478bd9Sstevel@tonic-gate p = name; 13557c478bd9Sstevel@tonic-gate 13567c478bd9Sstevel@tonic-gate /* 13577c478bd9Sstevel@tonic-gate * While there are characters left, 13587c478bd9Sstevel@tonic-gate * set name to start after last 13597c478bd9Sstevel@tonic-gate * delimiter. 13607c478bd9Sstevel@tonic-gate */ 13617c478bd9Sstevel@tonic-gate 13627c478bd9Sstevel@tonic-gate while (*p) 13637c478bd9Sstevel@tonic-gate if (*p++ == DELIM && *p) 13647c478bd9Sstevel@tonic-gate name = p; 13657c478bd9Sstevel@tonic-gate return (name); 13667c478bd9Sstevel@tonic-gate } 13677c478bd9Sstevel@tonic-gate 13687c478bd9Sstevel@tonic-gate static int 13697c478bd9Sstevel@tonic-gate getresp(void) 13707c478bd9Sstevel@tonic-gate { 13717c478bd9Sstevel@tonic-gate register int c, i; 13727c478bd9Sstevel@tonic-gate char ans_buf[SCHAR_MAX + 1]; 13737c478bd9Sstevel@tonic-gate 13747c478bd9Sstevel@tonic-gate /* 13757c478bd9Sstevel@tonic-gate * Get response from user. Based on 13767c478bd9Sstevel@tonic-gate * first character, make decision. 13777c478bd9Sstevel@tonic-gate * Discard rest of line. 13787c478bd9Sstevel@tonic-gate */ 13797c478bd9Sstevel@tonic-gate for (i = 0; ; i++) { 13807c478bd9Sstevel@tonic-gate c = getchar(); 13817c478bd9Sstevel@tonic-gate if (c == '\n' || c == 0 || c == EOF) { 13827c478bd9Sstevel@tonic-gate ans_buf[i] = 0; 13837c478bd9Sstevel@tonic-gate break; 13847c478bd9Sstevel@tonic-gate } 13857c478bd9Sstevel@tonic-gate if (i < SCHAR_MAX) 13867c478bd9Sstevel@tonic-gate ans_buf[i] = c; 13877c478bd9Sstevel@tonic-gate } 13887c478bd9Sstevel@tonic-gate if (i >= SCHAR_MAX) { 13897c478bd9Sstevel@tonic-gate i = SCHAR_MAX; 13907c478bd9Sstevel@tonic-gate ans_buf[SCHAR_MAX] = 0; 13917c478bd9Sstevel@tonic-gate } 13927c478bd9Sstevel@tonic-gate if ((i == 0) | (strncmp(yeschr, ans_buf, i))) 13937c478bd9Sstevel@tonic-gate return (1); 13947c478bd9Sstevel@tonic-gate return (0); 13957c478bd9Sstevel@tonic-gate } 13967c478bd9Sstevel@tonic-gate 13977c478bd9Sstevel@tonic-gate static void 13987c478bd9Sstevel@tonic-gate usage(void) 13997c478bd9Sstevel@tonic-gate { 14007c478bd9Sstevel@tonic-gate /* 14017c478bd9Sstevel@tonic-gate * Display usage message. 14027c478bd9Sstevel@tonic-gate */ 14037c478bd9Sstevel@tonic-gate 14047c478bd9Sstevel@tonic-gate if (mve) { 14057c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext( 14067c478bd9Sstevel@tonic-gate "Usage: mv [-f] [-i] f1 f2\n" 14077c478bd9Sstevel@tonic-gate " mv [-f] [-i] f1 ... fn d1\n" 14087c478bd9Sstevel@tonic-gate " mv [-f] [-i] d1 d2\n")); 14097c478bd9Sstevel@tonic-gate } else if (lnk) { 14107c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext( 14117c478bd9Sstevel@tonic-gate #ifdef XPG4 14127c478bd9Sstevel@tonic-gate "Usage: ln [-f] [-s] f1 [f2]\n" 14137c478bd9Sstevel@tonic-gate " ln [-f] [-s] f1 ... fn d1\n" 14147c478bd9Sstevel@tonic-gate " ln [-f] -s d1 d2\n")); 14157c478bd9Sstevel@tonic-gate #else 14167c478bd9Sstevel@tonic-gate "Usage: ln [-f] [-n] [-s] f1 [f2]\n" 14177c478bd9Sstevel@tonic-gate " ln [-f] [-n] [-s] f1 ... fn d1\n" 14187c478bd9Sstevel@tonic-gate " ln [-f] [-n] -s d1 d2\n")); 14197c478bd9Sstevel@tonic-gate #endif 14207c478bd9Sstevel@tonic-gate } else if (cpy) { 14217c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext( 14227c478bd9Sstevel@tonic-gate "Usage: cp [-f] [-i] [-p] [-@] f1 f2\n" 14237c478bd9Sstevel@tonic-gate " cp [-f] [-i] [-p] [-@] f1 ... fn d1\n" 14247c478bd9Sstevel@tonic-gate " cp -r|-R [-H|-L|-P] [-f] [-i] [-p] [-@] " 14257c478bd9Sstevel@tonic-gate "d1 ... dn-1 dn\n")); 14267c478bd9Sstevel@tonic-gate } 14277c478bd9Sstevel@tonic-gate exit(2); 14287c478bd9Sstevel@tonic-gate } 14297c478bd9Sstevel@tonic-gate 14307c478bd9Sstevel@tonic-gate /* 14317c478bd9Sstevel@tonic-gate * chg_time() 14327c478bd9Sstevel@tonic-gate * 14337c478bd9Sstevel@tonic-gate * Try to preserve modification and access time. 14347c478bd9Sstevel@tonic-gate * If 1) pflg is not set, or 2) pflg is set and this is the Solaris version, 14357c478bd9Sstevel@tonic-gate * don't report a utime() failure. 14367c478bd9Sstevel@tonic-gate * If this is the XPG4 version and utime fails, if 1) pflg is set (cp -p) 14377c478bd9Sstevel@tonic-gate * or 2) we are doing a mv, print a diagnostic message; arrange for a non-zero 14387c478bd9Sstevel@tonic-gate * exit status only if pflg is set. 14397c478bd9Sstevel@tonic-gate * utimes(2) is being used to achieve granularity in 14407c478bd9Sstevel@tonic-gate * microseconds while setting file times. 14417c478bd9Sstevel@tonic-gate */ 14427c478bd9Sstevel@tonic-gate static int 14437c478bd9Sstevel@tonic-gate chg_time(char *to, struct stat ss) 14447c478bd9Sstevel@tonic-gate { 14457c478bd9Sstevel@tonic-gate struct timeval times[2]; 14467c478bd9Sstevel@tonic-gate int rc; 14477c478bd9Sstevel@tonic-gate 14487c478bd9Sstevel@tonic-gate timestruc_to_timeval(&ss.st_atim, times); 14497c478bd9Sstevel@tonic-gate timestruc_to_timeval(&ss.st_mtim, times + 1); 14507c478bd9Sstevel@tonic-gate 14517c478bd9Sstevel@tonic-gate rc = utimes(to, times); 14527c478bd9Sstevel@tonic-gate #ifdef XPG4 14537c478bd9Sstevel@tonic-gate if ((pflg || mve) && rc != 0) { 14547c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 14557c478bd9Sstevel@tonic-gate gettext("%s: cannot set times for %s: "), cmd, to); 14567c478bd9Sstevel@tonic-gate perror(""); 14577c478bd9Sstevel@tonic-gate if (pflg) 14587c478bd9Sstevel@tonic-gate return (1); 14597c478bd9Sstevel@tonic-gate } 14607c478bd9Sstevel@tonic-gate #endif 14617c478bd9Sstevel@tonic-gate 14627c478bd9Sstevel@tonic-gate return (0); 14637c478bd9Sstevel@tonic-gate 14647c478bd9Sstevel@tonic-gate } 14657c478bd9Sstevel@tonic-gate 14667c478bd9Sstevel@tonic-gate /* 14677c478bd9Sstevel@tonic-gate * chg_mode() 14687c478bd9Sstevel@tonic-gate * 14697c478bd9Sstevel@tonic-gate * This function is called upon "cp -p" or mv across filesystems. 14707c478bd9Sstevel@tonic-gate * 14717c478bd9Sstevel@tonic-gate * Try to preserve the owner and group id. If chown() fails, 14727c478bd9Sstevel@tonic-gate * only print a diagnostic message if doing a mv in the XPG4 version; 14737c478bd9Sstevel@tonic-gate * try to clear S_ISUID and S_ISGID bits in the target. If unable to clear 14747c478bd9Sstevel@tonic-gate * S_ISUID and S_ISGID bits, print a diagnostic message and arrange for a 14757c478bd9Sstevel@tonic-gate * non-zero exit status because this is a security violation. 14767c478bd9Sstevel@tonic-gate * Try to preserve permissions. 14777c478bd9Sstevel@tonic-gate * If this is the XPG4 version and chmod() fails, print a diagnostic message 14787c478bd9Sstevel@tonic-gate * and arrange for a non-zero exit status. 14797c478bd9Sstevel@tonic-gate * If this is the Solaris version and chmod() fails, do not print a 14807c478bd9Sstevel@tonic-gate * diagnostic message or exit with a non-zero value. 14817c478bd9Sstevel@tonic-gate */ 14827c478bd9Sstevel@tonic-gate static int 14837c478bd9Sstevel@tonic-gate chg_mode(char *target, uid_t uid, gid_t gid, mode_t mode) 14847c478bd9Sstevel@tonic-gate { 14857c478bd9Sstevel@tonic-gate int clearflg = 0; /* controls message printed upon chown() error */ 14867c478bd9Sstevel@tonic-gate 14877c478bd9Sstevel@tonic-gate if (chown(target, uid, gid) != 0) { 14887c478bd9Sstevel@tonic-gate #ifdef XPG4 14897c478bd9Sstevel@tonic-gate if (mve) { 14907c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s: cannot change" 14917c478bd9Sstevel@tonic-gate " owner and group of %s: "), cmd, target); 14927c478bd9Sstevel@tonic-gate perror(""); 14937c478bd9Sstevel@tonic-gate } 14947c478bd9Sstevel@tonic-gate #endif 14957c478bd9Sstevel@tonic-gate if (mode & (S_ISUID | S_ISGID)) { 14967c478bd9Sstevel@tonic-gate /* try to clear S_ISUID and S_ISGID */ 14977c478bd9Sstevel@tonic-gate mode &= ~S_ISUID & ~S_ISGID; 14987c478bd9Sstevel@tonic-gate ++clearflg; 14997c478bd9Sstevel@tonic-gate } 15007c478bd9Sstevel@tonic-gate } 15017c478bd9Sstevel@tonic-gate if (chmod(target, mode) != 0) { 15027c478bd9Sstevel@tonic-gate if (clearflg) { 15037c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext( 15047c478bd9Sstevel@tonic-gate "%s: cannot clear S_ISUID and S_ISGID bits in" 15057c478bd9Sstevel@tonic-gate " %s: "), cmd, target); 15067c478bd9Sstevel@tonic-gate perror(""); 15077c478bd9Sstevel@tonic-gate /* cp -p should get non-zero exit; mv should not */ 15087c478bd9Sstevel@tonic-gate if (pflg) 15097c478bd9Sstevel@tonic-gate return (1); 15107c478bd9Sstevel@tonic-gate } 15117c478bd9Sstevel@tonic-gate #ifdef XPG4 15127c478bd9Sstevel@tonic-gate else { 15137c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext( 15147c478bd9Sstevel@tonic-gate "%s: cannot set permissions for %s: "), cmd, target); 15157c478bd9Sstevel@tonic-gate perror(""); 15167c478bd9Sstevel@tonic-gate /* cp -p should get non-zero exit; mv should not */ 15177c478bd9Sstevel@tonic-gate if (pflg) 15187c478bd9Sstevel@tonic-gate return (1); 15197c478bd9Sstevel@tonic-gate } 15207c478bd9Sstevel@tonic-gate #endif 15217c478bd9Sstevel@tonic-gate } 15227c478bd9Sstevel@tonic-gate return (0); 15237c478bd9Sstevel@tonic-gate 15247c478bd9Sstevel@tonic-gate } 15257c478bd9Sstevel@tonic-gate 15267c478bd9Sstevel@tonic-gate static void 15277c478bd9Sstevel@tonic-gate Perror(char *s) 15287c478bd9Sstevel@tonic-gate { 15297c478bd9Sstevel@tonic-gate char buf[PATH_MAX + 10]; 15307c478bd9Sstevel@tonic-gate 15317c478bd9Sstevel@tonic-gate (void) snprintf(buf, sizeof (buf), "%s: %s", cmd, s); 15327c478bd9Sstevel@tonic-gate perror(buf); 15337c478bd9Sstevel@tonic-gate } 15347c478bd9Sstevel@tonic-gate 15357c478bd9Sstevel@tonic-gate static void 15367c478bd9Sstevel@tonic-gate Perror2(char *s1, char *s2) 15377c478bd9Sstevel@tonic-gate { 15387c478bd9Sstevel@tonic-gate char buf[PATH_MAX + 20]; 15397c478bd9Sstevel@tonic-gate 15407c478bd9Sstevel@tonic-gate (void) snprintf(buf, sizeof (buf), "%s: %s: %s", 15417c478bd9Sstevel@tonic-gate cmd, gettext(s1), gettext(s2)); 15427c478bd9Sstevel@tonic-gate perror(buf); 15437c478bd9Sstevel@tonic-gate } 15447c478bd9Sstevel@tonic-gate 15457c478bd9Sstevel@tonic-gate /* 15467c478bd9Sstevel@tonic-gate * used for cp -R and for mv across file systems 15477c478bd9Sstevel@tonic-gate */ 15487c478bd9Sstevel@tonic-gate static int 15497c478bd9Sstevel@tonic-gate copydir(char *source, char *target) 15507c478bd9Sstevel@tonic-gate { 15517c478bd9Sstevel@tonic-gate int ret, attret = 0; 15527c478bd9Sstevel@tonic-gate int pret = 0; /* need separate flag if -p is specified */ 15537c478bd9Sstevel@tonic-gate mode_t fixmode = (mode_t)0; /* cleanup mode after copy */ 15547c478bd9Sstevel@tonic-gate struct stat s1save; 1555*fa9e4066Sahrens acl_t *s1acl_save; 1556*fa9e4066Sahrens 1557*fa9e4066Sahrens s1acl_save = NULL; 15587c478bd9Sstevel@tonic-gate 15597c478bd9Sstevel@tonic-gate if (cpy && !rflg) { 15607c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 15617c478bd9Sstevel@tonic-gate gettext("%s: %s: is a directory\n"), cmd, source); 15627c478bd9Sstevel@tonic-gate return (1); 15637c478bd9Sstevel@tonic-gate } 15647c478bd9Sstevel@tonic-gate 15657c478bd9Sstevel@tonic-gate if (stat(target, &s2) < 0) { 15667c478bd9Sstevel@tonic-gate if (mkdir(target, (s1.st_mode & MODEBITS)) < 0) { 15677c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: ", cmd); 15687c478bd9Sstevel@tonic-gate perror(target); 15697c478bd9Sstevel@tonic-gate return (1); 15707c478bd9Sstevel@tonic-gate } 15717c478bd9Sstevel@tonic-gate if (stat(target, &s2) == 0) { 15727c478bd9Sstevel@tonic-gate fixmode = s2.st_mode; 15737c478bd9Sstevel@tonic-gate } else { 15747c478bd9Sstevel@tonic-gate fixmode = s1.st_mode; 15757c478bd9Sstevel@tonic-gate } 15767c478bd9Sstevel@tonic-gate (void) chmod(target, ((fixmode & MODEBITS) | S_IRWXU)); 15777c478bd9Sstevel@tonic-gate } else if (!(ISDIR(s2))) { 15787c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 15797c478bd9Sstevel@tonic-gate gettext("%s: %s: not a directory.\n"), cmd, target); 15807c478bd9Sstevel@tonic-gate return (1); 15817c478bd9Sstevel@tonic-gate } 15827c478bd9Sstevel@tonic-gate if (pflg || mve) { 15837c478bd9Sstevel@tonic-gate /* 15847c478bd9Sstevel@tonic-gate * Save s1 (stat information for source dir) and acl info, 15857c478bd9Sstevel@tonic-gate * if any, so that ownership, modes, times, and acl's can 15867c478bd9Sstevel@tonic-gate * be reserved during "cp -p" or mv. 15877c478bd9Sstevel@tonic-gate * s1 gets overwritten when doing the recursive copy. 15887c478bd9Sstevel@tonic-gate */ 15897c478bd9Sstevel@tonic-gate s1save = s1; 1590*fa9e4066Sahrens if (s1acl != NULL) { 1591*fa9e4066Sahrens s1acl_save = acl_dup(s1acl); 1592*fa9e4066Sahrens if (s1acl_save == NULL) { 1593*fa9e4066Sahrens (void) fprintf(stderr, gettext("%s: " 1594*fa9e4066Sahrens "Insufficient memory to save acl" 1595*fa9e4066Sahrens " entry\n"), cmd); 1596*fa9e4066Sahrens if (pflg) 1597*fa9e4066Sahrens return (1); 1598*fa9e4066Sahrens 15997c478bd9Sstevel@tonic-gate } 16007c478bd9Sstevel@tonic-gate #ifdef XPG4 16017c478bd9Sstevel@tonic-gate else { 16027c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s: " 16037c478bd9Sstevel@tonic-gate "Insufficient memory to save acl" 16047c478bd9Sstevel@tonic-gate " entry\n"), cmd); 16057c478bd9Sstevel@tonic-gate if (pflg) 16067c478bd9Sstevel@tonic-gate return (1); 16077c478bd9Sstevel@tonic-gate } 16087c478bd9Sstevel@tonic-gate #endif 16097c478bd9Sstevel@tonic-gate } 16107c478bd9Sstevel@tonic-gate } 16117c478bd9Sstevel@tonic-gate 16127c478bd9Sstevel@tonic-gate ret = rcopy(source, target); 16137c478bd9Sstevel@tonic-gate 16147c478bd9Sstevel@tonic-gate /* 16157c478bd9Sstevel@tonic-gate * Once we created a directory, go ahead and set 16167c478bd9Sstevel@tonic-gate * its attributes, e.g. acls and time. The info 16177c478bd9Sstevel@tonic-gate * may get overwritten if we continue traversing 16187c478bd9Sstevel@tonic-gate * down the tree. 16197c478bd9Sstevel@tonic-gate * 16207c478bd9Sstevel@tonic-gate * ACL for directory 16217c478bd9Sstevel@tonic-gate */ 16227c478bd9Sstevel@tonic-gate if (pflg || mve) { 1623*fa9e4066Sahrens if (s1acl_save != NULL) { 1624*fa9e4066Sahrens if (acl_set(target, s1acl_save) < 0) { 16257c478bd9Sstevel@tonic-gate #ifdef XPG4 16267c478bd9Sstevel@tonic-gate if (pflg || mve) { 16277c478bd9Sstevel@tonic-gate #else 16287c478bd9Sstevel@tonic-gate if (pflg) { 16297c478bd9Sstevel@tonic-gate #endif 16307c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext( 16317c478bd9Sstevel@tonic-gate "%s: failed to set acl entries " 16327c478bd9Sstevel@tonic-gate "on %s\n"), cmd, target); 16337c478bd9Sstevel@tonic-gate if (pflg) { 1634*fa9e4066Sahrens acl_free(s1acl_save); 1635*fa9e4066Sahrens s1acl_save = NULL; 16367c478bd9Sstevel@tonic-gate ret++; 16377c478bd9Sstevel@tonic-gate } 16387c478bd9Sstevel@tonic-gate } 16397c478bd9Sstevel@tonic-gate /* else: silent and continue */ 16407c478bd9Sstevel@tonic-gate } 1641*fa9e4066Sahrens acl_free(s1acl_save); 1642*fa9e4066Sahrens s1acl_save = NULL; 16437c478bd9Sstevel@tonic-gate } 16447c478bd9Sstevel@tonic-gate if ((pret = chg_mode(target, UID(s1save), GID(s1save), 16457c478bd9Sstevel@tonic-gate FMODE(s1save))) == 0) 16467c478bd9Sstevel@tonic-gate pret = chg_time(target, s1save); 16477c478bd9Sstevel@tonic-gate ret += pret; 16487c478bd9Sstevel@tonic-gate } else if (fixmode != (mode_t)0) 16497c478bd9Sstevel@tonic-gate (void) chmod(target, fixmode & MODEBITS); 16507c478bd9Sstevel@tonic-gate 16517c478bd9Sstevel@tonic-gate if (pflg || atflg || mve) { 16527c478bd9Sstevel@tonic-gate attret = copyattributes(source, target); 16537c478bd9Sstevel@tonic-gate if (!attrsilent && attret != 0) { 16547c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s: Failed to preserve" 16557c478bd9Sstevel@tonic-gate " extended attributes of directory" 16567c478bd9Sstevel@tonic-gate " %s\n"), cmd, source); 16577c478bd9Sstevel@tonic-gate } else { 16587c478bd9Sstevel@tonic-gate /* 16597c478bd9Sstevel@tonic-gate * Otherwise ignore failure. 16607c478bd9Sstevel@tonic-gate */ 16617c478bd9Sstevel@tonic-gate attret = 0; 16627c478bd9Sstevel@tonic-gate } 16637c478bd9Sstevel@tonic-gate } 16647c478bd9Sstevel@tonic-gate if (attret != 0) 16657c478bd9Sstevel@tonic-gate return (attret); 16667c478bd9Sstevel@tonic-gate return (ret); 16677c478bd9Sstevel@tonic-gate } 16687c478bd9Sstevel@tonic-gate 16697c478bd9Sstevel@tonic-gate static int 16707c478bd9Sstevel@tonic-gate copyspecial(char *target) 16717c478bd9Sstevel@tonic-gate { 16727c478bd9Sstevel@tonic-gate int ret = 0; 16737c478bd9Sstevel@tonic-gate 16747c478bd9Sstevel@tonic-gate if (mknod(target, s1.st_mode, s1.st_rdev) != 0) { 16757c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext( 16767c478bd9Sstevel@tonic-gate "cp: cannot create special file %s: "), target); 16777c478bd9Sstevel@tonic-gate perror(""); 16787c478bd9Sstevel@tonic-gate return (1); 16797c478bd9Sstevel@tonic-gate } 16807c478bd9Sstevel@tonic-gate 16817c478bd9Sstevel@tonic-gate if (pflg) { 16827c478bd9Sstevel@tonic-gate if ((ret = chg_mode(target, UID(s1), GID(s1), FMODE(s1))) == 0) 16837c478bd9Sstevel@tonic-gate ret = chg_time(target, s1); 16847c478bd9Sstevel@tonic-gate } 16857c478bd9Sstevel@tonic-gate 16867c478bd9Sstevel@tonic-gate return (ret); 16877c478bd9Sstevel@tonic-gate } 16887c478bd9Sstevel@tonic-gate 16897c478bd9Sstevel@tonic-gate static int 16907c478bd9Sstevel@tonic-gate use_stdin(void) 16917c478bd9Sstevel@tonic-gate { 16927c478bd9Sstevel@tonic-gate #ifdef XPG4 16937c478bd9Sstevel@tonic-gate return (1); 16947c478bd9Sstevel@tonic-gate #else 16957c478bd9Sstevel@tonic-gate return (isatty(fileno(stdin))); 16967c478bd9Sstevel@tonic-gate #endif 16977c478bd9Sstevel@tonic-gate } 16987c478bd9Sstevel@tonic-gate 16997c478bd9Sstevel@tonic-gate static int 17007c478bd9Sstevel@tonic-gate copyattributes(char *source, char *target) 17017c478bd9Sstevel@tonic-gate { 17027c478bd9Sstevel@tonic-gate int sourcedirfd, targetdirfd; 17037c478bd9Sstevel@tonic-gate int srcfd, targfd; 17047c478bd9Sstevel@tonic-gate int tmpfd; 17057c478bd9Sstevel@tonic-gate DIR *srcdirp; 17067c478bd9Sstevel@tonic-gate int srcattrfd, targattrfd; 17077c478bd9Sstevel@tonic-gate struct dirent *dp; 17087c478bd9Sstevel@tonic-gate char *attrstr; 17097c478bd9Sstevel@tonic-gate char *srcbuf, *targbuf; 17107c478bd9Sstevel@tonic-gate size_t src_size, targ_size; 17117c478bd9Sstevel@tonic-gate int error = 0; 1712*fa9e4066Sahrens int aclerror; 17137c478bd9Sstevel@tonic-gate mode_t mode; 17147c478bd9Sstevel@tonic-gate int clearflg = 0; 1715*fa9e4066Sahrens acl_t *xacl = NULL; 1716*fa9e4066Sahrens acl_t *attrdiracl = NULL; 17177c478bd9Sstevel@tonic-gate struct stat attrdir, s3, s4; 17187c478bd9Sstevel@tonic-gate struct timeval times[2]; 17197c478bd9Sstevel@tonic-gate mode_t targmode; 17207c478bd9Sstevel@tonic-gate 17217c478bd9Sstevel@tonic-gate srcdirp = NULL; 17227c478bd9Sstevel@tonic-gate srcfd = targfd = tmpfd = -1; 17237c478bd9Sstevel@tonic-gate sourcedirfd = targetdirfd = srcattrfd = targattrfd = -1; 17247c478bd9Sstevel@tonic-gate srcbuf = targbuf = NULL; 17257c478bd9Sstevel@tonic-gate 17267c478bd9Sstevel@tonic-gate if (pathconf(source, _PC_XATTR_EXISTS) != 1) 17277c478bd9Sstevel@tonic-gate return (0); 17287c478bd9Sstevel@tonic-gate 17297c478bd9Sstevel@tonic-gate if (pathconf(target, _PC_XATTR_ENABLED) != 1) { 17307c478bd9Sstevel@tonic-gate if (!attrsilent) { 17317c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 17327c478bd9Sstevel@tonic-gate gettext( 17337c478bd9Sstevel@tonic-gate "%s: cannot preserve extended attributes, " 17347c478bd9Sstevel@tonic-gate "operation not supported on file" 17357c478bd9Sstevel@tonic-gate " %s\n"), cmd, target); 17367c478bd9Sstevel@tonic-gate } 17377c478bd9Sstevel@tonic-gate return (1); 17387c478bd9Sstevel@tonic-gate } 17397c478bd9Sstevel@tonic-gate 17407c478bd9Sstevel@tonic-gate 17417c478bd9Sstevel@tonic-gate if ((srcfd = open(source, O_RDONLY)) == -1) { 17427c478bd9Sstevel@tonic-gate if (pflg && attrsilent) { 17437c478bd9Sstevel@tonic-gate error = 0; 17447c478bd9Sstevel@tonic-gate goto out; 17457c478bd9Sstevel@tonic-gate } 17467c478bd9Sstevel@tonic-gate if (!attrsilent) { 17477c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 17487c478bd9Sstevel@tonic-gate gettext("%s: cannot open file" 17497c478bd9Sstevel@tonic-gate " %s: "), cmd, source); 17507c478bd9Sstevel@tonic-gate perror(""); 17517c478bd9Sstevel@tonic-gate } 17527c478bd9Sstevel@tonic-gate ++error; 17537c478bd9Sstevel@tonic-gate goto out; 17547c478bd9Sstevel@tonic-gate } 17557c478bd9Sstevel@tonic-gate if ((targfd = open(target, O_RDONLY)) == -1) { 17567c478bd9Sstevel@tonic-gate 17577c478bd9Sstevel@tonic-gate if (pflg && attrsilent) { 17587c478bd9Sstevel@tonic-gate error = 0; 17597c478bd9Sstevel@tonic-gate goto out; 17607c478bd9Sstevel@tonic-gate } 17617c478bd9Sstevel@tonic-gate if (!attrsilent) { 17627c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 17637c478bd9Sstevel@tonic-gate gettext("%s: cannot open file" 17647c478bd9Sstevel@tonic-gate " %s: "), cmd, source); 17657c478bd9Sstevel@tonic-gate perror(""); 17667c478bd9Sstevel@tonic-gate } 17677c478bd9Sstevel@tonic-gate ++error; 17687c478bd9Sstevel@tonic-gate goto out; 17697c478bd9Sstevel@tonic-gate } 17707c478bd9Sstevel@tonic-gate 17717c478bd9Sstevel@tonic-gate if ((sourcedirfd = openat(srcfd, ".", O_RDONLY|O_XATTR)) == -1) { 17727c478bd9Sstevel@tonic-gate if (pflg && attrsilent) { 17737c478bd9Sstevel@tonic-gate error = 0; 17747c478bd9Sstevel@tonic-gate goto out; 17757c478bd9Sstevel@tonic-gate } 17767c478bd9Sstevel@tonic-gate if (!attrsilent) { 17777c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 17787c478bd9Sstevel@tonic-gate gettext("%s: cannot open attribute" 17797c478bd9Sstevel@tonic-gate " directory for %s: "), cmd, source); 17807c478bd9Sstevel@tonic-gate perror(""); 17817c478bd9Sstevel@tonic-gate ++error; 17827c478bd9Sstevel@tonic-gate } 17837c478bd9Sstevel@tonic-gate goto out; 17847c478bd9Sstevel@tonic-gate } 17857c478bd9Sstevel@tonic-gate 17867c478bd9Sstevel@tonic-gate if (fstat(sourcedirfd, &attrdir) == -1) { 17877c478bd9Sstevel@tonic-gate if (pflg && attrsilent) { 17887c478bd9Sstevel@tonic-gate error = 0; 17897c478bd9Sstevel@tonic-gate goto out; 17907c478bd9Sstevel@tonic-gate } 17917c478bd9Sstevel@tonic-gate 17927c478bd9Sstevel@tonic-gate if (!attrsilent) { 17937c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 17947c478bd9Sstevel@tonic-gate gettext("%s: could not retrieve stat" 17957c478bd9Sstevel@tonic-gate " information for attribute directory" 17967c478bd9Sstevel@tonic-gate "of file %s: "), cmd, source); 17977c478bd9Sstevel@tonic-gate perror(""); 17987c478bd9Sstevel@tonic-gate ++error; 17997c478bd9Sstevel@tonic-gate } 18007c478bd9Sstevel@tonic-gate goto out; 18017c478bd9Sstevel@tonic-gate } 18027c478bd9Sstevel@tonic-gate if ((targetdirfd = openat(targfd, ".", O_RDONLY|O_XATTR)) == -1) { 18037c478bd9Sstevel@tonic-gate /* 18047c478bd9Sstevel@tonic-gate * We couldn't create the attribute directory 18057c478bd9Sstevel@tonic-gate * 18067c478bd9Sstevel@tonic-gate * Lets see if we can add write support to the mode 18077c478bd9Sstevel@tonic-gate * and create the directory and then put the mode back 18087c478bd9Sstevel@tonic-gate * to way it should be. 18097c478bd9Sstevel@tonic-gate */ 18107c478bd9Sstevel@tonic-gate 18117c478bd9Sstevel@tonic-gate targmode = FMODE(s1) | S_IWUSR; 18127c478bd9Sstevel@tonic-gate if (fchmod(targfd, targmode) == 0) { 18137c478bd9Sstevel@tonic-gate targetdirfd = openat(targfd, ".", O_RDONLY|O_XATTR); 18147c478bd9Sstevel@tonic-gate /* 18157c478bd9Sstevel@tonic-gate * Put mode back to what it was 18167c478bd9Sstevel@tonic-gate */ 18177c478bd9Sstevel@tonic-gate targmode = FMODE(s1) & MODEBITS; 18187c478bd9Sstevel@tonic-gate if (fchmod(targfd, targmode) == -1) { 18197c478bd9Sstevel@tonic-gate if (pflg && attrsilent) { 18207c478bd9Sstevel@tonic-gate error = 0; 18217c478bd9Sstevel@tonic-gate goto out; 18227c478bd9Sstevel@tonic-gate } 18237c478bd9Sstevel@tonic-gate if (!attrsilent) { 18247c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 18257c478bd9Sstevel@tonic-gate gettext("%s: failed to set" 18267c478bd9Sstevel@tonic-gate " mode correctly on file" 18277c478bd9Sstevel@tonic-gate " %s: "), cmd, target); 18287c478bd9Sstevel@tonic-gate perror(""); 18297c478bd9Sstevel@tonic-gate ++error; 18307c478bd9Sstevel@tonic-gate goto out; 18317c478bd9Sstevel@tonic-gate } 18327c478bd9Sstevel@tonic-gate } 18337c478bd9Sstevel@tonic-gate } else { 18347c478bd9Sstevel@tonic-gate if (pflg && attrsilent) { 18357c478bd9Sstevel@tonic-gate error = 0; 18367c478bd9Sstevel@tonic-gate goto out; 18377c478bd9Sstevel@tonic-gate } 18387c478bd9Sstevel@tonic-gate if (!attrsilent) { 18397c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 18407c478bd9Sstevel@tonic-gate gettext("%s: cannot open attribute" 18417c478bd9Sstevel@tonic-gate " directory for %s: "), cmd, target); 18427c478bd9Sstevel@tonic-gate perror(""); 18437c478bd9Sstevel@tonic-gate ++error; 18447c478bd9Sstevel@tonic-gate } 18457c478bd9Sstevel@tonic-gate goto out; 18467c478bd9Sstevel@tonic-gate } 18477c478bd9Sstevel@tonic-gate } 18487c478bd9Sstevel@tonic-gate 18497c478bd9Sstevel@tonic-gate if (targetdirfd == -1) { 18507c478bd9Sstevel@tonic-gate if (pflg && attrsilent) { 18517c478bd9Sstevel@tonic-gate error = 0; 18527c478bd9Sstevel@tonic-gate goto out; 18537c478bd9Sstevel@tonic-gate } 18547c478bd9Sstevel@tonic-gate if (!attrsilent) { 18557c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 18567c478bd9Sstevel@tonic-gate gettext("%s: cannot open attribute directory" 18577c478bd9Sstevel@tonic-gate " for %s: "), cmd, target); 18587c478bd9Sstevel@tonic-gate perror(""); 18597c478bd9Sstevel@tonic-gate ++error; 18607c478bd9Sstevel@tonic-gate } 18617c478bd9Sstevel@tonic-gate goto out; 18627c478bd9Sstevel@tonic-gate } 18637c478bd9Sstevel@tonic-gate 18647c478bd9Sstevel@tonic-gate /* 18657c478bd9Sstevel@tonic-gate * Set mode of attribute directory same as on source, 18667c478bd9Sstevel@tonic-gate * if pflg set or this is a move. 18677c478bd9Sstevel@tonic-gate */ 18687c478bd9Sstevel@tonic-gate 18697c478bd9Sstevel@tonic-gate if (pflg || mve) { 18707c478bd9Sstevel@tonic-gate if (fchmod(targetdirfd, attrdir.st_mode) == -1) { 18717c478bd9Sstevel@tonic-gate if (!attrsilent) { 18727c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 18737c478bd9Sstevel@tonic-gate gettext("%s: failed to set file mode" 18747c478bd9Sstevel@tonic-gate " correctly on attribute directory of" 18757c478bd9Sstevel@tonic-gate " file %s: "), cmd, target); 18767c478bd9Sstevel@tonic-gate perror(""); 18777c478bd9Sstevel@tonic-gate ++error; 18787c478bd9Sstevel@tonic-gate } 18797c478bd9Sstevel@tonic-gate } 18807c478bd9Sstevel@tonic-gate 18817c478bd9Sstevel@tonic-gate if (fchown(targetdirfd, attrdir.st_uid, attrdir.st_gid) == -1) { 18827c478bd9Sstevel@tonic-gate if (!attrsilent) { 18837c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 18847c478bd9Sstevel@tonic-gate gettext("%s: failed to set file" 18857c478bd9Sstevel@tonic-gate " ownership correctly on attribute" 18867c478bd9Sstevel@tonic-gate " directory of file %s: "), cmd, target); 18877c478bd9Sstevel@tonic-gate perror(""); 18887c478bd9Sstevel@tonic-gate ++error; 18897c478bd9Sstevel@tonic-gate } 18907c478bd9Sstevel@tonic-gate } 18917c478bd9Sstevel@tonic-gate /* 18927c478bd9Sstevel@tonic-gate * Now that we are the owner we can update st_ctime by calling 18937c478bd9Sstevel@tonic-gate * futimesat. 18947c478bd9Sstevel@tonic-gate */ 18957c478bd9Sstevel@tonic-gate times[0].tv_sec = attrdir.st_atime; 18967c478bd9Sstevel@tonic-gate times[0].tv_usec = 0; 18977c478bd9Sstevel@tonic-gate times[1].tv_sec = attrdir.st_mtime; 18987c478bd9Sstevel@tonic-gate times[1].tv_usec = 0; 18997c478bd9Sstevel@tonic-gate if (futimesat(targetdirfd, ".", times) < 0) { 19007c478bd9Sstevel@tonic-gate if (!attrsilent) { 19017c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 19027c478bd9Sstevel@tonic-gate gettext("%s: cannot set attribute times" 19037c478bd9Sstevel@tonic-gate " for %s: "), cmd, target); 19047c478bd9Sstevel@tonic-gate perror(""); 19057c478bd9Sstevel@tonic-gate ++error; 19067c478bd9Sstevel@tonic-gate } 19077c478bd9Sstevel@tonic-gate } 19087c478bd9Sstevel@tonic-gate 19097c478bd9Sstevel@tonic-gate /* 19107c478bd9Sstevel@tonic-gate * Now set owner and group of attribute directory, implies 19117c478bd9Sstevel@tonic-gate * changing the ACL of the hidden attribute directory first. 19127c478bd9Sstevel@tonic-gate */ 1913*fa9e4066Sahrens if ((aclerror = facl_get(sourcedirfd, 1914*fa9e4066Sahrens ACL_NO_TRIVIAL, &attrdiracl)) != 0) { 19157c478bd9Sstevel@tonic-gate if (!attrsilent) { 19167c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext( 19177c478bd9Sstevel@tonic-gate "%s: failed to get acl entries of" 19187c478bd9Sstevel@tonic-gate " attribute directory for" 1919*fa9e4066Sahrens " %s : %s\n"), cmd, 1920*fa9e4066Sahrens source, acl_strerror(aclerror)); 19217c478bd9Sstevel@tonic-gate ++error; 19227c478bd9Sstevel@tonic-gate } 19237c478bd9Sstevel@tonic-gate } 1924*fa9e4066Sahrens 1925*fa9e4066Sahrens if (attrdiracl) { 1926*fa9e4066Sahrens if (facl_set(targetdirfd, attrdiracl) != 0) { 19277c478bd9Sstevel@tonic-gate if (!attrsilent) { 19287c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext( 1929*fa9e4066Sahrens "%s: failed to set acl entries" 1930*fa9e4066Sahrens " on attribute directory " 1931*fa9e4066Sahrens "for %s\n"), cmd, target); 19327c478bd9Sstevel@tonic-gate ++error; 19337c478bd9Sstevel@tonic-gate } 1934*fa9e4066Sahrens acl_free(attrdiracl); 1935*fa9e4066Sahrens attrdiracl = NULL; 19367c478bd9Sstevel@tonic-gate } 19377c478bd9Sstevel@tonic-gate } 19387c478bd9Sstevel@tonic-gate } 19397c478bd9Sstevel@tonic-gate 19407c478bd9Sstevel@tonic-gate /* 19417c478bd9Sstevel@tonic-gate * dup sourcedirfd for use by fdopendir(). 19427c478bd9Sstevel@tonic-gate * fdopendir will take ownership of given fd and will close 19437c478bd9Sstevel@tonic-gate * it when closedir() is called. 19447c478bd9Sstevel@tonic-gate */ 19457c478bd9Sstevel@tonic-gate 19467c478bd9Sstevel@tonic-gate if ((tmpfd = dup(sourcedirfd)) == -1) { 19477c478bd9Sstevel@tonic-gate if (pflg && attrsilent) { 19487c478bd9Sstevel@tonic-gate error = 0; 19497c478bd9Sstevel@tonic-gate goto out; 19507c478bd9Sstevel@tonic-gate } 19517c478bd9Sstevel@tonic-gate if (!attrsilent) { 19527c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 19537c478bd9Sstevel@tonic-gate gettext( 19547c478bd9Sstevel@tonic-gate "%s: unable to dup attribute directory" 19557c478bd9Sstevel@tonic-gate " file descriptor for %s: "), cmd, source); 19567c478bd9Sstevel@tonic-gate perror(""); 19577c478bd9Sstevel@tonic-gate ++error; 19587c478bd9Sstevel@tonic-gate } 19597c478bd9Sstevel@tonic-gate goto out; 19607c478bd9Sstevel@tonic-gate } 19617c478bd9Sstevel@tonic-gate if ((srcdirp = fdopendir(tmpfd)) == NULL) { 19627c478bd9Sstevel@tonic-gate if (pflg && attrsilent) { 19637c478bd9Sstevel@tonic-gate error = 0; 19647c478bd9Sstevel@tonic-gate goto out; 19657c478bd9Sstevel@tonic-gate } 19667c478bd9Sstevel@tonic-gate if (!attrsilent) { 19677c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 19687c478bd9Sstevel@tonic-gate gettext("%s: failed to open attribute" 19697c478bd9Sstevel@tonic-gate " directory for %s: "), cmd, source); 19707c478bd9Sstevel@tonic-gate perror(""); 19717c478bd9Sstevel@tonic-gate ++error; 19727c478bd9Sstevel@tonic-gate } 19737c478bd9Sstevel@tonic-gate goto out; 19747c478bd9Sstevel@tonic-gate } 19757c478bd9Sstevel@tonic-gate 19767c478bd9Sstevel@tonic-gate while (dp = readdir(srcdirp)) { 19777c478bd9Sstevel@tonic-gate if ((dp->d_name[0] == '.' && dp->d_name[1] == '\0') || 19787c478bd9Sstevel@tonic-gate (dp->d_name[0] == '.' && dp->d_name[1] == '.' && 19797c478bd9Sstevel@tonic-gate dp->d_name[2] == '\0')) 19807c478bd9Sstevel@tonic-gate continue; 19817c478bd9Sstevel@tonic-gate 19827c478bd9Sstevel@tonic-gate if ((srcattrfd = openat(sourcedirfd, dp->d_name, 19837c478bd9Sstevel@tonic-gate O_RDONLY)) == -1) { 19847c478bd9Sstevel@tonic-gate if (!attrsilent) { 19857c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 19867c478bd9Sstevel@tonic-gate gettext("%s: cannot open attribute %s on" 19877c478bd9Sstevel@tonic-gate " file %s: "), cmd, dp->d_name, source); 19887c478bd9Sstevel@tonic-gate perror(""); 19897c478bd9Sstevel@tonic-gate ++error; 19907c478bd9Sstevel@tonic-gate goto next; 19917c478bd9Sstevel@tonic-gate } 19927c478bd9Sstevel@tonic-gate } 19937c478bd9Sstevel@tonic-gate 19947c478bd9Sstevel@tonic-gate if (fstat(srcattrfd, &s3) < 0) { 19957c478bd9Sstevel@tonic-gate if (!attrsilent) { 19967c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 19977c478bd9Sstevel@tonic-gate gettext("%s: could not stat attribute" 19987c478bd9Sstevel@tonic-gate " %s on file" 19997c478bd9Sstevel@tonic-gate " %s: "), cmd, dp->d_name, source); 20007c478bd9Sstevel@tonic-gate perror(""); 20017c478bd9Sstevel@tonic-gate ++error; 20027c478bd9Sstevel@tonic-gate } 20037c478bd9Sstevel@tonic-gate goto next; 20047c478bd9Sstevel@tonic-gate } 20057c478bd9Sstevel@tonic-gate 20067c478bd9Sstevel@tonic-gate if (pflg || mve) { 2007*fa9e4066Sahrens if ((aclerror = facl_get(srcattrfd, 2008*fa9e4066Sahrens ACL_NO_TRIVIAL, &xacl)) != 0) { 20097c478bd9Sstevel@tonic-gate if (!attrsilent) { 20107c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext( 20117c478bd9Sstevel@tonic-gate "%s: failed to get acl entries of" 20127c478bd9Sstevel@tonic-gate " attribute %s for" 2013*fa9e4066Sahrens " %s: %s"), cmd, dp->d_name, 2014*fa9e4066Sahrens source, acl_strerror(aclerror)); 20157c478bd9Sstevel@tonic-gate ++error; 20167c478bd9Sstevel@tonic-gate } 20177c478bd9Sstevel@tonic-gate } 20187c478bd9Sstevel@tonic-gate } 20197c478bd9Sstevel@tonic-gate 20207c478bd9Sstevel@tonic-gate (void) unlinkat(targetdirfd, dp->d_name, 0); 20217c478bd9Sstevel@tonic-gate if ((targattrfd = openat(targetdirfd, dp->d_name, 20227c478bd9Sstevel@tonic-gate O_RDWR|O_CREAT|O_TRUNC, s3.st_mode & MODEBITS)) == -1) { 20237c478bd9Sstevel@tonic-gate if (!attrsilent) { 20247c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 20257c478bd9Sstevel@tonic-gate gettext("%s: could not create attribute" 20267c478bd9Sstevel@tonic-gate " %s on file" 20277c478bd9Sstevel@tonic-gate " %s: "), cmd, dp->d_name, target); 20287c478bd9Sstevel@tonic-gate perror(""); 20297c478bd9Sstevel@tonic-gate ++error; 20307c478bd9Sstevel@tonic-gate } 20317c478bd9Sstevel@tonic-gate goto next; 20327c478bd9Sstevel@tonic-gate } 20337c478bd9Sstevel@tonic-gate 20347c478bd9Sstevel@tonic-gate /* 20357c478bd9Sstevel@tonic-gate * preserve ACL 20367c478bd9Sstevel@tonic-gate */ 2037*fa9e4066Sahrens if ((pflg || mve) && xacl != NULL) { 2038*fa9e4066Sahrens if ((facl_set(targattrfd, xacl)) < 0) { 20397c478bd9Sstevel@tonic-gate if (!attrsilent) { 20407c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext( 20417c478bd9Sstevel@tonic-gate "%s: failed to set acl entries on" 20427c478bd9Sstevel@tonic-gate " attribute %s for" 20437c478bd9Sstevel@tonic-gate "%s\n"), cmd, dp->d_name, target); 20447c478bd9Sstevel@tonic-gate ++error; 20457c478bd9Sstevel@tonic-gate } 2046*fa9e4066Sahrens acl_free(xacl); 2047*fa9e4066Sahrens xacl = NULL; 20487c478bd9Sstevel@tonic-gate } 20497c478bd9Sstevel@tonic-gate } 20507c478bd9Sstevel@tonic-gate 20517c478bd9Sstevel@tonic-gate if (fstat(targattrfd, &s4) < 0) { 20527c478bd9Sstevel@tonic-gate if (!attrsilent) { 20537c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 20547c478bd9Sstevel@tonic-gate gettext("%s: could not stat attribute" 20557c478bd9Sstevel@tonic-gate " %s on file" 20567c478bd9Sstevel@tonic-gate " %s: "), cmd, dp->d_name, source); 20577c478bd9Sstevel@tonic-gate perror(""); 20587c478bd9Sstevel@tonic-gate ++error; 20597c478bd9Sstevel@tonic-gate } 20607c478bd9Sstevel@tonic-gate goto next; 20617c478bd9Sstevel@tonic-gate } 20627c478bd9Sstevel@tonic-gate 20637c478bd9Sstevel@tonic-gate /* 20647c478bd9Sstevel@tonic-gate * setup path string to be passed to writefile 20657c478bd9Sstevel@tonic-gate * 20667c478bd9Sstevel@tonic-gate * We need to include attribute in the string so that 20677c478bd9Sstevel@tonic-gate * a useful error message can be printed in the case of a failure. 20687c478bd9Sstevel@tonic-gate */ 20697c478bd9Sstevel@tonic-gate attrstr = gettext(" attribute "); 20707c478bd9Sstevel@tonic-gate src_size = strlen(source) + 20717c478bd9Sstevel@tonic-gate strlen(dp->d_name) + strlen(attrstr) + 1; 20727c478bd9Sstevel@tonic-gate srcbuf = malloc(src_size); 20737c478bd9Sstevel@tonic-gate 20747c478bd9Sstevel@tonic-gate if (srcbuf == NULL) { 20757c478bd9Sstevel@tonic-gate if (!attrsilent) { 20767c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 20777c478bd9Sstevel@tonic-gate gettext("%s: could not allocate memory" 20787c478bd9Sstevel@tonic-gate " for path buffer: "), cmd); 20797c478bd9Sstevel@tonic-gate perror(""); 20807c478bd9Sstevel@tonic-gate ++error; 20817c478bd9Sstevel@tonic-gate } 20827c478bd9Sstevel@tonic-gate goto next; 20837c478bd9Sstevel@tonic-gate } 20847c478bd9Sstevel@tonic-gate targ_size = strlen(target) + 20857c478bd9Sstevel@tonic-gate strlen(dp->d_name) + strlen(attrstr) + 1; 20867c478bd9Sstevel@tonic-gate targbuf = malloc(targ_size); 20877c478bd9Sstevel@tonic-gate if (targbuf == NULL) { 20887c478bd9Sstevel@tonic-gate if (!attrsilent) { 20897c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 20907c478bd9Sstevel@tonic-gate gettext("%s: could not allocate memory" 20917c478bd9Sstevel@tonic-gate " for path buffer: "), cmd); 20927c478bd9Sstevel@tonic-gate perror(""); 20937c478bd9Sstevel@tonic-gate ++error; 20947c478bd9Sstevel@tonic-gate } 20957c478bd9Sstevel@tonic-gate goto next; 20967c478bd9Sstevel@tonic-gate } 20977c478bd9Sstevel@tonic-gate 20987c478bd9Sstevel@tonic-gate (void) snprintf(srcbuf, src_size, "%s%s%s", 20997c478bd9Sstevel@tonic-gate source, attrstr, dp->d_name); 21007c478bd9Sstevel@tonic-gate (void) snprintf(targbuf, targ_size, "%s%s%s", 21017c478bd9Sstevel@tonic-gate target, attrstr, dp->d_name); 21027c478bd9Sstevel@tonic-gate 21037c478bd9Sstevel@tonic-gate if (writefile(srcattrfd, targattrfd, 21047c478bd9Sstevel@tonic-gate srcbuf, targbuf, &s3, &s4) != 0) { 21057c478bd9Sstevel@tonic-gate if (!attrsilent) { 21067c478bd9Sstevel@tonic-gate ++error; 21077c478bd9Sstevel@tonic-gate } 21087c478bd9Sstevel@tonic-gate goto next; 21097c478bd9Sstevel@tonic-gate } 21107c478bd9Sstevel@tonic-gate 21117c478bd9Sstevel@tonic-gate if (pflg || mve) { 21127c478bd9Sstevel@tonic-gate mode = FMODE(s3); 21137c478bd9Sstevel@tonic-gate 21147c478bd9Sstevel@tonic-gate if (fchown(targattrfd, UID(s3), GID(s3)) != 0) { 21157c478bd9Sstevel@tonic-gate if (!attrsilent) { 21167c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 21177c478bd9Sstevel@tonic-gate gettext("%s: cannot change" 21187c478bd9Sstevel@tonic-gate " owner and group of" 21197c478bd9Sstevel@tonic-gate " attribute %s for" " file" 21207c478bd9Sstevel@tonic-gate " %s: "), cmd, dp->d_name, target); 21217c478bd9Sstevel@tonic-gate perror(""); 21227c478bd9Sstevel@tonic-gate ++error; 21237c478bd9Sstevel@tonic-gate } 21247c478bd9Sstevel@tonic-gate if (mode & (S_ISUID | S_ISGID)) { 21257c478bd9Sstevel@tonic-gate /* try to clear S_ISUID and S_ISGID */ 21267c478bd9Sstevel@tonic-gate mode &= ~S_ISUID & ~S_ISGID; 21277c478bd9Sstevel@tonic-gate ++clearflg; 21287c478bd9Sstevel@tonic-gate } 21297c478bd9Sstevel@tonic-gate } 21307c478bd9Sstevel@tonic-gate /* tv_usec were cleared above */ 21317c478bd9Sstevel@tonic-gate times[0].tv_sec = s3.st_atime; 21327c478bd9Sstevel@tonic-gate times[1].tv_sec = s3.st_mtime; 21337c478bd9Sstevel@tonic-gate if (futimesat(targetdirfd, dp->d_name, times) < 0) { 21347c478bd9Sstevel@tonic-gate if (!attrsilent) { 21357c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 21367c478bd9Sstevel@tonic-gate gettext("%s: cannot set attribute" 21377c478bd9Sstevel@tonic-gate " times for %s: "), cmd, target); 21387c478bd9Sstevel@tonic-gate perror(""); 21397c478bd9Sstevel@tonic-gate ++error; 21407c478bd9Sstevel@tonic-gate } 21417c478bd9Sstevel@tonic-gate } 21427c478bd9Sstevel@tonic-gate if (fchmod(targattrfd, mode) != 0) { 21437c478bd9Sstevel@tonic-gate if (clearflg) { 21447c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext( 21457c478bd9Sstevel@tonic-gate "%s: cannot clear S_ISUID and " 21467c478bd9Sstevel@tonic-gate "S_ISGID bits in attribute %s" 21477c478bd9Sstevel@tonic-gate " for file" 21487c478bd9Sstevel@tonic-gate " %s: "), cmd, dp->d_name, target); 21497c478bd9Sstevel@tonic-gate } else { 21507c478bd9Sstevel@tonic-gate if (!attrsilent) { 21517c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 21527c478bd9Sstevel@tonic-gate gettext( 21537c478bd9Sstevel@tonic-gate "%s: cannot set permissions of attribute" 21547c478bd9Sstevel@tonic-gate " %s for %s: "), cmd, dp->d_name, target); 21557c478bd9Sstevel@tonic-gate perror(""); 21567c478bd9Sstevel@tonic-gate ++error; 21577c478bd9Sstevel@tonic-gate } 21587c478bd9Sstevel@tonic-gate } 21597c478bd9Sstevel@tonic-gate } 21607c478bd9Sstevel@tonic-gate } 21617c478bd9Sstevel@tonic-gate next: 2162*fa9e4066Sahrens if (xacl != NULL) { 2163*fa9e4066Sahrens acl_free(xacl); 2164*fa9e4066Sahrens xacl = NULL; 21657c478bd9Sstevel@tonic-gate } 21667c478bd9Sstevel@tonic-gate if (srcbuf != NULL) 21677c478bd9Sstevel@tonic-gate free(srcbuf); 21687c478bd9Sstevel@tonic-gate if (targbuf != NULL) 21697c478bd9Sstevel@tonic-gate free(targbuf); 21707c478bd9Sstevel@tonic-gate if (srcattrfd != -1) 21717c478bd9Sstevel@tonic-gate (void) close(srcattrfd); 21727c478bd9Sstevel@tonic-gate if (targattrfd != -1) 21737c478bd9Sstevel@tonic-gate (void) close(targattrfd); 21747c478bd9Sstevel@tonic-gate srcattrfd = targattrfd = -1; 21757c478bd9Sstevel@tonic-gate srcbuf = targbuf = NULL; 21767c478bd9Sstevel@tonic-gate } 21777c478bd9Sstevel@tonic-gate out: 2178*fa9e4066Sahrens if (xacl != NULL) { 2179*fa9e4066Sahrens acl_free(xacl); 2180*fa9e4066Sahrens xacl = NULL; 2181*fa9e4066Sahrens } 2182*fa9e4066Sahrens if (attrdiracl != NULL) { 2183*fa9e4066Sahrens acl_free(attrdiracl); 2184*fa9e4066Sahrens attrdiracl = NULL; 2185*fa9e4066Sahrens } 21867c478bd9Sstevel@tonic-gate if (srcbuf) 21877c478bd9Sstevel@tonic-gate free(srcbuf); 21887c478bd9Sstevel@tonic-gate if (targbuf) 21897c478bd9Sstevel@tonic-gate free(targbuf); 21907c478bd9Sstevel@tonic-gate if (sourcedirfd != -1) 21917c478bd9Sstevel@tonic-gate (void) close(sourcedirfd); 21927c478bd9Sstevel@tonic-gate if (targetdirfd != -1) 21937c478bd9Sstevel@tonic-gate (void) close(targetdirfd); 21947c478bd9Sstevel@tonic-gate if (srcdirp != NULL) 21957c478bd9Sstevel@tonic-gate (void) closedir(srcdirp); 21967c478bd9Sstevel@tonic-gate if (srcfd != -1) 21977c478bd9Sstevel@tonic-gate (void) close(srcfd); 21987c478bd9Sstevel@tonic-gate if (targfd != -1) 21997c478bd9Sstevel@tonic-gate (void) close(targfd); 22007c478bd9Sstevel@tonic-gate return (error == 0 ? 0 : 1); 22017c478bd9Sstevel@tonic-gate } 22027c478bd9Sstevel@tonic-gate 22037c478bd9Sstevel@tonic-gate /* 22047c478bd9Sstevel@tonic-gate * nanoseconds are rounded off to microseconds by flooring. 22057c478bd9Sstevel@tonic-gate */ 22067c478bd9Sstevel@tonic-gate static void 22077c478bd9Sstevel@tonic-gate timestruc_to_timeval(timestruc_t *ts, struct timeval *tv) 22087c478bd9Sstevel@tonic-gate { 22097c478bd9Sstevel@tonic-gate tv->tv_sec = ts->tv_sec; 22107c478bd9Sstevel@tonic-gate tv->tv_usec = ts->tv_nsec / 1000; 22117c478bd9Sstevel@tonic-gate } 2212