4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2013 Nexenta Systems, Inc. All rights reserved.
27 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
28 * Use is subject to license terms.
31 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
32 /* All Rights Reserved */
35 * University Copyright- Copyright (c) 1982, 1986, 1988
36 * The Regents of the University of California
39 * University Acknowledgment- Portions of this document are derived from
40 * software developed by the University of California, Berkeley, and its
45 * Combined mv/cp/ln command:
48 * mv file1 ... filen dir1
55 #include <libcmdutils.h>
57 #include "getresponse.h"
59 #define FTYPE(A) (A.st_mode)
60 #define FMODE(A) (A.st_mode)
61 #define UID(A) (A.st_uid)
62 #define GID(A) (A.st_gid)
63 #define IDENTICAL(A, B) (A.st_dev == B.st_dev && A.st_ino == B.st_ino)
64 #define ISDIR(A) ((A.st_mode & S_IFMT) == S_IFDIR)
65 #define ISDOOR(A) ((A.st_mode & S_IFMT) == S_IFDOOR)
66 #define ISLNK(A) ((A.st_mode & S_IFMT) == S_IFLNK)
67 #define ISREG(A) (((A).st_mode & S_IFMT) == S_IFREG)
68 #define ISDEV(A) ((A.st_mode & S_IFMT) == S_IFCHR || \
69 (A.st_mode & S_IFMT) == S_IFBLK || \
70 (A.st_mode & S_IFMT) == S_IFIFO)
71 #define ISSOCK(A) ((A.st_mode & S_IFMT) == S_IFSOCK)
74 #define EQ(x, y) (strcmp(x, y) == 0)
76 #define MODEBITS (S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO)
79 static char *dname(char *);
80 static int lnkfil(char *, char *);
81 static int cpymve(char *, char *);
82 static int chkfiles(char *, char **);
83 static int rcopy(char *, char *);
84 static int chk_different(char *, char *);
85 static int chg_time(char *, struct stat
);
86 static int chg_mode(char *, uid_t
, gid_t
, mode_t
);
87 static int copydir(char *, char *);
88 static int copyspecial(char *);
89 static int getrealpath(char *, char *);
90 static void usage(void);
91 static void Perror(char *);
92 static void Perror2(char *, char *);
93 static int use_stdin(void);
94 static int copyattributes(char *, char *);
95 static int copy_sysattr(char *, char *);
96 static tree_node_t
*create_tnode(dev_t
, ino_t
);
98 static struct stat s1
, s2
, s3
, s4
;
99 static int cpy
= FALSE
;
100 static int mve
= FALSE
;
101 static int lnk
= FALSE
;
103 static int silent
= 0;
107 static int Rflg
= 0; /* recursive copy */
108 static int rflg
= 0; /* recursive copy */
110 static int Hflg
= 0; /* follow cmd line arg symlink to dir */
111 static int Lflg
= 0; /* follow symlinks */
112 static int Pflg
= 0; /* do not follow symlinks */
113 static int atflg
= 0;
114 static int attrsilent
= 0;
115 static int targetexists
= 0;
116 static int cmdarg
; /* command line argument */
117 static avl_tree_t
*stree
= NULL
; /* source file inode search tree */
119 static int saflg
= 0; /* 'cp' extended system attr. */
120 static int srcfd
= -1;
121 static int targfd
= -1;
122 static int sourcedirfd
= -1;
123 static int targetdirfd
= -1;
124 static DIR *srcdirp
= NULL
;
125 static int srcattrfd
= -1;
126 static int targattrfd
= -1;
127 static struct stat attrdir
;
129 /* Extended system attributes support */
131 static int open_source(char *);
132 static int open_target_srctarg_attrdirs(char *, char *);
133 static int open_attrdirp(char *);
134 static int traverse_attrfile(struct dirent
*, char *, char *, int);
135 static void rewind_attrdir(DIR *);
136 static void close_all();
140 main(int argc
, char *argv
[])
142 int c
, i
, r
, errflg
= 0;
143 char target
[PATH_MAX
];
144 int (*move
)(char *, char *);
147 * Determine command invoked (mv, cp, or ln)
150 if (cmd
= strrchr(argv
[0], '/'))
156 * Set flags based on command.
159 (void) setlocale(LC_ALL
, "");
160 #if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */
161 #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it weren't */
163 (void) textdomain(TEXT_DOMAIN
);
164 if (init_yes() < 0) {
165 (void) fprintf(stderr
, gettext(ERR_MSG_INIT_YES
),
172 else if (EQ(cmd
, "ln"))
174 else if (EQ(cmd
, "cp"))
177 (void) fprintf(stderr
,
178 gettext("Invalid command name (%s); expecting "
179 "mv, cp, or ln.\n"), cmd
);
185 * cp [ -r|-R [-H|-L|-P]] [-afip@/] file1 [file2 ...] target
186 * cp [-afiprR@/] file1 [file2 ...] target
187 * ln [-f] [-n] [-s] file1 [file2 ...] target
188 * ln [-f] [-n] [-s] file1 [file2 ...]
189 * mv [-f|i] file1 [file2 ...] target
190 * mv [-f|i] dir1 target
194 while ((c
= getopt(argc
, argv
, "afHiLpPrR@/")) != EOF
)
215 * If more than one of -H, -L, or -P are
216 * specified, only the last option specified
217 * determines the behavior.
232 * The default behavior of cp -R|-r
233 * when specified without -H|-L|-P
266 /* -R or -r must be specified with -H, -L, or -P */
267 if ((Hflg
|| Lflg
|| Pflg
) && !(Rflg
|| rflg
)) {
272 while ((c
= getopt(argc
, argv
, "fis")) != EOF
)
290 while ((c
= getopt(argc
, argv
, "fns")) != EOF
)
296 /* silently ignored; this is the default */
307 * For BSD compatibility allow - to delimit the end of
310 if (mve
&& optind
< argc
&& (strcmp(argv
[optind
], "-") == 0))
314 * Check for sufficient arguments
319 argv
= &argv
[optind
];
321 if ((argc
< 2 && lnk
!= TRUE
) || (argc
< 1 && lnk
== TRUE
)) {
322 (void) fprintf(stderr
,
323 gettext("%s: Insufficient arguments (%d)\n"),
332 * If there is more than a source and target,
333 * the last argument (the target) must be a directory
334 * which really exists.
338 if (stat(argv
[argc
-1], &s2
) < 0) {
339 (void) fprintf(stderr
,
340 gettext("%s: %s not found\n"),
346 (void) fprintf(stderr
,
347 gettext("%s: Target %s must be a directory\n"),
353 if (strlen(argv
[argc
-1]) >= PATH_MAX
) {
354 (void) fprintf(stderr
,
355 gettext("%s: Target %s file name length exceeds PATH_MAX"
356 " %d\n"), cmd
, argv
[argc
-1], PATH_MAX
);
363 (void) strcpy(target
, ".");
365 (void) strcpy(target
, argv
[--argc
]);
369 * Perform a multiple argument mv|cp|ln by
370 * multiple invocations of cpymve() or lnkfil().
378 for (i
= 0; i
< argc
; i
++) {
381 r
+= move(argv
[i
], target
);
385 * Show errors by nonzero exit code.
392 lnkfil(char *source
, char *target
)
399 * If target is a directory make complete
400 * name of the new symbolic link within that
404 if ((stat(target
, &s2
) >= 0) && ISDIR(s2
)) {
407 len
= strlen(target
) + strlen(dname(source
)) + 4;
408 if ((buf
= (char *)malloc(len
)) == NULL
) {
409 (void) fprintf(stderr
,
410 gettext("%s: Insufficient memory "
411 "to %s %s\n"), cmd
, cmd
, source
);
414 (void) snprintf(buf
, len
, "%s/%s",
415 target
, dname(source
));
420 * Check to see if the file exists already.
421 * In this case we use lstat() instead of stat():
422 * unlink(2) and symlink(2) will operate on the file
423 * itself, not its reference, if the file is a symlink.
426 if ((lstat(target
, &s2
) == 0)) {
428 * Check if the silent flag is set ie. the -f option
429 * is used. If so, use unlink to remove the current
430 * target to replace with the new target, specified
431 * on the command line. Proceed with symlink.
435 * Don't allow silent (-f) removal of an existing
436 * directory; could leave unreferenced directory
440 (void) fprintf(stderr
,
441 gettext("%s: cannot create link "
442 "over directory %s\n"), cmd
,
446 if (unlink(target
) < 0) {
447 (void) fprintf(stderr
,
448 gettext("%s: cannot unlink %s: "),
458 * Create a symbolic link to the source.
461 if (symlink(source
, target
) < 0) {
462 (void) fprintf(stderr
,
463 gettext("%s: cannot create %s: "),
473 switch (chkfiles(source
, &target
)) {
476 /* default - fall through */
480 * Make sure source file is not a directory,
481 * we cannot link directories...
485 (void) fprintf(stderr
,
486 gettext("%s: %s is a directory\n"), cmd
, source
);
491 * hard link, call link() and return.
494 if (link(source
, target
) < 0) {
496 (void) fprintf(stderr
,
497 gettext("%s: %s is on a different file system\n"),
500 (void) fprintf(stderr
,
501 gettext("%s: cannot create link %s: "),
514 cpymve(char *source
, char *target
)
524 switch (chkfiles(source
, &target
)) {
527 /* default - fall through */
531 * If it's a recursive copy and source
532 * is a directory, then call rcopy (from copydir).
537 avl_index_t where
= 0;
540 dev_t save_dev
= s1
.st_dev
;
541 ino_t save_ino
= s1
.st_ino
;
544 * We will be recursing into the directory so
545 * save the inode information to a search tree
546 * to avoid getting into an endless loop.
548 if ((rc
= add_tnode(&stree
, save_dev
, save_ino
)) != 1) {
551 * We've already visited this directory.
552 * Don't remove the search tree entry
553 * to make sure we don't get into an
554 * endless loop if revisited from a
555 * different part of the hierarchy.
557 (void) fprintf(stderr
, gettext(
558 "%s: cycle detected: %s\n"),
567 rc
= copydir(source
, target
);
570 * Create a tnode to get an index to the matching
571 * node (same dev and inode) in the search tree,
572 * then use the index to remove the matching node
573 * so it we do not wrongly detect a cycle when
574 * revisiting this directory from another part of
577 if ((tnode
= create_tnode(save_dev
,
578 save_ino
)) == NULL
) {
582 if ((tptr
= avl_find(stree
, tnode
, &where
)) != NULL
) {
583 avl_remove(stree
, tptr
);
589 } else if (ISDEV(s1
) && Rflg
) {
590 return (copyspecial(target
));
597 if (rename(source
, target
) >= 0)
599 if (errno
!= EXDEV
) {
600 if (errno
== ENOTDIR
&& ISDIR(s1
)) {
601 (void) fprintf(stderr
,
602 gettext("%s: %s is a directory\n"),
606 (void) fprintf(stderr
,
607 gettext("%s: cannot rename %s to %s: "),
608 cmd
, source
, target
);
614 * cannot move a non-directory (source) onto an existing
618 if (targetexists
&& ISDIR(s2
) && (!ISDIR(s1
))) {
619 (void) fprintf(stderr
,
620 gettext("%s: cannot mv a non directory %s "
621 "over existing directory"
622 " %s \n"), cmd
, source
, target
);
627 if (targetexists
&& ISDIR(s2
)) {
628 /* existing target dir must be empty */
629 if (rmdir(target
) < 0) {
631 (void) fprintf(stderr
,
632 gettext("%s: cannot rmdir %s: "),
640 if ((n
= copydir(source
, target
)) == 0)
641 (void) rmdir(source
);
645 /* doors cannot be moved across filesystems */
647 (void) fprintf(stderr
,
648 gettext("%s: %s: cannot move door "
649 "across file systems\n"), cmd
, source
);
653 /* sockets cannot be moved across filesystems */
655 (void) fprintf(stderr
,
656 gettext("%s: %s: cannot move socket "
657 "across file systems\n"), cmd
, source
);
662 * File cannot be renamed, try to recreate the symbolic
663 * link or special device, or copy the file wholesale
664 * between file systems.
669 char symln
[PATH_MAX
+ 1];
671 if (targetexists
&& unlink(target
) < 0) {
672 (void) fprintf(stderr
,
673 gettext("%s: cannot unlink %s: "),
679 if ((m
= readlink(source
, symln
,
680 sizeof (symln
) - 1)) < 0) {
686 md
= umask(~(s1
.st_mode
& MODEBITS
));
687 if (symlink(symln
, target
) < 0) {
692 m
= lchown(target
, UID(s1
), GID(s1
));
695 (void) fprintf(stderr
, gettext("%s: cannot"
696 " change owner and group of"
697 " %s: "), cmd
, target
);
705 if (targetexists
&& unlink(target
) < 0) {
706 (void) fprintf(stderr
,
707 gettext("%s: cannot unlink %s: "),
713 if (mknod(target
, s1
.st_mode
, s1
.st_rdev
) < 0) {
718 (void) chg_mode(target
, UID(s1
), GID(s1
), FMODE(s1
));
719 (void) chg_time(target
, s1
);
725 if (targetexists
&& rmdir(target
) < 0) {
726 (void) fprintf(stderr
,
727 gettext("%s: cannot rmdir %s: "),
733 if (targetexists
&& unlink(target
) < 0) {
734 (void) fprintf(stderr
,
735 gettext("%s: cannot unlink %s: "),
745 * If the source file is a symlink, and either
746 * -P or -H flag (only if -H is specified and the
747 * source file is not a command line argument)
748 * were specified, then action is taken on the symlink
749 * itself, not the file referenced by the symlink.
750 * Note: this is executed for 'cp' only.
752 if (cpy
&& (Pflg
|| (Hflg
&& !cmdarg
)) && (ISLNK(s1
))) {
755 char symln
[PATH_MAX
+ 1];
757 m
= readlink(source
, symln
, sizeof (symln
) - 1);
766 * Copy the sym link to the target.
767 * Note: If the target exists, write a
768 * diagnostic message, do nothing more
769 * with the source file, and return to
770 * process any remaining files.
772 md
= umask(~(s1
.st_mode
& MODEBITS
));
773 if (symlink(symln
, target
) < 0) {
778 m
= lchown(target
, UID(s1
), GID(s1
));
781 (void) fprintf(stderr
, gettext(
782 "cp: cannot change owner and "
783 "group of %s:"), target
);
788 * Copy the file. If it happens to be a
789 * symlink, copy the file referenced
792 fi
= open(source
, O_RDONLY
);
794 (void) fprintf(stderr
,
795 gettext("%s: cannot open %s: "),
801 fo
= creat(target
, s1
.st_mode
& MODEBITS
);
804 * If -f and creat() failed, unlink
808 (void) unlink(target
);
810 s1
.st_mode
& MODEBITS
);
814 (void) fprintf(stderr
,
815 gettext("%s: cannot create %s: "),
821 /* stat the new file, its used below */
822 (void) stat(target
, &s2
);
826 * Set target's permissions to the source
827 * before any copying so that any partially
828 * copied file will have the source's
829 * permissions (at most) or umask permissions
830 * whichever is the most restrictive.
832 * ACL for regular files
836 (void) chmod(target
, FMODE(s1
));
841 (void) fprintf(stderr
,
857 if (fstat(fi
, &s1
) < 0) {
858 (void) fprintf(stderr
,
859 gettext("%s: cannot access %s\n"),
863 if (IDENTICAL(s1
, s2
)) {
864 (void) fprintf(stderr
,
866 "%s: %s and %s are identical\n"),
867 cmd
, source
, target
);
871 if (writefile(fi
, fo
, source
, target
, NULL
,
872 NULL
, &s1
, &s2
) != 0) {
878 Perror2(target
, "write");
882 /* Copy regular extended attributes */
883 if (pflg
|| atflg
|| mve
|| saflg
) {
884 attret
= copyattributes(source
, target
);
885 if (attret
!= 0 && !attrsilent
) {
886 (void) fprintf(stderr
, gettext(
887 "%s: Failed to preserve"
888 " extended attributes of file"
889 " %s\n"), cmd
, source
);
891 /* Copy extended system attributes */
892 if (pflg
|| mve
|| saflg
)
893 sattret
= copy_sysattr(source
, target
);
894 if (mve
&& attret
!= 0) {
895 (void) unlink(target
);
904 * XPG4: the write system call will clear setgid
905 * and setuid bits, so set them again.
908 if ((ret
= chg_mode(target
, UID(s1
), GID(s1
),
912 * Reapply ACL, since chmod may have
916 if ((acl_set(target
, s1acl
)) < 0) {
918 (void) fprintf(stderr
,
919 gettext("%s: Failed to "
921 "on %s\n"), cmd
, target
);
928 if ((ret
= chg_time(target
, s1
)) > 0)
932 if (error
!= 0 || attret
!= 0 || sattret
!= 0)
938 (void) fprintf(stderr
,
939 gettext("%s: %s: unknown file type 0x%x\n"), cmd
,
940 source
, (s1
.st_mode
& S_IFMT
));
944 if (unlink(source
) < 0) {
945 (void) unlink(target
);
946 (void) fprintf(stderr
,
947 gettext("%s: cannot unlink %s: "),
952 if (error
!= 0 || attret
!= 0 || sattret
!= 0)
963 * Create a node for use with the search tree which contains the
964 * inode information (device id and inode number).
971 * tnode - NULL on error, otherwise returns a tnode structure
972 * which contains the input device id and inode number.
975 create_tnode(dev_t dev
, ino_t ino
)
979 if ((tnode
= (tree_node_t
*)malloc(sizeof (tree_node_t
))) != NULL
) {
980 tnode
->node_dev
= dev
;
981 tnode
->node_ino
= ino
;
988 chkfiles(char *source
, char **to
)
991 int (*statf
)() = (cpy
&&
992 !(Pflg
|| (Hflg
&& !cmdarg
))) ? stat
: lstat
;
997 * Make sure source file exists.
999 if ((*statf
)(source
, &s1
) < 0) {
1001 * Keep the old error message except when someone tries to
1002 * mv/cp/ln a symbolic link that has a trailing slash and
1005 if (errno
== ENOTDIR
)
1006 (void) fprintf(stderr
, "%s: %s: %s\n", cmd
, source
,
1009 (void) fprintf(stderr
,
1010 gettext("%s: cannot access %s\n"), cmd
, source
);
1015 * Get ACL info: don't bother with ln or cp/mv'ing symlinks
1017 if (!lnk
&& !ISLNK(s1
)) {
1018 if (s1acl
!= NULL
) {
1022 if ((error
= acl_get(source
, ACL_NO_TRIVIAL
, &s1acl
)) != 0) {
1023 (void) fprintf(stderr
,
1024 "%s: failed to get acl entries: %s\n", source
,
1025 acl_strerror(error
));
1028 /* else: just permission bits */
1032 * If stat fails, then the target doesn't exist,
1033 * we will create a new target with default file type of regular.
1036 FTYPE(s2
) = S_IFREG
;
1038 if ((*statf
)(target
, &s2
) >= 0) {
1040 (void) stat(target
, &s2
);
1042 * If target is a directory,
1043 * make complete name of new file
1044 * within that directory.
1049 len
= strlen(target
) + strlen(dname(source
)) + 4;
1050 if ((buf
= (char *)malloc(len
)) == NULL
) {
1051 (void) fprintf(stderr
,
1052 gettext("%s: Insufficient memory to "
1053 "%s %s\n "), cmd
, cmd
, source
);
1056 (void) snprintf(buf
, len
, "%s/%s",
1057 target
, dname(source
));
1061 if ((*statf
)(target
, &s2
) >= 0) {
1062 int overwrite
= FALSE
;
1063 int override
= FALSE
;
1068 * For cp and mv, it is an error if the
1069 * source and target are the same file.
1070 * Check for the same inode and file
1071 * system, but don't check for the same
1072 * absolute pathname because it is an
1073 * error when the source and target are
1074 * hard links to the same file.
1076 if (IDENTICAL(s1
, s2
)) {
1077 (void) fprintf(stderr
,
1079 "%s: %s and %s are identical\n"),
1080 cmd
, source
, target
);
1087 * For ln, it is an error if the source and
1088 * target are identical files (same inode,
1089 * same file system, and filenames resolve
1090 * to same absolute pathname).
1092 if (!chk_different(source
, target
)) {
1097 if (lnk
&& !silent
) {
1098 (void) fprintf(stderr
,
1099 gettext("%s: %s: File exists\n"),
1107 * If the user does not have access to
1108 * the target, ask ----if it is not
1109 * silent and user invoked command
1113 * If not silent, and stdin is a terminal, and
1114 * there's no write access, and the file isn't a
1115 * symbolic link, ask for permission.
1117 * XPG4: both overwrite and override:
1118 * ask only one question.
1120 * TRANSLATION_NOTE - The following messages will
1121 * contain the first character of the strings for
1122 * "yes" and "no" defined in the file
1123 * "nl_langinfo.po". After substitution, the
1124 * message will appear as follows:
1125 * <cmd>: overwrite <filename> (y/n)?
1126 * where <cmd> is the name of the command
1127 * (cp, mv) and <filename> is the destination file
1131 overwrite
= iflg
&& !silent
&& use_stdin();
1132 override
= !cpy
&& (access(target
, 2) < 0) &&
1133 !silent
&& use_stdin() && !ISLNK(s2
);
1135 if (overwrite
&& override
) {
1136 (void) fprintf(stderr
,
1137 gettext("%s: overwrite %s and override "
1138 "protection %o? "), cmd
, target
,
1139 FMODE(s2
) & MODEBITS
);
1144 } else if (overwrite
&& ISREG(s2
)) {
1145 (void) fprintf(stderr
,
1146 gettext("%s: overwrite %s? "),
1152 } else if (override
) {
1153 (void) fprintf(stderr
,
1154 gettext("%s: %s: override protection %o? "),
1155 cmd
, target
, FMODE(s2
) & MODEBITS
);
1162 if (lnk
&& unlink(target
) < 0) {
1163 (void) fprintf(stderr
,
1164 gettext("%s: cannot unlink %s: "),
1175 * check whether source and target are different
1176 * return 1 when they are different
1177 * return 0 when they are identical, or when unable to resolve a pathname
1180 chk_different(char *source
, char *target
)
1182 char rtarget
[PATH_MAX
], rsource
[PATH_MAX
];
1184 if (IDENTICAL(s1
, s2
)) {
1186 * IDENTICAL will be true for hard links, therefore
1187 * check whether the filenames are different
1189 if ((getrealpath(source
, rsource
) == 0) ||
1190 (getrealpath(target
, rtarget
) == 0)) {
1193 if (strncmp(rsource
, rtarget
, PATH_MAX
) == 0) {
1194 (void) fprintf(stderr
, gettext(
1195 "%s: %s and %s are identical\n"),
1196 cmd
, source
, target
);
1204 * get real path (resolved absolute pathname)
1205 * return 1 on success, 0 on failure
1208 getrealpath(char *path
, char *rpath
)
1210 if (realpath(path
, rpath
) == NULL
) {
1211 int errno_save
= errno
;
1212 (void) fprintf(stderr
, gettext(
1213 "%s: cannot resolve path %s: "), cmd
, path
);
1222 rcopy(char *from
, char *to
)
1224 DIR *fold
= opendir(from
);
1226 struct stat statb
, s1save
;
1228 char fromname
[PATH_MAX
];
1230 if (fold
== 0 || ((pflg
|| mve
) && fstat(fold
->dd_fd
, &statb
) < 0)) {
1236 * Save s1 (stat information for source dir) so that
1237 * mod and access times can be reserved during "cp -p"
1238 * or mv, since s1 gets overwritten.
1245 (void) closedir(fold
);
1247 return (chg_time(to
, s1save
) + errs
);
1252 if ((strcmp(dp
->d_name
, ".") == 0) ||
1253 (strcmp(dp
->d_name
, "..") == 0))
1255 if (strlen(from
)+1+strlen(dp
->d_name
) >=
1256 sizeof (fromname
) - 1) {
1257 (void) fprintf(stderr
,
1258 gettext("%s : %s/%s: Name too long\n"),
1259 cmd
, from
, dp
->d_name
);
1263 (void) snprintf(fromname
, sizeof (fromname
),
1264 "%s/%s", from
, dp
->d_name
);
1265 errs
+= cpymve(fromname
, to
);
1275 * Return just the file name given the complete path.
1282 * While there are characters left,
1283 * set name to start after last
1288 if (*p
++ == DELIM
&& *p
)
1297 * Display usage message.
1301 (void) fprintf(stderr
, gettext(
1302 "Usage: mv [-f] [-i] f1 f2\n"
1303 " mv [-f] [-i] f1 ... fn d1\n"
1304 " mv [-f] [-i] d1 d2\n"));
1307 (void) fprintf(stderr
, gettext(
1308 "Usage: ln [-f] [-s] f1 [f2]\n"
1309 " ln [-f] [-s] f1 ... fn d1\n"
1310 " ln [-f] -s d1 d2\n"));
1312 (void) fprintf(stderr
, gettext(
1313 "Usage: ln [-f] [-n] [-s] f1 [f2]\n"
1314 " ln [-f] [-n] [-s] f1 ... fn d1\n"
1315 " ln [-f] [-n] -s d1 d2\n"));
1318 (void) fprintf(stderr
, gettext(
1319 "Usage: cp [-a] [-f] [-i] [-p] [-@] [-/] f1 f2\n"
1320 " cp [-a] [-f] [-i] [-p] [-@] [-/] f1 ... fn d1\n"
1321 " cp [-r|-R [-H|-L|-P]] [-a] [-f] [-i] [-p] [-@] "
1322 "[-/] d1 ... dn-1 dn\n"));
1330 * Try to preserve modification and access time.
1331 * If 1) pflg is not set, or 2) pflg is set and this is the Solaris version,
1332 * don't report a utimensat() failure.
1333 * If this is the XPG4 version and utimensat fails, if 1) pflg is set (cp -p)
1334 * or 2) we are doing a mv, print a diagnostic message; arrange for a non-zero
1335 * exit status only if pflg is set.
1336 * utimensat(2) is being used to achieve granularity in nanoseconds
1337 * (if supported by the underlying file system) while setting file times.
1340 chg_time(char *to
, struct stat ss
)
1342 struct timespec times
[2];
1345 times
[0] = ss
.st_atim
;
1346 times
[1] = ss
.st_mtim
;
1348 rc
= utimensat(AT_FDCWD
, to
, times
,
1349 ISLNK(s1
) ? AT_SYMLINK_NOFOLLOW
: 0);
1351 if ((pflg
|| mve
) && rc
!= 0) {
1352 (void) fprintf(stderr
,
1353 gettext("%s: cannot set times for %s: "), cmd
, to
);
1367 * This function is called upon "cp -p" or mv across filesystems.
1369 * Try to preserve the owner and group id. If chown() fails,
1370 * only print a diagnostic message if doing a mv in the XPG4 version;
1371 * try to clear S_ISUID and S_ISGID bits in the target. If unable to clear
1372 * S_ISUID and S_ISGID bits, print a diagnostic message and arrange for a
1373 * non-zero exit status because this is a security violation.
1374 * Try to preserve permissions.
1375 * If this is the XPG4 version and chmod() fails, print a diagnostic message
1376 * and arrange for a non-zero exit status.
1377 * If this is the Solaris version and chmod() fails, do not print a
1378 * diagnostic message or exit with a non-zero value.
1381 chg_mode(char *target
, uid_t uid
, gid_t gid
, mode_t mode
)
1383 int clearflg
= 0; /* controls message printed upon chown() error */
1386 /* Don't change mode if target is symlink */
1387 if (lstat(target
, &st
) == 0 && ISLNK(st
))
1390 if (chown(target
, uid
, gid
) != 0) {
1393 (void) fprintf(stderr
, gettext("%s: cannot change"
1394 " owner and group of %s: "), cmd
, target
);
1398 if (mode
& (S_ISUID
| S_ISGID
)) {
1399 /* try to clear S_ISUID and S_ISGID */
1400 mode
&= ~S_ISUID
& ~S_ISGID
;
1404 if (chmod(target
, mode
) != 0) {
1406 (void) fprintf(stderr
, gettext(
1407 "%s: cannot clear S_ISUID and S_ISGID bits in"
1408 " %s: "), cmd
, target
);
1410 /* cp -p should get non-zero exit; mv should not */
1416 (void) fprintf(stderr
, gettext(
1417 "%s: cannot set permissions for %s: "), cmd
, target
);
1419 /* cp -p should get non-zero exit; mv should not */
1432 char buf
[PATH_MAX
+ 10];
1434 (void) snprintf(buf
, sizeof (buf
), "%s: %s", cmd
, s
);
1439 Perror2(char *s1
, char *s2
)
1441 char buf
[PATH_MAX
+ 20];
1443 (void) snprintf(buf
, sizeof (buf
), "%s: %s: %s",
1444 cmd
, gettext(s1
), gettext(s2
));
1449 * used for cp -R and for mv across file systems
1452 copydir(char *source
, char *target
)
1454 int ret
, attret
= 0;
1456 int pret
= 0; /* need separate flag if -p is specified */
1457 mode_t fixmode
= (mode_t
)0; /* cleanup mode after copy */
1465 (void) fprintf(stderr
,
1466 gettext("%s: %s: is a directory\n"), cmd
, source
);
1470 if (stat(target
, &s2
) < 0) {
1471 if (mkdir(target
, (s1
.st_mode
& MODEBITS
)) < 0) {
1472 (void) fprintf(stderr
, "%s: ", cmd
);
1476 if (stat(target
, &s2
) == 0) {
1477 fixmode
= s2
.st_mode
;
1479 fixmode
= s1
.st_mode
;
1481 (void) chmod(target
, ((fixmode
& MODEBITS
) | S_IRWXU
));
1482 } else if (!(ISDIR(s2
))) {
1483 (void) fprintf(stderr
,
1484 gettext("%s: %s: not a directory.\n"), cmd
, target
);
1489 * Save s1 (stat information for source dir) and acl info,
1490 * if any, so that ownership, modes, times, and acl's can
1491 * be reserved during "cp -p" or mv.
1492 * s1 gets overwritten when doing the recursive copy.
1495 if (s1acl
!= NULL
) {
1496 s1acl_save
= acl_dup(s1acl
);
1497 if (s1acl_save
== NULL
) {
1498 (void) fprintf(stderr
, gettext("%s: "
1499 "Insufficient memory to save acl"
1507 (void) fprintf(stderr
, gettext("%s: "
1508 "Insufficient memory to save acl"
1517 ret
= rcopy(source
, target
);
1520 * Once we created a directory, go ahead and set
1521 * its attributes, e.g. acls and time. The info
1522 * may get overwritten if we continue traversing
1528 if ((pret
= chg_mode(target
, UID(s1save
), GID(s1save
),
1529 FMODE(s1save
))) == 0)
1530 pret
= chg_time(target
, s1save
);
1532 if (s1acl_save
!= NULL
) {
1533 if (acl_set(target
, s1acl_save
) < 0) {
1540 (void) fprintf(stderr
, gettext(
1541 "%s: failed to set acl entries "
1542 "on %s\n"), cmd
, target
);
1544 acl_free(s1acl_save
);
1549 /* else: silent and continue */
1551 acl_free(s1acl_save
);
1554 } else if (fixmode
!= (mode_t
)0)
1555 (void) chmod(target
, fixmode
& MODEBITS
);
1557 if (pflg
|| atflg
|| mve
|| saflg
) {
1558 attret
= copyattributes(source
, target
);
1559 if (!attrsilent
&& attret
!= 0) {
1560 (void) fprintf(stderr
, gettext("%s: Failed to preserve"
1561 " extended attributes of directory"
1562 " %s\n"), cmd
, source
);
1565 * Otherwise ignore failure.
1569 /* Copy extended system attributes */
1570 if (pflg
|| mve
|| saflg
) {
1571 sattret
= copy_sysattr(source
, target
);
1573 (void) fprintf(stderr
, gettext(
1574 "%s: Failed to preserve "
1575 "extended system attributes "
1576 "of directory %s\n"), cmd
, source
);
1580 if (attret
!= 0 || sattret
!= 0 || error
!= 0)
1586 copyspecial(char *target
)
1590 if (mknod(target
, s1
.st_mode
, s1
.st_rdev
) != 0) {
1591 (void) fprintf(stderr
, gettext(
1592 "cp: cannot create special file %s: "), target
);
1598 if ((ret
= chg_mode(target
, UID(s1
), GID(s1
), FMODE(s1
))) == 0)
1599 ret
= chg_time(target
, s1
);
1611 return (isatty(fileno(stdin
)));
1615 /* Copy non-system extended attributes */
1618 copyattributes(char *source
, char *target
)
1626 acl_t
*attrdiracl
= NULL
;
1627 struct timespec times
[2];
1630 if (pathconf(source
, _PC_XATTR_EXISTS
) != 1)
1633 if (pathconf(target
, _PC_XATTR_ENABLED
) != 1) {
1635 (void) fprintf(stderr
,
1637 "%s: cannot preserve extended attributes, "
1638 "operation not supported on file"
1639 " %s\n"), cmd
, target
);
1643 if (open_source(source
) != 0)
1645 if (open_target_srctarg_attrdirs(source
, target
) != 0)
1647 if (open_attrdirp(source
) != 0)
1651 if (fchmod(targetdirfd
, attrdir
.st_mode
) == -1) {
1653 (void) fprintf(stderr
,
1654 gettext("%s: failed to set file mode"
1655 " correctly on attribute directory of"
1656 " file %s: "), cmd
, target
);
1662 if (fchown(targetdirfd
, attrdir
.st_uid
, attrdir
.st_gid
) == -1) {
1664 (void) fprintf(stderr
,
1665 gettext("%s: failed to set file"
1666 " ownership correctly on attribute"
1667 " directory of file %s: "), cmd
, target
);
1673 * Now that we are the owner we can update st_ctime by calling
1676 times
[0] = attrdir
.st_atim
;
1677 times
[1] = attrdir
.st_mtim
;
1678 if (utimensat(targetdirfd
, ".", times
, 0) < 0) {
1680 (void) fprintf(stderr
,
1681 gettext("%s: cannot set attribute times"
1682 " for %s: "), cmd
, target
);
1689 * Now set owner and group of attribute directory, implies
1690 * changing the ACL of the hidden attribute directory first.
1692 if ((aclerror
= facl_get(sourcedirfd
,
1693 ACL_NO_TRIVIAL
, &attrdiracl
)) != 0) {
1695 (void) fprintf(stderr
, gettext(
1696 "%s: failed to get acl entries of"
1697 " attribute directory for"
1699 source
, acl_strerror(aclerror
));
1705 if (facl_set(targetdirfd
, attrdiracl
) != 0) {
1707 (void) fprintf(stderr
, gettext(
1708 "%s: failed to set acl entries"
1709 " on attribute directory "
1710 "for %s\n"), cmd
, target
);
1713 acl_free(attrdiracl
);
1719 while ((dp
= readdir(srcdirp
)) != NULL
) {
1722 if ((ret
= traverse_attrfile(dp
, source
, target
, 1)) == -1)
1730 if ((aclerror
= facl_get(srcattrfd
,
1731 ACL_NO_TRIVIAL
, &xacl
)) != 0) {
1733 (void) fprintf(stderr
, gettext(
1734 "%s: failed to get acl entries of"
1736 " %s: %s"), cmd
, dp
->d_name
,
1737 source
, acl_strerror(aclerror
));
1746 if ((pflg
|| mve
) && xacl
!= NULL
) {
1747 if ((facl_set(targattrfd
, xacl
)) < 0) {
1749 (void) fprintf(stderr
, gettext(
1750 "%s: failed to set acl entries on"
1752 "%s\n"), cmd
, dp
->d_name
, target
);
1760 if (writefile(srcattrfd
, targattrfd
, source
, target
,
1761 dp
->d_name
, dp
->d_name
, &s3
, &s4
) != 0) {
1771 if (fchown(targattrfd
, UID(s3
), GID(s3
)) != 0) {
1773 (void) fprintf(stderr
,
1774 gettext("%s: cannot change"
1775 " owner and group of"
1776 " attribute %s for" " file"
1777 " %s: "), cmd
, dp
->d_name
, target
);
1781 if (mode
& (S_ISUID
| S_ISGID
)) {
1782 /* try to clear S_ISUID and S_ISGID */
1783 mode
&= ~S_ISUID
& ~S_ISGID
;
1787 times
[0] = s3
.st_atim
;
1788 times
[1] = s3
.st_mtim
;
1789 if (utimensat(targetdirfd
, dp
->d_name
, times
, 0) < 0) {
1791 (void) fprintf(stderr
,
1792 gettext("%s: cannot set attribute"
1793 " times for %s: "), cmd
, target
);
1798 if (fchmod(targattrfd
, mode
) != 0) {
1800 (void) fprintf(stderr
, gettext(
1801 "%s: cannot clear S_ISUID and "
1802 "S_ISGID bits in attribute %s"
1804 " %s: "), cmd
, dp
->d_name
, target
);
1807 (void) fprintf(stderr
,
1809 "%s: cannot set permissions of attribute"
1810 " %s for %s: "), cmd
, dp
->d_name
, target
);
1816 if (xacl
&& ((facl_set(targattrfd
, xacl
)) < 0)) {
1818 (void) fprintf(stderr
, gettext(
1819 "%s: failed to set acl entries on"
1821 "%s\n"), cmd
, dp
->d_name
, target
);
1833 if (srcattrfd
!= -1)
1834 (void) close(srcattrfd
);
1835 if (targattrfd
!= -1)
1836 (void) close(targattrfd
);
1837 srcattrfd
= targattrfd
= -1;
1844 if (attrdiracl
!= NULL
) {
1845 acl_free(attrdiracl
);
1849 if (!saflg
&& !pflg
&& !mve
)
1851 return (error
== 0 ? 0 : 1);
1854 /* Copy extended system attributes from source to target */
1857 copy_sysattr(char *source
, char *target
)
1862 int target_sa_support
= 0;
1864 if (sysattr_support(source
, _PC_SATTR_EXISTS
) != 1)
1867 if (open_source(source
) != 0)
1871 * Gets non default extended system attributes from the
1872 * source file to copy to the target. The target has
1873 * the defaults set when its created and thus no need
1874 * to copy the defaults.
1876 response
= sysattr_list(cmd
, srcfd
, source
);
1878 if (sysattr_support(target
, _PC_SATTR_ENABLED
) != 1) {
1879 if (response
!= NULL
) {
1880 (void) fprintf(stderr
,
1882 "%s: cannot preserve extended system "
1883 "attribute, operation not supported on file"
1884 " %s\n"), cmd
, target
);
1889 target_sa_support
= 1;
1892 if (target_sa_support
) {
1893 if (srcdirp
== NULL
) {
1894 if (open_target_srctarg_attrdirs(source
,
1899 if (open_attrdirp(source
) != 0) {
1904 rewind_attrdir(srcdirp
);
1906 while ((dp
= readdir(srcdirp
)) != NULL
) {
1910 if ((ret
= traverse_attrfile(dp
, source
, target
,
1918 * Gets non default extended system attributes from the
1919 * attribute file to copy to the target. The target has
1920 * the defaults set when its created and thus no need
1921 * to copy the defaults.
1923 if (dp
->d_name
!= NULL
) {
1924 res
= sysattr_list(cmd
, srcattrfd
, dp
->d_name
);
1929 * Copy non default extended system attributes of named
1932 if (fsetattr(targattrfd
,
1933 XATTR_VIEW_READWRITE
, res
) != 0) {
1935 (void) fprintf(stderr
, gettext("%s: "
1936 "Failed to copy extended system "
1937 "attributes from attribute file "
1938 "%s of %s to %s\n"), cmd
,
1939 dp
->d_name
, source
, target
);
1943 if (srcattrfd
!= -1)
1944 (void) close(srcattrfd
);
1945 if (targattrfd
!= -1)
1946 (void) close(targattrfd
);
1947 srcattrfd
= targattrfd
= -1;
1951 /* Copy source file non default extended system attributes to target */
1952 if (target_sa_support
&& (response
!= NULL
) &&
1953 (fsetattr(targfd
, XATTR_VIEW_READWRITE
, response
)) != 0) {
1955 (void) fprintf(stderr
, gettext("%s: Failed to "
1956 "copy extended system attributes from "
1957 "%s to %s\n"), cmd
, source
, target
);
1960 nvlist_free(response
);
1962 return (error
== 0 ? 0 : 1);
1965 /* Open the source file */
1968 open_source(char *src
)
1973 if ((srcfd
= open(src
, O_RDONLY
)) == -1) {
1974 if (pflg
&& attrsilent
) {
1979 (void) fprintf(stderr
,
1980 gettext("%s: cannot open file"
1981 " %s: "), cmd
, src
);
1989 return (error
== 0 ? 0 : 1);
1992 /* Open source attribute dir, target and target attribute dir. */
1995 open_target_srctarg_attrdirs(char *src
, char *targ
)
1999 targfd
= sourcedirfd
= targetdirfd
= -1;
2001 if ((targfd
= open(targ
, O_RDONLY
)) == -1) {
2002 if (pflg
&& attrsilent
) {
2007 (void) fprintf(stderr
,
2008 gettext("%s: cannot open file"
2009 " %s: "), cmd
, targ
);
2016 if ((sourcedirfd
= openat(srcfd
, ".", O_RDONLY
|O_XATTR
)) == -1) {
2017 if (pflg
&& attrsilent
) {
2022 (void) fprintf(stderr
,
2023 gettext("%s: cannot open attribute"
2024 " directory for %s: "), cmd
, src
);
2031 if (fstat(sourcedirfd
, &attrdir
) == -1) {
2032 if (pflg
&& attrsilent
) {
2038 (void) fprintf(stderr
,
2039 gettext("%s: could not retrieve stat"
2040 " information for attribute directory"
2041 "of file %s: "), cmd
, src
);
2047 if ((targetdirfd
= openat(targfd
, ".", O_RDONLY
|O_XATTR
)) == -1) {
2048 if (pflg
&& attrsilent
) {
2053 (void) fprintf(stderr
,
2054 gettext("%s: cannot open attribute"
2055 " directory for %s: "), cmd
, targ
);
2063 return (error
== 0 ? 0 : 1);
2067 open_attrdirp(char *source
)
2073 * dup sourcedirfd for use by fdopendir().
2074 * fdopendir will take ownership of given fd and will close
2075 * it when closedir() is called.
2078 if ((tmpfd
= dup(sourcedirfd
)) == -1) {
2079 if (pflg
&& attrsilent
) {
2084 (void) fprintf(stderr
,
2086 "%s: unable to dup attribute directory"
2087 " file descriptor for %s: "), cmd
, source
);
2093 if ((srcdirp
= fdopendir(tmpfd
)) == NULL
) {
2094 if (pflg
&& attrsilent
) {
2099 (void) fprintf(stderr
,
2100 gettext("%s: failed to open attribute"
2101 " directory for %s: "), cmd
, source
);
2109 return (error
== 0 ? 0 : 1);
2112 /* Skips through ., .., and system attribute 'view' files */
2114 traverse_attrfile(struct dirent
*dp
, char *source
, char *target
, int first
)
2118 srcattrfd
= targattrfd
= -1;
2120 if ((dp
->d_name
[0] == '.' && dp
->d_name
[1] == '\0') ||
2121 (dp
->d_name
[0] == '.' && dp
->d_name
[1] == '.' &&
2122 dp
->d_name
[2] == '\0') ||
2123 (sysattr_type(dp
->d_name
) == _RO_SATTR
) ||
2124 (sysattr_type(dp
->d_name
) == _RW_SATTR
))
2127 if ((srcattrfd
= openat(sourcedirfd
, dp
->d_name
,
2130 (void) fprintf(stderr
,
2131 gettext("%s: cannot open attribute %s on"
2132 " file %s: "), cmd
, dp
->d_name
, source
);
2139 if (fstat(srcattrfd
, &s3
) < 0) {
2141 (void) fprintf(stderr
,
2142 gettext("%s: could not stat attribute"
2144 " %s: "), cmd
, dp
->d_name
, source
);
2152 (void) unlinkat(targetdirfd
, dp
->d_name
, 0);
2153 if ((targattrfd
= openat(targetdirfd
, dp
->d_name
,
2154 O_RDWR
|O_CREAT
|O_TRUNC
, s3
.st_mode
& MODEBITS
)) == -1) {
2156 (void) fprintf(stderr
,
2157 gettext("%s: could not create attribute"
2158 " %s on file %s: "), cmd
, dp
->d_name
,
2166 if ((targattrfd
= openat(targetdirfd
, dp
->d_name
,
2169 (void) fprintf(stderr
,
2170 gettext("%s: could not open attribute"
2171 " %s on file %s: "), cmd
, dp
->d_name
,
2181 if (fstat(targattrfd
, &s4
) < 0) {
2183 (void) fprintf(stderr
,
2184 gettext("%s: could not stat attribute"
2186 " %s: "), cmd
, dp
->d_name
, target
);
2194 if (srcattrfd
!= -1)
2195 (void) close(srcattrfd
);
2196 if (targattrfd
!= -1)
2197 (void) close(targattrfd
);
2198 srcattrfd
= targattrfd
= -1;
2200 return (error
== 0 ? 0 :1);
2204 rewind_attrdir(DIR * sdp
)
2208 pwdfd
= open(".", O_RDONLY
);
2209 if ((pwdfd
!= -1) && (fchdir(sourcedirfd
) == 0)) {
2211 (void) fchdir(pwdfd
);
2212 (void) close(pwdfd
);
2215 (void) fprintf(stderr
, gettext("%s: "
2216 "failed to rewind attribute dir\n"),
2225 if (srcattrfd
!= -1)
2226 (void) close(srcattrfd
);
2227 if (targattrfd
!= -1)
2228 (void) close(targattrfd
);
2229 if (sourcedirfd
!= -1)
2230 (void) close(sourcedirfd
);
2231 if (targetdirfd
!= -1)
2232 (void) close(targetdirfd
);
2233 if (srcdirp
!= NULL
) {
2234 (void) closedir(srcdirp
);
2238 (void) close(srcfd
);
2240 (void) close(targfd
);