]> git.saurik.com Git - apple/file_cmds.git/blobdiff - cp/cp.c
file_cmds-242.tar.gz
[apple/file_cmds.git] / cp / cp.c
diff --git a/cp/cp.c b/cp/cp.c
index 55377bcd68cacde960981f1de3e3127eae1dadce..7af6bbcf945fb524d48dd0fe406c40d7546214f9 100644 (file)
--- a/cp/cp.c
+++ b/cp/cp.c
@@ -1,6 +1,4 @@
-/*     $NetBSD: cp.c,v 1.24 1998/08/19 01:29:11 thorpej Exp $  */
-
-/*
+/*-
  * Copyright (c) 1988, 1993, 1994
  *     The Regents of the University of California.  All rights reserved.
  *
  * 2. Redistributions in binary form must reproduce the above copyright
  *    notice, this list of conditions and the following disclaimer in the
  *    documentation and/or other materials provided with the distribution.
- * 3. All advertising materials mentioning features or use of this software
- *    must display the following acknowledgement:
- *     This product includes software developed by the University of
- *     California, Berkeley and its contributors.
  * 4. Neither the name of the University nor the names of its contributors
  *    may be used to endorse or promote products derived from this software
  *    without specific prior written permission.
  * SUCH DAMAGE.
  */
 
-#include <sys/cdefs.h>
+#if 0
 #ifndef lint
-__COPYRIGHT(
+static char const copyright[] =
 "@(#) Copyright (c) 1988, 1993, 1994\n\
-       The Regents of the University of California.  All rights reserved.\n");
+       The Regents of the University of California.  All rights reserved.\n";
 #endif /* not lint */
 
 #ifndef lint
-#if 0
-static char sccsid[] = "@(#)cp.c       8.5 (Berkeley) 4/29/95";
-#else
-__RCSID("$NetBSD: cp.c,v 1.24 1998/08/19 01:29:11 thorpej Exp $");
-#endif
+static char sccsid[] = "@(#)cp.c       8.2 (Berkeley) 4/1/94";
 #endif /* not lint */
+#endif
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD: src/bin/cp/cp.c,v 1.52 2005/09/05 04:36:08 csjp Exp $");
 
 /*
  * Cp copies source files to target files.
- * 
+ *
  * The global PATH_T structure "to" always contains the path to the
  * current target file.  Since fts(3) does not change directories,
  * this path can be either absolute or dot-relative.
- * 
+ *
  * The basic algorithm is to initialize "to" and use fts(3) to traverse
  * the file hierarchy rooted in the argument list.  A trivial case is the
  * case of 'cp file1 file2'.  The more interesting case is the case of
@@ -66,21 +59,26 @@ __RCSID("$NetBSD: cp.c,v 1.24 1998/08/19 01:29:11 thorpej Exp $");
  * in "to") to form the final target path.
  */
 
-#include <sys/param.h>
+#include <sys/types.h>
 #include <sys/stat.h>
-#include <sys/mman.h>
-#include <sys/time.h>
 
-#include <dirent.h>
 #include <err.h>
 #include <errno.h>
-#include <fcntl.h>
 #include <fts.h>
+#include <limits.h>
+#include <signal.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
 
+#ifdef __APPLE__
+#include <copyfile.h>
+#include <get_compat.h>
+#else /* !__APPLE__ */
+#define COMPAT_MODE(a,b) (1)
+#endif /* __APPLE__ */
+
 #include "extern.h"
 
 #define        STRIP_TRAILING_SLASH(p) {                                       \
@@ -88,30 +86,32 @@ __RCSID("$NetBSD: cp.c,v 1.24 1998/08/19 01:29:11 thorpej Exp $");
                 *--(p).p_end = 0;                                      \
 }
 
-PATH_T to = { to.p_path, "" };
+static char emptystring[] = "";
+
+PATH_T to = { to.p_path, emptystring, "" };
 
