Home | History | Annotate | Line # | Download | only in cp
cp.c revision 1.44.2.1
      1  1.44.2.1      ghen /* $NetBSD: cp.c,v 1.44.2.1 2006/08/21 10:59:53 ghen Exp $ */
      2      1.12       cgd 
      3       1.1       cgd /*
      4      1.10   mycroft  * Copyright (c) 1988, 1993, 1994
      5      1.10   mycroft  *	The Regents of the University of California.  All rights reserved.
      6       1.1       cgd  *
      7       1.1       cgd  * This code is derived from software contributed to Berkeley by
      8       1.1       cgd  * David Hitz of Auspex Systems Inc.
      9       1.1       cgd  *
     10       1.1       cgd  * Redistribution and use in source and binary forms, with or without
     11       1.1       cgd  * modification, are permitted provided that the following conditions
     12       1.1       cgd  * are met:
     13       1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     14       1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     15       1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     16       1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     17       1.1       cgd  *    documentation and/or other materials provided with the distribution.
     18      1.34       agc  * 3. Neither the name of the University nor the names of its contributors
     19       1.1       cgd  *    may be used to endorse or promote products derived from this software
     20       1.1       cgd  *    without specific prior written permission.
     21       1.1       cgd  *
     22       1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23       1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24       1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25       1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26       1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27       1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28       1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29       1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30       1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31       1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32       1.1       cgd  * SUCH DAMAGE.
     33       1.1       cgd  */
     34       1.1       cgd 
     35      1.18   thorpej #include <sys/cdefs.h>
     36       1.1       cgd #ifndef lint
     37      1.18   thorpej __COPYRIGHT(
     38      1.10   mycroft "@(#) Copyright (c) 1988, 1993, 1994\n\
     39      1.18   thorpej 	The Regents of the University of California.  All rights reserved.\n");
     40       1.1       cgd #endif /* not lint */
     41       1.1       cgd 
     42       1.1       cgd #ifndef lint
     43      1.12       cgd #if 0
     44      1.14       jtc static char sccsid[] = "@(#)cp.c	8.5 (Berkeley) 4/29/95";
     45      1.12       cgd #else
     46  1.44.2.1      ghen __RCSID("$NetBSD: cp.c,v 1.44.2.1 2006/08/21 10:59:53 ghen Exp $");
     47      1.12       cgd #endif
     48       1.1       cgd #endif /* not lint */
     49       1.1       cgd 
     50       1.1       cgd /*
     51      1.10   mycroft  * Cp copies source files to target files.
     52       1.1       cgd  *
     53      1.10   mycroft  * The global PATH_T structure "to" always contains the path to the
     54      1.10   mycroft  * current target file.  Since fts(3) does not change directories,
     55      1.10   mycroft  * this path can be either absolute or dot-relative.
     56       1.1       cgd  *
     57      1.10   mycroft  * The basic algorithm is to initialize "to" and use fts(3) to traverse
     58      1.10   mycroft  * the file hierarchy rooted in the argument list.  A trivial case is the
     59      1.10   mycroft  * case of 'cp file1 file2'.  The more interesting case is the case of
     60      1.10   mycroft  * 'cp file1 file2 ... fileN dir' where the hierarchy is traversed and the
     61      1.10   mycroft  * path (relative to the root of the traversal) is appended to dir (stored
     62      1.10   mycroft  * in "to") to form the final target path.
     63       1.1       cgd  */
     64       1.1       cgd 
     65       1.1       cgd #include <sys/param.h>
     66       1.1       cgd #include <sys/stat.h>
     67      1.10   mycroft 
     68      1.10   mycroft #include <err.h>
     69      1.10   mycroft #include <errno.h>
     70      1.10   mycroft #include <fts.h>
     71      1.28    kleink #include <locale.h>
     72      1.29      matt #include <stdlib.h>
     73       1.1       cgd #include <stdio.h>
     74       1.1       cgd #include <string.h>
     75      1.10   mycroft #include <unistd.h>
     76      1.10   mycroft 
     77       1.5   mycroft #include "extern.h"
     78       1.1       cgd 
     79      1.10   mycroft #define	STRIP_TRAILING_SLASH(p) {					\
     80      1.15    kleink         while ((p).p_end > (p).p_path + 1 && (p).p_end[-1] == '/')	\
     81      1.27       mjl                 *--(p).p_end = '\0';					\
     82      1.10   mycroft }
     83       1.5   mycroft 
     84      1.36  christos static char empty[] = "";
     85      1.36  christos PATH_T to = { to.p_path, empty };
     86       1.1       cgd 
     87       1.1       cgd uid_t myuid;
     88      1.44  jschauma int Hflag, Lflag, Rflag, Pflag, fflag, iflag, pflag, rflag, vflag, Nflag;
     89      1.20   mycroft mode_t myumask;
     90      1.10   mycroft 
     91      1.10   mycroft enum op { FILE_TO_FILE, FILE_TO_DIR, DIR_TO_DNE };
     92       1.1       cgd 
     93      1.33  jschauma int 	main(int, char *[]);
     94      1.33  jschauma int 	copy(char *[], enum op, int);
     95      1.33  jschauma int 	mastercmp(const FTSENT **, const FTSENT **);
     96      1.10   mycroft 
     97      1.10   mycroft int
     98      1.31       wiz main(int argc, char *argv[])
     99       1.1       cgd {
    100      1.10   mycroft 	struct stat to_stat, tmp_stat;
    101      1.10   mycroft 	enum op type;
    102      1.44  jschauma 	int ch, fts_options, r, have_trailing_slash;
    103      1.40  christos 	char *target, **src;
    104      1.28    kleink 
    105      1.28    kleink 	(void)setlocale(LC_ALL, "");
    106      1.10   mycroft 
    107      1.10   mycroft 	Hflag = Lflag = Pflag = Rflag = 0;
    108      1.37      elad 	while ((ch = getopt(argc, argv, "HLNPRfiprv")) != -1)
    109      1.10   mycroft 		switch (ch) {
    110      1.10   mycroft 		case 'H':
    111      1.10   mycroft 			Hflag = 1;
    112      1.10   mycroft 			Lflag = Pflag = 0;
    113      1.10   mycroft 			break;
    114      1.10   mycroft 		case 'L':
    115      1.10   mycroft 			Lflag = 1;
    116      1.10   mycroft 			Hflag = Pflag = 0;
    117      1.10   mycroft 			break;
    118      1.37      elad 		case 'N':
    119      1.37      elad 			Nflag = 1;
    120      1.37      elad 			break;
    121      1.10   mycroft 		case 'P':
    122      1.10   mycroft 			Pflag = 1;
    123      1.10   mycroft 			Hflag = Lflag = 0;
    124      1.10   mycroft 			break;
    125      1.10   mycroft 		case 'R':
    126      1.10   mycroft 			Rflag = 1;
    127      1.10   mycroft 			break;
    128       1.1       cgd 		case 'f':
    129      1.25  wsanchez 			fflag = 1;
    130       1.1       cgd 			iflag = 0;
    131       1.1       cgd 			break;
    132       1.1       cgd 		case 'i':
    133       1.1       cgd 			iflag = isatty(fileno(stdin));
    134      1.25  wsanchez 			fflag = 0;
    135       1.1       cgd 			break;
    136       1.1       cgd 		case 'p':
    137       1.1       cgd 			pflag = 1;
    138       1.1       cgd 			break;
    139      1.10   mycroft 		case 'r':
    140       1.1       cgd 			rflag = 1;
    141       1.1       cgd 			break;
    142      1.32       jrf 		case 'v':
    143      1.32       jrf 			vflag = 1;
    144      1.32       jrf 			break;
    145       1.1       cgd 		case '?':
    146       1.1       cgd 		default:
    147       1.1       cgd 			usage();
    148      1.38  christos 			/* NOTREACHED */
    149       1.1       cgd 		}
    150       1.1       cgd 	argc -= optind;
    151       1.1       cgd 	argv += optind;
    152       1.1       cgd 
    153       1.1       cgd 	if (argc < 2)
    154       1.1       cgd 		usage();
    155       1.1       cgd 
    156      1.10   mycroft 	fts_options = FTS_NOCHDIR | FTS_PHYSICAL;
    157      1.10   mycroft 	if (rflag) {
    158      1.33  jschauma 		if (Rflag) {
    159      1.33  jschauma 			errx(EXIT_FAILURE,
    160      1.10   mycroft 		    "the -R and -r options may not be specified together.");
    161      1.33  jschauma 			/* NOTREACHED */
    162      1.33  jschauma 		}
    163      1.33  jschauma 		if (Hflag || Lflag || Pflag) {
    164      1.33  jschauma 			errx(EXIT_FAILURE,
    165      1.10   mycroft 	"the -H, -L, and -P options may not be specified with the -r option.");
    166      1.33  jschauma 			/* NOTREACHED */
    167      1.33  jschauma 		}
    168      1.10   mycroft 		fts_options &= ~FTS_PHYSICAL;
    169      1.10   mycroft 		fts_options |= FTS_LOGICAL;
    170      1.10   mycroft 	}
    171      1.44  jschauma 
    172      1.10   mycroft 	if (Rflag) {
    173      1.10   mycroft 		if (Hflag)
    174      1.10   mycroft 			fts_options |= FTS_COMFOLLOW;
    175      1.10   mycroft 		if (Lflag) {
    176      1.10   mycroft 			fts_options &= ~FTS_PHYSICAL;
    177      1.10   mycroft 			fts_options |= FTS_LOGICAL;
    178      1.10   mycroft 		}
    179      1.44  jschauma 	} else if (!Pflag) {
    180      1.10   mycroft 		fts_options &= ~FTS_PHYSICAL;
    181      1.27       mjl 		fts_options |= FTS_LOGICAL | FTS_COMFOLLOW;
    182       1.1       cgd 	}
    183       1.1       cgd 
    184       1.1       cgd 	myuid = getuid();
    185       1.1       cgd 
    186      1.10   mycroft 	/* Copy the umask for explicit mode setting. */
    187       1.1       cgd 	myumask = umask(0);
    188       1.1       cgd 	(void)umask(myumask);
    189       1.1       cgd 
    190      1.10   mycroft 	/* Save the target base in "to". */
    191      1.10   mycroft 	target = argv[--argc];
    192      1.40  christos 	if (strlcpy(to.p_path, target, sizeof(to.p_path)) >= sizeof(to.p_path))
    193      1.35  jschauma 		errx(EXIT_FAILURE, "%s: name too long", target);
    194      1.10   mycroft 	to.p_end = to.p_path + strlen(to.p_path);
    195      1.40  christos 	have_trailing_slash = (to.p_end[-1] == '/');
    196      1.40  christos 	if (have_trailing_slash)
    197      1.40  christos 		STRIP_TRAILING_SLASH(to);
    198      1.10   mycroft 	to.target_end = to.p_end;
    199      1.10   mycroft 
    200      1.10   mycroft 	/* Set end of argument list for fts(3). */
    201      1.10   mycroft 	argv[argc] = NULL;
    202      1.10   mycroft 
    203       1.1       cgd 	/*
    204       1.1       cgd 	 * Cp has two distinct cases:
    205       1.1       cgd 	 *
    206      1.10   mycroft 	 * cp [-R] source target
    207      1.10   mycroft 	 * cp [-R] source1 ... sourceN directory
    208       1.1       cgd 	 *
    209       1.1       cgd 	 * In both cases, source can be either a file or a directory.
    210       1.1       cgd 	 *
    211       1.1       cgd 	 * In (1), the target becomes a copy of the source. That is, if the
    212       1.1       cgd 	 * source is a file, the target will be a file, and likewise for
    213       1.1       cgd 	 * directories.
    214       1.1       cgd 	 *
    215       1.1       cgd 	 * In (2), the real target is not directory, but "directory/source".
    216       1.1       cgd 	 */
    217      1.44  jschauma 	if (Pflag)
    218      1.44  jschauma 		r = lstat(to.p_path, &to_stat);
    219      1.44  jschauma 	else
    220      1.44  jschauma 		r = stat(to.p_path, &to_stat);
    221      1.33  jschauma 	if (r == -1 && errno != ENOENT) {
    222      1.35  jschauma 		err(EXIT_FAILURE, "%s", to.p_path);
    223      1.33  jschauma 		/* NOTREACHED */
    224      1.33  jschauma 	}
    225       1.1       cgd 	if (r == -1 || !S_ISDIR(to_stat.st_mode)) {
    226       1.1       cgd 		/*
    227       1.1       cgd 		 * Case (1).  Target is not a directory.
    228      1.10   mycroft 		 */
    229      1.38  christos 		if (argc > 1)
    230       1.1       cgd 			usage();
    231      1.10   mycroft 		/*
    232      1.10   mycroft 		 * Need to detect the case:
    233      1.10   mycroft 		 *	cp -R dir foo
    234      1.10   mycroft 		 * Where dir is a directory and foo does not exist, where
    235      1.10   mycroft 		 * we want pathname concatenations turned on but not for
    236      1.10   mycroft 		 * the initial mkdir().
    237      1.10   mycroft 		 */
    238      1.10   mycroft 		if (r == -1) {
    239      1.10   mycroft 			if (rflag || (Rflag && (Lflag || Hflag)))
    240      1.21   mycroft 				r = stat(*argv, &tmp_stat);
    241      1.10   mycroft 			else
    242      1.21   mycroft 				r = lstat(*argv, &tmp_stat);
    243      1.33  jschauma 			if (r == -1) {
    244      1.35  jschauma 				err(EXIT_FAILURE, "%s", *argv);
    245      1.33  jschauma 				/* NOTREACHED */
    246      1.33  jschauma 			}
    247      1.10   mycroft 
    248      1.10   mycroft 			if (S_ISDIR(tmp_stat.st_mode) && (Rflag || rflag))
    249      1.10   mycroft 				type = DIR_TO_DNE;
    250      1.10   mycroft 			else
    251      1.10   mycroft 				type = FILE_TO_FILE;
    252      1.10   mycroft 		} else
    253      1.10   mycroft 			type = FILE_TO_FILE;
    254      1.40  christos 
    255      1.40  christos 		if (have_trailing_slash && type == FILE_TO_FILE) {
    256      1.40  christos 			if (r == -1)
    257      1.40  christos 				errx(1, "directory %s does not exist",
    258      1.40  christos 				     to.p_path);
    259      1.40  christos 			else
    260      1.40  christos 				errx(1, "%s is not a directory", to.p_path);
    261      1.40  christos 		}
    262      1.25  wsanchez 	} else {
    263       1.1       cgd 		/*
    264       1.1       cgd 		 * Case (2).  Target is a directory.
    265       1.1       cgd 		 */
    266      1.10   mycroft 		type = FILE_TO_DIR;
    267      1.25  wsanchez 	}
    268      1.10   mycroft 
    269      1.40  christos 	/*
    270      1.40  christos 	 * make "cp -rp src/ dst" behave like "cp -rp src dst" not
    271      1.40  christos 	 * like "cp -rp src/. dst"
    272      1.40  christos 	 */
    273      1.40  christos 	for (src = argv; *src; src++) {
    274      1.40  christos 		size_t len = strlen(*src);
    275      1.40  christos 		while (len-- > 1 && (*src)[len] == '/')
    276      1.40  christos 			(*src)[len] = '\0';
    277      1.40  christos 	}
    278      1.40  christos 
    279      1.31       wiz 	exit(copy(argv, type, fts_options));
    280      1.20   mycroft 	/* NOTREACHED */
    281       1.1       cgd }
    282       1.1       cgd 
    283      1.10   mycroft int
    284      1.31       wiz copy(char *argv[], enum op type, int fts_options)
    285       1.1       cgd {
    286      1.10   mycroft 	struct stat to_stat;
    287      1.10   mycroft 	FTS *ftsp;
    288      1.10   mycroft 	FTSENT *curr;
    289      1.44  jschauma 	int base, dne, rval, sval;
    290      1.38  christos 	size_t nlen;
    291      1.40  christos 	char *p, *target_mid;
    292       1.1       cgd 
    293      1.44  jschauma 	dne = 0;
    294      1.18   thorpej 	base = 0;	/* XXX gcc -Wuninitialized (see comment below) */
    295      1.18   thorpej 
    296      1.10   mycroft 	if ((ftsp = fts_open(argv, fts_options, mastercmp)) == NULL)
    297      1.35  jschauma 		err(EXIT_FAILURE, "%s", argv[0]);
    298      1.33  jschauma 		/* NOTREACHED */
    299      1.10   mycroft 	for (rval = 0; (curr = fts_read(ftsp)) != NULL;) {
    300      1.10   mycroft 		switch (curr->fts_info) {
    301      1.10   mycroft 		case FTS_NS:
    302      1.27       mjl 		case FTS_DNR:
    303      1.10   mycroft 		case FTS_ERR:
    304      1.35  jschauma 			warnx("%s: %s", curr->fts_path,
    305      1.35  jschauma 					strerror(curr->fts_errno));
    306      1.10   mycroft 			rval = 1;
    307      1.10   mycroft 			continue;
    308      1.10   mycroft 		case FTS_DC:			/* Warn, continue. */
    309      1.35  jschauma 			warnx("%s: directory causes a cycle", curr->fts_path);
    310      1.10   mycroft 			rval = 1;
    311      1.10   mycroft 			continue;
    312      1.10   mycroft 		}
    313      1.10   mycroft 
    314      1.10   mycroft 		/*
    315      1.10   mycroft 		 * If we are in case (2) or (3) above, we need to append the
    316      1.10   mycroft                  * source name to the target name.
    317      1.10   mycroft                  */
    318      1.10   mycroft 		if (type != FILE_TO_FILE) {
    319      1.10   mycroft 			if ((curr->fts_namelen +
    320      1.10   mycroft 			    to.target_end - to.p_path + 1) > MAXPATHLEN) {
    321      1.35  jschauma 				warnx("%s/%s: name too long (not copied)",
    322      1.35  jschauma 						to.p_path, curr->fts_name);
    323      1.10   mycroft 				rval = 1;
    324      1.10   mycroft 				continue;
    325      1.10   mycroft 			}
    326       1.1       cgd 
    327      1.10   mycroft 			/*
    328      1.10   mycroft 			 * Need to remember the roots of traversals to create
    329      1.10   mycroft 			 * correct pathnames.  If there's a directory being
    330      1.10   mycroft 			 * copied to a non-existent directory, e.g.
    331      1.10   mycroft 			 *	cp -R a/dir noexist
    332      1.10   mycroft 			 * the resulting path name should be noexist/foo, not
    333      1.10   mycroft 			 * noexist/dir/foo (where foo is a file in dir), which
    334      1.10   mycroft 			 * is the case where the target exists.
    335      1.10   mycroft 			 *
    336      1.10   mycroft 			 * Also, check for "..".  This is for correct path
    337      1.10   mycroft 			 * concatentation for paths ending in "..", e.g.
    338      1.10   mycroft 			 *	cp -R .. /tmp
    339      1.10   mycroft 			 * Paths ending in ".." are changed to ".".  This is
    340      1.10   mycroft 			 * tricky, but seems the easiest way to fix the problem.
    341      1.10   mycroft 			 *
    342      1.10   mycroft 			 * XXX
    343      1.10   mycroft 			 * Since the first level MUST be FTS_ROOTLEVEL, base
    344      1.10   mycroft 			 * is always initialized.
    345      1.10   mycroft 			 */
    346      1.24   thorpej 			if (curr->fts_level == FTS_ROOTLEVEL) {
    347      1.10   mycroft 				if (type != DIR_TO_DNE) {
    348      1.10   mycroft 					p = strrchr(curr->fts_path, '/');
    349      1.10   mycroft 					base = (p == NULL) ? 0 :
    350      1.10   mycroft 					    (int)(p - curr->fts_path + 1);
    351      1.10   mycroft 
    352      1.10   mycroft 					if (!strcmp(&curr->fts_path[base],
    353      1.10   mycroft 					    ".."))
    354      1.10   mycroft 						base += 1;
    355      1.10   mycroft 				} else
    356      1.10   mycroft 					base = curr->fts_pathlen;
    357      1.24   thorpej 			}
    358      1.10   mycroft 
    359      1.10   mycroft 			p = &curr->fts_path[base];
    360      1.10   mycroft 			nlen = curr->fts_pathlen - base;
    361      1.40  christos 			target_mid = to.target_end;
    362      1.40  christos 			if (*p != '/' && target_mid[-1] != '/')
    363      1.40  christos 				*target_mid++ = '/';
    364      1.40  christos 			*target_mid = 0;
    365      1.40  christos 
    366      1.40  christos 			if (target_mid - to.p_path + nlen >= PATH_MAX) {
    367      1.40  christos 				warnx("%s%s: name too long (not copied)",
    368      1.40  christos 				    to.p_path, p);
    369      1.40  christos 				rval = 1;
    370      1.40  christos 				continue;
    371      1.40  christos 			}
    372      1.40  christos 			(void)strncat(target_mid, p, nlen);
    373      1.40  christos 			to.p_end = target_mid + nlen;
    374      1.10   mycroft 			*to.p_end = 0;
    375      1.10   mycroft 			STRIP_TRAILING_SLASH(to);
    376      1.10   mycroft 		}
    377      1.10   mycroft 
    378      1.44  jschauma 		sval = Pflag ? lstat(to.p_path, &to_stat) : stat(to.p_path, &to_stat);
    379      1.10   mycroft 		/* Not an error but need to remember it happened */
    380      1.44  jschauma 		if (sval == -1)
    381      1.10   mycroft 			dne = 1;
    382      1.10   mycroft 		else {
    383      1.10   mycroft 			if (to_stat.st_dev == curr->fts_statp->st_dev &&
    384      1.10   mycroft 			    to_stat.st_ino == curr->fts_statp->st_ino) {
    385      1.10   mycroft 				warnx("%s and %s are identical (not copied).",
    386      1.10   mycroft 				    to.p_path, curr->fts_path);
    387      1.10   mycroft 				rval = 1;
    388      1.10   mycroft 				if (S_ISDIR(curr->fts_statp->st_mode))
    389      1.10   mycroft 					(void)fts_set(ftsp, curr, FTS_SKIP);
    390      1.10   mycroft 				continue;
    391      1.10   mycroft 			}
    392      1.10   mycroft 			if (!S_ISDIR(curr->fts_statp->st_mode) &&
    393      1.10   mycroft 			    S_ISDIR(to_stat.st_mode)) {
    394      1.14       jtc 		warnx("cannot overwrite directory %s with non-directory %s",
    395      1.35  jschauma 				    to.p_path, curr->fts_path);
    396      1.10   mycroft 				rval = 1;
    397      1.10   mycroft 				continue;
    398      1.10   mycroft 			}
    399      1.44  jschauma 			if (!S_ISDIR(curr->fts_statp->st_mode))
    400      1.44  jschauma 				dne = 0;
    401       1.1       cgd 		}
    402       1.1       cgd 
    403      1.10   mycroft 		switch (curr->fts_statp->st_mode & S_IFMT) {
    404      1.10   mycroft 		case S_IFLNK:
    405      1.27       mjl 			/* Catch special case of a non dangling symlink */
    406      1.27       mjl 			if((fts_options & FTS_LOGICAL) ||
    407      1.27       mjl 			   ((fts_options & FTS_COMFOLLOW) && curr->fts_level == 0)) {
    408      1.27       mjl 				if (copy_file(curr, dne))
    409      1.27       mjl 					rval = 1;
    410      1.27       mjl 			} else {
    411      1.27       mjl 				if (copy_link(curr, !dne))
    412      1.27       mjl 					rval = 1;
    413      1.27       mjl 			}
    414      1.10   mycroft 			break;
    415      1.10   mycroft 		case S_IFDIR:
    416      1.10   mycroft 			if (!Rflag && !rflag) {
    417      1.35  jschauma 				if (curr->fts_info == FTS_D)
    418      1.26      dean 					warnx("%s is a directory (not copied).",
    419      1.35  jschauma 					    curr->fts_path);
    420      1.10   mycroft 				(void)fts_set(ftsp, curr, FTS_SKIP);
    421      1.10   mycroft 				rval = 1;
    422      1.10   mycroft 				break;
    423      1.10   mycroft 			}
    424      1.25  wsanchez 
    425      1.25  wsanchez                         /*
    426      1.25  wsanchez                          * Directories get noticed twice:
    427      1.25  wsanchez                          *  In the first pass, create it if needed.
    428      1.25  wsanchez                          *  In the second pass, after the children have been copied, set the permissions.
    429      1.25  wsanchez                          */
    430      1.25  wsanchez 			if (curr->fts_info == FTS_D) /* First pass */
    431      1.25  wsanchez 			{
    432      1.25  wsanchez 				/*
    433      1.25  wsanchez 				 * If the directory doesn't exist, create the new
    434      1.25  wsanchez 				 * one with the from file mode plus owner RWX bits,
    435      1.25  wsanchez 				 * modified by the umask.  Trade-off between being
    436      1.25  wsanchez 				 * able to write the directory (if from directory is
    437      1.25  wsanchez 				 * 555) and not causing a permissions race.  If the
    438      1.25  wsanchez 				 * umask blocks owner writes, we fail..
    439      1.25  wsanchez 				 */
    440      1.25  wsanchez 				if (dne) {
    441      1.25  wsanchez 					if (mkdir(to.p_path,
    442      1.25  wsanchez 					    curr->fts_statp->st_mode | S_IRWXU) < 0)
    443      1.33  jschauma 						err(EXIT_FAILURE, "%s",
    444      1.35  jschauma 						    to.p_path);
    445      1.33  jschauma 						/* NOTREACHED */
    446      1.25  wsanchez 				} else if (!S_ISDIR(to_stat.st_mode)) {
    447      1.25  wsanchez 					errno = ENOTDIR;
    448      1.33  jschauma 					err(EXIT_FAILURE, "%s",
    449      1.35  jschauma 						to.p_path);
    450      1.33  jschauma 					/* NOTREACHED */
    451      1.25  wsanchez 				}
    452       1.1       cgd 			}
    453      1.25  wsanchez 			else if (curr->fts_info == FTS_DP) /* Second pass */
    454      1.25  wsanchez 			{
    455      1.25  wsanchez 	                        /*
    456      1.25  wsanchez 				 * If not -p and directory didn't exist, set it to be
    457      1.25  wsanchez 				 * the same as the from directory, umodified by the
    458      1.25  wsanchez                         	 * umask; arguably wrong, but it's been that way
    459      1.25  wsanchez                         	 * forever.
    460      1.25  wsanchez 				 */
    461      1.25  wsanchez 				if (pflag && setfile(curr->fts_statp, 0))
    462      1.25  wsanchez 					rval = 1;
    463      1.25  wsanchez 				else if (dne)
    464      1.25  wsanchez 					(void)chmod(to.p_path,
    465      1.25  wsanchez 					    curr->fts_statp->st_mode);
    466  1.44.2.1      ghen 
    467  1.44.2.1      ghen 				/*
    468  1.44.2.1      ghen 				 * Since this is the second pass, we already
    469  1.44.2.1      ghen 				 * noted (and acted on) the existence of the
    470  1.44.2.1      ghen 				 * directory.
    471  1.44.2.1      ghen 				 */
    472  1.44.2.1      ghen 				dne = 0;
    473      1.25  wsanchez 			}
    474      1.25  wsanchez 			else
    475      1.25  wsanchez                         {
    476      1.35  jschauma                         	warnx("directory %s encountered when not expected.",
    477      1.35  jschauma 						curr->fts_path);
    478      1.25  wsanchez                         	rval = 1;
    479      1.25  wsanchez                                 break;
    480      1.25  wsanchez                         }
    481      1.25  wsanchez 
    482      1.10   mycroft 			break;
    483      1.10   mycroft 		case S_IFBLK:
    484      1.10   mycroft 		case S_IFCHR:
    485      1.10   mycroft 			if (Rflag) {
    486      1.10   mycroft 				if (copy_special(curr->fts_statp, !dne))
    487      1.10   mycroft 					rval = 1;
    488      1.10   mycroft 			} else
    489      1.10   mycroft 				if (copy_file(curr, dne))
    490      1.10   mycroft 					rval = 1;
    491      1.10   mycroft 			break;
    492      1.10   mycroft 		case S_IFIFO:
    493      1.13   mycroft 			if (Rflag) {
    494      1.10   mycroft 				if (copy_fifo(curr->fts_statp, !dne))
    495      1.10   mycroft 					rval = 1;
    496      1.13   mycroft 			} else
    497      1.10   mycroft 				if (copy_file(curr, dne))
    498      1.10   mycroft 					rval = 1;
    499      1.10   mycroft 			break;
    500      1.10   mycroft 		default:
    501      1.10   mycroft 			if (copy_file(curr, dne))
    502      1.10   mycroft 				rval = 1;
    503      1.10   mycroft 			break;
    504       1.1       cgd 		}
    505      1.38  christos 		if (vflag && !rval)
    506      1.35  jschauma 			(void)printf("%s -> %s\n", curr->fts_path, to.p_path);
    507      1.33  jschauma 	}
    508      1.33  jschauma 	if (errno) {
    509      1.33  jschauma 		err(EXIT_FAILURE, "fts_read");
    510      1.33  jschauma 		/* NOTREACHED */
    511       1.1       cgd 	}
    512      1.41       erh 	(void)fts_close(ftsp);
    513      1.10   mycroft 	return (rval);
    514       1.1       cgd }
    515       1.1       cgd 
    516      1.10   mycroft /*
    517      1.10   mycroft  * mastercmp --
    518      1.10   mycroft  *	The comparison function for the copy order.  The order is to copy
    519      1.10   mycroft  *	non-directory files before directory files.  The reason for this
    520      1.10   mycroft  *	is because files tend to be in the same cylinder group as their
    521      1.10   mycroft  *	parent directory, whereas directories tend not to be.  Copying the
    522      1.10   mycroft  *	files first reduces seeking.
    523      1.10   mycroft  */
    524      1.10   mycroft int
    525      1.31       wiz mastercmp(const FTSENT **a, const FTSENT **b)
    526       1.1       cgd {
    527      1.10   mycroft 	int a_info, b_info;
    528       1.1       cgd 
    529      1.10   mycroft 	a_info = (*a)->fts_info;
    530      1.10   mycroft 	if (a_info == FTS_ERR || a_info == FTS_NS || a_info == FTS_DNR)
    531      1.10   mycroft 		return (0);
    532      1.10   mycroft 	b_info = (*b)->fts_info;
    533      1.10   mycroft 	if (b_info == FTS_ERR || b_info == FTS_NS || b_info == FTS_DNR)
    534      1.10   mycroft 		return (0);
    535      1.10   mycroft 	if (a_info == FTS_D)
    536      1.10   mycroft 		return (-1);
    537      1.10   mycroft 	if (b_info == FTS_D)
    538      1.10   mycroft 		return (1);
    539      1.10   mycroft 	return (0);
    540       1.1       cgd }
    541