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