-uid_t myuid;
-int Rflag, iflag, pflag, rflag, fflag;
-mode_t myumask;
+int fflag, iflag, nflag, pflag, vflag;
+#ifdef __APPLE__
+int Xflag;
+#endif /* __APPLE__ */
+static int Rflag, rflag;
+volatile sig_atomic_t info;
 
 enum op { FILE_TO_FILE, FILE_TO_DIR, DIR_TO_DNE };
 
-int main __P((int, char *[]));
-int copy __P((char *[], enum op, int));
-int mastercmp __P((const FTSENT **, const FTSENT **));
+static int copy(char *[], enum op, int);
+static void siginfo(int __unused);
 
 int
-main(argc, argv)
-       int argc;
-       char *argv[];
+main(int argc, char *argv[])
 {
        struct stat to_stat, tmp_stat;
        enum op type;
-       int Hflag, Lflag, Pflag, ch, fts_options, r;
+       int Hflag, Lflag, Pflag, ch, fts_options, r, have_trailing_slash;
        char *target;
 
-       Hflag = Lflag = Pflag = Rflag = 0;
-       while ((ch = getopt(argc, argv, "HLPRfipr")) != -1) 
+       Hflag = Lflag = Pflag = 0;
+       while ((ch = getopt(argc, argv, "HLPRXafinprv")) != -1)
                switch (ch) {
                case 'H':
                        Hflag = 1;
@@ -128,13 +128,27 @@ main(argc, argv)
                case 'R':
                        Rflag = 1;
                        break;
+               case 'X':
+                       Xflag = 1;
+                       break;
                case 'f':
                        fflag = 1;
-                       iflag = 0;
+                       /* Determine if the STD is SUSv3 or Legacy */
+                       if (COMPAT_MODE("bin/cp", "unix2003"))
+                               nflag = 0;      /* reset nflag, but not iflag */
+                       else
+                               iflag = nflag = 0;      /* reset both */
                        break;
                case 'i':
-                       iflag = isatty(fileno(stdin));
-                       fflag = 0;
+                       iflag = 1;
+                       if (COMPAT_MODE("bin/cp", "unix2003"))
+                               nflag = 0;      /* reset nflag, but not fflag */
+                       else
+                               fflag = nflag = 0;
+                       break;
+               case 'n':
+                       nflag = 1;
+                       fflag = iflag = 0;
                        break;
                case 'p':
                        pflag = 1;
@@ -142,7 +156,14 @@ main(argc, argv)
                case 'r':
                        rflag = 1;
                        break;
-               case '?':
+               case 'v':
+                       vflag = 1;
+                       break;
+               case 'a':
+                       pflag = 1;
+                       Pflag = 1;
+                       Rflag = 1;
+                       break;
                default:
                        usage();
                        break;
@@ -173,31 +194,27 @@ main(argc, argv)
                }
        } else {
                fts_options &= ~FTS_PHYSICAL;
-               fts_options |= FTS_LOGICAL;
+               fts_options |= FTS_LOGICAL | FTS_COMFOLLOW;
        }
-
-       myuid = getuid();
-
-       /* Copy the umask for explicit mode setting. */
-       myumask = umask(0);
-       (void)umask(myumask);
+       (void)signal(SIGINFO, siginfo);
 
        /* Save the target base in "to". */
        target = argv[--argc];
-       if (strlen(target) > MAXPATHLEN)
+       if (strlcpy(to.p_path, target, sizeof(to.p_path)) >= sizeof(to.p_path))
                errx(1, "%s: name too long", target);
-       (void)strcpy(to.p_path, target);
        to.p_end = to.p_path + strlen(to.p_path);
         if (to.p_path == to.p_end) {
                *to.p_end++ = '.';
                *to.p_end = 0;
        }
-        STRIP_TRAILING_SLASH(to);
+       have_trailing_slash = (to.p_end[-1] == '/');
+       if (have_trailing_slash)
+               STRIP_TRAILING_SLASH(to);
        to.target_end = to.p_end;
 
        /* Set end of argument list for fts(3). */
