add UNLEASHED_OBJ to unleashed.mk
[unleashed/tickless.git] / usr / src / cmd / mv / mv.c
blob7e263cdb330c12396cd5992b9582682cd23de2e3
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 int rc;
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);
1350 #ifdef XPG4
1351 if ((pflg || mve) && rc != 0) {
1352 (void) fprintf(stderr,
1353 gettext("%s: cannot set times for %s: "), cmd, to);
1354 perror("");
1355 if (pflg)
1356 return (1);
1358 #endif
1360 return (0);
1365 * chg_mode()
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.
1380 static int
1381 chg_mode(char *target, uid_t uid, gid_t gid, mode_t mode)
1383 int clearflg = 0; /* controls message printed upon chown() error */
1384 struct stat st;
1386 /* Don't change mode if target is symlink */
1387 if (lstat(target, &st) == 0 && ISLNK(st))
1388 return (0);
1390 if (chown(target, uid, gid) != 0) {
1391 #ifdef XPG4
1392 if (mve) {
1393 (void) fprintf(stderr, gettext("%s: cannot change"
1394 " owner and group of %s: "), cmd, target);
1395 perror("");
1397 #endif
1398 if (mode & (S_ISUID | S_ISGID)) {
1399 /* try to clear S_ISUID and S_ISGID */
1400 mode &= ~S_ISUID & ~S_ISGID;
1401 ++clearflg;
1404 if (chmod(target, mode) != 0) {
1405 if (clearflg) {
1406 (void) fprintf(stderr, gettext(
1407 "%s: cannot clear S_ISUID and S_ISGID bits in"
1408 " %s: "), cmd, target);
1409 perror("");
1410 /* cp -p should get non-zero exit; mv should not */
1411 if (pflg)
1412 return (1);
1414 #ifdef XPG4
1415 else {
1416 (void) fprintf(stderr, gettext(
1417 "%s: cannot set permissions for %s: "), cmd, target);
1418 perror("");
1419 /* cp -p should get non-zero exit; mv should not */
1420 if (pflg)
1421 return (1);
1423 #endif
1425 return (0);
1429 static void
1430 Perror(char *s)
1432 char buf[PATH_MAX + 10];
1434 (void) snprintf(buf, sizeof (buf), "%s: %s", cmd, s);
1435 perror(buf);
1438 static void
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));
1445 perror(buf);
1449 * used for cp -R and for mv across file systems
1451 static int
1452 copydir(char *source, char *target)
1454 int ret, attret = 0;
1455 int sattret = 0;
1456 int pret = 0; /* need separate flag if -p is specified */
1457 mode_t fixmode = (mode_t)0; /* cleanup mode after copy */
1458 struct stat s1save;
1459 acl_t *s1acl_save;
1460 int error = 0;
1462 s1acl_save = NULL;
1464 if (cpy && !rflg) {
1465 (void) fprintf(stderr,
1466 gettext("%s: %s: is a directory\n"), cmd, source);
1467 return (1);
1470 if (stat(target, &s2) < 0) {
1471 if (mkdir(target, (s1.st_mode & MODEBITS)) < 0) {
1472 (void) fprintf(stderr, "%s: ", cmd);
1473 perror(target);
1474 return (1);
1476 if (stat(target, &s2) == 0) {
1477 fixmode = s2.st_mode;
1478 } else {
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);
1485 return (1);
1487 if (pflg || mve) {
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.
1494 s1save = s1;
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"
1500 " entry\n"), cmd);
1501 if (pflg)
1502 return (1);
1505 #ifdef XPG4
1506 else {
1507 (void) fprintf(stderr, gettext("%s: "
1508 "Insufficient memory to save acl"
1509 " entry\n"), cmd);
1510 if (pflg)
1511 return (1);
1513 #endif
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
1523 * down the tree.
1525 * ACL for directory
1527 if (pflg || mve) {
1528 if ((pret = chg_mode(target, UID(s1save), GID(s1save),
1529 FMODE(s1save))) == 0)
1530 pret = chg_time(target, s1save);
1531 ret += pret;
1532 if (s1acl_save != NULL) {
1533 if (acl_set(target, s1acl_save) < 0) {
1534 error++;
1535 #ifdef XPG4
1536 if (pflg || mve) {
1537 #else
1538 if (pflg) {
1539 #endif
1540 (void) fprintf(stderr, gettext(
1541 "%s: failed to set acl entries "
1542 "on %s\n"), cmd, target);
1543 if (pflg) {
1544 acl_free(s1acl_save);
1545 s1acl_save = NULL;
1546 ret++;
1549 /* else: silent and continue */
1551 acl_free(s1acl_save);
1552 s1acl_save = NULL;
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);
1563 } else {
1565 * Otherwise ignore failure.
1567 attret = 0;
1569 /* Copy extended system attributes */
1570 if (pflg || mve || saflg) {
1571 sattret = copy_sysattr(source, target);
1572 if (sattret != 0) {
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)
1581 return (1);
1582 return (ret);
1585 static int
1586 copyspecial(char *target)
1588 int ret = 0;
1590 if (mknod(target, s1.st_mode, s1.st_rdev) != 0) {
1591 (void) fprintf(stderr, gettext(
1592 "cp: cannot create special file %s: "), target);
1593 perror("");
1594 return (1);
1597 if (pflg) {
1598 if ((ret = chg_mode(target, UID(s1), GID(s1), FMODE(s1))) == 0)
1599 ret = chg_time(target, s1);
1602 return (ret);
1605 static int
1606 use_stdin(void)
1608 #ifdef XPG4
1609 return (1);
1610 #else
1611 return (isatty(fileno(stdin)));
1612 #endif
1615 /* Copy non-system extended attributes */
1617 static int
1618 copyattributes(char *source, char *target)
1620 struct dirent *dp;
1621 int error = 0;
1622 int aclerror;
1623 mode_t mode;
1624 int clearflg = 0;
1625 acl_t *xacl = NULL;
1626 acl_t *attrdiracl = NULL;
1627 struct timespec times[2];
1630 if (pathconf(source, _PC_XATTR_EXISTS) != 1)
1631 return (0);
1633 if (pathconf(target, _PC_XATTR_ENABLED) != 1) {
1634 if (!attrsilent) {
1635 (void) fprintf(stderr,
1636 gettext(
1637 "%s: cannot preserve extended attributes, "
1638 "operation not supported on file"
1639 " %s\n"), cmd, target);
1641 return (1);
1643 if (open_source(source) != 0)
1644 return (1);
1645 if (open_target_srctarg_attrdirs(source, target) != 0)
1646 return (1);
1647 if (open_attrdirp(source) != 0)
1648 return (1);
1650 if (pflg || mve) {
1651 if (fchmod(targetdirfd, attrdir.st_mode) == -1) {
1652 if (!attrsilent) {
1653 (void) fprintf(stderr,
1654 gettext("%s: failed to set file mode"
1655 " correctly on attribute directory of"
1656 " file %s: "), cmd, target);
1657 perror("");
1658 ++error;
1662 if (fchown(targetdirfd, attrdir.st_uid, attrdir.st_gid) == -1) {
1663 if (!attrsilent) {
1664 (void) fprintf(stderr,
1665 gettext("%s: failed to set file"
1666 " ownership correctly on attribute"
1667 " directory of file %s: "), cmd, target);
1668 perror("");
1669 ++error;
1673 * Now that we are the owner we can update st_ctime by calling
1674 * utimensat.
1676 times[0] = attrdir.st_atim;
1677 times[1] = attrdir.st_mtim;
1678 if (utimensat(targetdirfd, ".", times, 0) < 0) {
1679 if (!attrsilent) {
1680 (void) fprintf(stderr,
1681 gettext("%s: cannot set attribute times"
1682 " for %s: "), cmd, target);
1683 perror("");
1684 ++error;
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) {
1694 if (!attrsilent) {
1695 (void) fprintf(stderr, gettext(
1696 "%s: failed to get acl entries of"
1697 " attribute directory for"
1698 " %s : %s\n"), cmd,
1699 source, acl_strerror(aclerror));
1700 ++error;
1704 if (attrdiracl) {
1705 if (facl_set(targetdirfd, attrdiracl) != 0) {
1706 if (!attrsilent) {
1707 (void) fprintf(stderr, gettext(
1708 "%s: failed to set acl entries"
1709 " on attribute directory "
1710 "for %s\n"), cmd, target);
1711 ++error;
1713 acl_free(attrdiracl);
1714 attrdiracl = NULL;
1719 while ((dp = readdir(srcdirp)) != NULL) {
1720 int ret;
1722 if ((ret = traverse_attrfile(dp, source, target, 1)) == -1)
1723 continue;
1724 else if (ret > 0) {
1725 ++error;
1726 goto out;
1729 if (pflg || mve) {
1730 if ((aclerror = facl_get(srcattrfd,
1731 ACL_NO_TRIVIAL, &xacl)) != 0) {
1732 if (!attrsilent) {
1733 (void) fprintf(stderr, gettext(
1734 "%s: failed to get acl entries of"
1735 " attribute %s for"
1736 " %s: %s"), cmd, dp->d_name,
1737 source, acl_strerror(aclerror));
1738 ++error;
1744 * preserve ACL
1746 if ((pflg || mve) && xacl != NULL) {
1747 if ((facl_set(targattrfd, xacl)) < 0) {
1748 if (!attrsilent) {
1749 (void) fprintf(stderr, gettext(
1750 "%s: failed to set acl entries on"
1751 " attribute %s for"
1752 "%s\n"), cmd, dp->d_name, target);
1753 ++error;
1755 acl_free(xacl);
1756 xacl = NULL;
1760 if (writefile(srcattrfd, targattrfd, source, target,
1761 dp->d_name, dp->d_name, &s3, &s4) != 0) {
1762 if (!attrsilent) {
1763 ++error;
1765 goto next;
1768 if (pflg || mve) {
1769 mode = FMODE(s3);
1771 if (fchown(targattrfd, UID(s3), GID(s3)) != 0) {
1772 if (!attrsilent) {
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);
1778 perror("");
1779 ++error;
1781 if (mode & (S_ISUID | S_ISGID)) {
1782 /* try to clear S_ISUID and S_ISGID */
1783 mode &= ~S_ISUID & ~S_ISGID;
1784 ++clearflg;
1787 times[0] = s3.st_atim;
1788 times[1] = s3.st_mtim;
1789 if (utimensat(targetdirfd, dp->d_name, times, 0) < 0) {
1790 if (!attrsilent) {
1791 (void) fprintf(stderr,
1792 gettext("%s: cannot set attribute"
1793 " times for %s: "), cmd, target);
1794 perror("");
1795 ++error;
1798 if (fchmod(targattrfd, mode) != 0) {
1799 if (clearflg) {
1800 (void) fprintf(stderr, gettext(
1801 "%s: cannot clear S_ISUID and "
1802 "S_ISGID bits in attribute %s"
1803 " for file"
1804 " %s: "), cmd, dp->d_name, target);
1805 } else {
1806 if (!attrsilent) {
1807 (void) fprintf(stderr,
1808 gettext(
1809 "%s: cannot set permissions of attribute"
1810 " %s for %s: "), cmd, dp->d_name, target);
1811 perror("");
1812 ++error;
1816 if (xacl && ((facl_set(targattrfd, xacl)) < 0)) {
1817 if (!attrsilent) {
1818 (void) fprintf(stderr, gettext(
1819 "%s: failed to set acl entries on"
1820 " attribute %s for"
1821 "%s\n"), cmd, dp->d_name, target);
1822 ++error;
1824 acl_free(xacl);
1825 xacl = NULL;
1828 next:
1829 if (xacl != NULL) {
1830 acl_free(xacl);
1831 xacl = NULL;
1833 if (srcattrfd != -1)
1834 (void) close(srcattrfd);
1835 if (targattrfd != -1)
1836 (void) close(targattrfd);
1837 srcattrfd = targattrfd = -1;
1839 out:
1840 if (xacl != NULL) {
1841 acl_free(xacl);
1842 xacl = NULL;
1844 if (attrdiracl != NULL) {
1845 acl_free(attrdiracl);
1846 attrdiracl = NULL;
1849 if (!saflg && !pflg && !mve)
1850 close_all();
1851 return (error == 0 ? 0 : 1);
1854 /* Copy extended system attributes from source to target */
1856 static int
1857 copy_sysattr(char *source, char *target)
1859 struct dirent *dp;
1860 nvlist_t *response;
1861 int error = 0;
1862 int target_sa_support = 0;
1864 if (sysattr_support(source, _PC_SATTR_EXISTS) != 1)
1865 return (0);
1867 if (open_source(source) != 0)
1868 return (1);
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,
1881 gettext(
1882 "%s: cannot preserve extended system "
1883 "attribute, operation not supported on file"
1884 " %s\n"), cmd, target);
1885 error++;
1886 goto out;
1888 } else {
1889 target_sa_support = 1;
1892 if (target_sa_support) {
1893 if (srcdirp == NULL) {
1894 if (open_target_srctarg_attrdirs(source,
1895 target) != 0) {
1896 error++;
1897 goto out;
1899 if (open_attrdirp(source) != 0) {
1900 error++;
1901 goto out;
1903 } else {
1904 rewind_attrdir(srcdirp);
1906 while ((dp = readdir(srcdirp)) != NULL) {
1907 nvlist_t *res;
1908 int ret;
1910 if ((ret = traverse_attrfile(dp, source, target,
1911 0)) == -1)
1912 continue;
1913 else if (ret > 0) {
1914 ++error;
1915 goto out;
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);
1925 if (res == NULL)
1926 goto next;
1929 * Copy non default extended system attributes of named
1930 * attribute file.
1932 if (fsetattr(targattrfd,
1933 XATTR_VIEW_READWRITE, res) != 0) {
1934 ++error;
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);
1942 next:
1943 if (srcattrfd != -1)
1944 (void) close(srcattrfd);
1945 if (targattrfd != -1)
1946 (void) close(targattrfd);
1947 srcattrfd = targattrfd = -1;
1948 nvlist_free(res);
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) {
1954 ++error;
1955 (void) fprintf(stderr, gettext("%s: Failed to "
1956 "copy extended system attributes from "
1957 "%s to %s\n"), cmd, source, target);
1959 out:
1960 nvlist_free(response);
1961 close_all();
1962 return (error == 0 ? 0 : 1);
1965 /* Open the source file */
1968 open_source(char *src)
1970 int error = 0;
1972 srcfd = -1;
1973 if ((srcfd = open(src, O_RDONLY)) == -1) {
1974 if (pflg && attrsilent) {
1975 error++;
1976 goto out;
1978 if (!attrsilent) {
1979 (void) fprintf(stderr,
1980 gettext("%s: cannot open file"
1981 " %s: "), cmd, src);
1982 perror("");
1984 ++error;
1986 out:
1987 if (error)
1988 close_all();
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)
1997 int error = 0;
1999 targfd = sourcedirfd = targetdirfd = -1;
2001 if ((targfd = open(targ, O_RDONLY)) == -1) {
2002 if (pflg && attrsilent) {
2003 error++;
2004 goto out;
2006 if (!attrsilent) {
2007 (void) fprintf(stderr,
2008 gettext("%s: cannot open file"
2009 " %s: "), cmd, targ);
2010 perror("");
2012 ++error;
2013 goto out;
2016 if ((sourcedirfd = openat(srcfd, ".", O_RDONLY|O_XATTR)) == -1) {
2017 if (pflg && attrsilent) {
2018 error++;
2019 goto out;
2021 if (!attrsilent) {
2022 (void) fprintf(stderr,
2023 gettext("%s: cannot open attribute"
2024 " directory for %s: "), cmd, src);
2025 perror("");
2027 ++error;
2028 goto out;
2031 if (fstat(sourcedirfd, &attrdir) == -1) {
2032 if (pflg && attrsilent) {
2033 error++;
2034 goto out;
2037 if (!attrsilent) {
2038 (void) fprintf(stderr,
2039 gettext("%s: could not retrieve stat"
2040 " information for attribute directory"
2041 "of file %s: "), cmd, src);
2042 perror("");
2044 ++error;
2045 goto out;
2047 if ((targetdirfd = openat(targfd, ".", O_RDONLY|O_XATTR)) == -1) {
2048 if (pflg && attrsilent) {
2049 error++;
2050 goto out;
2052 if (!attrsilent) {
2053 (void) fprintf(stderr,
2054 gettext("%s: cannot open attribute"
2055 " directory for %s: "), cmd, targ);
2056 perror("");
2058 ++error;
2060 out:
2061 if (error)
2062 close_all();
2063 return (error == 0 ? 0 : 1);
2067 open_attrdirp(char *source)
2069 int tmpfd = -1;
2070 int error = 0;
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) {
2080 error++;
2081 goto out;
2083 if (!attrsilent) {
2084 (void) fprintf(stderr,
2085 gettext(
2086 "%s: unable to dup attribute directory"
2087 " file descriptor for %s: "), cmd, source);
2088 perror("");
2089 ++error;
2091 goto out;
2093 if ((srcdirp = fdopendir(tmpfd)) == NULL) {
2094 if (pflg && attrsilent) {
2095 error++;
2096 goto out;
2098 if (!attrsilent) {
2099 (void) fprintf(stderr,
2100 gettext("%s: failed to open attribute"
2101 " directory for %s: "), cmd, source);
2102 perror("");
2103 ++error;
2106 out:
2107 if (error)
2108 close_all();
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)
2116 int error = 0;
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))
2125 return (-1);
2127 if ((srcattrfd = openat(sourcedirfd, dp->d_name,
2128 O_RDONLY)) == -1) {
2129 if (!attrsilent) {
2130 (void) fprintf(stderr,
2131 gettext("%s: cannot open attribute %s on"
2132 " file %s: "), cmd, dp->d_name, source);
2133 perror("");
2134 ++error;
2135 goto out;
2139 if (fstat(srcattrfd, &s3) < 0) {
2140 if (!attrsilent) {
2141 (void) fprintf(stderr,
2142 gettext("%s: could not stat attribute"
2143 " %s on file"
2144 " %s: "), cmd, dp->d_name, source);
2145 perror("");
2146 ++error;
2148 goto out;
2151 if (first) {
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) {
2155 if (!attrsilent) {
2156 (void) fprintf(stderr,
2157 gettext("%s: could not create attribute"
2158 " %s on file %s: "), cmd, dp->d_name,
2159 target);
2160 perror("");
2161 ++error;
2163 goto out;
2165 } else {
2166 if ((targattrfd = openat(targetdirfd, dp->d_name,
2167 O_RDONLY)) == -1) {
2168 if (!attrsilent) {
2169 (void) fprintf(stderr,
2170 gettext("%s: could not open attribute"
2171 " %s on file %s: "), cmd, dp->d_name,
2172 target);
2173 perror("");
2174 ++error;
2176 goto out;
2181 if (fstat(targattrfd, &s4) < 0) {
2182 if (!attrsilent) {
2183 (void) fprintf(stderr,
2184 gettext("%s: could not stat attribute"
2185 " %s on file"
2186 " %s: "), cmd, dp->d_name, target);
2187 perror("");
2188 ++error;
2192 out:
2193 if (error) {
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);
2203 void
2204 rewind_attrdir(DIR * sdp)
2206 int pwdfd;
2208 pwdfd = open(".", O_RDONLY);
2209 if ((pwdfd != -1) && (fchdir(sourcedirfd) == 0)) {
2210 rewinddir(sdp);
2211 (void) fchdir(pwdfd);
2212 (void) close(pwdfd);
2213 } else {
2214 if (!attrsilent) {
2215 (void) fprintf(stderr, gettext("%s: "
2216 "failed to rewind attribute dir\n"),
2217 cmd);
2222 void
2223 close_all()
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);
2235 srcdirp = NULL;
2237 if (srcfd != -1)
2238 (void) close(srcfd);
2239 if (targfd != -1)
2240 (void) close(targfd);