Merge commit '00f1a4f432b3d8aad1aa270e91c44c57f03ef407'
[unleashed.git] / usr / src / cmd / mv / mv.c
blobfc5542f3d01eefb73f9a05978ebeb5b5bf9e5545
1 /*
2 * CDDL HEADER START
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]
19 * CDDL HEADER END
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
37 * All Rights Reserved
39 * University Acknowledgment- Portions of this document are derived from
40 * software developed by the University of California, Berkeley, and its
41 * contributors.
45 * Combined mv/cp/ln command:
46 * mv file1 file2
47 * mv dir1 dir2
48 * mv file1 ... filen dir1
50 #include <sys/time.h>
51 #include <signal.h>
52 #include <locale.h>
53 #include <stdarg.h>
54 #include <sys/acl.h>
55 #include <libcmdutils.h>
56 #include <aclutils.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)
73 #define DELIM '/'
74 #define EQ(x, y) (strcmp(x, y) == 0)
75 #define FALSE 0
76 #define MODEBITS (S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO)
77 #define TRUE 1
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;
102 static char *cmd;
103 static int silent = 0;
104 static int fflg = 0;
105 static int iflg = 0;
106 static int pflg = 0;
107 static int Rflg = 0; /* recursive copy */
108 static int rflg = 0; /* recursive copy */
109 static int sflg = 0;
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 */
118 static acl_t *s1acl;
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], '/'))
151 ++cmd;
152 else
153 cmd = 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 */
162 #endif
163 (void) textdomain(TEXT_DOMAIN);
164 if (init_yes() < 0) {
165 (void) fprintf(stderr, gettext(ERR_MSG_INIT_YES),
166 strerror(errno));
167 exit(3);
170 if (EQ(cmd, "mv"))
171 mve = TRUE;
172 else if (EQ(cmd, "ln"))
173 lnk = TRUE;
174 else if (EQ(cmd, "cp"))
175 cpy = TRUE;
176 else {
177 (void) fprintf(stderr,
178 gettext("Invalid command name (%s); expecting "
179 "mv, cp, or ln.\n"), cmd);
180 exit(1);
184 * Check for options:
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
193 if (cpy) {
194 while ((c = getopt(argc, argv, "afHiLpPrR@/")) != EOF)
195 switch (c) {
196 case 'f':
197 fflg++;
198 break;
199 case 'i':
200 iflg++;
201 break;
202 case 'p':
203 pflg++;
204 #ifdef XPG4
205 attrsilent = 1;
206 atflg = 0;
207 saflg = 0;
208 #else
209 if (atflg == 0)
210 attrsilent = 1;
211 #endif
212 break;
213 case 'H':
215 * If more than one of -H, -L, or -P are
216 * specified, only the last option specified
217 * determines the behavior.
219 Lflg = Pflg = 0;
220 Hflg++;
221 break;
222 case 'L':
223 Hflg = Pflg = 0;
224 Lflg++;
225 break;
226 case 'P':
227 Lflg = Hflg = 0;
228 Pflg++;
229 break;
230 case 'R':
232 * The default behavior of cp -R|-r
233 * when specified without -H|-L|-P
234 * is -L.
236 Rflg++;
237 /*FALLTHROUGH*/
238 case 'r':
239 rflg++;
240 break;
241 case 'a':
242 Lflg = Hflg = 0;
243 pflg++;
244 Pflg++;
245 Rflg++;
246 rflg++;
247 break;
248 case '@':
249 atflg++;
250 attrsilent = 0;
251 #ifdef XPG4
252 pflg = 0;
253 #endif
254 break;
255 case '/':
256 saflg++;
257 attrsilent = 0;
258 #ifdef XPG4
259 pflg = 0;
260 #endif
261 break;
262 default:
263 errflg++;
266 /* -R or -r must be specified with -H, -L, or -P */
267 if ((Hflg || Lflg || Pflg) && !(Rflg || rflg)) {
268 errflg++;
271 } else if (mve) {
272 while ((c = getopt(argc, argv, "fis")) != EOF)
273 switch (c) {
274 case 'f':
275 silent++;
276 #ifdef XPG4
277 iflg = 0;
278 #endif
279 break;
280 case 'i':
281 iflg++;
282 #ifdef XPG4
283 silent = 0;
284 #endif
285 break;
286 default:
287 errflg++;
289 } else { /* ln */
290 while ((c = getopt(argc, argv, "fns")) != EOF)
291 switch (c) {
292 case 'f':
293 silent++;
294 break;
295 case 'n':
296 /* silently ignored; this is the default */
297 break;
298 case 's':
299 sflg++;
300 break;
301 default:
302 errflg++;
307 * For BSD compatibility allow - to delimit the end of
308 * options for mv.
310 if (mve && optind < argc && (strcmp(argv[optind], "-") == 0))
311 optind++;
314 * Check for sufficient arguments
315 * or a usage error.
318 argc -= optind;
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"),
324 cmd, argc);
325 usage();
328 if (errflg != 0)
329 usage();
332 * If there is more than a source and target,
333 * the last argument (the target) must be a directory
334 * which really exists.
337 if (argc > 2) {
338 if (stat(argv[argc-1], &s2) < 0) {
339 (void) fprintf(stderr,
340 gettext("%s: %s not found\n"),
341 cmd, argv[argc-1]);
342 exit(2);
345 if (!ISDIR(s2)) {
346 (void) fprintf(stderr,
347 gettext("%s: Target %s must be a directory\n"),
348 cmd, argv[argc-1]);
349 usage();
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);
357 exit(78);
360 if (argc == 1) {
361 if (!lnk)
362 usage();
363 (void) strcpy(target, ".");
364 } else {
365 (void) strcpy(target, argv[--argc]);
369 * Perform a multiple argument mv|cp|ln by
370 * multiple invocations of cpymve() or lnkfil().
372 if (lnk)
373 move = lnkfil;
374 else
375 move = cpymve;
377 r = 0;
378 for (i = 0; i < argc; i++) {
379 stree = NULL;
380 cmdarg = 1;
381 r += move(argv[i], target);
385 * Show errors by nonzero exit code.
388 return (r?2:0);
391 static int
392 lnkfil(char *source, char *target)
394 char *buf = NULL;
396 if (sflg) {
399 * If target is a directory make complete
400 * name of the new symbolic link within that
401 * directory.
404 if ((stat(target, &s2) >= 0) && ISDIR(s2)) {
405 size_t len;
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);
412 exit(3);
414 (void) snprintf(buf, len, "%s/%s",
415 target, dname(source));
416 target = buf;
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.
433 if (silent) {
435 * Don't allow silent (-f) removal of an existing
436 * directory; could leave unreferenced directory
437 * entries.
439 if (ISDIR(s2)) {
440 (void) fprintf(stderr,
441 gettext("%s: cannot create link "
442 "over directory %s\n"), cmd,
443 target);
444 return (1);
446 if (unlink(target) < 0) {
447 (void) fprintf(stderr,
448 gettext("%s: cannot unlink %s: "),
449 cmd, target);
450 perror("");
451 return (1);
458 * Create a symbolic link to the source.
461 if (symlink(source, target) < 0) {
462 (void) fprintf(stderr,
463 gettext("%s: cannot create %s: "),
464 cmd, target);
465 perror("");
466 free(buf);
467 return (1);
469 free(buf);
470 return (0);
473 switch (chkfiles(source, &target)) {
474 case 1: return (1);
475 case 2: return (0);
476 /* default - fall through */
480 * Make sure source file is not a directory,
481 * we cannot link directories...
484 if (ISDIR(s1)) {
485 (void) fprintf(stderr,
486 gettext("%s: %s is a directory\n"), cmd, source);
487 return (1);
491 * hard link, call link() and return.
494 if (link(source, target) < 0) {
495 if (errno == EXDEV)
496 (void) fprintf(stderr,
497 gettext("%s: %s is on a different file system\n"),
498 cmd, target);
499 else {
500 (void) fprintf(stderr,
501 gettext("%s: cannot create link %s: "),
502 cmd, target);
503 perror("");
505 free(buf);
506 return (1);
507 } else {
508 free(buf);
509 return (0);
513 static int
514 cpymve(char *source, char *target)
516 int n;
517 int fi, fo;
518 int ret = 0;
519 int attret = 0;
520 int sattret = 0;
521 int errno_save;
522 int error = 0;
524 switch (chkfiles(source, &target)) {
525 case 1: return (1);
526 case 2: return (0);
527 /* default - fall through */
531 * If it's a recursive copy and source
532 * is a directory, then call rcopy (from copydir).
534 if (cpy) {
535 if (ISDIR(s1)) {
536 int rc;
537 avl_index_t where = 0;
538 tree_node_t *tnode;
539 tree_node_t *tptr;
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) {
549 if (rc == 0) {
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"),
559 cmd, source);
560 } else {
561 Perror(source);
563 return (1);
566 cmdarg = 0;
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
575 * the hierarchy.
577 if ((tnode = create_tnode(save_dev,
578 save_ino)) == NULL) {
579 Perror(source);
580 return (1);
582 if ((tptr = avl_find(stree, tnode, &where)) != NULL) {
583 avl_remove(stree, tptr);
585 free(tptr);
586 free(tnode);
587 return (rc);
589 } else if (ISDEV(s1) && Rflg) {
590 return (copyspecial(target));
591 } else {
592 goto copy;
596 if (mve) {
597 if (rename(source, target) >= 0)
598 return (0);
599 if (errno != EXDEV) {
600 if (errno == ENOTDIR && ISDIR(s1)) {
601 (void) fprintf(stderr,
602 gettext("%s: %s is a directory\n"),
603 cmd, source);
604 return (1);
606 (void) fprintf(stderr,
607 gettext("%s: cannot rename %s to %s: "),
608 cmd, source, target);
609 perror("");
610 return (1);
614 * cannot move a non-directory (source) onto an existing
615 * directory (target)
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);
623 return (1);
625 if (ISDIR(s1)) {
626 #ifdef XPG4
627 if (targetexists && ISDIR(s2)) {
628 /* existing target dir must be empty */
629 if (rmdir(target) < 0) {
630 errno_save = errno;
631 (void) fprintf(stderr,
632 gettext("%s: cannot rmdir %s: "),
633 cmd, target);
634 errno = errno_save;
635 perror("");
636 return (1);
639 #endif
640 if ((n = copydir(source, target)) == 0)
641 (void) rmdir(source);
642 return (n);
645 /* doors cannot be moved across filesystems */
646 if (ISDOOR(s1)) {
647 (void) fprintf(stderr,
648 gettext("%s: %s: cannot move door "
649 "across file systems\n"), cmd, source);
650 return (1);
653 /* sockets cannot be moved across filesystems */
654 if (ISSOCK(s1)) {
655 (void) fprintf(stderr,
656 gettext("%s: %s: cannot move socket "
657 "across file systems\n"), cmd, source);
658 return (1);
662 * File cannot be renamed, try to recreate the symbolic
663 * link or special device, or copy the file wholesale
664 * between file systems.
666 if (ISLNK(s1)) {
667 register int m;
668 register mode_t md;
669 char symln[PATH_MAX + 1];
671 if (targetexists && unlink(target) < 0) {
672 (void) fprintf(stderr,
673 gettext("%s: cannot unlink %s: "),
674 cmd, target);
675 perror("");
676 return (1);
679 if ((m = readlink(source, symln,
680 sizeof (symln) - 1)) < 0) {
681 Perror(source);
682 return (1);
684 symln[m] = '\0';
686 md = umask(~(s1.st_mode & MODEBITS));
687 if (symlink(symln, target) < 0) {
688 Perror(target);
689 return (1);
691 (void) umask(md);
692 m = lchown(target, UID(s1), GID(s1));
693 #ifdef XPG4
694 if (m < 0) {
695 (void) fprintf(stderr, gettext("%s: cannot"
696 " change owner and group of"
697 " %s: "), cmd, target);
698 perror("");
700 #endif
701 goto cleanup;
703 if (ISDEV(s1)) {
705 if (targetexists && unlink(target) < 0) {
706 (void) fprintf(stderr,
707 gettext("%s: cannot unlink %s: "),
708 cmd, target);
709 perror("");
710 return (1);
713 if (mknod(target, s1.st_mode, s1.st_rdev) < 0) {
714 Perror(target);
715 return (1);
718 (void) chg_mode(target, UID(s1), GID(s1), FMODE(s1));
719 (void) chg_time(target, s1);
720 goto cleanup;
723 if (ISREG(s1)) {
724 if (ISDIR(s2)) {
725 if (targetexists && rmdir(target) < 0) {
726 (void) fprintf(stderr,
727 gettext("%s: cannot rmdir %s: "),
728 cmd, target);
729 perror("");
730 return (1);
732 } else {
733 if (targetexists && unlink(target) < 0) {
734 (void) fprintf(stderr,
735 gettext("%s: cannot unlink %s: "),
736 cmd, target);
737 perror("");
738 return (1);
743 copy:
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))) {
753 int m;
754 mode_t md;
755 char symln[PATH_MAX + 1];
757 m = readlink(source, symln, sizeof (symln) - 1);
759 if (m < 0) {
760 Perror(source);
761 return (1);
763 symln[m] = '\0';
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) {
774 Perror(target);
775 return (1);
777 (void) umask(md);
778 m = lchown(target, UID(s1), GID(s1));
780 if (m < 0) {
781 (void) fprintf(stderr, gettext(
782 "cp: cannot change owner and "
783 "group of %s:"), target);
784 perror("");
786 } else {
788 * Copy the file. If it happens to be a
789 * symlink, copy the file referenced
790 * by the symlink.
792 fi = open(source, O_RDONLY);
793 if (fi < 0) {
794 (void) fprintf(stderr,
795 gettext("%s: cannot open %s: "),
796 cmd, source);
797 perror("");
798 return (1);
801 fo = creat(target, s1.st_mode & MODEBITS);
802 if (fo < 0) {
804 * If -f and creat() failed, unlink
805 * and try again.
807 if (fflg) {
808 (void) unlink(target);
809 fo = creat(target,
810 s1.st_mode & MODEBITS);
813 if (fo < 0) {
814 (void) fprintf(stderr,
815 gettext("%s: cannot create %s: "),
816 cmd, target);
817 perror("");
818 (void) close(fi);
819 return (1);
820 } else {
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
835 if (pflg || mve) {
836 (void) chmod(target, FMODE(s1));
837 if (s1acl != NULL) {
838 if ((acl_set(target,
839 s1acl)) < 0) {
840 error++;
841 (void) fprintf(stderr,
842 gettext("%s: "
843 "Failed to set "
844 "acl entries "
845 "on %s\n"), cmd,
846 target);
847 acl_free(s1acl);
848 s1acl = NULL;
850 * else: silent and
851 * continue
857 if (fstat(fi, &s1) < 0) {
858 (void) fprintf(stderr,
859 gettext("%s: cannot access %s\n"),
860 cmd, source);
861 return (1);
863 if (IDENTICAL(s1, s2)) {
864 (void) fprintf(stderr,
865 gettext(
866 "%s: %s and %s are identical\n"),
867 cmd, source, target);
868 return (1);
871 if (writefile(fi, fo, source, target, NULL,
872 NULL, &s1, &s2) != 0) {
873 return (1);
876 (void) close(fi);
877 if (close(fo) < 0) {
878 Perror2(target, "write");
879 return (1);
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);
896 return (1);
898 if (attrsilent) {
899 attret = 0;
904 * XPG4: the write system call will clear setgid
905 * and setuid bits, so set them again.
907 if (pflg || mve) {
908 if ((ret = chg_mode(target, UID(s1), GID(s1),
909 FMODE(s1))) > 0)
910 return (1);
912 * Reapply ACL, since chmod may have
913 * altered ACL
915 if (s1acl != NULL) {
916 if ((acl_set(target, s1acl)) < 0) {
917 error++;
918 (void) fprintf(stderr,
919 gettext("%s: Failed to "
920 "set acl entries "
921 "on %s\n"), cmd, target);
923 * else: silent and
924 * continue
928 if ((ret = chg_time(target, s1)) > 0)
929 return (1);
931 if (cpy) {
932 if (error != 0 || attret != 0 || sattret != 0)
933 return (1);
934 return (0);
936 goto cleanup;
938 (void) fprintf(stderr,
939 gettext("%s: %s: unknown file type 0x%x\n"), cmd,
940 source, (s1.st_mode & S_IFMT));
941 return (1);
943 cleanup:
944 if (unlink(source) < 0) {
945 (void) unlink(target);
946 (void) fprintf(stderr,
947 gettext("%s: cannot unlink %s: "),
948 cmd, source);
949 perror("");
950 return (1);
952 if (error != 0 || attret != 0 || sattret != 0)
953 return (1);
954 return (ret);
956 /*NOTREACHED*/
957 return (ret);
961 * create_tnode()
963 * Create a node for use with the search tree which contains the
964 * inode information (device id and inode number).
966 * Input
967 * dev - device id
968 * ino - inode number
970 * Output
971 * tnode - NULL on error, otherwise returns a tnode structure
972 * which contains the input device id and inode number.
974 static tree_node_t *
975 create_tnode(dev_t dev, ino_t ino)
977 tree_node_t *tnode;
979 if ((tnode = (tree_node_t *)malloc(sizeof (tree_node_t))) != NULL) {
980 tnode->node_dev = dev;
981 tnode->node_ino = ino;
984 return (tnode);
987 static int
988 chkfiles(char *source, char **to)
990 char *buf = NULL;
991 int (*statf)() = (cpy &&
992 !(Pflg || (Hflg && !cmdarg))) ? stat : lstat;
993 char *target = *to;
994 int error;
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
1003 * points to a file.
1005 if (errno == ENOTDIR)
1006 (void) fprintf(stderr, "%s: %s: %s\n", cmd, source,
1007 strerror(errno));
1008 else
1009 (void) fprintf(stderr,
1010 gettext("%s: cannot access %s\n"), cmd, source);
1011 return (1);
1015 * Get ACL info: don't bother with ln or cp/mv'ing symlinks
1017 if (!lnk && !ISLNK(s1)) {
1018 if (s1acl != NULL) {
1019 acl_free(s1acl);
1020 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));
1026 return (1);
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;
1037 targetexists = 0;
1038 if ((*statf)(target, &s2) >= 0) {
1039 if (ISLNK(s2))
1040 (void) stat(target, &s2);
1042 * If target is a directory,
1043 * make complete name of new file
1044 * within that directory.
1046 if (ISDIR(s2)) {
1047 size_t len;
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);
1054 exit(3);
1056 (void) snprintf(buf, len, "%s/%s",
1057 target, dname(source));
1058 *to = target = buf;
1061 if ((*statf)(target, &s2) >= 0) {
1062 int overwrite = FALSE;
1063 int override = FALSE;
1065 targetexists++;
1066 if (cpy || mve) {
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,
1078 gettext(
1079 "%s: %s and %s are identical\n"),
1080 cmd, source, target);
1081 free(buf);
1082 return (1);
1085 if (lnk) {
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)) {
1093 free(buf);
1094 return (1);
1097 if (lnk && !silent) {
1098 (void) fprintf(stderr,
1099 gettext("%s: %s: File exists\n"),
1100 cmd, target);
1101 free(buf);
1102 return (1);
1106 * overwrite:
1107 * If the user does not have access to
1108 * the target, ask ----if it is not
1109 * silent and user invoked command
1110 * interactively.
1112 * override:
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);
1140 if (yes() == 0) {
1141 free(buf);
1142 return (2);
1144 } else if (overwrite && ISREG(s2)) {
1145 (void) fprintf(stderr,
1146 gettext("%s: overwrite %s? "),
1147 cmd, target);
1148 if (yes() == 0) {
1149 free(buf);
1150 return (2);
1152 } else if (override) {
1153 (void) fprintf(stderr,
1154 gettext("%s: %s: override protection %o? "),
1155 cmd, target, FMODE(s2) & MODEBITS);
1156 if (yes() == 0) {
1157 free(buf);
1158 return (2);
1162 if (lnk && unlink(target) < 0) {
1163 (void) fprintf(stderr,
1164 gettext("%s: cannot unlink %s: "),
1165 cmd, target);
1166 perror("");
1167 return (1);
1171 return (0);
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
1179 static int
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)) {
1191 return (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);
1197 return (0);
1200 return (1);
1204 * get real path (resolved absolute pathname)
1205 * return 1 on success, 0 on failure
1207 static int
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);
1214 errno = errno_save;
1215 perror("");
1216 return (0);
1218 return (1);
1221 static int
1222 rcopy(char *from, char *to)
1224 DIR *fold = opendir(from);
1225 struct dirent *dp;
1226 struct stat statb, s1save;
1227 int errs = 0;
1228 char fromname[PATH_MAX];
1230 if (fold == 0 || ((pflg || mve) && fstat(fold->dd_fd, &statb) < 0)) {
1231 Perror(from);
1232 return (1);
1234 if (pflg || mve) {
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.
1240 s1save = s1;
1242 for (;;) {
1243 dp = readdir(fold);
1244 if (dp == 0) {
1245 (void) closedir(fold);
1246 if (pflg || mve)
1247 return (chg_time(to, s1save) + errs);
1248 return (errs);
1250 if (dp->d_ino == 0)
1251 continue;
1252 if ((strcmp(dp->d_name, ".") == 0) ||
1253 (strcmp(dp->d_name, "..") == 0))
1254 continue;
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);
1260 errs++;
1261 continue;
1263 (void) snprintf(fromname, sizeof (fromname),
1264 "%s/%s", from, dp->d_name);
1265 errs += cpymve(fromname, to);
1269 static char *
1270 dname(char *name)
1272 register char *p;
1275 * Return just the file name given the complete path.
1276 * Like basename(1).
1279 p = name;
1282 * While there are characters left,
1283 * set name to start after last
1284 * delimiter.
1287 while (*p)
1288 if (*p++ == DELIM && *p)
1289 name = p;
1290 return (name);
1293 static void
1294 usage(void)
1297 * Display usage message.
1300 if (mve) {
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"));
1305 } else if (lnk) {
1306 #ifdef XPG4
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"));
1311 #else
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"));
1316 #endif
1317 } else if (cpy) {
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"));
1324 exit(2);
1328 * chg_time()
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.
1339 static int
1340 chg_time(char *to, struct stat ss)
1342 struct timespec times[2];
1343 #ifdef XPG4
1344 int rc;
1345 #endif
1347 times[0] = ss.st_atim;
1348 times[1] = ss.st_mtim;
1350 #ifdef XPG4
1351 rc = utimensat(AT_FDCWD, to, times,
1352 ISLNK(s1) ? AT_SYMLINK_NOFOLLOW : 0);
1353 if ((pflg || mve) && rc != 0) {
1354 (void) fprintf(stderr,
1355 gettext("%s: cannot set times for %s: "), cmd, to);
1356 perror("");
1357 if (pflg)
1358 return (1);
1360 #else
1361 (void) utimensat(AT_FDCWD, to, times,
1362 ISLNK(s1) ? AT_SYMLINK_NOFOLLOW : 0);
1363 #endif
1365 return (0);
1370 * chg_mode()
1372 * This function is called upon "cp -p" or mv across filesystems.
1374 * Try to preserve the owner and group id. If chown() fails,
1375 * only print a diagnostic message if doing a mv in the XPG4 version;
1376 * try to clear S_ISUID and S_ISGID bits in the target. If unable to clear
1377 * S_ISUID and S_ISGID bits, print a diagnostic message and arrange for a
1378 * non-zero exit status because this is a security violation.
1379 * Try to preserve permissions.
1380 * If this is the XPG4 version and chmod() fails, print a diagnostic message
1381 * and arrange for a non-zero exit status.
1382 * If this is the Solaris version and chmod() fails, do not print a
1383 * diagnostic message or exit with a non-zero value.
1385 static int
1386 chg_mode(char *target, uid_t uid, gid_t gid, mode_t mode)
1388 int clearflg = 0; /* controls message printed upon chown() error */
1389 struct stat st;
1391 /* Don't change mode if target is symlink */
1392 if (lstat(target, &st) == 0 && ISLNK(st))
1393 return (0);
1395 if (chown(target, uid, gid) != 0) {
1396 #ifdef XPG4
1397 if (mve) {
1398 (void) fprintf(stderr, gettext("%s: cannot change"
1399 " owner and group of %s: "), cmd, target);
1400 perror("");
1402 #endif
1403 if (mode & (S_ISUID | S_ISGID)) {
1404 /* try to clear S_ISUID and S_ISGID */
1405 mode &= ~S_ISUID & ~S_ISGID;
1406 ++clearflg;
1409 if (chmod(target, mode) != 0) {
1410 if (clearflg) {
1411 (void) fprintf(stderr, gettext(
1412 "%s: cannot clear S_ISUID and S_ISGID bits in"
1413 " %s: "), cmd, target);
1414 perror("");
1415 /* cp -p should get non-zero exit; mv should not */
1416 if (pflg)
1417 return (1);
1419 #ifdef XPG4
1420 else {
1421 (void) fprintf(stderr, gettext(
1422 "%s: cannot set permissions for %s: "), cmd, target);
1423 perror("");
1424 /* cp -p should get non-zero exit; mv should not */
1425 if (pflg)
1426 return (1);
1428 #endif
1430 return (0);
1434 static void
1435 Perror(char *s)
1437 char buf[PATH_MAX + 10];
1439 (void) snprintf(buf, sizeof (buf), "%s: %s", cmd, s);
1440 perror(buf);
1443 static void
1444 Perror2(char *s1, char *s2)
1446 char buf[PATH_MAX + 20];
1448 (void) snprintf(buf, sizeof (buf), "%s: %s: %s",
1449 cmd, gettext(s1), gettext(s2));
1450 perror(buf);
1454 * used for cp -R and for mv across file systems
1456 static int
1457 copydir(char *source, char *target)
1459 int ret, attret = 0;
1460 int sattret = 0;
1461 int pret = 0; /* need separate flag if -p is specified */
1462 mode_t fixmode = (mode_t)0; /* cleanup mode after copy */
1463 struct stat s1save;
1464 acl_t *s1acl_save;
1465 int error = 0;
1467 s1acl_save = NULL;
1469 if (cpy && !rflg) {
1470 (void) fprintf(stderr,
1471 gettext("%s: %s: is a directory\n"), cmd, source);
1472 return (1);
1475 if (stat(target, &s2) < 0) {
1476 if (mkdir(target, (s1.st_mode & MODEBITS)) < 0) {
1477 (void) fprintf(stderr, "%s: ", cmd);
1478 perror(target);
1479 return (1);
1481 if (stat(target, &s2) == 0) {
1482 fixmode = s2.st_mode;
1483 } else {
1484 fixmode = s1.st_mode;
1486 (void) chmod(target, ((fixmode & MODEBITS) | S_IRWXU));
1487 } else if (!(ISDIR(s2))) {
1488 (void) fprintf(stderr,
1489 gettext("%s: %s: not a directory.\n"), cmd, target);
1490 return (1);
1492 if (pflg || mve) {
1494 * Save s1 (stat information for source dir) and acl info,
1495 * if any, so that ownership, modes, times, and acl's can
1496 * be reserved during "cp -p" or mv.
1497 * s1 gets overwritten when doing the recursive copy.
1499 s1save = s1;
1500 if (s1acl != NULL) {
1501 s1acl_save = acl_dup(s1acl);
1502 if (s1acl_save == NULL) {
1503 (void) fprintf(stderr, gettext("%s: "
1504 "Insufficient memory to save acl"
1505 " entry\n"), cmd);
1506 if (pflg)
1507 return (1);
1510 #ifdef XPG4
1511 else {
1512 (void) fprintf(stderr, gettext("%s: "
1513 "Insufficient memory to save acl"
1514 " entry\n"), cmd);
1515 if (pflg)
1516 return (1);
1518 #endif
1522 ret = rcopy(source, target);
1525 * Once we created a directory, go ahead and set
1526 * its attributes, e.g. acls and time. The info
1527 * may get overwritten if we continue traversing
1528 * down the tree.
1530 * ACL for directory
1532 if (pflg || mve) {
1533 if ((pret = chg_mode(target, UID(s1save), GID(s1save),
1534 FMODE(s1save))) == 0)
1535 pret = chg_time(target, s1save);
1536 ret += pret;
1537 if (s1acl_save != NULL) {
1538 if (acl_set(target, s1acl_save) < 0) {
1539 error++;
1540 #ifdef XPG4
1541 if (pflg || mve) {
1542 #else
1543 if (pflg) {
1544 #endif
1545 (void) fprintf(stderr, gettext(
1546 "%s: failed to set acl entries "
1547 "on %s\n"), cmd, target);
1548 if (pflg) {
1549 acl_free(s1acl_save);
1550 s1acl_save = NULL;
1551 ret++;
1554 /* else: silent and continue */
1556 acl_free(s1acl_save);
1557 s1acl_save = NULL;
1559 } else if (fixmode != (mode_t)0)
1560 (void) chmod(target, fixmode & MODEBITS);
1562 if (pflg || atflg || mve || saflg) {
1563 attret = copyattributes(source, target);
1564 if (!attrsilent && attret != 0) {
1565 (void) fprintf(stderr, gettext("%s: Failed to preserve"
1566 " extended attributes of directory"
1567 " %s\n"), cmd, source);
1568 } else {
1570 * Otherwise ignore failure.
1572 attret = 0;
1574 /* Copy extended system attributes */
1575 if (pflg || mve || saflg) {
1576 sattret = copy_sysattr(source, target);
1577 if (sattret != 0) {
1578 (void) fprintf(stderr, gettext(
1579 "%s: Failed to preserve "
1580 "extended system attributes "
1581 "of directory %s\n"), cmd, source);
1585 if (attret != 0 || sattret != 0 || error != 0)
1586 return (1);
1587 return (ret);
1590 static int
1591 copyspecial(char *target)
1593 int ret = 0;
1595 if (mknod(target, s1.st_mode, s1.st_rdev) != 0) {
1596 (void) fprintf(stderr, gettext(
1597 "cp: cannot create special file %s: "), target);
1598 perror("");
1599 return (1);
1602 if (pflg) {
1603 if ((ret = chg_mode(target, UID(s1), GID(s1), FMODE(s1))) == 0)
1604 ret = chg_time(target, s1);
1607 return (ret);
1610 static int
1611 use_stdin(void)
1613 #ifdef XPG4
1614 return (1);
1615 #else
1616 return (isatty(fileno(stdin)));
1617 #endif
1620 /* Copy non-system extended attributes */
1622 static int
1623 copyattributes(char *source, char *target)
1625 struct dirent *dp;
1626 int error = 0;
1627 int aclerror;
1628 mode_t mode;
1629 int clearflg = 0;
1630 acl_t *xacl = NULL;
1631 acl_t *attrdiracl = NULL;
1632 struct timespec times[2];
1635 if (pathconf(source, _PC_XATTR_EXISTS) != 1)
1636 return (0);
1638 if (pathconf(target, _PC_XATTR_ENABLED) != 1) {
1639 if (!attrsilent) {
1640 (void) fprintf(stderr,
1641 gettext(
1642 "%s: cannot preserve extended attributes, "
1643 "operation not supported on file"
1644 " %s\n"), cmd, target);
1646 return (1);
1648 if (open_source(source) != 0)
1649 return (1);
1650 if (open_target_srctarg_attrdirs(source, target) != 0)
1651 return (1);
1652 if (open_attrdirp(source) != 0)
1653 return (1);
1655 if (pflg || mve) {
1656 if (fchmod(targetdirfd, attrdir.st_mode) == -1) {
1657 if (!attrsilent) {
1658 (void) fprintf(stderr,
1659 gettext("%s: failed to set file mode"
1660 " correctly on attribute directory of"
1661 " file %s: "), cmd, target);
1662 perror("");
1663 ++error;
1667 if (fchown(targetdirfd, attrdir.st_uid, attrdir.st_gid) == -1) {
1668 if (!attrsilent) {
1669 (void) fprintf(stderr,
1670 gettext("%s: failed to set file"
1671 " ownership correctly on attribute"
1672 " directory of file %s: "), cmd, target);
1673 perror("");
1674 ++error;
1678 * Now that we are the owner we can update st_ctime by calling
1679 * utimensat.
1681 times[0] = attrdir.st_atim;
1682 times[1] = attrdir.st_mtim;
1683 if (utimensat(targetdirfd, ".", times, 0) < 0) {
1684 if (!attrsilent) {
1685 (void) fprintf(stderr,
1686 gettext("%s: cannot set attribute times"
1687 " for %s: "), cmd, target);
1688 perror("");
1689 ++error;
1694 * Now set owner and group of attribute directory, implies
1695 * changing the ACL of the hidden attribute directory first.
1697 if ((aclerror = facl_get(sourcedirfd,
1698 ACL_NO_TRIVIAL, &attrdiracl)) != 0) {
1699 if (!attrsilent) {
1700 (void) fprintf(stderr, gettext(
1701 "%s: failed to get acl entries of"
1702 " attribute directory for"
1703 " %s : %s\n"), cmd,
1704 source, acl_strerror(aclerror));
1705 ++error;
1709 if (attrdiracl) {
1710 if (facl_set(targetdirfd, attrdiracl) != 0) {
1711 if (!attrsilent) {
1712 (void) fprintf(stderr, gettext(
1713 "%s: failed to set acl entries"
1714 " on attribute directory "
1715 "for %s\n"), cmd, target);
1716 ++error;
1718 acl_free(attrdiracl);
1719 attrdiracl = NULL;
1724 while ((dp = readdir(srcdirp)) != NULL) {
1725 int ret;
1727 if ((ret = traverse_attrfile(dp, source, target, 1)) == -1)
1728 continue;
1729 else if (ret > 0) {
1730 ++error;
1731 goto out;
1734 if (pflg || mve) {
1735 if ((aclerror = facl_get(srcattrfd,
1736 ACL_NO_TRIVIAL, &xacl)) != 0) {
1737 if (!attrsilent) {
1738 (void) fprintf(stderr, gettext(
1739 "%s: failed to get acl entries of"
1740 " attribute %s for"
1741 " %s: %s"), cmd, dp->d_name,
1742 source, acl_strerror(aclerror));
1743 ++error;
1749 * preserve ACL
1751 if ((pflg || mve) && xacl != NULL) {
1752 if ((facl_set(targattrfd, xacl)) < 0) {
1753 if (!attrsilent) {
1754 (void) fprintf(stderr, gettext(
1755 "%s: failed to set acl entries on"
1756 " attribute %s for"
1757 "%s\n"), cmd, dp->d_name, target);
1758 ++error;
1760 acl_free(xacl);
1761 xacl = NULL;
1765 if (writefile(srcattrfd, targattrfd, source, target,
1766 dp->d_name, dp->d_name, &s3, &s4) != 0) {
1767 if (!attrsilent) {
1768 ++error;
1770 goto next;
1773 if (pflg || mve) {
1774 mode = FMODE(s3);
1776 if (fchown(targattrfd, UID(s3), GID(s3)) != 0) {
1777 if (!attrsilent) {
1778 (void) fprintf(stderr,
1779 gettext("%s: cannot change"
1780 " owner and group of"
1781 " attribute %s for" " file"
1782 " %s: "), cmd, dp->d_name, target);
1783 perror("");
1784 ++error;
1786 if (mode & (S_ISUID | S_ISGID)) {
1787 /* try to clear S_ISUID and S_ISGID */
1788 mode &= ~S_ISUID & ~S_ISGID;
1789 ++clearflg;
1792 times[0] = s3.st_atim;
1793 times[1] = s3.st_mtim;
1794 if (utimensat(targetdirfd, dp->d_name, times, 0) < 0) {
1795 if (!attrsilent) {
1796 (void) fprintf(stderr,
1797 gettext("%s: cannot set attribute"
1798 " times for %s: "), cmd, target);
1799 perror("");
1800 ++error;
1803 if (fchmod(targattrfd, mode) != 0) {
1804 if (clearflg) {
1805 (void) fprintf(stderr, gettext(
1806 "%s: cannot clear S_ISUID and "
1807 "S_ISGID bits in attribute %s"
1808 " for file"
1809 " %s: "), cmd, dp->d_name, target);
1810 } else {
1811 if (!attrsilent) {
1812 (void) fprintf(stderr,
1813 gettext(
1814 "%s: cannot set permissions of attribute"
1815 " %s for %s: "), cmd, dp->d_name, target);
1816 perror("");
1817 ++error;
1821 if (xacl && ((facl_set(targattrfd, xacl)) < 0)) {
1822 if (!attrsilent) {
1823 (void) fprintf(stderr, gettext(
1824 "%s: failed to set acl entries on"
1825 " attribute %s for"
1826 "%s\n"), cmd, dp->d_name, target);
1827 ++error;
1829 acl_free(xacl);
1830 xacl = NULL;
1833 next:
1834 if (xacl != NULL) {
1835 acl_free(xacl);
1836 xacl = NULL;
1838 if (srcattrfd != -1)
1839 (void) close(srcattrfd);
1840 if (targattrfd != -1)
1841 (void) close(targattrfd);
1842 srcattrfd = targattrfd = -1;
1844 out:
1845 if (xacl != NULL) {
1846 acl_free(xacl);
1847 xacl = NULL;
1849 if (attrdiracl != NULL) {
1850 acl_free(attrdiracl);
1851 attrdiracl = NULL;
1854 if (!saflg && !pflg && !mve)
1855 close_all();
1856 return (error == 0 ? 0 : 1);
1859 /* Copy extended system attributes from source to target */
1861 static int
1862 copy_sysattr(char *source, char *target)
1864 struct dirent *dp;
1865 nvlist_t *response;
1866 int error = 0;
1867 int target_sa_support = 0;
1869 if (sysattr_support(source, _PC_SATTR_EXISTS) != 1)
1870 return (0);
1872 if (open_source(source) != 0)
1873 return (1);
1876 * Gets non default extended system attributes from the
1877 * source file to copy to the target. The target has
1878 * the defaults set when its created and thus no need
1879 * to copy the defaults.
1881 response = sysattr_list(cmd, srcfd, source);
1883 if (sysattr_support(target, _PC_SATTR_ENABLED) != 1) {
1884 if (response != NULL) {
1885 (void) fprintf(stderr,
1886 gettext(
1887 "%s: cannot preserve extended system "
1888 "attribute, operation not supported on file"
1889 " %s\n"), cmd, target);
1890 error++;
1891 goto out;
1893 } else {
1894 target_sa_support = 1;
1897 if (target_sa_support) {
1898 if (srcdirp == NULL) {
1899 if (open_target_srctarg_attrdirs(source,
1900 target) != 0) {
1901 error++;
1902 goto out;
1904 if (open_attrdirp(source) != 0) {
1905 error++;
1906 goto out;
1908 } else {
1909 rewind_attrdir(srcdirp);
1911 while ((dp = readdir(srcdirp)) != NULL) {
1912 nvlist_t *res;
1913 int ret;
1915 if ((ret = traverse_attrfile(dp, source, target,
1916 0)) == -1)
1917 continue;
1918 else if (ret > 0) {
1919 ++error;
1920 goto out;
1923 * Gets non default extended system attributes from the
1924 * attribute file to copy to the target. The target has
1925 * the defaults set when its created and thus no need
1926 * to copy the defaults.
1928 if (dp->d_name != NULL) {
1929 res = sysattr_list(cmd, srcattrfd, dp->d_name);
1930 if (res == NULL)
1931 goto next;
1934 * Copy non default extended system attributes of named
1935 * attribute file.
1937 if (fsetattr(targattrfd,
1938 XATTR_VIEW_READWRITE, res) != 0) {
1939 ++error;
1940 (void) fprintf(stderr, gettext("%s: "
1941 "Failed to copy extended system "
1942 "attributes from attribute file "
1943 "%s of %s to %s\n"), cmd,
1944 dp->d_name, source, target);
1947 next:
1948 if (srcattrfd != -1)
1949 (void) close(srcattrfd);
1950 if (targattrfd != -1)
1951 (void) close(targattrfd);
1952 srcattrfd = targattrfd = -1;
1953 nvlist_free(res);
1956 /* Copy source file non default extended system attributes to target */
1957 if (target_sa_support && (response != NULL) &&
1958 (fsetattr(targfd, XATTR_VIEW_READWRITE, response)) != 0) {
1959 ++error;
1960 (void) fprintf(stderr, gettext("%s: Failed to "
1961 "copy extended system attributes from "
1962 "%s to %s\n"), cmd, source, target);
1964 out:
1965 nvlist_free(response);
1966 close_all();
1967 return (error == 0 ? 0 : 1);
1970 /* Open the source file */
1973 open_source(char *src)
1975 int error = 0;
1977 srcfd = -1;
1978 if ((srcfd = open(src, O_RDONLY)) == -1) {
1979 if (pflg && attrsilent) {
1980 error++;
1981 goto out;
1983 if (!attrsilent) {
1984 (void) fprintf(stderr,
1985 gettext("%s: cannot open file"
1986 " %s: "), cmd, src);
1987 perror("");
1989 ++error;
1991 out:
1992 if (error)
1993 close_all();
1994 return (error == 0 ? 0 : 1);
1997 /* Open source attribute dir, target and target attribute dir. */
2000 open_target_srctarg_attrdirs(char *src, char *targ)
2002 int error = 0;
2004 targfd = sourcedirfd = targetdirfd = -1;
2006 if ((targfd = open(targ, O_RDONLY)) == -1) {
2007 if (pflg && attrsilent) {
2008 error++;
2009 goto out;
2011 if (!attrsilent) {
2012 (void) fprintf(stderr,
2013 gettext("%s: cannot open file"
2014 " %s: "), cmd, targ);
2015 perror("");
2017 ++error;
2018 goto out;
2021 if ((sourcedirfd = openat(srcfd, ".", O_RDONLY|O_XATTR)) == -1) {
2022 if (pflg && attrsilent) {
2023 error++;
2024 goto out;
2026 if (!attrsilent) {
2027 (void) fprintf(stderr,
2028 gettext("%s: cannot open attribute"
2029 " directory for %s: "), cmd, src);
2030 perror("");
2032 ++error;
2033 goto out;
2036 if (fstat(sourcedirfd, &attrdir) == -1) {
2037 if (pflg && attrsilent) {
2038 error++;
2039 goto out;
2042 if (!attrsilent) {
2043 (void) fprintf(stderr,
2044 gettext("%s: could not retrieve stat"
2045 " information for attribute directory"
2046 "of file %s: "), cmd, src);
2047 perror("");
2049 ++error;
2050 goto out;
2052 if ((targetdirfd = openat(targfd, ".", O_RDONLY|O_XATTR)) == -1) {
2053 if (pflg && attrsilent) {
2054 error++;
2055 goto out;
2057 if (!attrsilent) {
2058 (void) fprintf(stderr,
2059 gettext("%s: cannot open attribute"
2060 " directory for %s: "), cmd, targ);
2061 perror("");
2063 ++error;
2065 out:
2066 if (error)
2067 close_all();
2068 return (error == 0 ? 0 : 1);
2072 open_attrdirp(char *source)
2074 int tmpfd = -1;
2075 int error = 0;
2078 * dup sourcedirfd for use by fdopendir().
2079 * fdopendir will take ownership of given fd and will close
2080 * it when closedir() is called.
2083 if ((tmpfd = dup(sourcedirfd)) == -1) {
2084 if (pflg && attrsilent) {
2085 error++;
2086 goto out;
2088 if (!attrsilent) {
2089 (void) fprintf(stderr,
2090 gettext(
2091 "%s: unable to dup attribute directory"
2092 " file descriptor for %s: "), cmd, source);
2093 perror("");
2094 ++error;
2096 goto out;
2098 if ((srcdirp = fdopendir(tmpfd)) == NULL) {
2099 if (pflg && attrsilent) {
2100 error++;
2101 goto out;
2103 if (!attrsilent) {
2104 (void) fprintf(stderr,
2105 gettext("%s: failed to open attribute"
2106 " directory for %s: "), cmd, source);
2107 perror("");
2108 ++error;
2111 out:
2112 if (error)
2113 close_all();
2114 return (error == 0 ? 0 : 1);
2117 /* Skips through ., .., and system attribute 'view' files */
2119 traverse_attrfile(struct dirent *dp, char *source, char *target, int first)
2121 int error = 0;
2123 srcattrfd = targattrfd = -1;
2125 if ((dp->d_name[0] == '.' && dp->d_name[1] == '\0') ||
2126 (dp->d_name[0] == '.' && dp->d_name[1] == '.' &&
2127 dp->d_name[2] == '\0') ||
2128 (sysattr_type(dp->d_name) == _RO_SATTR) ||
2129 (sysattr_type(dp->d_name) == _RW_SATTR))
2130 return (-1);
2132 if ((srcattrfd = openat(sourcedirfd, dp->d_name,
2133 O_RDONLY)) == -1) {
2134 if (!attrsilent) {
2135 (void) fprintf(stderr,
2136 gettext("%s: cannot open attribute %s on"
2137 " file %s: "), cmd, dp->d_name, source);
2138 perror("");
2139 ++error;
2140 goto out;
2144 if (fstat(srcattrfd, &s3) < 0) {
2145 if (!attrsilent) {
2146 (void) fprintf(stderr,
2147 gettext("%s: could not stat attribute"
2148 " %s on file"
2149 " %s: "), cmd, dp->d_name, source);
2150 perror("");
2151 ++error;
2153 goto out;
2156 if (first) {
2157 (void) unlinkat(targetdirfd, dp->d_name, 0);
2158 if ((targattrfd = openat(targetdirfd, dp->d_name,
2159 O_RDWR|O_CREAT|O_TRUNC, s3.st_mode & MODEBITS)) == -1) {
2160 if (!attrsilent) {
2161 (void) fprintf(stderr,
2162 gettext("%s: could not create attribute"
2163 " %s on file %s: "), cmd, dp->d_name,
2164 target);
2165 perror("");
2166 ++error;
2168 goto out;
2170 } else {
2171 if ((targattrfd = openat(targetdirfd, dp->d_name,
2172 O_RDONLY)) == -1) {
2173 if (!attrsilent) {
2174 (void) fprintf(stderr,
2175 gettext("%s: could not open attribute"
2176 " %s on file %s: "), cmd, dp->d_name,
2177 target);
2178 perror("");
2179 ++error;
2181 goto out;
2186 if (fstat(targattrfd, &s4) < 0) {
2187 if (!attrsilent) {
2188 (void) fprintf(stderr,
2189 gettext("%s: could not stat attribute"
2190 " %s on file"
2191 " %s: "), cmd, dp->d_name, target);
2192 perror("");
2193 ++error;
2197 out:
2198 if (error) {
2199 if (srcattrfd != -1)
2200 (void) close(srcattrfd);
2201 if (targattrfd != -1)
2202 (void) close(targattrfd);
2203 srcattrfd = targattrfd = -1;
2205 return (error == 0 ? 0 :1);
2208 void
2209 rewind_attrdir(DIR * sdp)
2211 int pwdfd;
2213 pwdfd = open(".", O_RDONLY);
2214 if ((pwdfd != -1) && (fchdir(sourcedirfd) == 0)) {
2215 rewinddir(sdp);
2216 (void) fchdir(pwdfd);
2217 (void) close(pwdfd);
2218 } else {
2219 if (!attrsilent) {
2220 (void) fprintf(stderr, gettext("%s: "
2221 "failed to rewind attribute dir\n"),
2222 cmd);
2227 void
2228 close_all()
2230 if (srcattrfd != -1)
2231 (void) close(srcattrfd);
2232 if (targattrfd != -1)
2233 (void) close(targattrfd);
2234 if (sourcedirfd != -1)
2235 (void) close(sourcedirfd);
2236 if (targetdirfd != -1)
2237 (void) close(targetdirfd);
2238 if (srcdirp != NULL) {
2239 (void) closedir(srcdirp);
2240 srcdirp = NULL;
2242 if (srcfd != -1)
2243 (void) close(srcfd);
2244 if (targfd != -1)
2245 (void) close(targfd);