-       argv[argc] = NULL;     
-       
+       argv[argc] = NULL;
+
        /*
         * Cp has two distinct cases:
         *
@@ -218,7 +235,7 @@ main(argc, argv)
        if (r == -1 || !S_ISDIR(to_stat.st_mode)) {
                /*
                 * Case (1).  Target is not a directory.
-                */ 
+                */
                if (argc > 1) {
                        usage();
                        exit(1);
@@ -232,48 +249,57 @@ main(argc, argv)
                 */
                if (r == -1) {
                        if (rflag || (Rflag && (Lflag || Hflag)))
-                               r = stat(*argv, &tmp_stat);
+                               stat(*argv, &tmp_stat);
                        else
-                               r = lstat(*argv, &tmp_stat);
-                       if (r == -1)
-                               err(1, "%s", *argv);
-                       
+                               lstat(*argv, &tmp_stat);
+
                        if (S_ISDIR(tmp_stat.st_mode) && (Rflag || rflag))
                                type = DIR_TO_DNE;
                        else
                                type = FILE_TO_FILE;
                } else
                        type = FILE_TO_FILE;
-       } else {
+
+               if (have_trailing_slash && type == FILE_TO_FILE) {
+                       if (r == -1)
+                               errx(1, "directory %s does not exist",
+                                    to.p_path);
+                       else
+                               errx(1, "%s is not a directory", to.p_path);
+               }
+       } else
                /*
                 * Case (2).  Target is a directory.
                 */
                type = FILE_TO_DIR;
-       }
 
        exit (copy(argv, type, fts_options));
-       /* NOTREACHED */
 }
 
