xref: /freebsd/bin/cp/cp.c (revision f9dc2a8b)
14b88c807SRodney W. Grimes /*
24b88c807SRodney W. Grimes  * Copyright (c) 1988, 1993, 1994
34b88c807SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
44b88c807SRodney W. Grimes  *
54b88c807SRodney W. Grimes  * This code is derived from software contributed to Berkeley by
64b88c807SRodney W. Grimes  * David Hitz of Auspex Systems Inc.
74b88c807SRodney W. Grimes  *
84b88c807SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
94b88c807SRodney W. Grimes  * modification, are permitted provided that the following conditions
104b88c807SRodney W. Grimes  * are met:
114b88c807SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
124b88c807SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
134b88c807SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
144b88c807SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
154b88c807SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
164b88c807SRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
174b88c807SRodney W. Grimes  *    must display the following acknowledgement:
184b88c807SRodney W. Grimes  *	This product includes software developed by the University of
194b88c807SRodney W. Grimes  *	California, Berkeley and its contributors.
204b88c807SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
214b88c807SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
224b88c807SRodney W. Grimes  *    without specific prior written permission.
234b88c807SRodney W. Grimes  *
244b88c807SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
254b88c807SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
264b88c807SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
274b88c807SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
284b88c807SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
294b88c807SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
304b88c807SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
314b88c807SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
324b88c807SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
334b88c807SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
344b88c807SRodney W. Grimes  * SUCH DAMAGE.
354b88c807SRodney W. Grimes  */
364b88c807SRodney W. Grimes 
374b88c807SRodney W. Grimes #ifndef lint
38890acb95SSteve Price static char const copyright[] =
394b88c807SRodney W. Grimes "@(#) Copyright (c) 1988, 1993, 1994\n\
404b88c807SRodney W. Grimes 	The Regents of the University of California.  All rights reserved.\n";
414b88c807SRodney W. Grimes #endif /* not lint */
424b88c807SRodney W. Grimes 
434b88c807SRodney W. Grimes #ifndef lint
44cbf6f7d3SPhilippe Charnier #if 0
45c194af34SPhilippe Charnier static char sccsid[] = "@(#)cp.c	8.2 (Berkeley) 4/1/94";
46cbf6f7d3SPhilippe Charnier #endif
47cbf6f7d3SPhilippe Charnier static const char rcsid[] =
482a456239SPeter Wemm   "$FreeBSD$";
494b88c807SRodney W. Grimes #endif /* not lint */
504b88c807SRodney W. Grimes 
514b88c807SRodney W. Grimes /*
524b88c807SRodney W. Grimes  * Cp copies source files to target files.
534b88c807SRodney W. Grimes  *
544b88c807SRodney W. Grimes  * The global PATH_T structure "to" always contains the path to the
554b88c807SRodney W. Grimes  * current target file.  Since fts(3) does not change directories,
56890acb95SSteve Price  * this path can be either absolute or dot-relative.
574b88c807SRodney W. Grimes  *
584b88c807SRodney W. Grimes  * The basic algorithm is to initialize "to" and use fts(3) to traverse
594b88c807SRodney W. Grimes  * the file hierarchy rooted in the argument list.  A trivial case is the
604b88c807SRodney W. Grimes  * case of 'cp file1 file2'.  The more interesting case is the case of
614b88c807SRodney W. Grimes  * 'cp file1 file2 ... fileN dir' where the hierarchy is traversed and the
624b88c807SRodney W. Grimes  * path (relative to the root of the traversal) is appended to dir (stored
634b88c807SRodney W. Grimes  * in "to") to form the final target path.
644b88c807SRodney W. Grimes  */
654b88c807SRodney W. Grimes 
664b88c807SRodney W. Grimes #include <sys/param.h>
674b88c807SRodney W. Grimes #include <sys/stat.h>
684b88c807SRodney W. Grimes 
694b88c807SRodney W. Grimes #include <err.h>
704b88c807SRodney W. Grimes #include <errno.h>
714b88c807SRodney W. Grimes #include <fts.h>
7234f9c106SWarner Losh #include <limits.h>
734506e907SMichael Haro #include <stdio.h>
7426f6b0fbSDag-Erling Smørgrav #include <stdlib.h>
754b88c807SRodney W. Grimes #include <string.h>
764b88c807SRodney W. Grimes #include <unistd.h>
774b88c807SRodney W. Grimes 
784b88c807SRodney W. Grimes #include "extern.h"
794b88c807SRodney W. Grimes 
804b88c807SRodney W. Grimes #define	STRIP_TRAILING_SLASH(p) {					\
817658fd20SBruce Evans         while ((p).p_end > (p).p_path + 1 && (p).p_end[-1] == '/')	\
824b88c807SRodney W. Grimes                 *--(p).p_end = 0;					\
834b88c807SRodney W. Grimes }
844b88c807SRodney W. Grimes 
858bd08b5fSMark Murray static char emptystring[] = "";
864b88c807SRodney W. Grimes 
878bd08b5fSMark Murray PATH_T to = { to.p_path, emptystring, "" };
888bd08b5fSMark Murray 
898bd08b5fSMark Murray int iflag, pflag, fflag;
908bd08b5fSMark Murray static int Rflag, rflag, vflag;
914b88c807SRodney W. Grimes 
924b88c807SRodney W. Grimes enum op { FILE_TO_FILE, FILE_TO_DIR, DIR_TO_DNE };
934b88c807SRodney W. Grimes 
945dce647cSWarner Losh int copy(char *[], enum op, int);
955dce647cSWarner Losh int mastercmp(const FTSENT **, const FTSENT **);
964b88c807SRodney W. Grimes 
974b88c807SRodney W. Grimes int
985dce647cSWarner Losh main(int argc, char *argv[])
994b88c807SRodney W. Grimes {
1004b88c807SRodney W. Grimes 	struct stat to_stat, tmp_stat;
1014b88c807SRodney W. Grimes 	enum op type;
10227d3ae35SAndrey A. Chernov 	int Hflag, Lflag, Pflag, ch, fts_options, r, have_trailing_slash;
103486d0387SAndrey A. Chernov 	char *target;
1044b88c807SRodney W. Grimes 
105ee4f505eSPaul Traina 	Hflag = Lflag = Pflag = 0;
1064506e907SMichael Haro 	while ((ch = getopt(argc, argv, "HLPRfiprv")) != -1)
1074b88c807SRodney W. Grimes 		switch (ch) {
1084b88c807SRodney W. Grimes 		case 'H':
1094b88c807SRodney W. Grimes 			Hflag = 1;
1104b88c807SRodney W. Grimes 			Lflag = Pflag = 0;
1114b88c807SRodney W. Grimes 			break;
1124b88c807SRodney W. Grimes 		case 'L':
1134b88c807SRodney W. Grimes 			Lflag = 1;
1144b88c807SRodney W. Grimes 			Hflag = Pflag = 0;
1154b88c807SRodney W. Grimes 			break;
1164b88c807SRodney W. Grimes 		case 'P':
1174b88c807SRodney W. Grimes 			Pflag = 1;
1184b88c807SRodney W. Grimes 			Hflag = Lflag = 0;
1194b88c807SRodney W. Grimes 			break;
1204b88c807SRodney W. Grimes 		case 'R':
1214b88c807SRodney W. Grimes 			Rflag = 1;
1224b88c807SRodney W. Grimes 			break;
1234b88c807SRodney W. Grimes 		case 'f':
1246add522fSWolfram Schneider 			fflag = 1;
1254b88c807SRodney W. Grimes 			iflag = 0;
1264b88c807SRodney W. Grimes 			break;
1274b88c807SRodney W. Grimes 		case 'i':
1286add522fSWolfram Schneider 			iflag = 1;
1296add522fSWolfram Schneider 			fflag = 0;
1304b88c807SRodney W. Grimes 			break;
1314b88c807SRodney W. Grimes 		case 'p':
1324b88c807SRodney W. Grimes 			pflag = 1;
1334b88c807SRodney W. Grimes 			break;
1344b88c807SRodney W. Grimes 		case 'r':
1354b88c807SRodney W. Grimes 			rflag = 1;
1364b88c807SRodney W. Grimes 			break;
1374506e907SMichael Haro 		case 'v':
1384506e907SMichael Haro 			vflag = 1;
1394506e907SMichael Haro 			break;
1404b88c807SRodney W. Grimes 		default:
1414b88c807SRodney W. Grimes 			usage();
1424b88c807SRodney W. Grimes 			break;
1434b88c807SRodney W. Grimes 		}
1444b88c807SRodney W. Grimes 	argc -= optind;
1454b88c807SRodney W. Grimes 	argv += optind;
1464b88c807SRodney W. Grimes 
1474b88c807SRodney W. Grimes 	if (argc < 2)
1484b88c807SRodney W. Grimes 		usage();
1494b88c807SRodney W. Grimes 
1504b88c807SRodney W. Grimes 	fts_options = FTS_NOCHDIR | FTS_PHYSICAL;
1514b88c807SRodney W. Grimes 	if (rflag) {
1524b88c807SRodney W. Grimes 		if (Rflag)
1534b88c807SRodney W. Grimes 			errx(1,
1544b88c807SRodney W. Grimes 		    "the -R and -r options may not be specified together.");
1554b88c807SRodney W. Grimes 		if (Hflag || Lflag || Pflag)
1564b88c807SRodney W. Grimes 			errx(1,
1574b88c807SRodney W. Grimes 	"the -H, -L, and -P options may not be specified with the -r option.");
1584b88c807SRodney W. Grimes 		fts_options &= ~FTS_PHYSICAL;
1594b88c807SRodney W. Grimes 		fts_options |= FTS_LOGICAL;
1604b88c807SRodney W. Grimes 	}
1614b88c807SRodney W. Grimes 	if (Rflag) {
1624b88c807SRodney W. Grimes 		if (Hflag)
1634b88c807SRodney W. Grimes 			fts_options |= FTS_COMFOLLOW;
1644b88c807SRodney W. Grimes 		if (Lflag) {
1654b88c807SRodney W. Grimes 			fts_options &= ~FTS_PHYSICAL;
1664b88c807SRodney W. Grimes 			fts_options |= FTS_LOGICAL;
1674b88c807SRodney W. Grimes 		}
1684b88c807SRodney W. Grimes 	} else {
1694b88c807SRodney W. Grimes 		fts_options &= ~FTS_PHYSICAL;
170f9dc2a8bSTim J. Robbins 		fts_options |= FTS_LOGICAL | FTS_COMFOLLOW;
1714b88c807SRodney W. Grimes 	}
1724b88c807SRodney W. Grimes 
1734b88c807SRodney W. Grimes 	/* Save the target base in "to". */
1744b88c807SRodney W. Grimes 	target = argv[--argc];
17534f9c106SWarner Losh 	if (strlcpy(to.p_path, target, sizeof(to.p_path)) >= sizeof(to.p_path))
1764b88c807SRodney W. Grimes 		errx(1, "%s: name too long", target);
1774b88c807SRodney W. Grimes 	to.p_end = to.p_path + strlen(to.p_path);
1784b88c807SRodney W. Grimes         if (to.p_path == to.p_end) {
1794b88c807SRodney W. Grimes 		*to.p_end++ = '.';
1804b88c807SRodney W. Grimes 		*to.p_end = 0;
1814b88c807SRodney W. Grimes 	}
182486d0387SAndrey A. Chernov 	have_trailing_slash = (to.p_end[-1] == '/');
183486d0387SAndrey A. Chernov 	if (have_trailing_slash)
1844b88c807SRodney W. Grimes 		STRIP_TRAILING_SLASH(to);
1854b88c807SRodney W. Grimes 	to.target_end = to.p_end;
1864b88c807SRodney W. Grimes 
1874b88c807SRodney W. Grimes 	/* Set end of argument list for fts(3). */
1884b88c807SRodney W. Grimes 	argv[argc] = NULL;
1894b88c807SRodney W. Grimes 
1904b88c807SRodney W. Grimes 	/*
1914b88c807SRodney W. Grimes 	 * Cp has two distinct cases:
1924b88c807SRodney W. Grimes 	 *
1934b88c807SRodney W. Grimes 	 * cp [-R] source target
1944b88c807SRodney W. Grimes 	 * cp [-R] source1 ... sourceN directory
1954b88c807SRodney W. Grimes 	 *
1964b88c807SRodney W. Grimes 	 * In both cases, source can be either a file or a directory.
1974b88c807SRodney W. Grimes 	 *
1984b88c807SRodney W. Grimes 	 * In (1), the target becomes a copy of the source. That is, if the
1994b88c807SRodney W. Grimes 	 * source is a file, the target will be a file, and likewise for
2004b88c807SRodney W. Grimes 	 * directories.
2014b88c807SRodney W. Grimes 	 *
2024b88c807SRodney W. Grimes 	 * In (2), the real target is not directory, but "directory/source".
2034b88c807SRodney W. Grimes 	 */
2044b88c807SRodney W. Grimes 	r = stat(to.p_path, &to_stat);
2054b88c807SRodney W. Grimes 	if (r == -1 && errno != ENOENT)
2064b88c807SRodney W. Grimes 		err(1, "%s", to.p_path);
2074b88c807SRodney W. Grimes 	if (r == -1 || !S_ISDIR(to_stat.st_mode)) {
2084b88c807SRodney W. Grimes 		/*
2094b88c807SRodney W. Grimes 		 * Case (1).  Target is not a directory.
2104b88c807SRodney W. Grimes 		 */
2114b88c807SRodney W. Grimes 		if (argc > 1) {
2124b88c807SRodney W. Grimes 			usage();
2134b88c807SRodney W. Grimes 			exit(1);
2144b88c807SRodney W. Grimes 		}
2154b88c807SRodney W. Grimes 		/*
2164b88c807SRodney W. Grimes 		 * Need to detect the case:
2174b88c807SRodney W. Grimes 		 *	cp -R dir foo
2184b88c807SRodney W. Grimes 		 * Where dir is a directory and foo does not exist, where
2194b88c807SRodney W. Grimes 		 * we want pathname concatenations turned on but not for
2204b88c807SRodney W. Grimes 		 * the initial mkdir().
2214b88c807SRodney W. Grimes 		 */
2224b88c807SRodney W. Grimes 		if (r == -1) {
2234b88c807SRodney W. Grimes 			if (rflag || (Rflag && (Lflag || Hflag)))
2244b88c807SRodney W. Grimes 				stat(*argv, &tmp_stat);
2254b88c807SRodney W. Grimes 			else
2264b88c807SRodney W. Grimes 				lstat(*argv, &tmp_stat);
2274b88c807SRodney W. Grimes 
2284b88c807SRodney W. Grimes 			if (S_ISDIR(tmp_stat.st_mode) && (Rflag || rflag))
2294b88c807SRodney W. Grimes 				type = DIR_TO_DNE;
2304b88c807SRodney W. Grimes 			else
2314b88c807SRodney W. Grimes 				type = FILE_TO_FILE;
2324b88c807SRodney W. Grimes 		} else
2334b88c807SRodney W. Grimes 			type = FILE_TO_FILE;
23427d3ae35SAndrey A. Chernov 
23527d3ae35SAndrey A. Chernov 		if (have_trailing_slash && type == FILE_TO_FILE) {
23627d3ae35SAndrey A. Chernov 			if (r == -1)
23727d3ae35SAndrey A. Chernov 				errx(1, "directory %s does not exist",
23827d3ae35SAndrey A. Chernov 				     to.p_path);
23927d3ae35SAndrey A. Chernov 			else
24027d3ae35SAndrey A. Chernov 				errx(1, "%s is not a directory", to.p_path);
24127d3ae35SAndrey A. Chernov 		}
2424b88c807SRodney W. Grimes 	} else
2434b88c807SRodney W. Grimes 		/*
2444b88c807SRodney W. Grimes 		 * Case (2).  Target is a directory.
2454b88c807SRodney W. Grimes 		 */
2464b88c807SRodney W. Grimes 		type = FILE_TO_DIR;
2474b88c807SRodney W. Grimes 
2484b88c807SRodney W. Grimes 	exit (copy(argv, type, fts_options));
2494b88c807SRodney W. Grimes }
2504b88c807SRodney W. Grimes 
2514b88c807SRodney W. Grimes int
2525dce647cSWarner Losh copy(char *argv[], enum op type, int fts_options)
2534b88c807SRodney W. Grimes {
2544b88c807SRodney W. Grimes 	struct stat to_stat;
2554b88c807SRodney W. Grimes 	FTS *ftsp;
2564b88c807SRodney W. Grimes 	FTSENT *curr;
2578bd08b5fSMark Murray 	int base = 0, dne, badcp, rval;
2588bd08b5fSMark Murray 	size_t nlen;
2597658fd20SBruce Evans 	char *p, *target_mid;
26062de071bSStephen McKay 	mode_t mask, mode;
2614a5db7acSStephen McKay 
2624a5db7acSStephen McKay 	/*
2634a5db7acSStephen McKay 	 * Keep an inverted copy of the umask, for use in correcting
2644a5db7acSStephen McKay 	 * permissions on created directories when not using -p.
2654a5db7acSStephen McKay 	 */
2664a5db7acSStephen McKay 	mask = ~umask(0777);
2674a5db7acSStephen McKay 	umask(~mask);
2684b88c807SRodney W. Grimes 
2694b88c807SRodney W. Grimes 	if ((ftsp = fts_open(argv, fts_options, mastercmp)) == NULL)
2704b88c807SRodney W. Grimes 		err(1, NULL);
2710efa2040SMichael Haro 	for (badcp = rval = 0; (curr = fts_read(ftsp)) != NULL; badcp = 0) {
2724b88c807SRodney W. Grimes 		switch (curr->fts_info) {
2734b88c807SRodney W. Grimes 		case FTS_NS:
274f6fa1b35SDmitrij Tejblum 		case FTS_DNR:
2754b88c807SRodney W. Grimes 		case FTS_ERR:
2764b88c807SRodney W. Grimes 			warnx("%s: %s",
2774b88c807SRodney W. Grimes 			    curr->fts_path, strerror(curr->fts_errno));
2780efa2040SMichael Haro 			badcp = rval = 1;
2794b88c807SRodney W. Grimes 			continue;
2804b88c807SRodney W. Grimes 		case FTS_DC:			/* Warn, continue. */
2814b88c807SRodney W. Grimes 			warnx("%s: directory causes a cycle", curr->fts_path);
2820efa2040SMichael Haro 			badcp = rval = 1;
2834b88c807SRodney W. Grimes 			continue;
2848bd08b5fSMark Murray 		default:
28564baebf9SAlfred Perlstein 			;
2864b88c807SRodney W. Grimes 		}
2874b88c807SRodney W. Grimes 
2884b88c807SRodney W. Grimes 		/*
2894b88c807SRodney W. Grimes 		 * If we are in case (2) or (3) above, we need to append the
2904b88c807SRodney W. Grimes                  * source name to the target name.
2914b88c807SRodney W. Grimes                  */
2924b88c807SRodney W. Grimes 		if (type != FILE_TO_FILE) {
2934b88c807SRodney W. Grimes 			/*
2944b88c807SRodney W. Grimes 			 * Need to remember the roots of traversals to create
2954b88c807SRodney W. Grimes 			 * correct pathnames.  If there's a directory being
2964b88c807SRodney W. Grimes 			 * copied to a non-existent directory, e.g.
2974b88c807SRodney W. Grimes 			 *	cp -R a/dir noexist
2984b88c807SRodney W. Grimes 			 * the resulting path name should be noexist/foo, not
2994b88c807SRodney W. Grimes 			 * noexist/dir/foo (where foo is a file in dir), which
3004b88c807SRodney W. Grimes 			 * is the case where the target exists.
3014b88c807SRodney W. Grimes 			 *
3024b88c807SRodney W. Grimes 			 * Also, check for "..".  This is for correct path
30346be34b9SKris Kennaway 			 * concatenation for paths ending in "..", e.g.
3044b88c807SRodney W. Grimes 			 *	cp -R .. /tmp
3054b88c807SRodney W. Grimes 			 * Paths ending in ".." are changed to ".".  This is
3064b88c807SRodney W. Grimes 			 * tricky, but seems the easiest way to fix the problem.
3074b88c807SRodney W. Grimes 			 *
3084b88c807SRodney W. Grimes 			 * XXX
3094b88c807SRodney W. Grimes 			 * Since the first level MUST be FTS_ROOTLEVEL, base
3104b88c807SRodney W. Grimes 			 * is always initialized.
3114b88c807SRodney W. Grimes 			 */
312426e9c1dSWarner Losh 			if (curr->fts_level == FTS_ROOTLEVEL) {
3134b88c807SRodney W. Grimes 				if (type != DIR_TO_DNE) {
3144b88c807SRodney W. Grimes 					p = strrchr(curr->fts_path, '/');
3154b88c807SRodney W. Grimes 					base = (p == NULL) ? 0 :
3164b88c807SRodney W. Grimes 					    (int)(p - curr->fts_path + 1);
3174b88c807SRodney W. Grimes 
3184b88c807SRodney W. Grimes 					if (!strcmp(&curr->fts_path[base],
3194b88c807SRodney W. Grimes 					    ".."))
3204b88c807SRodney W. Grimes 						base += 1;
3214b88c807SRodney W. Grimes 				} else
3224b88c807SRodney W. Grimes 					base = curr->fts_pathlen;
323426e9c1dSWarner Losh 			}
3244b88c807SRodney W. Grimes 
3254b88c807SRodney W. Grimes 			p = &curr->fts_path[base];
3264b88c807SRodney W. Grimes 			nlen = curr->fts_pathlen - base;
3277658fd20SBruce Evans 			target_mid = to.target_end;
3287658fd20SBruce Evans 			if (*p != '/' && target_mid[-1] != '/')
3297658fd20SBruce Evans 				*target_mid++ = '/';
3307658fd20SBruce Evans 			*target_mid = 0;
33134f9c106SWarner Losh 			if (target_mid - to.p_path + nlen >= PATH_MAX) {
3327658fd20SBruce Evans 				warnx("%s%s: name too long (not copied)",
3337658fd20SBruce Evans 				    to.p_path, p);
3340efa2040SMichael Haro 				badcp = rval = 1;
3357658fd20SBruce Evans 				continue;
3367658fd20SBruce Evans 			}
3377658fd20SBruce Evans 			(void)strncat(target_mid, p, nlen);
3387658fd20SBruce Evans 			to.p_end = target_mid + nlen;
3394b88c807SRodney W. Grimes 			*to.p_end = 0;
3404b88c807SRodney W. Grimes 			STRIP_TRAILING_SLASH(to);
3414b88c807SRodney W. Grimes 		}
3424b88c807SRodney W. Grimes 
3434a5db7acSStephen McKay 		if (curr->fts_info == FTS_DP) {
3444a5db7acSStephen McKay 			/*
34516ef4ac3SStephen McKay 			 * We are nearly finished with this directory.  If we
34616ef4ac3SStephen McKay 			 * didn't actually copy it, or otherwise don't need to
34716ef4ac3SStephen McKay 			 * change its attributes, then we are done.
34862de071bSStephen McKay 			 */
34962de071bSStephen McKay 			if (!curr->fts_number)
35062de071bSStephen McKay 				continue;
35162de071bSStephen McKay 			/*
35262de071bSStephen McKay 			 * If -p is in effect, set all the attributes.
35362de071bSStephen McKay 			 * Otherwise, set the correct permissions, limited
35416ef4ac3SStephen McKay 			 * by the umask.  Optimise by avoiding a chmod()
35516ef4ac3SStephen McKay 			 * if possible (which is usually the case if we
35616ef4ac3SStephen McKay 			 * made the directory).  Note that mkdir() does not
35716ef4ac3SStephen McKay 			 * honour setuid, setgid and sticky bits, but we
35816ef4ac3SStephen McKay 			 * normally want to preserve them on directories.
3594a5db7acSStephen McKay 			 */
3604a5db7acSStephen McKay 			if (pflag)
3614a5db7acSStephen McKay 				rval = setfile(curr->fts_statp, 0);
36262de071bSStephen McKay 			else {
36362de071bSStephen McKay 				mode = curr->fts_statp->st_mode;
36462de071bSStephen McKay 				if ((mode & (S_ISUID | S_ISGID | S_ISTXT)) ||
36562de071bSStephen McKay 				    ((mode | S_IRWXU) & mask) != (mode & mask))
36662de071bSStephen McKay 					if (chmod(to.p_path, mode & mask) != 0){
3674a5db7acSStephen McKay 						warn("chmod: %s", to.p_path);
3684a5db7acSStephen McKay 						rval = 1;
3694a5db7acSStephen McKay 					}
3704a5db7acSStephen McKay 			}
3714a5db7acSStephen McKay 			continue;
3724a5db7acSStephen McKay 		}
3734a5db7acSStephen McKay 
3744b88c807SRodney W. Grimes 		/* Not an error but need to remember it happened */
3754b88c807SRodney W. Grimes 		if (stat(to.p_path, &to_stat) == -1)
3764b88c807SRodney W. Grimes 			dne = 1;
3774b88c807SRodney W. Grimes 		else {
3784b88c807SRodney W. Grimes 			if (to_stat.st_dev == curr->fts_statp->st_dev &&
3794b88c807SRodney W. Grimes 			    to_stat.st_ino == curr->fts_statp->st_ino) {
3804b88c807SRodney W. Grimes 				warnx("%s and %s are identical (not copied).",
3814b88c807SRodney W. Grimes 				    to.p_path, curr->fts_path);
3820efa2040SMichael Haro 				badcp = rval = 1;
3834b88c807SRodney W. Grimes 				if (S_ISDIR(curr->fts_statp->st_mode))
3844b88c807SRodney W. Grimes 					(void)fts_set(ftsp, curr, FTS_SKIP);
3854b88c807SRodney W. Grimes 				continue;
3864b88c807SRodney W. Grimes 			}
387890acb95SSteve Price 			if (!S_ISDIR(curr->fts_statp->st_mode) &&
388890acb95SSteve Price 			    S_ISDIR(to_stat.st_mode)) {
3898bd08b5fSMark Murray 				warnx("cannot overwrite directory %s with "
3908bd08b5fSMark Murray 				    "non-directory %s",
391890acb95SSteve Price 				    to.p_path, curr->fts_path);
3920efa2040SMichael Haro 				badcp = rval = 1;
393890acb95SSteve Price 				continue;
394890acb95SSteve Price 			}
3954b88c807SRodney W. Grimes 			dne = 0;
3964b88c807SRodney W. Grimes 		}
3974b88c807SRodney W. Grimes 
3984b88c807SRodney W. Grimes 		switch (curr->fts_statp->st_mode & S_IFMT) {
3994b88c807SRodney W. Grimes 		case S_IFLNK:
400f9dc2a8bSTim J. Robbins 			/* Catch special case of a non-dangling symlink */
401f9dc2a8bSTim J. Robbins 			if ((fts_options & FTS_LOGICAL) ||
402f9dc2a8bSTim J. Robbins 			    ((fts_options & FTS_COMFOLLOW) &&
403f9dc2a8bSTim J. Robbins 			    curr->fts_level == 0)) {
404f9dc2a8bSTim J. Robbins 				if (copy_file(curr, dne))
405f9dc2a8bSTim J. Robbins 					badcp = rval = 1;
406f9dc2a8bSTim J. Robbins 			} else {
4074b88c807SRodney W. Grimes 				if (copy_link(curr, !dne))
4080efa2040SMichael Haro 					badcp = rval = 1;
409f9dc2a8bSTim J. Robbins 			}
4104b88c807SRodney W. Grimes 			break;
4114b88c807SRodney W. Grimes 		case S_IFDIR:
4124b88c807SRodney W. Grimes 			if (!Rflag && !rflag) {
4134b88c807SRodney W. Grimes 				warnx("%s is a directory (not copied).",
4144b88c807SRodney W. Grimes 				    curr->fts_path);
4154b88c807SRodney W. Grimes 				(void)fts_set(ftsp, curr, FTS_SKIP);
4160efa2040SMichael Haro 				badcp = rval = 1;
4174b88c807SRodney W. Grimes 				break;
4184b88c807SRodney W. Grimes 			}
4194b88c807SRodney W. Grimes 			/*
4204b88c807SRodney W. Grimes 			 * If the directory doesn't exist, create the new
4214b88c807SRodney W. Grimes 			 * one with the from file mode plus owner RWX bits,
4224b88c807SRodney W. Grimes 			 * modified by the umask.  Trade-off between being
4234b88c807SRodney W. Grimes 			 * able to write the directory (if from directory is
4244b88c807SRodney W. Grimes 			 * 555) and not causing a permissions race.  If the
4254b88c807SRodney W. Grimes 			 * umask blocks owner writes, we fail..
4264b88c807SRodney W. Grimes 			 */
4274b88c807SRodney W. Grimes 			if (dne) {
4284b88c807SRodney W. Grimes 				if (mkdir(to.p_path,
4294b88c807SRodney W. Grimes 				    curr->fts_statp->st_mode | S_IRWXU) < 0)
4304b88c807SRodney W. Grimes 					err(1, "%s", to.p_path);
4314b88c807SRodney W. Grimes 			} else if (!S_ISDIR(to_stat.st_mode)) {
4324b88c807SRodney W. Grimes 				errno = ENOTDIR;
433a829865dSDavid Greenman 				err(1, "%s", to.p_path);
4344b88c807SRodney W. Grimes 			}
4354b88c807SRodney W. Grimes 			/*
43662de071bSStephen McKay 			 * Arrange to correct directory attributes later
4374a5db7acSStephen McKay 			 * (in the post-order phase) if this is a new
43862de071bSStephen McKay 			 * directory, or if the -p flag is in effect.
4394b88c807SRodney W. Grimes 			 */
44062de071bSStephen McKay 			curr->fts_number = pflag || dne;
4414b88c807SRodney W. Grimes 			break;
4424b88c807SRodney W. Grimes 		case S_IFBLK:
4434b88c807SRodney W. Grimes 		case S_IFCHR:
4444b88c807SRodney W. Grimes 			if (Rflag) {
4454b88c807SRodney W. Grimes 				if (copy_special(curr->fts_statp, !dne))
4460efa2040SMichael Haro 					badcp = rval = 1;
447439b2b1eSBruce Evans 			} else {
4484b88c807SRodney W. Grimes 				if (copy_file(curr, dne))
4490efa2040SMichael Haro 					badcp = rval = 1;
450439b2b1eSBruce Evans 			}
4514b88c807SRodney W. Grimes 			break;
4524b88c807SRodney W. Grimes 		case S_IFIFO:
453439b2b1eSBruce Evans 			if (Rflag) {
4544b88c807SRodney W. Grimes 				if (copy_fifo(curr->fts_statp, !dne))
4550efa2040SMichael Haro 					badcp = rval = 1;
456439b2b1eSBruce Evans 			} else {
4574b88c807SRodney W. Grimes 				if (copy_file(curr, dne))
4580efa2040SMichael Haro 					badcp = rval = 1;
459439b2b1eSBruce Evans 			}
4604b88c807SRodney W. Grimes 			break;
4614b88c807SRodney W. Grimes 		default:
4624b88c807SRodney W. Grimes 			if (copy_file(curr, dne))
4630efa2040SMichael Haro 				badcp = rval = 1;
4644b88c807SRodney W. Grimes 			break;
4654b88c807SRodney W. Grimes 		}
4660efa2040SMichael Haro 		if (vflag && !badcp)
467fcb2f1b3SMichael Haro 			(void)printf("%s -> %s\n", curr->fts_path, to.p_path);
4684b88c807SRodney W. Grimes 	}
4694b88c807SRodney W. Grimes 	if (errno)
4704b88c807SRodney W. Grimes 		err(1, "fts_read");
4714b88c807SRodney W. Grimes 	return (rval);
4724b88c807SRodney W. Grimes }
4734b88c807SRodney W. Grimes 
4744b88c807SRodney W. Grimes /*
4754b88c807SRodney W. Grimes  * mastercmp --
4764b88c807SRodney W. Grimes  *	The comparison function for the copy order.  The order is to copy
4774b88c807SRodney W. Grimes  *	non-directory files before directory files.  The reason for this
4784b88c807SRodney W. Grimes  *	is because files tend to be in the same cylinder group as their
4794b88c807SRodney W. Grimes  *	parent directory, whereas directories tend not to be.  Copying the
4804b88c807SRodney W. Grimes  *	files first reduces seeking.
4814b88c807SRodney W. Grimes  */
4824b88c807SRodney W. Grimes int
4835dce647cSWarner Losh mastercmp(const FTSENT **a, const FTSENT **b)
4844b88c807SRodney W. Grimes {
4854b88c807SRodney W. Grimes 	int a_info, b_info;
4864b88c807SRodney W. Grimes 
4874b88c807SRodney W. Grimes 	a_info = (*a)->fts_info;
4884b88c807SRodney W. Grimes 	if (a_info == FTS_ERR || a_info == FTS_NS || a_info == FTS_DNR)
4894b88c807SRodney W. Grimes 		return (0);
4904b88c807SRodney W. Grimes 	b_info = (*b)->fts_info;
4914b88c807SRodney W. Grimes 	if (b_info == FTS_ERR || b_info == FTS_NS || b_info == FTS_DNR)
4924b88c807SRodney W. Grimes 		return (0);
4934b88c807SRodney W. Grimes 	if (a_info == FTS_D)
4944b88c807SRodney W. Grimes 		return (-1);
4954b88c807SRodney W. Grimes 	if (b_info == FTS_D)
4964b88c807SRodney W. Grimes 		return (1);
4974b88c807SRodney W. Grimes 	return (0);
4984b88c807SRodney W. Grimes }
499