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