-int
-copy(argv, type, fts_options)
-       char *argv[];
-       enum op type;
-       int fts_options;
+static int
+copy(char *argv[], enum op type, int fts_options)
 {
        struct stat to_stat;
        FTS *ftsp;
        FTSENT *curr;
-       int base, dne, nlen, rval;
-       char *p, *tmp;
+       int base = 0, dne, badcp, rval;
+       size_t nlen;
+       char *p, *target_mid;
+       mode_t mask, mode;
 
-       base = 0;       /* XXX gcc -Wuninitialized (see comment below) */
+       /*
+        * Keep an inverted copy of the umask, for use in correcting
+        * permissions on created directories when not using -p.
+        */
+       mask = ~umask(0777);
+       umask(~mask);
 
-       if ((ftsp = fts_open(argv, fts_options, mastercmp)) == NULL)
-               err(1, argv[0]);
-       for (rval = 0; (curr = fts_read(ftsp)) != NULL;) {
+       if ((ftsp = fts_open(argv, fts_options, NULL)) == NULL)
+               err(1, "fts_open");
+       for (badcp = rval = 0; (curr = fts_read(ftsp)) != NULL; badcp = 0) {
                switch (curr->fts_info) {
                case FTS_NS:
+               case FTS_DNR:
                case FTS_ERR:
                        warnx("%s: %s",
                            curr->fts_path, strerror(curr->fts_errno));
@@ -283,21 +309,44 @@ copy(argv, type, fts_options)
                        warnx("%s: directory causes a cycle", curr->fts_path);
                        rval = 1;
                        continue;
+               default:
+                       ;
                }
+#ifdef __APPLE__
 
+#ifdef __clang__
+#pragma clang diagnostic push
+/* clang doesn't like fts_name[1], but we know better... */
+#pragma clang diagnostic ignored "-Warray-bounds"
+#endif
+               /* Skip ._<file> when using copyfile and <file> exists */
+               if ((pflag || !Xflag) && (curr->fts_level != FTS_ROOTLEVEL) &&
+                   (curr->fts_namelen > 2) && /* ._\0 is not AppleDouble */
+                   (curr->fts_name[0] == '.') && (curr->fts_name[1] == '_')) {
+#ifdef __clang__
+#pragma clang diagnostic pop
+#endif
+                       struct stat statbuf;
+                       char path[PATH_MAX];
+                       char *p = strrchr(curr->fts_path, '/');
+                       if (p) {
+                               size_t s = p + 2 - curr->fts_path;
+                               if (s > sizeof(path)) s = sizeof(path);
+                               strlcpy(path, curr->fts_path, s);
+                               strlcat(path, curr->fts_name+2, sizeof(path));
+                       } else {
+                               strlcpy(path, curr->fts_name+2, sizeof(path));
+                       }
+                       if (!lstat(path, &statbuf)) {
+                               continue;
+                       }
+               }
+#endif /* __APPLE__ */
                /*
-                * If we are in case (2) or (3) above, we need to append the 
-                 * source name to the target name.  
+                * If we are in case (2) or (3) above, we need to append the
+                 * source name to the target name.
                  */
                if (type != FILE_TO_FILE) {
-                       if ((curr->fts_namelen +
-                           to.target_end - to.p_path + 1) > MAXPATHLEN) {
-                               warnx("%s/%s: name too long (not copied)", 
-                                   to.p_path, curr->fts_name);
-                               rval = 1;
-                               continue;
-                       }
-
                        /*
                         * Need to remember the roots of traversals to create
                         * correct pathnames.  If there's a directory being
@@ -308,7 +357,7 @@ copy(argv, type, fts_options)
                         * is the case where the target exists.
                         *
                         * Also, check for "..".  This is for correct path
-                        * concatentation for paths ending in "..", e.g.
+                        * concatenation for paths ending in "..", e.g.
                         *      cp -R .. /tmp
                         * Paths ending in ".." are changed to ".".  This is
                         * tricky, but seems the easiest way to fix the problem.
@@ -320,10 +369,10 @@ copy(argv, type, fts_options)
                        if (curr->fts_level == FTS_ROOTLEVEL) {
                                if (type != DIR_TO_DNE) {
                                        p = strrchr(curr->fts_path, '/');
-                                       base = (p == NULL) ? 0 : 
+                                       base = (p == NULL) ? 0 :
                                            (int)(p - curr->fts_path + 1);
 
-                                       if (!strcmp(&curr->fts_path[base], 
+                                       if (!strcmp(&curr->fts_path[base],
                                            ".."))
                                                base += 1;
                                } else
@@ -332,18 +381,63 @@ copy(argv, type, fts_options)
 
                        p = &curr->fts_path[base];
                        nlen = curr->fts_pathlen - base;
-
-                       tmp = to.target_end;
-                       if (*p != '/' && *(tmp - 1) != '/')
-                               *tmp++ = '/';
-                       *tmp = 0;
-
-                       (void)strncat(tmp, p, nlen);
-                       to.p_end = tmp + nlen;
+                       target_mid = to.target_end;
+                       if (*p != '/' && target_mid[-1] != '/')
+                               *target_mid++ = '/';
+                       *target_mid = 0;
+                       if (target_mid - to.p_path + nlen >= PATH_MAX) {
+                               warnx("%s%s: name too long (not copied)",
+                                   to.p_path, p);
+                               rval = 1;
+                               continue;
+                       }
+                       (void)strncat(target_mid, p, nlen);
+                       to.p_end = target_mid + nlen;
                        *to.p_end = 0;
                        STRIP_TRAILING_SLASH(to);
                }
 
+               if (curr->fts_info == FTS_DP) {
+                       /*
+                        * We are nearly finished with this directory.  If we
+                        * didn't actually copy it, or otherwise don't need to
+                        * change its attributes, then we are done.
+                        */
+                       if (!curr->fts_number)
+                               continue;
+                       /*
+                        * If -p is in effect, set all the attributes.
+                        * Otherwise, set the correct permissions, limited
+                        * by the umask.  Optimise by avoiding a chmod()
+                        * if possible (which is usually the case if we
+                        * made the directory).  Note that mkdir() does not
+                        * honour setuid, setgid and sticky bits, but we
+                        * normally want to preserve them on directories.
+                        */
+                       if (pflag) {
+                               if (setfile(curr->fts_statp, -1))
+                                       rval = 1;
+#ifdef __APPLE__
+                               /* setfile will fail if writeattr is denied */
+                               if (copyfile(curr->fts_path, to.p_path, NULL, COPYFILE_ACL)<0)
+                                       warn("%s: unable to copy ACL to %s", curr->fts_path, to.p_path);
+#else  /* !__APPLE__ */
+                               if (preserve_dir_acls(curr->fts_statp,
+                                   curr->fts_accpath, to.p_path) != 0)
+                                       rval = 1;
+#endif /* __APPLE__ */
+                       } else {
+                               mode = curr->fts_statp->st_mode;
+                               if ((mode & (S_ISUID | S_ISGID | S_ISTXT)) ||
+                                   ((mode | S_IRWXU) & mask) != (mode & mask))
+                                       if (chmod(to.p_path, mode & mask) != 0){
+                                               warn("chmod: %s", to.p_path);
+                                               rval = 1;
+                                       }
+                       }
+                       continue;
+               }
+
                /* Not an error but need to remember it happened */
                if (stat(to.p_path, &to_stat) == -1)
                        dne = 1;
@@ -359,7 +453,8 @@ copy(argv, type, fts_options)
                        }
                        if (!S_ISDIR(curr->fts_statp->st_mode) &&
                            S_ISDIR(to_stat.st_mode)) {
-               warnx("cannot overwrite directory %s with non-directory %s",
+                               warnx("cannot overwrite directory %s with "
+                                   "non-directory %s",
                                    to.p_path, curr->fts_path);
                                rval = 1;
                                continue;
@@ -369,116 +464,100 @@ copy(argv, type, fts_options)
 
                switch (curr->fts_statp->st_mode & S_IFMT) {
                case S_IFLNK:
-                       if (copy_link(curr, !dne))
-                               rval = 1;
+                       /* Catch special case of a non-dangling symlink */
+                       if ((fts_options & FTS_LOGICAL) ||
+                           ((fts_options & FTS_COMFOLLOW) &&
+                           curr->fts_level == 0)) {
+                               if (copy_file(curr, dne))
+                                       badcp = rval = 1;
+                       } else {        
+                               if (copy_link(curr, !dne))
+                                       badcp = rval = 1;
+                       }
                        break;
                case S_IFDIR:
                        if (!Rflag && !rflag) {
-                               if (curr->fts_info == FTS_DP)
-                                       warnx("%s is a directory (not copied).",
-                                           curr->fts_path);
+                               warnx("%s is a directory (not copied).",
+                                   curr->fts_path);
                                (void)fts_set(ftsp, curr, FTS_SKIP);
-                               rval = 1;
+                               badcp = rval = 1;
                                break;
                        }
-
-                        /*
-                         * Directories get noticed twice:
-                         *  In the first pass, create it if needed.
-                         *  In the second pass, after the children have been copied, set the permissions.
-                         */
-                       if (curr->fts_info == FTS_D) /* First pass */
-                       {
-                               /*
-                                * If the directory doesn't exist, create the new
-                                * one with the from file mode plus owner RWX bits,
-                                * modified by the umask.  Trade-off between being
-                                * able to write the directory (if from directory is
-                                * 555) and not causing a permissions race.  If the
-                                * umask blocks owner writes, we fail..
-                                */
-                               if (dne) {
-                                       if (mkdir(to.p_path, 
-                                           curr->fts_statp->st_mode | S_IRWXU) < 0)
+                       /*
+                        * If the directory doesn't exist, create the new
+                        * one with the from file mode plus owner RWX bits,
+                        * modified by the umask.  Trade-off between being
+                        * able to write the directory (if from directory is
+                        * 555) and not causing a permissions race.  If the
+                        * umask blocks owner writes, we fail..
+                        */
+                       if (dne) {
+                               if (mkdir(to.p_path,
+                                         curr->fts_statp->st_mode | S_IRWXU) < 0) {
+                                       if (COMPAT_MODE("bin/cp", "unix2003")) {
+                                               warn("%s", to.p_path);
+                                       } else {
                                                err(1, "%s", to.p_path);
-                               } else if (!S_ISDIR(to_stat.st_mode)) {
-                                       errno = ENOTDIR;
+                                       }
+                               }
+                       } else if (!S_ISDIR(to_stat.st_mode)) {
+                               errno = ENOTDIR;
+                               if (COMPAT_MODE("bin/cp", "unix2003")) {
+                                       warn("%s", to.p_path);
+                               } else {
                                        err(1, "%s", to.p_path);
                                }
                        }
-                       else if (curr->fts_info == FTS_DP) /* Second pass */
-                       {
-                               /*
-                                * If not -p and directory didn't exist, set it to be
-                                * the same as the from directory, umodified by the 
-                                * umask; arguably wrong, but it's been that way 
-                                * forever.
-                                */
-                               if (pflag && setfile(curr->fts_statp, 0))
-                                       rval = 1;
-                               else if (dne)
-                                       (void)chmod(to.p_path, 
-                                           curr->fts_statp->st_mode);
+                       /*
+                        * Arrange to correct directory attributes later
+                        * (in the post-order phase) if this is a new
+                        * directory, or if the -p flag is in effect.
+                        */
+                       curr->fts_number = pflag || dne;
+#ifdef __APPLE__
+                       if (!Xflag) {
+                               if (copyfile(curr->fts_path, to.p_path, NULL, COPYFILE_XATTR) < 0)
+                                       warn("%s: unable to copy extended attributes to %s", curr->fts_path, to.p_path);
+                               /* ACL and mtime set in postorder traversal */
                        }
-                       else
-                        {
-                               warnx("directory %s encountered when not expected.", curr->fts_path);
-                               rval = 1;
-                                break;
-                        }
-
+#endif /* __APPLE__ */
                        break;
                case S_IFBLK:
                case S_IFCHR:
                        if (Rflag) {
                                if (copy_special(curr->fts_statp, !dne))
-                                       rval = 1;
-                       } else
+                                       badcp = rval = 1;
+                       } else {
                                if (copy_file(curr, dne))
-                                       rval = 1;
+                                       badcp = rval = 1;
+                       }
                        break;
                case S_IFIFO:
                        if (Rflag) {
                                if (copy_fifo(curr->fts_statp, !dne))
-                                       rval = 1;
-                       } else 
+                                       badcp = rval = 1;
+                       } else {
                                if (copy_file(curr, dne))
-                                       rval = 1;
+                                       badcp = rval = 1;
+                       }
                        break;
                default:
                        if (copy_file(curr, dne))
-                               rval = 1;
+                               badcp = rval = 1;
                        break;
                }
+               if (vflag && !badcp)
+                       (void)printf("%s -> %s\n", curr->fts_path, to.p_path);
        }
        if (errno)
                err(1, "fts_read");
+       fts_close(ftsp);
        return (rval);
 }
 
-/*
- * mastercmp --
- *     The comparison function for the copy order.  The order is to copy
- *     non-directory files before directory files.  The reason for this
- *     is because files tend to be in the same cylinder group as their
- *     parent directory, whereas directories tend not to be.  Copying the
- *     files first reduces seeking.
- */
-int
-mastercmp(a, b)
-       const FTSENT **a, **b;
+static void
+siginfo(int sig __unused)
 {
-       int a_info, b_info;
-
-       a_info = (*a)->fts_info;
-       if (a_info == FTS_ERR || a_info == FTS_NS || a_info == FTS_DNR)
-               return (0);
-       b_info = (*b)->fts_info;
-       if (b_info == FTS_ERR || b_info == FTS_NS || b_info == FTS_DNR)
-               return (0);
-       if (a_info == FTS_D)
-               return (-1);
-       if (b_info == FTS_D)
-               return (1);
-       return (0);
+
+       info = 1;
 }