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 /* 23*d2443e76Smarks * Copyright 2006 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> 67fa9e4066Sahrens #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 */ 144fa9e4066Sahrens static acl_t *s1acl; 1457c478bd9Sstevel@tonic-gate 146a77d64afScf46844 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 378a77d64afScf46844 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)); 805fa9e4066Sahrens if (s1acl != NULL) { 806fa9e4066Sahrens if ((acl_set(target, 807fa9e4066Sahrens 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); 875*d2443e76Smarks /* 876*d2443e76Smarks * Reapply ACL, since chmod may have 877*d2443e76Smarks * altered ACL 878*d2443e76Smarks */ 879*d2443e76Smarks if (s1acl != NULL) { 880*d2443e76Smarks if ((acl_set(target, s1acl)) < 0) { 881*d2443e76Smarks if (pflg || mve) { 882*d2443e76Smarks (void) fprintf( 883*d2443e76Smarks stderr, 884*d2443e76Smarks "%s: failed to set acl entries on %s\n", 885*d2443e76Smarks cmd, 886*d2443e76Smarks target); 887*d2443e76Smarks } 888*d2443e76Smarks /* 889*d2443e76Smarks * else: silent and 890*d2443e76Smarks * continue 891*d2443e76Smarks */ 892*d2443e76Smarks } 893*d2443e76Smarks } 8947c478bd9Sstevel@tonic-gate if ((ret = chg_time(target, s1)) > 0) 8957c478bd9Sstevel@tonic-gate return (1); 8967c478bd9Sstevel@tonic-gate } 8977c478bd9Sstevel@tonic-gate if (cpy) { 8987c478bd9Sstevel@tonic-gate if (attret != 0) 8997c478bd9Sstevel@tonic-gate return (1); 9007c478bd9Sstevel@tonic-gate return (0); 9017c478bd9Sstevel@tonic-gate } 9027c478bd9Sstevel@tonic-gate goto cleanup; 9037c478bd9Sstevel@tonic-gate } 9047c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 9057c478bd9Sstevel@tonic-gate gettext("%s: %s: unknown file type 0x%x\n"), cmd, 9067c478bd9Sstevel@tonic-gate source, (s1.st_mode & S_IFMT)); 9077c478bd9Sstevel@tonic-gate return (1); 9087c478bd9Sstevel@tonic-gate 9097c478bd9Sstevel@tonic-gate cleanup: 9107c478bd9Sstevel@tonic-gate if (unlink(source) < 0) { 9117c478bd9Sstevel@tonic-gate (void) unlink(target); 9127c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 9137c478bd9Sstevel@tonic-gate gettext("%s: cannot unlink %s: "), 9147c478bd9Sstevel@tonic-gate cmd, source); 9157c478bd9Sstevel@tonic-gate perror(""); 9167c478bd9Sstevel@tonic-gate return (1); 9177c478bd9Sstevel@tonic-gate } 9187c478bd9Sstevel@tonic-gate if (attret != 0) 9197c478bd9Sstevel@tonic-gate return (attret); 9207c478bd9Sstevel@tonic-gate return (ret); 9217c478bd9Sstevel@tonic-gate } 9227c478bd9Sstevel@tonic-gate /*NOTREACHED*/ 923a77d64afScf46844 return (ret); 9247c478bd9Sstevel@tonic-gate } 9257c478bd9Sstevel@tonic-gate 9267c478bd9Sstevel@tonic-gate static int 9277c478bd9Sstevel@tonic-gate writefile(int fi, int fo, char *source, char *target, 9287c478bd9Sstevel@tonic-gate struct stat *s1p, struct stat *s2p) 9297c478bd9Sstevel@tonic-gate { 9307c478bd9Sstevel@tonic-gate int mapsize, munmapsize; 9317c478bd9Sstevel@tonic-gate caddr_t cp; 9327c478bd9Sstevel@tonic-gate off_t filesize = s1p->st_size; 9337c478bd9Sstevel@tonic-gate off_t offset; 9347c478bd9Sstevel@tonic-gate int nbytes; 9357c478bd9Sstevel@tonic-gate int remains; 9367c478bd9Sstevel@tonic-gate int n; 9377c478bd9Sstevel@tonic-gate 9387c478bd9Sstevel@tonic-gate if (ISREG(*s1p) && s1p->st_size > SMALLFILESIZE) { 9397c478bd9Sstevel@tonic-gate /* 9407c478bd9Sstevel@tonic-gate * Determine size of initial mapping. This will determine the 9417c478bd9Sstevel@tonic-gate * size of the address space chunk we work with. This initial 9427c478bd9Sstevel@tonic-gate * mapping size will be used to perform munmap() in the future. 9437c478bd9Sstevel@tonic-gate */ 9447c478bd9Sstevel@tonic-gate mapsize = MAXMAPSIZE; 9457c478bd9Sstevel@tonic-gate if (s1p->st_size < mapsize) mapsize = s1p->st_size; 9467c478bd9Sstevel@tonic-gate munmapsize = mapsize; 9477c478bd9Sstevel@tonic-gate 9487c478bd9Sstevel@tonic-gate /* 9497c478bd9Sstevel@tonic-gate * Mmap time! 9507c478bd9Sstevel@tonic-gate */ 9517c478bd9Sstevel@tonic-gate if ((cp = mmap((caddr_t)NULL, mapsize, PROT_READ, 9527c478bd9Sstevel@tonic-gate MAP_SHARED, fi, (off_t)0)) == MAP_FAILED) 9537c478bd9Sstevel@tonic-gate mapsize = 0; /* can't mmap today */ 9547c478bd9Sstevel@tonic-gate } else 9557c478bd9Sstevel@tonic-gate mapsize = 0; 9567c478bd9Sstevel@tonic-gate 9577c478bd9Sstevel@tonic-gate if (mapsize != 0) { 9587c478bd9Sstevel@tonic-gate offset = 0; 9597c478bd9Sstevel@tonic-gate 9607c478bd9Sstevel@tonic-gate for (;;) { 9617c478bd9Sstevel@tonic-gate nbytes = write(fo, cp, mapsize); 9627c478bd9Sstevel@tonic-gate /* 9637c478bd9Sstevel@tonic-gate * if we write less than the mmaped size it's due to a 9647c478bd9Sstevel@tonic-gate * media error on the input file or out of space on 9657c478bd9Sstevel@tonic-gate * the output file. So, try again, and look for errno. 9667c478bd9Sstevel@tonic-gate */ 9677c478bd9Sstevel@tonic-gate if ((nbytes >= 0) && (nbytes != (int)mapsize)) { 9687c478bd9Sstevel@tonic-gate remains = mapsize - nbytes; 9697c478bd9Sstevel@tonic-gate while (remains > 0) { 9707c478bd9Sstevel@tonic-gate nbytes = write(fo, 9717c478bd9Sstevel@tonic-gate cp + mapsize - remains, remains); 9727c478bd9Sstevel@tonic-gate if (nbytes < 0) { 9737c478bd9Sstevel@tonic-gate if (errno == ENOSPC) 9747c478bd9Sstevel@tonic-gate Perror(target); 9757c478bd9Sstevel@tonic-gate else 9767c478bd9Sstevel@tonic-gate Perror(source); 9777c478bd9Sstevel@tonic-gate (void) close(fi); 9787c478bd9Sstevel@tonic-gate (void) close(fo); 9797c478bd9Sstevel@tonic-gate (void) munmap(cp, munmapsize); 9807c478bd9Sstevel@tonic-gate if (ISREG(*s2p)) 9817c478bd9Sstevel@tonic-gate (void) unlink(target); 9827c478bd9Sstevel@tonic-gate return (1); 9837c478bd9Sstevel@tonic-gate } 9847c478bd9Sstevel@tonic-gate remains -= nbytes; 9857c478bd9Sstevel@tonic-gate if (remains == 0) 9867c478bd9Sstevel@tonic-gate nbytes = mapsize; 9877c478bd9Sstevel@tonic-gate } 9887c478bd9Sstevel@tonic-gate } 9897c478bd9Sstevel@tonic-gate /* 9907c478bd9Sstevel@tonic-gate * although the write manual page doesn't specify this 9917c478bd9Sstevel@tonic-gate * as a possible errno, it is set when the nfs read 9927c478bd9Sstevel@tonic-gate * via the mmap'ed file is accessed, so report the 9937c478bd9Sstevel@tonic-gate * problem as a source access problem, not a target file 9947c478bd9Sstevel@tonic-gate * problem 9957c478bd9Sstevel@tonic-gate */ 9967c478bd9Sstevel@tonic-gate if (nbytes < 0) { 9977c478bd9Sstevel@tonic-gate if (errno == EACCES) 9987c478bd9Sstevel@tonic-gate Perror(source); 9997c478bd9Sstevel@tonic-gate else 10007c478bd9Sstevel@tonic-gate Perror(target); 10017c478bd9Sstevel@tonic-gate (void) close(fi); 10027c478bd9Sstevel@tonic-gate (void) close(fo); 10037c478bd9Sstevel@tonic-gate (void) munmap(cp, munmapsize); 10047c478bd9Sstevel@tonic-gate if (ISREG(*s2p)) 10057c478bd9Sstevel@tonic-gate (void) unlink(target); 10067c478bd9Sstevel@tonic-gate return (1); 10077c478bd9Sstevel@tonic-gate } 10087c478bd9Sstevel@tonic-gate filesize -= nbytes; 10097c478bd9Sstevel@tonic-gate if (filesize == 0) 10107c478bd9Sstevel@tonic-gate break; 10117c478bd9Sstevel@tonic-gate offset += nbytes; 10127c478bd9Sstevel@tonic-gate if (filesize < mapsize) 10137c478bd9Sstevel@tonic-gate mapsize = filesize; 10147c478bd9Sstevel@tonic-gate if (mmap(cp, mapsize, PROT_READ, MAP_SHARED | MAP_FIXED, 10157c478bd9Sstevel@tonic-gate fi, offset) == MAP_FAILED) { 10167c478bd9Sstevel@tonic-gate Perror(source); 10177c478bd9Sstevel@tonic-gate (void) close(fi); 10187c478bd9Sstevel@tonic-gate (void) close(fo); 10197c478bd9Sstevel@tonic-gate (void) munmap(cp, munmapsize); 10207c478bd9Sstevel@tonic-gate if (ISREG(*s2p)) 10217c478bd9Sstevel@tonic-gate (void) unlink(target); 10227c478bd9Sstevel@tonic-gate return (1); 10237c478bd9Sstevel@tonic-gate } 10247c478bd9Sstevel@tonic-gate } 10257c478bd9Sstevel@tonic-gate (void) munmap(cp, munmapsize); 10267c478bd9Sstevel@tonic-gate } else { 10277c478bd9Sstevel@tonic-gate char buf[SMALLFILESIZE]; 10287c478bd9Sstevel@tonic-gate for (;;) { 10297c478bd9Sstevel@tonic-gate n = read(fi, buf, sizeof (buf)); 10307c478bd9Sstevel@tonic-gate if (n == 0) { 10317c478bd9Sstevel@tonic-gate return (0); 10327c478bd9Sstevel@tonic-gate } else if (n < 0) { 10337c478bd9Sstevel@tonic-gate Perror2(source, "read"); 10347c478bd9Sstevel@tonic-gate (void) close(fi); 10357c478bd9Sstevel@tonic-gate (void) close(fo); 10367c478bd9Sstevel@tonic-gate if (ISREG(*s2p)) 10377c478bd9Sstevel@tonic-gate (void) unlink(target); 10387c478bd9Sstevel@tonic-gate return (1); 10397c478bd9Sstevel@tonic-gate } else if (write(fo, buf, n) != n) { 10407c478bd9Sstevel@tonic-gate Perror2(target, "write"); 10417c478bd9Sstevel@tonic-gate (void) close(fi); 10427c478bd9Sstevel@tonic-gate (void) close(fo); 10437c478bd9Sstevel@tonic-gate if (ISREG(*s2p)) 10447c478bd9Sstevel@tonic-gate (void) unlink(target); 10457c478bd9Sstevel@tonic-gate return (1); 10467c478bd9Sstevel@tonic-gate } 10477c478bd9Sstevel@tonic-gate } 10487c478bd9Sstevel@tonic-gate } 10497c478bd9Sstevel@tonic-gate return (0); 10507c478bd9Sstevel@tonic-gate } 10517c478bd9Sstevel@tonic-gate 10527c478bd9Sstevel@tonic-gate /* 10537c478bd9Sstevel@tonic-gate * create_tnode() 10547c478bd9Sstevel@tonic-gate * 10557c478bd9Sstevel@tonic-gate * Create a node for use with the search tree which contains the 10567c478bd9Sstevel@tonic-gate * inode information (device id and inode number). 10577c478bd9Sstevel@tonic-gate * 10587c478bd9Sstevel@tonic-gate * Input 10597c478bd9Sstevel@tonic-gate * dev - device id 10607c478bd9Sstevel@tonic-gate * ino - inode number 10617c478bd9Sstevel@tonic-gate * 10627c478bd9Sstevel@tonic-gate * Output 10637c478bd9Sstevel@tonic-gate * tnode - NULL on error, otherwise returns a tnode structure 10647c478bd9Sstevel@tonic-gate * which contains the input device id and inode number. 10657c478bd9Sstevel@tonic-gate */ 10667c478bd9Sstevel@tonic-gate static tree_node_t * 10677c478bd9Sstevel@tonic-gate create_tnode(dev_t dev, ino_t ino) 10687c478bd9Sstevel@tonic-gate { 10697c478bd9Sstevel@tonic-gate tree_node_t *tnode; 10707c478bd9Sstevel@tonic-gate 10717c478bd9Sstevel@tonic-gate if ((tnode = (tree_node_t *)malloc(sizeof (tree_node_t))) != NULL) { 10727c478bd9Sstevel@tonic-gate tnode->node_dev = dev; 10737c478bd9Sstevel@tonic-gate tnode->node_ino = ino; 10747c478bd9Sstevel@tonic-gate } 10757c478bd9Sstevel@tonic-gate 10767c478bd9Sstevel@tonic-gate return (tnode); 10777c478bd9Sstevel@tonic-gate } 10787c478bd9Sstevel@tonic-gate 10797c478bd9Sstevel@tonic-gate static int 10807c478bd9Sstevel@tonic-gate chkfiles(char *source, char **to) 10817c478bd9Sstevel@tonic-gate { 10827c478bd9Sstevel@tonic-gate char *buf = (char *)NULL; 10837c478bd9Sstevel@tonic-gate int (*statf)() = (cpy && 10847c478bd9Sstevel@tonic-gate !(Pflg || (Hflg && !cmdarg))) ? stat : lstat; 10857c478bd9Sstevel@tonic-gate char *target = *to; 1086fa9e4066Sahrens int error; 10877c478bd9Sstevel@tonic-gate 10887c478bd9Sstevel@tonic-gate /* 10897c478bd9Sstevel@tonic-gate * Make sure source file exists. 10907c478bd9Sstevel@tonic-gate */ 10917c478bd9Sstevel@tonic-gate if ((*statf)(source, &s1) < 0) { 10927c478bd9Sstevel@tonic-gate /* 10937c478bd9Sstevel@tonic-gate * Keep the old error message except when someone tries to 10947c478bd9Sstevel@tonic-gate * mv/cp/ln a symbolic link that has a trailing slash and 10957c478bd9Sstevel@tonic-gate * points to a file. 10967c478bd9Sstevel@tonic-gate */ 10977c478bd9Sstevel@tonic-gate if (errno == ENOTDIR) 10987c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: %s: %s\n", cmd, source, 10997c478bd9Sstevel@tonic-gate strerror(errno)); 11007c478bd9Sstevel@tonic-gate else 11017c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 11027c478bd9Sstevel@tonic-gate gettext("%s: cannot access %s\n"), cmd, source); 11037c478bd9Sstevel@tonic-gate return (1); 11047c478bd9Sstevel@tonic-gate } 11057c478bd9Sstevel@tonic-gate 11067c478bd9Sstevel@tonic-gate /* 11077c478bd9Sstevel@tonic-gate * Get ACL info: don't bother with ln or mv'ing symlinks 11087c478bd9Sstevel@tonic-gate */ 11097c478bd9Sstevel@tonic-gate if ((!lnk) && !(mve && ISLNK(s1))) { 1110fa9e4066Sahrens if (s1acl != NULL) { 1111fa9e4066Sahrens acl_free(s1acl); 1112fa9e4066Sahrens s1acl = NULL; 11137c478bd9Sstevel@tonic-gate } 1114fa9e4066Sahrens if ((error = acl_get(source, ACL_NO_TRIVIAL, &s1acl)) != 0) { 11157c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 1116fa9e4066Sahrens "%s: failed to get acl entries: %s\n", source, 1117fa9e4066Sahrens acl_strerror(error)); 11187c478bd9Sstevel@tonic-gate return (1); 11197c478bd9Sstevel@tonic-gate } 11207c478bd9Sstevel@tonic-gate /* else: just permission bits */ 11217c478bd9Sstevel@tonic-gate } 11227c478bd9Sstevel@tonic-gate 11237c478bd9Sstevel@tonic-gate /* 11247c478bd9Sstevel@tonic-gate * If stat fails, then the target doesn't exist, 11257c478bd9Sstevel@tonic-gate * we will create a new target with default file type of regular. 11267c478bd9Sstevel@tonic-gate */ 11277c478bd9Sstevel@tonic-gate 11287c478bd9Sstevel@tonic-gate FTYPE(s2) = S_IFREG; 11297c478bd9Sstevel@tonic-gate targetexists = 0; 11307c478bd9Sstevel@tonic-gate if ((*statf)(target, &s2) >= 0) { 11317c478bd9Sstevel@tonic-gate if (ISLNK(s2)) 11327c478bd9Sstevel@tonic-gate (void) stat(target, &s2); 11337c478bd9Sstevel@tonic-gate /* 11347c478bd9Sstevel@tonic-gate * If target is a directory, 11357c478bd9Sstevel@tonic-gate * make complete name of new file 11367c478bd9Sstevel@tonic-gate * within that directory. 11377c478bd9Sstevel@tonic-gate */ 11387c478bd9Sstevel@tonic-gate if (ISDIR(s2)) { 11397c478bd9Sstevel@tonic-gate size_t len; 11407c478bd9Sstevel@tonic-gate 11417c478bd9Sstevel@tonic-gate len = strlen(target) + strlen(dname(source)) + 4; 11427c478bd9Sstevel@tonic-gate if ((buf = (char *)malloc(len)) == NULL) { 11437c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 11447c478bd9Sstevel@tonic-gate gettext("%s: Insufficient memory to " 11457c478bd9Sstevel@tonic-gate "%s %s\n "), cmd, cmd, source); 11467c478bd9Sstevel@tonic-gate exit(3); 11477c478bd9Sstevel@tonic-gate } 11487c478bd9Sstevel@tonic-gate (void) snprintf(buf, len, "%s/%s", 11497c478bd9Sstevel@tonic-gate target, dname(source)); 11507c478bd9Sstevel@tonic-gate *to = target = buf; 11517c478bd9Sstevel@tonic-gate } 11527c478bd9Sstevel@tonic-gate 11537c478bd9Sstevel@tonic-gate if ((*statf)(target, &s2) >= 0) { 11547c478bd9Sstevel@tonic-gate int overwrite = FALSE; 11557c478bd9Sstevel@tonic-gate int override = FALSE; 11567c478bd9Sstevel@tonic-gate 11577c478bd9Sstevel@tonic-gate targetexists++; 11587c478bd9Sstevel@tonic-gate if (cpy || mve) { 11597c478bd9Sstevel@tonic-gate /* 11607c478bd9Sstevel@tonic-gate * For cp and mv, it is an error if the 11617c478bd9Sstevel@tonic-gate * source and target are the same file. 11627c478bd9Sstevel@tonic-gate * Check for the same inode and file 11637c478bd9Sstevel@tonic-gate * system, but don't check for the same 11647c478bd9Sstevel@tonic-gate * absolute pathname because it is an 11657c478bd9Sstevel@tonic-gate * error when the source and target are 11667c478bd9Sstevel@tonic-gate * hard links to the same file. 11677c478bd9Sstevel@tonic-gate */ 11687c478bd9Sstevel@tonic-gate if (IDENTICAL(s1, s2)) { 11697c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 11707c478bd9Sstevel@tonic-gate gettext( 11717c478bd9Sstevel@tonic-gate "%s: %s and %s are identical\n"), 11727c478bd9Sstevel@tonic-gate cmd, source, target); 11737c478bd9Sstevel@tonic-gate if (buf != NULL) 11747c478bd9Sstevel@tonic-gate free(buf); 11757c478bd9Sstevel@tonic-gate return (1); 11767c478bd9Sstevel@tonic-gate } 11777c478bd9Sstevel@tonic-gate } 11787c478bd9Sstevel@tonic-gate if (lnk) { 11797c478bd9Sstevel@tonic-gate /* 11807c478bd9Sstevel@tonic-gate * For ln, it is an error if the source and 11817c478bd9Sstevel@tonic-gate * target are identical files (same inode, 11827c478bd9Sstevel@tonic-gate * same file system, and filenames resolve 11837c478bd9Sstevel@tonic-gate * to same absolute pathname). 11847c478bd9Sstevel@tonic-gate */ 11857c478bd9Sstevel@tonic-gate if (!chk_different(source, target)) { 11867c478bd9Sstevel@tonic-gate if (buf != NULL) 11877c478bd9Sstevel@tonic-gate free(buf); 11887c478bd9Sstevel@tonic-gate return (1); 11897c478bd9Sstevel@tonic-gate } 11907c478bd9Sstevel@tonic-gate } 11917c478bd9Sstevel@tonic-gate if (lnk && !silent) { 11927c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 11937c478bd9Sstevel@tonic-gate gettext("%s: %s: File exists\n"), 11947c478bd9Sstevel@tonic-gate cmd, target); 11957c478bd9Sstevel@tonic-gate if (buf != NULL) 11967c478bd9Sstevel@tonic-gate free(buf); 11977c478bd9Sstevel@tonic-gate return (1); 11987c478bd9Sstevel@tonic-gate } 11997c478bd9Sstevel@tonic-gate 12007c478bd9Sstevel@tonic-gate /* 12017c478bd9Sstevel@tonic-gate * overwrite: 12027c478bd9Sstevel@tonic-gate * If the user does not have access to 12037c478bd9Sstevel@tonic-gate * the target, ask ----if it is not 12047c478bd9Sstevel@tonic-gate * silent and user invoked command 12057c478bd9Sstevel@tonic-gate * interactively. 12067c478bd9Sstevel@tonic-gate * 12077c478bd9Sstevel@tonic-gate * override: 12087c478bd9Sstevel@tonic-gate * If not silent, and stdin is a terminal, and 12097c478bd9Sstevel@tonic-gate * there's no write access, and the file isn't a 12107c478bd9Sstevel@tonic-gate * symbolic link, ask for permission. 12117c478bd9Sstevel@tonic-gate * 12127c478bd9Sstevel@tonic-gate * XPG4: both overwrite and override: 12137c478bd9Sstevel@tonic-gate * ask only one question. 12147c478bd9Sstevel@tonic-gate * 12157c478bd9Sstevel@tonic-gate * TRANSLATION_NOTE - The following messages will 12167c478bd9Sstevel@tonic-gate * contain the first character of the strings for 12177c478bd9Sstevel@tonic-gate * "yes" and "no" defined in the file 12187c478bd9Sstevel@tonic-gate * "nl_langinfo.po". After substitution, the 12197c478bd9Sstevel@tonic-gate * message will appear as follows: 12207c478bd9Sstevel@tonic-gate * <cmd>: overwrite <filename> (y/n)? 12217c478bd9Sstevel@tonic-gate * where <cmd> is the name of the command 12227c478bd9Sstevel@tonic-gate * (cp, mv) and <filename> is the destination file 12237c478bd9Sstevel@tonic-gate */ 12247c478bd9Sstevel@tonic-gate 12257c478bd9Sstevel@tonic-gate 12267c478bd9Sstevel@tonic-gate overwrite = iflg && !silent && use_stdin(); 12277c478bd9Sstevel@tonic-gate override = !cpy && (access(target, 2) < 0) && 12287c478bd9Sstevel@tonic-gate !silent && use_stdin() && !ISLNK(s2); 12297c478bd9Sstevel@tonic-gate 12307c478bd9Sstevel@tonic-gate if (overwrite && override) 12317c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 12327c478bd9Sstevel@tonic-gate gettext("%s: overwrite %s and override " 12337c478bd9Sstevel@tonic-gate "protection %o (%s/%s)? "), cmd, target, 12347c478bd9Sstevel@tonic-gate FMODE(s2) & MODEBITS, yeschr, nochr); 12357c478bd9Sstevel@tonic-gate else if (overwrite && ISREG(s2)) 12367c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 12377c478bd9Sstevel@tonic-gate gettext("%s: overwrite %s (%s/%s)? "), 12387c478bd9Sstevel@tonic-gate cmd, target, yeschr, nochr); 12397c478bd9Sstevel@tonic-gate else if (override) 12407c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 12417c478bd9Sstevel@tonic-gate gettext("%s: %s: override protection " 12427c478bd9Sstevel@tonic-gate /*CSTYLED*/ 12437c478bd9Sstevel@tonic-gate "%o (%s/%s)? "), 12447c478bd9Sstevel@tonic-gate /*CSTYLED*/ 12457c478bd9Sstevel@tonic-gate cmd, target, FMODE(s2) & MODEBITS, 12467c478bd9Sstevel@tonic-gate yeschr, nochr); 12477c478bd9Sstevel@tonic-gate if (overwrite || override) { 12487c478bd9Sstevel@tonic-gate if (ISREG(s2)) { 12497c478bd9Sstevel@tonic-gate if (getresp()) { 12507c478bd9Sstevel@tonic-gate if (buf != NULL) 12517c478bd9Sstevel@tonic-gate free(buf); 12527c478bd9Sstevel@tonic-gate return (2); 12537c478bd9Sstevel@tonic-gate } 12547c478bd9Sstevel@tonic-gate } 12557c478bd9Sstevel@tonic-gate } 12567c478bd9Sstevel@tonic-gate if (lnk && unlink(target) < 0) { 12577c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 12587c478bd9Sstevel@tonic-gate gettext("%s: cannot unlink %s: "), 12597c478bd9Sstevel@tonic-gate cmd, target); 12607c478bd9Sstevel@tonic-gate perror(""); 12617c478bd9Sstevel@tonic-gate return (1); 12627c478bd9Sstevel@tonic-gate } 12637c478bd9Sstevel@tonic-gate } 12647c478bd9Sstevel@tonic-gate } 12657c478bd9Sstevel@tonic-gate return (0); 12667c478bd9Sstevel@tonic-gate } 12677c478bd9Sstevel@tonic-gate 12687c478bd9Sstevel@tonic-gate /* 12697c478bd9Sstevel@tonic-gate * check whether source and target are different 12707c478bd9Sstevel@tonic-gate * return 1 when they are different 12717c478bd9Sstevel@tonic-gate * return 0 when they are identical, or when unable to resolve a pathname 12727c478bd9Sstevel@tonic-gate */ 12737c478bd9Sstevel@tonic-gate static int 12747c478bd9Sstevel@tonic-gate chk_different(char *source, char *target) 12757c478bd9Sstevel@tonic-gate { 12767c478bd9Sstevel@tonic-gate char rtarget[PATH_MAX], rsource[PATH_MAX]; 12777c478bd9Sstevel@tonic-gate 12787c478bd9Sstevel@tonic-gate if (IDENTICAL(s1, s2)) { 12797c478bd9Sstevel@tonic-gate /* 12807c478bd9Sstevel@tonic-gate * IDENTICAL will be true for hard links, therefore 12817c478bd9Sstevel@tonic-gate * check whether the filenames are different 12827c478bd9Sstevel@tonic-gate */ 12837c478bd9Sstevel@tonic-gate if ((getrealpath(source, rsource) == 0) || 12847c478bd9Sstevel@tonic-gate (getrealpath(target, rtarget) == 0)) { 12857c478bd9Sstevel@tonic-gate return (0); 12867c478bd9Sstevel@tonic-gate } 12877c478bd9Sstevel@tonic-gate if (strncmp(rsource, rtarget, PATH_MAX) == 0) { 12887c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext( 12897c478bd9Sstevel@tonic-gate "%s: %s and %s are identical\n"), 12907c478bd9Sstevel@tonic-gate cmd, source, target); 12917c478bd9Sstevel@tonic-gate return (0); 12927c478bd9Sstevel@tonic-gate } 12937c478bd9Sstevel@tonic-gate } 12947c478bd9Sstevel@tonic-gate return (1); 12957c478bd9Sstevel@tonic-gate } 12967c478bd9Sstevel@tonic-gate 12977c478bd9Sstevel@tonic-gate /* 12987c478bd9Sstevel@tonic-gate * get real path (resolved absolute pathname) 12997c478bd9Sstevel@tonic-gate * return 1 on success, 0 on failure 13007c478bd9Sstevel@tonic-gate */ 13017c478bd9Sstevel@tonic-gate static int 13027c478bd9Sstevel@tonic-gate getrealpath(char *path, char *rpath) 13037c478bd9Sstevel@tonic-gate { 13047c478bd9Sstevel@tonic-gate if (realpath(path, rpath) == NULL) { 13057c478bd9Sstevel@tonic-gate int errno_save = errno; 13067c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext( 13077c478bd9Sstevel@tonic-gate "%s: can't resolve path %s: "), cmd, path); 13087c478bd9Sstevel@tonic-gate errno = errno_save; 13097c478bd9Sstevel@tonic-gate perror(""); 13107c478bd9Sstevel@tonic-gate return (0); 13117c478bd9Sstevel@tonic-gate } 13127c478bd9Sstevel@tonic-gate return (1); 13137c478bd9Sstevel@tonic-gate } 13147c478bd9Sstevel@tonic-gate 13157c478bd9Sstevel@tonic-gate static int 13167c478bd9Sstevel@tonic-gate rcopy(char *from, char *to) 13177c478bd9Sstevel@tonic-gate { 13187c478bd9Sstevel@tonic-gate DIR *fold = opendir(from); 13197c478bd9Sstevel@tonic-gate struct dirent *dp; 13207c478bd9Sstevel@tonic-gate struct stat statb, s1save; 13217c478bd9Sstevel@tonic-gate int errs = 0; 13227c478bd9Sstevel@tonic-gate char fromname[PATH_MAX]; 13237c478bd9Sstevel@tonic-gate 13247c478bd9Sstevel@tonic-gate if (fold == 0 || ((pflg || mve) && fstat(fold->dd_fd, &statb) < 0)) { 13257c478bd9Sstevel@tonic-gate Perror(from); 13267c478bd9Sstevel@tonic-gate return (1); 13277c478bd9Sstevel@tonic-gate } 13287c478bd9Sstevel@tonic-gate if (pflg || mve) { 13297c478bd9Sstevel@tonic-gate /* 13307c478bd9Sstevel@tonic-gate * Save s1 (stat information for source dir) so that 13317c478bd9Sstevel@tonic-gate * mod and access times can be reserved during "cp -p" 13327c478bd9Sstevel@tonic-gate * or mv, since s1 gets overwritten. 13337c478bd9Sstevel@tonic-gate */ 13347c478bd9Sstevel@tonic-gate s1save = s1; 13357c478bd9Sstevel@tonic-gate } 13367c478bd9Sstevel@tonic-gate for (;;) { 13377c478bd9Sstevel@tonic-gate dp = readdir(fold); 13387c478bd9Sstevel@tonic-gate if (dp == 0) { 13397c478bd9Sstevel@tonic-gate (void) closedir(fold); 13407c478bd9Sstevel@tonic-gate if (pflg || mve) 13417c478bd9Sstevel@tonic-gate return (chg_time(to, s1save) + errs); 13427c478bd9Sstevel@tonic-gate return (errs); 13437c478bd9Sstevel@tonic-gate } 13447c478bd9Sstevel@tonic-gate if (dp->d_ino == 0) 13457c478bd9Sstevel@tonic-gate continue; 13467c478bd9Sstevel@tonic-gate if ((strcmp(dp->d_name, ".") == 0) || 13477c478bd9Sstevel@tonic-gate (strcmp(dp->d_name, "..") == 0)) 13487c478bd9Sstevel@tonic-gate continue; 13497c478bd9Sstevel@tonic-gate if (strlen(from)+1+strlen(dp->d_name) >= 13507c478bd9Sstevel@tonic-gate sizeof (fromname) - 1) { 13517c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 13527c478bd9Sstevel@tonic-gate gettext("%s : %s/%s: Name too long\n"), 13537c478bd9Sstevel@tonic-gate cmd, from, dp->d_name); 13547c478bd9Sstevel@tonic-gate errs++; 13557c478bd9Sstevel@tonic-gate continue; 13567c478bd9Sstevel@tonic-gate } 13577c478bd9Sstevel@tonic-gate (void) snprintf(fromname, sizeof (fromname), 13587c478bd9Sstevel@tonic-gate "%s/%s", from, dp->d_name); 13597c478bd9Sstevel@tonic-gate errs += cpymve(fromname, to); 13607c478bd9Sstevel@tonic-gate } 13617c478bd9Sstevel@tonic-gate } 13627c478bd9Sstevel@tonic-gate 13637c478bd9Sstevel@tonic-gate static char * 13647c478bd9Sstevel@tonic-gate dname(char *name) 13657c478bd9Sstevel@tonic-gate { 13667c478bd9Sstevel@tonic-gate register char *p; 13677c478bd9Sstevel@tonic-gate 13687c478bd9Sstevel@tonic-gate /* 13697c478bd9Sstevel@tonic-gate * Return just the file name given the complete path. 13707c478bd9Sstevel@tonic-gate * Like basename(1). 13717c478bd9Sstevel@tonic-gate */ 13727c478bd9Sstevel@tonic-gate 13737c478bd9Sstevel@tonic-gate p = name; 13747c478bd9Sstevel@tonic-gate 13757c478bd9Sstevel@tonic-gate /* 13767c478bd9Sstevel@tonic-gate * While there are characters left, 13777c478bd9Sstevel@tonic-gate * set name to start after last 13787c478bd9Sstevel@tonic-gate * delimiter. 13797c478bd9Sstevel@tonic-gate */ 13807c478bd9Sstevel@tonic-gate 13817c478bd9Sstevel@tonic-gate while (*p) 13827c478bd9Sstevel@tonic-gate if (*p++ == DELIM && *p) 13837c478bd9Sstevel@tonic-gate name = p; 13847c478bd9Sstevel@tonic-gate return (name); 13857c478bd9Sstevel@tonic-gate } 13867c478bd9Sstevel@tonic-gate 13877c478bd9Sstevel@tonic-gate static int 13887c478bd9Sstevel@tonic-gate getresp(void) 13897c478bd9Sstevel@tonic-gate { 13907c478bd9Sstevel@tonic-gate register int c, i; 13917c478bd9Sstevel@tonic-gate char ans_buf[SCHAR_MAX + 1]; 13927c478bd9Sstevel@tonic-gate 13937c478bd9Sstevel@tonic-gate /* 13947c478bd9Sstevel@tonic-gate * Get response from user. Based on 13957c478bd9Sstevel@tonic-gate * first character, make decision. 13967c478bd9Sstevel@tonic-gate * Discard rest of line. 13977c478bd9Sstevel@tonic-gate */ 13987c478bd9Sstevel@tonic-gate for (i = 0; ; i++) { 13997c478bd9Sstevel@tonic-gate c = getchar(); 14007c478bd9Sstevel@tonic-gate if (c == '\n' || c == 0 || c == EOF) { 14017c478bd9Sstevel@tonic-gate ans_buf[i] = 0; 14027c478bd9Sstevel@tonic-gate break; 14037c478bd9Sstevel@tonic-gate } 14047c478bd9Sstevel@tonic-gate if (i < SCHAR_MAX) 14057c478bd9Sstevel@tonic-gate ans_buf[i] = c; 14067c478bd9Sstevel@tonic-gate } 14077c478bd9Sstevel@tonic-gate if (i >= SCHAR_MAX) { 14087c478bd9Sstevel@tonic-gate i = SCHAR_MAX; 14097c478bd9Sstevel@tonic-gate ans_buf[SCHAR_MAX] = 0; 14107c478bd9Sstevel@tonic-gate } 14117c478bd9Sstevel@tonic-gate if ((i == 0) | (strncmp(yeschr, ans_buf, i))) 14127c478bd9Sstevel@tonic-gate return (1); 14137c478bd9Sstevel@tonic-gate return (0); 14147c478bd9Sstevel@tonic-gate } 14157c478bd9Sstevel@tonic-gate 14167c478bd9Sstevel@tonic-gate static void 14177c478bd9Sstevel@tonic-gate usage(void) 14187c478bd9Sstevel@tonic-gate { 14197c478bd9Sstevel@tonic-gate /* 14207c478bd9Sstevel@tonic-gate * Display usage message. 14217c478bd9Sstevel@tonic-gate */ 14227c478bd9Sstevel@tonic-gate 14237c478bd9Sstevel@tonic-gate if (mve) { 14247c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext( 14257c478bd9Sstevel@tonic-gate "Usage: mv [-f] [-i] f1 f2\n" 14267c478bd9Sstevel@tonic-gate " mv [-f] [-i] f1 ... fn d1\n" 14277c478bd9Sstevel@tonic-gate " mv [-f] [-i] d1 d2\n")); 14287c478bd9Sstevel@tonic-gate } else if (lnk) { 14297c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext( 14307c478bd9Sstevel@tonic-gate #ifdef XPG4 14317c478bd9Sstevel@tonic-gate "Usage: ln [-f] [-s] f1 [f2]\n" 14327c478bd9Sstevel@tonic-gate " ln [-f] [-s] f1 ... fn d1\n" 14337c478bd9Sstevel@tonic-gate " ln [-f] -s d1 d2\n")); 14347c478bd9Sstevel@tonic-gate #else 14357c478bd9Sstevel@tonic-gate "Usage: ln [-f] [-n] [-s] f1 [f2]\n" 14367c478bd9Sstevel@tonic-gate " ln [-f] [-n] [-s] f1 ... fn d1\n" 14377c478bd9Sstevel@tonic-gate " ln [-f] [-n] -s d1 d2\n")); 14387c478bd9Sstevel@tonic-gate #endif 14397c478bd9Sstevel@tonic-gate } else if (cpy) { 14407c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext( 14417c478bd9Sstevel@tonic-gate "Usage: cp [-f] [-i] [-p] [-@] f1 f2\n" 14427c478bd9Sstevel@tonic-gate " cp [-f] [-i] [-p] [-@] f1 ... fn d1\n" 14437c478bd9Sstevel@tonic-gate " cp -r|-R [-H|-L|-P] [-f] [-i] [-p] [-@] " 14447c478bd9Sstevel@tonic-gate "d1 ... dn-1 dn\n")); 14457c478bd9Sstevel@tonic-gate } 14467c478bd9Sstevel@tonic-gate exit(2); 14477c478bd9Sstevel@tonic-gate } 14487c478bd9Sstevel@tonic-gate 14497c478bd9Sstevel@tonic-gate /* 14507c478bd9Sstevel@tonic-gate * chg_time() 14517c478bd9Sstevel@tonic-gate * 14527c478bd9Sstevel@tonic-gate * Try to preserve modification and access time. 14537c478bd9Sstevel@tonic-gate * If 1) pflg is not set, or 2) pflg is set and this is the Solaris version, 14547c478bd9Sstevel@tonic-gate * don't report a utime() failure. 14557c478bd9Sstevel@tonic-gate * If this is the XPG4 version and utime fails, if 1) pflg is set (cp -p) 14567c478bd9Sstevel@tonic-gate * or 2) we are doing a mv, print a diagnostic message; arrange for a non-zero 14577c478bd9Sstevel@tonic-gate * exit status only if pflg is set. 14587c478bd9Sstevel@tonic-gate * utimes(2) is being used to achieve granularity in 14597c478bd9Sstevel@tonic-gate * microseconds while setting file times. 14607c478bd9Sstevel@tonic-gate */ 14617c478bd9Sstevel@tonic-gate static int 14627c478bd9Sstevel@tonic-gate chg_time(char *to, struct stat ss) 14637c478bd9Sstevel@tonic-gate { 14647c478bd9Sstevel@tonic-gate struct timeval times[2]; 14657c478bd9Sstevel@tonic-gate int rc; 14667c478bd9Sstevel@tonic-gate 14677c478bd9Sstevel@tonic-gate timestruc_to_timeval(&ss.st_atim, times); 14687c478bd9Sstevel@tonic-gate timestruc_to_timeval(&ss.st_mtim, times + 1); 14697c478bd9Sstevel@tonic-gate 14707c478bd9Sstevel@tonic-gate rc = utimes(to, times); 14717c478bd9Sstevel@tonic-gate #ifdef XPG4 14727c478bd9Sstevel@tonic-gate if ((pflg || mve) && rc != 0) { 14737c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 14747c478bd9Sstevel@tonic-gate gettext("%s: cannot set times for %s: "), cmd, to); 14757c478bd9Sstevel@tonic-gate perror(""); 14767c478bd9Sstevel@tonic-gate if (pflg) 14777c478bd9Sstevel@tonic-gate return (1); 14787c478bd9Sstevel@tonic-gate } 14797c478bd9Sstevel@tonic-gate #endif 14807c478bd9Sstevel@tonic-gate 14817c478bd9Sstevel@tonic-gate return (0); 14827c478bd9Sstevel@tonic-gate 14837c478bd9Sstevel@tonic-gate } 14847c478bd9Sstevel@tonic-gate 14857c478bd9Sstevel@tonic-gate /* 14867c478bd9Sstevel@tonic-gate * chg_mode() 14877c478bd9Sstevel@tonic-gate * 14887c478bd9Sstevel@tonic-gate * This function is called upon "cp -p" or mv across filesystems. 14897c478bd9Sstevel@tonic-gate * 14907c478bd9Sstevel@tonic-gate * Try to preserve the owner and group id. If chown() fails, 14917c478bd9Sstevel@tonic-gate * only print a diagnostic message if doing a mv in the XPG4 version; 14927c478bd9Sstevel@tonic-gate * try to clear S_ISUID and S_ISGID bits in the target. If unable to clear 14937c478bd9Sstevel@tonic-gate * S_ISUID and S_ISGID bits, print a diagnostic message and arrange for a 14947c478bd9Sstevel@tonic-gate * non-zero exit status because this is a security violation. 14957c478bd9Sstevel@tonic-gate * Try to preserve permissions. 14967c478bd9Sstevel@tonic-gate * If this is the XPG4 version and chmod() fails, print a diagnostic message 14977c478bd9Sstevel@tonic-gate * and arrange for a non-zero exit status. 14987c478bd9Sstevel@tonic-gate * If this is the Solaris version and chmod() fails, do not print a 14997c478bd9Sstevel@tonic-gate * diagnostic message or exit with a non-zero value. 15007c478bd9Sstevel@tonic-gate */ 15017c478bd9Sstevel@tonic-gate static int 15027c478bd9Sstevel@tonic-gate chg_mode(char *target, uid_t uid, gid_t gid, mode_t mode) 15037c478bd9Sstevel@tonic-gate { 15047c478bd9Sstevel@tonic-gate int clearflg = 0; /* controls message printed upon chown() error */ 15057c478bd9Sstevel@tonic-gate 15067c478bd9Sstevel@tonic-gate if (chown(target, uid, gid) != 0) { 15077c478bd9Sstevel@tonic-gate #ifdef XPG4 15087c478bd9Sstevel@tonic-gate if (mve) { 15097c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s: cannot change" 15107c478bd9Sstevel@tonic-gate " owner and group of %s: "), cmd, target); 15117c478bd9Sstevel@tonic-gate perror(""); 15127c478bd9Sstevel@tonic-gate } 15137c478bd9Sstevel@tonic-gate #endif 15147c478bd9Sstevel@tonic-gate if (mode & (S_ISUID | S_ISGID)) { 15157c478bd9Sstevel@tonic-gate /* try to clear S_ISUID and S_ISGID */ 15167c478bd9Sstevel@tonic-gate mode &= ~S_ISUID & ~S_ISGID; 15177c478bd9Sstevel@tonic-gate ++clearflg; 15187c478bd9Sstevel@tonic-gate } 15197c478bd9Sstevel@tonic-gate } 15207c478bd9Sstevel@tonic-gate if (chmod(target, mode) != 0) { 15217c478bd9Sstevel@tonic-gate if (clearflg) { 15227c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext( 15237c478bd9Sstevel@tonic-gate "%s: cannot clear S_ISUID and S_ISGID bits in" 15247c478bd9Sstevel@tonic-gate " %s: "), cmd, target); 15257c478bd9Sstevel@tonic-gate perror(""); 15267c478bd9Sstevel@tonic-gate /* cp -p should get non-zero exit; mv should not */ 15277c478bd9Sstevel@tonic-gate if (pflg) 15287c478bd9Sstevel@tonic-gate return (1); 15297c478bd9Sstevel@tonic-gate } 15307c478bd9Sstevel@tonic-gate #ifdef XPG4 15317c478bd9Sstevel@tonic-gate else { 15327c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext( 15337c478bd9Sstevel@tonic-gate "%s: cannot set permissions for %s: "), cmd, target); 15347c478bd9Sstevel@tonic-gate perror(""); 15357c478bd9Sstevel@tonic-gate /* cp -p should get non-zero exit; mv should not */ 15367c478bd9Sstevel@tonic-gate if (pflg) 15377c478bd9Sstevel@tonic-gate return (1); 15387c478bd9Sstevel@tonic-gate } 15397c478bd9Sstevel@tonic-gate #endif 15407c478bd9Sstevel@tonic-gate } 15417c478bd9Sstevel@tonic-gate return (0); 15427c478bd9Sstevel@tonic-gate 15437c478bd9Sstevel@tonic-gate } 15447c478bd9Sstevel@tonic-gate 15457c478bd9Sstevel@tonic-gate static void 15467c478bd9Sstevel@tonic-gate Perror(char *s) 15477c478bd9Sstevel@tonic-gate { 15487c478bd9Sstevel@tonic-gate char buf[PATH_MAX + 10]; 15497c478bd9Sstevel@tonic-gate 15507c478bd9Sstevel@tonic-gate (void) snprintf(buf, sizeof (buf), "%s: %s", cmd, s); 15517c478bd9Sstevel@tonic-gate perror(buf); 15527c478bd9Sstevel@tonic-gate } 15537c478bd9Sstevel@tonic-gate 15547c478bd9Sstevel@tonic-gate static void 15557c478bd9Sstevel@tonic-gate Perror2(char *s1, char *s2) 15567c478bd9Sstevel@tonic-gate { 15577c478bd9Sstevel@tonic-gate char buf[PATH_MAX + 20]; 15587c478bd9Sstevel@tonic-gate 15597c478bd9Sstevel@tonic-gate (void) snprintf(buf, sizeof (buf), "%s: %s: %s", 15607c478bd9Sstevel@tonic-gate cmd, gettext(s1), gettext(s2)); 15617c478bd9Sstevel@tonic-gate perror(buf); 15627c478bd9Sstevel@tonic-gate } 15637c478bd9Sstevel@tonic-gate 15647c478bd9Sstevel@tonic-gate /* 15657c478bd9Sstevel@tonic-gate * used for cp -R and for mv across file systems 15667c478bd9Sstevel@tonic-gate */ 15677c478bd9Sstevel@tonic-gate static int 15687c478bd9Sstevel@tonic-gate copydir(char *source, char *target) 15697c478bd9Sstevel@tonic-gate { 15707c478bd9Sstevel@tonic-gate int ret, attret = 0; 15717c478bd9Sstevel@tonic-gate int pret = 0; /* need separate flag if -p is specified */ 15727c478bd9Sstevel@tonic-gate mode_t fixmode = (mode_t)0; /* cleanup mode after copy */ 15737c478bd9Sstevel@tonic-gate struct stat s1save; 1574fa9e4066Sahrens acl_t *s1acl_save; 1575fa9e4066Sahrens 1576fa9e4066Sahrens s1acl_save = NULL; 15777c478bd9Sstevel@tonic-gate 15787c478bd9Sstevel@tonic-gate if (cpy && !rflg) { 15797c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 15807c478bd9Sstevel@tonic-gate gettext("%s: %s: is a directory\n"), cmd, source); 15817c478bd9Sstevel@tonic-gate return (1); 15827c478bd9Sstevel@tonic-gate } 15837c478bd9Sstevel@tonic-gate 15847c478bd9Sstevel@tonic-gate if (stat(target, &s2) < 0) { 15857c478bd9Sstevel@tonic-gate if (mkdir(target, (s1.st_mode & MODEBITS)) < 0) { 15867c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: ", cmd); 15877c478bd9Sstevel@tonic-gate perror(target); 15887c478bd9Sstevel@tonic-gate return (1); 15897c478bd9Sstevel@tonic-gate } 15907c478bd9Sstevel@tonic-gate if (stat(target, &s2) == 0) { 15917c478bd9Sstevel@tonic-gate fixmode = s2.st_mode; 15927c478bd9Sstevel@tonic-gate } else { 15937c478bd9Sstevel@tonic-gate fixmode = s1.st_mode; 15947c478bd9Sstevel@tonic-gate } 15957c478bd9Sstevel@tonic-gate (void) chmod(target, ((fixmode & MODEBITS) | S_IRWXU)); 15967c478bd9Sstevel@tonic-gate } else if (!(ISDIR(s2))) { 15977c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 15987c478bd9Sstevel@tonic-gate gettext("%s: %s: not a directory.\n"), cmd, target); 15997c478bd9Sstevel@tonic-gate return (1); 16007c478bd9Sstevel@tonic-gate } 16017c478bd9Sstevel@tonic-gate if (pflg || mve) { 16027c478bd9Sstevel@tonic-gate /* 16037c478bd9Sstevel@tonic-gate * Save s1 (stat information for source dir) and acl info, 16047c478bd9Sstevel@tonic-gate * if any, so that ownership, modes, times, and acl's can 16057c478bd9Sstevel@tonic-gate * be reserved during "cp -p" or mv. 16067c478bd9Sstevel@tonic-gate * s1 gets overwritten when doing the recursive copy. 16077c478bd9Sstevel@tonic-gate */ 16087c478bd9Sstevel@tonic-gate s1save = s1; 1609fa9e4066Sahrens if (s1acl != NULL) { 1610fa9e4066Sahrens s1acl_save = acl_dup(s1acl); 1611fa9e4066Sahrens if (s1acl_save == NULL) { 1612fa9e4066Sahrens (void) fprintf(stderr, gettext("%s: " 1613fa9e4066Sahrens "Insufficient memory to save acl" 1614fa9e4066Sahrens " entry\n"), cmd); 1615fa9e4066Sahrens if (pflg) 1616fa9e4066Sahrens return (1); 1617fa9e4066Sahrens 16187c478bd9Sstevel@tonic-gate } 16197c478bd9Sstevel@tonic-gate #ifdef XPG4 16207c478bd9Sstevel@tonic-gate else { 16217c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s: " 16227c478bd9Sstevel@tonic-gate "Insufficient memory to save acl" 16237c478bd9Sstevel@tonic-gate " entry\n"), cmd); 16247c478bd9Sstevel@tonic-gate if (pflg) 16257c478bd9Sstevel@tonic-gate return (1); 16267c478bd9Sstevel@tonic-gate } 16277c478bd9Sstevel@tonic-gate #endif 16287c478bd9Sstevel@tonic-gate } 16297c478bd9Sstevel@tonic-gate } 16307c478bd9Sstevel@tonic-gate 16317c478bd9Sstevel@tonic-gate ret = rcopy(source, target); 16327c478bd9Sstevel@tonic-gate 16337c478bd9Sstevel@tonic-gate /* 16347c478bd9Sstevel@tonic-gate * Once we created a directory, go ahead and set 16357c478bd9Sstevel@tonic-gate * its attributes, e.g. acls and time. The info 16367c478bd9Sstevel@tonic-gate * may get overwritten if we continue traversing 16377c478bd9Sstevel@tonic-gate * down the tree. 16387c478bd9Sstevel@tonic-gate * 16397c478bd9Sstevel@tonic-gate * ACL for directory 16407c478bd9Sstevel@tonic-gate */ 16417c478bd9Sstevel@tonic-gate if (pflg || mve) { 1642*d2443e76Smarks if ((pret = chg_mode(target, UID(s1save), GID(s1save), 1643*d2443e76Smarks FMODE(s1save))) == 0) 1644*d2443e76Smarks pret = chg_time(target, s1save); 1645*d2443e76Smarks ret += pret; 1646fa9e4066Sahrens if (s1acl_save != NULL) { 1647fa9e4066Sahrens if (acl_set(target, s1acl_save) < 0) { 16487c478bd9Sstevel@tonic-gate #ifdef XPG4 16497c478bd9Sstevel@tonic-gate if (pflg || mve) { 16507c478bd9Sstevel@tonic-gate #else 16517c478bd9Sstevel@tonic-gate if (pflg) { 16527c478bd9Sstevel@tonic-gate #endif 16537c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext( 16547c478bd9Sstevel@tonic-gate "%s: failed to set acl entries " 16557c478bd9Sstevel@tonic-gate "on %s\n"), cmd, target); 16567c478bd9Sstevel@tonic-gate if (pflg) { 1657fa9e4066Sahrens acl_free(s1acl_save); 1658fa9e4066Sahrens s1acl_save = NULL; 16597c478bd9Sstevel@tonic-gate ret++; 16607c478bd9Sstevel@tonic-gate } 16617c478bd9Sstevel@tonic-gate } 16627c478bd9Sstevel@tonic-gate /* else: silent and continue */ 16637c478bd9Sstevel@tonic-gate } 1664fa9e4066Sahrens acl_free(s1acl_save); 1665fa9e4066Sahrens s1acl_save = NULL; 16667c478bd9Sstevel@tonic-gate } 16677c478bd9Sstevel@tonic-gate } else if (fixmode != (mode_t)0) 16687c478bd9Sstevel@tonic-gate (void) chmod(target, fixmode & MODEBITS); 16697c478bd9Sstevel@tonic-gate 16707c478bd9Sstevel@tonic-gate if (pflg || atflg || mve) { 16717c478bd9Sstevel@tonic-gate attret = copyattributes(source, target); 16727c478bd9Sstevel@tonic-gate if (!attrsilent && attret != 0) { 16737c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s: Failed to preserve" 16747c478bd9Sstevel@tonic-gate " extended attributes of directory" 16757c478bd9Sstevel@tonic-gate " %s\n"), cmd, source); 16767c478bd9Sstevel@tonic-gate } else { 16777c478bd9Sstevel@tonic-gate /* 16787c478bd9Sstevel@tonic-gate * Otherwise ignore failure. 16797c478bd9Sstevel@tonic-gate */ 16807c478bd9Sstevel@tonic-gate attret = 0; 16817c478bd9Sstevel@tonic-gate } 16827c478bd9Sstevel@tonic-gate } 16837c478bd9Sstevel@tonic-gate if (attret != 0) 16847c478bd9Sstevel@tonic-gate return (attret); 16857c478bd9Sstevel@tonic-gate return (ret); 16867c478bd9Sstevel@tonic-gate } 16877c478bd9Sstevel@tonic-gate 16887c478bd9Sstevel@tonic-gate static int 16897c478bd9Sstevel@tonic-gate copyspecial(char *target) 16907c478bd9Sstevel@tonic-gate { 16917c478bd9Sstevel@tonic-gate int ret = 0; 16927c478bd9Sstevel@tonic-gate 16937c478bd9Sstevel@tonic-gate if (mknod(target, s1.st_mode, s1.st_rdev) != 0) { 16947c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext( 16957c478bd9Sstevel@tonic-gate "cp: cannot create special file %s: "), target); 16967c478bd9Sstevel@tonic-gate perror(""); 16977c478bd9Sstevel@tonic-gate return (1); 16987c478bd9Sstevel@tonic-gate } 16997c478bd9Sstevel@tonic-gate 17007c478bd9Sstevel@tonic-gate if (pflg) { 17017c478bd9Sstevel@tonic-gate if ((ret = chg_mode(target, UID(s1), GID(s1), FMODE(s1))) == 0) 17027c478bd9Sstevel@tonic-gate ret = chg_time(target, s1); 17037c478bd9Sstevel@tonic-gate } 17047c478bd9Sstevel@tonic-gate 17057c478bd9Sstevel@tonic-gate return (ret); 17067c478bd9Sstevel@tonic-gate } 17077c478bd9Sstevel@tonic-gate 17087c478bd9Sstevel@tonic-gate static int 17097c478bd9Sstevel@tonic-gate use_stdin(void) 17107c478bd9Sstevel@tonic-gate { 17117c478bd9Sstevel@tonic-gate #ifdef XPG4 17127c478bd9Sstevel@tonic-gate return (1); 17137c478bd9Sstevel@tonic-gate #else 17147c478bd9Sstevel@tonic-gate return (isatty(fileno(stdin))); 17157c478bd9Sstevel@tonic-gate #endif 17167c478bd9Sstevel@tonic-gate } 17177c478bd9Sstevel@tonic-gate 17187c478bd9Sstevel@tonic-gate static int 17197c478bd9Sstevel@tonic-gate copyattributes(char *source, char *target) 17207c478bd9Sstevel@tonic-gate { 17217c478bd9Sstevel@tonic-gate int sourcedirfd, targetdirfd; 17227c478bd9Sstevel@tonic-gate int srcfd, targfd; 17237c478bd9Sstevel@tonic-gate int tmpfd; 17247c478bd9Sstevel@tonic-gate DIR *srcdirp; 17257c478bd9Sstevel@tonic-gate int srcattrfd, targattrfd; 17267c478bd9Sstevel@tonic-gate struct dirent *dp; 17277c478bd9Sstevel@tonic-gate char *attrstr; 17287c478bd9Sstevel@tonic-gate char *srcbuf, *targbuf; 17297c478bd9Sstevel@tonic-gate size_t src_size, targ_size; 17307c478bd9Sstevel@tonic-gate int error = 0; 1731fa9e4066Sahrens int aclerror; 17327c478bd9Sstevel@tonic-gate mode_t mode; 17337c478bd9Sstevel@tonic-gate int clearflg = 0; 1734fa9e4066Sahrens acl_t *xacl = NULL; 1735fa9e4066Sahrens acl_t *attrdiracl = NULL; 17367c478bd9Sstevel@tonic-gate struct stat attrdir, s3, s4; 17377c478bd9Sstevel@tonic-gate struct timeval times[2]; 17387c478bd9Sstevel@tonic-gate mode_t targmode; 17397c478bd9Sstevel@tonic-gate 17407c478bd9Sstevel@tonic-gate srcdirp = NULL; 17417c478bd9Sstevel@tonic-gate srcfd = targfd = tmpfd = -1; 17427c478bd9Sstevel@tonic-gate sourcedirfd = targetdirfd = srcattrfd = targattrfd = -1; 17437c478bd9Sstevel@tonic-gate srcbuf = targbuf = NULL; 17447c478bd9Sstevel@tonic-gate 17457c478bd9Sstevel@tonic-gate if (pathconf(source, _PC_XATTR_EXISTS) != 1) 17467c478bd9Sstevel@tonic-gate return (0); 17477c478bd9Sstevel@tonic-gate 17487c478bd9Sstevel@tonic-gate if (pathconf(target, _PC_XATTR_ENABLED) != 1) { 17497c478bd9Sstevel@tonic-gate if (!attrsilent) { 17507c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 17517c478bd9Sstevel@tonic-gate gettext( 17527c478bd9Sstevel@tonic-gate "%s: cannot preserve extended attributes, " 17537c478bd9Sstevel@tonic-gate "operation not supported on file" 17547c478bd9Sstevel@tonic-gate " %s\n"), cmd, target); 17557c478bd9Sstevel@tonic-gate } 17567c478bd9Sstevel@tonic-gate return (1); 17577c478bd9Sstevel@tonic-gate } 17587c478bd9Sstevel@tonic-gate 17597c478bd9Sstevel@tonic-gate 17607c478bd9Sstevel@tonic-gate if ((srcfd = open(source, O_RDONLY)) == -1) { 17617c478bd9Sstevel@tonic-gate if (pflg && attrsilent) { 17627c478bd9Sstevel@tonic-gate error = 0; 17637c478bd9Sstevel@tonic-gate goto out; 17647c478bd9Sstevel@tonic-gate } 17657c478bd9Sstevel@tonic-gate if (!attrsilent) { 17667c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 17677c478bd9Sstevel@tonic-gate gettext("%s: cannot open file" 17687c478bd9Sstevel@tonic-gate " %s: "), cmd, source); 17697c478bd9Sstevel@tonic-gate perror(""); 17707c478bd9Sstevel@tonic-gate } 17717c478bd9Sstevel@tonic-gate ++error; 17727c478bd9Sstevel@tonic-gate goto out; 17737c478bd9Sstevel@tonic-gate } 17747c478bd9Sstevel@tonic-gate if ((targfd = open(target, O_RDONLY)) == -1) { 17757c478bd9Sstevel@tonic-gate 17767c478bd9Sstevel@tonic-gate if (pflg && attrsilent) { 17777c478bd9Sstevel@tonic-gate error = 0; 17787c478bd9Sstevel@tonic-gate goto out; 17797c478bd9Sstevel@tonic-gate } 17807c478bd9Sstevel@tonic-gate if (!attrsilent) { 17817c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 17827c478bd9Sstevel@tonic-gate gettext("%s: cannot open file" 17837c478bd9Sstevel@tonic-gate " %s: "), cmd, source); 17847c478bd9Sstevel@tonic-gate perror(""); 17857c478bd9Sstevel@tonic-gate } 17867c478bd9Sstevel@tonic-gate ++error; 17877c478bd9Sstevel@tonic-gate goto out; 17887c478bd9Sstevel@tonic-gate } 17897c478bd9Sstevel@tonic-gate 17907c478bd9Sstevel@tonic-gate if ((sourcedirfd = openat(srcfd, ".", O_RDONLY|O_XATTR)) == -1) { 17917c478bd9Sstevel@tonic-gate if (pflg && attrsilent) { 17927c478bd9Sstevel@tonic-gate error = 0; 17937c478bd9Sstevel@tonic-gate goto out; 17947c478bd9Sstevel@tonic-gate } 17957c478bd9Sstevel@tonic-gate if (!attrsilent) { 17967c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 17977c478bd9Sstevel@tonic-gate gettext("%s: cannot open attribute" 17987c478bd9Sstevel@tonic-gate " directory for %s: "), cmd, source); 17997c478bd9Sstevel@tonic-gate perror(""); 18007c478bd9Sstevel@tonic-gate ++error; 18017c478bd9Sstevel@tonic-gate } 18027c478bd9Sstevel@tonic-gate goto out; 18037c478bd9Sstevel@tonic-gate } 18047c478bd9Sstevel@tonic-gate 18057c478bd9Sstevel@tonic-gate if (fstat(sourcedirfd, &attrdir) == -1) { 18067c478bd9Sstevel@tonic-gate if (pflg && attrsilent) { 18077c478bd9Sstevel@tonic-gate error = 0; 18087c478bd9Sstevel@tonic-gate goto out; 18097c478bd9Sstevel@tonic-gate } 18107c478bd9Sstevel@tonic-gate 18117c478bd9Sstevel@tonic-gate if (!attrsilent) { 18127c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 18137c478bd9Sstevel@tonic-gate gettext("%s: could not retrieve stat" 18147c478bd9Sstevel@tonic-gate " information for attribute directory" 18157c478bd9Sstevel@tonic-gate "of file %s: "), cmd, source); 18167c478bd9Sstevel@tonic-gate perror(""); 18177c478bd9Sstevel@tonic-gate ++error; 18187c478bd9Sstevel@tonic-gate } 18197c478bd9Sstevel@tonic-gate goto out; 18207c478bd9Sstevel@tonic-gate } 18217c478bd9Sstevel@tonic-gate if ((targetdirfd = openat(targfd, ".", O_RDONLY|O_XATTR)) == -1) { 18227c478bd9Sstevel@tonic-gate /* 18237c478bd9Sstevel@tonic-gate * We couldn't create the attribute directory 18247c478bd9Sstevel@tonic-gate * 18257c478bd9Sstevel@tonic-gate * Lets see if we can add write support to the mode 18267c478bd9Sstevel@tonic-gate * and create the directory and then put the mode back 18277c478bd9Sstevel@tonic-gate * to way it should be. 18287c478bd9Sstevel@tonic-gate */ 18297c478bd9Sstevel@tonic-gate 18307c478bd9Sstevel@tonic-gate targmode = FMODE(s1) | S_IWUSR; 18317c478bd9Sstevel@tonic-gate if (fchmod(targfd, targmode) == 0) { 18327c478bd9Sstevel@tonic-gate targetdirfd = openat(targfd, ".", O_RDONLY|O_XATTR); 18337c478bd9Sstevel@tonic-gate /* 18347c478bd9Sstevel@tonic-gate * Put mode back to what it was 18357c478bd9Sstevel@tonic-gate */ 18367c478bd9Sstevel@tonic-gate targmode = FMODE(s1) & MODEBITS; 18377c478bd9Sstevel@tonic-gate if (fchmod(targfd, targmode) == -1) { 18387c478bd9Sstevel@tonic-gate if (pflg && attrsilent) { 18397c478bd9Sstevel@tonic-gate error = 0; 18407c478bd9Sstevel@tonic-gate goto out; 18417c478bd9Sstevel@tonic-gate } 18427c478bd9Sstevel@tonic-gate if (!attrsilent) { 18437c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 18447c478bd9Sstevel@tonic-gate gettext("%s: failed to set" 18457c478bd9Sstevel@tonic-gate " mode correctly on file" 18467c478bd9Sstevel@tonic-gate " %s: "), cmd, target); 18477c478bd9Sstevel@tonic-gate perror(""); 18487c478bd9Sstevel@tonic-gate ++error; 18497c478bd9Sstevel@tonic-gate goto out; 18507c478bd9Sstevel@tonic-gate } 18517c478bd9Sstevel@tonic-gate } 18527c478bd9Sstevel@tonic-gate } else { 18537c478bd9Sstevel@tonic-gate if (pflg && attrsilent) { 18547c478bd9Sstevel@tonic-gate error = 0; 18557c478bd9Sstevel@tonic-gate goto out; 18567c478bd9Sstevel@tonic-gate } 18577c478bd9Sstevel@tonic-gate if (!attrsilent) { 18587c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 18597c478bd9Sstevel@tonic-gate gettext("%s: cannot open attribute" 18607c478bd9Sstevel@tonic-gate " directory for %s: "), cmd, target); 18617c478bd9Sstevel@tonic-gate perror(""); 18627c478bd9Sstevel@tonic-gate ++error; 18637c478bd9Sstevel@tonic-gate } 18647c478bd9Sstevel@tonic-gate goto out; 18657c478bd9Sstevel@tonic-gate } 18667c478bd9Sstevel@tonic-gate } 18677c478bd9Sstevel@tonic-gate 18687c478bd9Sstevel@tonic-gate if (targetdirfd == -1) { 18697c478bd9Sstevel@tonic-gate if (pflg && attrsilent) { 18707c478bd9Sstevel@tonic-gate error = 0; 18717c478bd9Sstevel@tonic-gate goto out; 18727c478bd9Sstevel@tonic-gate } 18737c478bd9Sstevel@tonic-gate if (!attrsilent) { 18747c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 18757c478bd9Sstevel@tonic-gate gettext("%s: cannot open attribute directory" 18767c478bd9Sstevel@tonic-gate " for %s: "), cmd, target); 18777c478bd9Sstevel@tonic-gate perror(""); 18787c478bd9Sstevel@tonic-gate ++error; 18797c478bd9Sstevel@tonic-gate } 18807c478bd9Sstevel@tonic-gate goto out; 18817c478bd9Sstevel@tonic-gate } 18827c478bd9Sstevel@tonic-gate 18837c478bd9Sstevel@tonic-gate /* 18847c478bd9Sstevel@tonic-gate * Set mode of attribute directory same as on source, 18857c478bd9Sstevel@tonic-gate * if pflg set or this is a move. 18867c478bd9Sstevel@tonic-gate */ 18877c478bd9Sstevel@tonic-gate 18887c478bd9Sstevel@tonic-gate if (pflg || mve) { 18897c478bd9Sstevel@tonic-gate if (fchmod(targetdirfd, attrdir.st_mode) == -1) { 18907c478bd9Sstevel@tonic-gate if (!attrsilent) { 18917c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 18927c478bd9Sstevel@tonic-gate gettext("%s: failed to set file mode" 18937c478bd9Sstevel@tonic-gate " correctly on attribute directory of" 18947c478bd9Sstevel@tonic-gate " file %s: "), cmd, target); 18957c478bd9Sstevel@tonic-gate perror(""); 18967c478bd9Sstevel@tonic-gate ++error; 18977c478bd9Sstevel@tonic-gate } 18987c478bd9Sstevel@tonic-gate } 18997c478bd9Sstevel@tonic-gate 19007c478bd9Sstevel@tonic-gate if (fchown(targetdirfd, attrdir.st_uid, attrdir.st_gid) == -1) { 19017c478bd9Sstevel@tonic-gate if (!attrsilent) { 19027c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 19037c478bd9Sstevel@tonic-gate gettext("%s: failed to set file" 19047c478bd9Sstevel@tonic-gate " ownership correctly on attribute" 19057c478bd9Sstevel@tonic-gate " directory of file %s: "), cmd, target); 19067c478bd9Sstevel@tonic-gate perror(""); 19077c478bd9Sstevel@tonic-gate ++error; 19087c478bd9Sstevel@tonic-gate } 19097c478bd9Sstevel@tonic-gate } 19107c478bd9Sstevel@tonic-gate /* 19117c478bd9Sstevel@tonic-gate * Now that we are the owner we can update st_ctime by calling 19127c478bd9Sstevel@tonic-gate * futimesat. 19137c478bd9Sstevel@tonic-gate */ 19147c478bd9Sstevel@tonic-gate times[0].tv_sec = attrdir.st_atime; 19157c478bd9Sstevel@tonic-gate times[0].tv_usec = 0; 19167c478bd9Sstevel@tonic-gate times[1].tv_sec = attrdir.st_mtime; 19177c478bd9Sstevel@tonic-gate times[1].tv_usec = 0; 19187c478bd9Sstevel@tonic-gate if (futimesat(targetdirfd, ".", times) < 0) { 19197c478bd9Sstevel@tonic-gate if (!attrsilent) { 19207c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 19217c478bd9Sstevel@tonic-gate gettext("%s: cannot set attribute times" 19227c478bd9Sstevel@tonic-gate " for %s: "), cmd, target); 19237c478bd9Sstevel@tonic-gate perror(""); 19247c478bd9Sstevel@tonic-gate ++error; 19257c478bd9Sstevel@tonic-gate } 19267c478bd9Sstevel@tonic-gate } 19277c478bd9Sstevel@tonic-gate 19287c478bd9Sstevel@tonic-gate /* 19297c478bd9Sstevel@tonic-gate * Now set owner and group of attribute directory, implies 19307c478bd9Sstevel@tonic-gate * changing the ACL of the hidden attribute directory first. 19317c478bd9Sstevel@tonic-gate */ 1932fa9e4066Sahrens if ((aclerror = facl_get(sourcedirfd, 1933fa9e4066Sahrens ACL_NO_TRIVIAL, &attrdiracl)) != 0) { 19347c478bd9Sstevel@tonic-gate if (!attrsilent) { 19357c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext( 19367c478bd9Sstevel@tonic-gate "%s: failed to get acl entries of" 19377c478bd9Sstevel@tonic-gate " attribute directory for" 1938fa9e4066Sahrens " %s : %s\n"), cmd, 1939fa9e4066Sahrens source, acl_strerror(aclerror)); 19407c478bd9Sstevel@tonic-gate ++error; 19417c478bd9Sstevel@tonic-gate } 19427c478bd9Sstevel@tonic-gate } 19437c478bd9Sstevel@tonic-gate 1944fa9e4066Sahrens if (attrdiracl) { 1945fa9e4066Sahrens if (facl_set(targetdirfd, attrdiracl) != 0) { 19467c478bd9Sstevel@tonic-gate if (!attrsilent) { 19477c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext( 19487c478bd9Sstevel@tonic-gate "%s: failed to set acl entries" 19497c478bd9Sstevel@tonic-gate " on attribute directory " 19507c478bd9Sstevel@tonic-gate "for %s\n"), cmd, target); 19517c478bd9Sstevel@tonic-gate ++error; 19527c478bd9Sstevel@tonic-gate } 1953fa9e4066Sahrens acl_free(attrdiracl); 1954fa9e4066Sahrens attrdiracl = NULL; 19557c478bd9Sstevel@tonic-gate } 19567c478bd9Sstevel@tonic-gate } 19577c478bd9Sstevel@tonic-gate } 19587c478bd9Sstevel@tonic-gate 19597c478bd9Sstevel@tonic-gate /* 19607c478bd9Sstevel@tonic-gate * dup sourcedirfd for use by fdopendir(). 19617c478bd9Sstevel@tonic-gate * fdopendir will take ownership of given fd and will close 19627c478bd9Sstevel@tonic-gate * it when closedir() is called. 19637c478bd9Sstevel@tonic-gate */ 19647c478bd9Sstevel@tonic-gate 19657c478bd9Sstevel@tonic-gate if ((tmpfd = dup(sourcedirfd)) == -1) { 19667c478bd9Sstevel@tonic-gate if (pflg && attrsilent) { 19677c478bd9Sstevel@tonic-gate error = 0; 19687c478bd9Sstevel@tonic-gate goto out; 19697c478bd9Sstevel@tonic-gate } 19707c478bd9Sstevel@tonic-gate if (!attrsilent) { 19717c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 19727c478bd9Sstevel@tonic-gate gettext( 19737c478bd9Sstevel@tonic-gate "%s: unable to dup attribute directory" 19747c478bd9Sstevel@tonic-gate " file descriptor for %s: "), cmd, source); 19757c478bd9Sstevel@tonic-gate perror(""); 19767c478bd9Sstevel@tonic-gate ++error; 19777c478bd9Sstevel@tonic-gate } 19787c478bd9Sstevel@tonic-gate goto out; 19797c478bd9Sstevel@tonic-gate } 19807c478bd9Sstevel@tonic-gate if ((srcdirp = fdopendir(tmpfd)) == NULL) { 19817c478bd9Sstevel@tonic-gate if (pflg && attrsilent) { 19827c478bd9Sstevel@tonic-gate error = 0; 19837c478bd9Sstevel@tonic-gate goto out; 19847c478bd9Sstevel@tonic-gate } 19857c478bd9Sstevel@tonic-gate if (!attrsilent) { 19867c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 19877c478bd9Sstevel@tonic-gate gettext("%s: failed to open attribute" 19887c478bd9Sstevel@tonic-gate " directory for %s: "), cmd, source); 19897c478bd9Sstevel@tonic-gate perror(""); 19907c478bd9Sstevel@tonic-gate ++error; 19917c478bd9Sstevel@tonic-gate } 19927c478bd9Sstevel@tonic-gate goto out; 19937c478bd9Sstevel@tonic-gate } 19947c478bd9Sstevel@tonic-gate 19957c478bd9Sstevel@tonic-gate while (dp = readdir(srcdirp)) { 19967c478bd9Sstevel@tonic-gate if ((dp->d_name[0] == '.' && dp->d_name[1] == '\0') || 19977c478bd9Sstevel@tonic-gate (dp->d_name[0] == '.' && dp->d_name[1] == '.' && 19987c478bd9Sstevel@tonic-gate dp->d_name[2] == '\0')) 19997c478bd9Sstevel@tonic-gate continue; 20007c478bd9Sstevel@tonic-gate 20017c478bd9Sstevel@tonic-gate if ((srcattrfd = openat(sourcedirfd, dp->d_name, 20027c478bd9Sstevel@tonic-gate O_RDONLY)) == -1) { 20037c478bd9Sstevel@tonic-gate if (!attrsilent) { 20047c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 20057c478bd9Sstevel@tonic-gate gettext("%s: cannot open attribute %s on" 20067c478bd9Sstevel@tonic-gate " file %s: "), cmd, dp->d_name, source); 20077c478bd9Sstevel@tonic-gate perror(""); 20087c478bd9Sstevel@tonic-gate ++error; 20097c478bd9Sstevel@tonic-gate goto next; 20107c478bd9Sstevel@tonic-gate } 20117c478bd9Sstevel@tonic-gate } 20127c478bd9Sstevel@tonic-gate 20137c478bd9Sstevel@tonic-gate if (fstat(srcattrfd, &s3) < 0) { 20147c478bd9Sstevel@tonic-gate if (!attrsilent) { 20157c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 20167c478bd9Sstevel@tonic-gate gettext("%s: could not stat attribute" 20177c478bd9Sstevel@tonic-gate " %s on file" 20187c478bd9Sstevel@tonic-gate " %s: "), cmd, dp->d_name, source); 20197c478bd9Sstevel@tonic-gate perror(""); 20207c478bd9Sstevel@tonic-gate ++error; 20217c478bd9Sstevel@tonic-gate } 20227c478bd9Sstevel@tonic-gate goto next; 20237c478bd9Sstevel@tonic-gate } 20247c478bd9Sstevel@tonic-gate 20257c478bd9Sstevel@tonic-gate if (pflg || mve) { 2026fa9e4066Sahrens if ((aclerror = facl_get(srcattrfd, 2027fa9e4066Sahrens ACL_NO_TRIVIAL, &xacl)) != 0) { 20287c478bd9Sstevel@tonic-gate if (!attrsilent) { 20297c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext( 20307c478bd9Sstevel@tonic-gate "%s: failed to get acl entries of" 20317c478bd9Sstevel@tonic-gate " attribute %s for" 2032fa9e4066Sahrens " %s: %s"), cmd, dp->d_name, 2033fa9e4066Sahrens source, acl_strerror(aclerror)); 20347c478bd9Sstevel@tonic-gate ++error; 20357c478bd9Sstevel@tonic-gate } 20367c478bd9Sstevel@tonic-gate } 20377c478bd9Sstevel@tonic-gate } 20387c478bd9Sstevel@tonic-gate 20397c478bd9Sstevel@tonic-gate (void) unlinkat(targetdirfd, dp->d_name, 0); 20407c478bd9Sstevel@tonic-gate if ((targattrfd = openat(targetdirfd, dp->d_name, 20417c478bd9Sstevel@tonic-gate O_RDWR|O_CREAT|O_TRUNC, s3.st_mode & MODEBITS)) == -1) { 20427c478bd9Sstevel@tonic-gate if (!attrsilent) { 20437c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 20447c478bd9Sstevel@tonic-gate gettext("%s: could not create attribute" 20457c478bd9Sstevel@tonic-gate " %s on file" 20467c478bd9Sstevel@tonic-gate " %s: "), cmd, dp->d_name, target); 20477c478bd9Sstevel@tonic-gate perror(""); 20487c478bd9Sstevel@tonic-gate ++error; 20497c478bd9Sstevel@tonic-gate } 20507c478bd9Sstevel@tonic-gate goto next; 20517c478bd9Sstevel@tonic-gate } 20527c478bd9Sstevel@tonic-gate 20537c478bd9Sstevel@tonic-gate /* 20547c478bd9Sstevel@tonic-gate * preserve ACL 20557c478bd9Sstevel@tonic-gate */ 2056fa9e4066Sahrens if ((pflg || mve) && xacl != NULL) { 2057fa9e4066Sahrens if ((facl_set(targattrfd, xacl)) < 0) { 20587c478bd9Sstevel@tonic-gate if (!attrsilent) { 20597c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext( 20607c478bd9Sstevel@tonic-gate "%s: failed to set acl entries on" 20617c478bd9Sstevel@tonic-gate " attribute %s for" 20627c478bd9Sstevel@tonic-gate "%s\n"), cmd, dp->d_name, target); 20637c478bd9Sstevel@tonic-gate ++error; 20647c478bd9Sstevel@tonic-gate } 2065fa9e4066Sahrens acl_free(xacl); 2066fa9e4066Sahrens xacl = NULL; 20677c478bd9Sstevel@tonic-gate } 20687c478bd9Sstevel@tonic-gate } 20697c478bd9Sstevel@tonic-gate 20707c478bd9Sstevel@tonic-gate if (fstat(targattrfd, &s4) < 0) { 20717c478bd9Sstevel@tonic-gate if (!attrsilent) { 20727c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 20737c478bd9Sstevel@tonic-gate gettext("%s: could not stat attribute" 20747c478bd9Sstevel@tonic-gate " %s on file" 20757c478bd9Sstevel@tonic-gate " %s: "), cmd, dp->d_name, source); 20767c478bd9Sstevel@tonic-gate perror(""); 20777c478bd9Sstevel@tonic-gate ++error; 20787c478bd9Sstevel@tonic-gate } 20797c478bd9Sstevel@tonic-gate goto next; 20807c478bd9Sstevel@tonic-gate } 20817c478bd9Sstevel@tonic-gate 20827c478bd9Sstevel@tonic-gate /* 20837c478bd9Sstevel@tonic-gate * setup path string to be passed to writefile 20847c478bd9Sstevel@tonic-gate * 20857c478bd9Sstevel@tonic-gate * We need to include attribute in the string so that 20867c478bd9Sstevel@tonic-gate * a useful error message can be printed in the case of a failure. 20877c478bd9Sstevel@tonic-gate */ 20887c478bd9Sstevel@tonic-gate attrstr = gettext(" attribute "); 20897c478bd9Sstevel@tonic-gate src_size = strlen(source) + 20907c478bd9Sstevel@tonic-gate strlen(dp->d_name) + strlen(attrstr) + 1; 20917c478bd9Sstevel@tonic-gate srcbuf = malloc(src_size); 20927c478bd9Sstevel@tonic-gate 20937c478bd9Sstevel@tonic-gate if (srcbuf == NULL) { 20947c478bd9Sstevel@tonic-gate if (!attrsilent) { 20957c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 20967c478bd9Sstevel@tonic-gate gettext("%s: could not allocate memory" 20977c478bd9Sstevel@tonic-gate " for path buffer: "), cmd); 20987c478bd9Sstevel@tonic-gate perror(""); 20997c478bd9Sstevel@tonic-gate ++error; 21007c478bd9Sstevel@tonic-gate } 21017c478bd9Sstevel@tonic-gate goto next; 21027c478bd9Sstevel@tonic-gate } 21037c478bd9Sstevel@tonic-gate targ_size = strlen(target) + 21047c478bd9Sstevel@tonic-gate strlen(dp->d_name) + strlen(attrstr) + 1; 21057c478bd9Sstevel@tonic-gate targbuf = malloc(targ_size); 21067c478bd9Sstevel@tonic-gate if (targbuf == NULL) { 21077c478bd9Sstevel@tonic-gate if (!attrsilent) { 21087c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 21097c478bd9Sstevel@tonic-gate gettext("%s: could not allocate memory" 21107c478bd9Sstevel@tonic-gate " for path buffer: "), cmd); 21117c478bd9Sstevel@tonic-gate perror(""); 21127c478bd9Sstevel@tonic-gate ++error; 21137c478bd9Sstevel@tonic-gate } 21147c478bd9Sstevel@tonic-gate goto next; 21157c478bd9Sstevel@tonic-gate } 21167c478bd9Sstevel@tonic-gate 21177c478bd9Sstevel@tonic-gate (void) snprintf(srcbuf, src_size, "%s%s%s", 21187c478bd9Sstevel@tonic-gate source, attrstr, dp->d_name); 21197c478bd9Sstevel@tonic-gate (void) snprintf(targbuf, targ_size, "%s%s%s", 21207c478bd9Sstevel@tonic-gate target, attrstr, dp->d_name); 21217c478bd9Sstevel@tonic-gate 21227c478bd9Sstevel@tonic-gate if (writefile(srcattrfd, targattrfd, 21237c478bd9Sstevel@tonic-gate srcbuf, targbuf, &s3, &s4) != 0) { 21247c478bd9Sstevel@tonic-gate if (!attrsilent) { 21257c478bd9Sstevel@tonic-gate ++error; 21267c478bd9Sstevel@tonic-gate } 21277c478bd9Sstevel@tonic-gate goto next; 21287c478bd9Sstevel@tonic-gate } 21297c478bd9Sstevel@tonic-gate 21307c478bd9Sstevel@tonic-gate if (pflg || mve) { 21317c478bd9Sstevel@tonic-gate mode = FMODE(s3); 21327c478bd9Sstevel@tonic-gate 21337c478bd9Sstevel@tonic-gate if (fchown(targattrfd, UID(s3), GID(s3)) != 0) { 21347c478bd9Sstevel@tonic-gate if (!attrsilent) { 21357c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 21367c478bd9Sstevel@tonic-gate gettext("%s: cannot change" 21377c478bd9Sstevel@tonic-gate " owner and group of" 21387c478bd9Sstevel@tonic-gate " attribute %s for" " file" 21397c478bd9Sstevel@tonic-gate " %s: "), cmd, dp->d_name, target); 21407c478bd9Sstevel@tonic-gate perror(""); 21417c478bd9Sstevel@tonic-gate ++error; 21427c478bd9Sstevel@tonic-gate } 21437c478bd9Sstevel@tonic-gate if (mode & (S_ISUID | S_ISGID)) { 21447c478bd9Sstevel@tonic-gate /* try to clear S_ISUID and S_ISGID */ 21457c478bd9Sstevel@tonic-gate mode &= ~S_ISUID & ~S_ISGID; 21467c478bd9Sstevel@tonic-gate ++clearflg; 21477c478bd9Sstevel@tonic-gate } 21487c478bd9Sstevel@tonic-gate } 21497c478bd9Sstevel@tonic-gate /* tv_usec were cleared above */ 21507c478bd9Sstevel@tonic-gate times[0].tv_sec = s3.st_atime; 21517c478bd9Sstevel@tonic-gate times[1].tv_sec = s3.st_mtime; 21527c478bd9Sstevel@tonic-gate if (futimesat(targetdirfd, dp->d_name, times) < 0) { 21537c478bd9Sstevel@tonic-gate if (!attrsilent) { 21547c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 21557c478bd9Sstevel@tonic-gate gettext("%s: cannot set attribute" 21567c478bd9Sstevel@tonic-gate " times for %s: "), cmd, target); 21577c478bd9Sstevel@tonic-gate perror(""); 21587c478bd9Sstevel@tonic-gate ++error; 21597c478bd9Sstevel@tonic-gate } 21607c478bd9Sstevel@tonic-gate } 21617c478bd9Sstevel@tonic-gate if (fchmod(targattrfd, mode) != 0) { 21627c478bd9Sstevel@tonic-gate if (clearflg) { 21637c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext( 21647c478bd9Sstevel@tonic-gate "%s: cannot clear S_ISUID and " 21657c478bd9Sstevel@tonic-gate "S_ISGID bits in attribute %s" 21667c478bd9Sstevel@tonic-gate " for file" 21677c478bd9Sstevel@tonic-gate " %s: "), cmd, dp->d_name, target); 21687c478bd9Sstevel@tonic-gate } else { 21697c478bd9Sstevel@tonic-gate if (!attrsilent) { 21707c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 21717c478bd9Sstevel@tonic-gate gettext( 21727c478bd9Sstevel@tonic-gate "%s: cannot set permissions of attribute" 21737c478bd9Sstevel@tonic-gate " %s for %s: "), cmd, dp->d_name, target); 21747c478bd9Sstevel@tonic-gate perror(""); 21757c478bd9Sstevel@tonic-gate ++error; 21767c478bd9Sstevel@tonic-gate } 21777c478bd9Sstevel@tonic-gate } 21787c478bd9Sstevel@tonic-gate } 2179*d2443e76Smarks if (xacl && ((facl_set(targattrfd, xacl)) < 0)) { 2180*d2443e76Smarks if (!attrsilent) { 2181*d2443e76Smarks (void) fprintf(stderr, gettext( 2182*d2443e76Smarks "%s: failed to set acl entries on" 2183*d2443e76Smarks " attribute %s for" 2184*d2443e76Smarks "%s\n"), cmd, dp->d_name, target); 2185*d2443e76Smarks ++error; 2186*d2443e76Smarks } 2187*d2443e76Smarks acl_free(xacl); 2188*d2443e76Smarks xacl = NULL; 2189*d2443e76Smarks } 21907c478bd9Sstevel@tonic-gate } 21917c478bd9Sstevel@tonic-gate next: 2192fa9e4066Sahrens if (xacl != NULL) { 2193fa9e4066Sahrens acl_free(xacl); 2194fa9e4066Sahrens xacl = NULL; 21957c478bd9Sstevel@tonic-gate } 21967c478bd9Sstevel@tonic-gate if (srcbuf != NULL) 21977c478bd9Sstevel@tonic-gate free(srcbuf); 21987c478bd9Sstevel@tonic-gate if (targbuf != NULL) 21997c478bd9Sstevel@tonic-gate free(targbuf); 22007c478bd9Sstevel@tonic-gate if (srcattrfd != -1) 22017c478bd9Sstevel@tonic-gate (void) close(srcattrfd); 22027c478bd9Sstevel@tonic-gate if (targattrfd != -1) 22037c478bd9Sstevel@tonic-gate (void) close(targattrfd); 22047c478bd9Sstevel@tonic-gate srcattrfd = targattrfd = -1; 22057c478bd9Sstevel@tonic-gate srcbuf = targbuf = NULL; 22067c478bd9Sstevel@tonic-gate } 22077c478bd9Sstevel@tonic-gate out: 2208fa9e4066Sahrens if (xacl != NULL) { 2209fa9e4066Sahrens acl_free(xacl); 2210fa9e4066Sahrens xacl = NULL; 2211fa9e4066Sahrens } 2212fa9e4066Sahrens if (attrdiracl != NULL) { 2213fa9e4066Sahrens acl_free(attrdiracl); 2214fa9e4066Sahrens attrdiracl = NULL; 2215fa9e4066Sahrens } 22167c478bd9Sstevel@tonic-gate if (srcbuf) 22177c478bd9Sstevel@tonic-gate free(srcbuf); 22187c478bd9Sstevel@tonic-gate if (targbuf) 22197c478bd9Sstevel@tonic-gate free(targbuf); 22207c478bd9Sstevel@tonic-gate if (sourcedirfd != -1) 22217c478bd9Sstevel@tonic-gate (void) close(sourcedirfd); 22227c478bd9Sstevel@tonic-gate if (targetdirfd != -1) 22237c478bd9Sstevel@tonic-gate (void) close(targetdirfd); 22247c478bd9Sstevel@tonic-gate if (srcdirp != NULL) 22257c478bd9Sstevel@tonic-gate (void) closedir(srcdirp); 22267c478bd9Sstevel@tonic-gate if (srcfd != -1) 22277c478bd9Sstevel@tonic-gate (void) close(srcfd); 22287c478bd9Sstevel@tonic-gate if (targfd != -1) 22297c478bd9Sstevel@tonic-gate (void) close(targfd); 22307c478bd9Sstevel@tonic-gate return (error == 0 ? 0 : 1); 22317c478bd9Sstevel@tonic-gate } 22327c478bd9Sstevel@tonic-gate 22337c478bd9Sstevel@tonic-gate /* 22347c478bd9Sstevel@tonic-gate * nanoseconds are rounded off to microseconds by flooring. 22357c478bd9Sstevel@tonic-gate */ 22367c478bd9Sstevel@tonic-gate static void 22377c478bd9Sstevel@tonic-gate timestruc_to_timeval(timestruc_t *ts, struct timeval *tv) 22387c478bd9Sstevel@tonic-gate { 22397c478bd9Sstevel@tonic-gate tv->tv_sec = ts->tv_sec; 22407c478bd9Sstevel@tonic-gate tv->tv_usec = ts->tv_nsec / 1000; 22417c478bd9Sstevel@tonic-gate } 2242