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